1e57b7901SRyder Lee // SPDX-License-Identifier: ISC
2e57b7901SRyder Lee /* Copyright (C) 2020 MediaTek Inc. */
3e57b7901SRyder Lee 
4bbc1d415SShayne Chen #include <linux/firmware.h>
5e57b7901SRyder Lee #include "mt7915.h"
6e57b7901SRyder Lee #include "eeprom.h"
7e57b7901SRyder Lee 
8495184acSRyder Lee static int mt7915_eeprom_load_precal(struct mt7915_dev *dev)
9495184acSRyder Lee {
10495184acSRyder Lee 	struct mt76_dev *mdev = &dev->mt76;
1183d229d2SShayne Chen 	u8 *eeprom = mdev->eeprom.data;
1283d229d2SShayne Chen 	u32 val = eeprom[MT_EE_DO_PRE_CAL];
1365dec62fSBo Jiao 	u32 offs;
14495184acSRyder Lee 
15bbc1d415SShayne Chen 	if (!dev->flash_mode)
16bbc1d415SShayne Chen 		return 0;
17bbc1d415SShayne Chen 
18495184acSRyder Lee 	if (val != (MT_EE_WIFI_CAL_DPD | MT_EE_WIFI_CAL_GROUP))
19495184acSRyder Lee 		return 0;
20495184acSRyder Lee 
21495184acSRyder Lee 	val = MT_EE_CAL_GROUP_SIZE + MT_EE_CAL_DPD_SIZE;
22495184acSRyder Lee 	dev->cal = devm_kzalloc(mdev->dev, val, GFP_KERNEL);
23495184acSRyder Lee 	if (!dev->cal)
24495184acSRyder Lee 		return -ENOMEM;
25495184acSRyder Lee 
2665dec62fSBo Jiao 	offs = is_mt7915(&dev->mt76) ? MT_EE_PRECAL : MT_EE_PRECAL_V2;
2765dec62fSBo Jiao 
2865dec62fSBo Jiao 	return mt76_get_of_eeprom(mdev, dev->cal, offs, val);
29495184acSRyder Lee }
30495184acSRyder Lee 
31e57b7901SRyder Lee static int mt7915_check_eeprom(struct mt7915_dev *dev)
32e57b7901SRyder Lee {
33e57b7901SRyder Lee 	u8 *eeprom = dev->mt76.eeprom.data;
3483d229d2SShayne Chen 	u16 val = get_unaligned_le16(eeprom);
35e57b7901SRyder Lee 
36e57b7901SRyder Lee 	switch (val) {
37e57b7901SRyder Lee 	case 0x7915:
38beed8beaSBo Jiao 	case 0x7916:
3999ad32a4SBo Jiao 	case 0x7986:
40e57b7901SRyder Lee 		return 0;
41e57b7901SRyder Lee 	default:
42e57b7901SRyder Lee 		return -EINVAL;
43e57b7901SRyder Lee 	}
44e57b7901SRyder Lee }
45e57b7901SRyder Lee 
4699ad32a4SBo Jiao static char *mt7915_eeprom_name(struct mt7915_dev *dev)
4799ad32a4SBo Jiao {
4899ad32a4SBo Jiao 	switch (mt76_chip(&dev->mt76)) {
4999ad32a4SBo Jiao 	case 0x7915:
5099ad32a4SBo Jiao 		return dev->dbdc_support ?
5199ad32a4SBo Jiao 		       MT7915_EEPROM_DEFAULT_DBDC : MT7915_EEPROM_DEFAULT;
5299ad32a4SBo Jiao 	case 0x7986:
5399ad32a4SBo Jiao 		switch (mt7915_check_adie(dev, true)) {
5499ad32a4SBo Jiao 		case MT7976_ONE_ADIE_DBDC:
5599ad32a4SBo Jiao 			return MT7986_EEPROM_MT7976_DEFAULT_DBDC;
5699ad32a4SBo Jiao 		case MT7975_ONE_ADIE:
5799ad32a4SBo Jiao 			return MT7986_EEPROM_MT7975_DEFAULT;
5899ad32a4SBo Jiao 		case MT7976_ONE_ADIE:
5999ad32a4SBo Jiao 			return MT7986_EEPROM_MT7976_DEFAULT;
6099ad32a4SBo Jiao 		case MT7975_DUAL_ADIE:
6199ad32a4SBo Jiao 			return MT7986_EEPROM_MT7975_DUAL_DEFAULT;
6299ad32a4SBo Jiao 		case MT7976_DUAL_ADIE:
6399ad32a4SBo Jiao 			return MT7986_EEPROM_MT7976_DUAL_DEFAULT;
6499ad32a4SBo Jiao 		default:
6599ad32a4SBo Jiao 			break;
6699ad32a4SBo Jiao 		}
6799ad32a4SBo Jiao 		return NULL;
6899ad32a4SBo Jiao 	default:
6999ad32a4SBo Jiao 		return MT7916_EEPROM_DEFAULT;
7099ad32a4SBo Jiao 	}
7199ad32a4SBo Jiao }
7299ad32a4SBo Jiao 
73bbc1d415SShayne Chen static int
74bbc1d415SShayne Chen mt7915_eeprom_load_default(struct mt7915_dev *dev)
75bbc1d415SShayne Chen {
76bbc1d415SShayne Chen 	u8 *eeprom = dev->mt76.eeprom.data;
77bbc1d415SShayne Chen 	const struct firmware *fw = NULL;
78bbc1d415SShayne Chen 	int ret;
79bbc1d415SShayne Chen 
8099ad32a4SBo Jiao 	ret = request_firmware(&fw, mt7915_eeprom_name(dev), dev->mt76.dev);
81bbc1d415SShayne Chen 	if (ret)
82bbc1d415SShayne Chen 		return ret;
83bbc1d415SShayne Chen 
84bbc1d415SShayne Chen 	if (!fw || !fw->data) {
85bbc1d415SShayne Chen 		dev_err(dev->mt76.dev, "Invalid default bin\n");
86bbc1d415SShayne Chen 		ret = -EINVAL;
87bbc1d415SShayne Chen 		goto out;
88bbc1d415SShayne Chen 	}
89bbc1d415SShayne Chen 
90beed8beaSBo Jiao 	memcpy(eeprom, fw->data, mt7915_eeprom_size(dev));
91bbc1d415SShayne Chen 	dev->flash_mode = true;
92bbc1d415SShayne Chen 
93bbc1d415SShayne Chen out:
94bbc1d415SShayne Chen 	release_firmware(fw);
95bbc1d415SShayne Chen 
96bbc1d415SShayne Chen 	return ret;
97bbc1d415SShayne Chen }
98bbc1d415SShayne Chen 
99bbc1d415SShayne Chen static int mt7915_eeprom_load(struct mt7915_dev *dev)
100bbc1d415SShayne Chen {
101bbc1d415SShayne Chen 	int ret;
102beed8beaSBo Jiao 	u16 eeprom_size = mt7915_eeprom_size(dev);
103bbc1d415SShayne Chen 
104beed8beaSBo Jiao 	ret = mt76_eeprom_init(&dev->mt76, eeprom_size);
105bbc1d415SShayne Chen 	if (ret < 0)
106bbc1d415SShayne Chen 		return ret;
107bbc1d415SShayne Chen 
108bbc1d415SShayne Chen 	if (ret) {
109bbc1d415SShayne Chen 		dev->flash_mode = true;
110bbc1d415SShayne Chen 	} else {
111bbc1d415SShayne Chen 		u8 free_block_num;
112bbc1d415SShayne Chen 		u32 block_num, i;
113bbc1d415SShayne Chen 
114bbc1d415SShayne Chen 		mt7915_mcu_get_eeprom_free_block(dev, &free_block_num);
115bbc1d415SShayne Chen 		/* efuse info not enough */
116bbc1d415SShayne Chen 		if (free_block_num >= 29)
117bbc1d415SShayne Chen 			return -EINVAL;
118bbc1d415SShayne Chen 
119bbc1d415SShayne Chen 		/* read eeprom data from efuse */
120beed8beaSBo Jiao 		block_num = DIV_ROUND_UP(eeprom_size,
121bbc1d415SShayne Chen 					 MT7915_EEPROM_BLOCK_SIZE);
122bbc1d415SShayne Chen 		for (i = 0; i < block_num; i++)
123bbc1d415SShayne Chen 			mt7915_mcu_get_eeprom(dev,
124bbc1d415SShayne Chen 					      i * MT7915_EEPROM_BLOCK_SIZE);
125bbc1d415SShayne Chen 	}
126bbc1d415SShayne Chen 
127bbc1d415SShayne Chen 	return mt7915_check_eeprom(dev);
128bbc1d415SShayne Chen }
129bbc1d415SShayne Chen 
13065dec62fSBo Jiao static void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy)
131e57b7901SRyder Lee {
1324c430774SLorenzo Bianconi 	struct mt7915_dev *dev = phy->dev;
13383d229d2SShayne Chen 	u8 *eeprom = dev->mt76.eeprom.data;
1344c430774SLorenzo Bianconi 	u32 val;
135e57b7901SRyder Lee 
136006b9d4aSBo Jiao 	val = eeprom[MT_EE_WIFI_CONF + phy->band_idx];
137f7fc2bbeSFelix Fietkau 	val = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
138b4d093e3SMeiChia Chiu 
139b4d093e3SMeiChia Chiu 	if (!is_mt7915(&dev->mt76)) {
140b4d093e3SMeiChia Chiu 		switch (val) {
141b4d093e3SMeiChia Chiu 		case MT_EE_V2_BAND_SEL_5GHZ:
142b4d093e3SMeiChia Chiu 			phy->mt76->cap.has_5ghz = true;
143b4d093e3SMeiChia Chiu 			return;
144b4d093e3SMeiChia Chiu 		case MT_EE_V2_BAND_SEL_6GHZ:
145b4d093e3SMeiChia Chiu 			phy->mt76->cap.has_6ghz = true;
146b4d093e3SMeiChia Chiu 			return;
147b4d093e3SMeiChia Chiu 		case MT_EE_V2_BAND_SEL_5GHZ_6GHZ:
148b4d093e3SMeiChia Chiu 			phy->mt76->cap.has_5ghz = true;
149b4d093e3SMeiChia Chiu 			phy->mt76->cap.has_6ghz = true;
150b4d093e3SMeiChia Chiu 			return;
151b4d093e3SMeiChia Chiu 		default:
152b4d093e3SMeiChia Chiu 			phy->mt76->cap.has_2ghz = true;
153b4d093e3SMeiChia Chiu 			return;
154b4d093e3SMeiChia Chiu 		}
15596c77770SFelix Fietkau 	} else if (val == MT_EE_BAND_SEL_DEFAULT && dev->dbdc_support) {
15696c77770SFelix Fietkau 		val = phy->band_idx ? MT_EE_BAND_SEL_5GHZ : MT_EE_BAND_SEL_2GHZ;
157b4d093e3SMeiChia Chiu 	}
15845a8b67aSFelix Fietkau 
159e57b7901SRyder Lee 	switch (val) {
16045a8b67aSFelix Fietkau 	case MT_EE_BAND_SEL_5GHZ:
1614c430774SLorenzo Bianconi 		phy->mt76->cap.has_5ghz = true;
162e57b7901SRyder Lee 		break;
16345a8b67aSFelix Fietkau 	case MT_EE_BAND_SEL_2GHZ:
1644c430774SLorenzo Bianconi 		phy->mt76->cap.has_2ghz = true;
165e57b7901SRyder Lee 		break;
166e57b7901SRyder Lee 	default:
1674c430774SLorenzo Bianconi 		phy->mt76->cap.has_2ghz = true;
1684c430774SLorenzo Bianconi 		phy->mt76->cap.has_5ghz = true;
169e57b7901SRyder Lee 		break;
170e57b7901SRyder Lee 	}
1714c430774SLorenzo Bianconi }
1724c430774SLorenzo Bianconi 
17365dec62fSBo Jiao void mt7915_eeprom_parse_hw_cap(struct mt7915_dev *dev,
17465dec62fSBo Jiao 				struct mt7915_phy *phy)
1754c430774SLorenzo Bianconi {
176a7ec8bcfSShayne Chen 	u8 path, nss, nss_max = 4, *eeprom = dev->mt76.eeprom.data;
17765dec62fSBo Jiao 	struct mt76_phy *mphy = phy->mt76;
1784c430774SLorenzo Bianconi 
17965dec62fSBo Jiao 	mt7915_eeprom_parse_band_config(phy);
180e57b7901SRyder Lee 
181a7ec8bcfSShayne Chen 	/* read tx/rx path from eeprom */
18265dec62fSBo Jiao 	if (is_mt7915(&dev->mt76)) {
183a7ec8bcfSShayne Chen 		path = FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH,
18465dec62fSBo Jiao 				 eeprom[MT_EE_WIFI_CONF]);
18565dec62fSBo Jiao 	} else {
186a7ec8bcfSShayne Chen 		path = FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH,
187006b9d4aSBo Jiao 				 eeprom[MT_EE_WIFI_CONF + phy->band_idx]);
18865dec62fSBo Jiao 	}
18965dec62fSBo Jiao 
190a7ec8bcfSShayne Chen 	if (!path || path > 4)
191a7ec8bcfSShayne Chen 		path = 4;
192e57b7901SRyder Lee 
19365dec62fSBo Jiao 	/* read tx/rx stream */
194a7ec8bcfSShayne Chen 	nss = path;
195f7fc2bbeSFelix Fietkau 	if (dev->dbdc_support) {
19665dec62fSBo Jiao 		if (is_mt7915(&dev->mt76)) {
197*de147cc2SNicolas Cavallari 			path = min_t(u8, path, 2);
198a7ec8bcfSShayne Chen 			nss = FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B0,
199f7fc2bbeSFelix Fietkau 					eeprom[MT_EE_WIFI_CONF + 3]);
200006b9d4aSBo Jiao 			if (phy->band_idx)
201a7ec8bcfSShayne Chen 				nss = FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B1,
20265dec62fSBo Jiao 						eeprom[MT_EE_WIFI_CONF + 3]);
20365dec62fSBo Jiao 		} else {
204a7ec8bcfSShayne Chen 			nss = FIELD_GET(MT_EE_WIFI_CONF_STREAM_NUM,
205006b9d4aSBo Jiao 					eeprom[MT_EE_WIFI_CONF + 2 + phy->band_idx]);
2064c430774SLorenzo Bianconi 		}
2074c430774SLorenzo Bianconi 
208a7ec8bcfSShayne Chen 		if (!is_mt7986(&dev->mt76))
209a7ec8bcfSShayne Chen 			nss_max = 2;
21065dec62fSBo Jiao 	}
21165dec62fSBo Jiao 
212a7ec8bcfSShayne Chen 	if (!nss)
213a7ec8bcfSShayne Chen 		nss = nss_max;
214a7ec8bcfSShayne Chen 	nss = min_t(u8, min_t(u8, nss_max, nss), path);
21599ad32a4SBo Jiao 
216a7ec8bcfSShayne Chen 	mphy->chainmask = BIT(path) - 1;
217a7ec8bcfSShayne Chen 	if (phy->band_idx)
21899ad32a4SBo Jiao 		mphy->chainmask <<= dev->chainshift;
219a7ec8bcfSShayne Chen 	mphy->antenna_mask = BIT(nss) - 1;
22065dec62fSBo Jiao 	dev->chainmask |= mphy->chainmask;
22199ad32a4SBo Jiao 	dev->chainshift = hweight8(dev->mphy.chainmask);
222e57b7901SRyder Lee }
223e57b7901SRyder Lee 
224e57b7901SRyder Lee int mt7915_eeprom_init(struct mt7915_dev *dev)
225e57b7901SRyder Lee {
226e57b7901SRyder Lee 	int ret;
227e57b7901SRyder Lee 
228e57b7901SRyder Lee 	ret = mt7915_eeprom_load(dev);
229bbc1d415SShayne Chen 	if (ret < 0) {
230bbc1d415SShayne Chen 		if (ret != -EINVAL)
231e57b7901SRyder Lee 			return ret;
232e57b7901SRyder Lee 
233bbc1d415SShayne Chen 		dev_warn(dev->mt76.dev, "eeprom load fail, use default bin\n");
234bbc1d415SShayne Chen 		ret = mt7915_eeprom_load_default(dev);
235bbc1d415SShayne Chen 		if (ret)
236bbc1d415SShayne Chen 			return ret;
237bbc1d415SShayne Chen 	}
238bbc1d415SShayne Chen 
239bbc1d415SShayne Chen 	ret = mt7915_eeprom_load_precal(dev);
240e57b7901SRyder Lee 	if (ret)
241e57b7901SRyder Lee 		return ret;
242e57b7901SRyder Lee 
24365dec62fSBo Jiao 	mt7915_eeprom_parse_hw_cap(dev, &dev->phy);
24498df2baeSLorenzo Bianconi 	memcpy(dev->mphy.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR,
245e57b7901SRyder Lee 	       ETH_ALEN);
246e57b7901SRyder Lee 
24798df2baeSLorenzo Bianconi 	mt76_eeprom_override(&dev->mphy);
248e57b7901SRyder Lee 
249e57b7901SRyder Lee 	return 0;
250e57b7901SRyder Lee }
251e57b7901SRyder Lee 
252e57b7901SRyder Lee int mt7915_eeprom_get_target_power(struct mt7915_dev *dev,
253e57b7901SRyder Lee 				   struct ieee80211_channel *chan,
254e57b7901SRyder Lee 				   u8 chain_idx)
255e57b7901SRyder Lee {
25683d229d2SShayne Chen 	u8 *eeprom = dev->mt76.eeprom.data;
257a226ccd0SShayne Chen 	int index, target_power;
2581c2d366eSShayne Chen 	bool tssi_on, is_7976;
259e57b7901SRyder Lee 
260e57b7901SRyder Lee 	if (chain_idx > 3)
261e57b7901SRyder Lee 		return -EINVAL;
262e57b7901SRyder Lee 
263e57b7901SRyder Lee 	tssi_on = mt7915_tssi_enabled(dev, chan->band);
2641c2d366eSShayne Chen 	is_7976 = mt7915_check_adie(dev, false) || is_mt7916(&dev->mt76);
265e57b7901SRyder Lee 
266e57b7901SRyder Lee 	if (chan->band == NL80211_BAND_2GHZ) {
2671c2d366eSShayne Chen 		if (is_7976) {
2681c2d366eSShayne Chen 			index = MT_EE_TX0_POWER_2G_V2 + chain_idx;
2691c2d366eSShayne Chen 			target_power = eeprom[index];
2701c2d366eSShayne Chen 		} else {
2711c2d366eSShayne Chen 			index = MT_EE_TX0_POWER_2G + chain_idx * 3;
27283d229d2SShayne Chen 			target_power = eeprom[index];
273e57b7901SRyder Lee 
274a226ccd0SShayne Chen 			if (!tssi_on)
27583d229d2SShayne Chen 				target_power += eeprom[index + 1];
2761c2d366eSShayne Chen 		}
277ae146683SShayne Chen 	} else if (chan->band == NL80211_BAND_5GHZ) {
278ae146683SShayne Chen 		int group = mt7915_get_channel_group_5g(chan->hw_value, is_7976);
279a226ccd0SShayne Chen 
2801c2d366eSShayne Chen 		if (is_7976) {
2811c2d366eSShayne Chen 			index = MT_EE_TX0_POWER_5G_V2 + chain_idx * 5;
2821c2d366eSShayne Chen 			target_power = eeprom[index + group];
2831c2d366eSShayne Chen 		} else {
2841c2d366eSShayne Chen 			index = MT_EE_TX0_POWER_5G + chain_idx * 12;
28583d229d2SShayne Chen 			target_power = eeprom[index + group];
286a226ccd0SShayne Chen 
287a226ccd0SShayne Chen 			if (!tssi_on)
28883d229d2SShayne Chen 				target_power += eeprom[index + 8];
289e57b7901SRyder Lee 		}
290ae146683SShayne Chen 	} else {
291ae146683SShayne Chen 		int group = mt7915_get_channel_group_6g(chan->hw_value);
292ae146683SShayne Chen 
293ae146683SShayne Chen 		index = MT_EE_TX0_POWER_6G_V2 + chain_idx * 8;
294ae146683SShayne Chen 		target_power = is_7976 ? eeprom[index + group] : 0;
2951c2d366eSShayne Chen 	}
296e57b7901SRyder Lee 
297a226ccd0SShayne Chen 	return target_power;
298e57b7901SRyder Lee }
299f1d96236SRyder Lee 
300ecb187a7SShayne Chen s8 mt7915_eeprom_get_power_delta(struct mt7915_dev *dev, int band)
301f1d96236SRyder Lee {
30283d229d2SShayne Chen 	u8 *eeprom = dev->mt76.eeprom.data;
3031c2d366eSShayne Chen 	u32 val, offs;
304ecb187a7SShayne Chen 	s8 delta;
3051c2d366eSShayne Chen 	bool is_7976 = mt7915_check_adie(dev, false) || is_mt7916(&dev->mt76);
306f1d96236SRyder Lee 
307ecb187a7SShayne Chen 	if (band == NL80211_BAND_2GHZ)
3081c2d366eSShayne Chen 		offs = is_7976 ? MT_EE_RATE_DELTA_2G_V2 : MT_EE_RATE_DELTA_2G;
309ae146683SShayne Chen 	else if (band == NL80211_BAND_5GHZ)
3101c2d366eSShayne Chen 		offs = is_7976 ? MT_EE_RATE_DELTA_5G_V2 : MT_EE_RATE_DELTA_5G;
311ae146683SShayne Chen 	else
312ae146683SShayne Chen 		offs = is_7976 ? MT_EE_RATE_DELTA_6G_V2 : 0;
3131c2d366eSShayne Chen 
3141c2d366eSShayne Chen 	val = eeprom[offs];
315ecb187a7SShayne Chen 
316ae146683SShayne Chen 	if (!offs || !(val & MT_EE_RATE_DELTA_EN))
317f1d96236SRyder Lee 		return 0;
318f1d96236SRyder Lee 
319ecb187a7SShayne Chen 	delta = FIELD_GET(MT_EE_RATE_DELTA_MASK, val);
320ecb187a7SShayne Chen 
321ecb187a7SShayne Chen 	return val & MT_EE_RATE_DELTA_SIGN ? delta : -delta;
322f1d96236SRyder Lee }
323f1d96236SRyder Lee 
324ecb187a7SShayne Chen const u8 mt7915_sku_group_len[] = {
325ecb187a7SShayne Chen 	[SKU_CCK] = 4,
326ecb187a7SShayne Chen 	[SKU_OFDM] = 8,
327ecb187a7SShayne Chen 	[SKU_HT_BW20] = 8,
328ecb187a7SShayne Chen 	[SKU_HT_BW40] = 9,
329ecb187a7SShayne Chen 	[SKU_VHT_BW20] = 12,
330ecb187a7SShayne Chen 	[SKU_VHT_BW40] = 12,
331ecb187a7SShayne Chen 	[SKU_VHT_BW80] = 12,
332ecb187a7SShayne Chen 	[SKU_VHT_BW160] = 12,
333ecb187a7SShayne Chen 	[SKU_HE_RU26] = 12,
334ecb187a7SShayne Chen 	[SKU_HE_RU52] = 12,
335ecb187a7SShayne Chen 	[SKU_HE_RU106] = 12,
336ecb187a7SShayne Chen 	[SKU_HE_RU242] = 12,
337ecb187a7SShayne Chen 	[SKU_HE_RU484] = 12,
338ecb187a7SShayne Chen 	[SKU_HE_RU996] = 12,
339ecb187a7SShayne Chen 	[SKU_HE_RU2x996] = 12
340ecb187a7SShayne Chen };
341