blob: 63e31a4749c3f170ff1c4100a821d21afc2e2dce [file] [log] [blame]
Henry Ptasinskia9533e72010-09-08 21:04:42 -07001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef _bcmnvram_h_
18#define _bcmnvram_h_
19
20#ifndef _LANGUAGE_ASSEMBLY
21
Henry Ptasinskia9533e72010-09-08 21:04:42 -070022#include <bcmdefs.h>
23
24struct nvram_header {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -070025 u32 magic;
26 u32 len;
27 u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
28 u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
29 u32 config_ncdl; /* ncdl values for memc */
Henry Ptasinskia9533e72010-09-08 21:04:42 -070030};
31
32struct nvram_tuple {
33 char *name;
34 char *value;
35 struct nvram_tuple *next;
36};
37
38/*
39 * Get default value for an NVRAM variable
40 */
41extern char *nvram_default_get(const char *name);
42
43/*
44 * Initialize NVRAM access. May be unnecessary or undefined on certain
45 * platforms.
46 */
47extern int nvram_init(void *sih);
48
49/*
50 * Append a chunk of nvram variables to the global list
51 */
52extern int nvram_append(void *si, char *vars, uint varsz);
53
54/*
55 * Check for reset button press for restoring factory defaults.
56 */
57extern int nvram_reset(void *sih);
58
59/*
60 * Disable NVRAM access. May be unnecessary or undefined on certain
61 * platforms.
62 */
63extern void nvram_exit(void *sih);
64
65/*
66 * Get the value of an NVRAM variable. The pointer returned may be
67 * invalid after a set.
68 * @param name name of variable to get
69 * @return value of variable or NULL if undefined
70 */
71extern char *nvram_get(const char *name);
72
73/*
74 * Read the reset GPIO value from the nvram and set the GPIO
75 * as input
76 */
Jason Cooperb4f790e2010-10-11 10:02:58 -040077extern int nvram_resetgpio_init(void *sih);
Henry Ptasinskia9533e72010-09-08 21:04:42 -070078
79/*
80 * Get the value of an NVRAM variable.
81 * @param name name of variable to get
82 * @return value of variable or NUL if undefined
83 */
84#define nvram_safe_get(name) (nvram_get(name) ? : "")
85
86/*
87 * Match an NVRAM variable.
88 * @param name name of variable to match
89 * @param match value to compare against value of variable
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -070090 * @return true if variable is defined and its value is string equal
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -070091 * to match or false otherwise
Henry Ptasinskia9533e72010-09-08 21:04:42 -070092 */
Greg Kroah-Hartman2d956e22010-10-05 09:40:02 -070093static inline int nvram_match(char *name, char *match)
Henry Ptasinskia9533e72010-09-08 21:04:42 -070094{
95 const char *value = nvram_get(name);
Jason Cooper90ea2292010-09-14 09:45:32 -040096 return value && !strcmp(value, match);
Henry Ptasinskia9533e72010-09-08 21:04:42 -070097}
98
99/*
100 * Inversely match an NVRAM variable.
101 * @param name name of variable to match
102 * @param match value to compare against value of variable
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700103 * @return true if variable is defined and its value is not string
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700104 * equal to invmatch or false otherwise
Henry Ptasinskia9533e72010-09-08 21:04:42 -0700105 */
Greg Kroah-Hartman2d956e22010-10-05 09:40:02 -0700106static inline int nvram_invmatch(char *name, char *invmatch)
Henry Ptasinskia9533e72010-09-08 21:04:42 -0700107{
108 const char *value = nvram_get(name);
Jason Cooper90ea2292010-09-14 09:45:32 -0400109 return value && strcmp(value, invmatch);
Henry Ptasinskia9533e72010-09-08 21:04:42 -0700110}
111
112/*
113 * Set the value of an NVRAM variable. The name and value strings are
114 * copied into private storage. Pointers to previously set values
115 * may become invalid. The new value may be immediately
116 * retrieved but will not be permanently stored until a commit.
117 * @param name name of variable to set
118 * @param value value of variable
119 * @return 0 on success and errno on failure
120 */
121extern int nvram_set(const char *name, const char *value);
122
123/*
124 * Unset an NVRAM variable. Pointers to previously set values
125 * remain valid until a set.
126 * @param name name of variable to unset
127 * @return 0 on success and errno on failure
128 * NOTE: use nvram_commit to commit this change to flash.
129 */
130extern int nvram_unset(const char *name);
131
132/*
133 * Commit NVRAM variables to permanent storage. All pointers to values
134 * may be invalid after a commit.
135 * NVRAM values are undefined after a commit.
136 * @return 0 on success and errno on failure
137 */
138extern int nvram_commit(void);
139
140/*
141 * Get all NVRAM variables (format name=value\0 ... \0\0).
142 * @param buf buffer to store variables
143 * @param count size of buffer in bytes
144 * @return 0 on success and errno on failure
145 */
146extern int nvram_getall(char *nvram_buf, int count);
147
148/*
149 * returns the crc value of the nvram
150 * @param nvh nvram header pointer
151 */
Greg Kroah-Hartmande9bca62010-10-05 10:23:40 -0700152u8 nvram_calc_crc(struct nvram_header *nvh);
Henry Ptasinskia9533e72010-09-08 21:04:42 -0700153
154#endif /* _LANGUAGE_ASSEMBLY */
155
156/* The NVRAM version number stored as an NVRAM variable */
157#define NVRAM_SOFTWARE_VERSION "1"
158
159#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
160#define NVRAM_CLEAR_MAGIC 0x0
161#define NVRAM_INVALID_MAGIC 0xFFFFFFFF
162#define NVRAM_VERSION 1
163#define NVRAM_HEADER_SIZE 20
164#define NVRAM_SPACE 0x8000
165
166#define NVRAM_MAX_VALUE_LEN 255
167#define NVRAM_MAX_PARAM_LEN 64
168
169#define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */
170#define NVRAM_CRC_VER_MASK 0xffffff00 /* for crc_ver_init */
171
172#endif /* _bcmnvram_h_ */