1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #include "mt7915.h"
5 #include "eeprom.h"
6
mt7915_efuse_valid(u8 val)7 static inline bool mt7915_efuse_valid(u8 val)
8 {
9 return !(val == 0xff);
10 }
11
mt7915_eeprom_read(struct mt7915_dev * dev,u32 offset)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
mt7915_eeprom_load(struct mt7915_dev * dev)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
mt7915_check_eeprom(struct mt7915_dev * dev)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
mt7915_eeprom_parse_hw_cap(struct mt7915_dev * dev)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
mt7915_eeprom_init(struct mt7915_dev * dev)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
mt7915_eeprom_get_target_power(struct mt7915_dev * dev,struct ieee80211_channel * chan,u8 chain_idx)103 int mt7915_eeprom_get_target_power(struct mt7915_dev *dev,
104 struct ieee80211_channel *chan,
105 u8 chain_idx)
106 {
107 int index, target_power;
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;
117 target_power = mt7915_eeprom_read(dev, index);
118
119 if (!tssi_on)
120 target_power += mt7915_eeprom_read(dev, index + 1);
121 } else {
122 int group = mt7915_get_channel_group(chan->hw_value);
123
124 index = MT_EE_TX0_POWER_5G + chain_idx * 12;
125 target_power = mt7915_eeprom_read(dev, index + group);
126
127 if (!tssi_on)
128 target_power += mt7915_eeprom_read(dev, index + 8);
129 }
130
131 return target_power;
132 }
133
134 static const u8 sku_cck_delta_map[] = {
135 SKU_CCK_GROUP0,
136 SKU_CCK_GROUP0,
137 SKU_CCK_GROUP1,
138 SKU_CCK_GROUP1,
139 };
140
141 static const u8 sku_ofdm_delta_map[] = {
142 SKU_OFDM_GROUP0,
143 SKU_OFDM_GROUP0,
144 SKU_OFDM_GROUP1,
145 SKU_OFDM_GROUP1,
146 SKU_OFDM_GROUP2,
147 SKU_OFDM_GROUP2,
148 SKU_OFDM_GROUP3,
149 SKU_OFDM_GROUP4,
150 };
151
152 static const u8 sku_mcs_delta_map[] = {
153 SKU_MCS_GROUP0,
154 SKU_MCS_GROUP1,
155 SKU_MCS_GROUP1,
156 SKU_MCS_GROUP2,
157 SKU_MCS_GROUP2,
158 SKU_MCS_GROUP3,
159 SKU_MCS_GROUP4,
160 SKU_MCS_GROUP5,
161 SKU_MCS_GROUP6,
162 SKU_MCS_GROUP7,
163 SKU_MCS_GROUP8,
164 SKU_MCS_GROUP9,
165 };
166
167 #define SKU_GROUP(_mode, _len, _ofs_2g, _ofs_5g, _map) \
168 [_mode] = { \
169 .len = _len, \
170 .offset = { \
171 _ofs_2g, \
172 _ofs_5g, \
173 }, \
174 .delta_map = _map \
175 }
176
177 const struct sku_group mt7915_sku_groups[] = {
178 SKU_GROUP(SKU_CCK, 4, 0x252, 0, sku_cck_delta_map),
179 SKU_GROUP(SKU_OFDM, 8, 0x254, 0x29d, sku_ofdm_delta_map),
180
181 SKU_GROUP(SKU_HT_BW20, 8, 0x259, 0x2a2, sku_mcs_delta_map),
182 SKU_GROUP(SKU_HT_BW40, 9, 0x262, 0x2ab, sku_mcs_delta_map),
183 SKU_GROUP(SKU_VHT_BW20, 12, 0x259, 0x2a2, sku_mcs_delta_map),
184 SKU_GROUP(SKU_VHT_BW40, 12, 0x262, 0x2ab, sku_mcs_delta_map),
185 SKU_GROUP(SKU_VHT_BW80, 12, 0, 0x2b4, sku_mcs_delta_map),
186 SKU_GROUP(SKU_VHT_BW160, 12, 0, 0, sku_mcs_delta_map),
187
188 SKU_GROUP(SKU_HE_RU26, 12, 0x27f, 0x2dd, sku_mcs_delta_map),
189 SKU_GROUP(SKU_HE_RU52, 12, 0x289, 0x2e7, sku_mcs_delta_map),
190 SKU_GROUP(SKU_HE_RU106, 12, 0x293, 0x2f1, sku_mcs_delta_map),
191 SKU_GROUP(SKU_HE_RU242, 12, 0x26b, 0x2bf, sku_mcs_delta_map),
192 SKU_GROUP(SKU_HE_RU484, 12, 0x275, 0x2c9, sku_mcs_delta_map),
193 SKU_GROUP(SKU_HE_RU996, 12, 0, 0x2d3, sku_mcs_delta_map),
194 SKU_GROUP(SKU_HE_RU2x996, 12, 0, 0, sku_mcs_delta_map),
195 };
196
197 static s8
mt7915_get_sku_delta(struct mt7915_dev * dev,u32 addr)198 mt7915_get_sku_delta(struct mt7915_dev *dev, u32 addr)
199 {
200 u32 val = mt7915_eeprom_read(dev, addr);
201 s8 delta = FIELD_GET(SKU_DELTA_VAL, val);
202
203 if (!(val & SKU_DELTA_EN))
204 return 0;
205
206 return val & SKU_DELTA_ADD ? delta : -delta;
207 }
208
209 static void
mt7915_eeprom_init_sku_band(struct mt7915_dev * dev,struct ieee80211_supported_band * sband)210 mt7915_eeprom_init_sku_band(struct mt7915_dev *dev,
211 struct ieee80211_supported_band *sband)
212 {
213 int i, band = sband->band;
214 s8 *rate_power = dev->rate_power[band], max_delta = 0;
215 u8 idx = 0;
216
217 for (i = 0; i < ARRAY_SIZE(mt7915_sku_groups); i++) {
218 const struct sku_group *sku = &mt7915_sku_groups[i];
219 u32 offset = sku->offset[band];
220 int j;
221
222 if (!offset) {
223 idx += sku->len;
224 continue;
225 }
226
227 rate_power[idx++] = mt7915_get_sku_delta(dev, offset);
228 if (rate_power[idx - 1] > max_delta)
229 max_delta = rate_power[idx - 1];
230
231 if (i == SKU_HT_BW20 || i == SKU_VHT_BW20)
232 offset += 1;
233
234 for (j = 1; j < sku->len; j++) {
235 u32 addr = offset + sku->delta_map[j];
236
237 rate_power[idx++] = mt7915_get_sku_delta(dev, addr);
238 if (rate_power[idx - 1] > max_delta)
239 max_delta = rate_power[idx - 1];
240 }
241 }
242
243 rate_power[idx] = max_delta;
244 }
245
mt7915_eeprom_init_sku(struct mt7915_dev * dev)246 void mt7915_eeprom_init_sku(struct mt7915_dev *dev)
247 {
248 mt7915_eeprom_init_sku_band(dev, &dev->mphy.sband_2g.sband);
249 mt7915_eeprom_init_sku_band(dev, &dev->mphy.sband_5g.sband);
250 }
251