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