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 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 26 | static int nvram_find_and_copy(u32 base, u32 lim) |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 27 | { |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 28 | struct nvram_header *header; |
| 29 | int i; |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 30 | u32 off; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 31 | u32 *src, *dst; |
| 32 | |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 33 | off = FLASH_MIN; |
| 34 | while (off <= lim) { |
| 35 | /* Windowed flash access */ |
| 36 | header = (struct nvram_header *) |
| 37 | KSEG1ADDR(base + off - NVRAM_SPACE); |
| 38 | if (header->magic == NVRAM_HEADER) |
| 39 | goto found; |
| 40 | off <<= 1; |
| 41 | } |
| 42 | |
| 43 | /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ |
| 44 | header = (struct nvram_header *) KSEG1ADDR(base + 4096); |
| 45 | if (header->magic == NVRAM_HEADER) |
| 46 | goto found; |
| 47 | |
| 48 | header = (struct nvram_header *) KSEG1ADDR(base + 1024); |
| 49 | if (header->magic == NVRAM_HEADER) |
| 50 | goto found; |
| 51 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 52 | return -ENXIO; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 53 | |
| 54 | found: |
| 55 | src = (u32 *) header; |
| 56 | dst = (u32 *) nvram_buf; |
| 57 | for (i = 0; i < sizeof(struct nvram_header); i += 4) |
| 58 | *dst++ = *src++; |
| 59 | for (; i < header->len && i < NVRAM_SPACE; i += 4) |
| 60 | *dst++ = le32_to_cpu(*src++); |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 61 | |
| 62 | return 0; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_BCM47XX_SSB |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 66 | static int nvram_init_ssb(void) |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 67 | { |
| 68 | struct ssb_mipscore *mcore = &bcm47xx_bus.ssb.mipscore; |
| 69 | u32 base; |
| 70 | u32 lim; |
| 71 | |
| 72 | if (mcore->pflash.present) { |
| 73 | base = mcore->pflash.window; |
| 74 | lim = mcore->pflash.window_size; |
| 75 | } else { |
| 76 | pr_err("Couldn't find supported flash memory\n"); |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 77 | return -ENXIO; |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 80 | return nvram_find_and_copy(base, lim); |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #ifdef CONFIG_BCM47XX_BCMA |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 85 | static int nvram_init_bcma(void) |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 86 | { |
| 87 | struct bcma_drv_cc *cc = &bcm47xx_bus.bcma.bus.drv_cc; |
| 88 | u32 base; |
| 89 | u32 lim; |
| 90 | |
| 91 | if (cc->pflash.present) { |
| 92 | base = cc->pflash.window; |
| 93 | lim = cc->pflash.window_size; |
| 94 | #ifdef CONFIG_BCMA_SFLASH |
| 95 | } else if (cc->sflash.present) { |
| 96 | base = cc->sflash.window; |
| 97 | lim = cc->sflash.size; |
| 98 | #endif |
| 99 | } else { |
| 100 | pr_err("Couldn't find supported flash memory\n"); |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 101 | return -ENXIO; |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 104 | return nvram_find_and_copy(base, lim); |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 105 | } |
| 106 | #endif |
| 107 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 108 | static int early_nvram_init(void) |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 109 | { |
| 110 | switch (bcm47xx_bus_type) { |
| 111 | #ifdef CONFIG_BCM47XX_SSB |
| 112 | case BCM47XX_BUS_TYPE_SSB: |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 113 | return nvram_init_ssb(); |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 114 | #endif |
| 115 | #ifdef CONFIG_BCM47XX_BCMA |
| 116 | case BCM47XX_BUS_TYPE_BCMA: |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 117 | return nvram_init_bcma(); |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 118 | #endif |
| 119 | } |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 120 | return -ENXIO; |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 123 | int nvram_getenv(char *name, char *val, size_t val_len) |
| 124 | { |
| 125 | char *var, *value, *end, *eq; |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 126 | int err; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 127 | |
| 128 | if (!name) |
Hauke Mehrtens | ee7e2f3 | 2012-12-26 19:51:09 +0000 | [diff] [blame] | 129 | return -EINVAL; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 130 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame^] | 131 | if (!nvram_buf[0]) { |
| 132 | err = early_nvram_init(); |
| 133 | if (err) |
| 134 | return err; |
| 135 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 136 | |
| 137 | /* Look for name=value and return value */ |
| 138 | var = &nvram_buf[sizeof(struct nvram_header)]; |
| 139 | end = nvram_buf + sizeof(nvram_buf) - 2; |
| 140 | end[0] = end[1] = '\0'; |
| 141 | for (; *var; var = value + strlen(value) + 1) { |
| 142 | eq = strchr(var, '='); |
| 143 | if (!eq) |
| 144 | break; |
| 145 | value = eq + 1; |
| 146 | if ((eq - var) == strlen(name) && |
| 147 | strncmp(var, name, (eq - var)) == 0) { |
Hauke Mehrtens | 44d4b2a | 2012-02-28 00:56:11 +0100 | [diff] [blame] | 148 | return snprintf(val, val_len, "%s", value); |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
Hauke Mehrtens | ee7e2f3 | 2012-12-26 19:51:09 +0000 | [diff] [blame] | 151 | return -ENOENT; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL(nvram_getenv); |