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