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/nvmem-consumer.h>
10 #include <linux/etherdevice.h>
13 static int mt76_get_of_eeprom_data(struct mt76_dev *dev, void *eep, int len)
15 struct device_node *np = dev->dev->of_node;
19 data = of_get_property(np, "mediatek,eeprom-data", &size);
26 memcpy(eep, data, size);
31 int mt76_get_of_data_from_mtd(struct mt76_dev *dev, void *eep, int offset, int len)
34 struct device_node *np = dev->dev->of_node;
43 list = of_get_property(np, "mediatek,mtd-eeprom", &size);
47 phandle = be32_to_cpup(list++);
51 np = of_find_node_by_phandle(phandle);
55 part = of_get_property(np, "label", NULL);
59 mtd = get_mtd_device_nm(part);
65 if (size <= sizeof(*list)) {
70 offset += be32_to_cpup(list);
71 ret = mtd_read(mtd, offset, len, &retlen, eep);
73 if (mtd_is_bitflip(ret))
76 dev_err(dev->dev, "reading EEPROM from mtd %s failed: %i\n",
86 if (of_property_read_bool(dev->dev->of_node, "big-endian")) {
90 /* convert eeprom data in Little Endian */
91 for (i = 0; i < round_down(len, 2); i += 2)
92 put_unaligned_le16(get_unaligned_be16(&data[i]),
96 #ifdef CONFIG_NL80211_TESTMODE
97 dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
98 dev->test_mtd.offset = offset;
108 EXPORT_SYMBOL_GPL(mt76_get_of_data_from_mtd);
110 int mt76_get_of_data_from_nvmem(struct mt76_dev *dev, void *eep,
111 const char *cell_name, int len)
113 struct device_node *np = dev->dev->of_node;
114 struct nvmem_cell *cell;
119 cell = of_nvmem_cell_get(np, cell_name);
121 return PTR_ERR(cell);
123 data = nvmem_cell_read(cell, &retlen);
124 nvmem_cell_put(cell);
127 return PTR_ERR(data);
134 memcpy(eep, data, len);
141 EXPORT_SYMBOL_GPL(mt76_get_of_data_from_nvmem);
143 static int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int len)
145 struct device_node *np = dev->dev->of_node;
151 ret = mt76_get_of_eeprom_data(dev, eep, len);
155 ret = mt76_get_of_data_from_mtd(dev, eep, 0, len);
159 return mt76_get_of_data_from_nvmem(dev, eep, "eeprom", len);
163 mt76_eeprom_override(struct mt76_phy *phy)
165 struct mt76_dev *dev = phy->dev;
166 struct device_node *np = dev->dev->of_node;
168 of_get_mac_address(np, phy->macaddr);
170 if (!is_valid_ether_addr(phy->macaddr)) {
171 eth_random_addr(phy->macaddr);
173 "Invalid MAC address, using random address %pM\n",
177 EXPORT_SYMBOL_GPL(mt76_eeprom_override);
179 static bool mt76_string_prop_find(struct property *prop, const char *str)
181 const char *cp = NULL;
183 if (!prop || !str || !str[0])
186 while ((cp = of_prop_next_string(prop, cp)) != NULL)
187 if (!strcasecmp(cp, str))
194 mt76_find_power_limits_node(struct mt76_dev *dev)
196 struct device_node *np = dev->dev->of_node;
197 const char *const region_names[] = {
198 [NL80211_DFS_UNSET] = "ww",
199 [NL80211_DFS_ETSI] = "etsi",
200 [NL80211_DFS_FCC] = "fcc",
201 [NL80211_DFS_JP] = "jp",
203 struct device_node *cur, *fallback = NULL;
204 const char *region_name = NULL;
206 if (dev->region < ARRAY_SIZE(region_names))
207 region_name = region_names[dev->region];
209 np = of_get_child_by_name(np, "power-limits");
213 for_each_child_of_node(np, cur) {
214 struct property *country = of_find_property(cur, "country", NULL);
215 struct property *regd = of_find_property(cur, "regdomain", NULL);
217 if (!country && !regd) {
222 if (mt76_string_prop_find(country, dev->alpha2) ||
223 mt76_string_prop_find(regd, region_name)) {
232 EXPORT_SYMBOL_GPL(mt76_find_power_limits_node);
234 static const __be32 *
235 mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min)
237 struct property *prop = of_find_property(np, name, NULL);
239 if (!prop || !prop->value || prop->length < min * 4)
248 mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan)
250 struct device_node *cur;
254 for_each_child_of_node(np, cur) {
255 val = mt76_get_of_array(cur, "channels", &len, 2);
259 while (len >= 2 * sizeof(*val)) {
260 if (chan->hw_value >= be32_to_cpu(val[0]) &&
261 chan->hw_value <= be32_to_cpu(val[1]))
265 len -= 2 * sizeof(*val);
271 EXPORT_SYMBOL_GPL(mt76_find_channel_node);
275 mt76_get_txs_delta(struct device_node *np, u8 nss)
280 val = mt76_get_of_array(np, "txs-delta", &len, nss);
284 return be32_to_cpu(val[nss - 1]);
288 mt76_apply_array_limit(s8 *pwr, size_t pwr_len, const __be32 *data,
289 s8 target_power, s8 nss_delta, s8 *max_power)
296 for (i = 0; i < pwr_len; i++) {
297 pwr[i] = min_t(s8, target_power,
298 be32_to_cpu(data[i]) + nss_delta);
299 *max_power = max(*max_power, pwr[i]);
304 mt76_apply_multi_array_limit(s8 *pwr, size_t pwr_len, s8 pwr_num,
305 const __be32 *data, size_t len, s8 target_power,
306 s8 nss_delta, s8 *max_power)
314 cur = be32_to_cpu(data[0]);
315 for (i = 0; i < pwr_num; i++) {
316 if (len < pwr_len + 1)
319 mt76_apply_array_limit(pwr + pwr_len * i, pwr_len, data + 1,
320 target_power, nss_delta, max_power);
329 cur = be32_to_cpu(data[0]);
333 s8 mt76_get_rate_power_limits(struct mt76_phy *phy,
334 struct ieee80211_channel *chan,
335 struct mt76_power_limits *dest,
338 struct mt76_dev *dev = phy->dev;
339 struct device_node *np;
342 u32 mcs_rates = dev->drv->mcs_rates;
343 u32 ru_rates = ARRAY_SIZE(dest->ru[0]);
352 memset(dest, target_power, sizeof(*dest));
354 if (!IS_ENABLED(CONFIG_OF))
357 np = mt76_find_power_limits_node(dev);
361 switch (chan->band) {
362 case NL80211_BAND_2GHZ:
365 case NL80211_BAND_5GHZ:
368 case NL80211_BAND_6GHZ:
375 snprintf(name, sizeof(name), "txpower-%cg", band);
376 np = of_get_child_by_name(np, name);
380 np = mt76_find_channel_node(np, chan);
384 txs_delta = mt76_get_txs_delta(np, hweight16(phy->chainmask));
386 val = mt76_get_of_array(np, "rates-cck", &len, ARRAY_SIZE(dest->cck));
387 mt76_apply_array_limit(dest->cck, ARRAY_SIZE(dest->cck), val,
388 target_power, txs_delta, &max_power);
390 val = mt76_get_of_array(np, "rates-ofdm",
391 &len, ARRAY_SIZE(dest->ofdm));
392 mt76_apply_array_limit(dest->ofdm, ARRAY_SIZE(dest->ofdm), val,
393 target_power, txs_delta, &max_power);
395 val = mt76_get_of_array(np, "rates-mcs", &len, mcs_rates + 1);
396 mt76_apply_multi_array_limit(dest->mcs[0], ARRAY_SIZE(dest->mcs[0]),
397 ARRAY_SIZE(dest->mcs), val, len,
398 target_power, txs_delta, &max_power);
400 val = mt76_get_of_array(np, "rates-ru", &len, ru_rates + 1);
401 mt76_apply_multi_array_limit(dest->ru[0], ARRAY_SIZE(dest->ru[0]),
402 ARRAY_SIZE(dest->ru), val, len,
403 target_power, txs_delta, &max_power);
407 EXPORT_SYMBOL_GPL(mt76_get_rate_power_limits);
410 mt76_eeprom_init(struct mt76_dev *dev, int len)
412 dev->eeprom.size = len;
413 dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL);
414 if (!dev->eeprom.data)
417 return !mt76_get_of_eeprom(dev, dev->eeprom.data, len);
419 EXPORT_SYMBOL_GPL(mt76_eeprom_init);