xref: /openbmc/linux/arch/mips/bcm63xx/nvram.c (revision 5bdb102b)
1 /*
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
4  * for more details.
5  *
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>
9  */
10 
11 #define pr_fmt(fmt) "bcm63xx_nvram: " fmt
12 
13 #include <linux/bcm963xx_nvram.h>
14 #include <linux/init.h>
15 #include <linux/crc32.h>
16 #include <linux/export.h>
17 #include <linux/kernel.h>
18 #include <linux/if_ether.h>
19 
20 #include <bcm63xx_nvram.h>
21 
22 static struct bcm963xx_nvram nvram;
23 static int mac_addr_used;
24 
25 void __init bcm63xx_nvram_init(void *addr)
26 {
27 	u32 crc, expected_crc;
28 	u8 hcs_mac_addr[ETH_ALEN] = { 0x00, 0x10, 0x18, 0xff, 0xff, 0xff };
29 
30 	/* extract nvram data */
31 	memcpy(&nvram, addr, BCM963XX_NVRAM_V5_SIZE);
32 
33 	/* check checksum before using data */
34 	if (bcm963xx_nvram_checksum(&nvram, &expected_crc, &crc))
35 		pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
36 			expected_crc, crc);
37 
38 	/* Cable modems have a different NVRAM which is embedded in the eCos
39 	 * firmware and not easily extractible, give at least a MAC address
40 	 * pool.
41 	 */
42 	if (BCMCPU_IS_3368()) {
43 		memcpy(nvram.mac_addr_base, hcs_mac_addr, ETH_ALEN);
44 		nvram.mac_addr_count = 2;
45 	}
46 }
47 
48 u8 *bcm63xx_nvram_get_name(void)
49 {
50 	return nvram.name;
51 }
52 EXPORT_SYMBOL(bcm63xx_nvram_get_name);
53 
54 int bcm63xx_nvram_get_mac_address(u8 *mac)
55 {
56 	u8 *oui;
57 	int count;
58 
59 	if (mac_addr_used >= nvram.mac_addr_count) {
60 		pr_err("not enough mac addresses\n");
61 		return -ENODEV;
62 	}
63 
64 	memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
65 	oui = mac + ETH_ALEN/2 - 1;
66 	count = mac_addr_used;
67 
68 	while (count--) {
69 		u8 *p = mac + ETH_ALEN - 1;
70 
71 		do {
72 			(*p)++;
73 			if (*p != 0)
74 				break;
75 			p--;
76 		} while (p != oui);
77 
78 		if (p == oui) {
79 			pr_err("unable to fetch mac address\n");
80 			return -ENODEV;
81 		}
82 	}
83 
84 	mac_addr_used++;
85 	return 0;
86 }
87 EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address);
88