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