2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/of_net.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/etherdevice.h>
24 mt76_get_of_eeprom(struct mt76_dev
*dev
, int len
)
26 #if defined(CONFIG_OF) && defined(CONFIG_MTD)
27 struct device_node
*np
= dev
->dev
->of_node
;
40 list
= of_get_property(np
, "mediatek,mtd-eeprom", &size
);
44 phandle
= be32_to_cpup(list
++);
48 np
= of_find_node_by_phandle(phandle
);
52 part
= of_get_property(np
, "label", NULL
);
56 mtd
= get_mtd_device_nm(part
);
62 if (size
<= sizeof(*list
)) {
67 offset
= be32_to_cpup(list
);
68 ret
= mtd_read(mtd
, offset
, len
, &retlen
, dev
->eeprom
.data
);
87 mt76_eeprom_override(struct mt76_dev
*dev
)
90 struct device_node
*np
= dev
->dev
->of_node
;
96 mac
= of_get_mac_address(np
);
98 memcpy(dev
->macaddr
, mac
, ETH_ALEN
);
101 if (!is_valid_ether_addr(dev
->macaddr
)) {
102 eth_random_addr(dev
->macaddr
);
104 "Invalid MAC address, using random address %pM\n",
108 EXPORT_SYMBOL_GPL(mt76_eeprom_override
);
111 mt76_eeprom_init(struct mt76_dev
*dev
, int len
)
113 dev
->eeprom
.size
= len
;
114 dev
->eeprom
.data
= devm_kzalloc(dev
->dev
, len
, GFP_KERNEL
);
115 if (!dev
->eeprom
.data
)
118 return !mt76_get_of_eeprom(dev
, len
);
120 EXPORT_SYMBOL_GPL(mt76_eeprom_init
);