blob: 3ea2dcfb7378bd30fe8955d083737e96d27bcb27 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
2 * (C) Copyright 2005
3 * 2N Telekomunikace, a.s. <www.2n.cz>
4 * Ladislav Michl <michl@2n.cz>
5 *
Tom Rini5b8031c2016-01-14 22:05:13 -05006 * SPDX-License-Identifier: GPL-2.0
Wolfgang Denk932394a2005-08-17 12:55:25 +02007 */
8
9#include <common.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020010#include <nand.h>
Scott Wood578931b2012-01-12 19:07:23 -060011#include <errno.h>
Heiko Schocher5d29e272016-06-07 08:55:41 +020012#include <linux/mtd/concat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020013
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020014#ifndef CONFIG_SYS_NAND_BASE_LIST
15#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Wolfgang Denk932394a2005-08-17 12:55:25 +020016#endif
17
Valeriy Glushkovad09ab22009-01-19 16:32:59 +020018DECLARE_GLOBAL_DATA_PTR;
19
Wolfgang Denk932394a2005-08-17 12:55:25 +020020int nand_curr_device = -1;
Scott Wood578931b2012-01-12 19:07:23 -060021
22
Scott Woodb616d9b2016-05-30 13:57:55 -050023struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk932394a2005-08-17 12:55:25 +020024
Scott Wood578931b2012-01-12 19:07:23 -060025#ifndef CONFIG_SYS_NAND_SELF_INIT
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
27static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
Scott Wood578931b2012-01-12 19:07:23 -060028#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020029
Scott Wood578931b2012-01-12 19:07:23 -060030static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
Wolfgang Denk932394a2005-08-17 12:55:25 +020031
Scott Wood578931b2012-01-12 19:07:23 -060032static unsigned long total_nand_size; /* in kiB */
33
Scott Woodb616d9b2016-05-30 13:57:55 -050034int nand_mtd_to_devnum(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +020035{
Scott Woodb616d9b2016-05-30 13:57:55 -050036 int i;
Scott Wood578931b2012-01-12 19:07:23 -060037
Scott Woodb616d9b2016-05-30 13:57:55 -050038 for (i = 0; i < ARRAY_SIZE(nand_info); i++) {
39 if (mtd && nand_info[i] == mtd)
40 return i;
41 }
42
43 return -ENODEV;
44}
45
46/* Register an initialized NAND mtd device with the U-Boot NAND command. */
47int nand_register(int devnum, struct mtd_info *mtd)
48{
Scott Wood578931b2012-01-12 19:07:23 -060049 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
50 return -EINVAL;
51
Scott Woodb616d9b2016-05-30 13:57:55 -050052 nand_info[devnum] = mtd;
Scott Wood578931b2012-01-12 19:07:23 -060053
54 sprintf(dev_name[devnum], "nand%d", devnum);
55 mtd->name = dev_name[devnum];
56
57#ifdef CONFIG_MTD_DEVICE
58 /*
59 * Add MTD device so that we can reference it later
60 * via the mtdcore infrastructure (e.g. ubi).
61 */
62 add_mtd_device(mtd);
63#endif
64
65 total_nand_size += mtd->size / 1024;
66
67 if (nand_curr_device == -1)
68 nand_curr_device = devnum;
69
70 return 0;
71}
72
73#ifndef CONFIG_SYS_NAND_SELF_INIT
74static void nand_init_chip(int i)
75{
Scott Wood578931b2012-01-12 19:07:23 -060076 struct nand_chip *nand = &nand_chip[i];
Scott Wood17cb4b82016-05-30 13:57:56 -050077 struct mtd_info *mtd = nand_to_mtd(nand);
Scott Wood578931b2012-01-12 19:07:23 -060078 ulong base_addr = base_address[i];
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +010079 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
80
81 if (maxchips < 1)
82 maxchips = 1;
Scott Wood578931b2012-01-12 19:07:23 -060083
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +010084 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roesedbe29e32009-04-24 15:59:35 +020085
Scott Wood578931b2012-01-12 19:07:23 -060086 if (board_nand_init(nand))
87 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +020088
Scott Wood578931b2012-01-12 19:07:23 -060089 if (nand_scan(mtd, maxchips))
90 return;
91
Scott Woodb616d9b2016-05-30 13:57:55 -050092 nand_register(i, mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +020093}
Scott Wood578931b2012-01-12 19:07:23 -060094#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020095
Heiko Schocher5d29e272016-06-07 08:55:41 +020096#ifdef CONFIG_MTD_CONCAT
97static void create_mtd_concat(void)
98{
99 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
100 int nand_devices_found = 0;
101 int i;
102
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++;
107 }
108 }
109 if (nand_devices_found > 1) {
110 struct mtd_info *mtd;
111 char c_mtd_name[16];
112
113 /*
114 * We detected multiple devices. Concatenate them together.
115 */
116 sprintf(c_mtd_name, "nand%d", nand_devices_found);
117 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
118 c_mtd_name);
119
120 if (mtd == NULL)
121 return;
122
123 nand_register(nand_devices_found, mtd);
124 }
125
126 return;
127}
128#else
129static void create_mtd_concat(void)
130{
131}
132#endif
133
Wolfgang Denk932394a2005-08-17 12:55:25 +0200134void nand_init(void)
135{
Hou Zhiqiangd72158c2017-03-17 16:12:30 +0800136 static int initialized;
137
138 /*
139 * Avoid initializing NAND Flash multiple times,
140 * otherwise it will calculate a wrong total size.
141 */
142 if (initialized)
143 return;
144 initialized = 1;
145
Scott Wood578931b2012-01-12 19:07:23 -0600146#ifdef CONFIG_SYS_NAND_SELF_INIT
147 board_nand_init();
148#else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200149 int i;
Scott Wood578931b2012-01-12 19:07:23 -0600150
151 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
152 nand_init_chip(i);
153#endif
154
155 printf("%lu MiB\n", total_nand_size / 1024);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200156
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200157#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200158 /*
159 * Select the chip in the board/cpu specific driver
160 */
Scott Wood17cb4b82016-05-30 13:57:56 -0500161 board_nand_select_device(mtd_to_nand(nand_info[nand_curr_device]),
Scott Woodb616d9b2016-05-30 13:57:55 -0500162 nand_curr_device);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200163#endif
Heiko Schocher5d29e272016-06-07 08:55:41 +0200164
165 create_mtd_concat();
Wolfgang Denk932394a2005-08-17 12:55:25 +0200166}