blob: 0f4b491fd3b688868d21fcae80a918565fcbda37 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00009 */
10
11/*
12 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
13 *
14 * It might not be possible in all cases to use 'memcpy()' to copy
15 * the environment to NVRAM, as the NVRAM might not be mapped into
16 * the memory space. (I.e. this is the case for the BAB750). In those
17 * cases it might be possible to access the NVRAM using a different
18 * method. For example, the RTC on the BAB750 is accessible in IO
19 * space using its address and data registers. To enable usage of
20 * NVRAM in those cases I invented the functions 'nvram_read()' and
21 * 'nvram_write()', which will be activated upon the configuration
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
wdenkc6097192002-11-03 00:24:07 +000023 * strongly dependent on the used HW, and must be redefined for each
24 * board that wants to use them.
25 */
26
27#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000028#include <command.h>
29#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000030#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020031#include <search.h>
32#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000033
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020034DECLARE_GLOBAL_DATA_PTR;
35
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020036#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000037extern void *nvram_read(void *dest, const long src, size_t count);
38extern void nvram_write(long dest, const void *src, size_t count);
Igor Grinberg91494ca2011-11-07 01:14:04 +000039env_t *env_ptr;
wdenkc6097192002-11-03 00:24:07 +000040#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020041env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000042#endif
43
Igor Grinbergbf95df42011-12-26 03:33:10 +000044#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Simon Glasse5bce242017-08-03 12:22:01 -060045static uchar env_nvram_get_char(int index)
wdenkc6097192002-11-03 00:24:07 +000046{
wdenkc6097192002-11-03 00:24:07 +000047 uchar c;
48
Igor Grinberg91494ca2011-11-07 01:14:04 +000049 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
wdenkc6097192002-11-03 00:24:07 +000050
51 return c;
wdenkc6097192002-11-03 00:24:07 +000052}
Igor Grinbergbf95df42011-12-26 03:33:10 +000053#endif
wdenkc6097192002-11-03 00:24:07 +000054
Simon Glasse5bce242017-08-03 12:22:01 -060055static void env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000056{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040057 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020059#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +020060 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000061#else
Tom Rinicd0f4fa2013-04-05 14:55:21 -040062 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000063#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020064 env_import(buf, 1);
wdenkc6097192002-11-03 00:24:07 +000065}
66
Simon Glasse5bce242017-08-03 12:22:01 -060067static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000068{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040069 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020070 int rcode = 0;
71
Marek Vasut7ce15262014-03-05 19:59:50 +010072 rcode = env_export(&env_new);
73 if (rcode)
74 return rcode;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020076#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Tom Rinicd0f4fa2013-04-05 14:55:21 -040077 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
78#else
79 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
80 rcode = 1;
wdenkc6097192002-11-03 00:24:07 +000081#endif
82 return rcode;
83}
84
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085/*
wdenkc6097192002-11-03 00:24:07 +000086 * Initialize Environment use
87 *
88 * We are still running from ROM, so data use is limited
89 */
Simon Glasse5bce242017-08-03 12:22:01 -060090static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000091{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +000093 ulong crc;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040094 uchar data[ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020095
96 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg91494ca2011-11-07 01:14:04 +000097 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000098
99 if (crc32(0, data, ENV_SIZE) == crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000100 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000101#else
102 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000103 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +0000104#endif
Simon Glass203e94f2017-08-03 12:21:56 -0600105 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +0000106 } else {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000107 gd->env_addr = (ulong)&default_environment[0];
108 gd->env_valid = 0;
wdenkc6097192002-11-03 00:24:07 +0000109 }
Igor Grinberg91494ca2011-11-07 01:14:04 +0000110
111 return 0;
wdenkc6097192002-11-03 00:24:07 +0000112}
Simon Glass4415f1d2017-08-03 12:21:58 -0600113
114U_BOOT_ENV_LOCATION(nvram) = {
115 .location = ENVL_NVRAM,
Simon Glassac358be2017-08-03 12:22:03 -0600116 ENV_NAME("NVRAM")
Simon Glass4415f1d2017-08-03 12:21:58 -0600117#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Simon Glasse5bce242017-08-03 12:22:01 -0600118 .get_char = env_nvram_get_char,
Simon Glass4415f1d2017-08-03 12:21:58 -0600119#endif
Simon Glasse5bce242017-08-03 12:22:01 -0600120 .load = env_nvram_load,
121 .save = env_save_ptr(env_nvram_save),
122 .init = env_nvram_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600123};