blob: 4e994edb142501a89821fdc5ea28144d30ea3722 [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 Mehrtensfe6f3642011-05-10 23:31:32 +02006 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
Waldemar Brodkorb121915c2010-06-08 19:06:01 +02007 *
8 * 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
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/module.h>
17#include <linux/ssb/ssb.h>
18#include <linux/kernel.h>
19#include <linux/string.h>
20#include <asm/addrspace.h>
21#include <asm/mach-bcm47xx/nvram.h>
22#include <asm/mach-bcm47xx/bcm47xx.h>
23
24static char nvram_buf[NVRAM_SPACE];
25
26/* Probe for NVRAM header */
Hauke Mehrtensfe6f3642011-05-10 23:31:32 +020027static void early_nvram_init(void)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020028{
Hauke Mehrtensa656ffc2011-07-23 01:20:13 +020029#ifdef CONFIG_BCM47XX_SSB
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020030 struct ssb_mipscore *mcore_ssb;
Hauke Mehrtensa656ffc2011-07-23 01:20:13 +020031#endif
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020032 struct nvram_header *header;
33 int i;
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020034 u32 base = 0;
35 u32 lim = 0;
36 u32 off;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020037 u32 *src, *dst;
38
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020039 switch (bcm47xx_bus_type) {
Hauke Mehrtensa656ffc2011-07-23 01:20:13 +020040#ifdef CONFIG_BCM47XX_SSB
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020041 case BCM47XX_BUS_TYPE_SSB:
42 mcore_ssb = &bcm47xx_bus.ssb.mipscore;
43 base = mcore_ssb->flash_window;
44 lim = mcore_ssb->flash_window_size;
45 break;
Hauke Mehrtensa656ffc2011-07-23 01:20:13 +020046#endif
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020047 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020048
49 off = FLASH_MIN;
50 while (off <= lim) {
51 /* Windowed flash access */
52 header = (struct nvram_header *)
53 KSEG1ADDR(base + off - NVRAM_SPACE);
54 if (header->magic == NVRAM_HEADER)
55 goto found;
56 off <<= 1;
57 }
58
59 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
60 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
61 if (header->magic == NVRAM_HEADER)
62 goto found;
63
64 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
65 if (header->magic == NVRAM_HEADER)
66 goto found;
67
68 return;
69
70found:
71 src = (u32 *) header;
72 dst = (u32 *) nvram_buf;
73 for (i = 0; i < sizeof(struct nvram_header); i += 4)
74 *dst++ = *src++;
75 for (; i < header->len && i < NVRAM_SPACE; i += 4)
76 *dst++ = le32_to_cpu(*src++);
77}
78
79int nvram_getenv(char *name, char *val, size_t val_len)
80{
81 char *var, *value, *end, *eq;
82
83 if (!name)
Hauke Mehrtens47a34862010-08-02 23:56:22 +020084 return NVRAM_ERR_INV_PARAM;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020085
86 if (!nvram_buf[0])
87 early_nvram_init();
88
89 /* Look for name=value and return value */
90 var = &nvram_buf[sizeof(struct nvram_header)];
91 end = nvram_buf + sizeof(nvram_buf) - 2;
92 end[0] = end[1] = '\0';
93 for (; *var; var = value + strlen(value) + 1) {
94 eq = strchr(var, '=');
95 if (!eq)
96 break;
97 value = eq + 1;
98 if ((eq - var) == strlen(name) &&
99 strncmp(var, name, (eq - var)) == 0) {
100 snprintf(val, val_len, "%s", value);
101 return 0;
102 }
103 }
Hauke Mehrtens47a34862010-08-02 23:56:22 +0200104 return NVRAM_ERR_ENVNOTFOUND;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200105}
106EXPORT_SYMBOL(nvram_getenv);