Merge with /home/wd/git/u-boot/testing-NAND/ to add new NAND handling.
[kernel/u-boot.git] / common / env_flash.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6  * Andreas Heppel <aheppel@sysgo.de>
7
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 /* #define DEBUG */
28
29 #include <common.h>
30
31 #if defined(CFG_ENV_IS_IN_FLASH) /* Environment is in Flash */
32
33 #include <command.h>
34 #include <environment.h>
35 #include <linux/stddef.h>
36 #include <malloc.h>
37
38 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
39 #define CMD_SAVEENV
40 #elif defined(CFG_ENV_ADDR_REDUND)
41 #error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH
42 #endif
43
44 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
45 #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
46 #endif
47
48 #ifdef CONFIG_INFERNO
49 # ifdef CFG_ENV_ADDR_REDUND
50 #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
51 # endif
52 #endif
53
54 char * env_name_spec = "Flash";
55
56 #ifdef ENV_IS_EMBEDDED
57
58 extern uchar environment[];
59 env_t *env_ptr = (env_t *)(&environment[0]);
60
61 #ifdef CMD_SAVEENV
62 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
63 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
64 #endif
65
66 #else /* ! ENV_IS_EMBEDDED */
67
68 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
69 #ifdef CMD_SAVEENV
70 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
71 #endif
72
73 #endif /* ENV_IS_EMBEDDED */
74
75 #ifdef CFG_ENV_ADDR_REDUND
76 static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
77
78 /* CFG_ENV_ADDR is supposed to be on sector boundary */
79 static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
80 static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
81
82 #define ACTIVE_FLAG   1
83 #define OBSOLETE_FLAG 0
84 #endif /* CFG_ENV_ADDR_REDUND */
85
86 extern uchar default_environment[];
87 extern int default_environment_size;
88
89
90 uchar env_get_char_spec (int index)
91 {
92         DECLARE_GLOBAL_DATA_PTR;
93
94         return ( *((uchar *)(gd->env_addr + index)) );
95 }
96
97 #ifdef CFG_ENV_ADDR_REDUND
98
99 int  env_init(void)
100 {
101         DECLARE_GLOBAL_DATA_PTR;
102         int crc1_ok = 0, crc2_ok = 0;
103
104         uchar flag1 = flash_addr->flags;
105         uchar flag2 = flash_addr_new->flags;
106
107         ulong addr_default = (ulong)&default_environment[0];
108         ulong addr1 = (ulong)&(flash_addr->data);
109         ulong addr2 = (ulong)&(flash_addr_new->data);
110
111 #ifdef CONFIG_OMAP2420H4
112         int flash_probe(void);
113
114         if(flash_probe() == 0)
115                 goto bad_flash;
116 #endif
117
118         crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
119         crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
120
121         if (crc1_ok && ! crc2_ok) {
122                 gd->env_addr  = addr1;
123                 gd->env_valid = 1;
124         } else if (! crc1_ok && crc2_ok) {
125                 gd->env_addr  = addr2;
126                 gd->env_valid = 1;
127         } else if (! crc1_ok && ! crc2_ok) {
128                 gd->env_addr  = addr_default;
129                 gd->env_valid = 0;
130         } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
131                 gd->env_addr  = addr1;
132                 gd->env_valid = 1;
133         } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
134                 gd->env_addr  = addr2;
135                 gd->env_valid = 1;
136         } else if (flag1 == flag2) {
137                 gd->env_addr  = addr1;
138                 gd->env_valid = 2;
139         } else if (flag1 == 0xFF) {
140                 gd->env_addr  = addr1;
141                 gd->env_valid = 2;
142         } else if (flag2 == 0xFF) {
143                 gd->env_addr  = addr2;
144                 gd->env_valid = 2;
145         }
146
147 #ifdef CONFIG_OMAP2420H4
148 bad_flash:
149 #endif
150         return (0);
151 }
152
153 #ifdef CMD_SAVEENV
154 int saveenv(void)
155 {
156         char *saved_data = NULL;
157         int rc = 1;
158         char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
159 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
160         ulong up_data = 0;
161 #endif
162
163         debug ("Protect off %08lX ... %08lX\n",
164                 (ulong)flash_addr, end_addr);
165
166         if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
167                 goto Done;
168         }
169
170         debug ("Protect off %08lX ... %08lX\n",
171                 (ulong)flash_addr_new, end_addr_new);
172
173         if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
174                 goto Done;
175         }
176
177 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
178         up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
179         debug ("Data to save 0x%x\n", up_data);
180         if (up_data) {
181                 if ((saved_data = malloc(up_data)) == NULL) {
182                         printf("Unable to save the rest of sector (%ld)\n",
183                                 up_data);
184                         goto Done;
185                 }
186                 memcpy(saved_data,
187                         (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
188                 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
189                            (long)flash_addr_new + CFG_ENV_SIZE,
190                                 up_data, saved_data);
191         }
192 #endif
193         puts ("Erasing Flash...");
194         debug (" %08lX ... %08lX ...",
195                 (ulong)flash_addr_new, end_addr_new);
196
197         if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
198                 goto Done;
199         }
200
201         puts ("Writing to Flash... ");
202         debug (" %08lX ... %08lX ...",
203                 (ulong)&(flash_addr_new->data),
204                 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
205         if ((rc = flash_write((char *)env_ptr->data,
206                         (ulong)&(flash_addr_new->data),
207                         sizeof(env_ptr->data))) ||
208             (rc = flash_write((char *)&(env_ptr->crc),
209                         (ulong)&(flash_addr_new->crc),
210                         sizeof(env_ptr->crc))) ||
211             (rc = flash_write(&flag,
212                         (ulong)&(flash_addr->flags),
213                         sizeof(flash_addr->flags))) ||
214             (rc = flash_write(&new_flag,
215                         (ulong)&(flash_addr_new->flags),
216                         sizeof(flash_addr_new->flags))))
217         {
218                 flash_perror (rc);
219                 goto Done;
220         }
221         puts ("done\n");
222
223 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
224         if (up_data) { /* restore the rest of sector */
225                 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
226                            (long)flash_addr_new + CFG_ENV_SIZE, up_data);
227                 if (flash_write(saved_data,
228                                 (long)flash_addr_new + CFG_ENV_SIZE,
229                                 up_data)) {
230                         flash_perror(rc);
231                         goto Done;
232                 }
233         }
234 #endif
235         {
236                 env_t * etmp = flash_addr;
237                 ulong ltmp = end_addr;
238
239                 flash_addr = flash_addr_new;
240                 flash_addr_new = etmp;
241
242                 end_addr = end_addr_new;
243                 end_addr_new = ltmp;
244         }
245
246         rc = 0;
247 Done:
248
249         if (saved_data)
250                 free (saved_data);
251         /* try to re-protect */
252         (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
253         (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
254
255         return rc;
256 }
257 #endif /* CMD_SAVEENV */
258
259 #else /* ! CFG_ENV_ADDR_REDUND */
260
261 int  env_init(void)
262 {
263         DECLARE_GLOBAL_DATA_PTR;
264 #ifdef CONFIG_OMAP2420H4
265         int flash_probe(void);
266
267         if(flash_probe() == 0)
268                 goto bad_flash;
269 #endif
270         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
271                 gd->env_addr  = (ulong)&(env_ptr->data);
272                 gd->env_valid = 1;
273                 return(0);
274         }
275 #ifdef CONFIG_OMAP2420H4
276 bad_flash:
277 #endif
278         gd->env_addr  = (ulong)&default_environment[0];
279         gd->env_valid = 0;
280         return (0);
281 }
282
283 #ifdef CMD_SAVEENV
284
285 int saveenv(void)
286 {
287         int     len, rc;
288         ulong   end_addr;
289         ulong   flash_sect_addr;
290 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
291         ulong   flash_offset;
292         uchar   env_buffer[CFG_ENV_SECT_SIZE];
293 #else
294         uchar *env_buffer = (uchar *)env_ptr;
295 #endif  /* CFG_ENV_SECT_SIZE */
296         int rcode = 0;
297
298 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
299
300         flash_offset    = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
301         flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
302
303         debug ( "copy old content: "
304                 "sect_addr: %08lX  env_addr: %08lX  offset: %08lX\n",
305                 flash_sect_addr, (ulong)flash_addr, flash_offset);
306
307         /* copy old contents to temporary buffer */
308         memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
309
310         /* copy current environment to temporary buffer */
311         memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
312                 env_ptr,
313                 CFG_ENV_SIZE);
314
315         len      = CFG_ENV_SECT_SIZE;
316 #else
317         flash_sect_addr = (ulong)flash_addr;
318         len      = CFG_ENV_SIZE;
319 #endif  /* CFG_ENV_SECT_SIZE */
320
321 #ifndef CONFIG_INFERNO
322         end_addr = flash_sect_addr + len - 1;
323 #else
324         /* this is the last sector, and the size is hardcoded here */
325         /* otherwise we will get stack problems on loading 128 KB environment */
326         end_addr = flash_sect_addr + 0x20000 - 1;
327 #endif
328
329         debug ("Protect off %08lX ... %08lX\n",
330                 (ulong)flash_sect_addr, end_addr);
331
332         if (flash_sect_protect (0, flash_sect_addr, end_addr))
333                 return 1;
334
335         puts ("Erasing Flash...");
336         if (flash_sect_erase (flash_sect_addr, end_addr))
337                 return 1;
338
339         puts ("Writing to Flash... ");
340         rc = flash_write((char *)env_buffer, flash_sect_addr, len);
341         if (rc != 0) {
342                 flash_perror (rc);
343                 rcode = 1;
344         } else {
345                 puts ("done\n");
346         }
347
348         /* try to re-protect */
349         (void) flash_sect_protect (1, flash_sect_addr, end_addr);
350         return rcode;
351 }
352
353 #endif /* CMD_SAVEENV */
354
355 #endif /* CFG_ENV_ADDR_REDUND */
356
357 void env_relocate_spec (void)
358 {
359 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
360 #ifdef CFG_ENV_ADDR_REDUND
361         DECLARE_GLOBAL_DATA_PTR;
362
363         if (gd->env_addr != (ulong)&(flash_addr->data)) {
364                 env_t * etmp = flash_addr;
365                 ulong ltmp = end_addr;
366
367                 flash_addr = flash_addr_new;
368                 flash_addr_new = etmp;
369
370                 end_addr = end_addr_new;
371                 end_addr_new = ltmp;
372         }
373
374         if (flash_addr_new->flags != OBSOLETE_FLAG &&
375             crc32(0, flash_addr_new->data, ENV_SIZE) ==
376             flash_addr_new->crc) {
377                 char flag = OBSOLETE_FLAG;
378
379                 gd->env_valid = 2;
380                 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
381                 flash_write(&flag,
382                             (ulong)&(flash_addr_new->flags),
383                             sizeof(flash_addr_new->flags));
384                 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
385         }
386
387         if (flash_addr->flags != ACTIVE_FLAG &&
388             (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
389                 char flag = ACTIVE_FLAG;
390
391                 gd->env_valid = 2;
392                 flash_sect_protect (0, (ulong)flash_addr, end_addr);
393                 flash_write(&flag,
394                             (ulong)&(flash_addr->flags),
395                             sizeof(flash_addr->flags));
396                 flash_sect_protect (1, (ulong)flash_addr, end_addr);
397         }
398
399         if (gd->env_valid == 2)
400                 puts ("*** Warning - some problems detected "
401                       "reading environment; recovered successfully\n\n");
402 #endif /* CFG_ENV_ADDR_REDUND */
403         memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
404 #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
405 }
406
407 #endif /* CFG_ENV_IS_IN_FLASH */