brcm47xx: fix cpu clock detection on ASUS WL-520gU
[openwrt.git] / target / linux / brcm47xx / patches-3.6 / 080-MIPS-BCM47XX-rewrite-nvram-probing.patch
1 --- a/arch/mips/bcm47xx/nvram.c
2 +++ b/arch/mips/bcm47xx/nvram.c
3 @@ -3,7 +3,7 @@
4   *
5   * Copyright (C) 2005 Broadcom Corporation
6   * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
7 - * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de>
8 + * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
9   *
10   * This program is free software; you can redistribute  it and/or modify it
11   * under  the terms of  the GNU General  Public License as published by the
12 @@ -23,69 +23,139 @@
13  
14  static char nvram_buf[NVRAM_SPACE];
15  
16 +static u32 find_nvram_size(u32 end)
17 +{
18 +       struct nvram_header *header;
19 +       u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
20 +       int i;
21 +
22 +       for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
23 +               header = (struct nvram_header *)KSEG1ADDR(end - nvram_sizes[i]);
24 +               if (header->magic == NVRAM_HEADER)
25 +                       return nvram_sizes[i];
26 +       }
27 +
28 +       return 0;
29 +}
30 +
31  /* Probe for NVRAM header */
32 -static void early_nvram_init(void)
33 +static void early_nvram_init_fill(u32 base, u32 lim)
34  {
35 -#ifdef CONFIG_BCM47XX_SSB
36 -       struct ssb_mipscore *mcore_ssb;
37 -#endif
38 -#ifdef CONFIG_BCM47XX_BCMA
39 -       struct bcma_drv_cc *bcma_cc;
40 -#endif
41         struct nvram_header *header;
42         int i;
43 -       u32 base = 0;
44 -       u32 lim = 0;
45         u32 off;
46         u32 *src, *dst;
47 +       u32 size;
48  
49 -       switch (bcm47xx_bus_type) {
50 -#ifdef CONFIG_BCM47XX_SSB
51 -       case BCM47XX_BUS_TYPE_SSB:
52 -               mcore_ssb = &bcm47xx_bus.ssb.mipscore;
53 -               base = mcore_ssb->pflash.window;
54 -               lim = mcore_ssb->pflash.window_size;
55 -               break;
56 -#endif
57 -#ifdef CONFIG_BCM47XX_BCMA
58 -       case BCM47XX_BUS_TYPE_BCMA:
59 -               bcma_cc = &bcm47xx_bus.bcma.bus.drv_cc;
60 -               base = bcma_cc->pflash.window;
61 -               lim = bcma_cc->pflash.window_size;
62 -               break;
63 -#endif
64 -       }
65 -
66 +       /* TODO: when nvram is on nand flash check for bad blocks first. */
67         off = FLASH_MIN;
68         while (off <= lim) {
69                 /* Windowed flash access */
70 -               header = (struct nvram_header *)
71 -                       KSEG1ADDR(base + off - NVRAM_SPACE);
72 -               if (header->magic == NVRAM_HEADER)
73 +               size = find_nvram_size(base + off);
74 +               if (size) {
75 +                       header = (struct nvram_header *)KSEG1ADDR(base + off -
76 +                                                                 size);
77                         goto found;
78 +               }
79                 off <<= 1;
80         }
81  
82         /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
83         header = (struct nvram_header *) KSEG1ADDR(base + 4096);
84 -       if (header->magic == NVRAM_HEADER)
85 +       if (header->magic == NVRAM_HEADER) {
86 +               size = NVRAM_SPACE;
87                 goto found;
88 +       }
89  
90         header = (struct nvram_header *) KSEG1ADDR(base + 1024);
91 -       if (header->magic == NVRAM_HEADER)
92 +       if (header->magic == NVRAM_HEADER) {
93 +               size = NVRAM_SPACE;
94                 goto found;
95 +       }
96  
97 +       pr_err("no nvram found\n");
98         return;
99  
100  found:
101 +
102 +       if (header->len > size)
103 +               pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
104 +       if (header->len > NVRAM_SPACE)
105 +               pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
106 +                      header->len, NVRAM_SPACE);
107 +
108         src = (u32 *) header;
109         dst = (u32 *) nvram_buf;
110         for (i = 0; i < sizeof(struct nvram_header); i += 4)
111                 *dst++ = *src++;
112 -       for (; i < header->len && i < NVRAM_SPACE; i += 4)
113 +       for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
114                 *dst++ = le32_to_cpu(*src++);
115  }
116  
117 +#ifdef CONFIG_BCM47XX_BCMA
118 +static void early_nvram_init_bcma(void)
119 +{
120 +       struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
121 +       u32 base = 0;
122 +       u32 lim = 0;
123 +
124 +       if (cc->nflash.boot) {
125 +               base = BCMA_SOC_FLASH1;
126 +               lim = BCMA_SOC_FLASH1_SZ;
127 +       } else if (cc->pflash.present) {
128 +               base = cc->pflash.window;
129 +               lim = cc->pflash.window_size;
130 +       } else if (cc->sflash.present) {
131 +               base = cc->sflash.window;
132 +               lim = cc->sflash.size;
133 +       } else {
134 +               pr_err("No supported flash found\n");
135 +               return;
136 +       }
137 +
138 +       early_nvram_init_fill(base, lim);
139 +}
140 +#endif
141 +
142 +#ifdef CONFIG_BCM47XX_SSB
143 +static void early_nvram_init_ssb(void)
144 +{
145 +       struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore;
146 +       struct ssb_chipcommon *chipco = &bcm47xx_bus.ssb.chipco;
147 +       u32 base = 0;
148 +       u32 lim = 0;
149 +
150 +       if (mcore->pflash.present) {
151 +               base = mcore->pflash.window;
152 +               lim = mcore->pflash.window_size;
153 +       } else if (chipco->sflash.present) {
154 +               base = chipco->sflash.window;
155 +               lim = chipco->sflash.size;
156 +       } else {
157 +               pr_err("No supported flash found\n");
158 +               return;
159 +       }
160 +
161 +       early_nvram_init_fill(base, lim);
162 +}
163 +#endif
164 +
165 +static void early_nvram_init(void)
166 +{
167 +       switch (bcm47xx_bus_type) {
168 +#ifdef CONFIG_BCM47XX_SSB
169 +       case BCM47XX_BUS_TYPE_SSB:
170 +               early_nvram_init_ssb();
171 +               break;
172 +#endif
173 +#ifdef CONFIG_BCM47XX_BCMA
174 +       case BCM47XX_BUS_TYPE_BCMA:
175 +               early_nvram_init_bcma();
176 +               break;
177 +#endif
178 +       }
179 +}
180 +
181  int nvram_getenv(char *name, char *val, size_t val_len)
182  {
183         char *var, *value, *end, *eq;