2 * Read-only support for NVRAM on flash and otp.
4 * Copyright 2004, Broadcom Corporation
7 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
8 * the contents of this file may not be disclosed to third parties, copied
9 * or duplicated in any form, in whole or in part, without the prior
10 * written permission of Broadcom Corporation.
18 #include <bcmendian.h>
26 static char *fnv
= NULL
;
27 static char *fnvlim
= NULL
;
28 static char *otp
= NULL
;
29 static char *otplim
= NULL
;
30 static char *otp2
= NULL
;
31 static char *otp2lim
= NULL
;
34 static struct nvram_header
*
35 find_flash_nvram(uint32 base
, uint32 lim
)
37 struct nvram_header
*nvh
;
38 uint32 off
= FLASH_MIN
;
42 nvh
= (struct nvram_header
*) (base
+ off
- NVRAM_SPACE
);
43 if (nvh
->magic
== NVRAM_MAGIC
)
48 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
49 nvh
= (struct nvram_header
*) (base
+ 4096);
50 if (nvh
->magic
== NVRAM_MAGIC
)
53 nvh
= (struct nvram_header
*) (base
+ 1024);
54 if (nvh
->magic
== NVRAM_MAGIC
)
61 BCMINITFN(nvram_init
)(void *sbh
)
66 struct nvram_header
*nvh
= NULL
;
67 uint32 cap
= 0, base
, lim
;
76 idx
= sb_coreidx(sbh
);
77 if ((cc
= sb_setcore(sbh
, SB_CC
, 0)) != NULL
) {
78 base
= KSEG1ADDR(SB_FLASH2
);
80 cap
= R_REG(&cc
->capabilities
);
81 switch (cap
& CAP_FLASH_MASK
) {
88 if ((info
= sflash_init(cc
)) == NULL
)
98 base
= KSEG1ADDR(SB_FLASH1
);
103 nvh
= find_flash_nvram(base
, lim
);
106 fnv
= (char *)&nvh
[1];
107 fnvlim
= (char *)((uint32
)nvh
+ NVRAM_SPACE
);
111 if ((cc
!= NULL
) && ((lim
= cap
& CAP_OTPSIZE
) != 0)) {
116 otpw
= (uint16
*)((uint32
)cc
+ CC_OTP
);
117 lim
= 1 << ((lim
>> CAP_OTPSIZE_SHIFT
) + 5);
118 if ((otpw
[OTP_HWSIGN
] == OTP_SIGNATURE
) &&
119 (otpw
[0] == OTP_MAGIC
)) {
120 bound
= otpw
[OTP_BOUNDARY
];
122 printf("Bad boundary value in otp (%d >= %d)\n", bound
, lim
);
125 /* OK, we like otp, copy to ram, skipping the magic word */
126 if ((otp
= MALLOC(NULL
,bound
- 2)) == NULL
) {
127 printf("Out of memory for otp\n");
131 for (i
= 1; i
< (bound
/ 2); i
++)
133 otplim
= otp
+ bound
- 2;
134 printf ("otp size = %d, hwsign = 0x%x, magic = 0x%x, boundary = 0x%x\n",
135 lim
, otpw
[OTP_HWSIGN
], otpw
[0], bound
);
137 /* Now do it again for the "second" part of the otp */
138 if (otpw
[OTP_SWSIGN
] == OTP_SIGNATURE
) {
139 if ((otp2
= MALLOC(NULL
, lim
- bound
)) == NULL
) {
140 printf("Out of memory for otp2\n");
143 d16
= (uint16
*)otp2
;
144 while (i
< (lim
/ 2))
146 otp2lim
= otp2
+ lim
- bound
;
152 sb_setcoreidx(sbh
, idx
);
158 BCMINITFN(nvram_exit
)(void)
161 MFREE(NULL
, otp
, otplim
- otp
);
166 MFREE(NULL
, otp2
, otp2lim
- otp2
);
172 findvar(char *vars
, char *lim
, const char *name
)
179 for (s
= vars
; (s
< lim
) && *s
; ) {
180 if ((bcmp(s
, name
, len
) == 0) && (s
[len
] == '='))
191 BCMINITFN(nvram_get
)(const char *name
)
195 if ((fnv
!= NULL
) && ((v
= findvar(fnv
, fnvlim
, name
)) != NULL
))
198 if ((otp2
!= NULL
) && ((v
= findvar(otp2
, otp2lim
, name
)) != NULL
))
202 v
= findvar(otp
, otplim
, name
);
208 BCMINITFN(nvram_set
)(const char *name
, const char *value
)
214 BCMINITFN(nvram_unset
)(const char *name
)
220 BCMINITFN(nvram_commit
)(void)
226 BCMINITFN(nvram_getall
)(char *buf
, int count
)