xref: /openbmc/linux/drivers/net/wireless/realtek/rtw89/phy.c (revision bfbadacf37a204afbfb829d508412d501a22d62c)
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 
4472ef14155SZong-Zhe Yang #define RTW89_HW_RATE_BY_CHIP_GEN(rate) \
4482ef14155SZong-Zhe Yang 	{ \
4492ef14155SZong-Zhe Yang 		[RTW89_CHIP_AX] = RTW89_HW_RATE_ ## rate, \
4502ef14155SZong-Zhe Yang 		[RTW89_CHIP_BE] = RTW89_HW_RATE_V1_ ## rate, \
4512ef14155SZong-Zhe Yang 	}
4522ef14155SZong-Zhe Yang 
453e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev,
454e3ec7017SPing-Ke Shih 				struct ieee80211_vif *vif,
455e3ec7017SPing-Ke Shih 				const struct cfg80211_bitrate_mask *mask)
456e3ec7017SPing-Ke Shih {
457e3ec7017SPing-Ke Shih 	struct ieee80211_supported_band *sband;
458e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
459e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern next_pattern = {0};
460cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
4612ef14155SZong-Zhe Yang 	static const u16 hw_rate_he[][RTW89_CHIP_GEN_NUM] = {
4622ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS1_MCS0),
4632ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS2_MCS0),
4642ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS3_MCS0),
4652ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS4_MCS0),
4662ef14155SZong-Zhe Yang 	};
4672ef14155SZong-Zhe Yang 	static const u16 hw_rate_vht[][RTW89_CHIP_GEN_NUM] = {
4682ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS1_MCS0),
4692ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS2_MCS0),
4702ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS3_MCS0),
4712ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS4_MCS0),
4722ef14155SZong-Zhe Yang 	};
4732ef14155SZong-Zhe Yang 	static const u16 hw_rate_ht[][RTW89_CHIP_GEN_NUM] = {
4742ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS0),
4752ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS8),
4762ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS16),
4772ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS24),
4782ef14155SZong-Zhe Yang 	};
479cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
480a06d2dd7SZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
4812ef14155SZong-Zhe Yang 	enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
482e3ec7017SPing-Ke Shih 	u8 tx_nss = rtwdev->hal.tx_nss;
483e3ec7017SPing-Ke Shih 	u8 i;
484e3ec7017SPing-Ke Shih 
485e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
4862ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_he[i][chip_gen],
487e3ec7017SPing-Ke Shih 					  RA_MASK_HE_RATES, RTW89_RA_MODE_HE,
488a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].he_mcs[i],
489e3ec7017SPing-Ke Shih 					  0, true))
490e3ec7017SPing-Ke Shih 			goto out;
491e3ec7017SPing-Ke Shih 
492e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
4932ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i][chip_gen],
494e3ec7017SPing-Ke Shih 					  RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT,
495a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].vht_mcs[i],
496e3ec7017SPing-Ke Shih 					  0, true))
497e3ec7017SPing-Ke Shih 			goto out;
498e3ec7017SPing-Ke Shih 
499e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
5002ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i][chip_gen],
501e3ec7017SPing-Ke Shih 					  RA_MASK_HT_RATES, RTW89_RA_MODE_HT,
502a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].ht_mcs[i],
503e3ec7017SPing-Ke Shih 					  0, true))
504e3ec7017SPing-Ke Shih 			goto out;
505e3ec7017SPing-Ke Shih 
506e3ec7017SPing-Ke Shih 	/* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and
507e3ec7017SPing-Ke Shih 	 * require at least one basic rate for ieee80211_set_bitrate_mask,
508e3ec7017SPing-Ke Shih 	 * so the decision just depends on if all bitrates are set or not.
509e3ec7017SPing-Ke Shih 	 */
510a06d2dd7SZong-Zhe Yang 	sband = rtwdev->hw->wiphy->bands[nl_band];
511e3ec7017SPing-Ke Shih 	if (band == RTW89_BAND_2G) {
512e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1,
513e3ec7017SPing-Ke Shih 					  RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES,
514e3ec7017SPing-Ke Shih 					  RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM,
515a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
516e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
517e3ec7017SPing-Ke Shih 			goto out;
518e3ec7017SPing-Ke Shih 	} else {
519e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6,
520e3ec7017SPing-Ke Shih 					  RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM,
521a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
522e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
523e3ec7017SPing-Ke Shih 			goto out;
524e3ec7017SPing-Ke Shih 	}
525e3ec7017SPing-Ke Shih 
526e3ec7017SPing-Ke Shih 	if (!next_pattern.enable)
527e3ec7017SPing-Ke Shih 		goto out;
528e3ec7017SPing-Ke Shih 
529e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern = next_pattern;
530e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
531e3ec7017SPing-Ke Shih 		    "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n",
532e3ec7017SPing-Ke Shih 		    next_pattern.rate,
533e3ec7017SPing-Ke Shih 		    next_pattern.ra_mask,
534e3ec7017SPing-Ke Shih 		    next_pattern.ra_mode);
535e3ec7017SPing-Ke Shih 	return;
536e3ec7017SPing-Ke Shih 
537e3ec7017SPing-Ke Shih out:
538e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern.enable = false;
539e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n");
540e3ec7017SPing-Ke Shih }
541e3ec7017SPing-Ke Shih 
542e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta)
543e3ec7017SPing-Ke Shih {
544e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
545e3ec7017SPing-Ke Shih 
5469d9a9edcSPing-Ke Shih 	rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
547e3ec7017SPing-Ke Shih }
548e3ec7017SPing-Ke Shih 
549e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev)
550e3ec7017SPing-Ke Shih {
551e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
552e3ec7017SPing-Ke Shih 					  rtw89_phy_ra_updata_sta_iter,
553e3ec7017SPing-Ke Shih 					  rtwdev);
554e3ec7017SPing-Ke Shih }
555e3ec7017SPing-Ke Shih 
556e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta)
557e3ec7017SPing-Ke Shih {
558e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
559e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
560e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR;
561e3ec7017SPing-Ke Shih 	bool csi = rtw89_sta_has_beamformer_cap(sta);
562e3ec7017SPing-Ke Shih 
563e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, csi);
564e3ec7017SPing-Ke Shih 
565e3ec7017SPing-Ke Shih 	if (rssi > 40)
566e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 1;
567e3ec7017SPing-Ke Shih 	else if (rssi > 20)
568e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 2;
569e3ec7017SPing-Ke Shih 	else if (rssi > 1)
570e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 3;
571e3ec7017SPing-Ke Shih 	else
572e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 0;
573e3ec7017SPing-Ke Shih 	ra->upd_all = 1;
574e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
575e3ec7017SPing-Ke Shih 		    "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d",
576e3ec7017SPing-Ke Shih 		    ra->macid,
577e3ec7017SPing-Ke Shih 		    ra->mode_ctrl,
578e3ec7017SPing-Ke Shih 		    ra->bw_cap,
579e3ec7017SPing-Ke Shih 		    ra->ss_num,
580e3ec7017SPing-Ke Shih 		    ra->init_rate_lv);
581e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
582e3ec7017SPing-Ke Shih 		    "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d",
583e3ec7017SPing-Ke Shih 		    ra->dcm_cap,
584e3ec7017SPing-Ke Shih 		    ra->er_cap,
585e3ec7017SPing-Ke Shih 		    ra->ldpc_cap,
586e3ec7017SPing-Ke Shih 		    ra->stbc_cap,
587e3ec7017SPing-Ke Shih 		    ra->en_sgi,
588e3ec7017SPing-Ke Shih 		    ra->giltf);
589e3ec7017SPing-Ke Shih 
590e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, csi);
591e3ec7017SPing-Ke Shih }
592e3ec7017SPing-Ke Shih 
593e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev,
5943e5831caSZong-Zhe Yang 		      const struct rtw89_chan *chan,
595e3ec7017SPing-Ke Shih 		      enum rtw89_bandwidth dbw)
596e3ec7017SPing-Ke Shih {
5973e5831caSZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
5983e5831caSZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
5993e5831caSZong-Zhe Yang 	u8 central_ch = chan->channel;
600e3ec7017SPing-Ke Shih 	u8 txsc_idx = 0;
601e3ec7017SPing-Ke Shih 	u8 tmp = 0;
602e3ec7017SPing-Ke Shih 
603e3ec7017SPing-Ke Shih 	if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20)
604e3ec7017SPing-Ke Shih 		return txsc_idx;
605e3ec7017SPing-Ke Shih 
606e3ec7017SPing-Ke Shih 	switch (cbw) {
607e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
608e3ec7017SPing-Ke Shih 		txsc_idx = pri_ch > central_ch ? 1 : 2;
609e3ec7017SPing-Ke Shih 		break;
610e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
611e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
612e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
613e3ec7017SPing-Ke Shih 				txsc_idx = (pri_ch - central_ch) >> 1;
614e3ec7017SPing-Ke Shih 			else
615e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 1;
616e3ec7017SPing-Ke Shih 		} else {
617e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 9 : 10;
618e3ec7017SPing-Ke Shih 		}
619e3ec7017SPing-Ke Shih 		break;
620e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
621e3ec7017SPing-Ke Shih 		if (pri_ch > central_ch)
622e3ec7017SPing-Ke Shih 			tmp = (pri_ch - central_ch) >> 1;
623e3ec7017SPing-Ke Shih 		else
624e3ec7017SPing-Ke Shih 			tmp = ((central_ch - pri_ch) >> 1) + 1;
625e3ec7017SPing-Ke Shih 
626e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
627e3ec7017SPing-Ke Shih 			txsc_idx = tmp;
628e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
629e3ec7017SPing-Ke Shih 			if (tmp == 1 || tmp == 3)
630e3ec7017SPing-Ke Shih 				txsc_idx = 9;
631e3ec7017SPing-Ke Shih 			else if (tmp == 5 || tmp == 7)
632e3ec7017SPing-Ke Shih 				txsc_idx = 11;
633e3ec7017SPing-Ke Shih 			else if (tmp == 2 || tmp == 4)
634e3ec7017SPing-Ke Shih 				txsc_idx = 10;
635e3ec7017SPing-Ke Shih 			else if (tmp == 6 || tmp == 8)
636e3ec7017SPing-Ke Shih 				txsc_idx = 12;
637e3ec7017SPing-Ke Shih 			else
638e3ec7017SPing-Ke Shih 				return 0xff;
639e3ec7017SPing-Ke Shih 		} else {
640e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 13 : 14;
641e3ec7017SPing-Ke Shih 		}
642e3ec7017SPing-Ke Shih 		break;
643e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80_80:
644e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
645e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
646e3ec7017SPing-Ke Shih 				txsc_idx = (10 - (pri_ch - central_ch)) >> 1;
647e3ec7017SPing-Ke Shih 			else
648e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 5;
649e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
650e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 10 : 12;
651e3ec7017SPing-Ke Shih 		} else {
652e3ec7017SPing-Ke Shih 			txsc_idx = 14;
653e3ec7017SPing-Ke Shih 		}
654e3ec7017SPing-Ke Shih 		break;
655e3ec7017SPing-Ke Shih 	default:
656e3ec7017SPing-Ke Shih 		break;
657e3ec7017SPing-Ke Shih 	}
658e3ec7017SPing-Ke Shih 
659e3ec7017SPing-Ke Shih 	return txsc_idx;
660e3ec7017SPing-Ke Shih }
661861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc);
662e3ec7017SPing-Ke Shih 
66384d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev)
66484d0e33eSChung-Hsuan Hung {
66584d0e33eSChung-Hsuan Hung 	return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) ||
66684d0e33eSChung-Hsuan Hung 	       !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1);
66784d0e33eSChung-Hsuan Hung }
66884d0e33eSChung-Hsuan Hung 
669e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
670e3ec7017SPing-Ke Shih 		      u32 addr, u32 mask)
671e3ec7017SPing-Ke Shih {
672e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
673e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
674e3ec7017SPing-Ke Shih 	u32 val, direct_addr;
675e3ec7017SPing-Ke Shih 
676e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
677e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
678e3ec7017SPing-Ke Shih 		return INV_RF_DATA;
679e3ec7017SPing-Ke Shih 	}
680e3ec7017SPing-Ke Shih 
681e3ec7017SPing-Ke Shih 	addr &= 0xff;
682e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
683e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
684e3ec7017SPing-Ke Shih 
685e3ec7017SPing-Ke Shih 	val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask);
686e3ec7017SPing-Ke Shih 
687e3ec7017SPing-Ke Shih 	return val;
688e3ec7017SPing-Ke Shih }
689e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf);
690e3ec7017SPing-Ke Shih 
69184d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev,
69284d0e33eSChung-Hsuan Hung 			       enum rtw89_rf_path rf_path, u32 addr, u32 mask)
69384d0e33eSChung-Hsuan Hung {
69484d0e33eSChung-Hsuan Hung 	bool busy;
69584d0e33eSChung-Hsuan Hung 	bool done;
69684d0e33eSChung-Hsuan Hung 	u32 val;
69784d0e33eSChung-Hsuan Hung 	int ret;
69884d0e33eSChung-Hsuan Hung 
69984d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
70084d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
70184d0e33eSChung-Hsuan Hung 	if (ret) {
70284d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read rf busy swsi\n");
70384d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
70484d0e33eSChung-Hsuan Hung 	}
70584d0e33eSChung-Hsuan Hung 
70684d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
70784d0e33eSChung-Hsuan Hung 
70884d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) |
70984d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr);
71084d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val);
71184d0e33eSChung-Hsuan Hung 	udelay(2);
71284d0e33eSChung-Hsuan Hung 
71384d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1,
71484d0e33eSChung-Hsuan Hung 				       30, false, rtwdev, R_SWSI_V1,
71584d0e33eSChung-Hsuan Hung 				       B_SWSI_R_DATA_DONE_V1);
71684d0e33eSChung-Hsuan Hung 	if (ret) {
71784d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read swsi busy\n");
71884d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
71984d0e33eSChung-Hsuan Hung 	}
72084d0e33eSChung-Hsuan Hung 
72184d0e33eSChung-Hsuan Hung 	return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask);
72284d0e33eSChung-Hsuan Hung }
72384d0e33eSChung-Hsuan Hung 
72484d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
72584d0e33eSChung-Hsuan Hung 			 u32 addr, u32 mask)
72684d0e33eSChung-Hsuan Hung {
72784d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
72884d0e33eSChung-Hsuan Hung 
72984d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
73084d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
73184d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
73284d0e33eSChung-Hsuan Hung 	}
73384d0e33eSChung-Hsuan Hung 
73484d0e33eSChung-Hsuan Hung 	if (ad_sel)
73584d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask);
73684d0e33eSChung-Hsuan Hung 	else
73784d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask);
73884d0e33eSChung-Hsuan Hung }
73984d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1);
74084d0e33eSChung-Hsuan Hung 
741e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
742e3ec7017SPing-Ke Shih 			u32 addr, u32 mask, u32 data)
743e3ec7017SPing-Ke Shih {
744e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
745e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
746e3ec7017SPing-Ke Shih 	u32 direct_addr;
747e3ec7017SPing-Ke Shih 
748e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
749e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
750e3ec7017SPing-Ke Shih 		return false;
751e3ec7017SPing-Ke Shih 	}
752e3ec7017SPing-Ke Shih 
753e3ec7017SPing-Ke Shih 	addr &= 0xff;
754e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
755e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
756e3ec7017SPing-Ke Shih 
757e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data);
758e3ec7017SPing-Ke Shih 
759e3ec7017SPing-Ke Shih 	/* delay to ensure writing properly */
760e3ec7017SPing-Ke Shih 	udelay(1);
761e3ec7017SPing-Ke Shih 
762e3ec7017SPing-Ke Shih 	return true;
763e3ec7017SPing-Ke Shih }
764e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf);
765e3ec7017SPing-Ke Shih 
76684d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev,
76784d0e33eSChung-Hsuan Hung 				 enum rtw89_rf_path rf_path, u32 addr, u32 mask,
76884d0e33eSChung-Hsuan Hung 				 u32 data)
76984d0e33eSChung-Hsuan Hung {
77084d0e33eSChung-Hsuan Hung 	u8 bit_shift;
77184d0e33eSChung-Hsuan Hung 	u32 val;
77284d0e33eSChung-Hsuan Hung 	bool busy, b_msk_en = false;
77384d0e33eSChung-Hsuan Hung 	int ret;
77484d0e33eSChung-Hsuan Hung 
77584d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
77684d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
77784d0e33eSChung-Hsuan Hung 	if (ret) {
77884d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "write rf busy swsi\n");
77984d0e33eSChung-Hsuan Hung 		return false;
78084d0e33eSChung-Hsuan Hung 	}
78184d0e33eSChung-Hsuan Hung 
78284d0e33eSChung-Hsuan Hung 	data &= RFREG_MASK;
78384d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
78484d0e33eSChung-Hsuan Hung 
78584d0e33eSChung-Hsuan Hung 	if (mask != RFREG_MASK) {
78684d0e33eSChung-Hsuan Hung 		b_msk_en = true;
78784d0e33eSChung-Hsuan Hung 		rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK,
78884d0e33eSChung-Hsuan Hung 				       mask);
78984d0e33eSChung-Hsuan Hung 		bit_shift = __ffs(mask);
79084d0e33eSChung-Hsuan Hung 		data = (data << bit_shift) & RFREG_MASK;
79184d0e33eSChung-Hsuan Hung 	}
79284d0e33eSChung-Hsuan Hung 
79384d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) |
79484d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) |
79584d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) |
79684d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_VAL_V1, data);
79784d0e33eSChung-Hsuan Hung 
79884d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val);
79984d0e33eSChung-Hsuan Hung 
80084d0e33eSChung-Hsuan Hung 	return true;
80184d0e33eSChung-Hsuan Hung }
80284d0e33eSChung-Hsuan Hung 
80384d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
80484d0e33eSChung-Hsuan Hung 			   u32 addr, u32 mask, u32 data)
80584d0e33eSChung-Hsuan Hung {
80684d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
80784d0e33eSChung-Hsuan Hung 
80884d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
80984d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
81084d0e33eSChung-Hsuan Hung 		return false;
81184d0e33eSChung-Hsuan Hung 	}
81284d0e33eSChung-Hsuan Hung 
81384d0e33eSChung-Hsuan Hung 	if (ad_sel)
81484d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data);
81584d0e33eSChung-Hsuan Hung 	else
81684d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data);
81784d0e33eSChung-Hsuan Hung }
81884d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1);
81984d0e33eSChung-Hsuan Hung 
820d9112042SChih-Kang Chang static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev)
821d9112042SChih-Kang Chang {
822d9112042SChih-Kang Chang 	return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1;
823d9112042SChih-Kang Chang }
824d9112042SChih-Kang Chang 
825e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev,
826e3ec7017SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx)
827e3ec7017SPing-Ke Shih {
828e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
829e3ec7017SPing-Ke Shih 
830e3ec7017SPing-Ke Shih 	chip->ops->bb_reset(rtwdev, phy_idx);
831e3ec7017SPing-Ke Shih }
832e3ec7017SPing-Ke Shih 
833e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev,
834e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
835e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
836e3ec7017SPing-Ke Shih 				    void *extra_data)
837e3ec7017SPing-Ke Shih {
838e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe)
839e3ec7017SPing-Ke Shih 		mdelay(50);
840e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfd)
841e3ec7017SPing-Ke Shih 		mdelay(5);
842e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfc)
843e3ec7017SPing-Ke Shih 		mdelay(1);
844e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfb)
845e3ec7017SPing-Ke Shih 		udelay(50);
846e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfa)
847e3ec7017SPing-Ke Shih 		udelay(5);
848e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xf9)
849e3ec7017SPing-Ke Shih 		udelay(1);
850e3ec7017SPing-Ke Shih 	else
851e3ec7017SPing-Ke Shih 		rtw89_phy_write32(rtwdev, reg->addr, reg->data);
852e3ec7017SPing-Ke Shih }
853e3ec7017SPing-Ke Shih 
854e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg {
855e885871eSZong-Zhe Yang 	u32 addr;
856e885871eSZong-Zhe Yang 	struct {
857e885871eSZong-Zhe Yang 		union {
858e885871eSZong-Zhe Yang 			u8 type;
859e885871eSZong-Zhe Yang 			struct {
860e885871eSZong-Zhe Yang 				u8 rxsc_start:4;
861e885871eSZong-Zhe Yang 				u8 bw:4;
862e885871eSZong-Zhe Yang 			};
863e885871eSZong-Zhe Yang 		};
864e885871eSZong-Zhe Yang 		u8 path;
865e885871eSZong-Zhe Yang 		u8 gain_band;
866e885871eSZong-Zhe Yang 		u8 cfg_type;
867e885871eSZong-Zhe Yang 	};
868e885871eSZong-Zhe Yang } __packed;
869e885871eSZong-Zhe Yang 
870e885871eSZong-Zhe Yang static void
871e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev,
872e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
873e885871eSZong-Zhe Yang {
874e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
875e885871eSZong-Zhe Yang 	u8 type = arg.type;
876e885871eSZong-Zhe Yang 	u8 path = arg.path;
877e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
878e885871eSZong-Zhe Yang 	int i;
879e885871eSZong-Zhe Yang 
880e885871eSZong-Zhe Yang 	switch (type) {
881e885871eSZong-Zhe Yang 	case 0:
882e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
883e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
884e885871eSZong-Zhe Yang 		break;
885e885871eSZong-Zhe Yang 	case 1:
886e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
887e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
888e885871eSZong-Zhe Yang 		break;
889e885871eSZong-Zhe Yang 	case 2:
890e885871eSZong-Zhe Yang 		for (i = 0; i < 2; i++, data >>= 8)
891e885871eSZong-Zhe Yang 			gain->tia_gain[gband][path][i] = data & 0xff;
892e885871eSZong-Zhe Yang 		break;
893e885871eSZong-Zhe Yang 	default:
894e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
895e885871eSZong-Zhe Yang 			   "bb gain error {0x%x:0x%x} with unknown type: %d\n",
896e885871eSZong-Zhe Yang 			   arg.addr, data, type);
897e885871eSZong-Zhe Yang 		break;
898e885871eSZong-Zhe Yang 	}
899e885871eSZong-Zhe Yang }
900e885871eSZong-Zhe Yang 
901e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx {
902e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_FULL = 0,
903e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20 = 1,
904e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20_1 = 5,
905e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_40 = 9,
906e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_80 = 13,
907e885871eSZong-Zhe Yang };
908e885871eSZong-Zhe Yang 
909e885871eSZong-Zhe Yang static void
910e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev,
911e885871eSZong-Zhe Yang 			  union rtw89_phy_bb_gain_arg arg, u32 data)
912e885871eSZong-Zhe Yang {
913e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
914e885871eSZong-Zhe Yang 	u8 rxsc_start = arg.rxsc_start;
915e885871eSZong-Zhe Yang 	u8 bw = arg.bw;
916e885871eSZong-Zhe Yang 	u8 path = arg.path;
917e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
918e885871eSZong-Zhe Yang 	u8 rxsc;
919e885871eSZong-Zhe Yang 	s8 ofst;
920e885871eSZong-Zhe Yang 	int i;
921e885871eSZong-Zhe Yang 
922e885871eSZong-Zhe Yang 	switch (bw) {
923e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_20:
924e885871eSZong-Zhe Yang 		gain->rpl_ofst_20[gband][path] = (s8)data;
925e885871eSZong-Zhe Yang 		break;
926e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_40:
927e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
928e885871eSZong-Zhe Yang 			gain->rpl_ofst_40[gband][path][0] = (s8)data;
929e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
930e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
931e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
932e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
933e885871eSZong-Zhe Yang 				gain->rpl_ofst_40[gband][path][rxsc] = ofst;
934e885871eSZong-Zhe Yang 			}
935e885871eSZong-Zhe Yang 		}
936e885871eSZong-Zhe Yang 		break;
937e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_80:
938e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
939e885871eSZong-Zhe Yang 			gain->rpl_ofst_80[gband][path][0] = (s8)data;
940e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
941e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
942e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
943e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
944e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
945e885871eSZong-Zhe Yang 			}
946e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
947e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
948e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
949e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
950e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
951e885871eSZong-Zhe Yang 			}
952e885871eSZong-Zhe Yang 		}
953e885871eSZong-Zhe Yang 		break;
954e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
955e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
956e885871eSZong-Zhe Yang 			gain->rpl_ofst_160[gband][path][0] = (s8)data;
957e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
958e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
959e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
960e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
961e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
962e885871eSZong-Zhe Yang 			}
963e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) {
964e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
965e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i;
966e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
967e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
968e885871eSZong-Zhe Yang 			}
969e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
970e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
971e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
972e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
973e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
974e885871eSZong-Zhe Yang 			}
975e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) {
976e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
977e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_80 + i;
978e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
979e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
980e885871eSZong-Zhe Yang 			}
981e885871eSZong-Zhe Yang 		}
982e885871eSZong-Zhe Yang 		break;
983e885871eSZong-Zhe Yang 	default:
984e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
985e885871eSZong-Zhe Yang 			   "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n",
986e885871eSZong-Zhe Yang 			   arg.addr, data, bw);
987e885871eSZong-Zhe Yang 		break;
988e885871eSZong-Zhe Yang 	}
989e885871eSZong-Zhe Yang }
990e885871eSZong-Zhe Yang 
991e885871eSZong-Zhe Yang static void
992e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev,
993e885871eSZong-Zhe Yang 			     union rtw89_phy_bb_gain_arg arg, u32 data)
994e885871eSZong-Zhe Yang {
995e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
996e885871eSZong-Zhe Yang 	u8 type = arg.type;
997e885871eSZong-Zhe Yang 	u8 path = arg.path;
998e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
999e885871eSZong-Zhe Yang 	int i;
1000e885871eSZong-Zhe Yang 
1001e885871eSZong-Zhe Yang 	switch (type) {
1002e885871eSZong-Zhe Yang 	case 0:
1003e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1004e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
1005e885871eSZong-Zhe Yang 		break;
1006e885871eSZong-Zhe Yang 	case 1:
1007e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
1008e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
1009e885871eSZong-Zhe Yang 		break;
1010e885871eSZong-Zhe Yang 	default:
1011e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1012e885871eSZong-Zhe Yang 			   "bb gain bypass {0x%x:0x%x} with unknown type: %d\n",
1013e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1014e885871eSZong-Zhe Yang 		break;
1015e885871eSZong-Zhe Yang 	}
1016e885871eSZong-Zhe Yang }
1017e885871eSZong-Zhe Yang 
1018e885871eSZong-Zhe Yang static void
1019e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev,
1020e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
1021e885871eSZong-Zhe Yang {
1022e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
1023e885871eSZong-Zhe Yang 	u8 type = arg.type;
1024e885871eSZong-Zhe Yang 	u8 path = arg.path;
1025e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
1026e885871eSZong-Zhe Yang 	int i;
1027e885871eSZong-Zhe Yang 
1028e885871eSZong-Zhe Yang 	switch (type) {
1029e885871eSZong-Zhe Yang 	case 0:
1030e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1031e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1032e885871eSZong-Zhe Yang 		break;
1033e885871eSZong-Zhe Yang 	case 1:
1034e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
1035e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1036e885871eSZong-Zhe Yang 		break;
1037e885871eSZong-Zhe Yang 	case 2:
1038e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1039e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1040e885871eSZong-Zhe Yang 		break;
1041e885871eSZong-Zhe Yang 	case 3:
1042e885871eSZong-Zhe Yang 		for (i = 4; i < 8; i++, data >>= 8)
1043e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1044e885871eSZong-Zhe Yang 		break;
1045e885871eSZong-Zhe Yang 	default:
1046e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1047e885871eSZong-Zhe Yang 			   "bb gain op1db {0x%x:0x%x} with unknown type: %d\n",
1048e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1049e885871eSZong-Zhe Yang 		break;
1050e885871eSZong-Zhe Yang 	}
1051e885871eSZong-Zhe Yang }
1052e885871eSZong-Zhe Yang 
1053e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev,
1054e885871eSZong-Zhe Yang 				     const struct rtw89_reg2_def *reg,
1055e885871eSZong-Zhe Yang 				     enum rtw89_rf_path rf_path,
1056e885871eSZong-Zhe Yang 				     void *extra_data)
1057e885871eSZong-Zhe Yang {
1058e885871eSZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
1059e885871eSZong-Zhe Yang 	union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr };
1060c6a9d360SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
1061e885871eSZong-Zhe Yang 
1062e885871eSZong-Zhe Yang 	if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR)
1063e885871eSZong-Zhe Yang 		return;
1064e885871eSZong-Zhe Yang 
1065e885871eSZong-Zhe Yang 	if (arg.path >= chip->rf_path_num)
1066e885871eSZong-Zhe Yang 		return;
1067e885871eSZong-Zhe Yang 
1068e885871eSZong-Zhe Yang 	if (arg.addr >= 0xf9 && arg.addr <= 0xfe) {
1069e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev, "bb gain table with flow ctrl\n");
1070e885871eSZong-Zhe Yang 		return;
1071e885871eSZong-Zhe Yang 	}
1072e885871eSZong-Zhe Yang 
1073e885871eSZong-Zhe Yang 	switch (arg.cfg_type) {
1074e885871eSZong-Zhe Yang 	case 0:
1075e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data);
1076e885871eSZong-Zhe Yang 		break;
1077e885871eSZong-Zhe Yang 	case 1:
1078e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data);
1079e885871eSZong-Zhe Yang 		break;
1080e885871eSZong-Zhe Yang 	case 2:
1081e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data);
1082e885871eSZong-Zhe Yang 		break;
1083e885871eSZong-Zhe Yang 	case 3:
1084e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data);
1085e885871eSZong-Zhe Yang 		break;
1086c6a9d360SPing-Ke Shih 	case 4:
1087c6a9d360SPing-Ke Shih 		/* This cfg_type is only used by rfe_type >= 50 with eFEM */
1088c6a9d360SPing-Ke Shih 		if (efuse->rfe_type < 50)
1089c6a9d360SPing-Ke Shih 			break;
1090c6a9d360SPing-Ke Shih 		fallthrough;
1091e885871eSZong-Zhe Yang 	default:
1092e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1093e885871eSZong-Zhe Yang 			   "bb gain {0x%x:0x%x} with unknown cfg type: %d\n",
1094e885871eSZong-Zhe Yang 			   arg.addr, reg->data, arg.cfg_type);
1095e885871eSZong-Zhe Yang 		break;
1096e885871eSZong-Zhe Yang 	}
1097e885871eSZong-Zhe Yang }
1098e885871eSZong-Zhe Yang 
1099e3ec7017SPing-Ke Shih static void
1100e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev,
1101e3ec7017SPing-Ke Shih 			     const struct rtw89_reg2_def *reg,
1102e3ec7017SPing-Ke Shih 			     enum rtw89_rf_path rf_path,
1103e3ec7017SPing-Ke Shih 			     struct rtw89_fw_h2c_rf_reg_info *info)
1104e3ec7017SPing-Ke Shih {
1105e3ec7017SPing-Ke Shih 	u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE;
1106e3ec7017SPing-Ke Shih 	u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE;
1107e3ec7017SPing-Ke Shih 
110830101812SPing-Ke Shih 	if (page >= RTW89_H2C_RF_PAGE_NUM) {
110930101812SPing-Ke Shih 		rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d",
111030101812SPing-Ke Shih 			   rf_path, info->curr_idx);
111130101812SPing-Ke Shih 		return;
111230101812SPing-Ke Shih 	}
111330101812SPing-Ke Shih 
1114e3ec7017SPing-Ke Shih 	info->rtw89_phy_config_rf_h2c[page][idx] =
1115e3ec7017SPing-Ke Shih 		cpu_to_le32((reg->addr << 20) | reg->data);
1116e3ec7017SPing-Ke Shih 	info->curr_idx++;
1117e3ec7017SPing-Ke Shih }
1118e3ec7017SPing-Ke Shih 
1119e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev,
1120e3ec7017SPing-Ke Shih 				      struct rtw89_fw_h2c_rf_reg_info *info)
1121e3ec7017SPing-Ke Shih {
112230101812SPing-Ke Shih 	u16 remain = info->curr_idx;
112330101812SPing-Ke Shih 	u16 len = 0;
1124e3ec7017SPing-Ke Shih 	u8 i;
1125e3ec7017SPing-Ke Shih 	int ret = 0;
1126e3ec7017SPing-Ke Shih 
112730101812SPing-Ke Shih 	if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) {
1128e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev,
112930101812SPing-Ke Shih 			   "rf reg h2c total len %d larger than %d\n",
113030101812SPing-Ke Shih 			   remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE);
113130101812SPing-Ke Shih 		ret = -EINVAL;
113230101812SPing-Ke Shih 		goto out;
1133e3ec7017SPing-Ke Shih 	}
1134e3ec7017SPing-Ke Shih 
113530101812SPing-Ke Shih 	for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) {
113630101812SPing-Ke Shih 		len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain;
113730101812SPing-Ke Shih 		ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i);
1138e3ec7017SPing-Ke Shih 		if (ret)
113930101812SPing-Ke Shih 			goto out;
1140e3ec7017SPing-Ke Shih 	}
114130101812SPing-Ke Shih out:
1142e3ec7017SPing-Ke Shih 	info->curr_idx = 0;
1143e3ec7017SPing-Ke Shih 
114430101812SPing-Ke Shih 	return ret;
1145e3ec7017SPing-Ke Shih }
1146e3ec7017SPing-Ke Shih 
1147d9112042SChih-Kang Chang static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev,
1148d9112042SChih-Kang Chang 					 const struct rtw89_reg2_def *reg,
1149d9112042SChih-Kang Chang 					 enum rtw89_rf_path rf_path,
1150d9112042SChih-Kang Chang 					 void *extra_data)
1151d9112042SChih-Kang Chang {
1152d9112042SChih-Kang Chang 	u32 addr = reg->addr;
1153d9112042SChih-Kang Chang 
1154d9112042SChih-Kang Chang 	if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb ||
1155d9112042SChih-Kang Chang 	    addr == 0xfa || addr == 0xf9)
1156d9112042SChih-Kang Chang 		return;
1157d9112042SChih-Kang Chang 
1158d9112042SChih-Kang Chang 	if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100)
1159d9112042SChih-Kang Chang 		return;
1160d9112042SChih-Kang Chang 
1161d9112042SChih-Kang Chang 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1162d9112042SChih-Kang Chang 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1163d9112042SChih-Kang Chang }
1164d9112042SChih-Kang Chang 
1165e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev,
1166e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
1167e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
1168e3ec7017SPing-Ke Shih 				    void *extra_data)
1169e3ec7017SPing-Ke Shih {
1170e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe) {
1171e3ec7017SPing-Ke Shih 		mdelay(50);
1172e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfd) {
1173e3ec7017SPing-Ke Shih 		mdelay(5);
1174e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfc) {
1175e3ec7017SPing-Ke Shih 		mdelay(1);
1176e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfb) {
1177e3ec7017SPing-Ke Shih 		udelay(50);
1178e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfa) {
1179e3ec7017SPing-Ke Shih 		udelay(5);
1180e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xf9) {
1181e3ec7017SPing-Ke Shih 		udelay(1);
1182e3ec7017SPing-Ke Shih 	} else {
1183e3ec7017SPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data);
1184e3ec7017SPing-Ke Shih 		rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1185e3ec7017SPing-Ke Shih 					     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1186e3ec7017SPing-Ke Shih 	}
1187e3ec7017SPing-Ke Shih }
1188e3ec7017SPing-Ke Shih 
11892a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev,
11902a5f2b32SPing-Ke Shih 				const struct rtw89_reg2_def *reg,
11912a5f2b32SPing-Ke Shih 				enum rtw89_rf_path rf_path,
11922a5f2b32SPing-Ke Shih 				void *extra_data)
11932a5f2b32SPing-Ke Shih {
11942a5f2b32SPing-Ke Shih 	rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data);
11952a5f2b32SPing-Ke Shih 
11962a5f2b32SPing-Ke Shih 	if (reg->addr < 0x100)
11972a5f2b32SPing-Ke Shih 		return;
11982a5f2b32SPing-Ke Shih 
11992a5f2b32SPing-Ke Shih 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
12002a5f2b32SPing-Ke Shih 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
12012a5f2b32SPing-Ke Shih }
12022a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1);
12032a5f2b32SPing-Ke Shih 
1204e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev,
1205e3ec7017SPing-Ke Shih 				  const struct rtw89_phy_table *table,
1206e3ec7017SPing-Ke Shih 				  u32 *headline_size, u32 *headline_idx,
1207e3ec7017SPing-Ke Shih 				  u8 rfe, u8 cv)
1208e3ec7017SPing-Ke Shih {
1209e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1210e3ec7017SPing-Ke Shih 	u32 headline;
1211e3ec7017SPing-Ke Shih 	u32 compare, target;
1212e3ec7017SPing-Ke Shih 	u8 rfe_para, cv_para;
1213e3ec7017SPing-Ke Shih 	u8 cv_max = 0;
1214e3ec7017SPing-Ke Shih 	bool case_matched = false;
1215e3ec7017SPing-Ke Shih 	u32 i;
1216e3ec7017SPing-Ke Shih 
1217e3ec7017SPing-Ke Shih 	for (i = 0; i < table->n_regs; i++) {
1218e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1219e3ec7017SPing-Ke Shih 		headline = get_phy_headline(reg->addr);
1220e3ec7017SPing-Ke Shih 		if (headline != PHY_HEADLINE_VALID)
1221e3ec7017SPing-Ke Shih 			break;
1222e3ec7017SPing-Ke Shih 	}
1223e3ec7017SPing-Ke Shih 	*headline_size = i;
1224e3ec7017SPing-Ke Shih 	if (*headline_size == 0)
1225e3ec7017SPing-Ke Shih 		return 0;
1226e3ec7017SPing-Ke Shih 
1227e3ec7017SPing-Ke Shih 	/* case 1: RFE match, CV match */
1228e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, cv);
1229e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1230e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1231e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1232e3ec7017SPing-Ke Shih 		if (target == compare) {
1233e3ec7017SPing-Ke Shih 			*headline_idx = i;
1234e3ec7017SPing-Ke Shih 			return 0;
1235e3ec7017SPing-Ke Shih 		}
1236e3ec7017SPing-Ke Shih 	}
1237e3ec7017SPing-Ke Shih 
1238e3ec7017SPing-Ke Shih 	/* case 2: RFE match, CV don't care */
1239e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, PHY_COND_DONT_CARE);
1240e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1241e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1242e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1243e3ec7017SPing-Ke Shih 		if (target == compare) {
1244e3ec7017SPing-Ke Shih 			*headline_idx = i;
1245e3ec7017SPing-Ke Shih 			return 0;
1246e3ec7017SPing-Ke Shih 		}
1247e3ec7017SPing-Ke Shih 	}
1248e3ec7017SPing-Ke Shih 
1249e3ec7017SPing-Ke Shih 	/* case 3: RFE match, CV max in table */
1250e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1251e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1252e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1253e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1254e3ec7017SPing-Ke Shih 		if (rfe_para == rfe) {
1255e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1256e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1257e3ec7017SPing-Ke Shih 				*headline_idx = i;
1258e3ec7017SPing-Ke Shih 				case_matched = true;
1259e3ec7017SPing-Ke Shih 			}
1260e3ec7017SPing-Ke Shih 		}
1261e3ec7017SPing-Ke Shih 	}
1262e3ec7017SPing-Ke Shih 
1263e3ec7017SPing-Ke Shih 	if (case_matched)
1264e3ec7017SPing-Ke Shih 		return 0;
1265e3ec7017SPing-Ke Shih 
1266e3ec7017SPing-Ke Shih 	/* case 4: RFE don't care, CV max in table */
1267e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1268e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1269e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1270e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1271e3ec7017SPing-Ke Shih 		if (rfe_para == PHY_COND_DONT_CARE) {
1272e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1273e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1274e3ec7017SPing-Ke Shih 				*headline_idx = i;
1275e3ec7017SPing-Ke Shih 				case_matched = true;
1276e3ec7017SPing-Ke Shih 			}
1277e3ec7017SPing-Ke Shih 		}
1278e3ec7017SPing-Ke Shih 	}
1279e3ec7017SPing-Ke Shih 
1280e3ec7017SPing-Ke Shih 	if (case_matched)
1281e3ec7017SPing-Ke Shih 		return 0;
1282e3ec7017SPing-Ke Shih 
1283e3ec7017SPing-Ke Shih 	return -EINVAL;
1284e3ec7017SPing-Ke Shih }
1285e3ec7017SPing-Ke Shih 
1286e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev,
1287e3ec7017SPing-Ke Shih 			       const struct rtw89_phy_table *table,
1288e3ec7017SPing-Ke Shih 			       void (*config)(struct rtw89_dev *rtwdev,
1289e3ec7017SPing-Ke Shih 					      const struct rtw89_reg2_def *reg,
1290e3ec7017SPing-Ke Shih 					      enum rtw89_rf_path rf_path,
1291e3ec7017SPing-Ke Shih 					      void *data),
1292e3ec7017SPing-Ke Shih 			       void *extra_data)
1293e3ec7017SPing-Ke Shih {
1294e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1295e3ec7017SPing-Ke Shih 	enum rtw89_rf_path rf_path = table->rf_path;
1296e3ec7017SPing-Ke Shih 	u8 rfe = rtwdev->efuse.rfe_type;
1297e3ec7017SPing-Ke Shih 	u8 cv = rtwdev->hal.cv;
1298e3ec7017SPing-Ke Shih 	u32 i;
1299e3ec7017SPing-Ke Shih 	u32 headline_size = 0, headline_idx = 0;
1300e3ec7017SPing-Ke Shih 	u32 target = 0, cfg_target;
1301e3ec7017SPing-Ke Shih 	u8 cond;
1302e3ec7017SPing-Ke Shih 	bool is_matched = true;
1303e3ec7017SPing-Ke Shih 	bool target_found = false;
1304e3ec7017SPing-Ke Shih 	int ret;
1305e3ec7017SPing-Ke Shih 
1306e3ec7017SPing-Ke Shih 	ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size,
1307e3ec7017SPing-Ke Shih 				     &headline_idx, rfe, cv);
1308e3ec7017SPing-Ke Shih 	if (ret) {
1309e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv);
1310e3ec7017SPing-Ke Shih 		return;
1311e3ec7017SPing-Ke Shih 	}
1312e3ec7017SPing-Ke Shih 
1313e3ec7017SPing-Ke Shih 	cfg_target = get_phy_target(table->regs[headline_idx].addr);
1314e3ec7017SPing-Ke Shih 	for (i = headline_size; i < table->n_regs; i++) {
1315e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1316e3ec7017SPing-Ke Shih 		cond = get_phy_cond(reg->addr);
1317e3ec7017SPing-Ke Shih 		switch (cond) {
1318e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_IF:
1319e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELIF:
1320e3ec7017SPing-Ke Shih 			target = get_phy_target(reg->addr);
1321e3ec7017SPing-Ke Shih 			break;
1322e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELSE:
1323e3ec7017SPing-Ke Shih 			is_matched = false;
1324e3ec7017SPing-Ke Shih 			if (!target_found) {
1325e3ec7017SPing-Ke Shih 				rtw89_warn(rtwdev, "failed to load CR %x/%x\n",
1326e3ec7017SPing-Ke Shih 					   reg->addr, reg->data);
1327e3ec7017SPing-Ke Shih 				return;
1328e3ec7017SPing-Ke Shih 			}
1329e3ec7017SPing-Ke Shih 			break;
1330e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_END:
1331e3ec7017SPing-Ke Shih 			is_matched = true;
1332e3ec7017SPing-Ke Shih 			target_found = false;
1333e3ec7017SPing-Ke Shih 			break;
1334e3ec7017SPing-Ke Shih 		case PHY_COND_CHECK:
1335e3ec7017SPing-Ke Shih 			if (target_found) {
1336e3ec7017SPing-Ke Shih 				is_matched = false;
1337e3ec7017SPing-Ke Shih 				break;
1338e3ec7017SPing-Ke Shih 			}
1339e3ec7017SPing-Ke Shih 
1340e3ec7017SPing-Ke Shih 			if (target == cfg_target) {
1341e3ec7017SPing-Ke Shih 				is_matched = true;
1342e3ec7017SPing-Ke Shih 				target_found = true;
1343e3ec7017SPing-Ke Shih 			} else {
1344e3ec7017SPing-Ke Shih 				is_matched = false;
1345e3ec7017SPing-Ke Shih 				target_found = false;
1346e3ec7017SPing-Ke Shih 			}
1347e3ec7017SPing-Ke Shih 			break;
1348e3ec7017SPing-Ke Shih 		default:
1349e3ec7017SPing-Ke Shih 			if (is_matched)
1350e3ec7017SPing-Ke Shih 				config(rtwdev, reg, rf_path, extra_data);
1351e3ec7017SPing-Ke Shih 			break;
1352e3ec7017SPing-Ke Shih 		}
1353e3ec7017SPing-Ke Shih 	}
1354e3ec7017SPing-Ke Shih }
1355e3ec7017SPing-Ke Shih 
1356e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev)
1357e3ec7017SPing-Ke Shih {
135889474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1359e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
136089474720SPing-Ke Shih 	const struct rtw89_phy_table *bb_table;
136189474720SPing-Ke Shih 	const struct rtw89_phy_table *bb_gain_table;
1362e3ec7017SPing-Ke Shih 
136389474720SPing-Ke Shih 	bb_table = elm_info->bb_tbl ? elm_info->bb_tbl : chip->bb_table;
1364e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL);
1365e3ec7017SPing-Ke Shih 	rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0);
136689474720SPing-Ke Shih 
136789474720SPing-Ke Shih 	bb_gain_table = elm_info->bb_gain ? elm_info->bb_gain : chip->bb_gain_table;
1368e885871eSZong-Zhe Yang 	if (bb_gain_table)
1369e885871eSZong-Zhe Yang 		rtw89_phy_init_reg(rtwdev, bb_gain_table,
1370e885871eSZong-Zhe Yang 				   rtw89_phy_config_bb_gain, NULL);
1371e3ec7017SPing-Ke Shih 	rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0);
1372e3ec7017SPing-Ke Shih }
1373e3ec7017SPing-Ke Shih 
1374e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev)
1375e3ec7017SPing-Ke Shih {
1376e3ec7017SPing-Ke Shih 	rtw89_phy_write32(rtwdev, 0x8080, 0x4);
1377e3ec7017SPing-Ke Shih 	udelay(1);
1378e3ec7017SPing-Ke Shih 	return rtw89_phy_read32(rtwdev, 0x8080);
1379e3ec7017SPing-Ke Shih }
1380e3ec7017SPing-Ke Shih 
1381d9112042SChih-Kang Chang void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio)
1382e3ec7017SPing-Ke Shih {
13832a5f2b32SPing-Ke Shih 	void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg,
13842a5f2b32SPing-Ke Shih 		       enum rtw89_rf_path rf_path, void *data);
138589474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1386e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1387e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *rf_table;
1388e3ec7017SPing-Ke Shih 	struct rtw89_fw_h2c_rf_reg_info *rf_reg_info;
1389e3ec7017SPing-Ke Shih 	u8 path;
1390e3ec7017SPing-Ke Shih 
1391e3ec7017SPing-Ke Shih 	rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL);
1392e3ec7017SPing-Ke Shih 	if (!rf_reg_info)
1393e3ec7017SPing-Ke Shih 		return;
1394e3ec7017SPing-Ke Shih 
1395e3ec7017SPing-Ke Shih 	for (path = RF_PATH_A; path < chip->rf_path_num; path++) {
139689474720SPing-Ke Shih 		rf_table = elm_info->rf_radio[path] ?
139789474720SPing-Ke Shih 			   elm_info->rf_radio[path] : chip->rf_table[path];
13982a5f2b32SPing-Ke Shih 		rf_reg_info->rf_path = rf_table->rf_path;
1399d9112042SChih-Kang Chang 		if (noio)
1400d9112042SChih-Kang Chang 			config = rtw89_phy_config_rf_reg_noio;
1401d9112042SChih-Kang Chang 		else
1402d9112042SChih-Kang Chang 			config = rf_table->config ? rf_table->config :
1403d9112042SChih-Kang Chang 				 rtw89_phy_config_rf_reg;
14042a5f2b32SPing-Ke Shih 		rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info);
1405e3ec7017SPing-Ke Shih 		if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info))
1406e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n",
14072a5f2b32SPing-Ke Shih 				   rf_reg_info->rf_path);
1408e3ec7017SPing-Ke Shih 	}
1409e3ec7017SPing-Ke Shih 	kfree(rf_reg_info);
1410e3ec7017SPing-Ke Shih }
1411e3ec7017SPing-Ke Shih 
1412e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev)
1413e3ec7017SPing-Ke Shih {
141489474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1415e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1416e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *nctl_table;
1417e3ec7017SPing-Ke Shih 	u32 val;
1418e3ec7017SPing-Ke Shih 	int ret;
1419e3ec7017SPing-Ke Shih 
1420e3ec7017SPing-Ke Shih 	/* IQK/DPK clock & reset */
1421d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3);
1422d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1);
1423d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000);
1424a24be8bbSPing-Ke Shih 	if (chip->chip_id != RTL8851B)
1425d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000);
1426d0c820ccSPing-Ke Shih 	if (chip->chip_id == RTL8852B)
1427d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2);
1428e3ec7017SPing-Ke Shih 
1429e3ec7017SPing-Ke Shih 	/* check 0x8080 */
1430d0c820ccSPing-Ke Shih 	rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8);
1431e3ec7017SPing-Ke Shih 
1432e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10,
1433e3ec7017SPing-Ke Shih 				1000, false, rtwdev);
1434e3ec7017SPing-Ke Shih 	if (ret)
1435e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to poll nctl block\n");
1436e3ec7017SPing-Ke Shih 
143789474720SPing-Ke Shih 	nctl_table = elm_info->rf_nctl ? elm_info->rf_nctl : chip->nctl_table;
1438e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL);
1439a24be8bbSPing-Ke Shih 
1440a24be8bbSPing-Ke Shih 	if (chip->nctl_post_table)
1441a24be8bbSPing-Ke Shih 		rtw89_rfk_parser(rtwdev, chip->nctl_post_table);
1442e3ec7017SPing-Ke Shih }
1443e3ec7017SPing-Ke Shih 
1444e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr)
1445e3ec7017SPing-Ke Shih {
1446e3ec7017SPing-Ke Shih 	u32 phy_page = addr >> 8;
1447e3ec7017SPing-Ke Shih 	u32 ofst = 0;
1448e3ec7017SPing-Ke Shih 
1449e3ec7017SPing-Ke Shih 	switch (phy_page) {
1450e3ec7017SPing-Ke Shih 	case 0x6:
1451e3ec7017SPing-Ke Shih 	case 0x7:
1452e3ec7017SPing-Ke Shih 	case 0x8:
1453e3ec7017SPing-Ke Shih 	case 0x9:
1454e3ec7017SPing-Ke Shih 	case 0xa:
1455e3ec7017SPing-Ke Shih 	case 0xb:
1456e3ec7017SPing-Ke Shih 	case 0xc:
1457e3ec7017SPing-Ke Shih 	case 0xd:
1458e3ec7017SPing-Ke Shih 	case 0x19:
1459e3ec7017SPing-Ke Shih 	case 0x1a:
1460e3ec7017SPing-Ke Shih 	case 0x1b:
1461e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1462e3ec7017SPing-Ke Shih 		break;
1463e3ec7017SPing-Ke Shih 	default:
1464e3ec7017SPing-Ke Shih 		/* warning case */
1465e3ec7017SPing-Ke Shih 		ofst = 0;
1466e3ec7017SPing-Ke Shih 		break;
1467e3ec7017SPing-Ke Shih 	}
1468e3ec7017SPing-Ke Shih 
1469e3ec7017SPing-Ke Shih 	if (phy_page >= 0x40 && phy_page <= 0x4f)
1470e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1471e3ec7017SPing-Ke Shih 
1472e3ec7017SPing-Ke Shih 	return ofst;
1473e3ec7017SPing-Ke Shih }
1474e3ec7017SPing-Ke Shih 
1475e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1476e3ec7017SPing-Ke Shih 			   u32 data, enum rtw89_phy_idx phy_idx)
1477e3ec7017SPing-Ke Shih {
1478e3ec7017SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1479e3ec7017SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1480e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, addr, mask, data);
1481e3ec7017SPing-Ke Shih }
1482861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx);
1483e3ec7017SPing-Ke Shih 
14846b069898SPing-Ke Shih u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
14856b069898SPing-Ke Shih 			 enum rtw89_phy_idx phy_idx)
14866b069898SPing-Ke Shih {
14876b069898SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
14886b069898SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
14896b069898SPing-Ke Shih 	return rtw89_phy_read32_mask(rtwdev, addr, mask);
14906b069898SPing-Ke Shih }
14916b069898SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read32_idx);
14926b069898SPing-Ke Shih 
1493e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1494e3ec7017SPing-Ke Shih 			    u32 val)
1495e3ec7017SPing-Ke Shih {
1496e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0);
1497e3ec7017SPing-Ke Shih 
1498e3ec7017SPing-Ke Shih 	if (!rtwdev->dbcc_en)
1499e3ec7017SPing-Ke Shih 		return;
1500e3ec7017SPing-Ke Shih 
1501e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1);
1502e3ec7017SPing-Ke Shih }
1503e3ec7017SPing-Ke Shih 
1504e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev,
1505e3ec7017SPing-Ke Shih 			      const struct rtw89_phy_reg3_tbl *tbl)
1506e3ec7017SPing-Ke Shih {
1507e3ec7017SPing-Ke Shih 	const struct rtw89_reg3_def *reg3;
1508e3ec7017SPing-Ke Shih 	int i;
1509e3ec7017SPing-Ke Shih 
1510e3ec7017SPing-Ke Shih 	for (i = 0; i < tbl->size; i++) {
1511e3ec7017SPing-Ke Shih 		reg3 = &tbl->reg3[i];
1512e3ec7017SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data);
1513e3ec7017SPing-Ke Shih 	}
1514e3ec7017SPing-Ke Shih }
1515861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl);
1516e3ec7017SPing-Ke Shih 
1517b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_idx_num[] = {
1518b4a283fbSZong-Zhe Yang 	[RTW89_RS_CCK] = RTW89_RATE_CCK_NUM,
1519b4a283fbSZong-Zhe Yang 	[RTW89_RS_OFDM] = RTW89_RATE_OFDM_NUM,
1520b4a283fbSZong-Zhe Yang 	[RTW89_RS_MCS] = RTW89_RATE_MCS_NUM,
1521b4a283fbSZong-Zhe Yang 	[RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_NUM,
1522b4a283fbSZong-Zhe Yang 	[RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_NUM,
1523e3ec7017SPing-Ke Shih };
1524e3ec7017SPing-Ke Shih 
1525b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_nss_num[] = {
1526e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = 1,
1527e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = 1,
1528b4a283fbSZong-Zhe Yang 	[RTW89_RS_MCS] = RTW89_NSS_NUM,
1529b4a283fbSZong-Zhe Yang 	[RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_NUM,
1530e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = 1,
1531e3ec7017SPing-Ke Shih };
1532e3ec7017SPing-Ke Shih 
1533e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = {
1534e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck),
1535e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm),
1536e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs),
1537e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm),
1538e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset),
1539e3ec7017SPing-Ke Shih };
1540e3ec7017SPing-Ke Shih 
1541e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs])
1542b4a283fbSZong-Zhe Yang #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_num[rs] + (idx))
1543e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \
1544b4a283fbSZong-Zhe Yang 	((nss) < rtw89_rs_nss_num[rs] && (idx) < rtw89_rs_idx_num[rs])
1545e3ec7017SPing-Ke Shih 
1546e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev,
1547e3ec7017SPing-Ke Shih 				 const struct rtw89_txpwr_table *tbl)
1548e3ec7017SPing-Ke Shih {
1549e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data;
1550e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size;
1551e3ec7017SPing-Ke Shih 	s8 *byr;
1552e3ec7017SPing-Ke Shih 	u32 data;
1553e3ec7017SPing-Ke Shih 	u8 i, idx;
1554e3ec7017SPing-Ke Shih 
1555e3ec7017SPing-Ke Shih 	for (; cfg < end; cfg++) {
1556e3ec7017SPing-Ke Shih 		byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]);
1557e3ec7017SPing-Ke Shih 		data = cfg->data;
1558e3ec7017SPing-Ke Shih 
1559e3ec7017SPing-Ke Shih 		for (i = 0; i < cfg->len; i++, data >>= 8) {
1560e3ec7017SPing-Ke Shih 			idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i));
1561e3ec7017SPing-Ke Shih 			byr[idx] = (s8)(data & 0xff);
1562e3ec7017SPing-Ke Shih 		}
1563e3ec7017SPing-Ke Shih 	}
1564e3ec7017SPing-Ke Shih }
1565861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate);
1566e3ec7017SPing-Ke Shih 
1567e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf)				\
1568e3ec7017SPing-Ke Shih ({									\
1569e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *__c = (rtwdev)->chip;		\
1570e3ec7017SPing-Ke Shih 	(txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac);	\
1571e3ec7017SPing-Ke Shih })
1572e3ec7017SPing-Ke Shih 
15739b43bd1aSZong-Zhe Yang static
157407ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band,
1575e3ec7017SPing-Ke Shih 			       const struct rtw89_rate_desc *rate_desc)
1576e3ec7017SPing-Ke Shih {
1577e3ec7017SPing-Ke Shih 	s8 *byr;
1578e3ec7017SPing-Ke Shih 	u8 idx;
1579e3ec7017SPing-Ke Shih 
1580e3ec7017SPing-Ke Shih 	if (rate_desc->rs == RTW89_RS_CCK)
1581e3ec7017SPing-Ke Shih 		band = RTW89_BAND_2G;
1582e3ec7017SPing-Ke Shih 
1583e3ec7017SPing-Ke Shih 	if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) {
1584e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1585e3ec7017SPing-Ke Shih 			    "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n",
1586e3ec7017SPing-Ke Shih 			    rate_desc->rs, rate_desc->nss, rate_desc->idx);
1587e3ec7017SPing-Ke Shih 
1588e3ec7017SPing-Ke Shih 		return 0;
1589e3ec7017SPing-Ke Shih 	}
1590e3ec7017SPing-Ke Shih 
1591e3ec7017SPing-Ke Shih 	byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]);
1592e3ec7017SPing-Ke Shih 	idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx);
1593e3ec7017SPing-Ke Shih 
1594e3ec7017SPing-Ke Shih 	return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]);
1595e3ec7017SPing-Ke Shih }
1596e3ec7017SPing-Ke Shih 
1597ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g)
1598e3ec7017SPing-Ke Shih {
1599ac74f016SZong-Zhe Yang 	switch (channel_6g) {
1600ac74f016SZong-Zhe Yang 	case 1 ... 29:
1601ac74f016SZong-Zhe Yang 		return (channel_6g - 1) / 2;
1602ac74f016SZong-Zhe Yang 	case 33 ... 61:
1603ac74f016SZong-Zhe Yang 		return (channel_6g - 3) / 2;
1604ac74f016SZong-Zhe Yang 	case 65 ... 93:
1605ac74f016SZong-Zhe Yang 		return (channel_6g - 5) / 2;
1606ac74f016SZong-Zhe Yang 	case 97 ... 125:
1607ac74f016SZong-Zhe Yang 		return (channel_6g - 7) / 2;
1608ac74f016SZong-Zhe Yang 	case 129 ... 157:
1609ac74f016SZong-Zhe Yang 		return (channel_6g - 9) / 2;
1610ac74f016SZong-Zhe Yang 	case 161 ... 189:
1611ac74f016SZong-Zhe Yang 		return (channel_6g - 11) / 2;
1612ac74f016SZong-Zhe Yang 	case 193 ... 221:
1613ac74f016SZong-Zhe Yang 		return (channel_6g - 13) / 2;
1614ac74f016SZong-Zhe Yang 	case 225 ... 253:
1615ac74f016SZong-Zhe Yang 		return (channel_6g - 15) / 2;
1616ac74f016SZong-Zhe Yang 	default:
1617ac74f016SZong-Zhe Yang 		rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g);
1618ac74f016SZong-Zhe Yang 		return 0;
1619ac74f016SZong-Zhe Yang 	}
1620ac74f016SZong-Zhe Yang }
1621ac74f016SZong-Zhe Yang 
1622ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel)
1623ac74f016SZong-Zhe Yang {
1624ac74f016SZong-Zhe Yang 	if (band == RTW89_BAND_6G)
1625ac74f016SZong-Zhe Yang 		return rtw89_channel_6g_to_idx(rtwdev, channel);
1626ac74f016SZong-Zhe Yang 
1627e3ec7017SPing-Ke Shih 	switch (channel) {
1628e3ec7017SPing-Ke Shih 	case 1 ... 14:
1629e3ec7017SPing-Ke Shih 		return channel - 1;
1630e3ec7017SPing-Ke Shih 	case 36 ... 64:
1631e3ec7017SPing-Ke Shih 		return (channel - 36) / 2;
1632e3ec7017SPing-Ke Shih 	case 100 ... 144:
1633e3ec7017SPing-Ke Shih 		return ((channel - 100) / 2) + 15;
1634e3ec7017SPing-Ke Shih 	case 149 ... 177:
1635e3ec7017SPing-Ke Shih 		return ((channel - 149) / 2) + 38;
1636e3ec7017SPing-Ke Shih 	default:
1637e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown channel: %d\n", channel);
1638e3ec7017SPing-Ke Shih 		return 0;
1639e3ec7017SPing-Ke Shih 	}
1640e3ec7017SPing-Ke Shih }
1641e3ec7017SPing-Ke Shih 
164207ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band,
1643e3ec7017SPing-Ke Shih 			      u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch)
1644e3ec7017SPing-Ke Shih {
16455395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
16465395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
16475395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
16485395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
16492a8ec45fSZong-Zhe Yang 	struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory;
1650*bfbadacfSZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
1651*bfbadacfSZong-Zhe Yang 	u32 freq = ieee80211_channel_to_frequency(ch, nl_band);
1652ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1653e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
16542a8ec45fSZong-Zhe Yang 	u8 reg6 = regulatory->reg_6ghz_power;
1655e3ec7017SPing-Ke Shih 	s8 lmt = 0, sar;
1656e3ec7017SPing-Ke Shih 
1657e3ec7017SPing-Ke Shih 	switch (band) {
1658e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
16595395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16605395482aSZong-Zhe Yang 		if (lmt)
16615395482aSZong-Zhe Yang 			break;
16625395482aSZong-Zhe Yang 
16635395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1664e3ec7017SPing-Ke Shih 		break;
1665e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
16665395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16675395482aSZong-Zhe Yang 		if (lmt)
16685395482aSZong-Zhe Yang 			break;
16695395482aSZong-Zhe Yang 
16705395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1671e3ec7017SPing-Ke Shih 		break;
1672ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
16732a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][regd][reg6][ch_idx];
16745395482aSZong-Zhe Yang 		if (lmt)
16755395482aSZong-Zhe Yang 			break;
16765395482aSZong-Zhe Yang 
16772a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][RTW89_WW]
16782a8ec45fSZong-Zhe Yang 				       [RTW89_REG_6GHZ_POWER_DFLT]
16792a8ec45fSZong-Zhe Yang 				       [ch_idx];
1680ac74f016SZong-Zhe Yang 		break;
1681e3ec7017SPing-Ke Shih 	default:
1682e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1683e3ec7017SPing-Ke Shih 		return 0;
1684e3ec7017SPing-Ke Shih 	}
1685e3ec7017SPing-Ke Shih 
1686e3ec7017SPing-Ke Shih 	lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt);
1687*bfbadacfSZong-Zhe Yang 	sar = rtw89_query_sar(rtwdev, freq);
1688e3ec7017SPing-Ke Shih 
1689e3ec7017SPing-Ke Shih 	return min(lmt, sar);
1690e3ec7017SPing-Ke Shih }
1691861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit);
1692e3ec7017SPing-Ke Shih 
169307ef5f2fSZong-Zhe Yang #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch)		\
1694e3ec7017SPing-Ke Shih 	do {								\
1695e3ec7017SPing-Ke Shih 		u8 __i;							\
1696e3ec7017SPing-Ke Shih 		for (__i = 0; __i < RTW89_BF_NUM; __i++)		\
1697e3ec7017SPing-Ke Shih 			ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev,	\
169807ef5f2fSZong-Zhe Yang 							      band,	\
1699e3ec7017SPing-Ke Shih 							      bw, ntx,	\
1700e3ec7017SPing-Ke Shih 							      rs, __i,	\
1701e3ec7017SPing-Ke Shih 							      (ch));	\
1702e3ec7017SPing-Ke Shih 	} while (0)
1703e3ec7017SPing-Ke Shih 
1704e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev,
1705e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
170607ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch)
1707e3ec7017SPing-Ke Shih {
170807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1709e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
171007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1711e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
171207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1713e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_OFDM, ch);
171407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
171507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1716e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1717e3ec7017SPing-Ke Shih }
1718e3ec7017SPing-Ke Shih 
1719e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev,
1720e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
172107ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1722e3ec7017SPing-Ke Shih {
172307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1724e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch - 2);
172507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1726e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
172707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
172894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
172907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
173007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1731e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
173207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
173307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1734e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
173507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
173607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1737e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1738e3ec7017SPing-Ke Shih }
1739e3ec7017SPing-Ke Shih 
1740e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev,
1741e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
174207ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1743e3ec7017SPing-Ke Shih {
1744e3ec7017SPing-Ke Shih 	s8 val_0p5_n[RTW89_BF_NUM];
1745e3ec7017SPing-Ke Shih 	s8 val_0p5_p[RTW89_BF_NUM];
1746e3ec7017SPing-Ke Shih 	u8 i;
1747e3ec7017SPing-Ke Shih 
174807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
174994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
175007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
175107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1752e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 6);
175307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
175407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1755e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
175607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
175707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1758e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
175907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
176007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1761e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 6);
176207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
176307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1764e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
176507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
176607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1767e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
176807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
176907ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
1770e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1771e3ec7017SPing-Ke Shih 
177207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
1773e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
177407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
1775e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
1776e3ec7017SPing-Ke Shih 
1777e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_BF_NUM; i++)
1778e3ec7017SPing-Ke Shih 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1779e3ec7017SPing-Ke Shih }
1780e3ec7017SPing-Ke Shih 
178194b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev,
178294b70cafSZong-Zhe Yang 					    struct rtw89_txpwr_limit *lmt,
178307ef5f2fSZong-Zhe Yang 					    u8 band, u8 ntx, u8 ch, u8 pri_ch)
178494b70cafSZong-Zhe Yang {
178594b70cafSZong-Zhe Yang 	s8 val_0p5_n[RTW89_BF_NUM];
178694b70cafSZong-Zhe Yang 	s8 val_0p5_p[RTW89_BF_NUM];
178794b70cafSZong-Zhe Yang 	s8 val_2p5_n[RTW89_BF_NUM];
178894b70cafSZong-Zhe Yang 	s8 val_2p5_p[RTW89_BF_NUM];
178994b70cafSZong-Zhe Yang 	u8 i;
179094b70cafSZong-Zhe Yang 
179194b70cafSZong-Zhe Yang 	/* fill ofdm section */
179207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
179394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
179494b70cafSZong-Zhe Yang 
179594b70cafSZong-Zhe Yang 	/* fill mcs 20m section */
179607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
179707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
179894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 14);
179907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
180007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 10);
180207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
180307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 6);
180507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
180607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 2);
180807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band,
180907ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181094b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 2);
181107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band,
181207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 6);
181407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band,
181507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 10);
181707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band,
181807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 14);
182094b70cafSZong-Zhe Yang 
182194b70cafSZong-Zhe Yang 	/* fill mcs 40m section */
182207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
182307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
182494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 12);
182507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
182607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
182794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
182807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band,
182907ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
183094b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
183107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band,
183207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
183394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 12);
183494b70cafSZong-Zhe Yang 
183594b70cafSZong-Zhe Yang 	/* fill mcs 80m section */
183607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
183707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
183894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
183907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band,
184007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
184194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
184294b70cafSZong-Zhe Yang 
184394b70cafSZong-Zhe Yang 	/* fill mcs 160m section */
184407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band,
184507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_160,
184694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch);
184794b70cafSZong-Zhe Yang 
184894b70cafSZong-Zhe Yang 	/* fill mcs 40m 0p5 section */
184907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
185094b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
185107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
185294b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
185394b70cafSZong-Zhe Yang 
185494b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
185594b70cafSZong-Zhe Yang 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
185694b70cafSZong-Zhe Yang 
185794b70cafSZong-Zhe Yang 	/* fill mcs 40m 2p5 section */
185807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40,
185994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
186007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40,
186194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
186294b70cafSZong-Zhe Yang 
186394b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
186494b70cafSZong-Zhe Yang 		lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]);
186594b70cafSZong-Zhe Yang }
186694b70cafSZong-Zhe Yang 
18679b43bd1aSZong-Zhe Yang static
1868e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev,
186907ef5f2fSZong-Zhe Yang 				const struct rtw89_chan *chan,
1870e3ec7017SPing-Ke Shih 				struct rtw89_txpwr_limit *lmt,
1871e3ec7017SPing-Ke Shih 				u8 ntx)
1872e3ec7017SPing-Ke Shih {
187307ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
1874cbb145b9SZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
1875cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
1876cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
1877e3ec7017SPing-Ke Shih 
1878e3ec7017SPing-Ke Shih 	memset(lmt, 0, sizeof(*lmt));
1879e3ec7017SPing-Ke Shih 
1880e3ec7017SPing-Ke Shih 	switch (bw) {
1881e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
188207ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch);
1883e3ec7017SPing-Ke Shih 		break;
1884e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
188507ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch,
188607ef5f2fSZong-Zhe Yang 					       pri_ch);
1887e3ec7017SPing-Ke Shih 		break;
1888e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
188907ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch,
189007ef5f2fSZong-Zhe Yang 					       pri_ch);
189194b70cafSZong-Zhe Yang 		break;
189294b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
189307ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch,
189407ef5f2fSZong-Zhe Yang 						pri_ch);
1895e3ec7017SPing-Ke Shih 		break;
1896e3ec7017SPing-Ke Shih 	}
1897e3ec7017SPing-Ke Shih }
1898e3ec7017SPing-Ke Shih 
189907ef5f2fSZong-Zhe Yang static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band,
1900e3ec7017SPing-Ke Shih 					u8 ru, u8 ntx, u8 ch)
1901e3ec7017SPing-Ke Shih {
19025395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
19035395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
19045395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
19055395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
1906dad142c3SZong-Zhe Yang 	struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory;
1907*bfbadacfSZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
1908*bfbadacfSZong-Zhe Yang 	u32 freq = ieee80211_channel_to_frequency(ch, nl_band);
1909ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1910e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1911dad142c3SZong-Zhe Yang 	u8 reg6 = regulatory->reg_6ghz_power;
1912e3ec7017SPing-Ke Shih 	s8 lmt_ru = 0, sar;
1913e3ec7017SPing-Ke Shih 
1914e3ec7017SPing-Ke Shih 	switch (band) {
1915e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
19165395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][regd][ch_idx];
19175395482aSZong-Zhe Yang 		if (lmt_ru)
19185395482aSZong-Zhe Yang 			break;
19195395482aSZong-Zhe Yang 
19205395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1921e3ec7017SPing-Ke Shih 		break;
1922e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
19235395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][regd][ch_idx];
19245395482aSZong-Zhe Yang 		if (lmt_ru)
19255395482aSZong-Zhe Yang 			break;
19265395482aSZong-Zhe Yang 
19275395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1928e3ec7017SPing-Ke Shih 		break;
1929ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
1930dad142c3SZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][regd][reg6][ch_idx];
19315395482aSZong-Zhe Yang 		if (lmt_ru)
19325395482aSZong-Zhe Yang 			break;
19335395482aSZong-Zhe Yang 
1934dad142c3SZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][RTW89_WW]
1935dad142c3SZong-Zhe Yang 					     [RTW89_REG_6GHZ_POWER_DFLT]
1936dad142c3SZong-Zhe Yang 					     [ch_idx];
1937ac74f016SZong-Zhe Yang 		break;
1938e3ec7017SPing-Ke Shih 	default:
1939e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1940e3ec7017SPing-Ke Shih 		return 0;
1941e3ec7017SPing-Ke Shih 	}
1942e3ec7017SPing-Ke Shih 
1943e3ec7017SPing-Ke Shih 	lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru);
1944*bfbadacfSZong-Zhe Yang 	sar = rtw89_query_sar(rtwdev, freq);
1945e3ec7017SPing-Ke Shih 
1946e3ec7017SPing-Ke Shih 	return min(lmt_ru, sar);
1947e3ec7017SPing-Ke Shih }
1948e3ec7017SPing-Ke Shih 
1949e3ec7017SPing-Ke Shih static void
1950e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev,
1951e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
195207ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1953e3ec7017SPing-Ke Shih {
195407ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
195507ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1956e3ec7017SPing-Ke Shih 							ntx, ch);
195707ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
195807ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1959e3ec7017SPing-Ke Shih 							ntx, ch);
196007ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
196107ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1962e3ec7017SPing-Ke Shih 							 ntx, ch);
1963e3ec7017SPing-Ke Shih }
1964e3ec7017SPing-Ke Shih 
1965e3ec7017SPing-Ke Shih static void
1966e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev,
1967e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
196807ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1969e3ec7017SPing-Ke Shih {
197007ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = 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[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197407ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1975e3ec7017SPing-Ke Shih 							ntx, ch + 2);
197607ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197707ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1978e3ec7017SPing-Ke Shih 							ntx, ch - 2);
197907ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198007ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1981e3ec7017SPing-Ke Shih 							ntx, ch + 2);
198207ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198307ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1984e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
198507ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198607ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1987e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
1988e3ec7017SPing-Ke Shih }
1989e3ec7017SPing-Ke Shih 
1990e3ec7017SPing-Ke Shih static void
1991e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev,
1992e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
199307ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1994e3ec7017SPing-Ke Shih {
199507ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199607ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1997e3ec7017SPing-Ke Shih 							ntx, ch - 6);
199807ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199907ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2000e3ec7017SPing-Ke Shih 							ntx, ch - 2);
200107ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200207ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2003e3ec7017SPing-Ke Shih 							ntx, ch + 2);
200407ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200507ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2006e3ec7017SPing-Ke Shih 							ntx, ch + 6);
200707ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200807ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2009e3ec7017SPing-Ke Shih 							ntx, ch - 6);
201007ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201107ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2012e3ec7017SPing-Ke Shih 							ntx, ch - 2);
201307ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201407ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2015e3ec7017SPing-Ke Shih 							ntx, ch + 2);
201607ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201707ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2018e3ec7017SPing-Ke Shih 							ntx, ch + 6);
201907ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202007ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2021e3ec7017SPing-Ke Shih 							 ntx, ch - 6);
202207ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202307ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2024e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
202507ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202607ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2027e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
202807ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202907ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2030e3ec7017SPing-Ke Shih 							 ntx, ch + 6);
2031e3ec7017SPing-Ke Shih }
2032e3ec7017SPing-Ke Shih 
203394b70cafSZong-Zhe Yang static void
203494b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev,
203594b70cafSZong-Zhe Yang 				   struct rtw89_txpwr_limit_ru *lmt_ru,
203607ef5f2fSZong-Zhe Yang 				   u8 band, u8 ntx, u8 ch)
203794b70cafSZong-Zhe Yang {
203894b70cafSZong-Zhe Yang 	static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 };
203994b70cafSZong-Zhe Yang 	int i;
204094b70cafSZong-Zhe Yang 
204194b70cafSZong-Zhe Yang 	static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM);
204294b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_RU_SEC_NUM; i++) {
204307ef5f2fSZong-Zhe Yang 		lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
204494b70cafSZong-Zhe Yang 								RTW89_RU26,
204594b70cafSZong-Zhe Yang 								ntx,
204694b70cafSZong-Zhe Yang 								ch + ofst[i]);
204707ef5f2fSZong-Zhe Yang 		lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
204894b70cafSZong-Zhe Yang 								RTW89_RU52,
204994b70cafSZong-Zhe Yang 								ntx,
205094b70cafSZong-Zhe Yang 								ch + ofst[i]);
205107ef5f2fSZong-Zhe Yang 		lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
205294b70cafSZong-Zhe Yang 								 RTW89_RU106,
205394b70cafSZong-Zhe Yang 								 ntx,
205494b70cafSZong-Zhe Yang 								 ch + ofst[i]);
205594b70cafSZong-Zhe Yang 	}
205694b70cafSZong-Zhe Yang }
205794b70cafSZong-Zhe Yang 
20589b43bd1aSZong-Zhe Yang static
2059e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev,
206007ef5f2fSZong-Zhe Yang 				   const struct rtw89_chan *chan,
2061e3ec7017SPing-Ke Shih 				   struct rtw89_txpwr_limit_ru *lmt_ru,
2062e3ec7017SPing-Ke Shih 				   u8 ntx)
2063e3ec7017SPing-Ke Shih {
206407ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
2065cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
2066cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
2067e3ec7017SPing-Ke Shih 
2068e3ec7017SPing-Ke Shih 	memset(lmt_ru, 0, sizeof(*lmt_ru));
2069e3ec7017SPing-Ke Shih 
2070e3ec7017SPing-Ke Shih 	switch (bw) {
2071e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
207207ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx,
207307ef5f2fSZong-Zhe Yang 						  ch);
2074e3ec7017SPing-Ke Shih 		break;
2075e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
207607ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx,
207707ef5f2fSZong-Zhe Yang 						  ch);
2078e3ec7017SPing-Ke Shih 		break;
2079e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
208007ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx,
208107ef5f2fSZong-Zhe Yang 						  ch);
2082e3ec7017SPing-Ke Shih 		break;
208394b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
208407ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx,
208507ef5f2fSZong-Zhe Yang 						   ch);
208694b70cafSZong-Zhe Yang 		break;
2087e3ec7017SPing-Ke Shih 	}
2088e3ec7017SPing-Ke Shih }
20899b43bd1aSZong-Zhe Yang 
20909b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev,
20919b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
20929b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
20939b43bd1aSZong-Zhe Yang {
2094ddf9a2eaSZong-Zhe Yang 	u8 max_nss_num = rtwdev->chip->rf_path_num;
20959b43bd1aSZong-Zhe Yang 	static const u8 rs[] = {
20969b43bd1aSZong-Zhe Yang 		RTW89_RS_CCK,
20979b43bd1aSZong-Zhe Yang 		RTW89_RS_OFDM,
20989b43bd1aSZong-Zhe Yang 		RTW89_RS_MCS,
20999b43bd1aSZong-Zhe Yang 		RTW89_RS_HEDCM,
21009b43bd1aSZong-Zhe Yang 	};
21019b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc cur;
21029b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
21039b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21049b43bd1aSZong-Zhe Yang 	u32 addr, val;
21059b43bd1aSZong-Zhe Yang 	s8 v[4] = {};
21069b43bd1aSZong-Zhe Yang 	u8 i;
21079b43bd1aSZong-Zhe Yang 
21089b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21099b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr byrate with ch=%d\n", ch);
21109b43bd1aSZong-Zhe Yang 
2111b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_CCK] % 4);
2112b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_OFDM] % 4);
2113b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_MCS] % 4);
2114b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_HEDCM] % 4);
21159b43bd1aSZong-Zhe Yang 
21169b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_BY_RATE;
2117ddf9a2eaSZong-Zhe Yang 	for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) {
21189b43bd1aSZong-Zhe Yang 		for (i = 0; i < ARRAY_SIZE(rs); i++) {
2119b4a283fbSZong-Zhe Yang 			if (cur.nss >= rtw89_rs_nss_num[rs[i]])
21209b43bd1aSZong-Zhe Yang 				continue;
21219b43bd1aSZong-Zhe Yang 
21229b43bd1aSZong-Zhe Yang 			cur.rs = rs[i];
2123b4a283fbSZong-Zhe Yang 			for (cur.idx = 0; cur.idx < rtw89_rs_idx_num[rs[i]];
21249b43bd1aSZong-Zhe Yang 			     cur.idx++) {
21259b43bd1aSZong-Zhe Yang 				v[cur.idx % 4] =
21269b43bd1aSZong-Zhe Yang 					rtw89_phy_read_txpwr_byrate(rtwdev,
21279b43bd1aSZong-Zhe Yang 								    band,
21289b43bd1aSZong-Zhe Yang 								    &cur);
21299b43bd1aSZong-Zhe Yang 
21309b43bd1aSZong-Zhe Yang 				if ((cur.idx + 1) % 4)
21319b43bd1aSZong-Zhe Yang 					continue;
21329b43bd1aSZong-Zhe Yang 
21339b43bd1aSZong-Zhe Yang 				val = FIELD_PREP(GENMASK(7, 0), v[0]) |
21349b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(15, 8), v[1]) |
21359b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(23, 16), v[2]) |
21369b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(31, 24), v[3]);
21379b43bd1aSZong-Zhe Yang 
21389b43bd1aSZong-Zhe Yang 				rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr,
21399b43bd1aSZong-Zhe Yang 							val);
21409b43bd1aSZong-Zhe Yang 				addr += 4;
21419b43bd1aSZong-Zhe Yang 			}
21429b43bd1aSZong-Zhe Yang 		}
21439b43bd1aSZong-Zhe Yang 	}
21449b43bd1aSZong-Zhe Yang }
21459b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate);
21469b43bd1aSZong-Zhe Yang 
21479b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev,
21489b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
21499b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
21509b43bd1aSZong-Zhe Yang {
21519b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc desc = {
21529b43bd1aSZong-Zhe Yang 		.nss = RTW89_NSS_1,
21539b43bd1aSZong-Zhe Yang 		.rs = RTW89_RS_OFFSET,
21549b43bd1aSZong-Zhe Yang 	};
21559b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
2156b4a283fbSZong-Zhe Yang 	s8 v[RTW89_RATE_OFFSET_NUM] = {};
21579b43bd1aSZong-Zhe Yang 	u32 val;
21589b43bd1aSZong-Zhe Yang 
21599b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n");
21609b43bd1aSZong-Zhe Yang 
2161b4a283fbSZong-Zhe Yang 	for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_NUM; desc.idx++)
21629b43bd1aSZong-Zhe Yang 		v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc);
21639b43bd1aSZong-Zhe Yang 
2164b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(RTW89_RATE_OFFSET_NUM != 5);
21659b43bd1aSZong-Zhe Yang 	val = FIELD_PREP(GENMASK(3, 0), v[0]) |
21669b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(7, 4), v[1]) |
21679b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(11, 8), v[2]) |
21689b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(15, 12), v[3]) |
21699b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(19, 16), v[4]);
21709b43bd1aSZong-Zhe Yang 
21719b43bd1aSZong-Zhe Yang 	rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL,
21729b43bd1aSZong-Zhe Yang 				     GENMASK(19, 0), val);
21739b43bd1aSZong-Zhe Yang }
21749b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset);
21759b43bd1aSZong-Zhe Yang 
21769b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev,
21779b43bd1aSZong-Zhe Yang 			       const struct rtw89_chan *chan,
21789b43bd1aSZong-Zhe Yang 			       enum rtw89_phy_idx phy_idx)
21799b43bd1aSZong-Zhe Yang {
2180ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
21819b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit lmt;
21829b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21839b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
21849b43bd1aSZong-Zhe Yang 	const s8 *ptr;
21859b43bd1aSZong-Zhe Yang 	u32 addr, val;
21869b43bd1aSZong-Zhe Yang 	u8 i, j;
21879b43bd1aSZong-Zhe Yang 
21889b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21899b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw);
21909b43bd1aSZong-Zhe Yang 
21919b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) !=
21929b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_PAGE_SIZE);
21939b43bd1aSZong-Zhe Yang 
21949b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_LMT;
2195ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
21969b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i);
21979b43bd1aSZong-Zhe Yang 
21989b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt;
21999b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE;
22009b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
22019b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
22029b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
22039b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
22049b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
22059b43bd1aSZong-Zhe Yang 
22069b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
22079b43bd1aSZong-Zhe Yang 		}
22089b43bd1aSZong-Zhe Yang 	}
22099b43bd1aSZong-Zhe Yang }
22109b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit);
22119b43bd1aSZong-Zhe Yang 
22129b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev,
22139b43bd1aSZong-Zhe Yang 				  const struct rtw89_chan *chan,
22149b43bd1aSZong-Zhe Yang 				  enum rtw89_phy_idx phy_idx)
22159b43bd1aSZong-Zhe Yang {
2216ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
22179b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit_ru lmt_ru;
22189b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
22199b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
22209b43bd1aSZong-Zhe Yang 	const s8 *ptr;
22219b43bd1aSZong-Zhe Yang 	u32 addr, val;
22229b43bd1aSZong-Zhe Yang 	u8 i, j;
22239b43bd1aSZong-Zhe Yang 
22249b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
22259b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw);
22269b43bd1aSZong-Zhe Yang 
22279b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) !=
22289b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_RU_PAGE_SIZE);
22299b43bd1aSZong-Zhe Yang 
22309b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_RU_LMT;
2231ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
22329b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i);
22339b43bd1aSZong-Zhe Yang 
22349b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt_ru;
22359b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE;
22369b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
22379b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
22389b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
22399b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
22409b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
22419b43bd1aSZong-Zhe Yang 
22429b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
22439b43bd1aSZong-Zhe Yang 		}
22449b43bd1aSZong-Zhe Yang 	}
22459b43bd1aSZong-Zhe Yang }
22469b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru);
2247e3ec7017SPing-Ke Shih 
2248e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data {
2249e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
2250e3ec7017SPing-Ke Shih 	struct sk_buff *c2h;
2251e3ec7017SPing-Ke Shih };
2252e3ec7017SPing-Ke Shih 
2253e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta)
2254e3ec7017SPing-Ke Shih {
2255e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data;
2256e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = ra_data->rtwdev;
2257e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
225857cafeb1SPing-Ke Shih 	const struct rtw89_c2h_ra_rpt *c2h =
225957cafeb1SPing-Ke Shih 		(const struct rtw89_c2h_ra_rpt *)ra_data->c2h->data;
2260e3ec7017SPing-Ke Shih 	struct rtw89_ra_report *ra_report = &rtwsta->ra_report;
22615c152231SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
22625c152231SPing-Ke Shih 	bool format_v1 = chip->chip_gen == RTW89_CHIP_BE;
2263e3ec7017SPing-Ke Shih 	u8 mode, rate, bw, giltf, mac_id;
22649a3a593cSPing-Ke Shih 	u16 legacy_bitrate;
22659a3a593cSPing-Ke Shih 	bool valid;
22660d466f05SPing-Ke Shih 	u8 mcs = 0;
22675c152231SPing-Ke Shih 	u8 t;
2268e3ec7017SPing-Ke Shih 
226957cafeb1SPing-Ke Shih 	mac_id = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MACID);
2270e3ec7017SPing-Ke Shih 	if (mac_id != rtwsta->mac_id)
2271e3ec7017SPing-Ke Shih 		return;
2272e3ec7017SPing-Ke Shih 
227357cafeb1SPing-Ke Shih 	rate = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MCSNSS);
227457cafeb1SPing-Ke Shih 	bw = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW);
227557cafeb1SPing-Ke Shih 	giltf = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_GILTF);
227657cafeb1SPing-Ke Shih 	mode = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL);
2277e3ec7017SPing-Ke Shih 
22785c152231SPing-Ke Shih 	if (format_v1) {
22795c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MCSNSS_B7);
22805c152231SPing-Ke Shih 		rate |= u8_encode_bits(t, BIT(7));
22815c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW_B2);
22825c152231SPing-Ke Shih 		bw |= u8_encode_bits(t, BIT(2));
22835c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL_B2);
22845c152231SPing-Ke Shih 		mode |= u8_encode_bits(t, BIT(2));
22855c152231SPing-Ke Shih 	}
22865c152231SPing-Ke Shih 
22879a3a593cSPing-Ke Shih 	if (mode == RTW89_RA_RPT_MODE_LEGACY) {
22889a3a593cSPing-Ke Shih 		valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate);
22899a3a593cSPing-Ke Shih 		if (!valid)
22909a3a593cSPing-Ke Shih 			return;
22919a3a593cSPing-Ke Shih 	}
22929a3a593cSPing-Ke Shih 
22930d466f05SPing-Ke Shih 	memset(&ra_report->txrate, 0, sizeof(ra_report->txrate));
22949a3a593cSPing-Ke Shih 
2295e3ec7017SPing-Ke Shih 	switch (mode) {
2296e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_LEGACY:
22979a3a593cSPing-Ke Shih 		ra_report->txrate.legacy = legacy_bitrate;
2298e3ec7017SPing-Ke Shih 		break;
2299e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HT:
2300e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS;
230111fe4ccdSZong-Zhe Yang 		if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw))
2302e3ec7017SPing-Ke Shih 			rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate),
2303e3ec7017SPing-Ke Shih 						FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate));
2304e3ec7017SPing-Ke Shih 		else
2305e3ec7017SPing-Ke Shih 			rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate);
2306e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = rate;
2307e3ec7017SPing-Ke Shih 		if (giltf)
2308e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
23090d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs & 0x07;
2310e3ec7017SPing-Ke Shih 		break;
2311e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_VHT:
2312e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
23135c152231SPing-Ke Shih 		ra_report->txrate.mcs = format_v1 ?
23145c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) :
23155c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS);
23165c152231SPing-Ke Shih 		ra_report->txrate.nss = format_v1 ?
23175c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 :
23185c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1;
2319e3ec7017SPing-Ke Shih 		if (giltf)
2320e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
23210d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2322e3ec7017SPing-Ke Shih 		break;
2323e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HE:
2324e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS;
23255c152231SPing-Ke Shih 		ra_report->txrate.mcs = format_v1 ?
23265c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) :
23275c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS);
23285c152231SPing-Ke Shih 		ra_report->txrate.nss  = format_v1 ?
23295c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 :
23305c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1;
2331e3ec7017SPing-Ke Shih 		if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08)
2332e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2333e3ec7017SPing-Ke Shih 		else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16)
2334e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2335e3ec7017SPing-Ke Shih 		else
2336e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2;
23370d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2338e3ec7017SPing-Ke Shih 		break;
2339e3ec7017SPing-Ke Shih 	}
2340e3ec7017SPing-Ke Shih 
2341167044afSPing-Ke Shih 	ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw);
2342e3ec7017SPing-Ke Shih 	ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate);
23435c152231SPing-Ke Shih 	ra_report->hw_rate = format_v1 ?
23445c152231SPing-Ke Shih 			     u16_encode_bits(mode, RTW89_HW_RATE_V1_MASK_MOD) |
23455c152231SPing-Ke Shih 			     u16_encode_bits(rate, RTW89_HW_RATE_V1_MASK_VAL) :
23465c152231SPing-Ke Shih 			     u16_encode_bits(mode, RTW89_HW_RATE_MASK_MOD) |
23475c152231SPing-Ke Shih 			     u16_encode_bits(rate, RTW89_HW_RATE_MASK_VAL);
23480d466f05SPing-Ke Shih 	ra_report->might_fallback_legacy = mcs <= 2;
23494c51541dSBenjamin Berg 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report);
23504c51541dSBenjamin Berg 	rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1;
2351e3ec7017SPing-Ke Shih }
2352e3ec7017SPing-Ke Shih 
2353e3ec7017SPing-Ke Shih static void
2354e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
2355e3ec7017SPing-Ke Shih {
2356e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data ra_data;
2357e3ec7017SPing-Ke Shih 
2358e3ec7017SPing-Ke Shih 	ra_data.rtwdev = rtwdev;
2359e3ec7017SPing-Ke Shih 	ra_data.c2h = c2h;
2360e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2361e3ec7017SPing-Ke Shih 					  rtw89_phy_c2h_ra_rpt_iter,
2362e3ec7017SPing-Ke Shih 					  &ra_data);
2363e3ec7017SPing-Ke Shih }
2364e3ec7017SPing-Ke Shih 
2365e3ec7017SPing-Ke Shih static
2366e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev,
2367e3ec7017SPing-Ke Shih 					  struct sk_buff *c2h, u32 len) = {
2368e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt,
2369e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL,
2370e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_TXSTS] = NULL,
2371e3ec7017SPing-Ke Shih };
2372e3ec7017SPing-Ke Shih 
2373e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
2374e3ec7017SPing-Ke Shih 			  u32 len, u8 class, u8 func)
2375e3ec7017SPing-Ke Shih {
2376e3ec7017SPing-Ke Shih 	void (*handler)(struct rtw89_dev *rtwdev,
2377e3ec7017SPing-Ke Shih 			struct sk_buff *c2h, u32 len) = NULL;
2378e3ec7017SPing-Ke Shih 
2379e3ec7017SPing-Ke Shih 	switch (class) {
2380e3ec7017SPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_RA:
2381e3ec7017SPing-Ke Shih 		if (func < RTW89_PHY_C2H_FUNC_RA_MAX)
2382e3ec7017SPing-Ke Shih 			handler = rtw89_phy_c2h_ra_handler[func];
2383e3ec7017SPing-Ke Shih 		break;
23843b66519bSPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_DM:
23853b66519bSPing-Ke Shih 		if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY)
23863b66519bSPing-Ke Shih 			return;
23873b66519bSPing-Ke Shih 		fallthrough;
2388e3ec7017SPing-Ke Shih 	default:
2389e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
2390e3ec7017SPing-Ke Shih 		return;
2391e3ec7017SPing-Ke Shih 	}
2392e3ec7017SPing-Ke Shih 	if (!handler) {
2393e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
2394e3ec7017SPing-Ke Shih 			   func);
2395e3ec7017SPing-Ke Shih 		return;
2396e3ec7017SPing-Ke Shih 	}
2397e3ec7017SPing-Ke Shih 	handler(rtwdev, skb, len);
2398e3ec7017SPing-Ke Shih }
2399e3ec7017SPing-Ke Shih 
2400e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo)
2401e3ec7017SPing-Ke Shih {
24020789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2403e3ec7017SPing-Ke Shih 	u32 reg_mask;
2404e3ec7017SPing-Ke Shih 
2405e3ec7017SPing-Ke Shih 	if (sc_xo)
24060789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2407e3ec7017SPing-Ke Shih 	else
24080789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2409e3ec7017SPing-Ke Shih 
24100789881aSChia-Yuan Li 	return (u8)rtw89_read32_mask(rtwdev, xtal->xcap_reg, reg_mask);
2411e3ec7017SPing-Ke Shih }
2412e3ec7017SPing-Ke Shih 
2413e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo,
2414e3ec7017SPing-Ke Shih 				       u8 val)
2415e3ec7017SPing-Ke Shih {
24160789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2417e3ec7017SPing-Ke Shih 	u32 reg_mask;
2418e3ec7017SPing-Ke Shih 
2419e3ec7017SPing-Ke Shih 	if (sc_xo)
24200789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2421e3ec7017SPing-Ke Shih 	else
24220789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2423e3ec7017SPing-Ke Shih 
24240789881aSChia-Yuan Li 	rtw89_write32_mask(rtwdev, xtal->xcap_reg, reg_mask, val);
2425e3ec7017SPing-Ke Shih }
2426e3ec7017SPing-Ke Shih 
2427e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev,
2428e3ec7017SPing-Ke Shih 					  u8 crystal_cap, bool force)
2429e3ec7017SPing-Ke Shih {
2430e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
24318379fa61SYuan-Han Zhang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2432e3ec7017SPing-Ke Shih 	u8 sc_xi_val, sc_xo_val;
2433e3ec7017SPing-Ke Shih 
2434e3ec7017SPing-Ke Shih 	if (!force && cfo->crystal_cap == crystal_cap)
2435e3ec7017SPing-Ke Shih 		return;
2436e3ec7017SPing-Ke Shih 	crystal_cap = clamp_t(u8, crystal_cap, 0, 127);
24370789881aSChia-Yuan Li 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8851B) {
2438e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap);
2439e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap);
2440e3ec7017SPing-Ke Shih 		sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true);
2441e3ec7017SPing-Ke Shih 		sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false);
24428379fa61SYuan-Han Zhang 	} else {
24438379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO,
24448379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XO_MASK);
24458379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI,
24468379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XI_MASK);
24478379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val);
24488379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val);
24498379fa61SYuan-Han Zhang 	}
2450e3ec7017SPing-Ke Shih 	cfo->crystal_cap = sc_xi_val;
2451e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap);
2452e3ec7017SPing-Ke Shih 
2453e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val);
2454e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val);
2455e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n",
2456e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2457e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n");
2458e3ec7017SPing-Ke Shih }
2459e3ec7017SPing-Ke Shih 
2460e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev)
2461e3ec7017SPing-Ke Shih {
2462e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2463e3ec7017SPing-Ke Shih 	u8 cap;
2464e3ec7017SPing-Ke Shih 
2465e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK;
2466e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2467e3ec7017SPing-Ke Shih 	if (cfo->crystal_cap == cfo->def_x_cap)
2468e3ec7017SPing-Ke Shih 		return;
2469e3ec7017SPing-Ke Shih 	cap = cfo->crystal_cap;
2470e3ec7017SPing-Ke Shih 	cap += (cap > cfo->def_x_cap ? -1 : 1);
2471e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false);
2472e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2473e3ec7017SPing-Ke Shih 		    "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap,
2474e3ec7017SPing-Ke Shih 		    cfo->def_x_cap);
2475e3ec7017SPing-Ke Shih }
2476e3ec7017SPing-Ke Shih 
2477e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo)
2478e3ec7017SPing-Ke Shih {
2479b7379148SYuan-Han Zhang 	const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp;
2480e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
2481e3ec7017SPing-Ke Shih 	s32 cfo_avg_312;
2482b7379148SYuan-Han Zhang 	s32 dcfo_comp_val;
2483e3ec7017SPing-Ke Shih 	int sign;
2484e3ec7017SPing-Ke Shih 
2485e3ec7017SPing-Ke Shih 	if (!is_linked) {
2486e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n",
2487e3ec7017SPing-Ke Shih 			    is_linked);
2488e3ec7017SPing-Ke Shih 		return;
2489e3ec7017SPing-Ke Shih 	}
2490e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo);
2491e3ec7017SPing-Ke Shih 	if (curr_cfo == 0)
2492e3ec7017SPing-Ke Shih 		return;
2493b7379148SYuan-Han Zhang 	dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO);
2494e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
24959f9882dbSEric Huang 	cfo_avg_312 = curr_cfo / 625 + sign * dcfo_comp_val;
24969f9882dbSEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "avg_cfo_312=%d step\n", cfo_avg_312);
2497e3ec7017SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV)
2498e3ec7017SPing-Ke Shih 		cfo_avg_312 = -cfo_avg_312;
2499b7379148SYuan-Han Zhang 	rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask,
2500e3ec7017SPing-Ke Shih 			       cfo_avg_312);
2501e3ec7017SPing-Ke Shih }
2502e3ec7017SPing-Ke Shih 
2503e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev)
2504e3ec7017SPing-Ke Shih {
25059f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
25069f9882dbSEric Huang 
2507e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1);
2508e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8);
25099f9882dbSEric Huang 
25109f9882dbSEric Huang 	if (chip->cfo_hw_comp)
25119f9882dbSEric Huang 		rtw89_write32_mask(rtwdev, R_AX_PWR_UL_CTRL2,
25129f9882dbSEric Huang 				   B_AX_PWR_UL_CFO_MASK, 0x6);
25139f9882dbSEric Huang 	else
2514e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK);
2515e3ec7017SPing-Ke Shih }
2516e3ec7017SPing-Ke Shih 
2517e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev)
2518e3ec7017SPing-Ke Shih {
2519e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2520e3ec7017SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
2521e3ec7017SPing-Ke Shih 
2522e3ec7017SPing-Ke Shih 	cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK;
2523e3ec7017SPing-Ke Shih 	cfo->crystal_cap = cfo->crystal_cap_default;
2524e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap;
2525a9e06f2eSYi-Tang Chiu 	cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f);
2526a9e06f2eSYi-Tang Chiu 	cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1);
2527e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2528a9e06f2eSYi-Tang Chiu 	cfo->divergence_lock_en = false;
2529e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = 0;
2530a9e06f2eSYi-Tang Chiu 	cfo->lock_cnt = 0;
2531e3ec7017SPing-Ke Shih 	cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE;
2532e3ec7017SPing-Ke Shih 	cfo->apply_compensation = false;
2533e3ec7017SPing-Ke Shih 	cfo->residual_cfo_acc = 0;
2534e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n",
2535e3ec7017SPing-Ke Shih 		    cfo->crystal_cap_default);
2536e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true);
2537e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1);
2538e3ec7017SPing-Ke Shih 	rtw89_dcfo_comp_init(rtwdev);
2539e3ec7017SPing-Ke Shih 	cfo->cfo_timer_ms = 2000;
2540e3ec7017SPing-Ke Shih 	cfo->cfo_trig_by_timer_en = false;
2541e3ec7017SPing-Ke Shih 	cfo->phy_cfo_trk_cnt = 0;
2542e3ec7017SPing-Ke Shih 	cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2543bc013052SEric Huang 	cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE;
2544e3ec7017SPing-Ke Shih }
2545e3ec7017SPing-Ke Shih 
2546e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev,
2547e3ec7017SPing-Ke Shih 					     s32 curr_cfo)
2548e3ec7017SPing-Ke Shih {
2549e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2550e3ec7017SPing-Ke Shih 	s8 crystal_cap = cfo->crystal_cap;
2551e3ec7017SPing-Ke Shih 	s32 cfo_abs = abs(curr_cfo);
2552e3ec7017SPing-Ke Shih 	int sign;
2553e3ec7017SPing-Ke Shih 
2554e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2555e3ec7017SPing-Ke Shih 		if (cfo_abs > CFO_TRK_ENABLE_TH)
2556e3ec7017SPing-Ke Shih 			cfo->is_adjust = true;
2557e3ec7017SPing-Ke Shih 	} else {
2558e3ec7017SPing-Ke Shih 		if (cfo_abs < CFO_TRK_STOP_TH)
2559e3ec7017SPing-Ke Shih 			cfo->is_adjust = false;
2560e3ec7017SPing-Ke Shih 	}
2561e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2562e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n");
2563e3ec7017SPing-Ke Shih 		return;
2564e3ec7017SPing-Ke Shih 	}
2565e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
2566e3ec7017SPing-Ke Shih 	if (cfo_abs > CFO_TRK_STOP_TH_4)
2567e3ec7017SPing-Ke Shih 		crystal_cap += 7 * sign;
2568e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_3)
2569e3ec7017SPing-Ke Shih 		crystal_cap += 5 * sign;
2570e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_2)
2571e3ec7017SPing-Ke Shih 		crystal_cap += 3 * sign;
2572e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_1)
2573e3ec7017SPing-Ke Shih 		crystal_cap += 1 * sign;
2574e3ec7017SPing-Ke Shih 	else
2575e3ec7017SPing-Ke Shih 		return;
2576e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false);
2577e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2578e3ec7017SPing-Ke Shih 		    "X_cap{Curr,Default}={0x%x,0x%x}\n",
2579e3ec7017SPing-Ke Shih 		    cfo->crystal_cap, cfo->def_x_cap);
2580e3ec7017SPing-Ke Shih }
2581e3ec7017SPing-Ke Shih 
2582e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev)
2583e3ec7017SPing-Ke Shih {
25849f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2585e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2586e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2587e3ec7017SPing-Ke Shih 	s32 cfo_cnt_all = 0;
2588e3ec7017SPing-Ke Shih 	s32 cfo_all_avg = 0;
2589e3ec7017SPing-Ke Shih 	u8 i;
2590e3ec7017SPing-Ke Shih 
2591e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc != 1)
2592e3ec7017SPing-Ke Shih 		return 0;
2593e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n");
2594e3ec7017SPing-Ke Shih 	for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2595e3ec7017SPing-Ke Shih 		if (cfo->cfo_cnt[i] == 0)
2596e3ec7017SPing-Ke Shih 			continue;
2597e3ec7017SPing-Ke Shih 		cfo_khz_all += cfo->cfo_tail[i];
2598e3ec7017SPing-Ke Shih 		cfo_cnt_all += cfo->cfo_cnt[i];
2599e3ec7017SPing-Ke Shih 		cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all);
2600e3ec7017SPing-Ke Shih 		cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
26019f9882dbSEric Huang 		cfo->dcfo_avg = phy_div(cfo_khz_all << chip->dcfo_comp_sft,
26029f9882dbSEric Huang 					cfo_cnt_all);
2603e3ec7017SPing-Ke Shih 	}
2604e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2605e3ec7017SPing-Ke Shih 		    "CFO track for macid = %d\n", i);
2606e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2607e3ec7017SPing-Ke Shih 		    "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n",
2608e3ec7017SPing-Ke Shih 		    cfo_khz_all, cfo_cnt_all, cfo_all_avg);
2609e3ec7017SPing-Ke Shih 	return cfo_all_avg;
2610e3ec7017SPing-Ke Shih }
2611e3ec7017SPing-Ke Shih 
2612e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev)
2613e3ec7017SPing-Ke Shih {
2614e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2615e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2616e3ec7017SPing-Ke Shih 	s32 target_cfo = 0;
2617e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2618e3ec7017SPing-Ke Shih 	s32 cfo_khz_all_tp_wgt = 0;
2619e3ec7017SPing-Ke Shih 	s32 cfo_avg = 0;
2620e3ec7017SPing-Ke Shih 	s32 max_cfo_lb = BIT(31);
2621e3ec7017SPing-Ke Shih 	s32 min_cfo_ub = GENMASK(30, 0);
2622e3ec7017SPing-Ke Shih 	u16 cfo_cnt_all = 0;
2623e3ec7017SPing-Ke Shih 	u8 active_entry_cnt = 0;
2624e3ec7017SPing-Ke Shih 	u8 sta_cnt = 0;
2625e3ec7017SPing-Ke Shih 	u32 tp_all = 0;
2626e3ec7017SPing-Ke Shih 	u8 i;
2627e3ec7017SPing-Ke Shih 	u8 cfo_tol = 0;
2628e3ec7017SPing-Ke Shih 
2629e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n");
2630e3ec7017SPing-Ke Shih 	if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) {
2631e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n");
2632e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2633e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2634e3ec7017SPing-Ke Shih 				continue;
2635e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_tail[i];
2636e3ec7017SPing-Ke Shih 			cfo_cnt_all += cfo->cfo_cnt[i];
2637e3ec7017SPing-Ke Shih 			cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all);
2638e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2639e3ec7017SPing-Ke Shih 				    "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n",
2640e3ec7017SPing-Ke Shih 				    cfo_khz_all, cfo_cnt_all, cfo_avg);
2641e3ec7017SPing-Ke Shih 			target_cfo = cfo_avg;
2642e3ec7017SPing-Ke Shih 		}
2643e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) {
2644e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n");
2645e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2646e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2647e3ec7017SPing-Ke Shih 				continue;
2648e3ec7017SPing-Ke Shih 			cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2649e3ec7017SPing-Ke Shih 						  (s32)cfo->cfo_cnt[i]);
2650e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2651e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2652e3ec7017SPing-Ke Shih 				    "Macid=%d, cfo_avg=%d\n", i,
2653e3ec7017SPing-Ke Shih 				    cfo->cfo_avg[i]);
2654e3ec7017SPing-Ke Shih 		}
2655e3ec7017SPing-Ke Shih 		sta_cnt = rtwdev->total_sta_assoc;
2656e3ec7017SPing-Ke Shih 		cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt);
2657e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2658e3ec7017SPing-Ke Shih 			    "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n",
2659e3ec7017SPing-Ke Shih 			    cfo_khz_all, sta_cnt, cfo_avg);
2660e3ec7017SPing-Ke Shih 		target_cfo = cfo_avg;
2661e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) {
2662e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n");
2663e3ec7017SPing-Ke Shih 		cfo_tol = cfo->sta_cfo_tolerance;
2664e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2665e3ec7017SPing-Ke Shih 			sta_cnt++;
2666e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] != 0) {
2667e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2668e3ec7017SPing-Ke Shih 							  (s32)cfo->cfo_cnt[i]);
2669e3ec7017SPing-Ke Shih 				active_entry_cnt++;
2670e3ec7017SPing-Ke Shih 			} else {
2671e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = cfo->pre_cfo_avg[i];
2672e3ec7017SPing-Ke Shih 			}
2673e3ec7017SPing-Ke Shih 			max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb);
2674e3ec7017SPing-Ke Shih 			min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub);
2675e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2676e3ec7017SPing-Ke Shih 			/* need tp for each entry */
2677e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2678e3ec7017SPing-Ke Shih 				    "[%d] cfo_avg=%d, tp=tbd\n",
2679e3ec7017SPing-Ke Shih 				    i, cfo->cfo_avg[i]);
2680e3ec7017SPing-Ke Shih 			if (sta_cnt >= rtwdev->total_sta_assoc)
2681e3ec7017SPing-Ke Shih 				break;
2682e3ec7017SPing-Ke Shih 		}
2683e3ec7017SPing-Ke Shih 		tp_all = stats->rx_throughput; /* need tp for each entry */
2684e3ec7017SPing-Ke Shih 		cfo_avg =  phy_div(cfo_khz_all_tp_wgt, (s32)tp_all);
2685e3ec7017SPing-Ke Shih 
2686e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n",
2687e3ec7017SPing-Ke Shih 			    sta_cnt);
2688e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n",
2689e3ec7017SPing-Ke Shih 			    active_entry_cnt);
2690e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2691e3ec7017SPing-Ke Shih 			    "Msta cfo with tp_wgt=%d, avg_cfo=%d\n",
2692e3ec7017SPing-Ke Shih 			    cfo_khz_all_tp_wgt, cfo_avg);
2693e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n",
2694e3ec7017SPing-Ke Shih 			    max_cfo_lb, min_cfo_ub);
2695e3ec7017SPing-Ke Shih 		if (max_cfo_lb <= min_cfo_ub) {
2696e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2697e3ec7017SPing-Ke Shih 				    "cfo win_size=%d\n",
2698e3ec7017SPing-Ke Shih 				    min_cfo_ub - max_cfo_lb);
2699e3ec7017SPing-Ke Shih 			target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub);
2700e3ec7017SPing-Ke Shih 		} else {
2701e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2702c51ed740SColin Ian King 				    "No intersection of cfo tolerance windows\n");
2703e3ec7017SPing-Ke Shih 			target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt);
2704e3ec7017SPing-Ke Shih 		}
2705e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++)
2706e3ec7017SPing-Ke Shih 			cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2707e3ec7017SPing-Ke Shih 	}
2708e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo);
2709e3ec7017SPing-Ke Shih 	return target_cfo;
2710e3ec7017SPing-Ke Shih }
2711e3ec7017SPing-Ke Shih 
2712e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev)
2713e3ec7017SPing-Ke Shih {
2714e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2715e3ec7017SPing-Ke Shih 
2716e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail));
2717e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt));
2718e3ec7017SPing-Ke Shih 	cfo->packet_count = 0;
2719e3ec7017SPing-Ke Shih 	cfo->packet_count_pre = 0;
2720e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = 0;
2721e3ec7017SPing-Ke Shih }
2722e3ec7017SPing-Ke Shih 
2723e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev)
2724e3ec7017SPing-Ke Shih {
2725e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2726e3ec7017SPing-Ke Shih 	s32 new_cfo = 0;
2727e3ec7017SPing-Ke Shih 	bool x_cap_update = false;
2728e3ec7017SPing-Ke Shih 	u8 pre_x_cap = cfo->crystal_cap;
27299f9882dbSEric Huang 	u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft;
2730e3ec7017SPing-Ke Shih 
27319f9882dbSEric Huang 	cfo->dcfo_avg = 0;
2732e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n",
2733e3ec7017SPing-Ke Shih 		    rtwdev->total_sta_assoc);
2734e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 0) {
2735e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_reset(rtwdev);
2736e3ec7017SPing-Ke Shih 		return;
2737e3ec7017SPing-Ke Shih 	}
2738e3ec7017SPing-Ke Shih 	if (cfo->packet_count == 0) {
2739e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n");
2740e3ec7017SPing-Ke Shih 		return;
2741e3ec7017SPing-Ke Shih 	}
2742e3ec7017SPing-Ke Shih 	if (cfo->packet_count == cfo->packet_count_pre) {
2743e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n");
2744e3ec7017SPing-Ke Shih 		return;
2745e3ec7017SPing-Ke Shih 	}
2746e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 1)
2747e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_average_cfo_calc(rtwdev);
2748e3ec7017SPing-Ke Shih 	else
2749e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev);
2750e3ec7017SPing-Ke Shih 	if (new_cfo == 0) {
2751e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n");
2752e3ec7017SPing-Ke Shih 		return;
2753e3ec7017SPing-Ke Shih 	}
2754a9e06f2eSYi-Tang Chiu 	if (cfo->divergence_lock_en) {
2755a9e06f2eSYi-Tang Chiu 		cfo->lock_cnt++;
2756a9e06f2eSYi-Tang Chiu 		if (cfo->lock_cnt > CFO_PERIOD_CNT) {
2757a9e06f2eSYi-Tang Chiu 			cfo->divergence_lock_en = false;
2758a9e06f2eSYi-Tang Chiu 			cfo->lock_cnt = 0;
2759a9e06f2eSYi-Tang Chiu 		} else {
2760a9e06f2eSYi-Tang Chiu 			rtw89_phy_cfo_reset(rtwdev);
2761a9e06f2eSYi-Tang Chiu 		}
2762a9e06f2eSYi-Tang Chiu 		return;
2763a9e06f2eSYi-Tang Chiu 	}
2764a9e06f2eSYi-Tang Chiu 	if (cfo->crystal_cap >= cfo->x_cap_ub ||
2765a9e06f2eSYi-Tang Chiu 	    cfo->crystal_cap <= cfo->x_cap_lb) {
2766a9e06f2eSYi-Tang Chiu 		cfo->divergence_lock_en = true;
2767a9e06f2eSYi-Tang Chiu 		rtw89_phy_cfo_reset(rtwdev);
2768a9e06f2eSYi-Tang Chiu 		return;
2769a9e06f2eSYi-Tang Chiu 	}
2770a9e06f2eSYi-Tang Chiu 
2771e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo);
2772e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = new_cfo;
27739f9882dbSEric Huang 	cfo->dcfo_avg_pre = cfo->dcfo_avg;
27741646ce8fSYe Guojin 	x_cap_update =  cfo->crystal_cap != pre_x_cap;
2775e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update);
2776e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n",
2777e3ec7017SPing-Ke Shih 		    cfo->def_x_cap, pre_x_cap, cfo->crystal_cap,
2778e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2779e3ec7017SPing-Ke Shih 	if (x_cap_update) {
27809f9882dbSEric Huang 		if (cfo->dcfo_avg > 0)
27819f9882dbSEric Huang 			cfo->dcfo_avg -= CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2782e3ec7017SPing-Ke Shih 		else
27839f9882dbSEric Huang 			cfo->dcfo_avg += CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2784e3ec7017SPing-Ke Shih 	}
27859f9882dbSEric Huang 	rtw89_dcfo_comp(rtwdev, cfo->dcfo_avg);
2786e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_statistics_reset(rtwdev);
2787e3ec7017SPing-Ke Shih }
2788e3ec7017SPing-Ke Shih 
2789e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work)
2790e3ec7017SPing-Ke Shih {
2791e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2792e3ec7017SPing-Ke Shih 						cfo_track_work.work);
2793e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2794e3ec7017SPing-Ke Shih 
2795e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
2796e3ec7017SPing-Ke Shih 	if (!cfo->cfo_trig_by_timer_en)
2797e3ec7017SPing-Ke Shih 		goto out;
2798e3ec7017SPing-Ke Shih 	rtw89_leave_ps_mode(rtwdev);
2799e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2800e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2801e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2802e3ec7017SPing-Ke Shih out:
2803e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
2804e3ec7017SPing-Ke Shih }
2805e3ec7017SPing-Ke Shih 
2806e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev)
2807e3ec7017SPing-Ke Shih {
2808e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2809e3ec7017SPing-Ke Shih 
2810e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2811e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2812e3ec7017SPing-Ke Shih }
2813e3ec7017SPing-Ke Shih 
2814e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev)
2815e3ec7017SPing-Ke Shih {
2816e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2817e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2818bc013052SEric Huang 	bool is_ul_ofdma = false, ofdma_acc_en = false;
2819bc013052SEric Huang 
2820bc013052SEric Huang 	if (stats->rx_tf_periodic > CFO_TF_CNT_TH)
2821bc013052SEric Huang 		is_ul_ofdma = true;
2822bc013052SEric Huang 	if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE &&
2823bc013052SEric Huang 	    is_ul_ofdma)
2824bc013052SEric Huang 		ofdma_acc_en = true;
2825e3ec7017SPing-Ke Shih 
2826e3ec7017SPing-Ke Shih 	switch (cfo->phy_cfo_status) {
2827e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_NORMAL:
2828e3ec7017SPing-Ke Shih 		if (stats->tx_throughput >= CFO_TP_UPPER) {
2829e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE;
2830e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = true;
2831e3ec7017SPing-Ke Shih 			cfo->cfo_timer_ms = CFO_COMP_PERIOD;
2832e3ec7017SPing-Ke Shih 			rtw89_phy_cfo_start_work(rtwdev);
2833e3ec7017SPing-Ke Shih 		}
2834e3ec7017SPing-Ke Shih 		break;
2835e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_ENHANCE:
2836bc013052SEric Huang 		if (stats->tx_throughput <= CFO_TP_LOWER)
2837bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2838bc013052SEric Huang 		else if (ofdma_acc_en &&
2839bc013052SEric Huang 			 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT)
2840bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD;
2841bc013052SEric Huang 		else
2842bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2843bc013052SEric Huang 
2844bc013052SEric Huang 		if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) {
2845e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2846e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2847e3ec7017SPing-Ke Shih 		}
2848bc013052SEric Huang 		break;
2849bc013052SEric Huang 	case RTW89_PHY_DCFO_STATE_HOLD:
2850e3ec7017SPing-Ke Shih 		if (stats->tx_throughput <= CFO_TP_LOWER) {
2851e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2852e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2853e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2854bc013052SEric Huang 		} else {
2855bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2856e3ec7017SPing-Ke Shih 		}
2857e3ec7017SPing-Ke Shih 		break;
2858e3ec7017SPing-Ke Shih 	default:
2859e3ec7017SPing-Ke Shih 		cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2860e3ec7017SPing-Ke Shih 		cfo->phy_cfo_trk_cnt = 0;
2861e3ec7017SPing-Ke Shih 		break;
2862e3ec7017SPing-Ke Shih 	}
2863e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2864e3ec7017SPing-Ke Shih 		    "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n",
2865e3ec7017SPing-Ke Shih 		    stats->tx_throughput, cfo->phy_cfo_status,
2866e3ec7017SPing-Ke Shih 		    cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt,
2867e3ec7017SPing-Ke Shih 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[0]));
2868e3ec7017SPing-Ke Shih 	if (cfo->cfo_trig_by_timer_en)
2869e3ec7017SPing-Ke Shih 		return;
2870e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2871e3ec7017SPing-Ke Shih }
2872e3ec7017SPing-Ke Shih 
2873e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val,
2874e3ec7017SPing-Ke Shih 			 struct rtw89_rx_phy_ppdu *phy_ppdu)
2875e3ec7017SPing-Ke Shih {
2876e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2877e3ec7017SPing-Ke Shih 	u8 macid = phy_ppdu->mac_id;
2878e3ec7017SPing-Ke Shih 
287997df8587SPing-Ke Shih 	if (macid >= CFO_TRACK_MAX_USER) {
288097df8587SPing-Ke Shih 		rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid);
288197df8587SPing-Ke Shih 		return;
288297df8587SPing-Ke Shih 	}
288397df8587SPing-Ke Shih 
2884e3ec7017SPing-Ke Shih 	cfo->cfo_tail[macid] += cfo_val;
2885e3ec7017SPing-Ke Shih 	cfo->cfo_cnt[macid]++;
2886e3ec7017SPing-Ke Shih 	cfo->packet_count++;
2887e3ec7017SPing-Ke Shih }
2888e3ec7017SPing-Ke Shih 
288929136c95SEric Huang void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
289029136c95SEric Huang {
289129136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
289229136c95SEric Huang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
289329136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
289429136c95SEric Huang 
289529136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
289629136c95SEric Huang 		return;
289729136c95SEric Huang 
289829136c95SEric Huang 	rtwvif->def_tri_idx =
289929136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG);
290029136c95SEric Huang 
290129136c95SEric Huang 	if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV)
290229136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
290329136c95SEric Huang 	else if (chan->band_type >= RTW89_BAND_5G &&
290429136c95SEric Huang 		 chan->band_width >= RTW89_CHANNEL_WIDTH_40)
290529136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = true;
290629136c95SEric Huang 	else
290729136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
290829136c95SEric Huang 
290929136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
291029136c95SEric Huang 		    "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n",
291129136c95SEric Huang 		    ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx);
291229136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
291329136c95SEric Huang 		    "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n",
291429136c95SEric Huang 		    rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en);
291529136c95SEric Huang }
291629136c95SEric Huang 
291729136c95SEric Huang struct rtw89_phy_ul_tb_check_data {
291829136c95SEric Huang 	bool valid;
291929136c95SEric Huang 	bool high_tf_client;
292029136c95SEric Huang 	bool low_tf_client;
292129136c95SEric Huang 	bool dyn_tb_bedge_en;
292229136c95SEric Huang 	u8 def_tri_idx;
292329136c95SEric Huang };
292429136c95SEric Huang 
292529136c95SEric Huang static
292629136c95SEric Huang void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev,
292729136c95SEric Huang 				struct rtw89_vif *rtwvif,
292829136c95SEric Huang 				struct rtw89_phy_ul_tb_check_data *ul_tb_data)
292929136c95SEric Huang {
293029136c95SEric Huang 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
293129136c95SEric Huang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
293229136c95SEric Huang 
293329136c95SEric Huang 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION)
293429136c95SEric Huang 		return;
293529136c95SEric Huang 
293629136c95SEric Huang 	if (!vif->cfg.assoc)
293729136c95SEric Huang 		return;
293829136c95SEric Huang 
293929136c95SEric Huang 	if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH)
294029136c95SEric Huang 		ul_tb_data->high_tf_client = true;
294129136c95SEric Huang 	else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH)
294229136c95SEric Huang 		ul_tb_data->low_tf_client = true;
294329136c95SEric Huang 
294429136c95SEric Huang 	ul_tb_data->valid = true;
294529136c95SEric Huang 	ul_tb_data->def_tri_idx = rtwvif->def_tri_idx;
294629136c95SEric Huang 	ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en;
294729136c95SEric Huang }
294829136c95SEric Huang 
294929136c95SEric Huang void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev)
295029136c95SEric Huang {
295129136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
295229136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
295329136c95SEric Huang 	struct rtw89_phy_ul_tb_check_data ul_tb_data = {};
295429136c95SEric Huang 	struct rtw89_vif *rtwvif;
295529136c95SEric Huang 
295629136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
295729136c95SEric Huang 		return;
295829136c95SEric Huang 
295929136c95SEric Huang 	if (rtwdev->total_sta_assoc != 1)
296029136c95SEric Huang 		return;
296129136c95SEric Huang 
296229136c95SEric Huang 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
296329136c95SEric Huang 		rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data);
296429136c95SEric Huang 
296529136c95SEric Huang 	if (!ul_tb_data.valid)
296629136c95SEric Huang 		return;
296729136c95SEric Huang 
296829136c95SEric Huang 	if (ul_tb_data.dyn_tb_bedge_en) {
296929136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
297029136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0);
297129136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
297229136c95SEric Huang 				    "[ULTB] Turn off if_bandedge\n");
297329136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
297429136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN,
297529136c95SEric Huang 					       ul_tb_info->def_if_bandedge);
297629136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
297729136c95SEric Huang 				    "[ULTB] Set to default if_bandedge = %d\n",
297829136c95SEric Huang 				    ul_tb_info->def_if_bandedge);
297929136c95SEric Huang 		}
298029136c95SEric Huang 	}
298129136c95SEric Huang 
298229136c95SEric Huang 	if (ul_tb_info->dyn_tb_tri_en) {
298329136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
298429136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
298529136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG, 0);
298629136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
298729136c95SEric Huang 				    "[ULTB] Turn off Tx triangle\n");
298829136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
298929136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
299029136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG,
299129136c95SEric Huang 					       ul_tb_data.def_tri_idx);
299229136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
299329136c95SEric Huang 				    "[ULTB] Set to default tx_shap_idx = %d\n",
299429136c95SEric Huang 				    ul_tb_data.def_tri_idx);
299529136c95SEric Huang 		}
299629136c95SEric Huang 	}
299729136c95SEric Huang }
299829136c95SEric Huang 
299929136c95SEric Huang static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev)
300029136c95SEric Huang {
300129136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
300229136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
300329136c95SEric Huang 
300429136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
300529136c95SEric Huang 		return;
300629136c95SEric Huang 
300729136c95SEric Huang 	ul_tb_info->dyn_tb_tri_en = true;
300829136c95SEric Huang 	ul_tb_info->def_if_bandedge =
300929136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN);
301029136c95SEric Huang }
301129136c95SEric Huang 
3012e3715859SEric Huang static
3013e3715859SEric Huang void rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats *antdiv_sts)
3014e3715859SEric Huang {
3015e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->cck_rssi_avg);
3016e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->ofdm_rssi_avg);
3017e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->non_legacy_rssi_avg);
3018e3715859SEric Huang 	antdiv_sts->pkt_cnt_cck = 0;
3019e3715859SEric Huang 	antdiv_sts->pkt_cnt_ofdm = 0;
3020e3715859SEric Huang 	antdiv_sts->pkt_cnt_non_legacy = 0;
30215feecb40SEric Huang 	antdiv_sts->evm = 0;
3022e3715859SEric Huang }
3023e3715859SEric Huang 
3024e3715859SEric Huang static void rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev *rtwdev,
3025e3715859SEric Huang 					      struct rtw89_rx_phy_ppdu *phy_ppdu,
3026e3715859SEric Huang 					      struct rtw89_antdiv_stats *stats)
3027e3715859SEric Huang {
3028023d2f14SPing-Ke Shih 	if (rtw89_get_data_rate_mode(rtwdev, phy_ppdu->rate) == DATA_RATE_MODE_NON_HT) {
3029e3715859SEric Huang 		if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) {
3030e3715859SEric Huang 			ewma_rssi_add(&stats->cck_rssi_avg, phy_ppdu->rssi_avg);
3031e3715859SEric Huang 			stats->pkt_cnt_cck++;
3032e3715859SEric Huang 		} else {
3033e3715859SEric Huang 			ewma_rssi_add(&stats->ofdm_rssi_avg, phy_ppdu->rssi_avg);
3034e3715859SEric Huang 			stats->pkt_cnt_ofdm++;
30355feecb40SEric Huang 			stats->evm += phy_ppdu->ofdm.evm_min;
3036e3715859SEric Huang 		}
3037e3715859SEric Huang 	} else {
3038e3715859SEric Huang 		ewma_rssi_add(&stats->non_legacy_rssi_avg, phy_ppdu->rssi_avg);
3039e3715859SEric Huang 		stats->pkt_cnt_non_legacy++;
30405feecb40SEric Huang 		stats->evm += phy_ppdu->ofdm.evm_min;
3041e3715859SEric Huang 	}
3042e3715859SEric Huang }
3043e3715859SEric Huang 
3044e3715859SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats *stats)
3045e3715859SEric Huang {
3046e3715859SEric Huang 	if (stats->pkt_cnt_non_legacy >= stats->pkt_cnt_cck &&
3047e3715859SEric Huang 	    stats->pkt_cnt_non_legacy >= stats->pkt_cnt_ofdm)
3048e3715859SEric Huang 		return ewma_rssi_read(&stats->non_legacy_rssi_avg);
3049e3715859SEric Huang 	else if (stats->pkt_cnt_ofdm >= stats->pkt_cnt_cck &&
3050e3715859SEric Huang 		 stats->pkt_cnt_ofdm >= stats->pkt_cnt_non_legacy)
3051e3715859SEric Huang 		return ewma_rssi_read(&stats->ofdm_rssi_avg);
3052e3715859SEric Huang 	else
3053e3715859SEric Huang 		return ewma_rssi_read(&stats->cck_rssi_avg);
3054e3715859SEric Huang }
3055e3715859SEric Huang 
30565feecb40SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats *stats)
30575feecb40SEric Huang {
30585feecb40SEric Huang 	return phy_div(stats->evm, stats->pkt_cnt_non_legacy + stats->pkt_cnt_ofdm);
30595feecb40SEric Huang }
30605feecb40SEric Huang 
3061e3715859SEric Huang void rtw89_phy_antdiv_parse(struct rtw89_dev *rtwdev,
3062e3715859SEric Huang 			    struct rtw89_rx_phy_ppdu *phy_ppdu)
3063e3715859SEric Huang {
3064e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3065e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3066e3715859SEric Huang 
3067e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
3068e3715859SEric Huang 		return;
3069e3715859SEric Huang 
3070e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->target_stats);
3071e3715859SEric Huang 
3072e3715859SEric Huang 	if (!antdiv->get_stats)
3073e3715859SEric Huang 		return;
3074e3715859SEric Huang 
3075e3715859SEric Huang 	if (hal->antenna_rx == RF_A)
3076e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->main_stats);
3077e3715859SEric Huang 	else if (hal->antenna_rx == RF_B)
3078e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->aux_stats);
3079e3715859SEric Huang }
3080e3715859SEric Huang 
3081a90c613dSEric Huang static void rtw89_phy_antdiv_reg_init(struct rtw89_dev *rtwdev)
3082a90c613dSEric Huang {
3083a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_ANT_TRAIN_EN,
3084a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3085a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_TX_ANT_SEL,
3086a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3087a90c613dSEric Huang 
3088a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_TRSW_TX_EXTEND,
3089a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3090a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_HW_ANTSW_DIS_BY_GNT_BT,
3091a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3092a90c613dSEric Huang 
3093a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_BT_FORCE_ANTIDX_EN,
3094a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3095a90c613dSEric Huang 
3096a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_RFSW_CTRL_ANT0_BASE, B_RFSW_CTRL_ANT_MAPPING,
3097a90c613dSEric Huang 			      0x0100, RTW89_PHY_0);
3098a90c613dSEric Huang 
3099a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_BTG_TRX,
3100a90c613dSEric Huang 			      0x1, RTW89_PHY_0);
3101a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_HW_CTRL,
3102a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3103a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_2G,
3104a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3105a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_5G,
3106a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3107a90c613dSEric Huang }
3108a90c613dSEric Huang 
3109e3715859SEric Huang static void rtw89_phy_antdiv_sts_reset(struct rtw89_dev *rtwdev)
3110e3715859SEric Huang {
3111e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3112e3715859SEric Huang 
3113e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
3114e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->main_stats);
3115e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->aux_stats);
3116e3715859SEric Huang }
3117e3715859SEric Huang 
3118a90c613dSEric Huang static void rtw89_phy_antdiv_init(struct rtw89_dev *rtwdev)
3119a90c613dSEric Huang {
3120e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3121a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3122a90c613dSEric Huang 
3123a90c613dSEric Huang 	if (!hal->ant_diversity)
3124a90c613dSEric Huang 		return;
3125a90c613dSEric Huang 
3126e3715859SEric Huang 	antdiv->get_stats = false;
3127e3715859SEric Huang 	antdiv->rssi_pre = 0;
3128e3715859SEric Huang 	rtw89_phy_antdiv_sts_reset(rtwdev);
3129a90c613dSEric Huang 	rtw89_phy_antdiv_reg_init(rtwdev);
3130a90c613dSEric Huang }
3131a90c613dSEric Huang 
3132e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev)
3133e3ec7017SPing-Ke Shih {
3134e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3135e3ec7017SPing-Ke Shih 	int i;
3136e3ec7017SPing-Ke Shih 	u8 th;
3137e3ec7017SPing-Ke Shih 
3138e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++) {
3139e3ec7017SPing-Ke Shih 		th = rtw89_chip_get_thermal(rtwdev, i);
3140e3ec7017SPing-Ke Shih 		if (th)
3141e3ec7017SPing-Ke Shih 			ewma_thermal_add(&phystat->avg_thermal[i], th);
3142e3ec7017SPing-Ke Shih 
3143e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3144e3ec7017SPing-Ke Shih 			    "path(%d) thermal cur=%u avg=%ld", i, th,
3145e3ec7017SPing-Ke Shih 			    ewma_thermal_read(&phystat->avg_thermal[i]));
3146e3ec7017SPing-Ke Shih 	}
3147e3ec7017SPing-Ke Shih }
3148e3ec7017SPing-Ke Shih 
3149e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data {
3150e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
3151e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info;
3152e3ec7017SPing-Ke Shih 	bool rssi_changed;
3153e3ec7017SPing-Ke Shih };
3154e3ec7017SPing-Ke Shih 
3155e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data,
3156e3ec7017SPing-Ke Shih 					    struct ieee80211_sta *sta)
3157e3ec7017SPing-Ke Shih {
3158e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3159e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data *rssi_data =
3160e3ec7017SPing-Ke Shih 					(struct rtw89_phy_iter_rssi_data *)data;
3161e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info;
3162e3ec7017SPing-Ke Shih 	unsigned long rssi_curr;
3163e3ec7017SPing-Ke Shih 
3164e3ec7017SPing-Ke Shih 	rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi);
3165e3ec7017SPing-Ke Shih 
3166e3ec7017SPing-Ke Shih 	if (rssi_curr < ch_info->rssi_min) {
3167e3ec7017SPing-Ke Shih 		ch_info->rssi_min = rssi_curr;
3168e3ec7017SPing-Ke Shih 		ch_info->rssi_min_macid = rtwsta->mac_id;
3169e3ec7017SPing-Ke Shih 	}
3170e3ec7017SPing-Ke Shih 
3171e3ec7017SPing-Ke Shih 	if (rtwsta->prev_rssi == 0) {
3172e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3173e3ec7017SPing-Ke Shih 	} else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) {
3174e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3175e3ec7017SPing-Ke Shih 		rssi_data->rssi_changed = true;
3176e3ec7017SPing-Ke Shih 	}
3177e3ec7017SPing-Ke Shih }
3178e3ec7017SPing-Ke Shih 
3179e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev)
3180e3ec7017SPing-Ke Shih {
3181e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data rssi_data = {0};
3182e3ec7017SPing-Ke Shih 
3183e3ec7017SPing-Ke Shih 	rssi_data.rtwdev = rtwdev;
3184e3ec7017SPing-Ke Shih 	rssi_data.ch_info = &rtwdev->ch_info;
3185e3ec7017SPing-Ke Shih 	rssi_data.ch_info->rssi_min = U8_MAX;
3186e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
3187e3ec7017SPing-Ke Shih 					  rtw89_phy_stat_rssi_update_iter,
3188e3ec7017SPing-Ke Shih 					  &rssi_data);
3189e3ec7017SPing-Ke Shih 	if (rssi_data.rssi_changed)
3190e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_wl_sta(rtwdev);
3191e3ec7017SPing-Ke Shih }
3192e3ec7017SPing-Ke Shih 
3193e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev)
3194e3ec7017SPing-Ke Shih {
3195e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3196e3ec7017SPing-Ke Shih 	int i;
3197e3ec7017SPing-Ke Shih 
3198e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
3199e3ec7017SPing-Ke Shih 		ewma_thermal_init(&phystat->avg_thermal[i]);
3200e3ec7017SPing-Ke Shih 
3201e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3202e3ec7017SPing-Ke Shih 
3203e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3204e3ec7017SPing-Ke Shih 	memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat));
3205e3ec7017SPing-Ke Shih }
3206e3ec7017SPing-Ke Shih 
3207e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev)
3208e3ec7017SPing-Ke Shih {
3209e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3210e3ec7017SPing-Ke Shih 
3211e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3212e3ec7017SPing-Ke Shih 	rtw89_phy_stat_rssi_update(rtwdev);
3213e3ec7017SPing-Ke Shih 
3214e3ec7017SPing-Ke Shih 	phystat->last_pkt_stat = phystat->cur_pkt_stat;
3215e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3216e3ec7017SPing-Ke Shih }
3217e3ec7017SPing-Ke Shih 
3218e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us)
3219e3ec7017SPing-Ke Shih {
3220e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3221e3ec7017SPing-Ke Shih 
3222e3ec7017SPing-Ke Shih 	return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3223e3ec7017SPing-Ke Shih }
3224e3ec7017SPing-Ke Shih 
3225e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx)
3226e3ec7017SPing-Ke Shih {
3227e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3228e3ec7017SPing-Ke Shih 
3229e3ec7017SPing-Ke Shih 	return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3230e3ec7017SPing-Ke Shih }
3231e3ec7017SPing-Ke Shih 
3232e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev)
3233e3ec7017SPing-Ke Shih {
3234e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3235e3ec7017SPing-Ke Shih 
3236e3ec7017SPing-Ke Shih 	env->ccx_manual_ctrl = false;
3237e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3238e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3239e3ec7017SPing-Ke Shih 	env->ccx_period = 0;
3240e3ec7017SPing-Ke Shih 	env->ccx_unit_idx = RTW89_CCX_32_US;
3241e3ec7017SPing-Ke Shih 
3242e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1);
3243e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1);
3244e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3245e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK,
3246e3ec7017SPing-Ke Shih 			       RTW89_CCX_EDCCA_BW20_0);
3247e3ec7017SPing-Ke Shih }
3248e3ec7017SPing-Ke Shih 
3249e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report,
3250e3ec7017SPing-Ke Shih 				    u16 score)
3251e3ec7017SPing-Ke Shih {
3252e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3253e3ec7017SPing-Ke Shih 	u32 numer = 0;
3254e3ec7017SPing-Ke Shih 	u16 ret = 0;
3255e3ec7017SPing-Ke Shih 
3256e3ec7017SPing-Ke Shih 	numer = report * score + (env->ccx_period >> 1);
3257e3ec7017SPing-Ke Shih 	if (env->ccx_period)
3258e3ec7017SPing-Ke Shih 		ret = numer / env->ccx_period;
3259e3ec7017SPing-Ke Shih 
3260e3ec7017SPing-Ke Shih 	return ret >= score ? score - 1 : ret;
3261e3ec7017SPing-Ke Shih }
3262e3ec7017SPing-Ke Shih 
3263e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev,
3264e3ec7017SPing-Ke Shih 					    u16 time_ms, u32 *period,
3265e3ec7017SPing-Ke Shih 					    u32 *unit_idx)
3266e3ec7017SPing-Ke Shih {
3267e3ec7017SPing-Ke Shih 	u32 idx;
3268e3ec7017SPing-Ke Shih 	u8 quotient;
3269e3ec7017SPing-Ke Shih 
3270e3ec7017SPing-Ke Shih 	if (time_ms >= CCX_MAX_PERIOD)
3271e3ec7017SPing-Ke Shih 		time_ms = CCX_MAX_PERIOD;
3272e3ec7017SPing-Ke Shih 
3273e3ec7017SPing-Ke Shih 	quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD;
3274e3ec7017SPing-Ke Shih 
3275e3ec7017SPing-Ke Shih 	if (quotient < 4)
3276e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_4_US;
3277e3ec7017SPing-Ke Shih 	else if (quotient < 8)
3278e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_8_US;
3279e3ec7017SPing-Ke Shih 	else if (quotient < 16)
3280e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_16_US;
3281e3ec7017SPing-Ke Shih 	else
3282e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_32_US;
3283e3ec7017SPing-Ke Shih 
3284e3ec7017SPing-Ke Shih 	*unit_idx = idx;
3285e3ec7017SPing-Ke Shih 	*period = (time_ms * MS_TO_4US_RATIO) >> idx;
3286e3ec7017SPing-Ke Shih 
3287e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3288e3ec7017SPing-Ke Shih 		    "[Trigger Time] period:%d, unit_idx:%d\n",
3289e3ec7017SPing-Ke Shih 		    *period, *unit_idx);
3290e3ec7017SPing-Ke Shih }
3291e3ec7017SPing-Ke Shih 
3292e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev)
3293e3ec7017SPing-Ke Shih {
3294e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3295e3ec7017SPing-Ke Shih 
3296e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3297e3ec7017SPing-Ke Shih 		    "lv:(%d)->(0)\n", env->ccx_rac_lv);
3298e3ec7017SPing-Ke Shih 
3299e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3300e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3301e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3302e3ec7017SPing-Ke Shih }
3303e3ec7017SPing-Ke Shih 
3304e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev,
3305e3ec7017SPing-Ke Shih 					      struct rtw89_ccx_para_info *para)
3306e3ec7017SPing-Ke Shih {
3307e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3308e3ec7017SPing-Ke Shih 	bool is_update = env->ifs_clm_app != para->ifs_clm_app;
3309e3ec7017SPing-Ke Shih 	u8 i = 0;
3310e3ec7017SPing-Ke Shih 	u16 *ifs_th_l = env->ifs_clm_th_l;
3311e3ec7017SPing-Ke Shih 	u16 *ifs_th_h = env->ifs_clm_th_h;
3312e3ec7017SPing-Ke Shih 	u32 ifs_th0_us = 0, ifs_th_times = 0;
3313e3ec7017SPing-Ke Shih 	u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0};
3314e3ec7017SPing-Ke Shih 
3315e3ec7017SPing-Ke Shih 	if (!is_update)
3316e3ec7017SPing-Ke Shih 		goto ifs_update_finished;
3317e3ec7017SPing-Ke Shih 
3318e3ec7017SPing-Ke Shih 	switch (para->ifs_clm_app) {
3319e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_INIT:
3320e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_BACKGROUND:
3321e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_ACS:
3322e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG:
3323e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DIG:
3324e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_TDMA_DIG:
3325e3ec7017SPing-Ke Shih 		ifs_th0_us = IFS_CLM_TH0_UPPER;
3326e3ec7017SPing-Ke Shih 		ifs_th_times = IFS_CLM_TH_MUL;
3327e3ec7017SPing-Ke Shih 		break;
3328e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG_MANUAL:
3329e3ec7017SPing-Ke Shih 		ifs_th0_us = para->ifs_clm_manual_th0;
3330e3ec7017SPing-Ke Shih 		ifs_th_times = para->ifs_clm_manual_th_times;
3331e3ec7017SPing-Ke Shih 		break;
3332e3ec7017SPing-Ke Shih 	default:
3333e3ec7017SPing-Ke Shih 		break;
3334e3ec7017SPing-Ke Shih 	}
3335e3ec7017SPing-Ke Shih 
3336e3ec7017SPing-Ke Shih 	/* Set sampling threshold for 4 different regions, unit in idx_cnt.
3337e3ec7017SPing-Ke Shih 	 * low[i] = high[i-1] + 1
3338e3ec7017SPing-Ke Shih 	 * high[i] = high[i-1] * ifs_th_times
3339e3ec7017SPing-Ke Shih 	 */
3340e3ec7017SPing-Ke Shih 	ifs_th_l[IFS_CLM_TH_START_IDX] = 0;
3341e3ec7017SPing-Ke Shih 	ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us;
3342e3ec7017SPing-Ke Shih 	ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev,
3343e3ec7017SPing-Ke Shih 								 ifs_th0_us);
3344e3ec7017SPing-Ke Shih 	for (i = 1; i < RTW89_IFS_CLM_NUM; i++) {
3345e3ec7017SPing-Ke Shih 		ifs_th_l[i] = ifs_th_h[i - 1] + 1;
3346e3ec7017SPing-Ke Shih 		ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times;
3347e3ec7017SPing-Ke Shih 		ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]);
3348e3ec7017SPing-Ke Shih 	}
3349e3ec7017SPing-Ke Shih 
3350e3ec7017SPing-Ke Shih ifs_update_finished:
3351e3ec7017SPing-Ke Shih 	if (!is_update)
3352e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3353e3ec7017SPing-Ke Shih 			    "No need to update IFS_TH\n");
3354e3ec7017SPing-Ke Shih 
3355e3ec7017SPing-Ke Shih 	return is_update;
3356e3ec7017SPing-Ke Shih }
3357e3ec7017SPing-Ke Shih 
3358e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev)
3359e3ec7017SPing-Ke Shih {
3360e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3361e3ec7017SPing-Ke Shih 	u8 i = 0;
3362e3ec7017SPing-Ke Shih 
3363e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK,
3364e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[0]);
3365e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK,
3366e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[1]);
3367e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK,
3368e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[2]);
3369e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK,
3370e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[3]);
3371e3ec7017SPing-Ke Shih 
3372e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK,
3373e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[0]);
3374e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK,
3375e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[1]);
3376e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK,
3377e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[2]);
3378e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK,
3379e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[3]);
3380e3ec7017SPing-Ke Shih 
3381e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3382e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3383e3ec7017SPing-Ke Shih 			    "Update IFS_T%d_th{low, high} : {%d, %d}\n",
3384e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]);
3385e3ec7017SPing-Ke Shih }
3386e3ec7017SPing-Ke Shih 
3387e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev)
3388e3ec7017SPing-Ke Shih {
3389e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3390e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3391e3ec7017SPing-Ke Shih 
3392e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3393e3ec7017SPing-Ke Shih 	env->ifs_clm_mntr_time = 0;
3394e3ec7017SPing-Ke Shih 
3395e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_INIT;
3396e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, &para))
3397e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3398e3ec7017SPing-Ke Shih 
3399e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN,
3400e3ec7017SPing-Ke Shih 			       true);
3401e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true);
3402e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true);
3403e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true);
3404e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true);
3405e3ec7017SPing-Ke Shih }
3406e3ec7017SPing-Ke Shih 
3407e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev,
3408e3ec7017SPing-Ke Shih 				     enum rtw89_env_racing_lv level)
3409e3ec7017SPing-Ke Shih {
3410e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3411e3ec7017SPing-Ke Shih 	int ret = 0;
3412e3ec7017SPing-Ke Shih 
3413e3ec7017SPing-Ke Shih 	if (level >= RTW89_RAC_MAX_NUM) {
3414e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3415e3ec7017SPing-Ke Shih 			    "[WARNING] Wrong LV=%d\n", level);
3416e3ec7017SPing-Ke Shih 		return -EINVAL;
3417e3ec7017SPing-Ke Shih 	}
3418e3ec7017SPing-Ke Shih 
3419e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3420e3ec7017SPing-Ke Shih 		    "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing,
3421e3ec7017SPing-Ke Shih 		    env->ccx_rac_lv, level);
3422e3ec7017SPing-Ke Shih 
3423e3ec7017SPing-Ke Shih 	if (env->ccx_ongoing) {
3424e3ec7017SPing-Ke Shih 		if (level <= env->ccx_rac_lv)
3425e3ec7017SPing-Ke Shih 			ret = -EINVAL;
3426e3ec7017SPing-Ke Shih 		else
3427e3ec7017SPing-Ke Shih 			env->ccx_ongoing = false;
3428e3ec7017SPing-Ke Shih 	}
3429e3ec7017SPing-Ke Shih 
3430e3ec7017SPing-Ke Shih 	if (ret == 0)
3431e3ec7017SPing-Ke Shih 		env->ccx_rac_lv = level;
3432e3ec7017SPing-Ke Shih 
3433e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n",
3434e3ec7017SPing-Ke Shih 		    !ret);
3435e3ec7017SPing-Ke Shih 
3436e3ec7017SPing-Ke Shih 	return ret;
3437e3ec7017SPing-Ke Shih }
3438e3ec7017SPing-Ke Shih 
3439e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev)
3440e3ec7017SPing-Ke Shih {
3441e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3442e3ec7017SPing-Ke Shih 
3443e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0);
3444e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0);
3445e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1);
3446e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3447e3ec7017SPing-Ke Shih 
3448e3ec7017SPing-Ke Shih 	env->ccx_ongoing = true;
3449e3ec7017SPing-Ke Shih }
3450e3ec7017SPing-Ke Shih 
3451e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev)
3452e3ec7017SPing-Ke Shih {
3453e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3454e3ec7017SPing-Ke Shih 	u8 i = 0;
3455e3ec7017SPing-Ke Shih 	u32 res = 0;
3456e3ec7017SPing-Ke Shih 
3457e3ec7017SPing-Ke Shih 	env->ifs_clm_tx_ratio =
3458e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT);
3459e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca_ratio =
3460e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca,
3461e3ec7017SPing-Ke Shih 					 PERCENT);
3462e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_ratio =
3463e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT);
3464e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_ratio =
3465e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT);
3466e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_cca_excl_fa_ratio =
3467e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa,
3468e3ec7017SPing-Ke Shih 					 PERCENT);
3469e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_cca_excl_fa_ratio =
3470e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa,
3471e3ec7017SPing-Ke Shih 					 PERCENT);
3472e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_permil =
3473e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL);
3474e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_permil =
3475e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL);
3476e3ec7017SPing-Ke Shih 
3477e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++) {
3478e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) {
3479e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD;
3480e3ec7017SPing-Ke Shih 		} else {
3481e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] =
3482e3ec7017SPing-Ke Shih 				rtw89_phy_ccx_idx_to_us(rtwdev,
3483e3ec7017SPing-Ke Shih 							env->ifs_clm_avg[i]);
3484e3ec7017SPing-Ke Shih 		}
3485e3ec7017SPing-Ke Shih 
3486e3ec7017SPing-Ke Shih 		res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]);
3487e3ec7017SPing-Ke Shih 		res += env->ifs_clm_his[i] >> 1;
3488e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i])
3489e3ec7017SPing-Ke Shih 			res /= env->ifs_clm_his[i];
3490e3ec7017SPing-Ke Shih 		else
3491e3ec7017SPing-Ke Shih 			res = 0;
3492e3ec7017SPing-Ke Shih 		env->ifs_clm_cca_avg[i] = res;
3493e3ec7017SPing-Ke Shih 	}
3494e3ec7017SPing-Ke Shih 
3495e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3496e3ec7017SPing-Ke Shih 		    "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3497e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio);
3498e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3499e3ec7017SPing-Ke Shih 		    "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n",
3500e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio);
3501e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3502e3ec7017SPing-Ke Shih 		    "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n",
3503e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil);
3504e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3505e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n",
3506e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_cca_excl_fa_ratio,
3507e3ec7017SPing-Ke Shih 		    env->ifs_clm_ofdm_cca_excl_fa_ratio);
3508e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3509e3ec7017SPing-Ke Shih 		    "Time:[his, ifs_avg(us), cca_avg(us)]\n");
3510e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3511e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n",
3512e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i],
3513e3ec7017SPing-Ke Shih 			    env->ifs_clm_cca_avg[i]);
3514e3ec7017SPing-Ke Shih }
3515e3ec7017SPing-Ke Shih 
3516e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev)
3517e3ec7017SPing-Ke Shih {
3518e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3519e3ec7017SPing-Ke Shih 	u8 i = 0;
3520e3ec7017SPing-Ke Shih 
3521e3ec7017SPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) {
3522e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3523e3ec7017SPing-Ke Shih 			    "Get IFS_CLM report Fail\n");
3524e3ec7017SPing-Ke Shih 		return false;
3525e3ec7017SPing-Ke Shih 	}
3526e3ec7017SPing-Ke Shih 
3527e3ec7017SPing-Ke Shih 	env->ifs_clm_tx =
3528e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3529e3ec7017SPing-Ke Shih 				      B_IFS_CLM_TX_CNT_MSK);
3530e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca =
3531e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3532e3ec7017SPing-Ke Shih 				      B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK);
3533e3ec7017SPing-Ke Shih 	env->ifs_clm_cckcca_excl_fa =
3534e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3535e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK);
3536e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmcca_excl_fa =
3537e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3538e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK);
3539e3ec7017SPing-Ke Shih 	env->ifs_clm_cckfa =
3540e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3541e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCK_FA_MSK);
3542e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmfa =
3543e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3544e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDM_FA_MSK);
3545e3ec7017SPing-Ke Shih 
3546e3ec7017SPing-Ke Shih 	env->ifs_clm_his[0] =
3547e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK);
3548e3ec7017SPing-Ke Shih 	env->ifs_clm_his[1] =
3549e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK);
3550e3ec7017SPing-Ke Shih 	env->ifs_clm_his[2] =
3551e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK);
3552e3ec7017SPing-Ke Shih 	env->ifs_clm_his[3] =
3553e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK);
3554e3ec7017SPing-Ke Shih 
3555e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[0] =
3556e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK);
3557e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[1] =
3558e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK);
3559e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[2] =
3560e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK);
3561e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[3] =
3562e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK);
3563e3ec7017SPing-Ke Shih 
3564e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[0] =
3565e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK);
3566e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[1] =
3567e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK);
3568e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[2] =
3569e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK);
3570e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[3] =
3571e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK);
3572e3ec7017SPing-Ke Shih 
3573e3ec7017SPing-Ke Shih 	env->ifs_clm_total_ifs =
3574e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK);
3575e3ec7017SPing-Ke Shih 
3576e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n",
3577e3ec7017SPing-Ke Shih 		    env->ifs_clm_total_ifs);
3578e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3579e3ec7017SPing-Ke Shih 		    "{Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3580e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca);
3581e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3582e3ec7017SPing-Ke Shih 		    "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n",
3583e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckfa, env->ifs_clm_ofdmfa);
3584e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3585e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n",
3586e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa);
3587e3ec7017SPing-Ke Shih 
3588e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n");
3589e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3590e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3591e3ec7017SPing-Ke Shih 			    "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i],
3592e3ec7017SPing-Ke Shih 			    env->ifs_clm_avg[i], env->ifs_clm_cca[i]);
3593e3ec7017SPing-Ke Shih 
3594e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_get_utility(rtwdev);
3595e3ec7017SPing-Ke Shih 
3596e3ec7017SPing-Ke Shih 	return true;
3597e3ec7017SPing-Ke Shih }
3598e3ec7017SPing-Ke Shih 
3599e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev,
3600e3ec7017SPing-Ke Shih 				 struct rtw89_ccx_para_info *para)
3601e3ec7017SPing-Ke Shih {
3602e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3603e3ec7017SPing-Ke Shih 	u32 period = 0;
3604e3ec7017SPing-Ke Shih 	u32 unit_idx = 0;
3605e3ec7017SPing-Ke Shih 
3606e3ec7017SPing-Ke Shih 	if (para->mntr_time == 0) {
3607e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3608e3ec7017SPing-Ke Shih 			    "[WARN] MNTR_TIME is 0\n");
3609e3ec7017SPing-Ke Shih 		return -EINVAL;
3610e3ec7017SPing-Ke Shih 	}
3611e3ec7017SPing-Ke Shih 
3612e3ec7017SPing-Ke Shih 	if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv))
3613e3ec7017SPing-Ke Shih 		return -EINVAL;
3614e3ec7017SPing-Ke Shih 
3615e3ec7017SPing-Ke Shih 	if (para->mntr_time != env->ifs_clm_mntr_time) {
3616e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time,
3617e3ec7017SPing-Ke Shih 						&period, &unit_idx);
3618e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3619e3ec7017SPing-Ke Shih 				       B_IFS_CLM_PERIOD_MSK, period);
3620e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3621e3ec7017SPing-Ke Shih 				       B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx);
3622e3ec7017SPing-Ke Shih 
3623e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3624e3ec7017SPing-Ke Shih 			    "Update IFS-CLM time ((%d)) -> ((%d))\n",
3625e3ec7017SPing-Ke Shih 			    env->ifs_clm_mntr_time, para->mntr_time);
3626e3ec7017SPing-Ke Shih 
3627e3ec7017SPing-Ke Shih 		env->ifs_clm_mntr_time = para->mntr_time;
3628e3ec7017SPing-Ke Shih 		env->ccx_period = (u16)period;
3629e3ec7017SPing-Ke Shih 		env->ccx_unit_idx = (u8)unit_idx;
3630e3ec7017SPing-Ke Shih 	}
3631e3ec7017SPing-Ke Shih 
3632e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) {
3633e3ec7017SPing-Ke Shih 		env->ifs_clm_app = para->ifs_clm_app;
3634e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3635e3ec7017SPing-Ke Shih 	}
3636e3ec7017SPing-Ke Shih 
3637e3ec7017SPing-Ke Shih 	return 0;
3638e3ec7017SPing-Ke Shih }
3639e3ec7017SPing-Ke Shih 
3640e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev)
3641e3ec7017SPing-Ke Shih {
3642e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3643e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3644e3ec7017SPing-Ke Shih 	u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3645e3ec7017SPing-Ke Shih 
3646e3ec7017SPing-Ke Shih 	env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3647e3ec7017SPing-Ke Shih 	if (env->ccx_manual_ctrl) {
3648e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3649e3ec7017SPing-Ke Shih 			    "CCX in manual ctrl\n");
3650e3ec7017SPing-Ke Shih 		return;
3651e3ec7017SPing-Ke Shih 	}
3652e3ec7017SPing-Ke Shih 
3653e3ec7017SPing-Ke Shih 	/* only ifs_clm for now */
3654e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_get_result(rtwdev))
3655e3ec7017SPing-Ke Shih 		env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3656e3ec7017SPing-Ke Shih 
3657e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_racing_release(rtwdev);
3658e3ec7017SPing-Ke Shih 	para.mntr_time = 1900;
3659e3ec7017SPing-Ke Shih 	para.rac_lv = RTW89_RAC_LV_1;
3660e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3661e3ec7017SPing-Ke Shih 
3662e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_set(rtwdev, &para) == 0)
3663e3ec7017SPing-Ke Shih 		chk_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3664e3ec7017SPing-Ke Shih 	if (chk_result)
3665e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_trigger(rtwdev);
3666e3ec7017SPing-Ke Shih 
3667e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3668e3ec7017SPing-Ke Shih 		    "get_result=0x%x, chk_result:0x%x\n",
3669e3ec7017SPing-Ke Shih 		    env->ccx_watchdog_result, chk_result);
3670e3ec7017SPing-Ke Shih }
3671e3ec7017SPing-Ke Shih 
3672eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page)
3673eb4e52b3SPo Hao Huang {
36749e2f177dSZong-Zhe Yang 	if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM ||
3675eb4e52b3SPo Hao Huang 	    *ie_page == RTW89_RSVD_9)
3676eb4e52b3SPo Hao Huang 		return false;
3677eb4e52b3SPo Hao Huang 	else if (*ie_page > RTW89_RSVD_9)
3678eb4e52b3SPo Hao Huang 		*ie_page -= 1;
3679eb4e52b3SPo Hao Huang 
3680eb4e52b3SPo Hao Huang 	return true;
3681eb4e52b3SPo Hao Huang }
3682eb4e52b3SPo Hao Huang 
3683eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)
3684eb4e52b3SPo Hao Huang {
3685eb4e52b3SPo Hao Huang 	static const u8 ie_page_shift = 2;
3686eb4e52b3SPo Hao Huang 
3687eb4e52b3SPo Hao Huang 	return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift);
3688eb4e52b3SPo Hao Huang }
3689eb4e52b3SPo Hao Huang 
3690eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev,
3691eb4e52b3SPo Hao Huang 				      enum rtw89_phy_status_bitmap ie_page)
3692eb4e52b3SPo Hao Huang {
3693eb4e52b3SPo Hao Huang 	u32 addr;
3694eb4e52b3SPo Hao Huang 
3695eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3696eb4e52b3SPo Hao Huang 		return 0;
3697eb4e52b3SPo Hao Huang 
3698eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3699eb4e52b3SPo Hao Huang 
3700eb4e52b3SPo Hao Huang 	return rtw89_phy_read32(rtwdev, addr);
3701eb4e52b3SPo Hao Huang }
3702eb4e52b3SPo Hao Huang 
3703eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev,
3704eb4e52b3SPo Hao Huang 				       enum rtw89_phy_status_bitmap ie_page,
3705eb4e52b3SPo Hao Huang 				       u32 val)
3706eb4e52b3SPo Hao Huang {
3707eb4e52b3SPo Hao Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
3708eb4e52b3SPo Hao Huang 	u32 addr;
3709eb4e52b3SPo Hao Huang 
3710eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3711eb4e52b3SPo Hao Huang 		return;
3712eb4e52b3SPo Hao Huang 
3713eb4e52b3SPo Hao Huang 	if (chip->chip_id == RTL8852A)
3714eb4e52b3SPo Hao Huang 		val &= B_PHY_STS_BITMAP_MSK_52A;
3715eb4e52b3SPo Hao Huang 
3716eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3717eb4e52b3SPo Hao Huang 	rtw89_phy_write32(rtwdev, addr, val);
3718eb4e52b3SPo Hao Huang }
3719eb4e52b3SPo Hao Huang 
3720eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev,
3721eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_bitmap bitmap,
3722eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_ie_type ie,
3723eb4e52b3SPo Hao Huang 					  bool enable)
3724eb4e52b3SPo Hao Huang {
3725eb4e52b3SPo Hao Huang 	u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap);
3726eb4e52b3SPo Hao Huang 
3727eb4e52b3SPo Hao Huang 	if (enable)
3728eb4e52b3SPo Hao Huang 		val |= BIT(ie);
3729eb4e52b3SPo Hao Huang 	else
3730eb4e52b3SPo Hao Huang 		val &= ~BIT(ie);
3731eb4e52b3SPo Hao Huang 
3732eb4e52b3SPo Hao Huang 	rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val);
3733eb4e52b3SPo Hao Huang }
3734eb4e52b3SPo Hao Huang 
3735eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev,
3736eb4e52b3SPo Hao Huang 					    bool enable,
3737eb4e52b3SPo Hao Huang 					    enum rtw89_phy_idx phy_idx)
3738eb4e52b3SPo Hao Huang {
3739eb4e52b3SPo Hao Huang 	if (enable) {
3740eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3741eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3742eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3743eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3744eb4e52b3SPo Hao Huang 	} else {
3745eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3746eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3747eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3748eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3749eb4e52b3SPo Hao Huang 	}
3750eb4e52b3SPo Hao Huang }
3751eb4e52b3SPo Hao Huang 
3752eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev)
3753eb4e52b3SPo Hao Huang {
3754eb4e52b3SPo Hao Huang 	u8 i;
3755eb4e52b3SPo Hao Huang 
3756eb4e52b3SPo Hao Huang 	rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0);
3757eb4e52b3SPo Hao Huang 
3758eb4e52b3SPo Hao Huang 	for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) {
3759eb4e52b3SPo Hao Huang 		if (i >= RTW89_CCK_PKT)
3760eb4e52b3SPo Hao Huang 			rtw89_physts_enable_ie_bitmap(rtwdev, i,
3761eb4e52b3SPo Hao Huang 						      RTW89_PHYSTS_IE09_FTR_0,
3762eb4e52b3SPo Hao Huang 						      true);
3763eb4e52b3SPo Hao Huang 		if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) ||
3764eb4e52b3SPo Hao Huang 		    (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT))
3765eb4e52b3SPo Hao Huang 			continue;
3766eb4e52b3SPo Hao Huang 		rtw89_physts_enable_ie_bitmap(rtwdev, i,
3767eb4e52b3SPo Hao Huang 					      RTW89_PHYSTS_IE24_OFDM_TD_PATH_A,
3768eb4e52b3SPo Hao Huang 					      true);
3769eb4e52b3SPo Hao Huang 	}
3770eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT,
3771eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3772eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT,
3773eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3774eb4e52b3SPo Hao Huang 
3775eb4e52b3SPo Hao Huang 	/* force IE01 for channel index, only channel field is valid */
3776eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT,
3777eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE01_CMN_OFDM, true);
3778eb4e52b3SPo Hao Huang }
3779eb4e52b3SPo Hao Huang 
3780e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type)
3781e3ec7017SPing-Ke Shih {
3782e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3783e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3784e3ec7017SPing-Ke Shih 	const struct rtw89_phy_dig_gain_cfg *cfg;
3785e3ec7017SPing-Ke Shih 	const char *msg;
3786e3ec7017SPing-Ke Shih 	u8 i;
3787e3ec7017SPing-Ke Shih 	s8 gain_base;
3788e3ec7017SPing-Ke Shih 	s8 *gain_arr;
3789e3ec7017SPing-Ke Shih 	u32 tmp;
3790e3ec7017SPing-Ke Shih 
3791e3ec7017SPing-Ke Shih 	switch (type) {
3792e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_G:
3793e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_g;
3794e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3795e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_g;
3796e3ec7017SPing-Ke Shih 		msg = "lna_gain_g";
3797e3ec7017SPing-Ke Shih 		break;
3798e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_G:
3799e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_g;
3800e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_G;
3801e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_g;
3802e3ec7017SPing-Ke Shih 		msg = "tia_gain_g";
3803e3ec7017SPing-Ke Shih 		break;
3804e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_A:
3805e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_a;
3806e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3807e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_a;
3808e3ec7017SPing-Ke Shih 		msg = "lna_gain_a";
3809e3ec7017SPing-Ke Shih 		break;
3810e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_A:
3811e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_a;
3812e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_A;
3813e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_a;
3814e3ec7017SPing-Ke Shih 		msg = "tia_gain_a";
3815e3ec7017SPing-Ke Shih 		break;
3816e3ec7017SPing-Ke Shih 	default:
3817e3ec7017SPing-Ke Shih 		return;
3818e3ec7017SPing-Ke Shih 	}
3819e3ec7017SPing-Ke Shih 
3820e3ec7017SPing-Ke Shih 	for (i = 0; i < cfg->size; i++) {
3821e3ec7017SPing-Ke Shih 		tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr,
3822e3ec7017SPing-Ke Shih 					    cfg->table[i].mask);
3823e3ec7017SPing-Ke Shih 		tmp >>= DIG_GAIN_SHIFT;
3824e3ec7017SPing-Ke Shih 		gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base;
3825e3ec7017SPing-Ke Shih 		gain_base += DIG_GAIN;
3826e3ec7017SPing-Ke Shih 
3827e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n",
3828e3ec7017SPing-Ke Shih 			    msg, i, gain_arr[i]);
3829e3ec7017SPing-Ke Shih 	}
3830e3ec7017SPing-Ke Shih }
3831e3ec7017SPing-Ke Shih 
3832e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev)
3833e3ec7017SPing-Ke Shih {
3834e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3835e3ec7017SPing-Ke Shih 	u32 tmp;
3836e3ec7017SPing-Ke Shih 	u8 i;
3837e3ec7017SPing-Ke Shih 
3838d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
3839d264edb1SJohnson Lin 		return;
3840d264edb1SJohnson Lin 
3841e3ec7017SPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW,
3842e3ec7017SPing-Ke Shih 				    B_PATH0_IB_PKPW_MSK);
3843e3ec7017SPing-Ke Shih 	dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT);
3844e3ec7017SPing-Ke Shih 	dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK,
3845e3ec7017SPing-Ke Shih 					    B_PATH0_IB_PBK_MSK);
3846e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n",
3847e3ec7017SPing-Ke Shih 		    dig->ib_pkpwr, dig->ib_pbk);
3848e3ec7017SPing-Ke Shih 
3849e3ec7017SPing-Ke Shih 	for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++)
3850e3ec7017SPing-Ke Shih 		rtw89_phy_dig_read_gain_table(rtwdev, i);
3851e3ec7017SPing-Ke Shih }
3852e3ec7017SPing-Ke Shih 
3853e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22;
3854e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104};
3855e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88};
3856e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16};
3857e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528};
3858e3ec7017SPing-Ke Shih 
3859e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev)
3860e3ec7017SPing-Ke Shih {
3861e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info;
3862e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3863e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3864e3ec7017SPing-Ke Shih 
3865e3ec7017SPing-Ke Shih 	if (is_linked) {
3866e3ec7017SPing-Ke Shih 		dig->igi_rssi = ch_info->rssi_min >> 1;
3867e3ec7017SPing-Ke Shih 	} else {
3868e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n");
3869e3ec7017SPing-Ke Shih 		dig->igi_rssi = rssi_nolink;
3870e3ec7017SPing-Ke Shih 	}
3871e3ec7017SPing-Ke Shih }
3872e3ec7017SPing-Ke Shih 
3873e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev)
3874e3ec7017SPing-Ke Shih {
3875e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3876cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3877e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3878e3ec7017SPing-Ke Shih 	const u16 *fa_th_src = NULL;
3879e3ec7017SPing-Ke Shih 
3880cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
3881e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
3882e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_g;
3883e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_g;
3884e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_2g : fa_th_nolink;
3885e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = false;
3886e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3887e3ec7017SPing-Ke Shih 		break;
3888e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
3889e3ec7017SPing-Ke Shih 	default:
3890e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_a;
3891e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_a;
3892e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_5g : fa_th_nolink;
3893e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = true;
3894e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3895e3ec7017SPing-Ke Shih 		break;
3896e3ec7017SPing-Ke Shih 	}
3897e3ec7017SPing-Ke Shih 	memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th));
3898e3ec7017SPing-Ke Shih 	memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th));
3899e3ec7017SPing-Ke Shih }
3900e3ec7017SPing-Ke Shih 
3901e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20;
3902e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a;
3903e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max;
3904e3ec7017SPing-Ke Shih 
3905e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev)
3906e3ec7017SPing-Ke Shih {
3907e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3908e3ec7017SPing-Ke Shih 
3909e3ec7017SPing-Ke Shih 	dig->cur_gaincode.lna_idx = LNA_IDX_MAX;
3910e3ec7017SPing-Ke Shih 	dig->cur_gaincode.tia_idx = TIA_IDX_MAX;
3911e3ec7017SPing-Ke Shih 	dig->cur_gaincode.rxb_idx = RXB_IDX_MAX;
3912e3ec7017SPing-Ke Shih 	dig->force_gaincode.lna_idx = LNA_IDX_MAX;
3913e3ec7017SPing-Ke Shih 	dig->force_gaincode.tia_idx = TIA_IDX_MAX;
3914e3ec7017SPing-Ke Shih 	dig->force_gaincode.rxb_idx = RXB_IDX_MAX;
3915e3ec7017SPing-Ke Shih 
3916e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = igi_max_performance_mode;
3917e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = dynamic_igi_min;
3918e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dynamic_pd_threshold_max;
3919e3ec7017SPing-Ke Shih 	dig->pd_low_th_ofst = pd_low_th_offset;
3920e3ec7017SPing-Ke Shih 	dig->is_linked_pre = false;
3921e3ec7017SPing-Ke Shih }
3922e3ec7017SPing-Ke Shih 
3923e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev)
3924e3ec7017SPing-Ke Shih {
3925e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_gain_para(rtwdev);
3926e3ec7017SPing-Ke Shih 	rtw89_phy_dig_reset(rtwdev);
3927e3ec7017SPing-Ke Shih }
3928e3ec7017SPing-Ke Shih 
3929e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3930e3ec7017SPing-Ke Shih {
3931e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3932e3ec7017SPing-Ke Shih 	u8 lna_idx;
3933e3ec7017SPing-Ke Shih 
3934e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3935e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX6;
3936e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[1])
3937e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX5;
3938e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[2])
3939e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX4;
3940e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[3])
3941e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX3;
3942e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[4])
3943e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX2;
3944e3ec7017SPing-Ke Shih 	else
3945e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX1;
3946e3ec7017SPing-Ke Shih 
3947e3ec7017SPing-Ke Shih 	return lna_idx;
3948e3ec7017SPing-Ke Shih }
3949e3ec7017SPing-Ke Shih 
3950e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3951e3ec7017SPing-Ke Shih {
3952e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3953e3ec7017SPing-Ke Shih 	u8 tia_idx;
3954e3ec7017SPing-Ke Shih 
3955e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3956e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX1;
3957e3ec7017SPing-Ke Shih 	else
3958e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX0;
3959e3ec7017SPing-Ke Shih 
3960e3ec7017SPing-Ke Shih 	return tia_idx;
3961e3ec7017SPing-Ke Shih }
3962e3ec7017SPing-Ke Shih 
3963e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110
3964e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10
3965e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3966e3ec7017SPing-Ke Shih 					struct rtw89_agc_gaincode_set *set)
3967e3ec7017SPing-Ke Shih {
3968e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3969e3ec7017SPing-Ke Shih 	s8 lna_gain = dig->lna_gain[set->lna_idx];
3970e3ec7017SPing-Ke Shih 	s8 tia_gain = dig->tia_gain[set->tia_idx];
3971e3ec7017SPing-Ke Shih 	s32 wb_rssi = rssi + lna_gain + tia_gain;
3972e3ec7017SPing-Ke Shih 	s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE;
3973e3ec7017SPing-Ke Shih 	u8 rxb_idx;
3974e3ec7017SPing-Ke Shih 
3975e3ec7017SPing-Ke Shih 	rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi;
3976e3ec7017SPing-Ke Shih 	rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX);
3977e3ec7017SPing-Ke Shih 
3978e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n",
3979e3ec7017SPing-Ke Shih 		    wb_rssi, rxb_idx_tmp);
3980e3ec7017SPing-Ke Shih 
3981e3ec7017SPing-Ke Shih 	return rxb_idx;
3982e3ec7017SPing-Ke Shih }
3983e3ec7017SPing-Ke Shih 
3984e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3985e3ec7017SPing-Ke Shih 					   struct rtw89_agc_gaincode_set *set)
3986e3ec7017SPing-Ke Shih {
3987e3ec7017SPing-Ke Shih 	set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi);
3988e3ec7017SPing-Ke Shih 	set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi);
3989e3ec7017SPing-Ke Shih 	set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set);
3990e3ec7017SPing-Ke Shih 
3991e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3992e3ec7017SPing-Ke Shih 		    "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n",
3993e3ec7017SPing-Ke Shih 		    rssi, set->lna_idx, set->tia_idx, set->rxb_idx);
3994e3ec7017SPing-Ke Shih }
3995e3ec7017SPing-Ke Shih 
3996e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25
3997e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2
3998e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev)
3999e3ec7017SPing-Ke Shih {
4000e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4001e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
4002e3ec7017SPing-Ke Shih 	enum rtw89_dig_noisy_level noisy_lv;
4003e3ec7017SPing-Ke Shih 	u8 igi_offset = dig->fa_rssi_ofst;
4004e3ec7017SPing-Ke Shih 	u16 fa_ratio = 0;
4005e3ec7017SPing-Ke Shih 
4006e3ec7017SPing-Ke Shih 	fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil;
4007e3ec7017SPing-Ke Shih 
4008e3ec7017SPing-Ke Shih 	if (fa_ratio < dig->fa_th[0])
4009e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL0;
4010e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[1])
4011e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL1;
4012e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[2])
4013e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL2;
4014e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[3])
4015e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL3;
4016e3ec7017SPing-Ke Shih 	else
4017e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX;
4018e3ec7017SPing-Ke Shih 
4019e3ec7017SPing-Ke Shih 	if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2)
4020e3ec7017SPing-Ke Shih 		igi_offset = 0;
4021e3ec7017SPing-Ke Shih 	else
4022e3ec7017SPing-Ke Shih 		igi_offset += noisy_lv * IGI_OFFSET_MUL;
4023e3ec7017SPing-Ke Shih 
4024e3ec7017SPing-Ke Shih 	igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX);
4025e3ec7017SPing-Ke Shih 	dig->fa_rssi_ofst = igi_offset;
4026e3ec7017SPing-Ke Shih 
4027e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4028e3ec7017SPing-Ke Shih 		    "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n",
4029e3ec7017SPing-Ke Shih 		    dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]);
4030e3ec7017SPing-Ke Shih 
4031e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4032e3ec7017SPing-Ke Shih 		    "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n",
4033e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil,
4034e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil,
4035e3ec7017SPing-Ke Shih 		    noisy_lv, igi_offset);
4036e3ec7017SPing-Ke Shih }
4037e3ec7017SPing-Ke Shih 
4038e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx)
4039e3ec7017SPing-Ke Shih {
404087deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
404187deaad9SEric Huang 
404287deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr,
404387deaad9SEric Huang 			       dig_regs->p0_lna_init.mask, lna_idx);
404487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr,
404587deaad9SEric Huang 			       dig_regs->p1_lna_init.mask, lna_idx);
4046e3ec7017SPing-Ke Shih }
4047e3ec7017SPing-Ke Shih 
4048e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx)
4049e3ec7017SPing-Ke Shih {
405087deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
405187deaad9SEric Huang 
405287deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr,
405387deaad9SEric Huang 			       dig_regs->p0_tia_init.mask, tia_idx);
405487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr,
405587deaad9SEric Huang 			       dig_regs->p1_tia_init.mask, tia_idx);
4056e3ec7017SPing-Ke Shih }
4057e3ec7017SPing-Ke Shih 
4058e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx)
4059e3ec7017SPing-Ke Shih {
406087deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
406187deaad9SEric Huang 
406287deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr,
406387deaad9SEric Huang 			       dig_regs->p0_rxb_init.mask, rxb_idx);
406487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr,
406587deaad9SEric Huang 			       dig_regs->p1_rxb_init.mask, rxb_idx);
4066e3ec7017SPing-Ke Shih }
4067e3ec7017SPing-Ke Shih 
4068e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev,
4069e3ec7017SPing-Ke Shih 				     const struct rtw89_agc_gaincode_set set)
4070e3ec7017SPing-Ke Shih {
4071e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx);
4072e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx);
4073e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx);
4074e3ec7017SPing-Ke Shih 
4075e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n",
4076e3ec7017SPing-Ke Shih 		    set.lna_idx, set.tia_idx, set.rxb_idx);
4077e3ec7017SPing-Ke Shih }
4078e3ec7017SPing-Ke Shih 
4079e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev,
4080e3ec7017SPing-Ke Shih 						   bool enable)
4081e3ec7017SPing-Ke Shih {
408287deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4083e3ec7017SPing-Ke Shih 
408487deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr,
408587deaad9SEric Huang 			       dig_regs->p0_p20_pagcugc_en.mask, enable);
408687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr,
408787deaad9SEric Huang 			       dig_regs->p0_s20_pagcugc_en.mask, enable);
408887deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr,
408987deaad9SEric Huang 			       dig_regs->p1_p20_pagcugc_en.mask, enable);
409087deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr,
409187deaad9SEric Huang 			       dig_regs->p1_s20_pagcugc_en.mask, enable);
4092e3ec7017SPing-Ke Shih 
4093e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable);
4094e3ec7017SPing-Ke Shih }
4095e3ec7017SPing-Ke Shih 
4096bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev)
4097bed4045fSJohnson Lin {
4098bed4045fSJohnson Lin 	struct rtw89_dig_info *dig = &rtwdev->dig;
4099bed4045fSJohnson Lin 
4100d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
4101d264edb1SJohnson Lin 		return;
4102d264edb1SJohnson Lin 
4103bed4045fSJohnson Lin 	if (dig->force_gaincode_idx_en) {
4104bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4105bed4045fSJohnson Lin 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4106bed4045fSJohnson Lin 			    "Force gaincode index enabled.\n");
4107bed4045fSJohnson Lin 	} else {
4108bed4045fSJohnson Lin 		rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi,
4109bed4045fSJohnson Lin 					       &dig->cur_gaincode);
4110bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode);
4111bed4045fSJohnson Lin 	}
4112bed4045fSJohnson Lin }
4113bed4045fSJohnson Lin 
4114e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi,
4115e3ec7017SPing-Ke Shih 				    bool enable)
4116e3ec7017SPing-Ke Shih {
4117cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
411887deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4119cbb145b9SZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
4120e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4121e3ec7017SPing-Ke Shih 	u8 final_rssi = 0, under_region = dig->pd_low_th_ofst;
41221c2423deSJohnson Lin 	u8 ofdm_cca_th;
41231c2423deSJohnson Lin 	s8 cck_cca_th;
41241c2423deSJohnson Lin 	u32 pd_val = 0;
4125e3ec7017SPing-Ke Shih 
4126e3ec7017SPing-Ke Shih 	under_region += PD_TH_SB_FLTR_CMP_VAL;
4127e3ec7017SPing-Ke Shih 
4128e3ec7017SPing-Ke Shih 	switch (cbw) {
4129e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
4130e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW40_CMP_VAL;
4131e3ec7017SPing-Ke Shih 		break;
4132e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
4133e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW80_CMP_VAL;
4134e3ec7017SPing-Ke Shih 		break;
41351c2423deSJohnson Lin 	case RTW89_CHANNEL_WIDTH_160:
41361c2423deSJohnson Lin 		under_region += PD_TH_BW160_CMP_VAL;
41371c2423deSJohnson Lin 		break;
4138e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
4139e3ec7017SPing-Ke Shih 		fallthrough;
4140e3ec7017SPing-Ke Shih 	default:
4141e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW20_CMP_VAL;
4142e3ec7017SPing-Ke Shih 		break;
4143e3ec7017SPing-Ke Shih 	}
4144e3ec7017SPing-Ke Shih 
4145e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dig->igi_rssi;
4146e3ec7017SPing-Ke Shih 
4147e3ec7017SPing-Ke Shih 	final_rssi = min_t(u8, rssi, dig->igi_rssi);
41481c2423deSJohnson Lin 	ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region,
4149e3ec7017SPing-Ke Shih 			      PD_TH_MAX_RSSI + under_region);
4150e3ec7017SPing-Ke Shih 
4151e3ec7017SPing-Ke Shih 	if (enable) {
41521c2423deSJohnson Lin 		pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1;
4153e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
41541c2423deSJohnson Lin 			    "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n",
41551c2423deSJohnson Lin 			    final_rssi, ofdm_cca_th, under_region, pd_val);
4156e3ec7017SPing-Ke Shih 	} else {
4157e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4158c51ed740SColin Ian King 			    "Dynamic PD th disabled, Set PD_low_bd=0\n");
4159e3ec7017SPing-Ke Shih 	}
4160e3ec7017SPing-Ke Shih 
416187deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
416287deaad9SEric Huang 			       dig_regs->pd_lower_bound_mask, pd_val);
416387deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
416487deaad9SEric Huang 			       dig_regs->pd_spatial_reuse_en, enable);
41651c2423deSJohnson Lin 
41661c2423deSJohnson Lin 	if (!rtwdev->hal.support_cckpd)
41671c2423deSJohnson Lin 		return;
41681c2423deSJohnson Lin 
41691c2423deSJohnson Lin 	cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI);
41701c2423deSJohnson Lin 	pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX);
41711c2423deSJohnson Lin 
41721c2423deSJohnson Lin 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
41731c2423deSJohnson Lin 		    "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n",
41741c2423deSJohnson Lin 		    final_rssi, cck_cca_th, under_region, pd_val);
41751c2423deSJohnson Lin 
41761c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1,
41771c2423deSJohnson Lin 			       B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable);
41781c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1,
41791c2423deSJohnson Lin 			       B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val);
4180e3ec7017SPing-Ke Shih }
4181e3ec7017SPing-Ke Shih 
4182e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev)
4183e3ec7017SPing-Ke Shih {
4184e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4185e3ec7017SPing-Ke Shih 
4186e3ec7017SPing-Ke Shih 	dig->bypass_dig = false;
4187e3ec7017SPing-Ke Shih 	rtw89_phy_dig_para_reset(rtwdev);
4188e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4189e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false);
4190e3ec7017SPing-Ke Shih 	rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4191e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_para(rtwdev);
4192e3ec7017SPing-Ke Shih }
4193e3ec7017SPing-Ke Shih 
4194e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10
4195e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev)
4196e3ec7017SPing-Ke Shih {
4197e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4198e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
4199e3ec7017SPing-Ke Shih 
4200e3ec7017SPing-Ke Shih 	if (unlikely(dig->bypass_dig)) {
4201e3ec7017SPing-Ke Shih 		dig->bypass_dig = false;
4202e3ec7017SPing-Ke Shih 		return;
4203e3ec7017SPing-Ke Shih 	}
4204e3ec7017SPing-Ke Shih 
4205e3ec7017SPing-Ke Shih 	if (!dig->is_linked_pre && is_linked) {
4206e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n");
4207e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4208e3ec7017SPing-Ke Shih 	} else if (dig->is_linked_pre && !is_linked) {
4209e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n");
4210e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4211e3ec7017SPing-Ke Shih 	}
4212e3ec7017SPing-Ke Shih 	dig->is_linked_pre = is_linked;
4213e3ec7017SPing-Ke Shih 
4214e3ec7017SPing-Ke Shih 	rtw89_phy_dig_igi_offset_by_env(rtwdev);
4215e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_rssi_info(rtwdev);
4216e3ec7017SPing-Ke Shih 
4217e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ?
4218e3ec7017SPing-Ke Shih 			    dig->igi_rssi - IGI_RSSI_MIN : 0;
4219e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX;
4220e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst;
4221e3ec7017SPing-Ke Shih 
4222e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min,
4223e3ec7017SPing-Ke Shih 				 dig->dyn_igi_max);
4224e3ec7017SPing-Ke Shih 
4225e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4226e3ec7017SPing-Ke Shih 		    "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n",
4227e3ec7017SPing-Ke Shih 		    dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min,
4228e3ec7017SPing-Ke Shih 		    dig->igi_fa_rssi);
4229e3ec7017SPing-Ke Shih 
4230bed4045fSJohnson Lin 	rtw89_phy_dig_config_igi(rtwdev);
4231e3ec7017SPing-Ke Shih 
4232e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en);
4233e3ec7017SPing-Ke Shih 
4234e3ec7017SPing-Ke Shih 	if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max)
4235e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true);
4236e3ec7017SPing-Ke Shih 	else
4237e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4238e3ec7017SPing-Ke Shih }
4239e3ec7017SPing-Ke Shih 
42407dbdf655SPing-Ke Shih static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta)
42417dbdf655SPing-Ke Shih {
42427dbdf655SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
42437dbdf655SPing-Ke Shih 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
42447dbdf655SPing-Ke Shih 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
42457dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
42467dbdf655SPing-Ke Shih 	bool *done = data;
42477dbdf655SPing-Ke Shih 	u8 rssi_a, rssi_b;
42487dbdf655SPing-Ke Shih 	u32 candidate;
42497dbdf655SPing-Ke Shih 
42507dbdf655SPing-Ke Shih 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls)
42517dbdf655SPing-Ke Shih 		return;
42527dbdf655SPing-Ke Shih 
42537dbdf655SPing-Ke Shih 	if (*done)
42547dbdf655SPing-Ke Shih 		return;
42557dbdf655SPing-Ke Shih 
42567dbdf655SPing-Ke Shih 	*done = true;
42577dbdf655SPing-Ke Shih 
42587dbdf655SPing-Ke Shih 	rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]);
42597dbdf655SPing-Ke Shih 	rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]);
42607dbdf655SPing-Ke Shih 
42617dbdf655SPing-Ke Shih 	if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH)
42627dbdf655SPing-Ke Shih 		candidate = RF_A;
42637dbdf655SPing-Ke Shih 	else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH)
42647dbdf655SPing-Ke Shih 		candidate = RF_B;
42657dbdf655SPing-Ke Shih 	else
42667dbdf655SPing-Ke Shih 		return;
42677dbdf655SPing-Ke Shih 
42687dbdf655SPing-Ke Shih 	if (hal->antenna_tx == candidate)
42697dbdf655SPing-Ke Shih 		return;
42707dbdf655SPing-Ke Shih 
42717dbdf655SPing-Ke Shih 	hal->antenna_tx = candidate;
42727dbdf655SPing-Ke Shih 	rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta);
42737dbdf655SPing-Ke Shih 
42747dbdf655SPing-Ke Shih 	if (hal->antenna_tx == RF_A) {
42757dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12);
42767dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11);
42777dbdf655SPing-Ke Shih 	} else if (hal->antenna_tx == RF_B) {
42787dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11);
42797dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12);
42807dbdf655SPing-Ke Shih 	}
42817dbdf655SPing-Ke Shih }
42827dbdf655SPing-Ke Shih 
42837dbdf655SPing-Ke Shih void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev)
42847dbdf655SPing-Ke Shih {
42857dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
42867dbdf655SPing-Ke Shih 	bool done = false;
42877dbdf655SPing-Ke Shih 
42887dbdf655SPing-Ke Shih 	if (!hal->tx_path_diversity)
42897dbdf655SPing-Ke Shih 		return;
42907dbdf655SPing-Ke Shih 
42917dbdf655SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
42927dbdf655SPing-Ke Shih 					  rtw89_phy_tx_path_div_sta_iter,
42937dbdf655SPing-Ke Shih 					  &done);
42947dbdf655SPing-Ke Shih }
42957dbdf655SPing-Ke Shih 
4296a90c613dSEric Huang #define ANTDIV_MAIN 0
4297a90c613dSEric Huang #define ANTDIV_AUX 1
4298a90c613dSEric Huang 
4299a90c613dSEric Huang static void rtw89_phy_antdiv_set_ant(struct rtw89_dev *rtwdev)
4300a90c613dSEric Huang {
4301a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4302a90c613dSEric Huang 	u8 default_ant, optional_ant;
4303a90c613dSEric Huang 
4304a90c613dSEric Huang 	if (!hal->ant_diversity || hal->antenna_tx == 0)
4305a90c613dSEric Huang 		return;
4306a90c613dSEric Huang 
4307a90c613dSEric Huang 	if (hal->antenna_tx == RF_B) {
4308a90c613dSEric Huang 		default_ant = ANTDIV_AUX;
4309a90c613dSEric Huang 		optional_ant = ANTDIV_MAIN;
4310a90c613dSEric Huang 	} else {
4311a90c613dSEric Huang 		default_ant = ANTDIV_MAIN;
4312a90c613dSEric Huang 		optional_ant = ANTDIV_AUX;
4313a90c613dSEric Huang 	}
4314a90c613dSEric Huang 
4315a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_CGCS_CTRL,
4316a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4317a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ORI,
4318a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4319a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ALT,
4320a90c613dSEric Huang 			      optional_ant, RTW89_PHY_0);
4321a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_TX_ORI,
4322a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4323a90c613dSEric Huang }
4324a90c613dSEric Huang 
4325e3715859SEric Huang static void rtw89_phy_swap_hal_antenna(struct rtw89_dev *rtwdev)
4326e3715859SEric Huang {
4327e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4328e3715859SEric Huang 
4329e3715859SEric Huang 	hal->antenna_rx = hal->antenna_rx == RF_A ? RF_B : RF_A;
4330e3715859SEric Huang 	hal->antenna_tx = hal->antenna_rx;
4331e3715859SEric Huang }
4332e3715859SEric Huang 
4333e3715859SEric Huang static void rtw89_phy_antdiv_decision_state(struct rtw89_dev *rtwdev)
4334e3715859SEric Huang {
4335e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4336e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4337e3715859SEric Huang 	bool no_change = false;
4338e3715859SEric Huang 	u8 main_rssi, aux_rssi;
43395feecb40SEric Huang 	u8 main_evm, aux_evm;
4340e3715859SEric Huang 	u32 candidate;
4341e3715859SEric Huang 
4342e3715859SEric Huang 	antdiv->get_stats = false;
4343e3715859SEric Huang 	antdiv->training_count = 0;
4344e3715859SEric Huang 
4345e3715859SEric Huang 	main_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->main_stats);
43465feecb40SEric Huang 	main_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->main_stats);
4347e3715859SEric Huang 	aux_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->aux_stats);
43485feecb40SEric Huang 	aux_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->aux_stats);
4349e3715859SEric Huang 
43505feecb40SEric Huang 	if (main_evm > aux_evm + ANTDIV_EVM_DIFF_TH)
43515feecb40SEric Huang 		candidate = RF_A;
43525feecb40SEric Huang 	else if (aux_evm > main_evm + ANTDIV_EVM_DIFF_TH)
43535feecb40SEric Huang 		candidate = RF_B;
43545feecb40SEric Huang 	else if (main_rssi > aux_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4355e3715859SEric Huang 		candidate = RF_A;
4356e3715859SEric Huang 	else if (aux_rssi > main_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4357e3715859SEric Huang 		candidate = RF_B;
4358e3715859SEric Huang 	else
4359e3715859SEric Huang 		no_change = true;
4360e3715859SEric Huang 
4361e3715859SEric Huang 	if (no_change) {
4362e3715859SEric Huang 		/* swap back from training antenna to original */
4363e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4364e3715859SEric Huang 		return;
4365e3715859SEric Huang 	}
4366e3715859SEric Huang 
4367e3715859SEric Huang 	hal->antenna_tx = candidate;
4368e3715859SEric Huang 	hal->antenna_rx = candidate;
4369e3715859SEric Huang }
4370e3715859SEric Huang 
4371e3715859SEric Huang static void rtw89_phy_antdiv_training_state(struct rtw89_dev *rtwdev)
4372e3715859SEric Huang {
4373e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4374e3715859SEric Huang 	u64 state_period;
4375e3715859SEric Huang 
4376e3715859SEric Huang 	if (antdiv->training_count % 2 == 0) {
4377e3715859SEric Huang 		if (antdiv->training_count == 0)
4378e3715859SEric Huang 			rtw89_phy_antdiv_sts_reset(rtwdev);
4379e3715859SEric Huang 
4380e3715859SEric Huang 		antdiv->get_stats = true;
4381e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_TRAINNING_INTVL);
4382e3715859SEric Huang 	} else {
4383e3715859SEric Huang 		antdiv->get_stats = false;
4384e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_DELAY);
4385e3715859SEric Huang 
4386e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4387e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4388e3715859SEric Huang 	}
4389e3715859SEric Huang 
4390e3715859SEric Huang 	antdiv->training_count++;
4391e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work,
4392e3715859SEric Huang 				     state_period);
4393e3715859SEric Huang }
4394e3715859SEric Huang 
4395e3715859SEric Huang void rtw89_phy_antdiv_work(struct work_struct *work)
4396e3715859SEric Huang {
4397e3715859SEric Huang 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
4398e3715859SEric Huang 						antdiv_work.work);
4399e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4400e3715859SEric Huang 
4401e3715859SEric Huang 	mutex_lock(&rtwdev->mutex);
4402e3715859SEric Huang 
4403e3715859SEric Huang 	if (antdiv->training_count <= ANTDIV_TRAINNING_CNT) {
4404e3715859SEric Huang 		rtw89_phy_antdiv_training_state(rtwdev);
4405e3715859SEric Huang 	} else {
4406e3715859SEric Huang 		rtw89_phy_antdiv_decision_state(rtwdev);
4407e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4408e3715859SEric Huang 	}
4409e3715859SEric Huang 
4410e3715859SEric Huang 	mutex_unlock(&rtwdev->mutex);
4411e3715859SEric Huang }
4412e3715859SEric Huang 
4413e3715859SEric Huang void rtw89_phy_antdiv_track(struct rtw89_dev *rtwdev)
4414e3715859SEric Huang {
4415e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4416e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4417e3715859SEric Huang 	u8 rssi, rssi_pre;
4418e3715859SEric Huang 
4419e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
4420e3715859SEric Huang 		return;
4421e3715859SEric Huang 
4422e3715859SEric Huang 	rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->target_stats);
4423e3715859SEric Huang 	rssi_pre = antdiv->rssi_pre;
4424e3715859SEric Huang 	antdiv->rssi_pre = rssi;
4425e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
4426e3715859SEric Huang 
4427e3715859SEric Huang 	if (abs((int)rssi - (int)rssi_pre) < ANTDIV_RSSI_DIFF_TH)
4428e3715859SEric Huang 		return;
4429e3715859SEric Huang 
4430e3715859SEric Huang 	antdiv->training_count = 0;
4431e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 0);
4432e3715859SEric Huang }
4433e3715859SEric Huang 
4434e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev)
4435e3ec7017SPing-Ke Shih {
4436e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_top_setting_init(rtwdev);
4437e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_setting_init(rtwdev);
4438e3ec7017SPing-Ke Shih }
4439e3ec7017SPing-Ke Shih 
4440e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev)
4441e3ec7017SPing-Ke Shih {
4442e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4443e3ec7017SPing-Ke Shih 
4444e3ec7017SPing-Ke Shih 	rtw89_phy_stat_init(rtwdev);
4445e3ec7017SPing-Ke Shih 
4446e3ec7017SPing-Ke Shih 	rtw89_chip_bb_sethw(rtwdev);
4447e3ec7017SPing-Ke Shih 
4448e3ec7017SPing-Ke Shih 	rtw89_phy_env_monitor_init(rtwdev);
4449eb4e52b3SPo Hao Huang 	rtw89_physts_parsing_init(rtwdev);
4450e3ec7017SPing-Ke Shih 	rtw89_phy_dig_init(rtwdev);
4451e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_init(rtwdev);
445229136c95SEric Huang 	rtw89_phy_ul_tb_info_init(rtwdev);
4453a90c613dSEric Huang 	rtw89_phy_antdiv_init(rtwdev);
4454f03bd042SPing-Ke Shih 	rtw89_chip_rfe_gpio(rtwdev);
4455a90c613dSEric Huang 	rtw89_phy_antdiv_set_ant(rtwdev);
4456e3ec7017SPing-Ke Shih 
4457e3ec7017SPing-Ke Shih 	rtw89_phy_init_rf_nctl(rtwdev);
4458e3ec7017SPing-Ke Shih 	rtw89_chip_rfk_init(rtwdev);
4459e3ec7017SPing-Ke Shih 	rtw89_load_txpwr_table(rtwdev, chip->byr_table);
4460e3ec7017SPing-Ke Shih 	rtw89_chip_set_txpwr_ctrl(rtwdev);
4461e3ec7017SPing-Ke Shih 	rtw89_chip_power_trim(rtwdev);
4462cd89a471SPing-Ke Shih 	rtw89_chip_cfg_txrx_path(rtwdev);
4463e3ec7017SPing-Ke Shih }
4464e3ec7017SPing-Ke Shih 
4465e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
4466e3ec7017SPing-Ke Shih {
4467a48f4fd0SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
4468e3ec7017SPing-Ke Shih 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
4469e3ec7017SPing-Ke Shih 	u8 bss_color;
4470e3ec7017SPing-Ke Shih 
4471f276e20bSJohannes Berg 	if (!vif->bss_conf.he_support || !vif->cfg.assoc)
4472e3ec7017SPing-Ke Shih 		return;
4473e3ec7017SPing-Ke Shih 
4474e3ec7017SPing-Ke Shih 	bss_color = vif->bss_conf.he_bss_color.color;
4475e3ec7017SPing-Ke Shih 
4476a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_VLD0, 0x1,
4477e3ec7017SPing-Ke Shih 			      phy_idx);
4478a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT,
4479a48f4fd0SEric Huang 			      bss_color, phy_idx);
4480a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID,
4481f276e20bSJohannes Berg 			      vif->cfg.aid, phy_idx);
4482e3ec7017SPing-Ke Shih }
4483db7fa61aSZong-Zhe Yang 
4484db7fa61aSZong-Zhe Yang static void
4485db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4486db7fa61aSZong-Zhe Yang {
4487db7fa61aSZong-Zhe Yang 	rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data);
4488db7fa61aSZong-Zhe Yang }
4489db7fa61aSZong-Zhe Yang 
4490db7fa61aSZong-Zhe Yang static void
4491db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4492db7fa61aSZong-Zhe Yang {
4493db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
4494db7fa61aSZong-Zhe Yang }
4495db7fa61aSZong-Zhe Yang 
4496db7fa61aSZong-Zhe Yang static void
4497db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4498db7fa61aSZong-Zhe Yang {
4499db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_set(rtwdev, def->addr, def->mask);
4500db7fa61aSZong-Zhe Yang }
4501db7fa61aSZong-Zhe Yang 
4502db7fa61aSZong-Zhe Yang static void
4503db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4504db7fa61aSZong-Zhe Yang {
4505db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_clr(rtwdev, def->addr, def->mask);
4506db7fa61aSZong-Zhe Yang }
4507db7fa61aSZong-Zhe Yang 
4508db7fa61aSZong-Zhe Yang static void
4509db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4510db7fa61aSZong-Zhe Yang {
4511db7fa61aSZong-Zhe Yang 	udelay(def->data);
4512db7fa61aSZong-Zhe Yang }
4513db7fa61aSZong-Zhe Yang 
4514db7fa61aSZong-Zhe Yang static void
4515db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = {
4516db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WRF] = _rfk_write_rf,
4517db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WM] = _rfk_write32_mask,
4518db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WS] = _rfk_write32_set,
4519db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WC] = _rfk_write32_clr,
4520db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_DELAY] = _rfk_delay,
4521db7fa61aSZong-Zhe Yang };
4522db7fa61aSZong-Zhe Yang 
4523db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM);
4524db7fa61aSZong-Zhe Yang 
4525db7fa61aSZong-Zhe Yang void
4526db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl)
4527db7fa61aSZong-Zhe Yang {
4528db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *p = tbl->defs;
4529db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *end = tbl->defs + tbl->size;
4530db7fa61aSZong-Zhe Yang 
4531db7fa61aSZong-Zhe Yang 	for (; p < end; p++)
4532db7fa61aSZong-Zhe Yang 		_rfk_handler[p->flag](rtwdev, p);
4533db7fa61aSZong-Zhe Yang }
4534db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser);
4535c7845551SPing-Ke Shih 
4536c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4
4537c7845551SPing-Ke Shih 
4538c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = {
4539c7845551SPing-Ke Shih 	{0xD934, 0xff0000},
4540c7845551SPing-Ke Shih 	{0xD934, 0xff000000},
4541c7845551SPing-Ke Shih 	{0xD938, 0xff},
4542c7845551SPing-Ke Shih 	{0xD934, 0xff00},
4543c7845551SPing-Ke Shih };
4544c7845551SPing-Ke Shih 
4545c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = {
4546c7845551SPing-Ke Shih 	{0xD930, 0xff0000},
4547c7845551SPing-Ke Shih 	{0xD930, 0xff000000},
4548c7845551SPing-Ke Shih 	{0xD934, 0xff},
4549c7845551SPing-Ke Shih 	{0xD930, 0xff00},
4550c7845551SPing-Ke Shih };
4551c7845551SPing-Ke Shih 
4552c7845551SPing-Ke Shih static
4553c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev,
4554c7845551SPing-Ke Shih 					   enum rtw89_mac_idx mac_idx,
4555c7845551SPing-Ke Shih 					   enum rtw89_tssi_bandedge_cfg bandedge_cfg,
4556c7845551SPing-Ke Shih 					   u32 val)
4557c7845551SPing-Ke Shih {
4558c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4559c7845551SPing-Ke Shih 	u32 reg;
4560c7845551SPing-Ke Shih 	int i;
4561c7845551SPing-Ke Shih 
4562c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4563c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_flat;
4564c7845551SPing-Ke Shih 	else
4565c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_level;
4566c7845551SPing-Ke Shih 
4567c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) {
4568c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4569c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, val);
4570c7845551SPing-Ke Shih 	}
4571c7845551SPing-Ke Shih }
4572c7845551SPing-Ke Shih 
4573c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = {
4574c7845551SPing-Ke Shih 	{0xD91C, 0xff000000},
4575c7845551SPing-Ke Shih 	{0xD920, 0xff},
4576c7845551SPing-Ke Shih 	{0xD920, 0xff00},
4577c7845551SPing-Ke Shih 	{0xD920, 0xff0000},
4578c7845551SPing-Ke Shih 	{0xD920, 0xff000000},
4579c7845551SPing-Ke Shih 	{0xD924, 0xff},
4580c7845551SPing-Ke Shih 	{0xD924, 0xff00},
4581c7845551SPing-Ke Shih 	{0xD914, 0xff000000},
4582c7845551SPing-Ke Shih 	{0xD918, 0xff},
4583c7845551SPing-Ke Shih 	{0xD918, 0xff00},
4584c7845551SPing-Ke Shih 	{0xD918, 0xff0000},
4585c7845551SPing-Ke Shih 	{0xD918, 0xff000000},
4586c7845551SPing-Ke Shih 	{0xD91C, 0xff},
4587c7845551SPing-Ke Shih 	{0xD91C, 0xff00},
4588c7845551SPing-Ke Shih 	{0xD91C, 0xff0000},
4589c7845551SPing-Ke Shih };
4590c7845551SPing-Ke Shih 
4591c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = {
4592c7845551SPing-Ke Shih 	{0xD910, 0xff},
4593c7845551SPing-Ke Shih 	{0xD910, 0xff00},
4594c7845551SPing-Ke Shih 	{0xD910, 0xff0000},
4595c7845551SPing-Ke Shih 	{0xD910, 0xff000000},
4596c7845551SPing-Ke Shih 	{0xD914, 0xff},
4597c7845551SPing-Ke Shih 	{0xD914, 0xff00},
4598c7845551SPing-Ke Shih 	{0xD914, 0xff0000},
4599c7845551SPing-Ke Shih 	{0xD908, 0xff},
4600c7845551SPing-Ke Shih 	{0xD908, 0xff00},
4601c7845551SPing-Ke Shih 	{0xD908, 0xff0000},
4602c7845551SPing-Ke Shih 	{0xD908, 0xff000000},
4603c7845551SPing-Ke Shih 	{0xD90C, 0xff},
4604c7845551SPing-Ke Shih 	{0xD90C, 0xff00},
4605c7845551SPing-Ke Shih 	{0xD90C, 0xff0000},
4606c7845551SPing-Ke Shih 	{0xD90C, 0xff000000},
4607c7845551SPing-Ke Shih };
4608c7845551SPing-Ke Shih 
4609c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev,
4610c7845551SPing-Ke Shih 					  enum rtw89_mac_idx mac_idx,
4611c7845551SPing-Ke Shih 					  enum rtw89_tssi_bandedge_cfg bandedge_cfg)
4612c7845551SPing-Ke Shih {
4613c7845551SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4614c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4615c7845551SPing-Ke Shih 	const u32 *data;
4616c7845551SPing-Ke Shih 	u32 reg;
4617c7845551SPing-Ke Shih 	int i;
4618c7845551SPing-Ke Shih 
4619c7845551SPing-Ke Shih 	if (bandedge_cfg >= RTW89_TSSI_CFG_NUM)
4620c7845551SPing-Ke Shih 		return;
4621c7845551SPing-Ke Shih 
4622c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4623c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_flat;
4624c7845551SPing-Ke Shih 	else
4625c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_level;
4626c7845551SPing-Ke Shih 
4627c7845551SPing-Ke Shih 	data = chip->tssi_dbw_table->data[bandedge_cfg];
4628c7845551SPing-Ke Shih 
4629c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) {
4630c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4631c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]);
4632c7845551SPing-Ke Shih 	}
4633c7845551SPing-Ke Shih 
4634c7845551SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx);
4635c7845551SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg);
4636c7845551SPing-Ke Shih 
4637c7845551SPing-Ke Shih 	rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg,
4638c7845551SPing-Ke Shih 					      data[RTW89_TSSI_SBW20]);
4639c7845551SPing-Ke Shih }
4640c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg);
4641bb9040b3SPo-Hao Huang 
4642bb9040b3SPo-Hao Huang static
4643bb9040b3SPo-Hao Huang const u8 rtw89_ch_base_table[16] = {1, 0xff,
4644bb9040b3SPo-Hao Huang 				    36, 100, 132, 149, 0xff,
4645bb9040b3SPo-Hao Huang 				    1, 33, 65, 97, 129, 161, 193, 225, 0xff};
4646bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_2G		0
4647bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_FIRST	2
4648bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_LAST	5
4649bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_FIRST	7
4650bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_LAST	14
4651bb9040b3SPo-Hao Huang 
4652bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_MASK		GENMASK(7, 4)
4653bb9040b3SPo-Hao Huang #define RTW89_CH_OFFSET_MASK		GENMASK(3, 0)
4654bb9040b3SPo-Hao Huang 
4655bb9040b3SPo-Hao Huang u8 rtw89_encode_chan_idx(struct rtw89_dev *rtwdev, u8 central_ch, u8 band)
4656bb9040b3SPo-Hao Huang {
4657bb9040b3SPo-Hao Huang 	u8 chan_idx;
4658bb9040b3SPo-Hao Huang 	u8 last, first;
4659bb9040b3SPo-Hao Huang 	u8 idx;
4660bb9040b3SPo-Hao Huang 
4661bb9040b3SPo-Hao Huang 	switch (band) {
4662bb9040b3SPo-Hao Huang 	case RTW89_BAND_2G:
4663bb9040b3SPo-Hao Huang 		chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, RTW89_CH_BASE_IDX_2G) |
4664bb9040b3SPo-Hao Huang 			   FIELD_PREP(RTW89_CH_OFFSET_MASK, central_ch);
4665bb9040b3SPo-Hao Huang 		return chan_idx;
4666bb9040b3SPo-Hao Huang 	case RTW89_BAND_5G:
4667bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_5G_FIRST;
4668bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_5G_LAST;
4669bb9040b3SPo-Hao Huang 		break;
4670bb9040b3SPo-Hao Huang 	case RTW89_BAND_6G:
4671bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_6G_FIRST;
4672bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_6G_LAST;
4673bb9040b3SPo-Hao Huang 		break;
4674bb9040b3SPo-Hao Huang 	default:
4675bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unsupported band %d\n", band);
4676bb9040b3SPo-Hao Huang 		return 0;
4677bb9040b3SPo-Hao Huang 	}
4678bb9040b3SPo-Hao Huang 
4679bb9040b3SPo-Hao Huang 	for (idx = last; idx >= first; idx--)
4680bb9040b3SPo-Hao Huang 		if (central_ch >= rtw89_ch_base_table[idx])
4681bb9040b3SPo-Hao Huang 			break;
4682bb9040b3SPo-Hao Huang 
4683bb9040b3SPo-Hao Huang 	if (idx < first) {
4684bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unknown band %d channel %d\n", band, central_ch);
4685bb9040b3SPo-Hao Huang 		return 0;
4686bb9040b3SPo-Hao Huang 	}
4687bb9040b3SPo-Hao Huang 
4688bb9040b3SPo-Hao Huang 	chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, idx) |
4689bb9040b3SPo-Hao Huang 		   FIELD_PREP(RTW89_CH_OFFSET_MASK,
4690bb9040b3SPo-Hao Huang 			      (central_ch - rtw89_ch_base_table[idx]) >> 1);
4691bb9040b3SPo-Hao Huang 	return chan_idx;
4692bb9040b3SPo-Hao Huang }
4693bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_encode_chan_idx);
4694bb9040b3SPo-Hao Huang 
4695bb9040b3SPo-Hao Huang void rtw89_decode_chan_idx(struct rtw89_dev *rtwdev, u8 chan_idx,
4696bb9040b3SPo-Hao Huang 			   u8 *ch, enum nl80211_band *band)
4697bb9040b3SPo-Hao Huang {
4698bb9040b3SPo-Hao Huang 	u8 idx, offset;
4699bb9040b3SPo-Hao Huang 
4700bb9040b3SPo-Hao Huang 	idx = FIELD_GET(RTW89_CH_BASE_IDX_MASK, chan_idx);
4701bb9040b3SPo-Hao Huang 	offset = FIELD_GET(RTW89_CH_OFFSET_MASK, chan_idx);
4702bb9040b3SPo-Hao Huang 
4703bb9040b3SPo-Hao Huang 	if (idx == RTW89_CH_BASE_IDX_2G) {
4704bb9040b3SPo-Hao Huang 		*band = NL80211_BAND_2GHZ;
4705bb9040b3SPo-Hao Huang 		*ch = offset;
4706bb9040b3SPo-Hao Huang 		return;
4707bb9040b3SPo-Hao Huang 	}
4708bb9040b3SPo-Hao Huang 
4709bb9040b3SPo-Hao Huang 	*band = idx <= RTW89_CH_BASE_IDX_5G_LAST ? NL80211_BAND_5GHZ : NL80211_BAND_6GHZ;
4710bb9040b3SPo-Hao Huang 	*ch = rtw89_ch_base_table[idx] + (offset << 1);
4711bb9040b3SPo-Hao Huang }
4712bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_decode_chan_idx);
4713280c4447SChih-Kang Chang 
4714280c4447SChih-Kang Chang #define EDCCA_DEFAULT 249
4715280c4447SChih-Kang Chang void rtw89_phy_config_edcca(struct rtw89_dev *rtwdev, bool scan)
4716280c4447SChih-Kang Chang {
4717280c4447SChih-Kang Chang 	u32 reg = rtwdev->chip->edcca_lvl_reg;
4718280c4447SChih-Kang Chang 	struct rtw89_hal *hal = &rtwdev->hal;
4719280c4447SChih-Kang Chang 	u32 val;
4720280c4447SChih-Kang Chang 
4721280c4447SChih-Kang Chang 	if (scan) {
4722280c4447SChih-Kang Chang 		hal->edcca_bak = rtw89_phy_read32(rtwdev, reg);
4723280c4447SChih-Kang Chang 		val = hal->edcca_bak;
4724280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_A_MSK);
4725280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_P_MSK);
4726280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_PPDU_LVL_MSK);
4727280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, val);
4728280c4447SChih-Kang Chang 	} else {
4729280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, hal->edcca_bak);
4730280c4447SChih-Kang Chang 	}
4731280c4447SChih-Kang Chang }
4732