linux/arch/mips/bcm47xx/nvram.c
<<
>>
Prefs
   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-2012 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 <bcm47xx_nvram.h>
  22#include <asm/mach-bcm47xx/bcm47xx.h>
  23
  24static char nvram_buf[NVRAM_SPACE];
  25
  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 */
  42static int nvram_find_and_copy(u32 base, u32 lim)
  43{
  44        struct nvram_header *header;
  45        int i;
  46        u32 off;
  47        u32 *src, *dst;
  48        u32 size;
  49
  50        /* TODO: when nvram is on nand flash check for bad blocks first. */
  51        off = FLASH_MIN;
  52        while (off <= lim) {
  53                /* Windowed flash access */
  54                size = find_nvram_size(base + off);
  55                if (size) {
  56                        header = (struct nvram_header *)KSEG1ADDR(base + off -
  57                                                                  size);
  58                        goto found;
  59                }
  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);
  65        if (header->magic == NVRAM_HEADER) {
  66                size = NVRAM_SPACE;
  67                goto found;
  68        }
  69
  70        header = (struct nvram_header *) KSEG1ADDR(base + 1024);
  71        if (header->magic == NVRAM_HEADER) {
  72                size = NVRAM_SPACE;
  73                goto found;
  74        }
  75
  76        pr_err("no nvram found\n");
  77        return -ENXIO;
  78
  79found:
  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
  87        src = (u32 *) header;
  88        dst = (u32 *) nvram_buf;
  89        for (i = 0; i < sizeof(struct nvram_header); i += 4)
  90                *dst++ = *src++;
  91        for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
  92                *dst++ = le32_to_cpu(*src++);
  93        memset(dst, 0x0, NVRAM_SPACE - i);
  94
  95        return 0;
  96}
  97
  98#ifdef CONFIG_BCM47XX_SSB
  99static int nvram_init_ssb(void)
 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");
 110                return -ENXIO;
 111        }
 112
 113        return nvram_find_and_copy(base, lim);
 114}
 115#endif
 116
 117#ifdef CONFIG_BCM47XX_BCMA
 118static int nvram_init_bcma(void)
 119{
 120        struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc;
 121        u32 base;
 122        u32 lim;
 123
 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
 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");
 140                return -ENXIO;
 141        }
 142
 143        return nvram_find_and_copy(base, lim);
 144}
 145#endif
 146
 147static int nvram_init(void)
 148{
 149        switch (bcm47xx_bus_type) {
 150#ifdef CONFIG_BCM47XX_SSB
 151        case BCM47XX_BUS_TYPE_SSB:
 152                return nvram_init_ssb();
 153#endif
 154#ifdef CONFIG_BCM47XX_BCMA
 155        case BCM47XX_BUS_TYPE_BCMA:
 156                return nvram_init_bcma();
 157#endif
 158        }
 159        return -ENXIO;
 160}
 161
 162int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
 163{
 164        char *var, *value, *end, *eq;
 165        int err;
 166
 167        if (!name)
 168                return -EINVAL;
 169
 170        if (!nvram_buf[0]) {
 171                err = nvram_init();
 172                if (err)
 173                        return err;
 174        }
 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) {
 187                        return snprintf(val, val_len, "%s", value);
 188                }
 189        }
 190        return -ENOENT;
 191}
 192EXPORT_SYMBOL(bcm47xx_nvram_getenv);
 193