blob: eacd99c4e2756abf41fcda476acaef468f1f0c3c [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
8#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
Mugunthan V N7b670cc2017-06-26 19:12:51 -050044 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
45 if (mtd && get_nand_dev_by_index(i) == mtd)
Scott Wood2c1b7e12016-05-30 13:57:55 -050046 return i;
47 }
48
49 return -ENODEV;
50}
51
52/* Register an initialized NAND mtd device with the U-Boot NAND command. */
53int nand_register(int devnum, struct mtd_info *mtd)
54{
Scott Wood193a0f52012-01-12 19:07:23 -060055 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
56 return -EINVAL;
57
Scott Wood2c1b7e12016-05-30 13:57:55 -050058 nand_info[devnum] = mtd;
Scott Wood193a0f52012-01-12 19:07:23 -060059
60 sprintf(dev_name[devnum], "nand%d", devnum);
61 mtd->name = dev_name[devnum];
62
Miquel Raynal2e35dbb2019-10-03 19:50:05 +020063#ifdef CONFIG_MTD
Scott Wood193a0f52012-01-12 19:07:23 -060064 /*
65 * Add MTD device so that we can reference it later
66 * via the mtdcore infrastructure (e.g. ubi).
67 */
68 add_mtd_device(mtd);
69#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020070
Scott Wood193a0f52012-01-12 19:07:23 -060071 total_nand_size += mtd->size / 1024;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072
Scott Wood193a0f52012-01-12 19:07:23 -060073 if (nand_curr_device == -1)
74 nand_curr_device = devnum;
75
76 return 0;
77}
78
Tom Rini98372452021-12-12 22:12:36 -050079#if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
Scott Wood193a0f52012-01-12 19:07:23 -060080static void nand_init_chip(int i)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020081{
Scott Wood193a0f52012-01-12 19:07:23 -060082 struct nand_chip *nand = &nand_chip[i];
Scott Wood17fed142016-05-30 13:57:56 -050083 struct mtd_info *mtd = nand_to_mtd(nand);
Scott Wood193a0f52012-01-12 19:07:23 -060084 ulong base_addr = base_address[i];
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +010085 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
86
87 if (maxchips < 1)
88 maxchips = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020089
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010090 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roese4157c632009-04-24 15:59:35 +020091
Scott Wood193a0f52012-01-12 19:07:23 -060092 if (board_nand_init(nand))
93 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094
Scott Wood193a0f52012-01-12 19:07:23 -060095 if (nand_scan(mtd, maxchips))
96 return;
97
Scott Wood2c1b7e12016-05-30 13:57:55 -050098 nand_register(i, mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020099}
Scott Wood193a0f52012-01-12 19:07:23 -0600100#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200101
Heiko Schocheradb71df2016-06-07 08:55:41 +0200102#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++) {
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500110 struct mtd_info *mtd = get_nand_dev_by_index(i);
111 if (mtd != NULL) {
112 nand_info_list[nand_devices_found] = mtd;
Heiko Schocheradb71df2016-06-07 08:55:41 +0200113 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 * We detected multiple devices. Concatenate them together.
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
Hou Zhiqiang9c8b3c72017-03-17 16:12:31 +0800141unsigned long nand_size(void)
142{
143 return total_nand_size;
144}
145
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146void nand_init(void)
147{
Hou Zhiqiang5dac9d82017-03-17 16:12:30 +0800148 static int initialized;
149
150 /*
151 * Avoid initializing NAND Flash multiple times,
152 * otherwise it will calculate a wrong total size.
153 */
154 if (initialized)
155 return;
156 initialized = 1;
157
Tom Rini98372452021-12-12 22:12:36 -0500158#if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
Scott Wood193a0f52012-01-12 19:07:23 -0600159 board_nand_init();
160#else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161 int i;
Scott Wood193a0f52012-01-12 19:07:23 -0600162
163 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
164 nand_init_chip(i);
165#endif
166
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200167#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200168 /*
169 * Select the chip in the board/cpu specific driver
170 */
Mugunthan V N7b670cc2017-06-26 19:12:51 -0500171 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
Scott Wood2c1b7e12016-05-30 13:57:55 -0500172 nand_curr_device);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200173#endif
Heiko Schocheradb71df2016-06-07 08:55:41 +0200174
175 create_mtd_concat();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200176}