blob: 05757aed016cd6f72bedbd0ac31f379e8f0cd884 [file] [log] [blame]
Jonas Gorskie7e333c2012-11-07 08:25:28 +00001/*
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
Simon Arlott5a8b0b12015-12-13 22:46:15 +000013#include <linux/bcm963xx_nvram.h>
Jonas Gorskie7e333c2012-11-07 08:25:28 +000014#include <linux/init.h>
Jonas Gorskice8f0d02012-11-11 12:22:34 +000015#include <linux/crc32.h>
Jonas Gorskie7e333c2012-11-07 08:25:28 +000016#include <linux/export.h>
17#include <linux/kernel.h>
18#include <linux/if_ether.h>
19
20#include <bcm63xx_nvram.h>
21
Ralf Baechleb0a119f2016-01-27 20:50:36 +010022#define BCM63XX_DEFAULT_PSI_SIZE 64
23
Jonas Gorskie7e333c2012-11-07 08:25:28 +000024static struct bcm963xx_nvram nvram;
25static int mac_addr_used;
26
Jonas Gorski97367512013-03-19 13:08:27 +000027void __init bcm63xx_nvram_init(void *addr)
Jonas Gorskie7e333c2012-11-07 08:25:28 +000028{
Jonas Gorskice8f0d02012-11-11 12:22:34 +000029 u32 crc, expected_crc;
Florian Fainelli0680dc82013-06-18 16:55:42 +000030 u8 hcs_mac_addr[ETH_ALEN] = { 0x00, 0x10, 0x18, 0xff, 0xff, 0xff };
Jonas Gorskie7e333c2012-11-07 08:25:28 +000031
32 /* extract nvram data */
Simon Arlott5a8b0b12015-12-13 22:46:15 +000033 memcpy(&nvram, addr, BCM963XX_NVRAM_V5_SIZE);
Jonas Gorskie7e333c2012-11-07 08:25:28 +000034
35 /* check checksum before using data */
Simon Arlott5a8b0b12015-12-13 22:46:15 +000036 if (bcm963xx_nvram_checksum(&nvram, &expected_crc, &crc))
Jonas Gorski97367512013-03-19 13:08:27 +000037 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
38 expected_crc, crc);
Florian Fainelli0680dc82013-06-18 16:55:42 +000039
40 /* Cable modems have a different NVRAM which is embedded in the eCos
41 * firmware and not easily extractible, give at least a MAC address
42 * pool.
43 */
44 if (BCMCPU_IS_3368()) {
45 memcpy(nvram.mac_addr_base, hcs_mac_addr, ETH_ALEN);
46 nvram.mac_addr_count = 2;
47 }
Jonas Gorskie7e333c2012-11-07 08:25:28 +000048}
49
50u8 *bcm63xx_nvram_get_name(void)
51{
52 return nvram.name;
53}
54EXPORT_SYMBOL(bcm63xx_nvram_get_name);
55
56int bcm63xx_nvram_get_mac_address(u8 *mac)
57{
58 u8 *oui;
59 int count;
60
61 if (mac_addr_used >= nvram.mac_addr_count) {
62 pr_err("not enough mac addresses\n");
63 return -ENODEV;
64 }
65
66 memcpy(mac, nvram.mac_addr_base, ETH_ALEN);
67 oui = mac + ETH_ALEN/2 - 1;
68 count = mac_addr_used;
69
70 while (count--) {
71 u8 *p = mac + ETH_ALEN - 1;
72
73 do {
74 (*p)++;
75 if (*p != 0)
76 break;
77 p--;
78 } while (p != oui);
79
80 if (p == oui) {
81 pr_err("unable to fetch mac address\n");
82 return -ENODEV;
83 }
84 }
85
86 mac_addr_used++;
87 return 0;
88}
89EXPORT_SYMBOL(bcm63xx_nvram_get_mac_address);
Ralf Baechleb0a119f2016-01-27 20:50:36 +010090
91int bcm63xx_nvram_get_psi_size(void)
92{
93 if (nvram.psi_size > 0)
94 return nvram.psi_size;
95
96 return BCM63XX_DEFAULT_PSI_SIZE;
97}
98EXPORT_SYMBOL(bcm63xx_nvram_get_psi_size);