1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #include <linux/firmware.h>
7 #include "mt7996.h"
8 #include "eeprom.h"
9 
10 static int mt7996_check_eeprom(struct mt7996_dev *dev)
11 {
12 	u8 *eeprom = dev->mt76.eeprom.data;
13 	u16 val = get_unaligned_le16(eeprom);
14 
15 	switch (val) {
16 	case 0x7990:
17 		return 0;
18 	default:
19 		return -EINVAL;
20 	}
21 }
22 
23 static char *mt7996_eeprom_name(struct mt7996_dev *dev)
24 {
25 	/* reserve for future variants */
26 	return MT7996_EEPROM_DEFAULT;
27 }
28 
29 static int
30 mt7996_eeprom_load_default(struct mt7996_dev *dev)
31 {
32 	u8 *eeprom = dev->mt76.eeprom.data;
33 	const struct firmware *fw = NULL;
34 	int ret;
35 
36 	ret = request_firmware(&fw, mt7996_eeprom_name(dev), dev->mt76.dev);
37 	if (ret)
38 		return ret;
39 
40 	if (!fw || !fw->data) {
41 		dev_err(dev->mt76.dev, "Invalid default bin\n");
42 		ret = -EINVAL;
43 		goto out;
44 	}
45 
46 	memcpy(eeprom, fw->data, MT7996_EEPROM_SIZE);
47 	dev->flash_mode = true;
48 
49 out:
50 	release_firmware(fw);
51 
52 	return ret;
53 }
54 
55 static int mt7996_eeprom_load(struct mt7996_dev *dev)
56 {
57 	int ret;
58 
59 	ret = mt76_eeprom_init(&dev->mt76, MT7996_EEPROM_SIZE);
60 	if (ret < 0)
61 		return ret;
62 
63 	if (ret) {
64 		dev->flash_mode = true;
65 	} else {
66 		u8 free_block_num;
67 		u32 block_num, i;
68 		u32 eeprom_blk_size = MT7996_EEPROM_BLOCK_SIZE;
69 
70 		ret = mt7996_mcu_get_eeprom_free_block(dev, &free_block_num);
71 		if (ret < 0)
72 			return ret;
73 
74 		/* efuse info isn't enough */
75 		if (free_block_num >= 59)
76 			return -EINVAL;
77 
78 		/* read eeprom data from efuse */
79 		block_num = DIV_ROUND_UP(MT7996_EEPROM_SIZE, eeprom_blk_size);
80 		for (i = 0; i < block_num; i++) {
81 			ret = mt7996_mcu_get_eeprom(dev, i * eeprom_blk_size);
82 			if (ret < 0)
83 				return ret;
84 		}
85 	}
86 
87 	return mt7996_check_eeprom(dev);
88 }
89 
90 static int mt7996_eeprom_parse_efuse_hw_cap(struct mt7996_dev *dev)
91 {
92 #define MODE_HE_ONLY		BIT(0)
93 #define WTBL_SIZE_GROUP		GENMASK(31, 28)
94 	u32 cap = 0;
95 	int ret;
96 
97 	ret = mt7996_mcu_get_chip_config(dev, &cap);
98 	if (ret)
99 		return ret;
100 
101 	if (cap) {
102 		dev->has_eht = !(cap & MODE_HE_ONLY);
103 		dev->wtbl_size_group = u32_get_bits(cap, WTBL_SIZE_GROUP);
104 	}
105 
106 	if (dev->wtbl_size_group < 2 || dev->wtbl_size_group > 4)
107 		dev->wtbl_size_group = 2; /* set default */
108 
109 	return 0;
110 }
111 
112 static int mt7996_eeprom_parse_band_config(struct mt7996_phy *phy)
113 {
114 	u8 *eeprom = phy->dev->mt76.eeprom.data;
115 	u32 val = eeprom[MT_EE_WIFI_CONF];
116 	int ret = 0;
117 
118 	switch (phy->mt76->band_idx) {
119 	case MT_BAND1:
120 		val = FIELD_GET(MT_EE_WIFI_CONF1_BAND_SEL, val);
121 		break;
122 	case MT_BAND2:
123 		val = eeprom[MT_EE_WIFI_CONF + 1];
124 		val = FIELD_GET(MT_EE_WIFI_CONF2_BAND_SEL, val);
125 		break;
126 	default:
127 		val = FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL, val);
128 		break;
129 	}
130 
131 	switch (val) {
132 	case MT_EE_BAND_SEL_2GHZ:
133 		phy->mt76->cap.has_2ghz = true;
134 		break;
135 	case MT_EE_BAND_SEL_5GHZ:
136 		phy->mt76->cap.has_5ghz = true;
137 		break;
138 	case MT_EE_BAND_SEL_6GHZ:
139 		phy->mt76->cap.has_6ghz = true;
140 		break;
141 	case MT_EE_BAND_SEL_5GHZ_6GHZ:
142 		phy->mt76->cap.has_5ghz = true;
143 		phy->mt76->cap.has_6ghz = true;
144 		break;
145 	default:
146 		ret = -EINVAL;
147 		break;
148 	}
149 
150 	return ret;
151 }
152 
153 int mt7996_eeprom_parse_hw_cap(struct mt7996_dev *dev, struct mt7996_phy *phy)
154 {
155 	u8 path, nss, band_idx = phy->mt76->band_idx;
156 	u8 *eeprom = dev->mt76.eeprom.data;
157 	struct mt76_phy *mphy = phy->mt76;
158 	int ret;
159 
160 	switch (band_idx) {
161 	case MT_BAND1:
162 		path = FIELD_GET(MT_EE_WIFI_CONF2_TX_PATH_BAND1,
163 				 eeprom[MT_EE_WIFI_CONF + 2]);
164 		nss = FIELD_GET(MT_EE_WIFI_CONF5_STREAM_NUM_BAND1,
165 				eeprom[MT_EE_WIFI_CONF + 5]);
166 		break;
167 	case MT_BAND2:
168 		path = FIELD_GET(MT_EE_WIFI_CONF2_TX_PATH_BAND2,
169 				 eeprom[MT_EE_WIFI_CONF + 2]);
170 		nss = FIELD_GET(MT_EE_WIFI_CONF5_STREAM_NUM_BAND2,
171 				eeprom[MT_EE_WIFI_CONF + 5]);
172 		break;
173 	default:
174 		path = FIELD_GET(MT_EE_WIFI_CONF1_TX_PATH_BAND0,
175 				 eeprom[MT_EE_WIFI_CONF + 1]);
176 		nss = FIELD_GET(MT_EE_WIFI_CONF4_STREAM_NUM_BAND0,
177 				eeprom[MT_EE_WIFI_CONF + 4]);
178 		break;
179 	}
180 
181 	if (!path || path > 4)
182 		path = 4;
183 
184 	nss = min_t(u8, min_t(u8, 4, nss), path);
185 
186 	mphy->antenna_mask = BIT(nss) - 1;
187 	mphy->chainmask = (BIT(path) - 1) << dev->chainshift[band_idx];
188 	dev->chainmask |= mphy->chainmask;
189 	if (band_idx < MT_BAND2)
190 		dev->chainshift[band_idx + 1] = dev->chainshift[band_idx] +
191 						hweight16(mphy->chainmask);
192 
193 	ret = mt7996_eeprom_parse_efuse_hw_cap(dev);
194 	if (ret)
195 		return ret;
196 
197 	return mt7996_eeprom_parse_band_config(phy);
198 }
199 
200 int mt7996_eeprom_init(struct mt7996_dev *dev)
201 {
202 	int ret;
203 
204 	ret = mt7996_eeprom_load(dev);
205 	if (ret < 0) {
206 		if (ret != -EINVAL)
207 			return ret;
208 
209 		dev_warn(dev->mt76.dev, "eeprom load fail, use default bin\n");
210 		ret = mt7996_eeprom_load_default(dev);
211 		if (ret)
212 			return ret;
213 	}
214 
215 	ret = mt7996_eeprom_parse_hw_cap(dev, &dev->phy);
216 	if (ret < 0)
217 		return ret;
218 
219 	memcpy(dev->mphy.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, ETH_ALEN);
220 	mt76_eeprom_override(&dev->mphy);
221 
222 	return 0;
223 }
224 
225 int mt7996_eeprom_get_target_power(struct mt7996_dev *dev,
226 				   struct ieee80211_channel *chan)
227 {
228 	u8 *eeprom = dev->mt76.eeprom.data;
229 	int target_power;
230 
231 	if (chan->band == NL80211_BAND_5GHZ)
232 		target_power = eeprom[MT_EE_TX0_POWER_5G +
233 				      mt7996_get_channel_group_5g(chan->hw_value)];
234 	else if (chan->band == NL80211_BAND_6GHZ)
235 		target_power = eeprom[MT_EE_TX0_POWER_6G +
236 				      mt7996_get_channel_group_6g(chan->hw_value)];
237 	else
238 		target_power = eeprom[MT_EE_TX0_POWER_2G];
239 
240 	return target_power;
241 }
242 
243 s8 mt7996_eeprom_get_power_delta(struct mt7996_dev *dev, int band)
244 {
245 	u8 *eeprom = dev->mt76.eeprom.data;
246 	u32 val;
247 	s8 delta;
248 
249 	if (band == NL80211_BAND_5GHZ)
250 		val = eeprom[MT_EE_RATE_DELTA_5G];
251 	else if (band == NL80211_BAND_6GHZ)
252 		val = eeprom[MT_EE_RATE_DELTA_6G];
253 	else
254 		val = eeprom[MT_EE_RATE_DELTA_2G];
255 
256 	if (!(val & MT_EE_RATE_DELTA_EN))
257 		return 0;
258 
259 	delta = FIELD_GET(MT_EE_RATE_DELTA_MASK, val);
260 
261 	return val & MT_EE_RATE_DELTA_SIGN ? delta : -delta;
262 }
263