]> git.sur5r.net Git - u-boot/blob - drivers/mtd/nand/nand.c
sandbox: spi: Remove an incorrect free()
[u-boot] / drivers / mtd / nand / nand.c
1 /*
2  * (C) Copyright 2005
3  * 2N Telekomunikace, a.s. <www.2n.cz>
4  * Ladislav Michl <michl@2n.cz>
5  *
6  * SPDX-License-Identifier:     GPL-2.0
7  */
8
9 #include <common.h>
10 #include <nand.h>
11 #include <errno.h>
12
13 #ifndef CONFIG_SYS_NAND_BASE_LIST
14 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
15 #endif
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 int nand_curr_device = -1;
20
21
22 nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
23
24 #ifndef CONFIG_SYS_NAND_SELF_INIT
25 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
26 static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
27 #endif
28
29 static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
30
31 static unsigned long total_nand_size; /* in kiB */
32
33 /* Register an initialized NAND mtd device with the U-Boot NAND command. */
34 int nand_register(int devnum)
35 {
36         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
63 static 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];
68         int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
69
70         if (maxchips < 1)
71                 maxchips = 1;
72
73         mtd->priv = nand;
74         nand->IO_ADDR_R = nand->IO_ADDR_W = (void  __iomem *)base_addr;
75
76         if (board_nand_init(nand))
77                 return;
78
79         if (nand_scan(mtd, maxchips))
80                 return;
81
82         nand_register(i);
83 }
84 #endif
85
86 void nand_init(void)
87 {
88 #ifdef CONFIG_SYS_NAND_SELF_INIT
89         board_nand_init();
90 #else
91         int i;
92
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);
98
99 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
100         /*
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
105 }