blob: 80e352e0c9950554d5dac432cf1f286658a61076 [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
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000026static int 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
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000052 return -ENXIO;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020053
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++);
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000061
62 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020063}
64
Rafał Miłeckibb765632012-12-26 08:29:17 +000065#ifdef CONFIG_BCM47XX_SSB
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000066static int nvram_init_ssb(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +000067{
68 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
69 u32 base;
70 u32 lim;
71
72 if (mcore->pflash.present) {
73 base = mcore->pflash.window;
74 lim = mcore->pflash.window_size;
75 } else {
76 pr_err("Couldn't find supported flash memory\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000077 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +000078 }
79
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000080 return nvram_find_and_copy(base, lim);
Rafał Miłeckibb765632012-12-26 08:29:17 +000081}
82#endif
83
84#ifdef CONFIG_BCM47XX_BCMA
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000085static int nvram_init_bcma(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +000086{
87 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
88 u32 base;
89 u32 lim;
90
91 if (cc->pflash.present) {
92 base = cc->pflash.window;
93 lim = cc->pflash.window_size;
94#ifdef CONFIG_BCMA_SFLASH
95 } else if (cc->sflash.present) {
96 base = cc->sflash.window;
97 lim = cc->sflash.size;
98#endif
99 } else {
100 pr_err("Couldn't find supported flash memory\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000101 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000102 }
103
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000104 return nvram_find_and_copy(base, lim);
Rafał Miłeckibb765632012-12-26 08:29:17 +0000105}
106#endif
107
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000108static int early_nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000109{
110 switch (bcm47xx_bus_type) {
111#ifdef CONFIG_BCM47XX_SSB
112 case BCM47XX_BUS_TYPE_SSB:
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000113 return nvram_init_ssb();
Rafał Miłeckibb765632012-12-26 08:29:17 +0000114#endif
115#ifdef CONFIG_BCM47XX_BCMA
116 case BCM47XX_BUS_TYPE_BCMA:
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000117 return nvram_init_bcma();
Rafał Miłeckibb765632012-12-26 08:29:17 +0000118#endif
119 }
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000120 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000121}
122
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200123int nvram_getenv(char *name, char *val, size_t val_len)
124{
125 char *var, *value, *end, *eq;
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000126 int err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200127
128 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000129 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200130
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000131 if (!nvram_buf[0]) {
132 err = early_nvram_init();
133 if (err)
134 return err;
135 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200136
137 /* Look for name=value and return value */
138 var = &nvram_buf[sizeof(struct nvram_header)];
139 end = nvram_buf + sizeof(nvram_buf) - 2;
140 end[0] = end[1] = '\0';
141 for (; *var; var = value + strlen(value) + 1) {
142 eq = strchr(var, '=');
143 if (!eq)
144 break;
145 value = eq + 1;
146 if ((eq - var) == strlen(name) &&
147 strncmp(var, name, (eq - var)) == 0) {
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100148 return snprintf(val, val_len, "%s", value);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200149 }
150 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000151 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200152}
153EXPORT_SYMBOL(nvram_getenv);