aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/bcm47xx/nvram.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/mips/bcm47xx/nvram.c')
-rw-r--r--arch/mips/bcm47xx/nvram.c159
1 files changed, 118 insertions, 41 deletions
diff --git a/arch/mips/bcm47xx/nvram.c b/arch/mips/bcm47xx/nvram.c
index c37419c11c92..cc40b74940f5 100644
--- a/arch/mips/bcm47xx/nvram.c
+++ b/arch/mips/bcm47xx/nvram.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * Copyright (C) 2005 Broadcom Corporation 4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org> 5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de> 6 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify it 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 9 * under the terms of the GNU General Public License as published by the
@@ -18,83 +18,160 @@
18#include <linux/kernel.h> 18#include <linux/kernel.h>
19#include <linux/string.h> 19#include <linux/string.h>
20#include <asm/addrspace.h> 20#include <asm/addrspace.h>
21#include <asm/mach-bcm47xx/nvram.h> 21#include <bcm47xx_nvram.h>
22#include <asm/mach-bcm47xx/bcm47xx.h> 22#include <asm/mach-bcm47xx/bcm47xx.h>
23 23
24static char nvram_buf[NVRAM_SPACE]; 24static char nvram_buf[NVRAM_SPACE];
25 25
26static u32 find_nvram_size(u32 end)
27{
28 struct nvram_header *header;
29 u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
30 int i;
31
32 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
33 header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
34 if (header->magic == NVRAM_HEADER)
35 return nvram_sizes[i];
36 }
37
38 return 0;
39}
40
26/* Probe for NVRAM header */ 41/* Probe for NVRAM header */
27static void early_nvram_init(void) 42static int nvram_find_and_copy(u32 base, u32 lim)
28{ 43{
29#ifdef CONFIG_BCM47XX_SSB
30 struct ssb_mipscore *mcore_ssb;
31#endif
32#ifdef CONFIG_BCM47XX_BCMA
33 struct bcma_drv_cc *bcma_cc;
34#endif
35 struct nvram_header *header; 44 struct nvram_header *header;
36 int i; 45 int i;
37 u32 base = 0;
38 u32 lim = 0;
39 u32 off; 46 u32 off;
40 u32 *src, *dst; 47 u32 *src, *dst;
48 u32 size;
41 49
42 switch (bcm47xx_bus_type) { 50 /* TODO: when nvram is on nand flash check for bad blocks first. */
43#ifdef CONFIG_BCM47XX_SSB
44 case BCM47XX_BUS_TYPE_SSB:
45 mcore_ssb = &bcm47xx_bus.ssb.mipscore;
46 base = mcore_ssb->pflash.window;
47 lim = mcore_ssb->pflash.window_size;
48 break;
49#endif
50#ifdef CONFIG_BCM47XX_BCMA
51 case BCM47XX_BUS_TYPE_BCMA:
52 bcma_cc = &bcm47xx_bus.bcma.bus.drv_cc;
53 base = bcma_cc->pflash.window;
54 lim = bcma_cc->pflash.window_size;
55 break;
56#endif
57 }
58
59 off = FLASH_MIN; 51 off = FLASH_MIN;
60 while (off <= lim) { 52 while (off <= lim) {
61 /* Windowed flash access */ 53 /* Windowed flash access */
62 header = (struct nvram_header *) 54 size = find_nvram_size(base + off);
63 KSEG1ADDR(base + off - NVRAM_SPACE); 55 if (size) {
64 if (header->magic == NVRAM_HEADER) 56 header = (struct nvram_header *)KSEG1ADDR(base + off -
57 size);
65 goto found; 58 goto found;
59 }
66 off <<= 1; 60 off <<= 1;
67 } 61 }
68 62
69 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ 63 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
70 header = (struct nvram_header *) KSEG1ADDR(base + 4096); 64 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
71 if (header->magic == NVRAM_HEADER) 65 if (header->magic == NVRAM_HEADER) {
66 size = NVRAM_SPACE;
72 goto found; 67 goto found;
68 }
73 69
74 header = (struct nvram_header *) KSEG1ADDR(base + 1024); 70 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
75 if (header->magic == NVRAM_HEADER) 71 if (header->magic == NVRAM_HEADER) {
72 size = NVRAM_SPACE;
76 goto found; 73 goto found;
74 }
77 75
78 return; 76 pr_err("no nvram found\n");
77 return -ENXIO;
79 78
80found: 79found:
80
81 if (header->len > size)
82 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
83 if (header->len > NVRAM_SPACE)
84 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
85 header->len, NVRAM_SPACE);
86
81 src = (u32 *) header; 87 src = (u32 *) header;
82 dst = (u32 *) nvram_buf; 88 dst = (u32 *) nvram_buf;
83 for (i = 0; i < sizeof(struct nvram_header); i += 4) 89 for (i = 0; i < sizeof(struct nvram_header); i += 4)
84 *dst++ = *src++; 90 *dst++ = *src++;
85 for (; i < header->len && i < NVRAM_SPACE; i += 4) 91 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
86 *dst++ = le32_to_cpu(*src++); 92 *dst++ = le32_to_cpu(*src++);
93 memset(dst, 0x0, NVRAM_SPACE - i);
94
95 return 0;
87} 96}
88 97
89int nvram_getenv(char *name, char *val, size_t val_len) 98#ifdef CONFIG_BCM47XX_SSB
99static int nvram_init_ssb(void)
100{
101 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
102 u32 base;
103 u32 lim;
104
105 if (mcore->pflash.present) {
106 base = mcore->pflash.window;
107 lim = mcore->pflash.window_size;
108 } else {
109 pr_err("Couldn't find supported flash memory\n");
110 return -ENXIO;
111 }
112
113 return nvram_find_and_copy(base, lim);
114}
115#endif
116
117#ifdef CONFIG_BCM47XX_BCMA
118static int nvram_init_bcma(void)
119{
120 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
121 u32 base;
122 u32 lim;
123
124#ifdef CONFIG_BCMA_NFLASH
125 if (cc->nflash.boot) {
126 base = BCMA_SOC_FLASH1;
127 lim = BCMA_SOC_FLASH1_SZ;
128 } else
129#endif
130 if (cc->pflash.present) {
131 base = cc->pflash.window;
132 lim = cc->pflash.window_size;
133#ifdef CONFIG_BCMA_SFLASH
134 } else if (cc->sflash.present) {
135 base = cc->sflash.window;
136 lim = cc->sflash.size;
137#endif
138 } else {
139 pr_err("Couldn't find supported flash memory\n");
140 return -ENXIO;
141 }
142
143 return nvram_find_and_copy(base, lim);
144}
145#endif
146
147static int nvram_init(void)
148{
149 switch (bcm47xx_bus_type) {
150#ifdef CONFIG_BCM47XX_SSB
151 case BCM47XX_BUS_TYPE_SSB:
152 return nvram_init_ssb();
153#endif
154#ifdef CONFIG_BCM47XX_BCMA
155 case BCM47XX_BUS_TYPE_BCMA:
156 return nvram_init_bcma();
157#endif
158 }
159 return -ENXIO;
160}
161
162int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
90{ 163{
91 char *var, *value, *end, *eq; 164 char *var, *value, *end, *eq;
165 int err;
92 166
93 if (!name) 167 if (!name)
94 return NVRAM_ERR_INV_PARAM; 168 return -EINVAL;
95 169
96 if (!nvram_buf[0]) 170 if (!nvram_buf[0]) {
97 early_nvram_init(); 171 err = nvram_init();
172 if (err)
173 return err;
174 }
98 175
99 /* Look for name=value and return value */ 176 /* Look for name=value and return value */
100 var = &nvram_buf[sizeof(struct nvram_header)]; 177 var = &nvram_buf[sizeof(struct nvram_header)];
@@ -110,6 +187,6 @@ int nvram_getenv(char *name, char *val, size_t val_len)
110 return snprintf(val, val_len, "%s", value); 187 return snprintf(val, val_len, "%s", value);
111 } 188 }
112 } 189 }
113 return NVRAM_ERR_ENVNOTFOUND; 190 return -ENOENT;
114} 191}
115EXPORT_SYMBOL(nvram_getenv); 192EXPORT_SYMBOL(bcm47xx_nvram_getenv);