2 * NVRAM variable manipulation (direct mapped flash)
4 * Copyright 2004, Broadcom Corporation
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.
19 #include <bcmendian.h>
26 struct nvram_tuple
* BCMINIT(_nvram_realloc
)(struct nvram_tuple
*t
, const char *name
, const char *value
);
27 void BCMINIT(_nvram_free
)(struct nvram_tuple
*t
);
28 int BCMINIT(_nvram_read
)(void *buf
);
30 extern char * BCMINIT(_nvram_get
)(const char *name
);
31 extern int BCMINIT(_nvram_set
)(const char *name
, const char *value
);
32 extern int BCMINIT(_nvram_unset
)(const char *name
);
33 extern int BCMINIT(_nvram_getall
)(char *buf
, int count
);
34 extern int BCMINIT(_nvram_commit
)(struct nvram_header
*header
);
35 extern int BCMINIT(_nvram_init
)(void);
36 extern void BCMINIT(_nvram_exit
)(void);
38 static struct nvram_header
*nvram_header
= NULL
;
39 static ulong flash_base
= 0;
41 #define NVRAM_LOCK() do {} while (0)
42 #define NVRAM_UNLOCK() do {} while (0)
46 #define MB * 1024 * 1024
49 BCMINITFN(nvram_get
)(const char *name
)
54 value
= BCMINIT(_nvram_get
)(name
);
61 BCMINITFN(nvram_getall
)(char *buf
, int count
)
66 ret
= BCMINIT(_nvram_getall
)(buf
, count
);
73 BCMINITFN(nvram_set
)(const char *name
, const char *value
)
78 ret
= BCMINIT(_nvram_set
)(name
, value
);
85 BCMINITFN(nvram_unset
)(const char *name
)
90 ret
= BCMINIT(_nvram_unset
)(name
);
97 BCMINITFN(nvram_reset
)(void *sbh
)
101 uint32 watchdog
= 0, gpio
;
104 idx
= sb_coreidx(sbh
);
106 /* Check if we were soft reset */
107 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
108 watchdog
= R_REG(&cc
->intstatus
) & CI_WDRESET
;
109 sb_setcoreidx(sbh
, idx
);
114 value
= BCMINIT(nvram_get
)("reset_gpio");
118 gpio
= (uint32
) bcm_atoi(value
);
122 /* Setup GPIO input */
123 sb_gpioouten(sbh
, (1 << gpio
), 0);
125 /* GPIO reset is asserted low */
126 for (msec
= 0; msec
< 5000; msec
++) {
127 if (sb_gpioin(sbh
) & (1 << gpio
))
135 extern unsigned char embedded_nvram
[];
137 static struct nvram_header
*
138 BCMINITFN(find_nvram
)(bool embonly
, bool *isemb
)
140 struct nvram_header
*nvh
;
146 if (flash_base
== SB_FLASH1
)
152 nvh
= (struct nvram_header
*)KSEG1ADDR(flash_base
+ off
- NVRAM_SPACE
);
153 if (nvh
->magic
== NVRAM_MAGIC
)
159 /* Now check embedded nvram */
161 nvh
= (struct nvram_header
*)KSEG1ADDR(flash_base
+ (4 * 1024));
162 if (nvh
->magic
== NVRAM_MAGIC
)
164 nvh
= (struct nvram_header
*)KSEG1ADDR(flash_base
+ 1024);
165 if (nvh
->magic
== NVRAM_MAGIC
)
168 nvh
= (struct nvram_header
*)embedded_nvram
;
169 if (nvh
->magic
== NVRAM_MAGIC
)
176 BCMINITFN(nvram_init
)(void *sbh
)
183 idx
= sb_coreidx(sbh
);
184 if (sb_setcore(sbh
, SB_CC
, 0) != NULL
) {
185 flash_base
= SB_FLASH2
;
186 sb_setcoreidx(sbh
, idx
);
188 flash_base
= SB_FLASH1
;
190 /* Temporarily initialize with embedded NVRAM */
191 nvram_header
= BCMINIT(find_nvram
)(TRUE
, &isemb
);
192 ret
= BCMINIT(_nvram_init
)();
194 /* Restore defaults from embedded NVRAM if button held down */
195 if (BCMINIT(nvram_reset
)(sbh
)) {
199 BCMINIT(_nvram_exit
)();
203 nvram_header
= BCMINIT(find_nvram
)(FALSE
, &isemb
);
204 ret
= BCMINIT(_nvram_init
)();
206 /* Restore defaults if embedded NVRAM used */
207 if (nvram_header
&& isemb
) {
215 BCMINITFN(nvram_exit
)(void)
217 BCMINIT(_nvram_exit
)();
221 BCMINITFN(_nvram_read
)(void *buf
)
227 return -19; /* -ENODEV */
229 src
= (uint32
*) nvram_header
;
230 dst
= (uint32
*) buf
;
232 for (i
= 0; i
< sizeof(struct nvram_header
); i
+= 4)
235 for (; i
< nvram_header
->len
&& i
< NVRAM_SPACE
; i
+= 4)
236 *dst
++ = ltoh32(*src
++);
242 BCMINITFN(_nvram_realloc
)(struct nvram_tuple
*t
, const char *name
, const char *value
)
244 if (!(t
= MALLOC(NULL
, sizeof(struct nvram_tuple
) + strlen(name
) + 1 + strlen(value
) + 1))) {
245 printf("_nvram_realloc: our of memory\n");
250 t
->name
= (char *) &t
[1];
251 strcpy(t
->name
, name
);
254 t
->value
= t
->name
+ strlen(name
) + 1;
255 strcpy(t
->value
, value
);
261 BCMINITFN(_nvram_free
)(struct nvram_tuple
*t
)
264 MFREE(NULL
, t
, sizeof(struct nvram_tuple
) + strlen(t
->name
) + 1 + strlen(t
->value
) + 1);
268 BCMINITFN(nvram_commit
)(void)
270 struct nvram_header
*header
;
275 if (!(header
= (struct nvram_header
*) MALLOC(NULL
, NVRAM_SPACE
))) {
276 printf("nvram_commit: out of memory\n");
277 return -12; /* -ENOMEM */
282 /* Regenerate NVRAM */
283 ret
= BCMINIT(_nvram_commit
)(header
);
287 src
= (uint32
*) &header
[1];
290 for (i
= sizeof(struct nvram_header
); i
< header
->len
&& i
< NVRAM_SPACE
; i
+= 4)
291 *dst
++ = htol32(*src
++);
294 if ((ret
= cfe_open("flash0.nvram")) >= 0) {
295 cfe_writeblk(ret
, 0, (unsigned char *) header
, header
->len
);
299 if (sysFlashInit(NULL
) == 0)
300 nvWrite((unsigned short *) header
, NVRAM_SPACE
);
305 MFREE(NULL
, header
, NVRAM_SPACE
);