blob: f449316853924b9f91eb0dd6bfbc1e7577b55e95 [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 *
Tom Rinie2378802016-01-14 22:05:13 -05006 * SPDX-License-Identifier: GPL-2.0
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02007 */
8
9#include <common.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020010#include <nand.h>
Scott Wood193a0f52012-01-12 19:07:23 -060011#include <errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020012
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020013#ifndef CONFIG_SYS_NAND_BASE_LIST
14#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015#endif
16
Valeriy Glushkov1d012072009-01-19 16:32:59 +020017DECLARE_GLOBAL_DATA_PTR;
18
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019int nand_curr_device = -1;
Scott Wood193a0f52012-01-12 19:07:23 -060020
21
Scott Wood2c1b7e12016-05-30 13:57:55 -050022struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023
Scott Wood193a0f52012-01-12 19:07:23 -060024#ifndef CONFIG_SYS_NAND_SELF_INIT
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Wood193a0f52012-01-12 19:07:23 -060027#endif
28
29static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
30
31static unsigned long total_nand_size; /* in kiB */
32
Scott Wood2c1b7e12016-05-30 13:57:55 -050033int nand_mtd_to_devnum(struct mtd_info *mtd)
Scott Wood193a0f52012-01-12 19:07:23 -060034{
Scott Wood2c1b7e12016-05-30 13:57:55 -050035 int i;
Scott Wood193a0f52012-01-12 19:07:23 -060036
Scott Wood2c1b7e12016-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 Wood193a0f52012-01-12 19:07:23 -060048 if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
49 return -EINVAL;
50
Scott Wood2c1b7e12016-05-30 13:57:55 -050051 nand_info[devnum] = mtd;
Scott Wood193a0f52012-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
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020063
Scott Wood193a0f52012-01-12 19:07:23 -060064 total_nand_size += mtd->size / 1024;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020065
Scott Wood193a0f52012-01-12 19:07:23 -060066 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)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020074{
Scott Wood193a0f52012-01-12 19:07:23 -060075 struct nand_chip *nand = &nand_chip[i];
Scott Wood17fed142016-05-30 13:57:56 -050076 struct mtd_info *mtd = nand_to_mtd(nand);
Scott Wood193a0f52012-01-12 19:07:23 -060077 ulong base_addr = base_address[i];
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +010078 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
79
80 if (maxchips < 1)
81 maxchips = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020082
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010083 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Stefan Roese4157c632009-04-24 15:59:35 +020084
Scott Wood193a0f52012-01-12 19:07:23 -060085 if (board_nand_init(nand))
86 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020087
Scott Wood193a0f52012-01-12 19:07:23 -060088 if (nand_scan(mtd, maxchips))
89 return;
90
Scott Wood2c1b7e12016-05-30 13:57:55 -050091 nand_register(i, mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020092}
Scott Wood193a0f52012-01-12 19:07:23 -060093#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094
95void nand_init(void)
96{
Scott Wood193a0f52012-01-12 19:07:23 -060097#ifdef CONFIG_SYS_NAND_SELF_INIT
98 board_nand_init();
99#else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200100 int i;
Scott Wood193a0f52012-01-12 19:07:23 -0600101
102 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
103 nand_init_chip(i);
104#endif
105
106 printf("%lu MiB\n", total_nand_size / 1024);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200109 /*
110 * Select the chip in the board/cpu specific driver
111 */
Scott Wood17fed142016-05-30 13:57:56 -0500112 board_nand_select_device(mtd_to_nand(nand_info[nand_curr_device]),
Scott Wood2c1b7e12016-05-30 13:57:55 -0500113 nand_curr_device);
Stefan Roese3cdd3fd2006-10-20 14:28:52 +0200114#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200115}