blob: 7c77a88eedf90712b379c92acc4274166c1d0c01 [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>
Rafał Miłecki138173d2014-12-01 07:58:18 +010019#include <linux/bcm47xx_nvram.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020020
Rafał Miłecki341097f2014-10-30 12:50:03 +010021#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
22#define NVRAM_SPACE 0x8000
23
24#define FLASH_MIN 0x00020000 /* Minimum flash size */
25
26struct 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 */
32};
33
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020034static char nvram_buf[NVRAM_SPACE];
Hauke Mehrtensf4c4d582013-10-13 22:56:50 +020035static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020036
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020037static u32 find_nvram_size(void __iomem *end)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020038{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020039 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020040 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020041
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000042 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020043 header = (struct nvram_header *)(end - nvram_sizes[i]);
Rafał Miłecki341097f2014-10-30 12:50:03 +010044 if (header->magic == NVRAM_MAGIC)
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000045 return nvram_sizes[i];
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020046 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020047
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000048 return 0;
49}
50
51/* Probe for NVRAM header */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020052static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020053{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020054 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020055 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020056 u32 off;
57 u32 *src, *dst;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000058 u32 size;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020059
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020060 if (nvram_buf[0]) {
61 pr_warn("nvram already initialized\n");
62 return -EEXIST;
63 }
64
Hauke Mehrtensc4485672012-12-26 19:51:11 +000065 /* TODO: when nvram is on nand flash check for bad blocks first. */
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020066 off = FLASH_MIN;
67 while (off <= lim) {
68 /* Windowed flash access */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020069 size = find_nvram_size(iobase + off);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000070 if (size) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020071 header = (struct nvram_header *)(iobase + off - size);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020072 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000073 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020074 off <<= 1;
75 }
76
77 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020078 header = (struct nvram_header *)(iobase + 4096);
Rafał Miłecki341097f2014-10-30 12:50:03 +010079 if (header->magic == NVRAM_MAGIC) {
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000080 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020081 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000082 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020083
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020084 header = (struct nvram_header *)(iobase + 1024);
Rafał Miłecki341097f2014-10-30 12:50:03 +010085 if (header->magic == NVRAM_MAGIC) {
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000086 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020087 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000088 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020089
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000090 pr_err("no nvram found\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000091 return -ENXIO;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020092
93found:
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000094 if (header->len > size)
95 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
96 if (header->len > NVRAM_SPACE)
97 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
98 header->len, NVRAM_SPACE);
99
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200100 src = (u32 *) header;
101 dst = (u32 *) nvram_buf;
102 for (i = 0; i < sizeof(struct nvram_header); i += 4)
Rafał Miłecki23d2bc42014-12-10 11:49:53 +0100103 *dst++ = __raw_readl(src++);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +0000104 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
Rafał Miłecki23d2bc42014-12-10 11:49:53 +0100105 *dst++ = readl(src++);
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000106
107 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200108}
109
Rafał Miłecki21400f22014-09-03 22:59:45 +0200110/*
111 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
112 * subsystem to access flash. We can't even use platform device / driver to
113 * store memory offset.
114 * To handle this we provide following symbol. It's supposed to be called as
115 * soon as we get info about flash device, before any NVRAM entry is needed.
116 */
117int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
Rafał Miłecki8d602dd2014-09-03 22:51:06 +0200118{
119 void __iomem *iobase;
120 int err;
121
122 iobase = ioremap_nocache(base, lim);
123 if (!iobase)
124 return -ENOMEM;
125
126 err = nvram_find_and_copy(iobase, lim);
127
128 iounmap(iobase);
129
130 return err;
131}
132
Hauke Mehrtense58da162012-12-26 19:51:12 +0000133static int nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000134{
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100135#ifdef CONFIG_MTD
136 struct mtd_info *mtd;
137 struct nvram_header header;
138 size_t bytes_read;
139 int err, i;
140
141 mtd = get_mtd_device_nm("nvram");
142 if (IS_ERR(mtd))
143 return -ENODEV;
144
145 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
146 loff_t from = mtd->size - nvram_sizes[i];
147
148 if (from < 0)
149 continue;
150
151 err = mtd_read(mtd, from, sizeof(header), &bytes_read,
152 (uint8_t *)&header);
Rafał Miłecki341097f2014-10-30 12:50:03 +0100153 if (!err && header.magic == NVRAM_MAGIC) {
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100154 u8 *dst = (uint8_t *)nvram_buf;
155 size_t len = header.len;
156
157 if (header.len > NVRAM_SPACE) {
158 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
159 header.len, NVRAM_SPACE);
160 len = NVRAM_SPACE;
161 }
162
163 err = mtd_read(mtd, from, len, &bytes_read, dst);
164 if (err)
165 return err;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100166
167 return 0;
168 }
169 }
170#endif
Rafał Miłecki7177efc2014-10-28 13:30:23 +0100171
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000172 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000173}
174
Rafał Miłecki341097f2014-10-30 12:50:03 +0100175int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200176{
177 char *var, *value, *end, *eq;
Rafał Miłecki80aaaa82014-12-10 11:49:54 +0100178 int data_left, err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200179
180 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000181 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200182
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000183 if (!nvram_buf[0]) {
Hauke Mehrtense58da162012-12-26 19:51:12 +0000184 err = nvram_init();
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000185 if (err)
186 return err;
187 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200188
189 /* Look for name=value and return value */
190 var = &nvram_buf[sizeof(struct nvram_header)];
191 end = nvram_buf + sizeof(nvram_buf) - 2;
192 end[0] = end[1] = '\0';
193 for (; *var; var = value + strlen(value) + 1) {
Rafał Miłecki80aaaa82014-12-10 11:49:54 +0100194 data_left = end - var;
195
196 eq = strnchr(var, data_left, '=');
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200197 if (!eq)
198 break;
199 value = eq + 1;
200 if ((eq - var) == strlen(name) &&
201 strncmp(var, name, (eq - var)) == 0) {
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100202 return snprintf(val, val_len, "%s", value);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200203 }
204 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000205 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200206}
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000207EXPORT_SYMBOL(bcm47xx_nvram_getenv);
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200208
209int bcm47xx_nvram_gpio_pin(const char *name)
210{
211 int i, err;
212 char nvram_var[10];
213 char buf[30];
214
Rafał Miłecki4fe21692014-02-13 17:48:12 +0100215 for (i = 0; i < 32; i++) {
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200216 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
217 if (err <= 0)
218 continue;
219 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
220 if (err <= 0)
221 continue;
222 if (!strcmp(name, buf))
223 return i;
224 }
225 return -ENOENT;
226}
227EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);