1 /*
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 
22 #include <bcmdefs.h>
23 
24 struct nvram_header {
25 	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 */
30 };
31 
32 /*
33  * Get default value for an NVRAM variable
34  */
35 extern char *nvram_default_get(const char *name);
36 
37 /*
38  * Initialize NVRAM access. May be unnecessary or undefined on certain
39  * platforms.
40  */
41 extern int nvram_init(void *sih);
42 
43 /*
44  * Append a chunk of nvram variables to the global list
45  */
46 extern int nvram_append(void *si, char *vars, uint varsz);
47 
48 /*
49  * Check for reset button press for restoring factory defaults.
50  */
51 extern int nvram_reset(void *sih);
52 
53 /*
54  * Disable NVRAM access. May be unnecessary or undefined on certain
55  * platforms.
56  */
57 extern void nvram_exit(void *sih);
58 
59 /*
60  * Get the value of an NVRAM variable. The pointer returned may be
61  * invalid after a set.
62  * @param	name	name of variable to get
63  * @return	value of variable or NULL if undefined
64  */
65 extern char *nvram_get(const char *name);
66 
67 /*
68  * Read the reset GPIO value from the nvram and set the GPIO
69  * as input
70  */
71 extern int nvram_resetgpio_init(void *sih);
72 
73 /*
74  * Get the value of an NVRAM variable.
75  * @param	name	name of variable to get
76  * @return	value of variable or NUL if undefined
77  */
78 #define nvram_safe_get(name) (nvram_get(name) ? : "")
79 
80 /*
81  * Match an NVRAM variable.
82  * @param	name	name of variable to match
83  * @param	match	value to compare against value of variable
84  * @return	true if variable is defined and its value is string equal
85  *		to match or false otherwise
86  */
nvram_match(char * name,char * match)87 static inline int nvram_match(char *name, char *match)
88 {
89 	const char *value = nvram_get(name);
90 	return value && !strcmp(value, match);
91 }
92 
93 /*
94  * Inversely match an NVRAM variable.
95  * @param	name	name of variable to match
96  * @param	match	value to compare against value of variable
97  * @return	true if variable is defined and its value is not string
98  *		equal to invmatch or false otherwise
99  */
nvram_invmatch(char * name,char * invmatch)100 static inline int nvram_invmatch(char *name, char *invmatch)
101 {
102 	const char *value = nvram_get(name);
103 	return value && strcmp(value, invmatch);
104 }
105 
106 /*
107  * Set the value of an NVRAM variable. The name and value strings are
108  * copied into private storage. Pointers to previously set values
109  * may become invalid. The new value may be immediately
110  * retrieved but will not be permanently stored until a commit.
111  * @param	name	name of variable to set
112  * @param	value	value of variable
113  * @return	0 on success and errno on failure
114  */
115 extern int nvram_set(const char *name, const char *value);
116 
117 /*
118  * Unset an NVRAM variable. Pointers to previously set values
119  * remain valid until a set.
120  * @param	name	name of variable to unset
121  * @return	0 on success and errno on failure
122  * NOTE: use nvram_commit to commit this change to flash.
123  */
124 extern int nvram_unset(const char *name);
125 
126 /*
127  * Commit NVRAM variables to permanent storage. All pointers to values
128  * may be invalid after a commit.
129  * NVRAM values are undefined after a commit.
130  * @return	0 on success and errno on failure
131  */
132 extern int nvram_commit(void);
133 
134 /*
135  * Get all NVRAM variables (format name=value\0 ... \0\0).
136  * @param	buf	buffer to store variables
137  * @param	count	size of buffer in bytes
138  * @return	0 on success and errno on failure
139  */
140 extern int nvram_getall(char *nvram_buf, int count);
141 
142 /*
143  * returns the crc value of the nvram
144  * @param	nvh	nvram header pointer
145  */
146 u8 nvram_calc_crc(struct nvram_header *nvh);
147 
148 #endif				/* _LANGUAGE_ASSEMBLY */
149 
150 /* The NVRAM version number stored as an NVRAM variable */
151 #define NVRAM_SOFTWARE_VERSION	"1"
152 
153 #define NVRAM_MAGIC		0x48534C46	/* 'FLSH' */
154 #define NVRAM_CLEAR_MAGIC	0x0
155 #define NVRAM_INVALID_MAGIC	0xFFFFFFFF
156 #define NVRAM_VERSION		1
157 #define NVRAM_HEADER_SIZE	20
158 #define NVRAM_SPACE		0x8000
159 
160 #define NVRAM_MAX_VALUE_LEN 255
161 #define NVRAM_MAX_PARAM_LEN 64
162 
163 #define NVRAM_CRC_START_POSITION	9	/* magic, len, crc8 to be skipped */
164 #define NVRAM_CRC_VER_MASK	0xffffff00	/* for crc_ver_init */
165 
166 #endif				/* _bcmnvram_h_ */
167