]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/nand/nand.c
ddd8249d59a481a0e373f592c9b5ff1837731c56
[people/ms/u-boot.git] / drivers / mtd / nand / nand.c
1 /*
2 * (C) Copyright 2005
3 * 2N Telekomunikace, a.s. <www.2n.cz>
4 * Ladislav Michl <michl@2n.cz>
5 *
6 * SPDX-License-Identifier: GPL-2.0
7 */
8
9 #include <common.h>
10 #include <nand.h>
11 #include <errno.h>
12
13 #ifndef CONFIG_SYS_NAND_BASE_LIST
14 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
15 #endif
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 int nand_curr_device = -1;
20
21
22 struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
23
24 #ifndef CONFIG_SYS_NAND_SELF_INIT
25 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
26 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
27 #endif
28
29 static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
30
31 static unsigned long total_nand_size; /* in kiB */
32
33 int nand_mtd_to_devnum(struct mtd_info *mtd)
34 {
35 int i;
36
37 for (i = 0; i < ARRAY_SIZE(nand_info); i++) {
38 if (mtd && nand_info[i] == mtd)
39 return i;
40 }
41
42 return -ENODEV;
43 }
44
45 /* Register an initialized NAND mtd device with the U-Boot NAND command. */
46 int nand_register(int devnum, struct mtd_info *mtd)
47 {
48 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
49 return -EINVAL;
50
51 nand_info[devnum] = mtd;
52
53 sprintf(dev_name[devnum], "nand%d", devnum);
54 mtd->name = dev_name[devnum];
55
56 #ifdef CONFIG_MTD_DEVICE
57 /*
58 * Add MTD device so that we can reference it later
59 * via the mtdcore infrastructure (e.g. ubi).
60 */
61 add_mtd_device(mtd);
62 #endif
63
64 total_nand_size += mtd->size / 1024;
65
66 if (nand_curr_device == -1)
67 nand_curr_device = devnum;
68
69 return 0;
70 }
71
72 #ifndef CONFIG_SYS_NAND_SELF_INIT
73 static void nand_init_chip(int i)
74 {
75 struct nand_chip *nand = &nand_chip[i];
76 struct mtd_info *mtd = &nand->mtd;
77 ulong base_addr = base_address[i];
78 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
79
80 if (maxchips < 1)
81 maxchips = 1;
82
83 mtd->priv = nand;
84 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
85
86 if (board_nand_init(nand))
87 return;
88
89 if (nand_scan(mtd, maxchips))
90 return;
91
92 nand_register(i, mtd);
93 }
94 #endif
95
96 void nand_init(void)
97 {
98 #ifdef CONFIG_SYS_NAND_SELF_INIT
99 board_nand_init();
100 #else
101 int i;
102
103 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
104 nand_init_chip(i);
105 #endif
106
107 printf("%lu MiB\n", total_nand_size / 1024);
108
109 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
110 /*
111 * Select the chip in the board/cpu specific driver
112 */
113 board_nand_select_device(nand_info[nand_curr_device]->priv,
114 nand_curr_device);
115 #endif
116 }