1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt7915.h"
5 #include "eeprom.h"
6 
7 static u32 mt7915_eeprom_read(struct mt7915_dev *dev, u32 offset)
8 {
9 	u8 *data = dev->mt76.eeprom.data;
10 
11 	if (data[offset] == 0xff)
12 		mt7915_mcu_get_eeprom(dev, offset);
13 
14 	return data[offset];
15 }
16 
17 static int mt7915_eeprom_load(struct mt7915_dev *dev)
18 {
19 	int ret;
20 
21 	ret = mt76_eeprom_init(&dev->mt76, MT7915_EEPROM_SIZE);
22 	if (ret < 0)
23 		return ret;
24 
25 	memset(dev->mt76.eeprom.data, -1, MT7915_EEPROM_SIZE);
26 
27 	return 0;
28 }
29 
30 static int mt7915_check_eeprom(struct mt7915_dev *dev)
31 {
32 	u8 *eeprom = dev->mt76.eeprom.data;
33 	u16 val;
34 
35 	mt7915_eeprom_read(dev, MT_EE_CHIP_ID);
36 	val = get_unaligned_le16(eeprom);
37 
38 	switch (val) {
39 	case 0x7915:
40 		return 0;
41 	default:
42 		return -EINVAL;
43 	}
44 }
45 
46 void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy)
47 {
48 	struct mt7915_dev *dev = phy->dev;
49 	bool ext_phy = phy != &dev->phy;
50 	u32 val;
51 
52 	val = mt7915_eeprom_read(dev, MT_EE_WIFI_CONF + ext_phy);
53 	val = FIELD_GET(MT_EE_WIFI_CONF_BAND_SEL, val);
54 	switch (val) {
55 	case MT_EE_5GHZ:
56 		phy->mt76->cap.has_5ghz = true;
57 		break;
58 	case MT_EE_2GHZ:
59 		phy->mt76->cap.has_2ghz = true;
60 		break;
61 	default:
62 		phy->mt76->cap.has_2ghz = true;
63 		phy->mt76->cap.has_5ghz = true;
64 		break;
65 	}
66 }
67 
68 static void mt7915_eeprom_parse_hw_cap(struct mt7915_dev *dev)
69 {
70 	u8 nss, tx_mask[2] = {}, *eeprom = dev->mt76.eeprom.data;
71 
72 	mt7915_eeprom_parse_band_config(&dev->phy);
73 
74 	/* read tx mask from eeprom */
75 	tx_mask[0] = FIELD_GET(MT_EE_WIFI_CONF_TX_MASK,
76 			       eeprom[MT_EE_WIFI_CONF]);
77 	if (dev->dbdc_support)
78 		tx_mask[1] = FIELD_GET(MT_EE_WIFI_CONF_TX_MASK,
79 				       eeprom[MT_EE_WIFI_CONF + 1]);
80 
81 	nss = tx_mask[0] + tx_mask[1];
82 	if (!nss || nss > 4) {
83 		tx_mask[0] = 4;
84 		nss = 4;
85 	}
86 
87 	dev->chainmask = BIT(nss) - 1;
88 	dev->mphy.antenna_mask = BIT(tx_mask[0]) - 1;
89 	dev->phy.chainmask = dev->mphy.antenna_mask;
90 }
91 
92 int mt7915_eeprom_init(struct mt7915_dev *dev)
93 {
94 	int ret;
95 
96 	ret = mt7915_eeprom_load(dev);
97 	if (ret < 0)
98 		return ret;
99 
100 	ret = mt7915_check_eeprom(dev);
101 	if (ret)
102 		return ret;
103 
104 	mt7915_eeprom_parse_hw_cap(dev);
105 	memcpy(dev->mphy.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR,
106 	       ETH_ALEN);
107 
108 	mt76_eeprom_override(&dev->mphy);
109 
110 	return 0;
111 }
112 
113 int mt7915_eeprom_get_target_power(struct mt7915_dev *dev,
114 				   struct ieee80211_channel *chan,
115 				   u8 chain_idx)
116 {
117 	int index;
118 	bool tssi_on;
119 
120 	if (chain_idx > 3)
121 		return -EINVAL;
122 
123 	tssi_on = mt7915_tssi_enabled(dev, chan->band);
124 
125 	if (chan->band == NL80211_BAND_2GHZ) {
126 		index = MT_EE_TX0_POWER_2G + chain_idx * 3 + !tssi_on;
127 	} else {
128 		int group = tssi_on ?
129 			    mt7915_get_channel_group(chan->hw_value) : 8;
130 
131 		index = MT_EE_TX0_POWER_5G + chain_idx * 12 + group;
132 	}
133 
134 	return mt7915_eeprom_read(dev, index);
135 }
136 
137 static const u8 sku_cck_delta_map[] = {
138 	SKU_CCK_GROUP0,
139 	SKU_CCK_GROUP0,
140 	SKU_CCK_GROUP1,
141 	SKU_CCK_GROUP1,
142 };
143 
144 static const u8 sku_ofdm_delta_map[] = {
145 	SKU_OFDM_GROUP0,
146 	SKU_OFDM_GROUP0,
147 	SKU_OFDM_GROUP1,
148 	SKU_OFDM_GROUP1,
149 	SKU_OFDM_GROUP2,
150 	SKU_OFDM_GROUP2,
151 	SKU_OFDM_GROUP3,
152 	SKU_OFDM_GROUP4,
153 };
154 
155 static const u8 sku_mcs_delta_map[] = {
156 	SKU_MCS_GROUP0,
157 	SKU_MCS_GROUP1,
158 	SKU_MCS_GROUP1,
159 	SKU_MCS_GROUP2,
160 	SKU_MCS_GROUP2,
161 	SKU_MCS_GROUP3,
162 	SKU_MCS_GROUP4,
163 	SKU_MCS_GROUP5,
164 	SKU_MCS_GROUP6,
165 	SKU_MCS_GROUP7,
166 	SKU_MCS_GROUP8,
167 	SKU_MCS_GROUP9,
168 };
169 
170 #define SKU_GROUP(_mode, _len, _ofs_2g, _ofs_5g, _map)	\
171 	[_mode] = {					\
172 	.len = _len,					\
173 	.offset = {					\
174 		_ofs_2g,				\
175 		_ofs_5g,				\
176 	},						\
177 	.delta_map = _map				\
178 }
179 
180 const struct sku_group mt7915_sku_groups[] = {
181 	SKU_GROUP(SKU_CCK, 4, 0x252, 0, sku_cck_delta_map),
182 	SKU_GROUP(SKU_OFDM, 8, 0x254, 0x29d, sku_ofdm_delta_map),
183 
184 	SKU_GROUP(SKU_HT_BW20, 8, 0x259, 0x2a2, sku_mcs_delta_map),
185 	SKU_GROUP(SKU_HT_BW40, 9, 0x262, 0x2ab, sku_mcs_delta_map),
186 	SKU_GROUP(SKU_VHT_BW20, 12, 0x259, 0x2a2, sku_mcs_delta_map),
187 	SKU_GROUP(SKU_VHT_BW40, 12, 0x262, 0x2ab, sku_mcs_delta_map),
188 	SKU_GROUP(SKU_VHT_BW80, 12, 0, 0x2b4, sku_mcs_delta_map),
189 	SKU_GROUP(SKU_VHT_BW160, 12, 0, 0, sku_mcs_delta_map),
190 
191 	SKU_GROUP(SKU_HE_RU26, 12, 0x27f, 0x2dd, sku_mcs_delta_map),
192 	SKU_GROUP(SKU_HE_RU52, 12, 0x289, 0x2e7, sku_mcs_delta_map),
193 	SKU_GROUP(SKU_HE_RU106, 12, 0x293, 0x2f1, sku_mcs_delta_map),
194 	SKU_GROUP(SKU_HE_RU242, 12, 0x26b, 0x2bf, sku_mcs_delta_map),
195 	SKU_GROUP(SKU_HE_RU484, 12, 0x275, 0x2c9, sku_mcs_delta_map),
196 	SKU_GROUP(SKU_HE_RU996, 12, 0, 0x2d3, sku_mcs_delta_map),
197 	SKU_GROUP(SKU_HE_RU2x996, 12, 0, 0, sku_mcs_delta_map),
198 };
199 
200 static s8
201 mt7915_get_sku_delta(struct mt7915_dev *dev, u32 addr)
202 {
203 	u32 val = mt7915_eeprom_read(dev, addr);
204 	s8 delta = FIELD_GET(SKU_DELTA_VAL, val);
205 
206 	if (!(val & SKU_DELTA_EN))
207 		return 0;
208 
209 	return val & SKU_DELTA_ADD ? delta : -delta;
210 }
211 
212 static void
213 mt7915_eeprom_init_sku_band(struct mt7915_dev *dev,
214 			    struct ieee80211_supported_band *sband)
215 {
216 	int i, band = sband->band;
217 	s8 *rate_power = dev->rate_power[band], max_delta = 0;
218 	u8 idx = 0;
219 
220 	for (i = 0; i < ARRAY_SIZE(mt7915_sku_groups); i++) {
221 		const struct sku_group *sku = &mt7915_sku_groups[i];
222 		u32 offset = sku->offset[band];
223 		int j;
224 
225 		if (!offset) {
226 			idx += sku->len;
227 			continue;
228 		}
229 
230 		rate_power[idx++] = mt7915_get_sku_delta(dev, offset);
231 		if (rate_power[idx - 1] > max_delta)
232 			max_delta = rate_power[idx - 1];
233 
234 		if (i == SKU_HT_BW20 || i == SKU_VHT_BW20)
235 			offset += 1;
236 
237 		for (j = 1; j < sku->len; j++) {
238 			u32 addr = offset + sku->delta_map[j];
239 
240 			rate_power[idx++] = mt7915_get_sku_delta(dev, addr);
241 			if (rate_power[idx - 1] > max_delta)
242 				max_delta = rate_power[idx - 1];
243 		}
244 	}
245 
246 	rate_power[idx] = max_delta;
247 }
248 
249 void mt7915_eeprom_init_sku(struct mt7915_dev *dev)
250 {
251 	mt7915_eeprom_init_sku_band(dev, &dev->mphy.sband_2g.sband);
252 	mt7915_eeprom_init_sku_band(dev, &dev->mphy.sband_5g.sband);
253 }
254