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 |
Hauke Mehrtens | c1d1c5d | 2011-07-23 01:20:14 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_BCM47XX_BCMA |
| 33 | struct bcma_drv_cc *bcma_cc; |
| 34 | #endif |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 35 | struct nvram_header *header; |
| 36 | int i; |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 37 | u32 base = 0; |
| 38 | u32 lim = 0; |
| 39 | u32 off; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 40 | u32 *src, *dst; |
| 41 | |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 42 | switch (bcm47xx_bus_type) { |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_BCM47XX_SSB |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 44 | case BCM47XX_BUS_TYPE_SSB: |
| 45 | mcore_ssb = &bcm47xx_bus.ssb.mipscore; |
| 46 | base = mcore_ssb->flash_window; |
| 47 | lim = mcore_ssb->flash_window_size; |
| 48 | break; |
Hauke Mehrtens | a656ffc | 2011-07-23 01:20:13 +0200 | [diff] [blame] | 49 | #endif |
Hauke Mehrtens | c1d1c5d | 2011-07-23 01:20:14 +0200 | [diff] [blame] | 50 | #ifdef CONFIG_BCM47XX_BCMA |
| 51 | case BCM47XX_BUS_TYPE_BCMA: |
| 52 | bcma_cc = &bcm47xx_bus.bcma.bus.drv_cc; |
| 53 | base = bcma_cc->pflash.window; |
| 54 | lim = bcma_cc->pflash.window_size; |
| 55 | break; |
| 56 | #endif |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 57 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 58 | |
| 59 | off = FLASH_MIN; |
| 60 | while (off <= lim) { |
| 61 | /* Windowed flash access */ |
| 62 | header = (struct nvram_header *) |
| 63 | KSEG1ADDR(base + off - NVRAM_SPACE); |
| 64 | if (header->magic == NVRAM_HEADER) |
| 65 | goto found; |
| 66 | off <<= 1; |
| 67 | } |
| 68 | |
| 69 | /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ |
| 70 | header = (struct nvram_header *) KSEG1ADDR(base + 4096); |
| 71 | if (header->magic == NVRAM_HEADER) |
| 72 | goto found; |
| 73 | |
| 74 | header = (struct nvram_header *) KSEG1ADDR(base + 1024); |
| 75 | if (header->magic == NVRAM_HEADER) |
| 76 | goto found; |
| 77 | |
| 78 | return; |
| 79 | |
| 80 | found: |
| 81 | src = (u32 *) header; |
| 82 | dst = (u32 *) nvram_buf; |
| 83 | for (i = 0; i < sizeof(struct nvram_header); i += 4) |
| 84 | *dst++ = *src++; |
| 85 | for (; i < header->len && i < NVRAM_SPACE; i += 4) |
| 86 | *dst++ = le32_to_cpu(*src++); |
| 87 | } |
| 88 | |
| 89 | int nvram_getenv(char *name, char *val, size_t val_len) |
| 90 | { |
| 91 | char *var, *value, *end, *eq; |
| 92 | |
| 93 | if (!name) |
Hauke Mehrtens | 47a3486 | 2010-08-02 23:56:22 +0200 | [diff] [blame] | 94 | return NVRAM_ERR_INV_PARAM; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 95 | |
| 96 | if (!nvram_buf[0]) |
| 97 | early_nvram_init(); |
| 98 | |
| 99 | /* Look for name=value and return value */ |
| 100 | var = &nvram_buf[sizeof(struct nvram_header)]; |
| 101 | end = nvram_buf + sizeof(nvram_buf) - 2; |
| 102 | end[0] = end[1] = '\0'; |
| 103 | for (; *var; var = value + strlen(value) + 1) { |
| 104 | eq = strchr(var, '='); |
| 105 | if (!eq) |
| 106 | break; |
| 107 | value = eq + 1; |
| 108 | if ((eq - var) == strlen(name) && |
| 109 | strncmp(var, name, (eq - var)) == 0) { |
| 110 | snprintf(val, val_len, "%s", value); |
| 111 | return 0; |
| 112 | } |
| 113 | } |
Hauke Mehrtens | 47a3486 | 2010-08-02 23:56:22 +0200 | [diff] [blame] | 114 | return NVRAM_ERR_ENVNOTFOUND; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(nvram_getenv); |