258 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * BCM947xx nvram variable access4 *5 * Copyright (C) 2005 Broadcom Corporation6 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>7 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>8 */9 10#include <linux/io.h>11#include <linux/types.h>12#include <linux/module.h>13#include <linux/kernel.h>14#include <linux/string.h>15#include <linux/mtd/mtd.h>16#include <linux/bcm47xx_nvram.h>17 18#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */19#define NVRAM_SPACE 0x1000020#define NVRAM_MAX_GPIO_ENTRIES 3221#define NVRAM_MAX_GPIO_VALUE_LEN 3022 23#define FLASH_MIN 0x00020000 /* Minimum flash size */24 25struct nvram_header {26 u32 magic;27 u32 len;28 u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */29 u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */30 u32 config_ncdl; /* ncdl values for memc */31};32 33static char nvram_buf[NVRAM_SPACE];34static size_t nvram_len;35static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};36 37/**38 * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory39 */40static bool bcm47xx_nvram_is_valid(void __iomem *nvram)41{42 return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;43}44 45/**46 * bcm47xx_nvram_copy - copy NVRAM to internal buffer47 */48static void bcm47xx_nvram_copy(void __iomem *nvram_start, size_t res_size)49{50 struct nvram_header __iomem *header = nvram_start;51 size_t copy_size;52 53 copy_size = header->len;54 if (copy_size > res_size) {55 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");56 copy_size = res_size;57 }58 if (copy_size >= NVRAM_SPACE) {59 pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",60 copy_size, NVRAM_SPACE - 1);61 copy_size = NVRAM_SPACE - 1;62 }63 64 __ioread32_copy(nvram_buf, nvram_start, DIV_ROUND_UP(copy_size, 4));65 nvram_buf[NVRAM_SPACE - 1] = '\0';66 nvram_len = copy_size;67}68 69/**70 * bcm47xx_nvram_find_and_copy - find NVRAM on flash mapping & copy it71 */72static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_size)73{74 size_t flash_size;75 size_t offset;76 int i;77 78 if (nvram_len) {79 pr_warn("nvram already initialized\n");80 return -EEXIST;81 }82 83 /* TODO: when nvram is on nand flash check for bad blocks first. */84 85 /* Try every possible flash size and check for NVRAM at its end */86 for (flash_size = FLASH_MIN; flash_size <= res_size; flash_size <<= 1) {87 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {88 offset = flash_size - nvram_sizes[i];89 if (bcm47xx_nvram_is_valid(flash_start + offset))90 goto found;91 }92 }93 94 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */95 96 offset = 4096;97 if (bcm47xx_nvram_is_valid(flash_start + offset))98 goto found;99 100 offset = 1024;101 if (bcm47xx_nvram_is_valid(flash_start + offset))102 goto found;103 104 pr_err("no nvram found\n");105 return -ENXIO;106 107found:108 bcm47xx_nvram_copy(flash_start + offset, res_size - offset);109 110 return 0;111}112 113int bcm47xx_nvram_init_from_iomem(void __iomem *nvram_start, size_t res_size)114{115 if (nvram_len) {116 pr_warn("nvram already initialized\n");117 return -EEXIST;118 }119 120 if (!bcm47xx_nvram_is_valid(nvram_start)) {121 pr_err("No valid NVRAM found\n");122 return -ENOENT;123 }124 125 bcm47xx_nvram_copy(nvram_start, res_size);126 127 return 0;128}129EXPORT_SYMBOL_GPL(bcm47xx_nvram_init_from_iomem);130 131/*132 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd133 * subsystem to access flash. We can't even use platform device / driver to134 * store memory offset.135 * To handle this we provide following symbol. It's supposed to be called as136 * soon as we get info about flash device, before any NVRAM entry is needed.137 */138int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)139{140 void __iomem *iobase;141 int err;142 143 iobase = ioremap(base, lim);144 if (!iobase)145 return -ENOMEM;146 147 err = bcm47xx_nvram_find_and_copy(iobase, lim);148 149 iounmap(iobase);150 151 return err;152}153 154static int nvram_init(void)155{156#ifdef CONFIG_MTD157 struct mtd_info *mtd;158 struct nvram_header header;159 size_t bytes_read;160 int err;161 162 mtd = get_mtd_device_nm("nvram");163 if (IS_ERR(mtd))164 return -ENODEV;165 166 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);167 if (!err && header.magic == NVRAM_MAGIC &&168 header.len > sizeof(header)) {169 nvram_len = header.len;170 if (nvram_len >= NVRAM_SPACE) {171 pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",172 nvram_len, NVRAM_SPACE);173 nvram_len = NVRAM_SPACE - 1;174 }175 176 err = mtd_read(mtd, 0, nvram_len, &nvram_len,177 (u8 *)nvram_buf);178 return err;179 }180#endif181 182 return -ENXIO;183}184 185int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)186{187 char *var, *value, *end, *eq;188 int err;189 190 if (!name)191 return -EINVAL;192 193 if (!nvram_len) {194 err = nvram_init();195 if (err)196 return err;197 }198 199 /* Look for name=value and return value */200 var = &nvram_buf[sizeof(struct nvram_header)];201 end = nvram_buf + sizeof(nvram_buf);202 while (var < end && *var) {203 eq = strchr(var, '=');204 if (!eq)205 break;206 value = eq + 1;207 if (eq - var == strlen(name) &&208 strncmp(var, name, eq - var) == 0)209 return snprintf(val, val_len, "%s", value);210 var = value + strlen(value) + 1;211 }212 return -ENOENT;213}214EXPORT_SYMBOL(bcm47xx_nvram_getenv);215 216int bcm47xx_nvram_gpio_pin(const char *name)217{218 int i, err;219 char nvram_var[] = "gpioXX";220 char buf[NVRAM_MAX_GPIO_VALUE_LEN];221 222 /* TODO: Optimize it to don't call getenv so many times */223 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {224 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);225 if (err <= 0)226 continue;227 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));228 if (err <= 0)229 continue;230 if (!strcmp(name, buf))231 return i;232 }233 return -ENOENT;234}235EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);236 237char *bcm47xx_nvram_get_contents(size_t *nvram_size)238{239 int err;240 char *nvram;241 242 if (!nvram_len) {243 err = nvram_init();244 if (err)245 return NULL;246 }247 248 *nvram_size = nvram_len - sizeof(struct nvram_header);249 nvram = vmalloc(*nvram_size);250 if (!nvram)251 return NULL;252 memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);253 254 return nvram;255}256EXPORT_SYMBOL(bcm47xx_nvram_get_contents);257 258