blob: 8b6499100db3747bb8f797e00dd2a890d5188937 [file] [log] [blame]
Waldemar Brodkorb121915c2010-06-08 19:06:01 +02001/*
2 * BCM947xx nvram variable access
3 *
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
Hauke Mehrtensf36738d2012-12-26 19:51:13 +00006 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +02007 *
Ralf Baechle70342282013-01-22 12:59:30 +01008 * 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
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020010 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020014#include <linux/types.h>
15#include <linux/module.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020016#include <linux/kernel.h>
17#include <linux/string.h>
Rafał Miłecki9d1d0862014-10-29 10:05:06 +010018#include <linux/mtd/mtd.h>
Hauke Mehrtens111bd982012-12-26 19:51:14 +000019#include <bcm47xx_nvram.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020020
21static char nvram_buf[NVRAM_SPACE];
Hauke Mehrtensf4c4d582013-10-13 22:56:50 +020022static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020023
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020024static u32 find_nvram_size(void __iomem *end)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020025{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020026 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020027 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020028
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000029 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020030 header = (struct nvram_header *)(end - nvram_sizes[i]);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000031 if (header->magic == NVRAM_HEADER)
32 return nvram_sizes[i];
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020033 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020034
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000035 return 0;
36}
37
38/* Probe for NVRAM header */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020039static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020040{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020041 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020042 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020043 u32 off;
44 u32 *src, *dst;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000045 u32 size;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020046
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020047 if (nvram_buf[0]) {
48 pr_warn("nvram already initialized\n");
49 return -EEXIST;
50 }
51
Hauke Mehrtensc44856712012-12-26 19:51:11 +000052 /* TODO: when nvram is on nand flash check for bad blocks first. */
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020053 off = FLASH_MIN;
54 while (off <= lim) {
55 /* Windowed flash access */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020056 size = find_nvram_size(iobase + off);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000057 if (size) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020058 header = (struct nvram_header *)(iobase + off - size);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020059 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000060 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020061 off <<= 1;
62 }
63
64 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020065 header = (struct nvram_header *)(iobase + 4096);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000066 if (header->magic == NVRAM_HEADER) {
67 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020068 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000069 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020070
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020071 header = (struct nvram_header *)(iobase + 1024);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000072 if (header->magic == NVRAM_HEADER) {
73 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020074 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000075 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020076
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000077 pr_err("no nvram found\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000078 return -ENXIO;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020079
80found:
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000081
82 if (header->len > size)
83 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
84 if (header->len > NVRAM_SPACE)
85 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
86 header->len, NVRAM_SPACE);
87
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020088 src = (u32 *) header;
89 dst = (u32 *) nvram_buf;
90 for (i = 0; i < sizeof(struct nvram_header); i += 4)
91 *dst++ = *src++;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000092 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020093 *dst++ = le32_to_cpu(*src++);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000094 memset(dst, 0x0, NVRAM_SPACE - i);
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000095
96 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020097}
98
Rafał Miłecki21400f22014-09-03 22:59:45 +020099/*
100 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
101 * subsystem to access flash. We can't even use platform device / driver to
102 * store memory offset.
103 * To handle this we provide following symbol. It's supposed to be called as
104 * soon as we get info about flash device, before any NVRAM entry is needed.
105 */
106int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
Rafał Miłecki8d602dd2014-09-03 22:51:06 +0200107{
108 void __iomem *iobase;
109 int err;
110
111 iobase = ioremap_nocache(base, lim);
112 if (!iobase)
113 return -ENOMEM;
114
115 err = nvram_find_and_copy(iobase, lim);
116
117 iounmap(iobase);
118
119 return err;
120}
121
Hauke Mehrtense58da162012-12-26 19:51:12 +0000122static int nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000123{
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100124#ifdef CONFIG_MTD
125 struct mtd_info *mtd;
126 struct nvram_header header;
127 size_t bytes_read;
128 int err, i;
129
130 mtd = get_mtd_device_nm("nvram");
131 if (IS_ERR(mtd))
132 return -ENODEV;
133
134 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
135 loff_t from = mtd->size - nvram_sizes[i];
136
137 if (from < 0)
138 continue;
139
140 err = mtd_read(mtd, from, sizeof(header), &bytes_read,
141 (uint8_t *)&header);
142 if (!err && header.magic == NVRAM_HEADER) {
143 u8 *dst = (uint8_t *)nvram_buf;
144 size_t len = header.len;
145
146 if (header.len > NVRAM_SPACE) {
147 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
148 header.len, NVRAM_SPACE);
149 len = NVRAM_SPACE;
150 }
151
152 err = mtd_read(mtd, from, len, &bytes_read, dst);
153 if (err)
154 return err;
155 memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
156
157 return 0;
158 }
159 }
160#endif
Rafał Miłecki7177efc2014-10-28 13:30:23 +0100161
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000162 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000163}
164
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000165int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200166{
167 char *var, *value, *end, *eq;
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000168 int err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200169
170 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000171 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200172
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000173 if (!nvram_buf[0]) {
Hauke Mehrtense58da162012-12-26 19:51:12 +0000174 err = nvram_init();
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000175 if (err)
176 return err;
177 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200178
179 /* Look for name=value and return value */
180 var = &nvram_buf[sizeof(struct nvram_header)];
181 end = nvram_buf + sizeof(nvram_buf) - 2;
182 end[0] = end[1] = '\0';
183 for (; *var; var = value + strlen(value) + 1) {
184 eq = strchr(var, '=');
185 if (!eq)
186 break;
187 value = eq + 1;
188 if ((eq - var) == strlen(name) &&
189 strncmp(var, name, (eq - var)) == 0) {
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100190 return snprintf(val, val_len, "%s", value);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200191 }
192 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000193 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200194}
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000195EXPORT_SYMBOL(bcm47xx_nvram_getenv);
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200196
197int bcm47xx_nvram_gpio_pin(const char *name)
198{
199 int i, err;
200 char nvram_var[10];
201 char buf[30];
202
Rafał Miłecki4fe21692014-02-13 17:48:12 +0100203 for (i = 0; i < 32; i++) {
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200204 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
205 if (err <= 0)
206 continue;
207 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
208 if (err <= 0)
209 continue;
210 if (!strcmp(name, buf))
211 return i;
212 }
213 return -ENOENT;
214}
215EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);