1 // SPDX-License-Identifier: ISC
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
6 #include <linux/of_net.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/partitions.h>
9 #include <linux/etherdevice.h>
13 mt76_get_of_eeprom(struct mt76_dev
*dev
, int len
)
15 #if defined(CONFIG_OF) && defined(CONFIG_MTD)
16 struct device_node
*np
= dev
->dev
->of_node
;
29 list
= of_get_property(np
, "mediatek,mtd-eeprom", &size
);
33 phandle
= be32_to_cpup(list
++);
37 np
= of_find_node_by_phandle(phandle
);
41 part
= of_get_property(np
, "label", NULL
);
45 mtd
= get_mtd_device_nm(part
);
51 if (size
<= sizeof(*list
)) {
56 offset
= be32_to_cpup(list
);
57 ret
= mtd_read(mtd
, offset
, len
, &retlen
, dev
->eeprom
.data
);
67 if (of_property_read_bool(dev
->dev
->of_node
, "big-endian")) {
68 u8
*data
= (u8
*)dev
->eeprom
.data
;
71 /* convert eeprom data in Little Endian */
72 for (i
= 0; i
< round_down(len
, 2); i
+= 2)
73 put_unaligned_le16(get_unaligned_be16(&data
[i
]),
86 mt76_eeprom_override(struct mt76_dev
*dev
)
89 struct device_node
*np
= dev
->dev
->of_node
;
93 mac
= of_get_mac_address(np
);
94 if (!IS_ERR_OR_NULL(mac
))
95 ether_addr_copy(dev
->macaddr
, mac
);
98 if (!is_valid_ether_addr(dev
->macaddr
)) {
99 eth_random_addr(dev
->macaddr
);
101 "Invalid MAC address, using random address %pM\n",
105 EXPORT_SYMBOL_GPL(mt76_eeprom_override
);
108 mt76_eeprom_init(struct mt76_dev
*dev
, int len
)
110 dev
->eeprom
.size
= len
;
111 dev
->eeprom
.data
= devm_kzalloc(dev
->dev
, len
, GFP_KERNEL
);
112 if (!dev
->eeprom
.data
)
115 return !mt76_get_of_eeprom(dev
, len
);
117 EXPORT_SYMBOL_GPL(mt76_eeprom_init
);