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