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 static int mt76_get_of_epprom_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 109 static int mt76_get_of_epprom_from_nvmem(struct mt76_dev *dev, void *eep, int len) 110 { 111 struct device_node *np = dev->dev->of_node; 112 struct nvmem_cell *cell; 113 const void *data; 114 size_t retlen; 115 int ret = 0; 116 117 cell = of_nvmem_cell_get(np, "eeprom"); 118 if (IS_ERR(cell)) 119 return PTR_ERR(cell); 120 121 data = nvmem_cell_read(cell, &retlen); 122 nvmem_cell_put(cell); 123 124 if (IS_ERR(data)) 125 return PTR_ERR(data); 126 127 if (retlen < len) { 128 ret = -EINVAL; 129 goto exit; 130 } 131 132 memcpy(eep, data, len); 133 134 exit: 135 kfree(data); 136 137 return ret; 138 } 139 140 int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int offset, int len) 141 { 142 struct device_node *np = dev->dev->of_node; 143 int ret; 144 145 if (!np) 146 return -ENOENT; 147 148 ret = mt76_get_of_eeprom_data(dev, eep, len); 149 if (!ret) 150 return 0; 151 152 ret = mt76_get_of_epprom_from_mtd(dev, eep, offset, len); 153 if (!ret) 154 return 0; 155 156 return mt76_get_of_epprom_from_nvmem(dev, eep, len); 157 } 158 EXPORT_SYMBOL_GPL(mt76_get_of_eeprom); 159 160 void 161 mt76_eeprom_override(struct mt76_phy *phy) 162 { 163 struct mt76_dev *dev = phy->dev; 164 struct device_node *np = dev->dev->of_node; 165 166 of_get_mac_address(np, phy->macaddr); 167 168 if (!is_valid_ether_addr(phy->macaddr)) { 169 eth_random_addr(phy->macaddr); 170 dev_info(dev->dev, 171 "Invalid MAC address, using random address %pM\n", 172 phy->macaddr); 173 } 174 } 175 EXPORT_SYMBOL_GPL(mt76_eeprom_override); 176 177 static bool mt76_string_prop_find(struct property *prop, const char *str) 178 { 179 const char *cp = NULL; 180 181 if (!prop || !str || !str[0]) 182 return false; 183 184 while ((cp = of_prop_next_string(prop, cp)) != NULL) 185 if (!strcasecmp(cp, str)) 186 return true; 187 188 return false; 189 } 190 191 struct device_node * 192 mt76_find_power_limits_node(struct mt76_dev *dev) 193 { 194 struct device_node *np = dev->dev->of_node; 195 const char *const region_names[] = { 196 [NL80211_DFS_UNSET] = "ww", 197 [NL80211_DFS_ETSI] = "etsi", 198 [NL80211_DFS_FCC] = "fcc", 199 [NL80211_DFS_JP] = "jp", 200 }; 201 struct device_node *cur, *fallback = NULL; 202 const char *region_name = NULL; 203 204 if (dev->region < ARRAY_SIZE(region_names)) 205 region_name = region_names[dev->region]; 206 207 np = of_get_child_by_name(np, "power-limits"); 208 if (!np) 209 return NULL; 210 211 for_each_child_of_node(np, cur) { 212 struct property *country = of_find_property(cur, "country", NULL); 213 struct property *regd = of_find_property(cur, "regdomain", NULL); 214 215 if (!country && !regd) { 216 fallback = cur; 217 continue; 218 } 219 220 if (mt76_string_prop_find(country, dev->alpha2) || 221 mt76_string_prop_find(regd, region_name)) { 222 of_node_put(np); 223 return cur; 224 } 225 } 226 227 of_node_put(np); 228 return fallback; 229 } 230 EXPORT_SYMBOL_GPL(mt76_find_power_limits_node); 231 232 static const __be32 * 233 mt76_get_of_array(struct device_node *np, char *name, size_t *len, int min) 234 { 235 struct property *prop = of_find_property(np, name, NULL); 236 237 if (!prop || !prop->value || prop->length < min * 4) 238 return NULL; 239 240 *len = prop->length; 241 242 return prop->value; 243 } 244 245 struct device_node * 246 mt76_find_channel_node(struct device_node *np, struct ieee80211_channel *chan) 247 { 248 struct device_node *cur; 249 const __be32 *val; 250 size_t len; 251 252 for_each_child_of_node(np, cur) { 253 val = mt76_get_of_array(cur, "channels", &len, 2); 254 if (!val) 255 continue; 256 257 while (len >= 2 * sizeof(*val)) { 258 if (chan->hw_value >= be32_to_cpu(val[0]) && 259 chan->hw_value <= be32_to_cpu(val[1])) 260 return cur; 261 262 val += 2; 263 len -= 2 * sizeof(*val); 264 } 265 } 266 267 return NULL; 268 } 269 EXPORT_SYMBOL_GPL(mt76_find_channel_node); 270 271 272 static s8 273 mt76_get_txs_delta(struct device_node *np, u8 nss) 274 { 275 const __be32 *val; 276 size_t len; 277 278 val = mt76_get_of_array(np, "txs-delta", &len, nss); 279 if (!val) 280 return 0; 281 282 return be32_to_cpu(val[nss - 1]); 283 } 284 285 static void 286 mt76_apply_array_limit(s8 *pwr, size_t pwr_len, const __be32 *data, 287 s8 target_power, s8 nss_delta, s8 *max_power) 288 { 289 int i; 290 291 if (!data) 292 return; 293 294 for (i = 0; i < pwr_len; i++) { 295 pwr[i] = min_t(s8, target_power, 296 be32_to_cpu(data[i]) + nss_delta); 297 *max_power = max(*max_power, pwr[i]); 298 } 299 } 300 301 static void 302 mt76_apply_multi_array_limit(s8 *pwr, size_t pwr_len, s8 pwr_num, 303 const __be32 *data, size_t len, s8 target_power, 304 s8 nss_delta, s8 *max_power) 305 { 306 int i, cur; 307 308 if (!data) 309 return; 310 311 len /= 4; 312 cur = be32_to_cpu(data[0]); 313 for (i = 0; i < pwr_num; i++) { 314 if (len < pwr_len + 1) 315 break; 316 317 mt76_apply_array_limit(pwr + pwr_len * i, pwr_len, data + 1, 318 target_power, nss_delta, max_power); 319 if (--cur > 0) 320 continue; 321 322 data += pwr_len + 1; 323 len -= pwr_len + 1; 324 if (!len) 325 break; 326 327 cur = be32_to_cpu(data[0]); 328 } 329 } 330 331 s8 mt76_get_rate_power_limits(struct mt76_phy *phy, 332 struct ieee80211_channel *chan, 333 struct mt76_power_limits *dest, 334 s8 target_power) 335 { 336 struct mt76_dev *dev = phy->dev; 337 struct device_node *np; 338 const __be32 *val; 339 char name[16]; 340 u32 mcs_rates = dev->drv->mcs_rates; 341 u32 ru_rates = ARRAY_SIZE(dest->ru[0]); 342 char band; 343 size_t len; 344 s8 max_power = 0; 345 s8 txs_delta; 346 347 if (!mcs_rates) 348 mcs_rates = 10; 349 350 memset(dest, target_power, sizeof(*dest)); 351 352 if (!IS_ENABLED(CONFIG_OF)) 353 return target_power; 354 355 np = mt76_find_power_limits_node(dev); 356 if (!np) 357 return target_power; 358 359 switch (chan->band) { 360 case NL80211_BAND_2GHZ: 361 band = '2'; 362 break; 363 case NL80211_BAND_5GHZ: 364 band = '5'; 365 break; 366 case NL80211_BAND_6GHZ: 367 band = '6'; 368 break; 369 default: 370 return target_power; 371 } 372 373 snprintf(name, sizeof(name), "txpower-%cg", band); 374 np = of_get_child_by_name(np, name); 375 if (!np) 376 return target_power; 377 378 np = mt76_find_channel_node(np, chan); 379 if (!np) 380 return target_power; 381 382 txs_delta = mt76_get_txs_delta(np, hweight8(phy->antenna_mask)); 383 384 val = mt76_get_of_array(np, "rates-cck", &len, ARRAY_SIZE(dest->cck)); 385 mt76_apply_array_limit(dest->cck, ARRAY_SIZE(dest->cck), val, 386 target_power, txs_delta, &max_power); 387 388 val = mt76_get_of_array(np, "rates-ofdm", 389 &len, ARRAY_SIZE(dest->ofdm)); 390 mt76_apply_array_limit(dest->ofdm, ARRAY_SIZE(dest->ofdm), val, 391 target_power, txs_delta, &max_power); 392 393 val = mt76_get_of_array(np, "rates-mcs", &len, mcs_rates + 1); 394 mt76_apply_multi_array_limit(dest->mcs[0], ARRAY_SIZE(dest->mcs[0]), 395 ARRAY_SIZE(dest->mcs), val, len, 396 target_power, txs_delta, &max_power); 397 398 val = mt76_get_of_array(np, "rates-ru", &len, ru_rates + 1); 399 mt76_apply_multi_array_limit(dest->ru[0], ARRAY_SIZE(dest->ru[0]), 400 ARRAY_SIZE(dest->ru), val, len, 401 target_power, txs_delta, &max_power); 402 403 return max_power; 404 } 405 EXPORT_SYMBOL_GPL(mt76_get_rate_power_limits); 406 407 int 408 mt76_eeprom_init(struct mt76_dev *dev, int len) 409 { 410 dev->eeprom.size = len; 411 dev->eeprom.data = devm_kzalloc(dev->dev, len, GFP_KERNEL); 412 if (!dev->eeprom.data) 413 return -ENOMEM; 414 415 return !mt76_get_of_eeprom(dev, dev->eeprom.data, 0, len); 416 } 417 EXPORT_SYMBOL_GPL(mt76_eeprom_init); 418