3 * 2N Telekomunikace, a.s. <www.2n.cz>
4 * Ladislav Michl <michl@2n.cz>
6 * SPDX-License-Identifier: GPL-2.0
12 #include <linux/mtd/concat.h>
14 #ifndef CONFIG_SYS_NAND_BASE_LIST
15 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
18 int nand_curr_device = -1;
20 static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
22 #ifndef CONFIG_SYS_NAND_SELF_INIT
23 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
24 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
27 static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
29 static unsigned long total_nand_size; /* in kiB */
31 struct mtd_info *get_nand_dev_by_index(int dev)
33 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
34 !nand_info[dev]->name)
37 return nand_info[dev];
40 int nand_mtd_to_devnum(struct mtd_info *mtd)
44 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
45 if (mtd && get_nand_dev_by_index(i) == mtd)
52 /* Register an initialized NAND mtd device with the U-Boot NAND command. */
53 int nand_register(int devnum, struct mtd_info *mtd)
55 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
58 nand_info[devnum] = mtd;
60 sprintf(dev_name[devnum], "nand%d", devnum);
61 mtd->name = dev_name[devnum];
63 #ifdef CONFIG_MTD_DEVICE
65 * Add MTD device so that we can reference it later
66 * via the mtdcore infrastructure (e.g. ubi).
71 total_nand_size += mtd->size / 1024;
73 if (nand_curr_device == -1)
74 nand_curr_device = devnum;
79 #ifndef CONFIG_SYS_NAND_SELF_INIT
80 static void nand_init_chip(int i)
82 struct nand_chip *nand = &nand_chip[i];
83 struct mtd_info *mtd = nand_to_mtd(nand);
84 ulong base_addr = base_address[i];
85 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
90 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
92 if (board_nand_init(nand))
95 if (nand_scan(mtd, maxchips))
98 nand_register(i, mtd);
102 #ifdef CONFIG_MTD_CONCAT
103 static void create_mtd_concat(void)
105 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
106 int nand_devices_found = 0;
109 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
110 struct mtd_info *mtd = get_nand_dev_by_index(i);
112 nand_info_list[nand_devices_found] = mtd;
113 nand_devices_found++;
116 if (nand_devices_found > 1) {
117 struct mtd_info *mtd;
121 * We detected multiple devices. Concatenate them together.
123 sprintf(c_mtd_name, "nand%d", nand_devices_found);
124 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
130 nand_register(nand_devices_found, mtd);
136 static void create_mtd_concat(void)
141 unsigned long nand_size(void)
143 return total_nand_size;
148 static int initialized;
151 * Avoid initializing NAND Flash multiple times,
152 * otherwise it will calculate a wrong total size.
158 #ifdef CONFIG_SYS_NAND_SELF_INIT
163 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
167 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
169 * Select the chip in the board/cpu specific driver
171 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),