[PATCH v2 01/14] nand: move NAND initialization API to nand/core.c

Alexey Romanov avromanov at salutedevices.com
Tue Jan 9 18:32:13 CET 2024


nand_register() and nand_init() is generic API for both
RAW and SPI NAND's. We have to move this functions
from drivers/mtd/nand/raw/nand.c to drivers/mtd/nand/core.c.

Functions designed to work with RAW NAND should remain
in drivers/mtd/nand/raw/nand.c.

Signed-off-by: Alexey Romanov <avromanov at salutedevices.com>
---
 drivers/mtd/Kconfig          |   2 +-
 drivers/mtd/nand/Kconfig     |  10 ++
 drivers/mtd/nand/core.c      | 189 +++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/raw/Kconfig |  10 --
 drivers/mtd/nand/raw/nand.c  | 182 ---------------------------------
 include/nand.h               |   2 +
 6 files changed, 202 insertions(+), 193 deletions(-)

diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index c56840c849..1902351719 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -260,7 +260,7 @@ config SYS_NAND_MAX_ECCPOS
 
 config SYS_NAND_MAX_CHIPS
 	int "NAND max chips"
-	depends on MTD_RAW_NAND || CMD_ONENAND || TARGET_S5PC210_UNIVERSAL || \
+	depends on MTD_RAW_NAND || MTD_SPI_NAND || CMD_ONENAND || TARGET_S5PC210_UNIVERSAL || \
 		SPL_OMAP3_ID_NAND
 	default 1
 	help
diff --git a/drivers/mtd/nand/Kconfig b/drivers/mtd/nand/Kconfig
index 78ae04bdcb..9a1d4ac0dc 100644
--- a/drivers/mtd/nand/Kconfig
+++ b/drivers/mtd/nand/Kconfig
@@ -1,6 +1,16 @@
 config MTD_NAND_CORE
 	tristate
 
+config SYS_MAX_NAND_DEVICE
+	int "Maximum number of NAND devices to support"
+	default 1
+
+config SYS_NAND_SELF_INIT
+	bool
+	help
+	  This option, if enabled, provides more flexible and linux-like
+	  NAND initialization process.
+
 source "drivers/mtd/nand/raw/Kconfig"
 
 source "drivers/mtd/nand/spi/Kconfig"
diff --git a/drivers/mtd/nand/core.c b/drivers/mtd/nand/core.c
index f6d9c584f7..f792ef844a 100644
--- a/drivers/mtd/nand/core.c
+++ b/drivers/mtd/nand/core.c
@@ -10,6 +10,7 @@
 #define pr_fmt(fmt)	"nand: " fmt
 
 #include <common.h>
+#include <nand.h>
 #include <watchdog.h>
 #ifndef __UBOOT__
 #include <linux/compat.h>
@@ -19,6 +20,12 @@
 #include <linux/mtd/nand.h>
 #include <linux/printk.h>
 
+int nand_curr_device = -1;
+
+static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
+static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
+static unsigned long total_nand_size; /* in kiB */
+
 /**
  * nanddev_isbad() - Check if a block is bad
  * @nand: NAND device
@@ -251,6 +258,188 @@ void nanddev_cleanup(struct nand_device *nand)
 }
 EXPORT_SYMBOL_GPL(nanddev_cleanup);
 
+struct mtd_info *get_nand_dev_by_index(int dev)
+{
+	if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
+	    !nand_info[dev]->name)
+		return NULL;
+
+	return nand_info[dev];
+}
+EXPORT_SYMBOL_GPL(get_nand_dev_by_index);
+
+int nand_mtd_to_devnum(struct mtd_info *mtd)
+{
+	int i;
+
+	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
+		if (mtd && get_nand_dev_by_index(i) == mtd)
+			return i;
+	}
+
+	return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(nand_mtd_to_devnum);
+
+/* Register an initialized NAND mtd device with the U-Boot NAND command. */
+int nand_register(int devnum, struct mtd_info *mtd)
+{
+	if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
+		return -EINVAL;
+
+	nand_info[devnum] = 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;
+
+	if (nand_curr_device == -1)
+		nand_curr_device = devnum;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nand_register);
+
+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;
+}
+EXPORT_SYMBOL(nand_unregister);
+
+#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];
+	int nand_devices_found = 0;
+	int i;
+
+	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
+		struct mtd_info *mtd = get_nand_dev_by_index(i);
+		if (mtd != NULL) {
+			nand_info_list[nand_devices_found] = mtd;
+			nand_devices_found++;
+		}
+	}
+	if (nand_devices_found > 1) {
+		char c_mtd_name[16];
+
+		/*
+		 * We detected multiple devices. Concatenate them together.
+		 */
+		sprintf(c_mtd_name, "nand%d", nand_devices_found);
+		concat_mtd = mtd_concat_create(nand_info_list,
+					       nand_devices_found, c_mtd_name);
+
+		if (!concat_mtd)
+			return;
+
+		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)
+{
+	return total_nand_size;
+}
+EXPORT_SYMBOL(nand_size);
+
+static int initialized;
+
+void nand_init(void)
+{
+	/*
+	 * Avoid initializing NAND Flash multiple times,
+	 * otherwise it will calculate a wrong total size.
+	 */
+	if (initialized)
+		return;
+	initialized = 1;
+
+#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
+	board_nand_init();
+#else
+	int i;
+
+	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
+		nand_init_chip(i);
+#endif
+
+#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
+	/*
+	 * Select the chip in the board/cpu specific driver
+	 */
+	board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
+				 nand_curr_device);
+#endif
+
+	create_mtd_concat();
+}
+EXPORT_SYMBOL_GPL(nand_init);
+
+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();
+}
+EXPORT_SYMBOL(nand_reinit);
+
+unsigned int nand_page_size(void)
+{
+	struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
+
+	return mtd ? mtd->writesize : 1;
+}
+EXPORT_SYMBOL(nand_page_size);
+
 MODULE_DESCRIPTION("Generic NAND framework");
 MODULE_AUTHOR("Boris Brezillon <boris.brezillon at free-electrons.com>");
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index bb9994b862..c3003962f3 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -3,12 +3,6 @@ menuconfig MTD_RAW_NAND
 	bool "Raw NAND Device Support"
 if MTD_RAW_NAND
 
-config SYS_NAND_SELF_INIT
-	bool
-	help
-	  This option, if enabled, provides more flexible and linux-like
-	  NAND initialization process.
-
 config SPL_SYS_NAND_SELF_INIT
 	bool
 	depends on !SPL_NAND_SIMPLE
@@ -29,10 +23,6 @@ config TPL_NAND_INIT
 config SPL_NAND_INIT
 	bool
 
-config SYS_MAX_NAND_DEVICE
-	int "Maximum number of NAND devices to support"
-	default 1
-
 config SYS_NAND_DRIVER_ECC_LAYOUT
 	bool "Omit standard ECC layouts to save space"
 	help
diff --git a/drivers/mtd/nand/raw/nand.c b/drivers/mtd/nand/raw/nand.c
index 4c18861aa2..edca824d77 100644
--- a/drivers/mtd/nand/raw/nand.c
+++ b/drivers/mtd/nand/raw/nand.c
@@ -15,83 +15,10 @@
 #define CFG_SYS_NAND_BASE_LIST { CFG_SYS_NAND_BASE }
 #endif
 
-int nand_curr_device = -1;
-
-static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
-
 #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CFG_SYS_NAND_BASE_LIST;
-#endif
-
-static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
-
-static unsigned long total_nand_size; /* in kiB */
-
-struct mtd_info *get_nand_dev_by_index(int dev)
-{
-	if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
-	    !nand_info[dev]->name)
-		return NULL;
-
-	return nand_info[dev];
-}
-
-int nand_mtd_to_devnum(struct mtd_info *mtd)
-{
-	int i;
-
-	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
-		if (mtd && get_nand_dev_by_index(i) == mtd)
-			return i;
-	}
-
-	return -ENODEV;
-}
-
-/* Register an initialized NAND mtd device with the U-Boot NAND command. */
-int nand_register(int devnum, struct mtd_info *mtd)
-{
-	if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
-		return -EINVAL;
-
-	nand_info[devnum] = mtd;
-
-	sprintf(dev_name[devnum], "nand%d", devnum);
-	mtd->name = dev_name[devnum];
-
-	/*
-	 * Add MTD device so that we can reference it later
-	 * via the mtdcore infrastructure (e.g. ubi).
-	 */
-	add_mtd_device(mtd);
 
-	total_nand_size += mtd->size / 1024;
-
-	if (nand_curr_device == -1)
-		nand_curr_device = devnum;
-
-	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)
 {
 	struct nand_chip *nand = &nand_chip[i];
@@ -113,112 +40,3 @@ static void nand_init_chip(int i)
 	nand_register(i, mtd);
 }
 #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];
-	int nand_devices_found = 0;
-	int i;
-
-	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
-		struct mtd_info *mtd = get_nand_dev_by_index(i);
-		if (mtd != NULL) {
-			nand_info_list[nand_devices_found] = mtd;
-			nand_devices_found++;
-		}
-	}
-	if (nand_devices_found > 1) {
-		char c_mtd_name[16];
-
-		/*
-		 * We detected multiple devices. Concatenate them together.
-		 */
-		sprintf(c_mtd_name, "nand%d", nand_devices_found);
-		concat_mtd = mtd_concat_create(nand_info_list,
-					       nand_devices_found, c_mtd_name);
-
-		if (!concat_mtd)
-			return;
-
-		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)
-{
-	return total_nand_size;
-}
-
-static int initialized;
-
-void nand_init(void)
-{
-	/*
-	 * Avoid initializing NAND Flash multiple times,
-	 * otherwise it will calculate a wrong total size.
-	 */
-	if (initialized)
-		return;
-	initialized = 1;
-
-#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
-	board_nand_init();
-#else
-	int i;
-
-	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
-		nand_init_chip(i);
-#endif
-
-#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
-	/*
-	 * Select the chip in the board/cpu specific driver
-	 */
-	board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
-				 nand_curr_device);
-#endif
-
-	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);
-
-	return mtd ? mtd->writesize : 1;
-}
diff --git a/include/nand.h b/include/nand.h
index 220ffa202e..766cd04aea 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -28,6 +28,8 @@ void nand_unregister(struct mtd_info *mtd);
 struct nand_chip;
 
 extern int board_nand_init(struct nand_chip *nand);
+
+void nand_init_chip(int index);
 #endif
 
 extern int nand_curr_device;
-- 
2.30.1



More information about the U-Boot mailing list