blob: 95d028ca6f8ada96836042cb5488c749a77d3413 [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
Rafał Miłecki50c97912015-04-01 08:23:03 +020014#include <linux/io.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020015#include <linux/types.h>
16#include <linux/module.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020017#include <linux/kernel.h>
18#include <linux/string.h>
Rafał Miłecki9d1d0862014-10-29 10:05:06 +010019#include <linux/mtd/mtd.h>
Rafał Miłecki138173d2014-12-01 07:58:18 +010020#include <linux/bcm47xx_nvram.h>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020021
Rafał Miłeckid548ca62014-12-10 17:38:26 +010022#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
Rafał Miłecki6ab7c292015-04-01 08:23:04 +020023#define NVRAM_SPACE 0x10000
Rafał Miłeckid548ca62014-12-10 17:38:26 +010024#define NVRAM_MAX_GPIO_ENTRIES 32
25#define NVRAM_MAX_GPIO_VALUE_LEN 30
Rafał Miłecki341097f2014-10-30 12:50:03 +010026
27#define FLASH_MIN 0x00020000 /* Minimum flash size */
28
29struct 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
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020037static char nvram_buf[NVRAM_SPACE];
Hauke Mehrtensf4c4d582013-10-13 22:56:50 +020038static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020039
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020040static u32 find_nvram_size(void __iomem *end)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020041{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020042 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020043 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020044
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000045 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020046 header = (struct nvram_header *)(end - nvram_sizes[i]);
Rafał Miłecki341097f2014-10-30 12:50:03 +010047 if (header->magic == NVRAM_MAGIC)
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000048 return nvram_sizes[i];
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020049 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020050
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000051 return 0;
52}
53
54/* Probe for NVRAM header */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020055static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020056{
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020057 struct nvram_header __iomem *header;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020058 int i;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020059 u32 off;
60 u32 *src, *dst;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000061 u32 size;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020062
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020063 if (nvram_buf[0]) {
64 pr_warn("nvram already initialized\n");
65 return -EEXIST;
66 }
67
Hauke Mehrtensc4485672012-12-26 19:51:11 +000068 /* TODO: when nvram is on nand flash check for bad blocks first. */
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020069 off = FLASH_MIN;
70 while (off <= lim) {
71 /* Windowed flash access */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020072 size = find_nvram_size(iobase + off);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000073 if (size) {
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020074 header = (struct nvram_header *)(iobase + off - size);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020075 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000076 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020077 off <<= 1;
78 }
79
80 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020081 header = (struct nvram_header *)(iobase + 4096);
Rafał Miłecki341097f2014-10-30 12:50:03 +010082 if (header->magic == NVRAM_MAGIC) {
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000083 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020084 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000085 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020086
Rafał Miłecki8d602dd2014-09-03 22:51:06 +020087 header = (struct nvram_header *)(iobase + 1024);
Rafał Miłecki341097f2014-10-30 12:50:03 +010088 if (header->magic == NVRAM_MAGIC) {
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000089 size = NVRAM_SPACE;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020090 goto found;
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000091 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020092
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000093 pr_err("no nvram found\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000094 return -ENXIO;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020095
96found:
Hauke Mehrtensf36738d2012-12-26 19:51:13 +000097 if (header->len > size)
98 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
99 if (header->len > NVRAM_SPACE)
100 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
Rafał Miłeckid28c9a52015-05-12 18:46:11 +0200101 header->len, NVRAM_SPACE - 1);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +0000102
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100103 src = (u32 *)header;
104 dst = (u32 *)nvram_buf;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200105 for (i = 0; i < sizeof(struct nvram_header); i += 4)
Rafał Miłecki23d2bc42014-12-10 11:49:53 +0100106 *dst++ = __raw_readl(src++);
Hauke Mehrtensf36738d2012-12-26 19:51:13 +0000107 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
Rafał Miłecki23d2bc42014-12-10 11:49:53 +0100108 *dst++ = readl(src++);
Rafał Miłeckid28c9a52015-05-12 18:46:11 +0200109 nvram_buf[NVRAM_SPACE - 1] = '\0';
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000110
111 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200112}
113
Rafał Miłecki21400f22014-09-03 22:59:45 +0200114/*
115 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
116 * subsystem to access flash. We can't even use platform device / driver to
117 * store memory offset.
118 * To handle this we provide following symbol. It's supposed to be called as
119 * soon as we get info about flash device, before any NVRAM entry is needed.
120 */
121int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
Rafał Miłecki8d602dd2014-09-03 22:51:06 +0200122{
123 void __iomem *iobase;
124 int err;
125
126 iobase = ioremap_nocache(base, lim);
127 if (!iobase)
128 return -ENOMEM;
129
130 err = nvram_find_and_copy(iobase, lim);
131
132 iounmap(iobase);
133
134 return err;
135}
136
Hauke Mehrtense58da162012-12-26 19:51:12 +0000137static int nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000138{
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100139#ifdef CONFIG_MTD
140 struct mtd_info *mtd;
141 struct nvram_header header;
142 size_t bytes_read;
Rafał Miłecki40d12172015-04-01 08:23:05 +0200143 int err;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100144
145 mtd = get_mtd_device_nm("nvram");
146 if (IS_ERR(mtd))
147 return -ENODEV;
148
Rafał Miłecki40d12172015-04-01 08:23:05 +0200149 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
150 if (!err && header.magic == NVRAM_MAGIC) {
151 u8 *dst = (uint8_t *)nvram_buf;
152 size_t len = header.len;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100153
Rafał Miłeckid28c9a52015-05-12 18:46:11 +0200154 if (len >= NVRAM_SPACE) {
155 len = NVRAM_SPACE - 1;
Rafał Miłecki40d12172015-04-01 08:23:05 +0200156 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
Rafał Miłeckid28c9a52015-05-12 18:46:11 +0200157 header.len, len);
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100158 }
Rafał Miłecki40d12172015-04-01 08:23:05 +0200159
160 err = mtd_read(mtd, 0, len, &bytes_read, dst);
161 if (err)
162 return err;
163
164 return 0;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100165 }
166#endif
Rafał Miłecki7177efc2014-10-28 13:30:23 +0100167
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000168 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000169}
170
Rafał Miłecki341097f2014-10-30 12:50:03 +0100171int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200172{
173 char *var, *value, *end, *eq;
Rafał Miłecki5bfad4e2015-05-12 18:46:12 +0200174 int err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200175
176 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000177 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200178
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000179 if (!nvram_buf[0]) {
Hauke Mehrtense58da162012-12-26 19:51:12 +0000180 err = nvram_init();
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000181 if (err)
182 return err;
183 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200184
185 /* Look for name=value and return value */
186 var = &nvram_buf[sizeof(struct nvram_header)];
Rafał Miłecki5bfad4e2015-05-12 18:46:12 +0200187 end = nvram_buf + sizeof(nvram_buf);
188 while (var < end && *var) {
189 eq = strchr(var, '=');
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200190 if (!eq)
191 break;
192 value = eq + 1;
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100193 if (eq - var == strlen(name) &&
194 strncmp(var, name, eq - var) == 0)
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100195 return snprintf(val, val_len, "%s", value);
Rafał Miłecki5bfad4e2015-05-12 18:46:12 +0200196 var = value + strlen(value) + 1;
Rafał Miłecki50c97912015-04-01 08:23:03 +0200197 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000198 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200199}
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000200EXPORT_SYMBOL(bcm47xx_nvram_getenv);
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200201
202int bcm47xx_nvram_gpio_pin(const char *name)
203{
204 int i, err;
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100205 char nvram_var[] = "gpioXX";
206 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200207
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100208 /* TODO: Optimize it to don't call getenv so many times */
209 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200210 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
211 if (err <= 0)
212 continue;
213 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
214 if (err <= 0)
215 continue;
216 if (!strcmp(name, buf))
217 return i;
218 }
219 return -ENOENT;
220}
221EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);