bcm47xx: make it possible to build bcm47xx without ssb.
[deliverable/linux.git] / arch / mips / bcm47xx / nvram.c
CommitLineData
121915c4
WB
1/*
2 * BCM947xx nvram variable access
3 *
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
fe6f3642 6 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
121915c4
WB
7 *
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 */
fe6f3642 27static void early_nvram_init(void)
121915c4 28{
a656ffcb 29#ifdef CONFIG_BCM47XX_SSB
08ccf572 30 struct ssb_mipscore *mcore_ssb;
a656ffcb 31#endif
121915c4
WB
32 struct nvram_header *header;
33 int i;
08ccf572
HM
34 u32 base = 0;
35 u32 lim = 0;
36 u32 off;
121915c4
WB
37 u32 *src, *dst;
38
08ccf572 39 switch (bcm47xx_bus_type) {
a656ffcb 40#ifdef CONFIG_BCM47XX_SSB
08ccf572
HM
41 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;
a656ffcb 46#endif
08ccf572 47 }
121915c4
WB
48
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)
47a34861 84 return NVRAM_ERR_INV_PARAM;
121915c4
WB
85
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 }
47a34861 104 return NVRAM_ERR_ENVNOTFOUND;
121915c4
WB
105}
106EXPORT_SYMBOL(nvram_getenv);
This page took 0.094106 seconds and 5 git commands to generate.