Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * BCM947xx nvram variable access |
| 3 | * |
| 4 | * Copyright (C) 2005 Broadcom Corporation |
| 5 | * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org> |
Hauke Mehrtens | fe6f364 | 2011-05-10 23:31:32 +0200 | [diff] [blame] | 6 | * Copyright (C) 2010-2011 Hauke Mehrtens <hauke@hauke-m.de> |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 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 */ |
Hauke Mehrtens | fe6f364 | 2011-05-10 23:31:32 +0200 | [diff] [blame] | 27 | static void early_nvram_init(void) |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 28 | { |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame^] | 29 | #ifdef CONFIG_BCM47XX_SSB |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 30 | struct ssb_mipscore *mcore_ssb; |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame^] | 31 | #endif |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 32 | struct nvram_header *header; |
| 33 | int i; |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 34 | u32 base = 0; |
| 35 | u32 lim = 0; |
| 36 | u32 off; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 37 | u32 *src, *dst; |
| 38 | |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 39 | switch (bcm47xx_bus_type) { |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame^] | 40 | #ifdef CONFIG_BCM47XX_SSB |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 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; |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame^] | 46 | #endif |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 47 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 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) |
Hauke Mehrtens | 47a3486 | 2010-08-02 23:56:22 +0200 | [diff] [blame] | 84 | return NVRAM_ERR_INV_PARAM; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 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 | } |
Hauke Mehrtens | 47a3486 | 2010-08-02 23:56:22 +0200 | [diff] [blame] | 104 | return NVRAM_ERR_ENVNOTFOUND; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 105 | } |
| 106 | EXPORT_SYMBOL(nvram_getenv); |