nilfs2: do not allocate multiple super block instances for a device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / mips / bcm47xx / nvram.c
blobe5b6615731e5c24dd963190bc2d244930ba08299
1 /*
2 * BCM947xx nvram variable access
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/init.h>
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 <asm/mach-bcm47xx/nvram.h>
21 #include <asm/mach-bcm47xx/bcm47xx.h>
23 static char nvram_buf[NVRAM_SPACE];
25 /* Probe for NVRAM header */
26 static void __init early_nvram_init(void)
28 struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
29 struct nvram_header *header;
30 int i;
31 u32 base, lim, off;
32 u32 *src, *dst;
34 base = mcore->flash_window;
35 lim = mcore->flash_window_size;
37 off = FLASH_MIN;
38 while (off <= lim) {
39 /* Windowed flash access */
40 header = (struct nvram_header *)
41 KSEG1ADDR(base + off - NVRAM_SPACE);
42 if (header->magic == NVRAM_HEADER)
43 goto found;
44 off <<= 1;
47 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
48 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
49 if (header->magic == NVRAM_HEADER)
50 goto found;
52 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
53 if (header->magic == NVRAM_HEADER)
54 goto found;
56 return;
58 found:
59 src = (u32 *) header;
60 dst = (u32 *) nvram_buf;
61 for (i = 0; i < sizeof(struct nvram_header); i += 4)
62 *dst++ = *src++;
63 for (; i < header->len && i < NVRAM_SPACE; i += 4)
64 *dst++ = le32_to_cpu(*src++);
67 int nvram_getenv(char *name, char *val, size_t val_len)
69 char *var, *value, *end, *eq;
71 if (!name)
72 return NVRAM_ERR_INV_PARAM;
74 if (!nvram_buf[0])
75 early_nvram_init();
77 /* Look for name=value and return value */
78 var = &nvram_buf[sizeof(struct nvram_header)];
79 end = nvram_buf + sizeof(nvram_buf) - 2;
80 end[0] = end[1] = '\0';
81 for (; *var; var = value + strlen(value) + 1) {
82 eq = strchr(var, '=');
83 if (!eq)
84 break;
85 value = eq + 1;
86 if ((eq - var) == strlen(name) &&
87 strncmp(var, name, (eq - var)) == 0) {
88 snprintf(val, val_len, "%s", value);
89 return 0;
92 return NVRAM_ERR_ENVNOTFOUND;
94 EXPORT_SYMBOL(nvram_getenv);