blob: ec3f79ad927e4b4a619f41a61490a475a9b5e89b [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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>
25#include <nand.h>
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
28#include <part.h>
29
30#include <boot_mode.h>
31
32
33#ifndef CONFIG_SYS_NAND_BASE_LIST
34#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
35#endif
36
37DECLARE_GLOBAL_DATA_PTR;
38
39static __attribute__((unused)) char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
40
41int nand_curr_device = -1;
42nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
43
44struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; //zhouqi
45static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
46
47static const char default_nand_name[] = "nand";
48struct flash_ops flash;
49
50
51/*******************************************************************************
52 * Function:
53 * Description:
54 * Parameters:
55 * Input:
56 *
57 * Output:
58 *
59 * Returns:
60 *
61 *
62 * Others:
63 ********************************************************************************/
64int board_nand_init(struct nand_chip *nand)
65{
66 int ret = 0;
67 int boot_flash_type = 0;
68
69 boot_flash_type = read_boot_flashtype();
70
71 if(boot_flash_type== IF_TYPE_NAND)
72 {
73 printf("nand: ");
74 ret = board_nand_init_denali(nand);
75 return ret;
76 }
77 else if(boot_flash_type == IF_TYPE_SPI_NAND)
78 {
79 printf("spi-nand: ");
80
81 ret = board_nand_init_spifc(nand);
82 return ret;
83 }
84 else if(boot_flash_type == IF_TYPE_NOR)
85 {
86 printf("spi-nor: ");
87 ret = fsl_qspi_probe();
88 return ret;
89 }
90 else
91 {
92 printf("boot_flash_type err\n");
93 return -1;
94 }
95
96 return 0;
97}
98
99struct mtd_info* get_mtd_info(void)
100{
101 if (-1 == nand_curr_device || nand_curr_device > CONFIG_SYS_MAX_NAND_DEVICE)
102 {
103 return NULL;
104 }
105
106 return &nand_info[nand_curr_device];
107}
108
109static void nand_init_chip(struct mtd_info *mtd, struct nand_chip *nand,
110 ulong base_addr)
111{
112 mtd->priv = nand;
113 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
114
115 if (board_nand_init(nand) == 0)
116 {
117 if (!mtd->name)
118 {
119 mtd->name = (char *)default_nand_name;
120 }
121
122#ifdef CONFIG_MTD_DEVICE
123 /*
124 * Add MTD device so that we can reference it later
125 * via the mtdcore infrastructure (e.g. ubi).
126 */
127 sprintf(dev_name[i], "nand%d", i);
128 mtd->name = dev_name[i++];
129 add_mtd_device(mtd);
130#endif
131 }
132 else
133 {
134 mtd->name = NULL;
135 mtd->size = 0;
136 }
137}
138
139void set_flash_opt(void)
140{
141 nand_info_t *nand = &nand_info[nand_curr_device];
142
143 flash.read = nand_read_skip_bad;
144 flash.read_no_ecc = nand_read_page_with_no_ecc;
145 flash.read_with_ecc = nand_read_page_with_ecc;
146 flash.write = nand_write_skip_bad;
147 flash.page_size = nand->writesize;
148 flash.erase = nand_erase_opts;
149 }
150
151struct flash_ops *get_flash_ops(void)
152{
153 struct flash_ops *m = NULL;
154 m = &flash;
155
156 return m;
157}
158extern int jffs2_lzma_init(void);
159
160void nand_init(void)
161{
162 int i;
163 unsigned int size = 0;
164 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
165 {
166 nand_init_chip(&nand_info[i], &nand_chip[i], base_address[i]);
167 size += nand_info[i].size / 1024;
168 if (nand_curr_device == -1)
169 {
170 nand_curr_device = i;
171 }
172 }
173 BOOT_PRINTF(UBOOT_NOTICE, "%u MiB.\n", (size / 1024));
174
175#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
176 /*
177 * Select the chip in the board/cpu specific driver
178 */
179 board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device);
180#endif
181
182 set_flash_opt();
183 jffs2_lzma_init();
184}