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/types.h>
15 #include <linux/module.h>
16 #include <linux/ssb/ssb.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <asm/addrspace.h>
20 #include <bcm47xx_nvram.h>
21 #include <asm/mach-bcm47xx/bcm47xx.h>
23 static char nvram_buf[NVRAM_SPACE];
24 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
26 static u32 find_nvram_size(u32 end)
28 struct nvram_header *header;
31 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
32 header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
33 if (header->magic == NVRAM_HEADER)
34 return nvram_sizes[i];
40 /* Probe for NVRAM header */
41 static int nvram_find_and_copy(u32 base, u32 lim)
43 struct nvram_header *header;
49 /* TODO: when nvram is on nand flash check for bad blocks first. */
52 /* Windowed flash access */
53 size = find_nvram_size(base + off);
55 header = (struct nvram_header *)KSEG1ADDR(base + off -
62 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
63 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
64 if (header->magic == NVRAM_HEADER) {
69 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
70 if (header->magic == NVRAM_HEADER) {
75 pr_err("no nvram found\n");
80 if (header->len > size)
81 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
82 if (header->len > NVRAM_SPACE)
83 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
84 header->len, NVRAM_SPACE);
87 dst = (u32 *) nvram_buf;
88 for (i = 0; i < sizeof(struct nvram_header); i += 4)
90 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
91 *dst++ = le32_to_cpu(*src++);
92 memset(dst, 0x0, NVRAM_SPACE - i);
97 #ifdef CONFIG_BCM47XX_SSB
98 static int nvram_init_ssb(void)
100 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
104 if (mcore->pflash.present) {
105 base = mcore->pflash.window;
106 lim = mcore->pflash.window_size;
108 pr_err("Couldn't find supported flash memory\n");
112 return nvram_find_and_copy(base, lim);
116 #ifdef CONFIG_BCM47XX_BCMA
117 static int nvram_init_bcma(void)
119 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
123 #ifdef CONFIG_BCMA_NFLASH
124 if (cc->nflash.boot) {
125 base = BCMA_SOC_FLASH1;
126 lim = BCMA_SOC_FLASH1_SZ;
129 if (cc->pflash.present) {
130 base = cc->pflash.window;
131 lim = cc->pflash.window_size;
132 #ifdef CONFIG_BCMA_SFLASH
133 } else if (cc->sflash.present) {
134 base = cc->sflash.window;
135 lim = cc->sflash.size;
138 pr_err("Couldn't find supported flash memory\n");
142 return nvram_find_and_copy(base, lim);
146 static int nvram_init(void)
148 switch (bcm47xx_bus_type) {
149 #ifdef CONFIG_BCM47XX_SSB
150 case BCM47XX_BUS_TYPE_SSB:
151 return nvram_init_ssb();
153 #ifdef CONFIG_BCM47XX_BCMA
154 case BCM47XX_BUS_TYPE_BCMA:
155 return nvram_init_bcma();
161 int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
163 char *var, *value, *end, *eq;
175 /* Look for name=value and return value */
176 var = &nvram_buf[sizeof(struct nvram_header)];
177 end = nvram_buf + sizeof(nvram_buf) - 2;
178 end[0] = end[1] = '\0';
179 for (; *var; var = value + strlen(value) + 1) {
180 eq = strchr(var, '=');
184 if ((eq - var) == strlen(name) &&
185 strncmp(var, name, (eq - var)) == 0) {
186 return snprintf(val, val_len, "%s", value);
191 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
193 int bcm47xx_nvram_gpio_pin(const char *name)
199 for (i = 0; i < 16; i++) {
200 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
203 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
206 if (!strcmp(name, buf))
211 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);