1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
4 #include <linux/firmware.h>
8 static int mt7915_eeprom_load_precal(struct mt7915_dev
*dev
)
10 struct mt76_dev
*mdev
= &dev
->mt76
;
11 u8
*eeprom
= mdev
->eeprom
.data
;
12 u32 offs
= is_mt7915(&dev
->mt76
) ? MT_EE_DO_PRE_CAL
: MT_EE_DO_PRE_CAL_V2
;
13 u32 size
, val
= eeprom
[offs
];
16 if (!dev
->flash_mode
|| !val
)
19 size
= mt7915_get_cal_group_size(dev
) + mt7915_get_cal_dpd_size(dev
);
21 dev
->cal
= devm_kzalloc(mdev
->dev
, size
, GFP_KERNEL
);
25 offs
= is_mt7915(&dev
->mt76
) ? MT_EE_PRECAL
: MT_EE_PRECAL_V2
;
27 ret
= mt76_get_of_data_from_mtd(mdev
, dev
->cal
, offs
, size
);
31 ret
= mt76_get_of_data_from_nvmem(mdev
, dev
->cal
, "precal", size
);
35 dev_warn(mdev
->dev
, "missing precal data, size=%d\n", size
);
36 devm_kfree(mdev
->dev
, dev
->cal
);
42 static int mt7915_check_eeprom(struct mt7915_dev
*dev
)
44 u8
*eeprom
= dev
->mt76
.eeprom
.data
;
45 u16 val
= get_unaligned_le16(eeprom
);
47 #define CHECK_EEPROM_ERR(match) (match ? 0 : -EINVAL)
50 return CHECK_EEPROM_ERR(is_mt7915(&dev
->mt76
));
52 return CHECK_EEPROM_ERR(is_mt7916(&dev
->mt76
));
54 return CHECK_EEPROM_ERR(is_mt7981(&dev
->mt76
));
56 return CHECK_EEPROM_ERR(is_mt7986(&dev
->mt76
));
62 static char *mt7915_eeprom_name(struct mt7915_dev
*dev
)
64 switch (mt76_chip(&dev
->mt76
)) {
66 return dev
->dbdc_support
?
67 MT7915_EEPROM_DEFAULT_DBDC
: MT7915_EEPROM_DEFAULT
;
69 /* mt7981 only supports mt7976 and only in DBDC mode */
70 return MT7981_EEPROM_MT7976_DEFAULT_DBDC
;
72 switch (mt7915_check_adie(dev
, true)) {
73 case MT7976_ONE_ADIE_DBDC
:
74 return MT7986_EEPROM_MT7976_DEFAULT_DBDC
;
76 return MT7986_EEPROM_MT7975_DEFAULT
;
78 return MT7986_EEPROM_MT7976_DEFAULT
;
79 case MT7975_DUAL_ADIE
:
80 return MT7986_EEPROM_MT7975_DUAL_DEFAULT
;
81 case MT7976_DUAL_ADIE
:
82 return MT7986_EEPROM_MT7976_DUAL_DEFAULT
;
88 return MT7916_EEPROM_DEFAULT
;
93 mt7915_eeprom_load_default(struct mt7915_dev
*dev
)
95 u8
*eeprom
= dev
->mt76
.eeprom
.data
;
96 const struct firmware
*fw
= NULL
;
99 ret
= request_firmware(&fw
, mt7915_eeprom_name(dev
), dev
->mt76
.dev
);
103 if (!fw
|| !fw
->data
) {
104 dev_err(dev
->mt76
.dev
, "Invalid default bin\n");
109 memcpy(eeprom
, fw
->data
, mt7915_eeprom_size(dev
));
110 dev
->flash_mode
= true;
113 release_firmware(fw
);
118 static int mt7915_eeprom_load(struct mt7915_dev
*dev
)
121 u16 eeprom_size
= mt7915_eeprom_size(dev
);
123 ret
= mt76_eeprom_init(&dev
->mt76
, eeprom_size
);
128 dev
->flash_mode
= true;
132 u32 eeprom_blk_size
= MT7915_EEPROM_BLOCK_SIZE
;
134 ret
= mt7915_mcu_get_eeprom_free_block(dev
, &free_block_num
);
138 /* efuse info isn't enough */
139 if (free_block_num
>= 29)
142 /* read eeprom data from efuse */
143 block_num
= DIV_ROUND_UP(eeprom_size
, eeprom_blk_size
);
144 for (i
= 0; i
< block_num
; i
++) {
145 ret
= mt7915_mcu_get_eeprom(dev
, i
* eeprom_blk_size
);
151 return mt7915_check_eeprom(dev
);
154 static void mt7915_eeprom_parse_band_config(struct mt7915_phy
*phy
)
156 struct mt7915_dev
*dev
= phy
->dev
;
157 u8
*eeprom
= dev
->mt76
.eeprom
.data
;
158 u8 band
= phy
->mt76
->band_idx
;
161 val
= eeprom
[MT_EE_WIFI_CONF
+ band
];
162 val
= FIELD_GET(MT_EE_WIFI_CONF0_BAND_SEL
, val
);
164 if (!is_mt7915(&dev
->mt76
)) {
166 case MT_EE_V2_BAND_SEL_5GHZ
:
167 phy
->mt76
->cap
.has_5ghz
= true;
169 case MT_EE_V2_BAND_SEL_6GHZ
:
170 phy
->mt76
->cap
.has_6ghz
= true;
172 case MT_EE_V2_BAND_SEL_5GHZ_6GHZ
:
173 phy
->mt76
->cap
.has_5ghz
= true;
174 phy
->mt76
->cap
.has_6ghz
= true;
177 phy
->mt76
->cap
.has_2ghz
= true;
180 } else if (val
== MT_EE_BAND_SEL_DEFAULT
&& dev
->dbdc_support
) {
181 val
= band
? MT_EE_BAND_SEL_5GHZ
: MT_EE_BAND_SEL_2GHZ
;
185 case MT_EE_BAND_SEL_5GHZ
:
186 phy
->mt76
->cap
.has_5ghz
= true;
188 case MT_EE_BAND_SEL_2GHZ
:
189 phy
->mt76
->cap
.has_2ghz
= true;
192 phy
->mt76
->cap
.has_2ghz
= true;
193 phy
->mt76
->cap
.has_5ghz
= true;
198 void mt7915_eeprom_parse_hw_cap(struct mt7915_dev
*dev
,
199 struct mt7915_phy
*phy
)
201 u8 path
, nss
, nss_max
= 4, *eeprom
= dev
->mt76
.eeprom
.data
;
202 struct mt76_phy
*mphy
= phy
->mt76
;
203 u8 band
= phy
->mt76
->band_idx
;
205 mt7915_eeprom_parse_band_config(phy
);
207 /* read tx/rx path from eeprom */
208 if (is_mt7915(&dev
->mt76
)) {
209 path
= FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH
,
210 eeprom
[MT_EE_WIFI_CONF
]);
212 path
= FIELD_GET(MT_EE_WIFI_CONF0_TX_PATH
,
213 eeprom
[MT_EE_WIFI_CONF
+ band
]);
216 if (!path
|| path
> 4)
219 /* read tx/rx stream */
221 if (dev
->dbdc_support
) {
222 if (is_mt7915(&dev
->mt76
)) {
223 path
= min_t(u8
, path
, 2);
224 nss
= FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B0
,
225 eeprom
[MT_EE_WIFI_CONF
+ 3]);
227 nss
= FIELD_GET(MT_EE_WIFI_CONF3_TX_PATH_B1
,
228 eeprom
[MT_EE_WIFI_CONF
+ 3]);
230 nss
= FIELD_GET(MT_EE_WIFI_CONF_STREAM_NUM
,
231 eeprom
[MT_EE_WIFI_CONF
+ 2 + band
]);
234 if (!is_mt798x(&dev
->mt76
))
240 nss
= min_t(u8
, min_t(u8
, nss_max
, nss
), path
);
242 mphy
->chainmask
= BIT(path
) - 1;
244 mphy
->chainmask
<<= dev
->chainshift
;
245 mphy
->antenna_mask
= BIT(nss
) - 1;
246 dev
->chainmask
|= mphy
->chainmask
;
247 dev
->chainshift
= hweight8(dev
->mphy
.chainmask
);
250 int mt7915_eeprom_init(struct mt7915_dev
*dev
)
254 ret
= mt7915_eeprom_load(dev
);
259 dev_warn(dev
->mt76
.dev
, "eeprom load fail, use default bin\n");
260 ret
= mt7915_eeprom_load_default(dev
);
265 mt7915_eeprom_load_precal(dev
);
266 mt7915_eeprom_parse_hw_cap(dev
, &dev
->phy
);
267 memcpy(dev
->mphy
.macaddr
, dev
->mt76
.eeprom
.data
+ MT_EE_MAC_ADDR
,
270 mt76_eeprom_override(&dev
->mphy
);
275 int mt7915_eeprom_get_target_power(struct mt7915_dev
*dev
,
276 struct ieee80211_channel
*chan
,
279 u8
*eeprom
= dev
->mt76
.eeprom
.data
;
280 int index
, target_power
;
281 bool tssi_on
, is_7976
;
286 tssi_on
= mt7915_tssi_enabled(dev
, chan
->band
);
287 is_7976
= mt7915_check_adie(dev
, false) || is_mt7916(&dev
->mt76
);
289 if (chan
->band
== NL80211_BAND_2GHZ
) {
291 index
= MT_EE_TX0_POWER_2G_V2
+ chain_idx
;
292 target_power
= eeprom
[index
];
294 index
= MT_EE_TX0_POWER_2G
+ chain_idx
* 3;
295 target_power
= eeprom
[index
];
298 target_power
+= eeprom
[index
+ 1];
300 } else if (chan
->band
== NL80211_BAND_5GHZ
) {
301 int group
= mt7915_get_channel_group_5g(chan
->hw_value
, is_7976
);
304 index
= MT_EE_TX0_POWER_5G_V2
+ chain_idx
* 5;
305 target_power
= eeprom
[index
+ group
];
307 index
= MT_EE_TX0_POWER_5G
+ chain_idx
* 12;
308 target_power
= eeprom
[index
+ group
];
311 target_power
+= eeprom
[index
+ 8];
314 int group
= mt7915_get_channel_group_6g(chan
->hw_value
);
316 index
= MT_EE_TX0_POWER_6G_V2
+ chain_idx
* 8;
317 target_power
= is_7976
? eeprom
[index
+ group
] : 0;
323 s8
mt7915_eeprom_get_power_delta(struct mt7915_dev
*dev
, int band
)
325 u8
*eeprom
= dev
->mt76
.eeprom
.data
;
328 bool is_7976
= mt7915_check_adie(dev
, false) || is_mt7916(&dev
->mt76
);
330 if (band
== NL80211_BAND_2GHZ
)
331 offs
= is_7976
? MT_EE_RATE_DELTA_2G_V2
: MT_EE_RATE_DELTA_2G
;
332 else if (band
== NL80211_BAND_5GHZ
)
333 offs
= is_7976
? MT_EE_RATE_DELTA_5G_V2
: MT_EE_RATE_DELTA_5G
;
335 offs
= is_7976
? MT_EE_RATE_DELTA_6G_V2
: 0;
339 if (!offs
|| !(val
& MT_EE_RATE_DELTA_EN
))
342 delta
= FIELD_GET(MT_EE_RATE_DELTA_MASK
, val
);
344 return val
& MT_EE_RATE_DELTA_SIGN
? delta
: -delta
;
347 const u8 mt7915_sku_group_len
[] = {
355 [SKU_VHT_BW160
] = 12,
362 [SKU_HE_RU2x996
] = 12