blob: b591170346d04722c22c77a6131f9e5be77ef47b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002/*
3 * (C) Copyright 2005
4 * 2N Telekomunikace, a.s. <www.2n.cz>
5 * Ladislav Michl <michl@2n.cz>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 */
7
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009#include <nand.h>
Scott Wood193a0f52012-01-12 19:07:23 -060010#include <errno.h>
Heiko Schocheradb71df2016-06-07 08:55:41 +020011#include <linux/mtd/concat.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040012#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020013
Tom Rinib4213492022-11-12 17:36:51 -050014#ifndef CFG_SYS_NAND_BASE_LIST
15#define CFG_SYS_NAND_BASE_LIST { CFG_SYS_NAND_BASE }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020016#endif
17
18int nand_curr_device = -1;
Scott Wood193a0f52012-01-12 19:07:23 -060019
Grygorii Strashkoaa5528b2017-06-26 19:13:08 -050020static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020021
Roger Quadrosbdd58e22022-09-28 14:42:35 +030022#if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
Tom Rinib4213492022-11-12 17:36:51 -050024static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CFG_SYS_NAND_BASE_LIST;
Scott Wood193a0f52012-01-12 19:07:23 -060025#endif
26
27static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
28
29static unsigned long total_nand_size; /* in kiB */
30
Mugunthan V N7b670cc2017-06-26 19:12:51 -050031struct 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
Scott Wood2c1b7e12016-05-30 13:57:55 -050040int nand_mtd_to_devnum(struct mtd_info *mtd)
Scott Wood193a0f52012-01-12 19:07:23 -060041{
Scott Wood2c1b7e12016-05-30 13:57:55 -050042 int i;
Scott Wood193a0f52012-01-12 19:07:23 -060043
Dario Binacchi968d9882023-11-02 12:38:22 +010044 if (!mtd)
45 return -ENODEV;
46
Mugunthan V N7b670cc2017-06-26 19:12:51 -050047 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Dario Binacchi968d9882023-11-02 12:38:22 +010048 if (get_nand_dev_by_index(i) == mtd)
Scott Wood2c1b7e12016-05-30 13:57:55 -050049 return i;
50 }
51
52 return -ENODEV;
53}
54
55/* Register an initialized NAND mtd device with the U-Boot NAND command. */
56int nand_register(int devnum, struct mtd_info *mtd)
57{
Dario Binacchib11dec52023-11-02 12:27:33 +010058 if (!mtd || devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
Scott Wood193a0f52012-01-12 19:07:23 -060059 return -EINVAL;
60
Scott Wood2c1b7e12016-05-30 13:57:55 -050061 nand_info[devnum] = mtd;
Scott Wood193a0f52012-01-12 19:07:23 -060062
63 sprintf(dev_name[devnum], "nand%d", devnum);
64 mtd->name = dev_name[devnum];
65
Scott Wood193a0f52012-01-12 19:07:23 -060066 /*
67 * Add MTD device so that we can reference it later
68 * via the mtdcore infrastructure (e.g. ubi).
69 */
70 add_mtd_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020071
Scott Wood193a0f52012-01-12 19:07:23 -060072 total_nand_size += mtd->size / 1024;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020073
Scott Wood193a0f52012-01-12 19:07:23 -060074 if (nand_curr_device == -1)
75 nand_curr_device = devnum;
76
77 return 0;
78}
79
Sean Anderson96fc2052023-11-04 16:37:49 -040080void nand_unregister(struct mtd_info *mtd)
81{
82 int devnum = nand_mtd_to_devnum(mtd);
83
84 if (devnum < 0)
85 return;
86
87 if (nand_curr_device == devnum)
88 nand_curr_device = -1;
89
90 total_nand_size -= mtd->size / 1024;
91
92 del_mtd_device(nand_info[devnum]);
93
94 nand_info[devnum] = NULL;
95}
96
Tom Rini98372452021-12-12 22:12:36 -050097#if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
Scott Wood193a0f52012-01-12 19:07:23 -060098static void nand_init_chip(int i)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020099{
Scott Wood193a0f52012-01-12 19:07:23 -0600100 struct nand_chip *nand = &nand_chip[i];
Scott Wood17fed142016-05-30 13:57:56 -0500101 struct mtd_info *mtd = nand_to_mtd(nand);
Scott Wood193a0f52012-01-12 19:07:23 -0600102 ulong base_addr = base_address[i];
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +0100103 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
104
105 if (maxchips < 1)
106 maxchips = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200107
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100108 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roese4157c632009-04-24 15:59:35 +0200109
Scott Wood193a0f52012-01-12 19:07:23 -0600110 if (board_nand_init(nand))
111 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200112
Scott Wood193a0f52012-01-12 19:07:23 -0600113 if (nand_scan(mtd, maxchips))
114 return;
115
Scott Wood2c1b7e12016-05-30 13:57:55 -0500116 nand_register(i, mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200117}
Scott Wood193a0f52012-01-12 19:07:23 -0600118#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200119
Heiko Schocheradb71df2016-06-07 08:55:41 +0200120#ifdef CONFIG_MTD_CONCAT
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400121struct mtd_info *concat_mtd;
122
Heiko Schocheradb71df2016-06-07 08:55:41 +0200123static void create_mtd_concat(void)
124{
125 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
126 int nand_devices_found = 0;
127 int i;
128
129 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500130 struct mtd_info *mtd = get_nand_dev_by_index(i);
131 if (mtd != NULL) {
132 nand_info_list[nand_devices_found] = mtd;
Heiko Schocheradb71df2016-06-07 08:55:41 +0200133 nand_devices_found++;
134 }
135 }
136 if (nand_devices_found > 1) {
Heiko Schocheradb71df2016-06-07 08:55:41 +0200137 char c_mtd_name[16];
138
139 /*
140 * We detected multiple devices. Concatenate them together.
141 */
142 sprintf(c_mtd_name, "nand%d", nand_devices_found);
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400143 concat_mtd = mtd_concat_create(nand_info_list,
144 nand_devices_found, c_mtd_name);
Heiko Schocheradb71df2016-06-07 08:55:41 +0200145
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400146 if (!concat_mtd)
Heiko Schocheradb71df2016-06-07 08:55:41 +0200147 return;
148
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400149 nand_register(nand_devices_found, concat_mtd);
Heiko Schocheradb71df2016-06-07 08:55:41 +0200150 }
151
152 return;
153}
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400154
155static void destroy_mtd_concat(void)
156{
157 if (!concat_mtd)
158 return;
159
160 mtd_concat_destroy(concat_mtd);
161 concat_mtd = NULL;
162}
Heiko Schocheradb71df2016-06-07 08:55:41 +0200163#else
164static void create_mtd_concat(void)
165{
166}
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400167
168static void destroy_mtd_concat(void)
169{
170}
Heiko Schocheradb71df2016-06-07 08:55:41 +0200171#endif
172
Hou Zhiqiang9c8b3c72017-03-17 16:12:31 +0800173unsigned long nand_size(void)
174{
175 return total_nand_size;
176}
177
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400178static int initialized;
179
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200180void nand_init(void)
181{
Hou Zhiqiang5dac9d82017-03-17 16:12:30 +0800182 /*
183 * Avoid initializing NAND Flash multiple times,
184 * otherwise it will calculate a wrong total size.
185 */
186 if (initialized)
187 return;
188 initialized = 1;
189
Tom Rini98372452021-12-12 22:12:36 -0500190#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
Scott Wood193a0f52012-01-12 19:07:23 -0600191 board_nand_init();
192#else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200193 int i;
Scott Wood193a0f52012-01-12 19:07:23 -0600194
195 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
196 nand_init_chip(i);
197#endif
198
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200199#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200200 /*
201 * Select the chip in the board/cpu specific driver
202 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500203 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
Scott Wood2c1b7e12016-05-30 13:57:55 -0500204 nand_curr_device);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200205#endif
Heiko Schocheradb71df2016-06-07 08:55:41 +0200206
207 create_mtd_concat();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200208}
Sean Anderson8805f9d2023-11-04 16:37:44 -0400209
Sean Anderson28e2eaf2023-11-04 16:37:50 -0400210void nand_reinit(void)
211{
212 int i;
213
214 destroy_mtd_concat();
215 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
216 assert(!nand_info[i]);
217
218 initialized = 0;
219 nand_init();
220}
221
Sean Anderson8805f9d2023-11-04 16:37:44 -0400222unsigned int nand_page_size(void)
223{
224 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
225
226 return mtd ? mtd->writesize : 1;
227}