blob: 8f0a9210ec3683727bd673d485388da69fd61fef [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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022nand_info_t 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
33/* Register an initialized NAND mtd device with the U-Boot NAND command. */
34int nand_register(int devnum)
Wolfgang Denk932394a2005-08-17 12:55:25 +020035{
Scott Wood578931b2012-01-12 19:07:23 -060036 struct mtd_info *mtd;
37
38 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
39 return -EINVAL;
40
41 mtd = &nand_info[devnum];
42
43 sprintf(dev_name[devnum], "nand%d", devnum);
44 mtd->name = dev_name[devnum];
45
46#ifdef CONFIG_MTD_DEVICE
47 /*
48 * Add MTD device so that we can reference it later
49 * via the mtdcore infrastructure (e.g. ubi).
50 */
51 add_mtd_device(mtd);
52#endif
53
54 total_nand_size += mtd->size / 1024;
55
56 if (nand_curr_device == -1)
57 nand_curr_device = devnum;
58
59 return 0;
60}
61
62#ifndef CONFIG_SYS_NAND_SELF_INIT
63static void nand_init_chip(int i)
64{
65 struct mtd_info *mtd = &nand_info[i];
66 struct nand_chip *nand = &nand_chip[i];
67 ulong base_addr = base_address[i];
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +010068 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
69
70 if (maxchips < 1)
71 maxchips = 1;
Scott Wood578931b2012-01-12 19:07:23 -060072
Wolfgang Denk932394a2005-08-17 12:55:25 +020073 mtd->priv = nand;
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +010074 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roesedbe29e32009-04-24 15:59:35 +020075
Scott Wood578931b2012-01-12 19:07:23 -060076 if (board_nand_init(nand))
77 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +020078
Scott Wood578931b2012-01-12 19:07:23 -060079 if (nand_scan(mtd, maxchips))
80 return;
81
82 nand_register(i);
Wolfgang Denk932394a2005-08-17 12:55:25 +020083}
Scott Wood578931b2012-01-12 19:07:23 -060084#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020085
86void nand_init(void)
87{
Scott Wood578931b2012-01-12 19:07:23 -060088#ifdef CONFIG_SYS_NAND_SELF_INIT
89 board_nand_init();
90#else
Wolfgang Denk932394a2005-08-17 12:55:25 +020091 int i;
Scott Wood578931b2012-01-12 19:07:23 -060092
93 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
94 nand_init_chip(i);
95#endif
96
97 printf("%lu MiB\n", total_nand_size / 1024);
Stefan Roese43a2b0e2006-10-20 14:28:52 +020098
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200100 /*
101 * Select the chip in the board/cpu specific driver
102 */
103 board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device);
104#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200105}