xref: /openbmc/linux/drivers/net/wireless/realtek/rtw89/phy.c (revision 2a8ec45f4d0eff76cf0cef78b3040ccfd923a72e)
1e3ec7017SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3ec7017SPing-Ke Shih /* Copyright(c) 2019-2020  Realtek Corporation
3e3ec7017SPing-Ke Shih  */
4e3ec7017SPing-Ke Shih 
529136c95SEric Huang #include "coex.h"
6e3ec7017SPing-Ke Shih #include "debug.h"
7e3ec7017SPing-Ke Shih #include "fw.h"
88379fa61SYuan-Han Zhang #include "mac.h"
9e3ec7017SPing-Ke Shih #include "phy.h"
10e3ec7017SPing-Ke Shih #include "ps.h"
11e3ec7017SPing-Ke Shih #include "reg.h"
12e3ec7017SPing-Ke Shih #include "sar.h"
13e3715859SEric Huang #include "txrx.h"
1429136c95SEric Huang #include "util.h"
15e3ec7017SPing-Ke Shih 
16e3ec7017SPing-Ke Shih static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev,
17e3ec7017SPing-Ke Shih 			     const struct rtw89_ra_report *report)
18e3ec7017SPing-Ke Shih {
19e3ec7017SPing-Ke Shih 	u32 bit_rate = report->bit_rate;
20e3ec7017SPing-Ke Shih 
21e3ec7017SPing-Ke Shih 	/* lower than ofdm, do not aggregate */
22e3ec7017SPing-Ke Shih 	if (bit_rate < 550)
23e3ec7017SPing-Ke Shih 		return 1;
24e3ec7017SPing-Ke Shih 
250d466f05SPing-Ke Shih 	/* avoid AMSDU for legacy rate */
260d466f05SPing-Ke Shih 	if (report->might_fallback_legacy)
27e3ec7017SPing-Ke Shih 		return 1;
28e3ec7017SPing-Ke Shih 
29e3ec7017SPing-Ke Shih 	/* lower than 20M vht 2ss mcs8, make it small */
30e3ec7017SPing-Ke Shih 	if (bit_rate < 1800)
31e3ec7017SPing-Ke Shih 		return 1200;
32e3ec7017SPing-Ke Shih 
33e3ec7017SPing-Ke Shih 	/* lower than 40M vht 2ss mcs9, make it medium */
34e3ec7017SPing-Ke Shih 	if (bit_rate < 4000)
35e3ec7017SPing-Ke Shih 		return 2600;
36e3ec7017SPing-Ke Shih 
37e3ec7017SPing-Ke Shih 	/* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */
38e3ec7017SPing-Ke Shih 	if (bit_rate < 7000)
39e3ec7017SPing-Ke Shih 		return 3500;
40e3ec7017SPing-Ke Shih 
41e3ec7017SPing-Ke Shih 	return rtwdev->chip->max_amsdu_limit;
42e3ec7017SPing-Ke Shih }
43e3ec7017SPing-Ke Shih 
44e3ec7017SPing-Ke Shih static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap)
45e3ec7017SPing-Ke Shih {
46e3ec7017SPing-Ke Shih 	u64 ra_mask = 0;
47e3ec7017SPing-Ke Shih 	u8 mcs_cap;
48e3ec7017SPing-Ke Shih 	int i, nss;
49e3ec7017SPing-Ke Shih 
50e3ec7017SPing-Ke Shih 	for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) {
51e3ec7017SPing-Ke Shih 		mcs_cap = mcs_map & 0x3;
52e3ec7017SPing-Ke Shih 		switch (mcs_cap) {
53e3ec7017SPing-Ke Shih 		case 2:
54e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss;
55e3ec7017SPing-Ke Shih 			break;
56e3ec7017SPing-Ke Shih 		case 1:
57e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss;
58e3ec7017SPing-Ke Shih 			break;
59e3ec7017SPing-Ke Shih 		case 0:
60e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss;
61e3ec7017SPing-Ke Shih 			break;
62e3ec7017SPing-Ke Shih 		default:
63e3ec7017SPing-Ke Shih 			break;
64e3ec7017SPing-Ke Shih 		}
65e3ec7017SPing-Ke Shih 	}
66e3ec7017SPing-Ke Shih 
67e3ec7017SPing-Ke Shih 	return ra_mask;
68e3ec7017SPing-Ke Shih }
69e3ec7017SPing-Ke Shih 
70e3ec7017SPing-Ke Shih static u64 get_he_ra_mask(struct ieee80211_sta *sta)
71e3ec7017SPing-Ke Shih {
72046d2e7cSSriram R 	struct ieee80211_sta_he_cap cap = sta->deflink.he_cap;
73e3ec7017SPing-Ke Shih 	u16 mcs_map;
74e3ec7017SPing-Ke Shih 
75046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
76e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_160:
77e3ec7017SPing-Ke Shih 		if (cap.he_cap_elem.phy_cap_info[0] &
78e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
79e3ec7017SPing-Ke Shih 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80);
80e3ec7017SPing-Ke Shih 		else
81e3ec7017SPing-Ke Shih 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160);
82e3ec7017SPing-Ke Shih 		break;
83e3ec7017SPing-Ke Shih 	default:
84e3ec7017SPing-Ke Shih 		mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80);
85e3ec7017SPing-Ke Shih 	}
86e3ec7017SPing-Ke Shih 
87e3ec7017SPing-Ke Shih 	/* MCS11, MCS9, MCS7 */
88e3ec7017SPing-Ke Shih 	return get_mcs_ra_mask(mcs_map, 11, 2);
89e3ec7017SPing-Ke Shih }
90e3ec7017SPing-Ke Shih 
91e3ec7017SPing-Ke Shih #define RA_FLOOR_TABLE_SIZE	7
92e3ec7017SPing-Ke Shih #define RA_FLOOR_UP_GAP		3
93e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi,
94e3ec7017SPing-Ke Shih 				  u8 ratr_state)
95e3ec7017SPing-Ke Shih {
96e3ec7017SPing-Ke Shih 	u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100};
97e3ec7017SPing-Ke Shih 	u8 rssi_lv = 0;
98e3ec7017SPing-Ke Shih 	u8 i;
99e3ec7017SPing-Ke Shih 
100e3ec7017SPing-Ke Shih 	rssi >>= 1;
101e3ec7017SPing-Ke Shih 	for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) {
102e3ec7017SPing-Ke Shih 		if (i >= ratr_state)
103e3ec7017SPing-Ke Shih 			rssi_lv_t[i] += RA_FLOOR_UP_GAP;
104e3ec7017SPing-Ke Shih 		if (rssi < rssi_lv_t[i]) {
105e3ec7017SPing-Ke Shih 			rssi_lv = i;
106e3ec7017SPing-Ke Shih 			break;
107e3ec7017SPing-Ke Shih 		}
108e3ec7017SPing-Ke Shih 	}
109e3ec7017SPing-Ke Shih 	if (rssi_lv == 0)
110e3ec7017SPing-Ke Shih 		return 0xffffffffffffffffULL;
111e3ec7017SPing-Ke Shih 	else if (rssi_lv == 1)
112e3ec7017SPing-Ke Shih 		return 0xfffffffffffffff0ULL;
113e3ec7017SPing-Ke Shih 	else if (rssi_lv == 2)
1143c2c2e2eSChien-Hsun Liao 		return 0xffffffffffffefe0ULL;
115e3ec7017SPing-Ke Shih 	else if (rssi_lv == 3)
1163c2c2e2eSChien-Hsun Liao 		return 0xffffffffffffcfc0ULL;
117e3ec7017SPing-Ke Shih 	else if (rssi_lv == 4)
1183c2c2e2eSChien-Hsun Liao 		return 0xffffffffffff8f80ULL;
119e3ec7017SPing-Ke Shih 	else if (rssi_lv >= 5)
1203c2c2e2eSChien-Hsun Liao 		return 0xffffffffffff0f00ULL;
121e3ec7017SPing-Ke Shih 
122e3ec7017SPing-Ke Shih 	return 0xffffffffffffffffULL;
123e3ec7017SPing-Ke Shih }
124e3ec7017SPing-Ke Shih 
1253c2c2e2eSChien-Hsun Liao static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak)
1263c2c2e2eSChien-Hsun Liao {
1273c2c2e2eSChien-Hsun Liao 	if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0)
1283c2c2e2eSChien-Hsun Liao 		ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
1293c2c2e2eSChien-Hsun Liao 
1303c2c2e2eSChien-Hsun Liao 	if (ra_mask == 0)
1313c2c2e2eSChien-Hsun Liao 		ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
1323c2c2e2eSChien-Hsun Liao 
1333c2c2e2eSChien-Hsun Liao 	return ra_mask;
1343c2c2e2eSChien-Hsun Liao }
1353c2c2e2eSChien-Hsun Liao 
136e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta)
137e3ec7017SPing-Ke Shih {
138e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta);
139cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
140e3ec7017SPing-Ke Shih 	struct cfg80211_bitrate_mask *mask = &rtwsta->mask;
141e3ec7017SPing-Ke Shih 	enum nl80211_band band;
142e3ec7017SPing-Ke Shih 	u64 cfg_mask;
143e3ec7017SPing-Ke Shih 
144e3ec7017SPing-Ke Shih 	if (!rtwsta->use_cfg_mask)
145e3ec7017SPing-Ke Shih 		return -1;
146e3ec7017SPing-Ke Shih 
147cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
148e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
149e3ec7017SPing-Ke Shih 		band = NL80211_BAND_2GHZ;
150e3ec7017SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy,
151e3ec7017SPing-Ke Shih 					   RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES);
152e3ec7017SPing-Ke Shih 		break;
153e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
154e3ec7017SPing-Ke Shih 		band = NL80211_BAND_5GHZ;
155e3ec7017SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy,
156e3ec7017SPing-Ke Shih 					   RA_MASK_OFDM_RATES);
157e3ec7017SPing-Ke Shih 		break;
158f76b3276SPing-Ke Shih 	case RTW89_BAND_6G:
159f76b3276SPing-Ke Shih 		band = NL80211_BAND_6GHZ;
160f76b3276SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy,
161f76b3276SPing-Ke Shih 					   RA_MASK_OFDM_RATES);
162f76b3276SPing-Ke Shih 		break;
163e3ec7017SPing-Ke Shih 	default:
164cbb145b9SZong-Zhe Yang 		rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type);
165e3ec7017SPing-Ke Shih 		return -1;
166e3ec7017SPing-Ke Shih 	}
167e3ec7017SPing-Ke Shih 
168046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
169e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0],
170e3ec7017SPing-Ke Shih 					    RA_MASK_HE_1SS_RATES);
171e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1],
172e3ec7017SPing-Ke Shih 					    RA_MASK_HE_2SS_RATES);
173046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
174e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0],
175e3ec7017SPing-Ke Shih 					    RA_MASK_VHT_1SS_RATES);
176e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1],
177e3ec7017SPing-Ke Shih 					    RA_MASK_VHT_2SS_RATES);
178046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
179e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0],
180e3ec7017SPing-Ke Shih 					    RA_MASK_HT_1SS_RATES);
181e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1],
182e3ec7017SPing-Ke Shih 					    RA_MASK_HT_2SS_RATES);
183e3ec7017SPing-Ke Shih 	}
184e3ec7017SPing-Ke Shih 
185e3ec7017SPing-Ke Shih 	return cfg_mask;
186e3ec7017SPing-Ke Shih }
187e3ec7017SPing-Ke Shih 
188e3ec7017SPing-Ke Shih static const u64
189e3ec7017SPing-Ke Shih rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES,
190e3ec7017SPing-Ke Shih 			     RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES};
191e3ec7017SPing-Ke Shih static const u64
192e3ec7017SPing-Ke Shih rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES,
193e3ec7017SPing-Ke Shih 			      RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES};
194e3ec7017SPing-Ke Shih static const u64
195e3ec7017SPing-Ke Shih rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES,
196e3ec7017SPing-Ke Shih 			     RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES};
197e3ec7017SPing-Ke Shih 
1980891b366SKuan-Chung Chen static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev,
1990891b366SKuan-Chung Chen 				struct rtw89_sta *rtwsta,
2000891b366SKuan-Chung Chen 				bool *fix_giltf_en, u8 *fix_giltf)
2010891b366SKuan-Chung Chen {
2020891b366SKuan-Chung Chen 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2030891b366SKuan-Chung Chen 	struct cfg80211_bitrate_mask *mask = &rtwsta->mask;
2040891b366SKuan-Chung Chen 	u8 band = chan->band_type;
2050891b366SKuan-Chung Chen 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
2060891b366SKuan-Chung Chen 	u8 he_gi = mask->control[nl_band].he_gi;
2070891b366SKuan-Chung Chen 	u8 he_ltf = mask->control[nl_band].he_ltf;
2080891b366SKuan-Chung Chen 
2090891b366SKuan-Chung Chen 	if (!rtwsta->use_cfg_mask)
2100891b366SKuan-Chung Chen 		return;
2110891b366SKuan-Chung Chen 
2120891b366SKuan-Chung Chen 	if (he_ltf == 2 && he_gi == 2) {
2130891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_LGI_4XHE32;
2140891b366SKuan-Chung Chen 	} else if (he_ltf == 2 && he_gi == 0) {
2150891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_SGI_4XHE08;
2160891b366SKuan-Chung Chen 	} else if (he_ltf == 1 && he_gi == 1) {
2170891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_2XHE16;
2180891b366SKuan-Chung Chen 	} else if (he_ltf == 1 && he_gi == 0) {
2190891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_2XHE08;
2200891b366SKuan-Chung Chen 	} else if (he_ltf == 0 && he_gi == 1) {
2210891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_1XHE16;
2220891b366SKuan-Chung Chen 	} else if (he_ltf == 0 && he_gi == 0) {
2230891b366SKuan-Chung Chen 		*fix_giltf = RTW89_GILTF_1XHE08;
2240891b366SKuan-Chung Chen 	} else {
2250891b366SKuan-Chung Chen 		*fix_giltf_en = false;
2260891b366SKuan-Chung Chen 		return;
2270891b366SKuan-Chung Chen 	}
2280891b366SKuan-Chung Chen 
2290891b366SKuan-Chung Chen 	*fix_giltf_en = true;
2300891b366SKuan-Chung Chen }
2310891b366SKuan-Chung Chen 
232e3ec7017SPing-Ke Shih static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev,
233e3ec7017SPing-Ke Shih 				    struct ieee80211_sta *sta, bool csi)
234e3ec7017SPing-Ke Shih {
235e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
236e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
237e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
238e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
239cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2403788c599SDian-Syuan Yang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
241e3ec7017SPing-Ke Shih 	const u64 *high_rate_masks = rtw89_ra_mask_ht_rates;
242e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi);
243e3ec7017SPing-Ke Shih 	u64 ra_mask = 0;
2443c2c2e2eSChien-Hsun Liao 	u64 ra_mask_bak;
245e3ec7017SPing-Ke Shih 	u8 mode = 0;
246e3ec7017SPing-Ke Shih 	u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY;
247e3ec7017SPing-Ke Shih 	u8 bw_mode = 0;
248e3ec7017SPing-Ke Shih 	u8 stbc_en = 0;
249e3ec7017SPing-Ke Shih 	u8 ldpc_en = 0;
2500891b366SKuan-Chung Chen 	u8 fix_giltf = 0;
251e3ec7017SPing-Ke Shih 	u8 i;
252e3ec7017SPing-Ke Shih 	bool sgi = false;
2530891b366SKuan-Chung Chen 	bool fix_giltf_en = false;
254e3ec7017SPing-Ke Shih 
255e3ec7017SPing-Ke Shih 	memset(ra, 0, sizeof(*ra));
256e3ec7017SPing-Ke Shih 	/* Set the ra mask from sta's capability */
257046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
258e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HE;
259e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HE;
260e3ec7017SPing-Ke Shih 		ra_mask |= get_he_ra_mask(sta);
261e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_he_rates;
262046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] &
263e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
264e3ec7017SPing-Ke Shih 			stbc_en = 1;
265046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] &
266e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
267e3ec7017SPing-Ke Shih 			ldpc_en = 1;
2680891b366SKuan-Chung Chen 		rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, &fix_giltf_en, &fix_giltf);
269046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
270046d2e7cSSriram R 		u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map);
271e3ec7017SPing-Ke Shih 
272e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_VHT;
273e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_VHT;
274e3ec7017SPing-Ke Shih 		/* MCS9, MCS8, MCS7 */
275e3ec7017SPing-Ke Shih 		ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1);
276e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_vht_rates;
277046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
278e3ec7017SPing-Ke Shih 			stbc_en = 1;
279046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
280e3ec7017SPing-Ke Shih 			ldpc_en = 1;
281046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
282e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HT;
283e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HT;
284046d2e7cSSriram R 		ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) |
285046d2e7cSSriram R 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) |
286046d2e7cSSriram R 			   (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) |
287046d2e7cSSriram R 			   (sta->deflink.ht_cap.mcs.rx_mask[0] << 12);
288e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_ht_rates;
289046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
290e3ec7017SPing-Ke Shih 			stbc_en = 1;
291046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
292e3ec7017SPing-Ke Shih 			ldpc_en = 1;
293e3ec7017SPing-Ke Shih 	}
294e3ec7017SPing-Ke Shih 
295cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
296f76b3276SPing-Ke Shih 	case RTW89_BAND_2G:
297046d2e7cSSriram R 		ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ];
2983788c599SDian-Syuan Yang 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf)
299e3ec7017SPing-Ke Shih 			mode |= RTW89_RA_MODE_CCK;
3003788c599SDian-Syuan Yang 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0)
3013788c599SDian-Syuan Yang 			mode |= RTW89_RA_MODE_OFDM;
302f76b3276SPing-Ke Shih 		break;
303f76b3276SPing-Ke Shih 	case RTW89_BAND_5G:
304046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4;
305e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
306f76b3276SPing-Ke Shih 		break;
307f76b3276SPing-Ke Shih 	case RTW89_BAND_6G:
308046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4;
309f76b3276SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
310f76b3276SPing-Ke Shih 		break;
311f76b3276SPing-Ke Shih 	default:
312f76b3276SPing-Ke Shih 		rtw89_err(rtwdev, "Unknown band type\n");
313f76b3276SPing-Ke Shih 		break;
314e3ec7017SPing-Ke Shih 	}
315e3ec7017SPing-Ke Shih 
3163c2c2e2eSChien-Hsun Liao 	ra_mask_bak = ra_mask;
3173c2c2e2eSChien-Hsun Liao 
318e3ec7017SPing-Ke Shih 	if (mode >= RTW89_RA_MODE_HT) {
3193c2c2e2eSChien-Hsun Liao 		u64 mask = 0;
320e3ec7017SPing-Ke Shih 		for (i = 0; i < rtwdev->hal.tx_nss; i++)
3213c2c2e2eSChien-Hsun Liao 			mask |= high_rate_masks[i];
322e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_OFDM)
3233c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBOFDM_RATES;
324e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_CCK)
3253c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBCCK_RATES;
3263c2c2e2eSChien-Hsun Liao 		ra_mask &= mask;
327e3ec7017SPing-Ke Shih 	} else if (mode & RTW89_RA_MODE_OFDM) {
3283c2c2e2eSChien-Hsun Liao 		ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES);
329e3ec7017SPing-Ke Shih 	}
330e3ec7017SPing-Ke Shih 
3313c2c2e2eSChien-Hsun Liao 	if (mode != RTW89_RA_MODE_CCK)
332e3ec7017SPing-Ke Shih 		ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0);
3333c2c2e2eSChien-Hsun Liao 
3343c2c2e2eSChien-Hsun Liao 	ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak);
335e3ec7017SPing-Ke Shih 	ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
336e3ec7017SPing-Ke Shih 
337046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
338167044afSPing-Ke Shih 	case IEEE80211_STA_RX_BW_160:
339167044afSPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_160;
340046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
341046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160);
342167044afSPing-Ke Shih 		break;
343e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_80:
344e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_80;
345046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
346046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
347e3ec7017SPing-Ke Shih 		break;
348e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_40:
349e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_40;
350046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
351046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40);
352e3ec7017SPing-Ke Shih 		break;
353e3ec7017SPing-Ke Shih 	default:
354e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_20;
355046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
356046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
357e3ec7017SPing-Ke Shih 		break;
358e3ec7017SPing-Ke Shih 	}
359e3ec7017SPing-Ke Shih 
360046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] &
361e3ec7017SPing-Ke Shih 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM)
362e3ec7017SPing-Ke Shih 		ra->dcm_cap = 1;
363e3ec7017SPing-Ke Shih 
3643788c599SDian-Syuan Yang 	if (rate_pattern->enable && !vif->p2p) {
365e3ec7017SPing-Ke Shih 		ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
366e3ec7017SPing-Ke Shih 		ra_mask &= rate_pattern->ra_mask;
367e3ec7017SPing-Ke Shih 		mode = rate_pattern->ra_mode;
368e3ec7017SPing-Ke Shih 	}
369e3ec7017SPing-Ke Shih 
370e3ec7017SPing-Ke Shih 	ra->bw_cap = bw_mode;
37125ed1a17SPing-Ke Shih 	ra->er_cap = rtwsta->er_cap;
372e3ec7017SPing-Ke Shih 	ra->mode_ctrl = mode;
373e3ec7017SPing-Ke Shih 	ra->macid = rtwsta->mac_id;
374e3ec7017SPing-Ke Shih 	ra->stbc_cap = stbc_en;
375e3ec7017SPing-Ke Shih 	ra->ldpc_cap = ldpc_en;
376046d2e7cSSriram R 	ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1;
377e3ec7017SPing-Ke Shih 	ra->en_sgi = sgi;
378e3ec7017SPing-Ke Shih 	ra->ra_mask = ra_mask;
3790891b366SKuan-Chung Chen 	ra->fix_giltf_en = fix_giltf_en;
3800891b366SKuan-Chung Chen 	ra->fix_giltf = fix_giltf;
381e3ec7017SPing-Ke Shih 
382e3ec7017SPing-Ke Shih 	if (!csi)
383e3ec7017SPing-Ke Shih 		return;
384e3ec7017SPing-Ke Shih 
385e3ec7017SPing-Ke Shih 	ra->fixed_csi_rate_en = false;
386e3ec7017SPing-Ke Shih 	ra->ra_csi_rate_en = true;
387e3ec7017SPing-Ke Shih 	ra->cr_tbl_sel = false;
388e3ec7017SPing-Ke Shih 	ra->band_num = rtwvif->phy_idx;
389e3ec7017SPing-Ke Shih 	ra->csi_bw = bw_mode;
390e3ec7017SPing-Ke Shih 	ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32;
391e3ec7017SPing-Ke Shih 	ra->csi_mcs_ss_idx = 5;
392e3ec7017SPing-Ke Shih 	ra->csi_mode = csi_mode;
393e3ec7017SPing-Ke Shih }
394e3ec7017SPing-Ke Shih 
3959d9a9edcSPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta,
3969d9a9edcSPing-Ke Shih 			     u32 changed)
397e3ec7017SPing-Ke Shih {
398e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
399e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
400e3ec7017SPing-Ke Shih 
401e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, false);
4029d9a9edcSPing-Ke Shih 
4039d9a9edcSPing-Ke Shih 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED)
404e3ec7017SPing-Ke Shih 		ra->upd_mask = 1;
4059d9a9edcSPing-Ke Shih 	if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED))
4069d9a9edcSPing-Ke Shih 		ra->upd_bw_nss_mask = 1;
4079d9a9edcSPing-Ke Shih 
408e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
409e3ec7017SPing-Ke Shih 		    "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d",
410e3ec7017SPing-Ke Shih 		    ra->macid,
411e3ec7017SPing-Ke Shih 		    ra->bw_cap,
412e3ec7017SPing-Ke Shih 		    ra->ss_num,
413e3ec7017SPing-Ke Shih 		    ra->en_sgi,
414e3ec7017SPing-Ke Shih 		    ra->giltf);
415e3ec7017SPing-Ke Shih 
416e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, false);
417e3ec7017SPing-Ke Shih }
418e3ec7017SPing-Ke Shih 
419e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next,
420e3ec7017SPing-Ke Shih 				 u16 rate_base, u64 ra_mask, u8 ra_mode,
421e3ec7017SPing-Ke Shih 				 u32 rate_ctrl, u32 ctrl_skip, bool force)
422e3ec7017SPing-Ke Shih {
423e3ec7017SPing-Ke Shih 	u8 n, c;
424e3ec7017SPing-Ke Shih 
425e3ec7017SPing-Ke Shih 	if (rate_ctrl == ctrl_skip)
426e3ec7017SPing-Ke Shih 		return true;
427e3ec7017SPing-Ke Shih 
428e3ec7017SPing-Ke Shih 	n = hweight32(rate_ctrl);
429e3ec7017SPing-Ke Shih 	if (n == 0)
430e3ec7017SPing-Ke Shih 		return true;
431e3ec7017SPing-Ke Shih 
432e3ec7017SPing-Ke Shih 	if (force && n != 1)
433e3ec7017SPing-Ke Shih 		return false;
434e3ec7017SPing-Ke Shih 
435e3ec7017SPing-Ke Shih 	if (next->enable)
436e3ec7017SPing-Ke Shih 		return false;
437e3ec7017SPing-Ke Shih 
438e3ec7017SPing-Ke Shih 	c = __fls(rate_ctrl);
439e3ec7017SPing-Ke Shih 	next->rate = rate_base + c;
440e3ec7017SPing-Ke Shih 	next->ra_mode = ra_mode;
441e3ec7017SPing-Ke Shih 	next->ra_mask = ra_mask;
442e3ec7017SPing-Ke Shih 	next->enable = true;
443e3ec7017SPing-Ke Shih 
444e3ec7017SPing-Ke Shih 	return true;
445e3ec7017SPing-Ke Shih }
446e3ec7017SPing-Ke Shih 
447e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev,
448e3ec7017SPing-Ke Shih 				struct ieee80211_vif *vif,
449e3ec7017SPing-Ke Shih 				const struct cfg80211_bitrate_mask *mask)
450e3ec7017SPing-Ke Shih {
451e3ec7017SPing-Ke Shih 	struct ieee80211_supported_band *sband;
452e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
453e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern next_pattern = {0};
454cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
455e3ec7017SPing-Ke Shih 	static const u16 hw_rate_he[] = {RTW89_HW_RATE_HE_NSS1_MCS0,
456e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS2_MCS0,
457e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS3_MCS0,
458e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS4_MCS0};
459e3ec7017SPing-Ke Shih 	static const u16 hw_rate_vht[] = {RTW89_HW_RATE_VHT_NSS1_MCS0,
460e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS2_MCS0,
461e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS3_MCS0,
462e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS4_MCS0};
463e3ec7017SPing-Ke Shih 	static const u16 hw_rate_ht[] = {RTW89_HW_RATE_MCS0,
464e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS8,
465e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS16,
466e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS24};
467cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
468a06d2dd7SZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
469e3ec7017SPing-Ke Shih 	u8 tx_nss = rtwdev->hal.tx_nss;
470e3ec7017SPing-Ke Shih 	u8 i;
471e3ec7017SPing-Ke Shih 
472e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
473e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_he[i],
474e3ec7017SPing-Ke Shih 					  RA_MASK_HE_RATES, RTW89_RA_MODE_HE,
475a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].he_mcs[i],
476e3ec7017SPing-Ke Shih 					  0, true))
477e3ec7017SPing-Ke Shih 			goto out;
478e3ec7017SPing-Ke Shih 
479e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
480e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i],
481e3ec7017SPing-Ke Shih 					  RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT,
482a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].vht_mcs[i],
483e3ec7017SPing-Ke Shih 					  0, true))
484e3ec7017SPing-Ke Shih 			goto out;
485e3ec7017SPing-Ke Shih 
486e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
487e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i],
488e3ec7017SPing-Ke Shih 					  RA_MASK_HT_RATES, RTW89_RA_MODE_HT,
489a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].ht_mcs[i],
490e3ec7017SPing-Ke Shih 					  0, true))
491e3ec7017SPing-Ke Shih 			goto out;
492e3ec7017SPing-Ke Shih 
493e3ec7017SPing-Ke Shih 	/* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and
494e3ec7017SPing-Ke Shih 	 * require at least one basic rate for ieee80211_set_bitrate_mask,
495e3ec7017SPing-Ke Shih 	 * so the decision just depends on if all bitrates are set or not.
496e3ec7017SPing-Ke Shih 	 */
497a06d2dd7SZong-Zhe Yang 	sband = rtwdev->hw->wiphy->bands[nl_band];
498e3ec7017SPing-Ke Shih 	if (band == RTW89_BAND_2G) {
499e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1,
500e3ec7017SPing-Ke Shih 					  RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES,
501e3ec7017SPing-Ke Shih 					  RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM,
502a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
503e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
504e3ec7017SPing-Ke Shih 			goto out;
505e3ec7017SPing-Ke Shih 	} else {
506e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6,
507e3ec7017SPing-Ke Shih 					  RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM,
508a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
509e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
510e3ec7017SPing-Ke Shih 			goto out;
511e3ec7017SPing-Ke Shih 	}
512e3ec7017SPing-Ke Shih 
513e3ec7017SPing-Ke Shih 	if (!next_pattern.enable)
514e3ec7017SPing-Ke Shih 		goto out;
515e3ec7017SPing-Ke Shih 
516e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern = next_pattern;
517e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
518e3ec7017SPing-Ke Shih 		    "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n",
519e3ec7017SPing-Ke Shih 		    next_pattern.rate,
520e3ec7017SPing-Ke Shih 		    next_pattern.ra_mask,
521e3ec7017SPing-Ke Shih 		    next_pattern.ra_mode);
522e3ec7017SPing-Ke Shih 	return;
523e3ec7017SPing-Ke Shih 
524e3ec7017SPing-Ke Shih out:
525e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern.enable = false;
526e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n");
527e3ec7017SPing-Ke Shih }
528e3ec7017SPing-Ke Shih 
529e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta)
530e3ec7017SPing-Ke Shih {
531e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
532e3ec7017SPing-Ke Shih 
5339d9a9edcSPing-Ke Shih 	rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
534e3ec7017SPing-Ke Shih }
535e3ec7017SPing-Ke Shih 
536e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev)
537e3ec7017SPing-Ke Shih {
538e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
539e3ec7017SPing-Ke Shih 					  rtw89_phy_ra_updata_sta_iter,
540e3ec7017SPing-Ke Shih 					  rtwdev);
541e3ec7017SPing-Ke Shih }
542e3ec7017SPing-Ke Shih 
543e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta)
544e3ec7017SPing-Ke Shih {
545e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
546e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
547e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR;
548e3ec7017SPing-Ke Shih 	bool csi = rtw89_sta_has_beamformer_cap(sta);
549e3ec7017SPing-Ke Shih 
550e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, csi);
551e3ec7017SPing-Ke Shih 
552e3ec7017SPing-Ke Shih 	if (rssi > 40)
553e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 1;
554e3ec7017SPing-Ke Shih 	else if (rssi > 20)
555e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 2;
556e3ec7017SPing-Ke Shih 	else if (rssi > 1)
557e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 3;
558e3ec7017SPing-Ke Shih 	else
559e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 0;
560e3ec7017SPing-Ke Shih 	ra->upd_all = 1;
561e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
562e3ec7017SPing-Ke Shih 		    "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d",
563e3ec7017SPing-Ke Shih 		    ra->macid,
564e3ec7017SPing-Ke Shih 		    ra->mode_ctrl,
565e3ec7017SPing-Ke Shih 		    ra->bw_cap,
566e3ec7017SPing-Ke Shih 		    ra->ss_num,
567e3ec7017SPing-Ke Shih 		    ra->init_rate_lv);
568e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
569e3ec7017SPing-Ke Shih 		    "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d",
570e3ec7017SPing-Ke Shih 		    ra->dcm_cap,
571e3ec7017SPing-Ke Shih 		    ra->er_cap,
572e3ec7017SPing-Ke Shih 		    ra->ldpc_cap,
573e3ec7017SPing-Ke Shih 		    ra->stbc_cap,
574e3ec7017SPing-Ke Shih 		    ra->en_sgi,
575e3ec7017SPing-Ke Shih 		    ra->giltf);
576e3ec7017SPing-Ke Shih 
577e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, csi);
578e3ec7017SPing-Ke Shih }
579e3ec7017SPing-Ke Shih 
580e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev,
5813e5831caSZong-Zhe Yang 		      const struct rtw89_chan *chan,
582e3ec7017SPing-Ke Shih 		      enum rtw89_bandwidth dbw)
583e3ec7017SPing-Ke Shih {
5843e5831caSZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
5853e5831caSZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
5863e5831caSZong-Zhe Yang 	u8 central_ch = chan->channel;
587e3ec7017SPing-Ke Shih 	u8 txsc_idx = 0;
588e3ec7017SPing-Ke Shih 	u8 tmp = 0;
589e3ec7017SPing-Ke Shih 
590e3ec7017SPing-Ke Shih 	if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20)
591e3ec7017SPing-Ke Shih 		return txsc_idx;
592e3ec7017SPing-Ke Shih 
593e3ec7017SPing-Ke Shih 	switch (cbw) {
594e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
595e3ec7017SPing-Ke Shih 		txsc_idx = pri_ch > central_ch ? 1 : 2;
596e3ec7017SPing-Ke Shih 		break;
597e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
598e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
599e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
600e3ec7017SPing-Ke Shih 				txsc_idx = (pri_ch - central_ch) >> 1;
601e3ec7017SPing-Ke Shih 			else
602e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 1;
603e3ec7017SPing-Ke Shih 		} else {
604e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 9 : 10;
605e3ec7017SPing-Ke Shih 		}
606e3ec7017SPing-Ke Shih 		break;
607e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
608e3ec7017SPing-Ke Shih 		if (pri_ch > central_ch)
609e3ec7017SPing-Ke Shih 			tmp = (pri_ch - central_ch) >> 1;
610e3ec7017SPing-Ke Shih 		else
611e3ec7017SPing-Ke Shih 			tmp = ((central_ch - pri_ch) >> 1) + 1;
612e3ec7017SPing-Ke Shih 
613e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
614e3ec7017SPing-Ke Shih 			txsc_idx = tmp;
615e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
616e3ec7017SPing-Ke Shih 			if (tmp == 1 || tmp == 3)
617e3ec7017SPing-Ke Shih 				txsc_idx = 9;
618e3ec7017SPing-Ke Shih 			else if (tmp == 5 || tmp == 7)
619e3ec7017SPing-Ke Shih 				txsc_idx = 11;
620e3ec7017SPing-Ke Shih 			else if (tmp == 2 || tmp == 4)
621e3ec7017SPing-Ke Shih 				txsc_idx = 10;
622e3ec7017SPing-Ke Shih 			else if (tmp == 6 || tmp == 8)
623e3ec7017SPing-Ke Shih 				txsc_idx = 12;
624e3ec7017SPing-Ke Shih 			else
625e3ec7017SPing-Ke Shih 				return 0xff;
626e3ec7017SPing-Ke Shih 		} else {
627e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 13 : 14;
628e3ec7017SPing-Ke Shih 		}
629e3ec7017SPing-Ke Shih 		break;
630e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80_80:
631e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
632e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
633e3ec7017SPing-Ke Shih 				txsc_idx = (10 - (pri_ch - central_ch)) >> 1;
634e3ec7017SPing-Ke Shih 			else
635e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 5;
636e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
637e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 10 : 12;
638e3ec7017SPing-Ke Shih 		} else {
639e3ec7017SPing-Ke Shih 			txsc_idx = 14;
640e3ec7017SPing-Ke Shih 		}
641e3ec7017SPing-Ke Shih 		break;
642e3ec7017SPing-Ke Shih 	default:
643e3ec7017SPing-Ke Shih 		break;
644e3ec7017SPing-Ke Shih 	}
645e3ec7017SPing-Ke Shih 
646e3ec7017SPing-Ke Shih 	return txsc_idx;
647e3ec7017SPing-Ke Shih }
648861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc);
649e3ec7017SPing-Ke Shih 
65084d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev)
65184d0e33eSChung-Hsuan Hung {
65284d0e33eSChung-Hsuan Hung 	return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) ||
65384d0e33eSChung-Hsuan Hung 	       !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1);
65484d0e33eSChung-Hsuan Hung }
65584d0e33eSChung-Hsuan Hung 
656e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
657e3ec7017SPing-Ke Shih 		      u32 addr, u32 mask)
658e3ec7017SPing-Ke Shih {
659e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
660e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
661e3ec7017SPing-Ke Shih 	u32 val, direct_addr;
662e3ec7017SPing-Ke Shih 
663e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
664e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
665e3ec7017SPing-Ke Shih 		return INV_RF_DATA;
666e3ec7017SPing-Ke Shih 	}
667e3ec7017SPing-Ke Shih 
668e3ec7017SPing-Ke Shih 	addr &= 0xff;
669e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
670e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
671e3ec7017SPing-Ke Shih 
672e3ec7017SPing-Ke Shih 	val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask);
673e3ec7017SPing-Ke Shih 
674e3ec7017SPing-Ke Shih 	return val;
675e3ec7017SPing-Ke Shih }
676e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf);
677e3ec7017SPing-Ke Shih 
67884d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev,
67984d0e33eSChung-Hsuan Hung 			       enum rtw89_rf_path rf_path, u32 addr, u32 mask)
68084d0e33eSChung-Hsuan Hung {
68184d0e33eSChung-Hsuan Hung 	bool busy;
68284d0e33eSChung-Hsuan Hung 	bool done;
68384d0e33eSChung-Hsuan Hung 	u32 val;
68484d0e33eSChung-Hsuan Hung 	int ret;
68584d0e33eSChung-Hsuan Hung 
68684d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
68784d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
68884d0e33eSChung-Hsuan Hung 	if (ret) {
68984d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read rf busy swsi\n");
69084d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
69184d0e33eSChung-Hsuan Hung 	}
69284d0e33eSChung-Hsuan Hung 
69384d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
69484d0e33eSChung-Hsuan Hung 
69584d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) |
69684d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr);
69784d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val);
69884d0e33eSChung-Hsuan Hung 	udelay(2);
69984d0e33eSChung-Hsuan Hung 
70084d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1,
70184d0e33eSChung-Hsuan Hung 				       30, false, rtwdev, R_SWSI_V1,
70284d0e33eSChung-Hsuan Hung 				       B_SWSI_R_DATA_DONE_V1);
70384d0e33eSChung-Hsuan Hung 	if (ret) {
70484d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read swsi busy\n");
70584d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
70684d0e33eSChung-Hsuan Hung 	}
70784d0e33eSChung-Hsuan Hung 
70884d0e33eSChung-Hsuan Hung 	return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask);
70984d0e33eSChung-Hsuan Hung }
71084d0e33eSChung-Hsuan Hung 
71184d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
71284d0e33eSChung-Hsuan Hung 			 u32 addr, u32 mask)
71384d0e33eSChung-Hsuan Hung {
71484d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
71584d0e33eSChung-Hsuan Hung 
71684d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
71784d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
71884d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
71984d0e33eSChung-Hsuan Hung 	}
72084d0e33eSChung-Hsuan Hung 
72184d0e33eSChung-Hsuan Hung 	if (ad_sel)
72284d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask);
72384d0e33eSChung-Hsuan Hung 	else
72484d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask);
72584d0e33eSChung-Hsuan Hung }
72684d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1);
72784d0e33eSChung-Hsuan Hung 
728e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
729e3ec7017SPing-Ke Shih 			u32 addr, u32 mask, u32 data)
730e3ec7017SPing-Ke Shih {
731e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
732e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
733e3ec7017SPing-Ke Shih 	u32 direct_addr;
734e3ec7017SPing-Ke Shih 
735e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
736e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
737e3ec7017SPing-Ke Shih 		return false;
738e3ec7017SPing-Ke Shih 	}
739e3ec7017SPing-Ke Shih 
740e3ec7017SPing-Ke Shih 	addr &= 0xff;
741e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
742e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
743e3ec7017SPing-Ke Shih 
744e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data);
745e3ec7017SPing-Ke Shih 
746e3ec7017SPing-Ke Shih 	/* delay to ensure writing properly */
747e3ec7017SPing-Ke Shih 	udelay(1);
748e3ec7017SPing-Ke Shih 
749e3ec7017SPing-Ke Shih 	return true;
750e3ec7017SPing-Ke Shih }
751e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf);
752e3ec7017SPing-Ke Shih 
75384d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev,
75484d0e33eSChung-Hsuan Hung 				 enum rtw89_rf_path rf_path, u32 addr, u32 mask,
75584d0e33eSChung-Hsuan Hung 				 u32 data)
75684d0e33eSChung-Hsuan Hung {
75784d0e33eSChung-Hsuan Hung 	u8 bit_shift;
75884d0e33eSChung-Hsuan Hung 	u32 val;
75984d0e33eSChung-Hsuan Hung 	bool busy, b_msk_en = false;
76084d0e33eSChung-Hsuan Hung 	int ret;
76184d0e33eSChung-Hsuan Hung 
76284d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
76384d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
76484d0e33eSChung-Hsuan Hung 	if (ret) {
76584d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "write rf busy swsi\n");
76684d0e33eSChung-Hsuan Hung 		return false;
76784d0e33eSChung-Hsuan Hung 	}
76884d0e33eSChung-Hsuan Hung 
76984d0e33eSChung-Hsuan Hung 	data &= RFREG_MASK;
77084d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
77184d0e33eSChung-Hsuan Hung 
77284d0e33eSChung-Hsuan Hung 	if (mask != RFREG_MASK) {
77384d0e33eSChung-Hsuan Hung 		b_msk_en = true;
77484d0e33eSChung-Hsuan Hung 		rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK,
77584d0e33eSChung-Hsuan Hung 				       mask);
77684d0e33eSChung-Hsuan Hung 		bit_shift = __ffs(mask);
77784d0e33eSChung-Hsuan Hung 		data = (data << bit_shift) & RFREG_MASK;
77884d0e33eSChung-Hsuan Hung 	}
77984d0e33eSChung-Hsuan Hung 
78084d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) |
78184d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) |
78284d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) |
78384d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_VAL_V1, data);
78484d0e33eSChung-Hsuan Hung 
78584d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val);
78684d0e33eSChung-Hsuan Hung 
78784d0e33eSChung-Hsuan Hung 	return true;
78884d0e33eSChung-Hsuan Hung }
78984d0e33eSChung-Hsuan Hung 
79084d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
79184d0e33eSChung-Hsuan Hung 			   u32 addr, u32 mask, u32 data)
79284d0e33eSChung-Hsuan Hung {
79384d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
79484d0e33eSChung-Hsuan Hung 
79584d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
79684d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
79784d0e33eSChung-Hsuan Hung 		return false;
79884d0e33eSChung-Hsuan Hung 	}
79984d0e33eSChung-Hsuan Hung 
80084d0e33eSChung-Hsuan Hung 	if (ad_sel)
80184d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data);
80284d0e33eSChung-Hsuan Hung 	else
80384d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data);
80484d0e33eSChung-Hsuan Hung }
80584d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1);
80684d0e33eSChung-Hsuan Hung 
807d9112042SChih-Kang Chang static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev)
808d9112042SChih-Kang Chang {
809d9112042SChih-Kang Chang 	return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1;
810d9112042SChih-Kang Chang }
811d9112042SChih-Kang Chang 
812e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev,
813e3ec7017SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx)
814e3ec7017SPing-Ke Shih {
815e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
816e3ec7017SPing-Ke Shih 
817e3ec7017SPing-Ke Shih 	chip->ops->bb_reset(rtwdev, phy_idx);
818e3ec7017SPing-Ke Shih }
819e3ec7017SPing-Ke Shih 
820e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev,
821e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
822e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
823e3ec7017SPing-Ke Shih 				    void *extra_data)
824e3ec7017SPing-Ke Shih {
825e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe)
826e3ec7017SPing-Ke Shih 		mdelay(50);
827e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfd)
828e3ec7017SPing-Ke Shih 		mdelay(5);
829e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfc)
830e3ec7017SPing-Ke Shih 		mdelay(1);
831e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfb)
832e3ec7017SPing-Ke Shih 		udelay(50);
833e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfa)
834e3ec7017SPing-Ke Shih 		udelay(5);
835e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xf9)
836e3ec7017SPing-Ke Shih 		udelay(1);
837e3ec7017SPing-Ke Shih 	else
838e3ec7017SPing-Ke Shih 		rtw89_phy_write32(rtwdev, reg->addr, reg->data);
839e3ec7017SPing-Ke Shih }
840e3ec7017SPing-Ke Shih 
841e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg {
842e885871eSZong-Zhe Yang 	u32 addr;
843e885871eSZong-Zhe Yang 	struct {
844e885871eSZong-Zhe Yang 		union {
845e885871eSZong-Zhe Yang 			u8 type;
846e885871eSZong-Zhe Yang 			struct {
847e885871eSZong-Zhe Yang 				u8 rxsc_start:4;
848e885871eSZong-Zhe Yang 				u8 bw:4;
849e885871eSZong-Zhe Yang 			};
850e885871eSZong-Zhe Yang 		};
851e885871eSZong-Zhe Yang 		u8 path;
852e885871eSZong-Zhe Yang 		u8 gain_band;
853e885871eSZong-Zhe Yang 		u8 cfg_type;
854e885871eSZong-Zhe Yang 	};
855e885871eSZong-Zhe Yang } __packed;
856e885871eSZong-Zhe Yang 
857e885871eSZong-Zhe Yang static void
858e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev,
859e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
860e885871eSZong-Zhe Yang {
861e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
862e885871eSZong-Zhe Yang 	u8 type = arg.type;
863e885871eSZong-Zhe Yang 	u8 path = arg.path;
864e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
865e885871eSZong-Zhe Yang 	int i;
866e885871eSZong-Zhe Yang 
867e885871eSZong-Zhe Yang 	switch (type) {
868e885871eSZong-Zhe Yang 	case 0:
869e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
870e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
871e885871eSZong-Zhe Yang 		break;
872e885871eSZong-Zhe Yang 	case 1:
873e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
874e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
875e885871eSZong-Zhe Yang 		break;
876e885871eSZong-Zhe Yang 	case 2:
877e885871eSZong-Zhe Yang 		for (i = 0; i < 2; i++, data >>= 8)
878e885871eSZong-Zhe Yang 			gain->tia_gain[gband][path][i] = data & 0xff;
879e885871eSZong-Zhe Yang 		break;
880e885871eSZong-Zhe Yang 	default:
881e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
882e885871eSZong-Zhe Yang 			   "bb gain error {0x%x:0x%x} with unknown type: %d\n",
883e885871eSZong-Zhe Yang 			   arg.addr, data, type);
884e885871eSZong-Zhe Yang 		break;
885e885871eSZong-Zhe Yang 	}
886e885871eSZong-Zhe Yang }
887e885871eSZong-Zhe Yang 
888e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx {
889e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_FULL = 0,
890e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20 = 1,
891e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20_1 = 5,
892e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_40 = 9,
893e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_80 = 13,
894e885871eSZong-Zhe Yang };
895e885871eSZong-Zhe Yang 
896e885871eSZong-Zhe Yang static void
897e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev,
898e885871eSZong-Zhe Yang 			  union rtw89_phy_bb_gain_arg arg, u32 data)
899e885871eSZong-Zhe Yang {
900e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
901e885871eSZong-Zhe Yang 	u8 rxsc_start = arg.rxsc_start;
902e885871eSZong-Zhe Yang 	u8 bw = arg.bw;
903e885871eSZong-Zhe Yang 	u8 path = arg.path;
904e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
905e885871eSZong-Zhe Yang 	u8 rxsc;
906e885871eSZong-Zhe Yang 	s8 ofst;
907e885871eSZong-Zhe Yang 	int i;
908e885871eSZong-Zhe Yang 
909e885871eSZong-Zhe Yang 	switch (bw) {
910e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_20:
911e885871eSZong-Zhe Yang 		gain->rpl_ofst_20[gband][path] = (s8)data;
912e885871eSZong-Zhe Yang 		break;
913e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_40:
914e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
915e885871eSZong-Zhe Yang 			gain->rpl_ofst_40[gband][path][0] = (s8)data;
916e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
917e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
918e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
919e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
920e885871eSZong-Zhe Yang 				gain->rpl_ofst_40[gband][path][rxsc] = ofst;
921e885871eSZong-Zhe Yang 			}
922e885871eSZong-Zhe Yang 		}
923e885871eSZong-Zhe Yang 		break;
924e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_80:
925e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
926e885871eSZong-Zhe Yang 			gain->rpl_ofst_80[gband][path][0] = (s8)data;
927e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
928e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
929e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
930e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
931e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
932e885871eSZong-Zhe Yang 			}
933e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
934e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
935e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
936e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
937e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
938e885871eSZong-Zhe Yang 			}
939e885871eSZong-Zhe Yang 		}
940e885871eSZong-Zhe Yang 		break;
941e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
942e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
943e885871eSZong-Zhe Yang 			gain->rpl_ofst_160[gband][path][0] = (s8)data;
944e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
945e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
946e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
947e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
948e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
949e885871eSZong-Zhe Yang 			}
950e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) {
951e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
952e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i;
953e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
954e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
955e885871eSZong-Zhe Yang 			}
956e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
957e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
958e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
959e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
960e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
961e885871eSZong-Zhe Yang 			}
962e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) {
963e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
964e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_80 + i;
965e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
966e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
967e885871eSZong-Zhe Yang 			}
968e885871eSZong-Zhe Yang 		}
969e885871eSZong-Zhe Yang 		break;
970e885871eSZong-Zhe Yang 	default:
971e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
972e885871eSZong-Zhe Yang 			   "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n",
973e885871eSZong-Zhe Yang 			   arg.addr, data, bw);
974e885871eSZong-Zhe Yang 		break;
975e885871eSZong-Zhe Yang 	}
976e885871eSZong-Zhe Yang }
977e885871eSZong-Zhe Yang 
978e885871eSZong-Zhe Yang static void
979e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev,
980e885871eSZong-Zhe Yang 			     union rtw89_phy_bb_gain_arg arg, u32 data)
981e885871eSZong-Zhe Yang {
982e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
983e885871eSZong-Zhe Yang 	u8 type = arg.type;
984e885871eSZong-Zhe Yang 	u8 path = arg.path;
985e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
986e885871eSZong-Zhe Yang 	int i;
987e885871eSZong-Zhe Yang 
988e885871eSZong-Zhe Yang 	switch (type) {
989e885871eSZong-Zhe Yang 	case 0:
990e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
991e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
992e885871eSZong-Zhe Yang 		break;
993e885871eSZong-Zhe Yang 	case 1:
994e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
995e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
996e885871eSZong-Zhe Yang 		break;
997e885871eSZong-Zhe Yang 	default:
998e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
999e885871eSZong-Zhe Yang 			   "bb gain bypass {0x%x:0x%x} with unknown type: %d\n",
1000e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1001e885871eSZong-Zhe Yang 		break;
1002e885871eSZong-Zhe Yang 	}
1003e885871eSZong-Zhe Yang }
1004e885871eSZong-Zhe Yang 
1005e885871eSZong-Zhe Yang static void
1006e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev,
1007e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
1008e885871eSZong-Zhe Yang {
1009e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
1010e885871eSZong-Zhe Yang 	u8 type = arg.type;
1011e885871eSZong-Zhe Yang 	u8 path = arg.path;
1012e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
1013e885871eSZong-Zhe Yang 	int i;
1014e885871eSZong-Zhe Yang 
1015e885871eSZong-Zhe Yang 	switch (type) {
1016e885871eSZong-Zhe Yang 	case 0:
1017e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1018e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1019e885871eSZong-Zhe Yang 		break;
1020e885871eSZong-Zhe Yang 	case 1:
1021e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
1022e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1023e885871eSZong-Zhe Yang 		break;
1024e885871eSZong-Zhe Yang 	case 2:
1025e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1026e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1027e885871eSZong-Zhe Yang 		break;
1028e885871eSZong-Zhe Yang 	case 3:
1029e885871eSZong-Zhe Yang 		for (i = 4; i < 8; i++, data >>= 8)
1030e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1031e885871eSZong-Zhe Yang 		break;
1032e885871eSZong-Zhe Yang 	default:
1033e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1034e885871eSZong-Zhe Yang 			   "bb gain op1db {0x%x:0x%x} with unknown type: %d\n",
1035e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1036e885871eSZong-Zhe Yang 		break;
1037e885871eSZong-Zhe Yang 	}
1038e885871eSZong-Zhe Yang }
1039e885871eSZong-Zhe Yang 
1040e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev,
1041e885871eSZong-Zhe Yang 				     const struct rtw89_reg2_def *reg,
1042e885871eSZong-Zhe Yang 				     enum rtw89_rf_path rf_path,
1043e885871eSZong-Zhe Yang 				     void *extra_data)
1044e885871eSZong-Zhe Yang {
1045e885871eSZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
1046e885871eSZong-Zhe Yang 	union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr };
1047c6a9d360SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
1048e885871eSZong-Zhe Yang 
1049e885871eSZong-Zhe Yang 	if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR)
1050e885871eSZong-Zhe Yang 		return;
1051e885871eSZong-Zhe Yang 
1052e885871eSZong-Zhe Yang 	if (arg.path >= chip->rf_path_num)
1053e885871eSZong-Zhe Yang 		return;
1054e885871eSZong-Zhe Yang 
1055e885871eSZong-Zhe Yang 	if (arg.addr >= 0xf9 && arg.addr <= 0xfe) {
1056e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev, "bb gain table with flow ctrl\n");
1057e885871eSZong-Zhe Yang 		return;
1058e885871eSZong-Zhe Yang 	}
1059e885871eSZong-Zhe Yang 
1060e885871eSZong-Zhe Yang 	switch (arg.cfg_type) {
1061e885871eSZong-Zhe Yang 	case 0:
1062e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data);
1063e885871eSZong-Zhe Yang 		break;
1064e885871eSZong-Zhe Yang 	case 1:
1065e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data);
1066e885871eSZong-Zhe Yang 		break;
1067e885871eSZong-Zhe Yang 	case 2:
1068e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data);
1069e885871eSZong-Zhe Yang 		break;
1070e885871eSZong-Zhe Yang 	case 3:
1071e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data);
1072e885871eSZong-Zhe Yang 		break;
1073c6a9d360SPing-Ke Shih 	case 4:
1074c6a9d360SPing-Ke Shih 		/* This cfg_type is only used by rfe_type >= 50 with eFEM */
1075c6a9d360SPing-Ke Shih 		if (efuse->rfe_type < 50)
1076c6a9d360SPing-Ke Shih 			break;
1077c6a9d360SPing-Ke Shih 		fallthrough;
1078e885871eSZong-Zhe Yang 	default:
1079e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1080e885871eSZong-Zhe Yang 			   "bb gain {0x%x:0x%x} with unknown cfg type: %d\n",
1081e885871eSZong-Zhe Yang 			   arg.addr, reg->data, arg.cfg_type);
1082e885871eSZong-Zhe Yang 		break;
1083e885871eSZong-Zhe Yang 	}
1084e885871eSZong-Zhe Yang }
1085e885871eSZong-Zhe Yang 
1086e3ec7017SPing-Ke Shih static void
1087e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev,
1088e3ec7017SPing-Ke Shih 			     const struct rtw89_reg2_def *reg,
1089e3ec7017SPing-Ke Shih 			     enum rtw89_rf_path rf_path,
1090e3ec7017SPing-Ke Shih 			     struct rtw89_fw_h2c_rf_reg_info *info)
1091e3ec7017SPing-Ke Shih {
1092e3ec7017SPing-Ke Shih 	u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE;
1093e3ec7017SPing-Ke Shih 	u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE;
1094e3ec7017SPing-Ke Shih 
109530101812SPing-Ke Shih 	if (page >= RTW89_H2C_RF_PAGE_NUM) {
109630101812SPing-Ke Shih 		rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d",
109730101812SPing-Ke Shih 			   rf_path, info->curr_idx);
109830101812SPing-Ke Shih 		return;
109930101812SPing-Ke Shih 	}
110030101812SPing-Ke Shih 
1101e3ec7017SPing-Ke Shih 	info->rtw89_phy_config_rf_h2c[page][idx] =
1102e3ec7017SPing-Ke Shih 		cpu_to_le32((reg->addr << 20) | reg->data);
1103e3ec7017SPing-Ke Shih 	info->curr_idx++;
1104e3ec7017SPing-Ke Shih }
1105e3ec7017SPing-Ke Shih 
1106e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev,
1107e3ec7017SPing-Ke Shih 				      struct rtw89_fw_h2c_rf_reg_info *info)
1108e3ec7017SPing-Ke Shih {
110930101812SPing-Ke Shih 	u16 remain = info->curr_idx;
111030101812SPing-Ke Shih 	u16 len = 0;
1111e3ec7017SPing-Ke Shih 	u8 i;
1112e3ec7017SPing-Ke Shih 	int ret = 0;
1113e3ec7017SPing-Ke Shih 
111430101812SPing-Ke Shih 	if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) {
1115e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev,
111630101812SPing-Ke Shih 			   "rf reg h2c total len %d larger than %d\n",
111730101812SPing-Ke Shih 			   remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE);
111830101812SPing-Ke Shih 		ret = -EINVAL;
111930101812SPing-Ke Shih 		goto out;
1120e3ec7017SPing-Ke Shih 	}
1121e3ec7017SPing-Ke Shih 
112230101812SPing-Ke Shih 	for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) {
112330101812SPing-Ke Shih 		len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain;
112430101812SPing-Ke Shih 		ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i);
1125e3ec7017SPing-Ke Shih 		if (ret)
112630101812SPing-Ke Shih 			goto out;
1127e3ec7017SPing-Ke Shih 	}
112830101812SPing-Ke Shih out:
1129e3ec7017SPing-Ke Shih 	info->curr_idx = 0;
1130e3ec7017SPing-Ke Shih 
113130101812SPing-Ke Shih 	return ret;
1132e3ec7017SPing-Ke Shih }
1133e3ec7017SPing-Ke Shih 
1134d9112042SChih-Kang Chang static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev,
1135d9112042SChih-Kang Chang 					 const struct rtw89_reg2_def *reg,
1136d9112042SChih-Kang Chang 					 enum rtw89_rf_path rf_path,
1137d9112042SChih-Kang Chang 					 void *extra_data)
1138d9112042SChih-Kang Chang {
1139d9112042SChih-Kang Chang 	u32 addr = reg->addr;
1140d9112042SChih-Kang Chang 
1141d9112042SChih-Kang Chang 	if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb ||
1142d9112042SChih-Kang Chang 	    addr == 0xfa || addr == 0xf9)
1143d9112042SChih-Kang Chang 		return;
1144d9112042SChih-Kang Chang 
1145d9112042SChih-Kang Chang 	if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100)
1146d9112042SChih-Kang Chang 		return;
1147d9112042SChih-Kang Chang 
1148d9112042SChih-Kang Chang 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1149d9112042SChih-Kang Chang 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1150d9112042SChih-Kang Chang }
1151d9112042SChih-Kang Chang 
1152e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev,
1153e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
1154e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
1155e3ec7017SPing-Ke Shih 				    void *extra_data)
1156e3ec7017SPing-Ke Shih {
1157e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe) {
1158e3ec7017SPing-Ke Shih 		mdelay(50);
1159e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfd) {
1160e3ec7017SPing-Ke Shih 		mdelay(5);
1161e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfc) {
1162e3ec7017SPing-Ke Shih 		mdelay(1);
1163e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfb) {
1164e3ec7017SPing-Ke Shih 		udelay(50);
1165e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfa) {
1166e3ec7017SPing-Ke Shih 		udelay(5);
1167e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xf9) {
1168e3ec7017SPing-Ke Shih 		udelay(1);
1169e3ec7017SPing-Ke Shih 	} else {
1170e3ec7017SPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data);
1171e3ec7017SPing-Ke Shih 		rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1172e3ec7017SPing-Ke Shih 					     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1173e3ec7017SPing-Ke Shih 	}
1174e3ec7017SPing-Ke Shih }
1175e3ec7017SPing-Ke Shih 
11762a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev,
11772a5f2b32SPing-Ke Shih 				const struct rtw89_reg2_def *reg,
11782a5f2b32SPing-Ke Shih 				enum rtw89_rf_path rf_path,
11792a5f2b32SPing-Ke Shih 				void *extra_data)
11802a5f2b32SPing-Ke Shih {
11812a5f2b32SPing-Ke Shih 	rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data);
11822a5f2b32SPing-Ke Shih 
11832a5f2b32SPing-Ke Shih 	if (reg->addr < 0x100)
11842a5f2b32SPing-Ke Shih 		return;
11852a5f2b32SPing-Ke Shih 
11862a5f2b32SPing-Ke Shih 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
11872a5f2b32SPing-Ke Shih 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
11882a5f2b32SPing-Ke Shih }
11892a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1);
11902a5f2b32SPing-Ke Shih 
1191e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev,
1192e3ec7017SPing-Ke Shih 				  const struct rtw89_phy_table *table,
1193e3ec7017SPing-Ke Shih 				  u32 *headline_size, u32 *headline_idx,
1194e3ec7017SPing-Ke Shih 				  u8 rfe, u8 cv)
1195e3ec7017SPing-Ke Shih {
1196e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1197e3ec7017SPing-Ke Shih 	u32 headline;
1198e3ec7017SPing-Ke Shih 	u32 compare, target;
1199e3ec7017SPing-Ke Shih 	u8 rfe_para, cv_para;
1200e3ec7017SPing-Ke Shih 	u8 cv_max = 0;
1201e3ec7017SPing-Ke Shih 	bool case_matched = false;
1202e3ec7017SPing-Ke Shih 	u32 i;
1203e3ec7017SPing-Ke Shih 
1204e3ec7017SPing-Ke Shih 	for (i = 0; i < table->n_regs; i++) {
1205e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1206e3ec7017SPing-Ke Shih 		headline = get_phy_headline(reg->addr);
1207e3ec7017SPing-Ke Shih 		if (headline != PHY_HEADLINE_VALID)
1208e3ec7017SPing-Ke Shih 			break;
1209e3ec7017SPing-Ke Shih 	}
1210e3ec7017SPing-Ke Shih 	*headline_size = i;
1211e3ec7017SPing-Ke Shih 	if (*headline_size == 0)
1212e3ec7017SPing-Ke Shih 		return 0;
1213e3ec7017SPing-Ke Shih 
1214e3ec7017SPing-Ke Shih 	/* case 1: RFE match, CV match */
1215e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, cv);
1216e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1217e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1218e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1219e3ec7017SPing-Ke Shih 		if (target == compare) {
1220e3ec7017SPing-Ke Shih 			*headline_idx = i;
1221e3ec7017SPing-Ke Shih 			return 0;
1222e3ec7017SPing-Ke Shih 		}
1223e3ec7017SPing-Ke Shih 	}
1224e3ec7017SPing-Ke Shih 
1225e3ec7017SPing-Ke Shih 	/* case 2: RFE match, CV don't care */
1226e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, PHY_COND_DONT_CARE);
1227e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1228e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1229e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1230e3ec7017SPing-Ke Shih 		if (target == compare) {
1231e3ec7017SPing-Ke Shih 			*headline_idx = i;
1232e3ec7017SPing-Ke Shih 			return 0;
1233e3ec7017SPing-Ke Shih 		}
1234e3ec7017SPing-Ke Shih 	}
1235e3ec7017SPing-Ke Shih 
1236e3ec7017SPing-Ke Shih 	/* case 3: RFE match, CV max in table */
1237e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1238e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1239e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1240e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1241e3ec7017SPing-Ke Shih 		if (rfe_para == rfe) {
1242e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1243e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1244e3ec7017SPing-Ke Shih 				*headline_idx = i;
1245e3ec7017SPing-Ke Shih 				case_matched = true;
1246e3ec7017SPing-Ke Shih 			}
1247e3ec7017SPing-Ke Shih 		}
1248e3ec7017SPing-Ke Shih 	}
1249e3ec7017SPing-Ke Shih 
1250e3ec7017SPing-Ke Shih 	if (case_matched)
1251e3ec7017SPing-Ke Shih 		return 0;
1252e3ec7017SPing-Ke Shih 
1253e3ec7017SPing-Ke Shih 	/* case 4: RFE don't care, CV max in table */
1254e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1255e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1256e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1257e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1258e3ec7017SPing-Ke Shih 		if (rfe_para == PHY_COND_DONT_CARE) {
1259e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1260e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1261e3ec7017SPing-Ke Shih 				*headline_idx = i;
1262e3ec7017SPing-Ke Shih 				case_matched = true;
1263e3ec7017SPing-Ke Shih 			}
1264e3ec7017SPing-Ke Shih 		}
1265e3ec7017SPing-Ke Shih 	}
1266e3ec7017SPing-Ke Shih 
1267e3ec7017SPing-Ke Shih 	if (case_matched)
1268e3ec7017SPing-Ke Shih 		return 0;
1269e3ec7017SPing-Ke Shih 
1270e3ec7017SPing-Ke Shih 	return -EINVAL;
1271e3ec7017SPing-Ke Shih }
1272e3ec7017SPing-Ke Shih 
1273e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev,
1274e3ec7017SPing-Ke Shih 			       const struct rtw89_phy_table *table,
1275e3ec7017SPing-Ke Shih 			       void (*config)(struct rtw89_dev *rtwdev,
1276e3ec7017SPing-Ke Shih 					      const struct rtw89_reg2_def *reg,
1277e3ec7017SPing-Ke Shih 					      enum rtw89_rf_path rf_path,
1278e3ec7017SPing-Ke Shih 					      void *data),
1279e3ec7017SPing-Ke Shih 			       void *extra_data)
1280e3ec7017SPing-Ke Shih {
1281e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1282e3ec7017SPing-Ke Shih 	enum rtw89_rf_path rf_path = table->rf_path;
1283e3ec7017SPing-Ke Shih 	u8 rfe = rtwdev->efuse.rfe_type;
1284e3ec7017SPing-Ke Shih 	u8 cv = rtwdev->hal.cv;
1285e3ec7017SPing-Ke Shih 	u32 i;
1286e3ec7017SPing-Ke Shih 	u32 headline_size = 0, headline_idx = 0;
1287e3ec7017SPing-Ke Shih 	u32 target = 0, cfg_target;
1288e3ec7017SPing-Ke Shih 	u8 cond;
1289e3ec7017SPing-Ke Shih 	bool is_matched = true;
1290e3ec7017SPing-Ke Shih 	bool target_found = false;
1291e3ec7017SPing-Ke Shih 	int ret;
1292e3ec7017SPing-Ke Shih 
1293e3ec7017SPing-Ke Shih 	ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size,
1294e3ec7017SPing-Ke Shih 				     &headline_idx, rfe, cv);
1295e3ec7017SPing-Ke Shih 	if (ret) {
1296e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv);
1297e3ec7017SPing-Ke Shih 		return;
1298e3ec7017SPing-Ke Shih 	}
1299e3ec7017SPing-Ke Shih 
1300e3ec7017SPing-Ke Shih 	cfg_target = get_phy_target(table->regs[headline_idx].addr);
1301e3ec7017SPing-Ke Shih 	for (i = headline_size; i < table->n_regs; i++) {
1302e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1303e3ec7017SPing-Ke Shih 		cond = get_phy_cond(reg->addr);
1304e3ec7017SPing-Ke Shih 		switch (cond) {
1305e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_IF:
1306e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELIF:
1307e3ec7017SPing-Ke Shih 			target = get_phy_target(reg->addr);
1308e3ec7017SPing-Ke Shih 			break;
1309e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELSE:
1310e3ec7017SPing-Ke Shih 			is_matched = false;
1311e3ec7017SPing-Ke Shih 			if (!target_found) {
1312e3ec7017SPing-Ke Shih 				rtw89_warn(rtwdev, "failed to load CR %x/%x\n",
1313e3ec7017SPing-Ke Shih 					   reg->addr, reg->data);
1314e3ec7017SPing-Ke Shih 				return;
1315e3ec7017SPing-Ke Shih 			}
1316e3ec7017SPing-Ke Shih 			break;
1317e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_END:
1318e3ec7017SPing-Ke Shih 			is_matched = true;
1319e3ec7017SPing-Ke Shih 			target_found = false;
1320e3ec7017SPing-Ke Shih 			break;
1321e3ec7017SPing-Ke Shih 		case PHY_COND_CHECK:
1322e3ec7017SPing-Ke Shih 			if (target_found) {
1323e3ec7017SPing-Ke Shih 				is_matched = false;
1324e3ec7017SPing-Ke Shih 				break;
1325e3ec7017SPing-Ke Shih 			}
1326e3ec7017SPing-Ke Shih 
1327e3ec7017SPing-Ke Shih 			if (target == cfg_target) {
1328e3ec7017SPing-Ke Shih 				is_matched = true;
1329e3ec7017SPing-Ke Shih 				target_found = true;
1330e3ec7017SPing-Ke Shih 			} else {
1331e3ec7017SPing-Ke Shih 				is_matched = false;
1332e3ec7017SPing-Ke Shih 				target_found = false;
1333e3ec7017SPing-Ke Shih 			}
1334e3ec7017SPing-Ke Shih 			break;
1335e3ec7017SPing-Ke Shih 		default:
1336e3ec7017SPing-Ke Shih 			if (is_matched)
1337e3ec7017SPing-Ke Shih 				config(rtwdev, reg, rf_path, extra_data);
1338e3ec7017SPing-Ke Shih 			break;
1339e3ec7017SPing-Ke Shih 		}
1340e3ec7017SPing-Ke Shih 	}
1341e3ec7017SPing-Ke Shih }
1342e3ec7017SPing-Ke Shih 
1343e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev)
1344e3ec7017SPing-Ke Shih {
1345e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1346e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *bb_table = chip->bb_table;
1347e885871eSZong-Zhe Yang 	const struct rtw89_phy_table *bb_gain_table = chip->bb_gain_table;
1348e3ec7017SPing-Ke Shih 
1349e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL);
1350e3ec7017SPing-Ke Shih 	rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0);
1351e885871eSZong-Zhe Yang 	if (bb_gain_table)
1352e885871eSZong-Zhe Yang 		rtw89_phy_init_reg(rtwdev, bb_gain_table,
1353e885871eSZong-Zhe Yang 				   rtw89_phy_config_bb_gain, NULL);
1354e3ec7017SPing-Ke Shih 	rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0);
1355e3ec7017SPing-Ke Shih }
1356e3ec7017SPing-Ke Shih 
1357e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev)
1358e3ec7017SPing-Ke Shih {
1359e3ec7017SPing-Ke Shih 	rtw89_phy_write32(rtwdev, 0x8080, 0x4);
1360e3ec7017SPing-Ke Shih 	udelay(1);
1361e3ec7017SPing-Ke Shih 	return rtw89_phy_read32(rtwdev, 0x8080);
1362e3ec7017SPing-Ke Shih }
1363e3ec7017SPing-Ke Shih 
1364d9112042SChih-Kang Chang void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio)
1365e3ec7017SPing-Ke Shih {
13662a5f2b32SPing-Ke Shih 	void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg,
13672a5f2b32SPing-Ke Shih 		       enum rtw89_rf_path rf_path, void *data);
1368e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1369e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *rf_table;
1370e3ec7017SPing-Ke Shih 	struct rtw89_fw_h2c_rf_reg_info *rf_reg_info;
1371e3ec7017SPing-Ke Shih 	u8 path;
1372e3ec7017SPing-Ke Shih 
1373e3ec7017SPing-Ke Shih 	rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL);
1374e3ec7017SPing-Ke Shih 	if (!rf_reg_info)
1375e3ec7017SPing-Ke Shih 		return;
1376e3ec7017SPing-Ke Shih 
1377e3ec7017SPing-Ke Shih 	for (path = RF_PATH_A; path < chip->rf_path_num; path++) {
1378e3ec7017SPing-Ke Shih 		rf_table = chip->rf_table[path];
13792a5f2b32SPing-Ke Shih 		rf_reg_info->rf_path = rf_table->rf_path;
1380d9112042SChih-Kang Chang 		if (noio)
1381d9112042SChih-Kang Chang 			config = rtw89_phy_config_rf_reg_noio;
1382d9112042SChih-Kang Chang 		else
1383d9112042SChih-Kang Chang 			config = rf_table->config ? rf_table->config :
1384d9112042SChih-Kang Chang 				 rtw89_phy_config_rf_reg;
13852a5f2b32SPing-Ke Shih 		rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info);
1386e3ec7017SPing-Ke Shih 		if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info))
1387e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n",
13882a5f2b32SPing-Ke Shih 				   rf_reg_info->rf_path);
1389e3ec7017SPing-Ke Shih 	}
1390e3ec7017SPing-Ke Shih 	kfree(rf_reg_info);
1391e3ec7017SPing-Ke Shih }
1392e3ec7017SPing-Ke Shih 
1393e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev)
1394e3ec7017SPing-Ke Shih {
1395e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1396e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *nctl_table;
1397e3ec7017SPing-Ke Shih 	u32 val;
1398e3ec7017SPing-Ke Shih 	int ret;
1399e3ec7017SPing-Ke Shih 
1400e3ec7017SPing-Ke Shih 	/* IQK/DPK clock & reset */
1401d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3);
1402d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1);
1403d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000);
1404a24be8bbSPing-Ke Shih 	if (chip->chip_id != RTL8851B)
1405d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000);
1406d0c820ccSPing-Ke Shih 	if (chip->chip_id == RTL8852B)
1407d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2);
1408e3ec7017SPing-Ke Shih 
1409e3ec7017SPing-Ke Shih 	/* check 0x8080 */
1410d0c820ccSPing-Ke Shih 	rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8);
1411e3ec7017SPing-Ke Shih 
1412e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10,
1413e3ec7017SPing-Ke Shih 				1000, false, rtwdev);
1414e3ec7017SPing-Ke Shih 	if (ret)
1415e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to poll nctl block\n");
1416e3ec7017SPing-Ke Shih 
1417e3ec7017SPing-Ke Shih 	nctl_table = chip->nctl_table;
1418e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL);
1419a24be8bbSPing-Ke Shih 
1420a24be8bbSPing-Ke Shih 	if (chip->nctl_post_table)
1421a24be8bbSPing-Ke Shih 		rtw89_rfk_parser(rtwdev, chip->nctl_post_table);
1422e3ec7017SPing-Ke Shih }
1423e3ec7017SPing-Ke Shih 
1424e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr)
1425e3ec7017SPing-Ke Shih {
1426e3ec7017SPing-Ke Shih 	u32 phy_page = addr >> 8;
1427e3ec7017SPing-Ke Shih 	u32 ofst = 0;
1428e3ec7017SPing-Ke Shih 
1429e3ec7017SPing-Ke Shih 	switch (phy_page) {
1430e3ec7017SPing-Ke Shih 	case 0x6:
1431e3ec7017SPing-Ke Shih 	case 0x7:
1432e3ec7017SPing-Ke Shih 	case 0x8:
1433e3ec7017SPing-Ke Shih 	case 0x9:
1434e3ec7017SPing-Ke Shih 	case 0xa:
1435e3ec7017SPing-Ke Shih 	case 0xb:
1436e3ec7017SPing-Ke Shih 	case 0xc:
1437e3ec7017SPing-Ke Shih 	case 0xd:
1438e3ec7017SPing-Ke Shih 	case 0x19:
1439e3ec7017SPing-Ke Shih 	case 0x1a:
1440e3ec7017SPing-Ke Shih 	case 0x1b:
1441e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1442e3ec7017SPing-Ke Shih 		break;
1443e3ec7017SPing-Ke Shih 	default:
1444e3ec7017SPing-Ke Shih 		/* warning case */
1445e3ec7017SPing-Ke Shih 		ofst = 0;
1446e3ec7017SPing-Ke Shih 		break;
1447e3ec7017SPing-Ke Shih 	}
1448e3ec7017SPing-Ke Shih 
1449e3ec7017SPing-Ke Shih 	if (phy_page >= 0x40 && phy_page <= 0x4f)
1450e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1451e3ec7017SPing-Ke Shih 
1452e3ec7017SPing-Ke Shih 	return ofst;
1453e3ec7017SPing-Ke Shih }
1454e3ec7017SPing-Ke Shih 
1455e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1456e3ec7017SPing-Ke Shih 			   u32 data, enum rtw89_phy_idx phy_idx)
1457e3ec7017SPing-Ke Shih {
1458e3ec7017SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1459e3ec7017SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1460e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, addr, mask, data);
1461e3ec7017SPing-Ke Shih }
1462861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx);
1463e3ec7017SPing-Ke Shih 
14646b069898SPing-Ke Shih u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
14656b069898SPing-Ke Shih 			 enum rtw89_phy_idx phy_idx)
14666b069898SPing-Ke Shih {
14676b069898SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
14686b069898SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
14696b069898SPing-Ke Shih 	return rtw89_phy_read32_mask(rtwdev, addr, mask);
14706b069898SPing-Ke Shih }
14716b069898SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read32_idx);
14726b069898SPing-Ke Shih 
1473e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1474e3ec7017SPing-Ke Shih 			    u32 val)
1475e3ec7017SPing-Ke Shih {
1476e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0);
1477e3ec7017SPing-Ke Shih 
1478e3ec7017SPing-Ke Shih 	if (!rtwdev->dbcc_en)
1479e3ec7017SPing-Ke Shih 		return;
1480e3ec7017SPing-Ke Shih 
1481e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1);
1482e3ec7017SPing-Ke Shih }
1483e3ec7017SPing-Ke Shih 
1484e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev,
1485e3ec7017SPing-Ke Shih 			      const struct rtw89_phy_reg3_tbl *tbl)
1486e3ec7017SPing-Ke Shih {
1487e3ec7017SPing-Ke Shih 	const struct rtw89_reg3_def *reg3;
1488e3ec7017SPing-Ke Shih 	int i;
1489e3ec7017SPing-Ke Shih 
1490e3ec7017SPing-Ke Shih 	for (i = 0; i < tbl->size; i++) {
1491e3ec7017SPing-Ke Shih 		reg3 = &tbl->reg3[i];
1492e3ec7017SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data);
1493e3ec7017SPing-Ke Shih 	}
1494e3ec7017SPing-Ke Shih }
1495861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl);
1496e3ec7017SPing-Ke Shih 
14979b43bd1aSZong-Zhe Yang static const u8 rtw89_rs_idx_max[] = {
1498e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = RTW89_RATE_CCK_MAX,
1499e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = RTW89_RATE_OFDM_MAX,
1500e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = RTW89_RATE_MCS_MAX,
1501e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_MAX,
1502e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_MAX,
1503e3ec7017SPing-Ke Shih };
1504e3ec7017SPing-Ke Shih 
15059b43bd1aSZong-Zhe Yang static const u8 rtw89_rs_nss_max[] = {
1506e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = 1,
1507e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = 1,
1508e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = RTW89_NSS_MAX,
1509e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_MAX,
1510e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = 1,
1511e3ec7017SPing-Ke Shih };
1512e3ec7017SPing-Ke Shih 
1513e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = {
1514e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck),
1515e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm),
1516e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs),
1517e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm),
1518e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset),
1519e3ec7017SPing-Ke Shih };
1520e3ec7017SPing-Ke Shih 
1521e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs])
1522e3ec7017SPing-Ke Shih #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_max[rs] + (idx))
1523e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \
1524e3ec7017SPing-Ke Shih 	((nss) < rtw89_rs_nss_max[rs] && (idx) < rtw89_rs_idx_max[rs])
1525e3ec7017SPing-Ke Shih 
1526e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev,
1527e3ec7017SPing-Ke Shih 				 const struct rtw89_txpwr_table *tbl)
1528e3ec7017SPing-Ke Shih {
1529e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data;
1530e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size;
1531e3ec7017SPing-Ke Shih 	s8 *byr;
1532e3ec7017SPing-Ke Shih 	u32 data;
1533e3ec7017SPing-Ke Shih 	u8 i, idx;
1534e3ec7017SPing-Ke Shih 
1535e3ec7017SPing-Ke Shih 	for (; cfg < end; cfg++) {
1536e3ec7017SPing-Ke Shih 		byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]);
1537e3ec7017SPing-Ke Shih 		data = cfg->data;
1538e3ec7017SPing-Ke Shih 
1539e3ec7017SPing-Ke Shih 		for (i = 0; i < cfg->len; i++, data >>= 8) {
1540e3ec7017SPing-Ke Shih 			idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i));
1541e3ec7017SPing-Ke Shih 			byr[idx] = (s8)(data & 0xff);
1542e3ec7017SPing-Ke Shih 		}
1543e3ec7017SPing-Ke Shih 	}
1544e3ec7017SPing-Ke Shih }
1545861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate);
1546e3ec7017SPing-Ke Shih 
1547e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf)				\
1548e3ec7017SPing-Ke Shih ({									\
1549e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *__c = (rtwdev)->chip;		\
1550e3ec7017SPing-Ke Shih 	(txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac);	\
1551e3ec7017SPing-Ke Shih })
1552e3ec7017SPing-Ke Shih 
15539b43bd1aSZong-Zhe Yang static
155407ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band,
1555e3ec7017SPing-Ke Shih 			       const struct rtw89_rate_desc *rate_desc)
1556e3ec7017SPing-Ke Shih {
1557e3ec7017SPing-Ke Shih 	s8 *byr;
1558e3ec7017SPing-Ke Shih 	u8 idx;
1559e3ec7017SPing-Ke Shih 
1560e3ec7017SPing-Ke Shih 	if (rate_desc->rs == RTW89_RS_CCK)
1561e3ec7017SPing-Ke Shih 		band = RTW89_BAND_2G;
1562e3ec7017SPing-Ke Shih 
1563e3ec7017SPing-Ke Shih 	if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) {
1564e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1565e3ec7017SPing-Ke Shih 			    "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n",
1566e3ec7017SPing-Ke Shih 			    rate_desc->rs, rate_desc->nss, rate_desc->idx);
1567e3ec7017SPing-Ke Shih 
1568e3ec7017SPing-Ke Shih 		return 0;
1569e3ec7017SPing-Ke Shih 	}
1570e3ec7017SPing-Ke Shih 
1571e3ec7017SPing-Ke Shih 	byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]);
1572e3ec7017SPing-Ke Shih 	idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx);
1573e3ec7017SPing-Ke Shih 
1574e3ec7017SPing-Ke Shih 	return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]);
1575e3ec7017SPing-Ke Shih }
1576e3ec7017SPing-Ke Shih 
1577ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g)
1578e3ec7017SPing-Ke Shih {
1579ac74f016SZong-Zhe Yang 	switch (channel_6g) {
1580ac74f016SZong-Zhe Yang 	case 1 ... 29:
1581ac74f016SZong-Zhe Yang 		return (channel_6g - 1) / 2;
1582ac74f016SZong-Zhe Yang 	case 33 ... 61:
1583ac74f016SZong-Zhe Yang 		return (channel_6g - 3) / 2;
1584ac74f016SZong-Zhe Yang 	case 65 ... 93:
1585ac74f016SZong-Zhe Yang 		return (channel_6g - 5) / 2;
1586ac74f016SZong-Zhe Yang 	case 97 ... 125:
1587ac74f016SZong-Zhe Yang 		return (channel_6g - 7) / 2;
1588ac74f016SZong-Zhe Yang 	case 129 ... 157:
1589ac74f016SZong-Zhe Yang 		return (channel_6g - 9) / 2;
1590ac74f016SZong-Zhe Yang 	case 161 ... 189:
1591ac74f016SZong-Zhe Yang 		return (channel_6g - 11) / 2;
1592ac74f016SZong-Zhe Yang 	case 193 ... 221:
1593ac74f016SZong-Zhe Yang 		return (channel_6g - 13) / 2;
1594ac74f016SZong-Zhe Yang 	case 225 ... 253:
1595ac74f016SZong-Zhe Yang 		return (channel_6g - 15) / 2;
1596ac74f016SZong-Zhe Yang 	default:
1597ac74f016SZong-Zhe Yang 		rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g);
1598ac74f016SZong-Zhe Yang 		return 0;
1599ac74f016SZong-Zhe Yang 	}
1600ac74f016SZong-Zhe Yang }
1601ac74f016SZong-Zhe Yang 
1602ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel)
1603ac74f016SZong-Zhe Yang {
1604ac74f016SZong-Zhe Yang 	if (band == RTW89_BAND_6G)
1605ac74f016SZong-Zhe Yang 		return rtw89_channel_6g_to_idx(rtwdev, channel);
1606ac74f016SZong-Zhe Yang 
1607e3ec7017SPing-Ke Shih 	switch (channel) {
1608e3ec7017SPing-Ke Shih 	case 1 ... 14:
1609e3ec7017SPing-Ke Shih 		return channel - 1;
1610e3ec7017SPing-Ke Shih 	case 36 ... 64:
1611e3ec7017SPing-Ke Shih 		return (channel - 36) / 2;
1612e3ec7017SPing-Ke Shih 	case 100 ... 144:
1613e3ec7017SPing-Ke Shih 		return ((channel - 100) / 2) + 15;
1614e3ec7017SPing-Ke Shih 	case 149 ... 177:
1615e3ec7017SPing-Ke Shih 		return ((channel - 149) / 2) + 38;
1616e3ec7017SPing-Ke Shih 	default:
1617e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown channel: %d\n", channel);
1618e3ec7017SPing-Ke Shih 		return 0;
1619e3ec7017SPing-Ke Shih 	}
1620e3ec7017SPing-Ke Shih }
1621e3ec7017SPing-Ke Shih 
162207ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band,
1623e3ec7017SPing-Ke Shih 			      u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch)
1624e3ec7017SPing-Ke Shih {
16255395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
16265395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
16275395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
16285395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
1629*2a8ec45fSZong-Zhe Yang 	struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory;
1630ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1631e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1632*2a8ec45fSZong-Zhe Yang 	u8 reg6 = regulatory->reg_6ghz_power;
1633e3ec7017SPing-Ke Shih 	s8 lmt = 0, sar;
1634e3ec7017SPing-Ke Shih 
1635e3ec7017SPing-Ke Shih 	switch (band) {
1636e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
16375395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16385395482aSZong-Zhe Yang 		if (lmt)
16395395482aSZong-Zhe Yang 			break;
16405395482aSZong-Zhe Yang 
16415395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1642e3ec7017SPing-Ke Shih 		break;
1643e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
16445395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16455395482aSZong-Zhe Yang 		if (lmt)
16465395482aSZong-Zhe Yang 			break;
16475395482aSZong-Zhe Yang 
16485395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1649e3ec7017SPing-Ke Shih 		break;
1650ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
1651*2a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][regd][reg6][ch_idx];
16525395482aSZong-Zhe Yang 		if (lmt)
16535395482aSZong-Zhe Yang 			break;
16545395482aSZong-Zhe Yang 
1655*2a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][RTW89_WW]
1656*2a8ec45fSZong-Zhe Yang 				       [RTW89_REG_6GHZ_POWER_DFLT]
1657*2a8ec45fSZong-Zhe Yang 				       [ch_idx];
1658ac74f016SZong-Zhe Yang 		break;
1659e3ec7017SPing-Ke Shih 	default:
1660e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1661e3ec7017SPing-Ke Shih 		return 0;
1662e3ec7017SPing-Ke Shih 	}
1663e3ec7017SPing-Ke Shih 
1664e3ec7017SPing-Ke Shih 	lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt);
1665e3ec7017SPing-Ke Shih 	sar = rtw89_query_sar(rtwdev);
1666e3ec7017SPing-Ke Shih 
1667e3ec7017SPing-Ke Shih 	return min(lmt, sar);
1668e3ec7017SPing-Ke Shih }
1669861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit);
1670e3ec7017SPing-Ke Shih 
167107ef5f2fSZong-Zhe Yang #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch)		\
1672e3ec7017SPing-Ke Shih 	do {								\
1673e3ec7017SPing-Ke Shih 		u8 __i;							\
1674e3ec7017SPing-Ke Shih 		for (__i = 0; __i < RTW89_BF_NUM; __i++)		\
1675e3ec7017SPing-Ke Shih 			ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev,	\
167607ef5f2fSZong-Zhe Yang 							      band,	\
1677e3ec7017SPing-Ke Shih 							      bw, ntx,	\
1678e3ec7017SPing-Ke Shih 							      rs, __i,	\
1679e3ec7017SPing-Ke Shih 							      (ch));	\
1680e3ec7017SPing-Ke Shih 	} while (0)
1681e3ec7017SPing-Ke Shih 
1682e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev,
1683e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
168407ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch)
1685e3ec7017SPing-Ke Shih {
168607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1687e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
168807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1689e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
169007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1691e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_OFDM, ch);
169207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
169307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1694e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1695e3ec7017SPing-Ke Shih }
1696e3ec7017SPing-Ke Shih 
1697e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev,
1698e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
169907ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1700e3ec7017SPing-Ke Shih {
170107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1702e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch - 2);
170307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1704e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
170507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
170694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
170707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
170807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1709e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
171007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
171107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1712e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
171307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
171407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1715e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1716e3ec7017SPing-Ke Shih }
1717e3ec7017SPing-Ke Shih 
1718e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev,
1719e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
172007ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1721e3ec7017SPing-Ke Shih {
1722e3ec7017SPing-Ke Shih 	s8 val_0p5_n[RTW89_BF_NUM];
1723e3ec7017SPing-Ke Shih 	s8 val_0p5_p[RTW89_BF_NUM];
1724e3ec7017SPing-Ke Shih 	u8 i;
1725e3ec7017SPing-Ke Shih 
172607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
172794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
172807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
172907ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1730e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 6);
173107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
173207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1733e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
173407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
173507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1736e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
173707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
173807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1739e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 6);
174007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
174107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1742e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
174307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
174407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1745e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
174607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
174707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
1748e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1749e3ec7017SPing-Ke Shih 
175007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
1751e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
175207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
1753e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
1754e3ec7017SPing-Ke Shih 
1755e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_BF_NUM; i++)
1756e3ec7017SPing-Ke Shih 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1757e3ec7017SPing-Ke Shih }
1758e3ec7017SPing-Ke Shih 
175994b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev,
176094b70cafSZong-Zhe Yang 					    struct rtw89_txpwr_limit *lmt,
176107ef5f2fSZong-Zhe Yang 					    u8 band, u8 ntx, u8 ch, u8 pri_ch)
176294b70cafSZong-Zhe Yang {
176394b70cafSZong-Zhe Yang 	s8 val_0p5_n[RTW89_BF_NUM];
176494b70cafSZong-Zhe Yang 	s8 val_0p5_p[RTW89_BF_NUM];
176594b70cafSZong-Zhe Yang 	s8 val_2p5_n[RTW89_BF_NUM];
176694b70cafSZong-Zhe Yang 	s8 val_2p5_p[RTW89_BF_NUM];
176794b70cafSZong-Zhe Yang 	u8 i;
176894b70cafSZong-Zhe Yang 
176994b70cafSZong-Zhe Yang 	/* fill ofdm section */
177007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
177194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
177294b70cafSZong-Zhe Yang 
177394b70cafSZong-Zhe Yang 	/* fill mcs 20m section */
177407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
177507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
177694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 14);
177707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
177807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
177994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 10);
178007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
178107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
178294b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 6);
178307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
178407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
178594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 2);
178607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band,
178707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
178894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 2);
178907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band,
179007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
179194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 6);
179207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band,
179307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
179494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 10);
179507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band,
179607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
179794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 14);
179894b70cafSZong-Zhe Yang 
179994b70cafSZong-Zhe Yang 	/* fill mcs 40m section */
180007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
180107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
180294b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 12);
180307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
180407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
180594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
180607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band,
180707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
180894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
180907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band,
181007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
181194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 12);
181294b70cafSZong-Zhe Yang 
181394b70cafSZong-Zhe Yang 	/* fill mcs 80m section */
181407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
181507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
181694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
181707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band,
181807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
181994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
182094b70cafSZong-Zhe Yang 
182194b70cafSZong-Zhe Yang 	/* fill mcs 160m section */
182207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band,
182307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_160,
182494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch);
182594b70cafSZong-Zhe Yang 
182694b70cafSZong-Zhe Yang 	/* fill mcs 40m 0p5 section */
182707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
182894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
182907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
183094b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
183194b70cafSZong-Zhe Yang 
183294b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
183394b70cafSZong-Zhe Yang 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
183494b70cafSZong-Zhe Yang 
183594b70cafSZong-Zhe Yang 	/* fill mcs 40m 2p5 section */
183607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40,
183794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
183807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40,
183994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
184094b70cafSZong-Zhe Yang 
184194b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
184294b70cafSZong-Zhe Yang 		lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]);
184394b70cafSZong-Zhe Yang }
184494b70cafSZong-Zhe Yang 
18459b43bd1aSZong-Zhe Yang static
1846e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev,
184707ef5f2fSZong-Zhe Yang 				const struct rtw89_chan *chan,
1848e3ec7017SPing-Ke Shih 				struct rtw89_txpwr_limit *lmt,
1849e3ec7017SPing-Ke Shih 				u8 ntx)
1850e3ec7017SPing-Ke Shih {
185107ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
1852cbb145b9SZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
1853cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
1854cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
1855e3ec7017SPing-Ke Shih 
1856e3ec7017SPing-Ke Shih 	memset(lmt, 0, sizeof(*lmt));
1857e3ec7017SPing-Ke Shih 
1858e3ec7017SPing-Ke Shih 	switch (bw) {
1859e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
186007ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch);
1861e3ec7017SPing-Ke Shih 		break;
1862e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
186307ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch,
186407ef5f2fSZong-Zhe Yang 					       pri_ch);
1865e3ec7017SPing-Ke Shih 		break;
1866e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
186707ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch,
186807ef5f2fSZong-Zhe Yang 					       pri_ch);
186994b70cafSZong-Zhe Yang 		break;
187094b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
187107ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch,
187207ef5f2fSZong-Zhe Yang 						pri_ch);
1873e3ec7017SPing-Ke Shih 		break;
1874e3ec7017SPing-Ke Shih 	}
1875e3ec7017SPing-Ke Shih }
1876e3ec7017SPing-Ke Shih 
187707ef5f2fSZong-Zhe Yang static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band,
1878e3ec7017SPing-Ke Shih 					u8 ru, u8 ntx, u8 ch)
1879e3ec7017SPing-Ke Shih {
18805395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
18815395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
18825395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
18835395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
1884ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1885e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1886e3ec7017SPing-Ke Shih 	s8 lmt_ru = 0, sar;
1887e3ec7017SPing-Ke Shih 
1888e3ec7017SPing-Ke Shih 	switch (band) {
1889e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
18905395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][regd][ch_idx];
18915395482aSZong-Zhe Yang 		if (lmt_ru)
18925395482aSZong-Zhe Yang 			break;
18935395482aSZong-Zhe Yang 
18945395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1895e3ec7017SPing-Ke Shih 		break;
1896e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
18975395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][regd][ch_idx];
18985395482aSZong-Zhe Yang 		if (lmt_ru)
18995395482aSZong-Zhe Yang 			break;
19005395482aSZong-Zhe Yang 
19015395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1902e3ec7017SPing-Ke Shih 		break;
1903ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
19045395482aSZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][regd][ch_idx];
19055395482aSZong-Zhe Yang 		if (lmt_ru)
19065395482aSZong-Zhe Yang 			break;
19075395482aSZong-Zhe Yang 
19085395482aSZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1909ac74f016SZong-Zhe Yang 		break;
1910e3ec7017SPing-Ke Shih 	default:
1911e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1912e3ec7017SPing-Ke Shih 		return 0;
1913e3ec7017SPing-Ke Shih 	}
1914e3ec7017SPing-Ke Shih 
1915e3ec7017SPing-Ke Shih 	lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru);
1916e3ec7017SPing-Ke Shih 	sar = rtw89_query_sar(rtwdev);
1917e3ec7017SPing-Ke Shih 
1918e3ec7017SPing-Ke Shih 	return min(lmt_ru, sar);
1919e3ec7017SPing-Ke Shih }
1920e3ec7017SPing-Ke Shih 
1921e3ec7017SPing-Ke Shih static void
1922e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev,
1923e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
192407ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1925e3ec7017SPing-Ke Shih {
192607ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
192707ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1928e3ec7017SPing-Ke Shih 							ntx, ch);
192907ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
193007ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1931e3ec7017SPing-Ke Shih 							ntx, ch);
193207ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
193307ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1934e3ec7017SPing-Ke Shih 							 ntx, ch);
1935e3ec7017SPing-Ke Shih }
1936e3ec7017SPing-Ke Shih 
1937e3ec7017SPing-Ke Shih static void
1938e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev,
1939e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
194007ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1941e3ec7017SPing-Ke Shih {
194207ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
194307ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1944e3ec7017SPing-Ke Shih 							ntx, ch - 2);
194507ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
194607ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1947e3ec7017SPing-Ke Shih 							ntx, ch + 2);
194807ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
194907ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1950e3ec7017SPing-Ke Shih 							ntx, ch - 2);
195107ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
195207ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1953e3ec7017SPing-Ke Shih 							ntx, ch + 2);
195407ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
195507ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1956e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
195707ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
195807ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1959e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
1960e3ec7017SPing-Ke Shih }
1961e3ec7017SPing-Ke Shih 
1962e3ec7017SPing-Ke Shih static void
1963e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev,
1964e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
196507ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1966e3ec7017SPing-Ke Shih {
196707ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
196807ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1969e3ec7017SPing-Ke Shih 							ntx, ch - 6);
197007ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197107ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1972e3ec7017SPing-Ke Shih 							ntx, ch - 2);
197307ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197407ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1975e3ec7017SPing-Ke Shih 							ntx, ch + 2);
197607ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197707ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1978e3ec7017SPing-Ke Shih 							ntx, ch + 6);
197907ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198007ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1981e3ec7017SPing-Ke Shih 							ntx, ch - 6);
198207ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198307ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1984e3ec7017SPing-Ke Shih 							ntx, ch - 2);
198507ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198607ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1987e3ec7017SPing-Ke Shih 							ntx, ch + 2);
198807ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198907ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1990e3ec7017SPing-Ke Shih 							ntx, ch + 6);
199107ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199207ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1993e3ec7017SPing-Ke Shih 							 ntx, ch - 6);
199407ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199507ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1996e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
199707ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199807ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1999e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
200007ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200107ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2002e3ec7017SPing-Ke Shih 							 ntx, ch + 6);
2003e3ec7017SPing-Ke Shih }
2004e3ec7017SPing-Ke Shih 
200594b70cafSZong-Zhe Yang static void
200694b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev,
200794b70cafSZong-Zhe Yang 				   struct rtw89_txpwr_limit_ru *lmt_ru,
200807ef5f2fSZong-Zhe Yang 				   u8 band, u8 ntx, u8 ch)
200994b70cafSZong-Zhe Yang {
201094b70cafSZong-Zhe Yang 	static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 };
201194b70cafSZong-Zhe Yang 	int i;
201294b70cafSZong-Zhe Yang 
201394b70cafSZong-Zhe Yang 	static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM);
201494b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_RU_SEC_NUM; i++) {
201507ef5f2fSZong-Zhe Yang 		lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201694b70cafSZong-Zhe Yang 								RTW89_RU26,
201794b70cafSZong-Zhe Yang 								ntx,
201894b70cafSZong-Zhe Yang 								ch + ofst[i]);
201907ef5f2fSZong-Zhe Yang 		lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202094b70cafSZong-Zhe Yang 								RTW89_RU52,
202194b70cafSZong-Zhe Yang 								ntx,
202294b70cafSZong-Zhe Yang 								ch + ofst[i]);
202307ef5f2fSZong-Zhe Yang 		lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202494b70cafSZong-Zhe Yang 								 RTW89_RU106,
202594b70cafSZong-Zhe Yang 								 ntx,
202694b70cafSZong-Zhe Yang 								 ch + ofst[i]);
202794b70cafSZong-Zhe Yang 	}
202894b70cafSZong-Zhe Yang }
202994b70cafSZong-Zhe Yang 
20309b43bd1aSZong-Zhe Yang static
2031e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev,
203207ef5f2fSZong-Zhe Yang 				   const struct rtw89_chan *chan,
2033e3ec7017SPing-Ke Shih 				   struct rtw89_txpwr_limit_ru *lmt_ru,
2034e3ec7017SPing-Ke Shih 				   u8 ntx)
2035e3ec7017SPing-Ke Shih {
203607ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
2037cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
2038cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
2039e3ec7017SPing-Ke Shih 
2040e3ec7017SPing-Ke Shih 	memset(lmt_ru, 0, sizeof(*lmt_ru));
2041e3ec7017SPing-Ke Shih 
2042e3ec7017SPing-Ke Shih 	switch (bw) {
2043e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
204407ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx,
204507ef5f2fSZong-Zhe Yang 						  ch);
2046e3ec7017SPing-Ke Shih 		break;
2047e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
204807ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx,
204907ef5f2fSZong-Zhe Yang 						  ch);
2050e3ec7017SPing-Ke Shih 		break;
2051e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
205207ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx,
205307ef5f2fSZong-Zhe Yang 						  ch);
2054e3ec7017SPing-Ke Shih 		break;
205594b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
205607ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx,
205707ef5f2fSZong-Zhe Yang 						   ch);
205894b70cafSZong-Zhe Yang 		break;
2059e3ec7017SPing-Ke Shih 	}
2060e3ec7017SPing-Ke Shih }
20619b43bd1aSZong-Zhe Yang 
20629b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev,
20639b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
20649b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
20659b43bd1aSZong-Zhe Yang {
2066ddf9a2eaSZong-Zhe Yang 	u8 max_nss_num = rtwdev->chip->rf_path_num;
20679b43bd1aSZong-Zhe Yang 	static const u8 rs[] = {
20689b43bd1aSZong-Zhe Yang 		RTW89_RS_CCK,
20699b43bd1aSZong-Zhe Yang 		RTW89_RS_OFDM,
20709b43bd1aSZong-Zhe Yang 		RTW89_RS_MCS,
20719b43bd1aSZong-Zhe Yang 		RTW89_RS_HEDCM,
20729b43bd1aSZong-Zhe Yang 	};
20739b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc cur;
20749b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
20759b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
20769b43bd1aSZong-Zhe Yang 	u32 addr, val;
20779b43bd1aSZong-Zhe Yang 	s8 v[4] = {};
20789b43bd1aSZong-Zhe Yang 	u8 i;
20799b43bd1aSZong-Zhe Yang 
20809b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
20819b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr byrate with ch=%d\n", ch);
20829b43bd1aSZong-Zhe Yang 
20839b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_CCK] % 4);
20849b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_OFDM] % 4);
20859b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_MCS] % 4);
20869b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_HEDCM] % 4);
20879b43bd1aSZong-Zhe Yang 
20889b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_BY_RATE;
2089ddf9a2eaSZong-Zhe Yang 	for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) {
20909b43bd1aSZong-Zhe Yang 		for (i = 0; i < ARRAY_SIZE(rs); i++) {
20919b43bd1aSZong-Zhe Yang 			if (cur.nss >= rtw89_rs_nss_max[rs[i]])
20929b43bd1aSZong-Zhe Yang 				continue;
20939b43bd1aSZong-Zhe Yang 
20949b43bd1aSZong-Zhe Yang 			cur.rs = rs[i];
20959b43bd1aSZong-Zhe Yang 			for (cur.idx = 0; cur.idx < rtw89_rs_idx_max[rs[i]];
20969b43bd1aSZong-Zhe Yang 			     cur.idx++) {
20979b43bd1aSZong-Zhe Yang 				v[cur.idx % 4] =
20989b43bd1aSZong-Zhe Yang 					rtw89_phy_read_txpwr_byrate(rtwdev,
20999b43bd1aSZong-Zhe Yang 								    band,
21009b43bd1aSZong-Zhe Yang 								    &cur);
21019b43bd1aSZong-Zhe Yang 
21029b43bd1aSZong-Zhe Yang 				if ((cur.idx + 1) % 4)
21039b43bd1aSZong-Zhe Yang 					continue;
21049b43bd1aSZong-Zhe Yang 
21059b43bd1aSZong-Zhe Yang 				val = FIELD_PREP(GENMASK(7, 0), v[0]) |
21069b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(15, 8), v[1]) |
21079b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(23, 16), v[2]) |
21089b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(31, 24), v[3]);
21099b43bd1aSZong-Zhe Yang 
21109b43bd1aSZong-Zhe Yang 				rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr,
21119b43bd1aSZong-Zhe Yang 							val);
21129b43bd1aSZong-Zhe Yang 				addr += 4;
21139b43bd1aSZong-Zhe Yang 			}
21149b43bd1aSZong-Zhe Yang 		}
21159b43bd1aSZong-Zhe Yang 	}
21169b43bd1aSZong-Zhe Yang }
21179b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate);
21189b43bd1aSZong-Zhe Yang 
21199b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev,
21209b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
21219b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
21229b43bd1aSZong-Zhe Yang {
21239b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc desc = {
21249b43bd1aSZong-Zhe Yang 		.nss = RTW89_NSS_1,
21259b43bd1aSZong-Zhe Yang 		.rs = RTW89_RS_OFFSET,
21269b43bd1aSZong-Zhe Yang 	};
21279b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
21289b43bd1aSZong-Zhe Yang 	s8 v[RTW89_RATE_OFFSET_MAX] = {};
21299b43bd1aSZong-Zhe Yang 	u32 val;
21309b43bd1aSZong-Zhe Yang 
21319b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n");
21329b43bd1aSZong-Zhe Yang 
21339b43bd1aSZong-Zhe Yang 	for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_MAX; desc.idx++)
21349b43bd1aSZong-Zhe Yang 		v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc);
21359b43bd1aSZong-Zhe Yang 
21369b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(RTW89_RATE_OFFSET_MAX != 5);
21379b43bd1aSZong-Zhe Yang 	val = FIELD_PREP(GENMASK(3, 0), v[0]) |
21389b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(7, 4), v[1]) |
21399b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(11, 8), v[2]) |
21409b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(15, 12), v[3]) |
21419b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(19, 16), v[4]);
21429b43bd1aSZong-Zhe Yang 
21439b43bd1aSZong-Zhe Yang 	rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL,
21449b43bd1aSZong-Zhe Yang 				     GENMASK(19, 0), val);
21459b43bd1aSZong-Zhe Yang }
21469b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset);
21479b43bd1aSZong-Zhe Yang 
21489b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev,
21499b43bd1aSZong-Zhe Yang 			       const struct rtw89_chan *chan,
21509b43bd1aSZong-Zhe Yang 			       enum rtw89_phy_idx phy_idx)
21519b43bd1aSZong-Zhe Yang {
2152ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
21539b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit lmt;
21549b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21559b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
21569b43bd1aSZong-Zhe Yang 	const s8 *ptr;
21579b43bd1aSZong-Zhe Yang 	u32 addr, val;
21589b43bd1aSZong-Zhe Yang 	u8 i, j;
21599b43bd1aSZong-Zhe Yang 
21609b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21619b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw);
21629b43bd1aSZong-Zhe Yang 
21639b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) !=
21649b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_PAGE_SIZE);
21659b43bd1aSZong-Zhe Yang 
21669b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_LMT;
2167ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
21689b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i);
21699b43bd1aSZong-Zhe Yang 
21709b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt;
21719b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE;
21729b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
21739b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
21749b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
21759b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
21769b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
21779b43bd1aSZong-Zhe Yang 
21789b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
21799b43bd1aSZong-Zhe Yang 		}
21809b43bd1aSZong-Zhe Yang 	}
21819b43bd1aSZong-Zhe Yang }
21829b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit);
21839b43bd1aSZong-Zhe Yang 
21849b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev,
21859b43bd1aSZong-Zhe Yang 				  const struct rtw89_chan *chan,
21869b43bd1aSZong-Zhe Yang 				  enum rtw89_phy_idx phy_idx)
21879b43bd1aSZong-Zhe Yang {
2188ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
21899b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit_ru lmt_ru;
21909b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21919b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
21929b43bd1aSZong-Zhe Yang 	const s8 *ptr;
21939b43bd1aSZong-Zhe Yang 	u32 addr, val;
21949b43bd1aSZong-Zhe Yang 	u8 i, j;
21959b43bd1aSZong-Zhe Yang 
21969b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21979b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw);
21989b43bd1aSZong-Zhe Yang 
21999b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) !=
22009b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_RU_PAGE_SIZE);
22019b43bd1aSZong-Zhe Yang 
22029b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_RU_LMT;
2203ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
22049b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i);
22059b43bd1aSZong-Zhe Yang 
22069b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt_ru;
22079b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE;
22089b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
22099b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
22109b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
22119b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
22129b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
22139b43bd1aSZong-Zhe Yang 
22149b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
22159b43bd1aSZong-Zhe Yang 		}
22169b43bd1aSZong-Zhe Yang 	}
22179b43bd1aSZong-Zhe Yang }
22189b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru);
2219e3ec7017SPing-Ke Shih 
2220e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data {
2221e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
2222e3ec7017SPing-Ke Shih 	struct sk_buff *c2h;
2223e3ec7017SPing-Ke Shih };
2224e3ec7017SPing-Ke Shih 
2225e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta)
2226e3ec7017SPing-Ke Shih {
2227e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data;
2228e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = ra_data->rtwdev;
2229e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2230e3ec7017SPing-Ke Shih 	struct rtw89_ra_report *ra_report = &rtwsta->ra_report;
2231e3ec7017SPing-Ke Shih 	struct sk_buff *c2h = ra_data->c2h;
2232e3ec7017SPing-Ke Shih 	u8 mode, rate, bw, giltf, mac_id;
22339a3a593cSPing-Ke Shih 	u16 legacy_bitrate;
22349a3a593cSPing-Ke Shih 	bool valid;
22350d466f05SPing-Ke Shih 	u8 mcs = 0;
2236e3ec7017SPing-Ke Shih 
2237e3ec7017SPing-Ke Shih 	mac_id = RTW89_GET_PHY_C2H_RA_RPT_MACID(c2h->data);
2238e3ec7017SPing-Ke Shih 	if (mac_id != rtwsta->mac_id)
2239e3ec7017SPing-Ke Shih 		return;
2240e3ec7017SPing-Ke Shih 
2241e3ec7017SPing-Ke Shih 	rate = RTW89_GET_PHY_C2H_RA_RPT_MCSNSS(c2h->data);
2242e3ec7017SPing-Ke Shih 	bw = RTW89_GET_PHY_C2H_RA_RPT_BW(c2h->data);
2243e3ec7017SPing-Ke Shih 	giltf = RTW89_GET_PHY_C2H_RA_RPT_GILTF(c2h->data);
2244e3ec7017SPing-Ke Shih 	mode = RTW89_GET_PHY_C2H_RA_RPT_MD_SEL(c2h->data);
2245e3ec7017SPing-Ke Shih 
22469a3a593cSPing-Ke Shih 	if (mode == RTW89_RA_RPT_MODE_LEGACY) {
22479a3a593cSPing-Ke Shih 		valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate);
22489a3a593cSPing-Ke Shih 		if (!valid)
22499a3a593cSPing-Ke Shih 			return;
22509a3a593cSPing-Ke Shih 	}
22519a3a593cSPing-Ke Shih 
22520d466f05SPing-Ke Shih 	memset(&ra_report->txrate, 0, sizeof(ra_report->txrate));
22539a3a593cSPing-Ke Shih 
2254e3ec7017SPing-Ke Shih 	switch (mode) {
2255e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_LEGACY:
22569a3a593cSPing-Ke Shih 		ra_report->txrate.legacy = legacy_bitrate;
2257e3ec7017SPing-Ke Shih 		break;
2258e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HT:
2259e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS;
226011fe4ccdSZong-Zhe Yang 		if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw))
2261e3ec7017SPing-Ke Shih 			rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate),
2262e3ec7017SPing-Ke Shih 						FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate));
2263e3ec7017SPing-Ke Shih 		else
2264e3ec7017SPing-Ke Shih 			rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate);
2265e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = rate;
2266e3ec7017SPing-Ke Shih 		if (giltf)
2267e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
22680d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs & 0x07;
2269e3ec7017SPing-Ke Shih 		break;
2270e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_VHT:
2271e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
2272e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
2273e3ec7017SPing-Ke Shih 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
2274e3ec7017SPing-Ke Shih 		if (giltf)
2275e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
22760d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2277e3ec7017SPing-Ke Shih 		break;
2278e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HE:
2279e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS;
2280e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
2281e3ec7017SPing-Ke Shih 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
2282e3ec7017SPing-Ke Shih 		if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08)
2283e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2284e3ec7017SPing-Ke Shih 		else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16)
2285e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2286e3ec7017SPing-Ke Shih 		else
2287e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2;
22880d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2289e3ec7017SPing-Ke Shih 		break;
2290e3ec7017SPing-Ke Shih 	}
2291e3ec7017SPing-Ke Shih 
2292167044afSPing-Ke Shih 	ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw);
2293e3ec7017SPing-Ke Shih 	ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate);
2294e3ec7017SPing-Ke Shih 	ra_report->hw_rate = FIELD_PREP(RTW89_HW_RATE_MASK_MOD, mode) |
2295e3ec7017SPing-Ke Shih 			     FIELD_PREP(RTW89_HW_RATE_MASK_VAL, rate);
22960d466f05SPing-Ke Shih 	ra_report->might_fallback_legacy = mcs <= 2;
22974c51541dSBenjamin Berg 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report);
22984c51541dSBenjamin Berg 	rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1;
2299e3ec7017SPing-Ke Shih }
2300e3ec7017SPing-Ke Shih 
2301e3ec7017SPing-Ke Shih static void
2302e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
2303e3ec7017SPing-Ke Shih {
2304e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data ra_data;
2305e3ec7017SPing-Ke Shih 
2306e3ec7017SPing-Ke Shih 	ra_data.rtwdev = rtwdev;
2307e3ec7017SPing-Ke Shih 	ra_data.c2h = c2h;
2308e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2309e3ec7017SPing-Ke Shih 					  rtw89_phy_c2h_ra_rpt_iter,
2310e3ec7017SPing-Ke Shih 					  &ra_data);
2311e3ec7017SPing-Ke Shih }
2312e3ec7017SPing-Ke Shih 
2313e3ec7017SPing-Ke Shih static
2314e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev,
2315e3ec7017SPing-Ke Shih 					  struct sk_buff *c2h, u32 len) = {
2316e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt,
2317e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL,
2318e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_TXSTS] = NULL,
2319e3ec7017SPing-Ke Shih };
2320e3ec7017SPing-Ke Shih 
2321e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
2322e3ec7017SPing-Ke Shih 			  u32 len, u8 class, u8 func)
2323e3ec7017SPing-Ke Shih {
2324e3ec7017SPing-Ke Shih 	void (*handler)(struct rtw89_dev *rtwdev,
2325e3ec7017SPing-Ke Shih 			struct sk_buff *c2h, u32 len) = NULL;
2326e3ec7017SPing-Ke Shih 
2327e3ec7017SPing-Ke Shih 	switch (class) {
2328e3ec7017SPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_RA:
2329e3ec7017SPing-Ke Shih 		if (func < RTW89_PHY_C2H_FUNC_RA_MAX)
2330e3ec7017SPing-Ke Shih 			handler = rtw89_phy_c2h_ra_handler[func];
2331e3ec7017SPing-Ke Shih 		break;
23323b66519bSPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_DM:
23333b66519bSPing-Ke Shih 		if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY)
23343b66519bSPing-Ke Shih 			return;
23353b66519bSPing-Ke Shih 		fallthrough;
2336e3ec7017SPing-Ke Shih 	default:
2337e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
2338e3ec7017SPing-Ke Shih 		return;
2339e3ec7017SPing-Ke Shih 	}
2340e3ec7017SPing-Ke Shih 	if (!handler) {
2341e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
2342e3ec7017SPing-Ke Shih 			   func);
2343e3ec7017SPing-Ke Shih 		return;
2344e3ec7017SPing-Ke Shih 	}
2345e3ec7017SPing-Ke Shih 	handler(rtwdev, skb, len);
2346e3ec7017SPing-Ke Shih }
2347e3ec7017SPing-Ke Shih 
2348e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo)
2349e3ec7017SPing-Ke Shih {
23500789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2351e3ec7017SPing-Ke Shih 	u32 reg_mask;
2352e3ec7017SPing-Ke Shih 
2353e3ec7017SPing-Ke Shih 	if (sc_xo)
23540789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2355e3ec7017SPing-Ke Shih 	else
23560789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2357e3ec7017SPing-Ke Shih 
23580789881aSChia-Yuan Li 	return (u8)rtw89_read32_mask(rtwdev, xtal->xcap_reg, reg_mask);
2359e3ec7017SPing-Ke Shih }
2360e3ec7017SPing-Ke Shih 
2361e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo,
2362e3ec7017SPing-Ke Shih 				       u8 val)
2363e3ec7017SPing-Ke Shih {
23640789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2365e3ec7017SPing-Ke Shih 	u32 reg_mask;
2366e3ec7017SPing-Ke Shih 
2367e3ec7017SPing-Ke Shih 	if (sc_xo)
23680789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2369e3ec7017SPing-Ke Shih 	else
23700789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2371e3ec7017SPing-Ke Shih 
23720789881aSChia-Yuan Li 	rtw89_write32_mask(rtwdev, xtal->xcap_reg, reg_mask, val);
2373e3ec7017SPing-Ke Shih }
2374e3ec7017SPing-Ke Shih 
2375e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev,
2376e3ec7017SPing-Ke Shih 					  u8 crystal_cap, bool force)
2377e3ec7017SPing-Ke Shih {
2378e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
23798379fa61SYuan-Han Zhang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2380e3ec7017SPing-Ke Shih 	u8 sc_xi_val, sc_xo_val;
2381e3ec7017SPing-Ke Shih 
2382e3ec7017SPing-Ke Shih 	if (!force && cfo->crystal_cap == crystal_cap)
2383e3ec7017SPing-Ke Shih 		return;
2384e3ec7017SPing-Ke Shih 	crystal_cap = clamp_t(u8, crystal_cap, 0, 127);
23850789881aSChia-Yuan Li 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8851B) {
2386e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap);
2387e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap);
2388e3ec7017SPing-Ke Shih 		sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true);
2389e3ec7017SPing-Ke Shih 		sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false);
23908379fa61SYuan-Han Zhang 	} else {
23918379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO,
23928379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XO_MASK);
23938379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI,
23948379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XI_MASK);
23958379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val);
23968379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val);
23978379fa61SYuan-Han Zhang 	}
2398e3ec7017SPing-Ke Shih 	cfo->crystal_cap = sc_xi_val;
2399e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap);
2400e3ec7017SPing-Ke Shih 
2401e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val);
2402e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val);
2403e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n",
2404e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2405e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n");
2406e3ec7017SPing-Ke Shih }
2407e3ec7017SPing-Ke Shih 
2408e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev)
2409e3ec7017SPing-Ke Shih {
2410e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2411e3ec7017SPing-Ke Shih 	u8 cap;
2412e3ec7017SPing-Ke Shih 
2413e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK;
2414e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2415e3ec7017SPing-Ke Shih 	if (cfo->crystal_cap == cfo->def_x_cap)
2416e3ec7017SPing-Ke Shih 		return;
2417e3ec7017SPing-Ke Shih 	cap = cfo->crystal_cap;
2418e3ec7017SPing-Ke Shih 	cap += (cap > cfo->def_x_cap ? -1 : 1);
2419e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false);
2420e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2421e3ec7017SPing-Ke Shih 		    "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap,
2422e3ec7017SPing-Ke Shih 		    cfo->def_x_cap);
2423e3ec7017SPing-Ke Shih }
2424e3ec7017SPing-Ke Shih 
2425e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo)
2426e3ec7017SPing-Ke Shih {
2427b7379148SYuan-Han Zhang 	const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp;
2428e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
2429e3ec7017SPing-Ke Shih 	s32 cfo_avg_312;
2430b7379148SYuan-Han Zhang 	s32 dcfo_comp_val;
2431e3ec7017SPing-Ke Shih 	int sign;
2432e3ec7017SPing-Ke Shih 
2433e3ec7017SPing-Ke Shih 	if (!is_linked) {
2434e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n",
2435e3ec7017SPing-Ke Shih 			    is_linked);
2436e3ec7017SPing-Ke Shih 		return;
2437e3ec7017SPing-Ke Shih 	}
2438e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo);
2439e3ec7017SPing-Ke Shih 	if (curr_cfo == 0)
2440e3ec7017SPing-Ke Shih 		return;
2441b7379148SYuan-Han Zhang 	dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO);
2442e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
24439f9882dbSEric Huang 	cfo_avg_312 = curr_cfo / 625 + sign * dcfo_comp_val;
24449f9882dbSEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "avg_cfo_312=%d step\n", cfo_avg_312);
2445e3ec7017SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV)
2446e3ec7017SPing-Ke Shih 		cfo_avg_312 = -cfo_avg_312;
2447b7379148SYuan-Han Zhang 	rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask,
2448e3ec7017SPing-Ke Shih 			       cfo_avg_312);
2449e3ec7017SPing-Ke Shih }
2450e3ec7017SPing-Ke Shih 
2451e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev)
2452e3ec7017SPing-Ke Shih {
24539f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
24549f9882dbSEric Huang 
2455e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1);
2456e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8);
24579f9882dbSEric Huang 
24589f9882dbSEric Huang 	if (chip->cfo_hw_comp)
24599f9882dbSEric Huang 		rtw89_write32_mask(rtwdev, R_AX_PWR_UL_CTRL2,
24609f9882dbSEric Huang 				   B_AX_PWR_UL_CFO_MASK, 0x6);
24619f9882dbSEric Huang 	else
2462e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK);
2463e3ec7017SPing-Ke Shih }
2464e3ec7017SPing-Ke Shih 
2465e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev)
2466e3ec7017SPing-Ke Shih {
2467e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2468e3ec7017SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
2469e3ec7017SPing-Ke Shih 
2470e3ec7017SPing-Ke Shih 	cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK;
2471e3ec7017SPing-Ke Shih 	cfo->crystal_cap = cfo->crystal_cap_default;
2472e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap;
2473a9e06f2eSYi-Tang Chiu 	cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f);
2474a9e06f2eSYi-Tang Chiu 	cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1);
2475e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2476a9e06f2eSYi-Tang Chiu 	cfo->divergence_lock_en = false;
2477e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = 0;
2478a9e06f2eSYi-Tang Chiu 	cfo->lock_cnt = 0;
2479e3ec7017SPing-Ke Shih 	cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE;
2480e3ec7017SPing-Ke Shih 	cfo->apply_compensation = false;
2481e3ec7017SPing-Ke Shih 	cfo->residual_cfo_acc = 0;
2482e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n",
2483e3ec7017SPing-Ke Shih 		    cfo->crystal_cap_default);
2484e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true);
2485e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1);
2486e3ec7017SPing-Ke Shih 	rtw89_dcfo_comp_init(rtwdev);
2487e3ec7017SPing-Ke Shih 	cfo->cfo_timer_ms = 2000;
2488e3ec7017SPing-Ke Shih 	cfo->cfo_trig_by_timer_en = false;
2489e3ec7017SPing-Ke Shih 	cfo->phy_cfo_trk_cnt = 0;
2490e3ec7017SPing-Ke Shih 	cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2491bc013052SEric Huang 	cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE;
2492e3ec7017SPing-Ke Shih }
2493e3ec7017SPing-Ke Shih 
2494e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev,
2495e3ec7017SPing-Ke Shih 					     s32 curr_cfo)
2496e3ec7017SPing-Ke Shih {
2497e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2498e3ec7017SPing-Ke Shih 	s8 crystal_cap = cfo->crystal_cap;
2499e3ec7017SPing-Ke Shih 	s32 cfo_abs = abs(curr_cfo);
2500e3ec7017SPing-Ke Shih 	int sign;
2501e3ec7017SPing-Ke Shih 
2502e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2503e3ec7017SPing-Ke Shih 		if (cfo_abs > CFO_TRK_ENABLE_TH)
2504e3ec7017SPing-Ke Shih 			cfo->is_adjust = true;
2505e3ec7017SPing-Ke Shih 	} else {
2506e3ec7017SPing-Ke Shih 		if (cfo_abs < CFO_TRK_STOP_TH)
2507e3ec7017SPing-Ke Shih 			cfo->is_adjust = false;
2508e3ec7017SPing-Ke Shih 	}
2509e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2510e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n");
2511e3ec7017SPing-Ke Shih 		return;
2512e3ec7017SPing-Ke Shih 	}
2513e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
2514e3ec7017SPing-Ke Shih 	if (cfo_abs > CFO_TRK_STOP_TH_4)
2515e3ec7017SPing-Ke Shih 		crystal_cap += 7 * sign;
2516e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_3)
2517e3ec7017SPing-Ke Shih 		crystal_cap += 5 * sign;
2518e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_2)
2519e3ec7017SPing-Ke Shih 		crystal_cap += 3 * sign;
2520e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_1)
2521e3ec7017SPing-Ke Shih 		crystal_cap += 1 * sign;
2522e3ec7017SPing-Ke Shih 	else
2523e3ec7017SPing-Ke Shih 		return;
2524e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false);
2525e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2526e3ec7017SPing-Ke Shih 		    "X_cap{Curr,Default}={0x%x,0x%x}\n",
2527e3ec7017SPing-Ke Shih 		    cfo->crystal_cap, cfo->def_x_cap);
2528e3ec7017SPing-Ke Shih }
2529e3ec7017SPing-Ke Shih 
2530e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev)
2531e3ec7017SPing-Ke Shih {
25329f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2533e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2534e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2535e3ec7017SPing-Ke Shih 	s32 cfo_cnt_all = 0;
2536e3ec7017SPing-Ke Shih 	s32 cfo_all_avg = 0;
2537e3ec7017SPing-Ke Shih 	u8 i;
2538e3ec7017SPing-Ke Shih 
2539e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc != 1)
2540e3ec7017SPing-Ke Shih 		return 0;
2541e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n");
2542e3ec7017SPing-Ke Shih 	for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2543e3ec7017SPing-Ke Shih 		if (cfo->cfo_cnt[i] == 0)
2544e3ec7017SPing-Ke Shih 			continue;
2545e3ec7017SPing-Ke Shih 		cfo_khz_all += cfo->cfo_tail[i];
2546e3ec7017SPing-Ke Shih 		cfo_cnt_all += cfo->cfo_cnt[i];
2547e3ec7017SPing-Ke Shih 		cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all);
2548e3ec7017SPing-Ke Shih 		cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
25499f9882dbSEric Huang 		cfo->dcfo_avg = phy_div(cfo_khz_all << chip->dcfo_comp_sft,
25509f9882dbSEric Huang 					cfo_cnt_all);
2551e3ec7017SPing-Ke Shih 	}
2552e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2553e3ec7017SPing-Ke Shih 		    "CFO track for macid = %d\n", i);
2554e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2555e3ec7017SPing-Ke Shih 		    "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n",
2556e3ec7017SPing-Ke Shih 		    cfo_khz_all, cfo_cnt_all, cfo_all_avg);
2557e3ec7017SPing-Ke Shih 	return cfo_all_avg;
2558e3ec7017SPing-Ke Shih }
2559e3ec7017SPing-Ke Shih 
2560e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev)
2561e3ec7017SPing-Ke Shih {
2562e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2563e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2564e3ec7017SPing-Ke Shih 	s32 target_cfo = 0;
2565e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2566e3ec7017SPing-Ke Shih 	s32 cfo_khz_all_tp_wgt = 0;
2567e3ec7017SPing-Ke Shih 	s32 cfo_avg = 0;
2568e3ec7017SPing-Ke Shih 	s32 max_cfo_lb = BIT(31);
2569e3ec7017SPing-Ke Shih 	s32 min_cfo_ub = GENMASK(30, 0);
2570e3ec7017SPing-Ke Shih 	u16 cfo_cnt_all = 0;
2571e3ec7017SPing-Ke Shih 	u8 active_entry_cnt = 0;
2572e3ec7017SPing-Ke Shih 	u8 sta_cnt = 0;
2573e3ec7017SPing-Ke Shih 	u32 tp_all = 0;
2574e3ec7017SPing-Ke Shih 	u8 i;
2575e3ec7017SPing-Ke Shih 	u8 cfo_tol = 0;
2576e3ec7017SPing-Ke Shih 
2577e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n");
2578e3ec7017SPing-Ke Shih 	if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) {
2579e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n");
2580e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2581e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2582e3ec7017SPing-Ke Shih 				continue;
2583e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_tail[i];
2584e3ec7017SPing-Ke Shih 			cfo_cnt_all += cfo->cfo_cnt[i];
2585e3ec7017SPing-Ke Shih 			cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all);
2586e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2587e3ec7017SPing-Ke Shih 				    "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n",
2588e3ec7017SPing-Ke Shih 				    cfo_khz_all, cfo_cnt_all, cfo_avg);
2589e3ec7017SPing-Ke Shih 			target_cfo = cfo_avg;
2590e3ec7017SPing-Ke Shih 		}
2591e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) {
2592e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n");
2593e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2594e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2595e3ec7017SPing-Ke Shih 				continue;
2596e3ec7017SPing-Ke Shih 			cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2597e3ec7017SPing-Ke Shih 						  (s32)cfo->cfo_cnt[i]);
2598e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2599e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2600e3ec7017SPing-Ke Shih 				    "Macid=%d, cfo_avg=%d\n", i,
2601e3ec7017SPing-Ke Shih 				    cfo->cfo_avg[i]);
2602e3ec7017SPing-Ke Shih 		}
2603e3ec7017SPing-Ke Shih 		sta_cnt = rtwdev->total_sta_assoc;
2604e3ec7017SPing-Ke Shih 		cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt);
2605e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2606e3ec7017SPing-Ke Shih 			    "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n",
2607e3ec7017SPing-Ke Shih 			    cfo_khz_all, sta_cnt, cfo_avg);
2608e3ec7017SPing-Ke Shih 		target_cfo = cfo_avg;
2609e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) {
2610e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n");
2611e3ec7017SPing-Ke Shih 		cfo_tol = cfo->sta_cfo_tolerance;
2612e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2613e3ec7017SPing-Ke Shih 			sta_cnt++;
2614e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] != 0) {
2615e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2616e3ec7017SPing-Ke Shih 							  (s32)cfo->cfo_cnt[i]);
2617e3ec7017SPing-Ke Shih 				active_entry_cnt++;
2618e3ec7017SPing-Ke Shih 			} else {
2619e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = cfo->pre_cfo_avg[i];
2620e3ec7017SPing-Ke Shih 			}
2621e3ec7017SPing-Ke Shih 			max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb);
2622e3ec7017SPing-Ke Shih 			min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub);
2623e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2624e3ec7017SPing-Ke Shih 			/* need tp for each entry */
2625e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2626e3ec7017SPing-Ke Shih 				    "[%d] cfo_avg=%d, tp=tbd\n",
2627e3ec7017SPing-Ke Shih 				    i, cfo->cfo_avg[i]);
2628e3ec7017SPing-Ke Shih 			if (sta_cnt >= rtwdev->total_sta_assoc)
2629e3ec7017SPing-Ke Shih 				break;
2630e3ec7017SPing-Ke Shih 		}
2631e3ec7017SPing-Ke Shih 		tp_all = stats->rx_throughput; /* need tp for each entry */
2632e3ec7017SPing-Ke Shih 		cfo_avg =  phy_div(cfo_khz_all_tp_wgt, (s32)tp_all);
2633e3ec7017SPing-Ke Shih 
2634e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n",
2635e3ec7017SPing-Ke Shih 			    sta_cnt);
2636e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n",
2637e3ec7017SPing-Ke Shih 			    active_entry_cnt);
2638e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2639e3ec7017SPing-Ke Shih 			    "Msta cfo with tp_wgt=%d, avg_cfo=%d\n",
2640e3ec7017SPing-Ke Shih 			    cfo_khz_all_tp_wgt, cfo_avg);
2641e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n",
2642e3ec7017SPing-Ke Shih 			    max_cfo_lb, min_cfo_ub);
2643e3ec7017SPing-Ke Shih 		if (max_cfo_lb <= min_cfo_ub) {
2644e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2645e3ec7017SPing-Ke Shih 				    "cfo win_size=%d\n",
2646e3ec7017SPing-Ke Shih 				    min_cfo_ub - max_cfo_lb);
2647e3ec7017SPing-Ke Shih 			target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub);
2648e3ec7017SPing-Ke Shih 		} else {
2649e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2650c51ed740SColin Ian King 				    "No intersection of cfo tolerance windows\n");
2651e3ec7017SPing-Ke Shih 			target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt);
2652e3ec7017SPing-Ke Shih 		}
2653e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++)
2654e3ec7017SPing-Ke Shih 			cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2655e3ec7017SPing-Ke Shih 	}
2656e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo);
2657e3ec7017SPing-Ke Shih 	return target_cfo;
2658e3ec7017SPing-Ke Shih }
2659e3ec7017SPing-Ke Shih 
2660e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev)
2661e3ec7017SPing-Ke Shih {
2662e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2663e3ec7017SPing-Ke Shih 
2664e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail));
2665e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt));
2666e3ec7017SPing-Ke Shih 	cfo->packet_count = 0;
2667e3ec7017SPing-Ke Shih 	cfo->packet_count_pre = 0;
2668e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = 0;
2669e3ec7017SPing-Ke Shih }
2670e3ec7017SPing-Ke Shih 
2671e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev)
2672e3ec7017SPing-Ke Shih {
2673e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2674e3ec7017SPing-Ke Shih 	s32 new_cfo = 0;
2675e3ec7017SPing-Ke Shih 	bool x_cap_update = false;
2676e3ec7017SPing-Ke Shih 	u8 pre_x_cap = cfo->crystal_cap;
26779f9882dbSEric Huang 	u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft;
2678e3ec7017SPing-Ke Shih 
26799f9882dbSEric Huang 	cfo->dcfo_avg = 0;
2680e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n",
2681e3ec7017SPing-Ke Shih 		    rtwdev->total_sta_assoc);
2682e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 0) {
2683e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_reset(rtwdev);
2684e3ec7017SPing-Ke Shih 		return;
2685e3ec7017SPing-Ke Shih 	}
2686e3ec7017SPing-Ke Shih 	if (cfo->packet_count == 0) {
2687e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n");
2688e3ec7017SPing-Ke Shih 		return;
2689e3ec7017SPing-Ke Shih 	}
2690e3ec7017SPing-Ke Shih 	if (cfo->packet_count == cfo->packet_count_pre) {
2691e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n");
2692e3ec7017SPing-Ke Shih 		return;
2693e3ec7017SPing-Ke Shih 	}
2694e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 1)
2695e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_average_cfo_calc(rtwdev);
2696e3ec7017SPing-Ke Shih 	else
2697e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev);
2698e3ec7017SPing-Ke Shih 	if (new_cfo == 0) {
2699e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n");
2700e3ec7017SPing-Ke Shih 		return;
2701e3ec7017SPing-Ke Shih 	}
2702a9e06f2eSYi-Tang Chiu 	if (cfo->divergence_lock_en) {
2703a9e06f2eSYi-Tang Chiu 		cfo->lock_cnt++;
2704a9e06f2eSYi-Tang Chiu 		if (cfo->lock_cnt > CFO_PERIOD_CNT) {
2705a9e06f2eSYi-Tang Chiu 			cfo->divergence_lock_en = false;
2706a9e06f2eSYi-Tang Chiu 			cfo->lock_cnt = 0;
2707a9e06f2eSYi-Tang Chiu 		} else {
2708a9e06f2eSYi-Tang Chiu 			rtw89_phy_cfo_reset(rtwdev);
2709a9e06f2eSYi-Tang Chiu 		}
2710a9e06f2eSYi-Tang Chiu 		return;
2711a9e06f2eSYi-Tang Chiu 	}
2712a9e06f2eSYi-Tang Chiu 	if (cfo->crystal_cap >= cfo->x_cap_ub ||
2713a9e06f2eSYi-Tang Chiu 	    cfo->crystal_cap <= cfo->x_cap_lb) {
2714a9e06f2eSYi-Tang Chiu 		cfo->divergence_lock_en = true;
2715a9e06f2eSYi-Tang Chiu 		rtw89_phy_cfo_reset(rtwdev);
2716a9e06f2eSYi-Tang Chiu 		return;
2717a9e06f2eSYi-Tang Chiu 	}
2718a9e06f2eSYi-Tang Chiu 
2719e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo);
2720e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = new_cfo;
27219f9882dbSEric Huang 	cfo->dcfo_avg_pre = cfo->dcfo_avg;
27221646ce8fSYe Guojin 	x_cap_update =  cfo->crystal_cap != pre_x_cap;
2723e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update);
2724e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n",
2725e3ec7017SPing-Ke Shih 		    cfo->def_x_cap, pre_x_cap, cfo->crystal_cap,
2726e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2727e3ec7017SPing-Ke Shih 	if (x_cap_update) {
27289f9882dbSEric Huang 		if (cfo->dcfo_avg > 0)
27299f9882dbSEric Huang 			cfo->dcfo_avg -= CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2730e3ec7017SPing-Ke Shih 		else
27319f9882dbSEric Huang 			cfo->dcfo_avg += CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2732e3ec7017SPing-Ke Shih 	}
27339f9882dbSEric Huang 	rtw89_dcfo_comp(rtwdev, cfo->dcfo_avg);
2734e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_statistics_reset(rtwdev);
2735e3ec7017SPing-Ke Shih }
2736e3ec7017SPing-Ke Shih 
2737e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work)
2738e3ec7017SPing-Ke Shih {
2739e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2740e3ec7017SPing-Ke Shih 						cfo_track_work.work);
2741e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2742e3ec7017SPing-Ke Shih 
2743e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
2744e3ec7017SPing-Ke Shih 	if (!cfo->cfo_trig_by_timer_en)
2745e3ec7017SPing-Ke Shih 		goto out;
2746e3ec7017SPing-Ke Shih 	rtw89_leave_ps_mode(rtwdev);
2747e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2748e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2749e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2750e3ec7017SPing-Ke Shih out:
2751e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
2752e3ec7017SPing-Ke Shih }
2753e3ec7017SPing-Ke Shih 
2754e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev)
2755e3ec7017SPing-Ke Shih {
2756e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2757e3ec7017SPing-Ke Shih 
2758e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2759e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2760e3ec7017SPing-Ke Shih }
2761e3ec7017SPing-Ke Shih 
2762e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev)
2763e3ec7017SPing-Ke Shih {
2764e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2765e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2766bc013052SEric Huang 	bool is_ul_ofdma = false, ofdma_acc_en = false;
2767bc013052SEric Huang 
2768bc013052SEric Huang 	if (stats->rx_tf_periodic > CFO_TF_CNT_TH)
2769bc013052SEric Huang 		is_ul_ofdma = true;
2770bc013052SEric Huang 	if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE &&
2771bc013052SEric Huang 	    is_ul_ofdma)
2772bc013052SEric Huang 		ofdma_acc_en = true;
2773e3ec7017SPing-Ke Shih 
2774e3ec7017SPing-Ke Shih 	switch (cfo->phy_cfo_status) {
2775e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_NORMAL:
2776e3ec7017SPing-Ke Shih 		if (stats->tx_throughput >= CFO_TP_UPPER) {
2777e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE;
2778e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = true;
2779e3ec7017SPing-Ke Shih 			cfo->cfo_timer_ms = CFO_COMP_PERIOD;
2780e3ec7017SPing-Ke Shih 			rtw89_phy_cfo_start_work(rtwdev);
2781e3ec7017SPing-Ke Shih 		}
2782e3ec7017SPing-Ke Shih 		break;
2783e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_ENHANCE:
2784bc013052SEric Huang 		if (stats->tx_throughput <= CFO_TP_LOWER)
2785bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2786bc013052SEric Huang 		else if (ofdma_acc_en &&
2787bc013052SEric Huang 			 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT)
2788bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD;
2789bc013052SEric Huang 		else
2790bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2791bc013052SEric Huang 
2792bc013052SEric Huang 		if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) {
2793e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2794e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2795e3ec7017SPing-Ke Shih 		}
2796bc013052SEric Huang 		break;
2797bc013052SEric Huang 	case RTW89_PHY_DCFO_STATE_HOLD:
2798e3ec7017SPing-Ke Shih 		if (stats->tx_throughput <= CFO_TP_LOWER) {
2799e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2800e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2801e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2802bc013052SEric Huang 		} else {
2803bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2804e3ec7017SPing-Ke Shih 		}
2805e3ec7017SPing-Ke Shih 		break;
2806e3ec7017SPing-Ke Shih 	default:
2807e3ec7017SPing-Ke Shih 		cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2808e3ec7017SPing-Ke Shih 		cfo->phy_cfo_trk_cnt = 0;
2809e3ec7017SPing-Ke Shih 		break;
2810e3ec7017SPing-Ke Shih 	}
2811e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2812e3ec7017SPing-Ke Shih 		    "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n",
2813e3ec7017SPing-Ke Shih 		    stats->tx_throughput, cfo->phy_cfo_status,
2814e3ec7017SPing-Ke Shih 		    cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt,
2815e3ec7017SPing-Ke Shih 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[0]));
2816e3ec7017SPing-Ke Shih 	if (cfo->cfo_trig_by_timer_en)
2817e3ec7017SPing-Ke Shih 		return;
2818e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2819e3ec7017SPing-Ke Shih }
2820e3ec7017SPing-Ke Shih 
2821e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val,
2822e3ec7017SPing-Ke Shih 			 struct rtw89_rx_phy_ppdu *phy_ppdu)
2823e3ec7017SPing-Ke Shih {
2824e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2825e3ec7017SPing-Ke Shih 	u8 macid = phy_ppdu->mac_id;
2826e3ec7017SPing-Ke Shih 
282797df8587SPing-Ke Shih 	if (macid >= CFO_TRACK_MAX_USER) {
282897df8587SPing-Ke Shih 		rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid);
282997df8587SPing-Ke Shih 		return;
283097df8587SPing-Ke Shih 	}
283197df8587SPing-Ke Shih 
2832e3ec7017SPing-Ke Shih 	cfo->cfo_tail[macid] += cfo_val;
2833e3ec7017SPing-Ke Shih 	cfo->cfo_cnt[macid]++;
2834e3ec7017SPing-Ke Shih 	cfo->packet_count++;
2835e3ec7017SPing-Ke Shih }
2836e3ec7017SPing-Ke Shih 
283729136c95SEric Huang void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
283829136c95SEric Huang {
283929136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
284029136c95SEric Huang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
284129136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
284229136c95SEric Huang 
284329136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
284429136c95SEric Huang 		return;
284529136c95SEric Huang 
284629136c95SEric Huang 	rtwvif->def_tri_idx =
284729136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG);
284829136c95SEric Huang 
284929136c95SEric Huang 	if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV)
285029136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
285129136c95SEric Huang 	else if (chan->band_type >= RTW89_BAND_5G &&
285229136c95SEric Huang 		 chan->band_width >= RTW89_CHANNEL_WIDTH_40)
285329136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = true;
285429136c95SEric Huang 	else
285529136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
285629136c95SEric Huang 
285729136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
285829136c95SEric Huang 		    "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n",
285929136c95SEric Huang 		    ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx);
286029136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
286129136c95SEric Huang 		    "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n",
286229136c95SEric Huang 		    rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en);
286329136c95SEric Huang }
286429136c95SEric Huang 
286529136c95SEric Huang struct rtw89_phy_ul_tb_check_data {
286629136c95SEric Huang 	bool valid;
286729136c95SEric Huang 	bool high_tf_client;
286829136c95SEric Huang 	bool low_tf_client;
286929136c95SEric Huang 	bool dyn_tb_bedge_en;
287029136c95SEric Huang 	u8 def_tri_idx;
287129136c95SEric Huang };
287229136c95SEric Huang 
287329136c95SEric Huang static
287429136c95SEric Huang void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev,
287529136c95SEric Huang 				struct rtw89_vif *rtwvif,
287629136c95SEric Huang 				struct rtw89_phy_ul_tb_check_data *ul_tb_data)
287729136c95SEric Huang {
287829136c95SEric Huang 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
287929136c95SEric Huang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
288029136c95SEric Huang 
288129136c95SEric Huang 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION)
288229136c95SEric Huang 		return;
288329136c95SEric Huang 
288429136c95SEric Huang 	if (!vif->cfg.assoc)
288529136c95SEric Huang 		return;
288629136c95SEric Huang 
288729136c95SEric Huang 	if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH)
288829136c95SEric Huang 		ul_tb_data->high_tf_client = true;
288929136c95SEric Huang 	else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH)
289029136c95SEric Huang 		ul_tb_data->low_tf_client = true;
289129136c95SEric Huang 
289229136c95SEric Huang 	ul_tb_data->valid = true;
289329136c95SEric Huang 	ul_tb_data->def_tri_idx = rtwvif->def_tri_idx;
289429136c95SEric Huang 	ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en;
289529136c95SEric Huang }
289629136c95SEric Huang 
289729136c95SEric Huang void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev)
289829136c95SEric Huang {
289929136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
290029136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
290129136c95SEric Huang 	struct rtw89_phy_ul_tb_check_data ul_tb_data = {};
290229136c95SEric Huang 	struct rtw89_vif *rtwvif;
290329136c95SEric Huang 
290429136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
290529136c95SEric Huang 		return;
290629136c95SEric Huang 
290729136c95SEric Huang 	if (rtwdev->total_sta_assoc != 1)
290829136c95SEric Huang 		return;
290929136c95SEric Huang 
291029136c95SEric Huang 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
291129136c95SEric Huang 		rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data);
291229136c95SEric Huang 
291329136c95SEric Huang 	if (!ul_tb_data.valid)
291429136c95SEric Huang 		return;
291529136c95SEric Huang 
291629136c95SEric Huang 	if (ul_tb_data.dyn_tb_bedge_en) {
291729136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
291829136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0);
291929136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
292029136c95SEric Huang 				    "[ULTB] Turn off if_bandedge\n");
292129136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
292229136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN,
292329136c95SEric Huang 					       ul_tb_info->def_if_bandedge);
292429136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
292529136c95SEric Huang 				    "[ULTB] Set to default if_bandedge = %d\n",
292629136c95SEric Huang 				    ul_tb_info->def_if_bandedge);
292729136c95SEric Huang 		}
292829136c95SEric Huang 	}
292929136c95SEric Huang 
293029136c95SEric Huang 	if (ul_tb_info->dyn_tb_tri_en) {
293129136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
293229136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
293329136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG, 0);
293429136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
293529136c95SEric Huang 				    "[ULTB] Turn off Tx triangle\n");
293629136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
293729136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
293829136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG,
293929136c95SEric Huang 					       ul_tb_data.def_tri_idx);
294029136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
294129136c95SEric Huang 				    "[ULTB] Set to default tx_shap_idx = %d\n",
294229136c95SEric Huang 				    ul_tb_data.def_tri_idx);
294329136c95SEric Huang 		}
294429136c95SEric Huang 	}
294529136c95SEric Huang }
294629136c95SEric Huang 
294729136c95SEric Huang static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev)
294829136c95SEric Huang {
294929136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
295029136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
295129136c95SEric Huang 
295229136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
295329136c95SEric Huang 		return;
295429136c95SEric Huang 
295529136c95SEric Huang 	ul_tb_info->dyn_tb_tri_en = true;
295629136c95SEric Huang 	ul_tb_info->def_if_bandedge =
295729136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN);
295829136c95SEric Huang }
295929136c95SEric Huang 
2960e3715859SEric Huang static
2961e3715859SEric Huang void rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats *antdiv_sts)
2962e3715859SEric Huang {
2963e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->cck_rssi_avg);
2964e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->ofdm_rssi_avg);
2965e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->non_legacy_rssi_avg);
2966e3715859SEric Huang 	antdiv_sts->pkt_cnt_cck = 0;
2967e3715859SEric Huang 	antdiv_sts->pkt_cnt_ofdm = 0;
2968e3715859SEric Huang 	antdiv_sts->pkt_cnt_non_legacy = 0;
29695feecb40SEric Huang 	antdiv_sts->evm = 0;
2970e3715859SEric Huang }
2971e3715859SEric Huang 
2972e3715859SEric Huang static void rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev *rtwdev,
2973e3715859SEric Huang 					      struct rtw89_rx_phy_ppdu *phy_ppdu,
2974e3715859SEric Huang 					      struct rtw89_antdiv_stats *stats)
2975e3715859SEric Huang {
2976e3715859SEric Huang 	if (GET_DATA_RATE_MODE(phy_ppdu->rate) == DATA_RATE_MODE_NON_HT) {
2977e3715859SEric Huang 		if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) {
2978e3715859SEric Huang 			ewma_rssi_add(&stats->cck_rssi_avg, phy_ppdu->rssi_avg);
2979e3715859SEric Huang 			stats->pkt_cnt_cck++;
2980e3715859SEric Huang 		} else {
2981e3715859SEric Huang 			ewma_rssi_add(&stats->ofdm_rssi_avg, phy_ppdu->rssi_avg);
2982e3715859SEric Huang 			stats->pkt_cnt_ofdm++;
29835feecb40SEric Huang 			stats->evm += phy_ppdu->ofdm.evm_min;
2984e3715859SEric Huang 		}
2985e3715859SEric Huang 	} else {
2986e3715859SEric Huang 		ewma_rssi_add(&stats->non_legacy_rssi_avg, phy_ppdu->rssi_avg);
2987e3715859SEric Huang 		stats->pkt_cnt_non_legacy++;
29885feecb40SEric Huang 		stats->evm += phy_ppdu->ofdm.evm_min;
2989e3715859SEric Huang 	}
2990e3715859SEric Huang }
2991e3715859SEric Huang 
2992e3715859SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats *stats)
2993e3715859SEric Huang {
2994e3715859SEric Huang 	if (stats->pkt_cnt_non_legacy >= stats->pkt_cnt_cck &&
2995e3715859SEric Huang 	    stats->pkt_cnt_non_legacy >= stats->pkt_cnt_ofdm)
2996e3715859SEric Huang 		return ewma_rssi_read(&stats->non_legacy_rssi_avg);
2997e3715859SEric Huang 	else if (stats->pkt_cnt_ofdm >= stats->pkt_cnt_cck &&
2998e3715859SEric Huang 		 stats->pkt_cnt_ofdm >= stats->pkt_cnt_non_legacy)
2999e3715859SEric Huang 		return ewma_rssi_read(&stats->ofdm_rssi_avg);
3000e3715859SEric Huang 	else
3001e3715859SEric Huang 		return ewma_rssi_read(&stats->cck_rssi_avg);
3002e3715859SEric Huang }
3003e3715859SEric Huang 
30045feecb40SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats *stats)
30055feecb40SEric Huang {
30065feecb40SEric Huang 	return phy_div(stats->evm, stats->pkt_cnt_non_legacy + stats->pkt_cnt_ofdm);
30075feecb40SEric Huang }
30085feecb40SEric Huang 
3009e3715859SEric Huang void rtw89_phy_antdiv_parse(struct rtw89_dev *rtwdev,
3010e3715859SEric Huang 			    struct rtw89_rx_phy_ppdu *phy_ppdu)
3011e3715859SEric Huang {
3012e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3013e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3014e3715859SEric Huang 
3015e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
3016e3715859SEric Huang 		return;
3017e3715859SEric Huang 
3018e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->target_stats);
3019e3715859SEric Huang 
3020e3715859SEric Huang 	if (!antdiv->get_stats)
3021e3715859SEric Huang 		return;
3022e3715859SEric Huang 
3023e3715859SEric Huang 	if (hal->antenna_rx == RF_A)
3024e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->main_stats);
3025e3715859SEric Huang 	else if (hal->antenna_rx == RF_B)
3026e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->aux_stats);
3027e3715859SEric Huang }
3028e3715859SEric Huang 
3029a90c613dSEric Huang static void rtw89_phy_antdiv_reg_init(struct rtw89_dev *rtwdev)
3030a90c613dSEric Huang {
3031a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_ANT_TRAIN_EN,
3032a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3033a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_TX_ANT_SEL,
3034a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3035a90c613dSEric Huang 
3036a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_TRSW_TX_EXTEND,
3037a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3038a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_HW_ANTSW_DIS_BY_GNT_BT,
3039a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3040a90c613dSEric Huang 
3041a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_BT_FORCE_ANTIDX_EN,
3042a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3043a90c613dSEric Huang 
3044a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_RFSW_CTRL_ANT0_BASE, B_RFSW_CTRL_ANT_MAPPING,
3045a90c613dSEric Huang 			      0x0100, RTW89_PHY_0);
3046a90c613dSEric Huang 
3047a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_BTG_TRX,
3048a90c613dSEric Huang 			      0x1, RTW89_PHY_0);
3049a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_HW_CTRL,
3050a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3051a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_2G,
3052a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3053a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_5G,
3054a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3055a90c613dSEric Huang }
3056a90c613dSEric Huang 
3057e3715859SEric Huang static void rtw89_phy_antdiv_sts_reset(struct rtw89_dev *rtwdev)
3058e3715859SEric Huang {
3059e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3060e3715859SEric Huang 
3061e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
3062e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->main_stats);
3063e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->aux_stats);
3064e3715859SEric Huang }
3065e3715859SEric Huang 
3066a90c613dSEric Huang static void rtw89_phy_antdiv_init(struct rtw89_dev *rtwdev)
3067a90c613dSEric Huang {
3068e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3069a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3070a90c613dSEric Huang 
3071a90c613dSEric Huang 	if (!hal->ant_diversity)
3072a90c613dSEric Huang 		return;
3073a90c613dSEric Huang 
3074e3715859SEric Huang 	antdiv->get_stats = false;
3075e3715859SEric Huang 	antdiv->rssi_pre = 0;
3076e3715859SEric Huang 	rtw89_phy_antdiv_sts_reset(rtwdev);
3077a90c613dSEric Huang 	rtw89_phy_antdiv_reg_init(rtwdev);
3078a90c613dSEric Huang }
3079a90c613dSEric Huang 
3080e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev)
3081e3ec7017SPing-Ke Shih {
3082e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3083e3ec7017SPing-Ke Shih 	int i;
3084e3ec7017SPing-Ke Shih 	u8 th;
3085e3ec7017SPing-Ke Shih 
3086e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++) {
3087e3ec7017SPing-Ke Shih 		th = rtw89_chip_get_thermal(rtwdev, i);
3088e3ec7017SPing-Ke Shih 		if (th)
3089e3ec7017SPing-Ke Shih 			ewma_thermal_add(&phystat->avg_thermal[i], th);
3090e3ec7017SPing-Ke Shih 
3091e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3092e3ec7017SPing-Ke Shih 			    "path(%d) thermal cur=%u avg=%ld", i, th,
3093e3ec7017SPing-Ke Shih 			    ewma_thermal_read(&phystat->avg_thermal[i]));
3094e3ec7017SPing-Ke Shih 	}
3095e3ec7017SPing-Ke Shih }
3096e3ec7017SPing-Ke Shih 
3097e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data {
3098e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
3099e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info;
3100e3ec7017SPing-Ke Shih 	bool rssi_changed;
3101e3ec7017SPing-Ke Shih };
3102e3ec7017SPing-Ke Shih 
3103e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data,
3104e3ec7017SPing-Ke Shih 					    struct ieee80211_sta *sta)
3105e3ec7017SPing-Ke Shih {
3106e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3107e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data *rssi_data =
3108e3ec7017SPing-Ke Shih 					(struct rtw89_phy_iter_rssi_data *)data;
3109e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info;
3110e3ec7017SPing-Ke Shih 	unsigned long rssi_curr;
3111e3ec7017SPing-Ke Shih 
3112e3ec7017SPing-Ke Shih 	rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi);
3113e3ec7017SPing-Ke Shih 
3114e3ec7017SPing-Ke Shih 	if (rssi_curr < ch_info->rssi_min) {
3115e3ec7017SPing-Ke Shih 		ch_info->rssi_min = rssi_curr;
3116e3ec7017SPing-Ke Shih 		ch_info->rssi_min_macid = rtwsta->mac_id;
3117e3ec7017SPing-Ke Shih 	}
3118e3ec7017SPing-Ke Shih 
3119e3ec7017SPing-Ke Shih 	if (rtwsta->prev_rssi == 0) {
3120e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3121e3ec7017SPing-Ke Shih 	} else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) {
3122e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3123e3ec7017SPing-Ke Shih 		rssi_data->rssi_changed = true;
3124e3ec7017SPing-Ke Shih 	}
3125e3ec7017SPing-Ke Shih }
3126e3ec7017SPing-Ke Shih 
3127e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev)
3128e3ec7017SPing-Ke Shih {
3129e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data rssi_data = {0};
3130e3ec7017SPing-Ke Shih 
3131e3ec7017SPing-Ke Shih 	rssi_data.rtwdev = rtwdev;
3132e3ec7017SPing-Ke Shih 	rssi_data.ch_info = &rtwdev->ch_info;
3133e3ec7017SPing-Ke Shih 	rssi_data.ch_info->rssi_min = U8_MAX;
3134e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
3135e3ec7017SPing-Ke Shih 					  rtw89_phy_stat_rssi_update_iter,
3136e3ec7017SPing-Ke Shih 					  &rssi_data);
3137e3ec7017SPing-Ke Shih 	if (rssi_data.rssi_changed)
3138e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_wl_sta(rtwdev);
3139e3ec7017SPing-Ke Shih }
3140e3ec7017SPing-Ke Shih 
3141e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev)
3142e3ec7017SPing-Ke Shih {
3143e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3144e3ec7017SPing-Ke Shih 	int i;
3145e3ec7017SPing-Ke Shih 
3146e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
3147e3ec7017SPing-Ke Shih 		ewma_thermal_init(&phystat->avg_thermal[i]);
3148e3ec7017SPing-Ke Shih 
3149e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3150e3ec7017SPing-Ke Shih 
3151e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3152e3ec7017SPing-Ke Shih 	memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat));
3153e3ec7017SPing-Ke Shih }
3154e3ec7017SPing-Ke Shih 
3155e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev)
3156e3ec7017SPing-Ke Shih {
3157e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3158e3ec7017SPing-Ke Shih 
3159e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3160e3ec7017SPing-Ke Shih 	rtw89_phy_stat_rssi_update(rtwdev);
3161e3ec7017SPing-Ke Shih 
3162e3ec7017SPing-Ke Shih 	phystat->last_pkt_stat = phystat->cur_pkt_stat;
3163e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3164e3ec7017SPing-Ke Shih }
3165e3ec7017SPing-Ke Shih 
3166e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us)
3167e3ec7017SPing-Ke Shih {
3168e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3169e3ec7017SPing-Ke Shih 
3170e3ec7017SPing-Ke Shih 	return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3171e3ec7017SPing-Ke Shih }
3172e3ec7017SPing-Ke Shih 
3173e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx)
3174e3ec7017SPing-Ke Shih {
3175e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3176e3ec7017SPing-Ke Shih 
3177e3ec7017SPing-Ke Shih 	return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3178e3ec7017SPing-Ke Shih }
3179e3ec7017SPing-Ke Shih 
3180e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev)
3181e3ec7017SPing-Ke Shih {
3182e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3183e3ec7017SPing-Ke Shih 
3184e3ec7017SPing-Ke Shih 	env->ccx_manual_ctrl = false;
3185e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3186e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3187e3ec7017SPing-Ke Shih 	env->ccx_rpt_stamp = 0;
3188e3ec7017SPing-Ke Shih 	env->ccx_period = 0;
3189e3ec7017SPing-Ke Shih 	env->ccx_unit_idx = RTW89_CCX_32_US;
3190e3ec7017SPing-Ke Shih 	env->ccx_trigger_time = 0;
3191e3ec7017SPing-Ke Shih 	env->ccx_edcca_opt_bw_idx = RTW89_CCX_EDCCA_BW20_0;
3192e3ec7017SPing-Ke Shih 
3193e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1);
3194e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1);
3195e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3196e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK,
3197e3ec7017SPing-Ke Shih 			       RTW89_CCX_EDCCA_BW20_0);
3198e3ec7017SPing-Ke Shih }
3199e3ec7017SPing-Ke Shih 
3200e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report,
3201e3ec7017SPing-Ke Shih 				    u16 score)
3202e3ec7017SPing-Ke Shih {
3203e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3204e3ec7017SPing-Ke Shih 	u32 numer = 0;
3205e3ec7017SPing-Ke Shih 	u16 ret = 0;
3206e3ec7017SPing-Ke Shih 
3207e3ec7017SPing-Ke Shih 	numer = report * score + (env->ccx_period >> 1);
3208e3ec7017SPing-Ke Shih 	if (env->ccx_period)
3209e3ec7017SPing-Ke Shih 		ret = numer / env->ccx_period;
3210e3ec7017SPing-Ke Shih 
3211e3ec7017SPing-Ke Shih 	return ret >= score ? score - 1 : ret;
3212e3ec7017SPing-Ke Shih }
3213e3ec7017SPing-Ke Shih 
3214e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev,
3215e3ec7017SPing-Ke Shih 					    u16 time_ms, u32 *period,
3216e3ec7017SPing-Ke Shih 					    u32 *unit_idx)
3217e3ec7017SPing-Ke Shih {
3218e3ec7017SPing-Ke Shih 	u32 idx;
3219e3ec7017SPing-Ke Shih 	u8 quotient;
3220e3ec7017SPing-Ke Shih 
3221e3ec7017SPing-Ke Shih 	if (time_ms >= CCX_MAX_PERIOD)
3222e3ec7017SPing-Ke Shih 		time_ms = CCX_MAX_PERIOD;
3223e3ec7017SPing-Ke Shih 
3224e3ec7017SPing-Ke Shih 	quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD;
3225e3ec7017SPing-Ke Shih 
3226e3ec7017SPing-Ke Shih 	if (quotient < 4)
3227e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_4_US;
3228e3ec7017SPing-Ke Shih 	else if (quotient < 8)
3229e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_8_US;
3230e3ec7017SPing-Ke Shih 	else if (quotient < 16)
3231e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_16_US;
3232e3ec7017SPing-Ke Shih 	else
3233e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_32_US;
3234e3ec7017SPing-Ke Shih 
3235e3ec7017SPing-Ke Shih 	*unit_idx = idx;
3236e3ec7017SPing-Ke Shih 	*period = (time_ms * MS_TO_4US_RATIO) >> idx;
3237e3ec7017SPing-Ke Shih 
3238e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3239e3ec7017SPing-Ke Shih 		    "[Trigger Time] period:%d, unit_idx:%d\n",
3240e3ec7017SPing-Ke Shih 		    *period, *unit_idx);
3241e3ec7017SPing-Ke Shih }
3242e3ec7017SPing-Ke Shih 
3243e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev)
3244e3ec7017SPing-Ke Shih {
3245e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3246e3ec7017SPing-Ke Shih 
3247e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3248e3ec7017SPing-Ke Shih 		    "lv:(%d)->(0)\n", env->ccx_rac_lv);
3249e3ec7017SPing-Ke Shih 
3250e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3251e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3252e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3253e3ec7017SPing-Ke Shih }
3254e3ec7017SPing-Ke Shih 
3255e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev,
3256e3ec7017SPing-Ke Shih 					      struct rtw89_ccx_para_info *para)
3257e3ec7017SPing-Ke Shih {
3258e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3259e3ec7017SPing-Ke Shih 	bool is_update = env->ifs_clm_app != para->ifs_clm_app;
3260e3ec7017SPing-Ke Shih 	u8 i = 0;
3261e3ec7017SPing-Ke Shih 	u16 *ifs_th_l = env->ifs_clm_th_l;
3262e3ec7017SPing-Ke Shih 	u16 *ifs_th_h = env->ifs_clm_th_h;
3263e3ec7017SPing-Ke Shih 	u32 ifs_th0_us = 0, ifs_th_times = 0;
3264e3ec7017SPing-Ke Shih 	u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0};
3265e3ec7017SPing-Ke Shih 
3266e3ec7017SPing-Ke Shih 	if (!is_update)
3267e3ec7017SPing-Ke Shih 		goto ifs_update_finished;
3268e3ec7017SPing-Ke Shih 
3269e3ec7017SPing-Ke Shih 	switch (para->ifs_clm_app) {
3270e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_INIT:
3271e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_BACKGROUND:
3272e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_ACS:
3273e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG:
3274e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DIG:
3275e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_TDMA_DIG:
3276e3ec7017SPing-Ke Shih 		ifs_th0_us = IFS_CLM_TH0_UPPER;
3277e3ec7017SPing-Ke Shih 		ifs_th_times = IFS_CLM_TH_MUL;
3278e3ec7017SPing-Ke Shih 		break;
3279e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG_MANUAL:
3280e3ec7017SPing-Ke Shih 		ifs_th0_us = para->ifs_clm_manual_th0;
3281e3ec7017SPing-Ke Shih 		ifs_th_times = para->ifs_clm_manual_th_times;
3282e3ec7017SPing-Ke Shih 		break;
3283e3ec7017SPing-Ke Shih 	default:
3284e3ec7017SPing-Ke Shih 		break;
3285e3ec7017SPing-Ke Shih 	}
3286e3ec7017SPing-Ke Shih 
3287e3ec7017SPing-Ke Shih 	/* Set sampling threshold for 4 different regions, unit in idx_cnt.
3288e3ec7017SPing-Ke Shih 	 * low[i] = high[i-1] + 1
3289e3ec7017SPing-Ke Shih 	 * high[i] = high[i-1] * ifs_th_times
3290e3ec7017SPing-Ke Shih 	 */
3291e3ec7017SPing-Ke Shih 	ifs_th_l[IFS_CLM_TH_START_IDX] = 0;
3292e3ec7017SPing-Ke Shih 	ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us;
3293e3ec7017SPing-Ke Shih 	ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev,
3294e3ec7017SPing-Ke Shih 								 ifs_th0_us);
3295e3ec7017SPing-Ke Shih 	for (i = 1; i < RTW89_IFS_CLM_NUM; i++) {
3296e3ec7017SPing-Ke Shih 		ifs_th_l[i] = ifs_th_h[i - 1] + 1;
3297e3ec7017SPing-Ke Shih 		ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times;
3298e3ec7017SPing-Ke Shih 		ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]);
3299e3ec7017SPing-Ke Shih 	}
3300e3ec7017SPing-Ke Shih 
3301e3ec7017SPing-Ke Shih ifs_update_finished:
3302e3ec7017SPing-Ke Shih 	if (!is_update)
3303e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3304e3ec7017SPing-Ke Shih 			    "No need to update IFS_TH\n");
3305e3ec7017SPing-Ke Shih 
3306e3ec7017SPing-Ke Shih 	return is_update;
3307e3ec7017SPing-Ke Shih }
3308e3ec7017SPing-Ke Shih 
3309e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev)
3310e3ec7017SPing-Ke Shih {
3311e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3312e3ec7017SPing-Ke Shih 	u8 i = 0;
3313e3ec7017SPing-Ke Shih 
3314e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK,
3315e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[0]);
3316e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK,
3317e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[1]);
3318e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK,
3319e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[2]);
3320e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK,
3321e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[3]);
3322e3ec7017SPing-Ke Shih 
3323e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK,
3324e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[0]);
3325e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK,
3326e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[1]);
3327e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK,
3328e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[2]);
3329e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK,
3330e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[3]);
3331e3ec7017SPing-Ke Shih 
3332e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3333e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3334e3ec7017SPing-Ke Shih 			    "Update IFS_T%d_th{low, high} : {%d, %d}\n",
3335e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]);
3336e3ec7017SPing-Ke Shih }
3337e3ec7017SPing-Ke Shih 
3338e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev)
3339e3ec7017SPing-Ke Shih {
3340e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3341e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3342e3ec7017SPing-Ke Shih 
3343e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3344e3ec7017SPing-Ke Shih 	env->ifs_clm_mntr_time = 0;
3345e3ec7017SPing-Ke Shih 
3346e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_INIT;
3347e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, &para))
3348e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3349e3ec7017SPing-Ke Shih 
3350e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN,
3351e3ec7017SPing-Ke Shih 			       true);
3352e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true);
3353e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true);
3354e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true);
3355e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true);
3356e3ec7017SPing-Ke Shih }
3357e3ec7017SPing-Ke Shih 
3358e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev,
3359e3ec7017SPing-Ke Shih 				     enum rtw89_env_racing_lv level)
3360e3ec7017SPing-Ke Shih {
3361e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3362e3ec7017SPing-Ke Shih 	int ret = 0;
3363e3ec7017SPing-Ke Shih 
3364e3ec7017SPing-Ke Shih 	if (level >= RTW89_RAC_MAX_NUM) {
3365e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3366e3ec7017SPing-Ke Shih 			    "[WARNING] Wrong LV=%d\n", level);
3367e3ec7017SPing-Ke Shih 		return -EINVAL;
3368e3ec7017SPing-Ke Shih 	}
3369e3ec7017SPing-Ke Shih 
3370e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3371e3ec7017SPing-Ke Shih 		    "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing,
3372e3ec7017SPing-Ke Shih 		    env->ccx_rac_lv, level);
3373e3ec7017SPing-Ke Shih 
3374e3ec7017SPing-Ke Shih 	if (env->ccx_ongoing) {
3375e3ec7017SPing-Ke Shih 		if (level <= env->ccx_rac_lv)
3376e3ec7017SPing-Ke Shih 			ret = -EINVAL;
3377e3ec7017SPing-Ke Shih 		else
3378e3ec7017SPing-Ke Shih 			env->ccx_ongoing = false;
3379e3ec7017SPing-Ke Shih 	}
3380e3ec7017SPing-Ke Shih 
3381e3ec7017SPing-Ke Shih 	if (ret == 0)
3382e3ec7017SPing-Ke Shih 		env->ccx_rac_lv = level;
3383e3ec7017SPing-Ke Shih 
3384e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n",
3385e3ec7017SPing-Ke Shih 		    !ret);
3386e3ec7017SPing-Ke Shih 
3387e3ec7017SPing-Ke Shih 	return ret;
3388e3ec7017SPing-Ke Shih }
3389e3ec7017SPing-Ke Shih 
3390e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev)
3391e3ec7017SPing-Ke Shih {
3392e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3393e3ec7017SPing-Ke Shih 
3394e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0);
3395e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0);
3396e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1);
3397e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3398e3ec7017SPing-Ke Shih 
3399e3ec7017SPing-Ke Shih 	env->ccx_rpt_stamp++;
3400e3ec7017SPing-Ke Shih 	env->ccx_ongoing = true;
3401e3ec7017SPing-Ke Shih }
3402e3ec7017SPing-Ke Shih 
3403e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev)
3404e3ec7017SPing-Ke Shih {
3405e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3406e3ec7017SPing-Ke Shih 	u8 i = 0;
3407e3ec7017SPing-Ke Shih 	u32 res = 0;
3408e3ec7017SPing-Ke Shih 
3409e3ec7017SPing-Ke Shih 	env->ifs_clm_tx_ratio =
3410e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT);
3411e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca_ratio =
3412e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca,
3413e3ec7017SPing-Ke Shih 					 PERCENT);
3414e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_ratio =
3415e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT);
3416e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_ratio =
3417e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT);
3418e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_cca_excl_fa_ratio =
3419e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa,
3420e3ec7017SPing-Ke Shih 					 PERCENT);
3421e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_cca_excl_fa_ratio =
3422e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa,
3423e3ec7017SPing-Ke Shih 					 PERCENT);
3424e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_permil =
3425e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL);
3426e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_permil =
3427e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL);
3428e3ec7017SPing-Ke Shih 
3429e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++) {
3430e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) {
3431e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD;
3432e3ec7017SPing-Ke Shih 		} else {
3433e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] =
3434e3ec7017SPing-Ke Shih 				rtw89_phy_ccx_idx_to_us(rtwdev,
3435e3ec7017SPing-Ke Shih 							env->ifs_clm_avg[i]);
3436e3ec7017SPing-Ke Shih 		}
3437e3ec7017SPing-Ke Shih 
3438e3ec7017SPing-Ke Shih 		res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]);
3439e3ec7017SPing-Ke Shih 		res += env->ifs_clm_his[i] >> 1;
3440e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i])
3441e3ec7017SPing-Ke Shih 			res /= env->ifs_clm_his[i];
3442e3ec7017SPing-Ke Shih 		else
3443e3ec7017SPing-Ke Shih 			res = 0;
3444e3ec7017SPing-Ke Shih 		env->ifs_clm_cca_avg[i] = res;
3445e3ec7017SPing-Ke Shih 	}
3446e3ec7017SPing-Ke Shih 
3447e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3448e3ec7017SPing-Ke Shih 		    "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3449e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio);
3450e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3451e3ec7017SPing-Ke Shih 		    "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n",
3452e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio);
3453e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3454e3ec7017SPing-Ke Shih 		    "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n",
3455e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil);
3456e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3457e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n",
3458e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_cca_excl_fa_ratio,
3459e3ec7017SPing-Ke Shih 		    env->ifs_clm_ofdm_cca_excl_fa_ratio);
3460e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3461e3ec7017SPing-Ke Shih 		    "Time:[his, ifs_avg(us), cca_avg(us)]\n");
3462e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3463e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n",
3464e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i],
3465e3ec7017SPing-Ke Shih 			    env->ifs_clm_cca_avg[i]);
3466e3ec7017SPing-Ke Shih }
3467e3ec7017SPing-Ke Shih 
3468e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev)
3469e3ec7017SPing-Ke Shih {
3470e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3471e3ec7017SPing-Ke Shih 	u8 i = 0;
3472e3ec7017SPing-Ke Shih 
3473e3ec7017SPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) {
3474e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3475e3ec7017SPing-Ke Shih 			    "Get IFS_CLM report Fail\n");
3476e3ec7017SPing-Ke Shih 		return false;
3477e3ec7017SPing-Ke Shih 	}
3478e3ec7017SPing-Ke Shih 
3479e3ec7017SPing-Ke Shih 	env->ifs_clm_tx =
3480e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3481e3ec7017SPing-Ke Shih 				      B_IFS_CLM_TX_CNT_MSK);
3482e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca =
3483e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3484e3ec7017SPing-Ke Shih 				      B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK);
3485e3ec7017SPing-Ke Shih 	env->ifs_clm_cckcca_excl_fa =
3486e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3487e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK);
3488e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmcca_excl_fa =
3489e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3490e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK);
3491e3ec7017SPing-Ke Shih 	env->ifs_clm_cckfa =
3492e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3493e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCK_FA_MSK);
3494e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmfa =
3495e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3496e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDM_FA_MSK);
3497e3ec7017SPing-Ke Shih 
3498e3ec7017SPing-Ke Shih 	env->ifs_clm_his[0] =
3499e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK);
3500e3ec7017SPing-Ke Shih 	env->ifs_clm_his[1] =
3501e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK);
3502e3ec7017SPing-Ke Shih 	env->ifs_clm_his[2] =
3503e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK);
3504e3ec7017SPing-Ke Shih 	env->ifs_clm_his[3] =
3505e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK);
3506e3ec7017SPing-Ke Shih 
3507e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[0] =
3508e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK);
3509e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[1] =
3510e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK);
3511e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[2] =
3512e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK);
3513e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[3] =
3514e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK);
3515e3ec7017SPing-Ke Shih 
3516e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[0] =
3517e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK);
3518e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[1] =
3519e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK);
3520e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[2] =
3521e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK);
3522e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[3] =
3523e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK);
3524e3ec7017SPing-Ke Shih 
3525e3ec7017SPing-Ke Shih 	env->ifs_clm_total_ifs =
3526e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK);
3527e3ec7017SPing-Ke Shih 
3528e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n",
3529e3ec7017SPing-Ke Shih 		    env->ifs_clm_total_ifs);
3530e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3531e3ec7017SPing-Ke Shih 		    "{Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3532e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca);
3533e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3534e3ec7017SPing-Ke Shih 		    "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n",
3535e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckfa, env->ifs_clm_ofdmfa);
3536e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3537e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n",
3538e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa);
3539e3ec7017SPing-Ke Shih 
3540e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n");
3541e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3542e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3543e3ec7017SPing-Ke Shih 			    "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i],
3544e3ec7017SPing-Ke Shih 			    env->ifs_clm_avg[i], env->ifs_clm_cca[i]);
3545e3ec7017SPing-Ke Shih 
3546e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_get_utility(rtwdev);
3547e3ec7017SPing-Ke Shih 
3548e3ec7017SPing-Ke Shih 	return true;
3549e3ec7017SPing-Ke Shih }
3550e3ec7017SPing-Ke Shih 
3551e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev,
3552e3ec7017SPing-Ke Shih 				 struct rtw89_ccx_para_info *para)
3553e3ec7017SPing-Ke Shih {
3554e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3555e3ec7017SPing-Ke Shih 	u32 period = 0;
3556e3ec7017SPing-Ke Shih 	u32 unit_idx = 0;
3557e3ec7017SPing-Ke Shih 
3558e3ec7017SPing-Ke Shih 	if (para->mntr_time == 0) {
3559e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3560e3ec7017SPing-Ke Shih 			    "[WARN] MNTR_TIME is 0\n");
3561e3ec7017SPing-Ke Shih 		return -EINVAL;
3562e3ec7017SPing-Ke Shih 	}
3563e3ec7017SPing-Ke Shih 
3564e3ec7017SPing-Ke Shih 	if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv))
3565e3ec7017SPing-Ke Shih 		return -EINVAL;
3566e3ec7017SPing-Ke Shih 
3567e3ec7017SPing-Ke Shih 	if (para->mntr_time != env->ifs_clm_mntr_time) {
3568e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time,
3569e3ec7017SPing-Ke Shih 						&period, &unit_idx);
3570e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3571e3ec7017SPing-Ke Shih 				       B_IFS_CLM_PERIOD_MSK, period);
3572e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3573e3ec7017SPing-Ke Shih 				       B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx);
3574e3ec7017SPing-Ke Shih 
3575e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3576e3ec7017SPing-Ke Shih 			    "Update IFS-CLM time ((%d)) -> ((%d))\n",
3577e3ec7017SPing-Ke Shih 			    env->ifs_clm_mntr_time, para->mntr_time);
3578e3ec7017SPing-Ke Shih 
3579e3ec7017SPing-Ke Shih 		env->ifs_clm_mntr_time = para->mntr_time;
3580e3ec7017SPing-Ke Shih 		env->ccx_period = (u16)period;
3581e3ec7017SPing-Ke Shih 		env->ccx_unit_idx = (u8)unit_idx;
3582e3ec7017SPing-Ke Shih 	}
3583e3ec7017SPing-Ke Shih 
3584e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) {
3585e3ec7017SPing-Ke Shih 		env->ifs_clm_app = para->ifs_clm_app;
3586e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3587e3ec7017SPing-Ke Shih 	}
3588e3ec7017SPing-Ke Shih 
3589e3ec7017SPing-Ke Shih 	return 0;
3590e3ec7017SPing-Ke Shih }
3591e3ec7017SPing-Ke Shih 
3592e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev)
3593e3ec7017SPing-Ke Shih {
3594e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3595e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3596e3ec7017SPing-Ke Shih 	u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3597e3ec7017SPing-Ke Shih 
3598e3ec7017SPing-Ke Shih 	env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3599e3ec7017SPing-Ke Shih 	if (env->ccx_manual_ctrl) {
3600e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3601e3ec7017SPing-Ke Shih 			    "CCX in manual ctrl\n");
3602e3ec7017SPing-Ke Shih 		return;
3603e3ec7017SPing-Ke Shih 	}
3604e3ec7017SPing-Ke Shih 
3605e3ec7017SPing-Ke Shih 	/* only ifs_clm for now */
3606e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_get_result(rtwdev))
3607e3ec7017SPing-Ke Shih 		env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3608e3ec7017SPing-Ke Shih 
3609e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_racing_release(rtwdev);
3610e3ec7017SPing-Ke Shih 	para.mntr_time = 1900;
3611e3ec7017SPing-Ke Shih 	para.rac_lv = RTW89_RAC_LV_1;
3612e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3613e3ec7017SPing-Ke Shih 
3614e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_set(rtwdev, &para) == 0)
3615e3ec7017SPing-Ke Shih 		chk_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3616e3ec7017SPing-Ke Shih 	if (chk_result)
3617e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_trigger(rtwdev);
3618e3ec7017SPing-Ke Shih 
3619e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3620e3ec7017SPing-Ke Shih 		    "get_result=0x%x, chk_result:0x%x\n",
3621e3ec7017SPing-Ke Shih 		    env->ccx_watchdog_result, chk_result);
3622e3ec7017SPing-Ke Shih }
3623e3ec7017SPing-Ke Shih 
3624eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page)
3625eb4e52b3SPo Hao Huang {
36269e2f177dSZong-Zhe Yang 	if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM ||
3627eb4e52b3SPo Hao Huang 	    *ie_page == RTW89_RSVD_9)
3628eb4e52b3SPo Hao Huang 		return false;
3629eb4e52b3SPo Hao Huang 	else if (*ie_page > RTW89_RSVD_9)
3630eb4e52b3SPo Hao Huang 		*ie_page -= 1;
3631eb4e52b3SPo Hao Huang 
3632eb4e52b3SPo Hao Huang 	return true;
3633eb4e52b3SPo Hao Huang }
3634eb4e52b3SPo Hao Huang 
3635eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)
3636eb4e52b3SPo Hao Huang {
3637eb4e52b3SPo Hao Huang 	static const u8 ie_page_shift = 2;
3638eb4e52b3SPo Hao Huang 
3639eb4e52b3SPo Hao Huang 	return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift);
3640eb4e52b3SPo Hao Huang }
3641eb4e52b3SPo Hao Huang 
3642eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev,
3643eb4e52b3SPo Hao Huang 				      enum rtw89_phy_status_bitmap ie_page)
3644eb4e52b3SPo Hao Huang {
3645eb4e52b3SPo Hao Huang 	u32 addr;
3646eb4e52b3SPo Hao Huang 
3647eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3648eb4e52b3SPo Hao Huang 		return 0;
3649eb4e52b3SPo Hao Huang 
3650eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3651eb4e52b3SPo Hao Huang 
3652eb4e52b3SPo Hao Huang 	return rtw89_phy_read32(rtwdev, addr);
3653eb4e52b3SPo Hao Huang }
3654eb4e52b3SPo Hao Huang 
3655eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev,
3656eb4e52b3SPo Hao Huang 				       enum rtw89_phy_status_bitmap ie_page,
3657eb4e52b3SPo Hao Huang 				       u32 val)
3658eb4e52b3SPo Hao Huang {
3659eb4e52b3SPo Hao Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
3660eb4e52b3SPo Hao Huang 	u32 addr;
3661eb4e52b3SPo Hao Huang 
3662eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3663eb4e52b3SPo Hao Huang 		return;
3664eb4e52b3SPo Hao Huang 
3665eb4e52b3SPo Hao Huang 	if (chip->chip_id == RTL8852A)
3666eb4e52b3SPo Hao Huang 		val &= B_PHY_STS_BITMAP_MSK_52A;
3667eb4e52b3SPo Hao Huang 
3668eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3669eb4e52b3SPo Hao Huang 	rtw89_phy_write32(rtwdev, addr, val);
3670eb4e52b3SPo Hao Huang }
3671eb4e52b3SPo Hao Huang 
3672eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev,
3673eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_bitmap bitmap,
3674eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_ie_type ie,
3675eb4e52b3SPo Hao Huang 					  bool enable)
3676eb4e52b3SPo Hao Huang {
3677eb4e52b3SPo Hao Huang 	u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap);
3678eb4e52b3SPo Hao Huang 
3679eb4e52b3SPo Hao Huang 	if (enable)
3680eb4e52b3SPo Hao Huang 		val |= BIT(ie);
3681eb4e52b3SPo Hao Huang 	else
3682eb4e52b3SPo Hao Huang 		val &= ~BIT(ie);
3683eb4e52b3SPo Hao Huang 
3684eb4e52b3SPo Hao Huang 	rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val);
3685eb4e52b3SPo Hao Huang }
3686eb4e52b3SPo Hao Huang 
3687eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev,
3688eb4e52b3SPo Hao Huang 					    bool enable,
3689eb4e52b3SPo Hao Huang 					    enum rtw89_phy_idx phy_idx)
3690eb4e52b3SPo Hao Huang {
3691eb4e52b3SPo Hao Huang 	if (enable) {
3692eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3693eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3694eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3695eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3696eb4e52b3SPo Hao Huang 	} else {
3697eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3698eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3699eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3700eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3701eb4e52b3SPo Hao Huang 	}
3702eb4e52b3SPo Hao Huang }
3703eb4e52b3SPo Hao Huang 
3704eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev)
3705eb4e52b3SPo Hao Huang {
3706eb4e52b3SPo Hao Huang 	u8 i;
3707eb4e52b3SPo Hao Huang 
3708eb4e52b3SPo Hao Huang 	rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0);
3709eb4e52b3SPo Hao Huang 
3710eb4e52b3SPo Hao Huang 	for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) {
3711eb4e52b3SPo Hao Huang 		if (i >= RTW89_CCK_PKT)
3712eb4e52b3SPo Hao Huang 			rtw89_physts_enable_ie_bitmap(rtwdev, i,
3713eb4e52b3SPo Hao Huang 						      RTW89_PHYSTS_IE09_FTR_0,
3714eb4e52b3SPo Hao Huang 						      true);
3715eb4e52b3SPo Hao Huang 		if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) ||
3716eb4e52b3SPo Hao Huang 		    (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT))
3717eb4e52b3SPo Hao Huang 			continue;
3718eb4e52b3SPo Hao Huang 		rtw89_physts_enable_ie_bitmap(rtwdev, i,
3719eb4e52b3SPo Hao Huang 					      RTW89_PHYSTS_IE24_OFDM_TD_PATH_A,
3720eb4e52b3SPo Hao Huang 					      true);
3721eb4e52b3SPo Hao Huang 	}
3722eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT,
3723eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3724eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT,
3725eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3726eb4e52b3SPo Hao Huang 
3727eb4e52b3SPo Hao Huang 	/* force IE01 for channel index, only channel field is valid */
3728eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT,
3729eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE01_CMN_OFDM, true);
3730eb4e52b3SPo Hao Huang }
3731eb4e52b3SPo Hao Huang 
3732e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type)
3733e3ec7017SPing-Ke Shih {
3734e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3735e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3736e3ec7017SPing-Ke Shih 	const struct rtw89_phy_dig_gain_cfg *cfg;
3737e3ec7017SPing-Ke Shih 	const char *msg;
3738e3ec7017SPing-Ke Shih 	u8 i;
3739e3ec7017SPing-Ke Shih 	s8 gain_base;
3740e3ec7017SPing-Ke Shih 	s8 *gain_arr;
3741e3ec7017SPing-Ke Shih 	u32 tmp;
3742e3ec7017SPing-Ke Shih 
3743e3ec7017SPing-Ke Shih 	switch (type) {
3744e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_G:
3745e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_g;
3746e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3747e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_g;
3748e3ec7017SPing-Ke Shih 		msg = "lna_gain_g";
3749e3ec7017SPing-Ke Shih 		break;
3750e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_G:
3751e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_g;
3752e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_G;
3753e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_g;
3754e3ec7017SPing-Ke Shih 		msg = "tia_gain_g";
3755e3ec7017SPing-Ke Shih 		break;
3756e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_A:
3757e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_a;
3758e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3759e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_a;
3760e3ec7017SPing-Ke Shih 		msg = "lna_gain_a";
3761e3ec7017SPing-Ke Shih 		break;
3762e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_A:
3763e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_a;
3764e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_A;
3765e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_a;
3766e3ec7017SPing-Ke Shih 		msg = "tia_gain_a";
3767e3ec7017SPing-Ke Shih 		break;
3768e3ec7017SPing-Ke Shih 	default:
3769e3ec7017SPing-Ke Shih 		return;
3770e3ec7017SPing-Ke Shih 	}
3771e3ec7017SPing-Ke Shih 
3772e3ec7017SPing-Ke Shih 	for (i = 0; i < cfg->size; i++) {
3773e3ec7017SPing-Ke Shih 		tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr,
3774e3ec7017SPing-Ke Shih 					    cfg->table[i].mask);
3775e3ec7017SPing-Ke Shih 		tmp >>= DIG_GAIN_SHIFT;
3776e3ec7017SPing-Ke Shih 		gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base;
3777e3ec7017SPing-Ke Shih 		gain_base += DIG_GAIN;
3778e3ec7017SPing-Ke Shih 
3779e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n",
3780e3ec7017SPing-Ke Shih 			    msg, i, gain_arr[i]);
3781e3ec7017SPing-Ke Shih 	}
3782e3ec7017SPing-Ke Shih }
3783e3ec7017SPing-Ke Shih 
3784e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev)
3785e3ec7017SPing-Ke Shih {
3786e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3787e3ec7017SPing-Ke Shih 	u32 tmp;
3788e3ec7017SPing-Ke Shih 	u8 i;
3789e3ec7017SPing-Ke Shih 
3790d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
3791d264edb1SJohnson Lin 		return;
3792d264edb1SJohnson Lin 
3793e3ec7017SPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW,
3794e3ec7017SPing-Ke Shih 				    B_PATH0_IB_PKPW_MSK);
3795e3ec7017SPing-Ke Shih 	dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT);
3796e3ec7017SPing-Ke Shih 	dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK,
3797e3ec7017SPing-Ke Shih 					    B_PATH0_IB_PBK_MSK);
3798e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n",
3799e3ec7017SPing-Ke Shih 		    dig->ib_pkpwr, dig->ib_pbk);
3800e3ec7017SPing-Ke Shih 
3801e3ec7017SPing-Ke Shih 	for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++)
3802e3ec7017SPing-Ke Shih 		rtw89_phy_dig_read_gain_table(rtwdev, i);
3803e3ec7017SPing-Ke Shih }
3804e3ec7017SPing-Ke Shih 
3805e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22;
3806e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104};
3807e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88};
3808e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16};
3809e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528};
3810e3ec7017SPing-Ke Shih 
3811e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev)
3812e3ec7017SPing-Ke Shih {
3813e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info;
3814e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3815e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3816e3ec7017SPing-Ke Shih 
3817e3ec7017SPing-Ke Shih 	if (is_linked) {
3818e3ec7017SPing-Ke Shih 		dig->igi_rssi = ch_info->rssi_min >> 1;
3819e3ec7017SPing-Ke Shih 	} else {
3820e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n");
3821e3ec7017SPing-Ke Shih 		dig->igi_rssi = rssi_nolink;
3822e3ec7017SPing-Ke Shih 	}
3823e3ec7017SPing-Ke Shih }
3824e3ec7017SPing-Ke Shih 
3825e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev)
3826e3ec7017SPing-Ke Shih {
3827e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3828cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3829e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3830e3ec7017SPing-Ke Shih 	const u16 *fa_th_src = NULL;
3831e3ec7017SPing-Ke Shih 
3832cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
3833e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
3834e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_g;
3835e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_g;
3836e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_2g : fa_th_nolink;
3837e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = false;
3838e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3839e3ec7017SPing-Ke Shih 		break;
3840e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
3841e3ec7017SPing-Ke Shih 	default:
3842e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_a;
3843e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_a;
3844e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_5g : fa_th_nolink;
3845e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = true;
3846e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3847e3ec7017SPing-Ke Shih 		break;
3848e3ec7017SPing-Ke Shih 	}
3849e3ec7017SPing-Ke Shih 	memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th));
3850e3ec7017SPing-Ke Shih 	memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th));
3851e3ec7017SPing-Ke Shih }
3852e3ec7017SPing-Ke Shih 
3853e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20;
3854e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a;
3855e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max;
3856e3ec7017SPing-Ke Shih 
3857e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev)
3858e3ec7017SPing-Ke Shih {
3859e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3860e3ec7017SPing-Ke Shih 
3861e3ec7017SPing-Ke Shih 	dig->cur_gaincode.lna_idx = LNA_IDX_MAX;
3862e3ec7017SPing-Ke Shih 	dig->cur_gaincode.tia_idx = TIA_IDX_MAX;
3863e3ec7017SPing-Ke Shih 	dig->cur_gaincode.rxb_idx = RXB_IDX_MAX;
3864e3ec7017SPing-Ke Shih 	dig->force_gaincode.lna_idx = LNA_IDX_MAX;
3865e3ec7017SPing-Ke Shih 	dig->force_gaincode.tia_idx = TIA_IDX_MAX;
3866e3ec7017SPing-Ke Shih 	dig->force_gaincode.rxb_idx = RXB_IDX_MAX;
3867e3ec7017SPing-Ke Shih 
3868e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = igi_max_performance_mode;
3869e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = dynamic_igi_min;
3870e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dynamic_pd_threshold_max;
3871e3ec7017SPing-Ke Shih 	dig->pd_low_th_ofst = pd_low_th_offset;
3872e3ec7017SPing-Ke Shih 	dig->is_linked_pre = false;
3873e3ec7017SPing-Ke Shih }
3874e3ec7017SPing-Ke Shih 
3875e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev)
3876e3ec7017SPing-Ke Shih {
3877e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_gain_para(rtwdev);
3878e3ec7017SPing-Ke Shih 	rtw89_phy_dig_reset(rtwdev);
3879e3ec7017SPing-Ke Shih }
3880e3ec7017SPing-Ke Shih 
3881e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3882e3ec7017SPing-Ke Shih {
3883e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3884e3ec7017SPing-Ke Shih 	u8 lna_idx;
3885e3ec7017SPing-Ke Shih 
3886e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3887e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX6;
3888e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[1])
3889e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX5;
3890e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[2])
3891e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX4;
3892e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[3])
3893e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX3;
3894e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[4])
3895e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX2;
3896e3ec7017SPing-Ke Shih 	else
3897e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX1;
3898e3ec7017SPing-Ke Shih 
3899e3ec7017SPing-Ke Shih 	return lna_idx;
3900e3ec7017SPing-Ke Shih }
3901e3ec7017SPing-Ke Shih 
3902e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3903e3ec7017SPing-Ke Shih {
3904e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3905e3ec7017SPing-Ke Shih 	u8 tia_idx;
3906e3ec7017SPing-Ke Shih 
3907e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3908e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX1;
3909e3ec7017SPing-Ke Shih 	else
3910e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX0;
3911e3ec7017SPing-Ke Shih 
3912e3ec7017SPing-Ke Shih 	return tia_idx;
3913e3ec7017SPing-Ke Shih }
3914e3ec7017SPing-Ke Shih 
3915e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110
3916e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10
3917e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3918e3ec7017SPing-Ke Shih 					struct rtw89_agc_gaincode_set *set)
3919e3ec7017SPing-Ke Shih {
3920e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3921e3ec7017SPing-Ke Shih 	s8 lna_gain = dig->lna_gain[set->lna_idx];
3922e3ec7017SPing-Ke Shih 	s8 tia_gain = dig->tia_gain[set->tia_idx];
3923e3ec7017SPing-Ke Shih 	s32 wb_rssi = rssi + lna_gain + tia_gain;
3924e3ec7017SPing-Ke Shih 	s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE;
3925e3ec7017SPing-Ke Shih 	u8 rxb_idx;
3926e3ec7017SPing-Ke Shih 
3927e3ec7017SPing-Ke Shih 	rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi;
3928e3ec7017SPing-Ke Shih 	rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX);
3929e3ec7017SPing-Ke Shih 
3930e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n",
3931e3ec7017SPing-Ke Shih 		    wb_rssi, rxb_idx_tmp);
3932e3ec7017SPing-Ke Shih 
3933e3ec7017SPing-Ke Shih 	return rxb_idx;
3934e3ec7017SPing-Ke Shih }
3935e3ec7017SPing-Ke Shih 
3936e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3937e3ec7017SPing-Ke Shih 					   struct rtw89_agc_gaincode_set *set)
3938e3ec7017SPing-Ke Shih {
3939e3ec7017SPing-Ke Shih 	set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi);
3940e3ec7017SPing-Ke Shih 	set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi);
3941e3ec7017SPing-Ke Shih 	set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set);
3942e3ec7017SPing-Ke Shih 
3943e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3944e3ec7017SPing-Ke Shih 		    "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n",
3945e3ec7017SPing-Ke Shih 		    rssi, set->lna_idx, set->tia_idx, set->rxb_idx);
3946e3ec7017SPing-Ke Shih }
3947e3ec7017SPing-Ke Shih 
3948e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25
3949e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2
3950e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev)
3951e3ec7017SPing-Ke Shih {
3952e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3953e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3954e3ec7017SPing-Ke Shih 	enum rtw89_dig_noisy_level noisy_lv;
3955e3ec7017SPing-Ke Shih 	u8 igi_offset = dig->fa_rssi_ofst;
3956e3ec7017SPing-Ke Shih 	u16 fa_ratio = 0;
3957e3ec7017SPing-Ke Shih 
3958e3ec7017SPing-Ke Shih 	fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil;
3959e3ec7017SPing-Ke Shih 
3960e3ec7017SPing-Ke Shih 	if (fa_ratio < dig->fa_th[0])
3961e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL0;
3962e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[1])
3963e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL1;
3964e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[2])
3965e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL2;
3966e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[3])
3967e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL3;
3968e3ec7017SPing-Ke Shih 	else
3969e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX;
3970e3ec7017SPing-Ke Shih 
3971e3ec7017SPing-Ke Shih 	if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2)
3972e3ec7017SPing-Ke Shih 		igi_offset = 0;
3973e3ec7017SPing-Ke Shih 	else
3974e3ec7017SPing-Ke Shih 		igi_offset += noisy_lv * IGI_OFFSET_MUL;
3975e3ec7017SPing-Ke Shih 
3976e3ec7017SPing-Ke Shih 	igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX);
3977e3ec7017SPing-Ke Shih 	dig->fa_rssi_ofst = igi_offset;
3978e3ec7017SPing-Ke Shih 
3979e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3980e3ec7017SPing-Ke Shih 		    "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n",
3981e3ec7017SPing-Ke Shih 		    dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]);
3982e3ec7017SPing-Ke Shih 
3983e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3984e3ec7017SPing-Ke Shih 		    "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n",
3985e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil,
3986e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil,
3987e3ec7017SPing-Ke Shih 		    noisy_lv, igi_offset);
3988e3ec7017SPing-Ke Shih }
3989e3ec7017SPing-Ke Shih 
3990e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx)
3991e3ec7017SPing-Ke Shih {
399287deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
399387deaad9SEric Huang 
399487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr,
399587deaad9SEric Huang 			       dig_regs->p0_lna_init.mask, lna_idx);
399687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr,
399787deaad9SEric Huang 			       dig_regs->p1_lna_init.mask, lna_idx);
3998e3ec7017SPing-Ke Shih }
3999e3ec7017SPing-Ke Shih 
4000e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx)
4001e3ec7017SPing-Ke Shih {
400287deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
400387deaad9SEric Huang 
400487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr,
400587deaad9SEric Huang 			       dig_regs->p0_tia_init.mask, tia_idx);
400687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr,
400787deaad9SEric Huang 			       dig_regs->p1_tia_init.mask, tia_idx);
4008e3ec7017SPing-Ke Shih }
4009e3ec7017SPing-Ke Shih 
4010e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx)
4011e3ec7017SPing-Ke Shih {
401287deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
401387deaad9SEric Huang 
401487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr,
401587deaad9SEric Huang 			       dig_regs->p0_rxb_init.mask, rxb_idx);
401687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr,
401787deaad9SEric Huang 			       dig_regs->p1_rxb_init.mask, rxb_idx);
4018e3ec7017SPing-Ke Shih }
4019e3ec7017SPing-Ke Shih 
4020e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev,
4021e3ec7017SPing-Ke Shih 				     const struct rtw89_agc_gaincode_set set)
4022e3ec7017SPing-Ke Shih {
4023e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx);
4024e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx);
4025e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx);
4026e3ec7017SPing-Ke Shih 
4027e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n",
4028e3ec7017SPing-Ke Shih 		    set.lna_idx, set.tia_idx, set.rxb_idx);
4029e3ec7017SPing-Ke Shih }
4030e3ec7017SPing-Ke Shih 
4031e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev,
4032e3ec7017SPing-Ke Shih 						   bool enable)
4033e3ec7017SPing-Ke Shih {
403487deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4035e3ec7017SPing-Ke Shih 
403687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr,
403787deaad9SEric Huang 			       dig_regs->p0_p20_pagcugc_en.mask, enable);
403887deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr,
403987deaad9SEric Huang 			       dig_regs->p0_s20_pagcugc_en.mask, enable);
404087deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr,
404187deaad9SEric Huang 			       dig_regs->p1_p20_pagcugc_en.mask, enable);
404287deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr,
404387deaad9SEric Huang 			       dig_regs->p1_s20_pagcugc_en.mask, enable);
4044e3ec7017SPing-Ke Shih 
4045e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable);
4046e3ec7017SPing-Ke Shih }
4047e3ec7017SPing-Ke Shih 
4048bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev)
4049bed4045fSJohnson Lin {
4050bed4045fSJohnson Lin 	struct rtw89_dig_info *dig = &rtwdev->dig;
4051bed4045fSJohnson Lin 
4052d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
4053d264edb1SJohnson Lin 		return;
4054d264edb1SJohnson Lin 
4055bed4045fSJohnson Lin 	if (dig->force_gaincode_idx_en) {
4056bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4057bed4045fSJohnson Lin 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4058bed4045fSJohnson Lin 			    "Force gaincode index enabled.\n");
4059bed4045fSJohnson Lin 	} else {
4060bed4045fSJohnson Lin 		rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi,
4061bed4045fSJohnson Lin 					       &dig->cur_gaincode);
4062bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode);
4063bed4045fSJohnson Lin 	}
4064bed4045fSJohnson Lin }
4065bed4045fSJohnson Lin 
4066e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi,
4067e3ec7017SPing-Ke Shih 				    bool enable)
4068e3ec7017SPing-Ke Shih {
4069cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
407087deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4071cbb145b9SZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
4072e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4073e3ec7017SPing-Ke Shih 	u8 final_rssi = 0, under_region = dig->pd_low_th_ofst;
40741c2423deSJohnson Lin 	u8 ofdm_cca_th;
40751c2423deSJohnson Lin 	s8 cck_cca_th;
40761c2423deSJohnson Lin 	u32 pd_val = 0;
4077e3ec7017SPing-Ke Shih 
4078e3ec7017SPing-Ke Shih 	under_region += PD_TH_SB_FLTR_CMP_VAL;
4079e3ec7017SPing-Ke Shih 
4080e3ec7017SPing-Ke Shih 	switch (cbw) {
4081e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
4082e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW40_CMP_VAL;
4083e3ec7017SPing-Ke Shih 		break;
4084e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
4085e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW80_CMP_VAL;
4086e3ec7017SPing-Ke Shih 		break;
40871c2423deSJohnson Lin 	case RTW89_CHANNEL_WIDTH_160:
40881c2423deSJohnson Lin 		under_region += PD_TH_BW160_CMP_VAL;
40891c2423deSJohnson Lin 		break;
4090e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
4091e3ec7017SPing-Ke Shih 		fallthrough;
4092e3ec7017SPing-Ke Shih 	default:
4093e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW20_CMP_VAL;
4094e3ec7017SPing-Ke Shih 		break;
4095e3ec7017SPing-Ke Shih 	}
4096e3ec7017SPing-Ke Shih 
4097e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dig->igi_rssi;
4098e3ec7017SPing-Ke Shih 
4099e3ec7017SPing-Ke Shih 	final_rssi = min_t(u8, rssi, dig->igi_rssi);
41001c2423deSJohnson Lin 	ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region,
4101e3ec7017SPing-Ke Shih 			      PD_TH_MAX_RSSI + under_region);
4102e3ec7017SPing-Ke Shih 
4103e3ec7017SPing-Ke Shih 	if (enable) {
41041c2423deSJohnson Lin 		pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1;
4105e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
41061c2423deSJohnson Lin 			    "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n",
41071c2423deSJohnson Lin 			    final_rssi, ofdm_cca_th, under_region, pd_val);
4108e3ec7017SPing-Ke Shih 	} else {
4109e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4110c51ed740SColin Ian King 			    "Dynamic PD th disabled, Set PD_low_bd=0\n");
4111e3ec7017SPing-Ke Shih 	}
4112e3ec7017SPing-Ke Shih 
411387deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
411487deaad9SEric Huang 			       dig_regs->pd_lower_bound_mask, pd_val);
411587deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
411687deaad9SEric Huang 			       dig_regs->pd_spatial_reuse_en, enable);
41171c2423deSJohnson Lin 
41181c2423deSJohnson Lin 	if (!rtwdev->hal.support_cckpd)
41191c2423deSJohnson Lin 		return;
41201c2423deSJohnson Lin 
41211c2423deSJohnson Lin 	cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI);
41221c2423deSJohnson Lin 	pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX);
41231c2423deSJohnson Lin 
41241c2423deSJohnson Lin 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
41251c2423deSJohnson Lin 		    "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n",
41261c2423deSJohnson Lin 		    final_rssi, cck_cca_th, under_region, pd_val);
41271c2423deSJohnson Lin 
41281c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1,
41291c2423deSJohnson Lin 			       B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable);
41301c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1,
41311c2423deSJohnson Lin 			       B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val);
4132e3ec7017SPing-Ke Shih }
4133e3ec7017SPing-Ke Shih 
4134e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev)
4135e3ec7017SPing-Ke Shih {
4136e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4137e3ec7017SPing-Ke Shih 
4138e3ec7017SPing-Ke Shih 	dig->bypass_dig = false;
4139e3ec7017SPing-Ke Shih 	rtw89_phy_dig_para_reset(rtwdev);
4140e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4141e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false);
4142e3ec7017SPing-Ke Shih 	rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4143e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_para(rtwdev);
4144e3ec7017SPing-Ke Shih }
4145e3ec7017SPing-Ke Shih 
4146e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10
4147e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev)
4148e3ec7017SPing-Ke Shih {
4149e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4150e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
4151e3ec7017SPing-Ke Shih 
4152e3ec7017SPing-Ke Shih 	if (unlikely(dig->bypass_dig)) {
4153e3ec7017SPing-Ke Shih 		dig->bypass_dig = false;
4154e3ec7017SPing-Ke Shih 		return;
4155e3ec7017SPing-Ke Shih 	}
4156e3ec7017SPing-Ke Shih 
4157e3ec7017SPing-Ke Shih 	if (!dig->is_linked_pre && is_linked) {
4158e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n");
4159e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4160e3ec7017SPing-Ke Shih 	} else if (dig->is_linked_pre && !is_linked) {
4161e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n");
4162e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4163e3ec7017SPing-Ke Shih 	}
4164e3ec7017SPing-Ke Shih 	dig->is_linked_pre = is_linked;
4165e3ec7017SPing-Ke Shih 
4166e3ec7017SPing-Ke Shih 	rtw89_phy_dig_igi_offset_by_env(rtwdev);
4167e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_rssi_info(rtwdev);
4168e3ec7017SPing-Ke Shih 
4169e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ?
4170e3ec7017SPing-Ke Shih 			    dig->igi_rssi - IGI_RSSI_MIN : 0;
4171e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX;
4172e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst;
4173e3ec7017SPing-Ke Shih 
4174e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min,
4175e3ec7017SPing-Ke Shih 				 dig->dyn_igi_max);
4176e3ec7017SPing-Ke Shih 
4177e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4178e3ec7017SPing-Ke Shih 		    "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n",
4179e3ec7017SPing-Ke Shih 		    dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min,
4180e3ec7017SPing-Ke Shih 		    dig->igi_fa_rssi);
4181e3ec7017SPing-Ke Shih 
4182bed4045fSJohnson Lin 	rtw89_phy_dig_config_igi(rtwdev);
4183e3ec7017SPing-Ke Shih 
4184e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en);
4185e3ec7017SPing-Ke Shih 
4186e3ec7017SPing-Ke Shih 	if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max)
4187e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true);
4188e3ec7017SPing-Ke Shih 	else
4189e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4190e3ec7017SPing-Ke Shih }
4191e3ec7017SPing-Ke Shih 
41927dbdf655SPing-Ke Shih static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta)
41937dbdf655SPing-Ke Shih {
41947dbdf655SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
41957dbdf655SPing-Ke Shih 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
41967dbdf655SPing-Ke Shih 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
41977dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
41987dbdf655SPing-Ke Shih 	bool *done = data;
41997dbdf655SPing-Ke Shih 	u8 rssi_a, rssi_b;
42007dbdf655SPing-Ke Shih 	u32 candidate;
42017dbdf655SPing-Ke Shih 
42027dbdf655SPing-Ke Shih 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls)
42037dbdf655SPing-Ke Shih 		return;
42047dbdf655SPing-Ke Shih 
42057dbdf655SPing-Ke Shih 	if (*done)
42067dbdf655SPing-Ke Shih 		return;
42077dbdf655SPing-Ke Shih 
42087dbdf655SPing-Ke Shih 	*done = true;
42097dbdf655SPing-Ke Shih 
42107dbdf655SPing-Ke Shih 	rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]);
42117dbdf655SPing-Ke Shih 	rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]);
42127dbdf655SPing-Ke Shih 
42137dbdf655SPing-Ke Shih 	if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH)
42147dbdf655SPing-Ke Shih 		candidate = RF_A;
42157dbdf655SPing-Ke Shih 	else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH)
42167dbdf655SPing-Ke Shih 		candidate = RF_B;
42177dbdf655SPing-Ke Shih 	else
42187dbdf655SPing-Ke Shih 		return;
42197dbdf655SPing-Ke Shih 
42207dbdf655SPing-Ke Shih 	if (hal->antenna_tx == candidate)
42217dbdf655SPing-Ke Shih 		return;
42227dbdf655SPing-Ke Shih 
42237dbdf655SPing-Ke Shih 	hal->antenna_tx = candidate;
42247dbdf655SPing-Ke Shih 	rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta);
42257dbdf655SPing-Ke Shih 
42267dbdf655SPing-Ke Shih 	if (hal->antenna_tx == RF_A) {
42277dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12);
42287dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11);
42297dbdf655SPing-Ke Shih 	} else if (hal->antenna_tx == RF_B) {
42307dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11);
42317dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12);
42327dbdf655SPing-Ke Shih 	}
42337dbdf655SPing-Ke Shih }
42347dbdf655SPing-Ke Shih 
42357dbdf655SPing-Ke Shih void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev)
42367dbdf655SPing-Ke Shih {
42377dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
42387dbdf655SPing-Ke Shih 	bool done = false;
42397dbdf655SPing-Ke Shih 
42407dbdf655SPing-Ke Shih 	if (!hal->tx_path_diversity)
42417dbdf655SPing-Ke Shih 		return;
42427dbdf655SPing-Ke Shih 
42437dbdf655SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
42447dbdf655SPing-Ke Shih 					  rtw89_phy_tx_path_div_sta_iter,
42457dbdf655SPing-Ke Shih 					  &done);
42467dbdf655SPing-Ke Shih }
42477dbdf655SPing-Ke Shih 
4248a90c613dSEric Huang #define ANTDIV_MAIN 0
4249a90c613dSEric Huang #define ANTDIV_AUX 1
4250a90c613dSEric Huang 
4251a90c613dSEric Huang static void rtw89_phy_antdiv_set_ant(struct rtw89_dev *rtwdev)
4252a90c613dSEric Huang {
4253a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4254a90c613dSEric Huang 	u8 default_ant, optional_ant;
4255a90c613dSEric Huang 
4256a90c613dSEric Huang 	if (!hal->ant_diversity || hal->antenna_tx == 0)
4257a90c613dSEric Huang 		return;
4258a90c613dSEric Huang 
4259a90c613dSEric Huang 	if (hal->antenna_tx == RF_B) {
4260a90c613dSEric Huang 		default_ant = ANTDIV_AUX;
4261a90c613dSEric Huang 		optional_ant = ANTDIV_MAIN;
4262a90c613dSEric Huang 	} else {
4263a90c613dSEric Huang 		default_ant = ANTDIV_MAIN;
4264a90c613dSEric Huang 		optional_ant = ANTDIV_AUX;
4265a90c613dSEric Huang 	}
4266a90c613dSEric Huang 
4267a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_CGCS_CTRL,
4268a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4269a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ORI,
4270a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4271a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ALT,
4272a90c613dSEric Huang 			      optional_ant, RTW89_PHY_0);
4273a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_TX_ORI,
4274a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4275a90c613dSEric Huang }
4276a90c613dSEric Huang 
4277e3715859SEric Huang static void rtw89_phy_swap_hal_antenna(struct rtw89_dev *rtwdev)
4278e3715859SEric Huang {
4279e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4280e3715859SEric Huang 
4281e3715859SEric Huang 	hal->antenna_rx = hal->antenna_rx == RF_A ? RF_B : RF_A;
4282e3715859SEric Huang 	hal->antenna_tx = hal->antenna_rx;
4283e3715859SEric Huang }
4284e3715859SEric Huang 
4285e3715859SEric Huang static void rtw89_phy_antdiv_decision_state(struct rtw89_dev *rtwdev)
4286e3715859SEric Huang {
4287e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4288e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4289e3715859SEric Huang 	bool no_change = false;
4290e3715859SEric Huang 	u8 main_rssi, aux_rssi;
42915feecb40SEric Huang 	u8 main_evm, aux_evm;
4292e3715859SEric Huang 	u32 candidate;
4293e3715859SEric Huang 
4294e3715859SEric Huang 	antdiv->get_stats = false;
4295e3715859SEric Huang 	antdiv->training_count = 0;
4296e3715859SEric Huang 
4297e3715859SEric Huang 	main_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->main_stats);
42985feecb40SEric Huang 	main_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->main_stats);
4299e3715859SEric Huang 	aux_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->aux_stats);
43005feecb40SEric Huang 	aux_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->aux_stats);
4301e3715859SEric Huang 
43025feecb40SEric Huang 	if (main_evm > aux_evm + ANTDIV_EVM_DIFF_TH)
43035feecb40SEric Huang 		candidate = RF_A;
43045feecb40SEric Huang 	else if (aux_evm > main_evm + ANTDIV_EVM_DIFF_TH)
43055feecb40SEric Huang 		candidate = RF_B;
43065feecb40SEric Huang 	else if (main_rssi > aux_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4307e3715859SEric Huang 		candidate = RF_A;
4308e3715859SEric Huang 	else if (aux_rssi > main_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4309e3715859SEric Huang 		candidate = RF_B;
4310e3715859SEric Huang 	else
4311e3715859SEric Huang 		no_change = true;
4312e3715859SEric Huang 
4313e3715859SEric Huang 	if (no_change) {
4314e3715859SEric Huang 		/* swap back from training antenna to original */
4315e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4316e3715859SEric Huang 		return;
4317e3715859SEric Huang 	}
4318e3715859SEric Huang 
4319e3715859SEric Huang 	hal->antenna_tx = candidate;
4320e3715859SEric Huang 	hal->antenna_rx = candidate;
4321e3715859SEric Huang }
4322e3715859SEric Huang 
4323e3715859SEric Huang static void rtw89_phy_antdiv_training_state(struct rtw89_dev *rtwdev)
4324e3715859SEric Huang {
4325e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4326e3715859SEric Huang 	u64 state_period;
4327e3715859SEric Huang 
4328e3715859SEric Huang 	if (antdiv->training_count % 2 == 0) {
4329e3715859SEric Huang 		if (antdiv->training_count == 0)
4330e3715859SEric Huang 			rtw89_phy_antdiv_sts_reset(rtwdev);
4331e3715859SEric Huang 
4332e3715859SEric Huang 		antdiv->get_stats = true;
4333e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_TRAINNING_INTVL);
4334e3715859SEric Huang 	} else {
4335e3715859SEric Huang 		antdiv->get_stats = false;
4336e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_DELAY);
4337e3715859SEric Huang 
4338e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4339e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4340e3715859SEric Huang 	}
4341e3715859SEric Huang 
4342e3715859SEric Huang 	antdiv->training_count++;
4343e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work,
4344e3715859SEric Huang 				     state_period);
4345e3715859SEric Huang }
4346e3715859SEric Huang 
4347e3715859SEric Huang void rtw89_phy_antdiv_work(struct work_struct *work)
4348e3715859SEric Huang {
4349e3715859SEric Huang 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
4350e3715859SEric Huang 						antdiv_work.work);
4351e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4352e3715859SEric Huang 
4353e3715859SEric Huang 	mutex_lock(&rtwdev->mutex);
4354e3715859SEric Huang 
4355e3715859SEric Huang 	if (antdiv->training_count <= ANTDIV_TRAINNING_CNT) {
4356e3715859SEric Huang 		rtw89_phy_antdiv_training_state(rtwdev);
4357e3715859SEric Huang 	} else {
4358e3715859SEric Huang 		rtw89_phy_antdiv_decision_state(rtwdev);
4359e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4360e3715859SEric Huang 	}
4361e3715859SEric Huang 
4362e3715859SEric Huang 	mutex_unlock(&rtwdev->mutex);
4363e3715859SEric Huang }
4364e3715859SEric Huang 
4365e3715859SEric Huang void rtw89_phy_antdiv_track(struct rtw89_dev *rtwdev)
4366e3715859SEric Huang {
4367e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4368e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4369e3715859SEric Huang 	u8 rssi, rssi_pre;
4370e3715859SEric Huang 
4371e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
4372e3715859SEric Huang 		return;
4373e3715859SEric Huang 
4374e3715859SEric Huang 	rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->target_stats);
4375e3715859SEric Huang 	rssi_pre = antdiv->rssi_pre;
4376e3715859SEric Huang 	antdiv->rssi_pre = rssi;
4377e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
4378e3715859SEric Huang 
4379e3715859SEric Huang 	if (abs((int)rssi - (int)rssi_pre) < ANTDIV_RSSI_DIFF_TH)
4380e3715859SEric Huang 		return;
4381e3715859SEric Huang 
4382e3715859SEric Huang 	antdiv->training_count = 0;
4383e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 0);
4384e3715859SEric Huang }
4385e3715859SEric Huang 
4386e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev)
4387e3ec7017SPing-Ke Shih {
4388e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_top_setting_init(rtwdev);
4389e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_setting_init(rtwdev);
4390e3ec7017SPing-Ke Shih }
4391e3ec7017SPing-Ke Shih 
4392e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev)
4393e3ec7017SPing-Ke Shih {
4394e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4395e3ec7017SPing-Ke Shih 
4396e3ec7017SPing-Ke Shih 	rtw89_phy_stat_init(rtwdev);
4397e3ec7017SPing-Ke Shih 
4398e3ec7017SPing-Ke Shih 	rtw89_chip_bb_sethw(rtwdev);
4399e3ec7017SPing-Ke Shih 
4400e3ec7017SPing-Ke Shih 	rtw89_phy_env_monitor_init(rtwdev);
4401eb4e52b3SPo Hao Huang 	rtw89_physts_parsing_init(rtwdev);
4402e3ec7017SPing-Ke Shih 	rtw89_phy_dig_init(rtwdev);
4403e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_init(rtwdev);
440429136c95SEric Huang 	rtw89_phy_ul_tb_info_init(rtwdev);
4405a90c613dSEric Huang 	rtw89_phy_antdiv_init(rtwdev);
4406f03bd042SPing-Ke Shih 	rtw89_chip_rfe_gpio(rtwdev);
4407a90c613dSEric Huang 	rtw89_phy_antdiv_set_ant(rtwdev);
4408e3ec7017SPing-Ke Shih 
4409e3ec7017SPing-Ke Shih 	rtw89_phy_init_rf_nctl(rtwdev);
4410e3ec7017SPing-Ke Shih 	rtw89_chip_rfk_init(rtwdev);
4411e3ec7017SPing-Ke Shih 	rtw89_load_txpwr_table(rtwdev, chip->byr_table);
4412e3ec7017SPing-Ke Shih 	rtw89_chip_set_txpwr_ctrl(rtwdev);
4413e3ec7017SPing-Ke Shih 	rtw89_chip_power_trim(rtwdev);
4414cd89a471SPing-Ke Shih 	rtw89_chip_cfg_txrx_path(rtwdev);
4415e3ec7017SPing-Ke Shih }
4416e3ec7017SPing-Ke Shih 
4417e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
4418e3ec7017SPing-Ke Shih {
4419a48f4fd0SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
4420e3ec7017SPing-Ke Shih 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
4421e3ec7017SPing-Ke Shih 	u8 bss_color;
4422e3ec7017SPing-Ke Shih 
4423f276e20bSJohannes Berg 	if (!vif->bss_conf.he_support || !vif->cfg.assoc)
4424e3ec7017SPing-Ke Shih 		return;
4425e3ec7017SPing-Ke Shih 
4426e3ec7017SPing-Ke Shih 	bss_color = vif->bss_conf.he_bss_color.color;
4427e3ec7017SPing-Ke Shih 
4428a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_VLD0, 0x1,
4429e3ec7017SPing-Ke Shih 			      phy_idx);
4430a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT,
4431a48f4fd0SEric Huang 			      bss_color, phy_idx);
4432a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID,
4433f276e20bSJohannes Berg 			      vif->cfg.aid, phy_idx);
4434e3ec7017SPing-Ke Shih }
4435db7fa61aSZong-Zhe Yang 
4436db7fa61aSZong-Zhe Yang static void
4437db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4438db7fa61aSZong-Zhe Yang {
4439db7fa61aSZong-Zhe Yang 	rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data);
4440db7fa61aSZong-Zhe Yang }
4441db7fa61aSZong-Zhe Yang 
4442db7fa61aSZong-Zhe Yang static void
4443db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4444db7fa61aSZong-Zhe Yang {
4445db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
4446db7fa61aSZong-Zhe Yang }
4447db7fa61aSZong-Zhe Yang 
4448db7fa61aSZong-Zhe Yang static void
4449db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4450db7fa61aSZong-Zhe Yang {
4451db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_set(rtwdev, def->addr, def->mask);
4452db7fa61aSZong-Zhe Yang }
4453db7fa61aSZong-Zhe Yang 
4454db7fa61aSZong-Zhe Yang static void
4455db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4456db7fa61aSZong-Zhe Yang {
4457db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_clr(rtwdev, def->addr, def->mask);
4458db7fa61aSZong-Zhe Yang }
4459db7fa61aSZong-Zhe Yang 
4460db7fa61aSZong-Zhe Yang static void
4461db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4462db7fa61aSZong-Zhe Yang {
4463db7fa61aSZong-Zhe Yang 	udelay(def->data);
4464db7fa61aSZong-Zhe Yang }
4465db7fa61aSZong-Zhe Yang 
4466db7fa61aSZong-Zhe Yang static void
4467db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = {
4468db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WRF] = _rfk_write_rf,
4469db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WM] = _rfk_write32_mask,
4470db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WS] = _rfk_write32_set,
4471db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WC] = _rfk_write32_clr,
4472db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_DELAY] = _rfk_delay,
4473db7fa61aSZong-Zhe Yang };
4474db7fa61aSZong-Zhe Yang 
4475db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM);
4476db7fa61aSZong-Zhe Yang 
4477db7fa61aSZong-Zhe Yang void
4478db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl)
4479db7fa61aSZong-Zhe Yang {
4480db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *p = tbl->defs;
4481db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *end = tbl->defs + tbl->size;
4482db7fa61aSZong-Zhe Yang 
4483db7fa61aSZong-Zhe Yang 	for (; p < end; p++)
4484db7fa61aSZong-Zhe Yang 		_rfk_handler[p->flag](rtwdev, p);
4485db7fa61aSZong-Zhe Yang }
4486db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser);
4487c7845551SPing-Ke Shih 
4488c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4
4489c7845551SPing-Ke Shih 
4490c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = {
4491c7845551SPing-Ke Shih 	{0xD934, 0xff0000},
4492c7845551SPing-Ke Shih 	{0xD934, 0xff000000},
4493c7845551SPing-Ke Shih 	{0xD938, 0xff},
4494c7845551SPing-Ke Shih 	{0xD934, 0xff00},
4495c7845551SPing-Ke Shih };
4496c7845551SPing-Ke Shih 
4497c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = {
4498c7845551SPing-Ke Shih 	{0xD930, 0xff0000},
4499c7845551SPing-Ke Shih 	{0xD930, 0xff000000},
4500c7845551SPing-Ke Shih 	{0xD934, 0xff},
4501c7845551SPing-Ke Shih 	{0xD930, 0xff00},
4502c7845551SPing-Ke Shih };
4503c7845551SPing-Ke Shih 
4504c7845551SPing-Ke Shih static
4505c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev,
4506c7845551SPing-Ke Shih 					   enum rtw89_mac_idx mac_idx,
4507c7845551SPing-Ke Shih 					   enum rtw89_tssi_bandedge_cfg bandedge_cfg,
4508c7845551SPing-Ke Shih 					   u32 val)
4509c7845551SPing-Ke Shih {
4510c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4511c7845551SPing-Ke Shih 	u32 reg;
4512c7845551SPing-Ke Shih 	int i;
4513c7845551SPing-Ke Shih 
4514c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4515c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_flat;
4516c7845551SPing-Ke Shih 	else
4517c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_level;
4518c7845551SPing-Ke Shih 
4519c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) {
4520c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4521c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, val);
4522c7845551SPing-Ke Shih 	}
4523c7845551SPing-Ke Shih }
4524c7845551SPing-Ke Shih 
4525c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = {
4526c7845551SPing-Ke Shih 	{0xD91C, 0xff000000},
4527c7845551SPing-Ke Shih 	{0xD920, 0xff},
4528c7845551SPing-Ke Shih 	{0xD920, 0xff00},
4529c7845551SPing-Ke Shih 	{0xD920, 0xff0000},
4530c7845551SPing-Ke Shih 	{0xD920, 0xff000000},
4531c7845551SPing-Ke Shih 	{0xD924, 0xff},
4532c7845551SPing-Ke Shih 	{0xD924, 0xff00},
4533c7845551SPing-Ke Shih 	{0xD914, 0xff000000},
4534c7845551SPing-Ke Shih 	{0xD918, 0xff},
4535c7845551SPing-Ke Shih 	{0xD918, 0xff00},
4536c7845551SPing-Ke Shih 	{0xD918, 0xff0000},
4537c7845551SPing-Ke Shih 	{0xD918, 0xff000000},
4538c7845551SPing-Ke Shih 	{0xD91C, 0xff},
4539c7845551SPing-Ke Shih 	{0xD91C, 0xff00},
4540c7845551SPing-Ke Shih 	{0xD91C, 0xff0000},
4541c7845551SPing-Ke Shih };
4542c7845551SPing-Ke Shih 
4543c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = {
4544c7845551SPing-Ke Shih 	{0xD910, 0xff},
4545c7845551SPing-Ke Shih 	{0xD910, 0xff00},
4546c7845551SPing-Ke Shih 	{0xD910, 0xff0000},
4547c7845551SPing-Ke Shih 	{0xD910, 0xff000000},
4548c7845551SPing-Ke Shih 	{0xD914, 0xff},
4549c7845551SPing-Ke Shih 	{0xD914, 0xff00},
4550c7845551SPing-Ke Shih 	{0xD914, 0xff0000},
4551c7845551SPing-Ke Shih 	{0xD908, 0xff},
4552c7845551SPing-Ke Shih 	{0xD908, 0xff00},
4553c7845551SPing-Ke Shih 	{0xD908, 0xff0000},
4554c7845551SPing-Ke Shih 	{0xD908, 0xff000000},
4555c7845551SPing-Ke Shih 	{0xD90C, 0xff},
4556c7845551SPing-Ke Shih 	{0xD90C, 0xff00},
4557c7845551SPing-Ke Shih 	{0xD90C, 0xff0000},
4558c7845551SPing-Ke Shih 	{0xD90C, 0xff000000},
4559c7845551SPing-Ke Shih };
4560c7845551SPing-Ke Shih 
4561c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev,
4562c7845551SPing-Ke Shih 					  enum rtw89_mac_idx mac_idx,
4563c7845551SPing-Ke Shih 					  enum rtw89_tssi_bandedge_cfg bandedge_cfg)
4564c7845551SPing-Ke Shih {
4565c7845551SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4566c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4567c7845551SPing-Ke Shih 	const u32 *data;
4568c7845551SPing-Ke Shih 	u32 reg;
4569c7845551SPing-Ke Shih 	int i;
4570c7845551SPing-Ke Shih 
4571c7845551SPing-Ke Shih 	if (bandedge_cfg >= RTW89_TSSI_CFG_NUM)
4572c7845551SPing-Ke Shih 		return;
4573c7845551SPing-Ke Shih 
4574c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4575c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_flat;
4576c7845551SPing-Ke Shih 	else
4577c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_level;
4578c7845551SPing-Ke Shih 
4579c7845551SPing-Ke Shih 	data = chip->tssi_dbw_table->data[bandedge_cfg];
4580c7845551SPing-Ke Shih 
4581c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) {
4582c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4583c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]);
4584c7845551SPing-Ke Shih 	}
4585c7845551SPing-Ke Shih 
4586c7845551SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx);
4587c7845551SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg);
4588c7845551SPing-Ke Shih 
4589c7845551SPing-Ke Shih 	rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg,
4590c7845551SPing-Ke Shih 					      data[RTW89_TSSI_SBW20]);
4591c7845551SPing-Ke Shih }
4592c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg);
4593bb9040b3SPo-Hao Huang 
4594bb9040b3SPo-Hao Huang static
4595bb9040b3SPo-Hao Huang const u8 rtw89_ch_base_table[16] = {1, 0xff,
4596bb9040b3SPo-Hao Huang 				    36, 100, 132, 149, 0xff,
4597bb9040b3SPo-Hao Huang 				    1, 33, 65, 97, 129, 161, 193, 225, 0xff};
4598bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_2G		0
4599bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_FIRST	2
4600bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_LAST	5
4601bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_FIRST	7
4602bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_LAST	14
4603bb9040b3SPo-Hao Huang 
4604bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_MASK		GENMASK(7, 4)
4605bb9040b3SPo-Hao Huang #define RTW89_CH_OFFSET_MASK		GENMASK(3, 0)
4606bb9040b3SPo-Hao Huang 
4607bb9040b3SPo-Hao Huang u8 rtw89_encode_chan_idx(struct rtw89_dev *rtwdev, u8 central_ch, u8 band)
4608bb9040b3SPo-Hao Huang {
4609bb9040b3SPo-Hao Huang 	u8 chan_idx;
4610bb9040b3SPo-Hao Huang 	u8 last, first;
4611bb9040b3SPo-Hao Huang 	u8 idx;
4612bb9040b3SPo-Hao Huang 
4613bb9040b3SPo-Hao Huang 	switch (band) {
4614bb9040b3SPo-Hao Huang 	case RTW89_BAND_2G:
4615bb9040b3SPo-Hao Huang 		chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, RTW89_CH_BASE_IDX_2G) |
4616bb9040b3SPo-Hao Huang 			   FIELD_PREP(RTW89_CH_OFFSET_MASK, central_ch);
4617bb9040b3SPo-Hao Huang 		return chan_idx;
4618bb9040b3SPo-Hao Huang 	case RTW89_BAND_5G:
4619bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_5G_FIRST;
4620bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_5G_LAST;
4621bb9040b3SPo-Hao Huang 		break;
4622bb9040b3SPo-Hao Huang 	case RTW89_BAND_6G:
4623bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_6G_FIRST;
4624bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_6G_LAST;
4625bb9040b3SPo-Hao Huang 		break;
4626bb9040b3SPo-Hao Huang 	default:
4627bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unsupported band %d\n", band);
4628bb9040b3SPo-Hao Huang 		return 0;
4629bb9040b3SPo-Hao Huang 	}
4630bb9040b3SPo-Hao Huang 
4631bb9040b3SPo-Hao Huang 	for (idx = last; idx >= first; idx--)
4632bb9040b3SPo-Hao Huang 		if (central_ch >= rtw89_ch_base_table[idx])
4633bb9040b3SPo-Hao Huang 			break;
4634bb9040b3SPo-Hao Huang 
4635bb9040b3SPo-Hao Huang 	if (idx < first) {
4636bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unknown band %d channel %d\n", band, central_ch);
4637bb9040b3SPo-Hao Huang 		return 0;
4638bb9040b3SPo-Hao Huang 	}
4639bb9040b3SPo-Hao Huang 
4640bb9040b3SPo-Hao Huang 	chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, idx) |
4641bb9040b3SPo-Hao Huang 		   FIELD_PREP(RTW89_CH_OFFSET_MASK,
4642bb9040b3SPo-Hao Huang 			      (central_ch - rtw89_ch_base_table[idx]) >> 1);
4643bb9040b3SPo-Hao Huang 	return chan_idx;
4644bb9040b3SPo-Hao Huang }
4645bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_encode_chan_idx);
4646bb9040b3SPo-Hao Huang 
4647bb9040b3SPo-Hao Huang void rtw89_decode_chan_idx(struct rtw89_dev *rtwdev, u8 chan_idx,
4648bb9040b3SPo-Hao Huang 			   u8 *ch, enum nl80211_band *band)
4649bb9040b3SPo-Hao Huang {
4650bb9040b3SPo-Hao Huang 	u8 idx, offset;
4651bb9040b3SPo-Hao Huang 
4652bb9040b3SPo-Hao Huang 	idx = FIELD_GET(RTW89_CH_BASE_IDX_MASK, chan_idx);
4653bb9040b3SPo-Hao Huang 	offset = FIELD_GET(RTW89_CH_OFFSET_MASK, chan_idx);
4654bb9040b3SPo-Hao Huang 
4655bb9040b3SPo-Hao Huang 	if (idx == RTW89_CH_BASE_IDX_2G) {
4656bb9040b3SPo-Hao Huang 		*band = NL80211_BAND_2GHZ;
4657bb9040b3SPo-Hao Huang 		*ch = offset;
4658bb9040b3SPo-Hao Huang 		return;
4659bb9040b3SPo-Hao Huang 	}
4660bb9040b3SPo-Hao Huang 
4661bb9040b3SPo-Hao Huang 	*band = idx <= RTW89_CH_BASE_IDX_5G_LAST ? NL80211_BAND_5GHZ : NL80211_BAND_6GHZ;
4662bb9040b3SPo-Hao Huang 	*ch = rtw89_ch_base_table[idx] + (offset << 1);
4663bb9040b3SPo-Hao Huang }
4664bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_decode_chan_idx);
4665280c4447SChih-Kang Chang 
4666280c4447SChih-Kang Chang #define EDCCA_DEFAULT 249
4667280c4447SChih-Kang Chang void rtw89_phy_config_edcca(struct rtw89_dev *rtwdev, bool scan)
4668280c4447SChih-Kang Chang {
4669280c4447SChih-Kang Chang 	u32 reg = rtwdev->chip->edcca_lvl_reg;
4670280c4447SChih-Kang Chang 	struct rtw89_hal *hal = &rtwdev->hal;
4671280c4447SChih-Kang Chang 	u32 val;
4672280c4447SChih-Kang Chang 
4673280c4447SChih-Kang Chang 	if (scan) {
4674280c4447SChih-Kang Chang 		hal->edcca_bak = rtw89_phy_read32(rtwdev, reg);
4675280c4447SChih-Kang Chang 		val = hal->edcca_bak;
4676280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_A_MSK);
4677280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_P_MSK);
4678280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_PPDU_LVL_MSK);
4679280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, val);
4680280c4447SChih-Kang Chang 	} else {
4681280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, hal->edcca_bak);
4682280c4447SChih-Kang Chang 	}
4683280c4447SChih-Kang Chang }
4684