Commit | Line | Data |
---|---|---|
121915c4 WB |
1 | /* |
2 | * BCM947xx nvram variable access | |
3 | * | |
4 | * Copyright (C) 2005 Broadcom Corporation | |
5 | * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org> | |
fe6f3642 | 6 | * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de> |
121915c4 WB |
7 | * |
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. | |
12 | */ | |
13 | ||
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> | |
23 | ||
24 | static char nvram_buf[NVRAM_SPACE]; | |
25 | ||
26 | /* Probe for NVRAM header */ | |
fe6f3642 | 27 | static void early_nvram_init(void) |
121915c4 | 28 | { |
08ccf572 | 29 | struct ssb_mipscore *mcore_ssb; |
121915c4 WB |
30 | struct nvram_header *header; |
31 | int i; | |
08ccf572 HM |
32 | u32 base = 0; |
33 | u32 lim = 0; | |
34 | u32 off; | |
121915c4 WB |
35 | u32 *src, *dst; |
36 | ||
08ccf572 HM |
37 | switch (bcm47xx_bus_type) { |
38 | case BCM47XX_BUS_TYPE_SSB: | |
39 | mcore_ssb = &bcm47xx_bus.ssb.mipscore; | |
40 | base = mcore_ssb->flash_window; | |
41 | lim = mcore_ssb->flash_window_size; | |
42 | break; | |
43 | } | |
121915c4 WB |
44 | |
45 | off = FLASH_MIN; | |
46 | while (off <= lim) { | |
47 | /* Windowed flash access */ | |
48 | header = (struct nvram_header *) | |
49 | KSEG1ADDR(base + off - NVRAM_SPACE); | |
50 | if (header->magic == NVRAM_HEADER) | |
51 | goto found; | |
52 | off <<= 1; | |
53 | } | |
54 | ||
55 | /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ | |
56 | header = (struct nvram_header *) KSEG1ADDR(base + 4096); | |
57 | if (header->magic == NVRAM_HEADER) | |
58 | goto found; | |
59 | ||
60 | header = (struct nvram_header *) KSEG1ADDR(base + 1024); | |
61 | if (header->magic == NVRAM_HEADER) | |
62 | goto found; | |
63 | ||
64 | return; | |
65 | ||
66 | found: | |
67 | src = (u32 *) header; | |
68 | dst = (u32 *) nvram_buf; | |
69 | for (i = 0; i < sizeof(struct nvram_header); i += 4) | |
70 | *dst++ = *src++; | |
71 | for (; i < header->len && i < NVRAM_SPACE; i += 4) | |
72 | *dst++ = le32_to_cpu(*src++); | |
73 | } | |
74 | ||
75 | int nvram_getenv(char *name, char *val, size_t val_len) | |
76 | { | |
77 | char *var, *value, *end, *eq; | |
78 | ||
79 | if (!name) | |
47a34861 | 80 | return NVRAM_ERR_INV_PARAM; |
121915c4 WB |
81 | |
82 | if (!nvram_buf[0]) | |
83 | early_nvram_init(); | |
84 | ||
85 | /* Look for name=value and return value */ | |
86 | var = &nvram_buf[sizeof(struct nvram_header)]; | |
87 | end = nvram_buf + sizeof(nvram_buf) - 2; | |
88 | end[0] = end[1] = '\0'; | |
89 | for (; *var; var = value + strlen(value) + 1) { | |
90 | eq = strchr(var, '='); | |
91 | if (!eq) | |
92 | break; | |
93 | value = eq + 1; | |
94 | if ((eq - var) == strlen(name) && | |
95 | strncmp(var, name, (eq - var)) == 0) { | |
96 | snprintf(val, val_len, "%s", value); | |
97 | return 0; | |
98 | } | |
99 | } | |
47a34861 | 100 | return NVRAM_ERR_ENVNOTFOUND; |
121915c4 WB |
101 | } |
102 | EXPORT_SYMBOL(nvram_getenv); |