blob: d987f4c85c83539fe47ecf82b23650c1f8dbfab9 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-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 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020025#include <nand.h>
26
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020027#ifndef CONFIG_SYS_NAND_BASE_LIST
28#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020029#endif
30
Valeriy Glushkov1d012072009-01-19 16:32:59 +020031DECLARE_GLOBAL_DATA_PTR;
32
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020033int nand_curr_device = -1;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020034nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020035
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
37static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020038
39static const char default_nand_name[] = "nand";
Stefan Roese4157c632009-04-24 15:59:35 +020040static __attribute__((unused)) char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020042static void nand_init_chip(struct mtd_info *mtd, struct nand_chip *nand,
43 ulong base_addr)
44{
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +010045 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
Alexander Holler61a3d392010-12-18 02:16:28 +000046 static int __attribute__((unused)) i = 0;
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +010047
48 if (maxchips < 1)
49 maxchips = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050 mtd->priv = nand;
51
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010052 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Heiko Schocher3ec43662006-12-21 17:17:02 +010053 if (board_nand_init(nand) == 0) {
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +010054 if (nand_scan(mtd, maxchips) == 0) {
Heiko Schocher3ec43662006-12-21 17:17:02 +010055 if (!mtd->name)
56 mtd->name = (char *)default_nand_name;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020057#ifdef CONFIG_NEEDS_MANUAL_RELOC
Valeriy Glushkov1d012072009-01-19 16:32:59 +020058 else
59 mtd->name += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050060#endif
Stefan Roese4157c632009-04-24 15:59:35 +020061
Stefan Roese22ce6f92009-05-12 14:31:18 +020062#ifdef CONFIG_MTD_DEVICE
Stefan Roese4157c632009-04-24 15:59:35 +020063 /*
64 * Add MTD device so that we can reference it later
65 * via the mtdcore infrastructure (e.g. ubi).
66 */
67 sprintf(dev_name[i], "nand%d", i);
68 mtd->name = dev_name[i++];
69 add_mtd_device(mtd);
70#endif
Heiko Schocher3ec43662006-12-21 17:17:02 +010071 } else
72 mtd->name = NULL;
73 } else {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020074 mtd->name = NULL;
Heiko Schocher3ec43662006-12-21 17:17:02 +010075 mtd->size = 0;
76 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020077
78}
79
80void nand_init(void)
81{
82 int i;
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010083 unsigned int size = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020085 nand_init_chip(&nand_info[i], &nand_chip[i], base_address[i]);
Jason Jin3f088062008-09-19 17:32:49 +080086 size += nand_info[i].size / 1024;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020087 if (nand_curr_device == -1)
88 nand_curr_device = i;
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020089 }
Jason Jin3f088062008-09-19 17:32:49 +080090 printf("%u MiB\n", size / 1024);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020091
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020092#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese3cdd3fd2006-10-20 14:28:52 +020093 /*
94 * Select the chip in the board/cpu specific driver
95 */
96 board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device);
97#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020098}