blob: 034e3231693e3b751dc41f6e181a9672f03d6540 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk86765902003-12-06 23:55:10 +00006 */
7#include <common.h>
wdenk86765902003-12-06 23:55:10 +00008#include <command.h>
9#include <environment.h>
10#include <linux/stddef.h>
wdenk86765902003-12-06 23:55:10 +000011#include <dataflash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020012#include <search.h>
13#include <errno.h>
wdenk86765902003-12-06 23:55:10 +000014
Wolfgang Denk6405a152006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000017env_t *env_ptr;
wdenk86765902003-12-06 23:55:10 +000018
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000019char *env_name_spec = "dataflash";
wdenk86765902003-12-06 23:55:10 +000020
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020021uchar env_get_char_spec(int index)
wdenk86765902003-12-06 23:55:10 +000022{
23 uchar c;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020024
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000025 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020026 1, (char *)&c);
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000027 return c;
wdenk86765902003-12-06 23:55:10 +000028}
29
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020030void env_relocate_spec(void)
wdenk86765902003-12-06 23:55:10 +000031{
Bo Shen5c7bad52013-10-08 16:30:21 +080032 ulong crc, new = 0;
33 unsigned off;
Tom Rinia840b8e2013-04-05 14:55:21 -040034 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020035
Bo Shen5c7bad52013-10-08 16:30:21 +080036 /* Read old CRC */
37 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
38 sizeof(ulong), (char *)&crc);
39
40 /* Read whole environment */
Tom Rinia840b8e2013-04-05 14:55:21 -040041 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
42
Bo Shen5c7bad52013-10-08 16:30:21 +080043 /* Calculate the CRC */
44 off = offsetof(env_t, data);
45 new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
46
47 if (crc == new)
48 env_import(buf, 1);
49 else
50 set_default_env("!bad CRC");
wdenk86765902003-12-06 23:55:10 +000051}
52
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020053#ifdef CONFIG_ENV_OFFSET_REDUND
54#error No support for redundant environment on dataflash yet!
55#endif
56
wdenk86765902003-12-06 23:55:10 +000057int saveenv(void)
58{
Marek Vasutd73c1292014-03-05 19:59:50 +010059 env_t env_new;
60 int ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020061
Marek Vasutd73c1292014-03-05 19:59:50 +010062 ret = env_export(&env_new);
63 if (ret)
64 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020065
66 return write_dataflash(CONFIG_ENV_ADDR,
Tom Rinia840b8e2013-04-05 14:55:21 -040067 (unsigned long)&env_new,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068 CONFIG_ENV_SIZE);
wdenk86765902003-12-06 23:55:10 +000069}
70
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071/*
72 * Initialize environment use
wdenk86765902003-12-06 23:55:10 +000073 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074 * We are still running from ROM, so data use is limited.
wdenk86765902003-12-06 23:55:10 +000075 * Use a (moderately small) buffer on the stack
76 */
77int env_init(void)
78{
Bo Shen5c7bad52013-10-08 16:30:21 +080079 /* use default */
80 gd->env_addr = (ulong)&default_environment[0];
81 gd->env_valid = 1;
wdenk86765902003-12-06 23:55:10 +000082
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020083 return 0;
wdenk86765902003-12-06 23:55:10 +000084}