blob: 4896853530194ca0292ea83bf2bf139297b51275 [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
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>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010039#include <nand.h>
wdenk13a56952004-06-09 14:58:14 +000040
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
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058int nand_legacy_rw (struct nand_chip* nand, int cmd,
wdenk13a56952004-06-09 14:58:14 +000059 size_t start, size_t len,
60 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010061
62/* info for NAND chips, defined in drivers/nand/nand.c */
63extern nand_info_t nand_info[];
wdenk13a56952004-06-09 14:58:14 +000064
65/* references to names in env_common.c */
66extern uchar default_environment[];
67extern int default_environment_size;
68
69char * env_name_spec = "NAND";
70
71
72#ifdef ENV_IS_EMBEDDED
73extern uchar environment[];
74env_t *env_ptr = (env_t *)(&environment[0]);
75#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000076env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000077#endif /* ENV_IS_EMBEDDED */
78
79
80/* local functions */
81static void use_default(void);
82
83
84uchar 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 */
98int 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
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100109/*
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 */
wdenk13a56952004-06-09 14:58:14 +0000113int saveenv(void)
114{
115 int total, ret = 0;
wdenk13a56952004-06-09 14:58:14 +0000116
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100117 puts ("Erasing Nand...");
118 if (nand_erase(&nand_info[0], CFG_NEW_OFFSET, CFG_ENV_SIZE))
wdenk13a56952004-06-09 14:58:14 +0000119 return 1;
120
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100121 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;
wdenk13a56952004-06-09 14:58:14 +0000130}
131#endif /* CMD_SAVEENV */
132
133
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100134/*
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 */
wdenk13a56952004-06-09 14:58:14 +0000138void env_relocate_spec (void)
139{
140#if !defined(ENV_IS_EMBEDDED)
141 int ret, total;
142
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100143 total = CFG_ENV_SIZE;
144 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
145 (u_char*) env_ptr);
wdenk13a56952004-06-09 14:58:14 +0000146 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
155static 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 */