public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH 00/15] nand: Add sandbox tests
@ 2023-10-29  3:48 Sean Anderson
  2023-10-29  3:48 ` [PATCH 01/15] spl: nand: Fix NULL-pointer dereference Sean Anderson
                   ` (15 more replies)
  0 siblings, 16 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

This series tests raw nand flash in sandbox and fixes various bugs discovered in
the process. I've tried to do things in a contemporary manner, avoiding the
(numerous) variations present on only a few boards. The test is pretty minimal.
Future work could test the rest of the nand API as well as the MTD API.

Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
everything grows by a few bytes due to nand_page_size. A few boards grow more,
mostly those using nand_spl_loaders.c.

[1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2


Sean Anderson (15):
  spl: nand: Fix NULL-pointer dereference
  nand: Don't dereference NULL manufacturer_desc
  nand: Calculate SYS_NAND_PAGE_COUNT automatically
  nand: spl_loaders: Only read enough pages to load the image
  spl: legacy: Honor bl_len when decompressing
  spl: nand: Set bl_len to page size
  cmd: nand: Map memory before accessing it
  spl: nand: Map memory before accessing it
  mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
  mtd: Add some fallbacks for add/del_mtd_device
  nand: Add function to unregister NAND devices
  nand: Allow reinitialization
  arch: sandbox: Add function to create temporary files
  nand: Add sandbox driver
  test: spl: Add a test for NAND

 README                                        |   9 +-
 arch/sandbox/cpu/os.c                         |  17 +
 arch/sandbox/dts/test.dts                     |  67 ++
 arch/sandbox/include/asm/spl.h                |   1 +
 cmd/nand.c                                    |  26 +-
 common/spl/Kconfig                            |   2 +-
 common/spl/spl_legacy.c                       |  18 +-
 common/spl/spl_nand.c                         |  22 +-
 configs/am335x_baltos_defconfig               |   3 +-
 configs/am335x_evm_defconfig                  |   3 +-
 configs/am335x_evm_spiboot_defconfig          |   2 +-
 configs/am335x_guardian_defconfig             |   1 -
 configs/am335x_hs_evm_defconfig               |   2 +-
 configs/am335x_hs_evm_uart_defconfig          |   2 +-
 configs/am335x_igep003x_defconfig             |   3 +-
 configs/am335x_sl50_defconfig                 |   2 +-
 configs/am3517_evm_defconfig                  |   3 +-
 configs/am43xx_evm_defconfig                  |   3 +-
 configs/am43xx_evm_rtconly_defconfig          |   3 +-
 configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
 configs/am43xx_hs_evm_defconfig               |   3 +-
 configs/am62ax_evm_r5_defconfig               |   2 +-
 configs/am65x_evm_a53_defconfig               |   2 +-
 configs/axm_defconfig                         |   1 -
 configs/chiliboard_defconfig                  |   1 -
 configs/cm_t43_defconfig                      |   2 +-
 configs/corvus_defconfig                      |   1 -
 configs/da850evm_nand_defconfig               |   1 -
 configs/devkit3250_defconfig                  |   1 -
 configs/devkit8000_defconfig                  |   1 -
 configs/dra7xx_evm_defconfig                  |   1 -
 configs/draco_defconfig                       |   1 -
 configs/etamin_defconfig                      |   1 -
 .../gardena-smart-gateway-at91sam_defconfig   |   1 -
 configs/igep00x0_defconfig                    |   3 +-
 configs/imx6ulz_smm_m2_defconfig              |   2 +-
 configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
 configs/j7200_evm_a72_defconfig               |   2 +-
 configs/j7200_evm_r5_defconfig                |   2 +-
 configs/j721e_evm_a72_defconfig               |   2 +-
 configs/j721e_evm_r5_defconfig                |   2 +-
 configs/j721s2_evm_a72_defconfig              |   2 +-
 configs/j721s2_evm_r5_defconfig               |   2 +-
 configs/m53menlo_defconfig                    |   1 -
 configs/omap35_logic_defconfig                |   3 +-
 configs/omap35_logic_somlv_defconfig          |   3 +-
 configs/omap3_beagle_defconfig                |   3 +-
 configs/omap3_evm_defconfig                   |   3 +-
 configs/omap3_logic_defconfig                 |   3 +-
 configs/omap3_logic_somlv_defconfig           |   3 +-
 configs/omapl138_lcdk_defconfig               |   1 -
 configs/phycore-am335x-r2-regor_defconfig     |   3 +-
 configs/phycore-am335x-r2-wega_defconfig      |   3 +-
 configs/pxm2_defconfig                        |   1 -
 configs/rastaban_defconfig                    |   1 -
 configs/rut_defconfig                         |   1 -
 configs/sama5d3_xplained_nandflash_defconfig  |   1 -
 configs/sama5d3xek_nandflash_defconfig        |   1 -
 configs/sama5d4_xplained_nandflash_defconfig  |   1 -
 configs/sama5d4ek_nandflash_defconfig         |   1 -
 configs/sandbox64_defconfig                   |  10 +-
 configs/sandbox_defconfig                     |   9 +
 configs/sandbox_noinst_defconfig              |  21 +-
 configs/smartweb_defconfig                    |   1 -
 configs/socfpga_secu1_defconfig               |   2 +-
 configs/stm32746g-eval_spl_defconfig          |   2 +-
 configs/stm32f746-disco_spl_defconfig         |   2 +-
 configs/stm32f769-disco_spl_defconfig         |   2 +-
 configs/stm32mp15_basic_defconfig             |   2 +-
 configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
 configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
 configs/taurus_defconfig                      |   1 -
 configs/thuban_defconfig                      |   1 -
 drivers/mtd/Makefile                          |   2 +-
 drivers/mtd/nand/raw/Kconfig                  |  27 +-
 drivers/mtd/nand/raw/Makefile                 |   1 +
 drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
 drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
 drivers/mtd/nand/raw/denali_spl.c             |   5 +
 drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
 drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
 drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
 drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
 drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
 drivers/mtd/nand/raw/nand.c                   |  66 +-
 drivers/mtd/nand/raw/nand_base.c              |   7 +-
 drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
 drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
 drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
 drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
 drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
 drivers/mtd/onenand/onenand_uboot.c           |   2 -
 include/linux/mtd/mtd.h                       |  12 +
 include/mtd/cfi_flash.h                       |   2 +-
 include/nand.h                                |   3 +
 include/os.h                                  |  13 +
 include/system-constants.h                    |   3 +
 test/dm/Makefile                              |   1 +
 test/dm/nand.c                                | 106 +++
 test/image/Kconfig                            |   9 +
 test/image/Makefile                           |   1 +
 test/image/spl_load_nand.c                    |  54 ++
 102 files changed, 1269 insertions(+), 153 deletions(-)
 create mode 100644 drivers/mtd/nand/raw/sand_nand.c
 create mode 100644 test/dm/nand.c
 create mode 100644 test/image/spl_load_nand.c

-- 
2.37.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* [PATCH 01/15] spl: nand: Fix NULL-pointer dereference
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29 14:15   ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc Sean Anderson
                   ` (14 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, Daniel Schwierzeck, Peng Fan, Weijie Gao

spl_nand_fit_read unconditionally accesses load->priv. Ensure it is set.

Fixes: 00e180cc513 ("spl: nand: support loading i.MX container format file")
Fixes: 4620e8aabc1 ("spl: nand: support loading legacy image with payload compressed")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 common/spl/spl_nand.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/spl/spl_nand.c b/common/spl/spl_nand.c
index 07916bedbb9..a19236d9e6d 100644
--- a/common/spl/spl_nand.c
+++ b/common/spl/spl_nand.c
@@ -105,7 +105,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
 		struct spl_load_info load;
 
 		load.dev = NULL;
-		load.priv = NULL;
+		load.priv = &offset;
 		load.filename = NULL;
 		load.bl_len = bl_len;
 		load.read = spl_nand_fit_read;
@@ -116,7 +116,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
 
 		debug("Found legacy image\n");
 		load.dev = NULL;
-		load.priv = NULL;
+		load.priv = &offset;
 		load.filename = NULL;
 		load.bl_len = 1;
 		load.read = spl_nand_legacy_read;
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
  2023-10-29  3:48 ` [PATCH 01/15] spl: nand: Fix NULL-pointer dereference Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29 13:06   ` Michael Nazzareno Trimarchi
  2023-10-29  3:48 ` [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically Sean Anderson
                   ` (13 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, Heiko Schocher

When no manufacturer is matched, manufacturer_desc is NULL. Avoid
dereferencing it in that case.

Fixes: 4e67c571252 ("mtd,ubi,ubifs: sync with linux v3.15")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 drivers/mtd/nand/raw/nand_base.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 6b4adcf6bdc..44b6cb63a01 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -4462,17 +4462,14 @@ ident_done:
 	else if (chip->jedec_version)
 		pr_info("%s %s\n", manufacturer_desc->name,
 			chip->jedec_params.model);
-	else
+	else if (manufacturer_desc)
 		pr_info("%s %s\n", manufacturer_desc->name, type->name);
 #else
 	if (chip->jedec_version)
 		pr_info("%s %s\n", manufacturer_desc->name,
 			chip->jedec_params.model);
-	else
+	else if (manufacturer_desc)
 		pr_info("%s %s\n", manufacturer_desc->name, type->name);
-
-	pr_info("%s %s\n", manufacturer_desc->name,
-		type->name);
 #endif
 
 	pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
  2023-10-29  3:48 ` [PATCH 01/15] spl: nand: Fix NULL-pointer dereference Sean Anderson
  2023-10-29  3:48 ` [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02  9:53   ` Dario Binacchi
  2023-10-29  3:48 ` [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image Sean Anderson
                   ` (12 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

Contrary to what the help message says, this is the number of pages per
block. Calculate it automatically based on SYS_NAND_BLOCK_SIZE and
SYS_NAND_PAGE_SIZE.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 README                                          | 9 ++++-----
 configs/am335x_baltos_defconfig                 | 1 -
 configs/am335x_evm_defconfig                    | 1 -
 configs/am335x_guardian_defconfig               | 1 -
 configs/am335x_igep003x_defconfig               | 1 -
 configs/am3517_evm_defconfig                    | 1 -
 configs/am43xx_evm_defconfig                    | 1 -
 configs/am43xx_evm_rtconly_defconfig            | 1 -
 configs/am43xx_evm_usbhost_boot_defconfig       | 1 -
 configs/am43xx_hs_evm_defconfig                 | 1 -
 configs/axm_defconfig                           | 1 -
 configs/chiliboard_defconfig                    | 1 -
 configs/corvus_defconfig                        | 1 -
 configs/da850evm_nand_defconfig                 | 1 -
 configs/devkit3250_defconfig                    | 1 -
 configs/devkit8000_defconfig                    | 1 -
 configs/dra7xx_evm_defconfig                    | 1 -
 configs/draco_defconfig                         | 1 -
 configs/etamin_defconfig                        | 1 -
 configs/gardena-smart-gateway-at91sam_defconfig | 1 -
 configs/igep00x0_defconfig                      | 1 -
 configs/m53menlo_defconfig                      | 1 -
 configs/omap35_logic_defconfig                  | 1 -
 configs/omap35_logic_somlv_defconfig            | 1 -
 configs/omap3_beagle_defconfig                  | 1 -
 configs/omap3_evm_defconfig                     | 1 -
 configs/omap3_logic_defconfig                   | 1 -
 configs/omap3_logic_somlv_defconfig             | 1 -
 configs/omapl138_lcdk_defconfig                 | 1 -
 configs/phycore-am335x-r2-regor_defconfig       | 1 -
 configs/phycore-am335x-r2-wega_defconfig        | 1 -
 configs/pxm2_defconfig                          | 1 -
 configs/rastaban_defconfig                      | 1 -
 configs/rut_defconfig                           | 1 -
 configs/sama5d3_xplained_nandflash_defconfig    | 1 -
 configs/sama5d3xek_nandflash_defconfig          | 1 -
 configs/sama5d4_xplained_nandflash_defconfig    | 1 -
 configs/sama5d4ek_nandflash_defconfig           | 1 -
 configs/smartweb_defconfig                      | 1 -
 configs/taurus_defconfig                        | 1 -
 configs/thuban_defconfig                        | 1 -
 drivers/mtd/nand/raw/Kconfig                    | 8 --------
 drivers/mtd/nand/raw/am335x_spl_bch.c           | 3 ++-
 drivers/mtd/nand/raw/atmel_nand.c               | 5 +++--
 drivers/mtd/nand/raw/mxc_nand_spl.c             | 5 +++--
 drivers/mtd/nand/raw/nand_spl_loaders.c         | 2 +-
 drivers/mtd/nand/raw/nand_spl_simple.c          | 5 +++--
 drivers/mtd/nand/raw/omap_gpmc.c                | 3 ++-
 include/system-constants.h                      | 3 +++
 49 files changed, 21 insertions(+), 62 deletions(-)

diff --git a/README b/README
index 60c6b8a19db..00d422737fb 100644
--- a/README
+++ b/README
@@ -1191,11 +1191,10 @@ The following options need to be configured:
 		Support for a lightweight UBI (fastmap) scanner and
 		loader
 
-		CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_COUNT,
-		CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE,
-		CONFIG_SYS_NAND_BLOCK_SIZE, CONFIG_SYS_NAND_BAD_BLOCK_POS,
-		CFG_SYS_NAND_ECCPOS, CFG_SYS_NAND_ECCSIZE,
-		CFG_SYS_NAND_ECCBYTES
+		CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_SIZE,
+		CONFIG_SYS_NAND_OOBSIZE, CONFIG_SYS_NAND_BLOCK_SIZE,
+		CONFIG_SYS_NAND_BAD_BLOCK_POS, CFG_SYS_NAND_ECCPOS,
+		CFG_SYS_NAND_ECCSIZE, CFG_SYS_NAND_ECCBYTES
 		Defines the size and behavior of the NAND that SPL uses
 		to read U-Boot
 
diff --git a/configs/am335x_baltos_defconfig b/configs/am335x_baltos_defconfig
index 16993ef5386..3891e48e9c7 100644
--- a/configs/am335x_baltos_defconfig
+++ b/configs/am335x_baltos_defconfig
@@ -67,7 +67,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am335x_evm_defconfig b/configs/am335x_evm_defconfig
index f048e60f7f3..73f221d55bc 100644
--- a/configs/am335x_evm_defconfig
+++ b/configs/am335x_evm_defconfig
@@ -84,7 +84,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am335x_guardian_defconfig b/configs/am335x_guardian_defconfig
index 01d848ceede..5369e46bc1b 100644
--- a/configs/am335x_guardian_defconfig
+++ b/configs/am335x_guardian_defconfig
@@ -99,7 +99,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0x100
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am335x_igep003x_defconfig b/configs/am335x_igep003x_defconfig
index 4dd6366ef67..4c5c82f9bdd 100644
--- a/configs/am335x_igep003x_defconfig
+++ b/configs/am335x_igep003x_defconfig
@@ -85,7 +85,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_MTD_UBI_FASTMAP=y
diff --git a/configs/am3517_evm_defconfig b/configs/am3517_evm_defconfig
index 0a83ac9378c..111929fb912 100644
--- a/configs/am3517_evm_defconfig
+++ b/configs/am3517_evm_defconfig
@@ -75,7 +75,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/am43xx_evm_defconfig b/configs/am43xx_evm_defconfig
index 6571afd345e..5f2356bcd32 100644
--- a/configs/am43xx_evm_defconfig
+++ b/configs/am43xx_evm_defconfig
@@ -72,7 +72,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am43xx_evm_rtconly_defconfig b/configs/am43xx_evm_rtconly_defconfig
index e84aed4d54d..30681e7eeb6 100644
--- a/configs/am43xx_evm_rtconly_defconfig
+++ b/configs/am43xx_evm_rtconly_defconfig
@@ -62,7 +62,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am43xx_evm_usbhost_boot_defconfig b/configs/am43xx_evm_usbhost_boot_defconfig
index 0cae3242b04..6c4cc99e5ff 100644
--- a/configs/am43xx_evm_usbhost_boot_defconfig
+++ b/configs/am43xx_evm_usbhost_boot_defconfig
@@ -75,7 +75,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/am43xx_hs_evm_defconfig b/configs/am43xx_hs_evm_defconfig
index 370ee967258..49ecb4c9e66 100644
--- a/configs/am43xx_hs_evm_defconfig
+++ b/configs/am43xx_hs_evm_defconfig
@@ -68,7 +68,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/axm_defconfig b/configs/axm_defconfig
index f9c02144c4a..e1a01b24b88 100644
--- a/configs/axm_defconfig
+++ b/configs/axm_defconfig
@@ -86,7 +86,6 @@ CONFIG_MTD=y
 # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
 CONFIG_NAND_ATMEL=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/chiliboard_defconfig b/configs/chiliboard_defconfig
index 06642d282d1..0cd649d643a 100644
--- a/configs/chiliboard_defconfig
+++ b/configs/chiliboard_defconfig
@@ -63,7 +63,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/corvus_defconfig b/configs/corvus_defconfig
index ecf61f0d1e6..5eaa6dbb5fc 100644
--- a/configs/corvus_defconfig
+++ b/configs/corvus_defconfig
@@ -81,7 +81,6 @@ CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT=y
 # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
 CONFIG_NAND_ATMEL=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/da850evm_nand_defconfig b/configs/da850evm_nand_defconfig
index 5f6f5d788d0..62b8edde248 100644
--- a/configs/da850evm_nand_defconfig
+++ b/configs/da850evm_nand_defconfig
@@ -88,7 +88,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_USE_FLASH_BBT=y
 CONFIG_NAND_DAVINCI=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/devkit3250_defconfig b/configs/devkit3250_defconfig
index ec4031da51d..569b156c997 100644
--- a/configs/devkit3250_defconfig
+++ b/configs/devkit3250_defconfig
@@ -73,7 +73,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_USE_FLASH_BBT=y
 CONFIG_NAND_LPC32XX_SLC=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 # CONFIG_SYS_NAND_5_ADDR_CYCLE is not set
diff --git a/configs/devkit8000_defconfig b/configs/devkit8000_defconfig
index ef16da176a9..a7f82442eb0 100644
--- a/configs/devkit8000_defconfig
+++ b/configs/devkit8000_defconfig
@@ -77,7 +77,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/dra7xx_evm_defconfig b/configs/dra7xx_evm_defconfig
index 19ca89fa867..c3a3ec2cd42 100644
--- a/configs/dra7xx_evm_defconfig
+++ b/configs/dra7xx_evm_defconfig
@@ -104,7 +104,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/draco_defconfig b/configs/draco_defconfig
index ee19920a703..2a82087ab31 100644
--- a/configs/draco_defconfig
+++ b/configs/draco_defconfig
@@ -100,7 +100,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/etamin_defconfig b/configs/etamin_defconfig
index c0ce7a33569..4ac0abc56b0 100644
--- a/configs/etamin_defconfig
+++ b/configs/etamin_defconfig
@@ -104,7 +104,6 @@ CONFIG_SYS_MAX_NAND_DEVICE=3
 CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x80000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x80
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/gardena-smart-gateway-at91sam_defconfig b/configs/gardena-smart-gateway-at91sam_defconfig
index 439fcc05753..0fb92ff1638 100644
--- a/configs/gardena-smart-gateway-at91sam_defconfig
+++ b/configs/gardena-smart-gateway-at91sam_defconfig
@@ -99,7 +99,6 @@ CONFIG_MTD=y
 CONFIG_NAND_ATMEL=y
 CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/igep00x0_defconfig b/configs/igep00x0_defconfig
index 993bbe29c6b..3b1af07f057 100644
--- a/configs/igep00x0_defconfig
+++ b/configs/igep00x0_defconfig
@@ -78,7 +78,6 @@ CONFIG_SYS_MTDPARTS_RUNTIME=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/m53menlo_defconfig b/configs/m53menlo_defconfig
index ef9e15d1520..e1ddc0af5db 100644
--- a/configs/m53menlo_defconfig
+++ b/configs/m53menlo_defconfig
@@ -90,7 +90,6 @@ CONFIG_SYS_NAND_USE_FLASH_BBT=y
 CONFIG_NAND_MXC=y
 CONFIG_MXC_NAND_HWECC=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
index 539b0cfa4f7..3a656072534 100644
--- a/configs/omap35_logic_defconfig
+++ b/configs/omap35_logic_defconfig
@@ -74,7 +74,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omap35_logic_somlv_defconfig b/configs/omap35_logic_somlv_defconfig
index f14ce3d23db..868e89114b7 100644
--- a/configs/omap35_logic_somlv_defconfig
+++ b/configs/omap35_logic_somlv_defconfig
@@ -83,7 +83,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omap3_beagle_defconfig b/configs/omap3_beagle_defconfig
index cabd3659385..7f0b927528d 100644
--- a/configs/omap3_beagle_defconfig
+++ b/configs/omap3_beagle_defconfig
@@ -83,7 +83,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omap3_evm_defconfig b/configs/omap3_evm_defconfig
index 33ff39ff548..3434783d45b 100644
--- a/configs/omap3_evm_defconfig
+++ b/configs/omap3_evm_defconfig
@@ -74,7 +74,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
index 7d3d602c9d9..78bfbbd1a22 100644
--- a/configs/omap3_logic_defconfig
+++ b/configs/omap3_logic_defconfig
@@ -73,7 +73,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omap3_logic_somlv_defconfig b/configs/omap3_logic_somlv_defconfig
index 0141a425e07..aefd8861db3 100644
--- a/configs/omap3_logic_somlv_defconfig
+++ b/configs/omap3_logic_somlv_defconfig
@@ -84,7 +84,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/omapl138_lcdk_defconfig b/configs/omapl138_lcdk_defconfig
index 47feff21bd4..63554d56016 100644
--- a/configs/omapl138_lcdk_defconfig
+++ b/configs/omapl138_lcdk_defconfig
@@ -84,7 +84,6 @@ CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_USE_FLASH_BBT=y
 CONFIG_NAND_DAVINCI=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
diff --git a/configs/phycore-am335x-r2-regor_defconfig b/configs/phycore-am335x-r2-regor_defconfig
index 4bdf8822181..85d53d06357 100644
--- a/configs/phycore-am335x-r2-regor_defconfig
+++ b/configs/phycore-am335x-r2-regor_defconfig
@@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/phycore-am335x-r2-wega_defconfig b/configs/phycore-am335x-r2-wega_defconfig
index 7644fbec273..60607548a25 100644
--- a/configs/phycore-am335x-r2-wega_defconfig
+++ b/configs/phycore-am335x-r2-wega_defconfig
@@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/pxm2_defconfig b/configs/pxm2_defconfig
index 2ea007d0de5..e1d1066815b 100644
--- a/configs/pxm2_defconfig
+++ b/configs/pxm2_defconfig
@@ -99,7 +99,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/rastaban_defconfig b/configs/rastaban_defconfig
index 9f538a2f83d..21495341e26 100644
--- a/configs/rastaban_defconfig
+++ b/configs/rastaban_defconfig
@@ -100,7 +100,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/rut_defconfig b/configs/rut_defconfig
index ccf25667b50..248073be1f2 100644
--- a/configs/rut_defconfig
+++ b/configs/rut_defconfig
@@ -99,7 +99,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/sama5d3_xplained_nandflash_defconfig b/configs/sama5d3_xplained_nandflash_defconfig
index a1b6122f8eb..a0802f9c5fe 100644
--- a/configs/sama5d3_xplained_nandflash_defconfig
+++ b/configs/sama5d3_xplained_nandflash_defconfig
@@ -86,7 +86,6 @@ CONFIG_PMECC_CAP=4
 CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/sama5d3xek_nandflash_defconfig b/configs/sama5d3xek_nandflash_defconfig
index 6278a6c68a7..d96bb91e65d 100644
--- a/configs/sama5d3xek_nandflash_defconfig
+++ b/configs/sama5d3xek_nandflash_defconfig
@@ -90,7 +90,6 @@ CONFIG_PMECC_CAP=4
 CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/sama5d4_xplained_nandflash_defconfig b/configs/sama5d4_xplained_nandflash_defconfig
index d12f749d7f4..68101a12fc2 100644
--- a/configs/sama5d4_xplained_nandflash_defconfig
+++ b/configs/sama5d4_xplained_nandflash_defconfig
@@ -84,7 +84,6 @@ CONFIG_PMECC_CAP=8
 CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/sama5d4ek_nandflash_defconfig b/configs/sama5d4ek_nandflash_defconfig
index 27bfcdf0084..b8062db775d 100644
--- a/configs/sama5d4ek_nandflash_defconfig
+++ b/configs/sama5d4ek_nandflash_defconfig
@@ -81,7 +81,6 @@ CONFIG_PMECC_CAP=8
 CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x1000
 CONFIG_SYS_NAND_OOBSIZE=0xe0
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/smartweb_defconfig b/configs/smartweb_defconfig
index 10cbccb69b0..80d2c0f1cc1 100644
--- a/configs/smartweb_defconfig
+++ b/configs/smartweb_defconfig
@@ -84,7 +84,6 @@ CONFIG_MTD=y
 # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
 CONFIG_NAND_ATMEL=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/taurus_defconfig b/configs/taurus_defconfig
index cc5755178b7..9be30c8d8a4 100644
--- a/configs/taurus_defconfig
+++ b/configs/taurus_defconfig
@@ -94,7 +94,6 @@ CONFIG_MTD=y
 # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
 CONFIG_NAND_ATMEL=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/configs/thuban_defconfig b/configs/thuban_defconfig
index 116700e0df0..1134624f9d2 100644
--- a/configs/thuban_defconfig
+++ b/configs/thuban_defconfig
@@ -100,7 +100,6 @@ CONFIG_MTD=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
-CONFIG_SYS_NAND_PAGE_COUNT=0x40
 CONFIG_SYS_NAND_PAGE_SIZE=0x800
 CONFIG_SYS_NAND_OOBSIZE=0x40
 CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index 72547f00fbe..ee484dc0f51 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -660,14 +660,6 @@ config SYS_NAND_ONFI_DETECTION
 	  And fetching device parameters flashed on device, by parsing
 	  ONFI parameter page.
 
-config SYS_NAND_PAGE_COUNT
-	hex "NAND chip page count"
-	depends on SPL_NAND_SUPPORT && (NAND_ATMEL || NAND_MXC || \
-		SPL_NAND_AM33XX_BCH || SPL_NAND_LOAD || SPL_NAND_SIMPLE || \
-		NAND_OMAP_GPMC)
-	help
-	  Number of pages in the NAND chip.
-
 config SYS_NAND_PAGE_SIZE
 	hex "NAND chip page size"
 	depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
diff --git a/drivers/mtd/nand/raw/am335x_spl_bch.c b/drivers/mtd/nand/raw/am335x_spl_bch.c
index 6ab3f1f42c5..3843ffc9a24 100644
--- a/drivers/mtd/nand/raw/am335x_spl_bch.c
+++ b/drivers/mtd/nand/raw/am335x_spl_bch.c
@@ -11,6 +11,7 @@
 
 #include <common.h>
 #include <nand.h>
+#include <system-constants.h>
 #include <asm/io.h>
 #include <linux/delay.h>
 #include <linux/mtd/nand_ecc.h>
@@ -32,7 +33,7 @@ static int nand_command(int block, int page, uint32_t offs,
 	u8 cmd)
 {
 	struct nand_chip *this = mtd_to_nand(mtd);
-	int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
+	int page_addr = page + block * SYS_NAND_PAGE_COUNT;
 	void (*hwctrl)(struct mtd_info *mtd, int cmd,
 			unsigned int ctrl) = this->cmd_ctrl;
 
diff --git a/drivers/mtd/nand/raw/atmel_nand.c b/drivers/mtd/nand/raw/atmel_nand.c
index 6b17e744a69..cfc6578ae83 100644
--- a/drivers/mtd/nand/raw/atmel_nand.c
+++ b/drivers/mtd/nand/raw/atmel_nand.c
@@ -12,6 +12,7 @@
 
 #include <common.h>
 #include <log.h>
+#include <system-constants.h>
 #include <asm/gpio.h>
 #include <asm/arch/gpio.h>
 #include <dm/device_compat.h>
@@ -1258,7 +1259,7 @@ static struct nand_chip nand_chip;
 static int nand_command(int block, int page, uint32_t offs, u8 cmd)
 {
 	struct nand_chip *this = mtd_to_nand(mtd);
-	int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
+	int page_addr = page + block * SYS_NAND_PAGE_COUNT;
 	void (*hwctrl)(struct mtd_info *mtd, int cmd,
 			unsigned int ctrl) = this->cmd_ctrl;
 
@@ -1359,7 +1360,7 @@ int spl_nand_erase_one(int block, int page)
 	if (nand_chip.select_chip)
 		nand_chip.select_chip(mtd, 0);
 
-	page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
+	page_addr = page + block * SYS_NAND_PAGE_COUNT;
 	hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
 	/* Row address */
 	hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
diff --git a/drivers/mtd/nand/raw/mxc_nand_spl.c b/drivers/mtd/nand/raw/mxc_nand_spl.c
index 309e75d01e5..059bf38f50e 100644
--- a/drivers/mtd/nand/raw/mxc_nand_spl.c
+++ b/drivers/mtd/nand/raw/mxc_nand_spl.c
@@ -13,6 +13,7 @@
 #include <common.h>
 #include <hang.h>
 #include <nand.h>
+#include <system-constants.h>
 #include <linux/mtd/rawnand.h>
 #include <asm/arch/imx-regs.h>
 #include <asm/io.h>
@@ -304,13 +305,13 @@ int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
 		 * Check if we have crossed a block boundary, and if so
 		 * check for bad block.
 		 */
-		if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
+		if (!(page % SYS_NAND_PAGE_COUNT)) {
 			/*
 			 * Yes, new block. See if this block is good. If not,
 			 * loop until we find a good block.
 			 */
 			while (is_badblock(page)) {
-				page = page + CONFIG_SYS_NAND_PAGE_COUNT;
+				page = page + SYS_NAND_PAGE_COUNT;
 				/* Check i we've reached the end of flash. */
 				if (page >= maxpages)
 					return -1;
diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
index 156b44d8358..8848cb27db9 100644
--- a/drivers/mtd/nand/raw/nand_spl_loaders.c
+++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
@@ -12,7 +12,7 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
 	while (block <= lastblock) {
 		if (!nand_is_bad_block(block)) {
 			/* Skip bad blocks */
-			while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
+			while (page < SYS_NAND_PAGE_COUNT) {
 				nand_read_page(block, page, dst);
 				/*
 				 * When offs is not aligned to page address the
diff --git a/drivers/mtd/nand/raw/nand_spl_simple.c b/drivers/mtd/nand/raw/nand_spl_simple.c
index 2f3af9edd4c..2ebcac56900 100644
--- a/drivers/mtd/nand/raw/nand_spl_simple.c
+++ b/drivers/mtd/nand/raw/nand_spl_simple.c
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <nand.h>
+#include <system-constants.h>
 #include <asm/io.h>
 #include <linux/mtd/nand_ecc.h>
 #include <linux/mtd/rawnand.h>
@@ -27,7 +28,7 @@ static int nand_command(int block, int page, uint32_t offs,
 	u8 cmd)
 {
 	struct nand_chip *this = mtd_to_nand(mtd);
-	int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
+	int page_addr = page + block * SYS_NAND_PAGE_COUNT;
 
 	while (!this->dev_ready(mtd))
 		;
@@ -59,7 +60,7 @@ static int nand_command(int block, int page, uint32_t offs,
 	u8 cmd)
 {
 	struct nand_chip *this = mtd_to_nand(mtd);
-	int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
+	int page_addr = page + block * SYS_NAND_PAGE_COUNT;
 	void (*hwctrl)(struct mtd_info *mtd, int cmd,
 			unsigned int ctrl) = this->cmd_ctrl;
 
diff --git a/drivers/mtd/nand/raw/omap_gpmc.c b/drivers/mtd/nand/raw/omap_gpmc.c
index 1a5ed0de31a..6e99538dea1 100644
--- a/drivers/mtd/nand/raw/omap_gpmc.c
+++ b/drivers/mtd/nand/raw/omap_gpmc.c
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <log.h>
+#include <system-constants.h>
 #include <asm/io.h>
 #include <dm/uclass.h>
 #include <linux/errno.h>
@@ -1298,7 +1299,7 @@ static int nand_is_bad_block(int block)
 
 static int nand_read_page(int block, int page, uchar *dst)
 {
-	int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
+	int page_addr = block * SYS_NAND_PAGE_COUNT + page;
 	loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
 	int ret;
 	size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
diff --git a/include/system-constants.h b/include/system-constants.h
index 59371568d1e..c50d54f37a4 100644
--- a/include/system-constants.h
+++ b/include/system-constants.h
@@ -41,4 +41,7 @@
 #define SPL_PAYLOAD_ARGS_ADDR	0
 #endif
 
+#define SYS_NAND_PAGE_COUNT \
+	(CONFIG_SYS_NAND_BLOCK_SIZE / CONFIG_SYS_NAND_PAGE_SIZE)
+
 #endif
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (2 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29 13:08   ` Michael Nazzareno Trimarchi
  2023-10-29  3:48 ` [PATCH 05/15] spl: legacy: Honor bl_len when decompressing Sean Anderson
                   ` (11 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

All other implementations of nand_spl_load_image only read as many pages as
are necessary to load the image. However, nand_spl_loaders.c loads the full
block. Align it with other load functions so that it is easier to
determine how large of a load buffer we need.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 drivers/mtd/nand/raw/nand_spl_loaders.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
index 8848cb27db9..f071b5b57f5 100644
--- a/drivers/mtd/nand/raw/nand_spl_loaders.c
+++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
@@ -12,8 +12,11 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
 	while (block <= lastblock) {
 		if (!nand_is_bad_block(block)) {
 			/* Skip bad blocks */
-			while (page < SYS_NAND_PAGE_COUNT) {
+			while (size && page < SYS_NAND_PAGE_COUNT) {
 				nand_read_page(block, page, dst);
+
+				size -= min(size, CONFIG_SYS_NAND_PAGE_SIZE -
+						  page_offset);
 				/*
 				 * When offs is not aligned to page address the
 				 * extra offset is copied to dst as well. Copy
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 05/15] spl: legacy: Honor bl_len when decompressing
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (3 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 06/15] spl: nand: Set bl_len to page size Sean Anderson
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, Daniel Schwierzeck, Weijie Gao

When allocating a buffer to load compressed data into, we need to ensure we
have enough space for over- and under-flow due to alignment. Otherwise we
will clobber the malloc bookkeeping data. Calculate the correct amount of
overhead and use it when determining the size.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 common/spl/spl_legacy.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/common/spl/spl_legacy.c b/common/spl/spl_legacy.c
index 51656fb9617..9189576b774 100644
--- a/common/spl/spl_legacy.c
+++ b/common/spl/spl_legacy.c
@@ -133,25 +133,31 @@ int spl_load_legacy_img(struct spl_image_info *spl_image,
 			   map_sysmem(spl_image->load_addr, spl_image->size));
 		break;
 
-	case IH_COMP_LZMA:
+	case IH_COMP_LZMA: {
+		ulong overhead, size;
+
 		lzma_len = LZMA_LEN;
 
 		/* dataptr points to compressed payload  */
-		dataptr = offset + sizeof(*hdr);
+		dataptr = ALIGN_DOWN(sizeof(*hdr), load->bl_len);
+		overhead = sizeof(*hdr) - dataptr;
+		size = ALIGN(spl_image->size + overhead, load->bl_len);
+		dataptr += offset;
 
 		debug("LZMA: Decompressing %08lx to %08lx\n",
 		      dataptr, spl_image->load_addr);
-		src = malloc(spl_image->size);
+		src = malloc(size);
 		if (!src) {
 			printf("Unable to allocate %d bytes for LZMA\n",
 			       spl_image->size);
 			return -ENOMEM;
 		}
 
-		load->read(load, dataptr, spl_image->size, src);
+		load->read(load, dataptr, size, src);
 		ret = lzmaBuffToBuffDecompress(map_sysmem(spl_image->load_addr,
 							  spl_image->size),
-					       &lzma_len, src, spl_image->size);
+					       &lzma_len, src + overhead,
+					       spl_image->size);
 		if (ret) {
 			printf("LZMA decompression error: %d\n", ret);
 			return ret;
@@ -159,7 +165,7 @@ int spl_load_legacy_img(struct spl_image_info *spl_image,
 
 		spl_image->size = lzma_len;
 		break;
-
+	}
 	default:
 		debug("Compression method %s is not supported\n",
 		      genimg_get_comp_short_name(image_get_comp(hdr)));
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 06/15] spl: nand: Set bl_len to page size
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (4 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 05/15] spl: legacy: Honor bl_len when decompressing Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 07/15] cmd: nand: Map memory before accessing it Sean Anderson
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, GSS_MTK_Uboot_upstream, Weijie Gao

Since commit 34793598c83 ("mtd: nand: mxs_nand_spl: Remove the page aligned
access") there are no longer any users of nand_get_mtd. However, it is
still important to know what the page size is so we can allocate a
large-enough buffer. If the image size is not page-aligned, we will go off
the end of the buffer and clobber some memory.

Introduce a new function nand_page_size which returns the page size. For
most drivers it is easy to determine the page size. However, a few need to
be modified since they only keep the page size around temporarily.

It's possible that this patch could cause a regression on some platforms if
the offset is non-aligned and there is invalid address space immediately
before the load address. spl_load_legacy_img does not (except when
compressing) respect bl_len, so only boards with SPL_LOAD_FIT (8 boards) or
SPL_LOAD_IMX_CONTAINER (none in tree) would be affected.

defconfig               CONFIG_TEXT_BASE
======================= ================
am335x_evm              0x80800000
am43xx_evm              0x80800000
am43xx_evm_rtconly      0x80800000
am43xx_evm_usbhost_boot 0x80800000
am43xx_hs_evm           0x80800000
dra7xx_evm              0x80800000
gwventana_nand          0x17800000
imx8mn_bsh_smm_s2       0x40200000

All the sitara boards have DDR mapped at 0x80000000. gwventana is an i.MX6Q
which has DDR at 0x10000000. I don't have the IMX8MNRM handy, but on the
i.MX8M DDR starts at 0x40000000. Therefore all of these boards can handle a
little underflow.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 common/spl/spl_nand.c                   | 11 +++--------
 drivers/mtd/nand/raw/am335x_spl_bch.c   |  5 +++++
 drivers/mtd/nand/raw/atmel_nand.c       |  5 +++++
 drivers/mtd/nand/raw/denali_spl.c       |  5 +++++
 drivers/mtd/nand/raw/fsl_ifc_spl.c      |  8 ++++++++
 drivers/mtd/nand/raw/lpc32xx_nand_mlc.c |  5 +++++
 drivers/mtd/nand/raw/mt7621_nand_spl.c  |  5 +++++
 drivers/mtd/nand/raw/mxc_nand_spl.c     |  5 +++++
 drivers/mtd/nand/raw/mxs_nand_spl.c     |  5 +++++
 drivers/mtd/nand/raw/nand.c             |  7 +++++++
 drivers/mtd/nand/raw/nand_spl_simple.c  |  5 +++++
 drivers/mtd/nand/raw/sunxi_nand_spl.c   |  8 +++++++-
 include/nand.h                          |  1 +
 13 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/common/spl/spl_nand.c b/common/spl/spl_nand.c
index a19236d9e6d..509e6047999 100644
--- a/common/spl/spl_nand.c
+++ b/common/spl/spl_nand.c
@@ -72,23 +72,18 @@ static ulong spl_nand_legacy_read(struct spl_load_info *load, ulong offs,
 	return size;
 }
 
-struct mtd_info * __weak nand_get_mtd(void)
-{
-	return NULL;
-}
-
 static int spl_nand_load_element(struct spl_image_info *spl_image,
 				 struct spl_boot_device *bootdev,
 				 int offset, struct legacy_img_hdr *header)
 {
-	struct mtd_info *mtd = nand_get_mtd();
-	int bl_len = mtd ? mtd->writesize : 1;
+	int bl_len;
 	int err;
 
 	err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
 	if (err)
 		return err;
 
+	bl_len = nand_page_size();
 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
 	    image_get_magic(header) == FDT_MAGIC) {
 		struct spl_load_info load;
@@ -118,7 +113,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
 		load.dev = NULL;
 		load.priv = &offset;
 		load.filename = NULL;
-		load.bl_len = 1;
+		load.bl_len = IS_ENABLED(CONFIG_SPL_LZMA) ? bl_len : 1;
 		load.read = spl_nand_legacy_read;
 
 		return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
diff --git a/drivers/mtd/nand/raw/am335x_spl_bch.c b/drivers/mtd/nand/raw/am335x_spl_bch.c
index 3843ffc9a24..df84fb3955a 100644
--- a/drivers/mtd/nand/raw/am335x_spl_bch.c
+++ b/drivers/mtd/nand/raw/am335x_spl_bch.c
@@ -218,6 +218,11 @@ void nand_init(void)
 	nand_command(0, 0, 0, NAND_CMD_RESET);
 }
 
+unsigned int nand_page_size(void)
+{
+	return nand_to_mtd(&nand_chip)->writesize;
+}
+
 /* Unselect after operation */
 void nand_deselect(void)
 {
diff --git a/drivers/mtd/nand/raw/atmel_nand.c b/drivers/mtd/nand/raw/atmel_nand.c
index cfc6578ae83..f43ef00cb98 100644
--- a/drivers/mtd/nand/raw/atmel_nand.c
+++ b/drivers/mtd/nand/raw/atmel_nand.c
@@ -1452,6 +1452,11 @@ void nand_init(void)
 		nand_chip.select_chip(mtd, 0);
 }
 
+unsigned int nand_page_size(void)
+{
+	return nand_to_mtd(&nand_chip)->writesize;
+}
+
 void nand_deselect(void)
 {
 	if (nand_chip.select_chip)
diff --git a/drivers/mtd/nand/raw/denali_spl.c b/drivers/mtd/nand/raw/denali_spl.c
index 690279c9976..165a23312cb 100644
--- a/drivers/mtd/nand/raw/denali_spl.c
+++ b/drivers/mtd/nand/raw/denali_spl.c
@@ -234,4 +234,9 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
 	return 0;
 }
 
+unsigned int nand_page_size(void)
+{
+	return page_size;
+}
+
 void nand_deselect(void) {}
diff --git a/drivers/mtd/nand/raw/fsl_ifc_spl.c b/drivers/mtd/nand/raw/fsl_ifc_spl.c
index c67065eaf8c..69d26f1f79a 100644
--- a/drivers/mtd/nand/raw/fsl_ifc_spl.c
+++ b/drivers/mtd/nand/raw/fsl_ifc_spl.c
@@ -106,6 +106,8 @@ static inline int bad_block(uchar *marker, int port_size)
 		return __raw_readw((u16 *)marker) != 0xffff;
 }
 
+static int saved_page_size;
+
 int nand_spl_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
 {
 	struct fsl_ifc_fcm *gregs = (void *)CFG_SYS_IFC_ADDR;
@@ -150,6 +152,7 @@ int nand_spl_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
 		if (port_size == 8)
 			bad_marker = 5;
 	}
+	saved_page_size = page_size;
 
 	ver = ifc_in32(&gregs->ifc_rev);
 	if (ver >= FSL_IFC_V2_0_0)
@@ -302,6 +305,11 @@ void nand_init(void)
 {
 }
 
+unsigned int nand_page_size(void)
+{
+	return saved_page_size;
+}
+
 void nand_deselect(void)
 {
 }
diff --git a/drivers/mtd/nand/raw/lpc32xx_nand_mlc.c b/drivers/mtd/nand/raw/lpc32xx_nand_mlc.c
index ac2e669d46b..f8ae216d56c 100644
--- a/drivers/mtd/nand/raw/lpc32xx_nand_mlc.c
+++ b/drivers/mtd/nand/raw/lpc32xx_nand_mlc.c
@@ -765,4 +765,9 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
 	return 0;
 }
 
+unsigned int nand_page_size(void)
+{
+	return BYTES_PER_PAGE;
+}
+
 #endif /* CONFIG_SPL_BUILD */
diff --git a/drivers/mtd/nand/raw/mt7621_nand_spl.c b/drivers/mtd/nand/raw/mt7621_nand_spl.c
index 114fc8b7cea..a2be9ba80e0 100644
--- a/drivers/mtd/nand/raw/mt7621_nand_spl.c
+++ b/drivers/mtd/nand/raw/mt7621_nand_spl.c
@@ -203,6 +203,11 @@ unsigned long nand_size(void)
 	return SZ_2G;
 }
 
+unsigned int nand_page_size(void)
+{
+	return nfc_dev.nand.mtd.writesize;
+}
+
 void nand_deselect(void)
 {
 }
diff --git a/drivers/mtd/nand/raw/mxc_nand_spl.c b/drivers/mtd/nand/raw/mxc_nand_spl.c
index 059bf38f50e..2d89d4dbfc7 100644
--- a/drivers/mtd/nand/raw/mxc_nand_spl.c
+++ b/drivers/mtd/nand/raw/mxc_nand_spl.c
@@ -351,3 +351,8 @@ __used void nand_boot(void)
 
 void nand_init(void) {}
 void nand_deselect(void) {}
+
+unsigned int nand_page_size(void)
+{
+	return CONFIG_SYS_NAND_PAGE_SIZE;
+}
diff --git a/drivers/mtd/nand/raw/mxs_nand_spl.c b/drivers/mtd/nand/raw/mxs_nand_spl.c
index 300662994cf..f7d3f02f85a 100644
--- a/drivers/mtd/nand/raw/mxs_nand_spl.c
+++ b/drivers/mtd/nand/raw/mxs_nand_spl.c
@@ -295,6 +295,11 @@ int nand_default_bbt(struct mtd_info *mtd)
 	return 0;
 }
 
+unsigned int nand_page_size(void)
+{
+	return nand_to_mtd(&nand_chip)->writesize;
+}
+
 void nand_deselect(void)
 {
 }
diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
index eacd99c4e27..4da41438790 100644
--- a/drivers/mtd/nand/raw/nand.c
+++ b/drivers/mtd/nand/raw/nand.c
@@ -174,3 +174,10 @@ void nand_init(void)
 
 	create_mtd_concat();
 }
+
+unsigned int nand_page_size(void)
+{
+	struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
+
+	return mtd ? mtd->writesize : 1;
+}
diff --git a/drivers/mtd/nand/raw/nand_spl_simple.c b/drivers/mtd/nand/raw/nand_spl_simple.c
index 2ebcac56900..3d3d1d39cd5 100644
--- a/drivers/mtd/nand/raw/nand_spl_simple.c
+++ b/drivers/mtd/nand/raw/nand_spl_simple.c
@@ -227,6 +227,11 @@ void nand_init(void)
 		nand_chip.select_chip(mtd, 0);
 }
 
+unsigned int nand_page_size(void)
+{
+	return nand_to_mtd(&nand_chip)->writesize;
+}
+
 /* Unselect after operation */
 void nand_deselect(void)
 {
diff --git a/drivers/mtd/nand/raw/sunxi_nand_spl.c b/drivers/mtd/nand/raw/sunxi_nand_spl.c
index 6de0b0a3554..c9b8c78ed75 100644
--- a/drivers/mtd/nand/raw/sunxi_nand_spl.c
+++ b/drivers/mtd/nand/raw/sunxi_nand_spl.c
@@ -524,9 +524,10 @@ static int nand_read_buffer(struct nfc_config *conf, uint32_t offs,
 	return 0;
 }
 
+static struct nfc_config conf;
+
 int nand_spl_load_image(uint32_t offs, unsigned int size, void *dest)
 {
-	static struct nfc_config conf = { };
 	int ret;
 
 	ret = nand_detect_config(&conf, offs, dest);
@@ -536,6 +537,11 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dest)
 	return nand_read_buffer(&conf, offs, size, dest);
 }
 
+unsigned int nand_page_size(void)
+{
+	return conf.page_size;
+}
+
 void nand_deselect(void)
 {
 	struct sunxi_ccm_reg *const ccm =
diff --git a/include/nand.h b/include/nand.h
index 70c1286ccb4..c1d7533aaac 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -12,6 +12,7 @@
 
 extern void nand_init(void);
 unsigned long nand_size(void);
+unsigned int nand_page_size(void);
 
 #include <linux/compat.h>
 #include <linux/mtd/mtd.h>
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 07/15] cmd: nand: Map memory before accessing it
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (5 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 06/15] spl: nand: Set bl_len to page size Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 08/15] spl: " Sean Anderson
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

In sandbox, all memory must be mapped before accessing it. Do so for the
nand command.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 cmd/nand.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/cmd/nand.c b/cmd/nand.c
index 71b8f964429..fe834c4ac5c 100644
--- a/cmd/nand.c
+++ b/cmd/nand.c
@@ -34,6 +34,7 @@
 #include <env.h>
 #include <watchdog.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <asm/byteorder.h>
 #include <jffs2/jffs2.h>
 #include <nand.h>
@@ -432,7 +433,7 @@ static void nand_print_and_set_info(int idx)
 	env_set_hex("nand_erasesize", mtd->erasesize);
 }
 
-static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
+static int raw_access(struct mtd_info *mtd, void *buf, loff_t off,
 		      ulong count, int read, int no_verify)
 {
 	int ret = 0;
@@ -440,8 +441,8 @@ static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
 	while (count--) {
 		/* Raw access */
 		mtd_oob_ops_t ops = {
-			.datbuf = (u8 *)addr,
-			.oobbuf = ((u8 *)addr) + mtd->writesize,
+			.datbuf = buf,
+			.oobbuf = buf + mtd->writesize,
 			.len = mtd->writesize,
 			.ooblen = mtd->oobsize,
 			.mode = MTD_OPS_RAW
@@ -461,7 +462,7 @@ static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
 			break;
 		}
 
-		addr += mtd->writesize + mtd->oobsize;
+		buf += mtd->writesize + mtd->oobsize;
 		off += mtd->writesize;
 	}
 
@@ -675,6 +676,7 @@ static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
 		int read;
 		int raw = 0;
 		int no_verify = 0;
+		void *buf;
 
 		if (argc < 4)
 			goto usage;
@@ -730,32 +732,32 @@ static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
 		}
 
 		mtd = get_nand_dev_by_index(dev);
+		buf = map_sysmem(addr, maxsize);
 
 		if (!s || !strcmp(s, ".jffs2") ||
 		    !strcmp(s, ".e") || !strcmp(s, ".i")) {
 			if (read)
 				ret = nand_read_skip_bad(mtd, off, &rwsize,
-							 NULL, maxsize,
-							 (u_char *)addr);
+							 NULL, maxsize, buf);
 			else
 				ret = nand_write_skip_bad(mtd, off, &rwsize,
-							  NULL, maxsize,
-							  (u_char *)addr,
+							  NULL, maxsize, buf,
 							  WITH_WR_VERIFY);
 #ifdef CONFIG_CMD_NAND_TRIMFFS
 		} else if (!strcmp(s, ".trimffs")) {
 			if (read) {
 				printf("Unknown nand command suffix '%s'\n", s);
+				unmap_sysmem(buf);
 				return 1;
 			}
 			ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
-						maxsize, (u_char *)addr,
+						maxsize, buf,
 						WITH_DROP_FFS | WITH_WR_VERIFY);
 #endif
 		} else if (!strcmp(s, ".oob")) {
 			/* out-of-band data */
 			mtd_oob_ops_t ops = {
-				.oobbuf = (u8 *)addr,
+				.oobbuf = buf,
 				.ooblen = rwsize,
 				.mode = MTD_OPS_RAW
 			};
@@ -765,13 +767,15 @@ static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
 			else
 				ret = mtd_write_oob(mtd, off, &ops);
 		} else if (raw) {
-			ret = raw_access(mtd, addr, off, pagecount, read,
+			ret = raw_access(mtd, buf, off, pagecount, read,
 					 no_verify);
 		} else {
 			printf("Unknown nand command suffix '%s'.\n", s);
+			unmap_sysmem(buf);
 			return 1;
 		}
 
+		unmap_sysmem(buf);
 		printf(" %zu bytes %s: %s\n", rwsize,
 		       read ? "read" : "written", ret ? "ERROR" : "OK");
 
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 08/15] spl: nand: Map memory before accessing it
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (6 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 07/15] cmd: nand: Map memory before accessing it Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 09/15] mtd: Rename SPL_MTD_SUPPORT to SPL_MTD Sean Anderson
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

In sandbox we must map memory before accessing it. Do so for the NAND load
method.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 common/spl/spl_nand.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/common/spl/spl_nand.c b/common/spl/spl_nand.c
index 509e6047999..ff47767fe72 100644
--- a/common/spl/spl_nand.c
+++ b/common/spl/spl_nand.c
@@ -11,6 +11,7 @@
 #include <log.h>
 #include <spl.h>
 #include <asm/io.h>
+#include <mapmem.h>
 #include <nand.h>
 #include <linux/libfdt_env.h>
 #include <fdt.h>
@@ -32,7 +33,8 @@ static int spl_nand_load_image(struct spl_image_info *spl_image,
 
 	nand_spl_load_image(spl_nand_get_uboot_raw_page(),
 			    CFG_SYS_NAND_U_BOOT_SIZE,
-			    (void *)CFG_SYS_NAND_U_BOOT_DST);
+			    map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
+				       CFG_SYS_NAND_U_BOOT_SIZE));
 	spl_set_header_raw_uboot(spl_image);
 	nand_deselect();
 
@@ -122,7 +124,8 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
 		if (err)
 			return err;
 		return nand_spl_load_image(offset, spl_image->size,
-					   (void *)(ulong)spl_image->load_addr);
+					   map_sysmem(spl_image->load_addr,
+						      spl_image->size));
 	}
 }
 
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 09/15] mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (7 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 08/15] spl: " Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device Sean Anderson
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

Rename SPL_MTD_SUPPORT to SPL_MTD in order to match MTD. This allows using
CONFIG_IS_ENABLED to test for MTD support.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 common/spl/Kconfig                        | 2 +-
 configs/am335x_baltos_defconfig           | 2 +-
 configs/am335x_evm_defconfig              | 2 +-
 configs/am335x_evm_spiboot_defconfig      | 2 +-
 configs/am335x_hs_evm_defconfig           | 2 +-
 configs/am335x_hs_evm_uart_defconfig      | 2 +-
 configs/am335x_igep003x_defconfig         | 2 +-
 configs/am335x_sl50_defconfig             | 2 +-
 configs/am3517_evm_defconfig              | 2 +-
 configs/am43xx_evm_defconfig              | 2 +-
 configs/am43xx_evm_rtconly_defconfig      | 2 +-
 configs/am43xx_evm_usbhost_boot_defconfig | 2 +-
 configs/am43xx_hs_evm_defconfig           | 2 +-
 configs/am62ax_evm_r5_defconfig           | 2 +-
 configs/am65x_evm_a53_defconfig           | 2 +-
 configs/cm_t43_defconfig                  | 2 +-
 configs/igep00x0_defconfig                | 2 +-
 configs/imx6ulz_smm_m2_defconfig          | 2 +-
 configs/imx8mn_bsh_smm_s2_defconfig       | 2 +-
 configs/j7200_evm_a72_defconfig           | 2 +-
 configs/j7200_evm_r5_defconfig            | 2 +-
 configs/j721e_evm_a72_defconfig           | 2 +-
 configs/j721e_evm_r5_defconfig            | 2 +-
 configs/j721s2_evm_a72_defconfig          | 2 +-
 configs/j721s2_evm_r5_defconfig           | 2 +-
 configs/omap35_logic_defconfig            | 2 +-
 configs/omap35_logic_somlv_defconfig      | 2 +-
 configs/omap3_beagle_defconfig            | 2 +-
 configs/omap3_evm_defconfig               | 2 +-
 configs/omap3_logic_defconfig             | 2 +-
 configs/omap3_logic_somlv_defconfig       | 2 +-
 configs/phycore-am335x-r2-regor_defconfig | 2 +-
 configs/phycore-am335x-r2-wega_defconfig  | 2 +-
 configs/socfpga_secu1_defconfig           | 2 +-
 configs/stm32746g-eval_spl_defconfig      | 2 +-
 configs/stm32f746-disco_spl_defconfig     | 2 +-
 configs/stm32f769-disco_spl_defconfig     | 2 +-
 configs/stm32mp15_basic_defconfig         | 2 +-
 configs/stm32mp15_dhcom_basic_defconfig   | 2 +-
 configs/stm32mp15_dhcor_basic_defconfig   | 2 +-
 drivers/mtd/Makefile                      | 2 +-
 include/mtd/cfi_flash.h                   | 2 +-
 42 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/common/spl/Kconfig b/common/spl/Kconfig
index 25cd18afda7..00332cf243a 100644
--- a/common/spl/Kconfig
+++ b/common/spl/Kconfig
@@ -857,7 +857,7 @@ config SPL_MPC8XXX_INIT_DDR
 	  allows DRAM to be set up before loading U-Boot into that DRAM,
 	  where it can run.
 
-config SPL_MTD_SUPPORT
+config SPL_MTD
 	bool "Support MTD drivers"
 	help
 	  Enable support for MTD (Memory Technology Device) within SPL. MTD
diff --git a/configs/am335x_baltos_defconfig b/configs/am335x_baltos_defconfig
index 3891e48e9c7..0599ae217e6 100644
--- a/configs/am335x_baltos_defconfig
+++ b/configs/am335x_baltos_defconfig
@@ -22,7 +22,7 @@ CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am335x_evm_defconfig b/configs/am335x_evm_defconfig
index 73f221d55bc..5d38dad35bb 100644
--- a/configs/am335x_evm_defconfig
+++ b/configs/am335x_evm_defconfig
@@ -26,7 +26,7 @@ CONFIG_SPL_FIT_IMAGE_TINY=y
 CONFIG_SPL_ETH=y
 # CONFIG_SPL_FS_EXT4 is not set
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_MUSB_NEW=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
diff --git a/configs/am335x_evm_spiboot_defconfig b/configs/am335x_evm_spiboot_defconfig
index 9866246aa51..b5b11fb62c8 100644
--- a/configs/am335x_evm_spiboot_defconfig
+++ b/configs/am335x_evm_spiboot_defconfig
@@ -28,7 +28,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FIT_IMAGE_TINY=y
 # CONFIG_SPL_FS_EXT4 is not set
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_SPI_LOAD=y
diff --git a/configs/am335x_hs_evm_defconfig b/configs/am335x_hs_evm_defconfig
index b961b6c41f0..ea46e58be2d 100644
--- a/configs/am335x_hs_evm_defconfig
+++ b/configs/am335x_hs_evm_defconfig
@@ -27,7 +27,7 @@ CONFIG_SPL_FIT_IMAGE_TINY=y
 # CONFIG_SPL_ENV_SUPPORT is not set
 # CONFIG_SPL_FS_EXT4 is not set
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
diff --git a/configs/am335x_hs_evm_uart_defconfig b/configs/am335x_hs_evm_uart_defconfig
index b5d8eac9f3d..7886557c8da 100644
--- a/configs/am335x_hs_evm_uart_defconfig
+++ b/configs/am335x_hs_evm_uart_defconfig
@@ -29,7 +29,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FIT_IMAGE_TINY=y
 # CONFIG_SPL_ENV_SUPPORT is not set
 # CONFIG_SPL_FS_EXT4 is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
diff --git a/configs/am335x_igep003x_defconfig b/configs/am335x_igep003x_defconfig
index 4c5c82f9bdd..e2c5b70c405 100644
--- a/configs/am335x_igep003x_defconfig
+++ b/configs/am335x_igep003x_defconfig
@@ -24,7 +24,7 @@ CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am335x_sl50_defconfig b/configs/am335x_sl50_defconfig
index 9ba376feda8..fb61dd71c69 100644
--- a/configs/am335x_sl50_defconfig
+++ b/configs/am335x_sl50_defconfig
@@ -28,7 +28,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
diff --git a/configs/am3517_evm_defconfig b/configs/am3517_evm_defconfig
index 111929fb912..a96936c92d2 100644
--- a/configs/am3517_evm_defconfig
+++ b/configs/am3517_evm_defconfig
@@ -26,7 +26,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 # CONFIG_SPL_FS_EXT4 is not set
 # CONFIG_SPL_I2C is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/am43xx_evm_defconfig b/configs/am43xx_evm_defconfig
index 5f2356bcd32..d5ce2995548 100644
--- a/configs/am43xx_evm_defconfig
+++ b/configs/am43xx_evm_defconfig
@@ -22,7 +22,7 @@ CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_ETH=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am43xx_evm_rtconly_defconfig b/configs/am43xx_evm_rtconly_defconfig
index 30681e7eeb6..a0a9e8ac584 100644
--- a/configs/am43xx_evm_rtconly_defconfig
+++ b/configs/am43xx_evm_rtconly_defconfig
@@ -21,7 +21,7 @@ CONFIG_SPL_MAX_SIZE=0x439e0
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am43xx_evm_usbhost_boot_defconfig b/configs/am43xx_evm_usbhost_boot_defconfig
index 6c4cc99e5ff..cd47806204e 100644
--- a/configs/am43xx_evm_usbhost_boot_defconfig
+++ b/configs/am43xx_evm_usbhost_boot_defconfig
@@ -20,7 +20,7 @@ CONFIG_SPL_MAX_SIZE=0x37690
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am43xx_hs_evm_defconfig b/configs/am43xx_hs_evm_defconfig
index 49ecb4c9e66..d721664ecb1 100644
--- a/configs/am43xx_hs_evm_defconfig
+++ b/configs/am43xx_hs_evm_defconfig
@@ -28,7 +28,7 @@ CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_ETH=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/am62ax_evm_r5_defconfig b/configs/am62ax_evm_r5_defconfig
index d52de8bf8be..40704151255 100644
--- a/configs/am62ax_evm_r5_defconfig
+++ b/configs/am62ax_evm_r5_defconfig
@@ -43,7 +43,7 @@ CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0x400
 CONFIG_SPL_DMA=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_DM_RESET=y
 CONFIG_SPL_POWER_DOMAIN=y
diff --git a/configs/am65x_evm_a53_defconfig b/configs/am65x_evm_a53_defconfig
index f4369865bf6..55289b967b7 100644
--- a/configs/am65x_evm_a53_defconfig
+++ b/configs/am65x_evm_a53_defconfig
@@ -52,7 +52,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_DM_RESET=y
 CONFIG_SPL_POWER_DOMAIN=y
diff --git a/configs/cm_t43_defconfig b/configs/cm_t43_defconfig
index 73ccefd269a..1a558b030fe 100644
--- a/configs/cm_t43_defconfig
+++ b/configs/cm_t43_defconfig
@@ -37,7 +37,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0x480
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
diff --git a/configs/igep00x0_defconfig b/configs/igep00x0_defconfig
index 3b1af07f057..34439cf77be 100644
--- a/configs/igep00x0_defconfig
+++ b/configs/igep00x0_defconfig
@@ -24,7 +24,7 @@ CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SPL_FS_EXT4 is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/imx6ulz_smm_m2_defconfig b/configs/imx6ulz_smm_m2_defconfig
index d6edc719004..c471e04d9bd 100644
--- a/configs/imx6ulz_smm_m2_defconfig
+++ b/configs/imx6ulz_smm_m2_defconfig
@@ -25,7 +25,7 @@ CONFIG_BOARD_LATE_INIT=y
 CONFIG_SPL_BSS_START_ADDR=0x84100000
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_DMA=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_SUPPORT=y
 CONFIG_SPL_WATCHDOG=y
 CONFIG_CMD_DM=y
diff --git a/configs/imx8mn_bsh_smm_s2_defconfig b/configs/imx8mn_bsh_smm_s2_defconfig
index 657eb354c1b..a9c02976f8c 100644
--- a/configs/imx8mn_bsh_smm_s2_defconfig
+++ b/configs/imx8mn_bsh_smm_s2_defconfig
@@ -43,7 +43,7 @@ CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0x300
 CONFIG_SPL_DMA=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_SUPPORT=y
 CONFIG_SPL_NAND_BASE=y
 CONFIG_SPL_NAND_IDENT=y
diff --git a/configs/j7200_evm_a72_defconfig b/configs/j7200_evm_a72_defconfig
index cb4a141675d..01f19570d7e 100644
--- a/configs/j7200_evm_a72_defconfig
+++ b/configs/j7200_evm_a72_defconfig
@@ -50,7 +50,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/j7200_evm_r5_defconfig b/configs/j7200_evm_r5_defconfig
index d25dd8134b6..10b7205e49e 100644
--- a/configs/j7200_evm_r5_defconfig
+++ b/configs/j7200_evm_r5_defconfig
@@ -48,7 +48,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/j721e_evm_a72_defconfig b/configs/j721e_evm_a72_defconfig
index 99e0e168ebf..1d043f2fdeb 100644
--- a/configs/j721e_evm_a72_defconfig
+++ b/configs/j721e_evm_a72_defconfig
@@ -50,7 +50,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/j721e_evm_r5_defconfig b/configs/j721e_evm_r5_defconfig
index e76ab5997fe..55169bb1391 100644
--- a/configs/j721e_evm_r5_defconfig
+++ b/configs/j721e_evm_r5_defconfig
@@ -53,7 +53,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/j721s2_evm_a72_defconfig b/configs/j721s2_evm_a72_defconfig
index 876f07816a2..a7adb9282b0 100644
--- a/configs/j721s2_evm_a72_defconfig
+++ b/configs/j721s2_evm_a72_defconfig
@@ -49,7 +49,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/j721s2_evm_r5_defconfig b/configs/j721s2_evm_r5_defconfig
index 4990e271c3f..c0fdd86e1f0 100644
--- a/configs/j721s2_evm_r5_defconfig
+++ b/configs/j721s2_evm_r5_defconfig
@@ -55,7 +55,7 @@ CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
index 3a656072534..5040af058dd 100644
--- a/configs/omap35_logic_defconfig
+++ b/configs/omap35_logic_defconfig
@@ -30,7 +30,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 # CONFIG_SPL_FS_EXT4 is not set
 # CONFIG_SPL_I2C is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/omap35_logic_somlv_defconfig b/configs/omap35_logic_somlv_defconfig
index 868e89114b7..1ea35c7cfab 100644
--- a/configs/omap35_logic_somlv_defconfig
+++ b/configs/omap35_logic_somlv_defconfig
@@ -31,7 +31,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 # CONFIG_SPL_FS_EXT4 is not set
 # CONFIG_SPL_I2C is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/omap3_beagle_defconfig b/configs/omap3_beagle_defconfig
index 7f0b927528d..c8c9ae02f16 100644
--- a/configs/omap3_beagle_defconfig
+++ b/configs/omap3_beagle_defconfig
@@ -22,7 +22,7 @@ CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SPL_FS_EXT4 is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/omap3_evm_defconfig b/configs/omap3_evm_defconfig
index 3434783d45b..93427f3691c 100644
--- a/configs/omap3_evm_defconfig
+++ b/configs/omap3_evm_defconfig
@@ -22,7 +22,7 @@ CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SPL_FS_EXT4 is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
index 78bfbbd1a22..729586bfdc1 100644
--- a/configs/omap3_logic_defconfig
+++ b/configs/omap3_logic_defconfig
@@ -29,7 +29,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 # CONFIG_SPL_FS_EXT4 is not set
 # CONFIG_SPL_I2C is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/omap3_logic_somlv_defconfig b/configs/omap3_logic_somlv_defconfig
index aefd8861db3..f0f326d2f9c 100644
--- a/configs/omap3_logic_somlv_defconfig
+++ b/configs/omap3_logic_somlv_defconfig
@@ -31,7 +31,7 @@ CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 # CONFIG_SPL_FS_EXT4 is not set
 # CONFIG_SPL_I2C is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_SIMPLE=y
diff --git a/configs/phycore-am335x-r2-regor_defconfig b/configs/phycore-am335x-r2-regor_defconfig
index 85d53d06357..cbfe1cf7985 100644
--- a/configs/phycore-am335x-r2-regor_defconfig
+++ b/configs/phycore-am335x-r2-regor_defconfig
@@ -28,7 +28,7 @@ CONFIG_ARCH_MISC_INIT=y
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/phycore-am335x-r2-wega_defconfig b/configs/phycore-am335x-r2-wega_defconfig
index 60607548a25..b91b9665cc2 100644
--- a/configs/phycore-am335x-r2-wega_defconfig
+++ b/configs/phycore-am335x-r2-wega_defconfig
@@ -28,7 +28,7 @@ CONFIG_ARCH_MISC_INIT=y
 CONFIG_SPL_SYS_MALLOC=y
 CONFIG_SPL_SYS_MALLOC_SIZE=0x800000
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_DRIVERS=y
 CONFIG_SPL_NAND_ECC=y
 CONFIG_SPL_NAND_BASE=y
diff --git a/configs/socfpga_secu1_defconfig b/configs/socfpga_secu1_defconfig
index 157391a71c0..09a7a3453e8 100644
--- a/configs/socfpga_secu1_defconfig
+++ b/configs/socfpga_secu1_defconfig
@@ -43,7 +43,7 @@ CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK=y
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR is not set
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION=0x1
 # CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE is not set
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_NAND_SUPPORT=y
 CONFIG_SYS_MAXARGS=32
 CONFIG_SYS_BOOTM_LEN=0x4000000
diff --git a/configs/stm32746g-eval_spl_defconfig b/configs/stm32746g-eval_spl_defconfig
index 3864e21a180..f9711bedd7d 100644
--- a/configs/stm32746g-eval_spl_defconfig
+++ b/configs/stm32746g-eval_spl_defconfig
@@ -34,7 +34,7 @@ CONFIG_SPL_PAD_TO=0x9000
 CONFIG_SPL_NO_BSS_LIMIT=y
 CONFIG_SPL_BOARD_INIT=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_XIP_SUPPORT=y
 CONFIG_SPL_PAYLOAD_ARGS_ADDR=0x80c0000
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/stm32f746-disco_spl_defconfig b/configs/stm32f746-disco_spl_defconfig
index b2a786121c5..a2b740c9cce 100644
--- a/configs/stm32f746-disco_spl_defconfig
+++ b/configs/stm32f746-disco_spl_defconfig
@@ -34,7 +34,7 @@ CONFIG_SPL_PAD_TO=0x9000
 CONFIG_SPL_NO_BSS_LIMIT=y
 CONFIG_SPL_BOARD_INIT=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_XIP_SUPPORT=y
 CONFIG_SPL_PAYLOAD_ARGS_ADDR=0x80c0000
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/stm32f769-disco_spl_defconfig b/configs/stm32f769-disco_spl_defconfig
index 34622032210..37d22f8d95f 100644
--- a/configs/stm32f769-disco_spl_defconfig
+++ b/configs/stm32f769-disco_spl_defconfig
@@ -33,7 +33,7 @@ CONFIG_SPL_PAD_TO=0x9000
 CONFIG_SPL_NO_BSS_LIMIT=y
 CONFIG_SPL_BOARD_INIT=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_XIP_SUPPORT=y
 CONFIG_SPL_PAYLOAD_ARGS_ADDR=0x81c0000
 CONFIG_SPL_DM_RESET=y
diff --git a/configs/stm32mp15_basic_defconfig b/configs/stm32mp15_basic_defconfig
index 6df09352400..be553ad3c36 100644
--- a/configs/stm32mp15_basic_defconfig
+++ b/configs/stm32mp15_basic_defconfig
@@ -40,7 +40,7 @@ CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION=3
 CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_POWER=y
 CONFIG_SPL_SPI_FLASH_MTD=y
diff --git a/configs/stm32mp15_dhcom_basic_defconfig b/configs/stm32mp15_dhcom_basic_defconfig
index b6cd0a47fa7..3d5df307d40 100644
--- a/configs/stm32mp15_dhcom_basic_defconfig
+++ b/configs/stm32mp15_dhcom_basic_defconfig
@@ -47,7 +47,7 @@ CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION=3
 CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_POWER=y
 CONFIG_SPL_RAM_SUPPORT=y
diff --git a/configs/stm32mp15_dhcor_basic_defconfig b/configs/stm32mp15_dhcor_basic_defconfig
index d1acf9c657e..50a8882a074 100644
--- a/configs/stm32mp15_dhcor_basic_defconfig
+++ b/configs/stm32mp15_dhcor_basic_defconfig
@@ -45,7 +45,7 @@ CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION=y
 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION=3
 CONFIG_SPL_ENV_SUPPORT=y
 CONFIG_SPL_I2C=y
-CONFIG_SPL_MTD_SUPPORT=y
+CONFIG_SPL_MTD=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_POWER=y
 CONFIG_SPL_RAM_SUPPORT=y
diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
index c638980ea2b..c2fc80b10f0 100644
--- a/drivers/mtd/Makefile
+++ b/drivers/mtd/Makefile
@@ -31,7 +31,7 @@ obj-$(CONFIG_NVMXIP) += nvmxip/
 else
 
 ifneq ($(mtd-y),)
-obj-$(CONFIG_SPL_MTD_SUPPORT) += mtd.o
+obj-$(CONFIG_SPL_MTD) += mtd.o
 endif
 obj-$(CONFIG_$(SPL_TPL_)NAND_SUPPORT) += nand/
 obj-$(CONFIG_SPL_ONENAND_SUPPORT) += onenand/
diff --git a/include/mtd/cfi_flash.h b/include/mtd/cfi_flash.h
index 52cd1c4dbc4..f4aecaac75f 100644
--- a/include/mtd/cfi_flash.h
+++ b/include/mtd/cfi_flash.h
@@ -163,7 +163,7 @@ struct cfi_pri_hdr {
 #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
 /* map to cfi_flash_num_flash_banks only when supported */
 #if IS_ENABLED(CONFIG_FLASH_CFI_DRIVER) && \
-    (!IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_SPL_MTD_SUPPORT))
+    (!IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_SPL_MTD))
 #define CFI_FLASH_BANKS		(cfi_flash_num_flash_banks)
 /* board code can update this variable before CFI detection */
 extern int cfi_flash_num_flash_banks;
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (8 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 09/15] mtd: Rename SPL_MTD_SUPPORT to SPL_MTD Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02 10:45   ` Dario Binacchi
  2023-10-29  3:48 ` [PATCH 11/15] nand: Add function to unregister NAND devices Sean Anderson
                   ` (5 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

This allows using these functions without ifdefs. OneNAND depends on MTD,
so this ifdef was redundant in the first place.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 drivers/mtd/nand/raw/nand.c         |  2 --
 drivers/mtd/onenand/onenand_uboot.c |  2 --
 include/linux/mtd/mtd.h             | 12 ++++++++++++
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
index 4da41438790..3abd82068fb 100644
--- a/drivers/mtd/nand/raw/nand.c
+++ b/drivers/mtd/nand/raw/nand.c
@@ -60,13 +60,11 @@ int nand_register(int devnum, struct mtd_info *mtd)
 	sprintf(dev_name[devnum], "nand%d", devnum);
 	mtd->name = dev_name[devnum];
 
-#ifdef CONFIG_MTD
 	/*
 	 * Add MTD device so that we can reference it later
 	 * via the mtdcore infrastructure (e.g. ubi).
 	 */
 	add_mtd_device(mtd);
-#endif
 
 	total_nand_size += mtd->size / 1024;
 
diff --git a/drivers/mtd/onenand/onenand_uboot.c b/drivers/mtd/onenand/onenand_uboot.c
index 04791df69bb..ecacabefadc 100644
--- a/drivers/mtd/onenand/onenand_uboot.c
+++ b/drivers/mtd/onenand/onenand_uboot.c
@@ -44,14 +44,12 @@ void onenand_init(void)
 			puts("Flex-");
 		puts("OneNAND: ");
 
-#ifdef CONFIG_MTD
 		/*
 		 * Add MTD device so that we can reference it later
 		 * via the mtdcore infrastructure (e.g. ubi).
 		 */
 		onenand_mtd.name = dev_name;
 		add_mtd_device(&onenand_mtd);
-#endif
 	}
 	print_size(onenand_chip.chipsize, "\n");
 }
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 09f52698877..7a66c7af749 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -552,8 +552,20 @@ unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
 
 #ifdef __UBOOT__
 /* drivers/mtd/mtdcore.h */
+#if CONFIG_IS_ENABLED(MTD)
 int add_mtd_device(struct mtd_info *mtd);
 int del_mtd_device(struct mtd_info *mtd);
+#else
+static inline int add_mtd_device(struct mtd_info *mtd)
+{
+	return -ENOSYS;
+}
+
+static inline int del_mtd_device(struct mtd_info *mtd)
+{
+	return -ENOSYS;
+}
+#endif
 
 #ifdef CONFIG_MTD_PARTITIONS
 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 11/15] nand: Add function to unregister NAND devices
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (9 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02 10:58   ` Dario Binacchi
  2023-10-29  3:48 ` [PATCH 12/15] nand: Allow reinitialization Sean Anderson
                   ` (4 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

This performs the opposite of nand_register, allowing drivers to unregister
nand devices. This is probably unnecessary for most regular drivers, but we
expect sandbox drivers to get repeatedly bound/unbound, so this will help
avoid dangling pointers.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 drivers/mtd/nand/raw/nand.c | 17 +++++++++++++++++
 include/nand.h              |  1 +
 2 files changed, 18 insertions(+)

diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
index 3abd82068fb..80017b3dddd 100644
--- a/drivers/mtd/nand/raw/nand.c
+++ b/drivers/mtd/nand/raw/nand.c
@@ -74,6 +74,23 @@ int nand_register(int devnum, struct mtd_info *mtd)
 	return 0;
 }
 
+void nand_unregister(struct mtd_info *mtd)
+{
+	int devnum = nand_mtd_to_devnum(mtd);
+
+	if (devnum < 0)
+		return;
+
+	if (nand_curr_device == devnum)
+		nand_curr_device = -1;
+
+	total_nand_size -= mtd->size / 1024;
+
+	del_mtd_device(nand_info[devnum]);
+
+	nand_info[devnum] = NULL;
+}
+
 #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
 static void nand_init_chip(int i)
 {
diff --git a/include/nand.h b/include/nand.h
index c1d7533aaac..fc584f5ef7a 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -22,6 +22,7 @@ int nand_mtd_to_devnum(struct mtd_info *mtd);
 #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
 void board_nand_init(void);
 int nand_register(int devnum, struct mtd_info *mtd);
+void nand_unregister(struct mtd_info *mtd);
 #else
 struct nand_chip;
 
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 12/15] nand: Allow reinitialization
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (10 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 11/15] nand: Add function to unregister NAND devices Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02 11:08   ` Dario Binacchi
  2023-10-29  3:48 ` [PATCH 13/15] arch: sandbox: Add function to create temporary files Sean Anderson
                   ` (3 subsequent siblings)
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

NAND devices are destroyed in between unit tests. Provide a function to
reinitialize the subsystem at the beginning of each test.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 drivers/mtd/nand/raw/nand.c | 40 ++++++++++++++++++++++++++++++-------
 include/nand.h              |  1 +
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
index 80017b3dddd..4c18861aa25 100644
--- a/drivers/mtd/nand/raw/nand.c
+++ b/drivers/mtd/nand/raw/nand.c
@@ -115,6 +115,8 @@ static void nand_init_chip(int i)
 #endif
 
 #ifdef CONFIG_MTD_CONCAT
+struct mtd_info *concat_mtd;
+
 static void create_mtd_concat(void)
 {
 	struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
@@ -129,28 +131,40 @@ static void create_mtd_concat(void)
 		}
 	}
 	if (nand_devices_found > 1) {
-		struct mtd_info *mtd;
 		char c_mtd_name[16];
 
 		/*
 		 * We detected multiple devices. Concatenate them together.
 		 */
 		sprintf(c_mtd_name, "nand%d", nand_devices_found);
-		mtd = mtd_concat_create(nand_info_list, nand_devices_found,
-					c_mtd_name);
+		concat_mtd = mtd_concat_create(nand_info_list,
+					       nand_devices_found, c_mtd_name);
 
-		if (mtd == NULL)
+		if (!concat_mtd)
 			return;
 
-		nand_register(nand_devices_found, mtd);
+		nand_register(nand_devices_found, concat_mtd);
 	}
 
 	return;
 }
+
+static void destroy_mtd_concat(void)
+{
+	if (!concat_mtd)
+		return;
+
+	mtd_concat_destroy(concat_mtd);
+	concat_mtd = NULL;
+}
 #else
 static void create_mtd_concat(void)
 {
 }
+
+static void destroy_mtd_concat(void)
+{
+}
 #endif
 
 unsigned long nand_size(void)
@@ -158,10 +172,10 @@ unsigned long nand_size(void)
 	return total_nand_size;
 }
 
+static int initialized;
+
 void nand_init(void)
 {
-	static int initialized;
-
 	/*
 	 * Avoid initializing NAND Flash multiple times,
 	 * otherwise it will calculate a wrong total size.
@@ -190,6 +204,18 @@ void nand_init(void)
 	create_mtd_concat();
 }
 
+void nand_reinit(void)
+{
+	int i;
+
+	destroy_mtd_concat();
+	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
+		assert(!nand_info[i]);
+
+	initialized = 0;
+	nand_init();
+}
+
 unsigned int nand_page_size(void)
 {
 	struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
diff --git a/include/nand.h b/include/nand.h
index fc584f5ef7a..220ffa202ef 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -11,6 +11,7 @@
 #include <config.h>
 
 extern void nand_init(void);
+void nand_reinit(void);
 unsigned long nand_size(void);
 unsigned int nand_page_size(void);
 
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 13/15] arch: sandbox: Add function to create temporary files
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (11 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 12/15] nand: Allow reinitialization Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-10-29  3:48 ` [PATCH 14/15] nand: Add sandbox driver Sean Anderson
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Sean Anderson, Simon Glass

When working with sparse data buffers that may be larger than the address
space, it is convenient to work with files instead. Add a function to create
temporary files of a certain size.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 arch/sandbox/cpu/os.c | 17 +++++++++++++++++
 include/os.h          | 13 +++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 85d0d6a1703..8847c4cd0a8 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -282,6 +282,23 @@ int os_persistent_file(char *buf, int maxsize, const char *fname)
 	return 0;
 }
 
+int os_mktemp(char *fname, off_t size)
+{
+	int fd;
+
+	fd = mkostemp(fname, O_CLOEXEC);
+	if (fd < 0)
+		return -errno;
+
+	if (unlink(fname) < 0)
+		return -errno;
+
+	if (ftruncate(fd, size))
+		return -errno;
+
+	return fd;
+}
+
 /* Restore tty state when we exit */
 static struct termios orig_term;
 static bool term_setup;
diff --git a/include/os.h b/include/os.h
index fc8a1b15cbf..877404a6c13 100644
--- a/include/os.h
+++ b/include/os.h
@@ -108,6 +108,19 @@ int os_unlink(const char *pathname);
  */
 int os_persistent_file(char *buf, int maxsize, const char *fname);
 
+/**
+ * os_mktemp() - Create a temporary file
+ * @fname: The template to use for the file name. This must end with 6 Xs. It
+ *         will be modified to the opened filename on success.
+ * @size: The size of the file
+ *
+ * Create a temporary file using @fname as a template, unlink it, and truncate
+ * it to @size.
+ *
+ * Return: A file descriptor, or negative errno on error
+ */
+int os_mktemp(char *fname, off_t size);
+
 /**
  * os_exit() - access to the OS exit() system call
  *
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 14/15] nand: Add sandbox driver
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (12 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 13/15] arch: sandbox: Add function to create temporary files Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02 11:15   ` Dario Binacchi
  2023-10-29  3:48 ` [PATCH 15/15] test: spl: Add a test for NAND Sean Anderson
  2023-11-02 14:01 ` [PATCH 00/15] nand: Add sandbox tests Dario Binacchi
  15 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

Add a sandbox NAND flash driver to facilitate testing. This driver supports
any number of devices, each using a single chip-select. The OOB data is
stored in-band, with the separation enforced through the API.

For now, create two devices to test with. The first is a very small device
with basic ECC. The second is an 8G device (chosen to be larger than 32
bits). It uses ONFI, with the values copied from the datasheet. It also
doesn't need too strong ECC, which speeds things up.

Although the nand subsystem determines the parameters of a chip based on
the ID, the driver itself requires devicetree properties for each
parameter. We do not derive parameters from the ID because parsing the ID
is non-trivial. We do not just use the parameters that the nand subsystem
has calculated since that is something we should be testing.

Despite using file I/O to access the backing data, we do not support using
external files. In my experience, these are unnecessary for testing since
tests can generally be written to write their expected data beforehand.
Additionally, we would need to store the "programmed" information somewhere
(complicating the format and the programming process) or try to detect
whether block are erased at runtime (degrading probe speeds).

Information about whether each page has been programmed is stored in an
in-memory buffer. To simplify the implementation, we only support a single
program per erase. While this is accurate for many larger flashes, some
smaller flashes (512 byte) support multiple programs and/or subpage
programs. Support for this could be added later as I believe some
filesystems expect this.

To test ECC, we support error-injection. Surprisingly, only ECC bytes in
the OOB area are protected, even though all bytes are equally susceptible
to error. Because of this, we take care to only corrupt ECC bytes.
Similarly, because ECC covers "steps" and not the whole page, we must take
care to corrupt data in the same way.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 arch/sandbox/dts/test.dts        |  67 +++
 configs/sandbox64_defconfig      |  10 +-
 configs/sandbox_defconfig        |   9 +
 configs/sandbox_noinst_defconfig |  10 +-
 drivers/mtd/nand/raw/Kconfig     |  14 +
 drivers/mtd/nand/raw/Makefile    |   1 +
 drivers/mtd/nand/raw/sand_nand.c | 682 +++++++++++++++++++++++++++++++
 test/dm/Makefile                 |   1 +
 test/dm/nand.c                   | 106 +++++
 9 files changed, 898 insertions(+), 2 deletions(-)
 create mode 100644 drivers/mtd/nand/raw/sand_nand.c
 create mode 100644 test/dm/nand.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 5b54651a1da..5fba2943fad 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -1900,6 +1900,73 @@
 				compatible = "sandbox,arm-ffa";
 		};
 	};
+
+	nand-controller {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		compatible = "sandbox,nand";
+
+		nand@0 {
+			reg = <0>;
+			nand-ecc-mode = "soft";
+			sandbox,id = [00 e3];
+			sandbox,erasesize = <(8 * 1024)>;
+			sandbox,oobsize = <16>;
+			sandbox,pagesize = <512>;
+			sandbox,pages = <0x2000>;
+			sandbox,err-count = <1>;
+			sandbox,err-step-size = <512>;
+			sandbox,ecc-bytes = <6>;
+		};
+
+		/* MT29F64G08AKABA */
+		nand@1 {
+			reg = <1>;
+			nand-ecc-mode = "soft_bch";
+			sandbox,id = [2C 48 00 26 89 00 00 00];
+			sandbox,onfi = [
+				4f 4e 46 49 0e 00 5a 00
+				ff 01 00 00 00 00 03 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				4d 49 43 52 4f 4e 20 20
+				20 20 20 20 4d 54 32 39
+				46 36 34 47 30 38 41 4b
+				41 42 41 43 35 20 20 20
+				2c 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 10 00 00 e0 00 00 02
+				00 00 1c 00 80 00 00 00
+				00 10 00 00 02 23 01 50
+				00 01 05 01 00 00 04 00
+				04 01 1e 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				0e 1f 00 1f 00 f4 01 ac
+				0d 19 00 c8 00 00 00 00
+				00 00 00 00 00 00 0a 07
+				19 00 00 00 00 00 00 00
+				00 00 00 00 01 00 01 00
+				00 00 04 10 01 81 04 02
+				02 01 1e 90 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 00 00 00
+				00 00 00 00 00 03 20 7d
+			];
+			sandbox,erasesize = <(512 * 1024)>;
+			sandbox,oobsize = <224>;
+			sandbox,pagesize = <4096>;
+			sandbox,pages = <0x200000>;
+			sandbox,err-count = <3>;
+			sandbox,err-step-size = <512>;
+			sandbox,ecc-bytes = <7>;
+		};
+	};
 };
 
 #include "sandbox_pmic.dtsi"
diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index 1a01f51a0b7..d4e5eb3000b 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -1,4 +1,5 @@
 CONFIG_TEXT_BASE=0
+CONFIG_SYS_MALLOC_LEN=0x6000000
 CONFIG_NR_DRAM_BANKS=1
 CONFIG_ENV_SIZE=0x2000
 CONFIG_DEFAULT_DEVICE_TREE="sandbox64"
@@ -50,7 +51,7 @@ CONFIG_CMD_GPT_RENAME=y
 CONFIG_CMD_IDE=y
 CONFIG_CMD_I2C=y
 CONFIG_CMD_LOADM=y
-CONFIG_CMD_MBR=y
+CONFIG_CMD_MTD=y
 CONFIG_CMD_OSD=y
 CONFIG_CMD_PCI=y
 CONFIG_CMD_READ=y
@@ -168,6 +169,13 @@ CONFIG_PWRSEQ=y
 CONFIG_I2C_EEPROM=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_MTD=y
+CONFIG_DM_MTD=y
+CONFIG_MTD_RAW_NAND=y
+CONFIG_SYS_MAX_NAND_DEVICE=8
+CONFIG_SYS_NAND_USE_FLASH_BBT=y
+CONFIG_NAND_SANDBOX=y
+CONFIG_SYS_NAND_ONFI_DETECTION=y
+CONFIG_SYS_NAND_PAGE_SIZE=0x200
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_BOOTDEV_SPI_FLASH=y
 CONFIG_SPI_FLASH_ATMEL=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index e3a2f9eb170..1c71f0542a3 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -1,4 +1,5 @@
 CONFIG_TEXT_BASE=0
+CONFIG_SYS_MALLOC_LEN=0x6000000
 CONFIG_NR_DRAM_BANKS=1
 CONFIG_ENV_SIZE=0x2000
 CONFIG_DEFAULT_DEVICE_TREE="sandbox"
@@ -73,6 +74,7 @@ CONFIG_CMD_IDE=y
 CONFIG_CMD_I2C=y
 CONFIG_CMD_LOADM=y
 CONFIG_CMD_LSBLK=y
+CONFIG_CMD_MTD=y
 CONFIG_CMD_MUX=y
 CONFIG_CMD_OSD=y
 CONFIG_CMD_PCI=y
@@ -215,6 +217,13 @@ CONFIG_MMC_PCI=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_MMC_SDHCI=y
 CONFIG_MTD=y
+CONFIG_DM_MTD=y
+CONFIG_MTD_RAW_NAND=y
+CONFIG_SYS_MAX_NAND_DEVICE=8
+CONFIG_SYS_NAND_USE_FLASH_BBT=y
+CONFIG_NAND_SANDBOX=y
+CONFIG_SYS_NAND_ONFI_DETECTION=y
+CONFIG_SYS_NAND_PAGE_SIZE=0x200
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_BOOTDEV_SPI_FLASH=y
 CONFIG_SPI_FLASH_ATMEL=y
diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
index db05e630832..09ebafeccca 100644
--- a/configs/sandbox_noinst_defconfig
+++ b/configs/sandbox_noinst_defconfig
@@ -80,7 +80,7 @@ CONFIG_CMD_GPIO=y
 CONFIG_CMD_GPT=y
 CONFIG_CMD_IDE=y
 CONFIG_CMD_I2C=y
-CONFIG_CMD_MBR=y
+CONFIG_CMD_MTD=y
 CONFIG_CMD_OSD=y
 CONFIG_CMD_PCI=y
 CONFIG_CMD_REMOTEPROC=y
@@ -181,6 +181,14 @@ CONFIG_PWRSEQ=y
 CONFIG_SPL_PWRSEQ=y
 CONFIG_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
+CONFIG_MTD=y
+CONFIG_DM_MTD=y
+CONFIG_MTD_RAW_NAND=y
+CONFIG_SYS_MAX_NAND_DEVICE=8
+CONFIG_SYS_NAND_USE_FLASH_BBT=y
+CONFIG_NAND_SANDBOX=y
+CONFIG_SYS_NAND_ONFI_DETECTION=y
+CONFIG_SYS_NAND_PAGE_SIZE=0x200
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_SPI_FLASH_ATMEL=y
 CONFIG_SPI_FLASH_EON=y
diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index ee484dc0f51..6b34aa6782c 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -447,6 +447,20 @@ config NAND_PXA3XX
 	  This enables the driver for the NAND flash device found on
 	  PXA3xx processors (NFCv1) and also on Armada 370/XP (NFCv2).
 
+config NAND_SANDBOX
+	bool "Support for NAND in sandbox"
+	depends on SANDBOX
+	select SYS_NAND_SELF_INIT
+	select SYS_NAND_SOFT_ECC
+	select BCH
+	select NAND_ECC_BCH
+	imply CMD_NAND
+	help
+	  Enable a dummy NAND driver for sandbox. It simulates any number of
+	  arbitrary NAND chips with a RAM buffer. It will also inject errors to
+	  test ECC. At the moment, only 8-bit busses and single-chip devices are
+	  supported.
+
 config NAND_SUNXI
 	bool "Support for NAND on Allwinner SoCs"
 	default ARCH_SUNXI
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index add2b4cf655..ddbba899e58 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -70,6 +70,7 @@ obj-$(CONFIG_NAND_PXA3XX) += pxa3xx_nand.o
 obj-$(CONFIG_TEGRA_NAND) += tegra_nand.o
 obj-$(CONFIG_NAND_OMAP_GPMC) += omap_gpmc.o
 obj-$(CONFIG_NAND_OMAP_ELM) += omap_elm.o
+obj-$(CONFIG_NAND_SANDBOX) += sand_nand.o
 obj-$(CONFIG_NAND_SUNXI) += sunxi_nand.o
 obj-$(CONFIG_NAND_MXIC) += mxic_nand.o
 obj-$(CONFIG_NAND_ZYNQ) += zynq_nand.o
diff --git a/drivers/mtd/nand/raw/sand_nand.c b/drivers/mtd/nand/raw/sand_nand.c
new file mode 100644
index 00000000000..f1855877630
--- /dev/null
+++ b/drivers/mtd/nand/raw/sand_nand.c
@@ -0,0 +1,682 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) Sean Anderson <seanga2@gmail.com>
+ */
+
+#define LOG_CATEGORY UCLASS_MTD
+#include <errno.h>
+#include <hexdump.h>
+#include <log.h>
+#include <nand.h>
+#include <os.h>
+#include <rand.h>
+#include <dm/device_compat.h>
+#include <dm/read.h>
+#include <dm/uclass.h>
+#include <asm/bitops.h>
+#include <linux/bitmap.h>
+#include <linux/mtd/rawnand.h>
+#include <linux/sizes.h>
+
+enum sand_nand_state {
+	STATE_READY,
+	STATE_IDLE,
+	STATE_READ,
+	STATE_READ_ID,
+	STATE_READ_ONFI,
+	STATE_PARAM_ONFI,
+	STATE_STATUS,
+	STATE_PROG,
+	STATE_ERASE,
+};
+
+static const char *const state_name[] = {
+	[STATE_READY] = "READY",
+	[STATE_IDLE] = "IDLE",
+	[STATE_READ] = "READ",
+	[STATE_READ_ID] = "READ_ID",
+	[STATE_READ_ONFI] = "READ_ONFI",
+	[STATE_PARAM_ONFI] = "PARAM_ONFI",
+	[STATE_STATUS] = "STATUS",
+	[STATE_PROG] = "PROG",
+	[STATE_ERASE] = "ERASE",
+};
+
+/**
+ * struct sand_nand_chip - Per-device private data
+ * @nand: The nand chip
+ * @node: The next device in this controller
+ * @programmed: Bitmap of whether sectors are programmed
+ * @id: ID to report for NAND_CMD_READID
+ * @id_len: Length of @id
+ * @onfi: Three copies of ONFI parameter page
+ * @status: Status to report for NAND_CMD_STATUS
+ * @chunksize: Size of one "chunk" (page + oob) in bytes
+ * @pageize: Size of one page in bytes
+ * @pages: Total number of pages
+ * @pages_per_erase: Number of pages per eraseblock
+ * @err_count: Number of errors to inject per @err_step_bits of data
+ * @err_step_bits: Number of data bits per error "step"
+ * @err_steps: Number of err steps in a page
+ * @ecc_bits: Number of ECC bits uper @err_step_bits
+ * @cs: Chip select for this device
+ * @state: Current state of the device
+ * @column: Column of the most-recent command
+ * @page_addr: Page address of the most-recent command
+ * @fd: File descriptor for the backing data
+ * @fd_page_addr: Page address that @fd is seek'd to
+ * @selected: Whether this device is selected
+ * @tmp: "Cache" buffer used to store transferred data before committing it
+ * @tmp_dirty: Whether @tmp is dirty (modified) or clean (all ones)
+ */
+struct sand_nand_chip {
+	struct nand_chip nand;
+	struct list_head node;
+	long *programmed;
+	const u8 *id;
+	u32 chunksize, pagesize, pages, pages_per_erase;
+	u32 err_count, err_step_bits, err_steps, ecc_bits;
+	unsigned int cs;
+	enum sand_nand_state state;
+	int column, page_addr, fd, fd_page_addr;
+	bool selected, tmp_dirty;
+	u8 status;\f
+	u8 id_len;
+	u8 tmp[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
+	u8 onfi[sizeof(struct nand_onfi_params) * 3];
+};
+
+#define SAND_DEBUG(chip, fmt, ...) \
+	dev_dbg((chip)->nand.mtd.dev, "%u (%s): " fmt, (chip)->cs, \
+		state_name[(chip)->state], ##__VA_ARGS__)
+
+static inline void to_state(struct sand_nand_chip *chip,
+			    enum sand_nand_state new_state)
+{
+	if (new_state != chip->state)
+		SAND_DEBUG(chip, "to state %s\n", state_name[new_state]);
+	chip->state = new_state;
+}
+
+static inline struct sand_nand_chip *to_sand_nand(struct nand_chip *nand)
+{
+	return container_of(nand, struct sand_nand_chip, nand);
+}
+
+struct sand_nand_priv {
+	struct list_head chips;
+};
+
+static int sand_nand_dev_ready(struct mtd_info *mtd)
+{
+	return 1;
+}
+
+static int sand_nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
+{
+	u8 status;
+
+	return nand_status_op(chip, &status) ?: status;
+}
+
+static int sand_nand_seek(struct sand_nand_chip *chip)
+{
+	if (chip->fd_page_addr == chip->page_addr)
+		return 0;
+
+	if (os_lseek(chip->fd, (off_t)chip->page_addr * chip->chunksize,
+		     OS_SEEK_SET) < 0) {
+		SAND_DEBUG(chip, "could not seek: %d\n", errno);
+		return -EIO;
+	}
+
+	chip->fd_page_addr = chip->page_addr;
+	return 0;
+}
+
+static void sand_nand_inject_error(struct sand_nand_chip *chip,
+				   unsigned int step, unsigned int pos)
+{
+	int byte, index;
+
+	if (pos < chip->err_step_bits) {
+		__change_bit(step * chip->err_step_bits + pos, chip->tmp);
+		return;
+	}
+
+	/*
+	 * Only ECC bytes are covered in the OOB area, so
+	 * pretend that those are the only bytes which can have
+	 * errors.
+	 */
+	byte = (pos - chip->err_step_bits + step * chip->ecc_bits) / 8;
+	index = chip->nand.ecc.layout->eccpos[byte];
+	/* Avoid endianness issues by working with bytes */
+	chip->tmp[chip->pagesize + index] ^= BIT(pos & 0x7);
+}
+
+static int sand_nand_read(struct sand_nand_chip *chip)
+{
+	unsigned int i, stop = 0;
+
+	if (chip->column == chip->pagesize)
+		stop = chip->err_step_bits;
+
+	if (test_bit(chip->page_addr, chip->programmed)) {
+		if (sand_nand_seek(chip))
+			return -EIO;
+
+		if (os_read(chip->fd, chip->tmp, chip->chunksize) !=
+		    chip->chunksize) {
+			SAND_DEBUG(chip, "could not read: %d\n", errno);
+			return -EIO;
+		}
+		chip->fd_page_addr++;
+	} else if (chip->tmp_dirty) {
+		memset(chip->tmp + chip->column, 0xff,
+		       chip->chunksize - chip->column);
+	}
+
+	/*
+	 * Inject some errors; this is Method A from "An Efficient Algorithm for
+	 * Sequential Random Sampling" (Vitter 87). This is still slow when
+	 * generating a lot (dozens) of ECC errors.
+	 *
+	 * To avoid generating too many errors in any one ECC step, we separate
+	 * our error generation by ECC step.
+	 */
+	chip->tmp_dirty = true;
+	for (i = 0; i < chip->err_steps; i++) {
+		u32 bit_errors = chip->err_count;
+		unsigned int j = chip->err_step_bits + chip->ecc_bits;
+
+		while (bit_errors) {
+			unsigned int u = rand();
+			float quot = 1ULL << 32;
+
+			do {
+				quot *= j - bit_errors;
+				quot /= j;
+				j--;
+
+				if (j < stop)
+					goto next;
+			} while (u < quot);
+
+			sand_nand_inject_error(chip, i, j);
+			bit_errors--;
+		}
+next:
+		;
+	}
+
+	return 0;
+}
+
+static void sand_nand_command(struct mtd_info *mtd, unsigned int command,
+			      int column, int page_addr)
+{
+	struct nand_chip *nand = mtd_to_nand(mtd);
+	struct sand_nand_chip *chip = to_sand_nand(nand);
+	enum sand_nand_state new_state = chip->state;
+
+	SAND_DEBUG(chip, "command=%02x column=%d page_addr=%d\n", command,
+		   column, page_addr);
+
+	if (!chip->selected)
+		return;
+
+	switch (chip->state) {
+	case STATE_READY:
+		if (command == NAND_CMD_RESET)
+			goto reset;
+		break;
+	case STATE_PROG:
+		new_state = STATE_IDLE;
+		if (command != NAND_CMD_PAGEPROG ||
+		    test_and_set_bit(chip->page_addr, chip->programmed)) {
+			chip->status |= NAND_STATUS_FAIL;
+			break;
+		}
+
+		if (sand_nand_seek(chip)) {
+			chip->status |= NAND_STATUS_FAIL;
+			break;
+		}
+
+		if (os_write(chip->fd, chip->tmp, chip->chunksize) !=
+		    chip->chunksize) {
+			SAND_DEBUG(chip, "could not write: %d\n", errno);
+			chip->status |= NAND_STATUS_FAIL;
+			break;
+		}
+
+		chip->fd_page_addr++;
+		break;
+	case STATE_ERASE:
+		new_state = STATE_IDLE;
+		if (command != NAND_CMD_ERASE2) {
+			chip->status |= NAND_STATUS_FAIL;
+			break;
+		}
+
+		if (chip->page_addr < 0 ||
+		    chip->page_addr >= chip->pages ||
+		    chip->page_addr % chip->pages_per_erase)
+			chip->status |= NAND_STATUS_FAIL;
+		else
+			bitmap_clear(chip->programmed, chip->page_addr,
+				     chip->pages_per_erase);
+		break;
+	default:
+		chip->column = column;
+		chip->page_addr = page_addr;
+		switch (command) {
+		case NAND_CMD_READOOB:
+			if (column >= 0)
+				chip->column += chip->pagesize;
+			fallthrough;
+		case NAND_CMD_READ0:
+			new_state = STATE_IDLE;
+			if (page_addr < 0 || page_addr >= chip->pages)
+				break;
+
+			if (chip->column < 0 || chip->column >= chip->chunksize)
+				break;
+
+			if (sand_nand_read(chip))
+				break;
+
+			chip->page_addr = page_addr;
+			new_state = STATE_READ;
+			break;
+		case NAND_CMD_ERASE1:
+			new_state = STATE_ERASE;
+			chip->status = ~NAND_STATUS_FAIL;
+			break;
+		case NAND_CMD_STATUS:
+			new_state = STATE_STATUS;
+			chip->column = 0;
+			break;
+		case NAND_CMD_SEQIN:
+			new_state = STATE_PROG;
+			chip->status = ~NAND_STATUS_FAIL;
+			if (page_addr < 0 || page_addr >= chip->pages ||
+			    chip->column < 0 ||
+			    chip->column >= chip->chunksize) {
+				chip->status |= NAND_STATUS_FAIL;
+			} else if (chip->tmp_dirty) {
+				memset(chip->tmp, 0xff, chip->chunksize);
+				chip->tmp_dirty = false;
+			}
+			break;
+		case NAND_CMD_READID:
+			if (chip->onfi[0] && column == 0x20)
+				new_state = STATE_READ_ONFI;
+			else
+				new_state = STATE_READ_ID;
+			chip->column = 0;
+			break;
+		case NAND_CMD_PARAM:
+			if (chip->onfi[0] && !column)
+				new_state = STATE_PARAM_ONFI;
+			else
+				new_state = STATE_IDLE;
+			break;
+		case NAND_CMD_RESET:
+reset:
+			new_state = STATE_IDLE;
+			chip->column = -1;
+			chip->page_addr = -1;
+			chip->status = ~NAND_STATUS_FAIL;
+			break;
+		default:
+			new_state = STATE_IDLE;
+			SAND_DEBUG(chip, "Unsupported command %02x\n", command);
+		}
+	}
+
+	to_state(chip, new_state);
+}
+
+static void sand_nand_select_chip(struct mtd_info *mtd, int n)
+{
+	struct nand_chip *nand = mtd_to_nand(mtd);
+	struct sand_nand_chip *chip = to_sand_nand(nand);
+
+	chip->selected = !n;
+}
+
+static void sand_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
+{
+	struct nand_chip *nand = mtd_to_nand(mtd);
+	struct sand_nand_chip *chip = to_sand_nand(nand);
+	unsigned int to_copy;
+	int src_len = 0;
+	const u8 *src = NULL;
+
+	if (!chip->selected)
+		goto copy;
+
+	switch (chip->state) {
+	case STATE_READ:
+		src = chip->tmp;
+		src_len = chip->chunksize;
+		break;
+	case STATE_READ_ID:
+		src = chip->id;
+		src_len = chip->id_len;
+		break;
+	case STATE_READ_ONFI:
+		src = "ONFI";
+		src_len = 4;
+		break;
+	case STATE_PARAM_ONFI:
+		src = chip->onfi;
+		src_len = sizeof(chip->onfi);
+		break;
+	case STATE_STATUS:
+		src = &chip->status;
+		src_len = 1;
+		break;
+	default:
+		break;
+	}
+
+copy:
+	if (chip->column >= 0)
+		to_copy = max(min(len, src_len - chip->column), 0);
+	else
+		to_copy = 0;
+	memcpy(buf, src + chip->column, to_copy);
+	memset(buf + to_copy, 0xff, len - to_copy);
+	chip->column += to_copy;
+
+	if (len == 1) {
+		SAND_DEBUG(chip, "read [ %02x ]\n", buf[0]);
+	} else if (src_len) {
+		SAND_DEBUG(chip, "read %d bytes\n", len);
+#ifdef VERBOSE_DEBUG
+		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
+#endif
+	}
+
+	if (src_len && chip->column == src_len)
+		to_state(chip, STATE_IDLE);
+}
+
+static u8 sand_nand_read_byte(struct mtd_info *mtd)
+{
+	u8 ret;
+
+	sand_nand_read_buf(mtd, &ret, 1);
+	return ret;
+}
+
+static u16 sand_nand_read_word(struct mtd_info *mtd)
+{
+	struct nand_chip *nand = mtd_to_nand(mtd);
+	struct sand_nand_chip *chip = to_sand_nand(nand);
+
+	SAND_DEBUG(chip, "16-bit access unsupported\n");
+	return sand_nand_read_byte(mtd) | 0xff00;
+}
+
+static void sand_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
+{
+	struct nand_chip *nand = mtd_to_nand(mtd);
+	struct sand_nand_chip *chip = to_sand_nand(nand);
+
+	SAND_DEBUG(chip, "write %d bytes\n", len);
+#ifdef VERBOSE_DEBUG
+	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
+#endif
+
+	if (chip->state != STATE_PROG || chip->status & NAND_STATUS_FAIL)
+		return;
+
+	chip->tmp_dirty = true;
+	len = min((unsigned int)len, chip->chunksize - chip->column);
+	memcpy(chip->tmp + chip->column, buf, len);
+	chip->column += len;
+}
+
+static struct nand_chip *nand_chip;
+
+int sand_nand_remove(struct udevice *dev)
+{
+	struct sand_nand_priv *priv = dev_get_priv(dev);
+	struct sand_nand_chip *chip;
+
+	list_for_each_entry(chip, &priv->chips, node) {
+		struct nand_chip *nand = &chip->nand;
+
+		if (nand_chip == nand)
+			nand_chip = NULL;
+
+		nand_unregister(nand_to_mtd(nand));
+		free(chip->programmed);
+		os_close(chip->fd);
+		free(chip);
+	}
+
+	return 0;
+}
+
+static int sand_nand_probe(struct udevice *dev)
+{
+	struct sand_nand_priv *priv = dev_get_priv(dev);
+	struct sand_nand_chip *chip;
+	int ret, devnum = 0;
+	ofnode np;
+
+	INIT_LIST_HEAD(&priv->chips);
+
+	dev_for_each_subnode(np, dev) {
+		struct nand_chip *nand;
+		struct mtd_info *mtd;
+		u32 erasesize, oobsize, pagesize, pages;
+		u32 err_count, err_step_size, ecc_bytes;
+		off_t expected_size;
+		char filename[30];
+		fdt_addr_t cs;
+		const u8 *id, *onfi;
+		int id_len, onfi_len;
+
+		cs = ofnode_get_addr_size_index_notrans(np, 0, NULL);
+		if (cs == FDT_ADDR_T_NONE) {
+			dev_dbg(dev, "Invalid cs for chip %s\n",
+				ofnode_get_name(np));
+			ret = -ENOENT;
+			goto err;
+		}
+
+		id = ofnode_read_prop(np, "sandbox,id", &id_len);
+		if (!id) {
+			dev_dbg(dev, "No sandbox,id property for chip %s\n",
+				ofnode_get_name(np));
+			ret = -EINVAL;
+			goto err;
+		}
+
+		onfi = ofnode_read_prop(np, "sandbox,onfi", &onfi_len);
+		if (onfi && onfi_len != sizeof(struct nand_onfi_params)) {
+			dev_dbg(dev, "Invalid length %d for onfi params\n",
+				onfi_len);
+			ret = -EINVAL;
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,erasesize", &erasesize);
+		if (ret) {
+			dev_dbg(dev, "No sandbox,erasesize property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,oobsize", &oobsize);
+		if (ret) {
+			dev_dbg(dev, "No sandbox,oobsize property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,pagesize", &pagesize);
+		if (ret) {
+			dev_dbg(dev, "No sandbox,pagesize property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,pages", &pages);
+		if (ret) {
+			dev_dbg(dev, "No sandbox,pages property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,err-count", &err_count);
+		if (ret) {
+			dev_dbg(dev,
+				"No sandbox,err-count property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,err-step-size",
+				      &err_step_size);
+		if (ret) {
+			dev_dbg(dev,
+				"No sandbox,err-step-size property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		ret = ofnode_read_u32(np, "sandbox,ecc-bytes", &ecc_bytes);
+		if (ret) {
+			dev_dbg(dev,
+				"No sandbox,ecc-bytes property for chip %s",
+				ofnode_get_name(np));
+			goto err;
+		}
+
+		chip = calloc(sizeof(*chip), 1);
+		if (!chip) {
+			ret = -ENOMEM;
+			goto err;
+		}
+
+		chip->cs = cs;
+		chip->id = id;
+		chip->id_len = id_len;
+		chip->chunksize = pagesize + oobsize;
+		chip->pagesize = pagesize;
+		chip->pages = pages;
+		chip->pages_per_erase = erasesize / pagesize;
+		memset(chip->tmp, 0xff, chip->chunksize);
+
+		chip->err_count = err_count;
+		chip->err_step_bits = err_step_size * 8;
+		chip->err_steps = pagesize / err_step_size;
+		chip->ecc_bits = ecc_bytes * 8;
+
+		expected_size = (off_t)pages * chip->chunksize;
+		snprintf(filename, sizeof(filename),
+			 "/tmp/u-boot.nand%d.XXXXXX", devnum);
+		chip->fd = os_mktemp(filename, expected_size);
+		if (chip->fd < 0) {
+			dev_dbg(dev, "Could not create temp file %s\n",
+				filename);
+			ret = chip->fd;
+			goto err_chip;
+		}
+
+		chip->programmed = calloc(sizeof(long),
+					  BITS_TO_LONGS(pages));
+		if (!chip->programmed) {
+			ret = -ENOMEM;
+			goto err_fd;
+		}
+
+		if (onfi) {
+			memcpy(chip->onfi, onfi, onfi_len);
+			memcpy(chip->onfi + onfi_len, onfi, onfi_len);
+			memcpy(chip->onfi + 2 * onfi_len, onfi, onfi_len);
+		}
+
+		nand = &chip->nand;
+		nand->flash_node = np;
+		nand->dev_ready = sand_nand_dev_ready;
+		nand->cmdfunc = sand_nand_command;
+		nand->waitfunc = sand_nand_wait;
+		nand->select_chip = sand_nand_select_chip;
+		nand->read_byte = sand_nand_read_byte;
+		nand->read_word = sand_nand_read_word;
+		nand->read_buf = sand_nand_read_buf;
+		nand->write_buf = sand_nand_write_buf;
+		nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
+
+		mtd = nand_to_mtd(nand);
+		mtd->dev = dev;
+
+		ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
+		if (ret) {
+			dev_dbg(dev, "Could not scan chip %s: %d\n",
+				ofnode_get_name(np), ret);
+			goto err_prog;
+		}
+
+		ret = nand_register(devnum, mtd);
+		if (ret) {
+			dev_dbg(dev, "Could not register nand %d: %d\n", devnum,
+				ret);
+			goto err_prog;
+		}
+
+		if (!nand_chip)
+			nand_chip = nand;
+
+		list_add_tail(&chip->node, &priv->chips);
+		devnum++;
+		continue;
+
+err_prog:
+		free(chip->programmed);
+err_fd:
+		os_close(chip->fd);
+err_chip:
+		free(chip);
+		goto err;
+	}
+
+	return 0;
+
+err:
+	sand_nand_remove(dev);
+	return ret;
+}
+
+static const struct udevice_id sand_nand_ids[] = {
+	{ .compatible = "sandbox,nand" },
+	{ }
+};
+
+U_BOOT_DRIVER(sand_nand) = {
+	.name           = "sand-nand",
+	.id             = UCLASS_MTD,
+	.of_match       = sand_nand_ids,
+	.probe          = sand_nand_probe,
+	.remove		= sand_nand_remove,
+	.priv_auto	= sizeof(struct sand_nand_priv),
+};
+
+void board_nand_init(void)
+{
+	struct udevice *dev;
+	int err;
+
+	err = uclass_get_device_by_driver(UCLASS_MTD, DM_DRIVER_REF(sand_nand),
+					  &dev);
+	if (err && err != -ENODEV)
+		log_info("Failed to get sandbox NAND: %d\n", err);
+}
diff --git a/test/dm/Makefile b/test/dm/Makefile
index cb82d839f8a..a3ce7b3889f 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_CMD_MUX) += mux-cmd.o
 obj-$(CONFIG_MULTIPLEXER) += mux-emul.o
 obj-$(CONFIG_MUX_MMIO) += mux-mmio.o
 obj-y += fdtdec.o
+obj-$(CONFIG_MTD_RAW_NAND) += nand.o
 obj-$(CONFIG_UT_DM) += nop.o
 obj-y += ofnode.o
 obj-y += ofread.o
diff --git a/test/dm/nand.c b/test/dm/nand.c
new file mode 100644
index 00000000000..a1304965072
--- /dev/null
+++ b/test/dm/nand.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
+ */
+
+#include <nand.h>
+#include <part.h>
+#include <rand.h>
+#include <dm/test.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/rawnand.h>
+
+static int dm_test_nand(struct unit_test_state *uts, int dev, bool end)
+{
+	nand_erase_options_t opts = { };
+	struct mtd_info *mtd;
+	size_t length;
+	loff_t size;
+	char *buf;
+	int *gold;
+	u8 oob[NAND_MAX_OOBSIZE];
+	int i;
+	loff_t off = 0;
+	mtd_oob_ops_t ops = { };
+
+	/* Seed RNG for bit errors */
+	srand((off >> 32) ^ off ^ ~dev);
+
+	mtd = get_nand_dev_by_index(dev);
+	ut_assertnonnull(mtd);
+	size = mtd->erasesize * 4;
+	length = size;
+
+	buf = malloc(size);
+	ut_assertnonnull(buf);
+	gold = malloc(size);
+	ut_assertnonnull(gold);
+
+	/* Mark a block as bad */
+	ut_assertok(mtd_block_markbad(mtd, off + mtd->erasesize));
+	/* Save the OOB for later */
+	ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
+
+	/* Erase some stuff */
+	if (end)
+		off = mtd->size - size - mtd->erasesize;
+	opts.offset = off;
+	opts.length = size;
+	opts.spread = 1;
+	opts.lim = U32_MAX;
+	ut_assertok(nand_erase_opts(mtd, &opts));
+
+	/* Make sure everything is erased */
+	memset(gold, 0xff, size);
+	ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
+	ut_asserteq(size, length);
+	ut_asserteq_mem(gold, buf, size);
+
+	/* ...but our bad block marker is still there */
+	ops.oobbuf = oob;
+	ops.ooblen = mtd->oobsize;
+	ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
+	ut_asserteq(0, oob[mtd_to_nand(mtd)->badblockpos]);
+
+	/* Generate some data and write it */
+	for (i = 0; i < size / sizeof(int); i++)
+		gold[i] = 0;//rand();
+	ut_assertok(nand_write_skip_bad(mtd, off, &length, NULL, U64_MAX,
+					(void *)gold, 0));
+	ut_asserteq(size, length);
+
+	/* Verify */
+	ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
+	ut_asserteq(size, length);
+	ut_asserteq_mem(gold, buf, size);
+
+	/* Erase some blocks */
+	memset(((char *)gold) + mtd->erasesize, 0xff, mtd->erasesize * 2);
+	opts.offset = off + mtd->erasesize;
+	opts.length = mtd->erasesize * 2,
+	ut_assertok(nand_erase_opts(mtd, &opts));
+
+	/* Verify */
+	ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
+	ut_asserteq(size, length);
+	ut_asserteq_mem(gold, buf, size);
+
+	return 0;
+}
+
+#define DM_NAND_TEST(dev) \
+static int dm_test_nand##dev##_start(struct unit_test_state *uts) \
+{ \
+	return dm_test_nand(uts, dev, false); \
+} \
+DM_TEST(dm_test_nand##dev##_start, UT_TESTF_SCAN_FDT); \
+static int dm_test_nand##dev##_end(struct unit_test_state *uts) \
+{ \
+	return dm_test_nand(uts, dev, true); \
+} \
+DM_TEST(dm_test_nand##dev##_end, UT_TESTF_SCAN_FDT)
+
+DM_NAND_TEST(0);
+DM_NAND_TEST(1);
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH 15/15] test: spl: Add a test for NAND
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (13 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 14/15] nand: Add sandbox driver Sean Anderson
@ 2023-10-29  3:48 ` Sean Anderson
  2023-11-02 14:01 ` [PATCH 00/15] nand: Add sandbox tests Dario Binacchi
  15 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29  3:48 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi; +Cc: Tom Rini, Sean Anderson

Add a SPL test for the NAND load method. We use some different functions to
do the writing from the main test since things like nand_write_skip_bad
aren't available in SPL.

We disable BBT scanning, since scan_bbt is only populated when not in SPL.
We use nand_spl_loaders.c as it seems to be common to at least a few boards
already. However, we do not use nand_spl_simple.c because it would require
us to implement cmd_ctrl.  The various nand load functions are adapted from
omap_gpmc. However, they have been modified for simplicity/correctness.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 arch/sandbox/include/asm/spl.h   |  1 +
 configs/sandbox_noinst_defconfig | 11 +++++++
 drivers/mtd/nand/raw/Kconfig     |  5 ++-
 drivers/mtd/nand/raw/sand_nand.c | 29 +++++++++++++++++
 test/image/Kconfig               |  9 ++++++
 test/image/Makefile              |  1 +
 test/image/spl_load_nand.c       | 54 ++++++++++++++++++++++++++++++++
 7 files changed, 109 insertions(+), 1 deletion(-)
 create mode 100644 test/image/spl_load_nand.c

diff --git a/arch/sandbox/include/asm/spl.h b/arch/sandbox/include/asm/spl.h
index f349ea19971..4fab24cd156 100644
--- a/arch/sandbox/include/asm/spl.h
+++ b/arch/sandbox/include/asm/spl.h
@@ -15,6 +15,7 @@ enum {
 	BOOT_DEVICE_CPGMAC,
 	BOOT_DEVICE_NOR,
 	BOOT_DEVICE_SPI,
+	BOOT_DEVICE_NAND,
 };
 
 /**
diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
index 09ebafeccca..0e5f84abbd8 100644
--- a/configs/sandbox_noinst_defconfig
+++ b/configs/sandbox_noinst_defconfig
@@ -51,6 +51,13 @@ CONFIG_SPL_ETH=y
 CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
 CONFIG_SPL_MMC_WRITE=y
+CONFIG_SPL_MTD=y
+CONFIG_SPL_NAND_SUPPORT=y
+CONFIG_SPL_NAND_DRIVERS=y
+CONFIG_SPL_NAND_ECC=y
+CONFIG_SPL_NAND_SOFTECC=y
+CONFIG_SPL_NAND_BASE=y
+CONFIG_SPL_NAND_IDENT=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NET=y
 CONFIG_SPL_NOR_SUPPORT=y
@@ -183,12 +190,16 @@ CONFIG_FS_LOADER=y
 CONFIG_MMC_SANDBOX=y
 CONFIG_MTD=y
 CONFIG_DM_MTD=y
+CONFIG_MTD_CONCAT=y
 CONFIG_MTD_RAW_NAND=y
 CONFIG_SYS_MAX_NAND_DEVICE=8
 CONFIG_SYS_NAND_USE_FLASH_BBT=y
 CONFIG_NAND_SANDBOX=y
+CONFIG_SYS_NAND_BLOCK_SIZE=0x2000
 CONFIG_SYS_NAND_ONFI_DETECTION=y
 CONFIG_SYS_NAND_PAGE_SIZE=0x200
+CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
+CONFIG_SYS_NAND_U_BOOT_OFFS=0x0
 CONFIG_SPI_FLASH_SANDBOX=y
 CONFIG_SPI_FLASH_ATMEL=y
 CONFIG_SPI_FLASH_EON=y
diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index 6b34aa6782c..1e909a250d7 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -451,6 +451,8 @@ config NAND_SANDBOX
 	bool "Support for NAND in sandbox"
 	depends on SANDBOX
 	select SYS_NAND_SELF_INIT
+	select SPL_SYS_NAND_SELF_INIT
+	select SPL_NAND_INIT
 	select SYS_NAND_SOFT_ECC
 	select BCH
 	select NAND_ECC_BCH
@@ -679,7 +681,8 @@ config SYS_NAND_PAGE_SIZE
 	depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
 		SPL_NAND_SIMPLE || (NAND_MXC && SPL_NAND_SUPPORT) || \
 		MVEBU_SPL_BOOT_DEVICE_NAND || \
-		(NAND_ATMEL && SPL_NAND_SUPPORT) || SPL_GENERATE_ATMEL_PMECC_HEADER
+		(NAND_ATMEL && SPL_NAND_SUPPORT) || \
+		SPL_GENERATE_ATMEL_PMECC_HEADER || NAND_SANDBOX
 	depends on !NAND_MXS && !NAND_DENALI_DT && !NAND_LPC32XX_MLC && !NAND_MT7621
 	help
 	  Number of data bytes in one page for the NAND chip on the
diff --git a/drivers/mtd/nand/raw/sand_nand.c b/drivers/mtd/nand/raw/sand_nand.c
index f1855877630..667a633c02f 100644
--- a/drivers/mtd/nand/raw/sand_nand.c
+++ b/drivers/mtd/nand/raw/sand_nand.c
@@ -10,6 +10,8 @@
 #include <nand.h>
 #include <os.h>
 #include <rand.h>
+#include <spl.h>
+#include <system-constants.h>
 #include <dm/device_compat.h>
 #include <dm/read.h>
 #include <dm/uclass.h>
@@ -605,6 +607,7 @@ static int sand_nand_probe(struct udevice *dev)
 		}
 
 		nand = &chip->nand;
+		nand->options = spl_in_proper() ? 0 : NAND_SKIP_BBTSCAN;
 		nand->flash_node = np;
 		nand->dev_ready = sand_nand_dev_ready;
 		nand->cmdfunc = sand_nand_command;
@@ -680,3 +683,29 @@ void board_nand_init(void)
 	if (err && err != -ENODEV)
 		log_info("Failed to get sandbox NAND: %d\n", err);
 }
+
+#if IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_SPL_NAND_INIT)
+void nand_deselect(void)
+{
+	nand_chip->select_chip(nand_to_mtd(nand_chip), -1);
+}
+
+static int nand_is_bad_block(int block)
+{
+	struct mtd_info *mtd = nand_to_mtd(nand_chip);
+
+	return mtd_block_isbad(mtd, block << mtd->erasesize_shift);
+}
+
+static int nand_read_page(int block, int page, uchar *dst)
+{
+	struct mtd_info *mtd = nand_to_mtd(nand_chip);
+	loff_t ofs = ((loff_t)block << mtd->erasesize_shift) +
+		     ((loff_t)page << mtd->writesize_shift);
+	size_t len = mtd->writesize;
+
+	return nand_read(mtd, ofs, &len, dst);
+}
+
+#include "nand_spl_loaders.c"
+#endif /* CONFIG_SPL_NAND_INIT */
diff --git a/test/image/Kconfig b/test/image/Kconfig
index 8f9e6ae036b..6f0bb81f835 100644
--- a/test/image/Kconfig
+++ b/test/image/Kconfig
@@ -23,6 +23,15 @@ config SPL_UT_LOAD_FS
 	help
 	  Test filesystems and the various load methods which use them.
 
+config SPL_UT_LOAD_NAND
+	bool "Test loading from NAND flash"
+	depends on SANDBOX && SPL_OF_REAL
+	depends on SPL_NAND_SUPPORT
+	depends on SPL_MTD
+	default y
+	help
+	  Test the NAND flash load method.
+
 config SPL_UT_LOAD_NET
 	bool "Test loading over TFTP"
 	depends on SANDBOX && SPL_OF_REAL
diff --git a/test/image/Makefile b/test/image/Makefile
index b30210106a4..11ed25734e8 100644
--- a/test/image/Makefile
+++ b/test/image/Makefile
@@ -4,6 +4,7 @@
 
 obj-y += spl_load.o
 obj-$(CONFIG_SPL_UT_LOAD_FS) += spl_load_fs.o
+obj-$(CONFIG_SPL_UT_LOAD_NAND) += spl_load_nand.o
 obj-$(CONFIG_SPL_UT_LOAD_NET) += spl_load_net.o
 obj-$(CONFIG_SPL_NOR_SUPPORT) += spl_load_nor.o
 obj-$(CONFIG_SPL_UT_LOAD_OS) += spl_load_os.o
diff --git a/test/image/spl_load_nand.c b/test/image/spl_load_nand.c
new file mode 100644
index 00000000000..30179de98e7
--- /dev/null
+++ b/test/image/spl_load_nand.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
+ */
+
+#include <nand.h>
+#include <spl.h>
+#include <test/spl.h>
+#include <test/ut.h>
+
+uint32_t spl_nand_get_uboot_raw_page(void);
+
+static int spl_test_nand_write_image(struct unit_test_state *uts, void *img,
+				     size_t img_size)
+{
+	uint32_t off = spl_nand_get_uboot_raw_page();
+	struct mtd_info *mtd;
+	struct erase_info erase = { };
+	size_t length;
+
+	nand_reinit();
+	mtd = get_nand_dev_by_index(0);
+	ut_assertnonnull(mtd);
+
+	/* Mark the first block as bad to test that it gets skipped */
+	ut_assertok(mtd_block_markbad(mtd, off & ~mtd->erasesize_mask));
+	off += mtd->erasesize;
+
+	erase.mtd = mtd;
+	erase.len = img_size + (off & mtd->erasesize_mask);
+	erase.len += mtd->erasesize_mask;
+	erase.len &= ~mtd->erasesize_mask;
+	erase.addr = off & ~mtd->erasesize_mask;
+	erase.scrub = 1;
+	ut_assertok(mtd_erase(mtd, &erase));
+
+	ut_assertok(mtd_write(mtd, off, img_size, &length, img));
+
+	return 0;
+}
+
+static int spl_test_nand(struct unit_test_state *uts, const char *test_name,
+			 enum spl_test_image type)
+{
+	return do_spl_test_load(uts, test_name, type,
+				SPL_LOAD_IMAGE_GET(1, BOOT_DEVICE_NAND,
+						   spl_nand_load_image),
+				spl_test_nand_write_image);
+}
+SPL_IMG_TEST(spl_test_nand, LEGACY, DM_FLAGS);
+SPL_IMG_TEST(spl_test_nand, LEGACY_LZMA, DM_FLAGS);
+SPL_IMG_TEST(spl_test_nand, IMX8, DM_FLAGS);
+SPL_IMG_TEST(spl_test_nand, FIT_INTERNAL, DM_FLAGS);
+SPL_IMG_TEST(spl_test_nand, FIT_EXTERNAL, DM_FLAGS);
-- 
2.37.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc
  2023-10-29  3:48 ` [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc Sean Anderson
@ 2023-10-29 13:06   ` Michael Nazzareno Trimarchi
  0 siblings, 0 replies; 34+ messages in thread
From: Michael Nazzareno Trimarchi @ 2023-10-29 13:06 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Dario Binacchi, Tom Rini, Heiko Schocher

Hi

On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> When no manufacturer is matched, manufacturer_desc is NULL. Avoid
> dereferencing it in that case.
>
> Fixes: 4e67c571252 ("mtd,ubi,ubifs: sync with linux v3.15")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  drivers/mtd/nand/raw/nand_base.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
> index 6b4adcf6bdc..44b6cb63a01 100644
> --- a/drivers/mtd/nand/raw/nand_base.c
> +++ b/drivers/mtd/nand/raw/nand_base.c
> @@ -4462,17 +4462,14 @@ ident_done:
>         else if (chip->jedec_version)
>                 pr_info("%s %s\n", manufacturer_desc->name,
>                         chip->jedec_params.model);
> -       else
> +       else if (manufacturer_desc)
>                 pr_info("%s %s\n", manufacturer_desc->name, type->name);
>  #else
>         if (chip->jedec_version)
>                 pr_info("%s %s\n", manufacturer_desc->name,
>                         chip->jedec_params.model);
> -       else
> +       else if (manufacturer_desc)
>                 pr_info("%s %s\n", manufacturer_desc->name, type->name);
> -
> -       pr_info("%s %s\n", manufacturer_desc->name,
> -               type->name);
>  #endif
>
>         pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
> --
> 2.37.1
>

Reviewed-by: Michael Trimarchi <michael@amarulasolutions.com>


-- 
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
michael@amarulasolutions.com
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
info@amarulasolutions.com
www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image
  2023-10-29  3:48 ` [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image Sean Anderson
@ 2023-10-29 13:08   ` Michael Nazzareno Trimarchi
  0 siblings, 0 replies; 34+ messages in thread
From: Michael Nazzareno Trimarchi @ 2023-10-29 13:08 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Dario Binacchi, Tom Rini

Hi

On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> All other implementations of nand_spl_load_image only read as many pages as
> are necessary to load the image. However, nand_spl_loaders.c loads the full
> block. Align it with other load functions so that it is easier to
> determine how large of a load buffer we need.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  drivers/mtd/nand/raw/nand_spl_loaders.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
> index 8848cb27db9..f071b5b57f5 100644
> --- a/drivers/mtd/nand/raw/nand_spl_loaders.c
> +++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
> @@ -12,8 +12,11 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
>         while (block <= lastblock) {
>                 if (!nand_is_bad_block(block)) {
>                         /* Skip bad blocks */
> -                       while (page < SYS_NAND_PAGE_COUNT) {
> +                       while (size && page < SYS_NAND_PAGE_COUNT) {
>                                 nand_read_page(block, page, dst);
> +
> +                               size -= min(size, CONFIG_SYS_NAND_PAGE_SIZE -
> +                                                 page_offset);
>                                 /*
>                                  * When offs is not aligned to page address the
>                                  * extra offset is copied to dst as well. Copy
> --
> 2.37.1
>

Reviewed-by: Michael Trimarchi <michael@amarulasolutions.com>
-- 
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
michael@amarulasolutions.com
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
info@amarulasolutions.com
www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 01/15] spl: nand: Fix NULL-pointer dereference
  2023-10-29  3:48 ` [PATCH 01/15] spl: nand: Fix NULL-pointer dereference Sean Anderson
@ 2023-10-29 14:15   ` Sean Anderson
  0 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-10-29 14:15 UTC (permalink / raw)
  To: u-boot, Dario Binacchi, Michael Trimarchi
  Cc: Tom Rini, Daniel Schwierzeck, Peng Fan, Weijie Gao

On 10/28/23 23:48, Sean Anderson wrote:
> spl_nand_fit_read unconditionally accesses load->priv. Ensure it is set.
> 
> Fixes: 00e180cc513 ("spl: nand: support loading i.MX container format file")
> Fixes: 4620e8aabc1 ("spl: nand: support loading legacy image with payload compressed")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
> 
>   common/spl/spl_nand.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/common/spl/spl_nand.c b/common/spl/spl_nand.c
> index 07916bedbb9..a19236d9e6d 100644
> --- a/common/spl/spl_nand.c
> +++ b/common/spl/spl_nand.c
> @@ -105,7 +105,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
>   		struct spl_load_info load;
>   
>   		load.dev = NULL;
> -		load.priv = NULL;
> +		load.priv = &offset;
>   		load.filename = NULL;
>   		load.bl_len = bl_len;
>   		load.read = spl_nand_fit_read;
> @@ -116,7 +116,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
>   
>   		debug("Found legacy image\n");
>   		load.dev = NULL;
> -		load.priv = NULL;
> +		load.priv = &offset;
>   		load.filename = NULL;
>   		load.bl_len = 1;
>   		load.read = spl_nand_legacy_read;

Actually, since spl_nand_legacy_read doesn't reference priv, this second hunk is
unnecessary. Actually, spl_nand_legacy_read and spl_load_legacy_img are technically buggy
since size/offset are supposed to be in units of bl_len. However, this basically just
results in extra multiplies and divides, so I don't think it's desirable. I actually have
a patch to convert everything to bytes (keeping alignment), so "fixing" this is not
necessary for the moment.

--Sean

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically
  2023-10-29  3:48 ` [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically Sean Anderson
@ 2023-11-02  9:53   ` Dario Binacchi
  2023-11-02 14:17     ` Sean Anderson
  0 siblings, 1 reply; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02  9:53 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All

On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> Contrary to what the help message says, this is the number of pages per
> block. Calculate it automatically based on SYS_NAND_BLOCK_SIZE and
> SYS_NAND_PAGE_SIZE.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  README                                          | 9 ++++-----
>  configs/am335x_baltos_defconfig                 | 1 -
>  configs/am335x_evm_defconfig                    | 1 -
>  configs/am335x_guardian_defconfig               | 1 -
>  configs/am335x_igep003x_defconfig               | 1 -
>  configs/am3517_evm_defconfig                    | 1 -
>  configs/am43xx_evm_defconfig                    | 1 -
>  configs/am43xx_evm_rtconly_defconfig            | 1 -
>  configs/am43xx_evm_usbhost_boot_defconfig       | 1 -
>  configs/am43xx_hs_evm_defconfig                 | 1 -
>  configs/axm_defconfig                           | 1 -
>  configs/chiliboard_defconfig                    | 1 -
>  configs/corvus_defconfig                        | 1 -
>  configs/da850evm_nand_defconfig                 | 1 -
>  configs/devkit3250_defconfig                    | 1 -
>  configs/devkit8000_defconfig                    | 1 -
>  configs/dra7xx_evm_defconfig                    | 1 -
>  configs/draco_defconfig                         | 1 -
>  configs/etamin_defconfig                        | 1 -
>  configs/gardena-smart-gateway-at91sam_defconfig | 1 -
>  configs/igep00x0_defconfig                      | 1 -
>  configs/m53menlo_defconfig                      | 1 -
>  configs/omap35_logic_defconfig                  | 1 -
>  configs/omap35_logic_somlv_defconfig            | 1 -
>  configs/omap3_beagle_defconfig                  | 1 -
>  configs/omap3_evm_defconfig                     | 1 -
>  configs/omap3_logic_defconfig                   | 1 -
>  configs/omap3_logic_somlv_defconfig             | 1 -
>  configs/omapl138_lcdk_defconfig                 | 1 -
>  configs/phycore-am335x-r2-regor_defconfig       | 1 -
>  configs/phycore-am335x-r2-wega_defconfig        | 1 -
>  configs/pxm2_defconfig                          | 1 -
>  configs/rastaban_defconfig                      | 1 -
>  configs/rut_defconfig                           | 1 -
>  configs/sama5d3_xplained_nandflash_defconfig    | 1 -
>  configs/sama5d3xek_nandflash_defconfig          | 1 -
>  configs/sama5d4_xplained_nandflash_defconfig    | 1 -
>  configs/sama5d4ek_nandflash_defconfig           | 1 -
>  configs/smartweb_defconfig                      | 1 -
>  configs/taurus_defconfig                        | 1 -
>  configs/thuban_defconfig                        | 1 -
>  drivers/mtd/nand/raw/Kconfig                    | 8 --------
>  drivers/mtd/nand/raw/am335x_spl_bch.c           | 3 ++-
>  drivers/mtd/nand/raw/atmel_nand.c               | 5 +++--
>  drivers/mtd/nand/raw/mxc_nand_spl.c             | 5 +++--
>  drivers/mtd/nand/raw/nand_spl_loaders.c         | 2 +-
>  drivers/mtd/nand/raw/nand_spl_simple.c          | 5 +++--
>  drivers/mtd/nand/raw/omap_gpmc.c                | 3 ++-
>  include/system-constants.h                      | 3 +++
>  49 files changed, 21 insertions(+), 62 deletions(-)
>
> diff --git a/README b/README
> index 60c6b8a19db..00d422737fb 100644
> --- a/README
> +++ b/README
> @@ -1191,11 +1191,10 @@ The following options need to be configured:
>                 Support for a lightweight UBI (fastmap) scanner and
>                 loader
>
> -               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_COUNT,
> -               CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE,
> -               CONFIG_SYS_NAND_BLOCK_SIZE, CONFIG_SYS_NAND_BAD_BLOCK_POS,
> -               CFG_SYS_NAND_ECCPOS, CFG_SYS_NAND_ECCSIZE,
> -               CFG_SYS_NAND_ECCBYTES
> +               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_SIZE,
> +               CONFIG_SYS_NAND_OOBSIZE, CONFIG_SYS_NAND_BLOCK_SIZE,
> +               CONFIG_SYS_NAND_BAD_BLOCK_POS, CFG_SYS_NAND_ECCPOS,
> +               CFG_SYS_NAND_ECCSIZE, CFG_SYS_NAND_ECCBYTES
>                 Defines the size and behavior of the NAND that SPL uses
>                 to read U-Boot
>
> diff --git a/configs/am335x_baltos_defconfig b/configs/am335x_baltos_defconfig
> index 16993ef5386..3891e48e9c7 100644
> --- a/configs/am335x_baltos_defconfig
> +++ b/configs/am335x_baltos_defconfig
> @@ -67,7 +67,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am335x_evm_defconfig b/configs/am335x_evm_defconfig
> index f048e60f7f3..73f221d55bc 100644
> --- a/configs/am335x_evm_defconfig
> +++ b/configs/am335x_evm_defconfig
> @@ -84,7 +84,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am335x_guardian_defconfig b/configs/am335x_guardian_defconfig
> index 01d848ceede..5369e46bc1b 100644
> --- a/configs/am335x_guardian_defconfig
> +++ b/configs/am335x_guardian_defconfig
> @@ -99,7 +99,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0x100
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am335x_igep003x_defconfig b/configs/am335x_igep003x_defconfig
> index 4dd6366ef67..4c5c82f9bdd 100644
> --- a/configs/am335x_igep003x_defconfig
> +++ b/configs/am335x_igep003x_defconfig
> @@ -85,7 +85,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_MTD_UBI_FASTMAP=y
> diff --git a/configs/am3517_evm_defconfig b/configs/am3517_evm_defconfig
> index 0a83ac9378c..111929fb912 100644
> --- a/configs/am3517_evm_defconfig
> +++ b/configs/am3517_evm_defconfig
> @@ -75,7 +75,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/am43xx_evm_defconfig b/configs/am43xx_evm_defconfig
> index 6571afd345e..5f2356bcd32 100644
> --- a/configs/am43xx_evm_defconfig
> +++ b/configs/am43xx_evm_defconfig
> @@ -72,7 +72,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am43xx_evm_rtconly_defconfig b/configs/am43xx_evm_rtconly_defconfig
> index e84aed4d54d..30681e7eeb6 100644
> --- a/configs/am43xx_evm_rtconly_defconfig
> +++ b/configs/am43xx_evm_rtconly_defconfig
> @@ -62,7 +62,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am43xx_evm_usbhost_boot_defconfig b/configs/am43xx_evm_usbhost_boot_defconfig
> index 0cae3242b04..6c4cc99e5ff 100644
> --- a/configs/am43xx_evm_usbhost_boot_defconfig
> +++ b/configs/am43xx_evm_usbhost_boot_defconfig
> @@ -75,7 +75,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/am43xx_hs_evm_defconfig b/configs/am43xx_hs_evm_defconfig
> index 370ee967258..49ecb4c9e66 100644
> --- a/configs/am43xx_hs_evm_defconfig
> +++ b/configs/am43xx_hs_evm_defconfig
> @@ -68,7 +68,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/axm_defconfig b/configs/axm_defconfig
> index f9c02144c4a..e1a01b24b88 100644
> --- a/configs/axm_defconfig
> +++ b/configs/axm_defconfig
> @@ -86,7 +86,6 @@ CONFIG_MTD=y
>  # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>  CONFIG_NAND_ATMEL=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/chiliboard_defconfig b/configs/chiliboard_defconfig
> index 06642d282d1..0cd649d643a 100644
> --- a/configs/chiliboard_defconfig
> +++ b/configs/chiliboard_defconfig
> @@ -63,7 +63,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/corvus_defconfig b/configs/corvus_defconfig
> index ecf61f0d1e6..5eaa6dbb5fc 100644
> --- a/configs/corvus_defconfig
> +++ b/configs/corvus_defconfig
> @@ -81,7 +81,6 @@ CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT=y
>  # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>  CONFIG_NAND_ATMEL=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/da850evm_nand_defconfig b/configs/da850evm_nand_defconfig
> index 5f6f5d788d0..62b8edde248 100644
> --- a/configs/da850evm_nand_defconfig
> +++ b/configs/da850evm_nand_defconfig
> @@ -88,7 +88,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_USE_FLASH_BBT=y
>  CONFIG_NAND_DAVINCI=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/devkit3250_defconfig b/configs/devkit3250_defconfig
> index ec4031da51d..569b156c997 100644
> --- a/configs/devkit3250_defconfig
> +++ b/configs/devkit3250_defconfig
> @@ -73,7 +73,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_USE_FLASH_BBT=y
>  CONFIG_NAND_LPC32XX_SLC=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  # CONFIG_SYS_NAND_5_ADDR_CYCLE is not set
> diff --git a/configs/devkit8000_defconfig b/configs/devkit8000_defconfig
> index ef16da176a9..a7f82442eb0 100644
> --- a/configs/devkit8000_defconfig
> +++ b/configs/devkit8000_defconfig
> @@ -77,7 +77,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/dra7xx_evm_defconfig b/configs/dra7xx_evm_defconfig
> index 19ca89fa867..c3a3ec2cd42 100644
> --- a/configs/dra7xx_evm_defconfig
> +++ b/configs/dra7xx_evm_defconfig
> @@ -104,7 +104,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/draco_defconfig b/configs/draco_defconfig
> index ee19920a703..2a82087ab31 100644
> --- a/configs/draco_defconfig
> +++ b/configs/draco_defconfig
> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/etamin_defconfig b/configs/etamin_defconfig
> index c0ce7a33569..4ac0abc56b0 100644
> --- a/configs/etamin_defconfig
> +++ b/configs/etamin_defconfig
> @@ -104,7 +104,6 @@ CONFIG_SYS_MAX_NAND_DEVICE=3
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x80000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x80
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/gardena-smart-gateway-at91sam_defconfig b/configs/gardena-smart-gateway-at91sam_defconfig
> index 439fcc05753..0fb92ff1638 100644
> --- a/configs/gardena-smart-gateway-at91sam_defconfig
> +++ b/configs/gardena-smart-gateway-at91sam_defconfig
> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>  CONFIG_NAND_ATMEL=y
>  CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/igep00x0_defconfig b/configs/igep00x0_defconfig
> index 993bbe29c6b..3b1af07f057 100644
> --- a/configs/igep00x0_defconfig
> +++ b/configs/igep00x0_defconfig
> @@ -78,7 +78,6 @@ CONFIG_SYS_MTDPARTS_RUNTIME=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/m53menlo_defconfig b/configs/m53menlo_defconfig
> index ef9e15d1520..e1ddc0af5db 100644
> --- a/configs/m53menlo_defconfig
> +++ b/configs/m53menlo_defconfig
> @@ -90,7 +90,6 @@ CONFIG_SYS_NAND_USE_FLASH_BBT=y
>  CONFIG_NAND_MXC=y
>  CONFIG_MXC_NAND_HWECC=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
> index 539b0cfa4f7..3a656072534 100644
> --- a/configs/omap35_logic_defconfig
> +++ b/configs/omap35_logic_defconfig
> @@ -74,7 +74,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omap35_logic_somlv_defconfig b/configs/omap35_logic_somlv_defconfig
> index f14ce3d23db..868e89114b7 100644
> --- a/configs/omap35_logic_somlv_defconfig
> +++ b/configs/omap35_logic_somlv_defconfig
> @@ -83,7 +83,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omap3_beagle_defconfig b/configs/omap3_beagle_defconfig
> index cabd3659385..7f0b927528d 100644
> --- a/configs/omap3_beagle_defconfig
> +++ b/configs/omap3_beagle_defconfig
> @@ -83,7 +83,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omap3_evm_defconfig b/configs/omap3_evm_defconfig
> index 33ff39ff548..3434783d45b 100644
> --- a/configs/omap3_evm_defconfig
> +++ b/configs/omap3_evm_defconfig
> @@ -74,7 +74,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
> index 7d3d602c9d9..78bfbbd1a22 100644
> --- a/configs/omap3_logic_defconfig
> +++ b/configs/omap3_logic_defconfig
> @@ -73,7 +73,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omap3_logic_somlv_defconfig b/configs/omap3_logic_somlv_defconfig
> index 0141a425e07..aefd8861db3 100644
> --- a/configs/omap3_logic_somlv_defconfig
> +++ b/configs/omap3_logic_somlv_defconfig
> @@ -84,7 +84,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/omapl138_lcdk_defconfig b/configs/omapl138_lcdk_defconfig
> index 47feff21bd4..63554d56016 100644
> --- a/configs/omapl138_lcdk_defconfig
> +++ b/configs/omapl138_lcdk_defconfig
> @@ -84,7 +84,6 @@ CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_USE_FLASH_BBT=y
>  CONFIG_NAND_DAVINCI=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> diff --git a/configs/phycore-am335x-r2-regor_defconfig b/configs/phycore-am335x-r2-regor_defconfig
> index 4bdf8822181..85d53d06357 100644
> --- a/configs/phycore-am335x-r2-regor_defconfig
> +++ b/configs/phycore-am335x-r2-regor_defconfig
> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/phycore-am335x-r2-wega_defconfig b/configs/phycore-am335x-r2-wega_defconfig
> index 7644fbec273..60607548a25 100644
> --- a/configs/phycore-am335x-r2-wega_defconfig
> +++ b/configs/phycore-am335x-r2-wega_defconfig
> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/pxm2_defconfig b/configs/pxm2_defconfig
> index 2ea007d0de5..e1d1066815b 100644
> --- a/configs/pxm2_defconfig
> +++ b/configs/pxm2_defconfig
> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/rastaban_defconfig b/configs/rastaban_defconfig
> index 9f538a2f83d..21495341e26 100644
> --- a/configs/rastaban_defconfig
> +++ b/configs/rastaban_defconfig
> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/rut_defconfig b/configs/rut_defconfig
> index ccf25667b50..248073be1f2 100644
> --- a/configs/rut_defconfig
> +++ b/configs/rut_defconfig
> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/sama5d3_xplained_nandflash_defconfig b/configs/sama5d3_xplained_nandflash_defconfig
> index a1b6122f8eb..a0802f9c5fe 100644
> --- a/configs/sama5d3_xplained_nandflash_defconfig
> +++ b/configs/sama5d3_xplained_nandflash_defconfig
> @@ -86,7 +86,6 @@ CONFIG_PMECC_CAP=4
>  CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/sama5d3xek_nandflash_defconfig b/configs/sama5d3xek_nandflash_defconfig
> index 6278a6c68a7..d96bb91e65d 100644
> --- a/configs/sama5d3xek_nandflash_defconfig
> +++ b/configs/sama5d3xek_nandflash_defconfig
> @@ -90,7 +90,6 @@ CONFIG_PMECC_CAP=4
>  CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/sama5d4_xplained_nandflash_defconfig b/configs/sama5d4_xplained_nandflash_defconfig
> index d12f749d7f4..68101a12fc2 100644
> --- a/configs/sama5d4_xplained_nandflash_defconfig
> +++ b/configs/sama5d4_xplained_nandflash_defconfig
> @@ -84,7 +84,6 @@ CONFIG_PMECC_CAP=8
>  CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/sama5d4ek_nandflash_defconfig b/configs/sama5d4ek_nandflash_defconfig
> index 27bfcdf0084..b8062db775d 100644
> --- a/configs/sama5d4ek_nandflash_defconfig
> +++ b/configs/sama5d4ek_nandflash_defconfig
> @@ -81,7 +81,6 @@ CONFIG_PMECC_CAP=8
>  CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>  CONFIG_SYS_NAND_OOBSIZE=0xe0
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/smartweb_defconfig b/configs/smartweb_defconfig
> index 10cbccb69b0..80d2c0f1cc1 100644
> --- a/configs/smartweb_defconfig
> +++ b/configs/smartweb_defconfig
> @@ -84,7 +84,6 @@ CONFIG_MTD=y
>  # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>  CONFIG_NAND_ATMEL=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/taurus_defconfig b/configs/taurus_defconfig
> index cc5755178b7..9be30c8d8a4 100644
> --- a/configs/taurus_defconfig
> +++ b/configs/taurus_defconfig
> @@ -94,7 +94,6 @@ CONFIG_MTD=y
>  # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>  CONFIG_NAND_ATMEL=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/configs/thuban_defconfig b/configs/thuban_defconfig
> index 116700e0df0..1134624f9d2 100644
> --- a/configs/thuban_defconfig
> +++ b/configs/thuban_defconfig
> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>  CONFIG_MTD_RAW_NAND=y
>  CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>  CONFIG_SYS_NAND_ONFI_DETECTION=y
> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>  CONFIG_SYS_NAND_PAGE_SIZE=0x800
>  CONFIG_SYS_NAND_OOBSIZE=0x40
>  CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
> index 72547f00fbe..ee484dc0f51 100644
> --- a/drivers/mtd/nand/raw/Kconfig
> +++ b/drivers/mtd/nand/raw/Kconfig
> @@ -660,14 +660,6 @@ config SYS_NAND_ONFI_DETECTION
>           And fetching device parameters flashed on device, by parsing
>           ONFI parameter page.
>
> -config SYS_NAND_PAGE_COUNT
> -       hex "NAND chip page count"
> -       depends on SPL_NAND_SUPPORT && (NAND_ATMEL || NAND_MXC || \
> -               SPL_NAND_AM33XX_BCH || SPL_NAND_LOAD || SPL_NAND_SIMPLE || \
> -               NAND_OMAP_GPMC)
> -       help
> -         Number of pages in the NAND chip.
> -
>  config SYS_NAND_PAGE_SIZE
>         hex "NAND chip page size"
>         depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
> diff --git a/drivers/mtd/nand/raw/am335x_spl_bch.c b/drivers/mtd/nand/raw/am335x_spl_bch.c
> index 6ab3f1f42c5..3843ffc9a24 100644
> --- a/drivers/mtd/nand/raw/am335x_spl_bch.c
> +++ b/drivers/mtd/nand/raw/am335x_spl_bch.c
> @@ -11,6 +11,7 @@
>
>  #include <common.h>
>  #include <nand.h>
> +#include <system-constants.h>
>  #include <asm/io.h>
>  #include <linux/delay.h>
>  #include <linux/mtd/nand_ecc.h>
> @@ -32,7 +33,7 @@ static int nand_command(int block, int page, uint32_t offs,
>         u8 cmd)
>  {
>         struct nand_chip *this = mtd_to_nand(mtd);
> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>         void (*hwctrl)(struct mtd_info *mtd, int cmd,
>                         unsigned int ctrl) = this->cmd_ctrl;
>
> diff --git a/drivers/mtd/nand/raw/atmel_nand.c b/drivers/mtd/nand/raw/atmel_nand.c
> index 6b17e744a69..cfc6578ae83 100644
> --- a/drivers/mtd/nand/raw/atmel_nand.c
> +++ b/drivers/mtd/nand/raw/atmel_nand.c
> @@ -12,6 +12,7 @@
>
>  #include <common.h>
>  #include <log.h>
> +#include <system-constants.h>
>  #include <asm/gpio.h>
>  #include <asm/arch/gpio.h>
>  #include <dm/device_compat.h>
> @@ -1258,7 +1259,7 @@ static struct nand_chip nand_chip;
>  static int nand_command(int block, int page, uint32_t offs, u8 cmd)
>  {
>         struct nand_chip *this = mtd_to_nand(mtd);
> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>         void (*hwctrl)(struct mtd_info *mtd, int cmd,
>                         unsigned int ctrl) = this->cmd_ctrl;
>
> @@ -1359,7 +1360,7 @@ int spl_nand_erase_one(int block, int page)
>         if (nand_chip.select_chip)
>                 nand_chip.select_chip(mtd, 0);
>
> -       page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> +       page_addr = page + block * SYS_NAND_PAGE_COUNT;
>         hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
>         /* Row address */
>         hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
> diff --git a/drivers/mtd/nand/raw/mxc_nand_spl.c b/drivers/mtd/nand/raw/mxc_nand_spl.c
> index 309e75d01e5..059bf38f50e 100644
> --- a/drivers/mtd/nand/raw/mxc_nand_spl.c
> +++ b/drivers/mtd/nand/raw/mxc_nand_spl.c
> @@ -13,6 +13,7 @@
>  #include <common.h>
>  #include <hang.h>
>  #include <nand.h>
> +#include <system-constants.h>
>  #include <linux/mtd/rawnand.h>
>  #include <asm/arch/imx-regs.h>
>  #include <asm/io.h>
> @@ -304,13 +305,13 @@ int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
>                  * Check if we have crossed a block boundary, and if so
>                  * check for bad block.
>                  */
> -               if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
> +               if (!(page % SYS_NAND_PAGE_COUNT)) {
>                         /*
>                          * Yes, new block. See if this block is good. If not,
>                          * loop until we find a good block.
>                          */
>                         while (is_badblock(page)) {
> -                               page = page + CONFIG_SYS_NAND_PAGE_COUNT;
> +                               page = page + SYS_NAND_PAGE_COUNT;
>                                 /* Check i we've reached the end of flash. */
>                                 if (page >= maxpages)
>                                         return -1;
> diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
> index 156b44d8358..8848cb27db9 100644
> --- a/drivers/mtd/nand/raw/nand_spl_loaders.c
> +++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
> @@ -12,7 +12,7 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
>         while (block <= lastblock) {
>                 if (!nand_is_bad_block(block)) {
>                         /* Skip bad blocks */
> -                       while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
> +                       while (page < SYS_NAND_PAGE_COUNT) {
>                                 nand_read_page(block, page, dst);
>                                 /*
>                                  * When offs is not aligned to page address the
> diff --git a/drivers/mtd/nand/raw/nand_spl_simple.c b/drivers/mtd/nand/raw/nand_spl_simple.c
> index 2f3af9edd4c..2ebcac56900 100644
> --- a/drivers/mtd/nand/raw/nand_spl_simple.c
> +++ b/drivers/mtd/nand/raw/nand_spl_simple.c
> @@ -6,6 +6,7 @@
>
>  #include <common.h>
>  #include <nand.h>
> +#include <system-constants.h>
>  #include <asm/io.h>
>  #include <linux/mtd/nand_ecc.h>
>  #include <linux/mtd/rawnand.h>
> @@ -27,7 +28,7 @@ static int nand_command(int block, int page, uint32_t offs,
>         u8 cmd)
>  {
>         struct nand_chip *this = mtd_to_nand(mtd);
> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>
>         while (!this->dev_ready(mtd))
>                 ;
> @@ -59,7 +60,7 @@ static int nand_command(int block, int page, uint32_t offs,
>         u8 cmd)
>  {
>         struct nand_chip *this = mtd_to_nand(mtd);
> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>         void (*hwctrl)(struct mtd_info *mtd, int cmd,
>                         unsigned int ctrl) = this->cmd_ctrl;
>
> diff --git a/drivers/mtd/nand/raw/omap_gpmc.c b/drivers/mtd/nand/raw/omap_gpmc.c
> index 1a5ed0de31a..6e99538dea1 100644
> --- a/drivers/mtd/nand/raw/omap_gpmc.c
> +++ b/drivers/mtd/nand/raw/omap_gpmc.c
> @@ -6,6 +6,7 @@
>
>  #include <common.h>
>  #include <log.h>
> +#include <system-constants.h>
>  #include <asm/io.h>
>  #include <dm/uclass.h>
>  #include <linux/errno.h>
> @@ -1298,7 +1299,7 @@ static int nand_is_bad_block(int block)
>
>  static int nand_read_page(int block, int page, uchar *dst)
>  {
> -       int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
> +       int page_addr = block * SYS_NAND_PAGE_COUNT + page;
>         loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
>         int ret;
>         size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
> diff --git a/include/system-constants.h b/include/system-constants.h
> index 59371568d1e..c50d54f37a4 100644
> --- a/include/system-constants.h
> +++ b/include/system-constants.h
> @@ -41,4 +41,7 @@
>  #define SPL_PAYLOAD_ARGS_ADDR  0
>  #endif
>
> +#define SYS_NAND_PAGE_COUNT \
> +       (CONFIG_SYS_NAND_BLOCK_SIZE / CONFIG_SYS_NAND_PAGE_SIZE)
> +
>  #endif
> --
> 2.37.1
>

Reviewed-by:  Dario Binacchi <dario.binacchi@amarulasolutions.com>

What do you think about adding another patch to modify SYS_NAND_PAGE_COUNT
so that it is clearer that it refers to the number of pages in a block?

Thanks and regards,
Dario

--

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device
  2023-10-29  3:48 ` [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device Sean Anderson
@ 2023-11-02 10:45   ` Dario Binacchi
  0 siblings, 0 replies; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 10:45 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All

On Sun, Oct 29, 2023 at 4:49 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> This allows using these functions without ifdefs. OneNAND depends on MTD,
> so this ifdef was redundant in the first place.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  drivers/mtd/nand/raw/nand.c         |  2 --
>  drivers/mtd/onenand/onenand_uboot.c |  2 --
>  include/linux/mtd/mtd.h             | 12 ++++++++++++
>  3 files changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
> index 4da41438790..3abd82068fb 100644
> --- a/drivers/mtd/nand/raw/nand.c
> +++ b/drivers/mtd/nand/raw/nand.c
> @@ -60,13 +60,11 @@ int nand_register(int devnum, struct mtd_info *mtd)
>         sprintf(dev_name[devnum], "nand%d", devnum);
>         mtd->name = dev_name[devnum];
>
> -#ifdef CONFIG_MTD
>         /*
>          * Add MTD device so that we can reference it later
>          * via the mtdcore infrastructure (e.g. ubi).
>          */
>         add_mtd_device(mtd);
> -#endif
>
>         total_nand_size += mtd->size / 1024;
>
> diff --git a/drivers/mtd/onenand/onenand_uboot.c b/drivers/mtd/onenand/onenand_uboot.c
> index 04791df69bb..ecacabefadc 100644
> --- a/drivers/mtd/onenand/onenand_uboot.c
> +++ b/drivers/mtd/onenand/onenand_uboot.c
> @@ -44,14 +44,12 @@ void onenand_init(void)
>                         puts("Flex-");
>                 puts("OneNAND: ");
>
> -#ifdef CONFIG_MTD
>                 /*
>                  * Add MTD device so that we can reference it later
>                  * via the mtdcore infrastructure (e.g. ubi).
>                  */
>                 onenand_mtd.name = dev_name;
>                 add_mtd_device(&onenand_mtd);
> -#endif
>         }
>         print_size(onenand_chip.chipsize, "\n");
>  }
> diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
> index 09f52698877..7a66c7af749 100644
> --- a/include/linux/mtd/mtd.h
> +++ b/include/linux/mtd/mtd.h
> @@ -552,8 +552,20 @@ unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
>
>  #ifdef __UBOOT__
>  /* drivers/mtd/mtdcore.h */
> +#if CONFIG_IS_ENABLED(MTD)
>  int add_mtd_device(struct mtd_info *mtd);
>  int del_mtd_device(struct mtd_info *mtd);
> +#else
> +static inline int add_mtd_device(struct mtd_info *mtd)
> +{
> +       return -ENOSYS;
> +}
> +
> +static inline int del_mtd_device(struct mtd_info *mtd)
> +{
> +       return -ENOSYS;
> +}
> +#endif
>
>  #ifdef CONFIG_MTD_PARTITIONS
>  int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
> --
> 2.37.1
>

Reviewed-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

Thanks and regards,
Dario
-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 11/15] nand: Add function to unregister NAND devices
  2023-10-29  3:48 ` [PATCH 11/15] nand: Add function to unregister NAND devices Sean Anderson
@ 2023-11-02 10:58   ` Dario Binacchi
  0 siblings, 0 replies; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 10:58 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All,

On Sun, Oct 29, 2023 at 4:49 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> This performs the opposite of nand_register, allowing drivers to unregister
> nand devices. This is probably unnecessary for most regular drivers, but we
> expect sandbox drivers to get repeatedly bound/unbound, so this will help
> avoid dangling pointers.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  drivers/mtd/nand/raw/nand.c | 17 +++++++++++++++++
>  include/nand.h              |  1 +
>  2 files changed, 18 insertions(+)
>
> diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
> index 3abd82068fb..80017b3dddd 100644
> --- a/drivers/mtd/nand/raw/nand.c
> +++ b/drivers/mtd/nand/raw/nand.c
> @@ -74,6 +74,23 @@ int nand_register(int devnum, struct mtd_info *mtd)
>         return 0;
>  }
>
> +void nand_unregister(struct mtd_info *mtd)
> +{
> +       int devnum = nand_mtd_to_devnum(mtd);
> +
> +       if (devnum < 0)
> +               return;
> +
> +       if (nand_curr_device == devnum)
> +               nand_curr_device = -1;
> +
> +       total_nand_size -= mtd->size / 1024;
> +
> +       del_mtd_device(nand_info[devnum]);
> +
> +       nand_info[devnum] = NULL;
> +}
> +
>  #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
>  static void nand_init_chip(int i)
>  {
> diff --git a/include/nand.h b/include/nand.h
> index c1d7533aaac..fc584f5ef7a 100644
> --- a/include/nand.h
> +++ b/include/nand.h
> @@ -22,6 +22,7 @@ int nand_mtd_to_devnum(struct mtd_info *mtd);
>  #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
>  void board_nand_init(void);
>  int nand_register(int devnum, struct mtd_info *mtd);
> +void nand_unregister(struct mtd_info *mtd);
>  #else
>  struct nand_chip;
>
> --
> 2.37.1
>

Reviewed-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

Thanks and regards,

Dario
-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 12/15] nand: Allow reinitialization
  2023-10-29  3:48 ` [PATCH 12/15] nand: Allow reinitialization Sean Anderson
@ 2023-11-02 11:08   ` Dario Binacchi
  0 siblings, 0 replies; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 11:08 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All

On Sun, Oct 29, 2023 at 4:49 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> NAND devices are destroyed in between unit tests. Provide a function to
> reinitialize the subsystem at the beginning of each test.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  drivers/mtd/nand/raw/nand.c | 40 ++++++++++++++++++++++++++++++-------
>  include/nand.h              |  1 +
>  2 files changed, 34 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
> index 80017b3dddd..4c18861aa25 100644
> --- a/drivers/mtd/nand/raw/nand.c
> +++ b/drivers/mtd/nand/raw/nand.c
> @@ -115,6 +115,8 @@ static void nand_init_chip(int i)
>  #endif
>
>  #ifdef CONFIG_MTD_CONCAT
> +struct mtd_info *concat_mtd;
> +
>  static void create_mtd_concat(void)
>  {
>         struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
> @@ -129,28 +131,40 @@ static void create_mtd_concat(void)
>                 }
>         }
>         if (nand_devices_found > 1) {
> -               struct mtd_info *mtd;
>                 char c_mtd_name[16];
>
>                 /*
>                  * We detected multiple devices. Concatenate them together.
>                  */
>                 sprintf(c_mtd_name, "nand%d", nand_devices_found);
> -               mtd = mtd_concat_create(nand_info_list, nand_devices_found,
> -                                       c_mtd_name);
> +               concat_mtd = mtd_concat_create(nand_info_list,
> +                                              nand_devices_found, c_mtd_name);
>
> -               if (mtd == NULL)
> +               if (!concat_mtd)
>                         return;
>
> -               nand_register(nand_devices_found, mtd);
> +               nand_register(nand_devices_found, concat_mtd);
>         }
>
>         return;
>  }
> +
> +static void destroy_mtd_concat(void)
> +{
> +       if (!concat_mtd)
> +               return;
> +
> +       mtd_concat_destroy(concat_mtd);
> +       concat_mtd = NULL;
> +}
>  #else
>  static void create_mtd_concat(void)
>  {
>  }
> +
> +static void destroy_mtd_concat(void)
> +{
> +}
>  #endif
>
>  unsigned long nand_size(void)
> @@ -158,10 +172,10 @@ unsigned long nand_size(void)
>         return total_nand_size;
>  }
>
> +static int initialized;
> +
>  void nand_init(void)
>  {
> -       static int initialized;
> -
>         /*
>          * Avoid initializing NAND Flash multiple times,
>          * otherwise it will calculate a wrong total size.
> @@ -190,6 +204,18 @@ void nand_init(void)
>         create_mtd_concat();
>  }
>
> +void nand_reinit(void)
> +{
> +       int i;
> +
> +       destroy_mtd_concat();
> +       for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
> +               assert(!nand_info[i]);
> +
> +       initialized = 0;
> +       nand_init();
> +}
> +
>  unsigned int nand_page_size(void)
>  {
>         struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
> diff --git a/include/nand.h b/include/nand.h
> index fc584f5ef7a..220ffa202ef 100644
> --- a/include/nand.h
> +++ b/include/nand.h
> @@ -11,6 +11,7 @@
>  #include <config.h>
>
>  extern void nand_init(void);
> +void nand_reinit(void);
>  unsigned long nand_size(void);
>  unsigned int nand_page_size(void);
>
> --
> 2.37.1
>

Reviewed-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

Thanks and regards,
Dario

-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 14/15] nand: Add sandbox driver
  2023-10-29  3:48 ` [PATCH 14/15] nand: Add sandbox driver Sean Anderson
@ 2023-11-02 11:15   ` Dario Binacchi
  2023-11-02 13:54     ` Sean Anderson
  0 siblings, 1 reply; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 11:15 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All

On Sun, Oct 29, 2023 at 4:49 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> Add a sandbox NAND flash driver to facilitate testing. This driver supports
> any number of devices, each using a single chip-select. The OOB data is
> stored in-band, with the separation enforced through the API.
>
> For now, create two devices to test with. The first is a very small device
> with basic ECC. The second is an 8G device (chosen to be larger than 32
> bits). It uses ONFI, with the values copied from the datasheet. It also
> doesn't need too strong ECC, which speeds things up.
>
> Although the nand subsystem determines the parameters of a chip based on
> the ID, the driver itself requires devicetree properties for each
> parameter. We do not derive parameters from the ID because parsing the ID
> is non-trivial. We do not just use the parameters that the nand subsystem
> has calculated since that is something we should be testing.
>
> Despite using file I/O to access the backing data, we do not support using
> external files. In my experience, these are unnecessary for testing since
> tests can generally be written to write their expected data beforehand.
> Additionally, we would need to store the "programmed" information somewhere
> (complicating the format and the programming process) or try to detect
> whether block are erased at runtime (degrading probe speeds).
>
> Information about whether each page has been programmed is stored in an
> in-memory buffer. To simplify the implementation, we only support a single
> program per erase. While this is accurate for many larger flashes, some
> smaller flashes (512 byte) support multiple programs and/or subpage
> programs. Support for this could be added later as I believe some
> filesystems expect this.
>
> To test ECC, we support error-injection. Surprisingly, only ECC bytes in
> the OOB area are protected, even though all bytes are equally susceptible
> to error. Because of this, we take care to only corrupt ECC bytes.
> Similarly, because ECC covers "steps" and not the whole page, we must take
> care to corrupt data in the same way.
>
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  arch/sandbox/dts/test.dts        |  67 +++
>  configs/sandbox64_defconfig      |  10 +-
>  configs/sandbox_defconfig        |   9 +
>  configs/sandbox_noinst_defconfig |  10 +-
>  drivers/mtd/nand/raw/Kconfig     |  14 +
>  drivers/mtd/nand/raw/Makefile    |   1 +
>  drivers/mtd/nand/raw/sand_nand.c | 682 +++++++++++++++++++++++++++++++
>  test/dm/Makefile                 |   1 +
>  test/dm/nand.c                   | 106 +++++
>  9 files changed, 898 insertions(+), 2 deletions(-)
>  create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>  create mode 100644 test/dm/nand.c
>
> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
> index 5b54651a1da..5fba2943fad 100644
> --- a/arch/sandbox/dts/test.dts
> +++ b/arch/sandbox/dts/test.dts
> @@ -1900,6 +1900,73 @@
>                                 compatible = "sandbox,arm-ffa";
>                 };
>         };
> +
> +       nand-controller {
> +               #address-cells = <1>;
> +               #size-cells = <0>;
> +               compatible = "sandbox,nand";
> +
> +               nand@0 {
> +                       reg = <0>;
> +                       nand-ecc-mode = "soft";
> +                       sandbox,id = [00 e3];
> +                       sandbox,erasesize = <(8 * 1024)>;
> +                       sandbox,oobsize = <16>;
> +                       sandbox,pagesize = <512>;
> +                       sandbox,pages = <0x2000>;
> +                       sandbox,err-count = <1>;
> +                       sandbox,err-step-size = <512>;
> +                       sandbox,ecc-bytes = <6>;
> +               };
> +
> +               /* MT29F64G08AKABA */
> +               nand@1 {
> +                       reg = <1>;
> +                       nand-ecc-mode = "soft_bch";
> +                       sandbox,id = [2C 48 00 26 89 00 00 00];
> +                       sandbox,onfi = [
> +                               4f 4e 46 49 0e 00 5a 00
> +                               ff 01 00 00 00 00 03 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               4d 49 43 52 4f 4e 20 20
> +                               20 20 20 20 4d 54 32 39
> +                               46 36 34 47 30 38 41 4b
> +                               41 42 41 43 35 20 20 20
> +                               2c 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 10 00 00 e0 00 00 02
> +                               00 00 1c 00 80 00 00 00
> +                               00 10 00 00 02 23 01 50
> +                               00 01 05 01 00 00 04 00
> +                               04 01 1e 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               0e 1f 00 1f 00 f4 01 ac
> +                               0d 19 00 c8 00 00 00 00
> +                               00 00 00 00 00 00 0a 07
> +                               19 00 00 00 00 00 00 00
> +                               00 00 00 00 01 00 01 00
> +                               00 00 04 10 01 81 04 02
> +                               02 01 1e 90 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 00 00 00
> +                               00 00 00 00 00 03 20 7d
> +                       ];
> +                       sandbox,erasesize = <(512 * 1024)>;
> +                       sandbox,oobsize = <224>;
> +                       sandbox,pagesize = <4096>;
> +                       sandbox,pages = <0x200000>;
> +                       sandbox,err-count = <3>;
> +                       sandbox,err-step-size = <512>;
> +                       sandbox,ecc-bytes = <7>;
> +               };
> +       };
>  };
>
>  #include "sandbox_pmic.dtsi"
> diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
> index 1a01f51a0b7..d4e5eb3000b 100644
> --- a/configs/sandbox64_defconfig
> +++ b/configs/sandbox64_defconfig
> @@ -1,4 +1,5 @@
>  CONFIG_TEXT_BASE=0
> +CONFIG_SYS_MALLOC_LEN=0x6000000
>  CONFIG_NR_DRAM_BANKS=1
>  CONFIG_ENV_SIZE=0x2000
>  CONFIG_DEFAULT_DEVICE_TREE="sandbox64"
> @@ -50,7 +51,7 @@ CONFIG_CMD_GPT_RENAME=y
>  CONFIG_CMD_IDE=y
>  CONFIG_CMD_I2C=y
>  CONFIG_CMD_LOADM=y
> -CONFIG_CMD_MBR=y
> +CONFIG_CMD_MTD=y
>  CONFIG_CMD_OSD=y
>  CONFIG_CMD_PCI=y
>  CONFIG_CMD_READ=y
> @@ -168,6 +169,13 @@ CONFIG_PWRSEQ=y
>  CONFIG_I2C_EEPROM=y
>  CONFIG_MMC_SANDBOX=y
>  CONFIG_MTD=y
> +CONFIG_DM_MTD=y
> +CONFIG_MTD_RAW_NAND=y
> +CONFIG_SYS_MAX_NAND_DEVICE=8
> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
> +CONFIG_NAND_SANDBOX=y
> +CONFIG_SYS_NAND_ONFI_DETECTION=y
> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>  CONFIG_SPI_FLASH_SANDBOX=y
>  CONFIG_BOOTDEV_SPI_FLASH=y
>  CONFIG_SPI_FLASH_ATMEL=y
> diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
> index e3a2f9eb170..1c71f0542a3 100644
> --- a/configs/sandbox_defconfig
> +++ b/configs/sandbox_defconfig
> @@ -1,4 +1,5 @@
>  CONFIG_TEXT_BASE=0
> +CONFIG_SYS_MALLOC_LEN=0x6000000
>  CONFIG_NR_DRAM_BANKS=1
>  CONFIG_ENV_SIZE=0x2000
>  CONFIG_DEFAULT_DEVICE_TREE="sandbox"
> @@ -73,6 +74,7 @@ CONFIG_CMD_IDE=y
>  CONFIG_CMD_I2C=y
>  CONFIG_CMD_LOADM=y
>  CONFIG_CMD_LSBLK=y
> +CONFIG_CMD_MTD=y
>  CONFIG_CMD_MUX=y
>  CONFIG_CMD_OSD=y
>  CONFIG_CMD_PCI=y
> @@ -215,6 +217,13 @@ CONFIG_MMC_PCI=y
>  CONFIG_MMC_SANDBOX=y
>  CONFIG_MMC_SDHCI=y
>  CONFIG_MTD=y
> +CONFIG_DM_MTD=y
> +CONFIG_MTD_RAW_NAND=y
> +CONFIG_SYS_MAX_NAND_DEVICE=8
> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
> +CONFIG_NAND_SANDBOX=y
> +CONFIG_SYS_NAND_ONFI_DETECTION=y
> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>  CONFIG_SPI_FLASH_SANDBOX=y
>  CONFIG_BOOTDEV_SPI_FLASH=y
>  CONFIG_SPI_FLASH_ATMEL=y
> diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
> index db05e630832..09ebafeccca 100644
> --- a/configs/sandbox_noinst_defconfig
> +++ b/configs/sandbox_noinst_defconfig
> @@ -80,7 +80,7 @@ CONFIG_CMD_GPIO=y
>  CONFIG_CMD_GPT=y
>  CONFIG_CMD_IDE=y
>  CONFIG_CMD_I2C=y
> -CONFIG_CMD_MBR=y
> +CONFIG_CMD_MTD=y
>  CONFIG_CMD_OSD=y
>  CONFIG_CMD_PCI=y
>  CONFIG_CMD_REMOTEPROC=y
> @@ -181,6 +181,14 @@ CONFIG_PWRSEQ=y
>  CONFIG_SPL_PWRSEQ=y
>  CONFIG_FS_LOADER=y
>  CONFIG_MMC_SANDBOX=y
> +CONFIG_MTD=y
> +CONFIG_DM_MTD=y
> +CONFIG_MTD_RAW_NAND=y
> +CONFIG_SYS_MAX_NAND_DEVICE=8
> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
> +CONFIG_NAND_SANDBOX=y
> +CONFIG_SYS_NAND_ONFI_DETECTION=y
> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>  CONFIG_SPI_FLASH_SANDBOX=y
>  CONFIG_SPI_FLASH_ATMEL=y
>  CONFIG_SPI_FLASH_EON=y
> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
> index ee484dc0f51..6b34aa6782c 100644
> --- a/drivers/mtd/nand/raw/Kconfig
> +++ b/drivers/mtd/nand/raw/Kconfig
> @@ -447,6 +447,20 @@ config NAND_PXA3XX
>           This enables the driver for the NAND flash device found on
>           PXA3xx processors (NFCv1) and also on Armada 370/XP (NFCv2).
>
> +config NAND_SANDBOX
> +       bool "Support for NAND in sandbox"
> +       depends on SANDBOX
> +       select SYS_NAND_SELF_INIT
> +       select SYS_NAND_SOFT_ECC
> +       select BCH
> +       select NAND_ECC_BCH
> +       imply CMD_NAND
> +       help
> +         Enable a dummy NAND driver for sandbox. It simulates any number of
> +         arbitrary NAND chips with a RAM buffer. It will also inject errors to
> +         test ECC. At the moment, only 8-bit busses and single-chip devices are
> +         supported.
> +
>  config NAND_SUNXI
>         bool "Support for NAND on Allwinner SoCs"
>         default ARCH_SUNXI
> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
> index add2b4cf655..ddbba899e58 100644
> --- a/drivers/mtd/nand/raw/Makefile
> +++ b/drivers/mtd/nand/raw/Makefile
> @@ -70,6 +70,7 @@ obj-$(CONFIG_NAND_PXA3XX) += pxa3xx_nand.o
>  obj-$(CONFIG_TEGRA_NAND) += tegra_nand.o
>  obj-$(CONFIG_NAND_OMAP_GPMC) += omap_gpmc.o
>  obj-$(CONFIG_NAND_OMAP_ELM) += omap_elm.o
> +obj-$(CONFIG_NAND_SANDBOX) += sand_nand.o
>  obj-$(CONFIG_NAND_SUNXI) += sunxi_nand.o
>  obj-$(CONFIG_NAND_MXIC) += mxic_nand.o
>  obj-$(CONFIG_NAND_ZYNQ) += zynq_nand.o
> diff --git a/drivers/mtd/nand/raw/sand_nand.c b/drivers/mtd/nand/raw/sand_nand.c
> new file mode 100644
> index 00000000000..f1855877630
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/sand_nand.c
> @@ -0,0 +1,682 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) Sean Anderson <seanga2@gmail.com>
> + */
> +
> +#define LOG_CATEGORY UCLASS_MTD
> +#include <errno.h>
> +#include <hexdump.h>
> +#include <log.h>
> +#include <nand.h>
> +#include <os.h>
> +#include <rand.h>
> +#include <dm/device_compat.h>
> +#include <dm/read.h>
> +#include <dm/uclass.h>
> +#include <asm/bitops.h>
> +#include <linux/bitmap.h>
> +#include <linux/mtd/rawnand.h>
> +#include <linux/sizes.h>
> +
> +enum sand_nand_state {
> +       STATE_READY,
> +       STATE_IDLE,
> +       STATE_READ,
> +       STATE_READ_ID,
> +       STATE_READ_ONFI,
> +       STATE_PARAM_ONFI,
> +       STATE_STATUS,
> +       STATE_PROG,
> +       STATE_ERASE,
> +};
> +
> +static const char *const state_name[] = {
> +       [STATE_READY] = "READY",
> +       [STATE_IDLE] = "IDLE",
> +       [STATE_READ] = "READ",
> +       [STATE_READ_ID] = "READ_ID",
> +       [STATE_READ_ONFI] = "READ_ONFI",
> +       [STATE_PARAM_ONFI] = "PARAM_ONFI",
> +       [STATE_STATUS] = "STATUS",
> +       [STATE_PROG] = "PROG",
> +       [STATE_ERASE] = "ERASE",
> +};
> +
> +/**
> + * struct sand_nand_chip - Per-device private data
> + * @nand: The nand chip
> + * @node: The next device in this controller
> + * @programmed: Bitmap of whether sectors are programmed
> + * @id: ID to report for NAND_CMD_READID
> + * @id_len: Length of @id
> + * @onfi: Three copies of ONFI parameter page
> + * @status: Status to report for NAND_CMD_STATUS
> + * @chunksize: Size of one "chunk" (page + oob) in bytes
> + * @pageize: Size of one page in bytes
> + * @pages: Total number of pages
> + * @pages_per_erase: Number of pages per eraseblock
> + * @err_count: Number of errors to inject per @err_step_bits of data
> + * @err_step_bits: Number of data bits per error "step"
> + * @err_steps: Number of err steps in a page
> + * @ecc_bits: Number of ECC bits uper @err_step_bits
> + * @cs: Chip select for this device
> + * @state: Current state of the device
> + * @column: Column of the most-recent command
> + * @page_addr: Page address of the most-recent command
> + * @fd: File descriptor for the backing data
> + * @fd_page_addr: Page address that @fd is seek'd to
> + * @selected: Whether this device is selected
> + * @tmp: "Cache" buffer used to store transferred data before committing it
> + * @tmp_dirty: Whether @tmp is dirty (modified) or clean (all ones)
> + */
> +struct sand_nand_chip {
> +       struct nand_chip nand;
> +       struct list_head node;
> +       long *programmed;
> +       const u8 *id;
> +       u32 chunksize, pagesize, pages, pages_per_erase;
> +       u32 err_count, err_step_bits, err_steps, ecc_bits;
> +       unsigned int cs;
> +       enum sand_nand_state state;
> +       int column, page_addr, fd, fd_page_addr;
> +       bool selected, tmp_dirty;
> +       u8 status;

I see a character "^L".
Please remove it.

Thanks and regards,
Dario

> +       u8 id_len;
> +       u8 tmp[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
> +       u8 onfi[sizeof(struct nand_onfi_params) * 3];
> +};
> +
> +#define SAND_DEBUG(chip, fmt, ...) \
> +       dev_dbg((chip)->nand.mtd.dev, "%u (%s): " fmt, (chip)->cs, \
> +               state_name[(chip)->state], ##__VA_ARGS__)
> +
> +static inline void to_state(struct sand_nand_chip *chip,
> +                           enum sand_nand_state new_state)
> +{
> +       if (new_state != chip->state)
> +               SAND_DEBUG(chip, "to state %s\n", state_name[new_state]);
> +       chip->state = new_state;
> +}
> +
> +static inline struct sand_nand_chip *to_sand_nand(struct nand_chip *nand)
> +{
> +       return container_of(nand, struct sand_nand_chip, nand);
> +}
> +
> +struct sand_nand_priv {
> +       struct list_head chips;
> +};
> +
> +static int sand_nand_dev_ready(struct mtd_info *mtd)
> +{
> +       return 1;
> +}
> +
> +static int sand_nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
> +{
> +       u8 status;
> +
> +       return nand_status_op(chip, &status) ?: status;
> +}
> +
> +static int sand_nand_seek(struct sand_nand_chip *chip)
> +{
> +       if (chip->fd_page_addr == chip->page_addr)
> +               return 0;
> +
> +       if (os_lseek(chip->fd, (off_t)chip->page_addr * chip->chunksize,
> +                    OS_SEEK_SET) < 0) {
> +               SAND_DEBUG(chip, "could not seek: %d\n", errno);
> +               return -EIO;
> +       }
> +
> +       chip->fd_page_addr = chip->page_addr;
> +       return 0;
> +}
> +
> +static void sand_nand_inject_error(struct sand_nand_chip *chip,
> +                                  unsigned int step, unsigned int pos)
> +{
> +       int byte, index;
> +
> +       if (pos < chip->err_step_bits) {
> +               __change_bit(step * chip->err_step_bits + pos, chip->tmp);
> +               return;
> +       }
> +
> +       /*
> +        * Only ECC bytes are covered in the OOB area, so
> +        * pretend that those are the only bytes which can have
> +        * errors.
> +        */
> +       byte = (pos - chip->err_step_bits + step * chip->ecc_bits) / 8;
> +       index = chip->nand.ecc.layout->eccpos[byte];
> +       /* Avoid endianness issues by working with bytes */
> +       chip->tmp[chip->pagesize + index] ^= BIT(pos & 0x7);
> +}
> +
> +static int sand_nand_read(struct sand_nand_chip *chip)
> +{
> +       unsigned int i, stop = 0;
> +
> +       if (chip->column == chip->pagesize)
> +               stop = chip->err_step_bits;
> +
> +       if (test_bit(chip->page_addr, chip->programmed)) {
> +               if (sand_nand_seek(chip))
> +                       return -EIO;
> +
> +               if (os_read(chip->fd, chip->tmp, chip->chunksize) !=
> +                   chip->chunksize) {
> +                       SAND_DEBUG(chip, "could not read: %d\n", errno);
> +                       return -EIO;
> +               }
> +               chip->fd_page_addr++;
> +       } else if (chip->tmp_dirty) {
> +               memset(chip->tmp + chip->column, 0xff,
> +                      chip->chunksize - chip->column);
> +       }
> +
> +       /*
> +        * Inject some errors; this is Method A from "An Efficient Algorithm for
> +        * Sequential Random Sampling" (Vitter 87). This is still slow when
> +        * generating a lot (dozens) of ECC errors.
> +        *
> +        * To avoid generating too many errors in any one ECC step, we separate
> +        * our error generation by ECC step.
> +        */
> +       chip->tmp_dirty = true;
> +       for (i = 0; i < chip->err_steps; i++) {
> +               u32 bit_errors = chip->err_count;
> +               unsigned int j = chip->err_step_bits + chip->ecc_bits;
> +
> +               while (bit_errors) {
> +                       unsigned int u = rand();
> +                       float quot = 1ULL << 32;
> +
> +                       do {
> +                               quot *= j - bit_errors;
> +                               quot /= j;
> +                               j--;
> +
> +                               if (j < stop)
> +                                       goto next;
> +                       } while (u < quot);
> +
> +                       sand_nand_inject_error(chip, i, j);
> +                       bit_errors--;
> +               }
> +next:
> +               ;
> +       }
> +
> +       return 0;
> +}
> +
> +static void sand_nand_command(struct mtd_info *mtd, unsigned int command,
> +                             int column, int page_addr)
> +{
> +       struct nand_chip *nand = mtd_to_nand(mtd);
> +       struct sand_nand_chip *chip = to_sand_nand(nand);
> +       enum sand_nand_state new_state = chip->state;
> +
> +       SAND_DEBUG(chip, "command=%02x column=%d page_addr=%d\n", command,
> +                  column, page_addr);
> +
> +       if (!chip->selected)
> +               return;
> +
> +       switch (chip->state) {
> +       case STATE_READY:
> +               if (command == NAND_CMD_RESET)
> +                       goto reset;
> +               break;
> +       case STATE_PROG:
> +               new_state = STATE_IDLE;
> +               if (command != NAND_CMD_PAGEPROG ||
> +                   test_and_set_bit(chip->page_addr, chip->programmed)) {
> +                       chip->status |= NAND_STATUS_FAIL;
> +                       break;
> +               }
> +
> +               if (sand_nand_seek(chip)) {
> +                       chip->status |= NAND_STATUS_FAIL;
> +                       break;
> +               }
> +
> +               if (os_write(chip->fd, chip->tmp, chip->chunksize) !=
> +                   chip->chunksize) {
> +                       SAND_DEBUG(chip, "could not write: %d\n", errno);
> +                       chip->status |= NAND_STATUS_FAIL;
> +                       break;
> +               }
> +
> +               chip->fd_page_addr++;
> +               break;
> +       case STATE_ERASE:
> +               new_state = STATE_IDLE;
> +               if (command != NAND_CMD_ERASE2) {
> +                       chip->status |= NAND_STATUS_FAIL;
> +                       break;
> +               }
> +
> +               if (chip->page_addr < 0 ||
> +                   chip->page_addr >= chip->pages ||
> +                   chip->page_addr % chip->pages_per_erase)
> +                       chip->status |= NAND_STATUS_FAIL;
> +               else
> +                       bitmap_clear(chip->programmed, chip->page_addr,
> +                                    chip->pages_per_erase);
> +               break;
> +       default:
> +               chip->column = column;
> +               chip->page_addr = page_addr;
> +               switch (command) {
> +               case NAND_CMD_READOOB:
> +                       if (column >= 0)
> +                               chip->column += chip->pagesize;
> +                       fallthrough;
> +               case NAND_CMD_READ0:
> +                       new_state = STATE_IDLE;
> +                       if (page_addr < 0 || page_addr >= chip->pages)
> +                               break;
> +
> +                       if (chip->column < 0 || chip->column >= chip->chunksize)
> +                               break;
> +
> +                       if (sand_nand_read(chip))
> +                               break;
> +
> +                       chip->page_addr = page_addr;
> +                       new_state = STATE_READ;
> +                       break;
> +               case NAND_CMD_ERASE1:
> +                       new_state = STATE_ERASE;
> +                       chip->status = ~NAND_STATUS_FAIL;
> +                       break;
> +               case NAND_CMD_STATUS:
> +                       new_state = STATE_STATUS;
> +                       chip->column = 0;
> +                       break;
> +               case NAND_CMD_SEQIN:
> +                       new_state = STATE_PROG;
> +                       chip->status = ~NAND_STATUS_FAIL;
> +                       if (page_addr < 0 || page_addr >= chip->pages ||
> +                           chip->column < 0 ||
> +                           chip->column >= chip->chunksize) {
> +                               chip->status |= NAND_STATUS_FAIL;
> +                       } else if (chip->tmp_dirty) {
> +                               memset(chip->tmp, 0xff, chip->chunksize);
> +                               chip->tmp_dirty = false;
> +                       }
> +                       break;
> +               case NAND_CMD_READID:
> +                       if (chip->onfi[0] && column == 0x20)
> +                               new_state = STATE_READ_ONFI;
> +                       else
> +                               new_state = STATE_READ_ID;
> +                       chip->column = 0;
> +                       break;
> +               case NAND_CMD_PARAM:
> +                       if (chip->onfi[0] && !column)
> +                               new_state = STATE_PARAM_ONFI;
> +                       else
> +                               new_state = STATE_IDLE;
> +                       break;
> +               case NAND_CMD_RESET:
> +reset:
> +                       new_state = STATE_IDLE;
> +                       chip->column = -1;
> +                       chip->page_addr = -1;
> +                       chip->status = ~NAND_STATUS_FAIL;
> +                       break;
> +               default:
> +                       new_state = STATE_IDLE;
> +                       SAND_DEBUG(chip, "Unsupported command %02x\n", command);
> +               }
> +       }
> +
> +       to_state(chip, new_state);
> +}
> +
> +static void sand_nand_select_chip(struct mtd_info *mtd, int n)
> +{
> +       struct nand_chip *nand = mtd_to_nand(mtd);
> +       struct sand_nand_chip *chip = to_sand_nand(nand);
> +
> +       chip->selected = !n;
> +}
> +
> +static void sand_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
> +{
> +       struct nand_chip *nand = mtd_to_nand(mtd);
> +       struct sand_nand_chip *chip = to_sand_nand(nand);
> +       unsigned int to_copy;
> +       int src_len = 0;
> +       const u8 *src = NULL;
> +
> +       if (!chip->selected)
> +               goto copy;
> +
> +       switch (chip->state) {
> +       case STATE_READ:
> +               src = chip->tmp;
> +               src_len = chip->chunksize;
> +               break;
> +       case STATE_READ_ID:
> +               src = chip->id;
> +               src_len = chip->id_len;
> +               break;
> +       case STATE_READ_ONFI:
> +               src = "ONFI";
> +               src_len = 4;
> +               break;
> +       case STATE_PARAM_ONFI:
> +               src = chip->onfi;
> +               src_len = sizeof(chip->onfi);
> +               break;
> +       case STATE_STATUS:
> +               src = &chip->status;
> +               src_len = 1;
> +               break;
> +       default:
> +               break;
> +       }
> +
> +copy:
> +       if (chip->column >= 0)
> +               to_copy = max(min(len, src_len - chip->column), 0);
> +       else
> +               to_copy = 0;
> +       memcpy(buf, src + chip->column, to_copy);
> +       memset(buf + to_copy, 0xff, len - to_copy);
> +       chip->column += to_copy;
> +
> +       if (len == 1) {
> +               SAND_DEBUG(chip, "read [ %02x ]\n", buf[0]);
> +       } else if (src_len) {
> +               SAND_DEBUG(chip, "read %d bytes\n", len);
> +#ifdef VERBOSE_DEBUG
> +               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
> +#endif
> +       }
> +
> +       if (src_len && chip->column == src_len)
> +               to_state(chip, STATE_IDLE);
> +}
> +
> +static u8 sand_nand_read_byte(struct mtd_info *mtd)
> +{
> +       u8 ret;
> +
> +       sand_nand_read_buf(mtd, &ret, 1);
> +       return ret;
> +}
> +
> +static u16 sand_nand_read_word(struct mtd_info *mtd)
> +{
> +       struct nand_chip *nand = mtd_to_nand(mtd);
> +       struct sand_nand_chip *chip = to_sand_nand(nand);
> +
> +       SAND_DEBUG(chip, "16-bit access unsupported\n");
> +       return sand_nand_read_byte(mtd) | 0xff00;
> +}
> +
> +static void sand_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
> +{
> +       struct nand_chip *nand = mtd_to_nand(mtd);
> +       struct sand_nand_chip *chip = to_sand_nand(nand);
> +
> +       SAND_DEBUG(chip, "write %d bytes\n", len);
> +#ifdef VERBOSE_DEBUG
> +       print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
> +#endif
> +
> +       if (chip->state != STATE_PROG || chip->status & NAND_STATUS_FAIL)
> +               return;
> +
> +       chip->tmp_dirty = true;
> +       len = min((unsigned int)len, chip->chunksize - chip->column);
> +       memcpy(chip->tmp + chip->column, buf, len);
> +       chip->column += len;
> +}
> +
> +static struct nand_chip *nand_chip;
> +
> +int sand_nand_remove(struct udevice *dev)
> +{
> +       struct sand_nand_priv *priv = dev_get_priv(dev);
> +       struct sand_nand_chip *chip;
> +
> +       list_for_each_entry(chip, &priv->chips, node) {
> +               struct nand_chip *nand = &chip->nand;
> +
> +               if (nand_chip == nand)
> +                       nand_chip = NULL;
> +
> +               nand_unregister(nand_to_mtd(nand));
> +               free(chip->programmed);
> +               os_close(chip->fd);
> +               free(chip);
> +       }
> +
> +       return 0;
> +}
> +
> +static int sand_nand_probe(struct udevice *dev)
> +{
> +       struct sand_nand_priv *priv = dev_get_priv(dev);
> +       struct sand_nand_chip *chip;
> +       int ret, devnum = 0;
> +       ofnode np;
> +
> +       INIT_LIST_HEAD(&priv->chips);
> +
> +       dev_for_each_subnode(np, dev) {
> +               struct nand_chip *nand;
> +               struct mtd_info *mtd;
> +               u32 erasesize, oobsize, pagesize, pages;
> +               u32 err_count, err_step_size, ecc_bytes;
> +               off_t expected_size;
> +               char filename[30];
> +               fdt_addr_t cs;
> +               const u8 *id, *onfi;
> +               int id_len, onfi_len;
> +
> +               cs = ofnode_get_addr_size_index_notrans(np, 0, NULL);
> +               if (cs == FDT_ADDR_T_NONE) {
> +                       dev_dbg(dev, "Invalid cs for chip %s\n",
> +                               ofnode_get_name(np));
> +                       ret = -ENOENT;
> +                       goto err;
> +               }
> +
> +               id = ofnode_read_prop(np, "sandbox,id", &id_len);
> +               if (!id) {
> +                       dev_dbg(dev, "No sandbox,id property for chip %s\n",
> +                               ofnode_get_name(np));
> +                       ret = -EINVAL;
> +                       goto err;
> +               }
> +
> +               onfi = ofnode_read_prop(np, "sandbox,onfi", &onfi_len);
> +               if (onfi && onfi_len != sizeof(struct nand_onfi_params)) {
> +                       dev_dbg(dev, "Invalid length %d for onfi params\n",
> +                               onfi_len);
> +                       ret = -EINVAL;
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,erasesize", &erasesize);
> +               if (ret) {
> +                       dev_dbg(dev, "No sandbox,erasesize property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,oobsize", &oobsize);
> +               if (ret) {
> +                       dev_dbg(dev, "No sandbox,oobsize property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,pagesize", &pagesize);
> +               if (ret) {
> +                       dev_dbg(dev, "No sandbox,pagesize property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,pages", &pages);
> +               if (ret) {
> +                       dev_dbg(dev, "No sandbox,pages property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,err-count", &err_count);
> +               if (ret) {
> +                       dev_dbg(dev,
> +                               "No sandbox,err-count property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,err-step-size",
> +                                     &err_step_size);
> +               if (ret) {
> +                       dev_dbg(dev,
> +                               "No sandbox,err-step-size property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               ret = ofnode_read_u32(np, "sandbox,ecc-bytes", &ecc_bytes);
> +               if (ret) {
> +                       dev_dbg(dev,
> +                               "No sandbox,ecc-bytes property for chip %s",
> +                               ofnode_get_name(np));
> +                       goto err;
> +               }
> +
> +               chip = calloc(sizeof(*chip), 1);
> +               if (!chip) {
> +                       ret = -ENOMEM;
> +                       goto err;
> +               }
> +
> +               chip->cs = cs;
> +               chip->id = id;
> +               chip->id_len = id_len;
> +               chip->chunksize = pagesize + oobsize;
> +               chip->pagesize = pagesize;
> +               chip->pages = pages;
> +               chip->pages_per_erase = erasesize / pagesize;
> +               memset(chip->tmp, 0xff, chip->chunksize);
> +
> +               chip->err_count = err_count;
> +               chip->err_step_bits = err_step_size * 8;
> +               chip->err_steps = pagesize / err_step_size;
> +               chip->ecc_bits = ecc_bytes * 8;
> +
> +               expected_size = (off_t)pages * chip->chunksize;
> +               snprintf(filename, sizeof(filename),
> +                        "/tmp/u-boot.nand%d.XXXXXX", devnum);
> +               chip->fd = os_mktemp(filename, expected_size);
> +               if (chip->fd < 0) {
> +                       dev_dbg(dev, "Could not create temp file %s\n",
> +                               filename);
> +                       ret = chip->fd;
> +                       goto err_chip;
> +               }
> +
> +               chip->programmed = calloc(sizeof(long),
> +                                         BITS_TO_LONGS(pages));
> +               if (!chip->programmed) {
> +                       ret = -ENOMEM;
> +                       goto err_fd;
> +               }
> +
> +               if (onfi) {
> +                       memcpy(chip->onfi, onfi, onfi_len);
> +                       memcpy(chip->onfi + onfi_len, onfi, onfi_len);
> +                       memcpy(chip->onfi + 2 * onfi_len, onfi, onfi_len);
> +               }
> +
> +               nand = &chip->nand;
> +               nand->flash_node = np;
> +               nand->dev_ready = sand_nand_dev_ready;
> +               nand->cmdfunc = sand_nand_command;
> +               nand->waitfunc = sand_nand_wait;
> +               nand->select_chip = sand_nand_select_chip;
> +               nand->read_byte = sand_nand_read_byte;
> +               nand->read_word = sand_nand_read_word;
> +               nand->read_buf = sand_nand_read_buf;
> +               nand->write_buf = sand_nand_write_buf;
> +               nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
> +
> +               mtd = nand_to_mtd(nand);
> +               mtd->dev = dev;
> +
> +               ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
> +               if (ret) {
> +                       dev_dbg(dev, "Could not scan chip %s: %d\n",
> +                               ofnode_get_name(np), ret);
> +                       goto err_prog;
> +               }
> +
> +               ret = nand_register(devnum, mtd);
> +               if (ret) {
> +                       dev_dbg(dev, "Could not register nand %d: %d\n", devnum,
> +                               ret);
> +                       goto err_prog;
> +               }
> +
> +               if (!nand_chip)
> +                       nand_chip = nand;
> +
> +               list_add_tail(&chip->node, &priv->chips);
> +               devnum++;
> +               continue;
> +
> +err_prog:
> +               free(chip->programmed);
> +err_fd:
> +               os_close(chip->fd);
> +err_chip:
> +               free(chip);
> +               goto err;
> +       }
> +
> +       return 0;
> +
> +err:
> +       sand_nand_remove(dev);
> +       return ret;
> +}
> +
> +static const struct udevice_id sand_nand_ids[] = {
> +       { .compatible = "sandbox,nand" },
> +       { }
> +};
> +
> +U_BOOT_DRIVER(sand_nand) = {
> +       .name           = "sand-nand",
> +       .id             = UCLASS_MTD,
> +       .of_match       = sand_nand_ids,
> +       .probe          = sand_nand_probe,
> +       .remove         = sand_nand_remove,
> +       .priv_auto      = sizeof(struct sand_nand_priv),
> +};
> +
> +void board_nand_init(void)
> +{
> +       struct udevice *dev;
> +       int err;
> +
> +       err = uclass_get_device_by_driver(UCLASS_MTD, DM_DRIVER_REF(sand_nand),
> +                                         &dev);
> +       if (err && err != -ENODEV)
> +               log_info("Failed to get sandbox NAND: %d\n", err);
> +}
> diff --git a/test/dm/Makefile b/test/dm/Makefile
> index cb82d839f8a..a3ce7b3889f 100644
> --- a/test/dm/Makefile
> +++ b/test/dm/Makefile
> @@ -73,6 +73,7 @@ obj-$(CONFIG_CMD_MUX) += mux-cmd.o
>  obj-$(CONFIG_MULTIPLEXER) += mux-emul.o
>  obj-$(CONFIG_MUX_MMIO) += mux-mmio.o
>  obj-y += fdtdec.o
> +obj-$(CONFIG_MTD_RAW_NAND) += nand.o
>  obj-$(CONFIG_UT_DM) += nop.o
>  obj-y += ofnode.o
>  obj-y += ofread.o
> diff --git a/test/dm/nand.c b/test/dm/nand.c
> new file mode 100644
> index 00000000000..a1304965072
> --- /dev/null
> +++ b/test/dm/nand.c
> @@ -0,0 +1,106 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
> + */
> +
> +#include <nand.h>
> +#include <part.h>
> +#include <rand.h>
> +#include <dm/test.h>
> +#include <test/test.h>
> +#include <test/ut.h>
> +#include <linux/mtd/mtd.h>
> +#include <linux/mtd/rawnand.h>
> +
> +static int dm_test_nand(struct unit_test_state *uts, int dev, bool end)
> +{
> +       nand_erase_options_t opts = { };
> +       struct mtd_info *mtd;
> +       size_t length;
> +       loff_t size;
> +       char *buf;
> +       int *gold;
> +       u8 oob[NAND_MAX_OOBSIZE];
> +       int i;
> +       loff_t off = 0;
> +       mtd_oob_ops_t ops = { };
> +
> +       /* Seed RNG for bit errors */
> +       srand((off >> 32) ^ off ^ ~dev);
> +
> +       mtd = get_nand_dev_by_index(dev);
> +       ut_assertnonnull(mtd);
> +       size = mtd->erasesize * 4;
> +       length = size;
> +
> +       buf = malloc(size);
> +       ut_assertnonnull(buf);
> +       gold = malloc(size);
> +       ut_assertnonnull(gold);
> +
> +       /* Mark a block as bad */
> +       ut_assertok(mtd_block_markbad(mtd, off + mtd->erasesize));
> +       /* Save the OOB for later */
> +       ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
> +
> +       /* Erase some stuff */
> +       if (end)
> +               off = mtd->size - size - mtd->erasesize;
> +       opts.offset = off;
> +       opts.length = size;
> +       opts.spread = 1;
> +       opts.lim = U32_MAX;
> +       ut_assertok(nand_erase_opts(mtd, &opts));
> +
> +       /* Make sure everything is erased */
> +       memset(gold, 0xff, size);
> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
> +       ut_asserteq(size, length);
> +       ut_asserteq_mem(gold, buf, size);
> +
> +       /* ...but our bad block marker is still there */
> +       ops.oobbuf = oob;
> +       ops.ooblen = mtd->oobsize;
> +       ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
> +       ut_asserteq(0, oob[mtd_to_nand(mtd)->badblockpos]);
> +
> +       /* Generate some data and write it */
> +       for (i = 0; i < size / sizeof(int); i++)
> +               gold[i] = 0;//rand();
> +       ut_assertok(nand_write_skip_bad(mtd, off, &length, NULL, U64_MAX,
> +                                       (void *)gold, 0));
> +       ut_asserteq(size, length);
> +
> +       /* Verify */
> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
> +       ut_asserteq(size, length);
> +       ut_asserteq_mem(gold, buf, size);
> +
> +       /* Erase some blocks */
> +       memset(((char *)gold) + mtd->erasesize, 0xff, mtd->erasesize * 2);
> +       opts.offset = off + mtd->erasesize;
> +       opts.length = mtd->erasesize * 2,
> +       ut_assertok(nand_erase_opts(mtd, &opts));
> +
> +       /* Verify */
> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
> +       ut_asserteq(size, length);
> +       ut_asserteq_mem(gold, buf, size);
> +
> +       return 0;
> +}
> +
> +#define DM_NAND_TEST(dev) \
> +static int dm_test_nand##dev##_start(struct unit_test_state *uts) \
> +{ \
> +       return dm_test_nand(uts, dev, false); \
> +} \
> +DM_TEST(dm_test_nand##dev##_start, UT_TESTF_SCAN_FDT); \
> +static int dm_test_nand##dev##_end(struct unit_test_state *uts) \
> +{ \
> +       return dm_test_nand(uts, dev, true); \
> +} \
> +DM_TEST(dm_test_nand##dev##_end, UT_TESTF_SCAN_FDT)
> +
> +DM_NAND_TEST(0);
> +DM_NAND_TEST(1);
> --
> 2.37.1
>


-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 14/15] nand: Add sandbox driver
  2023-11-02 11:15   ` Dario Binacchi
@ 2023-11-02 13:54     ` Sean Anderson
  0 siblings, 0 replies; 34+ messages in thread
From: Sean Anderson @ 2023-11-02 13:54 UTC (permalink / raw)
  To: Dario Binacchi; +Cc: u-boot, Michael Trimarchi, Tom Rini

On 11/2/23 07:15, Dario Binacchi wrote:
> Sean, All
> 
> On Sun, Oct 29, 2023 at 4:49 AM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> Add a sandbox NAND flash driver to facilitate testing. This driver supports
>> any number of devices, each using a single chip-select. The OOB data is
>> stored in-band, with the separation enforced through the API.
>>
>> For now, create two devices to test with. The first is a very small device
>> with basic ECC. The second is an 8G device (chosen to be larger than 32
>> bits). It uses ONFI, with the values copied from the datasheet. It also
>> doesn't need too strong ECC, which speeds things up.
>>
>> Although the nand subsystem determines the parameters of a chip based on
>> the ID, the driver itself requires devicetree properties for each
>> parameter. We do not derive parameters from the ID because parsing the ID
>> is non-trivial. We do not just use the parameters that the nand subsystem
>> has calculated since that is something we should be testing.
>>
>> Despite using file I/O to access the backing data, we do not support using
>> external files. In my experience, these are unnecessary for testing since
>> tests can generally be written to write their expected data beforehand.
>> Additionally, we would need to store the "programmed" information somewhere
>> (complicating the format and the programming process) or try to detect
>> whether block are erased at runtime (degrading probe speeds).
>>
>> Information about whether each page has been programmed is stored in an
>> in-memory buffer. To simplify the implementation, we only support a single
>> program per erase. While this is accurate for many larger flashes, some
>> smaller flashes (512 byte) support multiple programs and/or subpage
>> programs. Support for this could be added later as I believe some
>> filesystems expect this.
>>
>> To test ECC, we support error-injection. Surprisingly, only ECC bytes in
>> the OOB area are protected, even though all bytes are equally susceptible
>> to error. Because of this, we take care to only corrupt ECC bytes.
>> Similarly, because ECC covers "steps" and not the whole page, we must take
>> care to corrupt data in the same way.
>>
>> Signed-off-by: Sean Anderson <seanga2@gmail.com>
>> ---
>>
>>   arch/sandbox/dts/test.dts        |  67 +++
>>   configs/sandbox64_defconfig      |  10 +-
>>   configs/sandbox_defconfig        |   9 +
>>   configs/sandbox_noinst_defconfig |  10 +-
>>   drivers/mtd/nand/raw/Kconfig     |  14 +
>>   drivers/mtd/nand/raw/Makefile    |   1 +
>>   drivers/mtd/nand/raw/sand_nand.c | 682 +++++++++++++++++++++++++++++++
>>   test/dm/Makefile                 |   1 +
>>   test/dm/nand.c                   | 106 +++++
>>   9 files changed, 898 insertions(+), 2 deletions(-)
>>   create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>>   create mode 100644 test/dm/nand.c
>>
>> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
>> index 5b54651a1da..5fba2943fad 100644
>> --- a/arch/sandbox/dts/test.dts
>> +++ b/arch/sandbox/dts/test.dts
>> @@ -1900,6 +1900,73 @@
>>                                  compatible = "sandbox,arm-ffa";
>>                  };
>>          };
>> +
>> +       nand-controller {
>> +               #address-cells = <1>;
>> +               #size-cells = <0>;
>> +               compatible = "sandbox,nand";
>> +
>> +               nand@0 {
>> +                       reg = <0>;
>> +                       nand-ecc-mode = "soft";
>> +                       sandbox,id = [00 e3];
>> +                       sandbox,erasesize = <(8 * 1024)>;
>> +                       sandbox,oobsize = <16>;
>> +                       sandbox,pagesize = <512>;
>> +                       sandbox,pages = <0x2000>;
>> +                       sandbox,err-count = <1>;
>> +                       sandbox,err-step-size = <512>;
>> +                       sandbox,ecc-bytes = <6>;
>> +               };
>> +
>> +               /* MT29F64G08AKABA */
>> +               nand@1 {
>> +                       reg = <1>;
>> +                       nand-ecc-mode = "soft_bch";
>> +                       sandbox,id = [2C 48 00 26 89 00 00 00];
>> +                       sandbox,onfi = [
>> +                               4f 4e 46 49 0e 00 5a 00
>> +                               ff 01 00 00 00 00 03 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               4d 49 43 52 4f 4e 20 20
>> +                               20 20 20 20 4d 54 32 39
>> +                               46 36 34 47 30 38 41 4b
>> +                               41 42 41 43 35 20 20 20
>> +                               2c 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 10 00 00 e0 00 00 02
>> +                               00 00 1c 00 80 00 00 00
>> +                               00 10 00 00 02 23 01 50
>> +                               00 01 05 01 00 00 04 00
>> +                               04 01 1e 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               0e 1f 00 1f 00 f4 01 ac
>> +                               0d 19 00 c8 00 00 00 00
>> +                               00 00 00 00 00 00 0a 07
>> +                               19 00 00 00 00 00 00 00
>> +                               00 00 00 00 01 00 01 00
>> +                               00 00 04 10 01 81 04 02
>> +                               02 01 1e 90 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 00 00 00
>> +                               00 00 00 00 00 03 20 7d
>> +                       ];
>> +                       sandbox,erasesize = <(512 * 1024)>;
>> +                       sandbox,oobsize = <224>;
>> +                       sandbox,pagesize = <4096>;
>> +                       sandbox,pages = <0x200000>;
>> +                       sandbox,err-count = <3>;
>> +                       sandbox,err-step-size = <512>;
>> +                       sandbox,ecc-bytes = <7>;
>> +               };
>> +       };
>>   };
>>
>>   #include "sandbox_pmic.dtsi"
>> diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
>> index 1a01f51a0b7..d4e5eb3000b 100644
>> --- a/configs/sandbox64_defconfig
>> +++ b/configs/sandbox64_defconfig
>> @@ -1,4 +1,5 @@
>>   CONFIG_TEXT_BASE=0
>> +CONFIG_SYS_MALLOC_LEN=0x6000000
>>   CONFIG_NR_DRAM_BANKS=1
>>   CONFIG_ENV_SIZE=0x2000
>>   CONFIG_DEFAULT_DEVICE_TREE="sandbox64"
>> @@ -50,7 +51,7 @@ CONFIG_CMD_GPT_RENAME=y
>>   CONFIG_CMD_IDE=y
>>   CONFIG_CMD_I2C=y
>>   CONFIG_CMD_LOADM=y
>> -CONFIG_CMD_MBR=y
>> +CONFIG_CMD_MTD=y
>>   CONFIG_CMD_OSD=y
>>   CONFIG_CMD_PCI=y
>>   CONFIG_CMD_READ=y
>> @@ -168,6 +169,13 @@ CONFIG_PWRSEQ=y
>>   CONFIG_I2C_EEPROM=y
>>   CONFIG_MMC_SANDBOX=y
>>   CONFIG_MTD=y
>> +CONFIG_DM_MTD=y
>> +CONFIG_MTD_RAW_NAND=y
>> +CONFIG_SYS_MAX_NAND_DEVICE=8
>> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
>> +CONFIG_NAND_SANDBOX=y
>> +CONFIG_SYS_NAND_ONFI_DETECTION=y
>> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>>   CONFIG_SPI_FLASH_SANDBOX=y
>>   CONFIG_BOOTDEV_SPI_FLASH=y
>>   CONFIG_SPI_FLASH_ATMEL=y
>> diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
>> index e3a2f9eb170..1c71f0542a3 100644
>> --- a/configs/sandbox_defconfig
>> +++ b/configs/sandbox_defconfig
>> @@ -1,4 +1,5 @@
>>   CONFIG_TEXT_BASE=0
>> +CONFIG_SYS_MALLOC_LEN=0x6000000
>>   CONFIG_NR_DRAM_BANKS=1
>>   CONFIG_ENV_SIZE=0x2000
>>   CONFIG_DEFAULT_DEVICE_TREE="sandbox"
>> @@ -73,6 +74,7 @@ CONFIG_CMD_IDE=y
>>   CONFIG_CMD_I2C=y
>>   CONFIG_CMD_LOADM=y
>>   CONFIG_CMD_LSBLK=y
>> +CONFIG_CMD_MTD=y
>>   CONFIG_CMD_MUX=y
>>   CONFIG_CMD_OSD=y
>>   CONFIG_CMD_PCI=y
>> @@ -215,6 +217,13 @@ CONFIG_MMC_PCI=y
>>   CONFIG_MMC_SANDBOX=y
>>   CONFIG_MMC_SDHCI=y
>>   CONFIG_MTD=y
>> +CONFIG_DM_MTD=y
>> +CONFIG_MTD_RAW_NAND=y
>> +CONFIG_SYS_MAX_NAND_DEVICE=8
>> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
>> +CONFIG_NAND_SANDBOX=y
>> +CONFIG_SYS_NAND_ONFI_DETECTION=y
>> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>>   CONFIG_SPI_FLASH_SANDBOX=y
>>   CONFIG_BOOTDEV_SPI_FLASH=y
>>   CONFIG_SPI_FLASH_ATMEL=y
>> diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
>> index db05e630832..09ebafeccca 100644
>> --- a/configs/sandbox_noinst_defconfig
>> +++ b/configs/sandbox_noinst_defconfig
>> @@ -80,7 +80,7 @@ CONFIG_CMD_GPIO=y
>>   CONFIG_CMD_GPT=y
>>   CONFIG_CMD_IDE=y
>>   CONFIG_CMD_I2C=y
>> -CONFIG_CMD_MBR=y
>> +CONFIG_CMD_MTD=y
>>   CONFIG_CMD_OSD=y
>>   CONFIG_CMD_PCI=y
>>   CONFIG_CMD_REMOTEPROC=y
>> @@ -181,6 +181,14 @@ CONFIG_PWRSEQ=y
>>   CONFIG_SPL_PWRSEQ=y
>>   CONFIG_FS_LOADER=y
>>   CONFIG_MMC_SANDBOX=y
>> +CONFIG_MTD=y
>> +CONFIG_DM_MTD=y
>> +CONFIG_MTD_RAW_NAND=y
>> +CONFIG_SYS_MAX_NAND_DEVICE=8
>> +CONFIG_SYS_NAND_USE_FLASH_BBT=y
>> +CONFIG_NAND_SANDBOX=y
>> +CONFIG_SYS_NAND_ONFI_DETECTION=y
>> +CONFIG_SYS_NAND_PAGE_SIZE=0x200
>>   CONFIG_SPI_FLASH_SANDBOX=y
>>   CONFIG_SPI_FLASH_ATMEL=y
>>   CONFIG_SPI_FLASH_EON=y
>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>> index ee484dc0f51..6b34aa6782c 100644
>> --- a/drivers/mtd/nand/raw/Kconfig
>> +++ b/drivers/mtd/nand/raw/Kconfig
>> @@ -447,6 +447,20 @@ config NAND_PXA3XX
>>            This enables the driver for the NAND flash device found on
>>            PXA3xx processors (NFCv1) and also on Armada 370/XP (NFCv2).
>>
>> +config NAND_SANDBOX
>> +       bool "Support for NAND in sandbox"
>> +       depends on SANDBOX
>> +       select SYS_NAND_SELF_INIT
>> +       select SYS_NAND_SOFT_ECC
>> +       select BCH
>> +       select NAND_ECC_BCH
>> +       imply CMD_NAND
>> +       help
>> +         Enable a dummy NAND driver for sandbox. It simulates any number of
>> +         arbitrary NAND chips with a RAM buffer. It will also inject errors to
>> +         test ECC. At the moment, only 8-bit busses and single-chip devices are
>> +         supported.
>> +
>>   config NAND_SUNXI
>>          bool "Support for NAND on Allwinner SoCs"
>>          default ARCH_SUNXI
>> diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
>> index add2b4cf655..ddbba899e58 100644
>> --- a/drivers/mtd/nand/raw/Makefile
>> +++ b/drivers/mtd/nand/raw/Makefile
>> @@ -70,6 +70,7 @@ obj-$(CONFIG_NAND_PXA3XX) += pxa3xx_nand.o
>>   obj-$(CONFIG_TEGRA_NAND) += tegra_nand.o
>>   obj-$(CONFIG_NAND_OMAP_GPMC) += omap_gpmc.o
>>   obj-$(CONFIG_NAND_OMAP_ELM) += omap_elm.o
>> +obj-$(CONFIG_NAND_SANDBOX) += sand_nand.o
>>   obj-$(CONFIG_NAND_SUNXI) += sunxi_nand.o
>>   obj-$(CONFIG_NAND_MXIC) += mxic_nand.o
>>   obj-$(CONFIG_NAND_ZYNQ) += zynq_nand.o
>> diff --git a/drivers/mtd/nand/raw/sand_nand.c b/drivers/mtd/nand/raw/sand_nand.c
>> new file mode 100644
>> index 00000000000..f1855877630
>> --- /dev/null
>> +++ b/drivers/mtd/nand/raw/sand_nand.c
>> @@ -0,0 +1,682 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * Copyright (C) Sean Anderson <seanga2@gmail.com>
>> + */
>> +
>> +#define LOG_CATEGORY UCLASS_MTD
>> +#include <errno.h>
>> +#include <hexdump.h>
>> +#include <log.h>
>> +#include <nand.h>
>> +#include <os.h>
>> +#include <rand.h>
>> +#include <dm/device_compat.h>
>> +#include <dm/read.h>
>> +#include <dm/uclass.h>
>> +#include <asm/bitops.h>
>> +#include <linux/bitmap.h>
>> +#include <linux/mtd/rawnand.h>
>> +#include <linux/sizes.h>
>> +
>> +enum sand_nand_state {
>> +       STATE_READY,
>> +       STATE_IDLE,
>> +       STATE_READ,
>> +       STATE_READ_ID,
>> +       STATE_READ_ONFI,
>> +       STATE_PARAM_ONFI,
>> +       STATE_STATUS,
>> +       STATE_PROG,
>> +       STATE_ERASE,
>> +};
>> +
>> +static const char *const state_name[] = {
>> +       [STATE_READY] = "READY",
>> +       [STATE_IDLE] = "IDLE",
>> +       [STATE_READ] = "READ",
>> +       [STATE_READ_ID] = "READ_ID",
>> +       [STATE_READ_ONFI] = "READ_ONFI",
>> +       [STATE_PARAM_ONFI] = "PARAM_ONFI",
>> +       [STATE_STATUS] = "STATUS",
>> +       [STATE_PROG] = "PROG",
>> +       [STATE_ERASE] = "ERASE",
>> +};
>> +
>> +/**
>> + * struct sand_nand_chip - Per-device private data
>> + * @nand: The nand chip
>> + * @node: The next device in this controller
>> + * @programmed: Bitmap of whether sectors are programmed
>> + * @id: ID to report for NAND_CMD_READID
>> + * @id_len: Length of @id
>> + * @onfi: Three copies of ONFI parameter page
>> + * @status: Status to report for NAND_CMD_STATUS
>> + * @chunksize: Size of one "chunk" (page + oob) in bytes
>> + * @pageize: Size of one page in bytes
>> + * @pages: Total number of pages
>> + * @pages_per_erase: Number of pages per eraseblock
>> + * @err_count: Number of errors to inject per @err_step_bits of data
>> + * @err_step_bits: Number of data bits per error "step"
>> + * @err_steps: Number of err steps in a page
>> + * @ecc_bits: Number of ECC bits uper @err_step_bits
>> + * @cs: Chip select for this device
>> + * @state: Current state of the device
>> + * @column: Column of the most-recent command
>> + * @page_addr: Page address of the most-recent command
>> + * @fd: File descriptor for the backing data
>> + * @fd_page_addr: Page address that @fd is seek'd to
>> + * @selected: Whether this device is selected
>> + * @tmp: "Cache" buffer used to store transferred data before committing it
>> + * @tmp_dirty: Whether @tmp is dirty (modified) or clean (all ones)
>> + */
>> +struct sand_nand_chip {
>> +       struct nand_chip nand;
>> +       struct list_head node;
>> +       long *programmed;
>> +       const u8 *id;
>> +       u32 chunksize, pagesize, pages, pages_per_erase;
>> +       u32 err_count, err_step_bits, err_steps, ecc_bits;
>> +       unsigned int cs;
>> +       enum sand_nand_state state;
>> +       int column, page_addr, fd, fd_page_addr;
>> +       bool selected, tmp_dirty;
>> +       u8 status;
> 
> I see a character "^L".
> Please remove it.

Good catch. It blends in with the background in my editor...

--Sean

>> +       u8 id_len;
>> +       u8 tmp[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
>> +       u8 onfi[sizeof(struct nand_onfi_params) * 3];
>> +};
>> +
>> +#define SAND_DEBUG(chip, fmt, ...) \
>> +       dev_dbg((chip)->nand.mtd.dev, "%u (%s): " fmt, (chip)->cs, \
>> +               state_name[(chip)->state], ##__VA_ARGS__)
>> +
>> +static inline void to_state(struct sand_nand_chip *chip,
>> +                           enum sand_nand_state new_state)
>> +{
>> +       if (new_state != chip->state)
>> +               SAND_DEBUG(chip, "to state %s\n", state_name[new_state]);
>> +       chip->state = new_state;
>> +}
>> +
>> +static inline struct sand_nand_chip *to_sand_nand(struct nand_chip *nand)
>> +{
>> +       return container_of(nand, struct sand_nand_chip, nand);
>> +}
>> +
>> +struct sand_nand_priv {
>> +       struct list_head chips;
>> +};
>> +
>> +static int sand_nand_dev_ready(struct mtd_info *mtd)
>> +{
>> +       return 1;
>> +}
>> +
>> +static int sand_nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
>> +{
>> +       u8 status;
>> +
>> +       return nand_status_op(chip, &status) ?: status;
>> +}
>> +
>> +static int sand_nand_seek(struct sand_nand_chip *chip)
>> +{
>> +       if (chip->fd_page_addr == chip->page_addr)
>> +               return 0;
>> +
>> +       if (os_lseek(chip->fd, (off_t)chip->page_addr * chip->chunksize,
>> +                    OS_SEEK_SET) < 0) {
>> +               SAND_DEBUG(chip, "could not seek: %d\n", errno);
>> +               return -EIO;
>> +       }
>> +
>> +       chip->fd_page_addr = chip->page_addr;
>> +       return 0;
>> +}
>> +
>> +static void sand_nand_inject_error(struct sand_nand_chip *chip,
>> +                                  unsigned int step, unsigned int pos)
>> +{
>> +       int byte, index;
>> +
>> +       if (pos < chip->err_step_bits) {
>> +               __change_bit(step * chip->err_step_bits + pos, chip->tmp);
>> +               return;
>> +       }
>> +
>> +       /*
>> +        * Only ECC bytes are covered in the OOB area, so
>> +        * pretend that those are the only bytes which can have
>> +        * errors.
>> +        */
>> +       byte = (pos - chip->err_step_bits + step * chip->ecc_bits) / 8;
>> +       index = chip->nand.ecc.layout->eccpos[byte];
>> +       /* Avoid endianness issues by working with bytes */
>> +       chip->tmp[chip->pagesize + index] ^= BIT(pos & 0x7);
>> +}
>> +
>> +static int sand_nand_read(struct sand_nand_chip *chip)
>> +{
>> +       unsigned int i, stop = 0;
>> +
>> +       if (chip->column == chip->pagesize)
>> +               stop = chip->err_step_bits;
>> +
>> +       if (test_bit(chip->page_addr, chip->programmed)) {
>> +               if (sand_nand_seek(chip))
>> +                       return -EIO;
>> +
>> +               if (os_read(chip->fd, chip->tmp, chip->chunksize) !=
>> +                   chip->chunksize) {
>> +                       SAND_DEBUG(chip, "could not read: %d\n", errno);
>> +                       return -EIO;
>> +               }
>> +               chip->fd_page_addr++;
>> +       } else if (chip->tmp_dirty) {
>> +               memset(chip->tmp + chip->column, 0xff,
>> +                      chip->chunksize - chip->column);
>> +       }
>> +
>> +       /*
>> +        * Inject some errors; this is Method A from "An Efficient Algorithm for
>> +        * Sequential Random Sampling" (Vitter 87). This is still slow when
>> +        * generating a lot (dozens) of ECC errors.
>> +        *
>> +        * To avoid generating too many errors in any one ECC step, we separate
>> +        * our error generation by ECC step.
>> +        */
>> +       chip->tmp_dirty = true;
>> +       for (i = 0; i < chip->err_steps; i++) {
>> +               u32 bit_errors = chip->err_count;
>> +               unsigned int j = chip->err_step_bits + chip->ecc_bits;
>> +
>> +               while (bit_errors) {
>> +                       unsigned int u = rand();
>> +                       float quot = 1ULL << 32;
>> +
>> +                       do {
>> +                               quot *= j - bit_errors;
>> +                               quot /= j;
>> +                               j--;
>> +
>> +                               if (j < stop)
>> +                                       goto next;
>> +                       } while (u < quot);
>> +
>> +                       sand_nand_inject_error(chip, i, j);
>> +                       bit_errors--;
>> +               }
>> +next:
>> +               ;
>> +       }
>> +
>> +       return 0;
>> +}
>> +
>> +static void sand_nand_command(struct mtd_info *mtd, unsigned int command,
>> +                             int column, int page_addr)
>> +{
>> +       struct nand_chip *nand = mtd_to_nand(mtd);
>> +       struct sand_nand_chip *chip = to_sand_nand(nand);
>> +       enum sand_nand_state new_state = chip->state;
>> +
>> +       SAND_DEBUG(chip, "command=%02x column=%d page_addr=%d\n", command,
>> +                  column, page_addr);
>> +
>> +       if (!chip->selected)
>> +               return;
>> +
>> +       switch (chip->state) {
>> +       case STATE_READY:
>> +               if (command == NAND_CMD_RESET)
>> +                       goto reset;
>> +               break;
>> +       case STATE_PROG:
>> +               new_state = STATE_IDLE;
>> +               if (command != NAND_CMD_PAGEPROG ||
>> +                   test_and_set_bit(chip->page_addr, chip->programmed)) {
>> +                       chip->status |= NAND_STATUS_FAIL;
>> +                       break;
>> +               }
>> +
>> +               if (sand_nand_seek(chip)) {
>> +                       chip->status |= NAND_STATUS_FAIL;
>> +                       break;
>> +               }
>> +
>> +               if (os_write(chip->fd, chip->tmp, chip->chunksize) !=
>> +                   chip->chunksize) {
>> +                       SAND_DEBUG(chip, "could not write: %d\n", errno);
>> +                       chip->status |= NAND_STATUS_FAIL;
>> +                       break;
>> +               }
>> +
>> +               chip->fd_page_addr++;
>> +               break;
>> +       case STATE_ERASE:
>> +               new_state = STATE_IDLE;
>> +               if (command != NAND_CMD_ERASE2) {
>> +                       chip->status |= NAND_STATUS_FAIL;
>> +                       break;
>> +               }
>> +
>> +               if (chip->page_addr < 0 ||
>> +                   chip->page_addr >= chip->pages ||
>> +                   chip->page_addr % chip->pages_per_erase)
>> +                       chip->status |= NAND_STATUS_FAIL;
>> +               else
>> +                       bitmap_clear(chip->programmed, chip->page_addr,
>> +                                    chip->pages_per_erase);
>> +               break;
>> +       default:
>> +               chip->column = column;
>> +               chip->page_addr = page_addr;
>> +               switch (command) {
>> +               case NAND_CMD_READOOB:
>> +                       if (column >= 0)
>> +                               chip->column += chip->pagesize;
>> +                       fallthrough;
>> +               case NAND_CMD_READ0:
>> +                       new_state = STATE_IDLE;
>> +                       if (page_addr < 0 || page_addr >= chip->pages)
>> +                               break;
>> +
>> +                       if (chip->column < 0 || chip->column >= chip->chunksize)
>> +                               break;
>> +
>> +                       if (sand_nand_read(chip))
>> +                               break;
>> +
>> +                       chip->page_addr = page_addr;
>> +                       new_state = STATE_READ;
>> +                       break;
>> +               case NAND_CMD_ERASE1:
>> +                       new_state = STATE_ERASE;
>> +                       chip->status = ~NAND_STATUS_FAIL;
>> +                       break;
>> +               case NAND_CMD_STATUS:
>> +                       new_state = STATE_STATUS;
>> +                       chip->column = 0;
>> +                       break;
>> +               case NAND_CMD_SEQIN:
>> +                       new_state = STATE_PROG;
>> +                       chip->status = ~NAND_STATUS_FAIL;
>> +                       if (page_addr < 0 || page_addr >= chip->pages ||
>> +                           chip->column < 0 ||
>> +                           chip->column >= chip->chunksize) {
>> +                               chip->status |= NAND_STATUS_FAIL;
>> +                       } else if (chip->tmp_dirty) {
>> +                               memset(chip->tmp, 0xff, chip->chunksize);
>> +                               chip->tmp_dirty = false;
>> +                       }
>> +                       break;
>> +               case NAND_CMD_READID:
>> +                       if (chip->onfi[0] && column == 0x20)
>> +                               new_state = STATE_READ_ONFI;
>> +                       else
>> +                               new_state = STATE_READ_ID;
>> +                       chip->column = 0;
>> +                       break;
>> +               case NAND_CMD_PARAM:
>> +                       if (chip->onfi[0] && !column)
>> +                               new_state = STATE_PARAM_ONFI;
>> +                       else
>> +                               new_state = STATE_IDLE;
>> +                       break;
>> +               case NAND_CMD_RESET:
>> +reset:
>> +                       new_state = STATE_IDLE;
>> +                       chip->column = -1;
>> +                       chip->page_addr = -1;
>> +                       chip->status = ~NAND_STATUS_FAIL;
>> +                       break;
>> +               default:
>> +                       new_state = STATE_IDLE;
>> +                       SAND_DEBUG(chip, "Unsupported command %02x\n", command);
>> +               }
>> +       }
>> +
>> +       to_state(chip, new_state);
>> +}
>> +
>> +static void sand_nand_select_chip(struct mtd_info *mtd, int n)
>> +{
>> +       struct nand_chip *nand = mtd_to_nand(mtd);
>> +       struct sand_nand_chip *chip = to_sand_nand(nand);
>> +
>> +       chip->selected = !n;
>> +}
>> +
>> +static void sand_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
>> +{
>> +       struct nand_chip *nand = mtd_to_nand(mtd);
>> +       struct sand_nand_chip *chip = to_sand_nand(nand);
>> +       unsigned int to_copy;
>> +       int src_len = 0;
>> +       const u8 *src = NULL;
>> +
>> +       if (!chip->selected)
>> +               goto copy;
>> +
>> +       switch (chip->state) {
>> +       case STATE_READ:
>> +               src = chip->tmp;
>> +               src_len = chip->chunksize;
>> +               break;
>> +       case STATE_READ_ID:
>> +               src = chip->id;
>> +               src_len = chip->id_len;
>> +               break;
>> +       case STATE_READ_ONFI:
>> +               src = "ONFI";
>> +               src_len = 4;
>> +               break;
>> +       case STATE_PARAM_ONFI:
>> +               src = chip->onfi;
>> +               src_len = sizeof(chip->onfi);
>> +               break;
>> +       case STATE_STATUS:
>> +               src = &chip->status;
>> +               src_len = 1;
>> +               break;
>> +       default:
>> +               break;
>> +       }
>> +
>> +copy:
>> +       if (chip->column >= 0)
>> +               to_copy = max(min(len, src_len - chip->column), 0);
>> +       else
>> +               to_copy = 0;
>> +       memcpy(buf, src + chip->column, to_copy);
>> +       memset(buf + to_copy, 0xff, len - to_copy);
>> +       chip->column += to_copy;
>> +
>> +       if (len == 1) {
>> +               SAND_DEBUG(chip, "read [ %02x ]\n", buf[0]);
>> +       } else if (src_len) {
>> +               SAND_DEBUG(chip, "read %d bytes\n", len);
>> +#ifdef VERBOSE_DEBUG
>> +               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
>> +#endif
>> +       }
>> +
>> +       if (src_len && chip->column == src_len)
>> +               to_state(chip, STATE_IDLE);
>> +}
>> +
>> +static u8 sand_nand_read_byte(struct mtd_info *mtd)
>> +{
>> +       u8 ret;
>> +
>> +       sand_nand_read_buf(mtd, &ret, 1);
>> +       return ret;
>> +}
>> +
>> +static u16 sand_nand_read_word(struct mtd_info *mtd)
>> +{
>> +       struct nand_chip *nand = mtd_to_nand(mtd);
>> +       struct sand_nand_chip *chip = to_sand_nand(nand);
>> +
>> +       SAND_DEBUG(chip, "16-bit access unsupported\n");
>> +       return sand_nand_read_byte(mtd) | 0xff00;
>> +}
>> +
>> +static void sand_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
>> +{
>> +       struct nand_chip *nand = mtd_to_nand(mtd);
>> +       struct sand_nand_chip *chip = to_sand_nand(nand);
>> +
>> +       SAND_DEBUG(chip, "write %d bytes\n", len);
>> +#ifdef VERBOSE_DEBUG
>> +       print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
>> +#endif
>> +
>> +       if (chip->state != STATE_PROG || chip->status & NAND_STATUS_FAIL)
>> +               return;
>> +
>> +       chip->tmp_dirty = true;
>> +       len = min((unsigned int)len, chip->chunksize - chip->column);
>> +       memcpy(chip->tmp + chip->column, buf, len);
>> +       chip->column += len;
>> +}
>> +
>> +static struct nand_chip *nand_chip;
>> +
>> +int sand_nand_remove(struct udevice *dev)
>> +{
>> +       struct sand_nand_priv *priv = dev_get_priv(dev);
>> +       struct sand_nand_chip *chip;
>> +
>> +       list_for_each_entry(chip, &priv->chips, node) {
>> +               struct nand_chip *nand = &chip->nand;
>> +
>> +               if (nand_chip == nand)
>> +                       nand_chip = NULL;
>> +
>> +               nand_unregister(nand_to_mtd(nand));
>> +               free(chip->programmed);
>> +               os_close(chip->fd);
>> +               free(chip);
>> +       }
>> +
>> +       return 0;
>> +}
>> +
>> +static int sand_nand_probe(struct udevice *dev)
>> +{
>> +       struct sand_nand_priv *priv = dev_get_priv(dev);
>> +       struct sand_nand_chip *chip;
>> +       int ret, devnum = 0;
>> +       ofnode np;
>> +
>> +       INIT_LIST_HEAD(&priv->chips);
>> +
>> +       dev_for_each_subnode(np, dev) {
>> +               struct nand_chip *nand;
>> +               struct mtd_info *mtd;
>> +               u32 erasesize, oobsize, pagesize, pages;
>> +               u32 err_count, err_step_size, ecc_bytes;
>> +               off_t expected_size;
>> +               char filename[30];
>> +               fdt_addr_t cs;
>> +               const u8 *id, *onfi;
>> +               int id_len, onfi_len;
>> +
>> +               cs = ofnode_get_addr_size_index_notrans(np, 0, NULL);
>> +               if (cs == FDT_ADDR_T_NONE) {
>> +                       dev_dbg(dev, "Invalid cs for chip %s\n",
>> +                               ofnode_get_name(np));
>> +                       ret = -ENOENT;
>> +                       goto err;
>> +               }
>> +
>> +               id = ofnode_read_prop(np, "sandbox,id", &id_len);
>> +               if (!id) {
>> +                       dev_dbg(dev, "No sandbox,id property for chip %s\n",
>> +                               ofnode_get_name(np));
>> +                       ret = -EINVAL;
>> +                       goto err;
>> +               }
>> +
>> +               onfi = ofnode_read_prop(np, "sandbox,onfi", &onfi_len);
>> +               if (onfi && onfi_len != sizeof(struct nand_onfi_params)) {
>> +                       dev_dbg(dev, "Invalid length %d for onfi params\n",
>> +                               onfi_len);
>> +                       ret = -EINVAL;
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,erasesize", &erasesize);
>> +               if (ret) {
>> +                       dev_dbg(dev, "No sandbox,erasesize property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,oobsize", &oobsize);
>> +               if (ret) {
>> +                       dev_dbg(dev, "No sandbox,oobsize property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,pagesize", &pagesize);
>> +               if (ret) {
>> +                       dev_dbg(dev, "No sandbox,pagesize property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,pages", &pages);
>> +               if (ret) {
>> +                       dev_dbg(dev, "No sandbox,pages property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,err-count", &err_count);
>> +               if (ret) {
>> +                       dev_dbg(dev,
>> +                               "No sandbox,err-count property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,err-step-size",
>> +                                     &err_step_size);
>> +               if (ret) {
>> +                       dev_dbg(dev,
>> +                               "No sandbox,err-step-size property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               ret = ofnode_read_u32(np, "sandbox,ecc-bytes", &ecc_bytes);
>> +               if (ret) {
>> +                       dev_dbg(dev,
>> +                               "No sandbox,ecc-bytes property for chip %s",
>> +                               ofnode_get_name(np));
>> +                       goto err;
>> +               }
>> +
>> +               chip = calloc(sizeof(*chip), 1);
>> +               if (!chip) {
>> +                       ret = -ENOMEM;
>> +                       goto err;
>> +               }
>> +
>> +               chip->cs = cs;
>> +               chip->id = id;
>> +               chip->id_len = id_len;
>> +               chip->chunksize = pagesize + oobsize;
>> +               chip->pagesize = pagesize;
>> +               chip->pages = pages;
>> +               chip->pages_per_erase = erasesize / pagesize;
>> +               memset(chip->tmp, 0xff, chip->chunksize);
>> +
>> +               chip->err_count = err_count;
>> +               chip->err_step_bits = err_step_size * 8;
>> +               chip->err_steps = pagesize / err_step_size;
>> +               chip->ecc_bits = ecc_bytes * 8;
>> +
>> +               expected_size = (off_t)pages * chip->chunksize;
>> +               snprintf(filename, sizeof(filename),
>> +                        "/tmp/u-boot.nand%d.XXXXXX", devnum);
>> +               chip->fd = os_mktemp(filename, expected_size);
>> +               if (chip->fd < 0) {
>> +                       dev_dbg(dev, "Could not create temp file %s\n",
>> +                               filename);
>> +                       ret = chip->fd;
>> +                       goto err_chip;
>> +               }
>> +
>> +               chip->programmed = calloc(sizeof(long),
>> +                                         BITS_TO_LONGS(pages));
>> +               if (!chip->programmed) {
>> +                       ret = -ENOMEM;
>> +                       goto err_fd;
>> +               }
>> +
>> +               if (onfi) {
>> +                       memcpy(chip->onfi, onfi, onfi_len);
>> +                       memcpy(chip->onfi + onfi_len, onfi, onfi_len);
>> +                       memcpy(chip->onfi + 2 * onfi_len, onfi, onfi_len);
>> +               }
>> +
>> +               nand = &chip->nand;
>> +               nand->flash_node = np;
>> +               nand->dev_ready = sand_nand_dev_ready;
>> +               nand->cmdfunc = sand_nand_command;
>> +               nand->waitfunc = sand_nand_wait;
>> +               nand->select_chip = sand_nand_select_chip;
>> +               nand->read_byte = sand_nand_read_byte;
>> +               nand->read_word = sand_nand_read_word;
>> +               nand->read_buf = sand_nand_read_buf;
>> +               nand->write_buf = sand_nand_write_buf;
>> +               nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
>> +
>> +               mtd = nand_to_mtd(nand);
>> +               mtd->dev = dev;
>> +
>> +               ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
>> +               if (ret) {
>> +                       dev_dbg(dev, "Could not scan chip %s: %d\n",
>> +                               ofnode_get_name(np), ret);
>> +                       goto err_prog;
>> +               }
>> +
>> +               ret = nand_register(devnum, mtd);
>> +               if (ret) {
>> +                       dev_dbg(dev, "Could not register nand %d: %d\n", devnum,
>> +                               ret);
>> +                       goto err_prog;
>> +               }
>> +
>> +               if (!nand_chip)
>> +                       nand_chip = nand;
>> +
>> +               list_add_tail(&chip->node, &priv->chips);
>> +               devnum++;
>> +               continue;
>> +
>> +err_prog:
>> +               free(chip->programmed);
>> +err_fd:
>> +               os_close(chip->fd);
>> +err_chip:
>> +               free(chip);
>> +               goto err;
>> +       }
>> +
>> +       return 0;
>> +
>> +err:
>> +       sand_nand_remove(dev);
>> +       return ret;
>> +}
>> +
>> +static const struct udevice_id sand_nand_ids[] = {
>> +       { .compatible = "sandbox,nand" },
>> +       { }
>> +};
>> +
>> +U_BOOT_DRIVER(sand_nand) = {
>> +       .name           = "sand-nand",
>> +       .id             = UCLASS_MTD,
>> +       .of_match       = sand_nand_ids,
>> +       .probe          = sand_nand_probe,
>> +       .remove         = sand_nand_remove,
>> +       .priv_auto      = sizeof(struct sand_nand_priv),
>> +};
>> +
>> +void board_nand_init(void)
>> +{
>> +       struct udevice *dev;
>> +       int err;
>> +
>> +       err = uclass_get_device_by_driver(UCLASS_MTD, DM_DRIVER_REF(sand_nand),
>> +                                         &dev);
>> +       if (err && err != -ENODEV)
>> +               log_info("Failed to get sandbox NAND: %d\n", err);
>> +}
>> diff --git a/test/dm/Makefile b/test/dm/Makefile
>> index cb82d839f8a..a3ce7b3889f 100644
>> --- a/test/dm/Makefile
>> +++ b/test/dm/Makefile
>> @@ -73,6 +73,7 @@ obj-$(CONFIG_CMD_MUX) += mux-cmd.o
>>   obj-$(CONFIG_MULTIPLEXER) += mux-emul.o
>>   obj-$(CONFIG_MUX_MMIO) += mux-mmio.o
>>   obj-y += fdtdec.o
>> +obj-$(CONFIG_MTD_RAW_NAND) += nand.o
>>   obj-$(CONFIG_UT_DM) += nop.o
>>   obj-y += ofnode.o
>>   obj-y += ofread.o
>> diff --git a/test/dm/nand.c b/test/dm/nand.c
>> new file mode 100644
>> index 00000000000..a1304965072
>> --- /dev/null
>> +++ b/test/dm/nand.c
>> @@ -0,0 +1,106 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
>> + */
>> +
>> +#include <nand.h>
>> +#include <part.h>
>> +#include <rand.h>
>> +#include <dm/test.h>
>> +#include <test/test.h>
>> +#include <test/ut.h>
>> +#include <linux/mtd/mtd.h>
>> +#include <linux/mtd/rawnand.h>
>> +
>> +static int dm_test_nand(struct unit_test_state *uts, int dev, bool end)
>> +{
>> +       nand_erase_options_t opts = { };
>> +       struct mtd_info *mtd;
>> +       size_t length;
>> +       loff_t size;
>> +       char *buf;
>> +       int *gold;
>> +       u8 oob[NAND_MAX_OOBSIZE];
>> +       int i;
>> +       loff_t off = 0;
>> +       mtd_oob_ops_t ops = { };
>> +
>> +       /* Seed RNG for bit errors */
>> +       srand((off >> 32) ^ off ^ ~dev);
>> +
>> +       mtd = get_nand_dev_by_index(dev);
>> +       ut_assertnonnull(mtd);
>> +       size = mtd->erasesize * 4;
>> +       length = size;
>> +
>> +       buf = malloc(size);
>> +       ut_assertnonnull(buf);
>> +       gold = malloc(size);
>> +       ut_assertnonnull(gold);
>> +
>> +       /* Mark a block as bad */
>> +       ut_assertok(mtd_block_markbad(mtd, off + mtd->erasesize));
>> +       /* Save the OOB for later */
>> +       ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
>> +
>> +       /* Erase some stuff */
>> +       if (end)
>> +               off = mtd->size - size - mtd->erasesize;
>> +       opts.offset = off;
>> +       opts.length = size;
>> +       opts.spread = 1;
>> +       opts.lim = U32_MAX;
>> +       ut_assertok(nand_erase_opts(mtd, &opts));
>> +
>> +       /* Make sure everything is erased */
>> +       memset(gold, 0xff, size);
>> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
>> +       ut_asserteq(size, length);
>> +       ut_asserteq_mem(gold, buf, size);
>> +
>> +       /* ...but our bad block marker is still there */
>> +       ops.oobbuf = oob;
>> +       ops.ooblen = mtd->oobsize;
>> +       ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
>> +       ut_asserteq(0, oob[mtd_to_nand(mtd)->badblockpos]);
>> +
>> +       /* Generate some data and write it */
>> +       for (i = 0; i < size / sizeof(int); i++)
>> +               gold[i] = 0;//rand();
>> +       ut_assertok(nand_write_skip_bad(mtd, off, &length, NULL, U64_MAX,
>> +                                       (void *)gold, 0));
>> +       ut_asserteq(size, length);
>> +
>> +       /* Verify */
>> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
>> +       ut_asserteq(size, length);
>> +       ut_asserteq_mem(gold, buf, size);
>> +
>> +       /* Erase some blocks */
>> +       memset(((char *)gold) + mtd->erasesize, 0xff, mtd->erasesize * 2);
>> +       opts.offset = off + mtd->erasesize;
>> +       opts.length = mtd->erasesize * 2,
>> +       ut_assertok(nand_erase_opts(mtd, &opts));
>> +
>> +       /* Verify */
>> +       ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
>> +       ut_asserteq(size, length);
>> +       ut_asserteq_mem(gold, buf, size);
>> +
>> +       return 0;
>> +}
>> +
>> +#define DM_NAND_TEST(dev) \
>> +static int dm_test_nand##dev##_start(struct unit_test_state *uts) \
>> +{ \
>> +       return dm_test_nand(uts, dev, false); \
>> +} \
>> +DM_TEST(dm_test_nand##dev##_start, UT_TESTF_SCAN_FDT); \
>> +static int dm_test_nand##dev##_end(struct unit_test_state *uts) \
>> +{ \
>> +       return dm_test_nand(uts, dev, true); \
>> +} \
>> +DM_TEST(dm_test_nand##dev##_end, UT_TESTF_SCAN_FDT)
>> +
>> +DM_NAND_TEST(0);
>> +DM_NAND_TEST(1);
>> --
>> 2.37.1
>>
> 
> 


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
                   ` (14 preceding siblings ...)
  2023-10-29  3:48 ` [PATCH 15/15] test: spl: Add a test for NAND Sean Anderson
@ 2023-11-02 14:01 ` Dario Binacchi
  2023-11-02 14:05   ` Sean Anderson
  15 siblings, 1 reply; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 14:01 UTC (permalink / raw)
  To: Sean Anderson
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

Sean, All,

On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>
> This series tests raw nand flash in sandbox and fixes various bugs discovered in
> the process. I've tried to do things in a contemporary manner, avoiding the
> (numerous) variations present on only a few boards. The test is pretty minimal.
> Future work could test the rest of the nand API as well as the MTD API.
>
> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
> everything grows by a few bytes due to nand_page_size. A few boards grow more,
> mostly those using nand_spl_loaders.c.
>
> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
>
>
> Sean Anderson (15):
>   spl: nand: Fix NULL-pointer dereference
>   nand: Don't dereference NULL manufacturer_desc
>   nand: Calculate SYS_NAND_PAGE_COUNT automatically
>   nand: spl_loaders: Only read enough pages to load the image
>   spl: legacy: Honor bl_len when decompressing
>   spl: nand: Set bl_len to page size
>   cmd: nand: Map memory before accessing it
>   spl: nand: Map memory before accessing it
>   mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
>   mtd: Add some fallbacks for add/del_mtd_device
>   nand: Add function to unregister NAND devices
>   nand: Allow reinitialization
>   arch: sandbox: Add function to create temporary files
>   nand: Add sandbox driver
>   test: spl: Add a test for NAND
>
>  README                                        |   9 +-
>  arch/sandbox/cpu/os.c                         |  17 +
>  arch/sandbox/dts/test.dts                     |  67 ++
>  arch/sandbox/include/asm/spl.h                |   1 +
>  cmd/nand.c                                    |  26 +-
>  common/spl/Kconfig                            |   2 +-
>  common/spl/spl_legacy.c                       |  18 +-
>  common/spl/spl_nand.c                         |  22 +-
>  configs/am335x_baltos_defconfig               |   3 +-
>  configs/am335x_evm_defconfig                  |   3 +-
>  configs/am335x_evm_spiboot_defconfig          |   2 +-
>  configs/am335x_guardian_defconfig             |   1 -
>  configs/am335x_hs_evm_defconfig               |   2 +-
>  configs/am335x_hs_evm_uart_defconfig          |   2 +-
>  configs/am335x_igep003x_defconfig             |   3 +-
>  configs/am335x_sl50_defconfig                 |   2 +-
>  configs/am3517_evm_defconfig                  |   3 +-
>  configs/am43xx_evm_defconfig                  |   3 +-
>  configs/am43xx_evm_rtconly_defconfig          |   3 +-
>  configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
>  configs/am43xx_hs_evm_defconfig               |   3 +-
>  configs/am62ax_evm_r5_defconfig               |   2 +-
>  configs/am65x_evm_a53_defconfig               |   2 +-
>  configs/axm_defconfig                         |   1 -
>  configs/chiliboard_defconfig                  |   1 -
>  configs/cm_t43_defconfig                      |   2 +-
>  configs/corvus_defconfig                      |   1 -
>  configs/da850evm_nand_defconfig               |   1 -
>  configs/devkit3250_defconfig                  |   1 -
>  configs/devkit8000_defconfig                  |   1 -
>  configs/dra7xx_evm_defconfig                  |   1 -
>  configs/draco_defconfig                       |   1 -
>  configs/etamin_defconfig                      |   1 -
>  .../gardena-smart-gateway-at91sam_defconfig   |   1 -
>  configs/igep00x0_defconfig                    |   3 +-
>  configs/imx6ulz_smm_m2_defconfig              |   2 +-
>  configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
>  configs/j7200_evm_a72_defconfig               |   2 +-
>  configs/j7200_evm_r5_defconfig                |   2 +-
>  configs/j721e_evm_a72_defconfig               |   2 +-
>  configs/j721e_evm_r5_defconfig                |   2 +-
>  configs/j721s2_evm_a72_defconfig              |   2 +-
>  configs/j721s2_evm_r5_defconfig               |   2 +-
>  configs/m53menlo_defconfig                    |   1 -
>  configs/omap35_logic_defconfig                |   3 +-
>  configs/omap35_logic_somlv_defconfig          |   3 +-
>  configs/omap3_beagle_defconfig                |   3 +-
>  configs/omap3_evm_defconfig                   |   3 +-
>  configs/omap3_logic_defconfig                 |   3 +-
>  configs/omap3_logic_somlv_defconfig           |   3 +-
>  configs/omapl138_lcdk_defconfig               |   1 -
>  configs/phycore-am335x-r2-regor_defconfig     |   3 +-
>  configs/phycore-am335x-r2-wega_defconfig      |   3 +-
>  configs/pxm2_defconfig                        |   1 -
>  configs/rastaban_defconfig                    |   1 -
>  configs/rut_defconfig                         |   1 -
>  configs/sama5d3_xplained_nandflash_defconfig  |   1 -
>  configs/sama5d3xek_nandflash_defconfig        |   1 -
>  configs/sama5d4_xplained_nandflash_defconfig  |   1 -
>  configs/sama5d4ek_nandflash_defconfig         |   1 -
>  configs/sandbox64_defconfig                   |  10 +-
>  configs/sandbox_defconfig                     |   9 +
>  configs/sandbox_noinst_defconfig              |  21 +-
>  configs/smartweb_defconfig                    |   1 -
>  configs/socfpga_secu1_defconfig               |   2 +-
>  configs/stm32746g-eval_spl_defconfig          |   2 +-
>  configs/stm32f746-disco_spl_defconfig         |   2 +-
>  configs/stm32f769-disco_spl_defconfig         |   2 +-
>  configs/stm32mp15_basic_defconfig             |   2 +-
>  configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
>  configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
>  configs/taurus_defconfig                      |   1 -
>  configs/thuban_defconfig                      |   1 -
>  drivers/mtd/Makefile                          |   2 +-
>  drivers/mtd/nand/raw/Kconfig                  |  27 +-
>  drivers/mtd/nand/raw/Makefile                 |   1 +
>  drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
>  drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
>  drivers/mtd/nand/raw/denali_spl.c             |   5 +
>  drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
>  drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
>  drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
>  drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
>  drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
>  drivers/mtd/nand/raw/nand.c                   |  66 +-
>  drivers/mtd/nand/raw/nand_base.c              |   7 +-
>  drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
>  drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
>  drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
>  drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
>  drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
>  drivers/mtd/onenand/onenand_uboot.c           |   2 -
>  include/linux/mtd/mtd.h                       |  12 +
>  include/mtd/cfi_flash.h                       |   2 +-
>  include/nand.h                                |   3 +
>  include/os.h                                  |  13 +
>  include/system-constants.h                    |   3 +
>  test/dm/Makefile                              |   1 +
>  test/dm/nand.c                                | 106 +++
>  test/image/Kconfig                            |   9 +
>  test/image/Makefile                           |   1 +
>  test/image/spl_load_nand.c                    |  54 ++
>  102 files changed, 1269 insertions(+), 153 deletions(-)
>  create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>  create mode 100644 test/dm/nand.c
>  create mode 100644 test/image/spl_load_nand.c
>
> --
> 2.37.1
>

The CI pipeline fails:
https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409

It seems like the problem is only for the clang tests (sandbox64 with
clang test.py and sandbox with clang test.py)

Regards,
Dario

-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-02 14:01 ` [PATCH 00/15] nand: Add sandbox tests Dario Binacchi
@ 2023-11-02 14:05   ` Sean Anderson
  2023-11-02 14:08     ` Dario Binacchi
  0 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-11-02 14:05 UTC (permalink / raw)
  To: Dario Binacchi
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

On 11/2/23 10:01, Dario Binacchi wrote:
> Sean, All,
> 
> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> This series tests raw nand flash in sandbox and fixes various bugs discovered in
>> the process. I've tried to do things in a contemporary manner, avoiding the
>> (numerous) variations present on only a few boards. The test is pretty minimal.
>> Future work could test the rest of the nand API as well as the MTD API.
>>
>> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
>> everything grows by a few bytes due to nand_page_size. A few boards grow more,
>> mostly those using nand_spl_loaders.c.
>>
>> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
>>
>>
>> Sean Anderson (15):
>>    spl: nand: Fix NULL-pointer dereference
>>    nand: Don't dereference NULL manufacturer_desc
>>    nand: Calculate SYS_NAND_PAGE_COUNT automatically
>>    nand: spl_loaders: Only read enough pages to load the image
>>    spl: legacy: Honor bl_len when decompressing
>>    spl: nand: Set bl_len to page size
>>    cmd: nand: Map memory before accessing it
>>    spl: nand: Map memory before accessing it
>>    mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
>>    mtd: Add some fallbacks for add/del_mtd_device
>>    nand: Add function to unregister NAND devices
>>    nand: Allow reinitialization
>>    arch: sandbox: Add function to create temporary files
>>    nand: Add sandbox driver
>>    test: spl: Add a test for NAND
>>
>>   README                                        |   9 +-
>>   arch/sandbox/cpu/os.c                         |  17 +
>>   arch/sandbox/dts/test.dts                     |  67 ++
>>   arch/sandbox/include/asm/spl.h                |   1 +
>>   cmd/nand.c                                    |  26 +-
>>   common/spl/Kconfig                            |   2 +-
>>   common/spl/spl_legacy.c                       |  18 +-
>>   common/spl/spl_nand.c                         |  22 +-
>>   configs/am335x_baltos_defconfig               |   3 +-
>>   configs/am335x_evm_defconfig                  |   3 +-
>>   configs/am335x_evm_spiboot_defconfig          |   2 +-
>>   configs/am335x_guardian_defconfig             |   1 -
>>   configs/am335x_hs_evm_defconfig               |   2 +-
>>   configs/am335x_hs_evm_uart_defconfig          |   2 +-
>>   configs/am335x_igep003x_defconfig             |   3 +-
>>   configs/am335x_sl50_defconfig                 |   2 +-
>>   configs/am3517_evm_defconfig                  |   3 +-
>>   configs/am43xx_evm_defconfig                  |   3 +-
>>   configs/am43xx_evm_rtconly_defconfig          |   3 +-
>>   configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
>>   configs/am43xx_hs_evm_defconfig               |   3 +-
>>   configs/am62ax_evm_r5_defconfig               |   2 +-
>>   configs/am65x_evm_a53_defconfig               |   2 +-
>>   configs/axm_defconfig                         |   1 -
>>   configs/chiliboard_defconfig                  |   1 -
>>   configs/cm_t43_defconfig                      |   2 +-
>>   configs/corvus_defconfig                      |   1 -
>>   configs/da850evm_nand_defconfig               |   1 -
>>   configs/devkit3250_defconfig                  |   1 -
>>   configs/devkit8000_defconfig                  |   1 -
>>   configs/dra7xx_evm_defconfig                  |   1 -
>>   configs/draco_defconfig                       |   1 -
>>   configs/etamin_defconfig                      |   1 -
>>   .../gardena-smart-gateway-at91sam_defconfig   |   1 -
>>   configs/igep00x0_defconfig                    |   3 +-
>>   configs/imx6ulz_smm_m2_defconfig              |   2 +-
>>   configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
>>   configs/j7200_evm_a72_defconfig               |   2 +-
>>   configs/j7200_evm_r5_defconfig                |   2 +-
>>   configs/j721e_evm_a72_defconfig               |   2 +-
>>   configs/j721e_evm_r5_defconfig                |   2 +-
>>   configs/j721s2_evm_a72_defconfig              |   2 +-
>>   configs/j721s2_evm_r5_defconfig               |   2 +-
>>   configs/m53menlo_defconfig                    |   1 -
>>   configs/omap35_logic_defconfig                |   3 +-
>>   configs/omap35_logic_somlv_defconfig          |   3 +-
>>   configs/omap3_beagle_defconfig                |   3 +-
>>   configs/omap3_evm_defconfig                   |   3 +-
>>   configs/omap3_logic_defconfig                 |   3 +-
>>   configs/omap3_logic_somlv_defconfig           |   3 +-
>>   configs/omapl138_lcdk_defconfig               |   1 -
>>   configs/phycore-am335x-r2-regor_defconfig     |   3 +-
>>   configs/phycore-am335x-r2-wega_defconfig      |   3 +-
>>   configs/pxm2_defconfig                        |   1 -
>>   configs/rastaban_defconfig                    |   1 -
>>   configs/rut_defconfig                         |   1 -
>>   configs/sama5d3_xplained_nandflash_defconfig  |   1 -
>>   configs/sama5d3xek_nandflash_defconfig        |   1 -
>>   configs/sama5d4_xplained_nandflash_defconfig  |   1 -
>>   configs/sama5d4ek_nandflash_defconfig         |   1 -
>>   configs/sandbox64_defconfig                   |  10 +-
>>   configs/sandbox_defconfig                     |   9 +
>>   configs/sandbox_noinst_defconfig              |  21 +-
>>   configs/smartweb_defconfig                    |   1 -
>>   configs/socfpga_secu1_defconfig               |   2 +-
>>   configs/stm32746g-eval_spl_defconfig          |   2 +-
>>   configs/stm32f746-disco_spl_defconfig         |   2 +-
>>   configs/stm32f769-disco_spl_defconfig         |   2 +-
>>   configs/stm32mp15_basic_defconfig             |   2 +-
>>   configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
>>   configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
>>   configs/taurus_defconfig                      |   1 -
>>   configs/thuban_defconfig                      |   1 -
>>   drivers/mtd/Makefile                          |   2 +-
>>   drivers/mtd/nand/raw/Kconfig                  |  27 +-
>>   drivers/mtd/nand/raw/Makefile                 |   1 +
>>   drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
>>   drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
>>   drivers/mtd/nand/raw/denali_spl.c             |   5 +
>>   drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
>>   drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
>>   drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
>>   drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
>>   drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
>>   drivers/mtd/nand/raw/nand.c                   |  66 +-
>>   drivers/mtd/nand/raw/nand_base.c              |   7 +-
>>   drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
>>   drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
>>   drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
>>   drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
>>   drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
>>   drivers/mtd/onenand/onenand_uboot.c           |   2 -
>>   include/linux/mtd/mtd.h                       |  12 +
>>   include/mtd/cfi_flash.h                       |   2 +-
>>   include/nand.h                                |   3 +
>>   include/os.h                                  |  13 +
>>   include/system-constants.h                    |   3 +
>>   test/dm/Makefile                              |   1 +
>>   test/dm/nand.c                                | 106 +++
>>   test/image/Kconfig                            |   9 +
>>   test/image/Makefile                           |   1 +
>>   test/image/spl_load_nand.c                    |  54 ++
>>   102 files changed, 1269 insertions(+), 153 deletions(-)
>>   create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>>   create mode 100644 test/dm/nand.c
>>   create mode 100644 test/image/spl_load_nand.c
>>
>> --
>> 2.37.1
>>
> 
> The CI pipeline fails:
> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
> 
> It seems like the problem is only for the clang tests (sandbox64 with
> clang test.py and sandbox with clang test.py)

I can't view that link. Can you post the error somewhere else?

I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.

--Sean

[1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355

    sandbox:  +   sandbox
+drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
+        default:
+        ^~~~~~~
+drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
+        memcpy(buf, src + chip->column, to_copy);
+                    ^~~
+drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
+        if (!chip->selected)
+            ^~~~~~~~~~~~~~~
+drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
+        ^~~~~~~~~~~~~~~~~~~~
+drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
+        const u8 *src;
+                     ^
+                      = NULL
+2 errors generated.
+make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
+make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
+make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
+make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
+make[1]: *** [Makefile:1858: drivers] Error 2
+make: *** [Makefile:177: sub-make] Error 2



^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-02 14:05   ` Sean Anderson
@ 2023-11-02 14:08     ` Dario Binacchi
  2023-11-02 14:13       ` Sean Anderson
  0 siblings, 1 reply; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 14:08 UTC (permalink / raw)
  To: Sean Anderson
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

On Thu, Nov 2, 2023 at 3:06 PM Sean Anderson <seanga2@gmail.com> wrote:
>
> On 11/2/23 10:01, Dario Binacchi wrote:
> > Sean, All,
> >
> > On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
> >>
> >> This series tests raw nand flash in sandbox and fixes various bugs discovered in
> >> the process. I've tried to do things in a contemporary manner, avoiding the
> >> (numerous) variations present on only a few boards. The test is pretty minimal.
> >> Future work could test the rest of the nand API as well as the MTD API.
> >>
> >> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
> >> everything grows by a few bytes due to nand_page_size. A few boards grow more,
> >> mostly those using nand_spl_loaders.c.
> >>
> >> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
> >>
> >>
> >> Sean Anderson (15):
> >>    spl: nand: Fix NULL-pointer dereference
> >>    nand: Don't dereference NULL manufacturer_desc
> >>    nand: Calculate SYS_NAND_PAGE_COUNT automatically
> >>    nand: spl_loaders: Only read enough pages to load the image
> >>    spl: legacy: Honor bl_len when decompressing
> >>    spl: nand: Set bl_len to page size
> >>    cmd: nand: Map memory before accessing it
> >>    spl: nand: Map memory before accessing it
> >>    mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
> >>    mtd: Add some fallbacks for add/del_mtd_device
> >>    nand: Add function to unregister NAND devices
> >>    nand: Allow reinitialization
> >>    arch: sandbox: Add function to create temporary files
> >>    nand: Add sandbox driver
> >>    test: spl: Add a test for NAND
> >>
> >>   README                                        |   9 +-
> >>   arch/sandbox/cpu/os.c                         |  17 +
> >>   arch/sandbox/dts/test.dts                     |  67 ++
> >>   arch/sandbox/include/asm/spl.h                |   1 +
> >>   cmd/nand.c                                    |  26 +-
> >>   common/spl/Kconfig                            |   2 +-
> >>   common/spl/spl_legacy.c                       |  18 +-
> >>   common/spl/spl_nand.c                         |  22 +-
> >>   configs/am335x_baltos_defconfig               |   3 +-
> >>   configs/am335x_evm_defconfig                  |   3 +-
> >>   configs/am335x_evm_spiboot_defconfig          |   2 +-
> >>   configs/am335x_guardian_defconfig             |   1 -
> >>   configs/am335x_hs_evm_defconfig               |   2 +-
> >>   configs/am335x_hs_evm_uart_defconfig          |   2 +-
> >>   configs/am335x_igep003x_defconfig             |   3 +-
> >>   configs/am335x_sl50_defconfig                 |   2 +-
> >>   configs/am3517_evm_defconfig                  |   3 +-
> >>   configs/am43xx_evm_defconfig                  |   3 +-
> >>   configs/am43xx_evm_rtconly_defconfig          |   3 +-
> >>   configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
> >>   configs/am43xx_hs_evm_defconfig               |   3 +-
> >>   configs/am62ax_evm_r5_defconfig               |   2 +-
> >>   configs/am65x_evm_a53_defconfig               |   2 +-
> >>   configs/axm_defconfig                         |   1 -
> >>   configs/chiliboard_defconfig                  |   1 -
> >>   configs/cm_t43_defconfig                      |   2 +-
> >>   configs/corvus_defconfig                      |   1 -
> >>   configs/da850evm_nand_defconfig               |   1 -
> >>   configs/devkit3250_defconfig                  |   1 -
> >>   configs/devkit8000_defconfig                  |   1 -
> >>   configs/dra7xx_evm_defconfig                  |   1 -
> >>   configs/draco_defconfig                       |   1 -
> >>   configs/etamin_defconfig                      |   1 -
> >>   .../gardena-smart-gateway-at91sam_defconfig   |   1 -
> >>   configs/igep00x0_defconfig                    |   3 +-
> >>   configs/imx6ulz_smm_m2_defconfig              |   2 +-
> >>   configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
> >>   configs/j7200_evm_a72_defconfig               |   2 +-
> >>   configs/j7200_evm_r5_defconfig                |   2 +-
> >>   configs/j721e_evm_a72_defconfig               |   2 +-
> >>   configs/j721e_evm_r5_defconfig                |   2 +-
> >>   configs/j721s2_evm_a72_defconfig              |   2 +-
> >>   configs/j721s2_evm_r5_defconfig               |   2 +-
> >>   configs/m53menlo_defconfig                    |   1 -
> >>   configs/omap35_logic_defconfig                |   3 +-
> >>   configs/omap35_logic_somlv_defconfig          |   3 +-
> >>   configs/omap3_beagle_defconfig                |   3 +-
> >>   configs/omap3_evm_defconfig                   |   3 +-
> >>   configs/omap3_logic_defconfig                 |   3 +-
> >>   configs/omap3_logic_somlv_defconfig           |   3 +-
> >>   configs/omapl138_lcdk_defconfig               |   1 -
> >>   configs/phycore-am335x-r2-regor_defconfig     |   3 +-
> >>   configs/phycore-am335x-r2-wega_defconfig      |   3 +-
> >>   configs/pxm2_defconfig                        |   1 -
> >>   configs/rastaban_defconfig                    |   1 -
> >>   configs/rut_defconfig                         |   1 -
> >>   configs/sama5d3_xplained_nandflash_defconfig  |   1 -
> >>   configs/sama5d3xek_nandflash_defconfig        |   1 -
> >>   configs/sama5d4_xplained_nandflash_defconfig  |   1 -
> >>   configs/sama5d4ek_nandflash_defconfig         |   1 -
> >>   configs/sandbox64_defconfig                   |  10 +-
> >>   configs/sandbox_defconfig                     |   9 +
> >>   configs/sandbox_noinst_defconfig              |  21 +-
> >>   configs/smartweb_defconfig                    |   1 -
> >>   configs/socfpga_secu1_defconfig               |   2 +-
> >>   configs/stm32746g-eval_spl_defconfig          |   2 +-
> >>   configs/stm32f746-disco_spl_defconfig         |   2 +-
> >>   configs/stm32f769-disco_spl_defconfig         |   2 +-
> >>   configs/stm32mp15_basic_defconfig             |   2 +-
> >>   configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
> >>   configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
> >>   configs/taurus_defconfig                      |   1 -
> >>   configs/thuban_defconfig                      |   1 -
> >>   drivers/mtd/Makefile                          |   2 +-
> >>   drivers/mtd/nand/raw/Kconfig                  |  27 +-
> >>   drivers/mtd/nand/raw/Makefile                 |   1 +
> >>   drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
> >>   drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
> >>   drivers/mtd/nand/raw/denali_spl.c             |   5 +
> >>   drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
> >>   drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
> >>   drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
> >>   drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
> >>   drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
> >>   drivers/mtd/nand/raw/nand.c                   |  66 +-
> >>   drivers/mtd/nand/raw/nand_base.c              |   7 +-
> >>   drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
> >>   drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
> >>   drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
> >>   drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
> >>   drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
> >>   drivers/mtd/onenand/onenand_uboot.c           |   2 -
> >>   include/linux/mtd/mtd.h                       |  12 +
> >>   include/mtd/cfi_flash.h                       |   2 +-
> >>   include/nand.h                                |   3 +
> >>   include/os.h                                  |  13 +
> >>   include/system-constants.h                    |   3 +
> >>   test/dm/Makefile                              |   1 +
> >>   test/dm/nand.c                                | 106 +++
> >>   test/image/Kconfig                            |   9 +
> >>   test/image/Makefile                           |   1 +
> >>   test/image/spl_load_nand.c                    |  54 ++
> >>   102 files changed, 1269 insertions(+), 153 deletions(-)
> >>   create mode 100644 drivers/mtd/nand/raw/sand_nand.c
> >>   create mode 100644 test/dm/nand.c
> >>   create mode 100644 test/image/spl_load_nand.c
> >>
> >> --
> >> 2.37.1
> >>
> >
> > The CI pipeline fails:
> > https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
> >
> > It seems like the problem is only for the clang tests (sandbox64 with
> > clang test.py and sandbox with clang test.py)
>
> I can't view that link. Can you post the error somewhere else?
>
> I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.
>
> --Sean
>
> [1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355
>
>     sandbox:  +   sandbox
> +drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
> +        default:
> +        ^~~~~~~
> +drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
> +        memcpy(buf, src + chip->column, to_copy);
> +                    ^~~
> +drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
> +        if (!chip->selected)
> +            ^~~~~~~~~~~~~~~
> +drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
> +        ^~~~~~~~~~~~~~~~~~~~
> +drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
> +        const u8 *src;
> +                     ^
> +                      = NULL
> +2 errors generated.
> +make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
> +make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
> +make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
> +make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
> +make[1]: *** [Makefile:1858: drivers] Error 2
> +make: *** [Makefile:177: sub-make] Error 2
>
>

 [0KRunning with gitlab-runner 16.5.0 (853330f9) [0;m
 [0K  on kaka wgFtTy4z, system ID: s_7c66e37ef434 [0;m
section_start:1698925804:prepare_executor
 [0K [0K [36;1mPreparing the "docker" executor [0;m [0;m
 [0KUsing Docker executor with image
docker.io/trini/u-boot-gitlab-ci-runner:jammy-20230804-25Aug2023
... [0;m
 [0KPulling docker image
docker.io/trini/u-boot-gitlab-ci-runner:jammy-20230804-25Aug2023
... [0;m
 [0KUsing docker image
sha256:1744666558bd76db2a46ea8d0e55b253c7570f134aff9b7e8fc91e13d99bbecc
for docker.io/trini/u-boot-gitlab-ci-runner:jammy-20230804-25Aug2023
with digest trini/u-boot-gitlab-ci-runner@sha256:42925546cba6b1b0e819e91fb0269f0b700d72e4a2a5faf5d3102ec130fb859d
... [0;m
section_end:1698925807:prepare_executor
 [0Ksection_start:1698925807:prepare_script
 [0K [0K [36;1mPreparing environment [0;m [0;m
Running on runner-wgftty4z-project-568-concurrent-0 via kaka...
section_end:1698925808:prepare_script
 [0Ksection_start:1698925808:get_sources
 [0K [0K [36;1mGetting source from Git repository [0;m [0;m
 [32;1mFetching changes... [0;m
Reinitialized existing Git repository in
/builds/u-boot/custodians/u-boot-nand-flash/.git/
 [32;1mChecking out 94b91f7c as detached HEAD (ref is nand-next)... [0;m
Removing multiplexed_log.css
Removing test-log.html
Removing tools/buildman/__pycache__/
Removing tools/patman/__pycache__/
Removing tools/u_boot_pylib/__pycache__/

 [32;1mSkipping Git submodules setup [0;m
section_end:1698925811:get_sources
 [0Ksection_start:1698925811:step_script
 [0K [0K [36;1mExecuting "step_script" stage of the job script [0;m [0;m
 [0KUsing docker image
sha256:1744666558bd76db2a46ea8d0e55b253c7570f134aff9b7e8fc91e13d99bbecc
for docker.io/trini/u-boot-gitlab-ci-runner:jammy-20230804-25Aug2023
with digest trini/u-boot-gitlab-ci-runner@sha256:42925546cba6b1b0e819e91fb0269f0b700d72e4a2a5faf5d3102ec130fb859d
... [0;m
 [32;1m$ git config --global --add safe.directory "${CI_PROJECT_DIR}" [0;m
 [32;1m$ git clone --depth=1
https://source.denx.de/u-boot/u-boot-test-hooks
/tmp/uboot-test-hooks [0;m
Cloning into '/tmp/uboot-test-hooks'...
warning: redirecting to https://source.denx.de/u-boot/u-boot-test-hooks.git/
 [32;1m$ ln -s travis-ci /tmp/uboot-test-hooks/bin/`hostname` [0;m
 [32;1m$ ln -s travis-ci /tmp/uboot-test-hooks/py/`hostname` [0;m
 [32;1m$ grub-mkimage --prefix="" -o ~/grub_x86.efi -O i386-efi normal
 echo lsefimmap lsefi lsefisystab efinet tftp minicmd [0;m
 [32;1m$ grub-mkimage --prefix="" -o ~/grub_x64.efi -O x86_64-efi
normal  echo lsefimmap lsefi lsefisystab efinet tftp minicmd [0;m
 [32;1m$ if [[ "${TEST_PY_BD}" == "qemu-riscv32_spl" ]]; then wget -O
- https://github.com/riscv-software-src/opensbi/releases/download/v1.2/opensbi-1.2-rv-bin.tar.xz
| tar -C /tmp -xJ; export
OPENSBI=/tmp/opensbi-1.2-rv-bin/share/opensbi/ilp32/generic/firmware/fw_dynamic.bin;
fi [0;m
 [32;1m$ if [[ "${TEST_PY_BD}" == "qemu-riscv64_spl" ]] || [[
"${TEST_PY_BD}" == "sifive_unleashed" ]]; then wget -O -
https://github.com/riscv-software-src/opensbi/releases/download/v1.2/opensbi-1.2-rv-bin.tar.xz
| tar -C /tmp -xJ; export
OPENSBI=/tmp/opensbi-1.2-rv-bin/share/opensbi/lp64/generic/firmware/fw_dynamic.bin;
fi [0;m
 [32;1m$ export UBOOT_TRAVIS_BUILD_DIR=/tmp/${TEST_PY_BD} [0;m
 [32;1m$ echo BUILD_ENV ${BUILD_ENV} [0;m
BUILD_ENV
 [32;1m$ if [ -n "${BUILD_ENV}" ]; then export ${BUILD_ENV}; fi [0;m
 [32;1m$ tools/buildman/buildman -o ${UBOOT_TRAVIS_BUILD_DIR} -w -E -W
-e --board ${TEST_PY_BD} ${OVERRIDE} [0;m
Building current source for 1 boards (1 thread, 32 jobs per thread)

Starting build...


    0    0    0 /1       -1      (starting)

   sandbox:  +   sandbox64
+test/dm/nand.c:83:2: error: expression result unused [-Werror,-Wunused-value]
+        ut_assertok(nand_erase_opts(mtd, &opts));
+        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+include/test/ut.h:309:27: note: expanded from macro 'ut_assertok'
+#define ut_assertok(cond)       ut_asserteq(0, cond)
+                                ^~~~~~~~~~~~~~~~~~~~
+include/test/ut.h:162:2: note: expanded from macro 'ut_asserteq'
+        __ret;                                                          \
+        ^~~~~
+1 error generated.
+make[3]: *** [scripts/Makefile.build:257: test/dm/nand.o] Error 1
+make[2]: *** [scripts/Makefile.build:397: test/dm] Error 2
+make[1]: *** [Makefile:1858: test] Error 2
+make: *** [Makefile:177: sub-make] Error 2

    0    0    1 /1              sandbox64
Completed: 1 total built, 1 newly), duration 0:00:10, rate 0.10
section_end:1698925825:step_script
 [0Ksection_start:1698925825:after_script
 [0K [0K [36;1mRunning after_script [0;m [0;m
 [32;1mRunning after script... [0;m
 [32;1m$ cp -v /tmp/${TEST_PY_BD}/*.{html,css} . [0;m
cp: cannot stat '/tmp/sandbox64/*.html': No such file or directory
cp: cannot stat '/tmp/sandbox64/*.css': No such file or directory
section_end:1698925826:after_script
 [0K [0;33mWARNING: after_script failed, but job will continue
unaffected: exit code 1 [0;m
section_start:1698925826:upload_artifacts_on_failure
 [0K [0K [36;1mUploading artifacts for failed job [0;m [0;m
 [32;1mUploading artifacts... [0;m
 [0;33mWARNING: *.html: no matching files. Ensure that the artifact
path is relative to the working directory
(/builds/u-boot/custodians/u-boot-nand-flash) [0;m
 [0;33mWARNING: *.css: no matching files. Ensure that the artifact
path is relative to the working directory
(/builds/u-boot/custodians/u-boot-nand-flash) [0;m
 [31;1mERROR: No files to upload                          [0;m
section_end:1698925827:upload_artifacts_on_failure
 [0Ksection_start:1698925827:cleanup_file_variables
 [0K [0K [36;1mCleaning up project directory and file based variables [0;m [0;m
section_end:1698925827:cleanup_file_variables
 [0K [31;1mERROR: Job failed: exit code 1
 [0;m

Thanks and regards,
Dario

-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-02 14:08     ` Dario Binacchi
@ 2023-11-02 14:13       ` Sean Anderson
  2023-11-02 14:18         ` Dario Binacchi
  0 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-11-02 14:13 UTC (permalink / raw)
  To: Dario Binacchi
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

On 11/2/23 10:08, Dario Binacchi wrote:
> On Thu, Nov 2, 2023 at 3:06 PM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> On 11/2/23 10:01, Dario Binacchi wrote:
>>> Sean, All,
>>>
>>> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>>>>
>>>> This series tests raw nand flash in sandbox and fixes various bugs discovered in
>>>> the process. I've tried to do things in a contemporary manner, avoiding the
>>>> (numerous) variations present on only a few boards. The test is pretty minimal.
>>>> Future work could test the rest of the nand API as well as the MTD API.
>>>>
>>>> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
>>>> everything grows by a few bytes due to nand_page_size. A few boards grow more,
>>>> mostly those using nand_spl_loaders.c.
>>>>
>>>> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
>>>>
>>>>
>>>> Sean Anderson (15):
>>>>     spl: nand: Fix NULL-pointer dereference
>>>>     nand: Don't dereference NULL manufacturer_desc
>>>>     nand: Calculate SYS_NAND_PAGE_COUNT automatically
>>>>     nand: spl_loaders: Only read enough pages to load the image
>>>>     spl: legacy: Honor bl_len when decompressing
>>>>     spl: nand: Set bl_len to page size
>>>>     cmd: nand: Map memory before accessing it
>>>>     spl: nand: Map memory before accessing it
>>>>     mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
>>>>     mtd: Add some fallbacks for add/del_mtd_device
>>>>     nand: Add function to unregister NAND devices
>>>>     nand: Allow reinitialization
>>>>     arch: sandbox: Add function to create temporary files
>>>>     nand: Add sandbox driver
>>>>     test: spl: Add a test for NAND
>>>>
>>>>    README                                        |   9 +-
>>>>    arch/sandbox/cpu/os.c                         |  17 +
>>>>    arch/sandbox/dts/test.dts                     |  67 ++
>>>>    arch/sandbox/include/asm/spl.h                |   1 +
>>>>    cmd/nand.c                                    |  26 +-
>>>>    common/spl/Kconfig                            |   2 +-
>>>>    common/spl/spl_legacy.c                       |  18 +-
>>>>    common/spl/spl_nand.c                         |  22 +-
>>>>    configs/am335x_baltos_defconfig               |   3 +-
>>>>    configs/am335x_evm_defconfig                  |   3 +-
>>>>    configs/am335x_evm_spiboot_defconfig          |   2 +-
>>>>    configs/am335x_guardian_defconfig             |   1 -
>>>>    configs/am335x_hs_evm_defconfig               |   2 +-
>>>>    configs/am335x_hs_evm_uart_defconfig          |   2 +-
>>>>    configs/am335x_igep003x_defconfig             |   3 +-
>>>>    configs/am335x_sl50_defconfig                 |   2 +-
>>>>    configs/am3517_evm_defconfig                  |   3 +-
>>>>    configs/am43xx_evm_defconfig                  |   3 +-
>>>>    configs/am43xx_evm_rtconly_defconfig          |   3 +-
>>>>    configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
>>>>    configs/am43xx_hs_evm_defconfig               |   3 +-
>>>>    configs/am62ax_evm_r5_defconfig               |   2 +-
>>>>    configs/am65x_evm_a53_defconfig               |   2 +-
>>>>    configs/axm_defconfig                         |   1 -
>>>>    configs/chiliboard_defconfig                  |   1 -
>>>>    configs/cm_t43_defconfig                      |   2 +-
>>>>    configs/corvus_defconfig                      |   1 -
>>>>    configs/da850evm_nand_defconfig               |   1 -
>>>>    configs/devkit3250_defconfig                  |   1 -
>>>>    configs/devkit8000_defconfig                  |   1 -
>>>>    configs/dra7xx_evm_defconfig                  |   1 -
>>>>    configs/draco_defconfig                       |   1 -
>>>>    configs/etamin_defconfig                      |   1 -
>>>>    .../gardena-smart-gateway-at91sam_defconfig   |   1 -
>>>>    configs/igep00x0_defconfig                    |   3 +-
>>>>    configs/imx6ulz_smm_m2_defconfig              |   2 +-
>>>>    configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
>>>>    configs/j7200_evm_a72_defconfig               |   2 +-
>>>>    configs/j7200_evm_r5_defconfig                |   2 +-
>>>>    configs/j721e_evm_a72_defconfig               |   2 +-
>>>>    configs/j721e_evm_r5_defconfig                |   2 +-
>>>>    configs/j721s2_evm_a72_defconfig              |   2 +-
>>>>    configs/j721s2_evm_r5_defconfig               |   2 +-
>>>>    configs/m53menlo_defconfig                    |   1 -
>>>>    configs/omap35_logic_defconfig                |   3 +-
>>>>    configs/omap35_logic_somlv_defconfig          |   3 +-
>>>>    configs/omap3_beagle_defconfig                |   3 +-
>>>>    configs/omap3_evm_defconfig                   |   3 +-
>>>>    configs/omap3_logic_defconfig                 |   3 +-
>>>>    configs/omap3_logic_somlv_defconfig           |   3 +-
>>>>    configs/omapl138_lcdk_defconfig               |   1 -
>>>>    configs/phycore-am335x-r2-regor_defconfig     |   3 +-
>>>>    configs/phycore-am335x-r2-wega_defconfig      |   3 +-
>>>>    configs/pxm2_defconfig                        |   1 -
>>>>    configs/rastaban_defconfig                    |   1 -
>>>>    configs/rut_defconfig                         |   1 -
>>>>    configs/sama5d3_xplained_nandflash_defconfig  |   1 -
>>>>    configs/sama5d3xek_nandflash_defconfig        |   1 -
>>>>    configs/sama5d4_xplained_nandflash_defconfig  |   1 -
>>>>    configs/sama5d4ek_nandflash_defconfig         |   1 -
>>>>    configs/sandbox64_defconfig                   |  10 +-
>>>>    configs/sandbox_defconfig                     |   9 +
>>>>    configs/sandbox_noinst_defconfig              |  21 +-
>>>>    configs/smartweb_defconfig                    |   1 -
>>>>    configs/socfpga_secu1_defconfig               |   2 +-
>>>>    configs/stm32746g-eval_spl_defconfig          |   2 +-
>>>>    configs/stm32f746-disco_spl_defconfig         |   2 +-
>>>>    configs/stm32f769-disco_spl_defconfig         |   2 +-
>>>>    configs/stm32mp15_basic_defconfig             |   2 +-
>>>>    configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
>>>>    configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
>>>>    configs/taurus_defconfig                      |   1 -
>>>>    configs/thuban_defconfig                      |   1 -
>>>>    drivers/mtd/Makefile                          |   2 +-
>>>>    drivers/mtd/nand/raw/Kconfig                  |  27 +-
>>>>    drivers/mtd/nand/raw/Makefile                 |   1 +
>>>>    drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
>>>>    drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
>>>>    drivers/mtd/nand/raw/denali_spl.c             |   5 +
>>>>    drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
>>>>    drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
>>>>    drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
>>>>    drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
>>>>    drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
>>>>    drivers/mtd/nand/raw/nand.c                   |  66 +-
>>>>    drivers/mtd/nand/raw/nand_base.c              |   7 +-
>>>>    drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
>>>>    drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
>>>>    drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
>>>>    drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
>>>>    drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
>>>>    drivers/mtd/onenand/onenand_uboot.c           |   2 -
>>>>    include/linux/mtd/mtd.h                       |  12 +
>>>>    include/mtd/cfi_flash.h                       |   2 +-
>>>>    include/nand.h                                |   3 +
>>>>    include/os.h                                  |  13 +
>>>>    include/system-constants.h                    |   3 +
>>>>    test/dm/Makefile                              |   1 +
>>>>    test/dm/nand.c                                | 106 +++
>>>>    test/image/Kconfig                            |   9 +
>>>>    test/image/Makefile                           |   1 +
>>>>    test/image/spl_load_nand.c                    |  54 ++
>>>>    102 files changed, 1269 insertions(+), 153 deletions(-)
>>>>    create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>>>>    create mode 100644 test/dm/nand.c
>>>>    create mode 100644 test/image/spl_load_nand.c
>>>>
>>>> --
>>>> 2.37.1
>>>>
>>>
>>> The CI pipeline fails:
>>> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
>>>
>>> It seems like the problem is only for the clang tests (sandbox64 with
>>> clang test.py and sandbox with clang test.py)
>>
>> I can't view that link. Can you post the error somewhere else?
>>
>> I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.
>>
>> --Sean
>>
>> [1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355
>>
>>      sandbox:  +   sandbox
>> +drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
>> +        default:
>> +        ^~~~~~~
>> +drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
>> +        memcpy(buf, src + chip->column, to_copy);
>> +                    ^~~
>> +drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
>> +        if (!chip->selected)
>> +            ^~~~~~~~~~~~~~~
>> +drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
>> +        ^~~~~~~~~~~~~~~~~~~~
>> +drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
>> +        const u8 *src;
>> +                     ^
>> +                      = NULL
>> +2 errors generated.
>> +make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
>> +make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
>> +make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
>> +make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
>> +make[1]: *** [Makefile:1858: drivers] Error 2
>> +make: *** [Makefile:177: sub-make] Error 2
>>
>>
> 
> +test/dm/nand.c:83:2: error: expression result unused [-Werror,-Wunused-value]
> +        ut_assertok(nand_erase_opts(mtd, &opts));
> +        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +include/test/ut.h:309:27: note: expanded from macro 'ut_assertok'
> +#define ut_assertok(cond)       ut_asserteq(0, cond)
> +                                ^~~~~~~~~~~~~~~~~~~~
> +include/test/ut.h:162:2: note: expanded from macro 'ut_asserteq'
> +        __ret;                                                          \
> +        ^~~~~
> +1 error generated.
> +make[3]: *** [scripts/Makefile.build:257: test/dm/nand.o] Error 1
> +make[2]: *** [scripts/Makefile.build:397: test/dm] Error 2
> +make[1]: *** [Makefile:1858: test] Error 2
> +make: *** [Makefile:177: sub-make] Error 2

...well that's a new one. Wonder why it doesn't complain about all the other uses of
ut_assertok (which, invariably, discard the value in the same way).

--Sean


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically
  2023-11-02  9:53   ` Dario Binacchi
@ 2023-11-02 14:17     ` Sean Anderson
  2023-11-02 14:19       ` Dario Binacchi
  0 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-11-02 14:17 UTC (permalink / raw)
  To: Dario Binacchi; +Cc: u-boot, Michael Trimarchi, Tom Rini

On 11/2/23 05:53, Dario Binacchi wrote:
> Sean, All
> 
> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> Contrary to what the help message says, this is the number of pages per
>> block. Calculate it automatically based on SYS_NAND_BLOCK_SIZE and
>> SYS_NAND_PAGE_SIZE.
>>
>> Signed-off-by: Sean Anderson <seanga2@gmail.com>
>> ---
>>
>>   README                                          | 9 ++++-----
>>   configs/am335x_baltos_defconfig                 | 1 -
>>   configs/am335x_evm_defconfig                    | 1 -
>>   configs/am335x_guardian_defconfig               | 1 -
>>   configs/am335x_igep003x_defconfig               | 1 -
>>   configs/am3517_evm_defconfig                    | 1 -
>>   configs/am43xx_evm_defconfig                    | 1 -
>>   configs/am43xx_evm_rtconly_defconfig            | 1 -
>>   configs/am43xx_evm_usbhost_boot_defconfig       | 1 -
>>   configs/am43xx_hs_evm_defconfig                 | 1 -
>>   configs/axm_defconfig                           | 1 -
>>   configs/chiliboard_defconfig                    | 1 -
>>   configs/corvus_defconfig                        | 1 -
>>   configs/da850evm_nand_defconfig                 | 1 -
>>   configs/devkit3250_defconfig                    | 1 -
>>   configs/devkit8000_defconfig                    | 1 -
>>   configs/dra7xx_evm_defconfig                    | 1 -
>>   configs/draco_defconfig                         | 1 -
>>   configs/etamin_defconfig                        | 1 -
>>   configs/gardena-smart-gateway-at91sam_defconfig | 1 -
>>   configs/igep00x0_defconfig                      | 1 -
>>   configs/m53menlo_defconfig                      | 1 -
>>   configs/omap35_logic_defconfig                  | 1 -
>>   configs/omap35_logic_somlv_defconfig            | 1 -
>>   configs/omap3_beagle_defconfig                  | 1 -
>>   configs/omap3_evm_defconfig                     | 1 -
>>   configs/omap3_logic_defconfig                   | 1 -
>>   configs/omap3_logic_somlv_defconfig             | 1 -
>>   configs/omapl138_lcdk_defconfig                 | 1 -
>>   configs/phycore-am335x-r2-regor_defconfig       | 1 -
>>   configs/phycore-am335x-r2-wega_defconfig        | 1 -
>>   configs/pxm2_defconfig                          | 1 -
>>   configs/rastaban_defconfig                      | 1 -
>>   configs/rut_defconfig                           | 1 -
>>   configs/sama5d3_xplained_nandflash_defconfig    | 1 -
>>   configs/sama5d3xek_nandflash_defconfig          | 1 -
>>   configs/sama5d4_xplained_nandflash_defconfig    | 1 -
>>   configs/sama5d4ek_nandflash_defconfig           | 1 -
>>   configs/smartweb_defconfig                      | 1 -
>>   configs/taurus_defconfig                        | 1 -
>>   configs/thuban_defconfig                        | 1 -
>>   drivers/mtd/nand/raw/Kconfig                    | 8 --------
>>   drivers/mtd/nand/raw/am335x_spl_bch.c           | 3 ++-
>>   drivers/mtd/nand/raw/atmel_nand.c               | 5 +++--
>>   drivers/mtd/nand/raw/mxc_nand_spl.c             | 5 +++--
>>   drivers/mtd/nand/raw/nand_spl_loaders.c         | 2 +-
>>   drivers/mtd/nand/raw/nand_spl_simple.c          | 5 +++--
>>   drivers/mtd/nand/raw/omap_gpmc.c                | 3 ++-
>>   include/system-constants.h                      | 3 +++
>>   49 files changed, 21 insertions(+), 62 deletions(-)
>>
>> diff --git a/README b/README
>> index 60c6b8a19db..00d422737fb 100644
>> --- a/README
>> +++ b/README
>> @@ -1191,11 +1191,10 @@ The following options need to be configured:
>>                  Support for a lightweight UBI (fastmap) scanner and
>>                  loader
>>
>> -               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_COUNT,
>> -               CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE,
>> -               CONFIG_SYS_NAND_BLOCK_SIZE, CONFIG_SYS_NAND_BAD_BLOCK_POS,
>> -               CFG_SYS_NAND_ECCPOS, CFG_SYS_NAND_ECCSIZE,
>> -               CFG_SYS_NAND_ECCBYTES
>> +               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_SIZE,
>> +               CONFIG_SYS_NAND_OOBSIZE, CONFIG_SYS_NAND_BLOCK_SIZE,
>> +               CONFIG_SYS_NAND_BAD_BLOCK_POS, CFG_SYS_NAND_ECCPOS,
>> +               CFG_SYS_NAND_ECCSIZE, CFG_SYS_NAND_ECCBYTES
>>                  Defines the size and behavior of the NAND that SPL uses
>>                  to read U-Boot
>>
>> diff --git a/configs/am335x_baltos_defconfig b/configs/am335x_baltos_defconfig
>> index 16993ef5386..3891e48e9c7 100644
>> --- a/configs/am335x_baltos_defconfig
>> +++ b/configs/am335x_baltos_defconfig
>> @@ -67,7 +67,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am335x_evm_defconfig b/configs/am335x_evm_defconfig
>> index f048e60f7f3..73f221d55bc 100644
>> --- a/configs/am335x_evm_defconfig
>> +++ b/configs/am335x_evm_defconfig
>> @@ -84,7 +84,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am335x_guardian_defconfig b/configs/am335x_guardian_defconfig
>> index 01d848ceede..5369e46bc1b 100644
>> --- a/configs/am335x_guardian_defconfig
>> +++ b/configs/am335x_guardian_defconfig
>> @@ -99,7 +99,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0x100
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am335x_igep003x_defconfig b/configs/am335x_igep003x_defconfig
>> index 4dd6366ef67..4c5c82f9bdd 100644
>> --- a/configs/am335x_igep003x_defconfig
>> +++ b/configs/am335x_igep003x_defconfig
>> @@ -85,7 +85,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_MTD_UBI_FASTMAP=y
>> diff --git a/configs/am3517_evm_defconfig b/configs/am3517_evm_defconfig
>> index 0a83ac9378c..111929fb912 100644
>> --- a/configs/am3517_evm_defconfig
>> +++ b/configs/am3517_evm_defconfig
>> @@ -75,7 +75,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/am43xx_evm_defconfig b/configs/am43xx_evm_defconfig
>> index 6571afd345e..5f2356bcd32 100644
>> --- a/configs/am43xx_evm_defconfig
>> +++ b/configs/am43xx_evm_defconfig
>> @@ -72,7 +72,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am43xx_evm_rtconly_defconfig b/configs/am43xx_evm_rtconly_defconfig
>> index e84aed4d54d..30681e7eeb6 100644
>> --- a/configs/am43xx_evm_rtconly_defconfig
>> +++ b/configs/am43xx_evm_rtconly_defconfig
>> @@ -62,7 +62,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am43xx_evm_usbhost_boot_defconfig b/configs/am43xx_evm_usbhost_boot_defconfig
>> index 0cae3242b04..6c4cc99e5ff 100644
>> --- a/configs/am43xx_evm_usbhost_boot_defconfig
>> +++ b/configs/am43xx_evm_usbhost_boot_defconfig
>> @@ -75,7 +75,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/am43xx_hs_evm_defconfig b/configs/am43xx_hs_evm_defconfig
>> index 370ee967258..49ecb4c9e66 100644
>> --- a/configs/am43xx_hs_evm_defconfig
>> +++ b/configs/am43xx_hs_evm_defconfig
>> @@ -68,7 +68,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/axm_defconfig b/configs/axm_defconfig
>> index f9c02144c4a..e1a01b24b88 100644
>> --- a/configs/axm_defconfig
>> +++ b/configs/axm_defconfig
>> @@ -86,7 +86,6 @@ CONFIG_MTD=y
>>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>>   CONFIG_NAND_ATMEL=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/chiliboard_defconfig b/configs/chiliboard_defconfig
>> index 06642d282d1..0cd649d643a 100644
>> --- a/configs/chiliboard_defconfig
>> +++ b/configs/chiliboard_defconfig
>> @@ -63,7 +63,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/corvus_defconfig b/configs/corvus_defconfig
>> index ecf61f0d1e6..5eaa6dbb5fc 100644
>> --- a/configs/corvus_defconfig
>> +++ b/configs/corvus_defconfig
>> @@ -81,7 +81,6 @@ CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT=y
>>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>>   CONFIG_NAND_ATMEL=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/da850evm_nand_defconfig b/configs/da850evm_nand_defconfig
>> index 5f6f5d788d0..62b8edde248 100644
>> --- a/configs/da850evm_nand_defconfig
>> +++ b/configs/da850evm_nand_defconfig
>> @@ -88,7 +88,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
>>   CONFIG_NAND_DAVINCI=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/devkit3250_defconfig b/configs/devkit3250_defconfig
>> index ec4031da51d..569b156c997 100644
>> --- a/configs/devkit3250_defconfig
>> +++ b/configs/devkit3250_defconfig
>> @@ -73,7 +73,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
>>   CONFIG_NAND_LPC32XX_SLC=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   # CONFIG_SYS_NAND_5_ADDR_CYCLE is not set
>> diff --git a/configs/devkit8000_defconfig b/configs/devkit8000_defconfig
>> index ef16da176a9..a7f82442eb0 100644
>> --- a/configs/devkit8000_defconfig
>> +++ b/configs/devkit8000_defconfig
>> @@ -77,7 +77,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/dra7xx_evm_defconfig b/configs/dra7xx_evm_defconfig
>> index 19ca89fa867..c3a3ec2cd42 100644
>> --- a/configs/dra7xx_evm_defconfig
>> +++ b/configs/dra7xx_evm_defconfig
>> @@ -104,7 +104,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/draco_defconfig b/configs/draco_defconfig
>> index ee19920a703..2a82087ab31 100644
>> --- a/configs/draco_defconfig
>> +++ b/configs/draco_defconfig
>> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/etamin_defconfig b/configs/etamin_defconfig
>> index c0ce7a33569..4ac0abc56b0 100644
>> --- a/configs/etamin_defconfig
>> +++ b/configs/etamin_defconfig
>> @@ -104,7 +104,6 @@ CONFIG_SYS_MAX_NAND_DEVICE=3
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x80000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x80
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/gardena-smart-gateway-at91sam_defconfig b/configs/gardena-smart-gateway-at91sam_defconfig
>> index 439fcc05753..0fb92ff1638 100644
>> --- a/configs/gardena-smart-gateway-at91sam_defconfig
>> +++ b/configs/gardena-smart-gateway-at91sam_defconfig
>> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>>   CONFIG_NAND_ATMEL=y
>>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/igep00x0_defconfig b/configs/igep00x0_defconfig
>> index 993bbe29c6b..3b1af07f057 100644
>> --- a/configs/igep00x0_defconfig
>> +++ b/configs/igep00x0_defconfig
>> @@ -78,7 +78,6 @@ CONFIG_SYS_MTDPARTS_RUNTIME=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/m53menlo_defconfig b/configs/m53menlo_defconfig
>> index ef9e15d1520..e1ddc0af5db 100644
>> --- a/configs/m53menlo_defconfig
>> +++ b/configs/m53menlo_defconfig
>> @@ -90,7 +90,6 @@ CONFIG_SYS_NAND_USE_FLASH_BBT=y
>>   CONFIG_NAND_MXC=y
>>   CONFIG_MXC_NAND_HWECC=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
>> index 539b0cfa4f7..3a656072534 100644
>> --- a/configs/omap35_logic_defconfig
>> +++ b/configs/omap35_logic_defconfig
>> @@ -74,7 +74,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omap35_logic_somlv_defconfig b/configs/omap35_logic_somlv_defconfig
>> index f14ce3d23db..868e89114b7 100644
>> --- a/configs/omap35_logic_somlv_defconfig
>> +++ b/configs/omap35_logic_somlv_defconfig
>> @@ -83,7 +83,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omap3_beagle_defconfig b/configs/omap3_beagle_defconfig
>> index cabd3659385..7f0b927528d 100644
>> --- a/configs/omap3_beagle_defconfig
>> +++ b/configs/omap3_beagle_defconfig
>> @@ -83,7 +83,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omap3_evm_defconfig b/configs/omap3_evm_defconfig
>> index 33ff39ff548..3434783d45b 100644
>> --- a/configs/omap3_evm_defconfig
>> +++ b/configs/omap3_evm_defconfig
>> @@ -74,7 +74,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
>> index 7d3d602c9d9..78bfbbd1a22 100644
>> --- a/configs/omap3_logic_defconfig
>> +++ b/configs/omap3_logic_defconfig
>> @@ -73,7 +73,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omap3_logic_somlv_defconfig b/configs/omap3_logic_somlv_defconfig
>> index 0141a425e07..aefd8861db3 100644
>> --- a/configs/omap3_logic_somlv_defconfig
>> +++ b/configs/omap3_logic_somlv_defconfig
>> @@ -84,7 +84,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/omapl138_lcdk_defconfig b/configs/omapl138_lcdk_defconfig
>> index 47feff21bd4..63554d56016 100644
>> --- a/configs/omapl138_lcdk_defconfig
>> +++ b/configs/omapl138_lcdk_defconfig
>> @@ -84,7 +84,6 @@ CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
>>   CONFIG_NAND_DAVINCI=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
>> diff --git a/configs/phycore-am335x-r2-regor_defconfig b/configs/phycore-am335x-r2-regor_defconfig
>> index 4bdf8822181..85d53d06357 100644
>> --- a/configs/phycore-am335x-r2-regor_defconfig
>> +++ b/configs/phycore-am335x-r2-regor_defconfig
>> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/phycore-am335x-r2-wega_defconfig b/configs/phycore-am335x-r2-wega_defconfig
>> index 7644fbec273..60607548a25 100644
>> --- a/configs/phycore-am335x-r2-wega_defconfig
>> +++ b/configs/phycore-am335x-r2-wega_defconfig
>> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/pxm2_defconfig b/configs/pxm2_defconfig
>> index 2ea007d0de5..e1d1066815b 100644
>> --- a/configs/pxm2_defconfig
>> +++ b/configs/pxm2_defconfig
>> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/rastaban_defconfig b/configs/rastaban_defconfig
>> index 9f538a2f83d..21495341e26 100644
>> --- a/configs/rastaban_defconfig
>> +++ b/configs/rastaban_defconfig
>> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/rut_defconfig b/configs/rut_defconfig
>> index ccf25667b50..248073be1f2 100644
>> --- a/configs/rut_defconfig
>> +++ b/configs/rut_defconfig
>> @@ -99,7 +99,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/sama5d3_xplained_nandflash_defconfig b/configs/sama5d3_xplained_nandflash_defconfig
>> index a1b6122f8eb..a0802f9c5fe 100644
>> --- a/configs/sama5d3_xplained_nandflash_defconfig
>> +++ b/configs/sama5d3_xplained_nandflash_defconfig
>> @@ -86,7 +86,6 @@ CONFIG_PMECC_CAP=4
>>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/sama5d3xek_nandflash_defconfig b/configs/sama5d3xek_nandflash_defconfig
>> index 6278a6c68a7..d96bb91e65d 100644
>> --- a/configs/sama5d3xek_nandflash_defconfig
>> +++ b/configs/sama5d3xek_nandflash_defconfig
>> @@ -90,7 +90,6 @@ CONFIG_PMECC_CAP=4
>>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/sama5d4_xplained_nandflash_defconfig b/configs/sama5d4_xplained_nandflash_defconfig
>> index d12f749d7f4..68101a12fc2 100644
>> --- a/configs/sama5d4_xplained_nandflash_defconfig
>> +++ b/configs/sama5d4_xplained_nandflash_defconfig
>> @@ -84,7 +84,6 @@ CONFIG_PMECC_CAP=8
>>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/sama5d4ek_nandflash_defconfig b/configs/sama5d4ek_nandflash_defconfig
>> index 27bfcdf0084..b8062db775d 100644
>> --- a/configs/sama5d4ek_nandflash_defconfig
>> +++ b/configs/sama5d4ek_nandflash_defconfig
>> @@ -81,7 +81,6 @@ CONFIG_PMECC_CAP=8
>>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
>>   CONFIG_SYS_NAND_OOBSIZE=0xe0
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/smartweb_defconfig b/configs/smartweb_defconfig
>> index 10cbccb69b0..80d2c0f1cc1 100644
>> --- a/configs/smartweb_defconfig
>> +++ b/configs/smartweb_defconfig
>> @@ -84,7 +84,6 @@ CONFIG_MTD=y
>>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>>   CONFIG_NAND_ATMEL=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/taurus_defconfig b/configs/taurus_defconfig
>> index cc5755178b7..9be30c8d8a4 100644
>> --- a/configs/taurus_defconfig
>> +++ b/configs/taurus_defconfig
>> @@ -94,7 +94,6 @@ CONFIG_MTD=y
>>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
>>   CONFIG_NAND_ATMEL=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/configs/thuban_defconfig b/configs/thuban_defconfig
>> index 116700e0df0..1134624f9d2 100644
>> --- a/configs/thuban_defconfig
>> +++ b/configs/thuban_defconfig
>> @@ -100,7 +100,6 @@ CONFIG_MTD=y
>>   CONFIG_MTD_RAW_NAND=y
>>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
>>   CONFIG_SYS_NAND_ONFI_DETECTION=y
>> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
>>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
>>   CONFIG_SYS_NAND_OOBSIZE=0x40
>>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
>> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
>> index 72547f00fbe..ee484dc0f51 100644
>> --- a/drivers/mtd/nand/raw/Kconfig
>> +++ b/drivers/mtd/nand/raw/Kconfig
>> @@ -660,14 +660,6 @@ config SYS_NAND_ONFI_DETECTION
>>            And fetching device parameters flashed on device, by parsing
>>            ONFI parameter page.
>>
>> -config SYS_NAND_PAGE_COUNT
>> -       hex "NAND chip page count"
>> -       depends on SPL_NAND_SUPPORT && (NAND_ATMEL || NAND_MXC || \
>> -               SPL_NAND_AM33XX_BCH || SPL_NAND_LOAD || SPL_NAND_SIMPLE || \
>> -               NAND_OMAP_GPMC)
>> -       help
>> -         Number of pages in the NAND chip.
>> -
>>   config SYS_NAND_PAGE_SIZE
>>          hex "NAND chip page size"
>>          depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
>> diff --git a/drivers/mtd/nand/raw/am335x_spl_bch.c b/drivers/mtd/nand/raw/am335x_spl_bch.c
>> index 6ab3f1f42c5..3843ffc9a24 100644
>> --- a/drivers/mtd/nand/raw/am335x_spl_bch.c
>> +++ b/drivers/mtd/nand/raw/am335x_spl_bch.c
>> @@ -11,6 +11,7 @@
>>
>>   #include <common.h>
>>   #include <nand.h>
>> +#include <system-constants.h>
>>   #include <asm/io.h>
>>   #include <linux/delay.h>
>>   #include <linux/mtd/nand_ecc.h>
>> @@ -32,7 +33,7 @@ static int nand_command(int block, int page, uint32_t offs,
>>          u8 cmd)
>>   {
>>          struct nand_chip *this = mtd_to_nand(mtd);
>> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
>> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
>>                          unsigned int ctrl) = this->cmd_ctrl;
>>
>> diff --git a/drivers/mtd/nand/raw/atmel_nand.c b/drivers/mtd/nand/raw/atmel_nand.c
>> index 6b17e744a69..cfc6578ae83 100644
>> --- a/drivers/mtd/nand/raw/atmel_nand.c
>> +++ b/drivers/mtd/nand/raw/atmel_nand.c
>> @@ -12,6 +12,7 @@
>>
>>   #include <common.h>
>>   #include <log.h>
>> +#include <system-constants.h>
>>   #include <asm/gpio.h>
>>   #include <asm/arch/gpio.h>
>>   #include <dm/device_compat.h>
>> @@ -1258,7 +1259,7 @@ static struct nand_chip nand_chip;
>>   static int nand_command(int block, int page, uint32_t offs, u8 cmd)
>>   {
>>          struct nand_chip *this = mtd_to_nand(mtd);
>> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
>> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
>>                          unsigned int ctrl) = this->cmd_ctrl;
>>
>> @@ -1359,7 +1360,7 @@ int spl_nand_erase_one(int block, int page)
>>          if (nand_chip.select_chip)
>>                  nand_chip.select_chip(mtd, 0);
>>
>> -       page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
>> +       page_addr = page + block * SYS_NAND_PAGE_COUNT;
>>          hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
>>          /* Row address */
>>          hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
>> diff --git a/drivers/mtd/nand/raw/mxc_nand_spl.c b/drivers/mtd/nand/raw/mxc_nand_spl.c
>> index 309e75d01e5..059bf38f50e 100644
>> --- a/drivers/mtd/nand/raw/mxc_nand_spl.c
>> +++ b/drivers/mtd/nand/raw/mxc_nand_spl.c
>> @@ -13,6 +13,7 @@
>>   #include <common.h>
>>   #include <hang.h>
>>   #include <nand.h>
>> +#include <system-constants.h>
>>   #include <linux/mtd/rawnand.h>
>>   #include <asm/arch/imx-regs.h>
>>   #include <asm/io.h>
>> @@ -304,13 +305,13 @@ int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
>>                   * Check if we have crossed a block boundary, and if so
>>                   * check for bad block.
>>                   */
>> -               if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
>> +               if (!(page % SYS_NAND_PAGE_COUNT)) {
>>                          /*
>>                           * Yes, new block. See if this block is good. If not,
>>                           * loop until we find a good block.
>>                           */
>>                          while (is_badblock(page)) {
>> -                               page = page + CONFIG_SYS_NAND_PAGE_COUNT;
>> +                               page = page + SYS_NAND_PAGE_COUNT;
>>                                  /* Check i we've reached the end of flash. */
>>                                  if (page >= maxpages)
>>                                          return -1;
>> diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
>> index 156b44d8358..8848cb27db9 100644
>> --- a/drivers/mtd/nand/raw/nand_spl_loaders.c
>> +++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
>> @@ -12,7 +12,7 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
>>          while (block <= lastblock) {
>>                  if (!nand_is_bad_block(block)) {
>>                          /* Skip bad blocks */
>> -                       while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
>> +                       while (page < SYS_NAND_PAGE_COUNT) {
>>                                  nand_read_page(block, page, dst);
>>                                  /*
>>                                   * When offs is not aligned to page address the
>> diff --git a/drivers/mtd/nand/raw/nand_spl_simple.c b/drivers/mtd/nand/raw/nand_spl_simple.c
>> index 2f3af9edd4c..2ebcac56900 100644
>> --- a/drivers/mtd/nand/raw/nand_spl_simple.c
>> +++ b/drivers/mtd/nand/raw/nand_spl_simple.c
>> @@ -6,6 +6,7 @@
>>
>>   #include <common.h>
>>   #include <nand.h>
>> +#include <system-constants.h>
>>   #include <asm/io.h>
>>   #include <linux/mtd/nand_ecc.h>
>>   #include <linux/mtd/rawnand.h>
>> @@ -27,7 +28,7 @@ static int nand_command(int block, int page, uint32_t offs,
>>          u8 cmd)
>>   {
>>          struct nand_chip *this = mtd_to_nand(mtd);
>> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
>> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>>
>>          while (!this->dev_ready(mtd))
>>                  ;
>> @@ -59,7 +60,7 @@ static int nand_command(int block, int page, uint32_t offs,
>>          u8 cmd)
>>   {
>>          struct nand_chip *this = mtd_to_nand(mtd);
>> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
>> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
>>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
>>                          unsigned int ctrl) = this->cmd_ctrl;
>>
>> diff --git a/drivers/mtd/nand/raw/omap_gpmc.c b/drivers/mtd/nand/raw/omap_gpmc.c
>> index 1a5ed0de31a..6e99538dea1 100644
>> --- a/drivers/mtd/nand/raw/omap_gpmc.c
>> +++ b/drivers/mtd/nand/raw/omap_gpmc.c
>> @@ -6,6 +6,7 @@
>>
>>   #include <common.h>
>>   #include <log.h>
>> +#include <system-constants.h>
>>   #include <asm/io.h>
>>   #include <dm/uclass.h>
>>   #include <linux/errno.h>
>> @@ -1298,7 +1299,7 @@ static int nand_is_bad_block(int block)
>>
>>   static int nand_read_page(int block, int page, uchar *dst)
>>   {
>> -       int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
>> +       int page_addr = block * SYS_NAND_PAGE_COUNT + page;
>>          loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
>>          int ret;
>>          size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
>> diff --git a/include/system-constants.h b/include/system-constants.h
>> index 59371568d1e..c50d54f37a4 100644
>> --- a/include/system-constants.h
>> +++ b/include/system-constants.h
>> @@ -41,4 +41,7 @@
>>   #define SPL_PAYLOAD_ARGS_ADDR  0
>>   #endif
>>
>> +#define SYS_NAND_PAGE_COUNT \
>> +       (CONFIG_SYS_NAND_BLOCK_SIZE / CONFIG_SYS_NAND_PAGE_SIZE)
>> +
>>   #endif
>> --
>> 2.37.1
>>
> 
> Reviewed-by:  Dario Binacchi <dario.binacchi@amarulasolutions.com>
> 
> What do you think about adding another patch to modify SYS_NAND_PAGE_COUNT
> so that it is clearer that it refers to the number of pages in a block?

I would probably just merge that into this patch. How about SYS_NAND_BLOCK_PAGES?

--Sean

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-02 14:13       ` Sean Anderson
@ 2023-11-02 14:18         ` Dario Binacchi
  2023-11-04 19:46           ` Sean Anderson
  0 siblings, 1 reply; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 14:18 UTC (permalink / raw)
  To: Sean Anderson
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

On Thu, Nov 2, 2023 at 3:13 PM Sean Anderson <seanga2@gmail.com> wrote:
>
> On 11/2/23 10:08, Dario Binacchi wrote:
> > On Thu, Nov 2, 2023 at 3:06 PM Sean Anderson <seanga2@gmail.com> wrote:
> >>
> >> On 11/2/23 10:01, Dario Binacchi wrote:
> >>> Sean, All,
> >>>
> >>> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
> >>>>
> >>>> This series tests raw nand flash in sandbox and fixes various bugs discovered in
> >>>> the process. I've tried to do things in a contemporary manner, avoiding the
> >>>> (numerous) variations present on only a few boards. The test is pretty minimal.
> >>>> Future work could test the rest of the nand API as well as the MTD API.
> >>>>
> >>>> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
> >>>> everything grows by a few bytes due to nand_page_size. A few boards grow more,
> >>>> mostly those using nand_spl_loaders.c.
> >>>>
> >>>> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
> >>>>
> >>>>
> >>>> Sean Anderson (15):
> >>>>     spl: nand: Fix NULL-pointer dereference
> >>>>     nand: Don't dereference NULL manufacturer_desc
> >>>>     nand: Calculate SYS_NAND_PAGE_COUNT automatically
> >>>>     nand: spl_loaders: Only read enough pages to load the image
> >>>>     spl: legacy: Honor bl_len when decompressing
> >>>>     spl: nand: Set bl_len to page size
> >>>>     cmd: nand: Map memory before accessing it
> >>>>     spl: nand: Map memory before accessing it
> >>>>     mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
> >>>>     mtd: Add some fallbacks for add/del_mtd_device
> >>>>     nand: Add function to unregister NAND devices
> >>>>     nand: Allow reinitialization
> >>>>     arch: sandbox: Add function to create temporary files
> >>>>     nand: Add sandbox driver
> >>>>     test: spl: Add a test for NAND
> >>>>
> >>>>    README                                        |   9 +-
> >>>>    arch/sandbox/cpu/os.c                         |  17 +
> >>>>    arch/sandbox/dts/test.dts                     |  67 ++
> >>>>    arch/sandbox/include/asm/spl.h                |   1 +
> >>>>    cmd/nand.c                                    |  26 +-
> >>>>    common/spl/Kconfig                            |   2 +-
> >>>>    common/spl/spl_legacy.c                       |  18 +-
> >>>>    common/spl/spl_nand.c                         |  22 +-
> >>>>    configs/am335x_baltos_defconfig               |   3 +-
> >>>>    configs/am335x_evm_defconfig                  |   3 +-
> >>>>    configs/am335x_evm_spiboot_defconfig          |   2 +-
> >>>>    configs/am335x_guardian_defconfig             |   1 -
> >>>>    configs/am335x_hs_evm_defconfig               |   2 +-
> >>>>    configs/am335x_hs_evm_uart_defconfig          |   2 +-
> >>>>    configs/am335x_igep003x_defconfig             |   3 +-
> >>>>    configs/am335x_sl50_defconfig                 |   2 +-
> >>>>    configs/am3517_evm_defconfig                  |   3 +-
> >>>>    configs/am43xx_evm_defconfig                  |   3 +-
> >>>>    configs/am43xx_evm_rtconly_defconfig          |   3 +-
> >>>>    configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
> >>>>    configs/am43xx_hs_evm_defconfig               |   3 +-
> >>>>    configs/am62ax_evm_r5_defconfig               |   2 +-
> >>>>    configs/am65x_evm_a53_defconfig               |   2 +-
> >>>>    configs/axm_defconfig                         |   1 -
> >>>>    configs/chiliboard_defconfig                  |   1 -
> >>>>    configs/cm_t43_defconfig                      |   2 +-
> >>>>    configs/corvus_defconfig                      |   1 -
> >>>>    configs/da850evm_nand_defconfig               |   1 -
> >>>>    configs/devkit3250_defconfig                  |   1 -
> >>>>    configs/devkit8000_defconfig                  |   1 -
> >>>>    configs/dra7xx_evm_defconfig                  |   1 -
> >>>>    configs/draco_defconfig                       |   1 -
> >>>>    configs/etamin_defconfig                      |   1 -
> >>>>    .../gardena-smart-gateway-at91sam_defconfig   |   1 -
> >>>>    configs/igep00x0_defconfig                    |   3 +-
> >>>>    configs/imx6ulz_smm_m2_defconfig              |   2 +-
> >>>>    configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
> >>>>    configs/j7200_evm_a72_defconfig               |   2 +-
> >>>>    configs/j7200_evm_r5_defconfig                |   2 +-
> >>>>    configs/j721e_evm_a72_defconfig               |   2 +-
> >>>>    configs/j721e_evm_r5_defconfig                |   2 +-
> >>>>    configs/j721s2_evm_a72_defconfig              |   2 +-
> >>>>    configs/j721s2_evm_r5_defconfig               |   2 +-
> >>>>    configs/m53menlo_defconfig                    |   1 -
> >>>>    configs/omap35_logic_defconfig                |   3 +-
> >>>>    configs/omap35_logic_somlv_defconfig          |   3 +-
> >>>>    configs/omap3_beagle_defconfig                |   3 +-
> >>>>    configs/omap3_evm_defconfig                   |   3 +-
> >>>>    configs/omap3_logic_defconfig                 |   3 +-
> >>>>    configs/omap3_logic_somlv_defconfig           |   3 +-
> >>>>    configs/omapl138_lcdk_defconfig               |   1 -
> >>>>    configs/phycore-am335x-r2-regor_defconfig     |   3 +-
> >>>>    configs/phycore-am335x-r2-wega_defconfig      |   3 +-
> >>>>    configs/pxm2_defconfig                        |   1 -
> >>>>    configs/rastaban_defconfig                    |   1 -
> >>>>    configs/rut_defconfig                         |   1 -
> >>>>    configs/sama5d3_xplained_nandflash_defconfig  |   1 -
> >>>>    configs/sama5d3xek_nandflash_defconfig        |   1 -
> >>>>    configs/sama5d4_xplained_nandflash_defconfig  |   1 -
> >>>>    configs/sama5d4ek_nandflash_defconfig         |   1 -
> >>>>    configs/sandbox64_defconfig                   |  10 +-
> >>>>    configs/sandbox_defconfig                     |   9 +
> >>>>    configs/sandbox_noinst_defconfig              |  21 +-
> >>>>    configs/smartweb_defconfig                    |   1 -
> >>>>    configs/socfpga_secu1_defconfig               |   2 +-
> >>>>    configs/stm32746g-eval_spl_defconfig          |   2 +-
> >>>>    configs/stm32f746-disco_spl_defconfig         |   2 +-
> >>>>    configs/stm32f769-disco_spl_defconfig         |   2 +-
> >>>>    configs/stm32mp15_basic_defconfig             |   2 +-
> >>>>    configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
> >>>>    configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
> >>>>    configs/taurus_defconfig                      |   1 -
> >>>>    configs/thuban_defconfig                      |   1 -
> >>>>    drivers/mtd/Makefile                          |   2 +-
> >>>>    drivers/mtd/nand/raw/Kconfig                  |  27 +-
> >>>>    drivers/mtd/nand/raw/Makefile                 |   1 +
> >>>>    drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
> >>>>    drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
> >>>>    drivers/mtd/nand/raw/denali_spl.c             |   5 +
> >>>>    drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
> >>>>    drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
> >>>>    drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
> >>>>    drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
> >>>>    drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
> >>>>    drivers/mtd/nand/raw/nand.c                   |  66 +-
> >>>>    drivers/mtd/nand/raw/nand_base.c              |   7 +-
> >>>>    drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
> >>>>    drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
> >>>>    drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
> >>>>    drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
> >>>>    drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
> >>>>    drivers/mtd/onenand/onenand_uboot.c           |   2 -
> >>>>    include/linux/mtd/mtd.h                       |  12 +
> >>>>    include/mtd/cfi_flash.h                       |   2 +-
> >>>>    include/nand.h                                |   3 +
> >>>>    include/os.h                                  |  13 +
> >>>>    include/system-constants.h                    |   3 +
> >>>>    test/dm/Makefile                              |   1 +
> >>>>    test/dm/nand.c                                | 106 +++
> >>>>    test/image/Kconfig                            |   9 +
> >>>>    test/image/Makefile                           |   1 +
> >>>>    test/image/spl_load_nand.c                    |  54 ++
> >>>>    102 files changed, 1269 insertions(+), 153 deletions(-)
> >>>>    create mode 100644 drivers/mtd/nand/raw/sand_nand.c
> >>>>    create mode 100644 test/dm/nand.c
> >>>>    create mode 100644 test/image/spl_load_nand.c
> >>>>
> >>>> --
> >>>> 2.37.1
> >>>>
> >>>
> >>> The CI pipeline fails:
> >>> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
> >>>
> >>> It seems like the problem is only for the clang tests (sandbox64 with
> >>> clang test.py and sandbox with clang test.py)
> >>
> >> I can't view that link. Can you post the error somewhere else?
> >>
> >> I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.
> >>
> >> --Sean
> >>
> >> [1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355
> >>
> >>      sandbox:  +   sandbox
> >> +drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
> >> +        default:
> >> +        ^~~~~~~
> >> +drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
> >> +        memcpy(buf, src + chip->column, to_copy);
> >> +                    ^~~
> >> +drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
> >> +        if (!chip->selected)
> >> +            ^~~~~~~~~~~~~~~
> >> +drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
> >> +        ^~~~~~~~~~~~~~~~~~~~
> >> +drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
> >> +        const u8 *src;
> >> +                     ^
> >> +                      = NULL
> >> +2 errors generated.
> >> +make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
> >> +make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
> >> +make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
> >> +make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
> >> +make[1]: *** [Makefile:1858: drivers] Error 2
> >> +make: *** [Makefile:177: sub-make] Error 2
> >>
> >>
> >
> > +test/dm/nand.c:83:2: error: expression result unused [-Werror,-Wunused-value]
> > +        ut_assertok(nand_erase_opts(mtd, &opts));
> > +        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > +include/test/ut.h:309:27: note: expanded from macro 'ut_assertok'
> > +#define ut_assertok(cond)       ut_asserteq(0, cond)
> > +                                ^~~~~~~~~~~~~~~~~~~~
> > +include/test/ut.h:162:2: note: expanded from macro 'ut_asserteq'
> > +        __ret;                                                          \
> > +        ^~~~~
> > +1 error generated.
> > +make[3]: *** [scripts/Makefile.build:257: test/dm/nand.o] Error 1
> > +make[2]: *** [scripts/Makefile.build:397: test/dm] Error 2
> > +make[1]: *** [Makefile:1858: test] Error 2
> > +make: *** [Makefile:177: sub-make] Error 2
>
> ...well that's a new one. Wonder why it doesn't complain about all the other uses of
> ut_assertok (which, invariably, discard the value in the same way).

That's what I thought as well. It seems to be related to CLANG. Could it be the
'unused-value' flag? I can't figure out why it causes the problem at
that point,
given that the 'ut_assertok()' is called even before the one that
generates the error.

Dario

>
> --Sean
>


--

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically
  2023-11-02 14:17     ` Sean Anderson
@ 2023-11-02 14:19       ` Dario Binacchi
  0 siblings, 0 replies; 34+ messages in thread
From: Dario Binacchi @ 2023-11-02 14:19 UTC (permalink / raw)
  To: Sean Anderson; +Cc: u-boot, Michael Trimarchi, Tom Rini

Sean, All

On Thu, Nov 2, 2023 at 3:17 PM Sean Anderson <seanga2@gmail.com> wrote:
>
> On 11/2/23 05:53, Dario Binacchi wrote:
> > Sean, All
> >
> > On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
> >>
> >> Contrary to what the help message says, this is the number of pages per
> >> block. Calculate it automatically based on SYS_NAND_BLOCK_SIZE and
> >> SYS_NAND_PAGE_SIZE.
> >>
> >> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> >> ---
> >>
> >>   README                                          | 9 ++++-----
> >>   configs/am335x_baltos_defconfig                 | 1 -
> >>   configs/am335x_evm_defconfig                    | 1 -
> >>   configs/am335x_guardian_defconfig               | 1 -
> >>   configs/am335x_igep003x_defconfig               | 1 -
> >>   configs/am3517_evm_defconfig                    | 1 -
> >>   configs/am43xx_evm_defconfig                    | 1 -
> >>   configs/am43xx_evm_rtconly_defconfig            | 1 -
> >>   configs/am43xx_evm_usbhost_boot_defconfig       | 1 -
> >>   configs/am43xx_hs_evm_defconfig                 | 1 -
> >>   configs/axm_defconfig                           | 1 -
> >>   configs/chiliboard_defconfig                    | 1 -
> >>   configs/corvus_defconfig                        | 1 -
> >>   configs/da850evm_nand_defconfig                 | 1 -
> >>   configs/devkit3250_defconfig                    | 1 -
> >>   configs/devkit8000_defconfig                    | 1 -
> >>   configs/dra7xx_evm_defconfig                    | 1 -
> >>   configs/draco_defconfig                         | 1 -
> >>   configs/etamin_defconfig                        | 1 -
> >>   configs/gardena-smart-gateway-at91sam_defconfig | 1 -
> >>   configs/igep00x0_defconfig                      | 1 -
> >>   configs/m53menlo_defconfig                      | 1 -
> >>   configs/omap35_logic_defconfig                  | 1 -
> >>   configs/omap35_logic_somlv_defconfig            | 1 -
> >>   configs/omap3_beagle_defconfig                  | 1 -
> >>   configs/omap3_evm_defconfig                     | 1 -
> >>   configs/omap3_logic_defconfig                   | 1 -
> >>   configs/omap3_logic_somlv_defconfig             | 1 -
> >>   configs/omapl138_lcdk_defconfig                 | 1 -
> >>   configs/phycore-am335x-r2-regor_defconfig       | 1 -
> >>   configs/phycore-am335x-r2-wega_defconfig        | 1 -
> >>   configs/pxm2_defconfig                          | 1 -
> >>   configs/rastaban_defconfig                      | 1 -
> >>   configs/rut_defconfig                           | 1 -
> >>   configs/sama5d3_xplained_nandflash_defconfig    | 1 -
> >>   configs/sama5d3xek_nandflash_defconfig          | 1 -
> >>   configs/sama5d4_xplained_nandflash_defconfig    | 1 -
> >>   configs/sama5d4ek_nandflash_defconfig           | 1 -
> >>   configs/smartweb_defconfig                      | 1 -
> >>   configs/taurus_defconfig                        | 1 -
> >>   configs/thuban_defconfig                        | 1 -
> >>   drivers/mtd/nand/raw/Kconfig                    | 8 --------
> >>   drivers/mtd/nand/raw/am335x_spl_bch.c           | 3 ++-
> >>   drivers/mtd/nand/raw/atmel_nand.c               | 5 +++--
> >>   drivers/mtd/nand/raw/mxc_nand_spl.c             | 5 +++--
> >>   drivers/mtd/nand/raw/nand_spl_loaders.c         | 2 +-
> >>   drivers/mtd/nand/raw/nand_spl_simple.c          | 5 +++--
> >>   drivers/mtd/nand/raw/omap_gpmc.c                | 3 ++-
> >>   include/system-constants.h                      | 3 +++
> >>   49 files changed, 21 insertions(+), 62 deletions(-)
> >>
> >> diff --git a/README b/README
> >> index 60c6b8a19db..00d422737fb 100644
> >> --- a/README
> >> +++ b/README
> >> @@ -1191,11 +1191,10 @@ The following options need to be configured:
> >>                  Support for a lightweight UBI (fastmap) scanner and
> >>                  loader
> >>
> >> -               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_COUNT,
> >> -               CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE,
> >> -               CONFIG_SYS_NAND_BLOCK_SIZE, CONFIG_SYS_NAND_BAD_BLOCK_POS,
> >> -               CFG_SYS_NAND_ECCPOS, CFG_SYS_NAND_ECCSIZE,
> >> -               CFG_SYS_NAND_ECCBYTES
> >> +               CONFIG_SYS_NAND_5_ADDR_CYCLE, CONFIG_SYS_NAND_PAGE_SIZE,
> >> +               CONFIG_SYS_NAND_OOBSIZE, CONFIG_SYS_NAND_BLOCK_SIZE,
> >> +               CONFIG_SYS_NAND_BAD_BLOCK_POS, CFG_SYS_NAND_ECCPOS,
> >> +               CFG_SYS_NAND_ECCSIZE, CFG_SYS_NAND_ECCBYTES
> >>                  Defines the size and behavior of the NAND that SPL uses
> >>                  to read U-Boot
> >>
> >> diff --git a/configs/am335x_baltos_defconfig b/configs/am335x_baltos_defconfig
> >> index 16993ef5386..3891e48e9c7 100644
> >> --- a/configs/am335x_baltos_defconfig
> >> +++ b/configs/am335x_baltos_defconfig
> >> @@ -67,7 +67,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am335x_evm_defconfig b/configs/am335x_evm_defconfig
> >> index f048e60f7f3..73f221d55bc 100644
> >> --- a/configs/am335x_evm_defconfig
> >> +++ b/configs/am335x_evm_defconfig
> >> @@ -84,7 +84,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am335x_guardian_defconfig b/configs/am335x_guardian_defconfig
> >> index 01d848ceede..5369e46bc1b 100644
> >> --- a/configs/am335x_guardian_defconfig
> >> +++ b/configs/am335x_guardian_defconfig
> >> @@ -99,7 +99,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0x100
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am335x_igep003x_defconfig b/configs/am335x_igep003x_defconfig
> >> index 4dd6366ef67..4c5c82f9bdd 100644
> >> --- a/configs/am335x_igep003x_defconfig
> >> +++ b/configs/am335x_igep003x_defconfig
> >> @@ -85,7 +85,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_MTD_UBI_FASTMAP=y
> >> diff --git a/configs/am3517_evm_defconfig b/configs/am3517_evm_defconfig
> >> index 0a83ac9378c..111929fb912 100644
> >> --- a/configs/am3517_evm_defconfig
> >> +++ b/configs/am3517_evm_defconfig
> >> @@ -75,7 +75,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/am43xx_evm_defconfig b/configs/am43xx_evm_defconfig
> >> index 6571afd345e..5f2356bcd32 100644
> >> --- a/configs/am43xx_evm_defconfig
> >> +++ b/configs/am43xx_evm_defconfig
> >> @@ -72,7 +72,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am43xx_evm_rtconly_defconfig b/configs/am43xx_evm_rtconly_defconfig
> >> index e84aed4d54d..30681e7eeb6 100644
> >> --- a/configs/am43xx_evm_rtconly_defconfig
> >> +++ b/configs/am43xx_evm_rtconly_defconfig
> >> @@ -62,7 +62,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am43xx_evm_usbhost_boot_defconfig b/configs/am43xx_evm_usbhost_boot_defconfig
> >> index 0cae3242b04..6c4cc99e5ff 100644
> >> --- a/configs/am43xx_evm_usbhost_boot_defconfig
> >> +++ b/configs/am43xx_evm_usbhost_boot_defconfig
> >> @@ -75,7 +75,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/am43xx_hs_evm_defconfig b/configs/am43xx_hs_evm_defconfig
> >> index 370ee967258..49ecb4c9e66 100644
> >> --- a/configs/am43xx_hs_evm_defconfig
> >> +++ b/configs/am43xx_hs_evm_defconfig
> >> @@ -68,7 +68,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/axm_defconfig b/configs/axm_defconfig
> >> index f9c02144c4a..e1a01b24b88 100644
> >> --- a/configs/axm_defconfig
> >> +++ b/configs/axm_defconfig
> >> @@ -86,7 +86,6 @@ CONFIG_MTD=y
> >>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
> >>   CONFIG_NAND_ATMEL=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/chiliboard_defconfig b/configs/chiliboard_defconfig
> >> index 06642d282d1..0cd649d643a 100644
> >> --- a/configs/chiliboard_defconfig
> >> +++ b/configs/chiliboard_defconfig
> >> @@ -63,7 +63,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/corvus_defconfig b/configs/corvus_defconfig
> >> index ecf61f0d1e6..5eaa6dbb5fc 100644
> >> --- a/configs/corvus_defconfig
> >> +++ b/configs/corvus_defconfig
> >> @@ -81,7 +81,6 @@ CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT=y
> >>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
> >>   CONFIG_NAND_ATMEL=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/da850evm_nand_defconfig b/configs/da850evm_nand_defconfig
> >> index 5f6f5d788d0..62b8edde248 100644
> >> --- a/configs/da850evm_nand_defconfig
> >> +++ b/configs/da850evm_nand_defconfig
> >> @@ -88,7 +88,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
> >>   CONFIG_NAND_DAVINCI=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/devkit3250_defconfig b/configs/devkit3250_defconfig
> >> index ec4031da51d..569b156c997 100644
> >> --- a/configs/devkit3250_defconfig
> >> +++ b/configs/devkit3250_defconfig
> >> @@ -73,7 +73,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
> >>   CONFIG_NAND_LPC32XX_SLC=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   # CONFIG_SYS_NAND_5_ADDR_CYCLE is not set
> >> diff --git a/configs/devkit8000_defconfig b/configs/devkit8000_defconfig
> >> index ef16da176a9..a7f82442eb0 100644
> >> --- a/configs/devkit8000_defconfig
> >> +++ b/configs/devkit8000_defconfig
> >> @@ -77,7 +77,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/dra7xx_evm_defconfig b/configs/dra7xx_evm_defconfig
> >> index 19ca89fa867..c3a3ec2cd42 100644
> >> --- a/configs/dra7xx_evm_defconfig
> >> +++ b/configs/dra7xx_evm_defconfig
> >> @@ -104,7 +104,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/draco_defconfig b/configs/draco_defconfig
> >> index ee19920a703..2a82087ab31 100644
> >> --- a/configs/draco_defconfig
> >> +++ b/configs/draco_defconfig
> >> @@ -100,7 +100,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/etamin_defconfig b/configs/etamin_defconfig
> >> index c0ce7a33569..4ac0abc56b0 100644
> >> --- a/configs/etamin_defconfig
> >> +++ b/configs/etamin_defconfig
> >> @@ -104,7 +104,6 @@ CONFIG_SYS_MAX_NAND_DEVICE=3
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH16_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x80000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x80
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/gardena-smart-gateway-at91sam_defconfig b/configs/gardena-smart-gateway-at91sam_defconfig
> >> index 439fcc05753..0fb92ff1638 100644
> >> --- a/configs/gardena-smart-gateway-at91sam_defconfig
> >> +++ b/configs/gardena-smart-gateway-at91sam_defconfig
> >> @@ -99,7 +99,6 @@ CONFIG_MTD=y
> >>   CONFIG_NAND_ATMEL=y
> >>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/igep00x0_defconfig b/configs/igep00x0_defconfig
> >> index 993bbe29c6b..3b1af07f057 100644
> >> --- a/configs/igep00x0_defconfig
> >> +++ b/configs/igep00x0_defconfig
> >> @@ -78,7 +78,6 @@ CONFIG_SYS_MTDPARTS_RUNTIME=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/m53menlo_defconfig b/configs/m53menlo_defconfig
> >> index ef9e15d1520..e1ddc0af5db 100644
> >> --- a/configs/m53menlo_defconfig
> >> +++ b/configs/m53menlo_defconfig
> >> @@ -90,7 +90,6 @@ CONFIG_SYS_NAND_USE_FLASH_BBT=y
> >>   CONFIG_NAND_MXC=y
> >>   CONFIG_MXC_NAND_HWECC=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
> >> index 539b0cfa4f7..3a656072534 100644
> >> --- a/configs/omap35_logic_defconfig
> >> +++ b/configs/omap35_logic_defconfig
> >> @@ -74,7 +74,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omap35_logic_somlv_defconfig b/configs/omap35_logic_somlv_defconfig
> >> index f14ce3d23db..868e89114b7 100644
> >> --- a/configs/omap35_logic_somlv_defconfig
> >> +++ b/configs/omap35_logic_somlv_defconfig
> >> @@ -83,7 +83,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omap3_beagle_defconfig b/configs/omap3_beagle_defconfig
> >> index cabd3659385..7f0b927528d 100644
> >> --- a/configs/omap3_beagle_defconfig
> >> +++ b/configs/omap3_beagle_defconfig
> >> @@ -83,7 +83,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_HAM1_CODE_HW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omap3_evm_defconfig b/configs/omap3_evm_defconfig
> >> index 33ff39ff548..3434783d45b 100644
> >> --- a/configs/omap3_evm_defconfig
> >> +++ b/configs/omap3_evm_defconfig
> >> @@ -74,7 +74,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
> >> index 7d3d602c9d9..78bfbbd1a22 100644
> >> --- a/configs/omap3_logic_defconfig
> >> +++ b/configs/omap3_logic_defconfig
> >> @@ -73,7 +73,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omap3_logic_somlv_defconfig b/configs/omap3_logic_somlv_defconfig
> >> index 0141a425e07..aefd8861db3 100644
> >> --- a/configs/omap3_logic_somlv_defconfig
> >> +++ b/configs/omap3_logic_somlv_defconfig
> >> @@ -84,7 +84,6 @@ CONFIG_SYS_MAX_FLASH_SECT=256
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_NAND_OMAP_ECCSCHEME_BCH8_CODE_HW_DETECTION_SW=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/omapl138_lcdk_defconfig b/configs/omapl138_lcdk_defconfig
> >> index 47feff21bd4..63554d56016 100644
> >> --- a/configs/omapl138_lcdk_defconfig
> >> +++ b/configs/omapl138_lcdk_defconfig
> >> @@ -84,7 +84,6 @@ CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_USE_FLASH_BBT=y
> >>   CONFIG_NAND_DAVINCI=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_BUSWIDTH_16BIT=y
> >> diff --git a/configs/phycore-am335x-r2-regor_defconfig b/configs/phycore-am335x-r2-regor_defconfig
> >> index 4bdf8822181..85d53d06357 100644
> >> --- a/configs/phycore-am335x-r2-regor_defconfig
> >> +++ b/configs/phycore-am335x-r2-regor_defconfig
> >> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/phycore-am335x-r2-wega_defconfig b/configs/phycore-am335x-r2-wega_defconfig
> >> index 7644fbec273..60607548a25 100644
> >> --- a/configs/phycore-am335x-r2-wega_defconfig
> >> +++ b/configs/phycore-am335x-r2-wega_defconfig
> >> @@ -76,7 +76,6 @@ CONFIG_DM_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/pxm2_defconfig b/configs/pxm2_defconfig
> >> index 2ea007d0de5..e1d1066815b 100644
> >> --- a/configs/pxm2_defconfig
> >> +++ b/configs/pxm2_defconfig
> >> @@ -99,7 +99,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/rastaban_defconfig b/configs/rastaban_defconfig
> >> index 9f538a2f83d..21495341e26 100644
> >> --- a/configs/rastaban_defconfig
> >> +++ b/configs/rastaban_defconfig
> >> @@ -100,7 +100,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/rut_defconfig b/configs/rut_defconfig
> >> index ccf25667b50..248073be1f2 100644
> >> --- a/configs/rut_defconfig
> >> +++ b/configs/rut_defconfig
> >> @@ -99,7 +99,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/sama5d3_xplained_nandflash_defconfig b/configs/sama5d3_xplained_nandflash_defconfig
> >> index a1b6122f8eb..a0802f9c5fe 100644
> >> --- a/configs/sama5d3_xplained_nandflash_defconfig
> >> +++ b/configs/sama5d3_xplained_nandflash_defconfig
> >> @@ -86,7 +86,6 @@ CONFIG_PMECC_CAP=4
> >>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/sama5d3xek_nandflash_defconfig b/configs/sama5d3xek_nandflash_defconfig
> >> index 6278a6c68a7..d96bb91e65d 100644
> >> --- a/configs/sama5d3xek_nandflash_defconfig
> >> +++ b/configs/sama5d3xek_nandflash_defconfig
> >> @@ -90,7 +90,6 @@ CONFIG_PMECC_CAP=4
> >>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/sama5d4_xplained_nandflash_defconfig b/configs/sama5d4_xplained_nandflash_defconfig
> >> index d12f749d7f4..68101a12fc2 100644
> >> --- a/configs/sama5d4_xplained_nandflash_defconfig
> >> +++ b/configs/sama5d4_xplained_nandflash_defconfig
> >> @@ -84,7 +84,6 @@ CONFIG_PMECC_CAP=8
> >>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/sama5d4ek_nandflash_defconfig b/configs/sama5d4ek_nandflash_defconfig
> >> index 27bfcdf0084..b8062db775d 100644
> >> --- a/configs/sama5d4ek_nandflash_defconfig
> >> +++ b/configs/sama5d4ek_nandflash_defconfig
> >> @@ -81,7 +81,6 @@ CONFIG_PMECC_CAP=8
> >>   CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x40000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x1000
> >>   CONFIG_SYS_NAND_OOBSIZE=0xe0
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/smartweb_defconfig b/configs/smartweb_defconfig
> >> index 10cbccb69b0..80d2c0f1cc1 100644
> >> --- a/configs/smartweb_defconfig
> >> +++ b/configs/smartweb_defconfig
> >> @@ -84,7 +84,6 @@ CONFIG_MTD=y
> >>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
> >>   CONFIG_NAND_ATMEL=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/taurus_defconfig b/configs/taurus_defconfig
> >> index cc5755178b7..9be30c8d8a4 100644
> >> --- a/configs/taurus_defconfig
> >> +++ b/configs/taurus_defconfig
> >> @@ -94,7 +94,6 @@ CONFIG_MTD=y
> >>   # CONFIG_SYS_NAND_USE_FLASH_BBT is not set
> >>   CONFIG_NAND_ATMEL=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/configs/thuban_defconfig b/configs/thuban_defconfig
> >> index 116700e0df0..1134624f9d2 100644
> >> --- a/configs/thuban_defconfig
> >> +++ b/configs/thuban_defconfig
> >> @@ -100,7 +100,6 @@ CONFIG_MTD=y
> >>   CONFIG_MTD_RAW_NAND=y
> >>   CONFIG_SYS_NAND_BLOCK_SIZE=0x20000
> >>   CONFIG_SYS_NAND_ONFI_DETECTION=y
> >> -CONFIG_SYS_NAND_PAGE_COUNT=0x40
> >>   CONFIG_SYS_NAND_PAGE_SIZE=0x800
> >>   CONFIG_SYS_NAND_OOBSIZE=0x40
> >>   CONFIG_SYS_NAND_U_BOOT_LOCATIONS=y
> >> diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
> >> index 72547f00fbe..ee484dc0f51 100644
> >> --- a/drivers/mtd/nand/raw/Kconfig
> >> +++ b/drivers/mtd/nand/raw/Kconfig
> >> @@ -660,14 +660,6 @@ config SYS_NAND_ONFI_DETECTION
> >>            And fetching device parameters flashed on device, by parsing
> >>            ONFI parameter page.
> >>
> >> -config SYS_NAND_PAGE_COUNT
> >> -       hex "NAND chip page count"
> >> -       depends on SPL_NAND_SUPPORT && (NAND_ATMEL || NAND_MXC || \
> >> -               SPL_NAND_AM33XX_BCH || SPL_NAND_LOAD || SPL_NAND_SIMPLE || \
> >> -               NAND_OMAP_GPMC)
> >> -       help
> >> -         Number of pages in the NAND chip.
> >> -
> >>   config SYS_NAND_PAGE_SIZE
> >>          hex "NAND chip page size"
> >>          depends on ARCH_SUNXI || NAND_OMAP_GPMC || NAND_LPC32XX_SLC || \
> >> diff --git a/drivers/mtd/nand/raw/am335x_spl_bch.c b/drivers/mtd/nand/raw/am335x_spl_bch.c
> >> index 6ab3f1f42c5..3843ffc9a24 100644
> >> --- a/drivers/mtd/nand/raw/am335x_spl_bch.c
> >> +++ b/drivers/mtd/nand/raw/am335x_spl_bch.c
> >> @@ -11,6 +11,7 @@
> >>
> >>   #include <common.h>
> >>   #include <nand.h>
> >> +#include <system-constants.h>
> >>   #include <asm/io.h>
> >>   #include <linux/delay.h>
> >>   #include <linux/mtd/nand_ecc.h>
> >> @@ -32,7 +33,7 @@ static int nand_command(int block, int page, uint32_t offs,
> >>          u8 cmd)
> >>   {
> >>          struct nand_chip *this = mtd_to_nand(mtd);
> >> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> >> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
> >>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
> >>                          unsigned int ctrl) = this->cmd_ctrl;
> >>
> >> diff --git a/drivers/mtd/nand/raw/atmel_nand.c b/drivers/mtd/nand/raw/atmel_nand.c
> >> index 6b17e744a69..cfc6578ae83 100644
> >> --- a/drivers/mtd/nand/raw/atmel_nand.c
> >> +++ b/drivers/mtd/nand/raw/atmel_nand.c
> >> @@ -12,6 +12,7 @@
> >>
> >>   #include <common.h>
> >>   #include <log.h>
> >> +#include <system-constants.h>
> >>   #include <asm/gpio.h>
> >>   #include <asm/arch/gpio.h>
> >>   #include <dm/device_compat.h>
> >> @@ -1258,7 +1259,7 @@ static struct nand_chip nand_chip;
> >>   static int nand_command(int block, int page, uint32_t offs, u8 cmd)
> >>   {
> >>          struct nand_chip *this = mtd_to_nand(mtd);
> >> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> >> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
> >>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
> >>                          unsigned int ctrl) = this->cmd_ctrl;
> >>
> >> @@ -1359,7 +1360,7 @@ int spl_nand_erase_one(int block, int page)
> >>          if (nand_chip.select_chip)
> >>                  nand_chip.select_chip(mtd, 0);
> >>
> >> -       page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> >> +       page_addr = page + block * SYS_NAND_PAGE_COUNT;
> >>          hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
> >>          /* Row address */
> >>          hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
> >> diff --git a/drivers/mtd/nand/raw/mxc_nand_spl.c b/drivers/mtd/nand/raw/mxc_nand_spl.c
> >> index 309e75d01e5..059bf38f50e 100644
> >> --- a/drivers/mtd/nand/raw/mxc_nand_spl.c
> >> +++ b/drivers/mtd/nand/raw/mxc_nand_spl.c
> >> @@ -13,6 +13,7 @@
> >>   #include <common.h>
> >>   #include <hang.h>
> >>   #include <nand.h>
> >> +#include <system-constants.h>
> >>   #include <linux/mtd/rawnand.h>
> >>   #include <asm/arch/imx-regs.h>
> >>   #include <asm/io.h>
> >> @@ -304,13 +305,13 @@ int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
> >>                   * Check if we have crossed a block boundary, and if so
> >>                   * check for bad block.
> >>                   */
> >> -               if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
> >> +               if (!(page % SYS_NAND_PAGE_COUNT)) {
> >>                          /*
> >>                           * Yes, new block. See if this block is good. If not,
> >>                           * loop until we find a good block.
> >>                           */
> >>                          while (is_badblock(page)) {
> >> -                               page = page + CONFIG_SYS_NAND_PAGE_COUNT;
> >> +                               page = page + SYS_NAND_PAGE_COUNT;
> >>                                  /* Check i we've reached the end of flash. */
> >>                                  if (page >= maxpages)
> >>                                          return -1;
> >> diff --git a/drivers/mtd/nand/raw/nand_spl_loaders.c b/drivers/mtd/nand/raw/nand_spl_loaders.c
> >> index 156b44d8358..8848cb27db9 100644
> >> --- a/drivers/mtd/nand/raw/nand_spl_loaders.c
> >> +++ b/drivers/mtd/nand/raw/nand_spl_loaders.c
> >> @@ -12,7 +12,7 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
> >>          while (block <= lastblock) {
> >>                  if (!nand_is_bad_block(block)) {
> >>                          /* Skip bad blocks */
> >> -                       while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
> >> +                       while (page < SYS_NAND_PAGE_COUNT) {
> >>                                  nand_read_page(block, page, dst);
> >>                                  /*
> >>                                   * When offs is not aligned to page address the
> >> diff --git a/drivers/mtd/nand/raw/nand_spl_simple.c b/drivers/mtd/nand/raw/nand_spl_simple.c
> >> index 2f3af9edd4c..2ebcac56900 100644
> >> --- a/drivers/mtd/nand/raw/nand_spl_simple.c
> >> +++ b/drivers/mtd/nand/raw/nand_spl_simple.c
> >> @@ -6,6 +6,7 @@
> >>
> >>   #include <common.h>
> >>   #include <nand.h>
> >> +#include <system-constants.h>
> >>   #include <asm/io.h>
> >>   #include <linux/mtd/nand_ecc.h>
> >>   #include <linux/mtd/rawnand.h>
> >> @@ -27,7 +28,7 @@ static int nand_command(int block, int page, uint32_t offs,
> >>          u8 cmd)
> >>   {
> >>          struct nand_chip *this = mtd_to_nand(mtd);
> >> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> >> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
> >>
> >>          while (!this->dev_ready(mtd))
> >>                  ;
> >> @@ -59,7 +60,7 @@ static int nand_command(int block, int page, uint32_t offs,
> >>          u8 cmd)
> >>   {
> >>          struct nand_chip *this = mtd_to_nand(mtd);
> >> -       int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
> >> +       int page_addr = page + block * SYS_NAND_PAGE_COUNT;
> >>          void (*hwctrl)(struct mtd_info *mtd, int cmd,
> >>                          unsigned int ctrl) = this->cmd_ctrl;
> >>
> >> diff --git a/drivers/mtd/nand/raw/omap_gpmc.c b/drivers/mtd/nand/raw/omap_gpmc.c
> >> index 1a5ed0de31a..6e99538dea1 100644
> >> --- a/drivers/mtd/nand/raw/omap_gpmc.c
> >> +++ b/drivers/mtd/nand/raw/omap_gpmc.c
> >> @@ -6,6 +6,7 @@
> >>
> >>   #include <common.h>
> >>   #include <log.h>
> >> +#include <system-constants.h>
> >>   #include <asm/io.h>
> >>   #include <dm/uclass.h>
> >>   #include <linux/errno.h>
> >> @@ -1298,7 +1299,7 @@ static int nand_is_bad_block(int block)
> >>
> >>   static int nand_read_page(int block, int page, uchar *dst)
> >>   {
> >> -       int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
> >> +       int page_addr = block * SYS_NAND_PAGE_COUNT + page;
> >>          loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
> >>          int ret;
> >>          size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
> >> diff --git a/include/system-constants.h b/include/system-constants.h
> >> index 59371568d1e..c50d54f37a4 100644
> >> --- a/include/system-constants.h
> >> +++ b/include/system-constants.h
> >> @@ -41,4 +41,7 @@
> >>   #define SPL_PAYLOAD_ARGS_ADDR  0
> >>   #endif
> >>
> >> +#define SYS_NAND_PAGE_COUNT \
> >> +       (CONFIG_SYS_NAND_BLOCK_SIZE / CONFIG_SYS_NAND_PAGE_SIZE)
> >> +
> >>   #endif
> >> --
> >> 2.37.1
> >>
> >
> > Reviewed-by:  Dario Binacchi <dario.binacchi@amarulasolutions.com>
> >
> > What do you think about adding another patch to modify SYS_NAND_PAGE_COUNT
> > so that it is clearer that it refers to the number of pages in a block?
>
> I would probably just merge that into this patch. How about SYS_NAND_BLOCK_PAGES?

It's okay with me.

Thanks and regards,
Dario

>
> --Sean



-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-02 14:18         ` Dario Binacchi
@ 2023-11-04 19:46           ` Sean Anderson
  2023-11-09  8:23             ` Dario Binacchi
  0 siblings, 1 reply; 34+ messages in thread
From: Sean Anderson @ 2023-11-04 19:46 UTC (permalink / raw)
  To: Dario Binacchi
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

On 11/2/23 10:18, Dario Binacchi wrote:
> On Thu, Nov 2, 2023 at 3:13 PM Sean Anderson <seanga2@gmail.com> wrote:
>>
>> On 11/2/23 10:08, Dario Binacchi wrote:
>>> On Thu, Nov 2, 2023 at 3:06 PM Sean Anderson <seanga2@gmail.com> wrote:
>>>>
>>>> On 11/2/23 10:01, Dario Binacchi wrote:
>>>>> Sean, All,
>>>>>
>>>>> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
>>>>>>
>>>>>> This series tests raw nand flash in sandbox and fixes various bugs discovered in
>>>>>> the process. I've tried to do things in a contemporary manner, avoiding the
>>>>>> (numerous) variations present on only a few boards. The test is pretty minimal.
>>>>>> Future work could test the rest of the nand API as well as the MTD API.
>>>>>>
>>>>>> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
>>>>>> everything grows by a few bytes due to nand_page_size. A few boards grow more,
>>>>>> mostly those using nand_spl_loaders.c.
>>>>>>
>>>>>> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
>>>>>>
>>>>>>
>>>>>> Sean Anderson (15):
>>>>>>      spl: nand: Fix NULL-pointer dereference
>>>>>>      nand: Don't dereference NULL manufacturer_desc
>>>>>>      nand: Calculate SYS_NAND_PAGE_COUNT automatically
>>>>>>      nand: spl_loaders: Only read enough pages to load the image
>>>>>>      spl: legacy: Honor bl_len when decompressing
>>>>>>      spl: nand: Set bl_len to page size
>>>>>>      cmd: nand: Map memory before accessing it
>>>>>>      spl: nand: Map memory before accessing it
>>>>>>      mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
>>>>>>      mtd: Add some fallbacks for add/del_mtd_device
>>>>>>      nand: Add function to unregister NAND devices
>>>>>>      nand: Allow reinitialization
>>>>>>      arch: sandbox: Add function to create temporary files
>>>>>>      nand: Add sandbox driver
>>>>>>      test: spl: Add a test for NAND
>>>>>>
>>>>>>     README                                        |   9 +-
>>>>>>     arch/sandbox/cpu/os.c                         |  17 +
>>>>>>     arch/sandbox/dts/test.dts                     |  67 ++
>>>>>>     arch/sandbox/include/asm/spl.h                |   1 +
>>>>>>     cmd/nand.c                                    |  26 +-
>>>>>>     common/spl/Kconfig                            |   2 +-
>>>>>>     common/spl/spl_legacy.c                       |  18 +-
>>>>>>     common/spl/spl_nand.c                         |  22 +-
>>>>>>     configs/am335x_baltos_defconfig               |   3 +-
>>>>>>     configs/am335x_evm_defconfig                  |   3 +-
>>>>>>     configs/am335x_evm_spiboot_defconfig          |   2 +-
>>>>>>     configs/am335x_guardian_defconfig             |   1 -
>>>>>>     configs/am335x_hs_evm_defconfig               |   2 +-
>>>>>>     configs/am335x_hs_evm_uart_defconfig          |   2 +-
>>>>>>     configs/am335x_igep003x_defconfig             |   3 +-
>>>>>>     configs/am335x_sl50_defconfig                 |   2 +-
>>>>>>     configs/am3517_evm_defconfig                  |   3 +-
>>>>>>     configs/am43xx_evm_defconfig                  |   3 +-
>>>>>>     configs/am43xx_evm_rtconly_defconfig          |   3 +-
>>>>>>     configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
>>>>>>     configs/am43xx_hs_evm_defconfig               |   3 +-
>>>>>>     configs/am62ax_evm_r5_defconfig               |   2 +-
>>>>>>     configs/am65x_evm_a53_defconfig               |   2 +-
>>>>>>     configs/axm_defconfig                         |   1 -
>>>>>>     configs/chiliboard_defconfig                  |   1 -
>>>>>>     configs/cm_t43_defconfig                      |   2 +-
>>>>>>     configs/corvus_defconfig                      |   1 -
>>>>>>     configs/da850evm_nand_defconfig               |   1 -
>>>>>>     configs/devkit3250_defconfig                  |   1 -
>>>>>>     configs/devkit8000_defconfig                  |   1 -
>>>>>>     configs/dra7xx_evm_defconfig                  |   1 -
>>>>>>     configs/draco_defconfig                       |   1 -
>>>>>>     configs/etamin_defconfig                      |   1 -
>>>>>>     .../gardena-smart-gateway-at91sam_defconfig   |   1 -
>>>>>>     configs/igep00x0_defconfig                    |   3 +-
>>>>>>     configs/imx6ulz_smm_m2_defconfig              |   2 +-
>>>>>>     configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
>>>>>>     configs/j7200_evm_a72_defconfig               |   2 +-
>>>>>>     configs/j7200_evm_r5_defconfig                |   2 +-
>>>>>>     configs/j721e_evm_a72_defconfig               |   2 +-
>>>>>>     configs/j721e_evm_r5_defconfig                |   2 +-
>>>>>>     configs/j721s2_evm_a72_defconfig              |   2 +-
>>>>>>     configs/j721s2_evm_r5_defconfig               |   2 +-
>>>>>>     configs/m53menlo_defconfig                    |   1 -
>>>>>>     configs/omap35_logic_defconfig                |   3 +-
>>>>>>     configs/omap35_logic_somlv_defconfig          |   3 +-
>>>>>>     configs/omap3_beagle_defconfig                |   3 +-
>>>>>>     configs/omap3_evm_defconfig                   |   3 +-
>>>>>>     configs/omap3_logic_defconfig                 |   3 +-
>>>>>>     configs/omap3_logic_somlv_defconfig           |   3 +-
>>>>>>     configs/omapl138_lcdk_defconfig               |   1 -
>>>>>>     configs/phycore-am335x-r2-regor_defconfig     |   3 +-
>>>>>>     configs/phycore-am335x-r2-wega_defconfig      |   3 +-
>>>>>>     configs/pxm2_defconfig                        |   1 -
>>>>>>     configs/rastaban_defconfig                    |   1 -
>>>>>>     configs/rut_defconfig                         |   1 -
>>>>>>     configs/sama5d3_xplained_nandflash_defconfig  |   1 -
>>>>>>     configs/sama5d3xek_nandflash_defconfig        |   1 -
>>>>>>     configs/sama5d4_xplained_nandflash_defconfig  |   1 -
>>>>>>     configs/sama5d4ek_nandflash_defconfig         |   1 -
>>>>>>     configs/sandbox64_defconfig                   |  10 +-
>>>>>>     configs/sandbox_defconfig                     |   9 +
>>>>>>     configs/sandbox_noinst_defconfig              |  21 +-
>>>>>>     configs/smartweb_defconfig                    |   1 -
>>>>>>     configs/socfpga_secu1_defconfig               |   2 +-
>>>>>>     configs/stm32746g-eval_spl_defconfig          |   2 +-
>>>>>>     configs/stm32f746-disco_spl_defconfig         |   2 +-
>>>>>>     configs/stm32f769-disco_spl_defconfig         |   2 +-
>>>>>>     configs/stm32mp15_basic_defconfig             |   2 +-
>>>>>>     configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
>>>>>>     configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
>>>>>>     configs/taurus_defconfig                      |   1 -
>>>>>>     configs/thuban_defconfig                      |   1 -
>>>>>>     drivers/mtd/Makefile                          |   2 +-
>>>>>>     drivers/mtd/nand/raw/Kconfig                  |  27 +-
>>>>>>     drivers/mtd/nand/raw/Makefile                 |   1 +
>>>>>>     drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
>>>>>>     drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
>>>>>>     drivers/mtd/nand/raw/denali_spl.c             |   5 +
>>>>>>     drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
>>>>>>     drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
>>>>>>     drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
>>>>>>     drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
>>>>>>     drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
>>>>>>     drivers/mtd/nand/raw/nand.c                   |  66 +-
>>>>>>     drivers/mtd/nand/raw/nand_base.c              |   7 +-
>>>>>>     drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
>>>>>>     drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
>>>>>>     drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
>>>>>>     drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
>>>>>>     drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
>>>>>>     drivers/mtd/onenand/onenand_uboot.c           |   2 -
>>>>>>     include/linux/mtd/mtd.h                       |  12 +
>>>>>>     include/mtd/cfi_flash.h                       |   2 +-
>>>>>>     include/nand.h                                |   3 +
>>>>>>     include/os.h                                  |  13 +
>>>>>>     include/system-constants.h                    |   3 +
>>>>>>     test/dm/Makefile                              |   1 +
>>>>>>     test/dm/nand.c                                | 106 +++
>>>>>>     test/image/Kconfig                            |   9 +
>>>>>>     test/image/Makefile                           |   1 +
>>>>>>     test/image/spl_load_nand.c                    |  54 ++
>>>>>>     102 files changed, 1269 insertions(+), 153 deletions(-)
>>>>>>     create mode 100644 drivers/mtd/nand/raw/sand_nand.c
>>>>>>     create mode 100644 test/dm/nand.c
>>>>>>     create mode 100644 test/image/spl_load_nand.c
>>>>>>
>>>>>> --
>>>>>> 2.37.1
>>>>>>
>>>>>
>>>>> The CI pipeline fails:
>>>>> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
>>>>>
>>>>> It seems like the problem is only for the clang tests (sandbox64 with
>>>>> clang test.py and sandbox with clang test.py)
>>>>
>>>> I can't view that link. Can you post the error somewhere else?
>>>>
>>>> I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.
>>>>
>>>> --Sean
>>>>
>>>> [1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355
>>>>
>>>>       sandbox:  +   sandbox
>>>> +drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
>>>> +        default:
>>>> +        ^~~~~~~
>>>> +drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
>>>> +        memcpy(buf, src + chip->column, to_copy);
>>>> +                    ^~~
>>>> +drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
>>>> +        if (!chip->selected)
>>>> +            ^~~~~~~~~~~~~~~
>>>> +drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
>>>> +        ^~~~~~~~~~~~~~~~~~~~
>>>> +drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
>>>> +        const u8 *src;
>>>> +                     ^
>>>> +                      = NULL
>>>> +2 errors generated.
>>>> +make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
>>>> +make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
>>>> +make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
>>>> +make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
>>>> +make[1]: *** [Makefile:1858: drivers] Error 2
>>>> +make: *** [Makefile:177: sub-make] Error 2
>>>>
>>>>
>>>
>>> +test/dm/nand.c:83:2: error: expression result unused [-Werror,-Wunused-value]
>>> +        ut_assertok(nand_erase_opts(mtd, &opts));
>>> +        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> +include/test/ut.h:309:27: note: expanded from macro 'ut_assertok'
>>> +#define ut_assertok(cond)       ut_asserteq(0, cond)
>>> +                                ^~~~~~~~~~~~~~~~~~~~
>>> +include/test/ut.h:162:2: note: expanded from macro 'ut_asserteq'
>>> +        __ret;                                                          \
>>> +        ^~~~~
>>> +1 error generated.
>>> +make[3]: *** [scripts/Makefile.build:257: test/dm/nand.o] Error 1
>>> +make[2]: *** [scripts/Makefile.build:397: test/dm] Error 2
>>> +make[1]: *** [Makefile:1858: test] Error 2
>>> +make: *** [Makefile:177: sub-make] Error 2
>>
>> ...well that's a new one. Wonder why it doesn't complain about all the other uses of
>> ut_assertok (which, invariably, discard the value in the same way).
> 
> That's what I thought as well. It seems to be related to CLANG. Could it be the
> 'unused-value' flag? I can't figure out why it causes the problem at
> that point,
> given that the 'ut_assertok()' is called even before the one that
> generates the error.

It was because of the comma instead of a semicolon on the previous line.

--Sean

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH 00/15] nand: Add sandbox tests
  2023-11-04 19:46           ` Sean Anderson
@ 2023-11-09  8:23             ` Dario Binacchi
  0 siblings, 0 replies; 34+ messages in thread
From: Dario Binacchi @ 2023-11-09  8:23 UTC (permalink / raw)
  To: Sean Anderson
  Cc: u-boot, Michael Trimarchi, Tom Rini, Daniel Schwierzeck,
	GSS_MTK_Uboot_upstream, Heiko Schocher, Peng Fan, Simon Glass,
	Weijie Gao

Sean, All

On Sat, Nov 4, 2023 at 8:46 PM Sean Anderson <seanga2@gmail.com> wrote:
>
> On 11/2/23 10:18, Dario Binacchi wrote:
> > On Thu, Nov 2, 2023 at 3:13 PM Sean Anderson <seanga2@gmail.com> wrote:
> >>
> >> On 11/2/23 10:08, Dario Binacchi wrote:
> >>> On Thu, Nov 2, 2023 at 3:06 PM Sean Anderson <seanga2@gmail.com> wrote:
> >>>>
> >>>> On 11/2/23 10:01, Dario Binacchi wrote:
> >>>>> Sean, All,
> >>>>>
> >>>>> On Sun, Oct 29, 2023 at 4:48 AM Sean Anderson <seanga2@gmail.com> wrote:
> >>>>>>
> >>>>>> This series tests raw nand flash in sandbox and fixes various bugs discovered in
> >>>>>> the process. I've tried to do things in a contemporary manner, avoiding the
> >>>>>> (numerous) variations present on only a few boards. The test is pretty minimal.
> >>>>>> Future work could test the rest of the nand API as well as the MTD API.
> >>>>>>
> >>>>>> Bloat at [1] (for boards with SPL_NAND_SUPPORT enabled). Almost
> >>>>>> everything grows by a few bytes due to nand_page_size. A few boards grow more,
> >>>>>> mostly those using nand_spl_loaders.c.
> >>>>>>
> >>>>>> [1] https://gist.github.com/Forty-Bot/9694f3401893c9e706ccc374922de6c2
> >>>>>>
> >>>>>>
> >>>>>> Sean Anderson (15):
> >>>>>>      spl: nand: Fix NULL-pointer dereference
> >>>>>>      nand: Don't dereference NULL manufacturer_desc
> >>>>>>      nand: Calculate SYS_NAND_PAGE_COUNT automatically
> >>>>>>      nand: spl_loaders: Only read enough pages to load the image
> >>>>>>      spl: legacy: Honor bl_len when decompressing
> >>>>>>      spl: nand: Set bl_len to page size
> >>>>>>      cmd: nand: Map memory before accessing it
> >>>>>>      spl: nand: Map memory before accessing it
> >>>>>>      mtd: Rename SPL_MTD_SUPPORT to SPL_MTD
> >>>>>>      mtd: Add some fallbacks for add/del_mtd_device
> >>>>>>      nand: Add function to unregister NAND devices
> >>>>>>      nand: Allow reinitialization
> >>>>>>      arch: sandbox: Add function to create temporary files
> >>>>>>      nand: Add sandbox driver
> >>>>>>      test: spl: Add a test for NAND
> >>>>>>
> >>>>>>     README                                        |   9 +-
> >>>>>>     arch/sandbox/cpu/os.c                         |  17 +
> >>>>>>     arch/sandbox/dts/test.dts                     |  67 ++
> >>>>>>     arch/sandbox/include/asm/spl.h                |   1 +
> >>>>>>     cmd/nand.c                                    |  26 +-
> >>>>>>     common/spl/Kconfig                            |   2 +-
> >>>>>>     common/spl/spl_legacy.c                       |  18 +-
> >>>>>>     common/spl/spl_nand.c                         |  22 +-
> >>>>>>     configs/am335x_baltos_defconfig               |   3 +-
> >>>>>>     configs/am335x_evm_defconfig                  |   3 +-
> >>>>>>     configs/am335x_evm_spiboot_defconfig          |   2 +-
> >>>>>>     configs/am335x_guardian_defconfig             |   1 -
> >>>>>>     configs/am335x_hs_evm_defconfig               |   2 +-
> >>>>>>     configs/am335x_hs_evm_uart_defconfig          |   2 +-
> >>>>>>     configs/am335x_igep003x_defconfig             |   3 +-
> >>>>>>     configs/am335x_sl50_defconfig                 |   2 +-
> >>>>>>     configs/am3517_evm_defconfig                  |   3 +-
> >>>>>>     configs/am43xx_evm_defconfig                  |   3 +-
> >>>>>>     configs/am43xx_evm_rtconly_defconfig          |   3 +-
> >>>>>>     configs/am43xx_evm_usbhost_boot_defconfig     |   3 +-
> >>>>>>     configs/am43xx_hs_evm_defconfig               |   3 +-
> >>>>>>     configs/am62ax_evm_r5_defconfig               |   2 +-
> >>>>>>     configs/am65x_evm_a53_defconfig               |   2 +-
> >>>>>>     configs/axm_defconfig                         |   1 -
> >>>>>>     configs/chiliboard_defconfig                  |   1 -
> >>>>>>     configs/cm_t43_defconfig                      |   2 +-
> >>>>>>     configs/corvus_defconfig                      |   1 -
> >>>>>>     configs/da850evm_nand_defconfig               |   1 -
> >>>>>>     configs/devkit3250_defconfig                  |   1 -
> >>>>>>     configs/devkit8000_defconfig                  |   1 -
> >>>>>>     configs/dra7xx_evm_defconfig                  |   1 -
> >>>>>>     configs/draco_defconfig                       |   1 -
> >>>>>>     configs/etamin_defconfig                      |   1 -
> >>>>>>     .../gardena-smart-gateway-at91sam_defconfig   |   1 -
> >>>>>>     configs/igep00x0_defconfig                    |   3 +-
> >>>>>>     configs/imx6ulz_smm_m2_defconfig              |   2 +-
> >>>>>>     configs/imx8mn_bsh_smm_s2_defconfig           |   2 +-
> >>>>>>     configs/j7200_evm_a72_defconfig               |   2 +-
> >>>>>>     configs/j7200_evm_r5_defconfig                |   2 +-
> >>>>>>     configs/j721e_evm_a72_defconfig               |   2 +-
> >>>>>>     configs/j721e_evm_r5_defconfig                |   2 +-
> >>>>>>     configs/j721s2_evm_a72_defconfig              |   2 +-
> >>>>>>     configs/j721s2_evm_r5_defconfig               |   2 +-
> >>>>>>     configs/m53menlo_defconfig                    |   1 -
> >>>>>>     configs/omap35_logic_defconfig                |   3 +-
> >>>>>>     configs/omap35_logic_somlv_defconfig          |   3 +-
> >>>>>>     configs/omap3_beagle_defconfig                |   3 +-
> >>>>>>     configs/omap3_evm_defconfig                   |   3 +-
> >>>>>>     configs/omap3_logic_defconfig                 |   3 +-
> >>>>>>     configs/omap3_logic_somlv_defconfig           |   3 +-
> >>>>>>     configs/omapl138_lcdk_defconfig               |   1 -
> >>>>>>     configs/phycore-am335x-r2-regor_defconfig     |   3 +-
> >>>>>>     configs/phycore-am335x-r2-wega_defconfig      |   3 +-
> >>>>>>     configs/pxm2_defconfig                        |   1 -
> >>>>>>     configs/rastaban_defconfig                    |   1 -
> >>>>>>     configs/rut_defconfig                         |   1 -
> >>>>>>     configs/sama5d3_xplained_nandflash_defconfig  |   1 -
> >>>>>>     configs/sama5d3xek_nandflash_defconfig        |   1 -
> >>>>>>     configs/sama5d4_xplained_nandflash_defconfig  |   1 -
> >>>>>>     configs/sama5d4ek_nandflash_defconfig         |   1 -
> >>>>>>     configs/sandbox64_defconfig                   |  10 +-
> >>>>>>     configs/sandbox_defconfig                     |   9 +
> >>>>>>     configs/sandbox_noinst_defconfig              |  21 +-
> >>>>>>     configs/smartweb_defconfig                    |   1 -
> >>>>>>     configs/socfpga_secu1_defconfig               |   2 +-
> >>>>>>     configs/stm32746g-eval_spl_defconfig          |   2 +-
> >>>>>>     configs/stm32f746-disco_spl_defconfig         |   2 +-
> >>>>>>     configs/stm32f769-disco_spl_defconfig         |   2 +-
> >>>>>>     configs/stm32mp15_basic_defconfig             |   2 +-
> >>>>>>     configs/stm32mp15_dhcom_basic_defconfig       |   2 +-
> >>>>>>     configs/stm32mp15_dhcor_basic_defconfig       |   2 +-
> >>>>>>     configs/taurus_defconfig                      |   1 -
> >>>>>>     configs/thuban_defconfig                      |   1 -
> >>>>>>     drivers/mtd/Makefile                          |   2 +-
> >>>>>>     drivers/mtd/nand/raw/Kconfig                  |  27 +-
> >>>>>>     drivers/mtd/nand/raw/Makefile                 |   1 +
> >>>>>>     drivers/mtd/nand/raw/am335x_spl_bch.c         |   8 +-
> >>>>>>     drivers/mtd/nand/raw/atmel_nand.c             |  10 +-
> >>>>>>     drivers/mtd/nand/raw/denali_spl.c             |   5 +
> >>>>>>     drivers/mtd/nand/raw/fsl_ifc_spl.c            |   8 +
> >>>>>>     drivers/mtd/nand/raw/lpc32xx_nand_mlc.c       |   5 +
> >>>>>>     drivers/mtd/nand/raw/mt7621_nand_spl.c        |   5 +
> >>>>>>     drivers/mtd/nand/raw/mxc_nand_spl.c           |  10 +-
> >>>>>>     drivers/mtd/nand/raw/mxs_nand_spl.c           |   5 +
> >>>>>>     drivers/mtd/nand/raw/nand.c                   |  66 +-
> >>>>>>     drivers/mtd/nand/raw/nand_base.c              |   7 +-
> >>>>>>     drivers/mtd/nand/raw/nand_spl_loaders.c       |   5 +-
> >>>>>>     drivers/mtd/nand/raw/nand_spl_simple.c        |  10 +-
> >>>>>>     drivers/mtd/nand/raw/omap_gpmc.c              |   3 +-
> >>>>>>     drivers/mtd/nand/raw/sand_nand.c              | 711 ++++++++++++++++++
> >>>>>>     drivers/mtd/nand/raw/sunxi_nand_spl.c         |   8 +-
> >>>>>>     drivers/mtd/onenand/onenand_uboot.c           |   2 -
> >>>>>>     include/linux/mtd/mtd.h                       |  12 +
> >>>>>>     include/mtd/cfi_flash.h                       |   2 +-
> >>>>>>     include/nand.h                                |   3 +
> >>>>>>     include/os.h                                  |  13 +
> >>>>>>     include/system-constants.h                    |   3 +
> >>>>>>     test/dm/Makefile                              |   1 +
> >>>>>>     test/dm/nand.c                                | 106 +++
> >>>>>>     test/image/Kconfig                            |   9 +
> >>>>>>     test/image/Makefile                           |   1 +
> >>>>>>     test/image/spl_load_nand.c                    |  54 ++
> >>>>>>     102 files changed, 1269 insertions(+), 153 deletions(-)
> >>>>>>     create mode 100644 drivers/mtd/nand/raw/sand_nand.c
> >>>>>>     create mode 100644 test/dm/nand.c
> >>>>>>     create mode 100644 test/image/spl_load_nand.c
> >>>>>>
> >>>>>> --
> >>>>>> 2.37.1
> >>>>>>
> >>>>>
> >>>>> The CI pipeline fails:
> >>>>> https://source.denx.de/u-boot/custodians/u-boot-nand-flash/-/pipelines/18409
> >>>>>
> >>>>> It seems like the problem is only for the clang tests (sandbox64 with
> >>>>> clang test.py and sandbox with clang test.py)
> >>>>
> >>>> I can't view that link. Can you post the error somewhere else?
> >>>>
> >>>> I saw a clang error [1] when running CI before sending this out, but I thought I had fixed it.
> >>>>
> >>>> --Sean
> >>>>
> >>>> [1] https://source.denx.de/u-boot/custodians/u-boot-clk/-/pipelines/18355
> >>>>
> >>>>       sandbox:  +   sandbox
> >>>> +drivers/mtd/nand/raw/sand_nand.c:386:2: error: variable 'src' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
> >>>> +        default:
> >>>> +        ^~~~~~~
> >>>> +drivers/mtd/nand/raw/sand_nand.c:395:14: note: uninitialized use occurs here
> >>>> +        memcpy(buf, src + chip->column, to_copy);
> >>>> +                    ^~~
> >>>> +drivers/mtd/nand/raw/sand_nand.c:362:6: error: variable 'src' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
> >>>> +        if (!chip->selected)
> >>>> +            ^~~~~~~~~~~~~~~
> >>>> +drivers/mtd/nand/raw/sand_nand.c:362:2: note: remove the 'if' if its condition is always false
> >>>> +        ^~~~~~~~~~~~~~~~~~~~
> >>>> +drivers/mtd/nand/raw/sand_nand.c:360:15: note: initialize the variable 'src' to silence this warning
> >>>> +        const u8 *src;
> >>>> +                     ^
> >>>> +                      = NULL
> >>>> +2 errors generated.
> >>>> +make[5]: *** [scripts/Makefile.build:256: drivers/mtd/nand/raw/sand_nand.o] Error 1
> >>>> +make[4]: *** [scripts/Makefile.build:397: drivers/mtd/nand/raw] Error 2
> >>>> +make[3]: *** [scripts/Makefile.build:397: drivers/mtd/nand] Error 2
> >>>> +make[2]: *** [scripts/Makefile.build:397: drivers/mtd] Error 2
> >>>> +make[1]: *** [Makefile:1858: drivers] Error 2
> >>>> +make: *** [Makefile:177: sub-make] Error 2
> >>>>
> >>>>
> >>>
> >>> +test/dm/nand.c:83:2: error: expression result unused [-Werror,-Wunused-value]
> >>> +        ut_assertok(nand_erase_opts(mtd, &opts));
> >>> +        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >>> +include/test/ut.h:309:27: note: expanded from macro 'ut_assertok'
> >>> +#define ut_assertok(cond)       ut_asserteq(0, cond)
> >>> +                                ^~~~~~~~~~~~~~~~~~~~
> >>> +include/test/ut.h:162:2: note: expanded from macro 'ut_asserteq'
> >>> +        __ret;                                                          \
> >>> +        ^~~~~
> >>> +1 error generated.
> >>> +make[3]: *** [scripts/Makefile.build:257: test/dm/nand.o] Error 1
> >>> +make[2]: *** [scripts/Makefile.build:397: test/dm] Error 2
> >>> +make[1]: *** [Makefile:1858: test] Error 2
> >>> +make: *** [Makefile:177: sub-make] Error 2
> >>
> >> ...well that's a new one. Wonder why it doesn't complain about all the other uses of
> >> ut_assertok (which, invariably, discard the value in the same way).
> >
> > That's what I thought as well. It seems to be related to CLANG. Could it be the
> > 'unused-value' flag? I can't figure out why it causes the problem at
> > that point,
> > given that the 'ut_assertok()' is called even before the one that
> > generates the error.
>
> It was because of the comma instead of a semicolon on the previous line.
Great!

5022c5112
< + opts.length = mtd->erasesize * 2,
---
> + opts.length = mtd->erasesize * 2;

Just before the ut_assertok() pointed by the test failure message.

And I confirm to you that the CI pipeline has now finished successfully.
Many thanks!

Regards
Dario
>
> --Sean



-- 

Dario Binacchi

Senior Embedded Linux Developer

dario.binacchi@amarulasolutions.com

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2023-11-09  8:23 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-29  3:48 [PATCH 00/15] nand: Add sandbox tests Sean Anderson
2023-10-29  3:48 ` [PATCH 01/15] spl: nand: Fix NULL-pointer dereference Sean Anderson
2023-10-29 14:15   ` Sean Anderson
2023-10-29  3:48 ` [PATCH 02/15] nand: Don't dereference NULL manufacturer_desc Sean Anderson
2023-10-29 13:06   ` Michael Nazzareno Trimarchi
2023-10-29  3:48 ` [PATCH 03/15] nand: Calculate SYS_NAND_PAGE_COUNT automatically Sean Anderson
2023-11-02  9:53   ` Dario Binacchi
2023-11-02 14:17     ` Sean Anderson
2023-11-02 14:19       ` Dario Binacchi
2023-10-29  3:48 ` [PATCH 04/15] nand: spl_loaders: Only read enough pages to load the image Sean Anderson
2023-10-29 13:08   ` Michael Nazzareno Trimarchi
2023-10-29  3:48 ` [PATCH 05/15] spl: legacy: Honor bl_len when decompressing Sean Anderson
2023-10-29  3:48 ` [PATCH 06/15] spl: nand: Set bl_len to page size Sean Anderson
2023-10-29  3:48 ` [PATCH 07/15] cmd: nand: Map memory before accessing it Sean Anderson
2023-10-29  3:48 ` [PATCH 08/15] spl: " Sean Anderson
2023-10-29  3:48 ` [PATCH 09/15] mtd: Rename SPL_MTD_SUPPORT to SPL_MTD Sean Anderson
2023-10-29  3:48 ` [PATCH 10/15] mtd: Add some fallbacks for add/del_mtd_device Sean Anderson
2023-11-02 10:45   ` Dario Binacchi
2023-10-29  3:48 ` [PATCH 11/15] nand: Add function to unregister NAND devices Sean Anderson
2023-11-02 10:58   ` Dario Binacchi
2023-10-29  3:48 ` [PATCH 12/15] nand: Allow reinitialization Sean Anderson
2023-11-02 11:08   ` Dario Binacchi
2023-10-29  3:48 ` [PATCH 13/15] arch: sandbox: Add function to create temporary files Sean Anderson
2023-10-29  3:48 ` [PATCH 14/15] nand: Add sandbox driver Sean Anderson
2023-11-02 11:15   ` Dario Binacchi
2023-11-02 13:54     ` Sean Anderson
2023-10-29  3:48 ` [PATCH 15/15] test: spl: Add a test for NAND Sean Anderson
2023-11-02 14:01 ` [PATCH 00/15] nand: Add sandbox tests Dario Binacchi
2023-11-02 14:05   ` Sean Anderson
2023-11-02 14:08     ` Dario Binacchi
2023-11-02 14:13       ` Sean Anderson
2023-11-02 14:18         ` Dario Binacchi
2023-11-04 19:46           ` Sean Anderson
2023-11-09  8:23             ` Dario Binacchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox