blob: 64613678ce84973779c82120010b868b4a7f1cfe [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
Rafał Miłeckibb765632012-12-26 08:29:17 +000026static void nvram_find_and_copy(u32 base, u32 lim)
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020027{
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020028 struct nvram_header *header;
29 int i;
Hauke Mehrtens08ccf5722011-07-23 01:20:12 +020030 u32 off;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020031 u32 *src, *dst;
32
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020033 off = FLASH_MIN;
34 while (off <= lim) {
35 /* Windowed flash access */
36 header = (struct nvram_header *)
37 KSEG1ADDR(base + off - NVRAM_SPACE);
38 if (header->magic == NVRAM_HEADER)
39 goto found;
40 off <<= 1;
41 }
42
43 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
44 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
45 if (header->magic == NVRAM_HEADER)
46 goto found;
47
48 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
49 if (header->magic == NVRAM_HEADER)
50 goto found;
51
52 return;
53
54found:
55 src = (u32 *) header;
56 dst = (u32 *) nvram_buf;
57 for (i = 0; i < sizeof(struct nvram_header); i += 4)
58 *dst++ = *src++;
59 for (; i < header->len && i < NVRAM_SPACE; i += 4)
60 *dst++ = le32_to_cpu(*src++);
61}
62
Rafał Miłeckibb765632012-12-26 08:29:17 +000063#ifdef CONFIG_BCM47XX_SSB
64static void nvram_init_ssb(void)
65{
66 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
67 u32 base;
68 u32 lim;
69
70 if (mcore->pflash.present) {
71 base = mcore->pflash.window;
72 lim = mcore->pflash.window_size;
73 } else {
74 pr_err("Couldn't find supported flash memory\n");
75 return;
76 }
77
78 nvram_find_and_copy(base, lim);
79}
80#endif
81
82#ifdef CONFIG_BCM47XX_BCMA
83static void nvram_init_bcma(void)
84{
85 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
86 u32 base;
87 u32 lim;
88
89 if (cc->pflash.present) {
90 base = cc->pflash.window;
91 lim = cc->pflash.window_size;
92#ifdef CONFIG_BCMA_SFLASH
93 } else if (cc->sflash.present) {
94 base = cc->sflash.window;
95 lim = cc->sflash.size;
96#endif
97 } else {
98 pr_err("Couldn't find supported flash memory\n");
99 return;
100 }
101
102 nvram_find_and_copy(base, lim);
103}
104#endif
105
106static void early_nvram_init(void)
107{
108 switch (bcm47xx_bus_type) {
109#ifdef CONFIG_BCM47XX_SSB
110 case BCM47XX_BUS_TYPE_SSB:
111 nvram_init_ssb();
112 break;
113#endif
114#ifdef CONFIG_BCM47XX_BCMA
115 case BCM47XX_BUS_TYPE_BCMA:
116 nvram_init_bcma();
117 break;
118#endif
119 }
120}
121
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200122int nvram_getenv(char *name, char *val, size_t val_len)
123{
124 char *var, *value, *end, *eq;
125
126 if (!name)
Hauke Mehrtens47a34862010-08-02 23:56:22 +0200127 return NVRAM_ERR_INV_PARAM;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200128
129 if (!nvram_buf[0])
130 early_nvram_init();
131
132 /* Look for name=value and return value */
133 var = &nvram_buf[sizeof(struct nvram_header)];
134 end = nvram_buf + sizeof(nvram_buf) - 2;
135 end[0] = end[1] = '\0';
136 for (; *var; var = value + strlen(value) + 1) {
137 eq = strchr(var, '=');
138 if (!eq)
139 break;
140 value = eq + 1;
141 if ((eq - var) == strlen(name) &&
142 strncmp(var, name, (eq - var)) == 0) {
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100143 return snprintf(val, val_len, "%s", value);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200144 }
145 }
Hauke Mehrtens47a34862010-08-02 23:56:22 +0200146 return NVRAM_ERR_ENVNOTFOUND;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200147}
148EXPORT_SYMBOL(nvram_getenv);