blob: ddd8249d59a481a0e373f592c9b5ff1837731c56 [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>
Wolfgang Denk932394a2005-08-17 12:55:25 +020012
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020013#ifndef CONFIG_SYS_NAND_BASE_LIST
14#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Wolfgang Denk932394a2005-08-17 12:55:25 +020015#endif
16
Valeriy Glushkovad09ab22009-01-19 16:32:59 +020017DECLARE_GLOBAL_DATA_PTR;
18
Wolfgang Denk932394a2005-08-17 12:55:25 +020019int nand_curr_device = -1;
Scott Wood578931b2012-01-12 19:07:23 -060020
21
Scott Woodb616d9b2016-05-30 13:57:55 -050022struct 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
Scott Woodb616d9b2016-05-30 13:57:55 -050033int nand_mtd_to_devnum(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +020034{
Scott Woodb616d9b2016-05-30 13:57:55 -050035 int i;
Scott Wood578931b2012-01-12 19:07:23 -060036
Scott Woodb616d9b2016-05-30 13:57:55 -050037 for (i = 0; i < ARRAY_SIZE(nand_info); i++) {
38 if (mtd && nand_info[i] == mtd)
39 return i;
40 }
41
42 return -ENODEV;
43}
44
45/* Register an initialized NAND mtd device with the U-Boot NAND command. */
46int nand_register(int devnum, struct mtd_info *mtd)
47{
Scott Wood578931b2012-01-12 19:07:23 -060048 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
49 return -EINVAL;
50
Scott Woodb616d9b2016-05-30 13:57:55 -050051 nand_info[devnum] = mtd;
Scott Wood578931b2012-01-12 19:07:23 -060052
53 sprintf(dev_name[devnum], "nand%d", devnum);
54 mtd->name = dev_name[devnum];
55
56#ifdef CONFIG_MTD_DEVICE
57 /*
58 * Add MTD device so that we can reference it later
59 * via the mtdcore infrastructure (e.g. ubi).
60 */
61 add_mtd_device(mtd);
62#endif
63
64 total_nand_size += mtd->size / 1024;
65
66 if (nand_curr_device == -1)
67 nand_curr_device = devnum;
68
69 return 0;
70}
71
72#ifndef CONFIG_SYS_NAND_SELF_INIT
73static void nand_init_chip(int i)
74{
Scott Wood578931b2012-01-12 19:07:23 -060075 struct nand_chip *nand = &nand_chip[i];
Scott Woodb616d9b2016-05-30 13:57:55 -050076 struct mtd_info *mtd = &nand->mtd;
Scott Wood578931b2012-01-12 19:07:23 -060077 ulong base_addr = base_address[i];
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +010078 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
79
80 if (maxchips < 1)
81 maxchips = 1;
Scott Wood578931b2012-01-12 19:07:23 -060082
Wolfgang Denk932394a2005-08-17 12:55:25 +020083 mtd->priv = nand;
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
96void nand_init(void)
97{
Scott Wood578931b2012-01-12 19:07:23 -060098#ifdef CONFIG_SYS_NAND_SELF_INIT
99 board_nand_init();
100#else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200101 int i;
Scott Wood578931b2012-01-12 19:07:23 -0600102
103 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
104 nand_init_chip(i);
105#endif
106
107 printf("%lu MiB\n", total_nand_size / 1024);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200108
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200110 /*
111 * Select the chip in the board/cpu specific driver
112 */
Scott Woodb616d9b2016-05-30 13:57:55 -0500113 board_nand_select_device(nand_info[nand_curr_device]->priv,
114 nand_curr_device);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200115#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200116}