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 DECLARE_GLOBAL_DATA_PTR;
20 int nand_curr_device = -1;
23 struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
25 #ifndef CONFIG_SYS_NAND_SELF_INIT
26 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
27 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
30 static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
32 static unsigned long total_nand_size; /* in kiB */
34 int nand_mtd_to_devnum(struct mtd_info *mtd)
38 for (i = 0; i < ARRAY_SIZE(nand_info); i++) {
39 if (mtd && nand_info[i] == mtd)
46 /* Register an initialized NAND mtd device with the U-Boot NAND command. */
47 int nand_register(int devnum, struct mtd_info *mtd)
49 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
52 nand_info[devnum] = mtd;
54 sprintf(dev_name[devnum], "nand%d", devnum);
55 mtd->name = dev_name[devnum];
57 #ifdef CONFIG_MTD_DEVICE
59 * Add MTD device so that we can reference it later
60 * via the mtdcore infrastructure (e.g. ubi).
65 total_nand_size += mtd->size / 1024;
67 if (nand_curr_device == -1)
68 nand_curr_device = devnum;
73 #ifndef CONFIG_SYS_NAND_SELF_INIT
74 static void nand_init_chip(int i)
76 struct nand_chip *nand = &nand_chip[i];
77 struct mtd_info *mtd = nand_to_mtd(nand);
78 ulong base_addr = base_address[i];
79 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
84 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
86 if (board_nand_init(nand))
89 if (nand_scan(mtd, maxchips))
92 nand_register(i, mtd);
96 #ifdef CONFIG_MTD_CONCAT
97 static void create_mtd_concat(void)
99 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
100 int nand_devices_found = 0;
103 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
104 if (nand_info[i] != NULL) {
105 nand_info_list[nand_devices_found] = nand_info[i];
106 nand_devices_found++;
109 if (nand_devices_found > 1) {
110 struct mtd_info *mtd;
114 * We detected multiple devices. Concatenate them together.
116 sprintf(c_mtd_name, "nand%d", nand_devices_found);
117 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
123 nand_register(nand_devices_found, mtd);
129 static void create_mtd_concat(void)
134 unsigned long nand_size(void)
136 return total_nand_size;
141 static int initialized;
144 * Avoid initializing NAND Flash multiple times,
145 * otherwise it will calculate a wrong total size.
151 #ifdef CONFIG_SYS_NAND_SELF_INIT
156 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
160 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
162 * Select the chip in the board/cpu specific driver
164 board_nand_select_device(mtd_to_nand(nand_info[nand_curr_device]),