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