1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Ryder Lee <ryder.lee@mediatek.com> 5 * Felix Fietkau <nbd@nbd.name> 6 */ 7 8 #include <linux/of.h> 9 #include "mt7615.h" 10 #include "eeprom.h" 11 12 static int mt7615_efuse_read(struct mt7615_dev *dev, u32 base, 13 u16 addr, u8 *data) 14 { 15 u32 val; 16 int i; 17 18 val = mt76_rr(dev, base + MT_EFUSE_CTRL); 19 val &= ~(MT_EFUSE_CTRL_AIN | MT_EFUSE_CTRL_MODE); 20 val |= FIELD_PREP(MT_EFUSE_CTRL_AIN, addr & ~0xf); 21 val |= MT_EFUSE_CTRL_KICK; 22 mt76_wr(dev, base + MT_EFUSE_CTRL, val); 23 24 if (!mt76_poll(dev, base + MT_EFUSE_CTRL, MT_EFUSE_CTRL_KICK, 0, 1000)) 25 return -ETIMEDOUT; 26 27 udelay(2); 28 29 val = mt76_rr(dev, base + MT_EFUSE_CTRL); 30 if ((val & MT_EFUSE_CTRL_AOUT) == MT_EFUSE_CTRL_AOUT || 31 WARN_ON_ONCE(!(val & MT_EFUSE_CTRL_VALID))) { 32 memset(data, 0x0, 16); 33 return 0; 34 } 35 36 for (i = 0; i < 4; i++) { 37 val = mt76_rr(dev, base + MT_EFUSE_RDATA(i)); 38 put_unaligned_le32(val, data + 4 * i); 39 } 40 41 return 0; 42 } 43 44 static int mt7615_efuse_init(struct mt7615_dev *dev, u32 base) 45 { 46 int i, len = MT7615_EEPROM_SIZE; 47 void *buf; 48 u32 val; 49 50 val = mt76_rr(dev, base + MT_EFUSE_BASE_CTRL); 51 if (val & MT_EFUSE_BASE_CTRL_EMPTY) 52 return 0; 53 54 dev->mt76.otp.data = devm_kzalloc(dev->mt76.dev, len, GFP_KERNEL); 55 dev->mt76.otp.size = len; 56 if (!dev->mt76.otp.data) 57 return -ENOMEM; 58 59 buf = dev->mt76.otp.data; 60 for (i = 0; i + 16 <= len; i += 16) { 61 int ret; 62 63 ret = mt7615_efuse_read(dev, base, i, buf + i); 64 if (ret) 65 return ret; 66 } 67 68 return 0; 69 } 70 71 static int mt7615_eeprom_load(struct mt7615_dev *dev, u32 addr) 72 { 73 int ret; 74 75 ret = mt76_eeprom_init(&dev->mt76, MT7615_EEPROM_SIZE + 76 MT7615_EEPROM_EXTRA_DATA); 77 if (ret < 0) 78 return ret; 79 80 return mt7615_efuse_init(dev, addr); 81 } 82 83 static int mt7615_check_eeprom(struct mt76_dev *dev) 84 { 85 u16 val = get_unaligned_le16(dev->eeprom.data); 86 87 switch (val) { 88 case 0x7615: 89 case 0x7622: 90 return 0; 91 default: 92 return -EINVAL; 93 } 94 } 95 96 static void 97 mt7615_eeprom_parse_hw_band_cap(struct mt7615_dev *dev) 98 { 99 u8 val, *eeprom = dev->mt76.eeprom.data; 100 101 if (is_mt7663(&dev->mt76)) { 102 /* dual band */ 103 dev->mt76.cap.has_2ghz = true; 104 dev->mt76.cap.has_5ghz = true; 105 return; 106 } 107 108 if (is_mt7622(&dev->mt76)) { 109 /* 2GHz only */ 110 dev->mt76.cap.has_2ghz = true; 111 return; 112 } 113 114 if (is_mt7611(&dev->mt76)) { 115 /* 5GHz only */ 116 dev->mt76.cap.has_5ghz = true; 117 return; 118 } 119 120 val = FIELD_GET(MT_EE_NIC_WIFI_CONF_BAND_SEL, 121 eeprom[MT_EE_WIFI_CONF]); 122 switch (val) { 123 case MT_EE_5GHZ: 124 dev->mt76.cap.has_5ghz = true; 125 break; 126 case MT_EE_2GHZ: 127 dev->mt76.cap.has_2ghz = true; 128 break; 129 default: 130 dev->mt76.cap.has_2ghz = true; 131 dev->mt76.cap.has_5ghz = true; 132 break; 133 } 134 } 135 136 static void mt7615_eeprom_parse_hw_cap(struct mt7615_dev *dev) 137 { 138 u8 *eeprom = dev->mt76.eeprom.data; 139 u8 tx_mask, max_nss; 140 141 mt7615_eeprom_parse_hw_band_cap(dev); 142 143 if (is_mt7663(&dev->mt76)) { 144 max_nss = 2; 145 tx_mask = FIELD_GET(MT_EE_HW_CONF1_TX_MASK, 146 eeprom[MT7663_EE_HW_CONF1]); 147 } else { 148 u32 val; 149 150 /* read tx-rx mask from eeprom */ 151 val = mt76_rr(dev, MT_TOP_STRAP_STA); 152 max_nss = val & MT_TOP_3NSS ? 3 : 4; 153 154 tx_mask = FIELD_GET(MT_EE_NIC_CONF_TX_MASK, 155 eeprom[MT_EE_NIC_CONF_0]); 156 } 157 if (!tx_mask || tx_mask > max_nss) 158 tx_mask = max_nss; 159 160 dev->chainmask = BIT(tx_mask) - 1; 161 dev->mphy.antenna_mask = dev->chainmask; 162 dev->phy.chainmask = dev->chainmask; 163 } 164 165 static int mt7663_eeprom_get_target_power_index(struct mt7615_dev *dev, 166 struct ieee80211_channel *chan, 167 u8 chain_idx) 168 { 169 int index, group; 170 171 if (chain_idx > 1) 172 return -EINVAL; 173 174 if (chan->band == NL80211_BAND_2GHZ) 175 return MT7663_EE_TX0_2G_TARGET_POWER + (chain_idx << 4); 176 177 group = mt7615_get_channel_group(chan->hw_value); 178 if (chain_idx == 1) 179 index = MT7663_EE_TX1_5G_G0_TARGET_POWER; 180 else 181 index = MT7663_EE_TX0_5G_G0_TARGET_POWER; 182 183 return index + group * 3; 184 } 185 186 int mt7615_eeprom_get_target_power_index(struct mt7615_dev *dev, 187 struct ieee80211_channel *chan, 188 u8 chain_idx) 189 { 190 int index; 191 192 if (is_mt7663(&dev->mt76)) 193 return mt7663_eeprom_get_target_power_index(dev, chan, 194 chain_idx); 195 196 if (chain_idx > 3) 197 return -EINVAL; 198 199 /* TSSI disabled */ 200 if (mt7615_ext_pa_enabled(dev, chan->band)) { 201 if (chan->band == NL80211_BAND_2GHZ) 202 return MT_EE_EXT_PA_2G_TARGET_POWER; 203 else 204 return MT_EE_EXT_PA_5G_TARGET_POWER; 205 } 206 207 /* TSSI enabled */ 208 if (chan->band == NL80211_BAND_2GHZ) { 209 index = MT_EE_TX0_2G_TARGET_POWER + chain_idx * 6; 210 } else { 211 int group = mt7615_get_channel_group(chan->hw_value); 212 213 switch (chain_idx) { 214 case 1: 215 index = MT_EE_TX1_5G_G0_TARGET_POWER; 216 break; 217 case 2: 218 index = MT_EE_TX2_5G_G0_TARGET_POWER; 219 break; 220 case 3: 221 index = MT_EE_TX3_5G_G0_TARGET_POWER; 222 break; 223 case 0: 224 default: 225 index = MT_EE_TX0_5G_G0_TARGET_POWER; 226 break; 227 } 228 index += 5 * group; 229 } 230 231 return index; 232 } 233 234 int mt7615_eeprom_get_power_delta_index(struct mt7615_dev *dev, 235 enum nl80211_band band) 236 { 237 /* assume the first rate has the highest power offset */ 238 if (is_mt7663(&dev->mt76)) { 239 if (band == NL80211_BAND_2GHZ) 240 return MT_EE_TX0_5G_G0_TARGET_POWER; 241 else 242 return MT7663_EE_5G_RATE_POWER; 243 } 244 245 if (band == NL80211_BAND_2GHZ) 246 return MT_EE_2G_RATE_POWER; 247 else 248 return MT_EE_5G_RATE_POWER; 249 } 250 251 static void mt7615_apply_cal_free_data(struct mt7615_dev *dev) 252 { 253 static const u16 ical[] = { 254 0x53, 0x54, 0x55, 0x56, 0x57, 0x5c, 0x5d, 0x62, 0x63, 0x68, 255 0x69, 0x6e, 0x6f, 0x73, 0x74, 0x78, 0x79, 0x82, 0x83, 0x87, 256 0x88, 0x8c, 0x8d, 0x91, 0x92, 0x96, 0x97, 0x9b, 0x9c, 0xa0, 257 0xa1, 0xaa, 0xab, 0xaf, 0xb0, 0xb4, 0xb5, 0xb9, 0xba, 0xf4, 258 0xf7, 0xff, 259 0x140, 0x141, 0x145, 0x146, 0x14a, 0x14b, 0x154, 0x155, 0x159, 260 0x15a, 0x15e, 0x15f, 0x163, 0x164, 0x168, 0x169, 0x16d, 0x16e, 261 0x172, 0x173, 0x17c, 0x17d, 0x181, 0x182, 0x186, 0x187, 0x18b, 262 0x18c 263 }; 264 static const u16 ical_nocheck[] = { 265 0x110, 0x111, 0x112, 0x113, 0x114, 0x115, 0x116, 0x117, 0x118, 266 0x1b5, 0x1b6, 0x1b7, 0x3ac, 0x3ad, 0x3ae, 0x3af, 0x3b0, 0x3b1, 267 0x3b2 268 }; 269 u8 *eeprom = dev->mt76.eeprom.data; 270 u8 *otp = dev->mt76.otp.data; 271 int i; 272 273 if (!otp) 274 return; 275 276 for (i = 0; i < ARRAY_SIZE(ical); i++) 277 if (!otp[ical[i]]) 278 return; 279 280 for (i = 0; i < ARRAY_SIZE(ical); i++) 281 eeprom[ical[i]] = otp[ical[i]]; 282 283 for (i = 0; i < ARRAY_SIZE(ical_nocheck); i++) 284 eeprom[ical_nocheck[i]] = otp[ical_nocheck[i]]; 285 } 286 287 static void mt7622_apply_cal_free_data(struct mt7615_dev *dev) 288 { 289 static const u16 ical[] = { 290 0x53, 0x54, 0x55, 0x56, 0xf4, 0xf7, 0x144, 0x156, 0x15b 291 }; 292 u8 *eeprom = dev->mt76.eeprom.data; 293 u8 *otp = dev->mt76.otp.data; 294 int i; 295 296 if (!otp) 297 return; 298 299 for (i = 0; i < ARRAY_SIZE(ical); i++) { 300 if (!otp[ical[i]]) 301 continue; 302 303 eeprom[ical[i]] = otp[ical[i]]; 304 } 305 } 306 307 static void mt7615_cal_free_data(struct mt7615_dev *dev) 308 { 309 struct device_node *np = dev->mt76.dev->of_node; 310 311 if (!np || !of_property_read_bool(np, "mediatek,eeprom-merge-otp")) 312 return; 313 314 switch (mt76_chip(&dev->mt76)) { 315 case 0x7622: 316 mt7622_apply_cal_free_data(dev); 317 break; 318 case 0x7615: 319 case 0x7611: 320 mt7615_apply_cal_free_data(dev); 321 break; 322 } 323 } 324 325 int mt7615_eeprom_init(struct mt7615_dev *dev, u32 addr) 326 { 327 int ret; 328 329 ret = mt7615_eeprom_load(dev, addr); 330 if (ret < 0) 331 return ret; 332 333 ret = mt7615_check_eeprom(&dev->mt76); 334 if (ret && dev->mt76.otp.data) { 335 memcpy(dev->mt76.eeprom.data, dev->mt76.otp.data, 336 MT7615_EEPROM_SIZE); 337 } else { 338 dev->flash_eeprom = true; 339 mt7615_cal_free_data(dev); 340 } 341 342 mt7615_eeprom_parse_hw_cap(dev); 343 memcpy(dev->mt76.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, 344 ETH_ALEN); 345 346 mt76_eeprom_override(&dev->mt76); 347 348 return 0; 349 } 350 EXPORT_SYMBOL_GPL(mt7615_eeprom_init); 351