blob: 3c5af37bf50c290afb33f111b945d539926bf0dd [file] [log] [blame]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001/*
2 * LowLevel function for DataFlash environment support
wdenk86765902003-12-06 23:55:10 +00003 * 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
wdenk86765902003-12-06 23:55:10 +000019 */
20#include <common.h>
wdenk86765902003-12-06 23:55:10 +000021#include <command.h>
22#include <environment.h>
23#include <linux/stddef.h>
wdenk86765902003-12-06 23:55:10 +000024#include <dataflash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020025#include <search.h>
26#include <errno.h>
wdenk86765902003-12-06 23:55:10 +000027
Wolfgang Denk6405a152006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
29
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000030env_t *env_ptr;
wdenk86765902003-12-06 23:55:10 +000031
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000032char *env_name_spec = "dataflash";
wdenk86765902003-12-06 23:55:10 +000033
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020034uchar env_get_char_spec(int index)
wdenk86765902003-12-06 23:55:10 +000035{
36 uchar c;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020037
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000038 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020039 1, (char *)&c);
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000040 return c;
wdenk86765902003-12-06 23:55:10 +000041}
42
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020043void env_relocate_spec(void)
wdenk86765902003-12-06 23:55:10 +000044{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020045 char buf[CONFIG_ENV_SIZE];
46
47 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
48
49 env_import(buf, 1);
wdenk86765902003-12-06 23:55:10 +000050}
51
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020052#ifdef CONFIG_ENV_OFFSET_REDUND
53#error No support for redundant environment on dataflash yet!
54#endif
55
wdenk86765902003-12-06 23:55:10 +000056int saveenv(void)
57{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 env_t env_new;
59 ssize_t len;
60 char *res;
61
62 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +010063 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020064 if (len < 0) {
65 error("Cannot export environment: errno = %d\n", errno);
66 return 1;
67 }
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000068 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069
70 return write_dataflash(CONFIG_ENV_ADDR,
71 (unsigned long)&env_new,
72 CONFIG_ENV_SIZE);
wdenk86765902003-12-06 23:55:10 +000073}
74
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020075/*
76 * Initialize environment use
wdenk86765902003-12-06 23:55:10 +000077 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078 * We are still running from ROM, so data use is limited.
wdenk86765902003-12-06 23:55:10 +000079 * Use a (moderately small) buffer on the stack
80 */
81int env_init(void)
82{
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000083 ulong crc, len = ENV_SIZE, new = 0;
wdenk86765902003-12-06 23:55:10 +000084 unsigned off;
85 uchar buf[64];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086
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);
wdenk86765902003-12-06 23:55:10 +000095
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000096 off = offsetof(env_t, data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 while (len > 0) {
98 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
99
100 read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
101
Igor Grinbergb90c1fc2011-11-07 01:14:02 +0000102 new = crc32(new, buf, n);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200103 len -= n;
104 off += n;
105 }
106
107 if (crc == new) {
Igor Grinbergb90c1fc2011-11-07 01:14:02 +0000108 gd->env_addr = offsetof(env_t, data);
109 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 } else {
Igor Grinbergb90c1fc2011-11-07 01:14:02 +0000111 gd->env_addr = (ulong)&default_environment[0];
112 gd->env_valid = 0;
wdenk86765902003-12-06 23:55:10 +0000113 }
114
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200115 return 0;
wdenk86765902003-12-06 23:55:10 +0000116}