blob: 8834da032bdd82f5b78acf51d772e9972057a1a4 [file] [log] [blame]
wdenk5779d8d2003-12-06 23:55:10 +00001/* LowLevel function for DataFlash environment support
2 * Author : Gilles Gastaldi (Atmel)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
18 *
19 */
20#include <common.h>
21
22#if defined(CFG_ENV_IS_IN_DATAFLASH) /* Environment is in DataFlash */
23
24#include <command.h>
25#include <environment.h>
26#include <linux/stddef.h>
wdenk5779d8d2003-12-06 23:55:10 +000027#include <dataflash.h>
28
29env_t *env_ptr = NULL;
30
31char * env_name_spec = "dataflash";
32
33extern int read_dataflash (unsigned long addr, unsigned long size, char
34*result);
35extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
36 unsigned long size);
37extern int AT91F_DataflashInit (void);
38extern uchar default_environment[];
39/* extern int default_environment_size; */
40
41
42uchar env_get_char_spec (int index)
43{
44 uchar c;
45 read_dataflash (CFG_ENV_ADDR+index+offsetof(env_t,data),1,&c);
46 return (c);
47}
48
49void env_relocate_spec (void)
50{
51 read_dataflash (CFG_ENV_ADDR,CFG_ENV_SIZE,(uchar *)env_ptr);
52}
53
54int saveenv(void)
55{
56/* env must be copied to do not alter env structure in memory*/
57unsigned char temp[CFG_ENV_SIZE];
58int i;
59 memcpy(temp, env_ptr, CFG_ENV_SIZE);
60 return write_dataflash (CFG_ENV_ADDR, (unsigned long)temp, CFG_ENV_SIZE);
61}
62
63/************************************************************************
64 * Initialize Environment use
65 *
66 * We are still running from ROM, so data use is limited
67 * Use a (moderately small) buffer on the stack
68 */
69int env_init(void)
70{
71 DECLARE_GLOBAL_DATA_PTR;
72
73 ulong crc, len, new;
74 unsigned off;
75 uchar buf[64];
76 if (gd->env_valid == 0){
77 AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
78
79 /* read old CRC */
80 read_dataflash (CFG_ENV_ADDR+offsetof(env_t,crc),sizeof(ulong),&crc);
81 new = 0;
82 len = ENV_SIZE;
83 off = offsetof(env_t,data);
84 while (len > 0) {
85 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
86 read_dataflash (CFG_ENV_ADDR+off,n , buf);
87 new = crc32 (new, buf, n);
88 len -= n;
89 off += n;
90 }
91 if (crc == new) {
92 gd->env_addr = offsetof(env_t,data);
93 gd->env_valid = 1;
94 } else {
95 gd->env_addr = (ulong)&default_environment[0];
96 gd->env_valid = 0;
97 }
98 }
99
100 return (0);
101}
102
103#endif /* CFG_ENV_IS_IN_DATAFLASH */