]>
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 | ||
5779d8d9 WD |
30 | env_t *env_ptr = NULL; |
31 | ||
32 | char * env_name_spec = "dataflash"; | |
33 | ||
ea882baf WD |
34 | extern int read_dataflash(unsigned long addr, unsigned long size, |
35 | char *result); | |
36 | extern int write_dataflash(unsigned long addr_dest, | |
37 | unsigned long addr_src, unsigned long size); | |
38 | extern int AT91F_DataflashInit(void); | |
5779d8d9 | 39 | |
ea882baf | 40 | uchar env_get_char_spec(int index) |
5779d8d9 WD |
41 | { |
42 | uchar c; | |
ea882baf | 43 | |
0e8d1586 | 44 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t,data), |
ea882baf | 45 | 1, (char *)&c); |
5779d8d9 WD |
46 | return (c); |
47 | } | |
48 | ||
ea882baf | 49 | void env_relocate_spec(void) |
5779d8d9 | 50 | { |
ea882baf WD |
51 | char buf[CONFIG_ENV_SIZE]; |
52 | ||
53 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); | |
54 | ||
55 | env_import(buf, 1); | |
5779d8d9 WD |
56 | } |
57 | ||
ea882baf WD |
58 | #ifdef CONFIG_ENV_OFFSET_REDUND |
59 | #error No support for redundant environment on dataflash yet! | |
60 | #endif | |
61 | ||
5779d8d9 WD |
62 | int saveenv(void) |
63 | { | |
ea882baf WD |
64 | env_t env_new; |
65 | ssize_t len; | |
66 | char *res; | |
67 | ||
68 | res = (char *)&env_new.data; | |
37f2fe74 | 69 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
70 | if (len < 0) { |
71 | error("Cannot export environment: errno = %d\n", errno); | |
72 | return 1; | |
73 | } | |
74 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
75 | ||
76 | return write_dataflash(CONFIG_ENV_ADDR, | |
77 | (unsigned long)&env_new, | |
78 | CONFIG_ENV_SIZE); | |
5779d8d9 WD |
79 | } |
80 | ||
ea882baf WD |
81 | /* |
82 | * Initialize environment use | |
5779d8d9 | 83 | * |
ea882baf | 84 | * We are still running from ROM, so data use is limited. |
5779d8d9 WD |
85 | * Use a (moderately small) buffer on the stack |
86 | */ | |
87 | int env_init(void) | |
88 | { | |
5779d8d9 WD |
89 | ulong crc, len, new; |
90 | unsigned off; | |
91 | uchar buf[64]; | |
ea882baf WD |
92 | |
93 | if (gd->env_valid) | |
94 | return 0; | |
95 | ||
96 | AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ | |
97 | ||
98 | /* read old CRC */ | |
99 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), | |
100 | sizeof(ulong), (char *)&crc); | |
101 | ||
102 | new = 0; | |
103 | len = ENV_SIZE; | |
104 | off = offsetof(env_t,data); | |
105 | while (len > 0) { | |
106 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; | |
107 | ||
108 | read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); | |
109 | ||
110 | new = crc32 (new, buf, n); | |
111 | len -= n; | |
112 | off += n; | |
113 | } | |
114 | ||
115 | if (crc == new) { | |
116 | gd->env_addr = offsetof(env_t,data); | |
117 | gd->env_valid = 1; | |
118 | } else { | |
119 | gd->env_addr = (ulong)&default_environment[0]; | |
120 | gd->env_valid = 0; | |
5779d8d9 WD |
121 | } |
122 | ||
ea882baf | 123 | return 0; |
5779d8d9 | 124 | } |