blob: 6aa909fdd9751b3b5a853d7b70c8d5e977fe28ea [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
Grygorii Strashko7a9dfe72017-06-26 19:13:08 -050022static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk932394a2005-08-17 12:55:25 +020023
Scott Wood578931b2012-01-12 19:07:23 -060024#ifndef CONFIG_SYS_NAND_SELF_INIT
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020025static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
26static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
Scott Wood578931b2012-01-12 19:07:23 -060027#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020028
Scott Wood578931b2012-01-12 19:07:23 -060029static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
Wolfgang Denk932394a2005-08-17 12:55:25 +020030
Scott Wood578931b2012-01-12 19:07:23 -060031static unsigned long total_nand_size; /* in kiB */
32
Mugunthan V Nad92dff2017-06-26 19:12:51 -050033struct mtd_info *get_nand_dev_by_index(int dev)
34{
35 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
36 !nand_info[dev]->name)
37 return NULL;
38
39 return nand_info[dev];
40}
41
Scott Woodb616d9b2016-05-30 13:57:55 -050042int nand_mtd_to_devnum(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +020043{
Scott Woodb616d9b2016-05-30 13:57:55 -050044 int i;
Scott Wood578931b2012-01-12 19:07:23 -060045
Mugunthan V Nad92dff2017-06-26 19:12:51 -050046 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
47 if (mtd && get_nand_dev_by_index(i) == mtd)
Scott Woodb616d9b2016-05-30 13:57:55 -050048 return i;
49 }
50
51 return -ENODEV;
52}
53
54/* Register an initialized NAND mtd device with the U-Boot NAND command. */
55int nand_register(int devnum, struct mtd_info *mtd)
56{
Scott Wood578931b2012-01-12 19:07:23 -060057 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
58 return -EINVAL;
59
Scott Woodb616d9b2016-05-30 13:57:55 -050060 nand_info[devnum] = mtd;
Scott Wood578931b2012-01-12 19:07:23 -060061
62 sprintf(dev_name[devnum], "nand%d", devnum);
63 mtd->name = dev_name[devnum];
64
65#ifdef CONFIG_MTD_DEVICE
66 /*
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);
71#endif
72
73 total_nand_size += mtd->size / 1024;
74
75 if (nand_curr_device == -1)
76 nand_curr_device = devnum;
77
78 return 0;
79}
80
81#ifndef CONFIG_SYS_NAND_SELF_INIT
82static void nand_init_chip(int i)
83{
Scott Wood578931b2012-01-12 19:07:23 -060084 struct nand_chip *nand = &nand_chip[i];
Scott Wood17cb4b82016-05-30 13:57:56 -050085 struct mtd_info *mtd = nand_to_mtd(nand);
Scott Wood578931b2012-01-12 19:07:23 -060086 ulong base_addr = base_address[i];
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +010087 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
88
89 if (maxchips < 1)
90 maxchips = 1;
Scott Wood578931b2012-01-12 19:07:23 -060091
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +010092 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roesedbe29e32009-04-24 15:59:35 +020093
Scott Wood578931b2012-01-12 19:07:23 -060094 if (board_nand_init(nand))
95 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +020096
Scott Wood578931b2012-01-12 19:07:23 -060097 if (nand_scan(mtd, maxchips))
98 return;
99
Scott Woodb616d9b2016-05-30 13:57:55 -0500100 nand_register(i, mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200101}
Scott Wood578931b2012-01-12 19:07:23 -0600102#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200103
Heiko Schocher5d29e272016-06-07 08:55:41 +0200104#ifdef CONFIG_MTD_CONCAT
105static void create_mtd_concat(void)
106{
107 struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
108 int nand_devices_found = 0;
109 int i;
110
111 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500112 struct mtd_info *mtd = get_nand_dev_by_index(i);
113 if (mtd != NULL) {
114 nand_info_list[nand_devices_found] = mtd;
Heiko Schocher5d29e272016-06-07 08:55:41 +0200115 nand_devices_found++;
116 }
117 }
118 if (nand_devices_found > 1) {
119 struct mtd_info *mtd;
120 char c_mtd_name[16];
121
122 /*
123 * We detected multiple devices. Concatenate them together.
124 */
125 sprintf(c_mtd_name, "nand%d", nand_devices_found);
126 mtd = mtd_concat_create(nand_info_list, nand_devices_found,
127 c_mtd_name);
128
129 if (mtd == NULL)
130 return;
131
132 nand_register(nand_devices_found, mtd);
133 }
134
135 return;
136}
137#else
138static void create_mtd_concat(void)
139{
140}
141#endif
142
Hou Zhiqiang203db382017-03-17 16:12:31 +0800143unsigned long nand_size(void)
144{
145 return total_nand_size;
146}
147
Wolfgang Denk932394a2005-08-17 12:55:25 +0200148void nand_init(void)
149{
Hou Zhiqiangd72158c2017-03-17 16:12:30 +0800150 static int initialized;
151
152 /*
153 * Avoid initializing NAND Flash multiple times,
154 * otherwise it will calculate a wrong total size.
155 */
156 if (initialized)
157 return;
158 initialized = 1;
159
Scott Wood578931b2012-01-12 19:07:23 -0600160#ifdef CONFIG_SYS_NAND_SELF_INIT
161 board_nand_init();
162#else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200163 int i;
Scott Wood578931b2012-01-12 19:07:23 -0600164
165 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
166 nand_init_chip(i);
167#endif
168
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200170 /*
171 * Select the chip in the board/cpu specific driver
172 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500173 board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
Scott Woodb616d9b2016-05-30 13:57:55 -0500174 nand_curr_device);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200175#endif
Heiko Schocher5d29e272016-06-07 08:55:41 +0200176
177 create_mtd_concat();
Wolfgang Denk932394a2005-08-17 12:55:25 +0200178}