]>
Commit | Line | Data |
---|---|---|
5779d8d9 WD |
1 | /* 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> | |
5779d8d9 WD |
21 | #include <command.h> |
22 | #include <environment.h> | |
23 | #include <linux/stddef.h> | |
5779d8d9 WD |
24 | #include <dataflash.h> |
25 | ||
d87080b7 WD |
26 | DECLARE_GLOBAL_DATA_PTR; |
27 | ||
5779d8d9 WD |
28 | env_t *env_ptr = NULL; |
29 | ||
30 | char * env_name_spec = "dataflash"; | |
31 | ||
32 | extern int read_dataflash (unsigned long addr, unsigned long size, char | |
33 | *result); | |
34 | extern int write_dataflash (unsigned long addr_dest, unsigned long addr_src, | |
35 | unsigned long size); | |
36 | extern int AT91F_DataflashInit (void); | |
37 | extern uchar default_environment[]; | |
38 | /* extern int default_environment_size; */ | |
39 | ||
40 | ||
41 | uchar env_get_char_spec (int index) | |
42 | { | |
43 | uchar c; | |
9604b6e5 SP |
44 | read_dataflash(CFG_ENV_ADDR + index + offsetof(env_t,data), |
45 | 1, (char *)&c); | |
5779d8d9 WD |
46 | return (c); |
47 | } | |
48 | ||
49 | void env_relocate_spec (void) | |
50 | { | |
9604b6e5 | 51 | read_dataflash(CFG_ENV_ADDR, CFG_ENV_SIZE, (char *)env_ptr); |
5779d8d9 WD |
52 | } |
53 | ||
54 | int saveenv(void) | |
55 | { | |
9604b6e5 SP |
56 | /* env must be copied to do not alter env structure in memory*/ |
57 | unsigned char temp[CFG_ENV_SIZE]; | |
5779d8d9 | 58 | memcpy(temp, env_ptr, CFG_ENV_SIZE); |
9604b6e5 | 59 | return write_dataflash(CFG_ENV_ADDR, (unsigned long)temp, CFG_ENV_SIZE); |
5779d8d9 WD |
60 | } |
61 | ||
62 | /************************************************************************ | |
63 | * Initialize Environment use | |
64 | * | |
65 | * We are still running from ROM, so data use is limited | |
66 | * Use a (moderately small) buffer on the stack | |
67 | */ | |
68 | int env_init(void) | |
69 | { | |
5779d8d9 WD |
70 | ulong crc, len, new; |
71 | unsigned off; | |
72 | uchar buf[64]; | |
73 | if (gd->env_valid == 0){ | |
74 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ | |
75 | ||
76 | /* read old CRC */ | |
9604b6e5 SP |
77 | read_dataflash(CFG_ENV_ADDR + offsetof(env_t, crc), |
78 | sizeof(ulong), (char *)&crc); | |
5779d8d9 WD |
79 | new = 0; |
80 | len = ENV_SIZE; | |
81 | off = offsetof(env_t,data); | |
82 | while (len > 0) { | |
83 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; | |
9604b6e5 | 84 | read_dataflash(CFG_ENV_ADDR + off, n, (char *)buf); |
5779d8d9 WD |
85 | new = crc32 (new, buf, n); |
86 | len -= n; | |
87 | off += n; | |
88 | } | |
89 | if (crc == new) { | |
90 | gd->env_addr = offsetof(env_t,data); | |
91 | gd->env_valid = 1; | |
92 | } else { | |
93 | gd->env_addr = (ulong)&default_environment[0]; | |
94 | gd->env_valid = 0; | |
95 | } | |
96 | } | |
97 | ||
53677ef1 | 98 | return (0); |
5779d8d9 | 99 | } |