Blame view

env/nvram.c 3.04 KB
c609719b8   wdenk   Initial revision
1
  /*
ea882baf9   Wolfgang Denk   New implementatio...
2
   * (C) Copyright 2000-2010
c609719b8   wdenk   Initial revision
3
4
5
6
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   * Andreas Heppel <aheppel@sysgo.de>
3765b3e7b   Wolfgang Denk   Coding Style clea...
7
   * SPDX-License-Identifier:	GPL-2.0+
c609719b8   wdenk   Initial revision
8
9
10
11
12
13
14
15
16
17
18
19
20
   */
  
  /*
   * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
   *
   * It might not be possible in all cases to use 'memcpy()' to copy
   * the environment to NVRAM, as the NVRAM might not be mapped into
   * the memory space. (I.e. this is the case for the BAB750). In those
   * cases it might be possible to access the NVRAM using a different
   * method. For example, the RTC on the BAB750 is accessible in IO
   * space using its address and data registers. To enable usage of
   * NVRAM in those cases I invented the functions 'nvram_read()' and
   * 'nvram_write()', which will be activated upon the configuration
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
21
   * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
c609719b8   wdenk   Initial revision
22
23
24
25
26
   * strongly dependent on the used HW, and must be redefined for each
   * board that wants to use them.
   */
  
  #include <common.h>
c609719b8   wdenk   Initial revision
27
28
  #include <command.h>
  #include <environment.h>
c609719b8   wdenk   Initial revision
29
  #include <linux/stddef.h>
ea882baf9   Wolfgang Denk   New implementatio...
30
31
  #include <search.h>
  #include <errno.h>
c609719b8   wdenk   Initial revision
32

957a0e695   Jean-Christophe PLAGNIOL-VILLARD   env_nvram: Move c...
33
  DECLARE_GLOBAL_DATA_PTR;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
34
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
c609719b8   wdenk   Initial revision
35
36
  extern void *nvram_read(void *dest, const long src, size_t count);
  extern void nvram_write(long dest, const void *src, size_t count);
c609719b8   wdenk   Initial revision
37
  #else
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
38
  env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b8   wdenk   Initial revision
39
  #endif
bf95df44f   Igor Grinberg   env: factor out t...
40
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
b2cdef486   Goldschmidt Simon   env: restore old ...
41
42
43
44
  /** Call this function from overridden env_get_char_spec() if you need
   * this functionality.
   */
  int env_nvram_get_char(int index)
c609719b8   wdenk   Initial revision
45
  {
c609719b8   wdenk   Initial revision
46
  	uchar c;
91494ca6a   Igor Grinberg   env: clean env_nv...
47
  	nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
c609719b8   wdenk   Initial revision
48
49
  
  	return c;
c609719b8   wdenk   Initial revision
50
  }
bf95df44f   Igor Grinberg   env: factor out t...
51
  #endif
c609719b8   wdenk   Initial revision
52

c59519919   Simon Glass   env: Adjust the l...
53
  static int env_nvram_load(void)
c609719b8   wdenk   Initial revision
54
  {
cd0f4fa1c   Tom Rini   Revert "env: fix ...
55
  	char buf[CONFIG_ENV_SIZE];
ea882baf9   Wolfgang Denk   New implementatio...
56

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
57
  #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
ea882baf9   Wolfgang Denk   New implementatio...
58
  	nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b8   wdenk   Initial revision
59
  #else
cd0f4fa1c   Tom Rini   Revert "env: fix ...
60
  	memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b8   wdenk   Initial revision
61
  #endif
2166ebf78   Simon Goldschmidt   env: make env dri...
62
  	return env_import(buf, 1);
c609719b8   wdenk   Initial revision
63
  }
e5bce247b   Simon Glass   env: Switch over ...
64
  static int env_nvram_save(void)
c609719b8   wdenk   Initial revision
65
  {
cd0f4fa1c   Tom Rini   Revert "env: fix ...
66
  	env_t	env_new;
ea882baf9   Wolfgang Denk   New implementatio...
67
  	int	rcode = 0;
7ce1526ed   Marek Vasut   env: Add env_expo...
68
69
70
  	rcode = env_export(&env_new);
  	if (rcode)
  		return rcode;
ea882baf9   Wolfgang Denk   New implementatio...
71

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
72
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
cd0f4fa1c   Tom Rini   Revert "env: fix ...
73
74
75
76
  	nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
  #else
  	if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
  		rcode = 1;
c609719b8   wdenk   Initial revision
77
78
79
  #endif
  	return rcode;
  }
ea882baf9   Wolfgang Denk   New implementatio...
80
  /*
c609719b8   wdenk   Initial revision
81
82
83
84
   * Initialize Environment use
   *
   * We are still running from ROM, so data use is limited
   */
e5bce247b   Simon Glass   env: Switch over ...
85
  static int env_nvram_init(void)
c609719b8   wdenk   Initial revision
86
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
87
  #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
c609719b8   wdenk   Initial revision
88
  	ulong crc;
cd0f4fa1c   Tom Rini   Revert "env: fix ...
89
  	uchar data[ENV_SIZE];
ea882baf9   Wolfgang Denk   New implementatio...
90
91
  
  	nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
91494ca6a   Igor Grinberg   env: clean env_nv...
92
  	nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
c609719b8   wdenk   Initial revision
93
94
  
  	if (crc32(0, data, ENV_SIZE) == crc) {
91494ca6a   Igor Grinberg   env: clean env_nv...
95
  		gd->env_addr	= (ulong)CONFIG_ENV_ADDR + sizeof(long);
c609719b8   wdenk   Initial revision
96
97
  #else
  	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
91494ca6a   Igor Grinberg   env: clean env_nv...
98
  		gd->env_addr	= (ulong)&env_ptr->data;
c609719b8   wdenk   Initial revision
99
  #endif
203e94f6c   Simon Glass   env: Add an enum ...
100
  		gd->env_valid = ENV_VALID;
c609719b8   wdenk   Initial revision
101
  	} else {
91494ca6a   Igor Grinberg   env: clean env_nv...
102
  		gd->env_addr	= (ulong)&default_environment[0];
2d7cb5b42   Simon Glass   env: Replace all ...
103
  		gd->env_valid	= ENV_INVALID;
c609719b8   wdenk   Initial revision
104
  	}
91494ca6a   Igor Grinberg   env: clean env_nv...
105
106
  
  	return 0;
c609719b8   wdenk   Initial revision
107
  }
4415f1d1f   Simon Glass   env: Create a loc...
108
109
110
  
  U_BOOT_ENV_LOCATION(nvram) = {
  	.location	= ENVL_NVRAM,
ac358beb8   Simon Glass   env: Drop the env...
111
  	ENV_NAME("NVRAM")
e5bce247b   Simon Glass   env: Switch over ...
112
113
114
  	.load		= env_nvram_load,
  	.save		= env_save_ptr(env_nvram_save),
  	.init		= env_nvram_init,
4415f1d1f   Simon Glass   env: Create a loc...
115
  };