Merge tag 'fixes-for-v4.0-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[linux/fpc-iii.git] / arch / mips / bcm47xx / nvram.c
blobc5c381c43f178f8adb3b8491b06e29bca152b5fb
1 /*
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 */
26 struct nvram_header {
27 u32 magic;
28 u32 len;
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;
40 int i;
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];
48 return 0;
51 /* Probe for NVRAM header */
52 static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
54 struct nvram_header __iomem *header;
55 int i;
56 u32 off;
57 u32 *src, *dst;
58 u32 size;
60 if (nvram_buf[0]) {
61 pr_warn("nvram already initialized\n");
62 return -EEXIST;
65 /* TODO: when nvram is on nand flash check for bad blocks first. */
66 off = FLASH_MIN;
67 while (off <= lim) {
68 /* Windowed flash access */
69 size = find_nvram_size(iobase + off);
70 if (size) {
71 header = (struct nvram_header *)(iobase + off - size);
72 goto found;
74 off <<= 1;
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) {
80 size = NVRAM_SPACE;
81 goto found;
84 header = (struct nvram_header *)(iobase + 1024);
85 if (header->magic == NVRAM_MAGIC) {
86 size = NVRAM_SPACE;
87 goto found;
90 pr_err("no nvram found\n");
91 return -ENXIO;
93 found:
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)
104 *dst++ = *src++;
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);
109 return 0;
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;
122 int err;
124 iobase = ioremap_nocache(base, lim);
125 if (!iobase)
126 return -ENOMEM;
128 err = nvram_find_and_copy(iobase, lim);
130 iounmap(iobase);
132 return err;
135 static int nvram_init(void)
137 #ifdef CONFIG_MTD
138 struct mtd_info *mtd;
139 struct nvram_header header;
140 size_t bytes_read;
141 int err, i;
143 mtd = get_mtd_device_nm("nvram");
144 if (IS_ERR(mtd))
145 return -ENODEV;
147 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
148 loff_t from = mtd->size - nvram_sizes[i];
150 if (from < 0)
151 continue;
153 err = mtd_read(mtd, from, sizeof(header), &bytes_read,
154 (uint8_t *)&header);
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);
162 len = NVRAM_SPACE;
165 err = mtd_read(mtd, from, len, &bytes_read, dst);
166 if (err)
167 return err;
168 memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
170 return 0;
173 #endif
175 return -ENXIO;
178 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
180 char *var, *value, *end, *eq;
181 int err;
183 if (!name)
184 return -EINVAL;
186 if (!nvram_buf[0]) {
187 err = nvram_init();
188 if (err)
189 return err;
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, '=');
198 if (!eq)
199 break;
200 value = eq + 1;
201 if ((eq - var) == strlen(name) &&
202 strncmp(var, name, (eq - var)) == 0) {
203 return snprintf(val, val_len, "%s", value);
206 return -ENOENT;
208 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
210 int bcm47xx_nvram_gpio_pin(const char *name)
212 int i, err;
213 char nvram_var[10];
214 char buf[30];
216 for (i = 0; i < 32; i++) {
217 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
218 if (err <= 0)
219 continue;
220 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
221 if (err <= 0)
222 continue;
223 if (!strcmp(name, buf))
224 return i;
226 return -ENOENT;
228 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);