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/nvmem-consumer.h> 10 #include <linux/etherdevice.h> 11 #include "mt76.h" 12 13 static int mt76_get_of_eeprom_data(struct mt76_dev *dev, void *eep, int len) 14 { 15 struct device_node *np = dev->dev->of_node; 16 const void *data; 17 int size; 18 19 data = of_get_property(np, "mediatek,eeprom-data", &size); 20 if (!data) 21 return -ENOENT; 22 23 if (size > len) 24 return -EINVAL; 25 26 memcpy(eep, data, size); 27 28 return 0; 29 } 30 31 int mt76_get_of_data_from_mtd(struct mt76_dev *dev, void *eep, int offset, int len) 32 { 33 #ifdef CONFIG_MTD 34 struct device_node *np = dev->dev->of_node; 35 struct mtd_info *mtd; 36 const __be32 *list; 37 const char *part; 38 phandle phandle; 39 size_t retlen; 40 int size; 41 int ret; 42 43 list = of_get_property(np, "mediatek,mtd-eeprom", &size); 44 if (!list) 45 return -ENOENT; 46 47 phandle = be32_to_cpup(list++); 48 if (!phandle) 49 return -ENOENT; 50 51 np = of_find_node_by_phandle(phandle); 52 if (!np) 53 return -EINVAL; 54 55 part = of_get_property(np, "label", NULL); 56 if (!part) 57 part = np->name; 58 59 mtd = get_mtd_device_nm(part); 60 if (IS_ERR(mtd)) { 61 ret = PTR_ERR(mtd); 62 goto out_put_node; 63 } 64 65 if (size <= sizeof(*list)) { 66 ret = -EINVAL; 67 goto out_put_node; 68 } 69 70 offset += be32_to_cpup(list); 71 ret = mtd_read(mtd, offset, len, &retlen, eep); 72 put_mtd_device(mtd); 73 if (mtd_is_bitflip(ret)) 74 ret = 0; 75 if (ret) { 76 dev_err(dev->dev, "reading EEPROM from mtd %s failed: %i\n", 77 part, ret); 78 goto out_put_node; 79 } 80 81 if (retlen < len) { 82 ret = -EINVAL; 83 goto out_put_node; 84 } 85 86 if (of_property_read_bool(dev->dev->of_node, "big-endian")) { 87 u8 *data = (u8 *)eep; 88 int i; 89 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]), 93 &data[i]); 94 } 95 96 #ifdef CONFIG_NL80211_TESTMODE 97 dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL); 98 dev->test_mtd.offset = offset; 99 #endif 100 101 out_put_node: 102 of_node_put(np); 103 return ret; 104 #else 105 return -ENOENT; 106 #endif 107 } 108 EXPORT_SYMBOL_GPL(mt76_get_of_data_from_mtd); 109 110 int mt76_get_of_data_from_nvmem(struct mt76_dev *dev, void *eep, 111 const char *cell_name, int len) 112 { 113 struct device_node *np = dev->dev->of_node; 114 struct nvmem_cell *cell; 115 const void *data; 116 size_t retlen; 117 int ret = 0; 118 119 cell = of_nvmem_cell_get(np, cell_name); 120 if (IS_ERR(cell)) 121 return PTR_ERR(cell); 122 123 data = nvmem_cell_read(cell, &retlen); 124 nvmem_cell_put(cell); 125 126 if (IS_ERR(data)) 127 return PTR_ERR(data); 128 129 if (retlen < len) { 130 ret = -EINVAL; 131 goto exit; 132 } 133 134 memcpy(eep, data, len); 135 136 exit: 137 kfree(data); 138 139 return ret; 140 } 141 EXPORT_SYMBOL_GPL(mt76_get_of_data_from_nvmem); 142 143 static int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int len) 144 { 145 struct device_node *np = dev->dev->of_node; 146 int ret; 147 148 if (!np) 149 return -ENOENT; 150 151 ret = mt76_get_of_eeprom_data(dev, eep, len); 152 if (!ret) 153 return 0; 154 155 ret = mt76_get_of_data_from_mtd(dev, eep, 0, len); 156 if (!ret) 157 return 0; 158 159 return mt76_get_of_data_from_nvmem(dev, eep, "eeprom", len); 160 } 161 162 void 163 mt76_eeprom_override(struct mt76_phy *phy) 164 { 165 struct mt76_dev *dev = phy->dev; 166 struct device_node *np = dev->dev->of_node; 167 168 of_get_mac_address(np, phy->macaddr); 169 170 if (!is_valid_ether_addr(phy->macaddr)) { 171 eth_random_addr(phy->macaddr); 172 dev_info(dev->dev, 173 "Invalid MAC address, using random address %pM\n", 174 phy->macaddr); 175 } 176 } 177 EXPORT_SYMBOL_GPL(mt76_eeprom_override); 178 179 static bool mt76_string_prop_find(struct property *prop, const char *str) 180 { 181 const char *cp = NULL; 182 183 if (!prop || !str || !str[0]) 184 return false; 185 186 while ((cp = of_prop_next_string(prop, cp)) != NULL) 187 if (!strcasecmp(cp, str)) 188 return true; 189 190 return false; 191 } 192 193 struct device_node * 194 mt76_find_power_limits_node(struct mt76_dev *dev) 195 { 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", 202 }; 203 struct device_node *cur, *fallback = NULL; 204 const char *region_name = NULL; 205 206 if (dev->region < ARRAY_SIZE(region_names)) 207 region_name = region_names[dev->region]; 208 209 np = of_get_child_by_name(np, "power-limits"); 210 if (!np) 211 return NULL; 212 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); 216 217 if (!country && !regd) { 218 fallback = cur; 219 continue; 220 } 221 222 if (mt76_string_prop_find(country, dev->alpha2) || 223 mt76_string_prop_find(regd, region_name)) { 224 of_node_put(np); 225 return cur; 226 } 227 } 228 229 of_node_put(np); 230 return fallback; 231 } 232 EXPORT_SYMBOL_GPL(mt76_find_power_limits_node); 233 234 static const __be32 * 235 mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min) 236 { 237 struct property *prop = of_find_property(np, name, NULL); 238 239 if (!prop || !prop->value || prop->length < min * 4) 240 return NULL; 241 242 *len = prop->length; 243 244 return prop->value; 245 } 246 247 struct device_node * 248 mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan) 249 { 250 struct device_node *cur; 251 const __be32 *val; 252 size_t len; 253 254 for_each_child_of_node(np, cur) { 255 val = mt76_get_of_array(cur, "channels", &len, 2); 256 if (!val) 257 continue; 258 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])) 262 return cur; 263 264 val += 2; 265 len -= 2 * sizeof(*val); 266 } 267 } 268 269 return NULL; 270 } 271 EXPORT_SYMBOL_GPL(mt76_find_channel_node); 272 273 274 static s8 275 mt76_get_txs_delta(struct device_node *np, u8 nss) 276 { 277 const __be32 *val; 278 size_t len; 279 280 val = mt76_get_of_array(np, "txs-delta", &len, nss); 281 if (!val) 282 return 0; 283 284 return be32_to_cpu(val[nss - 1]); 285 } 286 287 static void 288 mt76_apply_array_limit(s8 *pwr, size_t pwr_len, const __be32 *data, 289 s8 target_power, s8 nss_delta, s8 *max_power) 290 { 291 int i; 292 293 if (!data) 294 return; 295 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]); 300 } 301 } 302 303 static void 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) 307 { 308 int i, cur; 309 310 if (!data) 311 return; 312 313 len /= 4; 314 cur = be32_to_cpu(data[0]); 315 for (i = 0; i < pwr_num; i++) { 316 if (len < pwr_len + 1) 317 break; 318 319 mt76_apply_array_limit(pwr + pwr_len * i, pwr_len, data + 1, 320 target_power, nss_delta, max_power); 321 if (--cur > 0) 322 continue; 323 324 data += pwr_len + 1; 325 len -= pwr_len + 1; 326 if (!len) 327 break; 328 329 cur = be32_to_cpu(data[0]); 330 } 331 } 332 333 s8 mt76_get_rate_power_limits(struct mt76_phy *phy, 334 struct ieee80211_channel *chan, 335 struct mt76_power_limits *dest, 336 s8 target_power) 337 { 338 struct mt76_dev *dev = phy->dev; 339 struct device_node *np; 340 const __be32 *val; 341 char name[16]; 342 u32 mcs_rates = dev->drv->mcs_rates; 343 u32 ru_rates = ARRAY_SIZE(dest->ru[0]); 344 char band; 345 size_t len; 346 s8 max_power = 0; 347 s8 txs_delta; 348 349 if (!mcs_rates) 350 mcs_rates = 10; 351 352 memset(dest, target_power, sizeof(*dest)); 353 354 if (!IS_ENABLED(CONFIG_OF)) 355 return target_power; 356 357 np = mt76_find_power_limits_node(dev); 358 if (!np) 359 return target_power; 360 361 switch (chan->band) { 362 case NL80211_BAND_2GHZ: 363 band = '2'; 364 break; 365 case NL80211_BAND_5GHZ: 366 band = '5'; 367 break; 368 case NL80211_BAND_6GHZ: 369 band = '6'; 370 break; 371 default: 372 return target_power; 373 } 374 375 snprintf(name, sizeof(name), "txpower-%cg", band); 376 np = of_get_child_by_name(np, name); 377 if (!np) 378 return target_power; 379 380 np = mt76_find_channel_node(np, chan); 381 if (!np) 382 return target_power; 383 384 txs_delta = mt76_get_txs_delta(np, hweight16(phy->chainmask)); 385 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); 389 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); 394 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); 399 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); 404 405 return max_power; 406 } 407 EXPORT_SYMBOL_GPL(mt76_get_rate_power_limits); 408 409 int 410 mt76_eeprom_init(struct mt76_dev *dev, int len) 411 { 412 dev->eeprom.size = len; 413 dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL); 414 if (!dev->eeprom.data) 415 return -ENOMEM; 416 417 return !mt76_get_of_eeprom(dev, dev->eeprom.data, len); 418 } 419 EXPORT_SYMBOL_GPL(mt76_eeprom_init); 420