]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/mips/bcm47xx/nvram.c
4e994edb142501a89821fdc5ea28144d30ea3722
[mirror_ubuntu-bionic-kernel.git] / arch / mips / bcm47xx / nvram.c
1 /*
2 * BCM947xx nvram variable access
3 *
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
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
24 static char nvram_buf[NVRAM_SPACE];
25
26 /* Probe for NVRAM header */
27 static void early_nvram_init(void)
28 {
29 #ifdef CONFIG_BCM47XX_SSB
30 struct ssb_mipscore *mcore_ssb;
31 #endif
32 struct nvram_header *header;
33 int i;
34 u32 base = 0;
35 u32 lim = 0;
36 u32 off;
37 u32 *src, *dst;
38
39 switch (bcm47xx_bus_type) {
40 #ifdef CONFIG_BCM47XX_SSB
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;
46 #endif
47 }
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
70 found:
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
79 int nvram_getenv(char *name, char *val, size_t val_len)
80 {
81 char *var, *value, *end, *eq;
82
83 if (!name)
84 return NVRAM_ERR_INV_PARAM;
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 }
104 return NVRAM_ERR_ENVNOTFOUND;
105 }
106 EXPORT_SYMBOL(nvram_getenv);