]>
Commit | Line | Data |
---|---|---|
ea882baf WD |
1 | /* |
2 | * LowLevel function for DataFlash environment support | |
5779d8d9 WD |
3 | * Author : Gilles Gastaldi (Atmel) |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation; either version 2 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
18 | * MA 02111-1307 USA | |
5779d8d9 WD |
19 | */ |
20 | #include <common.h> | |
5779d8d9 WD |
21 | #include <command.h> |
22 | #include <environment.h> | |
23 | #include <linux/stddef.h> | |
5779d8d9 | 24 | #include <dataflash.h> |
ea882baf WD |
25 | #include <search.h> |
26 | #include <errno.h> | |
5779d8d9 | 27 | |
d87080b7 WD |
28 | DECLARE_GLOBAL_DATA_PTR; |
29 | ||
0901d9f8 | 30 | env_t *env_ptr; |
5779d8d9 | 31 | |
0901d9f8 | 32 | char *env_name_spec = "dataflash"; |
5779d8d9 | 33 | |
ea882baf | 34 | uchar env_get_char_spec(int index) |
5779d8d9 WD |
35 | { |
36 | uchar c; | |
ea882baf | 37 | |
0901d9f8 | 38 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
ea882baf | 39 | 1, (char *)&c); |
0901d9f8 | 40 | return c; |
5779d8d9 WD |
41 | } |
42 | ||
ea882baf | 43 | void env_relocate_spec(void) |
5779d8d9 | 44 | { |
ea882baf WD |
45 | char buf[CONFIG_ENV_SIZE]; |
46 | ||
47 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); | |
48 | ||
49 | env_import(buf, 1); | |
5779d8d9 WD |
50 | } |
51 | ||
ea882baf WD |
52 | #ifdef CONFIG_ENV_OFFSET_REDUND |
53 | #error No support for redundant environment on dataflash yet! | |
54 | #endif | |
55 | ||
5779d8d9 WD |
56 | int saveenv(void) |
57 | { | |
ea882baf WD |
58 | env_t env_new; |
59 | ssize_t len; | |
60 | char *res; | |
61 | ||
62 | res = (char *)&env_new.data; | |
37f2fe74 | 63 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
64 | if (len < 0) { |
65 | error("Cannot export environment: errno = %d\n", errno); | |
66 | return 1; | |
67 | } | |
0901d9f8 | 68 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
ea882baf WD |
69 | |
70 | return write_dataflash(CONFIG_ENV_ADDR, | |
71 | (unsigned long)&env_new, | |
72 | CONFIG_ENV_SIZE); | |
5779d8d9 WD |
73 | } |
74 | ||
ea882baf WD |
75 | /* |
76 | * Initialize environment use | |
5779d8d9 | 77 | * |
ea882baf | 78 | * We are still running from ROM, so data use is limited. |
5779d8d9 WD |
79 | * Use a (moderately small) buffer on the stack |
80 | */ | |
81 | int env_init(void) | |
82 | { | |
0901d9f8 | 83 | ulong crc, len = ENV_SIZE, new = 0; |
5779d8d9 WD |
84 | unsigned off; |
85 | uchar buf[64]; | |
ea882baf WD |
86 | |
87 | if (gd->env_valid) | |
88 | return 0; | |
89 | ||
90 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ | |
91 | ||
92 | /* read old CRC */ | |
93 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), | |
94 | sizeof(ulong), (char *)&crc); | |
95 | ||
0901d9f8 | 96 | off = offsetof(env_t, data); |
ea882baf WD |
97 | while (len > 0) { |
98 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; | |
99 | ||
100 | read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); | |
101 | ||
0901d9f8 | 102 | new = crc32(new, buf, n); |
ea882baf WD |
103 | len -= n; |
104 | off += n; | |
105 | } | |
106 | ||
107 | if (crc == new) { | |
0901d9f8 IG |
108 | gd->env_addr = offsetof(env_t, data); |
109 | gd->env_valid = 1; | |
ea882baf | 110 | } else { |
0901d9f8 IG |
111 | gd->env_addr = (ulong)&default_environment[0]; |
112 | gd->env_valid = 0; | |
5779d8d9 WD |
113 | } |
114 | ||
ea882baf | 115 | return 0; |
5779d8d9 | 116 | } |