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 | ||
cc4403e0 | 26 | static int nvram_find_and_copy(u32 base, u32 lim) |
121915c4 | 27 | { |
121915c4 WB |
28 | struct nvram_header *header; |
29 | int i; | |
08ccf572 | 30 | u32 off; |
121915c4 WB |
31 | u32 *src, *dst; |
32 | ||
c4485671 | 33 | /* TODO: when nvram is on nand flash check for bad blocks first. */ |
121915c4 WB |
34 | off = FLASH_MIN; |
35 | while (off <= lim) { | |
36 | /* Windowed flash access */ | |
37 | header = (struct nvram_header *) | |
38 | KSEG1ADDR(base + off - NVRAM_SPACE); | |
39 | if (header->magic == NVRAM_HEADER) | |
40 | goto found; | |
41 | off <<= 1; | |
42 | } | |
43 | ||
44 | /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ | |
45 | header = (struct nvram_header *) KSEG1ADDR(base + 4096); | |
46 | if (header->magic == NVRAM_HEADER) | |
47 | goto found; | |
48 | ||
49 | header = (struct nvram_header *) KSEG1ADDR(base + 1024); | |
50 | if (header->magic == NVRAM_HEADER) | |
51 | goto found; | |
52 | ||
cc4403e0 | 53 | return -ENXIO; |
121915c4 WB |
54 | |
55 | found: | |
56 | src = (u32 *) header; | |
57 | dst = (u32 *) nvram_buf; | |
58 | for (i = 0; i < sizeof(struct nvram_header); i += 4) | |
59 | *dst++ = *src++; | |
60 | for (; i < header->len && i < NVRAM_SPACE; i += 4) | |
61 | *dst++ = le32_to_cpu(*src++); | |
cc4403e0 HM |
62 | |
63 | return 0; | |
121915c4 WB |
64 | } |
65 | ||
bb765632 | 66 | #ifdef CONFIG_BCM47XX_SSB |
cc4403e0 | 67 | static int nvram_init_ssb(void) |
bb765632 RM |
68 | { |
69 | struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore; | |
70 | u32 base; | |
71 | u32 lim; | |
72 | ||
73 | if (mcore->pflash.present) { | |
74 | base = mcore->pflash.window; | |
75 | lim = mcore->pflash.window_size; | |
76 | } else { | |
77 | pr_err("Couldn't find supported flash memory\n"); | |
cc4403e0 | 78 | return -ENXIO; |
bb765632 RM |
79 | } |
80 | ||
cc4403e0 | 81 | return nvram_find_and_copy(base, lim); |
bb765632 RM |
82 | } |
83 | #endif | |
84 | ||
85 | #ifdef CONFIG_BCM47XX_BCMA | |
cc4403e0 | 86 | static int nvram_init_bcma(void) |
bb765632 RM |
87 | { |
88 | struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc; | |
89 | u32 base; | |
90 | u32 lim; | |
91 | ||
c4485671 HM |
92 | #ifdef CONFIG_BCMA_NFLASH |
93 | if (cc->nflash.boot) { | |
94 | base = BCMA_SOC_FLASH1; | |
95 | lim = BCMA_SOC_FLASH1_SZ; | |
96 | } else | |
97 | #endif | |
bb765632 RM |
98 | if (cc->pflash.present) { |
99 | base = cc->pflash.window; | |
100 | lim = cc->pflash.window_size; | |
101 | #ifdef CONFIG_BCMA_SFLASH | |
102 | } else if (cc->sflash.present) { | |
103 | base = cc->sflash.window; | |
104 | lim = cc->sflash.size; | |
105 | #endif | |
106 | } else { | |
107 | pr_err("Couldn't find supported flash memory\n"); | |
cc4403e0 | 108 | return -ENXIO; |
bb765632 RM |
109 | } |
110 | ||
cc4403e0 | 111 | return nvram_find_and_copy(base, lim); |
bb765632 RM |
112 | } |
113 | #endif | |
114 | ||
cc4403e0 | 115 | static int early_nvram_init(void) |
bb765632 RM |
116 | { |
117 | switch (bcm47xx_bus_type) { | |
118 | #ifdef CONFIG_BCM47XX_SSB | |
119 | case BCM47XX_BUS_TYPE_SSB: | |
cc4403e0 | 120 | return nvram_init_ssb(); |
bb765632 RM |
121 | #endif |
122 | #ifdef CONFIG_BCM47XX_BCMA | |
123 | case BCM47XX_BUS_TYPE_BCMA: | |
cc4403e0 | 124 | return nvram_init_bcma(); |
bb765632 RM |
125 | #endif |
126 | } | |
cc4403e0 | 127 | return -ENXIO; |
bb765632 RM |
128 | } |
129 | ||
121915c4 WB |
130 | int nvram_getenv(char *name, char *val, size_t val_len) |
131 | { | |
132 | char *var, *value, *end, *eq; | |
cc4403e0 | 133 | int err; |
121915c4 WB |
134 | |
135 | if (!name) | |
ee7e2f3c | 136 | return -EINVAL; |
121915c4 | 137 | |
cc4403e0 HM |
138 | if (!nvram_buf[0]) { |
139 | err = early_nvram_init(); | |
140 | if (err) | |
141 | return err; | |
142 | } | |
121915c4 WB |
143 | |
144 | /* Look for name=value and return value */ | |
145 | var = &nvram_buf[sizeof(struct nvram_header)]; | |
146 | end = nvram_buf + sizeof(nvram_buf) - 2; | |
147 | end[0] = end[1] = '\0'; | |
148 | for (; *var; var = value + strlen(value) + 1) { | |
149 | eq = strchr(var, '='); | |
150 | if (!eq) | |
151 | break; | |
152 | value = eq + 1; | |
153 | if ((eq - var) == strlen(name) && | |
154 | strncmp(var, name, (eq - var)) == 0) { | |
44d4b2ae | 155 | return snprintf(val, val_len, "%s", value); |
121915c4 WB |
156 | } |
157 | } | |
ee7e2f3c | 158 | return -ENOENT; |
121915c4 WB |
159 | } |
160 | EXPORT_SYMBOL(nvram_getenv); |