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