convert brcm-2.4 to the new target structure
[openwrt.git] / target / linux / brcm-2.4 / files / arch / mips / bcm947xx / include / bcmnvram.h
1 /*
2  * NVRAM variable manipulation
3  *
4  * Copyright 2006, Broadcom Corporation
5  * All Rights Reserved.
6  * 
7  * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8  * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9  * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10  * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11  *
12  * $Id: bcmnvram.h,v 1.17 2006/03/02 12:33:44 honor Exp $
13  */
14
15 #ifndef _bcmnvram_h_
16 #define _bcmnvram_h_
17
18 #ifndef _LANGUAGE_ASSEMBLY
19
20 #include <typedefs.h>
21 #include <bcmdefs.h>
22
23 struct nvram_header {
24         uint32 magic;
25         uint32 len;
26         uint32 crc_ver_init;    /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
27         uint32 config_refresh;  /* 0:15 sdram_config, 16:31 sdram_refresh */
28         uint32 config_ncdl;     /* ncdl values for memc */
29 };
30
31 struct nvram_tuple {
32         char *name;
33         char *value;
34         struct nvram_tuple *next;
35 };
36
37 /*
38  * Initialize NVRAM access. May be unnecessary or undefined on certain
39  * platforms.
40  */
41 extern int nvram_init(void *sbh);
42
43 /*
44  * Disable NVRAM access. May be unnecessary or undefined on certain
45  * platforms.
46  */
47 extern void nvram_exit(void *sbh);
48
49 /*
50  * Get the value of an NVRAM variable. The pointer returned may be
51  * invalid after a set.
52  * @param       name    name of variable to get
53  * @return      value of variable or NULL if undefined
54  */
55 extern char * nvram_get(const char *name);
56
57 /* 
58  * Read the reset GPIO value from the nvram and set the GPIO
59  * as input
60  */
61 extern int BCMINITFN(nvram_resetgpio_init)(void *sbh);
62 extern int BCMINITFN(nvram_gpio_init)(const char *name, void *sbh);
63 extern int BCMINITFN(nvram_gpio_set)(const char *name, void *sbh, int type);
64
65 /* 
66  * Get the value of an NVRAM variable.
67  * @param       name    name of variable to get
68  * @return      value of variable or NUL if undefined
69  */
70 #define nvram_safe_get(name) (nvram_get(name) ? : "")
71
72 #define nvram_safe_unset(name) ({ \
73         if(nvram_get(name)) \
74                 nvram_unset(name); \
75 })
76
77 #define nvram_safe_set(name, value) ({ \
78         if(!nvram_get(name) || strcmp(nvram_get(name), value)) \
79                 nvram_set(name, value); \
80 })
81
82 /*
83  * Match an NVRAM variable.
84  * @param       name    name of variable to match
85  * @param       match   value to compare against value of variable
86  * @return      TRUE if variable is defined and its value is string equal
87  *              to match or FALSE otherwise
88  */
89 static INLINE int
90 nvram_match(char *name, char *match) {
91         const char *value = nvram_get(name);
92         return (value && !strcmp(value, match));
93 }
94
95 /*
96  * Inversely match an NVRAM variable.
97  * @param       name    name of variable to match
98  * @param       match   value to compare against value of variable
99  * @return      TRUE if variable is defined and its value is not string
100  *              equal to invmatch or FALSE otherwise
101  */
102 static INLINE int
103 nvram_invmatch(char *name, char *invmatch) {
104         const char *value = nvram_get(name);
105         return (value && strcmp(value, invmatch));
106 }
107
108 /*
109  * Set the value of an NVRAM variable. The name and value strings are
110  * copied into private storage. Pointers to previously set values
111  * may become invalid. The new value may be immediately
112  * retrieved but will not be permanently stored until a commit.
113  * @param       name    name of variable to set
114  * @param       value   value of variable
115  * @return      0 on success and errno on failure
116  */
117 extern int nvram_set(const char *name, const char *value);
118
119 /*
120  * Unset an NVRAM variable. Pointers to previously set values
121  * remain valid until a set.
122  * @param       name    name of variable to unset
123  * @return      0 on success and errno on failure
124  * NOTE: use nvram_commit to commit this change to flash.
125  */
126 extern int nvram_unset(const char *name);
127
128 /*
129  * Commit NVRAM variables to permanent storage. All pointers to values
130  * may be invalid after a commit.
131  * NVRAM values are undefined after a commit.
132  * @return      0 on success and errno on failure
133  */
134 extern int nvram_commit(void);
135
136 /*
137  * Get all NVRAM variables (format name=value\0 ... \0\0).
138  * @param       buf     buffer to store variables
139  * @param       count   size of buffer in bytes
140  * @return      0 on success and errno on failure
141  */
142 extern int nvram_getall(char *buf, int count);
143
144 extern int file2nvram(char *filename, char *varname);
145 extern int nvram2file(char *varname, char *filename);
146
147 #endif /* _LANGUAGE_ASSEMBLY */
148
149 #define NVRAM_MAGIC             0x48534C46      /* 'FLSH' */
150 #define NVRAM_CLEAR_MAGIC               0x0
151 #define NVRAM_INVALID_MAGIC     0xFFFFFFFF
152 #define NVRAM_VERSION           1
153 #define NVRAM_HEADER_SIZE       20
154 #define NVRAM_SPACE             0x8000
155
156 #define NVRAM_MAX_VALUE_LEN 255
157 #define NVRAM_MAX_PARAM_LEN 64
158
159 #endif /* _bcmnvram_h_ */