1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/firmware.h> 5 #include "mt7915.h" 6 #include "eeprom.h" 7 8 static int mt7915_eeprom_load_precal(struct mt7915_dev *dev) 9 { 10 struct mt76_dev *mdev = &dev->mt76; 11 u8 *eeprom = mdev->eeprom.data; 12 u32 val = eeprom[MT_EE_DO_PRE_CAL]; 13 u32 offs; 14 int ret; 15 16 if (!dev->flash_mode) 17 return 0; 18 19 if (val != (MT_EE_WIFI_CAL_DPD | MT_EE_WIFI_CAL_GROUP)) 20 return 0; 21 22 val = MT_EE_CAL_GROUP_SIZE + MT_EE_CAL_DPD_SIZE; 23 dev->cal = devm_kzalloc(mdev->dev, val, GFP_KERNEL); 24 if (!dev->cal) 25 return -ENOMEM; 26 27 offs = is_mt7915(&dev->mt76) ? MT_EE_PRECAL : MT_EE_PRECAL_V2; 28 29 ret = mt76_get_of_data_from_mtd(mdev, dev->cal, offs, val); 30 if (!ret) 31 return ret; 32 33 return mt76_get_of_data_from_nvmem(mdev, dev->cal, "precal", val); 34 } 35 36 static int mt7915_check_eeprom(struct mt7915_dev *dev) 37 { 38 u8 *eeprom = dev->mt76.eeprom.data; 39 u16 val = get_unaligned_le16(eeprom); 40 41 #define CHECK_EEPROM_ERR(match) (match ? 0 : -EINVAL) 42 switch (val) { 43 case 0x7915: 44 return CHECK_EEPROM_ERR(is_mt7915(&dev->mt76)); 45 case 0x7916: 46 return CHECK_EEPROM_ERR(is_mt7916(&dev->mt76)); 47 case 0x7981: 48 return CHECK_EEPROM_ERR(is_mt7981(&dev->mt76)); 49 case 0x7986: 50 return CHECK_EEPROM_ERR(is_mt7986(&dev->mt76)); 51 default: 52 return -EINVAL; 53 } 54 } 55 56 static char *mt7915_eeprom_name(struct mt7915_dev *dev) 57 { 58 switch (mt76_chip(&dev->mt76)) { 59 case 0x7915: 60 return dev->dbdc_support ? 61 MT7915_EEPROM_DEFAULT_DBDC : MT7915_EEPROM_DEFAULT; 62 case 0x7981: 63 /* mt7981 only supports mt7976 and only in DBDC mode */ 64 return MT7981_EEPROM_MT7976_DEFAULT_DBDC; 65 case 0x7986: 66 switch (mt7915_check_adie(dev, true)) { 67 case MT7976_ONE_ADIE_DBDC: 68 return MT7986_EEPROM_MT7976_DEFAULT_DBDC; 69 case MT7975_ONE_ADIE: 70 return MT7986_EEPROM_MT7975_DEFAULT; 71 case MT7976_ONE_ADIE: 72 return MT7986_EEPROM_MT7976_DEFAULT; 73 case MT7975_DUAL_ADIE: 74 return MT7986_EEPROM_MT7975_DUAL_DEFAULT; 75 case MT7976_DUAL_ADIE: 76 return MT7986_EEPROM_MT7976_DUAL_DEFAULT; 77 default: 78 break; 79 } 80 return NULL; 81 default: 82 return MT7916_EEPROM_DEFAULT; 83 } 84 } 85 86 static int 87 mt7915_eeprom_load_default(struct mt7915_dev *dev) 88 { 89 u8 *eeprom = dev->mt76.eeprom.data; 90 const struct firmware *fw = NULL; 91 int ret; 92 93 ret = request_firmware(&fw, mt7915_eeprom_name(dev), dev->mt76.dev); 94 if (ret) 95 return ret; 96 97 if (!fw || !fw->data) { 98 dev_err(dev->mt76.dev, "Invalid default bin\n"); 99 ret = -EINVAL; 100 goto out; 101 } 102 103 memcpy(eeprom, fw->data, mt7915_eeprom_size(dev)); 104 dev->flash_mode = true; 105 106 out: 107 release_firmware(fw); 108 109 return ret; 110 } 111 112 static int mt7915_eeprom_load(struct mt7915_dev *dev) 113 { 114 int ret; 115 u16 eeprom_size = mt7915_eeprom_size(dev); 116 117 ret = mt76_eeprom_init(&dev->mt76, eeprom_size); 118 if (ret < 0) 119 return ret; 120 121 if (ret) { 122 dev->flash_mode = true; 123 } else { 124 u8 free_block_num; 125 u32 block_num, i; 126 u32 eeprom_blk_size = MT7915_EEPROM_BLOCK_SIZE; 127 128 ret = mt7915_mcu_get_eeprom_free_block(dev, &free_block_num); 129 if (ret < 0) 130 return ret; 131 132 /* efuse info isn't enough */ 133 if (free_block_num >= 29) 134 return -EINVAL; 135 136 /* read eeprom data from efuse */ 137 block_num = DIV_ROUND_UP(eeprom_size, eeprom_blk_size); 138 for (i = 0; i < block_num; i++) { 139 ret = mt7915_mcu_get_eeprom(dev, i * eeprom_blk_size); 140 if (ret < 0) 141 return ret; 142 } 143 } 144 145 return mt7915_check_eeprom(dev); 146 } 147 148 static void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy) 149 { 150 struct mt7915_dev *dev = phy->dev; 151 u8 *eeprom = dev->mt76.eeprom.data; 152 u8 band = phy->mt76->band_idx; 153 u32 val; 154 155 val = eeprom[MT_EE_WIFI_CONF + band]; 156 val = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val); 157 158 if (!is_mt7915(&dev->mt76)) { 159 switch (val) { 160 case MT_EE_V2_BAND_SEL_5GHZ: 161 phy->mt76->cap.has_5ghz = true; 162 return; 163 case MT_EE_V2_BAND_SEL_6GHZ: 164 phy->mt76->cap.has_6ghz = true; 165 return; 166 case MT_EE_V2_BAND_SEL_5GHZ_6GHZ: 167 phy->mt76->cap.has_5ghz = true; 168 phy->mt76->cap.has_6ghz = true; 169 return; 170 default: 171 phy->mt76->cap.has_2ghz = true; 172 return; 173 } 174 } else if (val == MT_EE_BAND_SEL_DEFAULT && dev->dbdc_support) { 175 val = band ? MT_EE_BAND_SEL_5GHZ : MT_EE_BAND_SEL_2GHZ; 176 } 177 178 switch (val) { 179 case MT_EE_BAND_SEL_5GHZ: 180 phy->mt76->cap.has_5ghz = true; 181 break; 182 case MT_EE_BAND_SEL_2GHZ: 183 phy->mt76->cap.has_2ghz = true; 184 break; 185 default: 186 phy->mt76->cap.has_2ghz = true; 187 phy->mt76->cap.has_5ghz = true; 188 break; 189 } 190 } 191 192 void mt7915_eeprom_parse_hw_cap(struct mt7915_dev *dev, 193 struct mt7915_phy *phy) 194 { 195 u8 path, nss, nss_max = 4, *eeprom = dev->mt76.eeprom.data; 196 struct mt76_phy *mphy = phy->mt76; 197 u8 band = phy->mt76->band_idx; 198 199 mt7915_eeprom_parse_band_config(phy); 200 201 /* read tx/rx path from eeprom */ 202 if (is_mt7915(&dev->mt76)) { 203 path = FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH, 204 eeprom[MT_EE_WIFI_CONF]); 205 } else { 206 path = FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH, 207 eeprom[MT_EE_WIFI_CONF + band]); 208 } 209 210 if (!path || path > 4) 211 path = 4; 212 213 /* read tx/rx stream */ 214 nss = path; 215 if (dev->dbdc_support) { 216 if (is_mt7915(&dev->mt76)) { 217 path = min_t(u8, path, 2); 218 nss = FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B0, 219 eeprom[MT_EE_WIFI_CONF + 3]); 220 if (band) 221 nss = FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B1, 222 eeprom[MT_EE_WIFI_CONF + 3]); 223 } else { 224 nss = FIELD_GET(MT_EE_WIFI_CONF_STREAM_NUM, 225 eeprom[MT_EE_WIFI_CONF + 2 + band]); 226 } 227 228 if (!is_mt798x(&dev->mt76)) 229 nss_max = 2; 230 } 231 232 if (!nss) 233 nss = nss_max; 234 nss = min_t(u8, min_t(u8, nss_max, nss), path); 235 236 mphy->chainmask = BIT(path) - 1; 237 if (band) 238 mphy->chainmask <<= dev->chainshift; 239 mphy->antenna_mask = BIT(nss) - 1; 240 dev->chainmask |= mphy->chainmask; 241 dev->chainshift = hweight8(dev->mphy.chainmask); 242 } 243 244 int mt7915_eeprom_init(struct mt7915_dev *dev) 245 { 246 int ret; 247 248 ret = mt7915_eeprom_load(dev); 249 if (ret < 0) { 250 if (ret != -EINVAL) 251 return ret; 252 253 dev_warn(dev->mt76.dev, "eeprom load fail, use default bin\n"); 254 ret = mt7915_eeprom_load_default(dev); 255 if (ret) 256 return ret; 257 } 258 259 ret = mt7915_eeprom_load_precal(dev); 260 if (ret) 261 return ret; 262 263 mt7915_eeprom_parse_hw_cap(dev, &dev->phy); 264 memcpy(dev->mphy.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, 265 ETH_ALEN); 266 267 mt76_eeprom_override(&dev->mphy); 268 269 return 0; 270 } 271 272 int mt7915_eeprom_get_target_power(struct mt7915_dev *dev, 273 struct ieee80211_channel *chan, 274 u8 chain_idx) 275 { 276 u8 *eeprom = dev->mt76.eeprom.data; 277 int index, target_power; 278 bool tssi_on, is_7976; 279 280 if (chain_idx > 3) 281 return -EINVAL; 282 283 tssi_on = mt7915_tssi_enabled(dev, chan->band); 284 is_7976 = mt7915_check_adie(dev, false) || is_mt7916(&dev->mt76); 285 286 if (chan->band == NL80211_BAND_2GHZ) { 287 if (is_7976) { 288 index = MT_EE_TX0_POWER_2G_V2 + chain_idx; 289 target_power = eeprom[index]; 290 } else { 291 index = MT_EE_TX0_POWER_2G + chain_idx * 3; 292 target_power = eeprom[index]; 293 294 if (!tssi_on) 295 target_power += eeprom[index + 1]; 296 } 297 } else if (chan->band == NL80211_BAND_5GHZ) { 298 int group = mt7915_get_channel_group_5g(chan->hw_value, is_7976); 299 300 if (is_7976) { 301 index = MT_EE_TX0_POWER_5G_V2 + chain_idx * 5; 302 target_power = eeprom[index + group]; 303 } else { 304 index = MT_EE_TX0_POWER_5G + chain_idx * 12; 305 target_power = eeprom[index + group]; 306 307 if (!tssi_on) 308 target_power += eeprom[index + 8]; 309 } 310 } else { 311 int group = mt7915_get_channel_group_6g(chan->hw_value); 312 313 index = MT_EE_TX0_POWER_6G_V2 + chain_idx * 8; 314 target_power = is_7976 ? eeprom[index + group] : 0; 315 } 316 317 return target_power; 318 } 319 320 s8 mt7915_eeprom_get_power_delta(struct mt7915_dev *dev, int band) 321 { 322 u8 *eeprom = dev->mt76.eeprom.data; 323 u32 val, offs; 324 s8 delta; 325 bool is_7976 = mt7915_check_adie(dev, false) || is_mt7916(&dev->mt76); 326 327 if (band == NL80211_BAND_2GHZ) 328 offs = is_7976 ? MT_EE_RATE_DELTA_2G_V2 : MT_EE_RATE_DELTA_2G; 329 else if (band == NL80211_BAND_5GHZ) 330 offs = is_7976 ? MT_EE_RATE_DELTA_5G_V2 : MT_EE_RATE_DELTA_5G; 331 else 332 offs = is_7976 ? MT_EE_RATE_DELTA_6G_V2 : 0; 333 334 val = eeprom[offs]; 335 336 if (!offs || !(val & MT_EE_RATE_DELTA_EN)) 337 return 0; 338 339 delta = FIELD_GET(MT_EE_RATE_DELTA_MASK, val); 340 341 return val & MT_EE_RATE_DELTA_SIGN ? delta : -delta; 342 } 343 344 const u8 mt7915_sku_group_len[] = { 345 [SKU_CCK] = 4, 346 [SKU_OFDM] = 8, 347 [SKU_HT_BW20] = 8, 348 [SKU_HT_BW40] = 9, 349 [SKU_VHT_BW20] = 12, 350 [SKU_VHT_BW40] = 12, 351 [SKU_VHT_BW80] = 12, 352 [SKU_VHT_BW160] = 12, 353 [SKU_HE_RU26] = 12, 354 [SKU_HE_RU52] = 12, 355 [SKU_HE_RU106] = 12, 356 [SKU_HE_RU242] = 12, 357 [SKU_HE_RU484] = 12, 358 [SKU_HE_RU996] = 12, 359 [SKU_HE_RU2x996] = 12 360 }; 361