blob: 0591b999ad63e7a810d432241283c3bf26e52684 [file] [log] [blame]
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001/*
2 * LowLevel function for DataFlash environment support
wdenk5779d8d2003-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
wdenk5779d8d2003-12-06 23:55:10 +000019 */
20#include <common.h>
wdenk5779d8d2003-12-06 23:55:10 +000021#include <command.h>
22#include <environment.h>
23#include <linux/stddef.h>
wdenk5779d8d2003-12-06 23:55:10 +000024#include <dataflash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020025#include <search.h>
26#include <errno.h>
wdenk5779d8d2003-12-06 23:55:10 +000027
Wolfgang Denkd87080b2006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
29
Igor Grinberg0901d9f2011-11-07 01:14:02 +000030env_t *env_ptr;
wdenk5779d8d2003-12-06 23:55:10 +000031
Igor Grinberg0901d9f2011-11-07 01:14:02 +000032char *env_name_spec = "dataflash";
Rob Herring60d7d5a2013-03-22 11:26:21 +000033static char env_buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020034
Wolfgang Denkea882ba2010-06-20 23:33:59 +020035uchar env_get_char_spec(int index)
wdenk5779d8d2003-12-06 23:55:10 +000036{
37 uchar c;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020038
Igor Grinberg0901d9f2011-11-07 01:14:02 +000039 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
Wolfgang Denkea882ba2010-06-20 23:33:59 +020040 1, (char *)&c);
Igor Grinberg0901d9f2011-11-07 01:14:02 +000041 return c;
wdenk5779d8d2003-12-06 23:55:10 +000042}
43
Wolfgang Denkea882ba2010-06-20 23:33:59 +020044void env_relocate_spec(void)
wdenk5779d8d2003-12-06 23:55:10 +000045{
Rob Herring60d7d5a2013-03-22 11:26:21 +000046 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, env_buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020047
Rob Herring60d7d5a2013-03-22 11:26:21 +000048 env_import(env_buf, 1);
wdenk5779d8d2003-12-06 23:55:10 +000049}
50
Wolfgang Denkea882ba2010-06-20 23:33:59 +020051#ifdef CONFIG_ENV_OFFSET_REDUND
52#error No support for redundant environment on dataflash yet!
53#endif
54
wdenk5779d8d2003-12-06 23:55:10 +000055int saveenv(void)
56{
Rob Herring60d7d5a2013-03-22 11:26:21 +000057 env_t *env_new = (env_t *)env_buf;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058 ssize_t len;
59 char *res;
60
Rob Herring60d7d5a2013-03-22 11:26:21 +000061 res = (char *)env_new->data;
Joe Hershbergerbe112352012-12-11 22:16:23 -060062 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020063 if (len < 0) {
64 error("Cannot export environment: errno = %d\n", errno);
65 return 1;
66 }
Rob Herring60d7d5a2013-03-22 11:26:21 +000067 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020068
69 return write_dataflash(CONFIG_ENV_ADDR,
Rob Herring60d7d5a2013-03-22 11:26:21 +000070 (unsigned long)env_new,
Wolfgang Denkea882ba2010-06-20 23:33:59 +020071 CONFIG_ENV_SIZE);
wdenk5779d8d2003-12-06 23:55:10 +000072}
73
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074/*
75 * Initialize environment use
wdenk5779d8d2003-12-06 23:55:10 +000076 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077 * We are still running from ROM, so data use is limited.
wdenk5779d8d2003-12-06 23:55:10 +000078 * Use a (moderately small) buffer on the stack
79 */
80int env_init(void)
81{
Igor Grinberg0901d9f2011-11-07 01:14:02 +000082 ulong crc, len = ENV_SIZE, new = 0;
wdenk5779d8d2003-12-06 23:55:10 +000083 unsigned off;
84 uchar buf[64];
wdenk5779d8d2003-12-06 23:55:10 +000085
Wolfgang Denkea882ba2010-06-20 23:33:59 +020086 if (gd->env_valid)
87 return 0;
88
89 AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
90
91 /* read old CRC */
92 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
93 sizeof(ulong), (char *)&crc);
94
Igor Grinberg0901d9f2011-11-07 01:14:02 +000095 off = offsetof(env_t, data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096 while (len > 0) {
97 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
98
99 read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
100
Igor Grinberg0901d9f2011-11-07 01:14:02 +0000101 new = crc32(new, buf, n);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200102 len -= n;
103 off += n;
wdenk5779d8d2003-12-06 23:55:10 +0000104 }
105
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200106 if (crc == new) {
Igor Grinberg0901d9f2011-11-07 01:14:02 +0000107 gd->env_addr = offsetof(env_t, data);
108 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200109 } else {
Igor Grinberg0901d9f2011-11-07 01:14:02 +0000110 gd->env_addr = (ulong)&default_environment[0];
111 gd->env_valid = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200112 }
113
114 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000115}