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

Patchset is based on patchset for Linux (today merged to nand-next): https://lore.kernel.org/linux-mtd/20240507230903.3399594-1-avkrasnov@saluted...
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:
- Contain some metadata about hardware.
- 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(-)

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 avkrasnov@salutedevices.com --- 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_3 0x00004000
+/* + * 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_MEDIUM 0x00400000 + /* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG

Hi, sorry, pls ping :)
Thanks
On 02.06.2024 23:08, Arseniy Krasnov wrote:
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 avkrasnov@salutedevices.com
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_3 0x00004000
+/*
- 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_MEDIUM 0x00400000
/* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG

Hi
Yes I have seen and I will review today and cross-check. Did you lunch the testing on the other patches series? I would like to merge at all but we were having some build breakage
Michael
On Mon, Jun 24, 2024 at 7:16 AM Arseniy Krasnov avkrasnov@salutedevices.com wrote:
Hi, sorry, pls ping :)
Thanks
On 02.06.2024 23:08, Arseniy Krasnov wrote:
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 avkrasnov@salutedevices.com
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_3 0x00004000
+/*
- 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_MEDIUM 0x00400000
/* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG

Hi, thanks, ok! What build problem ? With this [PATCH v1] mtd: rawnand: macronix: OTP access for MX30LFxG18AC ?
Thanks
On 24.06.2024 11:37, Michael Nazzareno Trimarchi wrote:
Hi
Yes I have seen and I will review today and cross-check. Did you lunch the testing on the other patches series? I would like to merge at all but we were having some build breakage
Michael
On Mon, Jun 24, 2024 at 7:16 AM Arseniy Krasnov avkrasnov@salutedevices.com wrote:
Hi, sorry, pls ping :)
Thanks
On 02.06.2024 23:08, Arseniy Krasnov wrote:
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 avkrasnov@salutedevices.com
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_3 0x00004000
+/*
- 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_MEDIUM 0x00400000
/* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG

Hello Arseniy,
Am Sun, Jun 02, 2024 at 11:08:34PM +0300 schrieb 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 avkrasnov@salutedevices.com
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_3 0x00004000
+/*
- 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_MEDIUM 0x00400000
/* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
The entries in that file are ordered numerically. Comparing with the same file in Linux this should go before NAND_KEEP_TIMINGS, not anywhere in between. Makes later diffs and ports from Linux easier.
Greets Alex
-- 2.35.0

On 24.06.2024 12:14, Alexander Dahl wrote:
Hello Arseniy,
Am Sun, Jun 02, 2024 at 11:08:34PM +0300 schrieb 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 avkrasnov@salutedevices.com
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_3 0x00004000
+/*
- 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_MEDIUM 0x00400000
/* Options valid for Samsung large page devices */ #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
The entries in that file are ordered numerically. Comparing with the same file in Linux this should go before NAND_KEEP_TIMINGS, not anywhere in between. Makes later diffs and ports from Linux easier.
Ok, I'll update this in the next version.
Thanks
Greets Alex
-- 2.35.0

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 avkrasnov@salutedevices.com --- 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);

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 avkrasnov@salutedevices.com --- 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", + &meson_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", + &meson_chip->boot_page_step); + if (ret) { + dev_err(dev, "could not retrieve 'amlogic,boot-page-step' property: %d", + ret); + goto err_chip_buf_free; + } + } + ret = nand_register(0, mtd); if (ret) { dev_err(dev, "'nand_register()' failed: %d\n", ret);
participants (3)
-
Alexander Dahl
-
Arseniy Krasnov
-
Michael Nazzareno Trimarchi