Merge remote-tracking branch 's5p/for-next'
[linux-2.6/next.git] / arch / mips / bcm47xx / nvram.c
blob54db815bc86c46e3586e5b9c7061cb32e96f1e37
1 /*
2 * BCM947xx nvram variable access
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2011 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 <asm/mach-bcm47xx/nvram.h>
22 #include <asm/mach-bcm47xx/bcm47xx.h>
24 static char nvram_buf[NVRAM_SPACE];
26 /* Probe for NVRAM header */
27 static void early_nvram_init(void)
29 struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
30 struct nvram_header *header;
31 int i;
32 u32 base, lim, off;
33 u32 *src, *dst;
35 base = mcore->flash_window;
36 lim = mcore->flash_window_size;
38 off = FLASH_MIN;
39 while (off <= lim) {
40 /* Windowed flash access */
41 header = (struct nvram_header *)
42 KSEG1ADDR(base + off - NVRAM_SPACE);
43 if (header->magic == NVRAM_HEADER)
44 goto found;
45 off <<= 1;
48 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
49 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
50 if (header->magic == NVRAM_HEADER)
51 goto found;
53 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
54 if (header->magic == NVRAM_HEADER)
55 goto found;
57 return;
59 found:
60 src = (u32 *) header;
61 dst = (u32 *) nvram_buf;
62 for (i = 0; i < sizeof(struct nvram_header); i += 4)
63 *dst++ = *src++;
64 for (; i < header->len && i < NVRAM_SPACE; i += 4)
65 *dst++ = le32_to_cpu(*src++);
68 int nvram_getenv(char *name, char *val, size_t val_len)
70 char *var, *value, *end, *eq;
72 if (!name)
73 return NVRAM_ERR_INV_PARAM;
75 if (!nvram_buf[0])
76 early_nvram_init();
78 /* Look for name=value and return value */
79 var = &nvram_buf[sizeof(struct nvram_header)];
80 end = nvram_buf + sizeof(nvram_buf) - 2;
81 end[0] = end[1] = '\0';
82 for (; *var; var = value + strlen(value) + 1) {
83 eq = strchr(var, '=');
84 if (!eq)
85 break;
86 value = eq + 1;
87 if ((eq - var) == strlen(name) &&
88 strncmp(var, name, (eq - var)) == 0) {
89 snprintf(val, val_len, "%s", value);
90 return 0;
93 return NVRAM_ERR_ENVNOTFOUND;
95 EXPORT_SYMBOL(nvram_getenv);