2 * BCM947xx nvram variable access
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/ssb/ssb.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <asm/addrspace.h>
21 #include <bcm47xx_nvram.h>
22 #include <asm/mach-bcm47xx/bcm47xx.h>
24 static char nvram_buf
[NVRAM_SPACE
];
26 static u32
find_nvram_size(u32 end
)
28 struct nvram_header
*header
;
29 u32 nvram_sizes
[] = {0x8000, 0xF000, 0x10000};
32 for (i
= 0; i
< ARRAY_SIZE(nvram_sizes
); i
++) {
33 header
= (struct nvram_header
*)KSEG1ADDR(end
- nvram_sizes
[i
]);
34 if (header
->magic
== NVRAM_HEADER
)
35 return nvram_sizes
[i
];
41 /* Probe for NVRAM header */
42 static int nvram_find_and_copy(u32 base
, u32 lim
)
44 struct nvram_header
*header
;
50 /* TODO: when nvram is on nand flash check for bad blocks first. */
53 /* Windowed flash access */
54 size
= find_nvram_size(base
+ off
);
56 header
= (struct nvram_header
*)KSEG1ADDR(base
+ off
-
63 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
64 header
= (struct nvram_header
*) KSEG1ADDR(base
+ 4096);
65 if (header
->magic
== NVRAM_HEADER
) {
70 header
= (struct nvram_header
*) KSEG1ADDR(base
+ 1024);
71 if (header
->magic
== NVRAM_HEADER
) {
76 pr_err("no nvram found\n");
81 if (header
->len
> size
)
82 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
83 if (header
->len
> NVRAM_SPACE
)
84 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
85 header
->len
, NVRAM_SPACE
);
88 dst
= (u32
*) nvram_buf
;
89 for (i
= 0; i
< sizeof(struct nvram_header
); i
+= 4)
91 for (; i
< header
->len
&& i
< NVRAM_SPACE
&& i
< size
; i
+= 4)
92 *dst
++ = le32_to_cpu(*src
++);
93 memset(dst
, 0x0, NVRAM_SPACE
- i
);
98 #ifdef CONFIG_BCM47XX_SSB
99 static int nvram_init_ssb(void)
101 struct ssb_mipscore
*mcore
= &bcm47xx_bus
.ssb
.mipscore
;
105 if (mcore
->pflash
.present
) {
106 base
= mcore
->pflash
.window
;
107 lim
= mcore
->pflash
.window_size
;
109 pr_err("Couldn't find supported flash memory\n");
113 return nvram_find_and_copy(base
, lim
);
117 #ifdef CONFIG_BCM47XX_BCMA
118 static int nvram_init_bcma(void)
120 struct bcma_drv_cc
*cc
= &bcm47xx_bus
.bcma
.bus
.drv_cc
;
124 #ifdef CONFIG_BCMA_NFLASH
125 if (cc
->nflash
.boot
) {
126 base
= BCMA_SOC_FLASH1
;
127 lim
= BCMA_SOC_FLASH1_SZ
;
130 if (cc
->pflash
.present
) {
131 base
= cc
->pflash
.window
;
132 lim
= cc
->pflash
.window_size
;
133 #ifdef CONFIG_BCMA_SFLASH
134 } else if (cc
->sflash
.present
) {
135 base
= cc
->sflash
.window
;
136 lim
= cc
->sflash
.size
;
139 pr_err("Couldn't find supported flash memory\n");
143 return nvram_find_and_copy(base
, lim
);
147 static int nvram_init(void)
149 switch (bcm47xx_bus_type
) {
150 #ifdef CONFIG_BCM47XX_SSB
151 case BCM47XX_BUS_TYPE_SSB
:
152 return nvram_init_ssb();
154 #ifdef CONFIG_BCM47XX_BCMA
155 case BCM47XX_BUS_TYPE_BCMA
:
156 return nvram_init_bcma();
162 int bcm47xx_nvram_getenv(char *name
, char *val
, size_t val_len
)
164 char *var
, *value
, *end
, *eq
;
176 /* Look for name=value and return value */
177 var
= &nvram_buf
[sizeof(struct nvram_header
)];
178 end
= nvram_buf
+ sizeof(nvram_buf
) - 2;
179 end
[0] = end
[1] = '\0';
180 for (; *var
; var
= value
+ strlen(value
) + 1) {
181 eq
= strchr(var
, '=');
185 if ((eq
- var
) == strlen(name
) &&
186 strncmp(var
, name
, (eq
- var
)) == 0) {
187 return snprintf(val
, val_len
, "%s", value
);
192 EXPORT_SYMBOL(bcm47xx_nvram_getenv
);