Re-factoring the legacy NAND code (legacy NAND now only in board-specific
[platform/kernel/u-boot.git] / common / env_nand.c
1 /*
2  * (C) Copyright 2004
3  * Jian Zhang, Texas Instruments, jzhang@ti.com.
4
5  * (C) Copyright 2000-2004
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  *
8  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Andreas Heppel <aheppel@sysgo.de>
10
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  */
29
30 /* #define DEBUG */
31
32 #include <common.h>
33
34 #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
35
36 #include <command.h>
37 #include <environment.h>
38 #include <linux/stddef.h>
39 #include <nand.h>
40
41 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
42 #define CMD_SAVEENV
43 #endif
44
45 #if defined(CFG_ENV_SIZE_REDUND)
46 #error CFG_ENV_SIZE_REDUND  not supported yet
47 #endif
48
49 #if defined(CFG_ENV_ADDR_REDUND)
50 #error CFG_ENV_ADDR_REDUND and CFG_ENV_IS_IN_NAND not supported yet
51 #endif
52
53
54 #ifdef CONFIG_INFERNO
55 #error CONFIG_INFERNO not supported yet
56 #endif
57
58 int nand_legacy_rw (struct nand_chip* nand, int cmd,
59             size_t start, size_t len,
60             size_t * retlen, u_char * buf);
61
62 /* info for NAND chips, defined in drivers/nand/nand.c */
63 extern nand_info_t nand_info[];
64
65 /* references to names in env_common.c */
66 extern uchar default_environment[];
67 extern int default_environment_size;
68
69 char * env_name_spec = "NAND";
70
71
72 #ifdef ENV_IS_EMBEDDED
73 extern uchar environment[];
74 env_t *env_ptr = (env_t *)(&environment[0]);
75 #else /* ! ENV_IS_EMBEDDED */
76 env_t *env_ptr = 0;
77 #endif /* ENV_IS_EMBEDDED */
78
79
80 /* local functions */
81 static void use_default(void);
82
83
84 uchar env_get_char_spec (int index)
85 {
86         DECLARE_GLOBAL_DATA_PTR;
87
88         return ( *((uchar *)(gd->env_addr + index)) );
89 }
90
91
92 /* this is called before nand_init()
93  * so we can't read Nand to validate env data.
94  * Mark it OK for now. env_relocate() in env_common.c
95  * will call our relocate function which will does
96  * the real validation.
97  */
98 int env_init(void)
99 {
100         DECLARE_GLOBAL_DATA_PTR;
101
102         gd->env_addr  = (ulong)&default_environment[0];
103         gd->env_valid = 1;
104
105         return (0);
106 }
107
108 #ifdef CMD_SAVEENV
109 /*
110  * The legacy NAND code saved the environment in the first NAND device i.e.,
111  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
112  */
113 int saveenv(void)
114 {
115         int     total, ret = 0;
116
117         puts ("Erasing Nand...");
118         if (nand_erase(&nand_info[0], CFG_NEW_OFFSET, CFG_ENV_SIZE))
119                 return 1;
120
121         puts ("Writing to Nand... ");
122         total = CFG_ENV_SIZE;
123         ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
124                         (u_char*) env_ptr);
125         if (ret || total != CFG_ENV_SIZE)
126                 return 1;
127
128         puts ("done\n");
129         return ret;
130 }
131 #endif /* CMD_SAVEENV */
132
133
134 /*
135  * The legacy NAND code saved the environment in the first NAND device i.e.,
136  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
137  */
138 void env_relocate_spec (void)
139 {
140 #if !defined(ENV_IS_EMBEDDED)
141         int ret, total;
142
143         total = CFG_ENV_SIZE;
144         ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
145                         (u_char*) env_ptr);
146         if (ret || total != CFG_ENV_SIZE)
147                 return use_default();
148
149         if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
150                 return use_default();
151 #endif /* ! ENV_IS_EMBEDDED */
152
153 }
154
155 static void use_default()
156 {
157         DECLARE_GLOBAL_DATA_PTR;
158
159         puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
160
161         if (default_environment_size > CFG_ENV_SIZE){
162                 puts ("*** Error - default environment is too large\n\n");
163                 return;
164         }
165
166         memset (env_ptr, 0, sizeof(env_t));
167         memcpy (env_ptr->data,
168                         default_environment,
169                         default_environment_size);
170         env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
171         gd->env_valid = 1;
172
173 }
174
175 #endif /* CFG_ENV_IS_IN_NAND */