[PATCH v1 3/3] mtd: rawnand: meson: read/write access for boot ROM pages

2024-06-02 Thread Arseniy Krasnov
Boot ROM on Meson needs some pages to be read/written in a special mode:
384 byte ECC mode (so called "short" by Amlogic) and with scrambling
enabled. Such pages are located on the chip in the following way (for
example):

[ p0 ][ p1 ][ p2 ][ p3 ][ p4 ][ p5 ][ p6 ][ p7 ] ... [ pN ]
  ^   ^   ^   ^

pX is page number "X". "^" means "special" page used by boot ROM - e.g.
every 2nd page in the range of [0, 7]. Step (2 here) and last page in
range is read from the device tree.

Signed-off-by: Arseniy Krasnov 
---
 drivers/mtd/nand/raw/meson_nand.c | 56 +--
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/nand/raw/meson_nand.c 
b/drivers/mtd/nand/raw/meson_nand.c
index 19f005202b..54ea035d8d 100644
--- a/drivers/mtd/nand/raw/meson_nand.c
+++ b/drivers/mtd/nand/raw/meson_nand.c
@@ -40,6 +40,7 @@
 #define NFC_CMD_RB BIT(20)
 #define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
 #define NFC_CMD_SCRAMBLER_DISABLE  0
+#define NFC_CMD_SHORTMODE_ENABLE   1
 #define NFC_CMD_SHORTMODE_DISABLE  0
 #define NFC_CMD_RB_INT BIT(14)
 #define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
@@ -78,6 +79,8 @@
 
 #define DMA_DIR(dir)   ((dir) ? NFC_CMD_N2M : NFC_CMD_M2N)
 
+#define NFC_SHORT_MODE_ECC_SZ  384
+
 #define ECC_CHECK_RETURN_FF-1
 
 #define NAND_CE0   (0xe << 10)
@@ -141,6 +144,8 @@
 struct meson_nfc_nand_chip {
struct list_head node;
struct nand_chip nand;
+   u32 boot_pages;
+   u32 boot_page_step;
 
u32 bch_mode;
u8 *data_buf;
@@ -229,33 +234,46 @@ static void meson_nfc_cmd_seed(const struct meson_nfc 
*nfc, u32 seed)
   nfc->reg_base + NFC_REG_CMD);
 }
 
+static int meson_nfc_is_boot_page(struct nand_chip *nand, int page)
+{
+   const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
+
+   return (nand->options & NAND_IS_BOOT_MEDIUM) &&
+  !(page % meson_chip->boot_page_step) &&
+  (page < meson_chip->boot_pages);
+}
+
 static void meson_nfc_cmd_access(struct nand_chip *nand, bool raw, bool dir, 
int page)
 {
+   const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
struct mtd_info *mtd = nand_to_mtd(nand);
const struct meson_nfc *nfc = 
nand_get_controller_data(mtd_to_nand(mtd));
-   const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
-   u32 bch = meson_chip->bch_mode, cmd;
int len = mtd->writesize, pagesize, pages;
int scrambler;
+   u32 cmd;
 
if (nand->options & NAND_NEED_SCRAMBLING)
scrambler = NFC_CMD_SCRAMBLER_ENABLE;
else
scrambler = NFC_CMD_SCRAMBLER_DISABLE;
 
-   pagesize = nand->ecc.size;
-
if (raw) {
len = mtd->writesize + mtd->oobsize;
cmd = len | scrambler | DMA_DIR(dir);
-   writel(cmd, nfc->reg_base + NFC_REG_CMD);
-   return;
-   }
+   } else if (meson_nfc_is_boot_page(nand, page)) {
+   pagesize = NFC_SHORT_MODE_ECC_SZ >> 3;
+   pages = mtd->writesize / 512;
 
-   pages = len / nand->ecc.size;
+   scrambler = NFC_CMD_SCRAMBLER_ENABLE;
+   cmd = CMDRWGEN(DMA_DIR(dir), scrambler, NFC_ECC_BCH8_1K,
+  NFC_CMD_SHORTMODE_ENABLE, pagesize, pages);
+   } else {
+   pagesize = nand->ecc.size >> 3;
+   pages = len / nand->ecc.size;
 
-   cmd = CMDRWGEN(DMA_DIR(dir), scrambler, bch,
-  NFC_CMD_SHORTMODE_DISABLE, pagesize, pages);
+   cmd = CMDRWGEN(DMA_DIR(dir), scrambler, meson_chip->bch_mode,
+  NFC_CMD_SHORTMODE_DISABLE, pagesize, pages);
+   }
 
if (scrambler == NFC_CMD_SCRAMBLER_ENABLE)
meson_nfc_cmd_seed(nfc, page);
@@ -1132,6 +1150,24 @@ static int meson_nfc_nand_chip_init(struct udevice *dev, 
struct meson_nfc *nfc,
goto err_chip_buf_free;
}
 
+   if (nand->options & NAND_IS_BOOT_MEDIUM) {
+   ret = ofnode_read_u32(node, "amlogic,boot-pages",
+ _chip->boot_pages);
+   if (ret) {
+   dev_err(dev, "could not retrieve 'amlogic,boot-pages' 
property: %d",
+   ret);
+   goto err_chip_buf_free;
+   }
+
+   ret = ofnode_read_u32(node, "amlogic,boot-page-step",
+ _chip->boot_page_step);
+   if (ret) {
+   dev_err(dev, "could not retrieve 
'amlogic,boot-page-step' property: %d",
+   ret);
+   

[PATCH v1 1/3] mtd: rawnand: nand_base: support for 'NAND_IS_BOOT_MEDIUM' flag

2024-06-02 Thread Arseniy Krasnov
Based on Linux kernel:
commit f922bd798bb9 ("mtd: rawnand: add an option to specify NAND chip as a 
boot device")

Allow to define a NAND chip as a boot device. This can be helpful
for the selection of the ECC algorithm and strength in case the boot
ROM supports only a subset of controller provided options.

Signed-off-by: Arseniy Krasnov 
---
 drivers/mtd/nand/raw/nand_base.c | 3 +++
 include/linux/mtd/rawnand.h  | 6 ++
 2 files changed, 9 insertions(+)

diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index c40a0f23d7..ed605b4af5 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -4458,6 +4458,9 @@ static int nand_dt_init(struct mtd_info *mtd, struct 
nand_chip *chip, ofnode nod
if (ret == 16)
chip->options |= NAND_BUSWIDTH_16;
 
+   if (ofnode_read_bool(node, "nand-is-boot-medium"))
+   chip->options |= NAND_IS_BOOT_MEDIUM;
+
if (ofnode_read_bool(node, "nand-on-flash-bbt"))
chip->bbt_options |= NAND_BBT_USE_FLASH;
 
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index fb002ae641..4eb880d8fb 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -218,6 +218,12 @@ enum nand_ecc_algo {
 /* Device needs 3rd row address cycle */
 #define NAND_ROW_ADDR_30x4000
 
+/*
+ * Whether the NAND chip is a boot medium. Drivers might use this information
+ * to select ECC algorithms supported by the boot ROM or similar restrictions.
+ */
+#define NAND_IS_BOOT_MEDIUM0x0040
+
 /* Options valid for Samsung large page devices */
 #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
 
-- 
2.35.0



[PATCH v1 0/3] Meson: R/W support for pages used by boot ROM

2024-06-02 Thread Arseniy Krasnov
Patchset is based on patchset for Linux (today merged to nand-next):
https://lore.kernel.org/linux-mtd/20240507230903.3399594-1-avkras...@salutedevices.com/

Here is description from it:

 >  Amlogic's boot ROM code needs that some pages on NAND must be written
 >  in special "short" ECC mode with scrambling enabled. Such pages:
 >  1) Contain some metadata about hardware.
 >  2) Located with some interval starting from 0 offset, until some
 > specified offset. Interval and second offset are set in the
 > device tree.
 >  
 >  This patchset adds R/W support for such pages. To enable it we can setup
 >  it in dts:
 >  
 >  nand-is-boot-medium;
 >  amlogic,boot-pages = <1024>;
 >  amlogic,boot-page-step = <128>;
 >  
 >  It means that each 128th page in range 0 to 1024 pages will be accessed
 >  in special mode ("short" ECC + scrambling). In practice this feature is
 >  needed when we want to update first block of NAND - driver will enable
 >  required mode by itself using value from device tree.

The only difference is that patchset for Linux updates DT bindings, while
this adds NAND_IS_BOOT_MEDIUM flag support.

Arseniy Krasnov (3):
  mtd: rawnand: nand_base: support for 'NAND_IS_BOOT_MEDIUM' flag
  mtd: rawnand: meson: refactor use of 'meson_nfc_cmd_access()'
  mtd: rawnand: meson: read/write access for boot ROM pages

 drivers/mtd/nand/raw/meson_nand.c | 84 +--
 drivers/mtd/nand/raw/nand_base.c  |  3 ++
 include/linux/mtd/rawnand.h   |  6 +++
 3 files changed, 66 insertions(+), 27 deletions(-)

-- 
2.35.0



[PATCH v1 2/3] mtd: rawnand: meson: refactor use of 'meson_nfc_cmd_access()'

2024-06-02 Thread Arseniy Krasnov
Move call 'meson_nfc_cmd_seed()' and check for 'NAND_NEED_SCRAMBLING'
to 'meson_nfc_cmd_access()', thus removing code duplication.

Signed-off-by: Arseniy Krasnov 
---
 drivers/mtd/nand/raw/meson_nand.c | 30 --
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/drivers/mtd/nand/raw/meson_nand.c 
b/drivers/mtd/nand/raw/meson_nand.c
index 5d411c4594..19f005202b 100644
--- a/drivers/mtd/nand/raw/meson_nand.c
+++ b/drivers/mtd/nand/raw/meson_nand.c
@@ -229,14 +229,19 @@ static void meson_nfc_cmd_seed(const struct meson_nfc 
*nfc, u32 seed)
   nfc->reg_base + NFC_REG_CMD);
 }
 
-static void meson_nfc_cmd_access(struct nand_chip *nand, bool raw, bool dir,
-int scrambler)
+static void meson_nfc_cmd_access(struct nand_chip *nand, bool raw, bool dir, 
int page)
 {
struct mtd_info *mtd = nand_to_mtd(nand);
const struct meson_nfc *nfc = 
nand_get_controller_data(mtd_to_nand(mtd));
const struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand);
u32 bch = meson_chip->bch_mode, cmd;
int len = mtd->writesize, pagesize, pages;
+   int scrambler;
+
+   if (nand->options & NAND_NEED_SCRAMBLING)
+   scrambler = NFC_CMD_SCRAMBLER_ENABLE;
+   else
+   scrambler = NFC_CMD_SCRAMBLER_DISABLE;
 
pagesize = nand->ecc.size;
 
@@ -252,6 +257,9 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, 
bool raw, bool dir,
cmd = CMDRWGEN(DMA_DIR(dir), scrambler, bch,
   NFC_CMD_SHORTMODE_DISABLE, pagesize, pages);
 
+   if (scrambler == NFC_CMD_SCRAMBLER_ENABLE)
+   meson_nfc_cmd_seed(nfc, page);
+
writel(cmd, nfc->reg_base + NFC_REG_CMD);
 }
 
@@ -566,14 +574,7 @@ static int meson_nfc_write_page_sub(struct nand_chip *nand,
return ret;
}
 
-   if (nand->options & NAND_NEED_SCRAMBLING) {
-   meson_nfc_cmd_seed(nfc, page);
-   meson_nfc_cmd_access(nand, raw, DIRWRITE,
-NFC_CMD_SCRAMBLER_ENABLE);
-   } else {
-   meson_nfc_cmd_access(nand, raw, DIRWRITE,
-NFC_CMD_SCRAMBLER_DISABLE);
-   }
+   meson_nfc_cmd_access(nand, raw, DIRWRITE, page);
 
cmd = nfc->param.chip_select | NFC_CMD_CLE | NAND_CMD_PAGEPROG;
writel(cmd, nfc->reg_base + NFC_REG_CMD);
@@ -644,14 +645,7 @@ static int meson_nfc_read_page_sub(struct nand_chip *nand,
if (ret)
return ret;
 
-   if (nand->options & NAND_NEED_SCRAMBLING) {
-   meson_nfc_cmd_seed(nfc, page);
-   meson_nfc_cmd_access(nand, raw, DIRREAD,
-NFC_CMD_SCRAMBLER_ENABLE);
-   } else {
-   meson_nfc_cmd_access(nand, raw, DIRREAD,
-NFC_CMD_SCRAMBLER_DISABLE);
-   }
+   meson_nfc_cmd_access(nand, raw, DIRREAD, page);
 
meson_nfc_wait_dma_finish(nfc);
meson_nfc_check_ecc_pages_valid(nfc, nand, raw);
-- 
2.35.0



Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-05-24 Thread Arseniy Krasnov



On 24.05.2024 16:31, Dario Binacchi wrote:
> Hi Arseniy,
> 
> On Fri, May 24, 2024 at 11:25 AM Arseniy Krasnov
>  wrote:
>>
>> Hi Dario!
>>
>> Sorry, is this patch ok?
> 
> Sorry, I told you I was testing it but I forgot to tell you that
> testing was successfully
> completed for this patch.

Ah, no problem, it will be merged to next release ?

Thanks

> 
> Thanks and regards,
> Dario
> 
>>
>> Thanks
>>
>> On 18.04.2024 09:55, Dario Binacchi wrote:
>>> Arseniy, Michael, All
>>>
>>> On Wed, Apr 17, 2024 at 8:44 PM Michael Nazzareno Trimarchi
>>>  wrote:
>>>>
>>>> Hi
>>>>
>>>> Dario did you add those patches in CI and test them again?
>>>>
>>>> Michael
>>>>
>>>> On Wed, Apr 17, 2024 at 8:44 PM Arseniy Krasnov
>>>>  wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> Sorry, pls ping
>>>>>
>>>>> Thanks, Arseniy
>>>>>
>>>>> On 13.03.2024 09:46, Michael Nazzareno Trimarchi wrote:
>>>>>> Hi  Dario
>>>>>>
>>>>>> Can apply this series and put in CI?
>>>
>>> Sorry, but I mistakenly tagged it as 'superseded'.
>>> I just pushed it to my nand-next branch and I'm running the CI now.
>>>
>>> Thanks and regards,
>>> Dario
>>>
>>>>>>
>>>>>> Michael
>>>>>>
>>>>>> On Wed, Mar 13, 2024 at 7:43 AM Arseniy Krasnov
>>>>>>  wrote:
>>>>>>>
>>>>>>> Sorry, please ping
>>>>>>>
>>>>>>> Thanks, Arseniy
>>>>>>>
>>>>>>> On 11.02.2024 02:16, Arseniy Krasnov wrote:
>>>>>>>> Sorry, pls ping
>>>>>>>>
>>>>>>>> Thanks, Arseniy
>>>>>>>>
>>>>>>>> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>>>>>>>>> Sorry, pls ping
>>>>>>>>>
>>>>>>>>> Thanks, Arseniy
>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Michael Nazzareno Trimarchi
>>>> Co-Founder & Chief Executive Officer
>>>> M. +39 347 913 2170
>>>> mich...@amarulasolutions.com
>>>> __
>>>>
>>>> Amarula Solutions BV
>>>> Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
>>>> T. +31 (0)85 111 9172
>>>> i...@amarulasolutions.com
>>>> www.amarulasolutions.com
>>>
>>>
>>>
> 
> 
> 


Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-05-24 Thread Arseniy Krasnov
Hi Dario!

Sorry, is this patch ok?

Thanks

On 18.04.2024 09:55, Dario Binacchi wrote:
> Arseniy, Michael, All
> 
> On Wed, Apr 17, 2024 at 8:44 PM Michael Nazzareno Trimarchi
>  wrote:
>>
>> Hi
>>
>> Dario did you add those patches in CI and test them again?
>>
>> Michael
>>
>> On Wed, Apr 17, 2024 at 8:44 PM Arseniy Krasnov
>>  wrote:
>>>
>>> Hello,
>>>
>>> Sorry, pls ping
>>>
>>> Thanks, Arseniy
>>>
>>> On 13.03.2024 09:46, Michael Nazzareno Trimarchi wrote:
>>>> Hi  Dario
>>>>
>>>> Can apply this series and put in CI?
> 
> Sorry, but I mistakenly tagged it as 'superseded'.
> I just pushed it to my nand-next branch and I'm running the CI now.
> 
> Thanks and regards,
> Dario
> 
>>>>
>>>> Michael
>>>>
>>>> On Wed, Mar 13, 2024 at 7:43 AM Arseniy Krasnov
>>>>  wrote:
>>>>>
>>>>> Sorry, please ping
>>>>>
>>>>> Thanks, Arseniy
>>>>>
>>>>> On 11.02.2024 02:16, Arseniy Krasnov wrote:
>>>>>> Sorry, pls ping
>>>>>>
>>>>>> Thanks, Arseniy
>>>>>>
>>>>>> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>>>>>>> Sorry, pls ping
>>>>>>>
>>>>>>> Thanks, Arseniy
>>>>
>>>>
>>>>
>>
>>
>>
>> --
>> Michael Nazzareno Trimarchi
>> Co-Founder & Chief Executive Officer
>> M. +39 347 913 2170
>> mich...@amarulasolutions.com
>> __
>>
>> Amarula Solutions BV
>> Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
>> T. +31 (0)85 111 9172
>> i...@amarulasolutions.com
>> www.amarulasolutions.com
> 
> 
> 


Re: [PATCH v1] arm: dts: meson-axg: add NAND controller node for AXG

2024-04-26 Thread Arseniy Krasnov



On 26.04.2024 11:55, neil.armstr...@linaro.org wrote:
> On 26/04/2024 10:40, Arseniy Krasnov wrote:
>> Hi,
>>
>> On 26.04.2024 11:21, Neil Armstrong wrote:
>>> Hi,
>>>
>>> On 25/04/2024 19:50, Arseniy Krasnov wrote:
>>>> nfc: Synced from Linux commit 7ca2ef33179f ("Linux 6.6-rc1")
>>>> nand_all_pins: Synced from Linux commit be18d53c32b2 ("Linux 6.7-rc3")
>>>
>>> No need to sync DT anymore, this is handled by OF_UPSTREAM and for next 
>>> release
>>> it's already aligned with v6.9 DT.
>>>
>>> Please try your patches with master or next branch of U-Boot repo.
>>
>> Ok, so I can remove "Synced from" from commit description, thus make it 
>> empty, and test that patch applies correctly
>> on uboot master/ next ?
> 
> It won't apply because arch/arm/dts/meson-axg.dtsi doesn't exist anymore, DT 
> comes
> from dts/upstream, however if you need to add u-boot specific changes you can
> still update arch/arm/dts/meson-axgi-u-boot.dtsi

Ahh ok, I see. Thanks. This patch must be dropped, as same code is
already in uboot in dts/upstream

Thanks

> 
> Neil
> 
>>
>> Thanks
>>
>>>
>>> Neil
>>>
>>>>
>>>> Signed-off-by: Arseniy Krasnov 
>>>> ---
>>>>    arch/arm/dts/meson-axg.dtsi | 36 
>>>>    1 file changed, 36 insertions(+)
>>>>
>>>> diff --git a/arch/arm/dts/meson-axg.dtsi b/arch/arm/dts/meson-axg.dtsi
>>>> index 3f5254eeb4..4f6fdd5523 100644
>>>> --- a/arch/arm/dts/meson-axg.dtsi
>>>> +++ b/arch/arm/dts/meson-axg.dtsi
>>>> @@ -430,6 +430,27 @@
>>>>    };
>>>>    };
>>>>    +    nand_all_pins: nand_all_pins {
>>>> +    mux {
>>>> +    groups = "emmc_nand_d0",
>>>> + "emmc_nand_d1",
>>>> + "emmc_nand_d2",
>>>> + "emmc_nand_d3",
>>>> + "emmc_nand_d4",
>>>> + "emmc_nand_d5",
>>>> + "emmc_nand_d6",
>>>> + "emmc_nand_d7",
>>>> + "nand_ce0",
>>>> + "nand_ale",
>>>> + "nand_cle",
>>>> + "nand_wen_clk",
>>>> + "nand_ren_wr";
>>>> +    function = "nand";
>>>> +    input-enable;
>>>> +    bias-pull-up;
>>>> +    };
>>>> +    };
>>>> +
>>>>    emmc_ds_pins: emmc_ds {
>>>>    mux {
>>>>    groups = "emmc_ds";
>>>> @@ -1906,6 +1927,21 @@
>>>>    resets = < RESET_SD_EMMC_C>;
>>>>    };
>>>>    +    nfc: nand-controller@7800 {
>>>> +    compatible = "amlogic,meson-axg-nfc";
>>>> +    reg = <0x0 0x7800 0x0 0x100>,
>>>> +  <0x0 0x7000 0x0 0x800>;
>>>> +    reg-names = "nfc", "emmc";
>>>> +    pinctrl-0 = <_all_pins>;
>>>> +    pinctrl-names = "default";
>>>> +    #address-cells = <1>;
>>>> +    #size-cells = <0>;
>>>> +    interrupts = ;
>>>> +    clocks = < CLKID_SD_EMMC_C>,
>>>> + < CLKID_FCLK_DIV2>;
>>>> +    clock-names = "core", "device";
>>>> +    };
>>>> +
>>>>    usb2_phy1: phy@9020 {
>>>>    compatible = "amlogic,meson-gxl-usb2-phy";
>>>>    #phy-cells = <0>;
>>>
> 


Re: [PATCH v1] arm: dts: meson-axg: add NAND controller node for AXG

2024-04-26 Thread Arseniy Krasnov
Hi,

On 26.04.2024 11:21, Neil Armstrong wrote:
> Hi,
> 
> On 25/04/2024 19:50, Arseniy Krasnov wrote:
>> nfc: Synced from Linux commit 7ca2ef33179f ("Linux 6.6-rc1")
>> nand_all_pins: Synced from Linux commit be18d53c32b2 ("Linux 6.7-rc3")
> 
> No need to sync DT anymore, this is handled by OF_UPSTREAM and for next 
> release
> it's already aligned with v6.9 DT.
> 
> Please try your patches with master or next branch of U-Boot repo.

Ok, so I can remove "Synced from" from commit description, thus make it empty, 
and test that patch applies correctly
on uboot master/ next ?

Thanks

> 
> Neil
> 
>>
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>   arch/arm/dts/meson-axg.dtsi | 36 
>>   1 file changed, 36 insertions(+)
>>
>> diff --git a/arch/arm/dts/meson-axg.dtsi b/arch/arm/dts/meson-axg.dtsi
>> index 3f5254eeb4..4f6fdd5523 100644
>> --- a/arch/arm/dts/meson-axg.dtsi
>> +++ b/arch/arm/dts/meson-axg.dtsi
>> @@ -430,6 +430,27 @@
>>   };
>>   };
>>   +    nand_all_pins: nand_all_pins {
>> +    mux {
>> +    groups = "emmc_nand_d0",
>> + "emmc_nand_d1",
>> + "emmc_nand_d2",
>> + "emmc_nand_d3",
>> + "emmc_nand_d4",
>> + "emmc_nand_d5",
>> + "emmc_nand_d6",
>> + "emmc_nand_d7",
>> + "nand_ce0",
>> + "nand_ale",
>> + "nand_cle",
>> + "nand_wen_clk",
>> + "nand_ren_wr";
>> +    function = "nand";
>> +    input-enable;
>> +    bias-pull-up;
>> +    };
>> +    };
>> +
>>   emmc_ds_pins: emmc_ds {
>>   mux {
>>   groups = "emmc_ds";
>> @@ -1906,6 +1927,21 @@
>>   resets = < RESET_SD_EMMC_C>;
>>   };
>>   +    nfc: nand-controller@7800 {
>> +    compatible = "amlogic,meson-axg-nfc";
>> +    reg = <0x0 0x7800 0x0 0x100>,
>> +  <0x0 0x7000 0x0 0x800>;
>> +    reg-names = "nfc", "emmc";
>> +    pinctrl-0 = <_all_pins>;
>> +    pinctrl-names = "default";
>> +    #address-cells = <1>;
>> +    #size-cells = <0>;
>> +    interrupts = ;
>> +    clocks = < CLKID_SD_EMMC_C>,
>> + < CLKID_FCLK_DIV2>;
>> +    clock-names = "core", "device";
>> +    };
>> +
>>   usb2_phy1: phy@9020 {
>>   compatible = "amlogic,meson-gxl-usb2-phy";
>>   #phy-cells = <0>;
> 


[PATCH v1] arm: dts: meson-axg: add NAND controller node for AXG

2024-04-25 Thread Arseniy Krasnov
nfc: Synced from Linux commit 7ca2ef33179f ("Linux 6.6-rc1")
nand_all_pins: Synced from Linux commit be18d53c32b2 ("Linux 6.7-rc3")

Signed-off-by: Arseniy Krasnov 
---
 arch/arm/dts/meson-axg.dtsi | 36 
 1 file changed, 36 insertions(+)

diff --git a/arch/arm/dts/meson-axg.dtsi b/arch/arm/dts/meson-axg.dtsi
index 3f5254eeb4..4f6fdd5523 100644
--- a/arch/arm/dts/meson-axg.dtsi
+++ b/arch/arm/dts/meson-axg.dtsi
@@ -430,6 +430,27 @@
};
};
 
+   nand_all_pins: nand_all_pins {
+   mux {
+   groups = "emmc_nand_d0",
+"emmc_nand_d1",
+"emmc_nand_d2",
+"emmc_nand_d3",
+"emmc_nand_d4",
+"emmc_nand_d5",
+"emmc_nand_d6",
+"emmc_nand_d7",
+"nand_ce0",
+"nand_ale",
+"nand_cle",
+"nand_wen_clk",
+"nand_ren_wr";
+   function = "nand";
+   input-enable;
+   bias-pull-up;
+   };
+   };
+
emmc_ds_pins: emmc_ds {
mux {
groups = "emmc_ds";
@@ -1906,6 +1927,21 @@
resets = < RESET_SD_EMMC_C>;
};
 
+   nfc: nand-controller@7800 {
+   compatible = "amlogic,meson-axg-nfc";
+   reg = <0x0 0x7800 0x0 0x100>,
+ <0x0 0x7000 0x0 0x800>;
+   reg-names = "nfc", "emmc";
+   pinctrl-0 = <_all_pins>;
+   pinctrl-names = "default";
+   #address-cells = <1>;
+   #size-cells = <0>;
+   interrupts = ;
+   clocks = < CLKID_SD_EMMC_C>,
+< CLKID_FCLK_DIV2>;
+   clock-names = "core", "device";
+   };
+
usb2_phy1: phy@9020 {
compatible = "amlogic,meson-gxl-usb2-phy";
#phy-cells = <0>;
-- 
2.35.0



Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-04-17 Thread Arseniy Krasnov
Hello,

Sorry, pls ping

Thanks, Arseniy

On 13.03.2024 09:46, Michael Nazzareno Trimarchi wrote:
> Hi  Dario
> 
> Can apply this series and put in CI?
> 
> Michael
> 
> On Wed, Mar 13, 2024 at 7:43 AM Arseniy Krasnov
>  wrote:
>>
>> Sorry, please ping
>>
>> Thanks, Arseniy
>>
>> On 11.02.2024 02:16, Arseniy Krasnov wrote:
>>> Sorry, pls ping
>>>
>>> Thanks, Arseniy
>>>
>>> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>>>> Sorry, pls ping
>>>>
>>>> Thanks, Arseniy
> 
> 
> 


[PATCH v5] cmd: mtd: OTP access support

2024-03-26 Thread Arseniy Krasnov
Add access to OTP region. It supports info, dump, write and lock
operations. Usage example:

'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
 0x44 to offset 10 to user area of device 'nand0'.

'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.

Signed-off-by: Arseniy Krasnov 
---
 Changelog:
 v1 -> v2:
  * Remove warning that OTP can't be erased after write.
 v2 -> v3:
  * Commit message updated by adding usage.
  * R-b added.
 v3 -> v4:
  * Fix build failure due to invalid format strings for 'printf()'.
  * Rebase over latest version of cmd/mtd.c.
 v4 -> v5:
  * Implement commands from this patch as config option due to too big
final size of the uboot image.
  * R-b removed because of patch update.

 cmd/Kconfig |   7 ++
 cmd/mtd.c   | 234 
 2 files changed, 241 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 7292a150f5..832098e66e 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1366,6 +1366,13 @@ config CMD_MTD
help
  MTD commands support.
 
+config CMD_MTD_OTP
+   bool "mtd otp"
+   depends on CMD_MTD
+   select HEXDUMP
+   help
+ MTD commands for OTP access.
+
 config CMD_MUX
bool "mux"
depends on MULTIPLEXER
diff --git a/cmd/mtd.c b/cmd/mtd.c
index e63c011e79..c66105e373 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -11,6 +11,9 @@
 #include 
 #include 
 #include 
+#if CONFIG_IS_ENABLED(CMD_MTD_OTP)
+#include 
+#endif
 #include 
 #include 
 #include 
@@ -202,6 +205,221 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
return true;
 }
 
+#if CONFIG_IS_ENABLED(CMD_MTD_OTP)
+static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   off_t from;
+   size_t len;
+   bool user;
+   int ret;
+   u8 *buf;
+
+   if (argc != 5)
+   return CMD_RET_USAGE;
+
+   if (!strcmp(argv[2], "u"))
+   user = true;
+   else if (!strcmp(argv[2], "f"))
+   user = false;
+   else
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[3], NULL, 0);
+   len = simple_strtoul(argv[4], NULL, 0);
+
+   ret = CMD_RET_FAILURE;
+
+   buf = malloc(len);
+   if (!buf)
+   goto put_mtd;
+
+   printf("Reading %s OTP from 0x%lx, %zu bytes\n",
+  user ? "user" : "factory", from, len);
+
+   if (user)
+   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
+   else
+   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
+   if (ret) {
+   free(buf);
+   pr_err("OTP read failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != len)
+   pr_err("OTP read returns %zu, but %zu expected\n",
+  retlen, len);
+
+   print_hex_dump("", 0, 16, 1, buf, retlen, true);
+
+   free(buf);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   off_t from;
+   size_t len;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   len = simple_strtoul(argv[3], NULL, 0);
+
+   ret = mtd_lock_user_prot_reg(mtd, from, len);
+   if (ret) {
+   pr_err("OTP lock failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
+   char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   size_t binlen;
+   u8 *binbuf;
+   off_t from;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   binlen = strlen(argv[3]) / 2;
+
+   ret = CMD_RET_FAILURE;
+  

Re: [PATCH v4] cmd: mtd: OTP access support

2024-03-22 Thread Arseniy Krasnov



On 22.03.2024 11:17, Michael Nazzareno Trimarchi wrote:
> Hi Arseniy
> 
> On Fri, Mar 22, 2024 at 9:14 AM Arseniy Krasnov
>  wrote:
>>
>> Hi,
>>
>> On 22.03.2024 11:12, Michael Nazzareno Trimarchi wrote:
>>> Hi Arseniy
>>>
>>> On Wed, Mar 20, 2024 at 8:14 PM Arseniy Krasnov
>>>  wrote:
>>>>
>>>> Add access to OTP region. It supports info, dump, write and lock
>>>> operations. Usage example:
>>>>
>>>> 'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
>>>>  from offset 0 of device 'nand0'.
>>>>
>>>> 'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
>>>>  0x44 to offset 10 to user area of device 'nand0'.
>>>>
>>>> 'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
>>>>  from offset 0 of device 'nand0'.
>>>>
>>>> 'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.
>>>>
>>>> Signed-off-by: Arseniy Krasnov 
>>>> Reviewed-by: Michael Trimarchi 
>>>> ---
>>>>  Changelog:
>>>>  v1 -> v2:
>>>>   * Remove warning that OTP can't be erased after write.
>>>>  v2 -> v3:
>>>>   * Commit message updated by adding usage.
>>>>   * R-b added.
>>>>  v3 -> v4:
>>>>   * Fix build failure due to invalid format strings for 'printf()'.
>>>>   * Rebase over latest version of cmd/mtd.c.
>>>>
>>>>  cmd/Kconfig |   1 +
>>>>  cmd/mtd.c   | 224 
>>>>  2 files changed, 225 insertions(+)
>>>>
>>>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>>>> index 7292a150f5..f218dc6cf2 100644
>>>> --- a/cmd/Kconfig
>>>> +++ b/cmd/Kconfig
>>>> @@ -1363,6 +1363,7 @@ config CMD_MTD
>>>> bool "mtd"
>>>> depends on MTD
>>>> select MTD_PARTITIONS
>>>> +   select HEXDUMP
>>>
>>> Make those change forced in increase the size of uboot and break this build
>>>
>>> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/jobs/802378
>>>
>>> I think that those command could be optional and not forced to be
>>> included by default
>>>
>>
>> Ah, Ok:) I'll add it as config option which depends on CMD_MTD in the next 
>> version
>>
> 
> I am really glad how you interact with the mailing list ;) and how you
> are fast on feedback
> 

Sure, thanks!

> 
> Michael
> 
>> Thanks
>>
>>> Michael
>>>
>>>
>>>> help
>>>>   MTD commands support.
>>>>
>>>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>>>> index e63c011e79..2b03a85ef0 100644
>>>> --- a/cmd/mtd.c
>>>> +++ b/cmd/mtd.c
>>>> @@ -11,6 +11,7 @@
>>>>  #include 
>>>>  #include 
>>>>  #include 
>>>> +#include 
>>>>  #include 
>>>>  #include 
>>>>  #include 
>>>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct 
>>>> mtd_oob_ops *op)
>>>> return true;
>>>>  }
>>>>
>>>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>>>> +  char *const argv[])
>>>> +{
>>>> +   struct mtd_info *mtd;
>>>> +   size_t retlen;
>>>> +   off_t from;
>>>> +   size_t len;
>>>> +   bool user;
>>>> +   int ret;
>>>> +   u8 *buf;
>>>> +
>>>> +   if (argc != 5)
>>>> +   return CMD_RET_USAGE;
>>>> +
>>>> +   if (!strcmp(argv[2], "u"))
>>>> +   user = true;
>>>> +   else if (!strcmp(argv[2], "f"))
>>>> +   user = false;
>>>> +   else
>>>> +   return CMD_RET_USAGE;
>>>> +
>>>> +   mtd = get_mtd_by_name(argv[1]);
>>>> +   if (IS_ERR_OR_NULL(mtd))
>>>> +   return CMD_RET_FAILURE;
>>>> +
>>>> +   from = simple_strtoul(argv[3], NULL, 0);
>>>> +   len = simple_strtoul(argv[4], NULL, 0);
>>>> +
>>>> +   ret = CMD_RET_FAILURE;
>>>> +
>>>> +   buf = m

Re: [PATCH v4] cmd: mtd: OTP access support

2024-03-22 Thread Arseniy Krasnov
Hi,

On 22.03.2024 11:12, Michael Nazzareno Trimarchi wrote:
> Hi Arseniy
> 
> On Wed, Mar 20, 2024 at 8:14 PM Arseniy Krasnov
>  wrote:
>>
>> Add access to OTP region. It supports info, dump, write and lock
>> operations. Usage example:
>>
>> 'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
>>  from offset 0 of device 'nand0'.
>>
>> 'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
>>  0x44 to offset 10 to user area of device 'nand0'.
>>
>> 'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
>>  from offset 0 of device 'nand0'.
>>
>> 'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.
>>
>> Signed-off-by: Arseniy Krasnov 
>> Reviewed-by: Michael Trimarchi 
>> ---
>>  Changelog:
>>  v1 -> v2:
>>   * Remove warning that OTP can't be erased after write.
>>  v2 -> v3:
>>   * Commit message updated by adding usage.
>>   * R-b added.
>>  v3 -> v4:
>>   * Fix build failure due to invalid format strings for 'printf()'.
>>   * Rebase over latest version of cmd/mtd.c.
>>
>>  cmd/Kconfig |   1 +
>>  cmd/mtd.c   | 224 
>>  2 files changed, 225 insertions(+)
>>
>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>> index 7292a150f5..f218dc6cf2 100644
>> --- a/cmd/Kconfig
>> +++ b/cmd/Kconfig
>> @@ -1363,6 +1363,7 @@ config CMD_MTD
>> bool "mtd"
>> depends on MTD
>> select MTD_PARTITIONS
>> +   select HEXDUMP
> 
> Make those change forced in increase the size of uboot and break this build
> 
> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/jobs/802378
> 
> I think that those command could be optional and not forced to be
> included by default
> 

Ah, Ok:) I'll add it as config option which depends on CMD_MTD in the next 
version

Thanks

> Michael
> 
> 
>> help
>>   MTD commands support.
>>
>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>> index e63c011e79..2b03a85ef0 100644
>> --- a/cmd/mtd.c
>> +++ b/cmd/mtd.c
>> @@ -11,6 +11,7 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>>  #include 
>>  #include 
>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops 
>> *op)
>> return true;
>>  }
>>
>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>> +  char *const argv[])
>> +{
>> +   struct mtd_info *mtd;
>> +   size_t retlen;
>> +   off_t from;
>> +   size_t len;
>> +   bool user;
>> +   int ret;
>> +   u8 *buf;
>> +
>> +   if (argc != 5)
>> +   return CMD_RET_USAGE;
>> +
>> +   if (!strcmp(argv[2], "u"))
>> +   user = true;
>> +   else if (!strcmp(argv[2], "f"))
>> +   user = false;
>> +   else
>> +   return CMD_RET_USAGE;
>> +
>> +   mtd = get_mtd_by_name(argv[1]);
>> +   if (IS_ERR_OR_NULL(mtd))
>> +   return CMD_RET_FAILURE;
>> +
>> +   from = simple_strtoul(argv[3], NULL, 0);
>> +   len = simple_strtoul(argv[4], NULL, 0);
>> +
>> +   ret = CMD_RET_FAILURE;
>> +
>> +   buf = malloc(len);
>> +   if (!buf)
>> +   goto put_mtd;
>> +
>> +   printf("Reading %s OTP from 0x%lx, %zu bytes\n",
>> +  user ? "user" : "factory", from, len);
>> +
>> +   if (user)
>> +   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
>> +   else
>> +   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
>> +   if (ret) {
>> +   free(buf);
>> +   pr_err("OTP read failed: %d\n", ret);
>> +   ret = CMD_RET_FAILURE;
>> +   goto put_mtd;
>> +   }
>> +
>> +   if (retlen != len)
>> +   pr_err("OTP read returns %zu, but %zu expected\n",
>> +  retlen, len);
>> +
>> +   print_hex_dump("", 0, 16, 1, buf, retlen, true);
>> +
>> +   free(buf);
>> +
>> +   ret = CMD_RET_SUCCESS;
>> +
>> +put_mtd:
>> +   put_mtd_device(mtd);
>> +
>> +   return ret;
>> +}
>> +
>> +static int do_mtd_otp_lock(struct cmd_tbl 

Re: [PATCH v3] cmd: mtd: OTP access support

2024-03-20 Thread Arseniy Krasnov
Hello

On 20.03.2024 21:01, Michael Nazzareno Trimarchi wrote:
> Hi
> 
> On Wed, Mar 13, 2024 at 8:27 AM Arseniy Krasnov
>  wrote:
>>
>> Add access to OTP region. It supports info, dump, write and lock
>> operations. Usage example:
>>
>> 'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
>>  from offset 0 of device 'nand0'.
>>
>> 'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
>>  0x44 to offset 10 to user area of device 'nand0'.
>>
>> 'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
>>  from offset 0 of device 'nand0'.
>>
>> 'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.
>>
>> Signed-off-by: Arseniy Krasnov 
>> Reviewed-by: Michael Trimarchi 
>> ---
>>  Changelog:
>>  v1 -> v2:
>>   * Remove warning that OTP can't be erased after write.
>>  v2 -> v3:
>>   * Commit message updated by adding usage.
>>   * R-b added.
>>
> 
> Your patch build fail
> 
> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/jobs/801919
> 
> Michael

Sorry, yes, fixed in v4

Thanks

> 
>>  cmd/Kconfig |   1 +
>>  cmd/mtd.c   | 224 
>>  2 files changed, 225 insertions(+)
>>
>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>> index 90e4ef93e0..c47523a03b 100644
>> --- a/cmd/Kconfig
>> +++ b/cmd/Kconfig
>> @@ -1354,6 +1354,7 @@ config CMD_MTD
>> bool "mtd"
>> depends on MTD
>> select MTD_PARTITIONS
>> +   select HEXDUMP
>> help
>>   MTD commands support.
>>
>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>> index eb6e2d6892..1ab69b108b 100644
>> --- a/cmd/mtd.c
>> +++ b/cmd/mtd.c
>> @@ -11,6 +11,7 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>>  #include 
>>  #include 
>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops 
>> *op)
>> return true;
>>  }
>>
>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>> +  char *const argv[])
>> +{
>> +   struct mtd_info *mtd;
>> +   size_t retlen;
>> +   off_t from;
>> +   size_t len;
>> +   bool user;
>> +   int ret;
>> +   u8 *buf;
>> +
>> +   if (argc != 5)
>> +   return CMD_RET_USAGE;
>> +
>> +   if (!strcmp(argv[2], "u"))
>> +   user = true;
>> +   else if (!strcmp(argv[2], "f"))
>> +   user = false;
>> +   else
>> +   return CMD_RET_USAGE;
>> +
>> +   mtd = get_mtd_by_name(argv[1]);
>> +   if (IS_ERR_OR_NULL(mtd))
>> +   return CMD_RET_FAILURE;
>> +
>> +   from = simple_strtoul(argv[3], NULL, 0);
>> +   len = simple_strtoul(argv[4], NULL, 0);
>> +
>> +   ret = CMD_RET_FAILURE;
>> +
>> +   buf = malloc(len);
>> +   if (!buf)
>> +   goto put_mtd;
>> +
>> +   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
>> +  user ? "user" : "factory", from, len);
>> +
>> +   if (user)
>> +   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
>> +   else
>> +   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
>> +   if (ret) {
>> +   free(buf);
>> +   pr_err("OTP read failed: %d\n", ret);
>> +   ret = CMD_RET_FAILURE;
>> +   goto put_mtd;
>> +   }
>> +
>> +   if (retlen != len)
>> +   pr_err("OTP read returns %zu, but %zu expected\n",
>> +  retlen, len);
>> +
>> +   print_hex_dump("", 0, 16, 1, buf, retlen, true);
>> +
>> +   free(buf);
>> +
>> +   ret = CMD_RET_SUCCESS;
>> +
>> +put_mtd:
>> +   put_mtd_device(mtd);
>> +
>> +   return ret;
>> +}
>> +
>> +static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
>> +  char *const argv[])
>> +{
>> +   struct mtd_info *mtd;
>> +   off_t from;
>> +   size_t len;
>> +   int ret;
>> +
>> +   if (argc != 4)
>> +   return CMD_RET_USAGE;
>> +
>> +   mtd = get_mtd_by_name(argv[1]);
>> +

[PATCH v4] cmd: mtd: OTP access support

2024-03-20 Thread Arseniy Krasnov
Add access to OTP region. It supports info, dump, write and lock
operations. Usage example:

'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
 0x44 to offset 10 to user area of device 'nand0'.

'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.

Signed-off-by: Arseniy Krasnov 
Reviewed-by: Michael Trimarchi 
---
 Changelog:
 v1 -> v2:
  * Remove warning that OTP can't be erased after write.
 v2 -> v3:
  * Commit message updated by adding usage.
  * R-b added.
 v3 -> v4:
  * Fix build failure due to invalid format strings for 'printf()'.
  * Rebase over latest version of cmd/mtd.c.

 cmd/Kconfig |   1 +
 cmd/mtd.c   | 224 
 2 files changed, 225 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 7292a150f5..f218dc6cf2 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1363,6 +1363,7 @@ config CMD_MTD
bool "mtd"
depends on MTD
select MTD_PARTITIONS
+   select HEXDUMP
help
  MTD commands support.
 
diff --git a/cmd/mtd.c b/cmd/mtd.c
index e63c011e79..2b03a85ef0 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
return true;
 }
 
+static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   off_t from;
+   size_t len;
+   bool user;
+   int ret;
+   u8 *buf;
+
+   if (argc != 5)
+   return CMD_RET_USAGE;
+
+   if (!strcmp(argv[2], "u"))
+   user = true;
+   else if (!strcmp(argv[2], "f"))
+   user = false;
+   else
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[3], NULL, 0);
+   len = simple_strtoul(argv[4], NULL, 0);
+
+   ret = CMD_RET_FAILURE;
+
+   buf = malloc(len);
+   if (!buf)
+   goto put_mtd;
+
+   printf("Reading %s OTP from 0x%lx, %zu bytes\n",
+  user ? "user" : "factory", from, len);
+
+   if (user)
+   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
+   else
+   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
+   if (ret) {
+   free(buf);
+   pr_err("OTP read failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != len)
+   pr_err("OTP read returns %zu, but %zu expected\n",
+  retlen, len);
+
+   print_hex_dump("", 0, 16, 1, buf, retlen, true);
+
+   free(buf);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   off_t from;
+   size_t len;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   len = simple_strtoul(argv[3], NULL, 0);
+
+   ret = mtd_lock_user_prot_reg(mtd, from, len);
+   if (ret) {
+   pr_err("OTP lock failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
+   char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   size_t binlen;
+   u8 *binbuf;
+   off_t from;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   binlen = strlen(argv[3]) / 2;
+
+   ret = CMD_RET_FAILURE;
+   binbuf = malloc(binlen);
+   if (!binbuf)
+   goto put_mtd;
+
+   hex2bin(binbuf, argv[3], binlen);
+
+   printf("Will write:\n");
+
+   print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
+
+   printf("to 0x%lx\n", from);
+
+   printf("Continue (y/n)?\n");
+
+ 

Re: [PATCH v2] cmd: mtd: OTP access support

2024-03-13 Thread Arseniy Krasnov



On 13.03.2024 09:48, Michael Nazzareno Trimarchi wrote:
> Hi
> 
> On Wed, Mar 13, 2024 at 7:43 AM Arseniy Krasnov
>  wrote:
>>
>> Sorry, please ping
>>
>> Thanks, Arseniy
>>
>>
>> On 11.02.2024 02:16, Arseniy Krasnov wrote:
>>> Sorry, pls ping
>>>
>>> Thanks, Arseniy
>>>
>>> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>>>> Sorry, pls ping
>>>>
>>>> Thanks, Arseniy
>>>>
>>>> On 20.12.2023 22:36, Arseniy Krasnov wrote:
>>>>> Add access to OTP region. It supports info, dump, write and lock
>>>>> operations.
>>>>>
>>>>> Signed-off-by: Arseniy Krasnov 
>>>>> ---
> 
> Please extend the commit message with some example of the usage otherwise

Done in v3

Thanks, Arseniy

> 
> Reviewed-by: Michael Trimarchi 
> 
>>>>>  Changelog:
>>>>>  v1 -> v2:
>>>>>   * Remove warning that OTP can't be erased after write.
>>>>>
>>>>>  cmd/Kconfig |   1 +
>>>>>  cmd/mtd.c   | 224 
>>>>>  2 files changed, 225 insertions(+)
>>>>>
>>>>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>>>>> index 90e4ef93e0..c47523a03b 100644
>>>>> --- a/cmd/Kconfig
>>>>> +++ b/cmd/Kconfig
>>>>> @@ -1354,6 +1354,7 @@ config CMD_MTD
>>>>> bool "mtd"
>>>>> depends on MTD
>>>>> select MTD_PARTITIONS
>>>>> +   select HEXDUMP
>>>>> help
>>>>>   MTD commands support.
>>>>>
>>>>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>>>>> index eb6e2d6892..1ab69b108b 100644
>>>>> --- a/cmd/mtd.c
>>>>> +++ b/cmd/mtd.c
>>>>> @@ -11,6 +11,7 @@
>>>>>  #include 
>>>>>  #include 
>>>>>  #include 
>>>>> +#include 
>>>>>  #include 
>>>>>  #include 
>>>>>  #include 
>>>>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct 
>>>>> mtd_oob_ops *op)
>>>>> return true;
>>>>>  }
>>>>>
>>>>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>>>>> +  char *const argv[])
>>>>> +{
>>>>> +   struct mtd_info *mtd;
>>>>> +   size_t retlen;
>>>>> +   off_t from;
>>>>> +   size_t len;
>>>>> +   bool user;
>>>>> +   int ret;
>>>>> +   u8 *buf;
>>>>> +
>>>>> +   if (argc != 5)
>>>>> +   return CMD_RET_USAGE;
>>>>> +
>>>>> +   if (!strcmp(argv[2], "u"))
>>>>> +   user = true;
>>>>> +   else if (!strcmp(argv[2], "f"))
>>>>> +   user = false;
>>>>> +   else
>>>>> +   return CMD_RET_USAGE;
>>>>> +
>>>>> +   mtd = get_mtd_by_name(argv[1]);
>>>>> +   if (IS_ERR_OR_NULL(mtd))
>>>>> +   return CMD_RET_FAILURE;
>>>>> +
>>>>> +   from = simple_strtoul(argv[3], NULL, 0);
>>>>> +   len = simple_strtoul(argv[4], NULL, 0);
>>>>> +
>>>>> +   ret = CMD_RET_FAILURE;
>>>>> +
>>>>> +   buf = malloc(len);
>>>>> +   if (!buf)
>>>>> +   goto put_mtd;
>>>>> +
>>>>> +   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
>>>>> +  user ? "user" : "factory", from, len);
>>>>> +
>>>>> +   if (user)
>>>>> +   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
>>>>> +   else
>>>>> +   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
>>>>> +   if (ret) {
>>>>> +   free(buf);
>>>>> +   pr_err("OTP read failed: %d\n", ret);
>>>>> +   ret = CMD_RET_FAILURE;
>>>>> +   goto put_mtd;
>>>>> +   }
>>>>> +
>>>>> +   if (retlen != len)
>>>>> +   pr_err("OTP read returns %zu, but %zu expected\n",
>>>>> +  retlen, len);
>>>>> +
>>>>> +   prin

[PATCH v3] cmd: mtd: OTP access support

2024-03-13 Thread Arseniy Krasnov
Add access to OTP region. It supports info, dump, write and lock
operations. Usage example:

'mtd otpread nand0 u 0 1024' - dump 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpwrite nand0 10 11223344' - write binary data 0x11, 0x22, 0x33,
 0x44 to offset 10 to user area of device 'nand0'.

'mtd otplock nand0 0 1024' - lock 1024 bytes of user area starting
 from offset 0 of device 'nand0'.

'mtd otpinfo nand0 f' - show info about factory area of device 'nand0'.

Signed-off-by: Arseniy Krasnov 
Reviewed-by: Michael Trimarchi 
---
 Changelog:
 v1 -> v2:
  * Remove warning that OTP can't be erased after write.
 v2 -> v3:
  * Commit message updated by adding usage.
  * R-b added.

 cmd/Kconfig |   1 +
 cmd/mtd.c   | 224 
 2 files changed, 225 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 90e4ef93e0..c47523a03b 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1354,6 +1354,7 @@ config CMD_MTD
bool "mtd"
depends on MTD
select MTD_PARTITIONS
+   select HEXDUMP
help
  MTD commands support.
 
diff --git a/cmd/mtd.c b/cmd/mtd.c
index eb6e2d6892..1ab69b108b 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
return true;
 }
 
+static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   off_t from;
+   size_t len;
+   bool user;
+   int ret;
+   u8 *buf;
+
+   if (argc != 5)
+   return CMD_RET_USAGE;
+
+   if (!strcmp(argv[2], "u"))
+   user = true;
+   else if (!strcmp(argv[2], "f"))
+   user = false;
+   else
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[3], NULL, 0);
+   len = simple_strtoul(argv[4], NULL, 0);
+
+   ret = CMD_RET_FAILURE;
+
+   buf = malloc(len);
+   if (!buf)
+   goto put_mtd;
+
+   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
+  user ? "user" : "factory", from, len);
+
+   if (user)
+   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
+   else
+   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
+   if (ret) {
+   free(buf);
+   pr_err("OTP read failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != len)
+   pr_err("OTP read returns %zu, but %zu expected\n",
+  retlen, len);
+
+   print_hex_dump("", 0, 16, 1, buf, retlen, true);
+
+   free(buf);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   off_t from;
+   size_t len;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   len = simple_strtoul(argv[3], NULL, 0);
+
+   ret = mtd_lock_user_prot_reg(mtd, from, len);
+   if (ret) {
+   pr_err("OTP lock failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
+   char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   size_t binlen;
+   u8 *binbuf;
+   off_t from;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   binlen = strlen(argv[3]) / 2;
+
+   ret = CMD_RET_FAILURE;
+   binbuf = malloc(binlen);
+   if (!binbuf)
+   goto put_mtd;
+
+   hex2bin(binbuf, argv[3], binlen);
+
+   printf("Will write:\n");
+
+   print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
+
+   printf("to 0x%zx\n", from);
+
+   printf("Continue (y/n)?\n");
+
+   if (confirm_yesno() != 1) {
+   pr_err("OTP write canceled\n");
+   ret = CMD_RET_SUCCESS;
+ 

Re: [PATCH v2] cmd: mtd: OTP access support

2024-03-13 Thread Arseniy Krasnov
Sorry, please ping

Thanks, Arseniy


On 11.02.2024 02:16, Arseniy Krasnov wrote:
> Sorry, pls ping
> 
> Thanks, Arseniy
> 
> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>> Sorry, pls ping
>>
>> Thanks, Arseniy
>>
>> On 20.12.2023 22:36, Arseniy Krasnov wrote:
>>> Add access to OTP region. It supports info, dump, write and lock
>>> operations.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>>  Changelog:
>>>  v1 -> v2:
>>>   * Remove warning that OTP can't be erased after write.
>>>
>>>  cmd/Kconfig |   1 +
>>>  cmd/mtd.c   | 224 
>>>  2 files changed, 225 insertions(+)
>>>
>>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>>> index 90e4ef93e0..c47523a03b 100644
>>> --- a/cmd/Kconfig
>>> +++ b/cmd/Kconfig
>>> @@ -1354,6 +1354,7 @@ config CMD_MTD
>>> bool "mtd"
>>> depends on MTD
>>> select MTD_PARTITIONS
>>> +   select HEXDUMP
>>> help
>>>   MTD commands support.
>>>  
>>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>>> index eb6e2d6892..1ab69b108b 100644
>>> --- a/cmd/mtd.c
>>> +++ b/cmd/mtd.c
>>> @@ -11,6 +11,7 @@
>>>  #include 
>>>  #include 
>>>  #include 
>>> +#include 
>>>  #include 
>>>  #include 
>>>  #include 
>>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops 
>>> *op)
>>> return true;
>>>  }
>>>  
>>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>>> +  char *const argv[])
>>> +{
>>> +   struct mtd_info *mtd;
>>> +   size_t retlen;
>>> +   off_t from;
>>> +   size_t len;
>>> +   bool user;
>>> +   int ret;
>>> +   u8 *buf;
>>> +
>>> +   if (argc != 5)
>>> +   return CMD_RET_USAGE;
>>> +
>>> +   if (!strcmp(argv[2], "u"))
>>> +   user = true;
>>> +   else if (!strcmp(argv[2], "f"))
>>> +   user = false;
>>> +   else
>>> +   return CMD_RET_USAGE;
>>> +
>>> +   mtd = get_mtd_by_name(argv[1]);
>>> +   if (IS_ERR_OR_NULL(mtd))
>>> +   return CMD_RET_FAILURE;
>>> +
>>> +   from = simple_strtoul(argv[3], NULL, 0);
>>> +   len = simple_strtoul(argv[4], NULL, 0);
>>> +
>>> +   ret = CMD_RET_FAILURE;
>>> +
>>> +   buf = malloc(len);
>>> +   if (!buf)
>>> +   goto put_mtd;
>>> +
>>> +   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
>>> +  user ? "user" : "factory", from, len);
>>> +
>>> +   if (user)
>>> +   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
>>> +   else
>>> +   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
>>> +   if (ret) {
>>> +   free(buf);
>>> +   pr_err("OTP read failed: %d\n", ret);
>>> +   ret = CMD_RET_FAILURE;
>>> +   goto put_mtd;
>>> +   }
>>> +
>>> +   if (retlen != len)
>>> +   pr_err("OTP read returns %zu, but %zu expected\n",
>>> +  retlen, len);
>>> +
>>> +   print_hex_dump("", 0, 16, 1, buf, retlen, true);
>>> +
>>> +   free(buf);
>>> +
>>> +   ret = CMD_RET_SUCCESS;
>>> +
>>> +put_mtd:
>>> +   put_mtd_device(mtd);
>>> +
>>> +   return ret;
>>> +}
>>> +
>>> +static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
>>> +  char *const argv[])
>>> +{
>>> +   struct mtd_info *mtd;
>>> +   off_t from;
>>> +   size_t len;
>>> +   int ret;
>>> +
>>> +   if (argc != 4)
>>> +   return CMD_RET_USAGE;
>>> +
>>> +   mtd = get_mtd_by_name(argv[1]);
>>> +   if (IS_ERR_OR_NULL(mtd))
>>> +   return CMD_RET_FAILURE;
>>> +
>>> +   from = simple_strtoul(argv[2], NULL, 0);
>>> +   len = simple_strtoul(argv[3], NULL, 0);
>>> +
>>> +   ret = mtd_lock_user_prot_reg(mtd, from, len);
>>> +   if (ret) {
>>> +   pr_err("OTP lock failed: %d\n", ret);
>>> +   ret = CMD_RET_FAILURE;
>>

Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-03-13 Thread Arseniy Krasnov
Sorry, please ping

Thanks, Arseniy

On 11.02.2024 02:16, Arseniy Krasnov wrote:
> Sorry, pls ping
> 
> Thanks, Arseniy
> 
> On 08.01.2024 21:33, Arseniy Krasnov wrote:
>> Sorry, pls ping
>>
>> Thanks, Arseniy


Re: [PATCH v2] cmd: mtd: OTP access support

2024-02-10 Thread Arseniy Krasnov
Sorry, pls ping

Thanks, Arseniy

On 08.01.2024 21:33, Arseniy Krasnov wrote:
> Sorry, pls ping
> 
> Thanks, Arseniy
> 
> On 20.12.2023 22:36, Arseniy Krasnov wrote:
>> Add access to OTP region. It supports info, dump, write and lock
>> operations.
>>
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>  Changelog:
>>  v1 -> v2:
>>   * Remove warning that OTP can't be erased after write.
>>
>>  cmd/Kconfig |   1 +
>>  cmd/mtd.c   | 224 
>>  2 files changed, 225 insertions(+)
>>
>> diff --git a/cmd/Kconfig b/cmd/Kconfig
>> index 90e4ef93e0..c47523a03b 100644
>> --- a/cmd/Kconfig
>> +++ b/cmd/Kconfig
>> @@ -1354,6 +1354,7 @@ config CMD_MTD
>>  bool "mtd"
>>  depends on MTD
>>  select MTD_PARTITIONS
>> +select HEXDUMP
>>  help
>>MTD commands support.
>>  
>> diff --git a/cmd/mtd.c b/cmd/mtd.c
>> index eb6e2d6892..1ab69b108b 100644
>> --- a/cmd/mtd.c
>> +++ b/cmd/mtd.c
>> @@ -11,6 +11,7 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>>  #include 
>>  #include 
>> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops 
>> *op)
>>  return true;
>>  }
>>  
>> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
>> +   char *const argv[])
>> +{
>> +struct mtd_info *mtd;
>> +size_t retlen;
>> +off_t from;
>> +size_t len;
>> +bool user;
>> +int ret;
>> +u8 *buf;
>> +
>> +if (argc != 5)
>> +return CMD_RET_USAGE;
>> +
>> +if (!strcmp(argv[2], "u"))
>> +user = true;
>> +else if (!strcmp(argv[2], "f"))
>> +user = false;
>> +else
>> +return CMD_RET_USAGE;
>> +
>> +mtd = get_mtd_by_name(argv[1]);
>> +if (IS_ERR_OR_NULL(mtd))
>> +return CMD_RET_FAILURE;
>> +
>> +from = simple_strtoul(argv[3], NULL, 0);
>> +len = simple_strtoul(argv[4], NULL, 0);
>> +
>> +ret = CMD_RET_FAILURE;
>> +
>> +buf = malloc(len);
>> +if (!buf)
>> +goto put_mtd;
>> +
>> +printf("Reading %s OTP from 0x%lx, %lu bytes\n",
>> +   user ? "user" : "factory", from, len);
>> +
>> +if (user)
>> +ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
>> +else
>> +ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
>> +if (ret) {
>> +free(buf);
>> +pr_err("OTP read failed: %d\n", ret);
>> +ret = CMD_RET_FAILURE;
>> +goto put_mtd;
>> +}
>> +
>> +if (retlen != len)
>> +pr_err("OTP read returns %zu, but %zu expected\n",
>> +   retlen, len);
>> +
>> +print_hex_dump("", 0, 16, 1, buf, retlen, true);
>> +
>> +free(buf);
>> +
>> +ret = CMD_RET_SUCCESS;
>> +
>> +put_mtd:
>> +put_mtd_device(mtd);
>> +
>> +return ret;
>> +}
>> +
>> +static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
>> +   char *const argv[])
>> +{
>> +struct mtd_info *mtd;
>> +off_t from;
>> +size_t len;
>> +int ret;
>> +
>> +if (argc != 4)
>> +return CMD_RET_USAGE;
>> +
>> +mtd = get_mtd_by_name(argv[1]);
>> +if (IS_ERR_OR_NULL(mtd))
>> +return CMD_RET_FAILURE;
>> +
>> +from = simple_strtoul(argv[2], NULL, 0);
>> +len = simple_strtoul(argv[3], NULL, 0);
>> +
>> +ret = mtd_lock_user_prot_reg(mtd, from, len);
>> +if (ret) {
>> +pr_err("OTP lock failed: %d\n", ret);
>> +ret = CMD_RET_FAILURE;
>> +goto put_mtd;
>> +}
>> +
>> +ret = CMD_RET_SUCCESS;
>> +
>> +put_mtd:
>> +put_mtd_device(mtd);
>> +
>> +return ret;
>> +}
>> +
>> +static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
>> +char *const argv[])
>> +{
>> +struct mtd_info *mtd;
>> +size_t retlen;
>> +size_t binlen;
>> +u8 *binbuf;
>> +off_t from;
>> +int ret;
>&

Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-02-10 Thread Arseniy Krasnov
Sorry, pls ping

Thanks, Arseniy

On 08.01.2024 21:33, Arseniy Krasnov wrote:
> Sorry, pls ping
> 
> Thanks, Arseniy


[PATCH v3] mtd: rawnand: Meson NAND controller support

2024-02-10 Thread Arseniy Krasnov
Basic support for Amlogic Meson NAND controller on AXG. This version
works at only first EDO mode.

Based on Linux version 6.7.0-rc4.

Signed-off-by: Arseniy Krasnov 
---
 Changelog:
 v1 -> v2:
  * Update commit message with 'Based on Linux ...'.
  * Add Linux driver author to .c file header.
  * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
'NFC_DEFAULT_BUS_TIMING'.
  * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
 v2 -> v3:
  * Update commit message about EDO mode limitation.
  * Fix scrambling bit value in CMDRWGEN macro.
  * Add scrambling support for reading.

 drivers/mtd/nand/raw/Kconfig  |9 +
 drivers/mtd/nand/raw/Makefile |1 +
 drivers/mtd/nand/raw/meson_nand.c | 1248 +
 3 files changed, 1258 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/meson_nand.c

diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index 46a1460746..e946305ccc 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -479,6 +479,15 @@ config NAND_ARASAN
  controller. This uses the hardware ECC for read and
  write operations.
 
+config NAND_MESON
+   bool "Meson NAND support"
+   select SYS_NAND_SELF_INIT
+   depends on DM_MTD && ARCH_MESON
+   imply CMD_NAND
+   help
+ This enables Nand driver support for Meson raw NAND flash
+ controller.
+
 config NAND_MXC
bool "MXC NAND support"
depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index add2b4cf65..5b4efd52c9 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
 obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
 obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
 obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
+obj-$(CONFIG_NAND_MESON) += meson_nand.o
 obj-$(CONFIG_NAND_MXC) += mxc_nand.o
 obj-$(CONFIG_NAND_MXS) += mxs_nand.o
 obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
diff --git a/drivers/mtd/nand/raw/meson_nand.c 
b/drivers/mtd/nand/raw/meson_nand.c
new file mode 100644
index 00..5d411c4594
--- /dev/null
+++ b/drivers/mtd/nand/raw/meson_nand.c
@@ -0,0 +1,1248 @@
+// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
+/*
+ * Amlogic Meson Nand Flash Controller Driver
+ *
+ * Copyright (c) 2018 Amlogic, inc.
+ * Author: Liang Yang 
+ *
+ * Copyright (c) 2023 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define NFC_CMD_IDLE   (0xc << 14)
+#define NFC_CMD_CLE(0x5 << 14)
+#define NFC_CMD_ALE(0x6 << 14)
+#define NFC_CMD_DWR(0x4 << 14)
+#define NFC_CMD_DRD(0x8 << 14)
+#define NFC_CMD_ADL((0 << 16) | (3 << 20))
+#define NFC_CMD_ADH((1 << 16) | (3 << 20))
+#define NFC_CMD_AIL((2 << 16) | (3 << 20))
+#define NFC_CMD_AIH((3 << 16) | (3 << 20))
+#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
+#define NFC_CMD_M2N((0 << 17) | (2 << 20))
+#define NFC_CMD_N2M((1 << 17) | (2 << 20))
+#define NFC_CMD_RB BIT(20)
+#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
+#define NFC_CMD_SCRAMBLER_DISABLE  0
+#define NFC_CMD_SHORTMODE_DISABLE  0
+#define NFC_CMD_RB_INT BIT(14)
+#define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
+
+#define NFC_CMD_GET_SIZE(x)(((x) >> 22) & GENMASK(4, 0))
+
+#define NFC_REG_CMD0x00
+#define NFC_REG_CFG0x04
+#define NFC_REG_DADR   0x08
+#define NFC_REG_IADR   0x0c
+#define NFC_REG_BUF0x10
+#define NFC_REG_INFO   0x14
+#define NFC_REG_DC 0x18
+#define NFC_REG_ADR0x1c
+#define NFC_REG_DL 0x20
+#define NFC_REG_DH 0x24
+#define NFC_REG_CADR   0x28
+#define NFC_REG_SADR   0x2c
+#define NFC_REG_PINS   0x30
+#define NFC_REG_VER0x38
+
+#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)  \
+   (   \
+   (cmd_dir)   |   \
+   (ran)   |   \
+   ((bch) << 14)   |   \
+   ((short_mode) << 13)|   \
+   (((page_size) & 0x7f) << 6) | 

Re: [PATCH v2] mtd: rawnand: Meson NAND controller support

2024-02-04 Thread Arseniy Krasnov



On 05.02.2024 10:00, Michael Nazzareno Trimarchi wrote:
> Hi
> 
> Il lun 5 feb 2024, 07:43 Arseniy Krasnov  ha
> scritto:
> 
>> Hi, sorry, but pls, ping :)
>>
>> On 15.01.2024 09:01, Arseniy Krasnov wrote:
>>> Hi, thanks for review! Two questions below...
>>>
>>> On 09.01.2024 11:42, Michael Nazzareno Trimarchi wrote:
>>>> Hi Arseniy
>>>>
>>>>
>>>> On Fri, Dec 15, 2023 at 1:32 PM Arseniy Krasnov
>>>>  wrote:
>>>>>
>>>>> Basic support for Amlogic Meson NAND controller on AXG.
>>>>>
>>>>> Based on Linux version 6.7.0-rc4.
>>>>>
>>>>> Signed-off-by: Arseniy Krasnov 
>>>>> ---
>>>>>  Changelog:
>>>>>  v1 -> v2:
>>>>>   * Update commit message with 'Based on Linux ...'.
>>>>>   * Add Linux driver author to .c file header.
>>>>>   * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
>>>>> 'NFC_DEFAULT_BUS_TIMING'.
>>>>>   * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
>>>>>
>>>>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>>>>  drivers/mtd/nand/raw/Makefile |1 +
>>>>>  drivers/mtd/nand/raw/meson_nand.c | 1241 +
>>>>>  3 files changed, 1251 insertions(+)
>>>>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>>>>
>>>>> diff --git a/drivers/mtd/nand/raw/Kconfig
>> b/drivers/mtd/nand/raw/Kconfig
>>>>> index d624589a89..7b7b0226ab 100644
>>>>> --- a/drivers/mtd/nand/raw/Kconfig
>>>>> +++ b/drivers/mtd/nand/raw/Kconfig
>>>>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>>>>   controller. This uses the hardware ECC for read and
>>>>>   write operations.
>>>>>
>>>>> +config NAND_MESON
>>>>> +   bool "Meson NAND support"
>>>>> +   select SYS_NAND_SELF_INIT
>>>>> +   depends on DM_MTD && ARCH_MESON
>>>>> +   imply CMD_NAND
>>>>> +   help
>>>>> + This enables Nand driver support for Meson raw NAND flash
>>>>> + controller.
>>>>> +
>>>>>  config NAND_MXC
>>>>> bool "MXC NAND support"
>>>>> depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>>>>> diff --git a/drivers/mtd/nand/raw/Makefile
>> b/drivers/mtd/nand/raw/Makefile
>>>>> index add2b4cf65..5b4efd52c9 100644
>>>>> --- a/drivers/mtd/nand/raw/Makefile
>>>>> +++ b/drivers/mtd/nand/raw/Makefile
>>>>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>>>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>>>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>>>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>>>>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>>>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>>>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>>>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>>>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c
>> b/drivers/mtd/nand/raw/meson_nand.c
>>>>> new file mode 100644
>>>>> index 00..a6dbe99d84
>>>>> --- /dev/null
>>>>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>>>>> @@ -0,0 +1,1241 @@
>>>>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>>>>> +/*
>>>>> + * Amlogic Meson Nand Flash Controller Driver
>>>>> + *
>>>>> + * Copyright (c) 2018 Amlogic, inc.
>>>>> + * Author: Liang Yang 
>>>>> + *
>>>>> + * Copyright (c) 2023 SaluteDevices, Inc.
>>>>> + * Author: Arseniy Krasnov 
>>>>> + */
>>>>> +
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +#include 
>>>>> +
>>>>> +#define NFC_CMD_IDLE   (0xc << 14)
>>&g

Re: [PATCH v2] mtd: rawnand: Meson NAND controller support

2024-02-04 Thread Arseniy Krasnov
Hi, sorry, but pls, ping :)

On 15.01.2024 09:01, Arseniy Krasnov wrote:
> Hi, thanks for review! Two questions below...
> 
> On 09.01.2024 11:42, Michael Nazzareno Trimarchi wrote:
>> Hi Arseniy
>>
>>
>> On Fri, Dec 15, 2023 at 1:32 PM Arseniy Krasnov
>>  wrote:
>>>
>>> Basic support for Amlogic Meson NAND controller on AXG.
>>>
>>> Based on Linux version 6.7.0-rc4.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>>  Changelog:
>>>  v1 -> v2:
>>>   * Update commit message with 'Based on Linux ...'.
>>>   * Add Linux driver author to .c file header.
>>>   * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
>>> 'NFC_DEFAULT_BUS_TIMING'.
>>>   * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
>>>
>>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>>  drivers/mtd/nand/raw/Makefile |1 +
>>>  drivers/mtd/nand/raw/meson_nand.c | 1241 +
>>>  3 files changed, 1251 insertions(+)
>>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>>
>>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>>> index d624589a89..7b7b0226ab 100644
>>> --- a/drivers/mtd/nand/raw/Kconfig
>>> +++ b/drivers/mtd/nand/raw/Kconfig
>>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>>   controller. This uses the hardware ECC for read and
>>>   write operations.
>>>
>>> +config NAND_MESON
>>> +   bool "Meson NAND support"
>>> +   select SYS_NAND_SELF_INIT
>>> +   depends on DM_MTD && ARCH_MESON
>>> +   imply CMD_NAND
>>> +   help
>>> + This enables Nand driver support for Meson raw NAND flash
>>> + controller.
>>> +
>>>  config NAND_MXC
>>> bool "MXC NAND support"
>>> depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>>> index add2b4cf65..5b4efd52c9 100644
>>> --- a/drivers/mtd/nand/raw/Makefile
>>> +++ b/drivers/mtd/nand/raw/Makefile
>>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
>>> b/drivers/mtd/nand/raw/meson_nand.c
>>> new file mode 100644
>>> index 00..a6dbe99d84
>>> --- /dev/null
>>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>>> @@ -0,0 +1,1241 @@
>>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>>> +/*
>>> + * Amlogic Meson Nand Flash Controller Driver
>>> + *
>>> + * Copyright (c) 2018 Amlogic, inc.
>>> + * Author: Liang Yang 
>>> + *
>>> + * Copyright (c) 2023 SaluteDevices, Inc.
>>> + * Author: Arseniy Krasnov 
>>> + */
>>> +
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +
>>> +#define NFC_CMD_IDLE   (0xc << 14)
>>> +#define NFC_CMD_CLE(0x5 << 14)
>>> +#define NFC_CMD_ALE(0x6 << 14)
>>> +#define NFC_CMD_DWR(0x4 << 14)
>>> +#define NFC_CMD_DRD(0x8 << 14)
>>> +#define NFC_CMD_ADL((0 << 16) | (3 << 20))
>>> +#define NFC_CMD_ADH((1 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIL((2 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIH((3 << 16) | (3 << 20))
>>> +#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
>>> +#define NFC_CMD_M2N((0 << 17) | (2 << 20))
>>> +#define NFC_CMD_N2M((1 << 1

Re: [PATCH v2] mtd: rawnand: Meson NAND controller support

2024-01-14 Thread Arseniy Krasnov
Hi, thanks for review! Two questions below...

On 09.01.2024 11:42, Michael Nazzareno Trimarchi wrote:
> Hi Arseniy
> 
> 
> On Fri, Dec 15, 2023 at 1:32 PM Arseniy Krasnov
>  wrote:
>>
>> Basic support for Amlogic Meson NAND controller on AXG.
>>
>> Based on Linux version 6.7.0-rc4.
>>
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>  Changelog:
>>  v1 -> v2:
>>   * Update commit message with 'Based on Linux ...'.
>>   * Add Linux driver author to .c file header.
>>   * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
>> 'NFC_DEFAULT_BUS_TIMING'.
>>   * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
>>
>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>  drivers/mtd/nand/raw/Makefile |1 +
>>  drivers/mtd/nand/raw/meson_nand.c | 1241 +
>>  3 files changed, 1251 insertions(+)
>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>
>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>> index d624589a89..7b7b0226ab 100644
>> --- a/drivers/mtd/nand/raw/Kconfig
>> +++ b/drivers/mtd/nand/raw/Kconfig
>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>   controller. This uses the hardware ECC for read and
>>   write operations.
>>
>> +config NAND_MESON
>> +   bool "Meson NAND support"
>> +   select SYS_NAND_SELF_INIT
>> +   depends on DM_MTD && ARCH_MESON
>> +   imply CMD_NAND
>> +   help
>> + This enables Nand driver support for Meson raw NAND flash
>> + controller.
>> +
>>  config NAND_MXC
>> bool "MXC NAND support"
>> depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>> index add2b4cf65..5b4efd52c9 100644
>> --- a/drivers/mtd/nand/raw/Makefile
>> +++ b/drivers/mtd/nand/raw/Makefile
>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
>> b/drivers/mtd/nand/raw/meson_nand.c
>> new file mode 100644
>> index 00..a6dbe99d84
>> --- /dev/null
>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>> @@ -0,0 +1,1241 @@
>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>> +/*
>> + * Amlogic Meson Nand Flash Controller Driver
>> + *
>> + * Copyright (c) 2018 Amlogic, inc.
>> + * Author: Liang Yang 
>> + *
>> + * Copyright (c) 2023 SaluteDevices, Inc.
>> + * Author: Arseniy Krasnov 
>> + */
>> +
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define NFC_CMD_IDLE   (0xc << 14)
>> +#define NFC_CMD_CLE(0x5 << 14)
>> +#define NFC_CMD_ALE(0x6 << 14)
>> +#define NFC_CMD_DWR(0x4 << 14)
>> +#define NFC_CMD_DRD(0x8 << 14)
>> +#define NFC_CMD_ADL((0 << 16) | (3 << 20))
>> +#define NFC_CMD_ADH((1 << 16) | (3 << 20))
>> +#define NFC_CMD_AIL((2 << 16) | (3 << 20))
>> +#define NFC_CMD_AIH((3 << 16) | (3 << 20))
>> +#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
>> +#define NFC_CMD_M2N((0 << 17) | (2 << 20))
>> +#define NFC_CMD_N2M((1 << 17) | (2 << 20))
>> +#define NFC_CMD_RB BIT(20)
>> +#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
>> +#define NFC_CMD_SCRAMBLER_DISABLE  0
>> +#define NFC_CMD_SHORTMODE_DISABLE  0
>> +#define NFC_CMD_RB_INT BIT(14)
>> +#define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
>> +
>> +#define NFC_CMD_GET_SIZE(x)(((x) >> 22) & GENMASK(4, 0))
>> +
>> +#define NFC_REG_CMD0x00
>> +#define NFC_REG_CFG 

Re: [PATCH v2] mtd: rawnand: Meson NAND controller support

2024-01-08 Thread Arseniy Krasnov



On 08.01.2024 21:48, Michael Nazzareno Trimarchi wrote:
> Hi
> 
> On Mon, Jan 8, 2024 at 7:41 PM Arseniy Krasnov
>  wrote:
>>
>> Sorry, pls ping
>>
> 
> Sorry to be late, ;) I will give a review tomorrow

Sure no problem! Thanks :)

> 
> Michael
> 
>> Thanks, Arseniy
>>
>> On 15.12.2023 15:23, Arseniy Krasnov wrote:
>>> Basic support for Amlogic Meson NAND controller on AXG.
>>>
>>> Based on Linux version 6.7.0-rc4.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>>  Changelog:
>>>  v1 -> v2:
>>>   * Update commit message with 'Based on Linux ...'.
>>>   * Add Linux driver author to .c file header.
>>>   * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
>>> 'NFC_DEFAULT_BUS_TIMING'.
>>>   * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
>>>
>>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>>  drivers/mtd/nand/raw/Makefile |1 +
>>>  drivers/mtd/nand/raw/meson_nand.c | 1241 +
>>>  3 files changed, 1251 insertions(+)
>>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>>
>>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>>> index d624589a89..7b7b0226ab 100644
>>> --- a/drivers/mtd/nand/raw/Kconfig
>>> +++ b/drivers/mtd/nand/raw/Kconfig
>>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>> controller. This uses the hardware ECC for read and
>>> write operations.
>>>
>>> +config NAND_MESON
>>> + bool "Meson NAND support"
>>> + select SYS_NAND_SELF_INIT
>>> + depends on DM_MTD && ARCH_MESON
>>> + imply CMD_NAND
>>> + help
>>> +   This enables Nand driver support for Meson raw NAND flash
>>> +   controller.
>>> +
>>>  config NAND_MXC
>>>   bool "MXC NAND support"
>>>   depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>>> index add2b4cf65..5b4efd52c9 100644
>>> --- a/drivers/mtd/nand/raw/Makefile
>>> +++ b/drivers/mtd/nand/raw/Makefile
>>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
>>> b/drivers/mtd/nand/raw/meson_nand.c
>>> new file mode 100644
>>> index 00..a6dbe99d84
>>> --- /dev/null
>>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>>> @@ -0,0 +1,1241 @@
>>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>>> +/*
>>> + * Amlogic Meson Nand Flash Controller Driver
>>> + *
>>> + * Copyright (c) 2018 Amlogic, inc.
>>> + * Author: Liang Yang 
>>> + *
>>> + * Copyright (c) 2023 SaluteDevices, Inc.
>>> + * Author: Arseniy Krasnov 
>>> + */
>>> +
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +
>>> +#define NFC_CMD_IDLE (0xc << 14)
>>> +#define NFC_CMD_CLE  (0x5 << 14)
>>> +#define NFC_CMD_ALE  (0x6 << 14)
>>> +#define NFC_CMD_DWR  (0x4 << 14)
>>> +#define NFC_CMD_DRD  (0x8 << 14)
>>> +#define NFC_CMD_ADL  ((0 << 16) | (3 << 20))
>>> +#define NFC_CMD_ADH  ((1 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIL  ((2 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIH  ((3 << 16) | (3 << 20))
>>> +#define NFC_CMD_SEED ((8 << 16) | (3 << 20))
>>> +#define NFC_CMD_M2N  ((0 << 17) | (2 << 20))
>>> +#define NFC_CMD

Re: [PATCH v2] cmd: mtd: OTP access support

2024-01-08 Thread Arseniy Krasnov
Sorry, pls ping

Thanks, Arseniy

On 20.12.2023 22:36, Arseniy Krasnov wrote:
> Add access to OTP region. It supports info, dump, write and lock
> operations.
> 
> Signed-off-by: Arseniy Krasnov 
> ---
>  Changelog:
>  v1 -> v2:
>   * Remove warning that OTP can't be erased after write.
> 
>  cmd/Kconfig |   1 +
>  cmd/mtd.c   | 224 
>  2 files changed, 225 insertions(+)
> 
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 90e4ef93e0..c47523a03b 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1354,6 +1354,7 @@ config CMD_MTD
>   bool "mtd"
>   depends on MTD
>   select MTD_PARTITIONS
> + select HEXDUMP
>   help
> MTD commands support.
>  
> diff --git a/cmd/mtd.c b/cmd/mtd.c
> index eb6e2d6892..1ab69b108b 100644
> --- a/cmd/mtd.c
> +++ b/cmd/mtd.c
> @@ -11,6 +11,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops 
> *op)
>   return true;
>  }
>  
> +static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
> +char *const argv[])
> +{
> + struct mtd_info *mtd;
> + size_t retlen;
> + off_t from;
> + size_t len;
> + bool user;
> + int ret;
> + u8 *buf;
> +
> + if (argc != 5)
> + return CMD_RET_USAGE;
> +
> + if (!strcmp(argv[2], "u"))
> + user = true;
> + else if (!strcmp(argv[2], "f"))
> + user = false;
> + else
> + return CMD_RET_USAGE;
> +
> + mtd = get_mtd_by_name(argv[1]);
> + if (IS_ERR_OR_NULL(mtd))
> + return CMD_RET_FAILURE;
> +
> + from = simple_strtoul(argv[3], NULL, 0);
> + len = simple_strtoul(argv[4], NULL, 0);
> +
> + ret = CMD_RET_FAILURE;
> +
> + buf = malloc(len);
> + if (!buf)
> + goto put_mtd;
> +
> + printf("Reading %s OTP from 0x%lx, %lu bytes\n",
> +user ? "user" : "factory", from, len);
> +
> + if (user)
> + ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
> + else
> + ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
> + if (ret) {
> + free(buf);
> + pr_err("OTP read failed: %d\n", ret);
> + ret = CMD_RET_FAILURE;
> + goto put_mtd;
> + }
> +
> + if (retlen != len)
> + pr_err("OTP read returns %zu, but %zu expected\n",
> +retlen, len);
> +
> + print_hex_dump("", 0, 16, 1, buf, retlen, true);
> +
> + free(buf);
> +
> + ret = CMD_RET_SUCCESS;
> +
> +put_mtd:
> + put_mtd_device(mtd);
> +
> + return ret;
> +}
> +
> +static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
> +char *const argv[])
> +{
> + struct mtd_info *mtd;
> + off_t from;
> + size_t len;
> + int ret;
> +
> + if (argc != 4)
> + return CMD_RET_USAGE;
> +
> + mtd = get_mtd_by_name(argv[1]);
> + if (IS_ERR_OR_NULL(mtd))
> + return CMD_RET_FAILURE;
> +
> + from = simple_strtoul(argv[2], NULL, 0);
> + len = simple_strtoul(argv[3], NULL, 0);
> +
> + ret = mtd_lock_user_prot_reg(mtd, from, len);
> + if (ret) {
> + pr_err("OTP lock failed: %d\n", ret);
> + ret = CMD_RET_FAILURE;
> + goto put_mtd;
> + }
> +
> + ret = CMD_RET_SUCCESS;
> +
> +put_mtd:
> + put_mtd_device(mtd);
> +
> + return ret;
> +}
> +
> +static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
> + char *const argv[])
> +{
> + struct mtd_info *mtd;
> + size_t retlen;
> + size_t binlen;
> + u8 *binbuf;
> + off_t from;
> + int ret;
> +
> + if (argc != 4)
> + return CMD_RET_USAGE;
> +
> + mtd = get_mtd_by_name(argv[1]);
> + if (IS_ERR_OR_NULL(mtd))
> + return CMD_RET_FAILURE;
> +
> + from = simple_strtoul(argv[2], NULL, 0);
> + binlen = strlen(argv[3]) / 2;
> +
> + ret = CMD_RET_FAILURE;
> + binbuf = malloc(binlen);
> + if (!binbuf)
> + goto put_mtd;
> +
> + hex2bin(binbuf, argv[3], binlen);
> +
> + printf("Will write:\n");
> +
> + print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
> +
> + 

Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2024-01-08 Thread Arseniy Krasnov
Sorry, pls ping

Thanks, Arseniy

On 18.12.2023 14:54, Arseniy Krasnov wrote:
> cc: Jaime Liao
> 
> On 04.12.2023 22:23, Arseniy Krasnov wrote:
>> cc: Miquel Raynal
>>
>> On 30.11.2023 14:24, Arseniy Krasnov wrote:
>>> Support for OTP area access on MX30LFxG18AC chip series.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>>  drivers/mtd/nand/raw/nand_macronix.c | 170 +++
>>>  1 file changed, 170 insertions(+)
>>>
>>> diff --git a/drivers/mtd/nand/raw/nand_macronix.c 
>>> b/drivers/mtd/nand/raw/nand_macronix.c
>>> index dc972e5909..4c6ddd9233 100644
>>> --- a/drivers/mtd/nand/raw/nand_macronix.c
>>> +++ b/drivers/mtd/nand/raw/nand_macronix.c
>>> @@ -16,13 +16,183 @@
>>>   * GNU General Public License for more details.
>>>   */
>>>  
>>> +#include 
>>>  #include 
>>>  
>>> +#define ONFI_FEATURE_ADDR_30LFXG18AC_OTP   0x90
>>> +#define MACRONIX_30LFXG18AC_OTP_START_PAGE 2
>>> +#define MACRONIX_30LFXG18AC_OTP_PAGES  30
>>> +#define MACRONIX_30LFXG18AC_OTP_PAGE_SIZE  2112
>>> +#define MACRONIX_30LFXG18AC_OTP_SIZE_BYTES \
>>> +   (MACRONIX_30LFXG18AC_OTP_PAGES *\
>>> +MACRONIX_30LFXG18AC_OTP_PAGE_SIZE)
>>> +
>>> +#define MACRONIX_30LFXG18AC_OTP_EN BIT(0)
>>> +
>>> +static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t 
>>> len,
>>> +   size_t *retlen,
>>> +   struct otp_info *buf)
>>> +{
>>> +   if (len < sizeof(*buf))
>>> +   return -EINVAL;
>>> +
>>> +   /* Always report that OTP is unlocked. Reason is that this
>>> +* type of flash chip doesn't provide way to check that OTP
>>> +* is locked or not: subfeature parameter is implemented as
>>> +* volatile register. Technically OTP region could be locked
>>> +* and become readonly, but as there is no way to check it,
>>> +* don't allow to lock it ('_lock_user_prot_reg' callback
>>> +* always returns -EOPNOTSUPP) and thus we report that OTP
>>> +* is unlocked.
>>> +*/
>>> +   buf->locked = 0;
>>> +   buf->start = 0;
>>> +   buf->length = MACRONIX_30LFXG18AC_OTP_SIZE_BYTES;
>>> +
>>> +   *retlen = sizeof(*buf);
>>> +
>>> +   return 0;
>>> +}
>>> +
>>> +static int macronix_30lfxg18ac_otp_enable(struct nand_chip *nand)
>>> +{
>>> +   u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
>>> +   struct mtd_info *mtd;
>>> +
>>> +   mtd = nand_to_mtd(nand);
>>> +   feature_buf[0] = MACRONIX_30LFXG18AC_OTP_EN;
>>> +
>>> +   return nand->onfi_set_features(mtd, nand, 
>>> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
>>> +}
>>> +
>>> +static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
>>> +{
>>> +   u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
>>> +   struct mtd_info *mtd;
>>> +
>>> +   mtd = nand_to_mtd(nand);
>>> +   return nand->onfi_set_features(mtd, nand, 
>>> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
>>> +}
>>> +
>>> +static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
>>> +   loff_t offs_in_flash,
>>> +   size_t len, size_t *retlen,
>>> +   u_char *buf, bool write)
>>> +{
>>> +   struct nand_chip *nand;
>>> +   size_t bytes_handled;
>>> +   off_t offs_in_page;
>>> +   u64 page;
>>> +   int ret;
>>> +
>>> +   nand = mtd_to_nand(mtd);
>>> +   nand->select_chip(mtd, 0);
>>> +
>>> +   ret = macronix_30lfxg18ac_otp_enable(nand);
>>> +   if (ret)
>>> +   goto out_otp;
>>> +
>>> +   page = offs_in_flash;
>>> +   /* 'page' will be result of division. */
>>> +   offs_in_page = do_div(page, MACRONIX_30LFXG18AC_OTP_PAGE_SIZE);
>>> +   bytes_handled = 0;
>>> +
>>> +   while (bytes_handled < len &&
>>> +  page < MACRONIX_30LFXG18AC_OTP_PAGES) {
>>> +   size_t bytes_to_handle;
>>> +   u64 phys_page = page + MACRONIX_30LFXG18AC_OTP_START_PAGE;
>>> +
>>> +   bytes_to_handle = min_t(size_t, len - bytes_handled,
>>> +

Re: [PATCH v2] mtd: rawnand: Meson NAND controller support

2024-01-08 Thread Arseniy Krasnov
Sorry, pls ping

Thanks, Arseniy

On 15.12.2023 15:23, Arseniy Krasnov wrote:
> Basic support for Amlogic Meson NAND controller on AXG.
> 
> Based on Linux version 6.7.0-rc4.
> 
> Signed-off-by: Arseniy Krasnov 
> ---
>  Changelog:
>  v1 -> v2:
>   * Update commit message with 'Based on Linux ...'.
>   * Add Linux driver author to .c file header.
>   * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
> 'NFC_DEFAULT_BUS_TIMING'.
>   * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.
> 
>  drivers/mtd/nand/raw/Kconfig  |9 +
>  drivers/mtd/nand/raw/Makefile |1 +
>  drivers/mtd/nand/raw/meson_nand.c | 1241 +
>  3 files changed, 1251 insertions(+)
>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
> 
> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
> index d624589a89..7b7b0226ab 100644
> --- a/drivers/mtd/nand/raw/Kconfig
> +++ b/drivers/mtd/nand/raw/Kconfig
> @@ -488,6 +488,15 @@ config NAND_ARASAN
> controller. This uses the hardware ECC for read and
> write operations.
>  
> +config NAND_MESON
> + bool "Meson NAND support"
> + select SYS_NAND_SELF_INIT
> + depends on DM_MTD && ARCH_MESON
> + imply CMD_NAND
> + help
> +   This enables Nand driver support for Meson raw NAND flash
> +   controller.
> +
>  config NAND_MXC
>   bool "MXC NAND support"
>   depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
> index add2b4cf65..5b4efd52c9 100644
> --- a/drivers/mtd/nand/raw/Makefile
> +++ b/drivers/mtd/nand/raw/Makefile
> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
> b/drivers/mtd/nand/raw/meson_nand.c
> new file mode 100644
> index 00..a6dbe99d84
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/meson_nand.c
> @@ -0,0 +1,1241 @@
> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
> +/*
> + * Amlogic Meson Nand Flash Controller Driver
> + *
> + * Copyright (c) 2018 Amlogic, inc.
> + * Author: Liang Yang 
> + *
> + * Copyright (c) 2023 SaluteDevices, Inc.
> + * Author: Arseniy Krasnov 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define NFC_CMD_IDLE (0xc << 14)
> +#define NFC_CMD_CLE  (0x5 << 14)
> +#define NFC_CMD_ALE  (0x6 << 14)
> +#define NFC_CMD_DWR  (0x4 << 14)
> +#define NFC_CMD_DRD  (0x8 << 14)
> +#define NFC_CMD_ADL  ((0 << 16) | (3 << 20))
> +#define NFC_CMD_ADH  ((1 << 16) | (3 << 20))
> +#define NFC_CMD_AIL  ((2 << 16) | (3 << 20))
> +#define NFC_CMD_AIH  ((3 << 16) | (3 << 20))
> +#define NFC_CMD_SEED ((8 << 16) | (3 << 20))
> +#define NFC_CMD_M2N  ((0 << 17) | (2 << 20))
> +#define NFC_CMD_N2M  ((1 << 17) | (2 << 20))
> +#define NFC_CMD_RB   BIT(20)
> +#define NFC_CMD_SCRAMBLER_ENABLE BIT(19)
> +#define NFC_CMD_SCRAMBLER_DISABLE0
> +#define NFC_CMD_SHORTMODE_DISABLE0
> +#define NFC_CMD_RB_INT   BIT(14)
> +#define NFC_CMD_RB_INT_NO_PIN((0xb << 10) | BIT(18) | 
> BIT(16))
> +
> +#define NFC_CMD_GET_SIZE(x)  (((x) >> 22) & GENMASK(4, 0))
> +
> +#define NFC_REG_CMD  0x00
> +#define NFC_REG_CFG  0x04
> +#define NFC_REG_DADR 0x08
> +#define NFC_REG_IADR 0x0c
> +#define NFC_REG_BUF  0x10
> +#define NFC_REG_INFO 0x14
> +#define NFC_REG_DC   0x18
> +#define NFC_REG_ADR  0x1c
> +#define NFC_REG_DL   0x20
> +#define NFC_REG_DH   0x24
> +#define NFC_REG_CADR 0x28
> +#define NFC_REG_SADR 0x2c
> +#define NFC_REG_PINS 0x30
> +#define NFC_REG_VER  0x38
> +
> +#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)\
> + (

[PATCH v2] cmd: mtd: OTP access support

2023-12-20 Thread Arseniy Krasnov
Add access to OTP region. It supports info, dump, write and lock
operations.

Signed-off-by: Arseniy Krasnov 
---
 Changelog:
 v1 -> v2:
  * Remove warning that OTP can't be erased after write.

 cmd/Kconfig |   1 +
 cmd/mtd.c   | 224 
 2 files changed, 225 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 90e4ef93e0..c47523a03b 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1354,6 +1354,7 @@ config CMD_MTD
bool "mtd"
depends on MTD
select MTD_PARTITIONS
+   select HEXDUMP
help
  MTD commands support.
 
diff --git a/cmd/mtd.c b/cmd/mtd.c
index eb6e2d6892..1ab69b108b 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
return true;
 }
 
+static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   off_t from;
+   size_t len;
+   bool user;
+   int ret;
+   u8 *buf;
+
+   if (argc != 5)
+   return CMD_RET_USAGE;
+
+   if (!strcmp(argv[2], "u"))
+   user = true;
+   else if (!strcmp(argv[2], "f"))
+   user = false;
+   else
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[3], NULL, 0);
+   len = simple_strtoul(argv[4], NULL, 0);
+
+   ret = CMD_RET_FAILURE;
+
+   buf = malloc(len);
+   if (!buf)
+   goto put_mtd;
+
+   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
+  user ? "user" : "factory", from, len);
+
+   if (user)
+   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
+   else
+   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
+   if (ret) {
+   free(buf);
+   pr_err("OTP read failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != len)
+   pr_err("OTP read returns %zu, but %zu expected\n",
+  retlen, len);
+
+   print_hex_dump("", 0, 16, 1, buf, retlen, true);
+
+   free(buf);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   off_t from;
+   size_t len;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   len = simple_strtoul(argv[3], NULL, 0);
+
+   ret = mtd_lock_user_prot_reg(mtd, from, len);
+   if (ret) {
+   pr_err("OTP lock failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
+   char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   size_t binlen;
+   u8 *binbuf;
+   off_t from;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   binlen = strlen(argv[3]) / 2;
+
+   ret = CMD_RET_FAILURE;
+   binbuf = malloc(binlen);
+   if (!binbuf)
+   goto put_mtd;
+
+   hex2bin(binbuf, argv[3], binlen);
+
+   printf("Will write:\n");
+
+   print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
+
+   printf("to 0x%zx\n", from);
+
+   printf("Continue (y/n)?\n");
+
+   if (confirm_yesno() != 1) {
+   pr_err("OTP write canceled\n");
+   ret = CMD_RET_SUCCESS;
+   goto put_mtd;
+   }
+
+   ret = mtd_write_user_prot_reg(mtd, from, binlen, , binbuf);
+   if (ret) {
+   pr_err("OTP write failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != binlen)
+   pr_err("OTP write returns %zu, but %zu expected\n",
+  retlen, binlen);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   free(binbuf);
+   put_mtd_device(mtd);
+
+

Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2023-12-18 Thread Arseniy Krasnov
cc: Jaime Liao

On 04.12.2023 22:23, Arseniy Krasnov wrote:
> cc: Miquel Raynal
> 
> On 30.11.2023 14:24, Arseniy Krasnov wrote:
>> Support for OTP area access on MX30LFxG18AC chip series.
>>
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>  drivers/mtd/nand/raw/nand_macronix.c | 170 +++
>>  1 file changed, 170 insertions(+)
>>
>> diff --git a/drivers/mtd/nand/raw/nand_macronix.c 
>> b/drivers/mtd/nand/raw/nand_macronix.c
>> index dc972e5909..4c6ddd9233 100644
>> --- a/drivers/mtd/nand/raw/nand_macronix.c
>> +++ b/drivers/mtd/nand/raw/nand_macronix.c
>> @@ -16,13 +16,183 @@
>>   * GNU General Public License for more details.
>>   */
>>  
>> +#include 
>>  #include 
>>  
>> +#define ONFI_FEATURE_ADDR_30LFXG18AC_OTP0x90
>> +#define MACRONIX_30LFXG18AC_OTP_START_PAGE  2
>> +#define MACRONIX_30LFXG18AC_OTP_PAGES   30
>> +#define MACRONIX_30LFXG18AC_OTP_PAGE_SIZE   2112
>> +#define MACRONIX_30LFXG18AC_OTP_SIZE_BYTES  \
>> +(MACRONIX_30LFXG18AC_OTP_PAGES *\
>> + MACRONIX_30LFXG18AC_OTP_PAGE_SIZE)
>> +
>> +#define MACRONIX_30LFXG18AC_OTP_EN  BIT(0)
>> +
>> +static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t 
>> len,
>> +size_t *retlen,
>> +struct otp_info *buf)
>> +{
>> +if (len < sizeof(*buf))
>> +return -EINVAL;
>> +
>> +/* Always report that OTP is unlocked. Reason is that this
>> + * type of flash chip doesn't provide way to check that OTP
>> + * is locked or not: subfeature parameter is implemented as
>> + * volatile register. Technically OTP region could be locked
>> + * and become readonly, but as there is no way to check it,
>> + * don't allow to lock it ('_lock_user_prot_reg' callback
>> + * always returns -EOPNOTSUPP) and thus we report that OTP
>> + * is unlocked.
>> + */
>> +buf->locked = 0;
>> +buf->start = 0;
>> +buf->length = MACRONIX_30LFXG18AC_OTP_SIZE_BYTES;
>> +
>> +*retlen = sizeof(*buf);
>> +
>> +return 0;
>> +}
>> +
>> +static int macronix_30lfxg18ac_otp_enable(struct nand_chip *nand)
>> +{
>> +u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
>> +struct mtd_info *mtd;
>> +
>> +mtd = nand_to_mtd(nand);
>> +feature_buf[0] = MACRONIX_30LFXG18AC_OTP_EN;
>> +
>> +return nand->onfi_set_features(mtd, nand, 
>> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
>> +}
>> +
>> +static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
>> +{
>> +u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
>> +struct mtd_info *mtd;
>> +
>> +mtd = nand_to_mtd(nand);
>> +return nand->onfi_set_features(mtd, nand, 
>> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
>> +}
>> +
>> +static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
>> +loff_t offs_in_flash,
>> +size_t len, size_t *retlen,
>> +u_char *buf, bool write)
>> +{
>> +struct nand_chip *nand;
>> +size_t bytes_handled;
>> +off_t offs_in_page;
>> +u64 page;
>> +int ret;
>> +
>> +nand = mtd_to_nand(mtd);
>> +nand->select_chip(mtd, 0);
>> +
>> +ret = macronix_30lfxg18ac_otp_enable(nand);
>> +if (ret)
>> +goto out_otp;
>> +
>> +page = offs_in_flash;
>> +/* 'page' will be result of division. */
>> +offs_in_page = do_div(page, MACRONIX_30LFXG18AC_OTP_PAGE_SIZE);
>> +bytes_handled = 0;
>> +
>> +while (bytes_handled < len &&
>> +   page < MACRONIX_30LFXG18AC_OTP_PAGES) {
>> +size_t bytes_to_handle;
>> +u64 phys_page = page + MACRONIX_30LFXG18AC_OTP_START_PAGE;
>> +
>> +bytes_to_handle = min_t(size_t, len - bytes_handled,
>> +MACRONIX_30LFXG18AC_OTP_PAGE_SIZE -
>> +offs_in_page);
>> +
>> +if (write)
>> +ret = nand_prog_page_op(nand, phys_page, offs_in_page,
>> +[bytes_handled], 
>> bytes_to_handle);
>> +else
>> +ret = nand_read_page_op(nand, phys_page, offs_in_page,
>&g

[PATCH v2] mtd: rawnand: Meson NAND controller support

2023-12-15 Thread Arseniy Krasnov
Basic support for Amlogic Meson NAND controller on AXG.

Based on Linux version 6.7.0-rc4.

Signed-off-by: Arseniy Krasnov 
---
 Changelog:
 v1 -> v2:
  * Update commit message with 'Based on Linux ...'.
  * Add Linux driver author to .c file header.
  * Add comment for defines 'NFC_DEFAULT_BUS_CYCLE' and
'NFC_DEFAULT_BUS_TIMING'.
  * Use 'dev_read_addr_index_ptr()' instead of 'dev_read_addr()'.

 drivers/mtd/nand/raw/Kconfig  |9 +
 drivers/mtd/nand/raw/Makefile |1 +
 drivers/mtd/nand/raw/meson_nand.c | 1241 +
 3 files changed, 1251 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/meson_nand.c

diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index d624589a89..7b7b0226ab 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -488,6 +488,15 @@ config NAND_ARASAN
  controller. This uses the hardware ECC for read and
  write operations.
 
+config NAND_MESON
+   bool "Meson NAND support"
+   select SYS_NAND_SELF_INIT
+   depends on DM_MTD && ARCH_MESON
+   imply CMD_NAND
+   help
+ This enables Nand driver support for Meson raw NAND flash
+ controller.
+
 config NAND_MXC
bool "MXC NAND support"
depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index add2b4cf65..5b4efd52c9 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
 obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
 obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
 obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
+obj-$(CONFIG_NAND_MESON) += meson_nand.o
 obj-$(CONFIG_NAND_MXC) += mxc_nand.o
 obj-$(CONFIG_NAND_MXS) += mxs_nand.o
 obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
diff --git a/drivers/mtd/nand/raw/meson_nand.c 
b/drivers/mtd/nand/raw/meson_nand.c
new file mode 100644
index 00..a6dbe99d84
--- /dev/null
+++ b/drivers/mtd/nand/raw/meson_nand.c
@@ -0,0 +1,1241 @@
+// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
+/*
+ * Amlogic Meson Nand Flash Controller Driver
+ *
+ * Copyright (c) 2018 Amlogic, inc.
+ * Author: Liang Yang 
+ *
+ * Copyright (c) 2023 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define NFC_CMD_IDLE   (0xc << 14)
+#define NFC_CMD_CLE(0x5 << 14)
+#define NFC_CMD_ALE(0x6 << 14)
+#define NFC_CMD_DWR(0x4 << 14)
+#define NFC_CMD_DRD(0x8 << 14)
+#define NFC_CMD_ADL((0 << 16) | (3 << 20))
+#define NFC_CMD_ADH((1 << 16) | (3 << 20))
+#define NFC_CMD_AIL((2 << 16) | (3 << 20))
+#define NFC_CMD_AIH((3 << 16) | (3 << 20))
+#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
+#define NFC_CMD_M2N((0 << 17) | (2 << 20))
+#define NFC_CMD_N2M((1 << 17) | (2 << 20))
+#define NFC_CMD_RB BIT(20)
+#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
+#define NFC_CMD_SCRAMBLER_DISABLE  0
+#define NFC_CMD_SHORTMODE_DISABLE  0
+#define NFC_CMD_RB_INT BIT(14)
+#define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
+
+#define NFC_CMD_GET_SIZE(x)(((x) >> 22) & GENMASK(4, 0))
+
+#define NFC_REG_CMD0x00
+#define NFC_REG_CFG0x04
+#define NFC_REG_DADR   0x08
+#define NFC_REG_IADR   0x0c
+#define NFC_REG_BUF0x10
+#define NFC_REG_INFO   0x14
+#define NFC_REG_DC 0x18
+#define NFC_REG_ADR0x1c
+#define NFC_REG_DL 0x20
+#define NFC_REG_DH 0x24
+#define NFC_REG_CADR   0x28
+#define NFC_REG_SADR   0x2c
+#define NFC_REG_PINS   0x30
+#define NFC_REG_VER0x38
+
+#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)  \
+   (   \
+   (cmd_dir)   |   \
+   ((ran) << 19)   |   \
+   ((bch) << 14)   |   \
+   ((short_mode) << 13)|   \
+   (((page_size) & 0x7f) << 6) |   \
+   ((pages) & 0x3f)\
+   )
+
+#define GENCMDDADDRL(adl, addr)((adl) | ((addr) & 0x))
+#define GENCMDDA

Re: [PATCH v1] arm: dts: meson: add NAND controller node for AXG

2023-12-12 Thread Arseniy Krasnov
Hi,

On 12.12.2023 17:03, Neil Armstrong wrote:
> Hi,
> 
> On 30/11/2023 13:27, Arseniy Krasnov wrote:
> 
> Can you add a reference to the Linux patches that you submitted so
> we know it will be safe to sync the DT from Linux when they are merged ?

Sure:

https://lore.kernel.org/linux-amlogic/20230828133647.3712644-1-avkras...@salutedevices.com/
https://lore.kernel.org/linux-amlogic/170107314359.1083800.4264228970569502392.b4...@linaro.org/T/#m30d2fd46fca4f78098be19d7ec71097f98b60d29

Thanks, Arseniy

> 
> Thanks,
> Neil
> 
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>   arch/arm/dts/meson-axg.dtsi | 35 +++
>>   1 file changed, 35 insertions(+)
>>
>> diff --git a/arch/arm/dts/meson-axg.dtsi b/arch/arm/dts/meson-axg.dtsi
>> index 3f5254eeb4..c01ace3ff1 100644
>> --- a/arch/arm/dts/meson-axg.dtsi
>> +++ b/arch/arm/dts/meson-axg.dtsi
>> @@ -430,6 +430,27 @@
>>   };
>>   };
>>   +    nand_all_pins: nand_all_pins {
>> +    mux {
>> +    groups = "emmc_nand_d0",
>> + "emmc_nand_d1",
>> + "emmc_nand_d2",
>> + "emmc_nand_d3",
>> + "emmc_nand_d4",
>> + "emmc_nand_d5",
>> + "emmc_nand_d6",
>> + "emmc_nand_d7",
>> + "nand_ce0",
>> + "nand_ale",
>> + "nand_cle",
>> + "nand_wen_clk",
>> + "nand_ren_wr";
>> +    function = "nand";
>> +    input-enable;
>> +    bias-pull-up;
>> +    };
>> +    };
>> +
>>   emmc_ds_pins: emmc_ds {
>>   mux {
>>   groups = "emmc_ds";
>> @@ -1906,6 +1927,20 @@
>>   resets = < RESET_SD_EMMC_C>;
>>   };
>>   +    nfc: nand-controller@7800 {
>> +    #address-cells = <2>;
>> +    #size-cells = <2>;
>> +    pinctrl-0 = <_all_pins>;
>> +    pinctrl-names = "default";
>> +    compatible = "amlogic,meson-axg-nfc";
>> +    status = "okay";
>> +    reg = <0x0 0x7800 0x0 0x100>,
>> +  <0x0 0x7000 0x0 0x800>;
>> +    clocks = < CLKID_SD_EMMC_C>,
>> + < CLKID_FCLK_DIV2>;
>> +    clock-names = "core", "device";
>> +    };
>> +
>>   usb2_phy1: phy@9020 {
>>   compatible = "amlogic,meson-gxl-usb2-phy";
>>   #phy-cells = <0>;
> 


Re: [PATCH v1] mtd: rawnand: Meson NAND controller support

2023-12-10 Thread Arseniy Krasnov



On 08.12.2023 11:35, Arseniy Krasnov wrote:
> 
> 
> On 08.12.2023 11:09, Michael Nazzareno Trimarchi wrote:
>> Hi
>>
>> On Thu, Nov 30, 2023 at 12:29 PM Arseniy Krasnov
>>  wrote:
>>>
>>> Basic support for Amlogic Meson NAND controller on AXG.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>>  drivers/mtd/nand/raw/Makefile |1 +
>>>  drivers/mtd/nand/raw/meson_nand.c | 1231 +
>>>  3 files changed, 1241 insertions(+)
>>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>>
>>
>> This come almost from a linux kernel, please reference version in the
>> commit message
> 
> Ok

Hi @Michael,

Small question: if there is no version in linux, I can just use v1.0 here?

Thanks, Arseniy

> 
>>
>>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>>> index d624589a89..7b7b0226ab 100644
>>> --- a/drivers/mtd/nand/raw/Kconfig
>>> +++ b/drivers/mtd/nand/raw/Kconfig
>>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>>   controller. This uses the hardware ECC for read and
>>>   write operations.
>>>
>>> +config NAND_MESON
>>> +   bool "Meson NAND support"
>>> +   select SYS_NAND_SELF_INIT
>>> +   depends on DM_MTD && ARCH_MESON
>>> +   imply CMD_NAND
>>> +   help
>>> + This enables Nand driver support for Meson raw NAND flash
>>> + controller.
>>> +
>>>  config NAND_MXC
>>> bool "MXC NAND support"
>>> depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>>> index add2b4cf65..5b4efd52c9 100644
>>> --- a/drivers/mtd/nand/raw/Makefile
>>> +++ b/drivers/mtd/nand/raw/Makefile
>>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
>>> b/drivers/mtd/nand/raw/meson_nand.c
>>> new file mode 100644
>>> index 00..f1d49887ee
>>> --- /dev/null
>>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>>> @@ -0,0 +1,1231 @@
>>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>>> +/*
>>> + * Amlogic Meson Nand Flash Controller Driver
>>> + *
>>> + * Copyright (c) 2023 SaluteDevices, Inc.
>>> + * Author: Arseniy Krasnov 
>>> + */
>>> +
>>
>> Are you sure about the author? Should not be the one that wrote the
>> linux kernel one?
> 
> Agree, I'll add original author, because this driver is based on Linux
> version.
> 
>>
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +
>>> +#define NFC_CMD_IDLE   (0xc << 14)
>>> +#define NFC_CMD_CLE(0x5 << 14)
>>> +#define NFC_CMD_ALE(0x6 << 14)
>>> +#define NFC_CMD_DWR(0x4 << 14)
>>> +#define NFC_CMD_DRD(0x8 << 14)
>>> +#define NFC_CMD_ADL((0 << 16) | (3 << 20))
>>> +#define NFC_CMD_ADH((1 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIL((2 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIH((3 << 16) | (3 << 20))
>>> +#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
>>> +#define NFC_CMD_M2N((0 << 17) | (2 << 20))
>>> +#define NFC_CMD_N2M((1 << 17) | (2 << 20))
>>> +#define NFC_CMD_RB BIT(20)
>>> +#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
>>> +#define NFC_CM

Re: [PATCH v1] mtd: rawnand: Meson NAND controller support

2023-12-08 Thread Arseniy Krasnov



On 08.12.2023 11:09, Michael Nazzareno Trimarchi wrote:
> Hi
> 
> On Thu, Nov 30, 2023 at 12:29 PM Arseniy Krasnov
>  wrote:
>>
>> Basic support for Amlogic Meson NAND controller on AXG.
>>
>> Signed-off-by: Arseniy Krasnov 
>> ---
>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>  drivers/mtd/nand/raw/Makefile |1 +
>>  drivers/mtd/nand/raw/meson_nand.c | 1231 +
>>  3 files changed, 1241 insertions(+)
>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>
> 
> This come almost from a linux kernel, please reference version in the
> commit message

Ok

> 
>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>> index d624589a89..7b7b0226ab 100644
>> --- a/drivers/mtd/nand/raw/Kconfig
>> +++ b/drivers/mtd/nand/raw/Kconfig
>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>   controller. This uses the hardware ECC for read and
>>   write operations.
>>
>> +config NAND_MESON
>> +   bool "Meson NAND support"
>> +   select SYS_NAND_SELF_INIT
>> +   depends on DM_MTD && ARCH_MESON
>> +   imply CMD_NAND
>> +   help
>> + This enables Nand driver support for Meson raw NAND flash
>> + controller.
>> +
>>  config NAND_MXC
>> bool "MXC NAND support"
>> depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>> index add2b4cf65..5b4efd52c9 100644
>> --- a/drivers/mtd/nand/raw/Makefile
>> +++ b/drivers/mtd/nand/raw/Makefile
>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
>> b/drivers/mtd/nand/raw/meson_nand.c
>> new file mode 100644
>> index 00..f1d49887ee
>> --- /dev/null
>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>> @@ -0,0 +1,1231 @@
>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>> +/*
>> + * Amlogic Meson Nand Flash Controller Driver
>> + *
>> + * Copyright (c) 2023 SaluteDevices, Inc.
>> + * Author: Arseniy Krasnov 
>> + */
>> +
> 
> Are you sure about the author? Should not be the one that wrote the
> linux kernel one?

Agree, I'll add original author, because this driver is based on Linux
version.

> 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define NFC_CMD_IDLE   (0xc << 14)
>> +#define NFC_CMD_CLE(0x5 << 14)
>> +#define NFC_CMD_ALE(0x6 << 14)
>> +#define NFC_CMD_DWR(0x4 << 14)
>> +#define NFC_CMD_DRD(0x8 << 14)
>> +#define NFC_CMD_ADL((0 << 16) | (3 << 20))
>> +#define NFC_CMD_ADH((1 << 16) | (3 << 20))
>> +#define NFC_CMD_AIL((2 << 16) | (3 << 20))
>> +#define NFC_CMD_AIH((3 << 16) | (3 << 20))
>> +#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
>> +#define NFC_CMD_M2N((0 << 17) | (2 << 20))
>> +#define NFC_CMD_N2M((1 << 17) | (2 << 20))
>> +#define NFC_CMD_RB BIT(20)
>> +#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
>> +#define NFC_CMD_SCRAMBLER_DISABLE  0
>> +#define NFC_CMD_SHORTMODE_DISABLE  0
>> +#define NFC_CMD_RB_INT BIT(14)
>> +#define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
>> +
>> +#define NFC_CMD_GET_SIZE(x)(((x) >> 22) & GENMASK(4, 0))
>> +
>> +#define NFC_REG_CMD0x00
>> +#define NFC_REG_CFG0x04
>> +#define NFC_REG_DADR   0x08
>> +#define NFC_REG_IADR   0x0c
>> +#define NFC_REG_BUF0x10
>> +#define NFC_REG_INFO   0x14
>> +#define NFC_REG_DC

Re: [PATCH v1] mtd: rawnand: Meson NAND controller support

2023-12-04 Thread Arseniy Krasnov



On 04.12.2023 22:53, Michael Nazzareno Trimarchi wrote:
> Hi Arseniy
> 
> Il lun 4 dic 2023, 20:31 Arseniy Krasnov  ha
> scritto:
> 
>> cc: Miquel Raynal
>>
>> On 30.11.2023 14:21, Arseniy Krasnov wrote:
>>> Basic support for Amlogic Meson NAND controller on AXG.
>>>
>>> Signed-off-by: Arseniy Krasnov 
>>> ---
>>
> 
> 
> We are going to review your patches. It will be done this week
> 
> Michael

Hello! Thanks!

Thanks, Arseniy

> 
>>  drivers/mtd/nand/raw/Kconfig  |9 +
>>>  drivers/mtd/nand/raw/Makefile |1 +
>>>  drivers/mtd/nand/raw/meson_nand.c | 1231 +
>>>  3 files changed, 1241 insertions(+)
>>>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
>>>
>>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>>> index d624589a89..7b7b0226ab 100644
>>> --- a/drivers/mtd/nand/raw/Kconfig
>>> +++ b/drivers/mtd/nand/raw/Kconfig
>>> @@ -488,6 +488,15 @@ config NAND_ARASAN
>>> controller. This uses the hardware ECC for read and
>>> write operations.
>>>
>>> +config NAND_MESON
>>> + bool "Meson NAND support"
>>> + select SYS_NAND_SELF_INIT
>>> + depends on DM_MTD && ARCH_MESON
>>> + imply CMD_NAND
>>> + help
>>> +   This enables Nand driver support for Meson raw NAND flash
>>> +   controller.
>>> +
>>>  config NAND_MXC
>>>   bool "MXC NAND support"
>>>   depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
>>> diff --git a/drivers/mtd/nand/raw/Makefile
>> b/drivers/mtd/nand/raw/Makefile
>>> index add2b4cf65..5b4efd52c9 100644
>>> --- a/drivers/mtd/nand/raw/Makefile
>>> +++ b/drivers/mtd/nand/raw/Makefile
>>> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>>>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>>>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>>>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
>>> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>>>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>>>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>>>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
>>> diff --git a/drivers/mtd/nand/raw/meson_nand.c
>> b/drivers/mtd/nand/raw/meson_nand.c
>>> new file mode 100644
>>> index 00..f1d49887ee
>>> --- /dev/null
>>> +++ b/drivers/mtd/nand/raw/meson_nand.c
>>> @@ -0,0 +1,1231 @@
>>> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
>>> +/*
>>> + * Amlogic Meson Nand Flash Controller Driver
>>> + *
>>> + * Copyright (c) 2023 SaluteDevices, Inc.
>>> + * Author: Arseniy Krasnov 
>>> + */
>>> +
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +
>>> +#define NFC_CMD_IDLE (0xc << 14)
>>> +#define NFC_CMD_CLE  (0x5 << 14)
>>> +#define NFC_CMD_ALE  (0x6 << 14)
>>> +#define NFC_CMD_DWR  (0x4 << 14)
>>> +#define NFC_CMD_DRD  (0x8 << 14)
>>> +#define NFC_CMD_ADL  ((0 << 16) | (3 << 20))
>>> +#define NFC_CMD_ADH  ((1 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIL  ((2 << 16) | (3 << 20))
>>> +#define NFC_CMD_AIH  ((3 << 16) | (3 << 20))
>>> +#define NFC_CMD_SEED ((8 << 16) | (3 << 20))
>>> +#define NFC_CMD_M2N  ((0 << 17) | (2 << 20))
>>> +#define NFC_CMD_N2M  ((1 << 17) | (2 << 20))
>>> +#define NFC_CMD_RB   BIT(20)
>>> +#define NFC_CMD_SCRAMBLER_ENABLE BIT(19)
>>> +#define NFC_CMD_SCRAMBLER_DISABLE0
>>> +#define NFC_CMD_SHORTMODE_DISABLE0
>>> +#define NFC_CMD_RB_INT   BIT(14)
>>> +#define NFC_CMD_RB_INT_NO_PIN((0xb << 10) | BIT(18) |
>> BIT(16))
>>> +
>>> +#define NFC_CMD_GET_SIZE(x)  (((x) >> 22) & GENMASK(4, 0))
>>> +
>>> +#define NFC_REG_CMD  0x00
&g

Re: [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2023-12-04 Thread Arseniy Krasnov
cc: Miquel Raynal

On 30.11.2023 14:24, Arseniy Krasnov wrote:
> Support for OTP area access on MX30LFxG18AC chip series.
> 
> Signed-off-by: Arseniy Krasnov 
> ---
>  drivers/mtd/nand/raw/nand_macronix.c | 170 +++
>  1 file changed, 170 insertions(+)
> 
> diff --git a/drivers/mtd/nand/raw/nand_macronix.c 
> b/drivers/mtd/nand/raw/nand_macronix.c
> index dc972e5909..4c6ddd9233 100644
> --- a/drivers/mtd/nand/raw/nand_macronix.c
> +++ b/drivers/mtd/nand/raw/nand_macronix.c
> @@ -16,13 +16,183 @@
>   * GNU General Public License for more details.
>   */
>  
> +#include 
>  #include 
>  
> +#define ONFI_FEATURE_ADDR_30LFXG18AC_OTP 0x90
> +#define MACRONIX_30LFXG18AC_OTP_START_PAGE   2
> +#define MACRONIX_30LFXG18AC_OTP_PAGES30
> +#define MACRONIX_30LFXG18AC_OTP_PAGE_SIZE2112
> +#define MACRONIX_30LFXG18AC_OTP_SIZE_BYTES   \
> + (MACRONIX_30LFXG18AC_OTP_PAGES *\
> +  MACRONIX_30LFXG18AC_OTP_PAGE_SIZE)
> +
> +#define MACRONIX_30LFXG18AC_OTP_EN   BIT(0)
> +
> +static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t len,
> + size_t *retlen,
> + struct otp_info *buf)
> +{
> + if (len < sizeof(*buf))
> + return -EINVAL;
> +
> + /* Always report that OTP is unlocked. Reason is that this
> +  * type of flash chip doesn't provide way to check that OTP
> +  * is locked or not: subfeature parameter is implemented as
> +  * volatile register. Technically OTP region could be locked
> +  * and become readonly, but as there is no way to check it,
> +  * don't allow to lock it ('_lock_user_prot_reg' callback
> +  * always returns -EOPNOTSUPP) and thus we report that OTP
> +  * is unlocked.
> +  */
> + buf->locked = 0;
> + buf->start = 0;
> + buf->length = MACRONIX_30LFXG18AC_OTP_SIZE_BYTES;
> +
> + *retlen = sizeof(*buf);
> +
> + return 0;
> +}
> +
> +static int macronix_30lfxg18ac_otp_enable(struct nand_chip *nand)
> +{
> + u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
> + struct mtd_info *mtd;
> +
> + mtd = nand_to_mtd(nand);
> + feature_buf[0] = MACRONIX_30LFXG18AC_OTP_EN;
> +
> + return nand->onfi_set_features(mtd, nand, 
> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
> +}
> +
> +static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
> +{
> + u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
> + struct mtd_info *mtd;
> +
> + mtd = nand_to_mtd(nand);
> + return nand->onfi_set_features(mtd, nand, 
> ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
> +}
> +
> +static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
> + loff_t offs_in_flash,
> + size_t len, size_t *retlen,
> + u_char *buf, bool write)
> +{
> + struct nand_chip *nand;
> + size_t bytes_handled;
> + off_t offs_in_page;
> + u64 page;
> + int ret;
> +
> + nand = mtd_to_nand(mtd);
> + nand->select_chip(mtd, 0);
> +
> + ret = macronix_30lfxg18ac_otp_enable(nand);
> + if (ret)
> + goto out_otp;
> +
> + page = offs_in_flash;
> + /* 'page' will be result of division. */
> + offs_in_page = do_div(page, MACRONIX_30LFXG18AC_OTP_PAGE_SIZE);
> + bytes_handled = 0;
> +
> + while (bytes_handled < len &&
> +page < MACRONIX_30LFXG18AC_OTP_PAGES) {
> + size_t bytes_to_handle;
> + u64 phys_page = page + MACRONIX_30LFXG18AC_OTP_START_PAGE;
> +
> + bytes_to_handle = min_t(size_t, len - bytes_handled,
> + MACRONIX_30LFXG18AC_OTP_PAGE_SIZE -
> + offs_in_page);
> +
> + if (write)
> + ret = nand_prog_page_op(nand, phys_page, offs_in_page,
> + [bytes_handled], 
> bytes_to_handle);
> + else
> + ret = nand_read_page_op(nand, phys_page, offs_in_page,
> + [bytes_handled], 
> bytes_to_handle);
> + if (ret)
> + goto out_otp;
> +
> + bytes_handled += bytes_to_handle;
> + offs_in_page = 0;
> + page++;
> + }
> +
> + *retlen = bytes_handled;
> +
> +out_otp:
> + if (ret)
> + dev_err(mtd->dev, "failed to perform OTP 

Re: [PATCH v1] mtd: rawnand: Meson NAND controller support

2023-12-04 Thread Arseniy Krasnov
cc: Miquel Raynal

On 30.11.2023 14:21, Arseniy Krasnov wrote:
> Basic support for Amlogic Meson NAND controller on AXG.
> 
> Signed-off-by: Arseniy Krasnov 
> ---
>  drivers/mtd/nand/raw/Kconfig  |9 +
>  drivers/mtd/nand/raw/Makefile |1 +
>  drivers/mtd/nand/raw/meson_nand.c | 1231 +
>  3 files changed, 1241 insertions(+)
>  create mode 100644 drivers/mtd/nand/raw/meson_nand.c
> 
> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
> index d624589a89..7b7b0226ab 100644
> --- a/drivers/mtd/nand/raw/Kconfig
> +++ b/drivers/mtd/nand/raw/Kconfig
> @@ -488,6 +488,15 @@ config NAND_ARASAN
> controller. This uses the hardware ECC for read and
> write operations.
>  
> +config NAND_MESON
> + bool "Meson NAND support"
> + select SYS_NAND_SELF_INIT
> + depends on DM_MTD && ARCH_MESON
> + imply CMD_NAND
> + help
> +   This enables Nand driver support for Meson raw NAND flash
> +   controller.
> +
>  config NAND_MXC
>   bool "MXC NAND support"
>   depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
> index add2b4cf65..5b4efd52c9 100644
> --- a/drivers/mtd/nand/raw/Makefile
> +++ b/drivers/mtd/nand/raw/Makefile
> @@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
>  obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
>  obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
>  obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
> +obj-$(CONFIG_NAND_MESON) += meson_nand.o
>  obj-$(CONFIG_NAND_MXC) += mxc_nand.o
>  obj-$(CONFIG_NAND_MXS) += mxs_nand.o
>  obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
> diff --git a/drivers/mtd/nand/raw/meson_nand.c 
> b/drivers/mtd/nand/raw/meson_nand.c
> new file mode 100644
> index 00..f1d49887ee
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/meson_nand.c
> @@ -0,0 +1,1231 @@
> +// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
> +/*
> + * Amlogic Meson Nand Flash Controller Driver
> + *
> + * Copyright (c) 2023 SaluteDevices, Inc.
> + * Author: Arseniy Krasnov 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define NFC_CMD_IDLE (0xc << 14)
> +#define NFC_CMD_CLE  (0x5 << 14)
> +#define NFC_CMD_ALE  (0x6 << 14)
> +#define NFC_CMD_DWR  (0x4 << 14)
> +#define NFC_CMD_DRD  (0x8 << 14)
> +#define NFC_CMD_ADL  ((0 << 16) | (3 << 20))
> +#define NFC_CMD_ADH  ((1 << 16) | (3 << 20))
> +#define NFC_CMD_AIL  ((2 << 16) | (3 << 20))
> +#define NFC_CMD_AIH  ((3 << 16) | (3 << 20))
> +#define NFC_CMD_SEED ((8 << 16) | (3 << 20))
> +#define NFC_CMD_M2N  ((0 << 17) | (2 << 20))
> +#define NFC_CMD_N2M  ((1 << 17) | (2 << 20))
> +#define NFC_CMD_RB   BIT(20)
> +#define NFC_CMD_SCRAMBLER_ENABLE BIT(19)
> +#define NFC_CMD_SCRAMBLER_DISABLE0
> +#define NFC_CMD_SHORTMODE_DISABLE0
> +#define NFC_CMD_RB_INT   BIT(14)
> +#define NFC_CMD_RB_INT_NO_PIN((0xb << 10) | BIT(18) | 
> BIT(16))
> +
> +#define NFC_CMD_GET_SIZE(x)  (((x) >> 22) & GENMASK(4, 0))
> +
> +#define NFC_REG_CMD  0x00
> +#define NFC_REG_CFG  0x04
> +#define NFC_REG_DADR 0x08
> +#define NFC_REG_IADR 0x0c
> +#define NFC_REG_BUF  0x10
> +#define NFC_REG_INFO 0x14
> +#define NFC_REG_DC   0x18
> +#define NFC_REG_ADR  0x1c
> +#define NFC_REG_DL   0x20
> +#define NFC_REG_DH   0x24
> +#define NFC_REG_CADR 0x28
> +#define NFC_REG_SADR 0x2c
> +#define NFC_REG_PINS 0x30
> +#define NFC_REG_VER  0x38
> +
> +#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)\
> + (   \
> + (cmd_dir)   |   \
> + ((ran) << 19)   |   \
> + ((bch) << 14)   |   \
> + ((short_mode) << 13)|   \
> + (((page_size) & 0x7f) << 6) |   \
&g

Re: [PATCH v1] cmd: mtd: OTP access support

2023-12-01 Thread Arseniy Krasnov



On 01.12.2023 11:28, Michael Walle wrote:
> Hi,
> 
 +static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
 +    char *const argv[])
 +{
>>> ..
>>>
 +    printf("Caution! OTP data bits can't be erased! Continue (y/n)?\n");
>>>
>>> Please note, that with current SPI-NOR flashes this is not true and
>>> there is usually some kind of erase command for the OTP bits. Only
>>> the region lock is permanent and with that set, no more write or erase
>>> is possible.
>>
>> I see, so may be just rephrase this message, like just "Continue? (y/n)?"
> 
> I don't have any strong opinion here. The code may or may not be used
> to write to the OTP of an SPI-NOR flash. Also, this patchset doesn't
> support the erase op (I guess u-boot's MTD won't support it either). So
> from an user's perspective these OTP data can't be erased ;)

Ok, anyway if SPI-NOR supports erasing of OTP, I'll remove these words
from the message above. About erase-op - unfortunately I don't have SPI-NOR
hardware at this moment (only NAND), so I can't test such implementation.

Thanks, Arseniy

> 
> -michael


Re: [PATCH v1] cmd: mtd: OTP access support

2023-11-30 Thread Arseniy Krasnov



On 30.11.2023 16:35, Michael Walle wrote:
>> +static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
>> +    char *const argv[])
>> +{
> ..
> 
>> +    printf("Caution! OTP data bits can't be erased! Continue (y/n)?\n");
> 
> Please note, that with current SPI-NOR flashes this is not true and
> there is usually some kind of erase command for the OTP bits. Only
> the region lock is permanent and with that set, no more write or erase
> is possible.

I see, so may be just rephrase this message, like just "Continue? (y/n)?"

Thanks, Arseniy

> 
> -michael


[PATCH v1] arm: dts: meson: add NAND controller node for AXG

2023-11-30 Thread Arseniy Krasnov
Signed-off-by: Arseniy Krasnov 
---
 arch/arm/dts/meson-axg.dtsi | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/arch/arm/dts/meson-axg.dtsi b/arch/arm/dts/meson-axg.dtsi
index 3f5254eeb4..c01ace3ff1 100644
--- a/arch/arm/dts/meson-axg.dtsi
+++ b/arch/arm/dts/meson-axg.dtsi
@@ -430,6 +430,27 @@
};
};
 
+   nand_all_pins: nand_all_pins {
+   mux {
+   groups = "emmc_nand_d0",
+"emmc_nand_d1",
+"emmc_nand_d2",
+"emmc_nand_d3",
+"emmc_nand_d4",
+"emmc_nand_d5",
+"emmc_nand_d6",
+"emmc_nand_d7",
+"nand_ce0",
+"nand_ale",
+"nand_cle",
+"nand_wen_clk",
+"nand_ren_wr";
+   function = "nand";
+   input-enable;
+   bias-pull-up;
+   };
+   };
+
emmc_ds_pins: emmc_ds {
mux {
groups = "emmc_ds";
@@ -1906,6 +1927,20 @@
resets = < RESET_SD_EMMC_C>;
};
 
+   nfc: nand-controller@7800 {
+   #address-cells = <2>;
+   #size-cells = <2>;
+   pinctrl-0 = <_all_pins>;
+   pinctrl-names = "default";
+   compatible = "amlogic,meson-axg-nfc";
+   status = "okay";
+   reg = <0x0 0x7800 0x0 0x100>,
+ <0x0 0x7000 0x0 0x800>;
+   clocks = < CLKID_SD_EMMC_C>,
+< CLKID_FCLK_DIV2>;
+   clock-names = "core", "device";
+   };
+
usb2_phy1: phy@9020 {
compatible = "amlogic,meson-gxl-usb2-phy";
#phy-cells = <0>;
-- 
2.35.0



[PATCH v1] cmd: mtd: OTP access support

2023-11-30 Thread Arseniy Krasnov
Add access to OTP region. It supports info, dump, write and lock
operations.

Signed-off-by: Arseniy Krasnov 
---
 cmd/Kconfig |   1 +
 cmd/mtd.c   | 224 
 2 files changed, 225 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 90e4ef93e0..c47523a03b 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1354,6 +1354,7 @@ config CMD_MTD
bool "mtd"
depends on MTD
select MTD_PARTITIONS
+   select HEXDUMP
help
  MTD commands support.
 
diff --git a/cmd/mtd.c b/cmd/mtd.c
index eb6e2d6892..3ad15b0f8f 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -202,6 +203,219 @@ static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
return true;
 }
 
+static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   off_t from;
+   size_t len;
+   bool user;
+   int ret;
+   u8 *buf;
+
+   if (argc != 5)
+   return CMD_RET_USAGE;
+
+   if (!strcmp(argv[2], "u"))
+   user = true;
+   else if (!strcmp(argv[2], "f"))
+   user = false;
+   else
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[3], NULL, 0);
+   len = simple_strtoul(argv[4], NULL, 0);
+
+   ret = CMD_RET_FAILURE;
+
+   buf = malloc(len);
+   if (!buf)
+   goto put_mtd;
+
+   printf("Reading %s OTP from 0x%lx, %lu bytes\n",
+  user ? "user" : "factory", from, len);
+
+   if (user)
+   ret = mtd_read_user_prot_reg(mtd, from, len, , buf);
+   else
+   ret = mtd_read_fact_prot_reg(mtd, from, len, , buf);
+   if (ret) {
+   free(buf);
+   pr_err("OTP read failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != len)
+   pr_err("OTP read returns %zu, but %zu expected\n",
+  retlen, len);
+
+   print_hex_dump("", 0, 16, 1, buf, retlen, true);
+
+   free(buf);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   off_t from;
+   size_t len;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   len = simple_strtoul(argv[3], NULL, 0);
+
+   ret = mtd_lock_user_prot_reg(mtd, from, len);
+   if (ret) {
+   pr_err("OTP lock failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
+   char *const argv[])
+{
+   struct mtd_info *mtd;
+   size_t retlen;
+   size_t binlen;
+   u8 *binbuf;
+   off_t from;
+   int ret;
+
+   if (argc != 4)
+   return CMD_RET_USAGE;
+
+   mtd = get_mtd_by_name(argv[1]);
+   if (IS_ERR_OR_NULL(mtd))
+   return CMD_RET_FAILURE;
+
+   from = simple_strtoul(argv[2], NULL, 0);
+   binlen = strlen(argv[3]) / 2;
+
+   ret = CMD_RET_FAILURE;
+   binbuf = malloc(binlen);
+   if (!binbuf)
+   goto put_mtd;
+
+   hex2bin(binbuf, argv[3], binlen);
+
+   printf("Will write:\n");
+
+   print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
+
+   printf("to 0x%zx\n", from);
+
+   printf("Caution! OTP data bits can't be erased! Continue (y/n)?\n");
+
+   if (confirm_yesno() != 1) {
+   pr_err("OTP write canceled\n");
+   ret = CMD_RET_SUCCESS;
+   goto put_mtd;
+   }
+
+   ret = mtd_write_user_prot_reg(mtd, from, binlen, , binbuf);
+   if (ret) {
+   pr_err("OTP write failed: %d\n", ret);
+   ret = CMD_RET_FAILURE;
+   goto put_mtd;
+   }
+
+   if (retlen != binlen)
+   pr_err("OTP write returns %zu, but %zu expected\n",
+  retlen, binlen);
+
+   ret = CMD_RET_SUCCESS;
+
+put_mtd:
+   free(binbuf);
+   put_mtd_device(mtd);
+
+   return ret;
+}
+
+static int d

[PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC

2023-11-30 Thread Arseniy Krasnov
Support for OTP area access on MX30LFxG18AC chip series.

Signed-off-by: Arseniy Krasnov 
---
 drivers/mtd/nand/raw/nand_macronix.c | 170 +++
 1 file changed, 170 insertions(+)

diff --git a/drivers/mtd/nand/raw/nand_macronix.c 
b/drivers/mtd/nand/raw/nand_macronix.c
index dc972e5909..4c6ddd9233 100644
--- a/drivers/mtd/nand/raw/nand_macronix.c
+++ b/drivers/mtd/nand/raw/nand_macronix.c
@@ -16,13 +16,183 @@
  * GNU General Public License for more details.
  */
 
+#include 
 #include 
 
+#define ONFI_FEATURE_ADDR_30LFXG18AC_OTP   0x90
+#define MACRONIX_30LFXG18AC_OTP_START_PAGE 2
+#define MACRONIX_30LFXG18AC_OTP_PAGES  30
+#define MACRONIX_30LFXG18AC_OTP_PAGE_SIZE  2112
+#define MACRONIX_30LFXG18AC_OTP_SIZE_BYTES \
+   (MACRONIX_30LFXG18AC_OTP_PAGES *\
+MACRONIX_30LFXG18AC_OTP_PAGE_SIZE)
+
+#define MACRONIX_30LFXG18AC_OTP_EN BIT(0)
+
+static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t len,
+   size_t *retlen,
+   struct otp_info *buf)
+{
+   if (len < sizeof(*buf))
+   return -EINVAL;
+
+   /* Always report that OTP is unlocked. Reason is that this
+* type of flash chip doesn't provide way to check that OTP
+* is locked or not: subfeature parameter is implemented as
+* volatile register. Technically OTP region could be locked
+* and become readonly, but as there is no way to check it,
+* don't allow to lock it ('_lock_user_prot_reg' callback
+* always returns -EOPNOTSUPP) and thus we report that OTP
+* is unlocked.
+*/
+   buf->locked = 0;
+   buf->start = 0;
+   buf->length = MACRONIX_30LFXG18AC_OTP_SIZE_BYTES;
+
+   *retlen = sizeof(*buf);
+
+   return 0;
+}
+
+static int macronix_30lfxg18ac_otp_enable(struct nand_chip *nand)
+{
+   u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
+   struct mtd_info *mtd;
+
+   mtd = nand_to_mtd(nand);
+   feature_buf[0] = MACRONIX_30LFXG18AC_OTP_EN;
+
+   return nand->onfi_set_features(mtd, nand, 
ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
+}
+
+static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
+{
+   u8 feature_buf[ONFI_SUBFEATURE_PARAM_LEN] = { 0 };
+   struct mtd_info *mtd;
+
+   mtd = nand_to_mtd(nand);
+   return nand->onfi_set_features(mtd, nand, 
ONFI_FEATURE_ADDR_30LFXG18AC_OTP, feature_buf);
+}
+
+static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
+   loff_t offs_in_flash,
+   size_t len, size_t *retlen,
+   u_char *buf, bool write)
+{
+   struct nand_chip *nand;
+   size_t bytes_handled;
+   off_t offs_in_page;
+   u64 page;
+   int ret;
+
+   nand = mtd_to_nand(mtd);
+   nand->select_chip(mtd, 0);
+
+   ret = macronix_30lfxg18ac_otp_enable(nand);
+   if (ret)
+   goto out_otp;
+
+   page = offs_in_flash;
+   /* 'page' will be result of division. */
+   offs_in_page = do_div(page, MACRONIX_30LFXG18AC_OTP_PAGE_SIZE);
+   bytes_handled = 0;
+
+   while (bytes_handled < len &&
+  page < MACRONIX_30LFXG18AC_OTP_PAGES) {
+   size_t bytes_to_handle;
+   u64 phys_page = page + MACRONIX_30LFXG18AC_OTP_START_PAGE;
+
+   bytes_to_handle = min_t(size_t, len - bytes_handled,
+   MACRONIX_30LFXG18AC_OTP_PAGE_SIZE -
+   offs_in_page);
+
+   if (write)
+   ret = nand_prog_page_op(nand, phys_page, offs_in_page,
+   [bytes_handled], 
bytes_to_handle);
+   else
+   ret = nand_read_page_op(nand, phys_page, offs_in_page,
+   [bytes_handled], 
bytes_to_handle);
+   if (ret)
+   goto out_otp;
+
+   bytes_handled += bytes_to_handle;
+   offs_in_page = 0;
+   page++;
+   }
+
+   *retlen = bytes_handled;
+
+out_otp:
+   if (ret)
+   dev_err(mtd->dev, "failed to perform OTP IO: %i\n", ret);
+
+   ret = macronix_30lfxg18ac_otp_disable(nand);
+   if (ret)
+   dev_err(mtd->dev, "failed to leave OTP mode after %s\n",
+   write ? "write" : "read");
+
+   nand->select_chip(mtd, -1);
+
+   return ret;
+}
+
+static int macronix_30lfxg18ac_write_otp(struct mtd_info *mtd, loff_t to,
+size_t len, size_t *rlen,
+u_char *buf)
+{
+   return __macronix_30lfxg18ac_rw_otp(mtd, to, len, rlen

[PATCH v1] mtd: rawnand: Meson NAND controller support

2023-11-30 Thread Arseniy Krasnov
Basic support for Amlogic Meson NAND controller on AXG.

Signed-off-by: Arseniy Krasnov 
---
 drivers/mtd/nand/raw/Kconfig  |9 +
 drivers/mtd/nand/raw/Makefile |1 +
 drivers/mtd/nand/raw/meson_nand.c | 1231 +
 3 files changed, 1241 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/meson_nand.c

diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index d624589a89..7b7b0226ab 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -488,6 +488,15 @@ config NAND_ARASAN
  controller. This uses the hardware ECC for read and
  write operations.
 
+config NAND_MESON
+   bool "Meson NAND support"
+   select SYS_NAND_SELF_INIT
+   depends on DM_MTD && ARCH_MESON
+   imply CMD_NAND
+   help
+ This enables Nand driver support for Meson raw NAND flash
+ controller.
+
 config NAND_MXC
bool "MXC NAND support"
depends on CPU_ARM926EJS || CPU_ARM1136 || MX5
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index add2b4cf65..5b4efd52c9 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -61,6 +61,7 @@ obj-$(CONFIG_NAND_KMETER1) += kmeter1_nand.o
 obj-$(CONFIG_NAND_LPC32XX_MLC) += lpc32xx_nand_mlc.o
 obj-$(CONFIG_NAND_LPC32XX_SLC) += lpc32xx_nand_slc.o
 obj-$(CONFIG_NAND_VF610_NFC) += vf610_nfc.o
+obj-$(CONFIG_NAND_MESON) += meson_nand.o
 obj-$(CONFIG_NAND_MXC) += mxc_nand.o
 obj-$(CONFIG_NAND_MXS) += mxs_nand.o
 obj-$(CONFIG_NAND_MXS_DT) += mxs_nand_dt.o
diff --git a/drivers/mtd/nand/raw/meson_nand.c 
b/drivers/mtd/nand/raw/meson_nand.c
new file mode 100644
index 00..f1d49887ee
--- /dev/null
+++ b/drivers/mtd/nand/raw/meson_nand.c
@@ -0,0 +1,1231 @@
+// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
+/*
+ * Amlogic Meson Nand Flash Controller Driver
+ *
+ * Copyright (c) 2023 SaluteDevices, Inc.
+ * Author: Arseniy Krasnov 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define NFC_CMD_IDLE   (0xc << 14)
+#define NFC_CMD_CLE(0x5 << 14)
+#define NFC_CMD_ALE(0x6 << 14)
+#define NFC_CMD_DWR(0x4 << 14)
+#define NFC_CMD_DRD(0x8 << 14)
+#define NFC_CMD_ADL((0 << 16) | (3 << 20))
+#define NFC_CMD_ADH((1 << 16) | (3 << 20))
+#define NFC_CMD_AIL((2 << 16) | (3 << 20))
+#define NFC_CMD_AIH((3 << 16) | (3 << 20))
+#define NFC_CMD_SEED   ((8 << 16) | (3 << 20))
+#define NFC_CMD_M2N((0 << 17) | (2 << 20))
+#define NFC_CMD_N2M((1 << 17) | (2 << 20))
+#define NFC_CMD_RB BIT(20)
+#define NFC_CMD_SCRAMBLER_ENABLE   BIT(19)
+#define NFC_CMD_SCRAMBLER_DISABLE  0
+#define NFC_CMD_SHORTMODE_DISABLE  0
+#define NFC_CMD_RB_INT BIT(14)
+#define NFC_CMD_RB_INT_NO_PIN  ((0xb << 10) | BIT(18) | BIT(16))
+
+#define NFC_CMD_GET_SIZE(x)(((x) >> 22) & GENMASK(4, 0))
+
+#define NFC_REG_CMD0x00
+#define NFC_REG_CFG0x04
+#define NFC_REG_DADR   0x08
+#define NFC_REG_IADR   0x0c
+#define NFC_REG_BUF0x10
+#define NFC_REG_INFO   0x14
+#define NFC_REG_DC 0x18
+#define NFC_REG_ADR0x1c
+#define NFC_REG_DL 0x20
+#define NFC_REG_DH 0x24
+#define NFC_REG_CADR   0x28
+#define NFC_REG_SADR   0x2c
+#define NFC_REG_PINS   0x30
+#define NFC_REG_VER0x38
+
+#define CMDRWGEN(cmd_dir, ran, bch, short_mode, page_size, pages)  \
+   (   \
+   (cmd_dir)   |   \
+   ((ran) << 19)   |   \
+   ((bch) << 14)   |   \
+   ((short_mode) << 13)|   \
+   (((page_size) & 0x7f) << 6) |   \
+   ((pages) & 0x3f)\
+   )
+
+#define GENCMDDADDRL(adl, addr)((adl) | ((addr) & 0x))
+#define GENCMDDADDRH(adh, addr)((adh) | (((addr) >> 16) & 
0x))
+#define GENCMDIADDRL(ail, addr)((ail) | ((addr) & 0x))
+#define GENCMDIADDRH(aih, addr)((aih) | (((addr) >> 16) & 
0x))
+
+#define DMA_DIR(dir)   ((dir) ? NFC_CMD_N2M : NFC_CMD_M2N)
+
+#define ECC_CHECK_RETURN_FF-1
+
+#def