1
2
3
4
5
6
7
8#include <common.h>
9#include <nand.h>
10#include <errno.h>
11#include <linux/mtd/concat.h>
12#include <linux/mtd/rawnand.h>
13
14#ifndef CONFIG_SYS_NAND_BASE_LIST
15#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
16#endif
17
18int nand_curr_device = -1;
19
20static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
21
22#ifndef CONFIG_SYS_NAND_SELF_INIT
23static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
24static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
25#endif
26
27static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
28
29static unsigned long total_nand_size;
30
31struct mtd_info *get_nand_dev_by_index(int dev)
32{
33 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
34 !nand_info[dev]->name)
35 return NULL;
36
37 return nand_info[dev];
38}
39
40int nand_mtd_to_devnum(struct mtd_info *mtd)
41{
42 int i;
43
44 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
45 if (mtd && get_nand_dev_by_index(i) == mtd)
46 return i;
47 }
48
49 return -ENODEV;
50}
51
52
53int nand_register(int devnum, struct mtd_info *mtd)
54{
55 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
56 return -EINVAL;
57
58 nand_info[devnum] = mtd;
59
60 sprintf(dev_name[devnum], "nand%d", devnum);
61 mtd->name = dev_name[devnum];
62
63#ifdef CONFIG_MTD
64
65
66
67
68 add_mtd_device(mtd);
69#endif
70
71 total_nand_size += mtd->size / 1024;
72
73 if (nand_curr_device == -1)
74 nand_curr_device = devnum;
75
76 return 0;
77}
78
79#if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
80static void nand_init_chip(int i)
81{
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;
86
87 if (maxchips < 1)
88 maxchips = 1;
89
90 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
91
92 if (board_nand_init(nand))
93 return;
94
95 if (nand_scan(mtd, maxchips))
96 return;
97
98 nand_register(i, mtd);
99}
100#endif
101
102#ifdef CONFIG_MTD_CONCAT
103static void create_mtd_concat(void)
104{
105 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
106 int nand_devices_found = 0;
107 int i;
108
109 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
110 struct mtd_info *mtd = get_nand_dev_by_index(i);
111 if (mtd != NULL) {
112 nand_info_list[nand_devices_found] = mtd;
113 nand_devices_found++;
114 }
115 }
116 if (nand_devices_found > 1) {
117 struct mtd_info *mtd;
118 char c_mtd_name[16];
119
120
121
122
123 sprintf(c_mtd_name, "nand%d", nand_devices_found);
124 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
125 c_mtd_name);
126
127 if (mtd == NULL)
128 return;
129
130 nand_register(nand_devices_found, mtd);
131 }
132
133 return;
134}
135#else
136static void create_mtd_concat(void)
137{
138}
139#endif
140
141unsigned long nand_size(void)
142{
143 return total_nand_size;
144}
145
146void nand_init(void)
147{
148 static int initialized;
149
150
151
152
153
154 if (initialized)
155 return;
156 initialized = 1;
157
158#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
159 board_nand_init();
160#else
161 int i;
162
163 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
164 nand_init_chip(i);
165#endif
166
167#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
168
169
170
171 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
172 nand_curr_device);
173#endif
174
175 create_mtd_concat();
176}
177