]> Pileus Git - ~andy/linux/blob - arch/mips/bcm63xx/nvram.c
Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / arch / mips / bcm63xx / nvram.c
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/init.h>
14 #include <linux/crc32.h>
15 #include <linux/export.h>
16 #include <linux/kernel.h>
17 #include <linux/if_ether.h>
18
19 #include <bcm63xx_nvram.h>
20
21 /*
22  * nvram structure
23  */
24 struct bcm963xx_nvram {
25         u32     version;
26         u8      reserved1[256];
27         u8      name[16];
28         u32     main_tp_number;
29         u32     psi_size;
30         u32     mac_addr_count;
31         u8      mac_addr_base[ETH_ALEN];
32         u8      reserved2[2];
33         u32     checksum_old;
34         u8      reserved3[720];
35         u32     checksum_high;
36 };
37
38 static struct bcm963xx_nvram nvram;
39 static int mac_addr_used;
40
41 void __init bcm63xx_nvram_init(void *addr)
42 {
43         unsigned int check_len;
44         u32 crc, expected_crc;
45         u8 hcs_mac_addr[ETH_ALEN] = { 0x00, 0x10, 0x18, 0xff, 0xff, 0xff };
46
47         /* extract nvram data */
48         memcpy(&nvram, addr, sizeof(nvram));
49
50         /* check checksum before using data */
51         if (nvram.version <= 4) {
52                 check_len = offsetof(struct bcm963xx_nvram, reserved3);
53                 expected_crc = nvram.checksum_old;
54                 nvram.checksum_old = 0;
55         } else {
56                 check_len = sizeof(nvram);
57                 expected_crc = nvram.checksum_high;
58                 nvram.checksum_high = 0;
59         }
60
61         crc = crc32_le(~0, (u8 *)&nvram, check_len);
62
63         if (crc != expected_crc)
64                 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
65                         expected_crc, crc);
66
67         /* Cable modems have a different NVRAM which is embedded in the eCos
68          * firmware and not easily extractible, give at least a MAC address
69          * pool.
70          */
71         if (BCMCPU_IS_3368()) {
72                 memcpy(nvram.mac_addr_base, hcs_mac_addr, ETH_ALEN);
73                 nvram.mac_addr_count = 2;
74         }
75 }
76
77 u8 *bcm63xx_nvram_get_name(void)
78 {
79         return nvram.name;
80 }
81 EXPORT_SYMBOL(bcm63xx_nvram_get_name);
82
83 int bcm63xx_nvram_get_mac_address(u8 *mac)
84 {
85         u8 *oui;
86         int count;
87
88         if (mac_addr_used >= nvram.mac_addr_count) {
89                 pr_err("not enough mac addresses\n");
90                 return -ENODEV;
91         }
92
93         memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
94         oui = mac + ETH_ALEN/2 - 1;
95         count = mac_addr_used;
96
97         while (count--) {
98                 u8 *p = mac + ETH_ALEN - 1;
99
100                 do {
101                         (*p)++;
102                         if (*p != 0)
103                                 break;
104                         p--;
105                 } while (p != oui);
106
107                 if (p == oui) {
108                         pr_err("unable to fetch mac address\n");
109                         return -ENODEV;
110                 }
111         }
112
113         mac_addr_used++;
114         return 0;
115 }
116 EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address);