[PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property

Cheng Ming Lin posted 3 patches 3 weeks, 1 day ago
[PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Cheng Ming Lin 3 weeks, 1 day ago
From: Cheng Ming Lin <chengminglin@mxic.com.tw>

Add a new boolean property "enable-randomizer-otp" to enable the
randomizer feature on supported SPI-NAND devices.

Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
---
 Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
index 77a8727c7..432bc79e9 100644
--- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
+++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
@@ -21,6 +21,10 @@ properties:
     description: Encode the chip-select line on the SPI bus
     maxItems: 1
 
+  enable-randomizer-otp:
+    description: Enable the randomizer feature
+    type: boolean
+
 required:
   - compatible
   - reg
-- 
2.25.1
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Krzysztof Kozlowski 3 weeks, 1 day ago
On Wed, Sep 10, 2025 at 11:02:59AM +0800, Cheng Ming Lin wrote:
> From: Cheng Ming Lin <chengminglin@mxic.com.tw>
> 
> Add a new boolean property "enable-randomizer-otp" to enable the
> randomizer feature on supported SPI-NAND devices.

What is randomizer feature? Subject, commit msg, description - none of
them explain me that. Is this some standard for NAND? Why is this a
common property?

So many questions and zero explanations in the commit msg. Instead you
just wrote what we see in the diff - that's completely redundant.

Best regards,
Krzysztof
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Cheng Ming Lin 3 weeks ago
Hi Krzysztof,

Krzysztof Kozlowski <krzk@kernel.org> 於 2025年9月10日 週三 下午7:40寫道:
>
> On Wed, Sep 10, 2025 at 11:02:59AM +0800, Cheng Ming Lin wrote:
> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
> >
> > Add a new boolean property "enable-randomizer-otp" to enable the
> > randomizer feature on supported SPI-NAND devices.
>
> What is randomizer feature? Subject, commit msg, description - none of
> them explain me that. Is this some standard for NAND? Why is this a
> common property?

To the best of our knowledge, Macronix was the first vendor to
implement this feature in hardware to enhance data reliability and
endurance, especially under high P/E cycle stress.

Since this is a vendor-specific implementation rather than a common
NAND standard, I agree that this property should be handled within
the Marconix driver specifically, not as a common property. I will
make the necessary adjustments.

>
> So many questions and zero explanations in the commit msg. Instead you
> just wrote what we see in the diff - that's completely redundant.

Thank you for your feedback. I apologize that the commit message was not
clear and did not provide sufficient context for this feature.
Your questions are very valid and I should have included these details from
the beginning.

According to JEDEC standard JESD22-A117E, no single data pattern
represents a universal worst-case for all NAND flash failure mechanisms.
Different patterns, such as fully programmed, checkerboard, or mostly
erased, can disproportionately stress specific cells (e.g., programmed,
erased, or those influenced by adjacent states).

Given that no fixed pattern can cover all scenarios, the use of a
randomized data pattern is a practical and effective mitigation strategy.
Our hardware implements a randomizer feature that scrambles user data
before it is written to the flash and restores the original data upon read.

This ensures the data stored on the media is more evenly distributed,
thus reducing pattern-dependent degradation. This is especially crucial
for preventing errors caused by unbalanced data (e.g., all zeros or
all ones) in blocks with high program/erase (P/E) cycle counts.
Ultimately, the randomizer improves the long-term reliability and
endurance of the flash device.

Please refer to the following link for randomizer feature:
Link: https://www.mxic.com.tw/Lists/ApplicationNote/Attachments/2151/AN1051V1-The%20Introduction%20of%20Randomizer%20Feature%20on%20MX30xFxG28AD_MX35xFxG24AD.pdf

>
> Best regards,
> Krzysztof
>

Best regards,
Cheng Ming Lin
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Miquel Raynal 3 weeks, 1 day ago
Hello Cheng Ming,

On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:

> From: Cheng Ming Lin <chengminglin@mxic.com.tw>
>
> Add a new boolean property "enable-randomizer-otp" to enable the
> randomizer feature on supported SPI-NAND devices.
>
> Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
> ---
>  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> index 77a8727c7..432bc79e9 100644
> --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> @@ -21,6 +21,10 @@ properties:
>      description: Encode the chip-select line on the SPI bus
>      maxItems: 1
>  
> +  enable-randomizer-otp:

This is a NAND wide feature, so we should probably add a prefix, such as
"nand,".

Now, what about this "otp" suffix? Many (if not all) chips have a
volatile setting for that. About the naming, "otp" often reflects to the
OTP area, which is not what you imply here, as you want to insist
(rightfully) on the fact that this feature cannot be disabled.

Also, this is a per-chip configuration, while I would have welcomed a
per-partition configuration. I can easily imagine two cases:

- The boot ROM, for longevity purposes, expects the first blocks
  containing the bootloader to be scrambled. However the rest of the
  system does not really care and disables randomization.

- The boot ROM is not capable of de-scrambling, however the rest of the
  system relies on the (probably) more robust scrambling feature.

In both cases a chip wide variable is not relevant.

The fact that it is a one-time feature makes it even harder to fit into
something generic.

How do you think these constraints could fit with your needs? Can you
please explain again (sorry for the repetition) what Macronix chips have
the volatile capability and which ones do not?

Thanks,
Miquèl
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Cheng Ming Lin 3 weeks ago
Hi Miquel,

Miquel Raynal <miquel.raynal@bootlin.com> 於 2025年9月10日 週三 下午5:10寫道:
>
> Hello Cheng Ming,
>
> On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
>
> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
> >
> > Add a new boolean property "enable-randomizer-otp" to enable the
> > randomizer feature on supported SPI-NAND devices.
> >
> > Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
> > ---
> >  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > index 77a8727c7..432bc79e9 100644
> > --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > @@ -21,6 +21,10 @@ properties:
> >      description: Encode the chip-select line on the SPI bus
> >      maxItems: 1
> >
> > +  enable-randomizer-otp:
>
> This is a NAND wide feature, so we should probably add a prefix, such as
> "nand,".
>
> Now, what about this "otp" suffix? Many (if not all) chips have a
> volatile setting for that. About the naming, "otp" often reflects to the
> OTP area, which is not what you imply here, as you want to insist
> (rightfully) on the fact that this feature cannot be disabled.

Yes, my intention is that once the randomizer feature is enabled, it
should not be disabled again. You are correct that this bit does not
belong to the OTP area, but rather to the v2 volatile register. The v2
volatile register has a default value that can be changed through a
special OTP configuration register program operation. Regarding the
"otp" suffix, I will remove it to avoid misunderstanding.

>
> Also, this is a per-chip configuration, while I would have welcomed a
> per-partition configuration. I can easily imagine two cases:
>
> - The boot ROM, for longevity purposes, expects the first blocks
>   containing the bootloader to be scrambled. However the rest of the
>   system does not really care and disables randomization.
>
> - The boot ROM is not capable of de-scrambling, however the rest of the
>   system relies on the (probably) more robust scrambling feature.
>
> In both cases a chip wide variable is not relevant.

The scrambling and descrambling are handled by the NAND flash hardware
itself. Therefore, the boot ROM does not need to support a descrambling
feature.

In the case of Macronix parts, the randomizer is controlled through a
configuration register, and once it is enabled it covers the entire chip
(main and/or spare depending on the randopt bit). There is no hardware
mechanism to enable it only on certain ranges.

>
> The fact that it is a one-time feature makes it even harder to fit into
> something generic.
>

This isn't a true one-time programming feature. Its default value can be
changed through a specific procedure.

Please refer to page 29 and page 51 of the datasheet for specific
procedure and details.
Link: https://www.mxic.com.tw/Lists/Datasheet/Attachments/9036/MX35LF4G24AD,%203V,%204Gb,%20v1.4.pdf

> How do you think these constraints could fit with your needs? Can you
> please explain again (sorry for the repetition) what Macronix chips have
> the volatile capability and which ones do not?

Macronix chips that support the randomizer feature are V2 type registers.

>
> Thanks,
> Miquèl

Thanks,
Cheng Ming Lin
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Miquel Raynal 3 weeks ago
Hi Cheng Ming,

On 11/09/2025 at 15:24:35 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:

> Hi Miquel,
>
> Miquel Raynal <miquel.raynal@bootlin.com> 於 2025年9月10日 週三 下午5:10寫道:
>>
>> Hello Cheng Ming,
>>
>> On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
>>
>> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
>> >
>> > Add a new boolean property "enable-randomizer-otp" to enable the
>> > randomizer feature on supported SPI-NAND devices.
>> >
>> > Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
>> > ---
>> >  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
>> >  1 file changed, 4 insertions(+)
>> >
>> > diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > index 77a8727c7..432bc79e9 100644
>> > --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > @@ -21,6 +21,10 @@ properties:
>> >      description: Encode the chip-select line on the SPI bus
>> >      maxItems: 1
>> >
>> > +  enable-randomizer-otp:
>>
>> This is a NAND wide feature, so we should probably add a prefix, such as
>> "nand,".
>>
>> Now, what about this "otp" suffix? Many (if not all) chips have a
>> volatile setting for that. About the naming, "otp" often reflects to the
>> OTP area, which is not what you imply here, as you want to insist
>> (rightfully) on the fact that this feature cannot be disabled.
>
> Yes, my intention is that once the randomizer feature is enabled, it
> should not be disabled again.

I believe the motivation behind this choice is incorrect and does not
fit such an OS as Linux. We want to give the system administrator the
choice to select and unselect features as it sees fit. So if there is a
volatile way to enable something, we should go for it and we will pay
the extra penalty of a set_feature() (or whatever) command after each
boot. Making this type of change permanent makes development and
debugging much more painful. There are plenty of configurations that we
refuse to apply with non volatile configurations. In general, stateful
modes are problematic if they are kept after a reboot and we try our
best to avoid them.

Once this feature is enabled, for the lifetime of "a product", it does
not make sense to disable it indeed. And the DT must remain correct, if
it is changed in a non compatible way, that's the administrator
responsibility.

So are there ways to enable this feature in a way that returns to the
default state after a reset?

> You are correct that this bit does not
> belong to the OTP area, but rather to the v2 volatile register. The v2
> volatile register has a default value that can be changed through a
> special OTP configuration register program operation. Regarding the
> "otp" suffix, I will remove it to avoid misunderstanding.
>
>>
>> Also, this is a per-chip configuration, while I would have welcomed a
>> per-partition configuration. I can easily imagine two cases:
>>
>> - The boot ROM, for longevity purposes, expects the first blocks
>>   containing the bootloader to be scrambled. However the rest of the
>>   system does not really care and disables randomization.
>>
>> - The boot ROM is not capable of de-scrambling, however the rest of the
>>   system relies on the (probably) more robust scrambling feature.
>>
>> In both cases a chip wide variable is not relevant.
>
> The scrambling and descrambling are handled by the NAND flash hardware
> itself. Therefore, the boot ROM does not need to support a descrambling
> feature.
>
> In the case of Macronix parts, the randomizer is controlled through a
> configuration register, and once it is enabled it covers the entire chip
> (main and/or spare depending on the randopt bit). There is no hardware
> mechanism to enable it only on certain ranges.

After thinking once again, I am no longer aligned with myself from the
past. This is perhaps too Macronix specific in the end and if it is a
fully transparent feature, then why not.

Are there any ways we can read the raw data (unscrambled) once
scrambling has been enabled? (I mean, without changing the user default
OTP state). Maybe one big difference with the raw NAND world that needs
to be taken into account is that the chip itself always return
unscrambled data, hence we probably do not care much, from a Linux
perspective.

Thanks,
Miquèl
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Cheng Ming Lin 3 weeks ago
Hi Miquel,

Miquel Raynal <miquel.raynal@bootlin.com> 於 2025年9月11日 週四 下午3:59寫道:
>
> Hi Cheng Ming,
>
> On 11/09/2025 at 15:24:35 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
>
> > Hi Miquel,
> >
> > Miquel Raynal <miquel.raynal@bootlin.com> 於 2025年9月10日 週三 下午5:10寫道:
> >>
> >> Hello Cheng Ming,
> >>
> >> On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
> >>
> >> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
> >> >
> >> > Add a new boolean property "enable-randomizer-otp" to enable the
> >> > randomizer feature on supported SPI-NAND devices.
> >> >
> >> > Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
> >> > ---
> >> >  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
> >> >  1 file changed, 4 insertions(+)
> >> >
> >> > diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> >> > index 77a8727c7..432bc79e9 100644
> >> > --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> >> > +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> >> > @@ -21,6 +21,10 @@ properties:
> >> >      description: Encode the chip-select line on the SPI bus
> >> >      maxItems: 1
> >> >
> >> > +  enable-randomizer-otp:
> >>
> >> This is a NAND wide feature, so we should probably add a prefix, such as
> >> "nand,".
> >>
> >> Now, what about this "otp" suffix? Many (if not all) chips have a
> >> volatile setting for that. About the naming, "otp" often reflects to the
> >> OTP area, which is not what you imply here, as you want to insist
> >> (rightfully) on the fact that this feature cannot be disabled.
> >
> > Yes, my intention is that once the randomizer feature is enabled, it
> > should not be disabled again.
>
> I believe the motivation behind this choice is incorrect and does not
> fit such an OS as Linux. We want to give the system administrator the
> choice to select and unselect features as it sees fit. So if there is a
> volatile way to enable something, we should go for it and we will pay
> the extra penalty of a set_feature() (or whatever) command after each
> boot. Making this type of change permanent makes development and
> debugging much more painful. There are plenty of configurations that we
> refuse to apply with non volatile configurations. In general, stateful
> modes are problematic if they are kept after a reboot and we try our
> best to avoid them.
>
> Once this feature is enabled, for the lifetime of "a product", it does
> not make sense to disable it indeed. And the DT must remain correct, if
> it is changed in a non compatible way, that's the administrator
> responsibility.
>
> So are there ways to enable this feature in a way that returns to the
> default state after a reset?

Thank you for the detailed feedback. I understand and agree with the
principle of favoring volatile configurations to give administrators
more control and simplify debugging.

Yes, it is possible to enable this feature in a way that it returns
to the default state after a reset.

We can use the standard set_feature command to set the RANDEN bit to 1,
which enables the randomizer. This configuration is volatile and will
not persist through a power cycle. The feature will remain disabled by
default on each boot unless it is explicitly enabled again.

I will update the patch to implement this volatile approach.

>
> > You are correct that this bit does not
> > belong to the OTP area, but rather to the v2 volatile register. The v2
> > volatile register has a default value that can be changed through a
> > special OTP configuration register program operation. Regarding the
> > "otp" suffix, I will remove it to avoid misunderstanding.
> >
> >>
> >> Also, this is a per-chip configuration, while I would have welcomed a
> >> per-partition configuration. I can easily imagine two cases:
> >>
> >> - The boot ROM, for longevity purposes, expects the first blocks
> >>   containing the bootloader to be scrambled. However the rest of the
> >>   system does not really care and disables randomization.
> >>
> >> - The boot ROM is not capable of de-scrambling, however the rest of the
> >>   system relies on the (probably) more robust scrambling feature.
> >>
> >> In both cases a chip wide variable is not relevant.
> >
> > The scrambling and descrambling are handled by the NAND flash hardware
> > itself. Therefore, the boot ROM does not need to support a descrambling
> > feature.
> >
> > In the case of Macronix parts, the randomizer is controlled through a
> > configuration register, and once it is enabled it covers the entire chip
> > (main and/or spare depending on the randopt bit). There is no hardware
> > mechanism to enable it only on certain ranges.
>
> After thinking once again, I am no longer aligned with myself from the
> past. This is perhaps too Macronix specific in the end and if it is a
> fully transparent feature, then why not.
>
> Are there any ways we can read the raw data (unscrambled) once
> scrambling has been enabled? (I mean, without changing the user default
> OTP state). Maybe one big difference with the raw NAND world that needs
> to be taken into account is that the chip itself always return
> unscrambled data, hence we probably do not care much, from a Linux
> perspective.

Unfortunately, there is no way to do this. If a user decides to use the
randomizer feature, they must enable it before programming the user OTP
area. If the randomizer is not enabled before the user OTP is programmed,
any subsequent reads will result in corrupted data.

Marconix raw NAND chip itself does not always return unscrambled data.

The ability to read unscrambled data is dependent on whether the
randomizer feature was enabled and configured correctly. When the
randomizer is turned on, the chip's internal hardware automatically
handles the unscrambling process. If the feature is not enabled, the
data read from the memory will be the raw, scrambled information, which
is unusable without the correct key and algorithm. In other words, the
chip only returns unscrambled data if randomizer feature is enabled.

>
> Thanks,
> Miquèl

Thanks,
Cheng Ming Lin
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Miquel Raynal 3 weeks ago
Hi,

>> >> This is a NAND wide feature, so we should probably add a prefix, such as
>> >> "nand,".
>> >>
>> >> Now, what about this "otp" suffix? Many (if not all) chips have a
>> >> volatile setting for that. About the naming, "otp" often reflects to the
>> >> OTP area, which is not what you imply here, as you want to insist
>> >> (rightfully) on the fact that this feature cannot be disabled.
>> >
>> > Yes, my intention is that once the randomizer feature is enabled, it
>> > should not be disabled again.
>>
>> I believe the motivation behind this choice is incorrect and does not
>> fit such an OS as Linux. We want to give the system administrator the
>> choice to select and unselect features as it sees fit. So if there is a
>> volatile way to enable something, we should go for it and we will pay
>> the extra penalty of a set_feature() (or whatever) command after each
>> boot. Making this type of change permanent makes development and
>> debugging much more painful. There are plenty of configurations that we
>> refuse to apply with non volatile configurations. In general, stateful
>> modes are problematic if they are kept after a reboot and we try our
>> best to avoid them.
>>
>> Once this feature is enabled, for the lifetime of "a product", it does
>> not make sense to disable it indeed. And the DT must remain correct, if
>> it is changed in a non compatible way, that's the administrator
>> responsibility.
>>
>> So are there ways to enable this feature in a way that returns to the
>> default state after a reset?
>
> Thank you for the detailed feedback. I understand and agree with the
> principle of favoring volatile configurations to give administrators
> more control and simplify debugging.
>
> Yes, it is possible to enable this feature in a way that it returns
> to the default state after a reset.
>
> We can use the standard set_feature command to set the RANDEN bit to 1,
> which enables the randomizer. This configuration is volatile and will
> not persist through a power cycle. The feature will remain disabled by
> default on each boot unless it is explicitly enabled again.
>
> I will update the patch to implement this volatile approach.

I would be in favour of implementing this method. Hence, no more OTP
constraint to express in the binding either.

>> > You are correct that this bit does not
>> > belong to the OTP area, but rather to the v2 volatile register. The v2
>> > volatile register has a default value that can be changed through a
>> > special OTP configuration register program operation. Regarding the
>> > "otp" suffix, I will remove it to avoid misunderstanding.
>> >
>> >>
>> >> Also, this is a per-chip configuration, while I would have welcomed a
>> >> per-partition configuration. I can easily imagine two cases:
>> >>
>> >> - The boot ROM, for longevity purposes, expects the first blocks
>> >>   containing the bootloader to be scrambled. However the rest of the
>> >>   system does not really care and disables randomization.
>> >>
>> >> - The boot ROM is not capable of de-scrambling, however the rest of the
>> >>   system relies on the (probably) more robust scrambling feature.
>> >>
>> >> In both cases a chip wide variable is not relevant.
>> >
>> > The scrambling and descrambling are handled by the NAND flash hardware
>> > itself. Therefore, the boot ROM does not need to support a descrambling
>> > feature.
>> >
>> > In the case of Macronix parts, the randomizer is controlled through a
>> > configuration register, and once it is enabled it covers the entire chip
>> > (main and/or spare depending on the randopt bit). There is no hardware
>> > mechanism to enable it only on certain ranges.
>>
>> After thinking once again, I am no longer aligned with myself from the
>> past. This is perhaps too Macronix specific in the end and if it is a
>> fully transparent feature, then why not.
>>
>> Are there any ways we can read the raw data (unscrambled) once
>> scrambling has been enabled? (I mean, without changing the user default
>> OTP state). Maybe one big difference with the raw NAND world that needs
>> to be taken into account is that the chip itself always return
>> unscrambled data, hence we probably do not care much, from a Linux
>> perspective.
>
> Unfortunately, there is no way to do this. If a user decides to use the
> randomizer feature, they must enable it before programming the user OTP
> area. If the randomizer is not enabled before the user OTP is programmed,
> any subsequent reads will result in corrupted data.
>
> Marconix raw NAND chip itself does not always return unscrambled data.
>
> The ability to read unscrambled data is dependent on whether the
> randomizer feature was enabled and configured correctly. When the
> randomizer is turned on, the chip's internal hardware automatically
> handles the unscrambling process. If the feature is not enabled, the
> data read from the memory will be the raw, scrambled information, which
> is unusable without the correct key and algorithm. In other words, the
> chip only returns unscrambled data if randomizer feature is enabled.

That is interesting and might be useful. In Linux we will enable the
scrambler if the data property is enabled before we get to play with the
OTP anyway, so hopefully people will get it right.

Regarding the property name, it is going to be a per SPI NAND chip
property. I am still not sure whether we should make this prop generic
or not. Perhaps the safest approach is to keep it vendor specific, but
the concept of scrambling internal data for longevity purposes is common
enough to deserve a nand- prefix. Although it's gonna be like ECC
engines, we'll need to decide which part of the system handles the
scrambling (software, hardware on host or chip itself) and whether it is
enabled per-chip or per-partition when that's possible.

I am open to suggestions.

Thanks,
Miquèl
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Cheng Ming Lin 2 weeks, 6 days ago
Hi Miquel,

Miquel Raynal <miquel.raynal@bootlin.com> 於 2025年9月11日 週四 下午9:57寫道:
>
> Hi,
>
> >> >> This is a NAND wide feature, so we should probably add a prefix, such as
> >> >> "nand,".
> >> >>
> >> >> Now, what about this "otp" suffix? Many (if not all) chips have a
> >> >> volatile setting for that. About the naming, "otp" often reflects to the
> >> >> OTP area, which is not what you imply here, as you want to insist
> >> >> (rightfully) on the fact that this feature cannot be disabled.
> >> >
> >> > Yes, my intention is that once the randomizer feature is enabled, it
> >> > should not be disabled again.
> >>
> >> I believe the motivation behind this choice is incorrect and does not
> >> fit such an OS as Linux. We want to give the system administrator the
> >> choice to select and unselect features as it sees fit. So if there is a
> >> volatile way to enable something, we should go for it and we will pay
> >> the extra penalty of a set_feature() (or whatever) command after each
> >> boot. Making this type of change permanent makes development and
> >> debugging much more painful. There are plenty of configurations that we
> >> refuse to apply with non volatile configurations. In general, stateful
> >> modes are problematic if they are kept after a reboot and we try our
> >> best to avoid them.
> >>
> >> Once this feature is enabled, for the lifetime of "a product", it does
> >> not make sense to disable it indeed. And the DT must remain correct, if
> >> it is changed in a non compatible way, that's the administrator
> >> responsibility.
> >>
> >> So are there ways to enable this feature in a way that returns to the
> >> default state after a reset?
> >
> > Thank you for the detailed feedback. I understand and agree with the
> > principle of favoring volatile configurations to give administrators
> > more control and simplify debugging.
> >
> > Yes, it is possible to enable this feature in a way that it returns
> > to the default state after a reset.
> >
> > We can use the standard set_feature command to set the RANDEN bit to 1,
> > which enables the randomizer. This configuration is volatile and will
> > not persist through a power cycle. The feature will remain disabled by
> > default on each boot unless it is explicitly enabled again.
> >
> > I will update the patch to implement this volatile approach.
>
> I would be in favour of implementing this method. Hence, no more OTP
> constraint to express in the binding either.

I wonder if we could still go with two approaches.

For Macronix we may add a DT property in macronix.c to decide whether
to use a set feature operation or the special OTP program operations.

>
> >> > You are correct that this bit does not
> >> > belong to the OTP area, but rather to the v2 volatile register. The v2
> >> > volatile register has a default value that can be changed through a
> >> > special OTP configuration register program operation. Regarding the
> >> > "otp" suffix, I will remove it to avoid misunderstanding.
> >> >
> >> >>
> >> >> Also, this is a per-chip configuration, while I would have welcomed a
> >> >> per-partition configuration. I can easily imagine two cases:
> >> >>
> >> >> - The boot ROM, for longevity purposes, expects the first blocks
> >> >>   containing the bootloader to be scrambled. However the rest of the
> >> >>   system does not really care and disables randomization.
> >> >>
> >> >> - The boot ROM is not capable of de-scrambling, however the rest of the
> >> >>   system relies on the (probably) more robust scrambling feature.
> >> >>
> >> >> In both cases a chip wide variable is not relevant.
> >> >
> >> > The scrambling and descrambling are handled by the NAND flash hardware
> >> > itself. Therefore, the boot ROM does not need to support a descrambling
> >> > feature.
> >> >
> >> > In the case of Macronix parts, the randomizer is controlled through a
> >> > configuration register, and once it is enabled it covers the entire chip
> >> > (main and/or spare depending on the randopt bit). There is no hardware
> >> > mechanism to enable it only on certain ranges.
> >>
> >> After thinking once again, I am no longer aligned with myself from the
> >> past. This is perhaps too Macronix specific in the end and if it is a
> >> fully transparent feature, then why not.
> >>
> >> Are there any ways we can read the raw data (unscrambled) once
> >> scrambling has been enabled? (I mean, without changing the user default
> >> OTP state). Maybe one big difference with the raw NAND world that needs
> >> to be taken into account is that the chip itself always return
> >> unscrambled data, hence we probably do not care much, from a Linux
> >> perspective.
> >
> > Unfortunately, there is no way to do this. If a user decides to use the
> > randomizer feature, they must enable it before programming the user OTP
> > area. If the randomizer is not enabled before the user OTP is programmed,
> > any subsequent reads will result in corrupted data.
> >
> > Marconix raw NAND chip itself does not always return unscrambled data.
> >
> > The ability to read unscrambled data is dependent on whether the
> > randomizer feature was enabled and configured correctly. When the
> > randomizer is turned on, the chip's internal hardware automatically
> > handles the unscrambling process. If the feature is not enabled, the
> > data read from the memory will be the raw, scrambled information, which
> > is unusable without the correct key and algorithm. In other words, the
> > chip only returns unscrambled data if randomizer feature is enabled.
>
> That is interesting and might be useful. In Linux we will enable the
> scrambler if the data property is enabled before we get to play with the
> OTP anyway, so hopefully people will get it right.
>
> Regarding the property name, it is going to be a per SPI NAND chip
> property. I am still not sure whether we should make this prop generic
> or not. Perhaps the safest approach is to keep it vendor specific, but
> the concept of scrambling internal data for longevity purposes is common
> enough to deserve a nand- prefix. Although it's gonna be like ECC
> engines, we'll need to decide which part of the system handles the
> scrambling (software, hardware on host or chip itself) and whether it is
> enabled per-chip or per-partition when that's possible.
>
> I am open to suggestions.

I also think we may consider moving the randomizer support into the NAND
layer, not just SPI-NAND, since Macronix ONFI NAND devices also support
randomizer. And I agree it would be similar to ECC engines, where the
scrambling of data can be determined by software, hardware on the host,
or by the chip itself.

>
> Thanks,
> Miquèl

Thanks,
Cheng Ming Lin
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Rob Herring 3 weeks, 1 day ago
On Wed, Sep 10, 2025 at 11:09:35AM +0200, Miquel Raynal wrote:
> Hello Cheng Ming,
> 
> On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
> 
> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
> >
> > Add a new boolean property "enable-randomizer-otp" to enable the
> > randomizer feature on supported SPI-NAND devices.
> >
> > Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
> > ---
> >  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > index 77a8727c7..432bc79e9 100644
> > --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
> > @@ -21,6 +21,10 @@ properties:
> >      description: Encode the chip-select line on the SPI bus
> >      maxItems: 1
> >  
> > +  enable-randomizer-otp:
> 
> This is a NAND wide feature, so we should probably add a prefix, such as
> "nand,".

'nand' is not a vendor, so no.

Rob
Re: [PATCH v2 1/3] dt-bindings: mtd: spi-nand: Add enable-randomizer-otp property
Posted by Miquel Raynal 3 weeks ago
Hi,

On 10/09/2025 at 11:36:31 -05, Rob Herring <robh@kernel.org> wrote:

> On Wed, Sep 10, 2025 at 11:09:35AM +0200, Miquel Raynal wrote:
>> Hello Cheng Ming,
>> 
>> On 10/09/2025 at 11:02:59 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:
>> 
>> > From: Cheng Ming Lin <chengminglin@mxic.com.tw>
>> >
>> > Add a new boolean property "enable-randomizer-otp" to enable the
>> > randomizer feature on supported SPI-NAND devices.
>> >
>> > Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
>> > ---
>> >  Documentation/devicetree/bindings/mtd/spi-nand.yaml | 4 ++++
>> >  1 file changed, 4 insertions(+)
>> >
>> > diff --git a/Documentation/devicetree/bindings/mtd/spi-nand.yaml b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > index 77a8727c7..432bc79e9 100644
>> > --- a/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > +++ b/Documentation/devicetree/bindings/mtd/spi-nand.yaml
>> > @@ -21,6 +21,10 @@ properties:
>> >      description: Encode the chip-select line on the SPI bus
>> >      maxItems: 1
>> >  
>> > +  enable-randomizer-otp:
>> 
>> This is a NAND wide feature, so we should probably add a prefix, such as
>> "nand,".
>
> 'nand' is not a vendor, so no.

Sorry for the confusion I meant "nand-", like we already have:
- nand-ecc-engine
- nand-use-soft-ecc-engine
- nand-ecc-strength
- nand-ecc-step-size
etc

Thanks,
Miquèl