blob: 5f21d5c38e5b2c8eb7041c178ebe47e57420bb7e [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{
Tom Rinia840b8e2013-04-05 14:55:21 -040032 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020033
Tom Rinia840b8e2013-04-05 14:55:21 -040034 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
35
36 env_import(buf, 1);
wdenk86765902003-12-06 23:55:10 +000037}
38
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020039#ifdef CONFIG_ENV_OFFSET_REDUND
40#error No support for redundant environment on dataflash yet!
41#endif
42
wdenk86765902003-12-06 23:55:10 +000043int saveenv(void)
44{
Tom Rinia840b8e2013-04-05 14:55:21 -040045 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020046 ssize_t len;
47 char *res;
48
Tom Rinia840b8e2013-04-05 14:55:21 -040049 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -060050 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020051 if (len < 0) {
52 error("Cannot export environment: errno = %d\n", errno);
53 return 1;
54 }
Tom Rinia840b8e2013-04-05 14:55:21 -040055 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020056
57 return write_dataflash(CONFIG_ENV_ADDR,
Tom Rinia840b8e2013-04-05 14:55:21 -040058 (unsigned long)&env_new,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020059 CONFIG_ENV_SIZE);
wdenk86765902003-12-06 23:55:10 +000060}
61
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020062/*
63 * Initialize environment use
wdenk86765902003-12-06 23:55:10 +000064 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020065 * We are still running from ROM, so data use is limited.
wdenk86765902003-12-06 23:55:10 +000066 * Use a (moderately small) buffer on the stack
67 */
68int env_init(void)
69{
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000070 ulong crc, len = ENV_SIZE, new = 0;
wdenk86765902003-12-06 23:55:10 +000071 unsigned off;
72 uchar buf[64];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073
74 if (gd->env_valid)
75 return 0;
76
77 AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
78
79 /* read old CRC */
80 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
81 sizeof(ulong), (char *)&crc);
wdenk86765902003-12-06 23:55:10 +000082
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000083 off = offsetof(env_t, data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020084 while (len > 0) {
85 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
86
87 read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
88
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000089 new = crc32(new, buf, n);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020090 len -= n;
91 off += n;
92 }
93
94 if (crc == new) {
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000095 gd->env_addr = offsetof(env_t, data);
96 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 } else {
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000098 gd->env_addr = (ulong)&default_environment[0];
99 gd->env_valid = 0;
wdenk86765902003-12-06 23:55:10 +0000100 }
101
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200102 return 0;
wdenk86765902003-12-06 23:55:10 +0000103}