1 // SPDX-License-Identifier: ISC
6 #include <linux/of_net.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/partitions.h>
9 #include <linux/etherdevice.h>
12 int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int offset, int len)
14 #if defined(CONFIG_OF) && defined(CONFIG_MTD)
15 struct device_node *np = dev->dev->of_node;
28 data = of_get_property(np, "mediatek,eeprom-data", &size);
33 memcpy(eep, data, size);
38 list = of_get_property(np, "mediatek,mtd-eeprom", &size);
42 phandle = be32_to_cpup(list++);
46 np = of_find_node_by_phandle(phandle);
50 part = of_get_property(np, "label", NULL);
54 mtd = get_mtd_device_nm(part);
60 if (size <= sizeof(*list)) {
65 offset = be32_to_cpup(list);
66 ret = mtd_read(mtd, offset, len, &retlen, eep);
68 if (mtd_is_bitflip(ret))
71 dev_err(dev->dev, "reading EEPROM from mtd %s failed: %i\n",
81 if (of_property_read_bool(dev->dev->of_node, "big-endian")) {
85 /* convert eeprom data in Little Endian */
86 for (i = 0; i < round_down(len, 2); i += 2)
87 put_unaligned_le16(get_unaligned_be16(&data[i]),
91 #ifdef CONFIG_NL80211_TESTMODE
92 dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
93 dev->test_mtd.offset = offset;
103 EXPORT_SYMBOL_GPL(mt76_get_of_eeprom);
106 mt76_eeprom_override(struct mt76_phy *phy)
108 struct mt76_dev *dev = phy->dev;
109 struct device_node *np = dev->dev->of_node;
111 of_get_mac_address(np, phy->macaddr);
113 if (!is_valid_ether_addr(phy->macaddr)) {
114 eth_random_addr(phy->macaddr);
116 "Invalid MAC address, using random address %pM\n",
120 EXPORT_SYMBOL_GPL(mt76_eeprom_override);
122 static bool mt76_string_prop_find(struct property *prop, const char *str)
124 const char *cp = NULL;
126 if (!prop || !str || !str[0])
129 while ((cp = of_prop_next_string(prop, cp)) != NULL)
130 if (!strcasecmp(cp, str))
136 static struct device_node *
137 mt76_find_power_limits_node(struct mt76_dev *dev)
139 struct device_node *np = dev->dev->of_node;
140 const char *const region_names[] = {
141 [NL80211_DFS_UNSET] = "ww",
142 [NL80211_DFS_ETSI] = "etsi",
143 [NL80211_DFS_FCC] = "fcc",
144 [NL80211_DFS_JP] = "jp",
146 struct device_node *cur, *fallback = NULL;
147 const char *region_name = NULL;
149 if (dev->region < ARRAY_SIZE(region_names))
150 region_name = region_names[dev->region];
152 np = of_get_child_by_name(np, "power-limits");
156 for_each_child_of_node(np, cur) {
157 struct property *country = of_find_property(cur, "country", NULL);
158 struct property *regd = of_find_property(cur, "regdomain", NULL);
160 if (!country && !regd) {
165 if (mt76_string_prop_find(country, dev->alpha2) ||
166 mt76_string_prop_find(regd, region_name)) {
176 static const __be32 *
177 mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min)
179 struct property *prop = of_find_property(np, name, NULL);
181 if (!prop || !prop->value || prop->length < min * 4)
189 static struct device_node *
190 mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan)
192 struct device_node *cur;
196 for_each_child_of_node(np, cur) {
197 val = mt76_get_of_array(cur, "channels", &len, 2);
201 while (len >= 2 * sizeof(*val)) {
202 if (chan->hw_value >= be32_to_cpu(val[0]) &&
203 chan->hw_value <= be32_to_cpu(val[1]))
207 len -= 2 * sizeof(*val);
215 mt76_get_txs_delta(struct device_node *np, u8 nss)
220 val = mt76_get_of_array(np, "txs-delta", &len, nss);
224 return be32_to_cpu(val[nss - 1]);
228 mt76_apply_array_limit(s8 *pwr, size_t pwr_len, const __be32 *data,
229 s8 target_power, s8 nss_delta, s8 *max_power)
236 for (i = 0; i < pwr_len; i++) {
237 pwr[i] = min_t(s8, target_power,
238 be32_to_cpu(data[i]) + nss_delta);
239 *max_power = max(*max_power, pwr[i]);
244 mt76_apply_multi_array_limit(s8 *pwr, size_t pwr_len, s8 pwr_num,
245 const __be32 *data, size_t len, s8 target_power,
246 s8 nss_delta, s8 *max_power)
254 cur = be32_to_cpu(data[0]);
255 for (i = 0; i < pwr_num; i++) {
256 if (len < pwr_len + 1)
259 mt76_apply_array_limit(pwr + pwr_len * i, pwr_len, data + 1,
260 target_power, nss_delta, max_power);
269 cur = be32_to_cpu(data[0]);
273 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
274 struct ieee80211_channel *chan,
275 struct mt76_power_limits *dest,
278 struct mt76_dev *dev = phy->dev;
279 struct device_node *np;
282 u32 mcs_rates = dev->drv->mcs_rates;
283 u32 ru_rates = ARRAY_SIZE(dest->ru[0]);
292 memset(dest, target_power, sizeof(*dest));
294 if (!IS_ENABLED(CONFIG_OF))
297 np = mt76_find_power_limits_node(dev);
301 switch (chan->band) {
302 case NL80211_BAND_2GHZ:
305 case NL80211_BAND_5GHZ:
308 case NL80211_BAND_6GHZ:
315 snprintf(name, sizeof(name), "txpower-%cg", band);
316 np = of_get_child_by_name(np, name);
320 np = mt76_find_channel_node(np, chan);
324 txs_delta = mt76_get_txs_delta(np, hweight8(phy->antenna_mask));
326 val = mt76_get_of_array(np, "rates-cck", &len, ARRAY_SIZE(dest->cck));
327 mt76_apply_array_limit(dest->cck, ARRAY_SIZE(dest->cck), val,
328 target_power, txs_delta, &max_power);
330 val = mt76_get_of_array(np, "rates-ofdm",
331 &len, ARRAY_SIZE(dest->ofdm));
332 mt76_apply_array_limit(dest->ofdm, ARRAY_SIZE(dest->ofdm), val,
333 target_power, txs_delta, &max_power);
335 val = mt76_get_of_array(np, "rates-mcs", &len, mcs_rates + 1);
336 mt76_apply_multi_array_limit(dest->mcs[0], ARRAY_SIZE(dest->mcs[0]),
337 ARRAY_SIZE(dest->mcs), val, len,
338 target_power, txs_delta, &max_power);
340 val = mt76_get_of_array(np, "rates-ru", &len, ru_rates + 1);
341 mt76_apply_multi_array_limit(dest->ru[0], ARRAY_SIZE(dest->ru[0]),
342 ARRAY_SIZE(dest->ru), val, len,
343 target_power, txs_delta, &max_power);
347 EXPORT_SYMBOL_GPL(mt76_get_rate_power_limits);
350 mt76_eeprom_init(struct mt76_dev *dev, int len)
352 dev->eeprom.size = len;
353 dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL);
354 if (!dev->eeprom.data)
357 return !mt76_get_of_eeprom(dev, dev->eeprom.data, 0, len);
359 EXPORT_SYMBOL_GPL(mt76_eeprom_init);