Clean and tiddy-up files.
[tomato.git] / release / src-rt / shared / nvram / nvram_ro.c
blob05b74b43122e907d55f6eabd5bfd0588a4ab8037
1 /*
2 * Read-only support for NVRAM on flash and otp.
4 * Copyright 2004, Broadcom Corporation
5 * All Rights Reserved.
6 *
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.
12 * $Id$
15 #include <typedefs.h>
16 #include <osl.h>
17 #include <bcmnvram.h>
18 #include <bcmendian.h>
19 #include <bcmutils.h>
20 #include <sflash.h>
21 #include <sbconfig.h>
22 #include <sbchipc.h>
23 #include <sbutils.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;
40 nvh = NULL;
41 while (off <= lim) {
42 nvh = (struct nvram_header *) (base + off - NVRAM_SPACE);
43 if (nvh->magic == NVRAM_MAGIC)
44 return nvh;
45 off <<= 1;
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)
51 return nvh;
53 nvh = (struct nvram_header *) (base + 1024);
54 if (nvh->magic == NVRAM_MAGIC)
55 return nvh;
57 return NULL;
60 int
61 BCMINITFN(nvram_init)(void *sbh)
63 uint idx;
64 chipcregs_t *cc;
65 struct sflash *info;
66 struct nvram_header *nvh = NULL;
67 uint32 cap = 0, base, lim;
70 /* Make sure */
71 fnv = NULL;
72 otp = NULL;
73 otp2 = NULL;
75 /* Check for flash */
76 idx = sb_coreidx(sbh);
77 if ((cc = sb_setcore(sbh, SB_CC, 0)) != NULL) {
78 base = KSEG1ADDR(SB_FLASH2);
79 lim = 0;
80 cap = R_REG(&cc->capabilities);
81 switch (cap & CAP_FLASH_MASK) {
82 case PFLASH:
83 lim = SB_FLASH2_SZ;
84 break;
86 case SFLASH_ST:
87 case SFLASH_AT:
88 if ((info = sflash_init(cc)) == NULL)
89 break;
90 lim = info->size;
91 break;
93 case FLASH_NONE:
94 default:
95 break;
97 } else {
98 base = KSEG1ADDR(SB_FLASH1);
99 lim = SB_FLASH1_SZ;
102 if (lim != 0)
103 nvh = find_flash_nvram(base, lim);
105 if (nvh != NULL) {
106 fnv = (char *)&nvh[1];
107 fnvlim = (char *)((uint32)nvh + NVRAM_SPACE);
110 /* Check for otp */
111 if ((cc != NULL) && ((lim = cap & CAP_OTPSIZE) != 0)) {
112 uint32 bound;
113 uint16 *otpw, *d16;
114 int i;
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];
121 if (bound >= lim) {
122 printf("Bad boundary value in otp (%d >= %d)\n", bound, lim);
123 goto out;
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");
128 goto out;
130 d16 = (uint16 *)otp;
131 for (i = 1; i < (bound / 2); i++)
132 *d16++ = otpw[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");
141 goto out;
143 d16 = (uint16 *)otp2;
144 while (i < (lim / 2))
145 *d16++ = otpw[i++];
146 otp2lim = otp2 + lim - bound;
151 out: /* All done */
152 sb_setcoreidx(sbh, idx);
154 return 0;
157 void
158 BCMINITFN(nvram_exit)(void)
160 if (otp) {
161 MFREE(NULL, otp, otplim - otp);
162 otp = NULL;
165 if (otp2) {
166 MFREE(NULL, otp2, otp2lim - otp2);
167 otp2 = NULL;
171 static char *
172 findvar(char *vars, char *lim, const char *name)
174 char *s;
175 int len;
177 len = strlen(name);
179 for (s = vars; (s < lim) && *s; ) {
180 if ((bcmp(s, name, len) == 0) && (s[len] == '='))
181 return (&s[len+1]);
183 while (*s++)
187 return NULL;
190 char *
191 BCMINITFN(nvram_get)(const char *name)
193 char *v = NULL;
195 if ((fnv != NULL) && ((v = findvar(fnv, fnvlim, name)) != NULL))
196 return v;
198 if ((otp2 != NULL) && ((v = findvar(otp2, otp2lim, name)) != NULL))
199 return v;
201 if (otp != NULL)
202 v = findvar(otp, otplim, name);
204 return v;
208 BCMINITFN(nvram_set)(const char *name, const char *value)
210 return 0;
214 BCMINITFN(nvram_unset)(const char *name)
216 return 0;
220 BCMINITFN(nvram_commit)(void)
222 return 0;
226 BCMINITFN(nvram_getall)(char *buf, int count)
228 return 0;