ixp4xx: add support for linux 3.3.1
[openwrt.git] / target / linux / brcm47xx / patches-3.0 / 0025-bcm47xx-read-nvram-from-sflash.patch
1 From 1d693b2c9d5943cbe938f879041b837cd004737f Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sat, 23 Jul 2011 18:29:38 +0200
4 Subject: [PATCH 25/26] bcm47xx: read nvram from sflash
5
6 bcm47xx: add sflash support to nvram
7
8 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
9 ---
10  arch/mips/bcm47xx/nvram.c |   86 +++++++++++++++++++++++++++++++++++++++++++-
11  1 files changed, 84 insertions(+), 2 deletions(-)
12
13 --- a/arch/mips/bcm47xx/nvram.c
14 +++ b/arch/mips/bcm47xx/nvram.c
15 @@ -20,11 +20,12 @@
16  #include <asm/addrspace.h>
17  #include <asm/mach-bcm47xx/nvram.h>
18  #include <asm/mach-bcm47xx/bcm47xx.h>
19 +#include <asm/mach-bcm47xx/bus.h>
20  
21  static char nvram_buf[NVRAM_SPACE];
22  
23  /* Probe for NVRAM header */
24 -static void early_nvram_init(void)
25 +static void early_nvram_init_pflash(void)
26  {
27  #ifdef CONFIG_BCM47XX_SSB
28         struct ssb_chipcommon *ssb_cc;
29 @@ -86,7 +87,88 @@ found:
30         for (i = 0; i < sizeof(struct nvram_header); i += 4)
31                 *dst++ = *src++;
32         for (; i < header->len && i < NVRAM_SPACE; i += 4)
33 -               *dst++ = le32_to_cpu(*src++);
34 +               *dst++ = *src++;
35 +}
36 +
37 +static int early_nvram_init_sflash(void)
38 +{
39 +       struct nvram_header header;
40 +       u32 off;
41 +       int ret;
42 +       char *dst;
43 +       int len;
44 +
45 +       /* check if the struct is already initilized */
46 +       if (!bcm47xx_sflash.size)
47 +               return -1;
48 +
49 +       off = FLASH_MIN;
50 +       while (off <= bcm47xx_sflash.size) {
51 +               ret = bcm47xx_sflash.read(&bcm47xx_sflash, off - NVRAM_SPACE, sizeof(header), (u8 *)&header);
52 +               if (ret != sizeof(header))
53 +                       return ret;
54 +               if (header.magic == NVRAM_HEADER)
55 +                       goto found;
56 +               off <<= 1;
57 +       }
58 +
59 +       off = FLASH_MIN;
60 +       while (off <= bcm47xx_sflash.size) {
61 +               ret = bcm47xx_sflash.read(&bcm47xx_sflash, off - (2 * NVRAM_SPACE), sizeof(header), (u8 *)&header);
62 +               if (ret != sizeof(header))
63 +                       return ret;
64 +               if (header.magic == NVRAM_HEADER)
65 +                       goto found;
66 +               off <<= 1;
67 +       }
68 +       return -1;
69 +
70 +found:
71 +       len = NVRAM_SPACE;
72 +       dst = nvram_buf;
73 +       while (len) {
74 +               ret = bcm47xx_sflash.read(&bcm47xx_sflash, off - (2 * NVRAM_SPACE), len, dst);
75 +               if (ret < 0)
76 +                       return ret;
77 +               off += ret;
78 +               len -= ret;
79 +               dst += ret;
80 +       }
81 +       return 0;
82 +}
83 +
84 +static void early_nvram_init(void)
85 +{
86 +       int err = 0;
87 +
88 +       switch (bcm47xx_bus_type) {
89 +#ifdef CONFIG_BCM47XX_SSB
90 +       case BCM47XX_BUS_TYPE_SSB:
91 +               if (bcm47xx_bus.ssb.chipco.flash_type == SSB_PFLASH) {
92 +                       early_nvram_init_pflash();
93 +               } else if (bcm47xx_bus.ssb.chipco.flash_type == SSB_SFLASH) {
94 +                       err = early_nvram_init_sflash();
95 +                       if (err < 0)
96 +                               printk(KERN_WARNING "can not read from flash: %i\n", err);
97 +               } else {
98 +                       printk(KERN_WARNING "unknow flash type\n");
99 +               }
100 +               break;
101 +#endif
102 +#ifdef CONFIG_BCM47XX_BCMA
103 +       case BCM47XX_BUS_TYPE_BCMA:
104 +               if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_PFLASH) {
105 +                       early_nvram_init_pflash();
106 +               } else if (bcm47xx_bus.bcma.bus.drv_cc.flash_type == BCMA_SFLASH) {
107 +                       err = early_nvram_init_sflash();
108 +                       if (err < 0)
109 +                               printk(KERN_WARNING "can not read from flash: %i\n", err);
110 +               } else {
111 +                       printk(KERN_WARNING "unknow flash type\n");
112 +               }
113 +               break;
114 +#endif
115 +       }
116  }
117  
118  int nvram_getenv(char *name, char *val, size_t val_len)