Merge tag 'idmapped-mounts-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / net / wireless / mediatek / mt76 / eeprom.c
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 #include <linux/of.h>
6 #include <linux/of_net.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/partitions.h>
9 #include <linux/etherdevice.h>
10 #include "mt76.h"
11
12 static int
13 mt76_get_of_eeprom(struct mt76_dev *dev, int len)
14 {
15 #if defined(CONFIG_OF) && defined(CONFIG_MTD)
16         struct device_node *np = dev->dev->of_node;
17         struct mtd_info *mtd;
18         const __be32 *list;
19         const char *part;
20         phandle phandle;
21         int offset = 0;
22         int size;
23         size_t retlen;
24         int ret;
25
26         if (!np)
27                 return -ENOENT;
28
29         list = of_get_property(np, "mediatek,mtd-eeprom", &size);
30         if (!list)
31                 return -ENOENT;
32
33         phandle = be32_to_cpup(list++);
34         if (!phandle)
35                 return -ENOENT;
36
37         np = of_find_node_by_phandle(phandle);
38         if (!np)
39                 return -EINVAL;
40
41         part = of_get_property(np, "label", NULL);
42         if (!part)
43                 part = np->name;
44
45         mtd = get_mtd_device_nm(part);
46         if (IS_ERR(mtd)) {
47                 ret =  PTR_ERR(mtd);
48                 goto out_put_node;
49         }
50
51         if (size <= sizeof(*list)) {
52                 ret = -EINVAL;
53                 goto out_put_node;
54         }
55
56         offset = be32_to_cpup(list);
57         ret = mtd_read(mtd, offset, len, &retlen, dev->eeprom.data);
58         put_mtd_device(mtd);
59         if (ret)
60                 goto out_put_node;
61
62         if (retlen < len) {
63                 ret = -EINVAL;
64                 goto out_put_node;
65         }
66
67         if (of_property_read_bool(dev->dev->of_node, "big-endian")) {
68                 u8 *data = (u8 *)dev->eeprom.data;
69                 int i;
70
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]),
74                                            &data[i]);
75         }
76
77 #ifdef CONFIG_NL80211_TESTMODE
78         dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
79         dev->test_mtd.offset = offset;
80 #endif
81
82 out_put_node:
83         of_node_put(np);
84         return ret;
85 #else
86         return -ENOENT;
87 #endif
88 }
89
90 void
91 mt76_eeprom_override(struct mt76_phy *phy)
92 {
93         struct mt76_dev *dev = phy->dev;
94
95 #ifdef CONFIG_OF
96         struct device_node *np = dev->dev->of_node;
97         const u8 *mac = NULL;
98
99         if (np)
100                 mac = of_get_mac_address(np);
101         if (!IS_ERR_OR_NULL(mac))
102                 ether_addr_copy(phy->macaddr, mac);
103 #endif
104
105         if (!is_valid_ether_addr(phy->macaddr)) {
106                 eth_random_addr(phy->macaddr);
107                 dev_info(dev->dev,
108                          "Invalid MAC address, using random address %pM\n",
109                          phy->macaddr);
110         }
111 }
112 EXPORT_SYMBOL_GPL(mt76_eeprom_override);
113
114 int
115 mt76_eeprom_init(struct mt76_dev *dev, int len)
116 {
117         dev->eeprom.size = len;
118         dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL);
119         if (!dev->eeprom.data)
120                 return -ENOMEM;
121
122         return !mt76_get_of_eeprom(dev, len);
123 }
124 EXPORT_SYMBOL_GPL(mt76_eeprom_init);