xref: /openbmc/linux/drivers/net/wireless/realtek/rtw89/phy.c (revision fac59652993f075d57860769c99045b3ca18780d)
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 
get_max_amsdu_len(struct rtw89_dev * rtwdev,const struct rtw89_ra_report * report)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 
get_mcs_ra_mask(u16 mcs_map,u8 highest_mcs,u8 gap)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 
get_he_ra_mask(struct ieee80211_sta * sta)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
rtw89_phy_ra_mask_rssi(struct rtw89_dev * rtwdev,u8 rssi,u8 ratr_state)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 
rtw89_phy_ra_mask_recover(u64 ra_mask,u64 ra_mask_bak)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 
rtw89_phy_ra_mask_cfg(struct rtw89_dev * rtwdev,struct rtw89_sta * rtwsta,const struct rtw89_chan * chan)136ad3dc722SZong-Zhe Yang static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta,
137ad3dc722SZong-Zhe Yang 				 const struct rtw89_chan *chan)
138e3ec7017SPing-Ke Shih {
139e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta);
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 
rtw89_phy_ra_gi_ltf(struct rtw89_dev * rtwdev,struct rtw89_sta * rtwsta,const struct rtw89_chan * chan,bool * fix_giltf_en,u8 * fix_giltf)1980891b366SKuan-Chung Chen static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev,
1990891b366SKuan-Chung Chen 				struct rtw89_sta *rtwsta,
200ad3dc722SZong-Zhe Yang 				const struct rtw89_chan *chan,
2010891b366SKuan-Chung Chen 				bool *fix_giltf_en, u8 *fix_giltf)
2020891b366SKuan-Chung Chen {
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 
rtw89_phy_ra_sta_update(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta,bool csi)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;
239ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
240ad3dc722SZong-Zhe Yang 						       rtwvif->sub_entity_idx);
2413788c599SDian-Syuan Yang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
242e3ec7017SPing-Ke Shih 	const u64 *high_rate_masks = rtw89_ra_mask_ht_rates;
243e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi);
244e3ec7017SPing-Ke Shih 	u64 ra_mask = 0;
2453c2c2e2eSChien-Hsun Liao 	u64 ra_mask_bak;
246e3ec7017SPing-Ke Shih 	u8 mode = 0;
247e3ec7017SPing-Ke Shih 	u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY;
248e3ec7017SPing-Ke Shih 	u8 bw_mode = 0;
249e3ec7017SPing-Ke Shih 	u8 stbc_en = 0;
250e3ec7017SPing-Ke Shih 	u8 ldpc_en = 0;
2510891b366SKuan-Chung Chen 	u8 fix_giltf = 0;
252e3ec7017SPing-Ke Shih 	u8 i;
253e3ec7017SPing-Ke Shih 	bool sgi = false;
2540891b366SKuan-Chung Chen 	bool fix_giltf_en = false;
255e3ec7017SPing-Ke Shih 
256e3ec7017SPing-Ke Shih 	memset(ra, 0, sizeof(*ra));
257e3ec7017SPing-Ke Shih 	/* Set the ra mask from sta's capability */
258046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
259e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HE;
260e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HE;
261e3ec7017SPing-Ke Shih 		ra_mask |= get_he_ra_mask(sta);
262e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_he_rates;
263046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] &
264e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
265e3ec7017SPing-Ke Shih 			stbc_en = 1;
266046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] &
267e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
268e3ec7017SPing-Ke Shih 			ldpc_en = 1;
269ad3dc722SZong-Zhe Yang 		rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, chan, &fix_giltf_en, &fix_giltf);
270046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
271046d2e7cSSriram R 		u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map);
272e3ec7017SPing-Ke Shih 
273e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_VHT;
274e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_VHT;
275e3ec7017SPing-Ke Shih 		/* MCS9, MCS8, MCS7 */
276e3ec7017SPing-Ke Shih 		ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1);
277e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_vht_rates;
278046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
279e3ec7017SPing-Ke Shih 			stbc_en = 1;
280046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
281e3ec7017SPing-Ke Shih 			ldpc_en = 1;
282046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
283e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HT;
284e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HT;
285046d2e7cSSriram R 		ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) |
286046d2e7cSSriram R 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) |
287*239ac7faSPing-Ke Shih 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[1] << 24) |
288*239ac7faSPing-Ke Shih 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[0] << 12);
289e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_ht_rates;
290046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
291e3ec7017SPing-Ke Shih 			stbc_en = 1;
292046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
293e3ec7017SPing-Ke Shih 			ldpc_en = 1;
294e3ec7017SPing-Ke Shih 	}
295e3ec7017SPing-Ke Shih 
296cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
297f76b3276SPing-Ke Shih 	case RTW89_BAND_2G:
298046d2e7cSSriram R 		ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ];
2993788c599SDian-Syuan Yang 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf)
300e3ec7017SPing-Ke Shih 			mode |= RTW89_RA_MODE_CCK;
3013788c599SDian-Syuan Yang 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0)
3023788c599SDian-Syuan Yang 			mode |= RTW89_RA_MODE_OFDM;
303f76b3276SPing-Ke Shih 		break;
304f76b3276SPing-Ke Shih 	case RTW89_BAND_5G:
305046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4;
306e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
307f76b3276SPing-Ke Shih 		break;
308f76b3276SPing-Ke Shih 	case RTW89_BAND_6G:
309046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4;
310f76b3276SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
311f76b3276SPing-Ke Shih 		break;
312f76b3276SPing-Ke Shih 	default:
313f76b3276SPing-Ke Shih 		rtw89_err(rtwdev, "Unknown band type\n");
314f76b3276SPing-Ke Shih 		break;
315e3ec7017SPing-Ke Shih 	}
316e3ec7017SPing-Ke Shih 
3173c2c2e2eSChien-Hsun Liao 	ra_mask_bak = ra_mask;
3183c2c2e2eSChien-Hsun Liao 
319e3ec7017SPing-Ke Shih 	if (mode >= RTW89_RA_MODE_HT) {
3203c2c2e2eSChien-Hsun Liao 		u64 mask = 0;
321e3ec7017SPing-Ke Shih 		for (i = 0; i < rtwdev->hal.tx_nss; i++)
3223c2c2e2eSChien-Hsun Liao 			mask |= high_rate_masks[i];
323e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_OFDM)
3243c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBOFDM_RATES;
325e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_CCK)
3263c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBCCK_RATES;
3273c2c2e2eSChien-Hsun Liao 		ra_mask &= mask;
328e3ec7017SPing-Ke Shih 	} else if (mode & RTW89_RA_MODE_OFDM) {
3293c2c2e2eSChien-Hsun Liao 		ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES);
330e3ec7017SPing-Ke Shih 	}
331e3ec7017SPing-Ke Shih 
3323c2c2e2eSChien-Hsun Liao 	if (mode != RTW89_RA_MODE_CCK)
333e3ec7017SPing-Ke Shih 		ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0);
3343c2c2e2eSChien-Hsun Liao 
3353c2c2e2eSChien-Hsun Liao 	ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak);
336ad3dc722SZong-Zhe Yang 	ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta, chan);
337e3ec7017SPing-Ke Shih 
338046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
339167044afSPing-Ke Shih 	case IEEE80211_STA_RX_BW_160:
340167044afSPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_160;
341046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
342046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160);
343167044afSPing-Ke Shih 		break;
344e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_80:
345e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_80;
346046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
347046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
348e3ec7017SPing-Ke Shih 		break;
349e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_40:
350e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_40;
351046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
352046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40);
353e3ec7017SPing-Ke Shih 		break;
354e3ec7017SPing-Ke Shih 	default:
355e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_20;
356046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
357046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
358e3ec7017SPing-Ke Shih 		break;
359e3ec7017SPing-Ke Shih 	}
360e3ec7017SPing-Ke Shih 
361046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] &
362e3ec7017SPing-Ke Shih 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM)
363e3ec7017SPing-Ke Shih 		ra->dcm_cap = 1;
364e3ec7017SPing-Ke Shih 
3653788c599SDian-Syuan Yang 	if (rate_pattern->enable && !vif->p2p) {
366ad3dc722SZong-Zhe Yang 		ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta, chan);
367e3ec7017SPing-Ke Shih 		ra_mask &= rate_pattern->ra_mask;
368e3ec7017SPing-Ke Shih 		mode = rate_pattern->ra_mode;
369e3ec7017SPing-Ke Shih 	}
370e3ec7017SPing-Ke Shih 
371e3ec7017SPing-Ke Shih 	ra->bw_cap = bw_mode;
37225ed1a17SPing-Ke Shih 	ra->er_cap = rtwsta->er_cap;
373e3ec7017SPing-Ke Shih 	ra->mode_ctrl = mode;
374e3ec7017SPing-Ke Shih 	ra->macid = rtwsta->mac_id;
375e3ec7017SPing-Ke Shih 	ra->stbc_cap = stbc_en;
376e3ec7017SPing-Ke Shih 	ra->ldpc_cap = ldpc_en;
377046d2e7cSSriram R 	ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1;
378e3ec7017SPing-Ke Shih 	ra->en_sgi = sgi;
379e3ec7017SPing-Ke Shih 	ra->ra_mask = ra_mask;
3800891b366SKuan-Chung Chen 	ra->fix_giltf_en = fix_giltf_en;
3810891b366SKuan-Chung Chen 	ra->fix_giltf = fix_giltf;
382e3ec7017SPing-Ke Shih 
383e3ec7017SPing-Ke Shih 	if (!csi)
384e3ec7017SPing-Ke Shih 		return;
385e3ec7017SPing-Ke Shih 
386e3ec7017SPing-Ke Shih 	ra->fixed_csi_rate_en = false;
387e3ec7017SPing-Ke Shih 	ra->ra_csi_rate_en = true;
388e3ec7017SPing-Ke Shih 	ra->cr_tbl_sel = false;
389e3ec7017SPing-Ke Shih 	ra->band_num = rtwvif->phy_idx;
390e3ec7017SPing-Ke Shih 	ra->csi_bw = bw_mode;
391e3ec7017SPing-Ke Shih 	ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32;
392e3ec7017SPing-Ke Shih 	ra->csi_mcs_ss_idx = 5;
393e3ec7017SPing-Ke Shih 	ra->csi_mode = csi_mode;
394e3ec7017SPing-Ke Shih }
395e3ec7017SPing-Ke Shih 
rtw89_phy_ra_updata_sta(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta,u32 changed)3969d9a9edcSPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta,
3979d9a9edcSPing-Ke Shih 			     u32 changed)
398e3ec7017SPing-Ke Shih {
399e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
400e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
401e3ec7017SPing-Ke Shih 
402e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, false);
4039d9a9edcSPing-Ke Shih 
4049d9a9edcSPing-Ke Shih 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED)
405e3ec7017SPing-Ke Shih 		ra->upd_mask = 1;
4069d9a9edcSPing-Ke Shih 	if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED))
4079d9a9edcSPing-Ke Shih 		ra->upd_bw_nss_mask = 1;
4089d9a9edcSPing-Ke Shih 
409e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
410e3ec7017SPing-Ke Shih 		    "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d",
411e3ec7017SPing-Ke Shih 		    ra->macid,
412e3ec7017SPing-Ke Shih 		    ra->bw_cap,
413e3ec7017SPing-Ke Shih 		    ra->ss_num,
414e3ec7017SPing-Ke Shih 		    ra->en_sgi,
415e3ec7017SPing-Ke Shih 		    ra->giltf);
416e3ec7017SPing-Ke Shih 
417e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, false);
418e3ec7017SPing-Ke Shih }
419e3ec7017SPing-Ke Shih 
__check_rate_pattern(struct rtw89_phy_rate_pattern * next,u16 rate_base,u64 ra_mask,u8 ra_mode,u32 rate_ctrl,u32 ctrl_skip,bool force)420e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next,
421e3ec7017SPing-Ke Shih 				 u16 rate_base, u64 ra_mask, u8 ra_mode,
422e3ec7017SPing-Ke Shih 				 u32 rate_ctrl, u32 ctrl_skip, bool force)
423e3ec7017SPing-Ke Shih {
424e3ec7017SPing-Ke Shih 	u8 n, c;
425e3ec7017SPing-Ke Shih 
426e3ec7017SPing-Ke Shih 	if (rate_ctrl == ctrl_skip)
427e3ec7017SPing-Ke Shih 		return true;
428e3ec7017SPing-Ke Shih 
429e3ec7017SPing-Ke Shih 	n = hweight32(rate_ctrl);
430e3ec7017SPing-Ke Shih 	if (n == 0)
431e3ec7017SPing-Ke Shih 		return true;
432e3ec7017SPing-Ke Shih 
433e3ec7017SPing-Ke Shih 	if (force && n != 1)
434e3ec7017SPing-Ke Shih 		return false;
435e3ec7017SPing-Ke Shih 
436e3ec7017SPing-Ke Shih 	if (next->enable)
437e3ec7017SPing-Ke Shih 		return false;
438e3ec7017SPing-Ke Shih 
439e3ec7017SPing-Ke Shih 	c = __fls(rate_ctrl);
440e3ec7017SPing-Ke Shih 	next->rate = rate_base + c;
441e3ec7017SPing-Ke Shih 	next->ra_mode = ra_mode;
442e3ec7017SPing-Ke Shih 	next->ra_mask = ra_mask;
443e3ec7017SPing-Ke Shih 	next->enable = true;
444e3ec7017SPing-Ke Shih 
445e3ec7017SPing-Ke Shih 	return true;
446e3ec7017SPing-Ke Shih }
447e3ec7017SPing-Ke Shih 
4482ef14155SZong-Zhe Yang #define RTW89_HW_RATE_BY_CHIP_GEN(rate) \
4492ef14155SZong-Zhe Yang 	{ \
4502ef14155SZong-Zhe Yang 		[RTW89_CHIP_AX] = RTW89_HW_RATE_ ## rate, \
4512ef14155SZong-Zhe Yang 		[RTW89_CHIP_BE] = RTW89_HW_RATE_V1_ ## rate, \
4522ef14155SZong-Zhe Yang 	}
4532ef14155SZong-Zhe Yang 
rtw89_phy_rate_pattern_vif(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)454e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev,
455e3ec7017SPing-Ke Shih 				struct ieee80211_vif *vif,
456e3ec7017SPing-Ke Shih 				const struct cfg80211_bitrate_mask *mask)
457e3ec7017SPing-Ke Shih {
458e3ec7017SPing-Ke Shih 	struct ieee80211_supported_band *sband;
459e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
460e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern next_pattern = {0};
461ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
462ad3dc722SZong-Zhe Yang 						       rtwvif->sub_entity_idx);
4632ef14155SZong-Zhe Yang 	static const u16 hw_rate_he[][RTW89_CHIP_GEN_NUM] = {
4642ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS1_MCS0),
4652ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS2_MCS0),
4662ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS3_MCS0),
4672ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS4_MCS0),
4682ef14155SZong-Zhe Yang 	};
4692ef14155SZong-Zhe Yang 	static const u16 hw_rate_vht[][RTW89_CHIP_GEN_NUM] = {
4702ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS1_MCS0),
4712ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS2_MCS0),
4722ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS3_MCS0),
4732ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS4_MCS0),
4742ef14155SZong-Zhe Yang 	};
4752ef14155SZong-Zhe Yang 	static const u16 hw_rate_ht[][RTW89_CHIP_GEN_NUM] = {
4762ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS0),
4772ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS8),
4782ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS16),
4792ef14155SZong-Zhe Yang 		RTW89_HW_RATE_BY_CHIP_GEN(MCS24),
4802ef14155SZong-Zhe Yang 	};
481cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
482a06d2dd7SZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
4832ef14155SZong-Zhe Yang 	enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
484e3ec7017SPing-Ke Shih 	u8 tx_nss = rtwdev->hal.tx_nss;
485e3ec7017SPing-Ke Shih 	u8 i;
486e3ec7017SPing-Ke Shih 
487e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
4882ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_he[i][chip_gen],
489e3ec7017SPing-Ke Shih 					  RA_MASK_HE_RATES, RTW89_RA_MODE_HE,
490a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].he_mcs[i],
491e3ec7017SPing-Ke Shih 					  0, true))
492e3ec7017SPing-Ke Shih 			goto out;
493e3ec7017SPing-Ke Shih 
494e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
4952ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i][chip_gen],
496e3ec7017SPing-Ke Shih 					  RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT,
497a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].vht_mcs[i],
498e3ec7017SPing-Ke Shih 					  0, true))
499e3ec7017SPing-Ke Shih 			goto out;
500e3ec7017SPing-Ke Shih 
501e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
5022ef14155SZong-Zhe Yang 		if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i][chip_gen],
503e3ec7017SPing-Ke Shih 					  RA_MASK_HT_RATES, RTW89_RA_MODE_HT,
504a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].ht_mcs[i],
505e3ec7017SPing-Ke Shih 					  0, true))
506e3ec7017SPing-Ke Shih 			goto out;
507e3ec7017SPing-Ke Shih 
508e3ec7017SPing-Ke Shih 	/* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and
509e3ec7017SPing-Ke Shih 	 * require at least one basic rate for ieee80211_set_bitrate_mask,
510e3ec7017SPing-Ke Shih 	 * so the decision just depends on if all bitrates are set or not.
511e3ec7017SPing-Ke Shih 	 */
512a06d2dd7SZong-Zhe Yang 	sband = rtwdev->hw->wiphy->bands[nl_band];
513e3ec7017SPing-Ke Shih 	if (band == RTW89_BAND_2G) {
514e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1,
515e3ec7017SPing-Ke Shih 					  RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES,
516e3ec7017SPing-Ke Shih 					  RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM,
517a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
518e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
519e3ec7017SPing-Ke Shih 			goto out;
520e3ec7017SPing-Ke Shih 	} else {
521e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6,
522e3ec7017SPing-Ke Shih 					  RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM,
523a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
524e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
525e3ec7017SPing-Ke Shih 			goto out;
526e3ec7017SPing-Ke Shih 	}
527e3ec7017SPing-Ke Shih 
528e3ec7017SPing-Ke Shih 	if (!next_pattern.enable)
529e3ec7017SPing-Ke Shih 		goto out;
530e3ec7017SPing-Ke Shih 
531e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern = next_pattern;
532e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
533e3ec7017SPing-Ke Shih 		    "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n",
534e3ec7017SPing-Ke Shih 		    next_pattern.rate,
535e3ec7017SPing-Ke Shih 		    next_pattern.ra_mask,
536e3ec7017SPing-Ke Shih 		    next_pattern.ra_mode);
537e3ec7017SPing-Ke Shih 	return;
538e3ec7017SPing-Ke Shih 
539e3ec7017SPing-Ke Shih out:
540e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern.enable = false;
541e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n");
542e3ec7017SPing-Ke Shih }
543e3ec7017SPing-Ke Shih 
rtw89_phy_ra_updata_sta_iter(void * data,struct ieee80211_sta * sta)544e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta)
545e3ec7017SPing-Ke Shih {
546e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
547e3ec7017SPing-Ke Shih 
5489d9a9edcSPing-Ke Shih 	rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
549e3ec7017SPing-Ke Shih }
550e3ec7017SPing-Ke Shih 
rtw89_phy_ra_update(struct rtw89_dev * rtwdev)551e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev)
552e3ec7017SPing-Ke Shih {
553e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
554e3ec7017SPing-Ke Shih 					  rtw89_phy_ra_updata_sta_iter,
555e3ec7017SPing-Ke Shih 					  rtwdev);
556e3ec7017SPing-Ke Shih }
557e3ec7017SPing-Ke Shih 
rtw89_phy_ra_assoc(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta)558e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta)
559e3ec7017SPing-Ke Shih {
560e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
561e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
562e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR;
563e3ec7017SPing-Ke Shih 	bool csi = rtw89_sta_has_beamformer_cap(sta);
564e3ec7017SPing-Ke Shih 
565e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, csi);
566e3ec7017SPing-Ke Shih 
567e3ec7017SPing-Ke Shih 	if (rssi > 40)
568e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 1;
569e3ec7017SPing-Ke Shih 	else if (rssi > 20)
570e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 2;
571e3ec7017SPing-Ke Shih 	else if (rssi > 1)
572e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 3;
573e3ec7017SPing-Ke Shih 	else
574e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 0;
575e3ec7017SPing-Ke Shih 	ra->upd_all = 1;
576e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
577e3ec7017SPing-Ke Shih 		    "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d",
578e3ec7017SPing-Ke Shih 		    ra->macid,
579e3ec7017SPing-Ke Shih 		    ra->mode_ctrl,
580e3ec7017SPing-Ke Shih 		    ra->bw_cap,
581e3ec7017SPing-Ke Shih 		    ra->ss_num,
582e3ec7017SPing-Ke Shih 		    ra->init_rate_lv);
583e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
584e3ec7017SPing-Ke Shih 		    "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d",
585e3ec7017SPing-Ke Shih 		    ra->dcm_cap,
586e3ec7017SPing-Ke Shih 		    ra->er_cap,
587e3ec7017SPing-Ke Shih 		    ra->ldpc_cap,
588e3ec7017SPing-Ke Shih 		    ra->stbc_cap,
589e3ec7017SPing-Ke Shih 		    ra->en_sgi,
590e3ec7017SPing-Ke Shih 		    ra->giltf);
591e3ec7017SPing-Ke Shih 
592e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, csi);
593e3ec7017SPing-Ke Shih }
594e3ec7017SPing-Ke Shih 
rtw89_phy_get_txsc(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_bandwidth dbw)595e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev,
5963e5831caSZong-Zhe Yang 		      const struct rtw89_chan *chan,
597e3ec7017SPing-Ke Shih 		      enum rtw89_bandwidth dbw)
598e3ec7017SPing-Ke Shih {
5993e5831caSZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
6003e5831caSZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
6013e5831caSZong-Zhe Yang 	u8 central_ch = chan->channel;
602e3ec7017SPing-Ke Shih 	u8 txsc_idx = 0;
603e3ec7017SPing-Ke Shih 	u8 tmp = 0;
604e3ec7017SPing-Ke Shih 
605e3ec7017SPing-Ke Shih 	if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20)
606e3ec7017SPing-Ke Shih 		return txsc_idx;
607e3ec7017SPing-Ke Shih 
608e3ec7017SPing-Ke Shih 	switch (cbw) {
609e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
610e3ec7017SPing-Ke Shih 		txsc_idx = pri_ch > central_ch ? 1 : 2;
611e3ec7017SPing-Ke Shih 		break;
612e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
613e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
614e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
615e3ec7017SPing-Ke Shih 				txsc_idx = (pri_ch - central_ch) >> 1;
616e3ec7017SPing-Ke Shih 			else
617e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 1;
618e3ec7017SPing-Ke Shih 		} else {
619e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 9 : 10;
620e3ec7017SPing-Ke Shih 		}
621e3ec7017SPing-Ke Shih 		break;
622e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
623e3ec7017SPing-Ke Shih 		if (pri_ch > central_ch)
624e3ec7017SPing-Ke Shih 			tmp = (pri_ch - central_ch) >> 1;
625e3ec7017SPing-Ke Shih 		else
626e3ec7017SPing-Ke Shih 			tmp = ((central_ch - pri_ch) >> 1) + 1;
627e3ec7017SPing-Ke Shih 
628e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
629e3ec7017SPing-Ke Shih 			txsc_idx = tmp;
630e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
631e3ec7017SPing-Ke Shih 			if (tmp == 1 || tmp == 3)
632e3ec7017SPing-Ke Shih 				txsc_idx = 9;
633e3ec7017SPing-Ke Shih 			else if (tmp == 5 || tmp == 7)
634e3ec7017SPing-Ke Shih 				txsc_idx = 11;
635e3ec7017SPing-Ke Shih 			else if (tmp == 2 || tmp == 4)
636e3ec7017SPing-Ke Shih 				txsc_idx = 10;
637e3ec7017SPing-Ke Shih 			else if (tmp == 6 || tmp == 8)
638e3ec7017SPing-Ke Shih 				txsc_idx = 12;
639e3ec7017SPing-Ke Shih 			else
640e3ec7017SPing-Ke Shih 				return 0xff;
641e3ec7017SPing-Ke Shih 		} else {
642e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 13 : 14;
643e3ec7017SPing-Ke Shih 		}
644e3ec7017SPing-Ke Shih 		break;
645e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80_80:
646e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
647e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
648e3ec7017SPing-Ke Shih 				txsc_idx = (10 - (pri_ch - central_ch)) >> 1;
649e3ec7017SPing-Ke Shih 			else
650e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 5;
651e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
652e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 10 : 12;
653e3ec7017SPing-Ke Shih 		} else {
654e3ec7017SPing-Ke Shih 			txsc_idx = 14;
655e3ec7017SPing-Ke Shih 		}
656e3ec7017SPing-Ke Shih 		break;
657e3ec7017SPing-Ke Shih 	default:
658e3ec7017SPing-Ke Shih 		break;
659e3ec7017SPing-Ke Shih 	}
660e3ec7017SPing-Ke Shih 
661e3ec7017SPing-Ke Shih 	return txsc_idx;
662e3ec7017SPing-Ke Shih }
663861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc);
664e3ec7017SPing-Ke Shih 
rtw89_phy_check_swsi_busy(struct rtw89_dev * rtwdev)66584d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev)
66684d0e33eSChung-Hsuan Hung {
66784d0e33eSChung-Hsuan Hung 	return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) ||
66884d0e33eSChung-Hsuan Hung 	       !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1);
66984d0e33eSChung-Hsuan Hung }
67084d0e33eSChung-Hsuan Hung 
rtw89_phy_read_rf(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask)671e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
672e3ec7017SPing-Ke Shih 		      u32 addr, u32 mask)
673e3ec7017SPing-Ke Shih {
674e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
675e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
676e3ec7017SPing-Ke Shih 	u32 val, direct_addr;
677e3ec7017SPing-Ke Shih 
678e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
679e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
680e3ec7017SPing-Ke Shih 		return INV_RF_DATA;
681e3ec7017SPing-Ke Shih 	}
682e3ec7017SPing-Ke Shih 
683e3ec7017SPing-Ke Shih 	addr &= 0xff;
684e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
685e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
686e3ec7017SPing-Ke Shih 
687e3ec7017SPing-Ke Shih 	val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask);
688e3ec7017SPing-Ke Shih 
689e3ec7017SPing-Ke Shih 	return val;
690e3ec7017SPing-Ke Shih }
691e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf);
692e3ec7017SPing-Ke Shih 
rtw89_phy_read_rf_a(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask)69384d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev,
69484d0e33eSChung-Hsuan Hung 			       enum rtw89_rf_path rf_path, u32 addr, u32 mask)
69584d0e33eSChung-Hsuan Hung {
69684d0e33eSChung-Hsuan Hung 	bool busy;
69784d0e33eSChung-Hsuan Hung 	bool done;
69884d0e33eSChung-Hsuan Hung 	u32 val;
69984d0e33eSChung-Hsuan Hung 	int ret;
70084d0e33eSChung-Hsuan Hung 
70184d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
70284d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
70384d0e33eSChung-Hsuan Hung 	if (ret) {
70484d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read rf busy swsi\n");
70584d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
70684d0e33eSChung-Hsuan Hung 	}
70784d0e33eSChung-Hsuan Hung 
70884d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
70984d0e33eSChung-Hsuan Hung 
71084d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) |
71184d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr);
71284d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val);
71384d0e33eSChung-Hsuan Hung 	udelay(2);
71484d0e33eSChung-Hsuan Hung 
71584d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1,
71684d0e33eSChung-Hsuan Hung 				       30, false, rtwdev, R_SWSI_V1,
71784d0e33eSChung-Hsuan Hung 				       B_SWSI_R_DATA_DONE_V1);
71884d0e33eSChung-Hsuan Hung 	if (ret) {
71984d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read swsi busy\n");
72084d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
72184d0e33eSChung-Hsuan Hung 	}
72284d0e33eSChung-Hsuan Hung 
72384d0e33eSChung-Hsuan Hung 	return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask);
72484d0e33eSChung-Hsuan Hung }
72584d0e33eSChung-Hsuan Hung 
rtw89_phy_read_rf_v1(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask)72684d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
72784d0e33eSChung-Hsuan Hung 			 u32 addr, u32 mask)
72884d0e33eSChung-Hsuan Hung {
72984d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
73084d0e33eSChung-Hsuan Hung 
73184d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
73284d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
73384d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
73484d0e33eSChung-Hsuan Hung 	}
73584d0e33eSChung-Hsuan Hung 
73684d0e33eSChung-Hsuan Hung 	if (ad_sel)
73784d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask);
73884d0e33eSChung-Hsuan Hung 	else
73984d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask);
74084d0e33eSChung-Hsuan Hung }
74184d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1);
74284d0e33eSChung-Hsuan Hung 
rtw89_phy_write_rf(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask,u32 data)743e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
744e3ec7017SPing-Ke Shih 			u32 addr, u32 mask, u32 data)
745e3ec7017SPing-Ke Shih {
746e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
747e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
748e3ec7017SPing-Ke Shih 	u32 direct_addr;
749e3ec7017SPing-Ke Shih 
750e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
751e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
752e3ec7017SPing-Ke Shih 		return false;
753e3ec7017SPing-Ke Shih 	}
754e3ec7017SPing-Ke Shih 
755e3ec7017SPing-Ke Shih 	addr &= 0xff;
756e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
757e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
758e3ec7017SPing-Ke Shih 
759e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data);
760e3ec7017SPing-Ke Shih 
761e3ec7017SPing-Ke Shih 	/* delay to ensure writing properly */
762e3ec7017SPing-Ke Shih 	udelay(1);
763e3ec7017SPing-Ke Shih 
764e3ec7017SPing-Ke Shih 	return true;
765e3ec7017SPing-Ke Shih }
766e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf);
767e3ec7017SPing-Ke Shih 
rtw89_phy_write_rf_a(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask,u32 data)76884d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev,
76984d0e33eSChung-Hsuan Hung 				 enum rtw89_rf_path rf_path, u32 addr, u32 mask,
77084d0e33eSChung-Hsuan Hung 				 u32 data)
77184d0e33eSChung-Hsuan Hung {
77284d0e33eSChung-Hsuan Hung 	u8 bit_shift;
77384d0e33eSChung-Hsuan Hung 	u32 val;
77484d0e33eSChung-Hsuan Hung 	bool busy, b_msk_en = false;
77584d0e33eSChung-Hsuan Hung 	int ret;
77684d0e33eSChung-Hsuan Hung 
77784d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
77884d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
77984d0e33eSChung-Hsuan Hung 	if (ret) {
78084d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "write rf busy swsi\n");
78184d0e33eSChung-Hsuan Hung 		return false;
78284d0e33eSChung-Hsuan Hung 	}
78384d0e33eSChung-Hsuan Hung 
78484d0e33eSChung-Hsuan Hung 	data &= RFREG_MASK;
78584d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
78684d0e33eSChung-Hsuan Hung 
78784d0e33eSChung-Hsuan Hung 	if (mask != RFREG_MASK) {
78884d0e33eSChung-Hsuan Hung 		b_msk_en = true;
78984d0e33eSChung-Hsuan Hung 		rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK,
79084d0e33eSChung-Hsuan Hung 				       mask);
79184d0e33eSChung-Hsuan Hung 		bit_shift = __ffs(mask);
79284d0e33eSChung-Hsuan Hung 		data = (data << bit_shift) & RFREG_MASK;
79384d0e33eSChung-Hsuan Hung 	}
79484d0e33eSChung-Hsuan Hung 
79584d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) |
79684d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) |
79784d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) |
79884d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_VAL_V1, data);
79984d0e33eSChung-Hsuan Hung 
80084d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val);
80184d0e33eSChung-Hsuan Hung 
80284d0e33eSChung-Hsuan Hung 	return true;
80384d0e33eSChung-Hsuan Hung }
80484d0e33eSChung-Hsuan Hung 
rtw89_phy_write_rf_v1(struct rtw89_dev * rtwdev,enum rtw89_rf_path rf_path,u32 addr,u32 mask,u32 data)80584d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
80684d0e33eSChung-Hsuan Hung 			   u32 addr, u32 mask, u32 data)
80784d0e33eSChung-Hsuan Hung {
80884d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
80984d0e33eSChung-Hsuan Hung 
81084d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
81184d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
81284d0e33eSChung-Hsuan Hung 		return false;
81384d0e33eSChung-Hsuan Hung 	}
81484d0e33eSChung-Hsuan Hung 
81584d0e33eSChung-Hsuan Hung 	if (ad_sel)
81684d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data);
81784d0e33eSChung-Hsuan Hung 	else
81884d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data);
81984d0e33eSChung-Hsuan Hung }
82084d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1);
82184d0e33eSChung-Hsuan Hung 
rtw89_chip_rf_v1(struct rtw89_dev * rtwdev)822d9112042SChih-Kang Chang static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev)
823d9112042SChih-Kang Chang {
824d9112042SChih-Kang Chang 	return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1;
825d9112042SChih-Kang Chang }
826d9112042SChih-Kang Chang 
rtw89_phy_bb_reset(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)827e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev,
828e3ec7017SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx)
829e3ec7017SPing-Ke Shih {
830e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
831e3ec7017SPing-Ke Shih 
832e3ec7017SPing-Ke Shih 	chip->ops->bb_reset(rtwdev, phy_idx);
833e3ec7017SPing-Ke Shih }
834e3ec7017SPing-Ke Shih 
rtw89_phy_config_bb_reg(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * extra_data)835e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev,
836e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
837e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
838e3ec7017SPing-Ke Shih 				    void *extra_data)
839e3ec7017SPing-Ke Shih {
840e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe)
841e3ec7017SPing-Ke Shih 		mdelay(50);
842e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfd)
843e3ec7017SPing-Ke Shih 		mdelay(5);
844e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfc)
845e3ec7017SPing-Ke Shih 		mdelay(1);
846e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfb)
847e3ec7017SPing-Ke Shih 		udelay(50);
848e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfa)
849e3ec7017SPing-Ke Shih 		udelay(5);
850e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xf9)
851e3ec7017SPing-Ke Shih 		udelay(1);
852e3ec7017SPing-Ke Shih 	else
853e3ec7017SPing-Ke Shih 		rtw89_phy_write32(rtwdev, reg->addr, reg->data);
854e3ec7017SPing-Ke Shih }
855e3ec7017SPing-Ke Shih 
856e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg {
857e885871eSZong-Zhe Yang 	u32 addr;
858e885871eSZong-Zhe Yang 	struct {
859e885871eSZong-Zhe Yang 		union {
860e885871eSZong-Zhe Yang 			u8 type;
861e885871eSZong-Zhe Yang 			struct {
862e885871eSZong-Zhe Yang 				u8 rxsc_start:4;
863e885871eSZong-Zhe Yang 				u8 bw:4;
864e885871eSZong-Zhe Yang 			};
865e885871eSZong-Zhe Yang 		};
866e885871eSZong-Zhe Yang 		u8 path;
867e885871eSZong-Zhe Yang 		u8 gain_band;
868e885871eSZong-Zhe Yang 		u8 cfg_type;
869e885871eSZong-Zhe Yang 	};
870e885871eSZong-Zhe Yang } __packed;
871e885871eSZong-Zhe Yang 
872e885871eSZong-Zhe Yang static void
rtw89_phy_cfg_bb_gain_error(struct rtw89_dev * rtwdev,union rtw89_phy_bb_gain_arg arg,u32 data)873e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev,
874e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
875e885871eSZong-Zhe Yang {
876e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
877e885871eSZong-Zhe Yang 	u8 type = arg.type;
878e885871eSZong-Zhe Yang 	u8 path = arg.path;
879e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
880e885871eSZong-Zhe Yang 	int i;
881e885871eSZong-Zhe Yang 
882e885871eSZong-Zhe Yang 	switch (type) {
883e885871eSZong-Zhe Yang 	case 0:
884e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
885e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
886e885871eSZong-Zhe Yang 		break;
887e885871eSZong-Zhe Yang 	case 1:
888e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
889e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
890e885871eSZong-Zhe Yang 		break;
891e885871eSZong-Zhe Yang 	case 2:
892e885871eSZong-Zhe Yang 		for (i = 0; i < 2; i++, data >>= 8)
893e885871eSZong-Zhe Yang 			gain->tia_gain[gband][path][i] = data & 0xff;
894e885871eSZong-Zhe Yang 		break;
895e885871eSZong-Zhe Yang 	default:
896e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
897e885871eSZong-Zhe Yang 			   "bb gain error {0x%x:0x%x} with unknown type: %d\n",
898e885871eSZong-Zhe Yang 			   arg.addr, data, type);
899e885871eSZong-Zhe Yang 		break;
900e885871eSZong-Zhe Yang 	}
901e885871eSZong-Zhe Yang }
902e885871eSZong-Zhe Yang 
903e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx {
904e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_FULL = 0,
905e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20 = 1,
906e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20_1 = 5,
907e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_40 = 9,
908e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_80 = 13,
909e885871eSZong-Zhe Yang };
910e885871eSZong-Zhe Yang 
911e885871eSZong-Zhe Yang static void
rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev * rtwdev,union rtw89_phy_bb_gain_arg arg,u32 data)912e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev,
913e885871eSZong-Zhe Yang 			  union rtw89_phy_bb_gain_arg arg, u32 data)
914e885871eSZong-Zhe Yang {
915e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
916e885871eSZong-Zhe Yang 	u8 rxsc_start = arg.rxsc_start;
917e885871eSZong-Zhe Yang 	u8 bw = arg.bw;
918e885871eSZong-Zhe Yang 	u8 path = arg.path;
919e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
920e885871eSZong-Zhe Yang 	u8 rxsc;
921e885871eSZong-Zhe Yang 	s8 ofst;
922e885871eSZong-Zhe Yang 	int i;
923e885871eSZong-Zhe Yang 
924e885871eSZong-Zhe Yang 	switch (bw) {
925e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_20:
926e885871eSZong-Zhe Yang 		gain->rpl_ofst_20[gband][path] = (s8)data;
927e885871eSZong-Zhe Yang 		break;
928e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_40:
929e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
930e885871eSZong-Zhe Yang 			gain->rpl_ofst_40[gband][path][0] = (s8)data;
931e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
932e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
933e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
934e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
935e885871eSZong-Zhe Yang 				gain->rpl_ofst_40[gband][path][rxsc] = ofst;
936e885871eSZong-Zhe Yang 			}
937e885871eSZong-Zhe Yang 		}
938e885871eSZong-Zhe Yang 		break;
939e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_80:
940e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
941e885871eSZong-Zhe Yang 			gain->rpl_ofst_80[gband][path][0] = (s8)data;
942e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
943e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
944e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
945e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
946e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
947e885871eSZong-Zhe Yang 			}
948e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
949e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
950e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
951e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
952e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
953e885871eSZong-Zhe Yang 			}
954e885871eSZong-Zhe Yang 		}
955e885871eSZong-Zhe Yang 		break;
956e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
957e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
958e885871eSZong-Zhe Yang 			gain->rpl_ofst_160[gband][path][0] = (s8)data;
959e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
960e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
961e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
962e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
963e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
964e885871eSZong-Zhe Yang 			}
965e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) {
966e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
967e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i;
968e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
969e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
970e885871eSZong-Zhe Yang 			}
971e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
972e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
973e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
974e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
975e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
976e885871eSZong-Zhe Yang 			}
977e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) {
978e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
979e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_80 + i;
980e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
981e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
982e885871eSZong-Zhe Yang 			}
983e885871eSZong-Zhe Yang 		}
984e885871eSZong-Zhe Yang 		break;
985e885871eSZong-Zhe Yang 	default:
986e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
987e885871eSZong-Zhe Yang 			   "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n",
988e885871eSZong-Zhe Yang 			   arg.addr, data, bw);
989e885871eSZong-Zhe Yang 		break;
990e885871eSZong-Zhe Yang 	}
991e885871eSZong-Zhe Yang }
992e885871eSZong-Zhe Yang 
993e885871eSZong-Zhe Yang static void
rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev * rtwdev,union rtw89_phy_bb_gain_arg arg,u32 data)994e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev,
995e885871eSZong-Zhe Yang 			     union rtw89_phy_bb_gain_arg arg, u32 data)
996e885871eSZong-Zhe Yang {
997e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
998e885871eSZong-Zhe Yang 	u8 type = arg.type;
999e885871eSZong-Zhe Yang 	u8 path = arg.path;
1000e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
1001e885871eSZong-Zhe Yang 	int i;
1002e885871eSZong-Zhe Yang 
1003e885871eSZong-Zhe Yang 	switch (type) {
1004e885871eSZong-Zhe Yang 	case 0:
1005e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1006e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
1007e885871eSZong-Zhe Yang 		break;
1008e885871eSZong-Zhe Yang 	case 1:
1009e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
1010e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
1011e885871eSZong-Zhe Yang 		break;
1012e885871eSZong-Zhe Yang 	default:
1013e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1014e885871eSZong-Zhe Yang 			   "bb gain bypass {0x%x:0x%x} with unknown type: %d\n",
1015e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1016e885871eSZong-Zhe Yang 		break;
1017e885871eSZong-Zhe Yang 	}
1018e885871eSZong-Zhe Yang }
1019e885871eSZong-Zhe Yang 
1020e885871eSZong-Zhe Yang static void
rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev * rtwdev,union rtw89_phy_bb_gain_arg arg,u32 data)1021e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev,
1022e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
1023e885871eSZong-Zhe Yang {
1024e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
1025e885871eSZong-Zhe Yang 	u8 type = arg.type;
1026e885871eSZong-Zhe Yang 	u8 path = arg.path;
1027e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
1028e885871eSZong-Zhe Yang 	int i;
1029e885871eSZong-Zhe Yang 
1030e885871eSZong-Zhe Yang 	switch (type) {
1031e885871eSZong-Zhe Yang 	case 0:
1032e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1033e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1034e885871eSZong-Zhe Yang 		break;
1035e885871eSZong-Zhe Yang 	case 1:
1036e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
1037e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
1038e885871eSZong-Zhe Yang 		break;
1039e885871eSZong-Zhe Yang 	case 2:
1040e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
1041e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1042e885871eSZong-Zhe Yang 		break;
1043e885871eSZong-Zhe Yang 	case 3:
1044e885871eSZong-Zhe Yang 		for (i = 4; i < 8; i++, data >>= 8)
1045e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1046e885871eSZong-Zhe Yang 		break;
1047e885871eSZong-Zhe Yang 	default:
1048e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1049e885871eSZong-Zhe Yang 			   "bb gain op1db {0x%x:0x%x} with unknown type: %d\n",
1050e885871eSZong-Zhe Yang 			   arg.addr, data, type);
1051e885871eSZong-Zhe Yang 		break;
1052e885871eSZong-Zhe Yang 	}
1053e885871eSZong-Zhe Yang }
1054e885871eSZong-Zhe Yang 
rtw89_phy_config_bb_gain(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * extra_data)1055e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev,
1056e885871eSZong-Zhe Yang 				     const struct rtw89_reg2_def *reg,
1057e885871eSZong-Zhe Yang 				     enum rtw89_rf_path rf_path,
1058e885871eSZong-Zhe Yang 				     void *extra_data)
1059e885871eSZong-Zhe Yang {
1060e885871eSZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
1061e885871eSZong-Zhe Yang 	union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr };
1062c6a9d360SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
1063e885871eSZong-Zhe Yang 
1064e885871eSZong-Zhe Yang 	if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR)
1065e885871eSZong-Zhe Yang 		return;
1066e885871eSZong-Zhe Yang 
1067e885871eSZong-Zhe Yang 	if (arg.path >= chip->rf_path_num)
1068e885871eSZong-Zhe Yang 		return;
1069e885871eSZong-Zhe Yang 
1070e885871eSZong-Zhe Yang 	if (arg.addr >= 0xf9 && arg.addr <= 0xfe) {
1071e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev, "bb gain table with flow ctrl\n");
1072e885871eSZong-Zhe Yang 		return;
1073e885871eSZong-Zhe Yang 	}
1074e885871eSZong-Zhe Yang 
1075e885871eSZong-Zhe Yang 	switch (arg.cfg_type) {
1076e885871eSZong-Zhe Yang 	case 0:
1077e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data);
1078e885871eSZong-Zhe Yang 		break;
1079e885871eSZong-Zhe Yang 	case 1:
1080e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data);
1081e885871eSZong-Zhe Yang 		break;
1082e885871eSZong-Zhe Yang 	case 2:
1083e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data);
1084e885871eSZong-Zhe Yang 		break;
1085e885871eSZong-Zhe Yang 	case 3:
1086e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data);
1087e885871eSZong-Zhe Yang 		break;
1088c6a9d360SPing-Ke Shih 	case 4:
1089c6a9d360SPing-Ke Shih 		/* This cfg_type is only used by rfe_type >= 50 with eFEM */
1090c6a9d360SPing-Ke Shih 		if (efuse->rfe_type < 50)
1091c6a9d360SPing-Ke Shih 			break;
1092c6a9d360SPing-Ke Shih 		fallthrough;
1093e885871eSZong-Zhe Yang 	default:
1094e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1095e885871eSZong-Zhe Yang 			   "bb gain {0x%x:0x%x} with unknown cfg type: %d\n",
1096e885871eSZong-Zhe Yang 			   arg.addr, reg->data, arg.cfg_type);
1097e885871eSZong-Zhe Yang 		break;
1098e885871eSZong-Zhe Yang 	}
1099e885871eSZong-Zhe Yang }
1100e885871eSZong-Zhe Yang 
1101e3ec7017SPing-Ke Shih static void
rtw89_phy_cofig_rf_reg_store(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,struct rtw89_fw_h2c_rf_reg_info * info)1102e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev,
1103e3ec7017SPing-Ke Shih 			     const struct rtw89_reg2_def *reg,
1104e3ec7017SPing-Ke Shih 			     enum rtw89_rf_path rf_path,
1105e3ec7017SPing-Ke Shih 			     struct rtw89_fw_h2c_rf_reg_info *info)
1106e3ec7017SPing-Ke Shih {
1107e3ec7017SPing-Ke Shih 	u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE;
1108e3ec7017SPing-Ke Shih 	u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE;
1109e3ec7017SPing-Ke Shih 
111030101812SPing-Ke Shih 	if (page >= RTW89_H2C_RF_PAGE_NUM) {
111130101812SPing-Ke Shih 		rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d",
111230101812SPing-Ke Shih 			   rf_path, info->curr_idx);
111330101812SPing-Ke Shih 		return;
111430101812SPing-Ke Shih 	}
111530101812SPing-Ke Shih 
1116e3ec7017SPing-Ke Shih 	info->rtw89_phy_config_rf_h2c[page][idx] =
1117e3ec7017SPing-Ke Shih 		cpu_to_le32((reg->addr << 20) | reg->data);
1118e3ec7017SPing-Ke Shih 	info->curr_idx++;
1119e3ec7017SPing-Ke Shih }
1120e3ec7017SPing-Ke Shih 
rtw89_phy_config_rf_reg_fw(struct rtw89_dev * rtwdev,struct rtw89_fw_h2c_rf_reg_info * info)1121e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev,
1122e3ec7017SPing-Ke Shih 				      struct rtw89_fw_h2c_rf_reg_info *info)
1123e3ec7017SPing-Ke Shih {
112430101812SPing-Ke Shih 	u16 remain = info->curr_idx;
112530101812SPing-Ke Shih 	u16 len = 0;
1126e3ec7017SPing-Ke Shih 	u8 i;
1127e3ec7017SPing-Ke Shih 	int ret = 0;
1128e3ec7017SPing-Ke Shih 
112930101812SPing-Ke Shih 	if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) {
1130e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev,
113130101812SPing-Ke Shih 			   "rf reg h2c total len %d larger than %d\n",
113230101812SPing-Ke Shih 			   remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE);
113330101812SPing-Ke Shih 		ret = -EINVAL;
113430101812SPing-Ke Shih 		goto out;
1135e3ec7017SPing-Ke Shih 	}
1136e3ec7017SPing-Ke Shih 
113730101812SPing-Ke Shih 	for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) {
113830101812SPing-Ke Shih 		len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain;
113930101812SPing-Ke Shih 		ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i);
1140e3ec7017SPing-Ke Shih 		if (ret)
114130101812SPing-Ke Shih 			goto out;
1142e3ec7017SPing-Ke Shih 	}
114330101812SPing-Ke Shih out:
1144e3ec7017SPing-Ke Shih 	info->curr_idx = 0;
1145e3ec7017SPing-Ke Shih 
114630101812SPing-Ke Shih 	return ret;
1147e3ec7017SPing-Ke Shih }
1148e3ec7017SPing-Ke Shih 
rtw89_phy_config_rf_reg_noio(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * extra_data)1149d9112042SChih-Kang Chang static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev,
1150d9112042SChih-Kang Chang 					 const struct rtw89_reg2_def *reg,
1151d9112042SChih-Kang Chang 					 enum rtw89_rf_path rf_path,
1152d9112042SChih-Kang Chang 					 void *extra_data)
1153d9112042SChih-Kang Chang {
1154d9112042SChih-Kang Chang 	u32 addr = reg->addr;
1155d9112042SChih-Kang Chang 
1156d9112042SChih-Kang Chang 	if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb ||
1157d9112042SChih-Kang Chang 	    addr == 0xfa || addr == 0xf9)
1158d9112042SChih-Kang Chang 		return;
1159d9112042SChih-Kang Chang 
1160d9112042SChih-Kang Chang 	if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100)
1161d9112042SChih-Kang Chang 		return;
1162d9112042SChih-Kang Chang 
1163d9112042SChih-Kang Chang 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1164d9112042SChih-Kang Chang 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1165d9112042SChih-Kang Chang }
1166d9112042SChih-Kang Chang 
rtw89_phy_config_rf_reg(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * extra_data)1167e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev,
1168e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
1169e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
1170e3ec7017SPing-Ke Shih 				    void *extra_data)
1171e3ec7017SPing-Ke Shih {
1172e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe) {
1173e3ec7017SPing-Ke Shih 		mdelay(50);
1174e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfd) {
1175e3ec7017SPing-Ke Shih 		mdelay(5);
1176e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfc) {
1177e3ec7017SPing-Ke Shih 		mdelay(1);
1178e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfb) {
1179e3ec7017SPing-Ke Shih 		udelay(50);
1180e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfa) {
1181e3ec7017SPing-Ke Shih 		udelay(5);
1182e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xf9) {
1183e3ec7017SPing-Ke Shih 		udelay(1);
1184e3ec7017SPing-Ke Shih 	} else {
1185e3ec7017SPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data);
1186e3ec7017SPing-Ke Shih 		rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1187e3ec7017SPing-Ke Shih 					     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1188e3ec7017SPing-Ke Shih 	}
1189e3ec7017SPing-Ke Shih }
1190e3ec7017SPing-Ke Shih 
rtw89_phy_config_rf_reg_v1(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * extra_data)11912a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev,
11922a5f2b32SPing-Ke Shih 				const struct rtw89_reg2_def *reg,
11932a5f2b32SPing-Ke Shih 				enum rtw89_rf_path rf_path,
11942a5f2b32SPing-Ke Shih 				void *extra_data)
11952a5f2b32SPing-Ke Shih {
11962a5f2b32SPing-Ke Shih 	rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data);
11972a5f2b32SPing-Ke Shih 
11982a5f2b32SPing-Ke Shih 	if (reg->addr < 0x100)
11992a5f2b32SPing-Ke Shih 		return;
12002a5f2b32SPing-Ke Shih 
12012a5f2b32SPing-Ke Shih 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
12022a5f2b32SPing-Ke Shih 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
12032a5f2b32SPing-Ke Shih }
12042a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1);
12052a5f2b32SPing-Ke Shih 
rtw89_phy_sel_headline(struct rtw89_dev * rtwdev,const struct rtw89_phy_table * table,u32 * headline_size,u32 * headline_idx,u8 rfe,u8 cv)1206e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev,
1207e3ec7017SPing-Ke Shih 				  const struct rtw89_phy_table *table,
1208e3ec7017SPing-Ke Shih 				  u32 *headline_size, u32 *headline_idx,
1209e3ec7017SPing-Ke Shih 				  u8 rfe, u8 cv)
1210e3ec7017SPing-Ke Shih {
1211e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1212e3ec7017SPing-Ke Shih 	u32 headline;
1213e3ec7017SPing-Ke Shih 	u32 compare, target;
1214e3ec7017SPing-Ke Shih 	u8 rfe_para, cv_para;
1215e3ec7017SPing-Ke Shih 	u8 cv_max = 0;
1216e3ec7017SPing-Ke Shih 	bool case_matched = false;
1217e3ec7017SPing-Ke Shih 	u32 i;
1218e3ec7017SPing-Ke Shih 
1219e3ec7017SPing-Ke Shih 	for (i = 0; i < table->n_regs; i++) {
1220e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1221e3ec7017SPing-Ke Shih 		headline = get_phy_headline(reg->addr);
1222e3ec7017SPing-Ke Shih 		if (headline != PHY_HEADLINE_VALID)
1223e3ec7017SPing-Ke Shih 			break;
1224e3ec7017SPing-Ke Shih 	}
1225e3ec7017SPing-Ke Shih 	*headline_size = i;
1226e3ec7017SPing-Ke Shih 	if (*headline_size == 0)
1227e3ec7017SPing-Ke Shih 		return 0;
1228e3ec7017SPing-Ke Shih 
1229e3ec7017SPing-Ke Shih 	/* case 1: RFE match, CV match */
1230e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, cv);
1231e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1232e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1233e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1234e3ec7017SPing-Ke Shih 		if (target == compare) {
1235e3ec7017SPing-Ke Shih 			*headline_idx = i;
1236e3ec7017SPing-Ke Shih 			return 0;
1237e3ec7017SPing-Ke Shih 		}
1238e3ec7017SPing-Ke Shih 	}
1239e3ec7017SPing-Ke Shih 
1240e3ec7017SPing-Ke Shih 	/* case 2: RFE match, CV don't care */
1241e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, PHY_COND_DONT_CARE);
1242e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1243e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1244e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1245e3ec7017SPing-Ke Shih 		if (target == compare) {
1246e3ec7017SPing-Ke Shih 			*headline_idx = i;
1247e3ec7017SPing-Ke Shih 			return 0;
1248e3ec7017SPing-Ke Shih 		}
1249e3ec7017SPing-Ke Shih 	}
1250e3ec7017SPing-Ke Shih 
1251e3ec7017SPing-Ke Shih 	/* case 3: RFE match, CV max in table */
1252e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1253e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1254e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1255e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1256e3ec7017SPing-Ke Shih 		if (rfe_para == rfe) {
1257e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1258e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1259e3ec7017SPing-Ke Shih 				*headline_idx = i;
1260e3ec7017SPing-Ke Shih 				case_matched = true;
1261e3ec7017SPing-Ke Shih 			}
1262e3ec7017SPing-Ke Shih 		}
1263e3ec7017SPing-Ke Shih 	}
1264e3ec7017SPing-Ke Shih 
1265e3ec7017SPing-Ke Shih 	if (case_matched)
1266e3ec7017SPing-Ke Shih 		return 0;
1267e3ec7017SPing-Ke Shih 
1268e3ec7017SPing-Ke Shih 	/* case 4: RFE don't care, CV max in table */
1269e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1270e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1271e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1272e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1273e3ec7017SPing-Ke Shih 		if (rfe_para == PHY_COND_DONT_CARE) {
1274e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1275e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1276e3ec7017SPing-Ke Shih 				*headline_idx = i;
1277e3ec7017SPing-Ke Shih 				case_matched = true;
1278e3ec7017SPing-Ke Shih 			}
1279e3ec7017SPing-Ke Shih 		}
1280e3ec7017SPing-Ke Shih 	}
1281e3ec7017SPing-Ke Shih 
1282e3ec7017SPing-Ke Shih 	if (case_matched)
1283e3ec7017SPing-Ke Shih 		return 0;
1284e3ec7017SPing-Ke Shih 
1285e3ec7017SPing-Ke Shih 	return -EINVAL;
1286e3ec7017SPing-Ke Shih }
1287e3ec7017SPing-Ke Shih 
rtw89_phy_init_reg(struct rtw89_dev * rtwdev,const struct rtw89_phy_table * table,void (* config)(struct rtw89_dev * rtwdev,const struct rtw89_reg2_def * reg,enum rtw89_rf_path rf_path,void * data),void * extra_data)1288e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev,
1289e3ec7017SPing-Ke Shih 			       const struct rtw89_phy_table *table,
1290e3ec7017SPing-Ke Shih 			       void (*config)(struct rtw89_dev *rtwdev,
1291e3ec7017SPing-Ke Shih 					      const struct rtw89_reg2_def *reg,
1292e3ec7017SPing-Ke Shih 					      enum rtw89_rf_path rf_path,
1293e3ec7017SPing-Ke Shih 					      void *data),
1294e3ec7017SPing-Ke Shih 			       void *extra_data)
1295e3ec7017SPing-Ke Shih {
1296e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1297e3ec7017SPing-Ke Shih 	enum rtw89_rf_path rf_path = table->rf_path;
1298e3ec7017SPing-Ke Shih 	u8 rfe = rtwdev->efuse.rfe_type;
1299e3ec7017SPing-Ke Shih 	u8 cv = rtwdev->hal.cv;
1300e3ec7017SPing-Ke Shih 	u32 i;
1301e3ec7017SPing-Ke Shih 	u32 headline_size = 0, headline_idx = 0;
1302e3ec7017SPing-Ke Shih 	u32 target = 0, cfg_target;
1303e3ec7017SPing-Ke Shih 	u8 cond;
1304e3ec7017SPing-Ke Shih 	bool is_matched = true;
1305e3ec7017SPing-Ke Shih 	bool target_found = false;
1306e3ec7017SPing-Ke Shih 	int ret;
1307e3ec7017SPing-Ke Shih 
1308e3ec7017SPing-Ke Shih 	ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size,
1309e3ec7017SPing-Ke Shih 				     &headline_idx, rfe, cv);
1310e3ec7017SPing-Ke Shih 	if (ret) {
1311e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv);
1312e3ec7017SPing-Ke Shih 		return;
1313e3ec7017SPing-Ke Shih 	}
1314e3ec7017SPing-Ke Shih 
1315e3ec7017SPing-Ke Shih 	cfg_target = get_phy_target(table->regs[headline_idx].addr);
1316e3ec7017SPing-Ke Shih 	for (i = headline_size; i < table->n_regs; i++) {
1317e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1318e3ec7017SPing-Ke Shih 		cond = get_phy_cond(reg->addr);
1319e3ec7017SPing-Ke Shih 		switch (cond) {
1320e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_IF:
1321e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELIF:
1322e3ec7017SPing-Ke Shih 			target = get_phy_target(reg->addr);
1323e3ec7017SPing-Ke Shih 			break;
1324e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELSE:
1325e3ec7017SPing-Ke Shih 			is_matched = false;
1326e3ec7017SPing-Ke Shih 			if (!target_found) {
1327e3ec7017SPing-Ke Shih 				rtw89_warn(rtwdev, "failed to load CR %x/%x\n",
1328e3ec7017SPing-Ke Shih 					   reg->addr, reg->data);
1329e3ec7017SPing-Ke Shih 				return;
1330e3ec7017SPing-Ke Shih 			}
1331e3ec7017SPing-Ke Shih 			break;
1332e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_END:
1333e3ec7017SPing-Ke Shih 			is_matched = true;
1334e3ec7017SPing-Ke Shih 			target_found = false;
1335e3ec7017SPing-Ke Shih 			break;
1336e3ec7017SPing-Ke Shih 		case PHY_COND_CHECK:
1337e3ec7017SPing-Ke Shih 			if (target_found) {
1338e3ec7017SPing-Ke Shih 				is_matched = false;
1339e3ec7017SPing-Ke Shih 				break;
1340e3ec7017SPing-Ke Shih 			}
1341e3ec7017SPing-Ke Shih 
1342e3ec7017SPing-Ke Shih 			if (target == cfg_target) {
1343e3ec7017SPing-Ke Shih 				is_matched = true;
1344e3ec7017SPing-Ke Shih 				target_found = true;
1345e3ec7017SPing-Ke Shih 			} else {
1346e3ec7017SPing-Ke Shih 				is_matched = false;
1347e3ec7017SPing-Ke Shih 				target_found = false;
1348e3ec7017SPing-Ke Shih 			}
1349e3ec7017SPing-Ke Shih 			break;
1350e3ec7017SPing-Ke Shih 		default:
1351e3ec7017SPing-Ke Shih 			if (is_matched)
1352e3ec7017SPing-Ke Shih 				config(rtwdev, reg, rf_path, extra_data);
1353e3ec7017SPing-Ke Shih 			break;
1354e3ec7017SPing-Ke Shih 		}
1355e3ec7017SPing-Ke Shih 	}
1356e3ec7017SPing-Ke Shih }
1357e3ec7017SPing-Ke Shih 
rtw89_phy_init_bb_reg(struct rtw89_dev * rtwdev)1358e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev)
1359e3ec7017SPing-Ke Shih {
136089474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1361e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
136289474720SPing-Ke Shih 	const struct rtw89_phy_table *bb_table;
136389474720SPing-Ke Shih 	const struct rtw89_phy_table *bb_gain_table;
1364e3ec7017SPing-Ke Shih 
136589474720SPing-Ke Shih 	bb_table = elm_info->bb_tbl ? elm_info->bb_tbl : chip->bb_table;
1366e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL);
1367e3ec7017SPing-Ke Shih 	rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0);
136889474720SPing-Ke Shih 
136989474720SPing-Ke Shih 	bb_gain_table = elm_info->bb_gain ? elm_info->bb_gain : chip->bb_gain_table;
1370e885871eSZong-Zhe Yang 	if (bb_gain_table)
1371e885871eSZong-Zhe Yang 		rtw89_phy_init_reg(rtwdev, bb_gain_table,
1372e885871eSZong-Zhe Yang 				   rtw89_phy_config_bb_gain, NULL);
1373e3ec7017SPing-Ke Shih 	rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0);
1374e3ec7017SPing-Ke Shih }
1375e3ec7017SPing-Ke Shih 
rtw89_phy_nctl_poll(struct rtw89_dev * rtwdev)1376e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev)
1377e3ec7017SPing-Ke Shih {
1378e3ec7017SPing-Ke Shih 	rtw89_phy_write32(rtwdev, 0x8080, 0x4);
1379e3ec7017SPing-Ke Shih 	udelay(1);
1380e3ec7017SPing-Ke Shih 	return rtw89_phy_read32(rtwdev, 0x8080);
1381e3ec7017SPing-Ke Shih }
1382e3ec7017SPing-Ke Shih 
rtw89_phy_init_rf_reg(struct rtw89_dev * rtwdev,bool noio)1383d9112042SChih-Kang Chang void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio)
1384e3ec7017SPing-Ke Shih {
13852a5f2b32SPing-Ke Shih 	void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg,
13862a5f2b32SPing-Ke Shih 		       enum rtw89_rf_path rf_path, void *data);
138789474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1388e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1389e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *rf_table;
1390e3ec7017SPing-Ke Shih 	struct rtw89_fw_h2c_rf_reg_info *rf_reg_info;
1391e3ec7017SPing-Ke Shih 	u8 path;
1392e3ec7017SPing-Ke Shih 
1393e3ec7017SPing-Ke Shih 	rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL);
1394e3ec7017SPing-Ke Shih 	if (!rf_reg_info)
1395e3ec7017SPing-Ke Shih 		return;
1396e3ec7017SPing-Ke Shih 
1397e3ec7017SPing-Ke Shih 	for (path = RF_PATH_A; path < chip->rf_path_num; path++) {
139889474720SPing-Ke Shih 		rf_table = elm_info->rf_radio[path] ?
139989474720SPing-Ke Shih 			   elm_info->rf_radio[path] : chip->rf_table[path];
14002a5f2b32SPing-Ke Shih 		rf_reg_info->rf_path = rf_table->rf_path;
1401d9112042SChih-Kang Chang 		if (noio)
1402d9112042SChih-Kang Chang 			config = rtw89_phy_config_rf_reg_noio;
1403d9112042SChih-Kang Chang 		else
1404d9112042SChih-Kang Chang 			config = rf_table->config ? rf_table->config :
1405d9112042SChih-Kang Chang 				 rtw89_phy_config_rf_reg;
14062a5f2b32SPing-Ke Shih 		rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info);
1407e3ec7017SPing-Ke Shih 		if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info))
1408e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n",
14092a5f2b32SPing-Ke Shih 				   rf_reg_info->rf_path);
1410e3ec7017SPing-Ke Shih 	}
1411e3ec7017SPing-Ke Shih 	kfree(rf_reg_info);
1412e3ec7017SPing-Ke Shih }
1413e3ec7017SPing-Ke Shih 
rtw89_phy_init_rf_nctl(struct rtw89_dev * rtwdev)1414e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev)
1415e3ec7017SPing-Ke Shih {
141689474720SPing-Ke Shih 	struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info;
1417e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1418e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *nctl_table;
1419e3ec7017SPing-Ke Shih 	u32 val;
1420e3ec7017SPing-Ke Shih 	int ret;
1421e3ec7017SPing-Ke Shih 
1422e3ec7017SPing-Ke Shih 	/* IQK/DPK clock & reset */
1423d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3);
1424d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1);
1425d0c820ccSPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000);
1426a24be8bbSPing-Ke Shih 	if (chip->chip_id != RTL8851B)
1427d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000);
1428d0c820ccSPing-Ke Shih 	if (chip->chip_id == RTL8852B)
1429d0c820ccSPing-Ke Shih 		rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2);
1430e3ec7017SPing-Ke Shih 
1431e3ec7017SPing-Ke Shih 	/* check 0x8080 */
1432d0c820ccSPing-Ke Shih 	rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8);
1433e3ec7017SPing-Ke Shih 
1434e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10,
1435e3ec7017SPing-Ke Shih 				1000, false, rtwdev);
1436e3ec7017SPing-Ke Shih 	if (ret)
1437e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to poll nctl block\n");
1438e3ec7017SPing-Ke Shih 
143989474720SPing-Ke Shih 	nctl_table = elm_info->rf_nctl ? elm_info->rf_nctl : chip->nctl_table;
1440e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL);
1441a24be8bbSPing-Ke Shih 
1442a24be8bbSPing-Ke Shih 	if (chip->nctl_post_table)
1443a24be8bbSPing-Ke Shih 		rtw89_rfk_parser(rtwdev, chip->nctl_post_table);
1444e3ec7017SPing-Ke Shih }
1445e3ec7017SPing-Ke Shih 
rtw89_phy0_phy1_offset(struct rtw89_dev * rtwdev,u32 addr)1446e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr)
1447e3ec7017SPing-Ke Shih {
1448e3ec7017SPing-Ke Shih 	u32 phy_page = addr >> 8;
1449e3ec7017SPing-Ke Shih 	u32 ofst = 0;
1450e3ec7017SPing-Ke Shih 
14511165f571SPing-Ke Shih 	if (rtwdev->chip->chip_gen == RTW89_CHIP_BE)
14521165f571SPing-Ke Shih 		return addr < 0x10000 ? 0x20000 : 0;
14531165f571SPing-Ke Shih 
1454e3ec7017SPing-Ke Shih 	switch (phy_page) {
1455e3ec7017SPing-Ke Shih 	case 0x6:
1456e3ec7017SPing-Ke Shih 	case 0x7:
1457e3ec7017SPing-Ke Shih 	case 0x8:
1458e3ec7017SPing-Ke Shih 	case 0x9:
1459e3ec7017SPing-Ke Shih 	case 0xa:
1460e3ec7017SPing-Ke Shih 	case 0xb:
1461e3ec7017SPing-Ke Shih 	case 0xc:
1462e3ec7017SPing-Ke Shih 	case 0xd:
1463e3ec7017SPing-Ke Shih 	case 0x19:
1464e3ec7017SPing-Ke Shih 	case 0x1a:
1465e3ec7017SPing-Ke Shih 	case 0x1b:
1466e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1467e3ec7017SPing-Ke Shih 		break;
1468e3ec7017SPing-Ke Shih 	default:
1469e3ec7017SPing-Ke Shih 		/* warning case */
1470e3ec7017SPing-Ke Shih 		ofst = 0;
1471e3ec7017SPing-Ke Shih 		break;
1472e3ec7017SPing-Ke Shih 	}
1473e3ec7017SPing-Ke Shih 
1474e3ec7017SPing-Ke Shih 	if (phy_page >= 0x40 && phy_page <= 0x4f)
1475e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1476e3ec7017SPing-Ke Shih 
1477e3ec7017SPing-Ke Shih 	return ofst;
1478e3ec7017SPing-Ke Shih }
1479e3ec7017SPing-Ke Shih 
rtw89_phy_write32_idx(struct rtw89_dev * rtwdev,u32 addr,u32 mask,u32 data,enum rtw89_phy_idx phy_idx)1480e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1481e3ec7017SPing-Ke Shih 			   u32 data, enum rtw89_phy_idx phy_idx)
1482e3ec7017SPing-Ke Shih {
1483e3ec7017SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1484e3ec7017SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1485e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, addr, mask, data);
1486e3ec7017SPing-Ke Shih }
1487861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx);
1488e3ec7017SPing-Ke Shih 
rtw89_phy_read32_idx(struct rtw89_dev * rtwdev,u32 addr,u32 mask,enum rtw89_phy_idx phy_idx)14896b069898SPing-Ke Shih u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
14906b069898SPing-Ke Shih 			 enum rtw89_phy_idx phy_idx)
14916b069898SPing-Ke Shih {
14926b069898SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
14936b069898SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
14946b069898SPing-Ke Shih 	return rtw89_phy_read32_mask(rtwdev, addr, mask);
14956b069898SPing-Ke Shih }
14966b069898SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read32_idx);
14976b069898SPing-Ke Shih 
rtw89_phy_set_phy_regs(struct rtw89_dev * rtwdev,u32 addr,u32 mask,u32 val)1498e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1499e3ec7017SPing-Ke Shih 			    u32 val)
1500e3ec7017SPing-Ke Shih {
1501e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0);
1502e3ec7017SPing-Ke Shih 
1503e3ec7017SPing-Ke Shih 	if (!rtwdev->dbcc_en)
1504e3ec7017SPing-Ke Shih 		return;
1505e3ec7017SPing-Ke Shih 
1506e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1);
1507e3ec7017SPing-Ke Shih }
1508e3ec7017SPing-Ke Shih 
rtw89_phy_write_reg3_tbl(struct rtw89_dev * rtwdev,const struct rtw89_phy_reg3_tbl * tbl)1509e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev,
1510e3ec7017SPing-Ke Shih 			      const struct rtw89_phy_reg3_tbl *tbl)
1511e3ec7017SPing-Ke Shih {
1512e3ec7017SPing-Ke Shih 	const struct rtw89_reg3_def *reg3;
1513e3ec7017SPing-Ke Shih 	int i;
1514e3ec7017SPing-Ke Shih 
1515e3ec7017SPing-Ke Shih 	for (i = 0; i < tbl->size; i++) {
1516e3ec7017SPing-Ke Shih 		reg3 = &tbl->reg3[i];
1517e3ec7017SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data);
1518e3ec7017SPing-Ke Shih 	}
1519e3ec7017SPing-Ke Shih }
1520861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl);
1521e3ec7017SPing-Ke Shih 
1522b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_idx_num[] = {
1523b4a283fbSZong-Zhe Yang 	[RTW89_RS_CCK] = RTW89_RATE_CCK_NUM,
1524b4a283fbSZong-Zhe Yang 	[RTW89_RS_OFDM] = RTW89_RATE_OFDM_NUM,
1525b4a283fbSZong-Zhe Yang 	[RTW89_RS_MCS] = RTW89_RATE_MCS_NUM,
1526b4a283fbSZong-Zhe Yang 	[RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_NUM,
1527b4a283fbSZong-Zhe Yang 	[RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_NUM,
1528e3ec7017SPing-Ke Shih };
1529e3ec7017SPing-Ke Shih 
1530b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_nss_num[] = {
1531e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = 1,
1532e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = 1,
1533b4a283fbSZong-Zhe Yang 	[RTW89_RS_MCS] = RTW89_NSS_NUM,
1534b4a283fbSZong-Zhe Yang 	[RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_NUM,
1535e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = 1,
1536e3ec7017SPing-Ke Shih };
1537e3ec7017SPing-Ke Shih 
1538e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = {
1539e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck),
1540e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm),
1541e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs),
1542e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm),
1543e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset),
1544e3ec7017SPing-Ke Shih };
1545e3ec7017SPing-Ke Shih 
1546e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs])
1547b4a283fbSZong-Zhe Yang #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_num[rs] + (idx))
1548e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \
1549b4a283fbSZong-Zhe Yang 	((nss) < rtw89_rs_nss_num[rs] && (idx) < rtw89_rs_idx_num[rs])
1550e3ec7017SPing-Ke Shih 
rtw89_phy_load_txpwr_byrate(struct rtw89_dev * rtwdev,const struct rtw89_txpwr_table * tbl)1551e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev,
1552e3ec7017SPing-Ke Shih 				 const struct rtw89_txpwr_table *tbl)
1553e3ec7017SPing-Ke Shih {
1554e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data;
1555e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size;
1556e3ec7017SPing-Ke Shih 	s8 *byr;
1557e3ec7017SPing-Ke Shih 	u32 data;
1558e3ec7017SPing-Ke Shih 	u8 i, idx;
1559e3ec7017SPing-Ke Shih 
1560e3ec7017SPing-Ke Shih 	for (; cfg < end; cfg++) {
1561e3ec7017SPing-Ke Shih 		byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]);
1562e3ec7017SPing-Ke Shih 		data = cfg->data;
1563e3ec7017SPing-Ke Shih 
1564e3ec7017SPing-Ke Shih 		for (i = 0; i < cfg->len; i++, data >>= 8) {
1565e3ec7017SPing-Ke Shih 			idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i));
1566e3ec7017SPing-Ke Shih 			byr[idx] = (s8)(data & 0xff);
1567e3ec7017SPing-Ke Shih 		}
1568e3ec7017SPing-Ke Shih 	}
1569e3ec7017SPing-Ke Shih }
1570861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate);
1571e3ec7017SPing-Ke Shih 
1572e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf)				\
1573e3ec7017SPing-Ke Shih ({									\
1574e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *__c = (rtwdev)->chip;		\
1575e3ec7017SPing-Ke Shih 	(txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac);	\
1576e3ec7017SPing-Ke Shih })
1577e3ec7017SPing-Ke Shih 
15789b43bd1aSZong-Zhe Yang static
rtw89_phy_read_txpwr_byrate(struct rtw89_dev * rtwdev,u8 band,const struct rtw89_rate_desc * rate_desc)157907ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band,
1580e3ec7017SPing-Ke Shih 			       const struct rtw89_rate_desc *rate_desc)
1581e3ec7017SPing-Ke Shih {
1582e3ec7017SPing-Ke Shih 	s8 *byr;
1583e3ec7017SPing-Ke Shih 	u8 idx;
1584e3ec7017SPing-Ke Shih 
1585e3ec7017SPing-Ke Shih 	if (rate_desc->rs == RTW89_RS_CCK)
1586e3ec7017SPing-Ke Shih 		band = RTW89_BAND_2G;
1587e3ec7017SPing-Ke Shih 
1588e3ec7017SPing-Ke Shih 	if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) {
1589e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1590e3ec7017SPing-Ke Shih 			    "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n",
1591e3ec7017SPing-Ke Shih 			    rate_desc->rs, rate_desc->nss, rate_desc->idx);
1592e3ec7017SPing-Ke Shih 
1593e3ec7017SPing-Ke Shih 		return 0;
1594e3ec7017SPing-Ke Shih 	}
1595e3ec7017SPing-Ke Shih 
1596e3ec7017SPing-Ke Shih 	byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]);
1597e3ec7017SPing-Ke Shih 	idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx);
1598e3ec7017SPing-Ke Shih 
1599e3ec7017SPing-Ke Shih 	return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]);
1600e3ec7017SPing-Ke Shih }
1601e3ec7017SPing-Ke Shih 
rtw89_channel_6g_to_idx(struct rtw89_dev * rtwdev,u8 channel_6g)1602ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g)
1603e3ec7017SPing-Ke Shih {
1604ac74f016SZong-Zhe Yang 	switch (channel_6g) {
1605ac74f016SZong-Zhe Yang 	case 1 ... 29:
1606ac74f016SZong-Zhe Yang 		return (channel_6g - 1) / 2;
1607ac74f016SZong-Zhe Yang 	case 33 ... 61:
1608ac74f016SZong-Zhe Yang 		return (channel_6g - 3) / 2;
1609ac74f016SZong-Zhe Yang 	case 65 ... 93:
1610ac74f016SZong-Zhe Yang 		return (channel_6g - 5) / 2;
1611ac74f016SZong-Zhe Yang 	case 97 ... 125:
1612ac74f016SZong-Zhe Yang 		return (channel_6g - 7) / 2;
1613ac74f016SZong-Zhe Yang 	case 129 ... 157:
1614ac74f016SZong-Zhe Yang 		return (channel_6g - 9) / 2;
1615ac74f016SZong-Zhe Yang 	case 161 ... 189:
1616ac74f016SZong-Zhe Yang 		return (channel_6g - 11) / 2;
1617ac74f016SZong-Zhe Yang 	case 193 ... 221:
1618ac74f016SZong-Zhe Yang 		return (channel_6g - 13) / 2;
1619ac74f016SZong-Zhe Yang 	case 225 ... 253:
1620ac74f016SZong-Zhe Yang 		return (channel_6g - 15) / 2;
1621ac74f016SZong-Zhe Yang 	default:
1622ac74f016SZong-Zhe Yang 		rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g);
1623ac74f016SZong-Zhe Yang 		return 0;
1624ac74f016SZong-Zhe Yang 	}
1625ac74f016SZong-Zhe Yang }
1626ac74f016SZong-Zhe Yang 
rtw89_channel_to_idx(struct rtw89_dev * rtwdev,u8 band,u8 channel)1627ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel)
1628ac74f016SZong-Zhe Yang {
1629ac74f016SZong-Zhe Yang 	if (band == RTW89_BAND_6G)
1630ac74f016SZong-Zhe Yang 		return rtw89_channel_6g_to_idx(rtwdev, channel);
1631ac74f016SZong-Zhe Yang 
1632e3ec7017SPing-Ke Shih 	switch (channel) {
1633e3ec7017SPing-Ke Shih 	case 1 ... 14:
1634e3ec7017SPing-Ke Shih 		return channel - 1;
1635e3ec7017SPing-Ke Shih 	case 36 ... 64:
1636e3ec7017SPing-Ke Shih 		return (channel - 36) / 2;
1637e3ec7017SPing-Ke Shih 	case 100 ... 144:
1638e3ec7017SPing-Ke Shih 		return ((channel - 100) / 2) + 15;
1639e3ec7017SPing-Ke Shih 	case 149 ... 177:
1640e3ec7017SPing-Ke Shih 		return ((channel - 149) / 2) + 38;
1641e3ec7017SPing-Ke Shih 	default:
1642e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown channel: %d\n", channel);
1643e3ec7017SPing-Ke Shih 		return 0;
1644e3ec7017SPing-Ke Shih 	}
1645e3ec7017SPing-Ke Shih }
1646e3ec7017SPing-Ke Shih 
rtw89_phy_read_txpwr_limit(struct rtw89_dev * rtwdev,u8 band,u8 bw,u8 ntx,u8 rs,u8 bf,u8 ch)164707ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band,
1648e3ec7017SPing-Ke Shih 			      u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch)
1649e3ec7017SPing-Ke Shih {
16505395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
16515395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
16525395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
16535395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
16542a8ec45fSZong-Zhe Yang 	struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory;
1655bfbadacfSZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
1656bfbadacfSZong-Zhe Yang 	u32 freq = ieee80211_channel_to_frequency(ch, nl_band);
1657ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1658e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
16592a8ec45fSZong-Zhe Yang 	u8 reg6 = regulatory->reg_6ghz_power;
1660e3ec7017SPing-Ke Shih 	s8 lmt = 0, sar;
1661e3ec7017SPing-Ke Shih 
1662e3ec7017SPing-Ke Shih 	switch (band) {
1663e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
16645395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16655395482aSZong-Zhe Yang 		if (lmt)
16665395482aSZong-Zhe Yang 			break;
16675395482aSZong-Zhe Yang 
16685395482aSZong-Zhe Yang 		lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1669e3ec7017SPing-Ke Shih 		break;
1670e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
16715395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx];
16725395482aSZong-Zhe Yang 		if (lmt)
16735395482aSZong-Zhe Yang 			break;
16745395482aSZong-Zhe Yang 
16755395482aSZong-Zhe Yang 		lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx];
1676e3ec7017SPing-Ke Shih 		break;
1677ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
16782a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][regd][reg6][ch_idx];
16795395482aSZong-Zhe Yang 		if (lmt)
16805395482aSZong-Zhe Yang 			break;
16815395482aSZong-Zhe Yang 
16822a8ec45fSZong-Zhe Yang 		lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][RTW89_WW]
16832a8ec45fSZong-Zhe Yang 				       [RTW89_REG_6GHZ_POWER_DFLT]
16842a8ec45fSZong-Zhe Yang 				       [ch_idx];
1685ac74f016SZong-Zhe Yang 		break;
1686e3ec7017SPing-Ke Shih 	default:
1687e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1688e3ec7017SPing-Ke Shih 		return 0;
1689e3ec7017SPing-Ke Shih 	}
1690e3ec7017SPing-Ke Shih 
1691e3ec7017SPing-Ke Shih 	lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt);
1692bfbadacfSZong-Zhe Yang 	sar = rtw89_query_sar(rtwdev, freq);
1693e3ec7017SPing-Ke Shih 
1694e3ec7017SPing-Ke Shih 	return min(lmt, sar);
1695e3ec7017SPing-Ke Shih }
1696861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit);
1697e3ec7017SPing-Ke Shih 
169807ef5f2fSZong-Zhe Yang #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch)		\
1699e3ec7017SPing-Ke Shih 	do {								\
1700e3ec7017SPing-Ke Shih 		u8 __i;							\
1701e3ec7017SPing-Ke Shih 		for (__i = 0; __i < RTW89_BF_NUM; __i++)		\
1702e3ec7017SPing-Ke Shih 			ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev,	\
170307ef5f2fSZong-Zhe Yang 							      band,	\
1704e3ec7017SPing-Ke Shih 							      bw, ntx,	\
1705e3ec7017SPing-Ke Shih 							      rs, __i,	\
1706e3ec7017SPing-Ke Shih 							      (ch));	\
1707e3ec7017SPing-Ke Shih 	} while (0)
1708e3ec7017SPing-Ke Shih 
rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit * lmt,u8 band,u8 ntx,u8 ch)1709e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev,
1710e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
171107ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch)
1712e3ec7017SPing-Ke Shih {
171307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1714e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
171507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1716e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
171707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1718e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_OFDM, ch);
171907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
172007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1721e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1722e3ec7017SPing-Ke Shih }
1723e3ec7017SPing-Ke Shih 
rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit * lmt,u8 band,u8 ntx,u8 ch,u8 pri_ch)1724e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev,
1725e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
172607ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1727e3ec7017SPing-Ke Shih {
172807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1729e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch - 2);
173007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1731e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
173207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
173394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
173407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
173507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1736e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
173707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
173807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1739e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
174007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
174107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1742e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1743e3ec7017SPing-Ke Shih }
1744e3ec7017SPing-Ke Shih 
rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit * lmt,u8 band,u8 ntx,u8 ch,u8 pri_ch)1745e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev,
1746e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
174707ef5f2fSZong-Zhe Yang 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1748e3ec7017SPing-Ke Shih {
1749e3ec7017SPing-Ke Shih 	s8 val_0p5_n[RTW89_BF_NUM];
1750e3ec7017SPing-Ke Shih 	s8 val_0p5_p[RTW89_BF_NUM];
1751e3ec7017SPing-Ke Shih 	u8 i;
1752e3ec7017SPing-Ke Shih 
175307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
175494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
175507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
175607ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1757e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 6);
175807ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
175907ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1760e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
176107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
176207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1763e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
176407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
176507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
1766e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 6);
176707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
176807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1769e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
177007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
177107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
1772e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
177307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
177407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
1775e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1776e3ec7017SPing-Ke Shih 
177707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
1778e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
177907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
1780e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
1781e3ec7017SPing-Ke Shih 
1782e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_BF_NUM; i++)
1783e3ec7017SPing-Ke Shih 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1784e3ec7017SPing-Ke Shih }
1785e3ec7017SPing-Ke Shih 
rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit * lmt,u8 band,u8 ntx,u8 ch,u8 pri_ch)178694b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev,
178794b70cafSZong-Zhe Yang 					    struct rtw89_txpwr_limit *lmt,
178807ef5f2fSZong-Zhe Yang 					    u8 band, u8 ntx, u8 ch, u8 pri_ch)
178994b70cafSZong-Zhe Yang {
179094b70cafSZong-Zhe Yang 	s8 val_0p5_n[RTW89_BF_NUM];
179194b70cafSZong-Zhe Yang 	s8 val_0p5_p[RTW89_BF_NUM];
179294b70cafSZong-Zhe Yang 	s8 val_2p5_n[RTW89_BF_NUM];
179394b70cafSZong-Zhe Yang 	s8 val_2p5_p[RTW89_BF_NUM];
179494b70cafSZong-Zhe Yang 	u8 i;
179594b70cafSZong-Zhe Yang 
179694b70cafSZong-Zhe Yang 	/* fill ofdm section */
179707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
179894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
179994b70cafSZong-Zhe Yang 
180094b70cafSZong-Zhe Yang 	/* fill mcs 20m section */
180107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
180207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 14);
180407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
180507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 10);
180707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
180807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
180994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 6);
181007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
181107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181294b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 2);
181307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band,
181407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 2);
181607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band,
181707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
181894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 6);
181907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band,
182007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
182194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 10);
182207ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band,
182307ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_20,
182494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 14);
182594b70cafSZong-Zhe Yang 
182694b70cafSZong-Zhe Yang 	/* fill mcs 40m section */
182707ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
182807ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
182994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 12);
183007ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
183107ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
183294b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
183307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band,
183407ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
183594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
183607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band,
183707ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_40,
183894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 12);
183994b70cafSZong-Zhe Yang 
184094b70cafSZong-Zhe Yang 	/* fill mcs 80m section */
184107ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
184207ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
184394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
184407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band,
184507ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_80,
184694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
184794b70cafSZong-Zhe Yang 
184894b70cafSZong-Zhe Yang 	/* fill mcs 160m section */
184907ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band,
185007ef5f2fSZong-Zhe Yang 				    RTW89_CHANNEL_WIDTH_160,
185194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch);
185294b70cafSZong-Zhe Yang 
185394b70cafSZong-Zhe Yang 	/* fill mcs 40m 0p5 section */
185407ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
185594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
185607ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
185794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
185894b70cafSZong-Zhe Yang 
185994b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
186094b70cafSZong-Zhe Yang 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
186194b70cafSZong-Zhe Yang 
186294b70cafSZong-Zhe Yang 	/* fill mcs 40m 2p5 section */
186307ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40,
186494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
186507ef5f2fSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40,
186694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
186794b70cafSZong-Zhe Yang 
186894b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
186994b70cafSZong-Zhe Yang 		lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]);
187094b70cafSZong-Zhe Yang }
187194b70cafSZong-Zhe Yang 
18729b43bd1aSZong-Zhe Yang static
rtw89_phy_fill_txpwr_limit(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,struct rtw89_txpwr_limit * lmt,u8 ntx)1873e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev,
187407ef5f2fSZong-Zhe Yang 				const struct rtw89_chan *chan,
1875e3ec7017SPing-Ke Shih 				struct rtw89_txpwr_limit *lmt,
1876e3ec7017SPing-Ke Shih 				u8 ntx)
1877e3ec7017SPing-Ke Shih {
187807ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
1879cbb145b9SZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
1880cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
1881cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
1882e3ec7017SPing-Ke Shih 
1883e3ec7017SPing-Ke Shih 	memset(lmt, 0, sizeof(*lmt));
1884e3ec7017SPing-Ke Shih 
1885e3ec7017SPing-Ke Shih 	switch (bw) {
1886e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
188707ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch);
1888e3ec7017SPing-Ke Shih 		break;
1889e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
189007ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch,
189107ef5f2fSZong-Zhe Yang 					       pri_ch);
1892e3ec7017SPing-Ke Shih 		break;
1893e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
189407ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch,
189507ef5f2fSZong-Zhe Yang 					       pri_ch);
189694b70cafSZong-Zhe Yang 		break;
189794b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
189807ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch,
189907ef5f2fSZong-Zhe Yang 						pri_ch);
1900e3ec7017SPing-Ke Shih 		break;
1901e3ec7017SPing-Ke Shih 	}
1902e3ec7017SPing-Ke Shih }
1903e3ec7017SPing-Ke Shih 
rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev * rtwdev,u8 band,u8 ru,u8 ntx,u8 ch)190407ef5f2fSZong-Zhe Yang static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band,
1905e3ec7017SPing-Ke Shih 					u8 ru, u8 ntx, u8 ch)
1906e3ec7017SPing-Ke Shih {
19075395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
19085395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz;
19095395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz;
19105395482aSZong-Zhe Yang 	const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz;
1911dad142c3SZong-Zhe Yang 	struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory;
1912bfbadacfSZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
1913bfbadacfSZong-Zhe Yang 	u32 freq = ieee80211_channel_to_frequency(ch, nl_band);
1914ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1915e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1916dad142c3SZong-Zhe Yang 	u8 reg6 = regulatory->reg_6ghz_power;
1917e3ec7017SPing-Ke Shih 	s8 lmt_ru = 0, sar;
1918e3ec7017SPing-Ke Shih 
1919e3ec7017SPing-Ke Shih 	switch (band) {
1920e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
19215395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][regd][ch_idx];
19225395482aSZong-Zhe Yang 		if (lmt_ru)
19235395482aSZong-Zhe Yang 			break;
19245395482aSZong-Zhe Yang 
19255395482aSZong-Zhe Yang 		lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1926e3ec7017SPing-Ke Shih 		break;
1927e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
19285395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][regd][ch_idx];
19295395482aSZong-Zhe Yang 		if (lmt_ru)
19305395482aSZong-Zhe Yang 			break;
19315395482aSZong-Zhe Yang 
19325395482aSZong-Zhe Yang 		lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx];
1933e3ec7017SPing-Ke Shih 		break;
1934ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
1935dad142c3SZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][regd][reg6][ch_idx];
19365395482aSZong-Zhe Yang 		if (lmt_ru)
19375395482aSZong-Zhe Yang 			break;
19385395482aSZong-Zhe Yang 
1939dad142c3SZong-Zhe Yang 		lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][RTW89_WW]
1940dad142c3SZong-Zhe Yang 					     [RTW89_REG_6GHZ_POWER_DFLT]
1941dad142c3SZong-Zhe Yang 					     [ch_idx];
1942ac74f016SZong-Zhe Yang 		break;
1943e3ec7017SPing-Ke Shih 	default:
1944e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1945e3ec7017SPing-Ke Shih 		return 0;
1946e3ec7017SPing-Ke Shih 	}
1947e3ec7017SPing-Ke Shih 
1948e3ec7017SPing-Ke Shih 	lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru);
1949bfbadacfSZong-Zhe Yang 	sar = rtw89_query_sar(rtwdev, freq);
1950e3ec7017SPing-Ke Shih 
1951e3ec7017SPing-Ke Shih 	return min(lmt_ru, sar);
1952e3ec7017SPing-Ke Shih }
1953e3ec7017SPing-Ke Shih 
1954e3ec7017SPing-Ke Shih static void
rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit_ru * lmt_ru,u8 band,u8 ntx,u8 ch)1955e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev,
1956e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
195707ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1958e3ec7017SPing-Ke Shih {
195907ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
196007ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1961e3ec7017SPing-Ke Shih 							ntx, ch);
196207ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
196307ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1964e3ec7017SPing-Ke Shih 							ntx, ch);
196507ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
196607ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1967e3ec7017SPing-Ke Shih 							 ntx, ch);
1968e3ec7017SPing-Ke Shih }
1969e3ec7017SPing-Ke Shih 
1970e3ec7017SPing-Ke Shih static void
rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit_ru * lmt_ru,u8 band,u8 ntx,u8 ch)1971e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev,
1972e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
197307ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1974e3ec7017SPing-Ke Shih {
197507ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197607ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1977e3ec7017SPing-Ke Shih 							ntx, ch - 2);
197807ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
197907ef5f2fSZong-Zhe Yang 							RTW89_RU26,
1980e3ec7017SPing-Ke Shih 							ntx, ch + 2);
198107ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198207ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1983e3ec7017SPing-Ke Shih 							ntx, ch - 2);
198407ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198507ef5f2fSZong-Zhe Yang 							RTW89_RU52,
1986e3ec7017SPing-Ke Shih 							ntx, ch + 2);
198707ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
198807ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1989e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
199007ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
199107ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
1992e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
1993e3ec7017SPing-Ke Shih }
1994e3ec7017SPing-Ke Shih 
1995e3ec7017SPing-Ke Shih static void
rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit_ru * lmt_ru,u8 band,u8 ntx,u8 ch)1996e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev,
1997e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
199807ef5f2fSZong-Zhe Yang 				  u8 band, u8 ntx, u8 ch)
1999e3ec7017SPing-Ke Shih {
200007ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200107ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2002e3ec7017SPing-Ke Shih 							ntx, ch - 6);
200307ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200407ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2005e3ec7017SPing-Ke Shih 							ntx, ch - 2);
200607ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
200707ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2008e3ec7017SPing-Ke Shih 							ntx, ch + 2);
200907ef5f2fSZong-Zhe Yang 	lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201007ef5f2fSZong-Zhe Yang 							RTW89_RU26,
2011e3ec7017SPing-Ke Shih 							ntx, ch + 6);
201207ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201307ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2014e3ec7017SPing-Ke Shih 							ntx, ch - 6);
201507ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201607ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2017e3ec7017SPing-Ke Shih 							ntx, ch - 2);
201807ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
201907ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2020e3ec7017SPing-Ke Shih 							ntx, ch + 2);
202107ef5f2fSZong-Zhe Yang 	lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202207ef5f2fSZong-Zhe Yang 							RTW89_RU52,
2023e3ec7017SPing-Ke Shih 							ntx, ch + 6);
202407ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202507ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2026e3ec7017SPing-Ke Shih 							 ntx, ch - 6);
202707ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
202807ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2029e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
203007ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
203107ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2032e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
203307ef5f2fSZong-Zhe Yang 	lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
203407ef5f2fSZong-Zhe Yang 							 RTW89_RU106,
2035e3ec7017SPing-Ke Shih 							 ntx, ch + 6);
2036e3ec7017SPing-Ke Shih }
2037e3ec7017SPing-Ke Shih 
203894b70cafSZong-Zhe Yang static void
rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev * rtwdev,struct rtw89_txpwr_limit_ru * lmt_ru,u8 band,u8 ntx,u8 ch)203994b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev,
204094b70cafSZong-Zhe Yang 				   struct rtw89_txpwr_limit_ru *lmt_ru,
204107ef5f2fSZong-Zhe Yang 				   u8 band, u8 ntx, u8 ch)
204294b70cafSZong-Zhe Yang {
204394b70cafSZong-Zhe Yang 	static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 };
204494b70cafSZong-Zhe Yang 	int i;
204594b70cafSZong-Zhe Yang 
204694b70cafSZong-Zhe Yang 	static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM);
204794b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_RU_SEC_NUM; i++) {
204807ef5f2fSZong-Zhe Yang 		lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
204994b70cafSZong-Zhe Yang 								RTW89_RU26,
205094b70cafSZong-Zhe Yang 								ntx,
205194b70cafSZong-Zhe Yang 								ch + ofst[i]);
205207ef5f2fSZong-Zhe Yang 		lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
205394b70cafSZong-Zhe Yang 								RTW89_RU52,
205494b70cafSZong-Zhe Yang 								ntx,
205594b70cafSZong-Zhe Yang 								ch + ofst[i]);
205607ef5f2fSZong-Zhe Yang 		lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
205794b70cafSZong-Zhe Yang 								 RTW89_RU106,
205894b70cafSZong-Zhe Yang 								 ntx,
205994b70cafSZong-Zhe Yang 								 ch + ofst[i]);
206094b70cafSZong-Zhe Yang 	}
206194b70cafSZong-Zhe Yang }
206294b70cafSZong-Zhe Yang 
20639b43bd1aSZong-Zhe Yang static
rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,struct rtw89_txpwr_limit_ru * lmt_ru,u8 ntx)2064e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev,
206507ef5f2fSZong-Zhe Yang 				   const struct rtw89_chan *chan,
2066e3ec7017SPing-Ke Shih 				   struct rtw89_txpwr_limit_ru *lmt_ru,
2067e3ec7017SPing-Ke Shih 				   u8 ntx)
2068e3ec7017SPing-Ke Shih {
206907ef5f2fSZong-Zhe Yang 	u8 band = chan->band_type;
2070cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
2071cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
2072e3ec7017SPing-Ke Shih 
2073e3ec7017SPing-Ke Shih 	memset(lmt_ru, 0, sizeof(*lmt_ru));
2074e3ec7017SPing-Ke Shih 
2075e3ec7017SPing-Ke Shih 	switch (bw) {
2076e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
207707ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx,
207807ef5f2fSZong-Zhe Yang 						  ch);
2079e3ec7017SPing-Ke Shih 		break;
2080e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
208107ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx,
208207ef5f2fSZong-Zhe Yang 						  ch);
2083e3ec7017SPing-Ke Shih 		break;
2084e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
208507ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx,
208607ef5f2fSZong-Zhe Yang 						  ch);
2087e3ec7017SPing-Ke Shih 		break;
208894b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
208907ef5f2fSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx,
209007ef5f2fSZong-Zhe Yang 						   ch);
209194b70cafSZong-Zhe Yang 		break;
2092e3ec7017SPing-Ke Shih 	}
2093e3ec7017SPing-Ke Shih }
20949b43bd1aSZong-Zhe Yang 
rtw89_phy_set_txpwr_byrate(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)20959b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev,
20969b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
20979b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
20989b43bd1aSZong-Zhe Yang {
2099ddf9a2eaSZong-Zhe Yang 	u8 max_nss_num = rtwdev->chip->rf_path_num;
21009b43bd1aSZong-Zhe Yang 	static const u8 rs[] = {
21019b43bd1aSZong-Zhe Yang 		RTW89_RS_CCK,
21029b43bd1aSZong-Zhe Yang 		RTW89_RS_OFDM,
21039b43bd1aSZong-Zhe Yang 		RTW89_RS_MCS,
21049b43bd1aSZong-Zhe Yang 		RTW89_RS_HEDCM,
21059b43bd1aSZong-Zhe Yang 	};
21069b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc cur;
21079b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
21089b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21099b43bd1aSZong-Zhe Yang 	u32 addr, val;
21109b43bd1aSZong-Zhe Yang 	s8 v[4] = {};
21119b43bd1aSZong-Zhe Yang 	u8 i;
21129b43bd1aSZong-Zhe Yang 
21139b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21149b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr byrate with ch=%d\n", ch);
21159b43bd1aSZong-Zhe Yang 
2116b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_CCK] % 4);
2117b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_OFDM] % 4);
2118b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_MCS] % 4);
2119b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_HEDCM] % 4);
21209b43bd1aSZong-Zhe Yang 
21219b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_BY_RATE;
2122ddf9a2eaSZong-Zhe Yang 	for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) {
21239b43bd1aSZong-Zhe Yang 		for (i = 0; i < ARRAY_SIZE(rs); i++) {
2124b4a283fbSZong-Zhe Yang 			if (cur.nss >= rtw89_rs_nss_num[rs[i]])
21259b43bd1aSZong-Zhe Yang 				continue;
21269b43bd1aSZong-Zhe Yang 
21279b43bd1aSZong-Zhe Yang 			cur.rs = rs[i];
2128b4a283fbSZong-Zhe Yang 			for (cur.idx = 0; cur.idx < rtw89_rs_idx_num[rs[i]];
21299b43bd1aSZong-Zhe Yang 			     cur.idx++) {
21309b43bd1aSZong-Zhe Yang 				v[cur.idx % 4] =
21319b43bd1aSZong-Zhe Yang 					rtw89_phy_read_txpwr_byrate(rtwdev,
21329b43bd1aSZong-Zhe Yang 								    band,
21339b43bd1aSZong-Zhe Yang 								    &cur);
21349b43bd1aSZong-Zhe Yang 
21359b43bd1aSZong-Zhe Yang 				if ((cur.idx + 1) % 4)
21369b43bd1aSZong-Zhe Yang 					continue;
21379b43bd1aSZong-Zhe Yang 
21389b43bd1aSZong-Zhe Yang 				val = FIELD_PREP(GENMASK(7, 0), v[0]) |
21399b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(15, 8), v[1]) |
21409b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(23, 16), v[2]) |
21419b43bd1aSZong-Zhe Yang 				      FIELD_PREP(GENMASK(31, 24), v[3]);
21429b43bd1aSZong-Zhe Yang 
21439b43bd1aSZong-Zhe Yang 				rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr,
21449b43bd1aSZong-Zhe Yang 							val);
21459b43bd1aSZong-Zhe Yang 				addr += 4;
21469b43bd1aSZong-Zhe Yang 			}
21479b43bd1aSZong-Zhe Yang 		}
21489b43bd1aSZong-Zhe Yang 	}
21499b43bd1aSZong-Zhe Yang }
21509b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate);
21519b43bd1aSZong-Zhe Yang 
rtw89_phy_set_txpwr_offset(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)21529b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev,
21539b43bd1aSZong-Zhe Yang 				const struct rtw89_chan *chan,
21549b43bd1aSZong-Zhe Yang 				enum rtw89_phy_idx phy_idx)
21559b43bd1aSZong-Zhe Yang {
21569b43bd1aSZong-Zhe Yang 	struct rtw89_rate_desc desc = {
21579b43bd1aSZong-Zhe Yang 		.nss = RTW89_NSS_1,
21589b43bd1aSZong-Zhe Yang 		.rs = RTW89_RS_OFFSET,
21599b43bd1aSZong-Zhe Yang 	};
21609b43bd1aSZong-Zhe Yang 	u8 band = chan->band_type;
2161b4a283fbSZong-Zhe Yang 	s8 v[RTW89_RATE_OFFSET_NUM] = {};
21629b43bd1aSZong-Zhe Yang 	u32 val;
21639b43bd1aSZong-Zhe Yang 
21649b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n");
21659b43bd1aSZong-Zhe Yang 
2166b4a283fbSZong-Zhe Yang 	for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_NUM; desc.idx++)
21679b43bd1aSZong-Zhe Yang 		v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc);
21689b43bd1aSZong-Zhe Yang 
2169b4a283fbSZong-Zhe Yang 	BUILD_BUG_ON(RTW89_RATE_OFFSET_NUM != 5);
21709b43bd1aSZong-Zhe Yang 	val = FIELD_PREP(GENMASK(3, 0), v[0]) |
21719b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(7, 4), v[1]) |
21729b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(11, 8), v[2]) |
21739b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(15, 12), v[3]) |
21749b43bd1aSZong-Zhe Yang 	      FIELD_PREP(GENMASK(19, 16), v[4]);
21759b43bd1aSZong-Zhe Yang 
21769b43bd1aSZong-Zhe Yang 	rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL,
21779b43bd1aSZong-Zhe Yang 				     GENMASK(19, 0), val);
21789b43bd1aSZong-Zhe Yang }
21799b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset);
21809b43bd1aSZong-Zhe Yang 
rtw89_phy_set_txpwr_limit(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)21819b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev,
21829b43bd1aSZong-Zhe Yang 			       const struct rtw89_chan *chan,
21839b43bd1aSZong-Zhe Yang 			       enum rtw89_phy_idx phy_idx)
21849b43bd1aSZong-Zhe Yang {
2185ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
21869b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit lmt;
21879b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
21889b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
21899b43bd1aSZong-Zhe Yang 	const s8 *ptr;
21909b43bd1aSZong-Zhe Yang 	u32 addr, val;
21919b43bd1aSZong-Zhe Yang 	u8 i, j;
21929b43bd1aSZong-Zhe Yang 
21939b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
21949b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw);
21959b43bd1aSZong-Zhe Yang 
21969b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) !=
21979b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_PAGE_SIZE);
21989b43bd1aSZong-Zhe Yang 
21999b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_LMT;
2200ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
22019b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i);
22029b43bd1aSZong-Zhe Yang 
22039b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt;
22049b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE;
22059b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
22069b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
22079b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
22089b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
22099b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
22109b43bd1aSZong-Zhe Yang 
22119b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
22129b43bd1aSZong-Zhe Yang 		}
22139b43bd1aSZong-Zhe Yang 	}
22149b43bd1aSZong-Zhe Yang }
22159b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit);
22169b43bd1aSZong-Zhe Yang 
rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)22179b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev,
22189b43bd1aSZong-Zhe Yang 				  const struct rtw89_chan *chan,
22199b43bd1aSZong-Zhe Yang 				  enum rtw89_phy_idx phy_idx)
22209b43bd1aSZong-Zhe Yang {
2221ddf9a2eaSZong-Zhe Yang 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
22229b43bd1aSZong-Zhe Yang 	struct rtw89_txpwr_limit_ru lmt_ru;
22239b43bd1aSZong-Zhe Yang 	u8 ch = chan->channel;
22249b43bd1aSZong-Zhe Yang 	u8 bw = chan->band_width;
22259b43bd1aSZong-Zhe Yang 	const s8 *ptr;
22269b43bd1aSZong-Zhe Yang 	u32 addr, val;
22279b43bd1aSZong-Zhe Yang 	u8 i, j;
22289b43bd1aSZong-Zhe Yang 
22299b43bd1aSZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
22309b43bd1aSZong-Zhe Yang 		    "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw);
22319b43bd1aSZong-Zhe Yang 
22329b43bd1aSZong-Zhe Yang 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) !=
22339b43bd1aSZong-Zhe Yang 		     RTW89_TXPWR_LMT_RU_PAGE_SIZE);
22349b43bd1aSZong-Zhe Yang 
22359b43bd1aSZong-Zhe Yang 	addr = R_AX_PWR_RU_LMT;
2236ddf9a2eaSZong-Zhe Yang 	for (i = 0; i < max_ntx_num; i++) {
22379b43bd1aSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i);
22389b43bd1aSZong-Zhe Yang 
22399b43bd1aSZong-Zhe Yang 		ptr = (s8 *)&lmt_ru;
22409b43bd1aSZong-Zhe Yang 		for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE;
22419b43bd1aSZong-Zhe Yang 		     j += 4, addr += 4, ptr += 4) {
22429b43bd1aSZong-Zhe Yang 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
22439b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
22449b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
22459b43bd1aSZong-Zhe Yang 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
22469b43bd1aSZong-Zhe Yang 
22479b43bd1aSZong-Zhe Yang 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
22489b43bd1aSZong-Zhe Yang 		}
22499b43bd1aSZong-Zhe Yang 	}
22509b43bd1aSZong-Zhe Yang }
22519b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru);
2252e3ec7017SPing-Ke Shih 
2253e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data {
2254e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
2255e3ec7017SPing-Ke Shih 	struct sk_buff *c2h;
2256e3ec7017SPing-Ke Shih };
2257e3ec7017SPing-Ke Shih 
rtw89_phy_c2h_ra_rpt_iter(void * data,struct ieee80211_sta * sta)2258e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta)
2259e3ec7017SPing-Ke Shih {
2260e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data;
2261e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = ra_data->rtwdev;
2262e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
226357cafeb1SPing-Ke Shih 	const struct rtw89_c2h_ra_rpt *c2h =
226457cafeb1SPing-Ke Shih 		(const struct rtw89_c2h_ra_rpt *)ra_data->c2h->data;
2265e3ec7017SPing-Ke Shih 	struct rtw89_ra_report *ra_report = &rtwsta->ra_report;
22665c152231SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
22675c152231SPing-Ke Shih 	bool format_v1 = chip->chip_gen == RTW89_CHIP_BE;
2268e3ec7017SPing-Ke Shih 	u8 mode, rate, bw, giltf, mac_id;
22699a3a593cSPing-Ke Shih 	u16 legacy_bitrate;
22709a3a593cSPing-Ke Shih 	bool valid;
22710d466f05SPing-Ke Shih 	u8 mcs = 0;
22725c152231SPing-Ke Shih 	u8 t;
2273e3ec7017SPing-Ke Shih 
227457cafeb1SPing-Ke Shih 	mac_id = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MACID);
2275e3ec7017SPing-Ke Shih 	if (mac_id != rtwsta->mac_id)
2276e3ec7017SPing-Ke Shih 		return;
2277e3ec7017SPing-Ke Shih 
227857cafeb1SPing-Ke Shih 	rate = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MCSNSS);
227957cafeb1SPing-Ke Shih 	bw = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW);
228057cafeb1SPing-Ke Shih 	giltf = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_GILTF);
228157cafeb1SPing-Ke Shih 	mode = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL);
2282e3ec7017SPing-Ke Shih 
22835c152231SPing-Ke Shih 	if (format_v1) {
22845c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MCSNSS_B7);
22855c152231SPing-Ke Shih 		rate |= u8_encode_bits(t, BIT(7));
22865c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW_B2);
22875c152231SPing-Ke Shih 		bw |= u8_encode_bits(t, BIT(2));
22885c152231SPing-Ke Shih 		t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL_B2);
22895c152231SPing-Ke Shih 		mode |= u8_encode_bits(t, BIT(2));
22905c152231SPing-Ke Shih 	}
22915c152231SPing-Ke Shih 
22929a3a593cSPing-Ke Shih 	if (mode == RTW89_RA_RPT_MODE_LEGACY) {
22939a3a593cSPing-Ke Shih 		valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate);
22949a3a593cSPing-Ke Shih 		if (!valid)
22959a3a593cSPing-Ke Shih 			return;
22969a3a593cSPing-Ke Shih 	}
22979a3a593cSPing-Ke Shih 
22980d466f05SPing-Ke Shih 	memset(&ra_report->txrate, 0, sizeof(ra_report->txrate));
22999a3a593cSPing-Ke Shih 
2300e3ec7017SPing-Ke Shih 	switch (mode) {
2301e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_LEGACY:
23029a3a593cSPing-Ke Shih 		ra_report->txrate.legacy = legacy_bitrate;
2303e3ec7017SPing-Ke Shih 		break;
2304e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HT:
2305e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS;
230611fe4ccdSZong-Zhe Yang 		if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw))
2307e3ec7017SPing-Ke Shih 			rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate),
2308e3ec7017SPing-Ke Shih 						FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate));
2309e3ec7017SPing-Ke Shih 		else
2310e3ec7017SPing-Ke Shih 			rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate);
2311e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = rate;
2312e3ec7017SPing-Ke Shih 		if (giltf)
2313e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
23140d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs & 0x07;
2315e3ec7017SPing-Ke Shih 		break;
2316e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_VHT:
2317e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
23185c152231SPing-Ke Shih 		ra_report->txrate.mcs = format_v1 ?
23195c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) :
23205c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS);
23215c152231SPing-Ke Shih 		ra_report->txrate.nss = format_v1 ?
23225c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 :
23235c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1;
2324e3ec7017SPing-Ke Shih 		if (giltf)
2325e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
23260d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2327e3ec7017SPing-Ke Shih 		break;
2328e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HE:
2329e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS;
23305c152231SPing-Ke Shih 		ra_report->txrate.mcs = format_v1 ?
23315c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) :
23325c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS);
23335c152231SPing-Ke Shih 		ra_report->txrate.nss  = format_v1 ?
23345c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 :
23355c152231SPing-Ke Shih 			u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1;
2336e3ec7017SPing-Ke Shih 		if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08)
2337e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2338e3ec7017SPing-Ke Shih 		else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16)
2339e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2340e3ec7017SPing-Ke Shih 		else
2341e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2;
23420d466f05SPing-Ke Shih 		mcs = ra_report->txrate.mcs;
2343e3ec7017SPing-Ke Shih 		break;
2344e3ec7017SPing-Ke Shih 	}
2345e3ec7017SPing-Ke Shih 
2346167044afSPing-Ke Shih 	ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw);
2347e3ec7017SPing-Ke Shih 	ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate);
23485c152231SPing-Ke Shih 	ra_report->hw_rate = format_v1 ?
23495c152231SPing-Ke Shih 			     u16_encode_bits(mode, RTW89_HW_RATE_V1_MASK_MOD) |
23505c152231SPing-Ke Shih 			     u16_encode_bits(rate, RTW89_HW_RATE_V1_MASK_VAL) :
23515c152231SPing-Ke Shih 			     u16_encode_bits(mode, RTW89_HW_RATE_MASK_MOD) |
23525c152231SPing-Ke Shih 			     u16_encode_bits(rate, RTW89_HW_RATE_MASK_VAL);
23530d466f05SPing-Ke Shih 	ra_report->might_fallback_legacy = mcs <= 2;
23544c51541dSBenjamin Berg 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report);
23554c51541dSBenjamin Berg 	rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1;
2356e3ec7017SPing-Ke Shih }
2357e3ec7017SPing-Ke Shih 
2358e3ec7017SPing-Ke Shih static void
rtw89_phy_c2h_ra_rpt(struct rtw89_dev * rtwdev,struct sk_buff * c2h,u32 len)2359e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
2360e3ec7017SPing-Ke Shih {
2361e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data ra_data;
2362e3ec7017SPing-Ke Shih 
2363e3ec7017SPing-Ke Shih 	ra_data.rtwdev = rtwdev;
2364e3ec7017SPing-Ke Shih 	ra_data.c2h = c2h;
2365e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2366e3ec7017SPing-Ke Shih 					  rtw89_phy_c2h_ra_rpt_iter,
2367e3ec7017SPing-Ke Shih 					  &ra_data);
2368e3ec7017SPing-Ke Shih }
2369e3ec7017SPing-Ke Shih 
2370e3ec7017SPing-Ke Shih static
2371e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev,
2372e3ec7017SPing-Ke Shih 					  struct sk_buff *c2h, u32 len) = {
2373e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt,
2374e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL,
2375e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_TXSTS] = NULL,
2376e3ec7017SPing-Ke Shih };
2377e3ec7017SPing-Ke Shih 
rtw89_phy_c2h_handle(struct rtw89_dev * rtwdev,struct sk_buff * skb,u32 len,u8 class,u8 func)2378e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
2379e3ec7017SPing-Ke Shih 			  u32 len, u8 class, u8 func)
2380e3ec7017SPing-Ke Shih {
2381e3ec7017SPing-Ke Shih 	void (*handler)(struct rtw89_dev *rtwdev,
2382e3ec7017SPing-Ke Shih 			struct sk_buff *c2h, u32 len) = NULL;
2383e3ec7017SPing-Ke Shih 
2384e3ec7017SPing-Ke Shih 	switch (class) {
2385e3ec7017SPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_RA:
2386e3ec7017SPing-Ke Shih 		if (func < RTW89_PHY_C2H_FUNC_RA_MAX)
2387e3ec7017SPing-Ke Shih 			handler = rtw89_phy_c2h_ra_handler[func];
2388e3ec7017SPing-Ke Shih 		break;
23893b66519bSPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_DM:
23903b66519bSPing-Ke Shih 		if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY)
23913b66519bSPing-Ke Shih 			return;
23923b66519bSPing-Ke Shih 		fallthrough;
2393e3ec7017SPing-Ke Shih 	default:
2394e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
2395e3ec7017SPing-Ke Shih 		return;
2396e3ec7017SPing-Ke Shih 	}
2397e3ec7017SPing-Ke Shih 	if (!handler) {
2398e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
2399e3ec7017SPing-Ke Shih 			   func);
2400e3ec7017SPing-Ke Shih 		return;
2401e3ec7017SPing-Ke Shih 	}
2402e3ec7017SPing-Ke Shih 	handler(rtwdev, skb, len);
2403e3ec7017SPing-Ke Shih }
2404e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev * rtwdev,bool sc_xo)2405e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo)
2406e3ec7017SPing-Ke Shih {
24070789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2408e3ec7017SPing-Ke Shih 	u32 reg_mask;
2409e3ec7017SPing-Ke Shih 
2410e3ec7017SPing-Ke Shih 	if (sc_xo)
24110789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2412e3ec7017SPing-Ke Shih 	else
24130789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2414e3ec7017SPing-Ke Shih 
24150789881aSChia-Yuan Li 	return (u8)rtw89_read32_mask(rtwdev, xtal->xcap_reg, reg_mask);
2416e3ec7017SPing-Ke Shih }
2417e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev * rtwdev,bool sc_xo,u8 val)2418e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo,
2419e3ec7017SPing-Ke Shih 				       u8 val)
2420e3ec7017SPing-Ke Shih {
24210789881aSChia-Yuan Li 	const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info;
2422e3ec7017SPing-Ke Shih 	u32 reg_mask;
2423e3ec7017SPing-Ke Shih 
2424e3ec7017SPing-Ke Shih 	if (sc_xo)
24250789881aSChia-Yuan Li 		reg_mask = xtal->sc_xo_mask;
2426e3ec7017SPing-Ke Shih 	else
24270789881aSChia-Yuan Li 		reg_mask = xtal->sc_xi_mask;
2428e3ec7017SPing-Ke Shih 
24290789881aSChia-Yuan Li 	rtw89_write32_mask(rtwdev, xtal->xcap_reg, reg_mask, val);
2430e3ec7017SPing-Ke Shih }
2431e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev * rtwdev,u8 crystal_cap,bool force)2432e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev,
2433e3ec7017SPing-Ke Shih 					  u8 crystal_cap, bool force)
2434e3ec7017SPing-Ke Shih {
2435e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
24368379fa61SYuan-Han Zhang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2437e3ec7017SPing-Ke Shih 	u8 sc_xi_val, sc_xo_val;
2438e3ec7017SPing-Ke Shih 
2439e3ec7017SPing-Ke Shih 	if (!force && cfo->crystal_cap == crystal_cap)
2440e3ec7017SPing-Ke Shih 		return;
2441e3ec7017SPing-Ke Shih 	crystal_cap = clamp_t(u8, crystal_cap, 0, 127);
24420789881aSChia-Yuan Li 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8851B) {
2443e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap);
2444e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap);
2445e3ec7017SPing-Ke Shih 		sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true);
2446e3ec7017SPing-Ke Shih 		sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false);
24478379fa61SYuan-Han Zhang 	} else {
24488379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO,
24498379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XO_MASK);
24508379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI,
24518379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XI_MASK);
24528379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val);
24538379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val);
24548379fa61SYuan-Han Zhang 	}
2455e3ec7017SPing-Ke Shih 	cfo->crystal_cap = sc_xi_val;
2456e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap);
2457e3ec7017SPing-Ke Shih 
2458e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val);
2459e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val);
2460e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n",
2461e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2462e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n");
2463e3ec7017SPing-Ke Shih }
2464e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_reset(struct rtw89_dev * rtwdev)2465e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev)
2466e3ec7017SPing-Ke Shih {
2467e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2468e3ec7017SPing-Ke Shih 	u8 cap;
2469e3ec7017SPing-Ke Shih 
2470e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK;
2471e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2472e3ec7017SPing-Ke Shih 	if (cfo->crystal_cap == cfo->def_x_cap)
2473e3ec7017SPing-Ke Shih 		return;
2474e3ec7017SPing-Ke Shih 	cap = cfo->crystal_cap;
2475e3ec7017SPing-Ke Shih 	cap += (cap > cfo->def_x_cap ? -1 : 1);
2476e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false);
2477e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2478e3ec7017SPing-Ke Shih 		    "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap,
2479e3ec7017SPing-Ke Shih 		    cfo->def_x_cap);
2480e3ec7017SPing-Ke Shih }
2481e3ec7017SPing-Ke Shih 
rtw89_dcfo_comp(struct rtw89_dev * rtwdev,s32 curr_cfo)2482e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo)
2483e3ec7017SPing-Ke Shih {
2484b7379148SYuan-Han Zhang 	const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp;
2485e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
2486e3ec7017SPing-Ke Shih 	s32 cfo_avg_312;
2487b7379148SYuan-Han Zhang 	s32 dcfo_comp_val;
2488e3ec7017SPing-Ke Shih 	int sign;
2489e3ec7017SPing-Ke Shih 
2490e3ec7017SPing-Ke Shih 	if (!is_linked) {
2491e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n",
2492e3ec7017SPing-Ke Shih 			    is_linked);
2493e3ec7017SPing-Ke Shih 		return;
2494e3ec7017SPing-Ke Shih 	}
2495e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo);
2496e3ec7017SPing-Ke Shih 	if (curr_cfo == 0)
2497e3ec7017SPing-Ke Shih 		return;
2498b7379148SYuan-Han Zhang 	dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO);
2499e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
25009f9882dbSEric Huang 	cfo_avg_312 = curr_cfo / 625 + sign * dcfo_comp_val;
25019f9882dbSEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "avg_cfo_312=%d step\n", cfo_avg_312);
2502e3ec7017SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV)
2503e3ec7017SPing-Ke Shih 		cfo_avg_312 = -cfo_avg_312;
2504b7379148SYuan-Han Zhang 	rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask,
2505e3ec7017SPing-Ke Shih 			       cfo_avg_312);
2506e3ec7017SPing-Ke Shih }
2507e3ec7017SPing-Ke Shih 
rtw89_dcfo_comp_init(struct rtw89_dev * rtwdev)2508e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev)
2509e3ec7017SPing-Ke Shih {
25109f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
25119f9882dbSEric Huang 
2512e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1);
2513e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8);
25149f9882dbSEric Huang 
25159f9882dbSEric Huang 	if (chip->cfo_hw_comp)
25169f9882dbSEric Huang 		rtw89_write32_mask(rtwdev, R_AX_PWR_UL_CTRL2,
25179f9882dbSEric Huang 				   B_AX_PWR_UL_CFO_MASK, 0x6);
25189f9882dbSEric Huang 	else
2519e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK);
2520e3ec7017SPing-Ke Shih }
2521e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_init(struct rtw89_dev * rtwdev)2522e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev)
2523e3ec7017SPing-Ke Shih {
2524e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2525e3ec7017SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
2526e3ec7017SPing-Ke Shih 
2527e3ec7017SPing-Ke Shih 	cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK;
2528e3ec7017SPing-Ke Shih 	cfo->crystal_cap = cfo->crystal_cap_default;
2529e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap;
2530a9e06f2eSYi-Tang Chiu 	cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f);
2531a9e06f2eSYi-Tang Chiu 	cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1);
2532e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2533a9e06f2eSYi-Tang Chiu 	cfo->divergence_lock_en = false;
2534e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = 0;
2535a9e06f2eSYi-Tang Chiu 	cfo->lock_cnt = 0;
2536e3ec7017SPing-Ke Shih 	cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE;
2537e3ec7017SPing-Ke Shih 	cfo->apply_compensation = false;
2538e3ec7017SPing-Ke Shih 	cfo->residual_cfo_acc = 0;
2539e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n",
2540e3ec7017SPing-Ke Shih 		    cfo->crystal_cap_default);
2541e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true);
2542e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1);
2543e3ec7017SPing-Ke Shih 	rtw89_dcfo_comp_init(rtwdev);
2544e3ec7017SPing-Ke Shih 	cfo->cfo_timer_ms = 2000;
2545e3ec7017SPing-Ke Shih 	cfo->cfo_trig_by_timer_en = false;
2546e3ec7017SPing-Ke Shih 	cfo->phy_cfo_trk_cnt = 0;
2547e3ec7017SPing-Ke Shih 	cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2548bc013052SEric Huang 	cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE;
2549e3ec7017SPing-Ke Shih }
2550e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev * rtwdev,s32 curr_cfo)2551e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev,
2552e3ec7017SPing-Ke Shih 					     s32 curr_cfo)
2553e3ec7017SPing-Ke Shih {
2554e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2555e3ec7017SPing-Ke Shih 	s8 crystal_cap = cfo->crystal_cap;
2556e3ec7017SPing-Ke Shih 	s32 cfo_abs = abs(curr_cfo);
2557e3ec7017SPing-Ke Shih 	int sign;
2558e3ec7017SPing-Ke Shih 
2559e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2560e3ec7017SPing-Ke Shih 		if (cfo_abs > CFO_TRK_ENABLE_TH)
2561e3ec7017SPing-Ke Shih 			cfo->is_adjust = true;
2562e3ec7017SPing-Ke Shih 	} else {
2563e3ec7017SPing-Ke Shih 		if (cfo_abs < CFO_TRK_STOP_TH)
2564e3ec7017SPing-Ke Shih 			cfo->is_adjust = false;
2565e3ec7017SPing-Ke Shih 	}
2566e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2567e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n");
2568e3ec7017SPing-Ke Shih 		return;
2569e3ec7017SPing-Ke Shih 	}
2570e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
2571e3ec7017SPing-Ke Shih 	if (cfo_abs > CFO_TRK_STOP_TH_4)
2572e3ec7017SPing-Ke Shih 		crystal_cap += 7 * sign;
2573e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_3)
2574e3ec7017SPing-Ke Shih 		crystal_cap += 5 * sign;
2575e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_2)
2576e3ec7017SPing-Ke Shih 		crystal_cap += 3 * sign;
2577e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_1)
2578e3ec7017SPing-Ke Shih 		crystal_cap += 1 * sign;
2579e3ec7017SPing-Ke Shih 	else
2580e3ec7017SPing-Ke Shih 		return;
2581e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false);
2582e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2583e3ec7017SPing-Ke Shih 		    "X_cap{Curr,Default}={0x%x,0x%x}\n",
2584e3ec7017SPing-Ke Shih 		    cfo->crystal_cap, cfo->def_x_cap);
2585e3ec7017SPing-Ke Shih }
2586e3ec7017SPing-Ke Shih 
rtw89_phy_average_cfo_calc(struct rtw89_dev * rtwdev)2587e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev)
2588e3ec7017SPing-Ke Shih {
25899f9882dbSEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2590e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2591e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2592e3ec7017SPing-Ke Shih 	s32 cfo_cnt_all = 0;
2593e3ec7017SPing-Ke Shih 	s32 cfo_all_avg = 0;
2594e3ec7017SPing-Ke Shih 	u8 i;
2595e3ec7017SPing-Ke Shih 
2596e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc != 1)
2597e3ec7017SPing-Ke Shih 		return 0;
2598e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n");
2599e3ec7017SPing-Ke Shih 	for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2600e3ec7017SPing-Ke Shih 		if (cfo->cfo_cnt[i] == 0)
2601e3ec7017SPing-Ke Shih 			continue;
2602e3ec7017SPing-Ke Shih 		cfo_khz_all += cfo->cfo_tail[i];
2603e3ec7017SPing-Ke Shih 		cfo_cnt_all += cfo->cfo_cnt[i];
2604e3ec7017SPing-Ke Shih 		cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all);
2605e3ec7017SPing-Ke Shih 		cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
26069f9882dbSEric Huang 		cfo->dcfo_avg = phy_div(cfo_khz_all << chip->dcfo_comp_sft,
26079f9882dbSEric Huang 					cfo_cnt_all);
2608e3ec7017SPing-Ke Shih 	}
2609e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2610e3ec7017SPing-Ke Shih 		    "CFO track for macid = %d\n", i);
2611e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2612e3ec7017SPing-Ke Shih 		    "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n",
2613e3ec7017SPing-Ke Shih 		    cfo_khz_all, cfo_cnt_all, cfo_all_avg);
2614e3ec7017SPing-Ke Shih 	return cfo_all_avg;
2615e3ec7017SPing-Ke Shih }
2616e3ec7017SPing-Ke Shih 
rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev * rtwdev)2617e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev)
2618e3ec7017SPing-Ke Shih {
2619e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2620e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2621e3ec7017SPing-Ke Shih 	s32 target_cfo = 0;
2622e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2623e3ec7017SPing-Ke Shih 	s32 cfo_khz_all_tp_wgt = 0;
2624e3ec7017SPing-Ke Shih 	s32 cfo_avg = 0;
2625e3ec7017SPing-Ke Shih 	s32 max_cfo_lb = BIT(31);
2626e3ec7017SPing-Ke Shih 	s32 min_cfo_ub = GENMASK(30, 0);
2627e3ec7017SPing-Ke Shih 	u16 cfo_cnt_all = 0;
2628e3ec7017SPing-Ke Shih 	u8 active_entry_cnt = 0;
2629e3ec7017SPing-Ke Shih 	u8 sta_cnt = 0;
2630e3ec7017SPing-Ke Shih 	u32 tp_all = 0;
2631e3ec7017SPing-Ke Shih 	u8 i;
2632e3ec7017SPing-Ke Shih 	u8 cfo_tol = 0;
2633e3ec7017SPing-Ke Shih 
2634e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n");
2635e3ec7017SPing-Ke Shih 	if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) {
2636e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n");
2637e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2638e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2639e3ec7017SPing-Ke Shih 				continue;
2640e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_tail[i];
2641e3ec7017SPing-Ke Shih 			cfo_cnt_all += cfo->cfo_cnt[i];
2642e3ec7017SPing-Ke Shih 			cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all);
2643e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2644e3ec7017SPing-Ke Shih 				    "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n",
2645e3ec7017SPing-Ke Shih 				    cfo_khz_all, cfo_cnt_all, cfo_avg);
2646e3ec7017SPing-Ke Shih 			target_cfo = cfo_avg;
2647e3ec7017SPing-Ke Shih 		}
2648e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) {
2649e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n");
2650e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2651e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2652e3ec7017SPing-Ke Shih 				continue;
2653e3ec7017SPing-Ke Shih 			cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2654e3ec7017SPing-Ke Shih 						  (s32)cfo->cfo_cnt[i]);
2655e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2656e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2657e3ec7017SPing-Ke Shih 				    "Macid=%d, cfo_avg=%d\n", i,
2658e3ec7017SPing-Ke Shih 				    cfo->cfo_avg[i]);
2659e3ec7017SPing-Ke Shih 		}
2660e3ec7017SPing-Ke Shih 		sta_cnt = rtwdev->total_sta_assoc;
2661e3ec7017SPing-Ke Shih 		cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt);
2662e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2663e3ec7017SPing-Ke Shih 			    "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n",
2664e3ec7017SPing-Ke Shih 			    cfo_khz_all, sta_cnt, cfo_avg);
2665e3ec7017SPing-Ke Shih 		target_cfo = cfo_avg;
2666e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) {
2667e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n");
2668e3ec7017SPing-Ke Shih 		cfo_tol = cfo->sta_cfo_tolerance;
2669e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2670e3ec7017SPing-Ke Shih 			sta_cnt++;
2671e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] != 0) {
2672e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2673e3ec7017SPing-Ke Shih 							  (s32)cfo->cfo_cnt[i]);
2674e3ec7017SPing-Ke Shih 				active_entry_cnt++;
2675e3ec7017SPing-Ke Shih 			} else {
2676e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = cfo->pre_cfo_avg[i];
2677e3ec7017SPing-Ke Shih 			}
2678e3ec7017SPing-Ke Shih 			max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb);
2679e3ec7017SPing-Ke Shih 			min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub);
2680e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2681e3ec7017SPing-Ke Shih 			/* need tp for each entry */
2682e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2683e3ec7017SPing-Ke Shih 				    "[%d] cfo_avg=%d, tp=tbd\n",
2684e3ec7017SPing-Ke Shih 				    i, cfo->cfo_avg[i]);
2685e3ec7017SPing-Ke Shih 			if (sta_cnt >= rtwdev->total_sta_assoc)
2686e3ec7017SPing-Ke Shih 				break;
2687e3ec7017SPing-Ke Shih 		}
2688e3ec7017SPing-Ke Shih 		tp_all = stats->rx_throughput; /* need tp for each entry */
2689e3ec7017SPing-Ke Shih 		cfo_avg =  phy_div(cfo_khz_all_tp_wgt, (s32)tp_all);
2690e3ec7017SPing-Ke Shih 
2691e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n",
2692e3ec7017SPing-Ke Shih 			    sta_cnt);
2693e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n",
2694e3ec7017SPing-Ke Shih 			    active_entry_cnt);
2695e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2696e3ec7017SPing-Ke Shih 			    "Msta cfo with tp_wgt=%d, avg_cfo=%d\n",
2697e3ec7017SPing-Ke Shih 			    cfo_khz_all_tp_wgt, cfo_avg);
2698e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n",
2699e3ec7017SPing-Ke Shih 			    max_cfo_lb, min_cfo_ub);
2700e3ec7017SPing-Ke Shih 		if (max_cfo_lb <= min_cfo_ub) {
2701e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2702e3ec7017SPing-Ke Shih 				    "cfo win_size=%d\n",
2703e3ec7017SPing-Ke Shih 				    min_cfo_ub - max_cfo_lb);
2704e3ec7017SPing-Ke Shih 			target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub);
2705e3ec7017SPing-Ke Shih 		} else {
2706e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2707c51ed740SColin Ian King 				    "No intersection of cfo tolerance windows\n");
2708e3ec7017SPing-Ke Shih 			target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt);
2709e3ec7017SPing-Ke Shih 		}
2710e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++)
2711e3ec7017SPing-Ke Shih 			cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2712e3ec7017SPing-Ke Shih 	}
2713e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo);
2714e3ec7017SPing-Ke Shih 	return target_cfo;
2715e3ec7017SPing-Ke Shih }
2716e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_statistics_reset(struct rtw89_dev * rtwdev)2717e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev)
2718e3ec7017SPing-Ke Shih {
2719e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2720e3ec7017SPing-Ke Shih 
2721e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail));
2722e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt));
2723e3ec7017SPing-Ke Shih 	cfo->packet_count = 0;
2724e3ec7017SPing-Ke Shih 	cfo->packet_count_pre = 0;
2725e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = 0;
2726e3ec7017SPing-Ke Shih }
2727e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_dm(struct rtw89_dev * rtwdev)2728e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev)
2729e3ec7017SPing-Ke Shih {
2730e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2731e3ec7017SPing-Ke Shih 	s32 new_cfo = 0;
2732e3ec7017SPing-Ke Shih 	bool x_cap_update = false;
2733e3ec7017SPing-Ke Shih 	u8 pre_x_cap = cfo->crystal_cap;
27349f9882dbSEric Huang 	u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft;
2735e3ec7017SPing-Ke Shih 
27369f9882dbSEric Huang 	cfo->dcfo_avg = 0;
2737e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n",
2738e3ec7017SPing-Ke Shih 		    rtwdev->total_sta_assoc);
2739e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 0) {
2740e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_reset(rtwdev);
2741e3ec7017SPing-Ke Shih 		return;
2742e3ec7017SPing-Ke Shih 	}
2743e3ec7017SPing-Ke Shih 	if (cfo->packet_count == 0) {
2744e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n");
2745e3ec7017SPing-Ke Shih 		return;
2746e3ec7017SPing-Ke Shih 	}
2747e3ec7017SPing-Ke Shih 	if (cfo->packet_count == cfo->packet_count_pre) {
2748e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n");
2749e3ec7017SPing-Ke Shih 		return;
2750e3ec7017SPing-Ke Shih 	}
2751e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 1)
2752e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_average_cfo_calc(rtwdev);
2753e3ec7017SPing-Ke Shih 	else
2754e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev);
2755e3ec7017SPing-Ke Shih 	if (new_cfo == 0) {
2756e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n");
2757e3ec7017SPing-Ke Shih 		return;
2758e3ec7017SPing-Ke Shih 	}
2759a9e06f2eSYi-Tang Chiu 	if (cfo->divergence_lock_en) {
2760a9e06f2eSYi-Tang Chiu 		cfo->lock_cnt++;
2761a9e06f2eSYi-Tang Chiu 		if (cfo->lock_cnt > CFO_PERIOD_CNT) {
2762a9e06f2eSYi-Tang Chiu 			cfo->divergence_lock_en = false;
2763a9e06f2eSYi-Tang Chiu 			cfo->lock_cnt = 0;
2764a9e06f2eSYi-Tang Chiu 		} else {
2765a9e06f2eSYi-Tang Chiu 			rtw89_phy_cfo_reset(rtwdev);
2766a9e06f2eSYi-Tang Chiu 		}
2767a9e06f2eSYi-Tang Chiu 		return;
2768a9e06f2eSYi-Tang Chiu 	}
2769a9e06f2eSYi-Tang Chiu 	if (cfo->crystal_cap >= cfo->x_cap_ub ||
2770a9e06f2eSYi-Tang Chiu 	    cfo->crystal_cap <= cfo->x_cap_lb) {
2771a9e06f2eSYi-Tang Chiu 		cfo->divergence_lock_en = true;
2772a9e06f2eSYi-Tang Chiu 		rtw89_phy_cfo_reset(rtwdev);
2773a9e06f2eSYi-Tang Chiu 		return;
2774a9e06f2eSYi-Tang Chiu 	}
2775a9e06f2eSYi-Tang Chiu 
2776e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo);
2777e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = new_cfo;
27789f9882dbSEric Huang 	cfo->dcfo_avg_pre = cfo->dcfo_avg;
27791646ce8fSYe Guojin 	x_cap_update =  cfo->crystal_cap != pre_x_cap;
2780e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update);
2781e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n",
2782e3ec7017SPing-Ke Shih 		    cfo->def_x_cap, pre_x_cap, cfo->crystal_cap,
2783e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2784e3ec7017SPing-Ke Shih 	if (x_cap_update) {
27859f9882dbSEric Huang 		if (cfo->dcfo_avg > 0)
27869f9882dbSEric Huang 			cfo->dcfo_avg -= CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2787e3ec7017SPing-Ke Shih 		else
27889f9882dbSEric Huang 			cfo->dcfo_avg += CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft;
2789e3ec7017SPing-Ke Shih 	}
27909f9882dbSEric Huang 	rtw89_dcfo_comp(rtwdev, cfo->dcfo_avg);
2791e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_statistics_reset(rtwdev);
2792e3ec7017SPing-Ke Shih }
2793e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_track_work(struct work_struct * work)2794e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work)
2795e3ec7017SPing-Ke Shih {
2796e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2797e3ec7017SPing-Ke Shih 						cfo_track_work.work);
2798e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2799e3ec7017SPing-Ke Shih 
2800e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
2801e3ec7017SPing-Ke Shih 	if (!cfo->cfo_trig_by_timer_en)
2802e3ec7017SPing-Ke Shih 		goto out;
2803e3ec7017SPing-Ke Shih 	rtw89_leave_ps_mode(rtwdev);
2804e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2805e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2806e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2807e3ec7017SPing-Ke Shih out:
2808e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
2809e3ec7017SPing-Ke Shih }
2810e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_start_work(struct rtw89_dev * rtwdev)2811e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev)
2812e3ec7017SPing-Ke Shih {
2813e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2814e3ec7017SPing-Ke Shih 
2815e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2816e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2817e3ec7017SPing-Ke Shih }
2818e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_track(struct rtw89_dev * rtwdev)2819e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev)
2820e3ec7017SPing-Ke Shih {
2821e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2822e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2823bc013052SEric Huang 	bool is_ul_ofdma = false, ofdma_acc_en = false;
2824bc013052SEric Huang 
2825bc013052SEric Huang 	if (stats->rx_tf_periodic > CFO_TF_CNT_TH)
2826bc013052SEric Huang 		is_ul_ofdma = true;
2827bc013052SEric Huang 	if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE &&
2828bc013052SEric Huang 	    is_ul_ofdma)
2829bc013052SEric Huang 		ofdma_acc_en = true;
2830e3ec7017SPing-Ke Shih 
2831e3ec7017SPing-Ke Shih 	switch (cfo->phy_cfo_status) {
2832e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_NORMAL:
2833e3ec7017SPing-Ke Shih 		if (stats->tx_throughput >= CFO_TP_UPPER) {
2834e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE;
2835e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = true;
2836e3ec7017SPing-Ke Shih 			cfo->cfo_timer_ms = CFO_COMP_PERIOD;
2837e3ec7017SPing-Ke Shih 			rtw89_phy_cfo_start_work(rtwdev);
2838e3ec7017SPing-Ke Shih 		}
2839e3ec7017SPing-Ke Shih 		break;
2840e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_ENHANCE:
2841bc013052SEric Huang 		if (stats->tx_throughput <= CFO_TP_LOWER)
2842bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2843bc013052SEric Huang 		else if (ofdma_acc_en &&
2844bc013052SEric Huang 			 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT)
2845bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD;
2846bc013052SEric Huang 		else
2847bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2848bc013052SEric Huang 
2849bc013052SEric Huang 		if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) {
2850e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2851e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2852e3ec7017SPing-Ke Shih 		}
2853bc013052SEric Huang 		break;
2854bc013052SEric Huang 	case RTW89_PHY_DCFO_STATE_HOLD:
2855e3ec7017SPing-Ke Shih 		if (stats->tx_throughput <= CFO_TP_LOWER) {
2856e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2857e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2858e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2859bc013052SEric Huang 		} else {
2860bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2861e3ec7017SPing-Ke Shih 		}
2862e3ec7017SPing-Ke Shih 		break;
2863e3ec7017SPing-Ke Shih 	default:
2864e3ec7017SPing-Ke Shih 		cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2865e3ec7017SPing-Ke Shih 		cfo->phy_cfo_trk_cnt = 0;
2866e3ec7017SPing-Ke Shih 		break;
2867e3ec7017SPing-Ke Shih 	}
2868e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2869e3ec7017SPing-Ke Shih 		    "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n",
2870e3ec7017SPing-Ke Shih 		    stats->tx_throughput, cfo->phy_cfo_status,
2871e3ec7017SPing-Ke Shih 		    cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt,
2872e3ec7017SPing-Ke Shih 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[0]));
2873e3ec7017SPing-Ke Shih 	if (cfo->cfo_trig_by_timer_en)
2874e3ec7017SPing-Ke Shih 		return;
2875e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2876e3ec7017SPing-Ke Shih }
2877e3ec7017SPing-Ke Shih 
rtw89_phy_cfo_parse(struct rtw89_dev * rtwdev,s16 cfo_val,struct rtw89_rx_phy_ppdu * phy_ppdu)2878e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val,
2879e3ec7017SPing-Ke Shih 			 struct rtw89_rx_phy_ppdu *phy_ppdu)
2880e3ec7017SPing-Ke Shih {
2881e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2882e3ec7017SPing-Ke Shih 	u8 macid = phy_ppdu->mac_id;
2883e3ec7017SPing-Ke Shih 
288497df8587SPing-Ke Shih 	if (macid >= CFO_TRACK_MAX_USER) {
288597df8587SPing-Ke Shih 		rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid);
288697df8587SPing-Ke Shih 		return;
288797df8587SPing-Ke Shih 	}
288897df8587SPing-Ke Shih 
2889e3ec7017SPing-Ke Shih 	cfo->cfo_tail[macid] += cfo_val;
2890e3ec7017SPing-Ke Shih 	cfo->cfo_cnt[macid]++;
2891e3ec7017SPing-Ke Shih 	cfo->packet_count++;
2892e3ec7017SPing-Ke Shih }
2893e3ec7017SPing-Ke Shih 
rtw89_phy_ul_tb_assoc(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)289429136c95SEric Huang void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
289529136c95SEric Huang {
289629136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2897ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
2898ad3dc722SZong-Zhe Yang 						       rtwvif->sub_entity_idx);
289929136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
290029136c95SEric Huang 
290129136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
290229136c95SEric Huang 		return;
290329136c95SEric Huang 
290429136c95SEric Huang 	rtwvif->def_tri_idx =
290529136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG);
290629136c95SEric Huang 
290729136c95SEric Huang 	if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV)
290829136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
290929136c95SEric Huang 	else if (chan->band_type >= RTW89_BAND_5G &&
291029136c95SEric Huang 		 chan->band_width >= RTW89_CHANNEL_WIDTH_40)
291129136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = true;
291229136c95SEric Huang 	else
291329136c95SEric Huang 		rtwvif->dyn_tb_bedge_en = false;
291429136c95SEric Huang 
291529136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
291629136c95SEric Huang 		    "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n",
291729136c95SEric Huang 		    ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx);
291829136c95SEric Huang 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
291929136c95SEric Huang 		    "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n",
292029136c95SEric Huang 		    rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en);
292129136c95SEric Huang }
292229136c95SEric Huang 
292329136c95SEric Huang struct rtw89_phy_ul_tb_check_data {
292429136c95SEric Huang 	bool valid;
292529136c95SEric Huang 	bool high_tf_client;
292629136c95SEric Huang 	bool low_tf_client;
292729136c95SEric Huang 	bool dyn_tb_bedge_en;
292829136c95SEric Huang 	u8 def_tri_idx;
292929136c95SEric Huang };
293029136c95SEric Huang 
293129136c95SEric Huang static
rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_phy_ul_tb_check_data * ul_tb_data)293229136c95SEric Huang void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev,
293329136c95SEric Huang 				struct rtw89_vif *rtwvif,
293429136c95SEric Huang 				struct rtw89_phy_ul_tb_check_data *ul_tb_data)
293529136c95SEric Huang {
293629136c95SEric Huang 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
293729136c95SEric Huang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
293829136c95SEric Huang 
293929136c95SEric Huang 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION)
294029136c95SEric Huang 		return;
294129136c95SEric Huang 
294229136c95SEric Huang 	if (!vif->cfg.assoc)
294329136c95SEric Huang 		return;
294429136c95SEric Huang 
294529136c95SEric Huang 	if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH)
294629136c95SEric Huang 		ul_tb_data->high_tf_client = true;
294729136c95SEric Huang 	else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH)
294829136c95SEric Huang 		ul_tb_data->low_tf_client = true;
294929136c95SEric Huang 
295029136c95SEric Huang 	ul_tb_data->valid = true;
295129136c95SEric Huang 	ul_tb_data->def_tri_idx = rtwvif->def_tri_idx;
295229136c95SEric Huang 	ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en;
295329136c95SEric Huang }
295429136c95SEric Huang 
rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev * rtwdev)295529136c95SEric Huang void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev)
295629136c95SEric Huang {
295729136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
295829136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
295929136c95SEric Huang 	struct rtw89_phy_ul_tb_check_data ul_tb_data = {};
296029136c95SEric Huang 	struct rtw89_vif *rtwvif;
296129136c95SEric Huang 
296229136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
296329136c95SEric Huang 		return;
296429136c95SEric Huang 
296529136c95SEric Huang 	if (rtwdev->total_sta_assoc != 1)
296629136c95SEric Huang 		return;
296729136c95SEric Huang 
296829136c95SEric Huang 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
296929136c95SEric Huang 		rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data);
297029136c95SEric Huang 
297129136c95SEric Huang 	if (!ul_tb_data.valid)
297229136c95SEric Huang 		return;
297329136c95SEric Huang 
297429136c95SEric Huang 	if (ul_tb_data.dyn_tb_bedge_en) {
297529136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
297629136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0);
297729136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
297829136c95SEric Huang 				    "[ULTB] Turn off if_bandedge\n");
297929136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
298029136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN,
298129136c95SEric Huang 					       ul_tb_info->def_if_bandedge);
298229136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
298329136c95SEric Huang 				    "[ULTB] Set to default if_bandedge = %d\n",
298429136c95SEric Huang 				    ul_tb_info->def_if_bandedge);
298529136c95SEric Huang 		}
298629136c95SEric Huang 	}
298729136c95SEric Huang 
298829136c95SEric Huang 	if (ul_tb_info->dyn_tb_tri_en) {
298929136c95SEric Huang 		if (ul_tb_data.high_tf_client) {
299029136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
299129136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG, 0);
299229136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
299329136c95SEric Huang 				    "[ULTB] Turn off Tx triangle\n");
299429136c95SEric Huang 		} else if (ul_tb_data.low_tf_client) {
299529136c95SEric Huang 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
299629136c95SEric Huang 					       B_TXSHAPE_TRIANGULAR_CFG,
299729136c95SEric Huang 					       ul_tb_data.def_tri_idx);
299829136c95SEric Huang 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
299929136c95SEric Huang 				    "[ULTB] Set to default tx_shap_idx = %d\n",
300029136c95SEric Huang 				    ul_tb_data.def_tri_idx);
300129136c95SEric Huang 		}
300229136c95SEric Huang 	}
300329136c95SEric Huang }
300429136c95SEric Huang 
rtw89_phy_ul_tb_info_init(struct rtw89_dev * rtwdev)300529136c95SEric Huang static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev)
300629136c95SEric Huang {
300729136c95SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
300829136c95SEric Huang 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
300929136c95SEric Huang 
301029136c95SEric Huang 	if (!chip->support_ul_tb_ctrl)
301129136c95SEric Huang 		return;
301229136c95SEric Huang 
301329136c95SEric Huang 	ul_tb_info->dyn_tb_tri_en = true;
301429136c95SEric Huang 	ul_tb_info->def_if_bandedge =
301529136c95SEric Huang 		rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN);
301629136c95SEric Huang }
301729136c95SEric Huang 
3018e3715859SEric Huang static
rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats * antdiv_sts)3019e3715859SEric Huang void rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats *antdiv_sts)
3020e3715859SEric Huang {
3021e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->cck_rssi_avg);
3022e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->ofdm_rssi_avg);
3023e3715859SEric Huang 	ewma_rssi_init(&antdiv_sts->non_legacy_rssi_avg);
3024e3715859SEric Huang 	antdiv_sts->pkt_cnt_cck = 0;
3025e3715859SEric Huang 	antdiv_sts->pkt_cnt_ofdm = 0;
3026e3715859SEric Huang 	antdiv_sts->pkt_cnt_non_legacy = 0;
30275feecb40SEric Huang 	antdiv_sts->evm = 0;
3028e3715859SEric Huang }
3029e3715859SEric Huang 
rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu,struct rtw89_antdiv_stats * stats)3030e3715859SEric Huang static void rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev *rtwdev,
3031e3715859SEric Huang 					      struct rtw89_rx_phy_ppdu *phy_ppdu,
3032e3715859SEric Huang 					      struct rtw89_antdiv_stats *stats)
3033e3715859SEric Huang {
3034023d2f14SPing-Ke Shih 	if (rtw89_get_data_rate_mode(rtwdev, phy_ppdu->rate) == DATA_RATE_MODE_NON_HT) {
3035e3715859SEric Huang 		if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) {
3036e3715859SEric Huang 			ewma_rssi_add(&stats->cck_rssi_avg, phy_ppdu->rssi_avg);
3037e3715859SEric Huang 			stats->pkt_cnt_cck++;
3038e3715859SEric Huang 		} else {
3039e3715859SEric Huang 			ewma_rssi_add(&stats->ofdm_rssi_avg, phy_ppdu->rssi_avg);
3040e3715859SEric Huang 			stats->pkt_cnt_ofdm++;
30415feecb40SEric Huang 			stats->evm += phy_ppdu->ofdm.evm_min;
3042e3715859SEric Huang 		}
3043e3715859SEric Huang 	} else {
3044e3715859SEric Huang 		ewma_rssi_add(&stats->non_legacy_rssi_avg, phy_ppdu->rssi_avg);
3045e3715859SEric Huang 		stats->pkt_cnt_non_legacy++;
30465feecb40SEric Huang 		stats->evm += phy_ppdu->ofdm.evm_min;
3047e3715859SEric Huang 	}
3048e3715859SEric Huang }
3049e3715859SEric Huang 
rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats * stats)3050e3715859SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats *stats)
3051e3715859SEric Huang {
3052e3715859SEric Huang 	if (stats->pkt_cnt_non_legacy >= stats->pkt_cnt_cck &&
3053e3715859SEric Huang 	    stats->pkt_cnt_non_legacy >= stats->pkt_cnt_ofdm)
3054e3715859SEric Huang 		return ewma_rssi_read(&stats->non_legacy_rssi_avg);
3055e3715859SEric Huang 	else if (stats->pkt_cnt_ofdm >= stats->pkt_cnt_cck &&
3056e3715859SEric Huang 		 stats->pkt_cnt_ofdm >= stats->pkt_cnt_non_legacy)
3057e3715859SEric Huang 		return ewma_rssi_read(&stats->ofdm_rssi_avg);
3058e3715859SEric Huang 	else
3059e3715859SEric Huang 		return ewma_rssi_read(&stats->cck_rssi_avg);
3060e3715859SEric Huang }
3061e3715859SEric Huang 
rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats * stats)30625feecb40SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats *stats)
30635feecb40SEric Huang {
30645feecb40SEric Huang 	return phy_div(stats->evm, stats->pkt_cnt_non_legacy + stats->pkt_cnt_ofdm);
30655feecb40SEric Huang }
30665feecb40SEric Huang 
rtw89_phy_antdiv_parse(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu)3067e3715859SEric Huang void rtw89_phy_antdiv_parse(struct rtw89_dev *rtwdev,
3068e3715859SEric Huang 			    struct rtw89_rx_phy_ppdu *phy_ppdu)
3069e3715859SEric Huang {
3070e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3071e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3072e3715859SEric Huang 
3073e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
3074e3715859SEric Huang 		return;
3075e3715859SEric Huang 
3076e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->target_stats);
3077e3715859SEric Huang 
3078e3715859SEric Huang 	if (!antdiv->get_stats)
3079e3715859SEric Huang 		return;
3080e3715859SEric Huang 
3081e3715859SEric Huang 	if (hal->antenna_rx == RF_A)
3082e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->main_stats);
3083e3715859SEric Huang 	else if (hal->antenna_rx == RF_B)
3084e3715859SEric Huang 		rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->aux_stats);
3085e3715859SEric Huang }
3086e3715859SEric Huang 
rtw89_phy_antdiv_reg_init(struct rtw89_dev * rtwdev)3087a90c613dSEric Huang static void rtw89_phy_antdiv_reg_init(struct rtw89_dev *rtwdev)
3088a90c613dSEric Huang {
3089a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_ANT_TRAIN_EN,
3090a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3091a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_TX_ANT_SEL,
3092a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3093a90c613dSEric Huang 
3094a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_TRSW_TX_EXTEND,
3095a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3096a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_HW_ANTSW_DIS_BY_GNT_BT,
3097a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3098a90c613dSEric Huang 
3099a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_BT_FORCE_ANTIDX_EN,
3100a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3101a90c613dSEric Huang 
3102a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_RFSW_CTRL_ANT0_BASE, B_RFSW_CTRL_ANT_MAPPING,
3103a90c613dSEric Huang 			      0x0100, RTW89_PHY_0);
3104a90c613dSEric Huang 
3105a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_BTG_TRX,
3106a90c613dSEric Huang 			      0x1, RTW89_PHY_0);
3107a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_HW_CTRL,
3108a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3109a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_2G,
3110a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3111a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_5G,
3112a90c613dSEric Huang 			      0x0, RTW89_PHY_0);
3113a90c613dSEric Huang }
3114a90c613dSEric Huang 
rtw89_phy_antdiv_sts_reset(struct rtw89_dev * rtwdev)3115e3715859SEric Huang static void rtw89_phy_antdiv_sts_reset(struct rtw89_dev *rtwdev)
3116e3715859SEric Huang {
3117e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3118e3715859SEric Huang 
3119e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
3120e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->main_stats);
3121e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->aux_stats);
3122e3715859SEric Huang }
3123e3715859SEric Huang 
rtw89_phy_antdiv_init(struct rtw89_dev * rtwdev)3124a90c613dSEric Huang static void rtw89_phy_antdiv_init(struct rtw89_dev *rtwdev)
3125a90c613dSEric Huang {
3126e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
3127a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
3128a90c613dSEric Huang 
3129a90c613dSEric Huang 	if (!hal->ant_diversity)
3130a90c613dSEric Huang 		return;
3131a90c613dSEric Huang 
3132e3715859SEric Huang 	antdiv->get_stats = false;
3133e3715859SEric Huang 	antdiv->rssi_pre = 0;
3134e3715859SEric Huang 	rtw89_phy_antdiv_sts_reset(rtwdev);
3135a90c613dSEric Huang 	rtw89_phy_antdiv_reg_init(rtwdev);
3136a90c613dSEric Huang }
3137a90c613dSEric Huang 
rtw89_phy_stat_thermal_update(struct rtw89_dev * rtwdev)3138e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev)
3139e3ec7017SPing-Ke Shih {
3140e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3141e3ec7017SPing-Ke Shih 	int i;
3142e3ec7017SPing-Ke Shih 	u8 th;
3143e3ec7017SPing-Ke Shih 
3144e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++) {
3145e3ec7017SPing-Ke Shih 		th = rtw89_chip_get_thermal(rtwdev, i);
3146e3ec7017SPing-Ke Shih 		if (th)
3147e3ec7017SPing-Ke Shih 			ewma_thermal_add(&phystat->avg_thermal[i], th);
3148e3ec7017SPing-Ke Shih 
3149e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3150e3ec7017SPing-Ke Shih 			    "path(%d) thermal cur=%u avg=%ld", i, th,
3151e3ec7017SPing-Ke Shih 			    ewma_thermal_read(&phystat->avg_thermal[i]));
3152e3ec7017SPing-Ke Shih 	}
3153e3ec7017SPing-Ke Shih }
3154e3ec7017SPing-Ke Shih 
3155e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data {
3156e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
3157e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info;
3158e3ec7017SPing-Ke Shih 	bool rssi_changed;
3159e3ec7017SPing-Ke Shih };
3160e3ec7017SPing-Ke Shih 
rtw89_phy_stat_rssi_update_iter(void * data,struct ieee80211_sta * sta)3161e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data,
3162e3ec7017SPing-Ke Shih 					    struct ieee80211_sta *sta)
3163e3ec7017SPing-Ke Shih {
3164e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3165e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data *rssi_data =
3166e3ec7017SPing-Ke Shih 					(struct rtw89_phy_iter_rssi_data *)data;
3167e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info;
3168e3ec7017SPing-Ke Shih 	unsigned long rssi_curr;
3169e3ec7017SPing-Ke Shih 
3170e3ec7017SPing-Ke Shih 	rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi);
3171e3ec7017SPing-Ke Shih 
3172e3ec7017SPing-Ke Shih 	if (rssi_curr < ch_info->rssi_min) {
3173e3ec7017SPing-Ke Shih 		ch_info->rssi_min = rssi_curr;
3174e3ec7017SPing-Ke Shih 		ch_info->rssi_min_macid = rtwsta->mac_id;
3175e3ec7017SPing-Ke Shih 	}
3176e3ec7017SPing-Ke Shih 
3177e3ec7017SPing-Ke Shih 	if (rtwsta->prev_rssi == 0) {
3178e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3179e3ec7017SPing-Ke Shih 	} else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) {
3180e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
3181e3ec7017SPing-Ke Shih 		rssi_data->rssi_changed = true;
3182e3ec7017SPing-Ke Shih 	}
3183e3ec7017SPing-Ke Shih }
3184e3ec7017SPing-Ke Shih 
rtw89_phy_stat_rssi_update(struct rtw89_dev * rtwdev)3185e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev)
3186e3ec7017SPing-Ke Shih {
3187e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data rssi_data = {0};
3188e3ec7017SPing-Ke Shih 
3189e3ec7017SPing-Ke Shih 	rssi_data.rtwdev = rtwdev;
3190e3ec7017SPing-Ke Shih 	rssi_data.ch_info = &rtwdev->ch_info;
3191e3ec7017SPing-Ke Shih 	rssi_data.ch_info->rssi_min = U8_MAX;
3192e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
3193e3ec7017SPing-Ke Shih 					  rtw89_phy_stat_rssi_update_iter,
3194e3ec7017SPing-Ke Shih 					  &rssi_data);
3195e3ec7017SPing-Ke Shih 	if (rssi_data.rssi_changed)
3196e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_wl_sta(rtwdev);
3197e3ec7017SPing-Ke Shih }
3198e3ec7017SPing-Ke Shih 
rtw89_phy_stat_init(struct rtw89_dev * rtwdev)3199e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev)
3200e3ec7017SPing-Ke Shih {
3201e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3202e3ec7017SPing-Ke Shih 	int i;
3203e3ec7017SPing-Ke Shih 
3204e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
3205e3ec7017SPing-Ke Shih 		ewma_thermal_init(&phystat->avg_thermal[i]);
3206e3ec7017SPing-Ke Shih 
3207e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3208e3ec7017SPing-Ke Shih 
3209e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3210e3ec7017SPing-Ke Shih 	memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat));
3211e3ec7017SPing-Ke Shih }
3212e3ec7017SPing-Ke Shih 
rtw89_phy_stat_track(struct rtw89_dev * rtwdev)3213e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev)
3214e3ec7017SPing-Ke Shih {
3215e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3216e3ec7017SPing-Ke Shih 
3217e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
3218e3ec7017SPing-Ke Shih 	rtw89_phy_stat_rssi_update(rtwdev);
3219e3ec7017SPing-Ke Shih 
3220e3ec7017SPing-Ke Shih 	phystat->last_pkt_stat = phystat->cur_pkt_stat;
3221e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3222e3ec7017SPing-Ke Shih }
3223e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_us_to_idx(struct rtw89_dev * rtwdev,u32 time_us)3224e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us)
3225e3ec7017SPing-Ke Shih {
3226e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3227e3ec7017SPing-Ke Shih 
3228e3ec7017SPing-Ke Shih 	return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3229e3ec7017SPing-Ke Shih }
3230e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_idx_to_us(struct rtw89_dev * rtwdev,u16 idx)3231e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx)
3232e3ec7017SPing-Ke Shih {
3233e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3234e3ec7017SPing-Ke Shih 
3235e3ec7017SPing-Ke Shih 	return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3236e3ec7017SPing-Ke Shih }
3237e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_top_setting_init(struct rtw89_dev * rtwdev)3238e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev)
3239e3ec7017SPing-Ke Shih {
3240058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3241e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3242058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3243e3ec7017SPing-Ke Shih 
3244e3ec7017SPing-Ke Shih 	env->ccx_manual_ctrl = false;
3245e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3246e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3247e3ec7017SPing-Ke Shih 	env->ccx_period = 0;
3248e3ec7017SPing-Ke Shih 	env->ccx_unit_idx = RTW89_CCX_32_US;
3249e3ec7017SPing-Ke Shih 
3250058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->en_mask, 1);
3251058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->trig_opt_mask, 1);
3252058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 1);
3253058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->edcca_opt_mask,
3254e3ec7017SPing-Ke Shih 			       RTW89_CCX_EDCCA_BW20_0);
3255e3ec7017SPing-Ke Shih }
3256e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_get_report(struct rtw89_dev * rtwdev,u16 report,u16 score)3257e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report,
3258e3ec7017SPing-Ke Shih 				    u16 score)
3259e3ec7017SPing-Ke Shih {
3260e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3261e3ec7017SPing-Ke Shih 	u32 numer = 0;
3262e3ec7017SPing-Ke Shih 	u16 ret = 0;
3263e3ec7017SPing-Ke Shih 
3264e3ec7017SPing-Ke Shih 	numer = report * score + (env->ccx_period >> 1);
3265e3ec7017SPing-Ke Shih 	if (env->ccx_period)
3266e3ec7017SPing-Ke Shih 		ret = numer / env->ccx_period;
3267e3ec7017SPing-Ke Shih 
3268e3ec7017SPing-Ke Shih 	return ret >= score ? score - 1 : ret;
3269e3ec7017SPing-Ke Shih }
3270e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev * rtwdev,u16 time_ms,u32 * period,u32 * unit_idx)3271e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev,
3272e3ec7017SPing-Ke Shih 					    u16 time_ms, u32 *period,
3273e3ec7017SPing-Ke Shih 					    u32 *unit_idx)
3274e3ec7017SPing-Ke Shih {
3275e3ec7017SPing-Ke Shih 	u32 idx;
3276e3ec7017SPing-Ke Shih 	u8 quotient;
3277e3ec7017SPing-Ke Shih 
3278e3ec7017SPing-Ke Shih 	if (time_ms >= CCX_MAX_PERIOD)
3279e3ec7017SPing-Ke Shih 		time_ms = CCX_MAX_PERIOD;
3280e3ec7017SPing-Ke Shih 
3281e3ec7017SPing-Ke Shih 	quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD;
3282e3ec7017SPing-Ke Shih 
3283e3ec7017SPing-Ke Shih 	if (quotient < 4)
3284e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_4_US;
3285e3ec7017SPing-Ke Shih 	else if (quotient < 8)
3286e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_8_US;
3287e3ec7017SPing-Ke Shih 	else if (quotient < 16)
3288e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_16_US;
3289e3ec7017SPing-Ke Shih 	else
3290e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_32_US;
3291e3ec7017SPing-Ke Shih 
3292e3ec7017SPing-Ke Shih 	*unit_idx = idx;
3293e3ec7017SPing-Ke Shih 	*period = (time_ms * MS_TO_4US_RATIO) >> idx;
3294e3ec7017SPing-Ke Shih 
3295e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3296e3ec7017SPing-Ke Shih 		    "[Trigger Time] period:%d, unit_idx:%d\n",
3297e3ec7017SPing-Ke Shih 		    *period, *unit_idx);
3298e3ec7017SPing-Ke Shih }
3299e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_racing_release(struct rtw89_dev * rtwdev)3300e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev)
3301e3ec7017SPing-Ke Shih {
3302e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3303e3ec7017SPing-Ke Shih 
3304e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3305e3ec7017SPing-Ke Shih 		    "lv:(%d)->(0)\n", env->ccx_rac_lv);
3306e3ec7017SPing-Ke Shih 
3307e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
3308e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3309e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3310e3ec7017SPing-Ke Shih }
3311e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev * rtwdev,struct rtw89_ccx_para_info * para)3312e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev,
3313e3ec7017SPing-Ke Shih 					      struct rtw89_ccx_para_info *para)
3314e3ec7017SPing-Ke Shih {
3315e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3316e3ec7017SPing-Ke Shih 	bool is_update = env->ifs_clm_app != para->ifs_clm_app;
3317e3ec7017SPing-Ke Shih 	u8 i = 0;
3318e3ec7017SPing-Ke Shih 	u16 *ifs_th_l = env->ifs_clm_th_l;
3319e3ec7017SPing-Ke Shih 	u16 *ifs_th_h = env->ifs_clm_th_h;
3320e3ec7017SPing-Ke Shih 	u32 ifs_th0_us = 0, ifs_th_times = 0;
3321e3ec7017SPing-Ke Shih 	u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0};
3322e3ec7017SPing-Ke Shih 
3323e3ec7017SPing-Ke Shih 	if (!is_update)
3324e3ec7017SPing-Ke Shih 		goto ifs_update_finished;
3325e3ec7017SPing-Ke Shih 
3326e3ec7017SPing-Ke Shih 	switch (para->ifs_clm_app) {
3327e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_INIT:
3328e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_BACKGROUND:
3329e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_ACS:
3330e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG:
3331e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DIG:
3332e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_TDMA_DIG:
3333e3ec7017SPing-Ke Shih 		ifs_th0_us = IFS_CLM_TH0_UPPER;
3334e3ec7017SPing-Ke Shih 		ifs_th_times = IFS_CLM_TH_MUL;
3335e3ec7017SPing-Ke Shih 		break;
3336e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG_MANUAL:
3337e3ec7017SPing-Ke Shih 		ifs_th0_us = para->ifs_clm_manual_th0;
3338e3ec7017SPing-Ke Shih 		ifs_th_times = para->ifs_clm_manual_th_times;
3339e3ec7017SPing-Ke Shih 		break;
3340e3ec7017SPing-Ke Shih 	default:
3341e3ec7017SPing-Ke Shih 		break;
3342e3ec7017SPing-Ke Shih 	}
3343e3ec7017SPing-Ke Shih 
3344e3ec7017SPing-Ke Shih 	/* Set sampling threshold for 4 different regions, unit in idx_cnt.
3345e3ec7017SPing-Ke Shih 	 * low[i] = high[i-1] + 1
3346e3ec7017SPing-Ke Shih 	 * high[i] = high[i-1] * ifs_th_times
3347e3ec7017SPing-Ke Shih 	 */
3348e3ec7017SPing-Ke Shih 	ifs_th_l[IFS_CLM_TH_START_IDX] = 0;
3349e3ec7017SPing-Ke Shih 	ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us;
3350e3ec7017SPing-Ke Shih 	ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev,
3351e3ec7017SPing-Ke Shih 								 ifs_th0_us);
3352e3ec7017SPing-Ke Shih 	for (i = 1; i < RTW89_IFS_CLM_NUM; i++) {
3353e3ec7017SPing-Ke Shih 		ifs_th_l[i] = ifs_th_h[i - 1] + 1;
3354e3ec7017SPing-Ke Shih 		ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times;
3355e3ec7017SPing-Ke Shih 		ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]);
3356e3ec7017SPing-Ke Shih 	}
3357e3ec7017SPing-Ke Shih 
3358e3ec7017SPing-Ke Shih ifs_update_finished:
3359e3ec7017SPing-Ke Shih 	if (!is_update)
3360e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3361e3ec7017SPing-Ke Shih 			    "No need to update IFS_TH\n");
3362e3ec7017SPing-Ke Shih 
3363e3ec7017SPing-Ke Shih 	return is_update;
3364e3ec7017SPing-Ke Shih }
3365e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev * rtwdev)3366e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev)
3367e3ec7017SPing-Ke Shih {
3368058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3369e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3370058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3371e3ec7017SPing-Ke Shih 	u8 i = 0;
3372e3ec7017SPing-Ke Shih 
3373058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_th_l_mask,
3374e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[0]);
3375058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_th_l_mask,
3376e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[1]);
3377058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_th_l_mask,
3378e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[2]);
3379058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_th_l_mask,
3380e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[3]);
3381e3ec7017SPing-Ke Shih 
3382058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_th_h_mask,
3383e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[0]);
3384058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_th_h_mask,
3385e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[1]);
3386058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_th_h_mask,
3387e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[2]);
3388058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_th_h_mask,
3389e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[3]);
3390e3ec7017SPing-Ke Shih 
3391e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3392e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3393e3ec7017SPing-Ke Shih 			    "Update IFS_T%d_th{low, high} : {%d, %d}\n",
3394e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]);
3395e3ec7017SPing-Ke Shih }
3396e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_setting_init(struct rtw89_dev * rtwdev)3397e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev)
3398e3ec7017SPing-Ke Shih {
3399058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3400e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3401058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3402e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3403e3ec7017SPing-Ke Shih 
3404e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3405e3ec7017SPing-Ke Shih 	env->ifs_clm_mntr_time = 0;
3406e3ec7017SPing-Ke Shih 
3407e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_INIT;
3408e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, &para))
3409e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3410e3ec7017SPing-Ke Shih 
3411058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_collect_en_mask, true);
3412058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_en_mask, true);
3413058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_en_mask, true);
3414058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_en_mask, true);
3415058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_en_mask, true);
3416e3ec7017SPing-Ke Shih }
3417e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_racing_ctrl(struct rtw89_dev * rtwdev,enum rtw89_env_racing_lv level)3418e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev,
3419e3ec7017SPing-Ke Shih 				     enum rtw89_env_racing_lv level)
3420e3ec7017SPing-Ke Shih {
3421e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3422e3ec7017SPing-Ke Shih 	int ret = 0;
3423e3ec7017SPing-Ke Shih 
3424e3ec7017SPing-Ke Shih 	if (level >= RTW89_RAC_MAX_NUM) {
3425e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3426e3ec7017SPing-Ke Shih 			    "[WARNING] Wrong LV=%d\n", level);
3427e3ec7017SPing-Ke Shih 		return -EINVAL;
3428e3ec7017SPing-Ke Shih 	}
3429e3ec7017SPing-Ke Shih 
3430e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3431e3ec7017SPing-Ke Shih 		    "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing,
3432e3ec7017SPing-Ke Shih 		    env->ccx_rac_lv, level);
3433e3ec7017SPing-Ke Shih 
3434e3ec7017SPing-Ke Shih 	if (env->ccx_ongoing) {
3435e3ec7017SPing-Ke Shih 		if (level <= env->ccx_rac_lv)
3436e3ec7017SPing-Ke Shih 			ret = -EINVAL;
3437e3ec7017SPing-Ke Shih 		else
3438e3ec7017SPing-Ke Shih 			env->ccx_ongoing = false;
3439e3ec7017SPing-Ke Shih 	}
3440e3ec7017SPing-Ke Shih 
3441e3ec7017SPing-Ke Shih 	if (ret == 0)
3442e3ec7017SPing-Ke Shih 		env->ccx_rac_lv = level;
3443e3ec7017SPing-Ke Shih 
3444e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n",
3445e3ec7017SPing-Ke Shih 		    !ret);
3446e3ec7017SPing-Ke Shih 
3447e3ec7017SPing-Ke Shih 	return ret;
3448e3ec7017SPing-Ke Shih }
3449e3ec7017SPing-Ke Shih 
rtw89_phy_ccx_trigger(struct rtw89_dev * rtwdev)3450e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev)
3451e3ec7017SPing-Ke Shih {
3452058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3453e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3454058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3455e3ec7017SPing-Ke Shih 
3456058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_clm_cnt_clear_mask, 0);
3457058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 0);
3458058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_clm_cnt_clear_mask, 1);
3459058b2074SCheng-Chieh Hsieh 	rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 1);
3460e3ec7017SPing-Ke Shih 
3461e3ec7017SPing-Ke Shih 	env->ccx_ongoing = true;
3462e3ec7017SPing-Ke Shih }
3463e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_get_utility(struct rtw89_dev * rtwdev)3464e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev)
3465e3ec7017SPing-Ke Shih {
3466e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3467e3ec7017SPing-Ke Shih 	u8 i = 0;
3468e3ec7017SPing-Ke Shih 	u32 res = 0;
3469e3ec7017SPing-Ke Shih 
3470e3ec7017SPing-Ke Shih 	env->ifs_clm_tx_ratio =
3471e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT);
3472e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca_ratio =
3473e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca,
3474e3ec7017SPing-Ke Shih 					 PERCENT);
3475e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_ratio =
3476e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT);
3477e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_ratio =
3478e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT);
3479e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_cca_excl_fa_ratio =
3480e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa,
3481e3ec7017SPing-Ke Shih 					 PERCENT);
3482e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_cca_excl_fa_ratio =
3483e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa,
3484e3ec7017SPing-Ke Shih 					 PERCENT);
3485e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_permil =
3486e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL);
3487e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_permil =
3488e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL);
3489e3ec7017SPing-Ke Shih 
3490e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++) {
3491e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) {
3492e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD;
3493e3ec7017SPing-Ke Shih 		} else {
3494e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] =
3495e3ec7017SPing-Ke Shih 				rtw89_phy_ccx_idx_to_us(rtwdev,
3496e3ec7017SPing-Ke Shih 							env->ifs_clm_avg[i]);
3497e3ec7017SPing-Ke Shih 		}
3498e3ec7017SPing-Ke Shih 
3499e3ec7017SPing-Ke Shih 		res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]);
3500e3ec7017SPing-Ke Shih 		res += env->ifs_clm_his[i] >> 1;
3501e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i])
3502e3ec7017SPing-Ke Shih 			res /= env->ifs_clm_his[i];
3503e3ec7017SPing-Ke Shih 		else
3504e3ec7017SPing-Ke Shih 			res = 0;
3505e3ec7017SPing-Ke Shih 		env->ifs_clm_cca_avg[i] = res;
3506e3ec7017SPing-Ke Shih 	}
3507e3ec7017SPing-Ke Shih 
3508e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3509e3ec7017SPing-Ke Shih 		    "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3510e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio);
3511e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3512e3ec7017SPing-Ke Shih 		    "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n",
3513e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio);
3514e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3515e3ec7017SPing-Ke Shih 		    "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n",
3516e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil);
3517e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3518e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n",
3519e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_cca_excl_fa_ratio,
3520e3ec7017SPing-Ke Shih 		    env->ifs_clm_ofdm_cca_excl_fa_ratio);
3521e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3522e3ec7017SPing-Ke Shih 		    "Time:[his, ifs_avg(us), cca_avg(us)]\n");
3523e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3524e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n",
3525e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i],
3526e3ec7017SPing-Ke Shih 			    env->ifs_clm_cca_avg[i]);
3527e3ec7017SPing-Ke Shih }
3528e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_get_result(struct rtw89_dev * rtwdev)3529e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev)
3530e3ec7017SPing-Ke Shih {
3531058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3532e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3533058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3534e3ec7017SPing-Ke Shih 	u8 i = 0;
3535e3ec7017SPing-Ke Shih 
3536058b2074SCheng-Chieh Hsieh 	if (rtw89_phy_read32_mask(rtwdev, ccx->ifs_total_addr,
3537058b2074SCheng-Chieh Hsieh 				  ccx->ifs_cnt_done_mask) == 0) {
3538e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3539e3ec7017SPing-Ke Shih 			    "Get IFS_CLM report Fail\n");
3540e3ec7017SPing-Ke Shih 		return false;
3541e3ec7017SPing-Ke Shih 	}
3542e3ec7017SPing-Ke Shih 
3543e3ec7017SPing-Ke Shih 	env->ifs_clm_tx =
3544058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_tx_cnt_addr,
3545058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_tx_cnt_msk);
3546e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca =
3547058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_tx_cnt_addr,
3548058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_edcca_excl_cca_fa_mask);
3549e3ec7017SPing-Ke Shih 	env->ifs_clm_cckcca_excl_fa =
3550058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_cca_addr,
3551058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_cckcca_excl_fa_mask);
3552e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmcca_excl_fa =
3553058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_cca_addr,
3554058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_ofdmcca_excl_fa_mask);
3555e3ec7017SPing-Ke Shih 	env->ifs_clm_cckfa =
3556058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_fa_addr,
3557058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_cck_fa_mask);
3558e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmfa =
3559058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_fa_addr,
3560058b2074SCheng-Chieh Hsieh 				      ccx->ifs_clm_ofdm_fa_mask);
3561e3ec7017SPing-Ke Shih 
3562e3ec7017SPing-Ke Shih 	env->ifs_clm_his[0] =
3563058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr,
3564058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t1_his_mask);
3565e3ec7017SPing-Ke Shih 	env->ifs_clm_his[1] =
3566058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr,
3567058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t2_his_mask);
3568e3ec7017SPing-Ke Shih 	env->ifs_clm_his[2] =
3569058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr,
3570058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t3_his_mask);
3571e3ec7017SPing-Ke Shih 	env->ifs_clm_his[3] =
3572058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr,
3573058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t4_his_mask);
3574e3ec7017SPing-Ke Shih 
3575e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[0] =
3576058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_l_addr,
3577058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t1_avg_mask);
3578e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[1] =
3579058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_l_addr,
3580058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t2_avg_mask);
3581e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[2] =
3582058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_h_addr,
3583058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t3_avg_mask);
3584e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[3] =
3585058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_h_addr,
3586058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t4_avg_mask);
3587e3ec7017SPing-Ke Shih 
3588e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[0] =
3589058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_l_addr,
3590058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t1_cca_mask);
3591e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[1] =
3592058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_l_addr,
3593058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t2_cca_mask);
3594e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[2] =
3595058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_h_addr,
3596058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t3_cca_mask);
3597e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[3] =
3598058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_h_addr,
3599058b2074SCheng-Chieh Hsieh 				      ccx->ifs_t4_cca_mask);
3600e3ec7017SPing-Ke Shih 
3601e3ec7017SPing-Ke Shih 	env->ifs_clm_total_ifs =
3602058b2074SCheng-Chieh Hsieh 		rtw89_phy_read32_mask(rtwdev, ccx->ifs_total_addr,
3603058b2074SCheng-Chieh Hsieh 				      ccx->ifs_total_mask);
3604e3ec7017SPing-Ke Shih 
3605e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n",
3606e3ec7017SPing-Ke Shih 		    env->ifs_clm_total_ifs);
3607e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3608e3ec7017SPing-Ke Shih 		    "{Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3609e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca);
3610e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3611e3ec7017SPing-Ke Shih 		    "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n",
3612e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckfa, env->ifs_clm_ofdmfa);
3613e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3614e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n",
3615e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa);
3616e3ec7017SPing-Ke Shih 
3617e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n");
3618e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3619e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3620e3ec7017SPing-Ke Shih 			    "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i],
3621e3ec7017SPing-Ke Shih 			    env->ifs_clm_avg[i], env->ifs_clm_cca[i]);
3622e3ec7017SPing-Ke Shih 
3623e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_get_utility(rtwdev);
3624e3ec7017SPing-Ke Shih 
3625e3ec7017SPing-Ke Shih 	return true;
3626e3ec7017SPing-Ke Shih }
3627e3ec7017SPing-Ke Shih 
rtw89_phy_ifs_clm_set(struct rtw89_dev * rtwdev,struct rtw89_ccx_para_info * para)3628e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev,
3629e3ec7017SPing-Ke Shih 				 struct rtw89_ccx_para_info *para)
3630e3ec7017SPing-Ke Shih {
3631058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3632e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3633058b2074SCheng-Chieh Hsieh 	const struct rtw89_ccx_regs *ccx = phy->ccx;
3634e3ec7017SPing-Ke Shih 	u32 period = 0;
3635e3ec7017SPing-Ke Shih 	u32 unit_idx = 0;
3636e3ec7017SPing-Ke Shih 
3637e3ec7017SPing-Ke Shih 	if (para->mntr_time == 0) {
3638e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3639e3ec7017SPing-Ke Shih 			    "[WARN] MNTR_TIME is 0\n");
3640e3ec7017SPing-Ke Shih 		return -EINVAL;
3641e3ec7017SPing-Ke Shih 	}
3642e3ec7017SPing-Ke Shih 
3643e3ec7017SPing-Ke Shih 	if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv))
3644e3ec7017SPing-Ke Shih 		return -EINVAL;
3645e3ec7017SPing-Ke Shih 
3646e3ec7017SPing-Ke Shih 	if (para->mntr_time != env->ifs_clm_mntr_time) {
3647e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time,
3648e3ec7017SPing-Ke Shih 						&period, &unit_idx);
3649058b2074SCheng-Chieh Hsieh 		rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr,
3650058b2074SCheng-Chieh Hsieh 				       ccx->ifs_clm_period_mask, period);
3651058b2074SCheng-Chieh Hsieh 		rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr,
3652058b2074SCheng-Chieh Hsieh 				       ccx->ifs_clm_cnt_unit_mask,
3653058b2074SCheng-Chieh Hsieh 				       unit_idx);
3654e3ec7017SPing-Ke Shih 
3655e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3656e3ec7017SPing-Ke Shih 			    "Update IFS-CLM time ((%d)) -> ((%d))\n",
3657e3ec7017SPing-Ke Shih 			    env->ifs_clm_mntr_time, para->mntr_time);
3658e3ec7017SPing-Ke Shih 
3659e3ec7017SPing-Ke Shih 		env->ifs_clm_mntr_time = para->mntr_time;
3660e3ec7017SPing-Ke Shih 		env->ccx_period = (u16)period;
3661e3ec7017SPing-Ke Shih 		env->ccx_unit_idx = (u8)unit_idx;
3662e3ec7017SPing-Ke Shih 	}
3663e3ec7017SPing-Ke Shih 
3664e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) {
3665e3ec7017SPing-Ke Shih 		env->ifs_clm_app = para->ifs_clm_app;
3666e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3667e3ec7017SPing-Ke Shih 	}
3668e3ec7017SPing-Ke Shih 
3669e3ec7017SPing-Ke Shih 	return 0;
3670e3ec7017SPing-Ke Shih }
3671e3ec7017SPing-Ke Shih 
rtw89_phy_env_monitor_track(struct rtw89_dev * rtwdev)3672e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev)
3673e3ec7017SPing-Ke Shih {
3674e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3675e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3676e3ec7017SPing-Ke Shih 	u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3677e3ec7017SPing-Ke Shih 
3678e3ec7017SPing-Ke Shih 	env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3679e3ec7017SPing-Ke Shih 	if (env->ccx_manual_ctrl) {
3680e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3681e3ec7017SPing-Ke Shih 			    "CCX in manual ctrl\n");
3682e3ec7017SPing-Ke Shih 		return;
3683e3ec7017SPing-Ke Shih 	}
3684e3ec7017SPing-Ke Shih 
3685e3ec7017SPing-Ke Shih 	/* only ifs_clm for now */
3686e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_get_result(rtwdev))
3687e3ec7017SPing-Ke Shih 		env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3688e3ec7017SPing-Ke Shih 
3689e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_racing_release(rtwdev);
3690e3ec7017SPing-Ke Shih 	para.mntr_time = 1900;
3691e3ec7017SPing-Ke Shih 	para.rac_lv = RTW89_RAC_LV_1;
3692e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3693e3ec7017SPing-Ke Shih 
3694e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_set(rtwdev, &para) == 0)
3695e3ec7017SPing-Ke Shih 		chk_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3696e3ec7017SPing-Ke Shih 	if (chk_result)
3697e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_trigger(rtwdev);
3698e3ec7017SPing-Ke Shih 
3699e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3700e3ec7017SPing-Ke Shih 		    "get_result=0x%x, chk_result:0x%x\n",
3701e3ec7017SPing-Ke Shih 		    env->ccx_watchdog_result, chk_result);
3702e3ec7017SPing-Ke Shih }
3703e3ec7017SPing-Ke Shih 
rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap * ie_page)3704eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page)
3705eb4e52b3SPo Hao Huang {
37069e2f177dSZong-Zhe Yang 	if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM ||
3707eb4e52b3SPo Hao Huang 	    *ie_page == RTW89_RSVD_9)
3708eb4e52b3SPo Hao Huang 		return false;
3709eb4e52b3SPo Hao Huang 	else if (*ie_page > RTW89_RSVD_9)
3710eb4e52b3SPo Hao Huang 		*ie_page -= 1;
3711eb4e52b3SPo Hao Huang 
3712eb4e52b3SPo Hao Huang 	return true;
3713eb4e52b3SPo Hao Huang }
3714eb4e52b3SPo Hao Huang 
rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)3715eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)
3716eb4e52b3SPo Hao Huang {
3717eb4e52b3SPo Hao Huang 	static const u8 ie_page_shift = 2;
3718eb4e52b3SPo Hao Huang 
3719eb4e52b3SPo Hao Huang 	return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift);
3720eb4e52b3SPo Hao Huang }
3721eb4e52b3SPo Hao Huang 
rtw89_physts_get_ie_bitmap(struct rtw89_dev * rtwdev,enum rtw89_phy_status_bitmap ie_page)3722eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev,
3723eb4e52b3SPo Hao Huang 				      enum rtw89_phy_status_bitmap ie_page)
3724eb4e52b3SPo Hao Huang {
3725eb4e52b3SPo Hao Huang 	u32 addr;
3726eb4e52b3SPo Hao Huang 
3727eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3728eb4e52b3SPo Hao Huang 		return 0;
3729eb4e52b3SPo Hao Huang 
3730eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3731eb4e52b3SPo Hao Huang 
3732eb4e52b3SPo Hao Huang 	return rtw89_phy_read32(rtwdev, addr);
3733eb4e52b3SPo Hao Huang }
3734eb4e52b3SPo Hao Huang 
rtw89_physts_set_ie_bitmap(struct rtw89_dev * rtwdev,enum rtw89_phy_status_bitmap ie_page,u32 val)3735eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev,
3736eb4e52b3SPo Hao Huang 				       enum rtw89_phy_status_bitmap ie_page,
3737eb4e52b3SPo Hao Huang 				       u32 val)
3738eb4e52b3SPo Hao Huang {
3739eb4e52b3SPo Hao Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
3740eb4e52b3SPo Hao Huang 	u32 addr;
3741eb4e52b3SPo Hao Huang 
3742eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3743eb4e52b3SPo Hao Huang 		return;
3744eb4e52b3SPo Hao Huang 
3745eb4e52b3SPo Hao Huang 	if (chip->chip_id == RTL8852A)
3746eb4e52b3SPo Hao Huang 		val &= B_PHY_STS_BITMAP_MSK_52A;
3747eb4e52b3SPo Hao Huang 
3748eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3749eb4e52b3SPo Hao Huang 	rtw89_phy_write32(rtwdev, addr, val);
3750eb4e52b3SPo Hao Huang }
3751eb4e52b3SPo Hao Huang 
rtw89_physts_enable_ie_bitmap(struct rtw89_dev * rtwdev,enum rtw89_phy_status_bitmap bitmap,enum rtw89_phy_status_ie_type ie,bool enable)3752eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev,
3753eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_bitmap bitmap,
3754eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_ie_type ie,
3755eb4e52b3SPo Hao Huang 					  bool enable)
3756eb4e52b3SPo Hao Huang {
3757eb4e52b3SPo Hao Huang 	u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap);
3758eb4e52b3SPo Hao Huang 
3759eb4e52b3SPo Hao Huang 	if (enable)
3760eb4e52b3SPo Hao Huang 		val |= BIT(ie);
3761eb4e52b3SPo Hao Huang 	else
3762eb4e52b3SPo Hao Huang 		val &= ~BIT(ie);
3763eb4e52b3SPo Hao Huang 
3764eb4e52b3SPo Hao Huang 	rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val);
3765eb4e52b3SPo Hao Huang }
3766eb4e52b3SPo Hao Huang 
rtw89_physts_enable_fail_report(struct rtw89_dev * rtwdev,bool enable,enum rtw89_phy_idx phy_idx)3767eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev,
3768eb4e52b3SPo Hao Huang 					    bool enable,
3769eb4e52b3SPo Hao Huang 					    enum rtw89_phy_idx phy_idx)
3770eb4e52b3SPo Hao Huang {
3771058b2074SCheng-Chieh Hsieh 	const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def;
3772058b2074SCheng-Chieh Hsieh 	const struct rtw89_physts_regs *physts = phy->physts;
3773058b2074SCheng-Chieh Hsieh 
3774eb4e52b3SPo Hao Huang 	if (enable) {
3775058b2074SCheng-Chieh Hsieh 		rtw89_phy_write32_clr(rtwdev, physts->setting_addr,
3776058b2074SCheng-Chieh Hsieh 				      physts->dis_trigger_fail_mask);
3777058b2074SCheng-Chieh Hsieh 		rtw89_phy_write32_clr(rtwdev, physts->setting_addr,
3778058b2074SCheng-Chieh Hsieh 				      physts->dis_trigger_brk_mask);
3779eb4e52b3SPo Hao Huang 	} else {
3780058b2074SCheng-Chieh Hsieh 		rtw89_phy_write32_set(rtwdev, physts->setting_addr,
3781058b2074SCheng-Chieh Hsieh 				      physts->dis_trigger_fail_mask);
3782058b2074SCheng-Chieh Hsieh 		rtw89_phy_write32_set(rtwdev, physts->setting_addr,
3783058b2074SCheng-Chieh Hsieh 				      physts->dis_trigger_brk_mask);
3784eb4e52b3SPo Hao Huang 	}
3785eb4e52b3SPo Hao Huang }
3786eb4e52b3SPo Hao Huang 
rtw89_physts_parsing_init(struct rtw89_dev * rtwdev)3787eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev)
3788eb4e52b3SPo Hao Huang {
3789eb4e52b3SPo Hao Huang 	u8 i;
3790eb4e52b3SPo Hao Huang 
3791eb4e52b3SPo Hao Huang 	rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0);
3792eb4e52b3SPo Hao Huang 
3793eb4e52b3SPo Hao Huang 	for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) {
3794eb4e52b3SPo Hao Huang 		if (i >= RTW89_CCK_PKT)
3795eb4e52b3SPo Hao Huang 			rtw89_physts_enable_ie_bitmap(rtwdev, i,
3796eb4e52b3SPo Hao Huang 						      RTW89_PHYSTS_IE09_FTR_0,
3797eb4e52b3SPo Hao Huang 						      true);
3798eb4e52b3SPo Hao Huang 		if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) ||
3799eb4e52b3SPo Hao Huang 		    (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT))
3800eb4e52b3SPo Hao Huang 			continue;
3801eb4e52b3SPo Hao Huang 		rtw89_physts_enable_ie_bitmap(rtwdev, i,
3802eb4e52b3SPo Hao Huang 					      RTW89_PHYSTS_IE24_OFDM_TD_PATH_A,
3803eb4e52b3SPo Hao Huang 					      true);
3804eb4e52b3SPo Hao Huang 	}
3805eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT,
3806eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3807eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT,
3808eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3809eb4e52b3SPo Hao Huang 
3810eb4e52b3SPo Hao Huang 	/* force IE01 for channel index, only channel field is valid */
3811eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT,
3812eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE01_CMN_OFDM, true);
3813eb4e52b3SPo Hao Huang }
3814eb4e52b3SPo Hao Huang 
rtw89_phy_dig_read_gain_table(struct rtw89_dev * rtwdev,int type)3815e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type)
3816e3ec7017SPing-Ke Shih {
3817e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3818e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3819e3ec7017SPing-Ke Shih 	const struct rtw89_phy_dig_gain_cfg *cfg;
3820e3ec7017SPing-Ke Shih 	const char *msg;
3821e3ec7017SPing-Ke Shih 	u8 i;
3822e3ec7017SPing-Ke Shih 	s8 gain_base;
3823e3ec7017SPing-Ke Shih 	s8 *gain_arr;
3824e3ec7017SPing-Ke Shih 	u32 tmp;
3825e3ec7017SPing-Ke Shih 
3826e3ec7017SPing-Ke Shih 	switch (type) {
3827e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_G:
3828e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_g;
3829e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3830e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_g;
3831e3ec7017SPing-Ke Shih 		msg = "lna_gain_g";
3832e3ec7017SPing-Ke Shih 		break;
3833e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_G:
3834e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_g;
3835e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_G;
3836e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_g;
3837e3ec7017SPing-Ke Shih 		msg = "tia_gain_g";
3838e3ec7017SPing-Ke Shih 		break;
3839e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_A:
3840e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_a;
3841e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3842e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_a;
3843e3ec7017SPing-Ke Shih 		msg = "lna_gain_a";
3844e3ec7017SPing-Ke Shih 		break;
3845e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_A:
3846e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_a;
3847e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_A;
3848e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_a;
3849e3ec7017SPing-Ke Shih 		msg = "tia_gain_a";
3850e3ec7017SPing-Ke Shih 		break;
3851e3ec7017SPing-Ke Shih 	default:
3852e3ec7017SPing-Ke Shih 		return;
3853e3ec7017SPing-Ke Shih 	}
3854e3ec7017SPing-Ke Shih 
3855e3ec7017SPing-Ke Shih 	for (i = 0; i < cfg->size; i++) {
3856e3ec7017SPing-Ke Shih 		tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr,
3857e3ec7017SPing-Ke Shih 					    cfg->table[i].mask);
3858e3ec7017SPing-Ke Shih 		tmp >>= DIG_GAIN_SHIFT;
3859e3ec7017SPing-Ke Shih 		gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base;
3860e3ec7017SPing-Ke Shih 		gain_base += DIG_GAIN;
3861e3ec7017SPing-Ke Shih 
3862e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n",
3863e3ec7017SPing-Ke Shih 			    msg, i, gain_arr[i]);
3864e3ec7017SPing-Ke Shih 	}
3865e3ec7017SPing-Ke Shih }
3866e3ec7017SPing-Ke Shih 
rtw89_phy_dig_update_gain_para(struct rtw89_dev * rtwdev)3867e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev)
3868e3ec7017SPing-Ke Shih {
3869e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3870e3ec7017SPing-Ke Shih 	u32 tmp;
3871e3ec7017SPing-Ke Shih 	u8 i;
3872e3ec7017SPing-Ke Shih 
3873d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
3874d264edb1SJohnson Lin 		return;
3875d264edb1SJohnson Lin 
3876e3ec7017SPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW,
3877e3ec7017SPing-Ke Shih 				    B_PATH0_IB_PKPW_MSK);
3878e3ec7017SPing-Ke Shih 	dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT);
3879e3ec7017SPing-Ke Shih 	dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK,
3880e3ec7017SPing-Ke Shih 					    B_PATH0_IB_PBK_MSK);
3881e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n",
3882e3ec7017SPing-Ke Shih 		    dig->ib_pkpwr, dig->ib_pbk);
3883e3ec7017SPing-Ke Shih 
3884e3ec7017SPing-Ke Shih 	for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++)
3885e3ec7017SPing-Ke Shih 		rtw89_phy_dig_read_gain_table(rtwdev, i);
3886e3ec7017SPing-Ke Shih }
3887e3ec7017SPing-Ke Shih 
3888e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22;
3889e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104};
3890e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88};
3891e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16};
3892e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528};
3893e3ec7017SPing-Ke Shih 
rtw89_phy_dig_update_rssi_info(struct rtw89_dev * rtwdev)3894e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev)
3895e3ec7017SPing-Ke Shih {
3896e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info;
3897e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3898e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3899e3ec7017SPing-Ke Shih 
3900e3ec7017SPing-Ke Shih 	if (is_linked) {
3901e3ec7017SPing-Ke Shih 		dig->igi_rssi = ch_info->rssi_min >> 1;
3902e3ec7017SPing-Ke Shih 	} else {
3903e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n");
3904e3ec7017SPing-Ke Shih 		dig->igi_rssi = rssi_nolink;
3905e3ec7017SPing-Ke Shih 	}
3906e3ec7017SPing-Ke Shih }
3907e3ec7017SPing-Ke Shih 
rtw89_phy_dig_update_para(struct rtw89_dev * rtwdev)3908e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev)
3909e3ec7017SPing-Ke Shih {
3910e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3911cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3912e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3913e3ec7017SPing-Ke Shih 	const u16 *fa_th_src = NULL;
3914e3ec7017SPing-Ke Shih 
3915cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
3916e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
3917e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_g;
3918e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_g;
3919e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_2g : fa_th_nolink;
3920e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = false;
3921e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3922e3ec7017SPing-Ke Shih 		break;
3923e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
3924e3ec7017SPing-Ke Shih 	default:
3925e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_a;
3926e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_a;
3927e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_5g : fa_th_nolink;
3928e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = true;
3929e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3930e3ec7017SPing-Ke Shih 		break;
3931e3ec7017SPing-Ke Shih 	}
3932e3ec7017SPing-Ke Shih 	memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th));
3933e3ec7017SPing-Ke Shih 	memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th));
3934e3ec7017SPing-Ke Shih }
3935e3ec7017SPing-Ke Shih 
3936e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20;
3937e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a;
3938e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max;
3939e3ec7017SPing-Ke Shih 
rtw89_phy_dig_para_reset(struct rtw89_dev * rtwdev)3940e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev)
3941e3ec7017SPing-Ke Shih {
3942e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3943e3ec7017SPing-Ke Shih 
3944e3ec7017SPing-Ke Shih 	dig->cur_gaincode.lna_idx = LNA_IDX_MAX;
3945e3ec7017SPing-Ke Shih 	dig->cur_gaincode.tia_idx = TIA_IDX_MAX;
3946e3ec7017SPing-Ke Shih 	dig->cur_gaincode.rxb_idx = RXB_IDX_MAX;
3947e3ec7017SPing-Ke Shih 	dig->force_gaincode.lna_idx = LNA_IDX_MAX;
3948e3ec7017SPing-Ke Shih 	dig->force_gaincode.tia_idx = TIA_IDX_MAX;
3949e3ec7017SPing-Ke Shih 	dig->force_gaincode.rxb_idx = RXB_IDX_MAX;
3950e3ec7017SPing-Ke Shih 
3951e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = igi_max_performance_mode;
3952e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = dynamic_igi_min;
3953e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dynamic_pd_threshold_max;
3954e3ec7017SPing-Ke Shih 	dig->pd_low_th_ofst = pd_low_th_offset;
3955e3ec7017SPing-Ke Shih 	dig->is_linked_pre = false;
3956e3ec7017SPing-Ke Shih }
3957e3ec7017SPing-Ke Shih 
rtw89_phy_dig_init(struct rtw89_dev * rtwdev)3958e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev)
3959e3ec7017SPing-Ke Shih {
3960e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_gain_para(rtwdev);
3961e3ec7017SPing-Ke Shih 	rtw89_phy_dig_reset(rtwdev);
3962e3ec7017SPing-Ke Shih }
3963e3ec7017SPing-Ke Shih 
rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev * rtwdev,u8 rssi)3964e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3965e3ec7017SPing-Ke Shih {
3966e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3967e3ec7017SPing-Ke Shih 	u8 lna_idx;
3968e3ec7017SPing-Ke Shih 
3969e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3970e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX6;
3971e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[1])
3972e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX5;
3973e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[2])
3974e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX4;
3975e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[3])
3976e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX3;
3977e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[4])
3978e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX2;
3979e3ec7017SPing-Ke Shih 	else
3980e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX1;
3981e3ec7017SPing-Ke Shih 
3982e3ec7017SPing-Ke Shih 	return lna_idx;
3983e3ec7017SPing-Ke Shih }
3984e3ec7017SPing-Ke Shih 
rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev * rtwdev,u8 rssi)3985e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3986e3ec7017SPing-Ke Shih {
3987e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3988e3ec7017SPing-Ke Shih 	u8 tia_idx;
3989e3ec7017SPing-Ke Shih 
3990e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3991e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX1;
3992e3ec7017SPing-Ke Shih 	else
3993e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX0;
3994e3ec7017SPing-Ke Shih 
3995e3ec7017SPing-Ke Shih 	return tia_idx;
3996e3ec7017SPing-Ke Shih }
3997e3ec7017SPing-Ke Shih 
3998e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110
3999e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10
rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev * rtwdev,u8 rssi,struct rtw89_agc_gaincode_set * set)4000e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
4001e3ec7017SPing-Ke Shih 					struct rtw89_agc_gaincode_set *set)
4002e3ec7017SPing-Ke Shih {
4003e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4004e3ec7017SPing-Ke Shih 	s8 lna_gain = dig->lna_gain[set->lna_idx];
4005e3ec7017SPing-Ke Shih 	s8 tia_gain = dig->tia_gain[set->tia_idx];
4006e3ec7017SPing-Ke Shih 	s32 wb_rssi = rssi + lna_gain + tia_gain;
4007e3ec7017SPing-Ke Shih 	s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE;
4008e3ec7017SPing-Ke Shih 	u8 rxb_idx;
4009e3ec7017SPing-Ke Shih 
4010e3ec7017SPing-Ke Shih 	rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi;
4011e3ec7017SPing-Ke Shih 	rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX);
4012e3ec7017SPing-Ke Shih 
4013e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n",
4014e3ec7017SPing-Ke Shih 		    wb_rssi, rxb_idx_tmp);
4015e3ec7017SPing-Ke Shih 
4016e3ec7017SPing-Ke Shih 	return rxb_idx;
4017e3ec7017SPing-Ke Shih }
4018e3ec7017SPing-Ke Shih 
rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev * rtwdev,u8 rssi,struct rtw89_agc_gaincode_set * set)4019e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
4020e3ec7017SPing-Ke Shih 					   struct rtw89_agc_gaincode_set *set)
4021e3ec7017SPing-Ke Shih {
4022e3ec7017SPing-Ke Shih 	set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi);
4023e3ec7017SPing-Ke Shih 	set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi);
4024e3ec7017SPing-Ke Shih 	set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set);
4025e3ec7017SPing-Ke Shih 
4026e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4027e3ec7017SPing-Ke Shih 		    "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n",
4028e3ec7017SPing-Ke Shih 		    rssi, set->lna_idx, set->tia_idx, set->rxb_idx);
4029e3ec7017SPing-Ke Shih }
4030e3ec7017SPing-Ke Shih 
4031e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25
4032e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2
rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev * rtwdev)4033e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev)
4034e3ec7017SPing-Ke Shih {
4035e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4036e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
4037e3ec7017SPing-Ke Shih 	enum rtw89_dig_noisy_level noisy_lv;
4038e3ec7017SPing-Ke Shih 	u8 igi_offset = dig->fa_rssi_ofst;
4039e3ec7017SPing-Ke Shih 	u16 fa_ratio = 0;
4040e3ec7017SPing-Ke Shih 
4041e3ec7017SPing-Ke Shih 	fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil;
4042e3ec7017SPing-Ke Shih 
4043e3ec7017SPing-Ke Shih 	if (fa_ratio < dig->fa_th[0])
4044e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL0;
4045e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[1])
4046e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL1;
4047e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[2])
4048e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL2;
4049e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[3])
4050e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL3;
4051e3ec7017SPing-Ke Shih 	else
4052e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX;
4053e3ec7017SPing-Ke Shih 
4054e3ec7017SPing-Ke Shih 	if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2)
4055e3ec7017SPing-Ke Shih 		igi_offset = 0;
4056e3ec7017SPing-Ke Shih 	else
4057e3ec7017SPing-Ke Shih 		igi_offset += noisy_lv * IGI_OFFSET_MUL;
4058e3ec7017SPing-Ke Shih 
4059e3ec7017SPing-Ke Shih 	igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX);
4060e3ec7017SPing-Ke Shih 	dig->fa_rssi_ofst = igi_offset;
4061e3ec7017SPing-Ke Shih 
4062e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4063e3ec7017SPing-Ke Shih 		    "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n",
4064e3ec7017SPing-Ke Shih 		    dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]);
4065e3ec7017SPing-Ke Shih 
4066e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4067e3ec7017SPing-Ke Shih 		    "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n",
4068e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil,
4069e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil,
4070e3ec7017SPing-Ke Shih 		    noisy_lv, igi_offset);
4071e3ec7017SPing-Ke Shih }
4072e3ec7017SPing-Ke Shih 
rtw89_phy_dig_set_lna_idx(struct rtw89_dev * rtwdev,u8 lna_idx)4073e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx)
4074e3ec7017SPing-Ke Shih {
407587deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
407687deaad9SEric Huang 
407787deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr,
407887deaad9SEric Huang 			       dig_regs->p0_lna_init.mask, lna_idx);
407987deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr,
408087deaad9SEric Huang 			       dig_regs->p1_lna_init.mask, lna_idx);
4081e3ec7017SPing-Ke Shih }
4082e3ec7017SPing-Ke Shih 
rtw89_phy_dig_set_tia_idx(struct rtw89_dev * rtwdev,u8 tia_idx)4083e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx)
4084e3ec7017SPing-Ke Shih {
408587deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
408687deaad9SEric Huang 
408787deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr,
408887deaad9SEric Huang 			       dig_regs->p0_tia_init.mask, tia_idx);
408987deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr,
409087deaad9SEric Huang 			       dig_regs->p1_tia_init.mask, tia_idx);
4091e3ec7017SPing-Ke Shih }
4092e3ec7017SPing-Ke Shih 
rtw89_phy_dig_set_rxb_idx(struct rtw89_dev * rtwdev,u8 rxb_idx)4093e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx)
4094e3ec7017SPing-Ke Shih {
409587deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
409687deaad9SEric Huang 
409787deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr,
409887deaad9SEric Huang 			       dig_regs->p0_rxb_init.mask, rxb_idx);
409987deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr,
410087deaad9SEric Huang 			       dig_regs->p1_rxb_init.mask, rxb_idx);
4101e3ec7017SPing-Ke Shih }
4102e3ec7017SPing-Ke Shih 
rtw89_phy_dig_set_igi_cr(struct rtw89_dev * rtwdev,const struct rtw89_agc_gaincode_set set)4103e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev,
4104e3ec7017SPing-Ke Shih 				     const struct rtw89_agc_gaincode_set set)
4105e3ec7017SPing-Ke Shih {
4106e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx);
4107e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx);
4108e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx);
4109e3ec7017SPing-Ke Shih 
4110e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n",
4111e3ec7017SPing-Ke Shih 		    set.lna_idx, set.tia_idx, set.rxb_idx);
4112e3ec7017SPing-Ke Shih }
4113e3ec7017SPing-Ke Shih 
rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev * rtwdev,bool enable)4114e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev,
4115e3ec7017SPing-Ke Shih 						   bool enable)
4116e3ec7017SPing-Ke Shih {
411787deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4118e3ec7017SPing-Ke Shih 
411987deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr,
412087deaad9SEric Huang 			       dig_regs->p0_p20_pagcugc_en.mask, enable);
412187deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr,
412287deaad9SEric Huang 			       dig_regs->p0_s20_pagcugc_en.mask, enable);
412387deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr,
412487deaad9SEric Huang 			       dig_regs->p1_p20_pagcugc_en.mask, enable);
412587deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr,
412687deaad9SEric Huang 			       dig_regs->p1_s20_pagcugc_en.mask, enable);
4127e3ec7017SPing-Ke Shih 
4128e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable);
4129e3ec7017SPing-Ke Shih }
4130e3ec7017SPing-Ke Shih 
rtw89_phy_dig_config_igi(struct rtw89_dev * rtwdev)4131bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev)
4132bed4045fSJohnson Lin {
4133bed4045fSJohnson Lin 	struct rtw89_dig_info *dig = &rtwdev->dig;
4134bed4045fSJohnson Lin 
4135d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
4136d264edb1SJohnson Lin 		return;
4137d264edb1SJohnson Lin 
4138bed4045fSJohnson Lin 	if (dig->force_gaincode_idx_en) {
4139bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4140bed4045fSJohnson Lin 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4141bed4045fSJohnson Lin 			    "Force gaincode index enabled.\n");
4142bed4045fSJohnson Lin 	} else {
4143bed4045fSJohnson Lin 		rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi,
4144bed4045fSJohnson Lin 					       &dig->cur_gaincode);
4145bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode);
4146bed4045fSJohnson Lin 	}
4147bed4045fSJohnson Lin }
4148bed4045fSJohnson Lin 
rtw89_phy_dig_dyn_pd_th(struct rtw89_dev * rtwdev,u8 rssi,bool enable)4149e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi,
4150e3ec7017SPing-Ke Shih 				    bool enable)
4151e3ec7017SPing-Ke Shih {
4152cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
415387deaad9SEric Huang 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
4154cbb145b9SZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
4155e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4156e3ec7017SPing-Ke Shih 	u8 final_rssi = 0, under_region = dig->pd_low_th_ofst;
41571c2423deSJohnson Lin 	u8 ofdm_cca_th;
41581c2423deSJohnson Lin 	s8 cck_cca_th;
41591c2423deSJohnson Lin 	u32 pd_val = 0;
4160e3ec7017SPing-Ke Shih 
4161e3ec7017SPing-Ke Shih 	under_region += PD_TH_SB_FLTR_CMP_VAL;
4162e3ec7017SPing-Ke Shih 
4163e3ec7017SPing-Ke Shih 	switch (cbw) {
4164e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
4165e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW40_CMP_VAL;
4166e3ec7017SPing-Ke Shih 		break;
4167e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
4168e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW80_CMP_VAL;
4169e3ec7017SPing-Ke Shih 		break;
41701c2423deSJohnson Lin 	case RTW89_CHANNEL_WIDTH_160:
41711c2423deSJohnson Lin 		under_region += PD_TH_BW160_CMP_VAL;
41721c2423deSJohnson Lin 		break;
4173e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
4174e3ec7017SPing-Ke Shih 		fallthrough;
4175e3ec7017SPing-Ke Shih 	default:
4176e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW20_CMP_VAL;
4177e3ec7017SPing-Ke Shih 		break;
4178e3ec7017SPing-Ke Shih 	}
4179e3ec7017SPing-Ke Shih 
4180e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dig->igi_rssi;
4181e3ec7017SPing-Ke Shih 
4182e3ec7017SPing-Ke Shih 	final_rssi = min_t(u8, rssi, dig->igi_rssi);
41831c2423deSJohnson Lin 	ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region,
4184e3ec7017SPing-Ke Shih 			      PD_TH_MAX_RSSI + under_region);
4185e3ec7017SPing-Ke Shih 
4186e3ec7017SPing-Ke Shih 	if (enable) {
41871c2423deSJohnson Lin 		pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1;
4188e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
41891c2423deSJohnson Lin 			    "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n",
41901c2423deSJohnson Lin 			    final_rssi, ofdm_cca_th, under_region, pd_val);
4191e3ec7017SPing-Ke Shih 	} else {
4192e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
4193c51ed740SColin Ian King 			    "Dynamic PD th disabled, Set PD_low_bd=0\n");
4194e3ec7017SPing-Ke Shih 	}
4195e3ec7017SPing-Ke Shih 
419687deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
419787deaad9SEric Huang 			       dig_regs->pd_lower_bound_mask, pd_val);
419887deaad9SEric Huang 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
419987deaad9SEric Huang 			       dig_regs->pd_spatial_reuse_en, enable);
42001c2423deSJohnson Lin 
42011c2423deSJohnson Lin 	if (!rtwdev->hal.support_cckpd)
42021c2423deSJohnson Lin 		return;
42031c2423deSJohnson Lin 
42041c2423deSJohnson Lin 	cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI);
42051c2423deSJohnson Lin 	pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX);
42061c2423deSJohnson Lin 
42071c2423deSJohnson Lin 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
42081c2423deSJohnson Lin 		    "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n",
42091c2423deSJohnson Lin 		    final_rssi, cck_cca_th, under_region, pd_val);
42101c2423deSJohnson Lin 
4211058b2074SCheng-Chieh Hsieh 	rtw89_phy_write32_mask(rtwdev, dig_regs->bmode_pd_reg,
4212058b2074SCheng-Chieh Hsieh 			       dig_regs->bmode_cca_rssi_limit_en, enable);
4213058b2074SCheng-Chieh Hsieh 	rtw89_phy_write32_mask(rtwdev, dig_regs->bmode_pd_lower_bound_reg,
4214058b2074SCheng-Chieh Hsieh 			       dig_regs->bmode_rssi_nocca_low_th_mask, pd_val);
4215e3ec7017SPing-Ke Shih }
4216e3ec7017SPing-Ke Shih 
rtw89_phy_dig_reset(struct rtw89_dev * rtwdev)4217e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev)
4218e3ec7017SPing-Ke Shih {
4219e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4220e3ec7017SPing-Ke Shih 
4221e3ec7017SPing-Ke Shih 	dig->bypass_dig = false;
4222e3ec7017SPing-Ke Shih 	rtw89_phy_dig_para_reset(rtwdev);
4223e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
4224e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false);
4225e3ec7017SPing-Ke Shih 	rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4226e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_para(rtwdev);
4227e3ec7017SPing-Ke Shih }
4228e3ec7017SPing-Ke Shih 
4229e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10
rtw89_phy_dig(struct rtw89_dev * rtwdev)4230e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev)
4231e3ec7017SPing-Ke Shih {
4232e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
4233e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
4234e3ec7017SPing-Ke Shih 
4235e3ec7017SPing-Ke Shih 	if (unlikely(dig->bypass_dig)) {
4236e3ec7017SPing-Ke Shih 		dig->bypass_dig = false;
4237e3ec7017SPing-Ke Shih 		return;
4238e3ec7017SPing-Ke Shih 	}
4239e3ec7017SPing-Ke Shih 
4240e3ec7017SPing-Ke Shih 	if (!dig->is_linked_pre && is_linked) {
4241e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n");
4242e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4243e3ec7017SPing-Ke Shih 	} else if (dig->is_linked_pre && !is_linked) {
4244e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n");
4245e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
4246e3ec7017SPing-Ke Shih 	}
4247e3ec7017SPing-Ke Shih 	dig->is_linked_pre = is_linked;
4248e3ec7017SPing-Ke Shih 
4249e3ec7017SPing-Ke Shih 	rtw89_phy_dig_igi_offset_by_env(rtwdev);
4250e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_rssi_info(rtwdev);
4251e3ec7017SPing-Ke Shih 
4252e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ?
4253e3ec7017SPing-Ke Shih 			    dig->igi_rssi - IGI_RSSI_MIN : 0;
4254e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX;
4255e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst;
4256e3ec7017SPing-Ke Shih 
4257e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min,
4258e3ec7017SPing-Ke Shih 				 dig->dyn_igi_max);
4259e3ec7017SPing-Ke Shih 
4260e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4261e3ec7017SPing-Ke Shih 		    "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n",
4262e3ec7017SPing-Ke Shih 		    dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min,
4263e3ec7017SPing-Ke Shih 		    dig->igi_fa_rssi);
4264e3ec7017SPing-Ke Shih 
4265bed4045fSJohnson Lin 	rtw89_phy_dig_config_igi(rtwdev);
4266e3ec7017SPing-Ke Shih 
4267e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en);
4268e3ec7017SPing-Ke Shih 
4269e3ec7017SPing-Ke Shih 	if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max)
4270e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true);
4271e3ec7017SPing-Ke Shih 	else
4272e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4273e3ec7017SPing-Ke Shih }
4274e3ec7017SPing-Ke Shih 
rtw89_phy_tx_path_div_sta_iter(void * data,struct ieee80211_sta * sta)42757dbdf655SPing-Ke Shih static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta)
42767dbdf655SPing-Ke Shih {
42777dbdf655SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
42787dbdf655SPing-Ke Shih 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
42797dbdf655SPing-Ke Shih 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
42807dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
42817dbdf655SPing-Ke Shih 	bool *done = data;
42827dbdf655SPing-Ke Shih 	u8 rssi_a, rssi_b;
42837dbdf655SPing-Ke Shih 	u32 candidate;
42847dbdf655SPing-Ke Shih 
42857dbdf655SPing-Ke Shih 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls)
42867dbdf655SPing-Ke Shih 		return;
42877dbdf655SPing-Ke Shih 
42887dbdf655SPing-Ke Shih 	if (*done)
42897dbdf655SPing-Ke Shih 		return;
42907dbdf655SPing-Ke Shih 
42917dbdf655SPing-Ke Shih 	*done = true;
42927dbdf655SPing-Ke Shih 
42937dbdf655SPing-Ke Shih 	rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]);
42947dbdf655SPing-Ke Shih 	rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]);
42957dbdf655SPing-Ke Shih 
42967dbdf655SPing-Ke Shih 	if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH)
42977dbdf655SPing-Ke Shih 		candidate = RF_A;
42987dbdf655SPing-Ke Shih 	else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH)
42997dbdf655SPing-Ke Shih 		candidate = RF_B;
43007dbdf655SPing-Ke Shih 	else
43017dbdf655SPing-Ke Shih 		return;
43027dbdf655SPing-Ke Shih 
43037dbdf655SPing-Ke Shih 	if (hal->antenna_tx == candidate)
43047dbdf655SPing-Ke Shih 		return;
43057dbdf655SPing-Ke Shih 
43067dbdf655SPing-Ke Shih 	hal->antenna_tx = candidate;
43077dbdf655SPing-Ke Shih 	rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta);
43087dbdf655SPing-Ke Shih 
43097dbdf655SPing-Ke Shih 	if (hal->antenna_tx == RF_A) {
43107dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12);
43117dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11);
43127dbdf655SPing-Ke Shih 	} else if (hal->antenna_tx == RF_B) {
43137dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11);
43147dbdf655SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12);
43157dbdf655SPing-Ke Shih 	}
43167dbdf655SPing-Ke Shih }
43177dbdf655SPing-Ke Shih 
rtw89_phy_tx_path_div_track(struct rtw89_dev * rtwdev)43187dbdf655SPing-Ke Shih void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev)
43197dbdf655SPing-Ke Shih {
43207dbdf655SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
43217dbdf655SPing-Ke Shih 	bool done = false;
43227dbdf655SPing-Ke Shih 
43237dbdf655SPing-Ke Shih 	if (!hal->tx_path_diversity)
43247dbdf655SPing-Ke Shih 		return;
43257dbdf655SPing-Ke Shih 
43267dbdf655SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
43277dbdf655SPing-Ke Shih 					  rtw89_phy_tx_path_div_sta_iter,
43287dbdf655SPing-Ke Shih 					  &done);
43297dbdf655SPing-Ke Shih }
43307dbdf655SPing-Ke Shih 
4331a90c613dSEric Huang #define ANTDIV_MAIN 0
4332a90c613dSEric Huang #define ANTDIV_AUX 1
4333a90c613dSEric Huang 
rtw89_phy_antdiv_set_ant(struct rtw89_dev * rtwdev)4334a90c613dSEric Huang static void rtw89_phy_antdiv_set_ant(struct rtw89_dev *rtwdev)
4335a90c613dSEric Huang {
4336a90c613dSEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4337a90c613dSEric Huang 	u8 default_ant, optional_ant;
4338a90c613dSEric Huang 
4339a90c613dSEric Huang 	if (!hal->ant_diversity || hal->antenna_tx == 0)
4340a90c613dSEric Huang 		return;
4341a90c613dSEric Huang 
4342a90c613dSEric Huang 	if (hal->antenna_tx == RF_B) {
4343a90c613dSEric Huang 		default_ant = ANTDIV_AUX;
4344a90c613dSEric Huang 		optional_ant = ANTDIV_MAIN;
4345a90c613dSEric Huang 	} else {
4346a90c613dSEric Huang 		default_ant = ANTDIV_MAIN;
4347a90c613dSEric Huang 		optional_ant = ANTDIV_AUX;
4348a90c613dSEric Huang 	}
4349a90c613dSEric Huang 
4350a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_CGCS_CTRL,
4351a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4352a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ORI,
4353a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4354a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ALT,
4355a90c613dSEric Huang 			      optional_ant, RTW89_PHY_0);
4356a90c613dSEric Huang 	rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_TX_ORI,
4357a90c613dSEric Huang 			      default_ant, RTW89_PHY_0);
4358a90c613dSEric Huang }
4359a90c613dSEric Huang 
rtw89_phy_swap_hal_antenna(struct rtw89_dev * rtwdev)4360e3715859SEric Huang static void rtw89_phy_swap_hal_antenna(struct rtw89_dev *rtwdev)
4361e3715859SEric Huang {
4362e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4363e3715859SEric Huang 
4364e3715859SEric Huang 	hal->antenna_rx = hal->antenna_rx == RF_A ? RF_B : RF_A;
4365e3715859SEric Huang 	hal->antenna_tx = hal->antenna_rx;
4366e3715859SEric Huang }
4367e3715859SEric Huang 
rtw89_phy_antdiv_decision_state(struct rtw89_dev * rtwdev)4368e3715859SEric Huang static void rtw89_phy_antdiv_decision_state(struct rtw89_dev *rtwdev)
4369e3715859SEric Huang {
4370e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4371e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4372e3715859SEric Huang 	bool no_change = false;
4373e3715859SEric Huang 	u8 main_rssi, aux_rssi;
43745feecb40SEric Huang 	u8 main_evm, aux_evm;
4375e3715859SEric Huang 	u32 candidate;
4376e3715859SEric Huang 
4377e3715859SEric Huang 	antdiv->get_stats = false;
4378e3715859SEric Huang 	antdiv->training_count = 0;
4379e3715859SEric Huang 
4380e3715859SEric Huang 	main_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->main_stats);
43815feecb40SEric Huang 	main_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->main_stats);
4382e3715859SEric Huang 	aux_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->aux_stats);
43835feecb40SEric Huang 	aux_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->aux_stats);
4384e3715859SEric Huang 
43855feecb40SEric Huang 	if (main_evm > aux_evm + ANTDIV_EVM_DIFF_TH)
43865feecb40SEric Huang 		candidate = RF_A;
43875feecb40SEric Huang 	else if (aux_evm > main_evm + ANTDIV_EVM_DIFF_TH)
43885feecb40SEric Huang 		candidate = RF_B;
43895feecb40SEric Huang 	else if (main_rssi > aux_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4390e3715859SEric Huang 		candidate = RF_A;
4391e3715859SEric Huang 	else if (aux_rssi > main_rssi + RTW89_TX_DIV_RSSI_RAW_TH)
4392e3715859SEric Huang 		candidate = RF_B;
4393e3715859SEric Huang 	else
4394e3715859SEric Huang 		no_change = true;
4395e3715859SEric Huang 
4396e3715859SEric Huang 	if (no_change) {
4397e3715859SEric Huang 		/* swap back from training antenna to original */
4398e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4399e3715859SEric Huang 		return;
4400e3715859SEric Huang 	}
4401e3715859SEric Huang 
4402e3715859SEric Huang 	hal->antenna_tx = candidate;
4403e3715859SEric Huang 	hal->antenna_rx = candidate;
4404e3715859SEric Huang }
4405e3715859SEric Huang 
rtw89_phy_antdiv_training_state(struct rtw89_dev * rtwdev)4406e3715859SEric Huang static void rtw89_phy_antdiv_training_state(struct rtw89_dev *rtwdev)
4407e3715859SEric Huang {
4408e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4409e3715859SEric Huang 	u64 state_period;
4410e3715859SEric Huang 
4411e3715859SEric Huang 	if (antdiv->training_count % 2 == 0) {
4412e3715859SEric Huang 		if (antdiv->training_count == 0)
4413e3715859SEric Huang 			rtw89_phy_antdiv_sts_reset(rtwdev);
4414e3715859SEric Huang 
4415e3715859SEric Huang 		antdiv->get_stats = true;
4416e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_TRAINNING_INTVL);
4417e3715859SEric Huang 	} else {
4418e3715859SEric Huang 		antdiv->get_stats = false;
4419e3715859SEric Huang 		state_period = msecs_to_jiffies(ANTDIV_DELAY);
4420e3715859SEric Huang 
4421e3715859SEric Huang 		rtw89_phy_swap_hal_antenna(rtwdev);
4422e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4423e3715859SEric Huang 	}
4424e3715859SEric Huang 
4425e3715859SEric Huang 	antdiv->training_count++;
4426e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work,
4427e3715859SEric Huang 				     state_period);
4428e3715859SEric Huang }
4429e3715859SEric Huang 
rtw89_phy_antdiv_work(struct work_struct * work)4430e3715859SEric Huang void rtw89_phy_antdiv_work(struct work_struct *work)
4431e3715859SEric Huang {
4432e3715859SEric Huang 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
4433e3715859SEric Huang 						antdiv_work.work);
4434e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4435e3715859SEric Huang 
4436e3715859SEric Huang 	mutex_lock(&rtwdev->mutex);
4437e3715859SEric Huang 
4438e3715859SEric Huang 	if (antdiv->training_count <= ANTDIV_TRAINNING_CNT) {
4439e3715859SEric Huang 		rtw89_phy_antdiv_training_state(rtwdev);
4440e3715859SEric Huang 	} else {
4441e3715859SEric Huang 		rtw89_phy_antdiv_decision_state(rtwdev);
4442e3715859SEric Huang 		rtw89_phy_antdiv_set_ant(rtwdev);
4443e3715859SEric Huang 	}
4444e3715859SEric Huang 
4445e3715859SEric Huang 	mutex_unlock(&rtwdev->mutex);
4446e3715859SEric Huang }
4447e3715859SEric Huang 
rtw89_phy_antdiv_track(struct rtw89_dev * rtwdev)4448e3715859SEric Huang void rtw89_phy_antdiv_track(struct rtw89_dev *rtwdev)
4449e3715859SEric Huang {
4450e3715859SEric Huang 	struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv;
4451e3715859SEric Huang 	struct rtw89_hal *hal = &rtwdev->hal;
4452e3715859SEric Huang 	u8 rssi, rssi_pre;
4453e3715859SEric Huang 
4454e3715859SEric Huang 	if (!hal->ant_diversity || hal->ant_diversity_fixed)
4455e3715859SEric Huang 		return;
4456e3715859SEric Huang 
4457e3715859SEric Huang 	rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->target_stats);
4458e3715859SEric Huang 	rssi_pre = antdiv->rssi_pre;
4459e3715859SEric Huang 	antdiv->rssi_pre = rssi;
4460e3715859SEric Huang 	rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats);
4461e3715859SEric Huang 
4462e3715859SEric Huang 	if (abs((int)rssi - (int)rssi_pre) < ANTDIV_RSSI_DIFF_TH)
4463e3715859SEric Huang 		return;
4464e3715859SEric Huang 
4465e3715859SEric Huang 	antdiv->training_count = 0;
4466e3715859SEric Huang 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 0);
4467e3715859SEric Huang }
4468e3715859SEric Huang 
rtw89_phy_env_monitor_init(struct rtw89_dev * rtwdev)4469e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev)
4470e3ec7017SPing-Ke Shih {
4471e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_top_setting_init(rtwdev);
4472e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_setting_init(rtwdev);
4473e3ec7017SPing-Ke Shih }
4474e3ec7017SPing-Ke Shih 
rtw89_phy_dm_init(struct rtw89_dev * rtwdev)4475e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev)
4476e3ec7017SPing-Ke Shih {
4477e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4478e3ec7017SPing-Ke Shih 
4479e3ec7017SPing-Ke Shih 	rtw89_phy_stat_init(rtwdev);
4480e3ec7017SPing-Ke Shih 
4481e3ec7017SPing-Ke Shih 	rtw89_chip_bb_sethw(rtwdev);
4482e3ec7017SPing-Ke Shih 
4483e3ec7017SPing-Ke Shih 	rtw89_phy_env_monitor_init(rtwdev);
4484eb4e52b3SPo Hao Huang 	rtw89_physts_parsing_init(rtwdev);
4485e3ec7017SPing-Ke Shih 	rtw89_phy_dig_init(rtwdev);
4486e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_init(rtwdev);
448729136c95SEric Huang 	rtw89_phy_ul_tb_info_init(rtwdev);
4488a90c613dSEric Huang 	rtw89_phy_antdiv_init(rtwdev);
4489f03bd042SPing-Ke Shih 	rtw89_chip_rfe_gpio(rtwdev);
4490a90c613dSEric Huang 	rtw89_phy_antdiv_set_ant(rtwdev);
4491e3ec7017SPing-Ke Shih 
4492e3ec7017SPing-Ke Shih 	rtw89_phy_init_rf_nctl(rtwdev);
4493e3ec7017SPing-Ke Shih 	rtw89_chip_rfk_init(rtwdev);
4494e3ec7017SPing-Ke Shih 	rtw89_load_txpwr_table(rtwdev, chip->byr_table);
4495e3ec7017SPing-Ke Shih 	rtw89_chip_set_txpwr_ctrl(rtwdev);
4496e3ec7017SPing-Ke Shih 	rtw89_chip_power_trim(rtwdev);
4497cd89a471SPing-Ke Shih 	rtw89_chip_cfg_txrx_path(rtwdev);
4498e3ec7017SPing-Ke Shih }
4499e3ec7017SPing-Ke Shih 
rtw89_phy_set_bss_color(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif)4500e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
4501e3ec7017SPing-Ke Shih {
4502a48f4fd0SEric Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
4503e3ec7017SPing-Ke Shih 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
4504e3ec7017SPing-Ke Shih 	u8 bss_color;
4505e3ec7017SPing-Ke Shih 
4506f276e20bSJohannes Berg 	if (!vif->bss_conf.he_support || !vif->cfg.assoc)
4507e3ec7017SPing-Ke Shih 		return;
4508e3ec7017SPing-Ke Shih 
4509e3ec7017SPing-Ke Shih 	bss_color = vif->bss_conf.he_bss_color.color;
4510e3ec7017SPing-Ke Shih 
4511a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_VLD0, 0x1,
4512e3ec7017SPing-Ke Shih 			      phy_idx);
4513a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT,
4514a48f4fd0SEric Huang 			      bss_color, phy_idx);
4515a48f4fd0SEric Huang 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID,
4516f276e20bSJohannes Berg 			      vif->cfg.aid, phy_idx);
4517e3ec7017SPing-Ke Shih }
4518db7fa61aSZong-Zhe Yang 
4519db7fa61aSZong-Zhe Yang static void
_rfk_write_rf(struct rtw89_dev * rtwdev,const struct rtw89_reg5_def * def)4520db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4521db7fa61aSZong-Zhe Yang {
4522db7fa61aSZong-Zhe Yang 	rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data);
4523db7fa61aSZong-Zhe Yang }
4524db7fa61aSZong-Zhe Yang 
4525db7fa61aSZong-Zhe Yang static void
_rfk_write32_mask(struct rtw89_dev * rtwdev,const struct rtw89_reg5_def * def)4526db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4527db7fa61aSZong-Zhe Yang {
4528db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
4529db7fa61aSZong-Zhe Yang }
4530db7fa61aSZong-Zhe Yang 
4531db7fa61aSZong-Zhe Yang static void
_rfk_write32_set(struct rtw89_dev * rtwdev,const struct rtw89_reg5_def * def)4532db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4533db7fa61aSZong-Zhe Yang {
4534db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_set(rtwdev, def->addr, def->mask);
4535db7fa61aSZong-Zhe Yang }
4536db7fa61aSZong-Zhe Yang 
4537db7fa61aSZong-Zhe Yang static void
_rfk_write32_clr(struct rtw89_dev * rtwdev,const struct rtw89_reg5_def * def)4538db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4539db7fa61aSZong-Zhe Yang {
4540db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_clr(rtwdev, def->addr, def->mask);
4541db7fa61aSZong-Zhe Yang }
4542db7fa61aSZong-Zhe Yang 
4543db7fa61aSZong-Zhe Yang static void
_rfk_delay(struct rtw89_dev * rtwdev,const struct rtw89_reg5_def * def)4544db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4545db7fa61aSZong-Zhe Yang {
4546db7fa61aSZong-Zhe Yang 	udelay(def->data);
4547db7fa61aSZong-Zhe Yang }
4548db7fa61aSZong-Zhe Yang 
4549db7fa61aSZong-Zhe Yang static void
4550db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = {
4551db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WRF] = _rfk_write_rf,
4552db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WM] = _rfk_write32_mask,
4553db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WS] = _rfk_write32_set,
4554db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WC] = _rfk_write32_clr,
4555db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_DELAY] = _rfk_delay,
4556db7fa61aSZong-Zhe Yang };
4557db7fa61aSZong-Zhe Yang 
4558db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM);
4559db7fa61aSZong-Zhe Yang 
4560db7fa61aSZong-Zhe Yang void
rtw89_rfk_parser(struct rtw89_dev * rtwdev,const struct rtw89_rfk_tbl * tbl)4561db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl)
4562db7fa61aSZong-Zhe Yang {
4563db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *p = tbl->defs;
4564db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *end = tbl->defs + tbl->size;
4565db7fa61aSZong-Zhe Yang 
4566db7fa61aSZong-Zhe Yang 	for (; p < end; p++)
4567db7fa61aSZong-Zhe Yang 		_rfk_handler[p->flag](rtwdev, p);
4568db7fa61aSZong-Zhe Yang }
4569db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser);
4570c7845551SPing-Ke Shih 
4571c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4
4572c7845551SPing-Ke Shih 
4573c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = {
4574c7845551SPing-Ke Shih 	{0xD934, 0xff0000},
4575c7845551SPing-Ke Shih 	{0xD934, 0xff000000},
4576c7845551SPing-Ke Shih 	{0xD938, 0xff},
4577c7845551SPing-Ke Shih 	{0xD934, 0xff00},
4578c7845551SPing-Ke Shih };
4579c7845551SPing-Ke Shih 
4580c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = {
4581c7845551SPing-Ke Shih 	{0xD930, 0xff0000},
4582c7845551SPing-Ke Shih 	{0xD930, 0xff000000},
4583c7845551SPing-Ke Shih 	{0xD934, 0xff},
4584c7845551SPing-Ke Shih 	{0xD930, 0xff00},
4585c7845551SPing-Ke Shih };
4586c7845551SPing-Ke Shih 
4587c7845551SPing-Ke Shih static
rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev * rtwdev,enum rtw89_mac_idx mac_idx,enum rtw89_tssi_bandedge_cfg bandedge_cfg,u32 val)4588c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev,
4589c7845551SPing-Ke Shih 					   enum rtw89_mac_idx mac_idx,
4590c7845551SPing-Ke Shih 					   enum rtw89_tssi_bandedge_cfg bandedge_cfg,
4591c7845551SPing-Ke Shih 					   u32 val)
4592c7845551SPing-Ke Shih {
4593c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4594c7845551SPing-Ke Shih 	u32 reg;
4595c7845551SPing-Ke Shih 	int i;
4596c7845551SPing-Ke Shih 
4597c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4598c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_flat;
4599c7845551SPing-Ke Shih 	else
4600c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_level;
4601c7845551SPing-Ke Shih 
4602c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) {
4603c220d08eSPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(rtwdev, regs[i].addr, mac_idx);
4604c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, val);
4605c7845551SPing-Ke Shih 	}
4606c7845551SPing-Ke Shih }
4607c7845551SPing-Ke Shih 
4608c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = {
4609c7845551SPing-Ke Shih 	{0xD91C, 0xff000000},
4610c7845551SPing-Ke Shih 	{0xD920, 0xff},
4611c7845551SPing-Ke Shih 	{0xD920, 0xff00},
4612c7845551SPing-Ke Shih 	{0xD920, 0xff0000},
4613c7845551SPing-Ke Shih 	{0xD920, 0xff000000},
4614c7845551SPing-Ke Shih 	{0xD924, 0xff},
4615c7845551SPing-Ke Shih 	{0xD924, 0xff00},
4616c7845551SPing-Ke Shih 	{0xD914, 0xff000000},
4617c7845551SPing-Ke Shih 	{0xD918, 0xff},
4618c7845551SPing-Ke Shih 	{0xD918, 0xff00},
4619c7845551SPing-Ke Shih 	{0xD918, 0xff0000},
4620c7845551SPing-Ke Shih 	{0xD918, 0xff000000},
4621c7845551SPing-Ke Shih 	{0xD91C, 0xff},
4622c7845551SPing-Ke Shih 	{0xD91C, 0xff00},
4623c7845551SPing-Ke Shih 	{0xD91C, 0xff0000},
4624c7845551SPing-Ke Shih };
4625c7845551SPing-Ke Shih 
4626c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = {
4627c7845551SPing-Ke Shih 	{0xD910, 0xff},
4628c7845551SPing-Ke Shih 	{0xD910, 0xff00},
4629c7845551SPing-Ke Shih 	{0xD910, 0xff0000},
4630c7845551SPing-Ke Shih 	{0xD910, 0xff000000},
4631c7845551SPing-Ke Shih 	{0xD914, 0xff},
4632c7845551SPing-Ke Shih 	{0xD914, 0xff00},
4633c7845551SPing-Ke Shih 	{0xD914, 0xff0000},
4634c7845551SPing-Ke Shih 	{0xD908, 0xff},
4635c7845551SPing-Ke Shih 	{0xD908, 0xff00},
4636c7845551SPing-Ke Shih 	{0xD908, 0xff0000},
4637c7845551SPing-Ke Shih 	{0xD908, 0xff000000},
4638c7845551SPing-Ke Shih 	{0xD90C, 0xff},
4639c7845551SPing-Ke Shih 	{0xD90C, 0xff00},
4640c7845551SPing-Ke Shih 	{0xD90C, 0xff0000},
4641c7845551SPing-Ke Shih 	{0xD90C, 0xff000000},
4642c7845551SPing-Ke Shih };
4643c7845551SPing-Ke Shih 
rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev * rtwdev,enum rtw89_mac_idx mac_idx,enum rtw89_tssi_bandedge_cfg bandedge_cfg)4644c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev,
4645c7845551SPing-Ke Shih 					  enum rtw89_mac_idx mac_idx,
4646c7845551SPing-Ke Shih 					  enum rtw89_tssi_bandedge_cfg bandedge_cfg)
4647c7845551SPing-Ke Shih {
4648c7845551SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4649c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
4650c7845551SPing-Ke Shih 	const u32 *data;
4651c7845551SPing-Ke Shih 	u32 reg;
4652c7845551SPing-Ke Shih 	int i;
4653c7845551SPing-Ke Shih 
4654c7845551SPing-Ke Shih 	if (bandedge_cfg >= RTW89_TSSI_CFG_NUM)
4655c7845551SPing-Ke Shih 		return;
4656c7845551SPing-Ke Shih 
4657c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4658c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_flat;
4659c7845551SPing-Ke Shih 	else
4660c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_level;
4661c7845551SPing-Ke Shih 
4662c7845551SPing-Ke Shih 	data = chip->tssi_dbw_table->data[bandedge_cfg];
4663c7845551SPing-Ke Shih 
4664c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) {
4665c220d08eSPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(rtwdev, regs[i].addr, mac_idx);
4666c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]);
4667c7845551SPing-Ke Shih 	}
4668c7845551SPing-Ke Shih 
4669c220d08eSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_BANDEDGE_CFG, mac_idx);
4670c7845551SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg);
4671c7845551SPing-Ke Shih 
4672c7845551SPing-Ke Shih 	rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg,
4673c7845551SPing-Ke Shih 					      data[RTW89_TSSI_SBW20]);
4674c7845551SPing-Ke Shih }
4675c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg);
4676bb9040b3SPo-Hao Huang 
4677bb9040b3SPo-Hao Huang static
4678bb9040b3SPo-Hao Huang const u8 rtw89_ch_base_table[16] = {1, 0xff,
4679bb9040b3SPo-Hao Huang 				    36, 100, 132, 149, 0xff,
4680bb9040b3SPo-Hao Huang 				    1, 33, 65, 97, 129, 161, 193, 225, 0xff};
4681bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_2G		0
4682bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_FIRST	2
4683bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_LAST	5
4684bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_FIRST	7
4685bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_LAST	14
4686bb9040b3SPo-Hao Huang 
4687bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_MASK		GENMASK(7, 4)
4688bb9040b3SPo-Hao Huang #define RTW89_CH_OFFSET_MASK		GENMASK(3, 0)
4689bb9040b3SPo-Hao Huang 
rtw89_encode_chan_idx(struct rtw89_dev * rtwdev,u8 central_ch,u8 band)4690bb9040b3SPo-Hao Huang u8 rtw89_encode_chan_idx(struct rtw89_dev *rtwdev, u8 central_ch, u8 band)
4691bb9040b3SPo-Hao Huang {
4692bb9040b3SPo-Hao Huang 	u8 chan_idx;
4693bb9040b3SPo-Hao Huang 	u8 last, first;
4694bb9040b3SPo-Hao Huang 	u8 idx;
4695bb9040b3SPo-Hao Huang 
4696bb9040b3SPo-Hao Huang 	switch (band) {
4697bb9040b3SPo-Hao Huang 	case RTW89_BAND_2G:
4698bb9040b3SPo-Hao Huang 		chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, RTW89_CH_BASE_IDX_2G) |
4699bb9040b3SPo-Hao Huang 			   FIELD_PREP(RTW89_CH_OFFSET_MASK, central_ch);
4700bb9040b3SPo-Hao Huang 		return chan_idx;
4701bb9040b3SPo-Hao Huang 	case RTW89_BAND_5G:
4702bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_5G_FIRST;
4703bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_5G_LAST;
4704bb9040b3SPo-Hao Huang 		break;
4705bb9040b3SPo-Hao Huang 	case RTW89_BAND_6G:
4706bb9040b3SPo-Hao Huang 		first = RTW89_CH_BASE_IDX_6G_FIRST;
4707bb9040b3SPo-Hao Huang 		last = RTW89_CH_BASE_IDX_6G_LAST;
4708bb9040b3SPo-Hao Huang 		break;
4709bb9040b3SPo-Hao Huang 	default:
4710bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unsupported band %d\n", band);
4711bb9040b3SPo-Hao Huang 		return 0;
4712bb9040b3SPo-Hao Huang 	}
4713bb9040b3SPo-Hao Huang 
4714bb9040b3SPo-Hao Huang 	for (idx = last; idx >= first; idx--)
4715bb9040b3SPo-Hao Huang 		if (central_ch >= rtw89_ch_base_table[idx])
4716bb9040b3SPo-Hao Huang 			break;
4717bb9040b3SPo-Hao Huang 
4718bb9040b3SPo-Hao Huang 	if (idx < first) {
4719bb9040b3SPo-Hao Huang 		rtw89_warn(rtwdev, "Unknown band %d channel %d\n", band, central_ch);
4720bb9040b3SPo-Hao Huang 		return 0;
4721bb9040b3SPo-Hao Huang 	}
4722bb9040b3SPo-Hao Huang 
4723bb9040b3SPo-Hao Huang 	chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, idx) |
4724bb9040b3SPo-Hao Huang 		   FIELD_PREP(RTW89_CH_OFFSET_MASK,
4725bb9040b3SPo-Hao Huang 			      (central_ch - rtw89_ch_base_table[idx]) >> 1);
4726bb9040b3SPo-Hao Huang 	return chan_idx;
4727bb9040b3SPo-Hao Huang }
4728bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_encode_chan_idx);
4729bb9040b3SPo-Hao Huang 
rtw89_decode_chan_idx(struct rtw89_dev * rtwdev,u8 chan_idx,u8 * ch,enum nl80211_band * band)4730bb9040b3SPo-Hao Huang void rtw89_decode_chan_idx(struct rtw89_dev *rtwdev, u8 chan_idx,
4731bb9040b3SPo-Hao Huang 			   u8 *ch, enum nl80211_band *band)
4732bb9040b3SPo-Hao Huang {
4733bb9040b3SPo-Hao Huang 	u8 idx, offset;
4734bb9040b3SPo-Hao Huang 
4735bb9040b3SPo-Hao Huang 	idx = FIELD_GET(RTW89_CH_BASE_IDX_MASK, chan_idx);
4736bb9040b3SPo-Hao Huang 	offset = FIELD_GET(RTW89_CH_OFFSET_MASK, chan_idx);
4737bb9040b3SPo-Hao Huang 
4738bb9040b3SPo-Hao Huang 	if (idx == RTW89_CH_BASE_IDX_2G) {
4739bb9040b3SPo-Hao Huang 		*band = NL80211_BAND_2GHZ;
4740bb9040b3SPo-Hao Huang 		*ch = offset;
4741bb9040b3SPo-Hao Huang 		return;
4742bb9040b3SPo-Hao Huang 	}
4743bb9040b3SPo-Hao Huang 
4744bb9040b3SPo-Hao Huang 	*band = idx <= RTW89_CH_BASE_IDX_5G_LAST ? NL80211_BAND_5GHZ : NL80211_BAND_6GHZ;
4745bb9040b3SPo-Hao Huang 	*ch = rtw89_ch_base_table[idx] + (offset << 1);
4746bb9040b3SPo-Hao Huang }
4747bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_decode_chan_idx);
4748280c4447SChih-Kang Chang 
4749280c4447SChih-Kang Chang #define EDCCA_DEFAULT 249
rtw89_phy_config_edcca(struct rtw89_dev * rtwdev,bool scan)4750280c4447SChih-Kang Chang void rtw89_phy_config_edcca(struct rtw89_dev *rtwdev, bool scan)
4751280c4447SChih-Kang Chang {
4752280c4447SChih-Kang Chang 	u32 reg = rtwdev->chip->edcca_lvl_reg;
4753280c4447SChih-Kang Chang 	struct rtw89_hal *hal = &rtwdev->hal;
4754280c4447SChih-Kang Chang 	u32 val;
4755280c4447SChih-Kang Chang 
4756280c4447SChih-Kang Chang 	if (scan) {
4757280c4447SChih-Kang Chang 		hal->edcca_bak = rtw89_phy_read32(rtwdev, reg);
4758280c4447SChih-Kang Chang 		val = hal->edcca_bak;
4759280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_A_MSK);
4760280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_P_MSK);
4761280c4447SChih-Kang Chang 		u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_PPDU_LVL_MSK);
4762280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, val);
4763280c4447SChih-Kang Chang 	} else {
4764280c4447SChih-Kang Chang 		rtw89_phy_write32(rtwdev, reg, hal->edcca_bak);
4765280c4447SChih-Kang Chang 	}
4766280c4447SChih-Kang Chang }
47671165f571SPing-Ke Shih 
4768058b2074SCheng-Chieh Hsieh static const struct rtw89_ccx_regs rtw89_ccx_regs_ax = {
4769058b2074SCheng-Chieh Hsieh 	.setting_addr = R_CCX,
4770058b2074SCheng-Chieh Hsieh 	.edcca_opt_mask = B_CCX_EDCCA_OPT_MSK,
4771058b2074SCheng-Chieh Hsieh 	.measurement_trig_mask = B_MEASUREMENT_TRIG_MSK,
4772058b2074SCheng-Chieh Hsieh 	.trig_opt_mask = B_CCX_TRIG_OPT_MSK,
4773058b2074SCheng-Chieh Hsieh 	.en_mask = B_CCX_EN_MSK,
4774058b2074SCheng-Chieh Hsieh 	.ifs_cnt_addr = R_IFS_COUNTER,
4775058b2074SCheng-Chieh Hsieh 	.ifs_clm_period_mask = B_IFS_CLM_PERIOD_MSK,
4776058b2074SCheng-Chieh Hsieh 	.ifs_clm_cnt_unit_mask = B_IFS_CLM_COUNTER_UNIT_MSK,
4777058b2074SCheng-Chieh Hsieh 	.ifs_clm_cnt_clear_mask = B_IFS_COUNTER_CLR_MSK,
4778058b2074SCheng-Chieh Hsieh 	.ifs_collect_en_mask = B_IFS_COLLECT_EN,
4779058b2074SCheng-Chieh Hsieh 	.ifs_t1_addr = R_IFS_T1,
4780058b2074SCheng-Chieh Hsieh 	.ifs_t1_th_h_mask = B_IFS_T1_TH_HIGH_MSK,
4781058b2074SCheng-Chieh Hsieh 	.ifs_t1_en_mask = B_IFS_T1_EN_MSK,
4782058b2074SCheng-Chieh Hsieh 	.ifs_t1_th_l_mask = B_IFS_T1_TH_LOW_MSK,
4783058b2074SCheng-Chieh Hsieh 	.ifs_t2_addr = R_IFS_T2,
4784058b2074SCheng-Chieh Hsieh 	.ifs_t2_th_h_mask = B_IFS_T2_TH_HIGH_MSK,
4785058b2074SCheng-Chieh Hsieh 	.ifs_t2_en_mask = B_IFS_T2_EN_MSK,
4786058b2074SCheng-Chieh Hsieh 	.ifs_t2_th_l_mask = B_IFS_T2_TH_LOW_MSK,
4787058b2074SCheng-Chieh Hsieh 	.ifs_t3_addr = R_IFS_T3,
4788058b2074SCheng-Chieh Hsieh 	.ifs_t3_th_h_mask = B_IFS_T3_TH_HIGH_MSK,
4789058b2074SCheng-Chieh Hsieh 	.ifs_t3_en_mask = B_IFS_T3_EN_MSK,
4790058b2074SCheng-Chieh Hsieh 	.ifs_t3_th_l_mask = B_IFS_T3_TH_LOW_MSK,
4791058b2074SCheng-Chieh Hsieh 	.ifs_t4_addr = R_IFS_T4,
4792058b2074SCheng-Chieh Hsieh 	.ifs_t4_th_h_mask = B_IFS_T4_TH_HIGH_MSK,
4793058b2074SCheng-Chieh Hsieh 	.ifs_t4_en_mask = B_IFS_T4_EN_MSK,
4794058b2074SCheng-Chieh Hsieh 	.ifs_t4_th_l_mask = B_IFS_T4_TH_LOW_MSK,
4795058b2074SCheng-Chieh Hsieh 	.ifs_clm_tx_cnt_addr = R_IFS_CLM_TX_CNT,
4796058b2074SCheng-Chieh Hsieh 	.ifs_clm_edcca_excl_cca_fa_mask = B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK,
4797058b2074SCheng-Chieh Hsieh 	.ifs_clm_tx_cnt_msk = B_IFS_CLM_TX_CNT_MSK,
4798058b2074SCheng-Chieh Hsieh 	.ifs_clm_cca_addr = R_IFS_CLM_CCA,
4799058b2074SCheng-Chieh Hsieh 	.ifs_clm_ofdmcca_excl_fa_mask = B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK,
4800058b2074SCheng-Chieh Hsieh 	.ifs_clm_cckcca_excl_fa_mask = B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK,
4801058b2074SCheng-Chieh Hsieh 	.ifs_clm_fa_addr = R_IFS_CLM_FA,
4802058b2074SCheng-Chieh Hsieh 	.ifs_clm_ofdm_fa_mask = B_IFS_CLM_OFDM_FA_MSK,
4803058b2074SCheng-Chieh Hsieh 	.ifs_clm_cck_fa_mask = B_IFS_CLM_CCK_FA_MSK,
4804058b2074SCheng-Chieh Hsieh 	.ifs_his_addr = R_IFS_HIS,
4805058b2074SCheng-Chieh Hsieh 	.ifs_t4_his_mask = B_IFS_T4_HIS_MSK,
4806058b2074SCheng-Chieh Hsieh 	.ifs_t3_his_mask = B_IFS_T3_HIS_MSK,
4807058b2074SCheng-Chieh Hsieh 	.ifs_t2_his_mask = B_IFS_T2_HIS_MSK,
4808058b2074SCheng-Chieh Hsieh 	.ifs_t1_his_mask = B_IFS_T1_HIS_MSK,
4809058b2074SCheng-Chieh Hsieh 	.ifs_avg_l_addr = R_IFS_AVG_L,
4810058b2074SCheng-Chieh Hsieh 	.ifs_t2_avg_mask = B_IFS_T2_AVG_MSK,
4811058b2074SCheng-Chieh Hsieh 	.ifs_t1_avg_mask = B_IFS_T1_AVG_MSK,
4812058b2074SCheng-Chieh Hsieh 	.ifs_avg_h_addr = R_IFS_AVG_H,
4813058b2074SCheng-Chieh Hsieh 	.ifs_t4_avg_mask = B_IFS_T4_AVG_MSK,
4814058b2074SCheng-Chieh Hsieh 	.ifs_t3_avg_mask = B_IFS_T3_AVG_MSK,
4815058b2074SCheng-Chieh Hsieh 	.ifs_cca_l_addr = R_IFS_CCA_L,
4816058b2074SCheng-Chieh Hsieh 	.ifs_t2_cca_mask = B_IFS_T2_CCA_MSK,
4817058b2074SCheng-Chieh Hsieh 	.ifs_t1_cca_mask = B_IFS_T1_CCA_MSK,
4818058b2074SCheng-Chieh Hsieh 	.ifs_cca_h_addr = R_IFS_CCA_H,
4819058b2074SCheng-Chieh Hsieh 	.ifs_t4_cca_mask = B_IFS_T4_CCA_MSK,
4820058b2074SCheng-Chieh Hsieh 	.ifs_t3_cca_mask = B_IFS_T3_CCA_MSK,
4821058b2074SCheng-Chieh Hsieh 	.ifs_total_addr = R_IFSCNT,
4822058b2074SCheng-Chieh Hsieh 	.ifs_cnt_done_mask = B_IFSCNT_DONE_MSK,
4823058b2074SCheng-Chieh Hsieh 	.ifs_total_mask = B_IFSCNT_TOTAL_CNT_MSK,
4824058b2074SCheng-Chieh Hsieh };
4825058b2074SCheng-Chieh Hsieh 
4826058b2074SCheng-Chieh Hsieh static const struct rtw89_physts_regs rtw89_physts_regs_ax = {
4827058b2074SCheng-Chieh Hsieh 	.setting_addr = R_PLCP_HISTOGRAM,
4828058b2074SCheng-Chieh Hsieh 	.dis_trigger_fail_mask = B_STS_DIS_TRIG_BY_FAIL,
4829058b2074SCheng-Chieh Hsieh 	.dis_trigger_brk_mask = B_STS_DIS_TRIG_BY_BRK,
4830058b2074SCheng-Chieh Hsieh };
4831058b2074SCheng-Chieh Hsieh 
48321165f571SPing-Ke Shih const struct rtw89_phy_gen_def rtw89_phy_gen_ax = {
48331165f571SPing-Ke Shih 	.cr_base = 0x10000,
4834058b2074SCheng-Chieh Hsieh 	.ccx = &rtw89_ccx_regs_ax,
4835058b2074SCheng-Chieh Hsieh 	.physts = &rtw89_physts_regs_ax,
48361165f571SPing-Ke Shih };
48371165f571SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_gen_ax);
4838