blob: ba632ff08a13c58838d734a4f5aadce860dffe3c [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",
101 header->len, NVRAM_SPACE);
102
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++);
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000109
110 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200111}
112
Rafał Miłecki21400f22014-09-03 22:59:45 +0200113/*
114 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
115 * subsystem to access flash. We can't even use platform device / driver to
116 * store memory offset.
117 * To handle this we provide following symbol. It's supposed to be called as
118 * soon as we get info about flash device, before any NVRAM entry is needed.
119 */
120int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
Rafał Miłecki8d602dd2014-09-03 22:51:06 +0200121{
122 void __iomem *iobase;
123 int err;
124
125 iobase = ioremap_nocache(base, lim);
126 if (!iobase)
127 return -ENOMEM;
128
129 err = nvram_find_and_copy(iobase, lim);
130
131 iounmap(iobase);
132
133 return err;
134}
135
Hauke Mehrtense58da162012-12-26 19:51:12 +0000136static int nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000137{
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100138#ifdef CONFIG_MTD
139 struct mtd_info *mtd;
140 struct nvram_header header;
141 size_t bytes_read;
Rafał Miłecki40d12172015-04-01 08:23:05 +0200142 int err;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100143
144 mtd = get_mtd_device_nm("nvram");
145 if (IS_ERR(mtd))
146 return -ENODEV;
147
Rafał Miłecki40d12172015-04-01 08:23:05 +0200148 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
149 if (!err && header.magic == NVRAM_MAGIC) {
150 u8 *dst = (uint8_t *)nvram_buf;
151 size_t len = header.len;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100152
Rafał Miłecki40d12172015-04-01 08:23:05 +0200153 if (header.len > NVRAM_SPACE) {
154 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
155 header.len, NVRAM_SPACE);
156 len = NVRAM_SPACE;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100157 }
Rafał Miłecki40d12172015-04-01 08:23:05 +0200158
159 err = mtd_read(mtd, 0, len, &bytes_read, dst);
160 if (err)
161 return err;
162
163 return 0;
Rafał Miłecki9d1d0862014-10-29 10:05:06 +0100164 }
165#endif
Rafał Miłecki7177efc2014-10-28 13:30:23 +0100166
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000167 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000168}
169
Rafał Miłecki341097f2014-10-30 12:50:03 +0100170int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200171{
172 char *var, *value, *end, *eq;
Rafał Miłecki80aaaa82014-12-10 11:49:54 +0100173 int data_left, err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200174
175 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000176 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200177
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000178 if (!nvram_buf[0]) {
Hauke Mehrtense58da162012-12-26 19:51:12 +0000179 err = nvram_init();
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000180 if (err)
181 return err;
182 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200183
184 /* Look for name=value and return value */
185 var = &nvram_buf[sizeof(struct nvram_header)];
186 end = nvram_buf + sizeof(nvram_buf) - 2;
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100187 end[0] = '\0';
188 end[1] = '\0';
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200189 for (; *var; var = value + strlen(value) + 1) {
Rafał Miłecki80aaaa82014-12-10 11:49:54 +0100190 data_left = end - var;
191
192 eq = strnchr(var, data_left, '=');
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200193 if (!eq)
194 break;
195 value = eq + 1;
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100196 if (eq - var == strlen(name) &&
197 strncmp(var, name, eq - var) == 0)
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100198 return snprintf(val, val_len, "%s", value);
Rafał Miłecki50c97912015-04-01 08:23:03 +0200199 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000200 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200201}
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000202EXPORT_SYMBOL(bcm47xx_nvram_getenv);
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200203
204int bcm47xx_nvram_gpio_pin(const char *name)
205{
206 int i, err;
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100207 char nvram_var[] = "gpioXX";
208 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200209
Rafał Miłeckid548ca62014-12-10 17:38:26 +0100210 /* TODO: Optimize it to don't call getenv so many times */
211 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
Hauke Mehrtens62cf3bc2013-09-18 13:31:15 +0200212 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
213 if (err <= 0)
214 continue;
215 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
216 if (err <= 0)
217 continue;
218 if (!strcmp(name, buf))
219 return i;
220 }
221 return -ENOENT;
222}
223EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);