1 /*
2  * BCM947xx nvram variable access
3  *
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>
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/io.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/bcm47xx_nvram.h>
21 
22 #define NVRAM_MAGIC			0x48534C46	/* 'FLSH' */
23 #define NVRAM_SPACE			0x10000
24 #define NVRAM_MAX_GPIO_ENTRIES		32
25 #define NVRAM_MAX_GPIO_VALUE_LEN	30
26 
27 #define FLASH_MIN		0x00020000	/* Minimum flash size */
28 
29 struct nvram_header {
30 	u32 magic;
31 	u32 len;
32 	u32 crc_ver_init;	/* 0:7 crc, 8:15 ver, 16:31 sdram_init */
33 	u32 config_refresh;	/* 0:15 sdram_config, 16:31 sdram_refresh */
34 	u32 config_ncdl;	/* ncdl values for memc */
35 };
36 
37 static char nvram_buf[NVRAM_SPACE];
38 static size_t nvram_len;
39 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
40 
41 static u32 find_nvram_size(void __iomem *end)
42 {
43 	struct nvram_header __iomem *header;
44 	int i;
45 
46 	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
47 		header = (struct nvram_header *)(end - nvram_sizes[i]);
48 		if (header->magic == NVRAM_MAGIC)
49 			return nvram_sizes[i];
50 	}
51 
52 	return 0;
53 }
54 
55 /* Probe for NVRAM header */
56 static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
57 {
58 	struct nvram_header __iomem *header;
59 	u32 off;
60 	u32 size;
61 
62 	if (nvram_len) {
63 		pr_warn("nvram already initialized\n");
64 		return -EEXIST;
65 	}
66 
67 	/* TODO: when nvram is on nand flash check for bad blocks first. */
68 	off = FLASH_MIN;
69 	while (off <= lim) {
70 		/* Windowed flash access */
71 		size = find_nvram_size(iobase + off);
72 		if (size) {
73 			header = (struct nvram_header *)(iobase + off - size);
74 			goto found;
75 		}
76 		off <<= 1;
77 	}
78 
79 	/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
80 	header = (struct nvram_header *)(iobase + 4096);
81 	if (header->magic == NVRAM_MAGIC) {
82 		size = NVRAM_SPACE;
83 		goto found;
84 	}
85 
86 	header = (struct nvram_header *)(iobase + 1024);
87 	if (header->magic == NVRAM_MAGIC) {
88 		size = NVRAM_SPACE;
89 		goto found;
90 	}
91 
92 	pr_err("no nvram found\n");
93 	return -ENXIO;
94 
95 found:
96 	__ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
97 	nvram_len = ((struct nvram_header *)(nvram_buf))->len;
98 	if (nvram_len > size) {
99 		pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
100 		nvram_len = size;
101 	}
102 	if (nvram_len >= NVRAM_SPACE) {
103 		pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
104 		       nvram_len, NVRAM_SPACE - 1);
105 		nvram_len = NVRAM_SPACE - 1;
106 	}
107 	/* proceed reading data after header */
108 	__ioread32_copy(nvram_buf + sizeof(*header), header + 1,
109 			DIV_ROUND_UP(nvram_len, 4));
110 	nvram_buf[NVRAM_SPACE - 1] = '\0';
111 
112 	return 0;
113 }
114 
115 /*
116  * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
117  * subsystem to access flash. We can't even use platform device / driver to
118  * store memory offset.
119  * To handle this we provide following symbol. It's supposed to be called as
120  * soon as we get info about flash device, before any NVRAM entry is needed.
121  */
122 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
123 {
124 	void __iomem *iobase;
125 	int err;
126 
127 	iobase = ioremap_nocache(base, lim);
128 	if (!iobase)
129 		return -ENOMEM;
130 
131 	err = nvram_find_and_copy(iobase, lim);
132 
133 	iounmap(iobase);
134 
135 	return err;
136 }
137 
138 static int nvram_init(void)
139 {
140 #ifdef CONFIG_MTD
141 	struct mtd_info *mtd;
142 	struct nvram_header header;
143 	size_t bytes_read;
144 	int err;
145 
146 	mtd = get_mtd_device_nm("nvram");
147 	if (IS_ERR(mtd))
148 		return -ENODEV;
149 
150 	err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
151 	if (!err && header.magic == NVRAM_MAGIC &&
152 	    header.len > sizeof(header)) {
153 		nvram_len = header.len;
154 		if (nvram_len >= NVRAM_SPACE) {
155 			pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
156 				header.len, NVRAM_SPACE);
157 			nvram_len = NVRAM_SPACE - 1;
158 		}
159 
160 		err = mtd_read(mtd, 0, nvram_len, &nvram_len,
161 			       (u8 *)nvram_buf);
162 		return err;
163 	}
164 #endif
165 
166 	return -ENXIO;
167 }
168 
169 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
170 {
171 	char *var, *value, *end, *eq;
172 	int err;
173 
174 	if (!name)
175 		return -EINVAL;
176 
177 	if (!nvram_len) {
178 		err = nvram_init();
179 		if (err)
180 			return err;
181 	}
182 
183 	/* Look for name=value and return value */
184 	var = &nvram_buf[sizeof(struct nvram_header)];
185 	end = nvram_buf + sizeof(nvram_buf);
186 	while (var < end && *var) {
187 		eq = strchr(var, '=');
188 		if (!eq)
189 			break;
190 		value = eq + 1;
191 		if (eq - var == strlen(name) &&
192 		    strncmp(var, name, eq - var) == 0)
193 			return snprintf(val, val_len, "%s", value);
194 		var = value + strlen(value) + 1;
195 	}
196 	return -ENOENT;
197 }
198 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
199 
200 int bcm47xx_nvram_gpio_pin(const char *name)
201 {
202 	int i, err;
203 	char nvram_var[] = "gpioXX";
204 	char buf[NVRAM_MAX_GPIO_VALUE_LEN];
205 
206 	/* TODO: Optimize it to don't call getenv so many times */
207 	for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
208 		err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
209 		if (err <= 0)
210 			continue;
211 		err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
212 		if (err <= 0)
213 			continue;
214 		if (!strcmp(name, buf))
215 			return i;
216 	}
217 	return -ENOENT;
218 }
219 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
220 
221 char *bcm47xx_nvram_get_contents(size_t *nvram_size)
222 {
223 	int err;
224 	char *nvram;
225 
226 	if (!nvram_len) {
227 		err = nvram_init();
228 		if (err)
229 			return NULL;
230 	}
231 
232 	*nvram_size = nvram_len - sizeof(struct nvram_header);
233 	nvram = vmalloc(*nvram_size);
234 	if (!nvram)
235 		return NULL;
236 	memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
237 
238 	return nvram;
239 }
240 EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
241 
242 MODULE_LICENSE("GPL v2");
243