MIPS: BCM47XX: add bcm47xx prefix in front of nvram function names
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
f36738dd 6 * Copyright (C) 2010-2012 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>
111bd981 21#include <bcm47xx_nvram.h>
121915c4
WB
22#include <asm/mach-bcm47xx/bcm47xx.h>
23
24static char nvram_buf[NVRAM_SPACE];
25
f36738dd
HM
26static u32 find_nvram_size(u32 end)
27{
28 struct nvram_header *header;
29 u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
30 int i;
31
32 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
33 header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
34 if (header->magic == NVRAM_HEADER)
35 return nvram_sizes[i];
36 }
37
38 return 0;
39}
40
41/* Probe for NVRAM header */
cc4403e0 42static int nvram_find_and_copy(u32 base, u32 lim)
121915c4 43{
121915c4
WB
44 struct nvram_header *header;
45 int i;
08ccf572 46 u32 off;
121915c4 47 u32 *src, *dst;
f36738dd 48 u32 size;
121915c4 49
c4485671 50 /* TODO: when nvram is on nand flash check for bad blocks first. */
121915c4
WB
51 off = FLASH_MIN;
52 while (off <= lim) {
53 /* Windowed flash access */
f36738dd
HM
54 size = find_nvram_size(base + off);
55 if (size) {
56 header = (struct nvram_header *)KSEG1ADDR(base + off -
57 size);
121915c4 58 goto found;
f36738dd 59 }
121915c4
WB
60 off <<= 1;
61 }
62
63 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
64 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
f36738dd
HM
65 if (header->magic == NVRAM_HEADER) {
66 size = NVRAM_SPACE;
121915c4 67 goto found;
f36738dd 68 }
121915c4
WB
69
70 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
f36738dd
HM
71 if (header->magic == NVRAM_HEADER) {
72 size = NVRAM_SPACE;
121915c4 73 goto found;
f36738dd 74 }
121915c4 75
f36738dd 76 pr_err("no nvram found\n");
cc4403e0 77 return -ENXIO;
121915c4
WB
78
79found:
f36738dd
HM
80
81 if (header->len > size)
82 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
83 if (header->len > NVRAM_SPACE)
84 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
85 header->len, NVRAM_SPACE);
86
121915c4
WB
87 src = (u32 *) header;
88 dst = (u32 *) nvram_buf;
89 for (i = 0; i < sizeof(struct nvram_header); i += 4)
90 *dst++ = *src++;
f36738dd 91 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
121915c4 92 *dst++ = le32_to_cpu(*src++);
f36738dd 93 memset(dst, 0x0, NVRAM_SPACE - i);
cc4403e0
HM
94
95 return 0;
121915c4
WB
96}
97
bb765632 98#ifdef CONFIG_BCM47XX_SSB
cc4403e0 99static int nvram_init_ssb(void)
bb765632
RM
100{
101 struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
102 u32 base;
103 u32 lim;
104
105 if (mcore->pflash.present) {
106 base = mcore->pflash.window;
107 lim = mcore->pflash.window_size;
108 } else {
109 pr_err("Couldn't find supported flash memory\n");
cc4403e0 110 return -ENXIO;
bb765632
RM
111 }
112
cc4403e0 113 return nvram_find_and_copy(base, lim);
bb765632
RM
114}
115#endif
116
117#ifdef CONFIG_BCM47XX_BCMA
cc4403e0 118static int nvram_init_bcma(void)
bb765632
RM
119{
120 struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
121 u32 base;
122 u32 lim;
123
c4485671
HM
124#ifdef CONFIG_BCMA_NFLASH
125 if (cc->nflash.boot) {
126 base = BCMA_SOC_FLASH1;
127 lim = BCMA_SOC_FLASH1_SZ;
128 } else
129#endif
bb765632
RM
130 if (cc->pflash.present) {
131 base = cc->pflash.window;
132 lim = cc->pflash.window_size;
133#ifdef CONFIG_BCMA_SFLASH
134 } else if (cc->sflash.present) {
135 base = cc->sflash.window;
136 lim = cc->sflash.size;
137#endif
138 } else {
139 pr_err("Couldn't find supported flash memory\n");
cc4403e0 140 return -ENXIO;
bb765632
RM
141 }
142
cc4403e0 143 return nvram_find_and_copy(base, lim);
bb765632
RM
144}
145#endif
146
e58da16f 147static int nvram_init(void)
bb765632
RM
148{
149 switch (bcm47xx_bus_type) {
150#ifdef CONFIG_BCM47XX_SSB
151 case BCM47XX_BUS_TYPE_SSB:
cc4403e0 152 return nvram_init_ssb();
bb765632
RM
153#endif
154#ifdef CONFIG_BCM47XX_BCMA
155 case BCM47XX_BUS_TYPE_BCMA:
cc4403e0 156 return nvram_init_bcma();
bb765632
RM
157#endif
158 }
cc4403e0 159 return -ENXIO;
bb765632
RM
160}
161
111bd981 162int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
121915c4
WB
163{
164 char *var, *value, *end, *eq;
cc4403e0 165 int err;
121915c4
WB
166
167 if (!name)
ee7e2f3c 168 return -EINVAL;
121915c4 169
cc4403e0 170 if (!nvram_buf[0]) {
e58da16f 171 err = nvram_init();
cc4403e0
HM
172 if (err)
173 return err;
174 }
121915c4
WB
175
176 /* Look for name=value and return value */
177 var = &nvram_buf[sizeof(struct nvram_header)];
178 end = nvram_buf + sizeof(nvram_buf) - 2;
179 end[0] = end[1] = '\0';
180 for (; *var; var = value + strlen(value) + 1) {
181 eq = strchr(var, '=');
182 if (!eq)
183 break;
184 value = eq + 1;
185 if ((eq - var) == strlen(name) &&
186 strncmp(var, name, (eq - var)) == 0) {
44d4b2ae 187 return snprintf(val, val_len, "%s", value);
121915c4
WB
188 }
189 }
ee7e2f3c 190 return -ENOENT;
121915c4 191}
111bd981 192EXPORT_SYMBOL(bcm47xx_nvram_getenv);