blob: 42e527121314947ae2d03dc2996f96a94ee8fa78 [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
Hauke Mehrtensc4485672012-12-26 19:51:11 +000033 /* TODO: when nvram is on nand flash check for bad blocks first. */
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020034 off = FLASH_MIN;
35 while (off <= lim) {
36 /* Windowed flash access */
37 header = (struct nvram_header *)
38 KSEG1ADDR(base + off - NVRAM_SPACE);
39 if (header->magic == NVRAM_HEADER)
40 goto found;
41 off <<= 1;
42 }
43
44 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
45 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
46 if (header->magic == NVRAM_HEADER)
47 goto found;
48
49 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
50 if (header->magic == NVRAM_HEADER)
51 goto found;
52
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000053 return -ENXIO;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020054
55found:
56 src = (u32 *) header;
57 dst = (u32 *) nvram_buf;
58 for (i = 0; i < sizeof(struct nvram_header); i += 4)
59 *dst++ = *src++;
60 for (; i < header->len && i < NVRAM_SPACE; i += 4)
61 *dst++ = le32_to_cpu(*src++);
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000062
63 return 0;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +020064}
65
Rafał Miłeckibb765632012-12-26 08:29:17 +000066#ifdef CONFIG_BCM47XX_SSB
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000067static int nvram_init_ssb(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +000068{
69 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
70 u32 base;
71 u32 lim;
72
73 if (mcore->pflash.present) {
74 base = mcore->pflash.window;
75 lim = mcore->pflash.window_size;
76 } else {
77 pr_err("Couldn't find supported flash memory\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000078 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +000079 }
80
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000081 return nvram_find_and_copy(base, lim);
Rafał Miłeckibb765632012-12-26 08:29:17 +000082}
83#endif
84
85#ifdef CONFIG_BCM47XX_BCMA
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +000086static int nvram_init_bcma(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +000087{
88 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
89 u32 base;
90 u32 lim;
91
Hauke Mehrtensc4485672012-12-26 19:51:11 +000092#ifdef CONFIG_BCMA_NFLASH
93 if (cc->nflash.boot) {
94 base = BCMA_SOC_FLASH1;
95 lim = BCMA_SOC_FLASH1_SZ;
96 } else
97#endif
Rafał Miłeckibb765632012-12-26 08:29:17 +000098 if (cc->pflash.present) {
99 base = cc->pflash.window;
100 lim = cc->pflash.window_size;
101#ifdef CONFIG_BCMA_SFLASH
102 } else if (cc->sflash.present) {
103 base = cc->sflash.window;
104 lim = cc->sflash.size;
105#endif
106 } else {
107 pr_err("Couldn't find supported flash memory\n");
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000108 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000109 }
110
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000111 return nvram_find_and_copy(base, lim);
Rafał Miłeckibb765632012-12-26 08:29:17 +0000112}
113#endif
114
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000115static int early_nvram_init(void)
Rafał Miłeckibb765632012-12-26 08:29:17 +0000116{
117 switch (bcm47xx_bus_type) {
118#ifdef CONFIG_BCM47XX_SSB
119 case BCM47XX_BUS_TYPE_SSB:
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000120 return nvram_init_ssb();
Rafał Miłeckibb765632012-12-26 08:29:17 +0000121#endif
122#ifdef CONFIG_BCM47XX_BCMA
123 case BCM47XX_BUS_TYPE_BCMA:
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000124 return nvram_init_bcma();
Rafał Miłeckibb765632012-12-26 08:29:17 +0000125#endif
126 }
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000127 return -ENXIO;
Rafał Miłeckibb765632012-12-26 08:29:17 +0000128}
129
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200130int nvram_getenv(char *name, char *val, size_t val_len)
131{
132 char *var, *value, *end, *eq;
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000133 int err;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200134
135 if (!name)
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000136 return -EINVAL;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200137
Hauke Mehrtenscc4403e2012-12-26 19:51:10 +0000138 if (!nvram_buf[0]) {
139 err = early_nvram_init();
140 if (err)
141 return err;
142 }
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200143
144 /* Look for name=value and return value */
145 var = &nvram_buf[sizeof(struct nvram_header)];
146 end = nvram_buf + sizeof(nvram_buf) - 2;
147 end[0] = end[1] = '\0';
148 for (; *var; var = value + strlen(value) + 1) {
149 eq = strchr(var, '=');
150 if (!eq)
151 break;
152 value = eq + 1;
153 if ((eq - var) == strlen(name) &&
154 strncmp(var, name, (eq - var)) == 0) {
Hauke Mehrtens44d4b2a2012-02-28 00:56:11 +0100155 return snprintf(val, val_len, "%s", value);
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200156 }
157 }
Hauke Mehrtensee7e2f32012-12-26 19:51:09 +0000158 return -ENOENT;
Waldemar Brodkorb121915c2010-06-08 19:06:01 +0200159}
160EXPORT_SYMBOL(nvram_getenv);