2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
8 * Copyright (C) 2012 Jonas Gorski <jonas.gorski@gmail.com>
11 #define pr_fmt(fmt) "bcm63xx_nvram: " fmt
13 #include <linux/init.h>
14 #include <linux/crc32.h>
15 #include <linux/export.h>
16 #include <linux/kernel.h>
17 #include <linux/if_ether.h>
19 #include <bcm63xx_nvram.h>
24 struct bcm963xx_nvram {
31 u8 mac_addr_base[ETH_ALEN];
38 #define BCM63XX_DEFAULT_PSI_SIZE 64
40 static struct bcm963xx_nvram nvram;
41 static int mac_addr_used;
43 void __init bcm63xx_nvram_init(void *addr)
45 unsigned int check_len;
46 u32 crc, expected_crc;
47 u8 hcs_mac_addr[ETH_ALEN] = { 0x00, 0x10, 0x18, 0xff, 0xff, 0xff };
49 /* extract nvram data */
50 memcpy(&nvram, addr, sizeof(nvram));
52 /* check checksum before using data */
53 if (nvram.version <= 4) {
54 check_len = offsetof(struct bcm963xx_nvram, reserved3);
55 expected_crc = nvram.checksum_old;
56 nvram.checksum_old = 0;
58 check_len = sizeof(nvram);
59 expected_crc = nvram.checksum_high;
60 nvram.checksum_high = 0;
63 crc = crc32_le(~0, (u8 *)&nvram, check_len);
65 if (crc != expected_crc)
66 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
69 /* Cable modems have a different NVRAM which is embedded in the eCos
70 * firmware and not easily extractible, give at least a MAC address
73 if (BCMCPU_IS_3368()) {
74 memcpy(nvram.mac_addr_base, hcs_mac_addr, ETH_ALEN);
75 nvram.mac_addr_count = 2;
79 u8 *bcm63xx_nvram_get_name(void)
83 EXPORT_SYMBOL(bcm63xx_nvram_get_name);
85 int bcm63xx_nvram_get_mac_address(u8 *mac)
90 if (mac_addr_used >= nvram.mac_addr_count) {
91 pr_err("not enough mac addresses\n");
95 memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
96 oui = mac + ETH_ALEN/2 - 1;
97 count = mac_addr_used;
100 u8 *p = mac + ETH_ALEN - 1;
110 pr_err("unable to fetch mac address\n");
118 EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address);
120 int bcm63xx_nvram_get_psi_size(void)
122 if (nvram.psi_size > 0)
123 return nvram.psi_size;
125 return BCM63XX_DEFAULT_PSI_SIZE;
127 EXPORT_SYMBOL(bcm63xx_nvram_get_psi_size);