[PATCH v1 01/12] nand: move NAND initialization API to nand/core.c

Sean Anderson sean.anderson at seco.com
Thu Dec 28 17:51:06 CET 2023


On 12/28/23 10:39, Alexey Romanov wrote:
> 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      | 136 +++++++++++++++++++++++++++++++++++
>  drivers/mtd/nand/raw/Kconfig |  10 ---
>  drivers/mtd/nand/raw/nand.c  | 134 ----------------------------------
>  include/nand.h               |   2 +
>  6 files changed, 149 insertions(+), 145 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 4b9dd6a926..ff298e3a0f 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>
> @@ -18,6 +19,12 @@
>  #include <linux/bitops.h>
>  #include <linux/mtd/nand.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
> @@ -250,6 +257,135 @@ 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);
> +
> +#ifdef CONFIG_MTD_CONCAT
> +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) {
> +		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);
> +
> +		if (mtd == NULL)
> +			return;
> +
> +		nand_register(nand_devices_found, mtd);
> +	}
> +
> +	return;
> +}
> +#else
> +static void create_mtd_concat(void)
> +{
> +}
> +#endif
> +
> +unsigned long nand_size(void)
> +{
> +	return total_nand_size;
> +}
> +EXPORT_SYMBOL_GPL(nand_size);
> +
> +void nand_init(void)
> +{
> +	static int initialized;
> +
> +	/*
> +	 * 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);
> +
>  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 f0100a601d..31949f1dff 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 eacd99c4e2..edca824d77 100644
> --- a/drivers/mtd/nand/raw/nand.c
> +++ b/drivers/mtd/nand/raw/nand.c
> @@ -15,68 +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];
> -
> -#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;
> -}
> -
> -#if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)

Please rebase on u-boot/next and also move nand_unregister.

--Sean

>  static void nand_init_chip(int i)
>  {
>  	struct nand_chip *nand = &nand_chip[i];
> @@ -98,79 +40,3 @@ static void nand_init_chip(int i)
>  	nand_register(i, mtd);
>  }
>  #endif
> -
> -#ifdef CONFIG_MTD_CONCAT
> -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) {
> -		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);
> -
> -		if (mtd == NULL)
> -			return;
> -
> -		nand_register(nand_devices_found, mtd);
> -	}
> -
> -	return;
> -}
> -#else
> -static void create_mtd_concat(void)
> -{
> -}
> -#endif
> -
> -unsigned long nand_size(void)
> -{
> -	return total_nand_size;
> -}
> -
> -void nand_init(void)
> -{
> -	static int initialized;
> -
> -	/*
> -	 * 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();
> -}
> diff --git a/include/nand.h b/include/nand.h
> index 70c1286ccb..16cfc2a24a 100644
> --- a/include/nand.h
> +++ b/include/nand.h
> @@ -25,6 +25,8 @@ int nand_register(int devnum, 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;



More information about the U-Boot mailing list