1 // SPDX-License-Identifier: GPL-2.0
4 * 2N Telekomunikace, a.s. <www.2n.cz>
5 * Ladislav Michl <michl@2n.cz>
11 #include <linux/mtd/concat.h>
13 #ifndef CONFIG_SYS_NAND_BASE_LIST
14 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
17 int nand_curr_device = -1;
19 static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
21 #ifndef CONFIG_SYS_NAND_SELF_INIT
22 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
23 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
26 static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
28 static unsigned long total_nand_size; /* in kiB */
30 struct mtd_info *get_nand_dev_by_index(int dev)
32 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
33 !nand_info[dev]->name)
36 return nand_info[dev];
39 int nand_mtd_to_devnum(struct mtd_info *mtd)
43 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
44 if (mtd && get_nand_dev_by_index(i) == mtd)
51 /* Register an initialized NAND mtd device with the U-Boot NAND command. */
52 int nand_register(int devnum, struct mtd_info *mtd)
54 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
57 nand_info[devnum] = mtd;
59 sprintf(dev_name[devnum], "nand%d", devnum);
60 mtd->name = dev_name[devnum];
62 #ifdef CONFIG_MTD_DEVICE
64 * Add MTD device so that we can reference it later
65 * via the mtdcore infrastructure (e.g. ubi).
70 total_nand_size += mtd->size / 1024;
72 if (nand_curr_device == -1)
73 nand_curr_device = devnum;
78 #ifndef CONFIG_SYS_NAND_SELF_INIT
79 static void nand_init_chip(int i)
81 struct nand_chip *nand = &nand_chip[i];
82 struct mtd_info *mtd = nand_to_mtd(nand);
83 ulong base_addr = base_address[i];
84 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
89 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
91 if (board_nand_init(nand))
94 if (nand_scan(mtd, maxchips))
97 nand_register(i, mtd);
101 #ifdef CONFIG_MTD_CONCAT
102 static void create_mtd_concat(void)
104 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
105 int nand_devices_found = 0;
108 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
109 struct mtd_info *mtd = get_nand_dev_by_index(i);
111 nand_info_list[nand_devices_found] = mtd;
112 nand_devices_found++;
115 if (nand_devices_found > 1) {
116 struct mtd_info *mtd;
120 * We detected multiple devices. Concatenate them together.
122 sprintf(c_mtd_name, "nand%d", nand_devices_found);
123 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
129 nand_register(nand_devices_found, mtd);
135 static void create_mtd_concat(void)
140 unsigned long nand_size(void)
142 return total_nand_size;
147 static int initialized;
150 * Avoid initializing NAND Flash multiple times,
151 * otherwise it will calculate a wrong total size.
157 #ifdef CONFIG_SYS_NAND_SELF_INIT
162 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
166 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
168 * Select the chip in the board/cpu specific driver
170 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),