2 * BCM947xx nvram variable access
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2012 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/types.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/mtd/mtd.h>
19 #include <bcm47xx_nvram.h>
21 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
22 #define NVRAM_SPACE 0x8000
24 #define FLASH_MIN 0x00020000 /* Minimum flash size */
29 u32 crc_ver_init
; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
30 u32 config_refresh
; /* 0:15 sdram_config, 16:31 sdram_refresh */
31 u32 config_ncdl
; /* ncdl values for memc */
34 static char nvram_buf
[NVRAM_SPACE
];
35 static const u32 nvram_sizes
[] = {0x8000, 0xF000, 0x10000};
37 static u32
find_nvram_size(void __iomem
*end
)
39 struct nvram_header __iomem
*header
;
42 for (i
= 0; i
< ARRAY_SIZE(nvram_sizes
); i
++) {
43 header
= (struct nvram_header
*)(end
- nvram_sizes
[i
]);
44 if (header
->magic
== NVRAM_MAGIC
)
45 return nvram_sizes
[i
];
51 /* Probe for NVRAM header */
52 static int nvram_find_and_copy(void __iomem
*iobase
, u32 lim
)
54 struct nvram_header __iomem
*header
;
61 pr_warn("nvram already initialized\n");
65 /* TODO: when nvram is on nand flash check for bad blocks first. */
68 /* Windowed flash access */
69 size
= find_nvram_size(iobase
+ off
);
71 header
= (struct nvram_header
*)(iobase
+ off
- size
);
77 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
78 header
= (struct nvram_header
*)(iobase
+ 4096);
79 if (header
->magic
== NVRAM_MAGIC
) {
84 header
= (struct nvram_header
*)(iobase
+ 1024);
85 if (header
->magic
== NVRAM_MAGIC
) {
90 pr_err("no nvram found\n");
95 if (header
->len
> size
)
96 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
97 if (header
->len
> NVRAM_SPACE
)
98 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
99 header
->len
, NVRAM_SPACE
);
101 src
= (u32
*) header
;
102 dst
= (u32
*) nvram_buf
;
103 for (i
= 0; i
< sizeof(struct nvram_header
); i
+= 4)
105 for (; i
< header
->len
&& i
< NVRAM_SPACE
&& i
< size
; i
+= 4)
106 *dst
++ = le32_to_cpu(*src
++);
107 memset(dst
, 0x0, NVRAM_SPACE
- i
);
113 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
114 * subsystem to access flash. We can't even use platform device / driver to
115 * store memory offset.
116 * To handle this we provide following symbol. It's supposed to be called as
117 * soon as we get info about flash device, before any NVRAM entry is needed.
119 int bcm47xx_nvram_init_from_mem(u32 base
, u32 lim
)
121 void __iomem
*iobase
;
124 iobase
= ioremap_nocache(base
, lim
);
128 err
= nvram_find_and_copy(iobase
, lim
);
135 static int nvram_init(void)
138 struct mtd_info
*mtd
;
139 struct nvram_header header
;
143 mtd
= get_mtd_device_nm("nvram");
147 for (i
= 0; i
< ARRAY_SIZE(nvram_sizes
); i
++) {
148 loff_t from
= mtd
->size
- nvram_sizes
[i
];
153 err
= mtd_read(mtd
, from
, sizeof(header
), &bytes_read
,
155 if (!err
&& header
.magic
== NVRAM_MAGIC
) {
156 u8
*dst
= (uint8_t *)nvram_buf
;
157 size_t len
= header
.len
;
159 if (header
.len
> NVRAM_SPACE
) {
160 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
161 header
.len
, NVRAM_SPACE
);
165 err
= mtd_read(mtd
, from
, len
, &bytes_read
, dst
);
168 memset(dst
+ bytes_read
, 0x0, NVRAM_SPACE
- bytes_read
);
178 int bcm47xx_nvram_getenv(const char *name
, char *val
, size_t val_len
)
180 char *var
, *value
, *end
, *eq
;
192 /* Look for name=value and return value */
193 var
= &nvram_buf
[sizeof(struct nvram_header
)];
194 end
= nvram_buf
+ sizeof(nvram_buf
) - 2;
195 end
[0] = end
[1] = '\0';
196 for (; *var
; var
= value
+ strlen(value
) + 1) {
197 eq
= strchr(var
, '=');
201 if ((eq
- var
) == strlen(name
) &&
202 strncmp(var
, name
, (eq
- var
)) == 0) {
203 return snprintf(val
, val_len
, "%s", value
);
208 EXPORT_SYMBOL(bcm47xx_nvram_getenv
);
210 int bcm47xx_nvram_gpio_pin(const char *name
)
216 for (i
= 0; i
< 32; i
++) {
217 err
= snprintf(nvram_var
, sizeof(nvram_var
), "gpio%i", i
);
220 err
= bcm47xx_nvram_getenv(nvram_var
, buf
, sizeof(buf
));
223 if (!strcmp(name
, buf
))
228 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin
);