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" 1329136c95SEric Huang #include "util.h" 14e3ec7017SPing-Ke Shih 15e3ec7017SPing-Ke Shih static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev, 16e3ec7017SPing-Ke Shih const struct rtw89_ra_report *report) 17e3ec7017SPing-Ke Shih { 18e3ec7017SPing-Ke Shih u32 bit_rate = report->bit_rate; 19e3ec7017SPing-Ke Shih 20e3ec7017SPing-Ke Shih /* lower than ofdm, do not aggregate */ 21e3ec7017SPing-Ke Shih if (bit_rate < 550) 22e3ec7017SPing-Ke Shih return 1; 23e3ec7017SPing-Ke Shih 240d466f05SPing-Ke Shih /* avoid AMSDU for legacy rate */ 250d466f05SPing-Ke Shih if (report->might_fallback_legacy) 26e3ec7017SPing-Ke Shih return 1; 27e3ec7017SPing-Ke Shih 28e3ec7017SPing-Ke Shih /* lower than 20M vht 2ss mcs8, make it small */ 29e3ec7017SPing-Ke Shih if (bit_rate < 1800) 30e3ec7017SPing-Ke Shih return 1200; 31e3ec7017SPing-Ke Shih 32e3ec7017SPing-Ke Shih /* lower than 40M vht 2ss mcs9, make it medium */ 33e3ec7017SPing-Ke Shih if (bit_rate < 4000) 34e3ec7017SPing-Ke Shih return 2600; 35e3ec7017SPing-Ke Shih 36e3ec7017SPing-Ke Shih /* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */ 37e3ec7017SPing-Ke Shih if (bit_rate < 7000) 38e3ec7017SPing-Ke Shih return 3500; 39e3ec7017SPing-Ke Shih 40e3ec7017SPing-Ke Shih return rtwdev->chip->max_amsdu_limit; 41e3ec7017SPing-Ke Shih } 42e3ec7017SPing-Ke Shih 43e3ec7017SPing-Ke Shih static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap) 44e3ec7017SPing-Ke Shih { 45e3ec7017SPing-Ke Shih u64 ra_mask = 0; 46e3ec7017SPing-Ke Shih u8 mcs_cap; 47e3ec7017SPing-Ke Shih int i, nss; 48e3ec7017SPing-Ke Shih 49e3ec7017SPing-Ke Shih for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) { 50e3ec7017SPing-Ke Shih mcs_cap = mcs_map & 0x3; 51e3ec7017SPing-Ke Shih switch (mcs_cap) { 52e3ec7017SPing-Ke Shih case 2: 53e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss; 54e3ec7017SPing-Ke Shih break; 55e3ec7017SPing-Ke Shih case 1: 56e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss; 57e3ec7017SPing-Ke Shih break; 58e3ec7017SPing-Ke Shih case 0: 59e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss; 60e3ec7017SPing-Ke Shih break; 61e3ec7017SPing-Ke Shih default: 62e3ec7017SPing-Ke Shih break; 63e3ec7017SPing-Ke Shih } 64e3ec7017SPing-Ke Shih } 65e3ec7017SPing-Ke Shih 66e3ec7017SPing-Ke Shih return ra_mask; 67e3ec7017SPing-Ke Shih } 68e3ec7017SPing-Ke Shih 69e3ec7017SPing-Ke Shih static u64 get_he_ra_mask(struct ieee80211_sta *sta) 70e3ec7017SPing-Ke Shih { 71046d2e7cSSriram R struct ieee80211_sta_he_cap cap = sta->deflink.he_cap; 72e3ec7017SPing-Ke Shih u16 mcs_map; 73e3ec7017SPing-Ke Shih 74046d2e7cSSriram R switch (sta->deflink.bandwidth) { 75e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_160: 76e3ec7017SPing-Ke Shih if (cap.he_cap_elem.phy_cap_info[0] & 77e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) 78e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80); 79e3ec7017SPing-Ke Shih else 80e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160); 81e3ec7017SPing-Ke Shih break; 82e3ec7017SPing-Ke Shih default: 83e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80); 84e3ec7017SPing-Ke Shih } 85e3ec7017SPing-Ke Shih 86e3ec7017SPing-Ke Shih /* MCS11, MCS9, MCS7 */ 87e3ec7017SPing-Ke Shih return get_mcs_ra_mask(mcs_map, 11, 2); 88e3ec7017SPing-Ke Shih } 89e3ec7017SPing-Ke Shih 90e3ec7017SPing-Ke Shih #define RA_FLOOR_TABLE_SIZE 7 91e3ec7017SPing-Ke Shih #define RA_FLOOR_UP_GAP 3 92e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi, 93e3ec7017SPing-Ke Shih u8 ratr_state) 94e3ec7017SPing-Ke Shih { 95e3ec7017SPing-Ke Shih u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100}; 96e3ec7017SPing-Ke Shih u8 rssi_lv = 0; 97e3ec7017SPing-Ke Shih u8 i; 98e3ec7017SPing-Ke Shih 99e3ec7017SPing-Ke Shih rssi >>= 1; 100e3ec7017SPing-Ke Shih for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 101e3ec7017SPing-Ke Shih if (i >= ratr_state) 102e3ec7017SPing-Ke Shih rssi_lv_t[i] += RA_FLOOR_UP_GAP; 103e3ec7017SPing-Ke Shih if (rssi < rssi_lv_t[i]) { 104e3ec7017SPing-Ke Shih rssi_lv = i; 105e3ec7017SPing-Ke Shih break; 106e3ec7017SPing-Ke Shih } 107e3ec7017SPing-Ke Shih } 108e3ec7017SPing-Ke Shih if (rssi_lv == 0) 109e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 110e3ec7017SPing-Ke Shih else if (rssi_lv == 1) 111e3ec7017SPing-Ke Shih return 0xfffffffffffffff0ULL; 112e3ec7017SPing-Ke Shih else if (rssi_lv == 2) 1133c2c2e2eSChien-Hsun Liao return 0xffffffffffffefe0ULL; 114e3ec7017SPing-Ke Shih else if (rssi_lv == 3) 1153c2c2e2eSChien-Hsun Liao return 0xffffffffffffcfc0ULL; 116e3ec7017SPing-Ke Shih else if (rssi_lv == 4) 1173c2c2e2eSChien-Hsun Liao return 0xffffffffffff8f80ULL; 118e3ec7017SPing-Ke Shih else if (rssi_lv >= 5) 1193c2c2e2eSChien-Hsun Liao return 0xffffffffffff0f00ULL; 120e3ec7017SPing-Ke Shih 121e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 122e3ec7017SPing-Ke Shih } 123e3ec7017SPing-Ke Shih 1243c2c2e2eSChien-Hsun Liao static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak) 1253c2c2e2eSChien-Hsun Liao { 1263c2c2e2eSChien-Hsun Liao if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0) 1273c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1283c2c2e2eSChien-Hsun Liao 1293c2c2e2eSChien-Hsun Liao if (ra_mask == 0) 1303c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1313c2c2e2eSChien-Hsun Liao 1323c2c2e2eSChien-Hsun Liao return ra_mask; 1333c2c2e2eSChien-Hsun Liao } 1343c2c2e2eSChien-Hsun Liao 135e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta) 136e3ec7017SPing-Ke Shih { 137e3ec7017SPing-Ke Shih struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta); 138cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 139e3ec7017SPing-Ke Shih struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 140e3ec7017SPing-Ke Shih enum nl80211_band band; 141e3ec7017SPing-Ke Shih u64 cfg_mask; 142e3ec7017SPing-Ke Shih 143e3ec7017SPing-Ke Shih if (!rtwsta->use_cfg_mask) 144e3ec7017SPing-Ke Shih return -1; 145e3ec7017SPing-Ke Shih 146cbb145b9SZong-Zhe Yang switch (chan->band_type) { 147e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 148e3ec7017SPing-Ke Shih band = NL80211_BAND_2GHZ; 149e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy, 150e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES); 151e3ec7017SPing-Ke Shih break; 152e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 153e3ec7017SPing-Ke Shih band = NL80211_BAND_5GHZ; 154e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy, 155e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES); 156e3ec7017SPing-Ke Shih break; 157f76b3276SPing-Ke Shih case RTW89_BAND_6G: 158f76b3276SPing-Ke Shih band = NL80211_BAND_6GHZ; 159f76b3276SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy, 160f76b3276SPing-Ke Shih RA_MASK_OFDM_RATES); 161f76b3276SPing-Ke Shih break; 162e3ec7017SPing-Ke Shih default: 163cbb145b9SZong-Zhe Yang rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type); 164e3ec7017SPing-Ke Shih return -1; 165e3ec7017SPing-Ke Shih } 166e3ec7017SPing-Ke Shih 167046d2e7cSSriram R if (sta->deflink.he_cap.has_he) { 168e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0], 169e3ec7017SPing-Ke Shih RA_MASK_HE_1SS_RATES); 170e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1], 171e3ec7017SPing-Ke Shih RA_MASK_HE_2SS_RATES); 172046d2e7cSSriram R } else if (sta->deflink.vht_cap.vht_supported) { 173e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0], 174e3ec7017SPing-Ke Shih RA_MASK_VHT_1SS_RATES); 175e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1], 176e3ec7017SPing-Ke Shih RA_MASK_VHT_2SS_RATES); 177046d2e7cSSriram R } else if (sta->deflink.ht_cap.ht_supported) { 178e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0], 179e3ec7017SPing-Ke Shih RA_MASK_HT_1SS_RATES); 180e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1], 181e3ec7017SPing-Ke Shih RA_MASK_HT_2SS_RATES); 182e3ec7017SPing-Ke Shih } 183e3ec7017SPing-Ke Shih 184e3ec7017SPing-Ke Shih return cfg_mask; 185e3ec7017SPing-Ke Shih } 186e3ec7017SPing-Ke Shih 187e3ec7017SPing-Ke Shih static const u64 188e3ec7017SPing-Ke Shih rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES, 189e3ec7017SPing-Ke Shih RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES}; 190e3ec7017SPing-Ke Shih static const u64 191e3ec7017SPing-Ke Shih rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES, 192e3ec7017SPing-Ke Shih RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES}; 193e3ec7017SPing-Ke Shih static const u64 194e3ec7017SPing-Ke Shih rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES, 195e3ec7017SPing-Ke Shih RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES}; 196e3ec7017SPing-Ke Shih 1970891b366SKuan-Chung Chen static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev, 1980891b366SKuan-Chung Chen struct rtw89_sta *rtwsta, 1990891b366SKuan-Chung Chen bool *fix_giltf_en, u8 *fix_giltf) 2000891b366SKuan-Chung Chen { 2010891b366SKuan-Chung Chen const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 2020891b366SKuan-Chung Chen struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 2030891b366SKuan-Chung Chen u8 band = chan->band_type; 2040891b366SKuan-Chung Chen enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 2050891b366SKuan-Chung Chen u8 he_gi = mask->control[nl_band].he_gi; 2060891b366SKuan-Chung Chen u8 he_ltf = mask->control[nl_band].he_ltf; 2070891b366SKuan-Chung Chen 2080891b366SKuan-Chung Chen if (!rtwsta->use_cfg_mask) 2090891b366SKuan-Chung Chen return; 2100891b366SKuan-Chung Chen 2110891b366SKuan-Chung Chen if (he_ltf == 2 && he_gi == 2) { 2120891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_LGI_4XHE32; 2130891b366SKuan-Chung Chen } else if (he_ltf == 2 && he_gi == 0) { 2140891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_SGI_4XHE08; 2150891b366SKuan-Chung Chen } else if (he_ltf == 1 && he_gi == 1) { 2160891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_2XHE16; 2170891b366SKuan-Chung Chen } else if (he_ltf == 1 && he_gi == 0) { 2180891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_2XHE08; 2190891b366SKuan-Chung Chen } else if (he_ltf == 0 && he_gi == 1) { 2200891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_1XHE16; 2210891b366SKuan-Chung Chen } else if (he_ltf == 0 && he_gi == 0) { 2220891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_1XHE08; 2230891b366SKuan-Chung Chen } else { 2240891b366SKuan-Chung Chen *fix_giltf_en = false; 2250891b366SKuan-Chung Chen return; 2260891b366SKuan-Chung Chen } 2270891b366SKuan-Chung Chen 2280891b366SKuan-Chung Chen *fix_giltf_en = true; 2290891b366SKuan-Chung Chen } 2300891b366SKuan-Chung Chen 231e3ec7017SPing-Ke Shih static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev, 232e3ec7017SPing-Ke Shih struct ieee80211_sta *sta, bool csi) 233e3ec7017SPing-Ke Shih { 234e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 235e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = rtwsta->rtwvif; 236e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern; 237e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 238cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 2393788c599SDian-Syuan Yang struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif); 240e3ec7017SPing-Ke Shih const u64 *high_rate_masks = rtw89_ra_mask_ht_rates; 241e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi); 242e3ec7017SPing-Ke Shih u64 ra_mask = 0; 2433c2c2e2eSChien-Hsun Liao u64 ra_mask_bak; 244e3ec7017SPing-Ke Shih u8 mode = 0; 245e3ec7017SPing-Ke Shih u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY; 246e3ec7017SPing-Ke Shih u8 bw_mode = 0; 247e3ec7017SPing-Ke Shih u8 stbc_en = 0; 248e3ec7017SPing-Ke Shih u8 ldpc_en = 0; 2490891b366SKuan-Chung Chen u8 fix_giltf = 0; 250e3ec7017SPing-Ke Shih u8 i; 251e3ec7017SPing-Ke Shih bool sgi = false; 2520891b366SKuan-Chung Chen bool fix_giltf_en = false; 253e3ec7017SPing-Ke Shih 254e3ec7017SPing-Ke Shih memset(ra, 0, sizeof(*ra)); 255e3ec7017SPing-Ke Shih /* Set the ra mask from sta's capability */ 256046d2e7cSSriram R if (sta->deflink.he_cap.has_he) { 257e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HE; 258e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HE; 259e3ec7017SPing-Ke Shih ra_mask |= get_he_ra_mask(sta); 260e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_he_rates; 261046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] & 262e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ) 263e3ec7017SPing-Ke Shih stbc_en = 1; 264046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] & 265e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD) 266e3ec7017SPing-Ke Shih ldpc_en = 1; 2670891b366SKuan-Chung Chen rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, &fix_giltf_en, &fix_giltf); 268046d2e7cSSriram R } else if (sta->deflink.vht_cap.vht_supported) { 269046d2e7cSSriram R u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map); 270e3ec7017SPing-Ke Shih 271e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_VHT; 272e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_VHT; 273e3ec7017SPing-Ke Shih /* MCS9, MCS8, MCS7 */ 274e3ec7017SPing-Ke Shih ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1); 275e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_vht_rates; 276046d2e7cSSriram R if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK) 277e3ec7017SPing-Ke Shih stbc_en = 1; 278046d2e7cSSriram R if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC) 279e3ec7017SPing-Ke Shih ldpc_en = 1; 280046d2e7cSSriram R } else if (sta->deflink.ht_cap.ht_supported) { 281e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HT; 282e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HT; 283046d2e7cSSriram R ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) | 284046d2e7cSSriram R ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) | 285046d2e7cSSriram R (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) | 286046d2e7cSSriram R (sta->deflink.ht_cap.mcs.rx_mask[0] << 12); 287e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_ht_rates; 288046d2e7cSSriram R if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC) 289e3ec7017SPing-Ke Shih stbc_en = 1; 290046d2e7cSSriram R if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING) 291e3ec7017SPing-Ke Shih ldpc_en = 1; 292e3ec7017SPing-Ke Shih } 293e3ec7017SPing-Ke Shih 294cbb145b9SZong-Zhe Yang switch (chan->band_type) { 295f76b3276SPing-Ke Shih case RTW89_BAND_2G: 296046d2e7cSSriram R ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ]; 2973788c599SDian-Syuan Yang if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf) 298e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_CCK; 2993788c599SDian-Syuan Yang if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0) 3003788c599SDian-Syuan Yang mode |= RTW89_RA_MODE_OFDM; 301f76b3276SPing-Ke Shih break; 302f76b3276SPing-Ke Shih case RTW89_BAND_5G: 303046d2e7cSSriram R ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4; 304e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 305f76b3276SPing-Ke Shih break; 306f76b3276SPing-Ke Shih case RTW89_BAND_6G: 307046d2e7cSSriram R ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4; 308f76b3276SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 309f76b3276SPing-Ke Shih break; 310f76b3276SPing-Ke Shih default: 311f76b3276SPing-Ke Shih rtw89_err(rtwdev, "Unknown band type\n"); 312f76b3276SPing-Ke Shih break; 313e3ec7017SPing-Ke Shih } 314e3ec7017SPing-Ke Shih 3153c2c2e2eSChien-Hsun Liao ra_mask_bak = ra_mask; 3163c2c2e2eSChien-Hsun Liao 317e3ec7017SPing-Ke Shih if (mode >= RTW89_RA_MODE_HT) { 3183c2c2e2eSChien-Hsun Liao u64 mask = 0; 319e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->hal.tx_nss; i++) 3203c2c2e2eSChien-Hsun Liao mask |= high_rate_masks[i]; 321e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_OFDM) 3223c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBOFDM_RATES; 323e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_CCK) 3243c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBCCK_RATES; 3253c2c2e2eSChien-Hsun Liao ra_mask &= mask; 326e3ec7017SPing-Ke Shih } else if (mode & RTW89_RA_MODE_OFDM) { 3273c2c2e2eSChien-Hsun Liao ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES); 328e3ec7017SPing-Ke Shih } 329e3ec7017SPing-Ke Shih 3303c2c2e2eSChien-Hsun Liao if (mode != RTW89_RA_MODE_CCK) 331e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0); 3323c2c2e2eSChien-Hsun Liao 3333c2c2e2eSChien-Hsun Liao ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak); 334e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 335e3ec7017SPing-Ke Shih 336046d2e7cSSriram R switch (sta->deflink.bandwidth) { 337167044afSPing-Ke Shih case IEEE80211_STA_RX_BW_160: 338167044afSPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_160; 339046d2e7cSSriram R sgi = sta->deflink.vht_cap.vht_supported && 340046d2e7cSSriram R (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160); 341167044afSPing-Ke Shih break; 342e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_80: 343e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_80; 344046d2e7cSSriram R sgi = sta->deflink.vht_cap.vht_supported && 345046d2e7cSSriram R (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80); 346e3ec7017SPing-Ke Shih break; 347e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_40: 348e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_40; 349046d2e7cSSriram R sgi = sta->deflink.ht_cap.ht_supported && 350046d2e7cSSriram R (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40); 351e3ec7017SPing-Ke Shih break; 352e3ec7017SPing-Ke Shih default: 353e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_20; 354046d2e7cSSriram R sgi = sta->deflink.ht_cap.ht_supported && 355046d2e7cSSriram R (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20); 356e3ec7017SPing-Ke Shih break; 357e3ec7017SPing-Ke Shih } 358e3ec7017SPing-Ke Shih 359046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] & 360e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM) 361e3ec7017SPing-Ke Shih ra->dcm_cap = 1; 362e3ec7017SPing-Ke Shih 3633788c599SDian-Syuan Yang if (rate_pattern->enable && !vif->p2p) { 364e3ec7017SPing-Ke Shih ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 365e3ec7017SPing-Ke Shih ra_mask &= rate_pattern->ra_mask; 366e3ec7017SPing-Ke Shih mode = rate_pattern->ra_mode; 367e3ec7017SPing-Ke Shih } 368e3ec7017SPing-Ke Shih 369e3ec7017SPing-Ke Shih ra->bw_cap = bw_mode; 370e3ec7017SPing-Ke Shih ra->mode_ctrl = mode; 371e3ec7017SPing-Ke Shih ra->macid = rtwsta->mac_id; 372e3ec7017SPing-Ke Shih ra->stbc_cap = stbc_en; 373e3ec7017SPing-Ke Shih ra->ldpc_cap = ldpc_en; 374046d2e7cSSriram R ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1; 375e3ec7017SPing-Ke Shih ra->en_sgi = sgi; 376e3ec7017SPing-Ke Shih ra->ra_mask = ra_mask; 3770891b366SKuan-Chung Chen ra->fix_giltf_en = fix_giltf_en; 3780891b366SKuan-Chung Chen ra->fix_giltf = fix_giltf; 379e3ec7017SPing-Ke Shih 380e3ec7017SPing-Ke Shih if (!csi) 381e3ec7017SPing-Ke Shih return; 382e3ec7017SPing-Ke Shih 383e3ec7017SPing-Ke Shih ra->fixed_csi_rate_en = false; 384e3ec7017SPing-Ke Shih ra->ra_csi_rate_en = true; 385e3ec7017SPing-Ke Shih ra->cr_tbl_sel = false; 386e3ec7017SPing-Ke Shih ra->band_num = rtwvif->phy_idx; 387e3ec7017SPing-Ke Shih ra->csi_bw = bw_mode; 388e3ec7017SPing-Ke Shih ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32; 389e3ec7017SPing-Ke Shih ra->csi_mcs_ss_idx = 5; 390e3ec7017SPing-Ke Shih ra->csi_mode = csi_mode; 391e3ec7017SPing-Ke Shih } 392e3ec7017SPing-Ke Shih 3939d9a9edcSPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta, 3949d9a9edcSPing-Ke Shih u32 changed) 395e3ec7017SPing-Ke Shih { 396e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 397e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 398e3ec7017SPing-Ke Shih 399e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, false); 4009d9a9edcSPing-Ke Shih 4019d9a9edcSPing-Ke Shih if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) 402e3ec7017SPing-Ke Shih ra->upd_mask = 1; 4039d9a9edcSPing-Ke Shih if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED)) 4049d9a9edcSPing-Ke Shih ra->upd_bw_nss_mask = 1; 4059d9a9edcSPing-Ke Shih 406e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 407e3ec7017SPing-Ke Shih "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d", 408e3ec7017SPing-Ke Shih ra->macid, 409e3ec7017SPing-Ke Shih ra->bw_cap, 410e3ec7017SPing-Ke Shih ra->ss_num, 411e3ec7017SPing-Ke Shih ra->en_sgi, 412e3ec7017SPing-Ke Shih ra->giltf); 413e3ec7017SPing-Ke Shih 414e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, false); 415e3ec7017SPing-Ke Shih } 416e3ec7017SPing-Ke Shih 417e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next, 418e3ec7017SPing-Ke Shih u16 rate_base, u64 ra_mask, u8 ra_mode, 419e3ec7017SPing-Ke Shih u32 rate_ctrl, u32 ctrl_skip, bool force) 420e3ec7017SPing-Ke Shih { 421e3ec7017SPing-Ke Shih u8 n, c; 422e3ec7017SPing-Ke Shih 423e3ec7017SPing-Ke Shih if (rate_ctrl == ctrl_skip) 424e3ec7017SPing-Ke Shih return true; 425e3ec7017SPing-Ke Shih 426e3ec7017SPing-Ke Shih n = hweight32(rate_ctrl); 427e3ec7017SPing-Ke Shih if (n == 0) 428e3ec7017SPing-Ke Shih return true; 429e3ec7017SPing-Ke Shih 430e3ec7017SPing-Ke Shih if (force && n != 1) 431e3ec7017SPing-Ke Shih return false; 432e3ec7017SPing-Ke Shih 433e3ec7017SPing-Ke Shih if (next->enable) 434e3ec7017SPing-Ke Shih return false; 435e3ec7017SPing-Ke Shih 436e3ec7017SPing-Ke Shih c = __fls(rate_ctrl); 437e3ec7017SPing-Ke Shih next->rate = rate_base + c; 438e3ec7017SPing-Ke Shih next->ra_mode = ra_mode; 439e3ec7017SPing-Ke Shih next->ra_mask = ra_mask; 440e3ec7017SPing-Ke Shih next->enable = true; 441e3ec7017SPing-Ke Shih 442e3ec7017SPing-Ke Shih return true; 443e3ec7017SPing-Ke Shih } 444e3ec7017SPing-Ke Shih 445e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev, 446e3ec7017SPing-Ke Shih struct ieee80211_vif *vif, 447e3ec7017SPing-Ke Shih const struct cfg80211_bitrate_mask *mask) 448e3ec7017SPing-Ke Shih { 449e3ec7017SPing-Ke Shih struct ieee80211_supported_band *sband; 450e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 451e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern next_pattern = {0}; 452cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 453e3ec7017SPing-Ke Shih static const u16 hw_rate_he[] = {RTW89_HW_RATE_HE_NSS1_MCS0, 454e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS2_MCS0, 455e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS3_MCS0, 456e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS4_MCS0}; 457e3ec7017SPing-Ke Shih static const u16 hw_rate_vht[] = {RTW89_HW_RATE_VHT_NSS1_MCS0, 458e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS2_MCS0, 459e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS3_MCS0, 460e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS4_MCS0}; 461e3ec7017SPing-Ke Shih static const u16 hw_rate_ht[] = {RTW89_HW_RATE_MCS0, 462e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS8, 463e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS16, 464e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS24}; 465cbb145b9SZong-Zhe Yang u8 band = chan->band_type; 466a06d2dd7SZong-Zhe Yang enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 467e3ec7017SPing-Ke Shih u8 tx_nss = rtwdev->hal.tx_nss; 468e3ec7017SPing-Ke Shih u8 i; 469e3ec7017SPing-Ke Shih 470e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 471e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_he[i], 472e3ec7017SPing-Ke Shih RA_MASK_HE_RATES, RTW89_RA_MODE_HE, 473a06d2dd7SZong-Zhe Yang mask->control[nl_band].he_mcs[i], 474e3ec7017SPing-Ke Shih 0, true)) 475e3ec7017SPing-Ke Shih goto out; 476e3ec7017SPing-Ke Shih 477e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 478e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i], 479e3ec7017SPing-Ke Shih RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT, 480a06d2dd7SZong-Zhe Yang mask->control[nl_band].vht_mcs[i], 481e3ec7017SPing-Ke Shih 0, true)) 482e3ec7017SPing-Ke Shih goto out; 483e3ec7017SPing-Ke Shih 484e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 485e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i], 486e3ec7017SPing-Ke Shih RA_MASK_HT_RATES, RTW89_RA_MODE_HT, 487a06d2dd7SZong-Zhe Yang mask->control[nl_band].ht_mcs[i], 488e3ec7017SPing-Ke Shih 0, true)) 489e3ec7017SPing-Ke Shih goto out; 490e3ec7017SPing-Ke Shih 491e3ec7017SPing-Ke Shih /* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and 492e3ec7017SPing-Ke Shih * require at least one basic rate for ieee80211_set_bitrate_mask, 493e3ec7017SPing-Ke Shih * so the decision just depends on if all bitrates are set or not. 494e3ec7017SPing-Ke Shih */ 495a06d2dd7SZong-Zhe Yang sband = rtwdev->hw->wiphy->bands[nl_band]; 496e3ec7017SPing-Ke Shih if (band == RTW89_BAND_2G) { 497e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1, 498e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES, 499e3ec7017SPing-Ke Shih RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM, 500a06d2dd7SZong-Zhe Yang mask->control[nl_band].legacy, 501e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 502e3ec7017SPing-Ke Shih goto out; 503e3ec7017SPing-Ke Shih } else { 504e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6, 505e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM, 506a06d2dd7SZong-Zhe Yang mask->control[nl_band].legacy, 507e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 508e3ec7017SPing-Ke Shih goto out; 509e3ec7017SPing-Ke Shih } 510e3ec7017SPing-Ke Shih 511e3ec7017SPing-Ke Shih if (!next_pattern.enable) 512e3ec7017SPing-Ke Shih goto out; 513e3ec7017SPing-Ke Shih 514e3ec7017SPing-Ke Shih rtwvif->rate_pattern = next_pattern; 515e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 516e3ec7017SPing-Ke Shih "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n", 517e3ec7017SPing-Ke Shih next_pattern.rate, 518e3ec7017SPing-Ke Shih next_pattern.ra_mask, 519e3ec7017SPing-Ke Shih next_pattern.ra_mode); 520e3ec7017SPing-Ke Shih return; 521e3ec7017SPing-Ke Shih 522e3ec7017SPing-Ke Shih out: 523e3ec7017SPing-Ke Shih rtwvif->rate_pattern.enable = false; 524e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n"); 525e3ec7017SPing-Ke Shih } 526e3ec7017SPing-Ke Shih 527e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta) 528e3ec7017SPing-Ke Shih { 529e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = (struct rtw89_dev *)data; 530e3ec7017SPing-Ke Shih 5319d9a9edcSPing-Ke Shih rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED); 532e3ec7017SPing-Ke Shih } 533e3ec7017SPing-Ke Shih 534e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev) 535e3ec7017SPing-Ke Shih { 536e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 537e3ec7017SPing-Ke Shih rtw89_phy_ra_updata_sta_iter, 538e3ec7017SPing-Ke Shih rtwdev); 539e3ec7017SPing-Ke Shih } 540e3ec7017SPing-Ke Shih 541e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta) 542e3ec7017SPing-Ke Shih { 543e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 544e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 545e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR; 546e3ec7017SPing-Ke Shih bool csi = rtw89_sta_has_beamformer_cap(sta); 547e3ec7017SPing-Ke Shih 548e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, csi); 549e3ec7017SPing-Ke Shih 550e3ec7017SPing-Ke Shih if (rssi > 40) 551e3ec7017SPing-Ke Shih ra->init_rate_lv = 1; 552e3ec7017SPing-Ke Shih else if (rssi > 20) 553e3ec7017SPing-Ke Shih ra->init_rate_lv = 2; 554e3ec7017SPing-Ke Shih else if (rssi > 1) 555e3ec7017SPing-Ke Shih ra->init_rate_lv = 3; 556e3ec7017SPing-Ke Shih else 557e3ec7017SPing-Ke Shih ra->init_rate_lv = 0; 558e3ec7017SPing-Ke Shih ra->upd_all = 1; 559e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 560e3ec7017SPing-Ke Shih "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d", 561e3ec7017SPing-Ke Shih ra->macid, 562e3ec7017SPing-Ke Shih ra->mode_ctrl, 563e3ec7017SPing-Ke Shih ra->bw_cap, 564e3ec7017SPing-Ke Shih ra->ss_num, 565e3ec7017SPing-Ke Shih ra->init_rate_lv); 566e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 567e3ec7017SPing-Ke Shih "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d", 568e3ec7017SPing-Ke Shih ra->dcm_cap, 569e3ec7017SPing-Ke Shih ra->er_cap, 570e3ec7017SPing-Ke Shih ra->ldpc_cap, 571e3ec7017SPing-Ke Shih ra->stbc_cap, 572e3ec7017SPing-Ke Shih ra->en_sgi, 573e3ec7017SPing-Ke Shih ra->giltf); 574e3ec7017SPing-Ke Shih 575e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, csi); 576e3ec7017SPing-Ke Shih } 577e3ec7017SPing-Ke Shih 578e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev, 5793e5831caSZong-Zhe Yang const struct rtw89_chan *chan, 580e3ec7017SPing-Ke Shih enum rtw89_bandwidth dbw) 581e3ec7017SPing-Ke Shih { 5823e5831caSZong-Zhe Yang enum rtw89_bandwidth cbw = chan->band_width; 5833e5831caSZong-Zhe Yang u8 pri_ch = chan->primary_channel; 5843e5831caSZong-Zhe Yang u8 central_ch = chan->channel; 585e3ec7017SPing-Ke Shih u8 txsc_idx = 0; 586e3ec7017SPing-Ke Shih u8 tmp = 0; 587e3ec7017SPing-Ke Shih 588e3ec7017SPing-Ke Shih if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20) 589e3ec7017SPing-Ke Shih return txsc_idx; 590e3ec7017SPing-Ke Shih 591e3ec7017SPing-Ke Shih switch (cbw) { 592e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 593e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 1 : 2; 594e3ec7017SPing-Ke Shih break; 595e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 596e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 597e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 598e3ec7017SPing-Ke Shih txsc_idx = (pri_ch - central_ch) >> 1; 599e3ec7017SPing-Ke Shih else 600e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 1; 601e3ec7017SPing-Ke Shih } else { 602e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 9 : 10; 603e3ec7017SPing-Ke Shih } 604e3ec7017SPing-Ke Shih break; 605e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_160: 606e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 607e3ec7017SPing-Ke Shih tmp = (pri_ch - central_ch) >> 1; 608e3ec7017SPing-Ke Shih else 609e3ec7017SPing-Ke Shih tmp = ((central_ch - pri_ch) >> 1) + 1; 610e3ec7017SPing-Ke Shih 611e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 612e3ec7017SPing-Ke Shih txsc_idx = tmp; 613e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 614e3ec7017SPing-Ke Shih if (tmp == 1 || tmp == 3) 615e3ec7017SPing-Ke Shih txsc_idx = 9; 616e3ec7017SPing-Ke Shih else if (tmp == 5 || tmp == 7) 617e3ec7017SPing-Ke Shih txsc_idx = 11; 618e3ec7017SPing-Ke Shih else if (tmp == 2 || tmp == 4) 619e3ec7017SPing-Ke Shih txsc_idx = 10; 620e3ec7017SPing-Ke Shih else if (tmp == 6 || tmp == 8) 621e3ec7017SPing-Ke Shih txsc_idx = 12; 622e3ec7017SPing-Ke Shih else 623e3ec7017SPing-Ke Shih return 0xff; 624e3ec7017SPing-Ke Shih } else { 625e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 13 : 14; 626e3ec7017SPing-Ke Shih } 627e3ec7017SPing-Ke Shih break; 628e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80_80: 629e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 630e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 631e3ec7017SPing-Ke Shih txsc_idx = (10 - (pri_ch - central_ch)) >> 1; 632e3ec7017SPing-Ke Shih else 633e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 5; 634e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 635e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 10 : 12; 636e3ec7017SPing-Ke Shih } else { 637e3ec7017SPing-Ke Shih txsc_idx = 14; 638e3ec7017SPing-Ke Shih } 639e3ec7017SPing-Ke Shih break; 640e3ec7017SPing-Ke Shih default: 641e3ec7017SPing-Ke Shih break; 642e3ec7017SPing-Ke Shih } 643e3ec7017SPing-Ke Shih 644e3ec7017SPing-Ke Shih return txsc_idx; 645e3ec7017SPing-Ke Shih } 646861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc); 647e3ec7017SPing-Ke Shih 64884d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev) 64984d0e33eSChung-Hsuan Hung { 65084d0e33eSChung-Hsuan Hung return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) || 65184d0e33eSChung-Hsuan Hung !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1); 65284d0e33eSChung-Hsuan Hung } 65384d0e33eSChung-Hsuan Hung 654e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 655e3ec7017SPing-Ke Shih u32 addr, u32 mask) 656e3ec7017SPing-Ke Shih { 657e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 658e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 659e3ec7017SPing-Ke Shih u32 val, direct_addr; 660e3ec7017SPing-Ke Shih 661e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 662e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 663e3ec7017SPing-Ke Shih return INV_RF_DATA; 664e3ec7017SPing-Ke Shih } 665e3ec7017SPing-Ke Shih 666e3ec7017SPing-Ke Shih addr &= 0xff; 667e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 668e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 669e3ec7017SPing-Ke Shih 670e3ec7017SPing-Ke Shih val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask); 671e3ec7017SPing-Ke Shih 672e3ec7017SPing-Ke Shih return val; 673e3ec7017SPing-Ke Shih } 674e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf); 675e3ec7017SPing-Ke Shih 67684d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev, 67784d0e33eSChung-Hsuan Hung enum rtw89_rf_path rf_path, u32 addr, u32 mask) 67884d0e33eSChung-Hsuan Hung { 67984d0e33eSChung-Hsuan Hung bool busy; 68084d0e33eSChung-Hsuan Hung bool done; 68184d0e33eSChung-Hsuan Hung u32 val; 68284d0e33eSChung-Hsuan Hung int ret; 68384d0e33eSChung-Hsuan Hung 68484d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 68584d0e33eSChung-Hsuan Hung 1, 30, false, rtwdev); 68684d0e33eSChung-Hsuan Hung if (ret) { 68784d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "read rf busy swsi\n"); 68884d0e33eSChung-Hsuan Hung return INV_RF_DATA; 68984d0e33eSChung-Hsuan Hung } 69084d0e33eSChung-Hsuan Hung 69184d0e33eSChung-Hsuan Hung mask &= RFREG_MASK; 69284d0e33eSChung-Hsuan Hung 69384d0e33eSChung-Hsuan Hung val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) | 69484d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr); 69584d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val); 69684d0e33eSChung-Hsuan Hung udelay(2); 69784d0e33eSChung-Hsuan Hung 69884d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1, 69984d0e33eSChung-Hsuan Hung 30, false, rtwdev, R_SWSI_V1, 70084d0e33eSChung-Hsuan Hung B_SWSI_R_DATA_DONE_V1); 70184d0e33eSChung-Hsuan Hung if (ret) { 70284d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "read swsi busy\n"); 70384d0e33eSChung-Hsuan Hung return INV_RF_DATA; 70484d0e33eSChung-Hsuan Hung } 70584d0e33eSChung-Hsuan Hung 70684d0e33eSChung-Hsuan Hung return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask); 70784d0e33eSChung-Hsuan Hung } 70884d0e33eSChung-Hsuan Hung 70984d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 71084d0e33eSChung-Hsuan Hung u32 addr, u32 mask) 71184d0e33eSChung-Hsuan Hung { 71284d0e33eSChung-Hsuan Hung bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 71384d0e33eSChung-Hsuan Hung 71484d0e33eSChung-Hsuan Hung if (rf_path >= rtwdev->chip->rf_path_num) { 71584d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 71684d0e33eSChung-Hsuan Hung return INV_RF_DATA; 71784d0e33eSChung-Hsuan Hung } 71884d0e33eSChung-Hsuan Hung 71984d0e33eSChung-Hsuan Hung if (ad_sel) 72084d0e33eSChung-Hsuan Hung return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask); 72184d0e33eSChung-Hsuan Hung else 72284d0e33eSChung-Hsuan Hung return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask); 72384d0e33eSChung-Hsuan Hung } 72484d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1); 72584d0e33eSChung-Hsuan Hung 726e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 727e3ec7017SPing-Ke Shih u32 addr, u32 mask, u32 data) 728e3ec7017SPing-Ke Shih { 729e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 730e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 731e3ec7017SPing-Ke Shih u32 direct_addr; 732e3ec7017SPing-Ke Shih 733e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 734e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 735e3ec7017SPing-Ke Shih return false; 736e3ec7017SPing-Ke Shih } 737e3ec7017SPing-Ke Shih 738e3ec7017SPing-Ke Shih addr &= 0xff; 739e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 740e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 741e3ec7017SPing-Ke Shih 742e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data); 743e3ec7017SPing-Ke Shih 744e3ec7017SPing-Ke Shih /* delay to ensure writing properly */ 745e3ec7017SPing-Ke Shih udelay(1); 746e3ec7017SPing-Ke Shih 747e3ec7017SPing-Ke Shih return true; 748e3ec7017SPing-Ke Shih } 749e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf); 750e3ec7017SPing-Ke Shih 75184d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev, 75284d0e33eSChung-Hsuan Hung enum rtw89_rf_path rf_path, u32 addr, u32 mask, 75384d0e33eSChung-Hsuan Hung u32 data) 75484d0e33eSChung-Hsuan Hung { 75584d0e33eSChung-Hsuan Hung u8 bit_shift; 75684d0e33eSChung-Hsuan Hung u32 val; 75784d0e33eSChung-Hsuan Hung bool busy, b_msk_en = false; 75884d0e33eSChung-Hsuan Hung int ret; 75984d0e33eSChung-Hsuan Hung 76084d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 76184d0e33eSChung-Hsuan Hung 1, 30, false, rtwdev); 76284d0e33eSChung-Hsuan Hung if (ret) { 76384d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "write rf busy swsi\n"); 76484d0e33eSChung-Hsuan Hung return false; 76584d0e33eSChung-Hsuan Hung } 76684d0e33eSChung-Hsuan Hung 76784d0e33eSChung-Hsuan Hung data &= RFREG_MASK; 76884d0e33eSChung-Hsuan Hung mask &= RFREG_MASK; 76984d0e33eSChung-Hsuan Hung 77084d0e33eSChung-Hsuan Hung if (mask != RFREG_MASK) { 77184d0e33eSChung-Hsuan Hung b_msk_en = true; 77284d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK, 77384d0e33eSChung-Hsuan Hung mask); 77484d0e33eSChung-Hsuan Hung bit_shift = __ffs(mask); 77584d0e33eSChung-Hsuan Hung data = (data << bit_shift) & RFREG_MASK; 77684d0e33eSChung-Hsuan Hung } 77784d0e33eSChung-Hsuan Hung 77884d0e33eSChung-Hsuan Hung val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) | 77984d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) | 78084d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) | 78184d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_VAL_V1, data); 78284d0e33eSChung-Hsuan Hung 78384d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val); 78484d0e33eSChung-Hsuan Hung 78584d0e33eSChung-Hsuan Hung return true; 78684d0e33eSChung-Hsuan Hung } 78784d0e33eSChung-Hsuan Hung 78884d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 78984d0e33eSChung-Hsuan Hung u32 addr, u32 mask, u32 data) 79084d0e33eSChung-Hsuan Hung { 79184d0e33eSChung-Hsuan Hung bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 79284d0e33eSChung-Hsuan Hung 79384d0e33eSChung-Hsuan Hung if (rf_path >= rtwdev->chip->rf_path_num) { 79484d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 79584d0e33eSChung-Hsuan Hung return false; 79684d0e33eSChung-Hsuan Hung } 79784d0e33eSChung-Hsuan Hung 79884d0e33eSChung-Hsuan Hung if (ad_sel) 79984d0e33eSChung-Hsuan Hung return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data); 80084d0e33eSChung-Hsuan Hung else 80184d0e33eSChung-Hsuan Hung return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data); 80284d0e33eSChung-Hsuan Hung } 80384d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1); 80484d0e33eSChung-Hsuan Hung 805d9112042SChih-Kang Chang static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev) 806d9112042SChih-Kang Chang { 807d9112042SChih-Kang Chang return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1; 808d9112042SChih-Kang Chang } 809d9112042SChih-Kang Chang 810e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev, 811e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx) 812e3ec7017SPing-Ke Shih { 813e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 814e3ec7017SPing-Ke Shih 815e3ec7017SPing-Ke Shih chip->ops->bb_reset(rtwdev, phy_idx); 816e3ec7017SPing-Ke Shih } 817e3ec7017SPing-Ke Shih 818e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev, 819e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 820e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 821e3ec7017SPing-Ke Shih void *extra_data) 822e3ec7017SPing-Ke Shih { 823e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) 824e3ec7017SPing-Ke Shih mdelay(50); 825e3ec7017SPing-Ke Shih else if (reg->addr == 0xfd) 826e3ec7017SPing-Ke Shih mdelay(5); 827e3ec7017SPing-Ke Shih else if (reg->addr == 0xfc) 828e3ec7017SPing-Ke Shih mdelay(1); 829e3ec7017SPing-Ke Shih else if (reg->addr == 0xfb) 830e3ec7017SPing-Ke Shih udelay(50); 831e3ec7017SPing-Ke Shih else if (reg->addr == 0xfa) 832e3ec7017SPing-Ke Shih udelay(5); 833e3ec7017SPing-Ke Shih else if (reg->addr == 0xf9) 834e3ec7017SPing-Ke Shih udelay(1); 835e3ec7017SPing-Ke Shih else 836e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, reg->addr, reg->data); 837e3ec7017SPing-Ke Shih } 838e3ec7017SPing-Ke Shih 839e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg { 840e885871eSZong-Zhe Yang u32 addr; 841e885871eSZong-Zhe Yang struct { 842e885871eSZong-Zhe Yang union { 843e885871eSZong-Zhe Yang u8 type; 844e885871eSZong-Zhe Yang struct { 845e885871eSZong-Zhe Yang u8 rxsc_start:4; 846e885871eSZong-Zhe Yang u8 bw:4; 847e885871eSZong-Zhe Yang }; 848e885871eSZong-Zhe Yang }; 849e885871eSZong-Zhe Yang u8 path; 850e885871eSZong-Zhe Yang u8 gain_band; 851e885871eSZong-Zhe Yang u8 cfg_type; 852e885871eSZong-Zhe Yang }; 853e885871eSZong-Zhe Yang } __packed; 854e885871eSZong-Zhe Yang 855e885871eSZong-Zhe Yang static void 856e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev, 857e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 858e885871eSZong-Zhe Yang { 859e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 860e885871eSZong-Zhe Yang u8 type = arg.type; 861e885871eSZong-Zhe Yang u8 path = arg.path; 862e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 863e885871eSZong-Zhe Yang int i; 864e885871eSZong-Zhe Yang 865e885871eSZong-Zhe Yang switch (type) { 866e885871eSZong-Zhe Yang case 0: 867e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 868e885871eSZong-Zhe Yang gain->lna_gain[gband][path][i] = data & 0xff; 869e885871eSZong-Zhe Yang break; 870e885871eSZong-Zhe Yang case 1: 871e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 872e885871eSZong-Zhe Yang gain->lna_gain[gband][path][i] = data & 0xff; 873e885871eSZong-Zhe Yang break; 874e885871eSZong-Zhe Yang case 2: 875e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) 876e885871eSZong-Zhe Yang gain->tia_gain[gband][path][i] = data & 0xff; 877e885871eSZong-Zhe Yang break; 878e885871eSZong-Zhe Yang default: 879e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 880e885871eSZong-Zhe Yang "bb gain error {0x%x:0x%x} with unknown type: %d\n", 881e885871eSZong-Zhe Yang arg.addr, data, type); 882e885871eSZong-Zhe Yang break; 883e885871eSZong-Zhe Yang } 884e885871eSZong-Zhe Yang } 885e885871eSZong-Zhe Yang 886e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx { 887e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_FULL = 0, 888e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_20 = 1, 889e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_20_1 = 5, 890e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_40 = 9, 891e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_80 = 13, 892e885871eSZong-Zhe Yang }; 893e885871eSZong-Zhe Yang 894e885871eSZong-Zhe Yang static void 895e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev, 896e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 897e885871eSZong-Zhe Yang { 898e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 899e885871eSZong-Zhe Yang u8 rxsc_start = arg.rxsc_start; 900e885871eSZong-Zhe Yang u8 bw = arg.bw; 901e885871eSZong-Zhe Yang u8 path = arg.path; 902e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 903e885871eSZong-Zhe Yang u8 rxsc; 904e885871eSZong-Zhe Yang s8 ofst; 905e885871eSZong-Zhe Yang int i; 906e885871eSZong-Zhe Yang 907e885871eSZong-Zhe Yang switch (bw) { 908e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_20: 909e885871eSZong-Zhe Yang gain->rpl_ofst_20[gband][path] = (s8)data; 910e885871eSZong-Zhe Yang break; 911e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_40: 912e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 913e885871eSZong-Zhe Yang gain->rpl_ofst_40[gband][path][0] = (s8)data; 914e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 915e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 916e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 917e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 918e885871eSZong-Zhe Yang gain->rpl_ofst_40[gband][path][rxsc] = ofst; 919e885871eSZong-Zhe Yang } 920e885871eSZong-Zhe Yang } 921e885871eSZong-Zhe Yang break; 922e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_80: 923e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 924e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][0] = (s8)data; 925e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 926e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 927e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 928e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 929e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][rxsc] = ofst; 930e885871eSZong-Zhe Yang } 931e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 932e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 933e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 934e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 935e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][rxsc] = ofst; 936e885871eSZong-Zhe Yang } 937e885871eSZong-Zhe Yang } 938e885871eSZong-Zhe Yang break; 939e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 940e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 941e885871eSZong-Zhe Yang gain->rpl_ofst_160[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_160[gband][path][rxsc] = ofst; 947e885871eSZong-Zhe Yang } 948e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) { 949e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 950e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i; 951e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 952e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 953e885871eSZong-Zhe Yang } 954e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 955e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 956e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 957e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 958e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 959e885871eSZong-Zhe Yang } 960e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) { 961e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 962e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_80 + i; 963e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 964e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 965e885871eSZong-Zhe Yang } 966e885871eSZong-Zhe Yang } 967e885871eSZong-Zhe Yang break; 968e885871eSZong-Zhe Yang default: 969e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 970e885871eSZong-Zhe Yang "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n", 971e885871eSZong-Zhe Yang arg.addr, data, bw); 972e885871eSZong-Zhe Yang break; 973e885871eSZong-Zhe Yang } 974e885871eSZong-Zhe Yang } 975e885871eSZong-Zhe Yang 976e885871eSZong-Zhe Yang static void 977e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev, 978e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 979e885871eSZong-Zhe Yang { 980e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 981e885871eSZong-Zhe Yang u8 type = arg.type; 982e885871eSZong-Zhe Yang u8 path = arg.path; 983e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 984e885871eSZong-Zhe Yang int i; 985e885871eSZong-Zhe Yang 986e885871eSZong-Zhe Yang switch (type) { 987e885871eSZong-Zhe Yang case 0: 988e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 989e885871eSZong-Zhe Yang gain->lna_gain_bypass[gband][path][i] = data & 0xff; 990e885871eSZong-Zhe Yang break; 991e885871eSZong-Zhe Yang case 1: 992e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 993e885871eSZong-Zhe Yang gain->lna_gain_bypass[gband][path][i] = data & 0xff; 994e885871eSZong-Zhe Yang break; 995e885871eSZong-Zhe Yang default: 996e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 997e885871eSZong-Zhe Yang "bb gain bypass {0x%x:0x%x} with unknown type: %d\n", 998e885871eSZong-Zhe Yang arg.addr, data, type); 999e885871eSZong-Zhe Yang break; 1000e885871eSZong-Zhe Yang } 1001e885871eSZong-Zhe Yang } 1002e885871eSZong-Zhe Yang 1003e885871eSZong-Zhe Yang static void 1004e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev, 1005e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 1006e885871eSZong-Zhe Yang { 1007e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 1008e885871eSZong-Zhe Yang u8 type = arg.type; 1009e885871eSZong-Zhe Yang u8 path = arg.path; 1010e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 1011e885871eSZong-Zhe Yang int i; 1012e885871eSZong-Zhe Yang 1013e885871eSZong-Zhe Yang switch (type) { 1014e885871eSZong-Zhe Yang case 0: 1015e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 1016e885871eSZong-Zhe Yang gain->lna_op1db[gband][path][i] = data & 0xff; 1017e885871eSZong-Zhe Yang break; 1018e885871eSZong-Zhe Yang case 1: 1019e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 1020e885871eSZong-Zhe Yang gain->lna_op1db[gband][path][i] = data & 0xff; 1021e885871eSZong-Zhe Yang break; 1022e885871eSZong-Zhe Yang case 2: 1023e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 1024e885871eSZong-Zhe Yang gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1025e885871eSZong-Zhe Yang break; 1026e885871eSZong-Zhe Yang case 3: 1027e885871eSZong-Zhe Yang for (i = 4; i < 8; i++, data >>= 8) 1028e885871eSZong-Zhe Yang gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1029e885871eSZong-Zhe Yang break; 1030e885871eSZong-Zhe Yang default: 1031e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 1032e885871eSZong-Zhe Yang "bb gain op1db {0x%x:0x%x} with unknown type: %d\n", 1033e885871eSZong-Zhe Yang arg.addr, data, type); 1034e885871eSZong-Zhe Yang break; 1035e885871eSZong-Zhe Yang } 1036e885871eSZong-Zhe Yang } 1037e885871eSZong-Zhe Yang 1038e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev, 1039e885871eSZong-Zhe Yang const struct rtw89_reg2_def *reg, 1040e885871eSZong-Zhe Yang enum rtw89_rf_path rf_path, 1041e885871eSZong-Zhe Yang void *extra_data) 1042e885871eSZong-Zhe Yang { 1043e885871eSZong-Zhe Yang const struct rtw89_chip_info *chip = rtwdev->chip; 1044e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr }; 1045c6a9d360SPing-Ke Shih struct rtw89_efuse *efuse = &rtwdev->efuse; 1046e885871eSZong-Zhe Yang 1047e885871eSZong-Zhe Yang if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR) 1048e885871eSZong-Zhe Yang return; 1049e885871eSZong-Zhe Yang 1050e885871eSZong-Zhe Yang if (arg.path >= chip->rf_path_num) 1051e885871eSZong-Zhe Yang return; 1052e885871eSZong-Zhe Yang 1053e885871eSZong-Zhe Yang if (arg.addr >= 0xf9 && arg.addr <= 0xfe) { 1054e885871eSZong-Zhe Yang rtw89_warn(rtwdev, "bb gain table with flow ctrl\n"); 1055e885871eSZong-Zhe Yang return; 1056e885871eSZong-Zhe Yang } 1057e885871eSZong-Zhe Yang 1058e885871eSZong-Zhe Yang switch (arg.cfg_type) { 1059e885871eSZong-Zhe Yang case 0: 1060e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data); 1061e885871eSZong-Zhe Yang break; 1062e885871eSZong-Zhe Yang case 1: 1063e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data); 1064e885871eSZong-Zhe Yang break; 1065e885871eSZong-Zhe Yang case 2: 1066e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data); 1067e885871eSZong-Zhe Yang break; 1068e885871eSZong-Zhe Yang case 3: 1069e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data); 1070e885871eSZong-Zhe Yang break; 1071c6a9d360SPing-Ke Shih case 4: 1072c6a9d360SPing-Ke Shih /* This cfg_type is only used by rfe_type >= 50 with eFEM */ 1073c6a9d360SPing-Ke Shih if (efuse->rfe_type < 50) 1074c6a9d360SPing-Ke Shih break; 1075c6a9d360SPing-Ke Shih fallthrough; 1076e885871eSZong-Zhe Yang default: 1077e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 1078e885871eSZong-Zhe Yang "bb gain {0x%x:0x%x} with unknown cfg type: %d\n", 1079e885871eSZong-Zhe Yang arg.addr, reg->data, arg.cfg_type); 1080e885871eSZong-Zhe Yang break; 1081e885871eSZong-Zhe Yang } 1082e885871eSZong-Zhe Yang } 1083e885871eSZong-Zhe Yang 1084e3ec7017SPing-Ke Shih static void 1085e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev, 1086e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1087e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1088e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 1089e3ec7017SPing-Ke Shih { 1090e3ec7017SPing-Ke Shih u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE; 1091e3ec7017SPing-Ke Shih u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE; 1092e3ec7017SPing-Ke Shih 109330101812SPing-Ke Shih if (page >= RTW89_H2C_RF_PAGE_NUM) { 109430101812SPing-Ke Shih rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d", 109530101812SPing-Ke Shih rf_path, info->curr_idx); 109630101812SPing-Ke Shih return; 109730101812SPing-Ke Shih } 109830101812SPing-Ke Shih 1099e3ec7017SPing-Ke Shih info->rtw89_phy_config_rf_h2c[page][idx] = 1100e3ec7017SPing-Ke Shih cpu_to_le32((reg->addr << 20) | reg->data); 1101e3ec7017SPing-Ke Shih info->curr_idx++; 1102e3ec7017SPing-Ke Shih } 1103e3ec7017SPing-Ke Shih 1104e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev, 1105e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 1106e3ec7017SPing-Ke Shih { 110730101812SPing-Ke Shih u16 remain = info->curr_idx; 110830101812SPing-Ke Shih u16 len = 0; 1109e3ec7017SPing-Ke Shih u8 i; 1110e3ec7017SPing-Ke Shih int ret = 0; 1111e3ec7017SPing-Ke Shih 111230101812SPing-Ke Shih if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) { 1113e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, 111430101812SPing-Ke Shih "rf reg h2c total len %d larger than %d\n", 111530101812SPing-Ke Shih remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE); 111630101812SPing-Ke Shih ret = -EINVAL; 111730101812SPing-Ke Shih goto out; 1118e3ec7017SPing-Ke Shih } 1119e3ec7017SPing-Ke Shih 112030101812SPing-Ke Shih for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) { 112130101812SPing-Ke Shih len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain; 112230101812SPing-Ke Shih ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i); 1123e3ec7017SPing-Ke Shih if (ret) 112430101812SPing-Ke Shih goto out; 1125e3ec7017SPing-Ke Shih } 112630101812SPing-Ke Shih out: 1127e3ec7017SPing-Ke Shih info->curr_idx = 0; 1128e3ec7017SPing-Ke Shih 112930101812SPing-Ke Shih return ret; 1130e3ec7017SPing-Ke Shih } 1131e3ec7017SPing-Ke Shih 1132d9112042SChih-Kang Chang static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev, 1133d9112042SChih-Kang Chang const struct rtw89_reg2_def *reg, 1134d9112042SChih-Kang Chang enum rtw89_rf_path rf_path, 1135d9112042SChih-Kang Chang void *extra_data) 1136d9112042SChih-Kang Chang { 1137d9112042SChih-Kang Chang u32 addr = reg->addr; 1138d9112042SChih-Kang Chang 1139d9112042SChih-Kang Chang if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb || 1140d9112042SChih-Kang Chang addr == 0xfa || addr == 0xf9) 1141d9112042SChih-Kang Chang return; 1142d9112042SChih-Kang Chang 1143d9112042SChih-Kang Chang if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100) 1144d9112042SChih-Kang Chang return; 1145d9112042SChih-Kang Chang 1146d9112042SChih-Kang Chang rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1147d9112042SChih-Kang Chang (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1148d9112042SChih-Kang Chang } 1149d9112042SChih-Kang Chang 1150e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev, 1151e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1152e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1153e3ec7017SPing-Ke Shih void *extra_data) 1154e3ec7017SPing-Ke Shih { 1155e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) { 1156e3ec7017SPing-Ke Shih mdelay(50); 1157e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfd) { 1158e3ec7017SPing-Ke Shih mdelay(5); 1159e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfc) { 1160e3ec7017SPing-Ke Shih mdelay(1); 1161e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfb) { 1162e3ec7017SPing-Ke Shih udelay(50); 1163e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfa) { 1164e3ec7017SPing-Ke Shih udelay(5); 1165e3ec7017SPing-Ke Shih } else if (reg->addr == 0xf9) { 1166e3ec7017SPing-Ke Shih udelay(1); 1167e3ec7017SPing-Ke Shih } else { 1168e3ec7017SPing-Ke Shih rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data); 1169e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1170e3ec7017SPing-Ke Shih (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1171e3ec7017SPing-Ke Shih } 1172e3ec7017SPing-Ke Shih } 1173e3ec7017SPing-Ke Shih 11742a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev, 11752a5f2b32SPing-Ke Shih const struct rtw89_reg2_def *reg, 11762a5f2b32SPing-Ke Shih enum rtw89_rf_path rf_path, 11772a5f2b32SPing-Ke Shih void *extra_data) 11782a5f2b32SPing-Ke Shih { 11792a5f2b32SPing-Ke Shih rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data); 11802a5f2b32SPing-Ke Shih 11812a5f2b32SPing-Ke Shih if (reg->addr < 0x100) 11822a5f2b32SPing-Ke Shih return; 11832a5f2b32SPing-Ke Shih 11842a5f2b32SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 11852a5f2b32SPing-Ke Shih (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 11862a5f2b32SPing-Ke Shih } 11872a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1); 11882a5f2b32SPing-Ke Shih 1189e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev, 1190e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 1191e3ec7017SPing-Ke Shih u32 *headline_size, u32 *headline_idx, 1192e3ec7017SPing-Ke Shih u8 rfe, u8 cv) 1193e3ec7017SPing-Ke Shih { 1194e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 1195e3ec7017SPing-Ke Shih u32 headline; 1196e3ec7017SPing-Ke Shih u32 compare, target; 1197e3ec7017SPing-Ke Shih u8 rfe_para, cv_para; 1198e3ec7017SPing-Ke Shih u8 cv_max = 0; 1199e3ec7017SPing-Ke Shih bool case_matched = false; 1200e3ec7017SPing-Ke Shih u32 i; 1201e3ec7017SPing-Ke Shih 1202e3ec7017SPing-Ke Shih for (i = 0; i < table->n_regs; i++) { 1203e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1204e3ec7017SPing-Ke Shih headline = get_phy_headline(reg->addr); 1205e3ec7017SPing-Ke Shih if (headline != PHY_HEADLINE_VALID) 1206e3ec7017SPing-Ke Shih break; 1207e3ec7017SPing-Ke Shih } 1208e3ec7017SPing-Ke Shih *headline_size = i; 1209e3ec7017SPing-Ke Shih if (*headline_size == 0) 1210e3ec7017SPing-Ke Shih return 0; 1211e3ec7017SPing-Ke Shih 1212e3ec7017SPing-Ke Shih /* case 1: RFE match, CV match */ 1213e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, cv); 1214e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1215e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1216e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1217e3ec7017SPing-Ke Shih if (target == compare) { 1218e3ec7017SPing-Ke Shih *headline_idx = i; 1219e3ec7017SPing-Ke Shih return 0; 1220e3ec7017SPing-Ke Shih } 1221e3ec7017SPing-Ke Shih } 1222e3ec7017SPing-Ke Shih 1223e3ec7017SPing-Ke Shih /* case 2: RFE match, CV don't care */ 1224e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, PHY_COND_DONT_CARE); 1225e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1226e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1227e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1228e3ec7017SPing-Ke Shih if (target == compare) { 1229e3ec7017SPing-Ke Shih *headline_idx = i; 1230e3ec7017SPing-Ke Shih return 0; 1231e3ec7017SPing-Ke Shih } 1232e3ec7017SPing-Ke Shih } 1233e3ec7017SPing-Ke Shih 1234e3ec7017SPing-Ke Shih /* case 3: RFE match, CV max in table */ 1235e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1236e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1237e3ec7017SPing-Ke Shih rfe_para = get_phy_cond_rfe(reg->addr); 1238e3ec7017SPing-Ke Shih cv_para = get_phy_cond_cv(reg->addr); 1239e3ec7017SPing-Ke Shih if (rfe_para == rfe) { 1240e3ec7017SPing-Ke Shih if (cv_para >= cv_max) { 1241e3ec7017SPing-Ke Shih cv_max = cv_para; 1242e3ec7017SPing-Ke Shih *headline_idx = i; 1243e3ec7017SPing-Ke Shih case_matched = true; 1244e3ec7017SPing-Ke Shih } 1245e3ec7017SPing-Ke Shih } 1246e3ec7017SPing-Ke Shih } 1247e3ec7017SPing-Ke Shih 1248e3ec7017SPing-Ke Shih if (case_matched) 1249e3ec7017SPing-Ke Shih return 0; 1250e3ec7017SPing-Ke Shih 1251e3ec7017SPing-Ke Shih /* case 4: RFE don't care, 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 == PHY_COND_DONT_CARE) { 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 return -EINVAL; 1269e3ec7017SPing-Ke Shih } 1270e3ec7017SPing-Ke Shih 1271e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev, 1272e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 1273e3ec7017SPing-Ke Shih void (*config)(struct rtw89_dev *rtwdev, 1274e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1275e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1276e3ec7017SPing-Ke Shih void *data), 1277e3ec7017SPing-Ke Shih void *extra_data) 1278e3ec7017SPing-Ke Shih { 1279e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 1280e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path = table->rf_path; 1281e3ec7017SPing-Ke Shih u8 rfe = rtwdev->efuse.rfe_type; 1282e3ec7017SPing-Ke Shih u8 cv = rtwdev->hal.cv; 1283e3ec7017SPing-Ke Shih u32 i; 1284e3ec7017SPing-Ke Shih u32 headline_size = 0, headline_idx = 0; 1285e3ec7017SPing-Ke Shih u32 target = 0, cfg_target; 1286e3ec7017SPing-Ke Shih u8 cond; 1287e3ec7017SPing-Ke Shih bool is_matched = true; 1288e3ec7017SPing-Ke Shih bool target_found = false; 1289e3ec7017SPing-Ke Shih int ret; 1290e3ec7017SPing-Ke Shih 1291e3ec7017SPing-Ke Shih ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size, 1292e3ec7017SPing-Ke Shih &headline_idx, rfe, cv); 1293e3ec7017SPing-Ke Shih if (ret) { 1294e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv); 1295e3ec7017SPing-Ke Shih return; 1296e3ec7017SPing-Ke Shih } 1297e3ec7017SPing-Ke Shih 1298e3ec7017SPing-Ke Shih cfg_target = get_phy_target(table->regs[headline_idx].addr); 1299e3ec7017SPing-Ke Shih for (i = headline_size; i < table->n_regs; i++) { 1300e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1301e3ec7017SPing-Ke Shih cond = get_phy_cond(reg->addr); 1302e3ec7017SPing-Ke Shih switch (cond) { 1303e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_IF: 1304e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELIF: 1305e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1306e3ec7017SPing-Ke Shih break; 1307e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELSE: 1308e3ec7017SPing-Ke Shih is_matched = false; 1309e3ec7017SPing-Ke Shih if (!target_found) { 1310e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "failed to load CR %x/%x\n", 1311e3ec7017SPing-Ke Shih reg->addr, reg->data); 1312e3ec7017SPing-Ke Shih return; 1313e3ec7017SPing-Ke Shih } 1314e3ec7017SPing-Ke Shih break; 1315e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_END: 1316e3ec7017SPing-Ke Shih is_matched = true; 1317e3ec7017SPing-Ke Shih target_found = false; 1318e3ec7017SPing-Ke Shih break; 1319e3ec7017SPing-Ke Shih case PHY_COND_CHECK: 1320e3ec7017SPing-Ke Shih if (target_found) { 1321e3ec7017SPing-Ke Shih is_matched = false; 1322e3ec7017SPing-Ke Shih break; 1323e3ec7017SPing-Ke Shih } 1324e3ec7017SPing-Ke Shih 1325e3ec7017SPing-Ke Shih if (target == cfg_target) { 1326e3ec7017SPing-Ke Shih is_matched = true; 1327e3ec7017SPing-Ke Shih target_found = true; 1328e3ec7017SPing-Ke Shih } else { 1329e3ec7017SPing-Ke Shih is_matched = false; 1330e3ec7017SPing-Ke Shih target_found = false; 1331e3ec7017SPing-Ke Shih } 1332e3ec7017SPing-Ke Shih break; 1333e3ec7017SPing-Ke Shih default: 1334e3ec7017SPing-Ke Shih if (is_matched) 1335e3ec7017SPing-Ke Shih config(rtwdev, reg, rf_path, extra_data); 1336e3ec7017SPing-Ke Shih break; 1337e3ec7017SPing-Ke Shih } 1338e3ec7017SPing-Ke Shih } 1339e3ec7017SPing-Ke Shih } 1340e3ec7017SPing-Ke Shih 1341e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev) 1342e3ec7017SPing-Ke Shih { 1343e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1344e3ec7017SPing-Ke Shih const struct rtw89_phy_table *bb_table = chip->bb_table; 1345e885871eSZong-Zhe Yang const struct rtw89_phy_table *bb_gain_table = chip->bb_gain_table; 1346e3ec7017SPing-Ke Shih 1347e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL); 1348e3ec7017SPing-Ke Shih rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0); 1349e885871eSZong-Zhe Yang if (bb_gain_table) 1350e885871eSZong-Zhe Yang rtw89_phy_init_reg(rtwdev, bb_gain_table, 1351e885871eSZong-Zhe Yang rtw89_phy_config_bb_gain, NULL); 1352e3ec7017SPing-Ke Shih rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0); 1353e3ec7017SPing-Ke Shih } 1354e3ec7017SPing-Ke Shih 1355e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev) 1356e3ec7017SPing-Ke Shih { 1357e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, 0x8080, 0x4); 1358e3ec7017SPing-Ke Shih udelay(1); 1359e3ec7017SPing-Ke Shih return rtw89_phy_read32(rtwdev, 0x8080); 1360e3ec7017SPing-Ke Shih } 1361e3ec7017SPing-Ke Shih 1362d9112042SChih-Kang Chang void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio) 1363e3ec7017SPing-Ke Shih { 13642a5f2b32SPing-Ke Shih void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg, 13652a5f2b32SPing-Ke Shih enum rtw89_rf_path rf_path, void *data); 1366e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1367e3ec7017SPing-Ke Shih const struct rtw89_phy_table *rf_table; 1368e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *rf_reg_info; 1369e3ec7017SPing-Ke Shih u8 path; 1370e3ec7017SPing-Ke Shih 1371e3ec7017SPing-Ke Shih rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL); 1372e3ec7017SPing-Ke Shih if (!rf_reg_info) 1373e3ec7017SPing-Ke Shih return; 1374e3ec7017SPing-Ke Shih 1375e3ec7017SPing-Ke Shih for (path = RF_PATH_A; path < chip->rf_path_num; path++) { 1376e3ec7017SPing-Ke Shih rf_table = chip->rf_table[path]; 13772a5f2b32SPing-Ke Shih rf_reg_info->rf_path = rf_table->rf_path; 1378d9112042SChih-Kang Chang if (noio) 1379d9112042SChih-Kang Chang config = rtw89_phy_config_rf_reg_noio; 1380d9112042SChih-Kang Chang else 1381d9112042SChih-Kang Chang config = rf_table->config ? rf_table->config : 1382d9112042SChih-Kang Chang rtw89_phy_config_rf_reg; 13832a5f2b32SPing-Ke Shih rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info); 1384e3ec7017SPing-Ke Shih if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info)) 1385e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n", 13862a5f2b32SPing-Ke Shih rf_reg_info->rf_path); 1387e3ec7017SPing-Ke Shih } 1388e3ec7017SPing-Ke Shih kfree(rf_reg_info); 1389e3ec7017SPing-Ke Shih } 1390e3ec7017SPing-Ke Shih 1391e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev) 1392e3ec7017SPing-Ke Shih { 1393e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1394e3ec7017SPing-Ke Shih const struct rtw89_phy_table *nctl_table; 1395e3ec7017SPing-Ke Shih u32 val; 1396e3ec7017SPing-Ke Shih int ret; 1397e3ec7017SPing-Ke Shih 1398e3ec7017SPing-Ke Shih /* IQK/DPK clock & reset */ 1399d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3); 1400d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1); 1401d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000); 1402d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000); 1403d0c820ccSPing-Ke Shih if (chip->chip_id == RTL8852B) 1404d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2); 1405e3ec7017SPing-Ke Shih 1406e3ec7017SPing-Ke Shih /* check 0x8080 */ 1407d0c820ccSPing-Ke Shih rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8); 1408e3ec7017SPing-Ke Shih 1409e3ec7017SPing-Ke Shih ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10, 1410e3ec7017SPing-Ke Shih 1000, false, rtwdev); 1411e3ec7017SPing-Ke Shih if (ret) 1412e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "failed to poll nctl block\n"); 1413e3ec7017SPing-Ke Shih 1414e3ec7017SPing-Ke Shih nctl_table = chip->nctl_table; 1415e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL); 1416e3ec7017SPing-Ke Shih } 1417e3ec7017SPing-Ke Shih 1418e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr) 1419e3ec7017SPing-Ke Shih { 1420e3ec7017SPing-Ke Shih u32 phy_page = addr >> 8; 1421e3ec7017SPing-Ke Shih u32 ofst = 0; 1422e3ec7017SPing-Ke Shih 1423e3ec7017SPing-Ke Shih switch (phy_page) { 1424e3ec7017SPing-Ke Shih case 0x6: 1425e3ec7017SPing-Ke Shih case 0x7: 1426e3ec7017SPing-Ke Shih case 0x8: 1427e3ec7017SPing-Ke Shih case 0x9: 1428e3ec7017SPing-Ke Shih case 0xa: 1429e3ec7017SPing-Ke Shih case 0xb: 1430e3ec7017SPing-Ke Shih case 0xc: 1431e3ec7017SPing-Ke Shih case 0xd: 1432e3ec7017SPing-Ke Shih case 0x19: 1433e3ec7017SPing-Ke Shih case 0x1a: 1434e3ec7017SPing-Ke Shih case 0x1b: 1435e3ec7017SPing-Ke Shih ofst = 0x2000; 1436e3ec7017SPing-Ke Shih break; 1437e3ec7017SPing-Ke Shih default: 1438e3ec7017SPing-Ke Shih /* warning case */ 1439e3ec7017SPing-Ke Shih ofst = 0; 1440e3ec7017SPing-Ke Shih break; 1441e3ec7017SPing-Ke Shih } 1442e3ec7017SPing-Ke Shih 1443e3ec7017SPing-Ke Shih if (phy_page >= 0x40 && phy_page <= 0x4f) 1444e3ec7017SPing-Ke Shih ofst = 0x2000; 1445e3ec7017SPing-Ke Shih 1446e3ec7017SPing-Ke Shih return ofst; 1447e3ec7017SPing-Ke Shih } 1448e3ec7017SPing-Ke Shih 1449e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1450e3ec7017SPing-Ke Shih u32 data, enum rtw89_phy_idx phy_idx) 1451e3ec7017SPing-Ke Shih { 1452e3ec7017SPing-Ke Shih if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 1453e3ec7017SPing-Ke Shih addr += rtw89_phy0_phy1_offset(rtwdev, addr); 1454e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, addr, mask, data); 1455e3ec7017SPing-Ke Shih } 1456861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx); 1457e3ec7017SPing-Ke Shih 14586b069898SPing-Ke Shih u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 14596b069898SPing-Ke Shih enum rtw89_phy_idx phy_idx) 14606b069898SPing-Ke Shih { 14616b069898SPing-Ke Shih if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 14626b069898SPing-Ke Shih addr += rtw89_phy0_phy1_offset(rtwdev, addr); 14636b069898SPing-Ke Shih return rtw89_phy_read32_mask(rtwdev, addr, mask); 14646b069898SPing-Ke Shih } 14656b069898SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read32_idx); 14666b069898SPing-Ke Shih 1467e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1468e3ec7017SPing-Ke Shih u32 val) 1469e3ec7017SPing-Ke Shih { 1470e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0); 1471e3ec7017SPing-Ke Shih 1472e3ec7017SPing-Ke Shih if (!rtwdev->dbcc_en) 1473e3ec7017SPing-Ke Shih return; 1474e3ec7017SPing-Ke Shih 1475e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1); 1476e3ec7017SPing-Ke Shih } 1477e3ec7017SPing-Ke Shih 1478e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev, 1479e3ec7017SPing-Ke Shih const struct rtw89_phy_reg3_tbl *tbl) 1480e3ec7017SPing-Ke Shih { 1481e3ec7017SPing-Ke Shih const struct rtw89_reg3_def *reg3; 1482e3ec7017SPing-Ke Shih int i; 1483e3ec7017SPing-Ke Shih 1484e3ec7017SPing-Ke Shih for (i = 0; i < tbl->size; i++) { 1485e3ec7017SPing-Ke Shih reg3 = &tbl->reg3[i]; 1486e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data); 1487e3ec7017SPing-Ke Shih } 1488e3ec7017SPing-Ke Shih } 1489861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl); 1490e3ec7017SPing-Ke Shih 14919b43bd1aSZong-Zhe Yang static const u8 rtw89_rs_idx_max[] = { 1492e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = RTW89_RATE_CCK_MAX, 1493e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = RTW89_RATE_OFDM_MAX, 1494e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = RTW89_RATE_MCS_MAX, 1495e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_MAX, 1496e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_MAX, 1497e3ec7017SPing-Ke Shih }; 1498e3ec7017SPing-Ke Shih 14999b43bd1aSZong-Zhe Yang static const u8 rtw89_rs_nss_max[] = { 1500e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = 1, 1501e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = 1, 1502e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = RTW89_NSS_MAX, 1503e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_MAX, 1504e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = 1, 1505e3ec7017SPing-Ke Shih }; 1506e3ec7017SPing-Ke Shih 1507e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = { 1508e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck), 1509e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm), 1510e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs), 1511e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm), 1512e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset), 1513e3ec7017SPing-Ke Shih }; 1514e3ec7017SPing-Ke Shih 1515e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs]) 1516e3ec7017SPing-Ke Shih #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_max[rs] + (idx)) 1517e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \ 1518e3ec7017SPing-Ke Shih ((nss) < rtw89_rs_nss_max[rs] && (idx) < rtw89_rs_idx_max[rs]) 1519e3ec7017SPing-Ke Shih 1520e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev, 1521e3ec7017SPing-Ke Shih const struct rtw89_txpwr_table *tbl) 1522e3ec7017SPing-Ke Shih { 1523e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data; 1524e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size; 1525e3ec7017SPing-Ke Shih s8 *byr; 1526e3ec7017SPing-Ke Shih u32 data; 1527e3ec7017SPing-Ke Shih u8 i, idx; 1528e3ec7017SPing-Ke Shih 1529e3ec7017SPing-Ke Shih for (; cfg < end; cfg++) { 1530e3ec7017SPing-Ke Shih byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]); 1531e3ec7017SPing-Ke Shih data = cfg->data; 1532e3ec7017SPing-Ke Shih 1533e3ec7017SPing-Ke Shih for (i = 0; i < cfg->len; i++, data >>= 8) { 1534e3ec7017SPing-Ke Shih idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i)); 1535e3ec7017SPing-Ke Shih byr[idx] = (s8)(data & 0xff); 1536e3ec7017SPing-Ke Shih } 1537e3ec7017SPing-Ke Shih } 1538e3ec7017SPing-Ke Shih } 1539861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate); 1540e3ec7017SPing-Ke Shih 1541e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf) \ 1542e3ec7017SPing-Ke Shih ({ \ 1543e3ec7017SPing-Ke Shih const struct rtw89_chip_info *__c = (rtwdev)->chip; \ 1544e3ec7017SPing-Ke Shih (txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac); \ 1545e3ec7017SPing-Ke Shih }) 1546e3ec7017SPing-Ke Shih 15479b43bd1aSZong-Zhe Yang static 154807ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band, 1549e3ec7017SPing-Ke Shih const struct rtw89_rate_desc *rate_desc) 1550e3ec7017SPing-Ke Shih { 1551e3ec7017SPing-Ke Shih s8 *byr; 1552e3ec7017SPing-Ke Shih u8 idx; 1553e3ec7017SPing-Ke Shih 1554e3ec7017SPing-Ke Shih if (rate_desc->rs == RTW89_RS_CCK) 1555e3ec7017SPing-Ke Shih band = RTW89_BAND_2G; 1556e3ec7017SPing-Ke Shih 1557e3ec7017SPing-Ke Shih if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) { 1558e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 1559e3ec7017SPing-Ke Shih "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n", 1560e3ec7017SPing-Ke Shih rate_desc->rs, rate_desc->nss, rate_desc->idx); 1561e3ec7017SPing-Ke Shih 1562e3ec7017SPing-Ke Shih return 0; 1563e3ec7017SPing-Ke Shih } 1564e3ec7017SPing-Ke Shih 1565e3ec7017SPing-Ke Shih byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]); 1566e3ec7017SPing-Ke Shih idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx); 1567e3ec7017SPing-Ke Shih 1568e3ec7017SPing-Ke Shih return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]); 1569e3ec7017SPing-Ke Shih } 1570e3ec7017SPing-Ke Shih 1571ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g) 1572e3ec7017SPing-Ke Shih { 1573ac74f016SZong-Zhe Yang switch (channel_6g) { 1574ac74f016SZong-Zhe Yang case 1 ... 29: 1575ac74f016SZong-Zhe Yang return (channel_6g - 1) / 2; 1576ac74f016SZong-Zhe Yang case 33 ... 61: 1577ac74f016SZong-Zhe Yang return (channel_6g - 3) / 2; 1578ac74f016SZong-Zhe Yang case 65 ... 93: 1579ac74f016SZong-Zhe Yang return (channel_6g - 5) / 2; 1580ac74f016SZong-Zhe Yang case 97 ... 125: 1581ac74f016SZong-Zhe Yang return (channel_6g - 7) / 2; 1582ac74f016SZong-Zhe Yang case 129 ... 157: 1583ac74f016SZong-Zhe Yang return (channel_6g - 9) / 2; 1584ac74f016SZong-Zhe Yang case 161 ... 189: 1585ac74f016SZong-Zhe Yang return (channel_6g - 11) / 2; 1586ac74f016SZong-Zhe Yang case 193 ... 221: 1587ac74f016SZong-Zhe Yang return (channel_6g - 13) / 2; 1588ac74f016SZong-Zhe Yang case 225 ... 253: 1589ac74f016SZong-Zhe Yang return (channel_6g - 15) / 2; 1590ac74f016SZong-Zhe Yang default: 1591ac74f016SZong-Zhe Yang rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g); 1592ac74f016SZong-Zhe Yang return 0; 1593ac74f016SZong-Zhe Yang } 1594ac74f016SZong-Zhe Yang } 1595ac74f016SZong-Zhe Yang 1596ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel) 1597ac74f016SZong-Zhe Yang { 1598ac74f016SZong-Zhe Yang if (band == RTW89_BAND_6G) 1599ac74f016SZong-Zhe Yang return rtw89_channel_6g_to_idx(rtwdev, channel); 1600ac74f016SZong-Zhe Yang 1601e3ec7017SPing-Ke Shih switch (channel) { 1602e3ec7017SPing-Ke Shih case 1 ... 14: 1603e3ec7017SPing-Ke Shih return channel - 1; 1604e3ec7017SPing-Ke Shih case 36 ... 64: 1605e3ec7017SPing-Ke Shih return (channel - 36) / 2; 1606e3ec7017SPing-Ke Shih case 100 ... 144: 1607e3ec7017SPing-Ke Shih return ((channel - 100) / 2) + 15; 1608e3ec7017SPing-Ke Shih case 149 ... 177: 1609e3ec7017SPing-Ke Shih return ((channel - 149) / 2) + 38; 1610e3ec7017SPing-Ke Shih default: 1611e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown channel: %d\n", channel); 1612e3ec7017SPing-Ke Shih return 0; 1613e3ec7017SPing-Ke Shih } 1614e3ec7017SPing-Ke Shih } 1615e3ec7017SPing-Ke Shih 161607ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band, 1617e3ec7017SPing-Ke Shih u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch) 1618e3ec7017SPing-Ke Shih { 1619e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1620ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1621e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 1622e3ec7017SPing-Ke Shih s8 lmt = 0, sar; 1623e3ec7017SPing-Ke Shih 1624e3ec7017SPing-Ke Shih switch (band) { 1625e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 1626e3ec7017SPing-Ke Shih lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf][regd][ch_idx]; 162754257714SZong-Zhe Yang if (!lmt) 162854257714SZong-Zhe Yang lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf] 162954257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1630e3ec7017SPing-Ke Shih break; 1631e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 1632e3ec7017SPing-Ke Shih lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf][regd][ch_idx]; 163354257714SZong-Zhe Yang if (!lmt) 163454257714SZong-Zhe Yang lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf] 163554257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1636e3ec7017SPing-Ke Shih break; 1637ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 1638ac74f016SZong-Zhe Yang lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf][regd][ch_idx]; 1639ac74f016SZong-Zhe Yang if (!lmt) 1640ac74f016SZong-Zhe Yang lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf] 1641ac74f016SZong-Zhe Yang [RTW89_WW][ch_idx]; 1642ac74f016SZong-Zhe Yang break; 1643e3ec7017SPing-Ke Shih default: 1644e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1645e3ec7017SPing-Ke Shih return 0; 1646e3ec7017SPing-Ke Shih } 1647e3ec7017SPing-Ke Shih 1648e3ec7017SPing-Ke Shih lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt); 1649e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1650e3ec7017SPing-Ke Shih 1651e3ec7017SPing-Ke Shih return min(lmt, sar); 1652e3ec7017SPing-Ke Shih } 1653861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit); 1654e3ec7017SPing-Ke Shih 165507ef5f2fSZong-Zhe Yang #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch) \ 1656e3ec7017SPing-Ke Shih do { \ 1657e3ec7017SPing-Ke Shih u8 __i; \ 1658e3ec7017SPing-Ke Shih for (__i = 0; __i < RTW89_BF_NUM; __i++) \ 1659e3ec7017SPing-Ke Shih ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev, \ 166007ef5f2fSZong-Zhe Yang band, \ 1661e3ec7017SPing-Ke Shih bw, ntx, \ 1662e3ec7017SPing-Ke Shih rs, __i, \ 1663e3ec7017SPing-Ke Shih (ch)); \ 1664e3ec7017SPing-Ke Shih } while (0) 1665e3ec7017SPing-Ke Shih 1666e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev, 1667e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 166807ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1669e3ec7017SPing-Ke Shih { 167007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 1671e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 167207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 1673e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 167407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 1675e3ec7017SPing-Ke Shih ntx, RTW89_RS_OFDM, ch); 167607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 167707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1678e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1679e3ec7017SPing-Ke Shih } 1680e3ec7017SPing-Ke Shih 1681e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev, 1682e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 168307ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 1684e3ec7017SPing-Ke Shih { 168507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 1686e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch - 2); 168707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 1688e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 168907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 169094b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 169107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 169207ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1693e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 169407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 169507ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1696e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 169707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 169807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1699e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1700e3ec7017SPing-Ke Shih } 1701e3ec7017SPing-Ke Shih 1702e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev, 1703e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 170407ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 1705e3ec7017SPing-Ke Shih { 1706e3ec7017SPing-Ke Shih s8 val_0p5_n[RTW89_BF_NUM]; 1707e3ec7017SPing-Ke Shih s8 val_0p5_p[RTW89_BF_NUM]; 1708e3ec7017SPing-Ke Shih u8 i; 1709e3ec7017SPing-Ke Shih 171007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 171194b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 171207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 171307ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1714e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 6); 171507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 171607ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1717e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 171807ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 171907ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1720e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 172107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 172207ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1723e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 6); 172407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 172507ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1726e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 172707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 172807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1729e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 173007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 173107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 1732e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1733e3ec7017SPing-Ke Shih 173407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 1735e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 173607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 1737e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 1738e3ec7017SPing-Ke Shih 1739e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_BF_NUM; i++) 1740e3ec7017SPing-Ke Shih lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 1741e3ec7017SPing-Ke Shih } 1742e3ec7017SPing-Ke Shih 174394b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev, 174494b70cafSZong-Zhe Yang struct rtw89_txpwr_limit *lmt, 174507ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 174694b70cafSZong-Zhe Yang { 174794b70cafSZong-Zhe Yang s8 val_0p5_n[RTW89_BF_NUM]; 174894b70cafSZong-Zhe Yang s8 val_0p5_p[RTW89_BF_NUM]; 174994b70cafSZong-Zhe Yang s8 val_2p5_n[RTW89_BF_NUM]; 175094b70cafSZong-Zhe Yang s8 val_2p5_p[RTW89_BF_NUM]; 175194b70cafSZong-Zhe Yang u8 i; 175294b70cafSZong-Zhe Yang 175394b70cafSZong-Zhe Yang /* fill ofdm section */ 175407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 175594b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 175694b70cafSZong-Zhe Yang 175794b70cafSZong-Zhe Yang /* fill mcs 20m section */ 175807ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 175907ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 176094b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 14); 176107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 176207ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 176394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 10); 176407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 176507ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 176694b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 6); 176707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 176807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 176994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 2); 177007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band, 177107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 177294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 2); 177307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band, 177407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 177594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 6); 177607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band, 177707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 177894b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 10); 177907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band, 178007ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 178194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 14); 178294b70cafSZong-Zhe Yang 178394b70cafSZong-Zhe Yang /* fill mcs 40m section */ 178407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 178507ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 178694b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 12); 178707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 178807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 178994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 179007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band, 179107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 179294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 179307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band, 179407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 179594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 12); 179694b70cafSZong-Zhe Yang 179794b70cafSZong-Zhe Yang /* fill mcs 80m section */ 179807ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 179907ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 180094b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 180107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band, 180207ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 180394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 180494b70cafSZong-Zhe Yang 180594b70cafSZong-Zhe Yang /* fill mcs 160m section */ 180607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band, 180707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_160, 180894b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch); 180994b70cafSZong-Zhe Yang 181094b70cafSZong-Zhe Yang /* fill mcs 40m 0p5 section */ 181107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 181294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 181307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 181494b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 181594b70cafSZong-Zhe Yang 181694b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 181794b70cafSZong-Zhe Yang lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 181894b70cafSZong-Zhe Yang 181994b70cafSZong-Zhe Yang /* fill mcs 40m 2p5 section */ 182007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40, 182194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 182207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40, 182394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 182494b70cafSZong-Zhe Yang 182594b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 182694b70cafSZong-Zhe Yang lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]); 182794b70cafSZong-Zhe Yang } 182894b70cafSZong-Zhe Yang 18299b43bd1aSZong-Zhe Yang static 1830e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev, 183107ef5f2fSZong-Zhe Yang const struct rtw89_chan *chan, 1832e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 1833e3ec7017SPing-Ke Shih u8 ntx) 1834e3ec7017SPing-Ke Shih { 183507ef5f2fSZong-Zhe Yang u8 band = chan->band_type; 1836cbb145b9SZong-Zhe Yang u8 pri_ch = chan->primary_channel; 1837cbb145b9SZong-Zhe Yang u8 ch = chan->channel; 1838cbb145b9SZong-Zhe Yang u8 bw = chan->band_width; 1839e3ec7017SPing-Ke Shih 1840e3ec7017SPing-Ke Shih memset(lmt, 0, sizeof(*lmt)); 1841e3ec7017SPing-Ke Shih 1842e3ec7017SPing-Ke Shih switch (bw) { 1843e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 184407ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch); 1845e3ec7017SPing-Ke Shih break; 1846e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 184707ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch, 184807ef5f2fSZong-Zhe Yang pri_ch); 1849e3ec7017SPing-Ke Shih break; 1850e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 185107ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch, 185207ef5f2fSZong-Zhe Yang pri_ch); 185394b70cafSZong-Zhe Yang break; 185494b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 185507ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch, 185607ef5f2fSZong-Zhe Yang pri_ch); 1857e3ec7017SPing-Ke Shih break; 1858e3ec7017SPing-Ke Shih } 1859e3ec7017SPing-Ke Shih } 1860e3ec7017SPing-Ke Shih 186107ef5f2fSZong-Zhe Yang static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band, 1862e3ec7017SPing-Ke Shih u8 ru, u8 ntx, u8 ch) 1863e3ec7017SPing-Ke Shih { 1864e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1865ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1866e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 1867e3ec7017SPing-Ke Shih s8 lmt_ru = 0, sar; 1868e3ec7017SPing-Ke Shih 1869e3ec7017SPing-Ke Shih switch (band) { 1870e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 1871e3ec7017SPing-Ke Shih lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx][regd][ch_idx]; 187254257714SZong-Zhe Yang if (!lmt_ru) 187354257714SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx] 187454257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1875e3ec7017SPing-Ke Shih break; 1876e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 1877e3ec7017SPing-Ke Shih lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx][regd][ch_idx]; 187854257714SZong-Zhe Yang if (!lmt_ru) 187954257714SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx] 188054257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1881e3ec7017SPing-Ke Shih break; 1882ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 1883ac74f016SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx][regd][ch_idx]; 1884ac74f016SZong-Zhe Yang if (!lmt_ru) 1885ac74f016SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx] 1886ac74f016SZong-Zhe Yang [RTW89_WW][ch_idx]; 1887ac74f016SZong-Zhe Yang break; 1888e3ec7017SPing-Ke Shih default: 1889e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1890e3ec7017SPing-Ke Shih return 0; 1891e3ec7017SPing-Ke Shih } 1892e3ec7017SPing-Ke Shih 1893e3ec7017SPing-Ke Shih lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru); 1894e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1895e3ec7017SPing-Ke Shih 1896e3ec7017SPing-Ke Shih return min(lmt_ru, sar); 1897e3ec7017SPing-Ke Shih } 1898e3ec7017SPing-Ke Shih 1899e3ec7017SPing-Ke Shih static void 1900e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev, 1901e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 190207ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1903e3ec7017SPing-Ke Shih { 190407ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 190507ef5f2fSZong-Zhe Yang RTW89_RU26, 1906e3ec7017SPing-Ke Shih ntx, ch); 190707ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 190807ef5f2fSZong-Zhe Yang RTW89_RU52, 1909e3ec7017SPing-Ke Shih ntx, ch); 191007ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 191107ef5f2fSZong-Zhe Yang RTW89_RU106, 1912e3ec7017SPing-Ke Shih ntx, ch); 1913e3ec7017SPing-Ke Shih } 1914e3ec7017SPing-Ke Shih 1915e3ec7017SPing-Ke Shih static void 1916e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev, 1917e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 191807ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1919e3ec7017SPing-Ke Shih { 192007ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 192107ef5f2fSZong-Zhe Yang RTW89_RU26, 1922e3ec7017SPing-Ke Shih ntx, ch - 2); 192307ef5f2fSZong-Zhe Yang lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 192407ef5f2fSZong-Zhe Yang RTW89_RU26, 1925e3ec7017SPing-Ke Shih ntx, ch + 2); 192607ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 192707ef5f2fSZong-Zhe Yang RTW89_RU52, 1928e3ec7017SPing-Ke Shih ntx, ch - 2); 192907ef5f2fSZong-Zhe Yang lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 193007ef5f2fSZong-Zhe Yang RTW89_RU52, 1931e3ec7017SPing-Ke Shih ntx, ch + 2); 193207ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 193307ef5f2fSZong-Zhe Yang RTW89_RU106, 1934e3ec7017SPing-Ke Shih ntx, ch - 2); 193507ef5f2fSZong-Zhe Yang lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 193607ef5f2fSZong-Zhe Yang RTW89_RU106, 1937e3ec7017SPing-Ke Shih ntx, ch + 2); 1938e3ec7017SPing-Ke Shih } 1939e3ec7017SPing-Ke Shih 1940e3ec7017SPing-Ke Shih static void 1941e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev, 1942e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 194307ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1944e3ec7017SPing-Ke Shih { 194507ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 194607ef5f2fSZong-Zhe Yang RTW89_RU26, 1947e3ec7017SPing-Ke Shih ntx, ch - 6); 194807ef5f2fSZong-Zhe Yang lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 194907ef5f2fSZong-Zhe Yang RTW89_RU26, 1950e3ec7017SPing-Ke Shih ntx, ch - 2); 195107ef5f2fSZong-Zhe Yang lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 195207ef5f2fSZong-Zhe Yang RTW89_RU26, 1953e3ec7017SPing-Ke Shih ntx, ch + 2); 195407ef5f2fSZong-Zhe Yang lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 195507ef5f2fSZong-Zhe Yang RTW89_RU26, 1956e3ec7017SPing-Ke Shih ntx, ch + 6); 195707ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 195807ef5f2fSZong-Zhe Yang RTW89_RU52, 1959e3ec7017SPing-Ke Shih ntx, ch - 6); 196007ef5f2fSZong-Zhe Yang lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196107ef5f2fSZong-Zhe Yang RTW89_RU52, 1962e3ec7017SPing-Ke Shih ntx, ch - 2); 196307ef5f2fSZong-Zhe Yang lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196407ef5f2fSZong-Zhe Yang RTW89_RU52, 1965e3ec7017SPing-Ke Shih ntx, ch + 2); 196607ef5f2fSZong-Zhe Yang lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196707ef5f2fSZong-Zhe Yang RTW89_RU52, 1968e3ec7017SPing-Ke Shih ntx, ch + 6); 196907ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197007ef5f2fSZong-Zhe Yang RTW89_RU106, 1971e3ec7017SPing-Ke Shih ntx, ch - 6); 197207ef5f2fSZong-Zhe Yang lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197307ef5f2fSZong-Zhe Yang RTW89_RU106, 1974e3ec7017SPing-Ke Shih ntx, ch - 2); 197507ef5f2fSZong-Zhe Yang lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197607ef5f2fSZong-Zhe Yang RTW89_RU106, 1977e3ec7017SPing-Ke Shih ntx, ch + 2); 197807ef5f2fSZong-Zhe Yang lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197907ef5f2fSZong-Zhe Yang RTW89_RU106, 1980e3ec7017SPing-Ke Shih ntx, ch + 6); 1981e3ec7017SPing-Ke Shih } 1982e3ec7017SPing-Ke Shih 198394b70cafSZong-Zhe Yang static void 198494b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev, 198594b70cafSZong-Zhe Yang struct rtw89_txpwr_limit_ru *lmt_ru, 198607ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 198794b70cafSZong-Zhe Yang { 198894b70cafSZong-Zhe Yang static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 }; 198994b70cafSZong-Zhe Yang int i; 199094b70cafSZong-Zhe Yang 199194b70cafSZong-Zhe Yang static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM); 199294b70cafSZong-Zhe Yang for (i = 0; i < RTW89_RU_SEC_NUM; i++) { 199307ef5f2fSZong-Zhe Yang lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 199494b70cafSZong-Zhe Yang RTW89_RU26, 199594b70cafSZong-Zhe Yang ntx, 199694b70cafSZong-Zhe Yang ch + ofst[i]); 199707ef5f2fSZong-Zhe Yang lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 199894b70cafSZong-Zhe Yang RTW89_RU52, 199994b70cafSZong-Zhe Yang ntx, 200094b70cafSZong-Zhe Yang ch + ofst[i]); 200107ef5f2fSZong-Zhe Yang lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 200294b70cafSZong-Zhe Yang RTW89_RU106, 200394b70cafSZong-Zhe Yang ntx, 200494b70cafSZong-Zhe Yang ch + ofst[i]); 200594b70cafSZong-Zhe Yang } 200694b70cafSZong-Zhe Yang } 200794b70cafSZong-Zhe Yang 20089b43bd1aSZong-Zhe Yang static 2009e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev, 201007ef5f2fSZong-Zhe Yang const struct rtw89_chan *chan, 2011e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 2012e3ec7017SPing-Ke Shih u8 ntx) 2013e3ec7017SPing-Ke Shih { 201407ef5f2fSZong-Zhe Yang u8 band = chan->band_type; 2015cbb145b9SZong-Zhe Yang u8 ch = chan->channel; 2016cbb145b9SZong-Zhe Yang u8 bw = chan->band_width; 2017e3ec7017SPing-Ke Shih 2018e3ec7017SPing-Ke Shih memset(lmt_ru, 0, sizeof(*lmt_ru)); 2019e3ec7017SPing-Ke Shih 2020e3ec7017SPing-Ke Shih switch (bw) { 2021e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 202207ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx, 202307ef5f2fSZong-Zhe Yang ch); 2024e3ec7017SPing-Ke Shih break; 2025e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 202607ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx, 202707ef5f2fSZong-Zhe Yang ch); 2028e3ec7017SPing-Ke Shih break; 2029e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 203007ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx, 203107ef5f2fSZong-Zhe Yang ch); 2032e3ec7017SPing-Ke Shih break; 203394b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 203407ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx, 203507ef5f2fSZong-Zhe Yang ch); 203694b70cafSZong-Zhe Yang break; 2037e3ec7017SPing-Ke Shih } 2038e3ec7017SPing-Ke Shih } 20399b43bd1aSZong-Zhe Yang 20409b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev, 20419b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 20429b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 20439b43bd1aSZong-Zhe Yang { 20449b43bd1aSZong-Zhe Yang static const u8 rs[] = { 20459b43bd1aSZong-Zhe Yang RTW89_RS_CCK, 20469b43bd1aSZong-Zhe Yang RTW89_RS_OFDM, 20479b43bd1aSZong-Zhe Yang RTW89_RS_MCS, 20489b43bd1aSZong-Zhe Yang RTW89_RS_HEDCM, 20499b43bd1aSZong-Zhe Yang }; 20509b43bd1aSZong-Zhe Yang struct rtw89_rate_desc cur; 20519b43bd1aSZong-Zhe Yang u8 band = chan->band_type; 20529b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 20539b43bd1aSZong-Zhe Yang u32 addr, val; 20549b43bd1aSZong-Zhe Yang s8 v[4] = {}; 20559b43bd1aSZong-Zhe Yang u8 i; 20569b43bd1aSZong-Zhe Yang 20579b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 20589b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr byrate with ch=%d\n", ch); 20599b43bd1aSZong-Zhe Yang 20609b43bd1aSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_CCK] % 4); 20619b43bd1aSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_OFDM] % 4); 20629b43bd1aSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_MCS] % 4); 20639b43bd1aSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_HEDCM] % 4); 20649b43bd1aSZong-Zhe Yang 20659b43bd1aSZong-Zhe Yang addr = R_AX_PWR_BY_RATE; 20669b43bd1aSZong-Zhe Yang for (cur.nss = 0; cur.nss <= RTW89_NSS_2; cur.nss++) { 20679b43bd1aSZong-Zhe Yang for (i = 0; i < ARRAY_SIZE(rs); i++) { 20689b43bd1aSZong-Zhe Yang if (cur.nss >= rtw89_rs_nss_max[rs[i]]) 20699b43bd1aSZong-Zhe Yang continue; 20709b43bd1aSZong-Zhe Yang 20719b43bd1aSZong-Zhe Yang cur.rs = rs[i]; 20729b43bd1aSZong-Zhe Yang for (cur.idx = 0; cur.idx < rtw89_rs_idx_max[rs[i]]; 20739b43bd1aSZong-Zhe Yang cur.idx++) { 20749b43bd1aSZong-Zhe Yang v[cur.idx % 4] = 20759b43bd1aSZong-Zhe Yang rtw89_phy_read_txpwr_byrate(rtwdev, 20769b43bd1aSZong-Zhe Yang band, 20779b43bd1aSZong-Zhe Yang &cur); 20789b43bd1aSZong-Zhe Yang 20799b43bd1aSZong-Zhe Yang if ((cur.idx + 1) % 4) 20809b43bd1aSZong-Zhe Yang continue; 20819b43bd1aSZong-Zhe Yang 20829b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), v[0]) | 20839b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), v[1]) | 20849b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), v[2]) | 20859b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), v[3]); 20869b43bd1aSZong-Zhe Yang 20879b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, 20889b43bd1aSZong-Zhe Yang val); 20899b43bd1aSZong-Zhe Yang addr += 4; 20909b43bd1aSZong-Zhe Yang } 20919b43bd1aSZong-Zhe Yang } 20929b43bd1aSZong-Zhe Yang } 20939b43bd1aSZong-Zhe Yang } 20949b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate); 20959b43bd1aSZong-Zhe Yang 20969b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev, 20979b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 20989b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 20999b43bd1aSZong-Zhe Yang { 21009b43bd1aSZong-Zhe Yang struct rtw89_rate_desc desc = { 21019b43bd1aSZong-Zhe Yang .nss = RTW89_NSS_1, 21029b43bd1aSZong-Zhe Yang .rs = RTW89_RS_OFFSET, 21039b43bd1aSZong-Zhe Yang }; 21049b43bd1aSZong-Zhe Yang u8 band = chan->band_type; 21059b43bd1aSZong-Zhe Yang s8 v[RTW89_RATE_OFFSET_MAX] = {}; 21069b43bd1aSZong-Zhe Yang u32 val; 21079b43bd1aSZong-Zhe Yang 21089b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n"); 21099b43bd1aSZong-Zhe Yang 21109b43bd1aSZong-Zhe Yang for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_MAX; desc.idx++) 21119b43bd1aSZong-Zhe Yang v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc); 21129b43bd1aSZong-Zhe Yang 21139b43bd1aSZong-Zhe Yang BUILD_BUG_ON(RTW89_RATE_OFFSET_MAX != 5); 21149b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(3, 0), v[0]) | 21159b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(7, 4), v[1]) | 21169b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(11, 8), v[2]) | 21179b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 12), v[3]) | 21189b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(19, 16), v[4]); 21199b43bd1aSZong-Zhe Yang 21209b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL, 21219b43bd1aSZong-Zhe Yang GENMASK(19, 0), val); 21229b43bd1aSZong-Zhe Yang } 21239b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset); 21249b43bd1aSZong-Zhe Yang 21259b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev, 21269b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 21279b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 21289b43bd1aSZong-Zhe Yang { 21299b43bd1aSZong-Zhe Yang struct rtw89_txpwr_limit lmt; 21309b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 21319b43bd1aSZong-Zhe Yang u8 bw = chan->band_width; 21329b43bd1aSZong-Zhe Yang const s8 *ptr; 21339b43bd1aSZong-Zhe Yang u32 addr, val; 21349b43bd1aSZong-Zhe Yang u8 i, j; 21359b43bd1aSZong-Zhe Yang 21369b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 21379b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw); 21389b43bd1aSZong-Zhe Yang 21399b43bd1aSZong-Zhe Yang BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) != 21409b43bd1aSZong-Zhe Yang RTW89_TXPWR_LMT_PAGE_SIZE); 21419b43bd1aSZong-Zhe Yang 21429b43bd1aSZong-Zhe Yang addr = R_AX_PWR_LMT; 21439b43bd1aSZong-Zhe Yang for (i = 0; i < RTW89_NTX_NUM; i++) { 21449b43bd1aSZong-Zhe Yang rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i); 21459b43bd1aSZong-Zhe Yang 21469b43bd1aSZong-Zhe Yang ptr = (s8 *)&lmt; 21479b43bd1aSZong-Zhe Yang for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE; 21489b43bd1aSZong-Zhe Yang j += 4, addr += 4, ptr += 4) { 21499b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 21509b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), ptr[1]) | 21519b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), ptr[2]) | 21529b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), ptr[3]); 21539b43bd1aSZong-Zhe Yang 21549b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 21559b43bd1aSZong-Zhe Yang } 21569b43bd1aSZong-Zhe Yang } 21579b43bd1aSZong-Zhe Yang } 21589b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit); 21599b43bd1aSZong-Zhe Yang 21609b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev, 21619b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 21629b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 21639b43bd1aSZong-Zhe Yang { 21649b43bd1aSZong-Zhe Yang struct rtw89_txpwr_limit_ru lmt_ru; 21659b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 21669b43bd1aSZong-Zhe Yang u8 bw = chan->band_width; 21679b43bd1aSZong-Zhe Yang const s8 *ptr; 21689b43bd1aSZong-Zhe Yang u32 addr, val; 21699b43bd1aSZong-Zhe Yang u8 i, j; 21709b43bd1aSZong-Zhe Yang 21719b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 21729b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw); 21739b43bd1aSZong-Zhe Yang 21749b43bd1aSZong-Zhe Yang BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) != 21759b43bd1aSZong-Zhe Yang RTW89_TXPWR_LMT_RU_PAGE_SIZE); 21769b43bd1aSZong-Zhe Yang 21779b43bd1aSZong-Zhe Yang addr = R_AX_PWR_RU_LMT; 21789b43bd1aSZong-Zhe Yang for (i = 0; i < RTW89_NTX_NUM; i++) { 21799b43bd1aSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i); 21809b43bd1aSZong-Zhe Yang 21819b43bd1aSZong-Zhe Yang ptr = (s8 *)&lmt_ru; 21829b43bd1aSZong-Zhe Yang for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE; 21839b43bd1aSZong-Zhe Yang j += 4, addr += 4, ptr += 4) { 21849b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 21859b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), ptr[1]) | 21869b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), ptr[2]) | 21879b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), ptr[3]); 21889b43bd1aSZong-Zhe Yang 21899b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 21909b43bd1aSZong-Zhe Yang } 21919b43bd1aSZong-Zhe Yang } 21929b43bd1aSZong-Zhe Yang } 21939b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru); 2194e3ec7017SPing-Ke Shih 2195e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data { 2196e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 2197e3ec7017SPing-Ke Shih struct sk_buff *c2h; 2198e3ec7017SPing-Ke Shih }; 2199e3ec7017SPing-Ke Shih 2200e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta) 2201e3ec7017SPing-Ke Shih { 2202e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data; 2203e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = ra_data->rtwdev; 2204e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2205e3ec7017SPing-Ke Shih struct rtw89_ra_report *ra_report = &rtwsta->ra_report; 2206e3ec7017SPing-Ke Shih struct sk_buff *c2h = ra_data->c2h; 2207e3ec7017SPing-Ke Shih u8 mode, rate, bw, giltf, mac_id; 22089a3a593cSPing-Ke Shih u16 legacy_bitrate; 22099a3a593cSPing-Ke Shih bool valid; 22100d466f05SPing-Ke Shih u8 mcs = 0; 2211e3ec7017SPing-Ke Shih 2212e3ec7017SPing-Ke Shih mac_id = RTW89_GET_PHY_C2H_RA_RPT_MACID(c2h->data); 2213e3ec7017SPing-Ke Shih if (mac_id != rtwsta->mac_id) 2214e3ec7017SPing-Ke Shih return; 2215e3ec7017SPing-Ke Shih 2216e3ec7017SPing-Ke Shih rate = RTW89_GET_PHY_C2H_RA_RPT_MCSNSS(c2h->data); 2217e3ec7017SPing-Ke Shih bw = RTW89_GET_PHY_C2H_RA_RPT_BW(c2h->data); 2218e3ec7017SPing-Ke Shih giltf = RTW89_GET_PHY_C2H_RA_RPT_GILTF(c2h->data); 2219e3ec7017SPing-Ke Shih mode = RTW89_GET_PHY_C2H_RA_RPT_MD_SEL(c2h->data); 2220e3ec7017SPing-Ke Shih 22219a3a593cSPing-Ke Shih if (mode == RTW89_RA_RPT_MODE_LEGACY) { 22229a3a593cSPing-Ke Shih valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate); 22239a3a593cSPing-Ke Shih if (!valid) 22249a3a593cSPing-Ke Shih return; 22259a3a593cSPing-Ke Shih } 22269a3a593cSPing-Ke Shih 22270d466f05SPing-Ke Shih memset(&ra_report->txrate, 0, sizeof(ra_report->txrate)); 22289a3a593cSPing-Ke Shih 2229e3ec7017SPing-Ke Shih switch (mode) { 2230e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_LEGACY: 22319a3a593cSPing-Ke Shih ra_report->txrate.legacy = legacy_bitrate; 2232e3ec7017SPing-Ke Shih break; 2233e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HT: 2234e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS; 223511fe4ccdSZong-Zhe Yang if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw)) 2236e3ec7017SPing-Ke Shih rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate), 2237e3ec7017SPing-Ke Shih FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate)); 2238e3ec7017SPing-Ke Shih else 2239e3ec7017SPing-Ke Shih rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate); 2240e3ec7017SPing-Ke Shih ra_report->txrate.mcs = rate; 2241e3ec7017SPing-Ke Shih if (giltf) 2242e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 22430d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs & 0x07; 2244e3ec7017SPing-Ke Shih break; 2245e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_VHT: 2246e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS; 2247e3ec7017SPing-Ke Shih ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate); 2248e3ec7017SPing-Ke Shih ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1; 2249e3ec7017SPing-Ke Shih if (giltf) 2250e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 22510d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs; 2252e3ec7017SPing-Ke Shih break; 2253e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HE: 2254e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS; 2255e3ec7017SPing-Ke Shih ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate); 2256e3ec7017SPing-Ke Shih ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1; 2257e3ec7017SPing-Ke Shih if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08) 2258e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8; 2259e3ec7017SPing-Ke Shih else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16) 2260e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6; 2261e3ec7017SPing-Ke Shih else 2262e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2; 22630d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs; 2264e3ec7017SPing-Ke Shih break; 2265e3ec7017SPing-Ke Shih } 2266e3ec7017SPing-Ke Shih 2267167044afSPing-Ke Shih ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw); 2268e3ec7017SPing-Ke Shih ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate); 2269e3ec7017SPing-Ke Shih ra_report->hw_rate = FIELD_PREP(RTW89_HW_RATE_MASK_MOD, mode) | 2270e3ec7017SPing-Ke Shih FIELD_PREP(RTW89_HW_RATE_MASK_VAL, rate); 22710d466f05SPing-Ke Shih ra_report->might_fallback_legacy = mcs <= 2; 22724c51541dSBenjamin Berg sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report); 22734c51541dSBenjamin Berg rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1; 2274e3ec7017SPing-Ke Shih } 2275e3ec7017SPing-Ke Shih 2276e3ec7017SPing-Ke Shih static void 2277e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2278e3ec7017SPing-Ke Shih { 2279e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data ra_data; 2280e3ec7017SPing-Ke Shih 2281e3ec7017SPing-Ke Shih ra_data.rtwdev = rtwdev; 2282e3ec7017SPing-Ke Shih ra_data.c2h = c2h; 2283e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 2284e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt_iter, 2285e3ec7017SPing-Ke Shih &ra_data); 2286e3ec7017SPing-Ke Shih } 2287e3ec7017SPing-Ke Shih 2288e3ec7017SPing-Ke Shih static 2289e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev, 2290e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = { 2291e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt, 2292e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL, 2293e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_TXSTS] = NULL, 2294e3ec7017SPing-Ke Shih }; 2295e3ec7017SPing-Ke Shih 2296e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb, 2297e3ec7017SPing-Ke Shih u32 len, u8 class, u8 func) 2298e3ec7017SPing-Ke Shih { 2299e3ec7017SPing-Ke Shih void (*handler)(struct rtw89_dev *rtwdev, 2300e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = NULL; 2301e3ec7017SPing-Ke Shih 2302e3ec7017SPing-Ke Shih switch (class) { 2303e3ec7017SPing-Ke Shih case RTW89_PHY_C2H_CLASS_RA: 2304e3ec7017SPing-Ke Shih if (func < RTW89_PHY_C2H_FUNC_RA_MAX) 2305e3ec7017SPing-Ke Shih handler = rtw89_phy_c2h_ra_handler[func]; 2306e3ec7017SPing-Ke Shih break; 23073b66519bSPing-Ke Shih case RTW89_PHY_C2H_CLASS_DM: 23083b66519bSPing-Ke Shih if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY) 23093b66519bSPing-Ke Shih return; 23103b66519bSPing-Ke Shih fallthrough; 2311e3ec7017SPing-Ke Shih default: 2312e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d not support\n", class); 2313e3ec7017SPing-Ke Shih return; 2314e3ec7017SPing-Ke Shih } 2315e3ec7017SPing-Ke Shih if (!handler) { 2316e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d func %d not support\n", class, 2317e3ec7017SPing-Ke Shih func); 2318e3ec7017SPing-Ke Shih return; 2319e3ec7017SPing-Ke Shih } 2320e3ec7017SPing-Ke Shih handler(rtwdev, skb, len); 2321e3ec7017SPing-Ke Shih } 2322e3ec7017SPing-Ke Shih 2323e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo) 2324e3ec7017SPing-Ke Shih { 2325e3ec7017SPing-Ke Shih u32 reg_mask; 2326e3ec7017SPing-Ke Shih 2327e3ec7017SPing-Ke Shih if (sc_xo) 2328e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XO_MASK; 2329e3ec7017SPing-Ke Shih else 2330e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XI_MASK; 2331e3ec7017SPing-Ke Shih 2332e3ec7017SPing-Ke Shih return (u8)rtw89_read32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask); 2333e3ec7017SPing-Ke Shih } 2334e3ec7017SPing-Ke Shih 2335e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo, 2336e3ec7017SPing-Ke Shih u8 val) 2337e3ec7017SPing-Ke Shih { 2338e3ec7017SPing-Ke Shih u32 reg_mask; 2339e3ec7017SPing-Ke Shih 2340e3ec7017SPing-Ke Shih if (sc_xo) 2341e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XO_MASK; 2342e3ec7017SPing-Ke Shih else 2343e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XI_MASK; 2344e3ec7017SPing-Ke Shih 2345e3ec7017SPing-Ke Shih rtw89_write32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask, val); 2346e3ec7017SPing-Ke Shih } 2347e3ec7017SPing-Ke Shih 2348e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev, 2349e3ec7017SPing-Ke Shih u8 crystal_cap, bool force) 2350e3ec7017SPing-Ke Shih { 2351e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 23528379fa61SYuan-Han Zhang const struct rtw89_chip_info *chip = rtwdev->chip; 2353e3ec7017SPing-Ke Shih u8 sc_xi_val, sc_xo_val; 2354e3ec7017SPing-Ke Shih 2355e3ec7017SPing-Ke Shih if (!force && cfo->crystal_cap == crystal_cap) 2356e3ec7017SPing-Ke Shih return; 2357e3ec7017SPing-Ke Shih crystal_cap = clamp_t(u8, crystal_cap, 0, 127); 23588379fa61SYuan-Han Zhang if (chip->chip_id == RTL8852A) { 2359e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap); 2360e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap); 2361e3ec7017SPing-Ke Shih sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true); 2362e3ec7017SPing-Ke Shih sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false); 23638379fa61SYuan-Han Zhang } else { 23648379fa61SYuan-Han Zhang rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, 23658379fa61SYuan-Han Zhang crystal_cap, XTAL_SC_XO_MASK); 23668379fa61SYuan-Han Zhang rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, 23678379fa61SYuan-Han Zhang crystal_cap, XTAL_SC_XI_MASK); 23688379fa61SYuan-Han Zhang rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val); 23698379fa61SYuan-Han Zhang rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val); 23708379fa61SYuan-Han Zhang } 2371e3ec7017SPing-Ke Shih cfo->crystal_cap = sc_xi_val; 2372e3ec7017SPing-Ke Shih cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap); 2373e3ec7017SPing-Ke Shih 2374e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val); 2375e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val); 2376e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n", 2377e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 2378e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n"); 2379e3ec7017SPing-Ke Shih } 2380e3ec7017SPing-Ke Shih 2381e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev) 2382e3ec7017SPing-Ke Shih { 2383e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2384e3ec7017SPing-Ke Shih u8 cap; 2385e3ec7017SPing-Ke Shih 2386e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK; 2387e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2388e3ec7017SPing-Ke Shih if (cfo->crystal_cap == cfo->def_x_cap) 2389e3ec7017SPing-Ke Shih return; 2390e3ec7017SPing-Ke Shih cap = cfo->crystal_cap; 2391e3ec7017SPing-Ke Shih cap += (cap > cfo->def_x_cap ? -1 : 1); 2392e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false); 2393e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2394e3ec7017SPing-Ke Shih "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap, 2395e3ec7017SPing-Ke Shih cfo->def_x_cap); 2396e3ec7017SPing-Ke Shih } 2397e3ec7017SPing-Ke Shih 2398e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo) 2399e3ec7017SPing-Ke Shih { 2400b7379148SYuan-Han Zhang const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp; 2401e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 2402e3ec7017SPing-Ke Shih s32 cfo_avg_312; 2403b7379148SYuan-Han Zhang s32 dcfo_comp_val; 2404b7379148SYuan-Han Zhang u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft; 2405e3ec7017SPing-Ke Shih int sign; 2406e3ec7017SPing-Ke Shih 2407e3ec7017SPing-Ke Shih if (!is_linked) { 2408e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n", 2409e3ec7017SPing-Ke Shih is_linked); 2410e3ec7017SPing-Ke Shih return; 2411e3ec7017SPing-Ke Shih } 2412e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo); 2413e3ec7017SPing-Ke Shih if (curr_cfo == 0) 2414e3ec7017SPing-Ke Shih return; 2415b7379148SYuan-Han Zhang dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO); 2416e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 2417b7379148SYuan-Han Zhang cfo_avg_312 = (curr_cfo << dcfo_comp_sft) / 5 + sign * dcfo_comp_val; 2418e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: avg_cfo=%d\n", cfo_avg_312); 2419e3ec7017SPing-Ke Shih if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV) 2420e3ec7017SPing-Ke Shih cfo_avg_312 = -cfo_avg_312; 2421b7379148SYuan-Han Zhang rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask, 2422e3ec7017SPing-Ke Shih cfo_avg_312); 2423e3ec7017SPing-Ke Shih } 2424e3ec7017SPing-Ke Shih 2425e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev) 2426e3ec7017SPing-Ke Shih { 2427e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1); 2428e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8); 2429e3ec7017SPing-Ke Shih rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK); 2430e3ec7017SPing-Ke Shih } 2431e3ec7017SPing-Ke Shih 2432e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev) 2433e3ec7017SPing-Ke Shih { 2434e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2435e3ec7017SPing-Ke Shih struct rtw89_efuse *efuse = &rtwdev->efuse; 2436e3ec7017SPing-Ke Shih 2437e3ec7017SPing-Ke Shih cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK; 2438e3ec7017SPing-Ke Shih cfo->crystal_cap = cfo->crystal_cap_default; 2439e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap; 2440a9e06f2eSYi-Tang Chiu cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f); 2441a9e06f2eSYi-Tang Chiu cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1); 2442e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2443a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 2444e3ec7017SPing-Ke Shih cfo->x_cap_ofst = 0; 2445a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 2446e3ec7017SPing-Ke Shih cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE; 2447e3ec7017SPing-Ke Shih cfo->apply_compensation = false; 2448e3ec7017SPing-Ke Shih cfo->residual_cfo_acc = 0; 2449e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n", 2450e3ec7017SPing-Ke Shih cfo->crystal_cap_default); 2451e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true); 2452e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1); 2453e3ec7017SPing-Ke Shih rtw89_dcfo_comp_init(rtwdev); 2454e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = 2000; 2455e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2456e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2457e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2458bc013052SEric Huang cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE; 2459e3ec7017SPing-Ke Shih } 2460e3ec7017SPing-Ke Shih 2461e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev, 2462e3ec7017SPing-Ke Shih s32 curr_cfo) 2463e3ec7017SPing-Ke Shih { 2464e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2465e3ec7017SPing-Ke Shih s8 crystal_cap = cfo->crystal_cap; 2466e3ec7017SPing-Ke Shih s32 cfo_abs = abs(curr_cfo); 2467e3ec7017SPing-Ke Shih int sign; 2468e3ec7017SPing-Ke Shih 2469e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 2470e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_ENABLE_TH) 2471e3ec7017SPing-Ke Shih cfo->is_adjust = true; 2472e3ec7017SPing-Ke Shih } else { 2473e3ec7017SPing-Ke Shih if (cfo_abs < CFO_TRK_STOP_TH) 2474e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2475e3ec7017SPing-Ke Shih } 2476e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 2477e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n"); 2478e3ec7017SPing-Ke Shih return; 2479e3ec7017SPing-Ke Shih } 2480e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 2481e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_STOP_TH_4) 2482e3ec7017SPing-Ke Shih crystal_cap += 7 * sign; 2483e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_3) 2484e3ec7017SPing-Ke Shih crystal_cap += 5 * sign; 2485e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_2) 2486e3ec7017SPing-Ke Shih crystal_cap += 3 * sign; 2487e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_1) 2488e3ec7017SPing-Ke Shih crystal_cap += 1 * sign; 2489e3ec7017SPing-Ke Shih else 2490e3ec7017SPing-Ke Shih return; 2491e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false); 2492e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2493e3ec7017SPing-Ke Shih "X_cap{Curr,Default}={0x%x,0x%x}\n", 2494e3ec7017SPing-Ke Shih cfo->crystal_cap, cfo->def_x_cap); 2495e3ec7017SPing-Ke Shih } 2496e3ec7017SPing-Ke Shih 2497e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev) 2498e3ec7017SPing-Ke Shih { 2499e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2500e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 2501e3ec7017SPing-Ke Shih s32 cfo_cnt_all = 0; 2502e3ec7017SPing-Ke Shih s32 cfo_all_avg = 0; 2503e3ec7017SPing-Ke Shih u8 i; 2504e3ec7017SPing-Ke Shih 2505e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc != 1) 2506e3ec7017SPing-Ke Shih return 0; 2507e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n"); 2508e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2509e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2510e3ec7017SPing-Ke Shih continue; 2511e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 2512e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 2513e3ec7017SPing-Ke Shih cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all); 2514e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 2515e3ec7017SPing-Ke Shih } 2516e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2517e3ec7017SPing-Ke Shih "CFO track for macid = %d\n", i); 2518e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2519e3ec7017SPing-Ke Shih "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n", 2520e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_all_avg); 2521e3ec7017SPing-Ke Shih return cfo_all_avg; 2522e3ec7017SPing-Ke Shih } 2523e3ec7017SPing-Ke Shih 2524e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev) 2525e3ec7017SPing-Ke Shih { 2526e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2527e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 2528e3ec7017SPing-Ke Shih s32 target_cfo = 0; 2529e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 2530e3ec7017SPing-Ke Shih s32 cfo_khz_all_tp_wgt = 0; 2531e3ec7017SPing-Ke Shih s32 cfo_avg = 0; 2532e3ec7017SPing-Ke Shih s32 max_cfo_lb = BIT(31); 2533e3ec7017SPing-Ke Shih s32 min_cfo_ub = GENMASK(30, 0); 2534e3ec7017SPing-Ke Shih u16 cfo_cnt_all = 0; 2535e3ec7017SPing-Ke Shih u8 active_entry_cnt = 0; 2536e3ec7017SPing-Ke Shih u8 sta_cnt = 0; 2537e3ec7017SPing-Ke Shih u32 tp_all = 0; 2538e3ec7017SPing-Ke Shih u8 i; 2539e3ec7017SPing-Ke Shih u8 cfo_tol = 0; 2540e3ec7017SPing-Ke Shih 2541e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n"); 2542e3ec7017SPing-Ke Shih if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) { 2543e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n"); 2544e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2545e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2546e3ec7017SPing-Ke Shih continue; 2547e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 2548e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 2549e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all); 2550e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2551e3ec7017SPing-Ke Shih "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n", 2552e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_avg); 2553e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 2554e3ec7017SPing-Ke Shih } 2555e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) { 2556e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n"); 2557e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2558e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2559e3ec7017SPing-Ke Shih continue; 2560e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 2561e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 2562e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 2563e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2564e3ec7017SPing-Ke Shih "Macid=%d, cfo_avg=%d\n", i, 2565e3ec7017SPing-Ke Shih cfo->cfo_avg[i]); 2566e3ec7017SPing-Ke Shih } 2567e3ec7017SPing-Ke Shih sta_cnt = rtwdev->total_sta_assoc; 2568e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt); 2569e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2570e3ec7017SPing-Ke Shih "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n", 2571e3ec7017SPing-Ke Shih cfo_khz_all, sta_cnt, cfo_avg); 2572e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 2573e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) { 2574e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n"); 2575e3ec7017SPing-Ke Shih cfo_tol = cfo->sta_cfo_tolerance; 2576e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2577e3ec7017SPing-Ke Shih sta_cnt++; 2578e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] != 0) { 2579e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 2580e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 2581e3ec7017SPing-Ke Shih active_entry_cnt++; 2582e3ec7017SPing-Ke Shih } else { 2583e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = cfo->pre_cfo_avg[i]; 2584e3ec7017SPing-Ke Shih } 2585e3ec7017SPing-Ke Shih max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb); 2586e3ec7017SPing-Ke Shih min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub); 2587e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 2588e3ec7017SPing-Ke Shih /* need tp for each entry */ 2589e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2590e3ec7017SPing-Ke Shih "[%d] cfo_avg=%d, tp=tbd\n", 2591e3ec7017SPing-Ke Shih i, cfo->cfo_avg[i]); 2592e3ec7017SPing-Ke Shih if (sta_cnt >= rtwdev->total_sta_assoc) 2593e3ec7017SPing-Ke Shih break; 2594e3ec7017SPing-Ke Shih } 2595e3ec7017SPing-Ke Shih tp_all = stats->rx_throughput; /* need tp for each entry */ 2596e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all_tp_wgt, (s32)tp_all); 2597e3ec7017SPing-Ke Shih 2598e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n", 2599e3ec7017SPing-Ke Shih sta_cnt); 2600e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n", 2601e3ec7017SPing-Ke Shih active_entry_cnt); 2602e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2603e3ec7017SPing-Ke Shih "Msta cfo with tp_wgt=%d, avg_cfo=%d\n", 2604e3ec7017SPing-Ke Shih cfo_khz_all_tp_wgt, cfo_avg); 2605e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n", 2606e3ec7017SPing-Ke Shih max_cfo_lb, min_cfo_ub); 2607e3ec7017SPing-Ke Shih if (max_cfo_lb <= min_cfo_ub) { 2608e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2609e3ec7017SPing-Ke Shih "cfo win_size=%d\n", 2610e3ec7017SPing-Ke Shih min_cfo_ub - max_cfo_lb); 2611e3ec7017SPing-Ke Shih target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub); 2612e3ec7017SPing-Ke Shih } else { 2613e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2614c51ed740SColin Ian King "No intersection of cfo tolerance windows\n"); 2615e3ec7017SPing-Ke Shih target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt); 2616e3ec7017SPing-Ke Shih } 2617e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) 2618e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 2619e3ec7017SPing-Ke Shih } 2620e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo); 2621e3ec7017SPing-Ke Shih return target_cfo; 2622e3ec7017SPing-Ke Shih } 2623e3ec7017SPing-Ke Shih 2624e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev) 2625e3ec7017SPing-Ke Shih { 2626e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2627e3ec7017SPing-Ke Shih 2628e3ec7017SPing-Ke Shih memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail)); 2629e3ec7017SPing-Ke Shih memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt)); 2630e3ec7017SPing-Ke Shih cfo->packet_count = 0; 2631e3ec7017SPing-Ke Shih cfo->packet_count_pre = 0; 2632e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = 0; 2633e3ec7017SPing-Ke Shih } 2634e3ec7017SPing-Ke Shih 2635e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev) 2636e3ec7017SPing-Ke Shih { 2637e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2638e3ec7017SPing-Ke Shih s32 new_cfo = 0; 2639e3ec7017SPing-Ke Shih bool x_cap_update = false; 2640e3ec7017SPing-Ke Shih u8 pre_x_cap = cfo->crystal_cap; 2641e3ec7017SPing-Ke Shih 2642e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n", 2643e3ec7017SPing-Ke Shih rtwdev->total_sta_assoc); 2644e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 0) { 2645e3ec7017SPing-Ke Shih rtw89_phy_cfo_reset(rtwdev); 2646e3ec7017SPing-Ke Shih return; 2647e3ec7017SPing-Ke Shih } 2648e3ec7017SPing-Ke Shih if (cfo->packet_count == 0) { 2649e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n"); 2650e3ec7017SPing-Ke Shih return; 2651e3ec7017SPing-Ke Shih } 2652e3ec7017SPing-Ke Shih if (cfo->packet_count == cfo->packet_count_pre) { 2653e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n"); 2654e3ec7017SPing-Ke Shih return; 2655e3ec7017SPing-Ke Shih } 2656e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 1) 2657e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_average_cfo_calc(rtwdev); 2658e3ec7017SPing-Ke Shih else 2659e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev); 2660e3ec7017SPing-Ke Shih if (new_cfo == 0) { 2661e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n"); 2662e3ec7017SPing-Ke Shih return; 2663e3ec7017SPing-Ke Shih } 2664a9e06f2eSYi-Tang Chiu if (cfo->divergence_lock_en) { 2665a9e06f2eSYi-Tang Chiu cfo->lock_cnt++; 2666a9e06f2eSYi-Tang Chiu if (cfo->lock_cnt > CFO_PERIOD_CNT) { 2667a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 2668a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 2669a9e06f2eSYi-Tang Chiu } else { 2670a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 2671a9e06f2eSYi-Tang Chiu } 2672a9e06f2eSYi-Tang Chiu return; 2673a9e06f2eSYi-Tang Chiu } 2674a9e06f2eSYi-Tang Chiu if (cfo->crystal_cap >= cfo->x_cap_ub || 2675a9e06f2eSYi-Tang Chiu cfo->crystal_cap <= cfo->x_cap_lb) { 2676a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = true; 2677a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 2678a9e06f2eSYi-Tang Chiu return; 2679a9e06f2eSYi-Tang Chiu } 2680a9e06f2eSYi-Tang Chiu 2681e3ec7017SPing-Ke Shih rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo); 2682e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = new_cfo; 26831646ce8fSYe Guojin x_cap_update = cfo->crystal_cap != pre_x_cap; 2684e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update); 2685e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n", 2686e3ec7017SPing-Ke Shih cfo->def_x_cap, pre_x_cap, cfo->crystal_cap, 2687e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 2688e3ec7017SPing-Ke Shih if (x_cap_update) { 2689e3ec7017SPing-Ke Shih if (new_cfo > 0) 2690e3ec7017SPing-Ke Shih new_cfo -= CFO_SW_COMP_FINE_TUNE; 2691e3ec7017SPing-Ke Shih else 2692e3ec7017SPing-Ke Shih new_cfo += CFO_SW_COMP_FINE_TUNE; 2693e3ec7017SPing-Ke Shih } 2694e3ec7017SPing-Ke Shih rtw89_dcfo_comp(rtwdev, new_cfo); 2695e3ec7017SPing-Ke Shih rtw89_phy_cfo_statistics_reset(rtwdev); 2696e3ec7017SPing-Ke Shih } 2697e3ec7017SPing-Ke Shih 2698e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work) 2699e3ec7017SPing-Ke Shih { 2700e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 2701e3ec7017SPing-Ke Shih cfo_track_work.work); 2702e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2703e3ec7017SPing-Ke Shih 2704e3ec7017SPing-Ke Shih mutex_lock(&rtwdev->mutex); 2705e3ec7017SPing-Ke Shih if (!cfo->cfo_trig_by_timer_en) 2706e3ec7017SPing-Ke Shih goto out; 2707e3ec7017SPing-Ke Shih rtw89_leave_ps_mode(rtwdev); 2708e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2709e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2710e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2711e3ec7017SPing-Ke Shih out: 2712e3ec7017SPing-Ke Shih mutex_unlock(&rtwdev->mutex); 2713e3ec7017SPing-Ke Shih } 2714e3ec7017SPing-Ke Shih 2715e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev) 2716e3ec7017SPing-Ke Shih { 2717e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2718e3ec7017SPing-Ke Shih 2719e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2720e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2721e3ec7017SPing-Ke Shih } 2722e3ec7017SPing-Ke Shih 2723e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev) 2724e3ec7017SPing-Ke Shih { 2725e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2726e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 2727bc013052SEric Huang bool is_ul_ofdma = false, ofdma_acc_en = false; 2728bc013052SEric Huang 2729bc013052SEric Huang if (stats->rx_tf_periodic > CFO_TF_CNT_TH) 2730bc013052SEric Huang is_ul_ofdma = true; 2731bc013052SEric Huang if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE && 2732bc013052SEric Huang is_ul_ofdma) 2733bc013052SEric Huang ofdma_acc_en = true; 2734e3ec7017SPing-Ke Shih 2735e3ec7017SPing-Ke Shih switch (cfo->phy_cfo_status) { 2736e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_NORMAL: 2737e3ec7017SPing-Ke Shih if (stats->tx_throughput >= CFO_TP_UPPER) { 2738e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE; 2739e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = true; 2740e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = CFO_COMP_PERIOD; 2741e3ec7017SPing-Ke Shih rtw89_phy_cfo_start_work(rtwdev); 2742e3ec7017SPing-Ke Shih } 2743e3ec7017SPing-Ke Shih break; 2744e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_ENHANCE: 2745bc013052SEric Huang if (stats->tx_throughput <= CFO_TP_LOWER) 2746bc013052SEric Huang cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2747bc013052SEric Huang else if (ofdma_acc_en && 2748bc013052SEric Huang cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT) 2749bc013052SEric Huang cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD; 2750bc013052SEric Huang else 2751bc013052SEric Huang cfo->phy_cfo_trk_cnt++; 2752bc013052SEric Huang 2753bc013052SEric Huang if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) { 2754e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2755e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2756e3ec7017SPing-Ke Shih } 2757bc013052SEric Huang break; 2758bc013052SEric Huang case RTW89_PHY_DCFO_STATE_HOLD: 2759e3ec7017SPing-Ke Shih if (stats->tx_throughput <= CFO_TP_LOWER) { 2760e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2761e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2762e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2763bc013052SEric Huang } else { 2764bc013052SEric Huang cfo->phy_cfo_trk_cnt++; 2765e3ec7017SPing-Ke Shih } 2766e3ec7017SPing-Ke Shih break; 2767e3ec7017SPing-Ke Shih default: 2768e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2769e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2770e3ec7017SPing-Ke Shih break; 2771e3ec7017SPing-Ke Shih } 2772e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2773e3ec7017SPing-Ke Shih "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n", 2774e3ec7017SPing-Ke Shih stats->tx_throughput, cfo->phy_cfo_status, 2775e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt, 2776e3ec7017SPing-Ke Shih ewma_thermal_read(&rtwdev->phystat.avg_thermal[0])); 2777e3ec7017SPing-Ke Shih if (cfo->cfo_trig_by_timer_en) 2778e3ec7017SPing-Ke Shih return; 2779e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2780e3ec7017SPing-Ke Shih } 2781e3ec7017SPing-Ke Shih 2782e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val, 2783e3ec7017SPing-Ke Shih struct rtw89_rx_phy_ppdu *phy_ppdu) 2784e3ec7017SPing-Ke Shih { 2785e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2786e3ec7017SPing-Ke Shih u8 macid = phy_ppdu->mac_id; 2787e3ec7017SPing-Ke Shih 278897df8587SPing-Ke Shih if (macid >= CFO_TRACK_MAX_USER) { 278997df8587SPing-Ke Shih rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid); 279097df8587SPing-Ke Shih return; 279197df8587SPing-Ke Shih } 279297df8587SPing-Ke Shih 2793e3ec7017SPing-Ke Shih cfo->cfo_tail[macid] += cfo_val; 2794e3ec7017SPing-Ke Shih cfo->cfo_cnt[macid]++; 2795e3ec7017SPing-Ke Shih cfo->packet_count++; 2796e3ec7017SPing-Ke Shih } 2797e3ec7017SPing-Ke Shih 279829136c95SEric Huang void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 279929136c95SEric Huang { 280029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 280129136c95SEric Huang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 280229136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 280329136c95SEric Huang 280429136c95SEric Huang if (!chip->support_ul_tb_ctrl) 280529136c95SEric Huang return; 280629136c95SEric Huang 280729136c95SEric Huang rtwvif->def_tri_idx = 280829136c95SEric Huang rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG); 280929136c95SEric Huang 281029136c95SEric Huang if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV) 281129136c95SEric Huang rtwvif->dyn_tb_bedge_en = false; 281229136c95SEric Huang else if (chan->band_type >= RTW89_BAND_5G && 281329136c95SEric Huang chan->band_width >= RTW89_CHANNEL_WIDTH_40) 281429136c95SEric Huang rtwvif->dyn_tb_bedge_en = true; 281529136c95SEric Huang else 281629136c95SEric Huang rtwvif->dyn_tb_bedge_en = false; 281729136c95SEric Huang 281829136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 281929136c95SEric Huang "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n", 282029136c95SEric Huang ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx); 282129136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 282229136c95SEric Huang "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n", 282329136c95SEric Huang rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en); 282429136c95SEric Huang } 282529136c95SEric Huang 282629136c95SEric Huang struct rtw89_phy_ul_tb_check_data { 282729136c95SEric Huang bool valid; 282829136c95SEric Huang bool high_tf_client; 282929136c95SEric Huang bool low_tf_client; 283029136c95SEric Huang bool dyn_tb_bedge_en; 283129136c95SEric Huang u8 def_tri_idx; 283229136c95SEric Huang }; 283329136c95SEric Huang 283429136c95SEric Huang static 283529136c95SEric Huang void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev, 283629136c95SEric Huang struct rtw89_vif *rtwvif, 283729136c95SEric Huang struct rtw89_phy_ul_tb_check_data *ul_tb_data) 283829136c95SEric Huang { 283929136c95SEric Huang struct rtw89_traffic_stats *stats = &rtwdev->stats; 284029136c95SEric Huang struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 284129136c95SEric Huang 284229136c95SEric Huang if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION) 284329136c95SEric Huang return; 284429136c95SEric Huang 284529136c95SEric Huang if (!vif->cfg.assoc) 284629136c95SEric Huang return; 284729136c95SEric Huang 284829136c95SEric Huang if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH) 284929136c95SEric Huang ul_tb_data->high_tf_client = true; 285029136c95SEric Huang else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH) 285129136c95SEric Huang ul_tb_data->low_tf_client = true; 285229136c95SEric Huang 285329136c95SEric Huang ul_tb_data->valid = true; 285429136c95SEric Huang ul_tb_data->def_tri_idx = rtwvif->def_tri_idx; 285529136c95SEric Huang ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en; 285629136c95SEric Huang } 285729136c95SEric Huang 285829136c95SEric Huang void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev) 285929136c95SEric Huang { 286029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 286129136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 286229136c95SEric Huang struct rtw89_phy_ul_tb_check_data ul_tb_data = {}; 286329136c95SEric Huang struct rtw89_vif *rtwvif; 286429136c95SEric Huang 286529136c95SEric Huang if (!chip->support_ul_tb_ctrl) 286629136c95SEric Huang return; 286729136c95SEric Huang 286829136c95SEric Huang if (rtwdev->total_sta_assoc != 1) 286929136c95SEric Huang return; 287029136c95SEric Huang 287129136c95SEric Huang rtw89_for_each_rtwvif(rtwdev, rtwvif) 287229136c95SEric Huang rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data); 287329136c95SEric Huang 287429136c95SEric Huang if (!ul_tb_data.valid) 287529136c95SEric Huang return; 287629136c95SEric Huang 287729136c95SEric Huang if (ul_tb_data.dyn_tb_bedge_en) { 287829136c95SEric Huang if (ul_tb_data.high_tf_client) { 287929136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0); 288029136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 288129136c95SEric Huang "[ULTB] Turn off if_bandedge\n"); 288229136c95SEric Huang } else if (ul_tb_data.low_tf_client) { 288329136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 288429136c95SEric Huang ul_tb_info->def_if_bandedge); 288529136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 288629136c95SEric Huang "[ULTB] Set to default if_bandedge = %d\n", 288729136c95SEric Huang ul_tb_info->def_if_bandedge); 288829136c95SEric Huang } 288929136c95SEric Huang } 289029136c95SEric Huang 289129136c95SEric Huang if (ul_tb_info->dyn_tb_tri_en) { 289229136c95SEric Huang if (ul_tb_data.high_tf_client) { 289329136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 289429136c95SEric Huang B_TXSHAPE_TRIANGULAR_CFG, 0); 289529136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 289629136c95SEric Huang "[ULTB] Turn off Tx triangle\n"); 289729136c95SEric Huang } else if (ul_tb_data.low_tf_client) { 289829136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 289929136c95SEric Huang B_TXSHAPE_TRIANGULAR_CFG, 290029136c95SEric Huang ul_tb_data.def_tri_idx); 290129136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 290229136c95SEric Huang "[ULTB] Set to default tx_shap_idx = %d\n", 290329136c95SEric Huang ul_tb_data.def_tri_idx); 290429136c95SEric Huang } 290529136c95SEric Huang } 290629136c95SEric Huang } 290729136c95SEric Huang 290829136c95SEric Huang static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev) 290929136c95SEric Huang { 291029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 291129136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 291229136c95SEric Huang 291329136c95SEric Huang if (!chip->support_ul_tb_ctrl) 291429136c95SEric Huang return; 291529136c95SEric Huang 291629136c95SEric Huang ul_tb_info->dyn_tb_tri_en = true; 291729136c95SEric Huang ul_tb_info->def_if_bandedge = 291829136c95SEric Huang rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN); 291929136c95SEric Huang } 292029136c95SEric Huang 2921e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev) 2922e3ec7017SPing-Ke Shih { 2923e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2924e3ec7017SPing-Ke Shih int i; 2925e3ec7017SPing-Ke Shih u8 th; 2926e3ec7017SPing-Ke Shih 2927e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) { 2928e3ec7017SPing-Ke Shih th = rtw89_chip_get_thermal(rtwdev, i); 2929e3ec7017SPing-Ke Shih if (th) 2930e3ec7017SPing-Ke Shih ewma_thermal_add(&phystat->avg_thermal[i], th); 2931e3ec7017SPing-Ke Shih 2932e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, 2933e3ec7017SPing-Ke Shih "path(%d) thermal cur=%u avg=%ld", i, th, 2934e3ec7017SPing-Ke Shih ewma_thermal_read(&phystat->avg_thermal[i])); 2935e3ec7017SPing-Ke Shih } 2936e3ec7017SPing-Ke Shih } 2937e3ec7017SPing-Ke Shih 2938e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data { 2939e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 2940e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info; 2941e3ec7017SPing-Ke Shih bool rssi_changed; 2942e3ec7017SPing-Ke Shih }; 2943e3ec7017SPing-Ke Shih 2944e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data, 2945e3ec7017SPing-Ke Shih struct ieee80211_sta *sta) 2946e3ec7017SPing-Ke Shih { 2947e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2948e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data *rssi_data = 2949e3ec7017SPing-Ke Shih (struct rtw89_phy_iter_rssi_data *)data; 2950e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info; 2951e3ec7017SPing-Ke Shih unsigned long rssi_curr; 2952e3ec7017SPing-Ke Shih 2953e3ec7017SPing-Ke Shih rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi); 2954e3ec7017SPing-Ke Shih 2955e3ec7017SPing-Ke Shih if (rssi_curr < ch_info->rssi_min) { 2956e3ec7017SPing-Ke Shih ch_info->rssi_min = rssi_curr; 2957e3ec7017SPing-Ke Shih ch_info->rssi_min_macid = rtwsta->mac_id; 2958e3ec7017SPing-Ke Shih } 2959e3ec7017SPing-Ke Shih 2960e3ec7017SPing-Ke Shih if (rtwsta->prev_rssi == 0) { 2961e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 2962e3ec7017SPing-Ke Shih } else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) { 2963e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 2964e3ec7017SPing-Ke Shih rssi_data->rssi_changed = true; 2965e3ec7017SPing-Ke Shih } 2966e3ec7017SPing-Ke Shih } 2967e3ec7017SPing-Ke Shih 2968e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev) 2969e3ec7017SPing-Ke Shih { 2970e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data rssi_data = {0}; 2971e3ec7017SPing-Ke Shih 2972e3ec7017SPing-Ke Shih rssi_data.rtwdev = rtwdev; 2973e3ec7017SPing-Ke Shih rssi_data.ch_info = &rtwdev->ch_info; 2974e3ec7017SPing-Ke Shih rssi_data.ch_info->rssi_min = U8_MAX; 2975e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 2976e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update_iter, 2977e3ec7017SPing-Ke Shih &rssi_data); 2978e3ec7017SPing-Ke Shih if (rssi_data.rssi_changed) 2979e3ec7017SPing-Ke Shih rtw89_btc_ntfy_wl_sta(rtwdev); 2980e3ec7017SPing-Ke Shih } 2981e3ec7017SPing-Ke Shih 2982e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev) 2983e3ec7017SPing-Ke Shih { 2984e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2985e3ec7017SPing-Ke Shih int i; 2986e3ec7017SPing-Ke Shih 2987e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) 2988e3ec7017SPing-Ke Shih ewma_thermal_init(&phystat->avg_thermal[i]); 2989e3ec7017SPing-Ke Shih 2990e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 2991e3ec7017SPing-Ke Shih 2992e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 2993e3ec7017SPing-Ke Shih memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat)); 2994e3ec7017SPing-Ke Shih } 2995e3ec7017SPing-Ke Shih 2996e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev) 2997e3ec7017SPing-Ke Shih { 2998e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2999e3ec7017SPing-Ke Shih 3000e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 3001e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update(rtwdev); 3002e3ec7017SPing-Ke Shih 3003e3ec7017SPing-Ke Shih phystat->last_pkt_stat = phystat->cur_pkt_stat; 3004e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 3005e3ec7017SPing-Ke Shih } 3006e3ec7017SPing-Ke Shih 3007e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us) 3008e3ec7017SPing-Ke Shih { 3009e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3010e3ec7017SPing-Ke Shih 3011e3ec7017SPing-Ke Shih return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 3012e3ec7017SPing-Ke Shih } 3013e3ec7017SPing-Ke Shih 3014e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx) 3015e3ec7017SPing-Ke Shih { 3016e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3017e3ec7017SPing-Ke Shih 3018e3ec7017SPing-Ke Shih return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 3019e3ec7017SPing-Ke Shih } 3020e3ec7017SPing-Ke Shih 3021e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev) 3022e3ec7017SPing-Ke Shih { 3023e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3024e3ec7017SPing-Ke Shih 3025e3ec7017SPing-Ke Shih env->ccx_manual_ctrl = false; 3026e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3027e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 3028e3ec7017SPing-Ke Shih env->ccx_rpt_stamp = 0; 3029e3ec7017SPing-Ke Shih env->ccx_period = 0; 3030e3ec7017SPing-Ke Shih env->ccx_unit_idx = RTW89_CCX_32_US; 3031e3ec7017SPing-Ke Shih env->ccx_trigger_time = 0; 3032e3ec7017SPing-Ke Shih env->ccx_edcca_opt_bw_idx = RTW89_CCX_EDCCA_BW20_0; 3033e3ec7017SPing-Ke Shih 3034e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1); 3035e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1); 3036e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 3037e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK, 3038e3ec7017SPing-Ke Shih RTW89_CCX_EDCCA_BW20_0); 3039e3ec7017SPing-Ke Shih } 3040e3ec7017SPing-Ke Shih 3041e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report, 3042e3ec7017SPing-Ke Shih u16 score) 3043e3ec7017SPing-Ke Shih { 3044e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3045e3ec7017SPing-Ke Shih u32 numer = 0; 3046e3ec7017SPing-Ke Shih u16 ret = 0; 3047e3ec7017SPing-Ke Shih 3048e3ec7017SPing-Ke Shih numer = report * score + (env->ccx_period >> 1); 3049e3ec7017SPing-Ke Shih if (env->ccx_period) 3050e3ec7017SPing-Ke Shih ret = numer / env->ccx_period; 3051e3ec7017SPing-Ke Shih 3052e3ec7017SPing-Ke Shih return ret >= score ? score - 1 : ret; 3053e3ec7017SPing-Ke Shih } 3054e3ec7017SPing-Ke Shih 3055e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev, 3056e3ec7017SPing-Ke Shih u16 time_ms, u32 *period, 3057e3ec7017SPing-Ke Shih u32 *unit_idx) 3058e3ec7017SPing-Ke Shih { 3059e3ec7017SPing-Ke Shih u32 idx; 3060e3ec7017SPing-Ke Shih u8 quotient; 3061e3ec7017SPing-Ke Shih 3062e3ec7017SPing-Ke Shih if (time_ms >= CCX_MAX_PERIOD) 3063e3ec7017SPing-Ke Shih time_ms = CCX_MAX_PERIOD; 3064e3ec7017SPing-Ke Shih 3065e3ec7017SPing-Ke Shih quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD; 3066e3ec7017SPing-Ke Shih 3067e3ec7017SPing-Ke Shih if (quotient < 4) 3068e3ec7017SPing-Ke Shih idx = RTW89_CCX_4_US; 3069e3ec7017SPing-Ke Shih else if (quotient < 8) 3070e3ec7017SPing-Ke Shih idx = RTW89_CCX_8_US; 3071e3ec7017SPing-Ke Shih else if (quotient < 16) 3072e3ec7017SPing-Ke Shih idx = RTW89_CCX_16_US; 3073e3ec7017SPing-Ke Shih else 3074e3ec7017SPing-Ke Shih idx = RTW89_CCX_32_US; 3075e3ec7017SPing-Ke Shih 3076e3ec7017SPing-Ke Shih *unit_idx = idx; 3077e3ec7017SPing-Ke Shih *period = (time_ms * MS_TO_4US_RATIO) >> idx; 3078e3ec7017SPing-Ke Shih 3079e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3080e3ec7017SPing-Ke Shih "[Trigger Time] period:%d, unit_idx:%d\n", 3081e3ec7017SPing-Ke Shih *period, *unit_idx); 3082e3ec7017SPing-Ke Shih } 3083e3ec7017SPing-Ke Shih 3084e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev) 3085e3ec7017SPing-Ke Shih { 3086e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3087e3ec7017SPing-Ke Shih 3088e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3089e3ec7017SPing-Ke Shih "lv:(%d)->(0)\n", env->ccx_rac_lv); 3090e3ec7017SPing-Ke Shih 3091e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3092e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 3093e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3094e3ec7017SPing-Ke Shih } 3095e3ec7017SPing-Ke Shih 3096e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev, 3097e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 3098e3ec7017SPing-Ke Shih { 3099e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3100e3ec7017SPing-Ke Shih bool is_update = env->ifs_clm_app != para->ifs_clm_app; 3101e3ec7017SPing-Ke Shih u8 i = 0; 3102e3ec7017SPing-Ke Shih u16 *ifs_th_l = env->ifs_clm_th_l; 3103e3ec7017SPing-Ke Shih u16 *ifs_th_h = env->ifs_clm_th_h; 3104e3ec7017SPing-Ke Shih u32 ifs_th0_us = 0, ifs_th_times = 0; 3105e3ec7017SPing-Ke Shih u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0}; 3106e3ec7017SPing-Ke Shih 3107e3ec7017SPing-Ke Shih if (!is_update) 3108e3ec7017SPing-Ke Shih goto ifs_update_finished; 3109e3ec7017SPing-Ke Shih 3110e3ec7017SPing-Ke Shih switch (para->ifs_clm_app) { 3111e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_INIT: 3112e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_BACKGROUND: 3113e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_ACS: 3114e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG: 3115e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DIG: 3116e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_TDMA_DIG: 3117e3ec7017SPing-Ke Shih ifs_th0_us = IFS_CLM_TH0_UPPER; 3118e3ec7017SPing-Ke Shih ifs_th_times = IFS_CLM_TH_MUL; 3119e3ec7017SPing-Ke Shih break; 3120e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG_MANUAL: 3121e3ec7017SPing-Ke Shih ifs_th0_us = para->ifs_clm_manual_th0; 3122e3ec7017SPing-Ke Shih ifs_th_times = para->ifs_clm_manual_th_times; 3123e3ec7017SPing-Ke Shih break; 3124e3ec7017SPing-Ke Shih default: 3125e3ec7017SPing-Ke Shih break; 3126e3ec7017SPing-Ke Shih } 3127e3ec7017SPing-Ke Shih 3128e3ec7017SPing-Ke Shih /* Set sampling threshold for 4 different regions, unit in idx_cnt. 3129e3ec7017SPing-Ke Shih * low[i] = high[i-1] + 1 3130e3ec7017SPing-Ke Shih * high[i] = high[i-1] * ifs_th_times 3131e3ec7017SPing-Ke Shih */ 3132e3ec7017SPing-Ke Shih ifs_th_l[IFS_CLM_TH_START_IDX] = 0; 3133e3ec7017SPing-Ke Shih ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us; 3134e3ec7017SPing-Ke Shih ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev, 3135e3ec7017SPing-Ke Shih ifs_th0_us); 3136e3ec7017SPing-Ke Shih for (i = 1; i < RTW89_IFS_CLM_NUM; i++) { 3137e3ec7017SPing-Ke Shih ifs_th_l[i] = ifs_th_h[i - 1] + 1; 3138e3ec7017SPing-Ke Shih ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times; 3139e3ec7017SPing-Ke Shih ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]); 3140e3ec7017SPing-Ke Shih } 3141e3ec7017SPing-Ke Shih 3142e3ec7017SPing-Ke Shih ifs_update_finished: 3143e3ec7017SPing-Ke Shih if (!is_update) 3144e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3145e3ec7017SPing-Ke Shih "No need to update IFS_TH\n"); 3146e3ec7017SPing-Ke Shih 3147e3ec7017SPing-Ke Shih return is_update; 3148e3ec7017SPing-Ke Shih } 3149e3ec7017SPing-Ke Shih 3150e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev) 3151e3ec7017SPing-Ke Shih { 3152e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3153e3ec7017SPing-Ke Shih u8 i = 0; 3154e3ec7017SPing-Ke Shih 3155e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK, 3156e3ec7017SPing-Ke Shih env->ifs_clm_th_l[0]); 3157e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK, 3158e3ec7017SPing-Ke Shih env->ifs_clm_th_l[1]); 3159e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK, 3160e3ec7017SPing-Ke Shih env->ifs_clm_th_l[2]); 3161e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK, 3162e3ec7017SPing-Ke Shih env->ifs_clm_th_l[3]); 3163e3ec7017SPing-Ke Shih 3164e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK, 3165e3ec7017SPing-Ke Shih env->ifs_clm_th_h[0]); 3166e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK, 3167e3ec7017SPing-Ke Shih env->ifs_clm_th_h[1]); 3168e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK, 3169e3ec7017SPing-Ke Shih env->ifs_clm_th_h[2]); 3170e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK, 3171e3ec7017SPing-Ke Shih env->ifs_clm_th_h[3]); 3172e3ec7017SPing-Ke Shih 3173e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3174e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3175e3ec7017SPing-Ke Shih "Update IFS_T%d_th{low, high} : {%d, %d}\n", 3176e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]); 3177e3ec7017SPing-Ke Shih } 3178e3ec7017SPing-Ke Shih 3179e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev) 3180e3ec7017SPing-Ke Shih { 3181e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3182e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 3183e3ec7017SPing-Ke Shih 3184e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3185e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = 0; 3186e3ec7017SPing-Ke Shih 3187e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_INIT; 3188e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, ¶)) 3189e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 3190e3ec7017SPing-Ke Shih 3191e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN, 3192e3ec7017SPing-Ke Shih true); 3193e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true); 3194e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true); 3195e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true); 3196e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true); 3197e3ec7017SPing-Ke Shih } 3198e3ec7017SPing-Ke Shih 3199e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev, 3200e3ec7017SPing-Ke Shih enum rtw89_env_racing_lv level) 3201e3ec7017SPing-Ke Shih { 3202e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3203e3ec7017SPing-Ke Shih int ret = 0; 3204e3ec7017SPing-Ke Shih 3205e3ec7017SPing-Ke Shih if (level >= RTW89_RAC_MAX_NUM) { 3206e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3207e3ec7017SPing-Ke Shih "[WARNING] Wrong LV=%d\n", level); 3208e3ec7017SPing-Ke Shih return -EINVAL; 3209e3ec7017SPing-Ke Shih } 3210e3ec7017SPing-Ke Shih 3211e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3212e3ec7017SPing-Ke Shih "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing, 3213e3ec7017SPing-Ke Shih env->ccx_rac_lv, level); 3214e3ec7017SPing-Ke Shih 3215e3ec7017SPing-Ke Shih if (env->ccx_ongoing) { 3216e3ec7017SPing-Ke Shih if (level <= env->ccx_rac_lv) 3217e3ec7017SPing-Ke Shih ret = -EINVAL; 3218e3ec7017SPing-Ke Shih else 3219e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3220e3ec7017SPing-Ke Shih } 3221e3ec7017SPing-Ke Shih 3222e3ec7017SPing-Ke Shih if (ret == 0) 3223e3ec7017SPing-Ke Shih env->ccx_rac_lv = level; 3224e3ec7017SPing-Ke Shih 3225e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n", 3226e3ec7017SPing-Ke Shih !ret); 3227e3ec7017SPing-Ke Shih 3228e3ec7017SPing-Ke Shih return ret; 3229e3ec7017SPing-Ke Shih } 3230e3ec7017SPing-Ke Shih 3231e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev) 3232e3ec7017SPing-Ke Shih { 3233e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3234e3ec7017SPing-Ke Shih 3235e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0); 3236e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0); 3237e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1); 3238e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 3239e3ec7017SPing-Ke Shih 3240e3ec7017SPing-Ke Shih env->ccx_rpt_stamp++; 3241e3ec7017SPing-Ke Shih env->ccx_ongoing = true; 3242e3ec7017SPing-Ke Shih } 3243e3ec7017SPing-Ke Shih 3244e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev) 3245e3ec7017SPing-Ke Shih { 3246e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3247e3ec7017SPing-Ke Shih u8 i = 0; 3248e3ec7017SPing-Ke Shih u32 res = 0; 3249e3ec7017SPing-Ke Shih 3250e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio = 3251e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT); 3252e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca_ratio = 3253e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca, 3254e3ec7017SPing-Ke Shih PERCENT); 3255e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio = 3256e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT); 3257e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_ratio = 3258e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT); 3259e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio = 3260e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa, 3261e3ec7017SPing-Ke Shih PERCENT); 3262e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio = 3263e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa, 3264e3ec7017SPing-Ke Shih PERCENT); 3265e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil = 3266e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL); 3267e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_permil = 3268e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL); 3269e3ec7017SPing-Ke Shih 3270e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) { 3271e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) { 3272e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD; 3273e3ec7017SPing-Ke Shih } else { 3274e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = 3275e3ec7017SPing-Ke Shih rtw89_phy_ccx_idx_to_us(rtwdev, 3276e3ec7017SPing-Ke Shih env->ifs_clm_avg[i]); 3277e3ec7017SPing-Ke Shih } 3278e3ec7017SPing-Ke Shih 3279e3ec7017SPing-Ke Shih res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]); 3280e3ec7017SPing-Ke Shih res += env->ifs_clm_his[i] >> 1; 3281e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i]) 3282e3ec7017SPing-Ke Shih res /= env->ifs_clm_his[i]; 3283e3ec7017SPing-Ke Shih else 3284e3ec7017SPing-Ke Shih res = 0; 3285e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i] = res; 3286e3ec7017SPing-Ke Shih } 3287e3ec7017SPing-Ke Shih 3288e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3289e3ec7017SPing-Ke Shih "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n", 3290e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio); 3291e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3292e3ec7017SPing-Ke Shih "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n", 3293e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio); 3294e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3295e3ec7017SPing-Ke Shih "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n", 3296e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil); 3297e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3298e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n", 3299e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio, 3300e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio); 3301e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3302e3ec7017SPing-Ke Shih "Time:[his, ifs_avg(us), cca_avg(us)]\n"); 3303e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3304e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n", 3305e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i], 3306e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i]); 3307e3ec7017SPing-Ke Shih } 3308e3ec7017SPing-Ke Shih 3309e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev) 3310e3ec7017SPing-Ke Shih { 3311e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3312e3ec7017SPing-Ke Shih u8 i = 0; 3313e3ec7017SPing-Ke Shih 3314e3ec7017SPing-Ke Shih if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) { 3315e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3316e3ec7017SPing-Ke Shih "Get IFS_CLM report Fail\n"); 3317e3ec7017SPing-Ke Shih return false; 3318e3ec7017SPing-Ke Shih } 3319e3ec7017SPing-Ke Shih 3320e3ec7017SPing-Ke Shih env->ifs_clm_tx = 3321e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 3322e3ec7017SPing-Ke Shih B_IFS_CLM_TX_CNT_MSK); 3323e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca = 3324e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 3325e3ec7017SPing-Ke Shih B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK); 3326e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa = 3327e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 3328e3ec7017SPing-Ke Shih B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK); 3329e3ec7017SPing-Ke Shih env->ifs_clm_ofdmcca_excl_fa = 3330e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 3331e3ec7017SPing-Ke Shih B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK); 3332e3ec7017SPing-Ke Shih env->ifs_clm_cckfa = 3333e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 3334e3ec7017SPing-Ke Shih B_IFS_CLM_CCK_FA_MSK); 3335e3ec7017SPing-Ke Shih env->ifs_clm_ofdmfa = 3336e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 3337e3ec7017SPing-Ke Shih B_IFS_CLM_OFDM_FA_MSK); 3338e3ec7017SPing-Ke Shih 3339e3ec7017SPing-Ke Shih env->ifs_clm_his[0] = 3340e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK); 3341e3ec7017SPing-Ke Shih env->ifs_clm_his[1] = 3342e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK); 3343e3ec7017SPing-Ke Shih env->ifs_clm_his[2] = 3344e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK); 3345e3ec7017SPing-Ke Shih env->ifs_clm_his[3] = 3346e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK); 3347e3ec7017SPing-Ke Shih 3348e3ec7017SPing-Ke Shih env->ifs_clm_avg[0] = 3349e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK); 3350e3ec7017SPing-Ke Shih env->ifs_clm_avg[1] = 3351e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK); 3352e3ec7017SPing-Ke Shih env->ifs_clm_avg[2] = 3353e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK); 3354e3ec7017SPing-Ke Shih env->ifs_clm_avg[3] = 3355e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK); 3356e3ec7017SPing-Ke Shih 3357e3ec7017SPing-Ke Shih env->ifs_clm_cca[0] = 3358e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK); 3359e3ec7017SPing-Ke Shih env->ifs_clm_cca[1] = 3360e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK); 3361e3ec7017SPing-Ke Shih env->ifs_clm_cca[2] = 3362e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK); 3363e3ec7017SPing-Ke Shih env->ifs_clm_cca[3] = 3364e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK); 3365e3ec7017SPing-Ke Shih 3366e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs = 3367e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK); 3368e3ec7017SPing-Ke Shih 3369e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n", 3370e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs); 3371e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3372e3ec7017SPing-Ke Shih "{Tx, EDCCA_exclu_cca} = {%d, %d}\n", 3373e3ec7017SPing-Ke Shih env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca); 3374e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3375e3ec7017SPing-Ke Shih "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n", 3376e3ec7017SPing-Ke Shih env->ifs_clm_cckfa, env->ifs_clm_ofdmfa); 3377e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3378e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n", 3379e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa); 3380e3ec7017SPing-Ke Shih 3381e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n"); 3382e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3383e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3384e3ec7017SPing-Ke Shih "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i], 3385e3ec7017SPing-Ke Shih env->ifs_clm_avg[i], env->ifs_clm_cca[i]); 3386e3ec7017SPing-Ke Shih 3387e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_get_utility(rtwdev); 3388e3ec7017SPing-Ke Shih 3389e3ec7017SPing-Ke Shih return true; 3390e3ec7017SPing-Ke Shih } 3391e3ec7017SPing-Ke Shih 3392e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev, 3393e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 3394e3ec7017SPing-Ke Shih { 3395e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3396e3ec7017SPing-Ke Shih u32 period = 0; 3397e3ec7017SPing-Ke Shih u32 unit_idx = 0; 3398e3ec7017SPing-Ke Shih 3399e3ec7017SPing-Ke Shih if (para->mntr_time == 0) { 3400e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3401e3ec7017SPing-Ke Shih "[WARN] MNTR_TIME is 0\n"); 3402e3ec7017SPing-Ke Shih return -EINVAL; 3403e3ec7017SPing-Ke Shih } 3404e3ec7017SPing-Ke Shih 3405e3ec7017SPing-Ke Shih if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv)) 3406e3ec7017SPing-Ke Shih return -EINVAL; 3407e3ec7017SPing-Ke Shih 3408e3ec7017SPing-Ke Shih if (para->mntr_time != env->ifs_clm_mntr_time) { 3409e3ec7017SPing-Ke Shih rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time, 3410e3ec7017SPing-Ke Shih &period, &unit_idx); 3411e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 3412e3ec7017SPing-Ke Shih B_IFS_CLM_PERIOD_MSK, period); 3413e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 3414e3ec7017SPing-Ke Shih B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx); 3415e3ec7017SPing-Ke Shih 3416e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3417e3ec7017SPing-Ke Shih "Update IFS-CLM time ((%d)) -> ((%d))\n", 3418e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time, para->mntr_time); 3419e3ec7017SPing-Ke Shih 3420e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = para->mntr_time; 3421e3ec7017SPing-Ke Shih env->ccx_period = (u16)period; 3422e3ec7017SPing-Ke Shih env->ccx_unit_idx = (u8)unit_idx; 3423e3ec7017SPing-Ke Shih } 3424e3ec7017SPing-Ke Shih 3425e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) { 3426e3ec7017SPing-Ke Shih env->ifs_clm_app = para->ifs_clm_app; 3427e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 3428e3ec7017SPing-Ke Shih } 3429e3ec7017SPing-Ke Shih 3430e3ec7017SPing-Ke Shih return 0; 3431e3ec7017SPing-Ke Shih } 3432e3ec7017SPing-Ke Shih 3433e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev) 3434e3ec7017SPing-Ke Shih { 3435e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3436e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 3437e3ec7017SPing-Ke Shih u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL; 3438e3ec7017SPing-Ke Shih 3439e3ec7017SPing-Ke Shih env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL; 3440e3ec7017SPing-Ke Shih if (env->ccx_manual_ctrl) { 3441e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3442e3ec7017SPing-Ke Shih "CCX in manual ctrl\n"); 3443e3ec7017SPing-Ke Shih return; 3444e3ec7017SPing-Ke Shih } 3445e3ec7017SPing-Ke Shih 3446e3ec7017SPing-Ke Shih /* only ifs_clm for now */ 3447e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_get_result(rtwdev)) 3448e3ec7017SPing-Ke Shih env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM; 3449e3ec7017SPing-Ke Shih 3450e3ec7017SPing-Ke Shih rtw89_phy_ccx_racing_release(rtwdev); 3451e3ec7017SPing-Ke Shih para.mntr_time = 1900; 3452e3ec7017SPing-Ke Shih para.rac_lv = RTW89_RAC_LV_1; 3453e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3454e3ec7017SPing-Ke Shih 3455e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_set(rtwdev, ¶) == 0) 3456e3ec7017SPing-Ke Shih chk_result |= RTW89_PHY_ENV_MON_IFS_CLM; 3457e3ec7017SPing-Ke Shih if (chk_result) 3458e3ec7017SPing-Ke Shih rtw89_phy_ccx_trigger(rtwdev); 3459e3ec7017SPing-Ke Shih 3460e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3461e3ec7017SPing-Ke Shih "get_result=0x%x, chk_result:0x%x\n", 3462e3ec7017SPing-Ke Shih env->ccx_watchdog_result, chk_result); 3463e3ec7017SPing-Ke Shih } 3464e3ec7017SPing-Ke Shih 3465eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page) 3466eb4e52b3SPo Hao Huang { 3467*9e2f177dSZong-Zhe Yang if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM || 3468eb4e52b3SPo Hao Huang *ie_page == RTW89_RSVD_9) 3469eb4e52b3SPo Hao Huang return false; 3470eb4e52b3SPo Hao Huang else if (*ie_page > RTW89_RSVD_9) 3471eb4e52b3SPo Hao Huang *ie_page -= 1; 3472eb4e52b3SPo Hao Huang 3473eb4e52b3SPo Hao Huang return true; 3474eb4e52b3SPo Hao Huang } 3475eb4e52b3SPo Hao Huang 3476eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page) 3477eb4e52b3SPo Hao Huang { 3478eb4e52b3SPo Hao Huang static const u8 ie_page_shift = 2; 3479eb4e52b3SPo Hao Huang 3480eb4e52b3SPo Hao Huang return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift); 3481eb4e52b3SPo Hao Huang } 3482eb4e52b3SPo Hao Huang 3483eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev, 3484eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page) 3485eb4e52b3SPo Hao Huang { 3486eb4e52b3SPo Hao Huang u32 addr; 3487eb4e52b3SPo Hao Huang 3488eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 3489eb4e52b3SPo Hao Huang return 0; 3490eb4e52b3SPo Hao Huang 3491eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 3492eb4e52b3SPo Hao Huang 3493eb4e52b3SPo Hao Huang return rtw89_phy_read32(rtwdev, addr); 3494eb4e52b3SPo Hao Huang } 3495eb4e52b3SPo Hao Huang 3496eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev, 3497eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page, 3498eb4e52b3SPo Hao Huang u32 val) 3499eb4e52b3SPo Hao Huang { 3500eb4e52b3SPo Hao Huang const struct rtw89_chip_info *chip = rtwdev->chip; 3501eb4e52b3SPo Hao Huang u32 addr; 3502eb4e52b3SPo Hao Huang 3503eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 3504eb4e52b3SPo Hao Huang return; 3505eb4e52b3SPo Hao Huang 3506eb4e52b3SPo Hao Huang if (chip->chip_id == RTL8852A) 3507eb4e52b3SPo Hao Huang val &= B_PHY_STS_BITMAP_MSK_52A; 3508eb4e52b3SPo Hao Huang 3509eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 3510eb4e52b3SPo Hao Huang rtw89_phy_write32(rtwdev, addr, val); 3511eb4e52b3SPo Hao Huang } 3512eb4e52b3SPo Hao Huang 3513eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev, 3514eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap bitmap, 3515eb4e52b3SPo Hao Huang enum rtw89_phy_status_ie_type ie, 3516eb4e52b3SPo Hao Huang bool enable) 3517eb4e52b3SPo Hao Huang { 3518eb4e52b3SPo Hao Huang u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap); 3519eb4e52b3SPo Hao Huang 3520eb4e52b3SPo Hao Huang if (enable) 3521eb4e52b3SPo Hao Huang val |= BIT(ie); 3522eb4e52b3SPo Hao Huang else 3523eb4e52b3SPo Hao Huang val &= ~BIT(ie); 3524eb4e52b3SPo Hao Huang 3525eb4e52b3SPo Hao Huang rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val); 3526eb4e52b3SPo Hao Huang } 3527eb4e52b3SPo Hao Huang 3528eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev, 3529eb4e52b3SPo Hao Huang bool enable, 3530eb4e52b3SPo Hao Huang enum rtw89_phy_idx phy_idx) 3531eb4e52b3SPo Hao Huang { 3532eb4e52b3SPo Hao Huang if (enable) { 3533eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 3534eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 3535eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 3536eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 3537eb4e52b3SPo Hao Huang } else { 3538eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 3539eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 3540eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 3541eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 3542eb4e52b3SPo Hao Huang } 3543eb4e52b3SPo Hao Huang } 3544eb4e52b3SPo Hao Huang 3545eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev) 3546eb4e52b3SPo Hao Huang { 3547eb4e52b3SPo Hao Huang u8 i; 3548eb4e52b3SPo Hao Huang 3549eb4e52b3SPo Hao Huang rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0); 3550eb4e52b3SPo Hao Huang 3551eb4e52b3SPo Hao Huang for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) { 3552eb4e52b3SPo Hao Huang if (i >= RTW89_CCK_PKT) 3553eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 3554eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE09_FTR_0, 3555eb4e52b3SPo Hao Huang true); 3556eb4e52b3SPo Hao Huang if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) || 3557eb4e52b3SPo Hao Huang (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT)) 3558eb4e52b3SPo Hao Huang continue; 3559eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 3560eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE24_OFDM_TD_PATH_A, 3561eb4e52b3SPo Hao Huang true); 3562eb4e52b3SPo Hao Huang } 3563eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT, 3564eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 3565eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT, 3566eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 3567eb4e52b3SPo Hao Huang 3568eb4e52b3SPo Hao Huang /* force IE01 for channel index, only channel field is valid */ 3569eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT, 3570eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE01_CMN_OFDM, true); 3571eb4e52b3SPo Hao Huang } 3572eb4e52b3SPo Hao Huang 3573e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type) 3574e3ec7017SPing-Ke Shih { 3575e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 3576e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3577e3ec7017SPing-Ke Shih const struct rtw89_phy_dig_gain_cfg *cfg; 3578e3ec7017SPing-Ke Shih const char *msg; 3579e3ec7017SPing-Ke Shih u8 i; 3580e3ec7017SPing-Ke Shih s8 gain_base; 3581e3ec7017SPing-Ke Shih s8 *gain_arr; 3582e3ec7017SPing-Ke Shih u32 tmp; 3583e3ec7017SPing-Ke Shih 3584e3ec7017SPing-Ke Shih switch (type) { 3585e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_G: 3586e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_g; 3587e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 3588e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_g; 3589e3ec7017SPing-Ke Shih msg = "lna_gain_g"; 3590e3ec7017SPing-Ke Shih break; 3591e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_G: 3592e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_g; 3593e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_G; 3594e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_g; 3595e3ec7017SPing-Ke Shih msg = "tia_gain_g"; 3596e3ec7017SPing-Ke Shih break; 3597e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_A: 3598e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_a; 3599e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 3600e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_a; 3601e3ec7017SPing-Ke Shih msg = "lna_gain_a"; 3602e3ec7017SPing-Ke Shih break; 3603e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_A: 3604e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_a; 3605e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_A; 3606e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_a; 3607e3ec7017SPing-Ke Shih msg = "tia_gain_a"; 3608e3ec7017SPing-Ke Shih break; 3609e3ec7017SPing-Ke Shih default: 3610e3ec7017SPing-Ke Shih return; 3611e3ec7017SPing-Ke Shih } 3612e3ec7017SPing-Ke Shih 3613e3ec7017SPing-Ke Shih for (i = 0; i < cfg->size; i++) { 3614e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr, 3615e3ec7017SPing-Ke Shih cfg->table[i].mask); 3616e3ec7017SPing-Ke Shih tmp >>= DIG_GAIN_SHIFT; 3617e3ec7017SPing-Ke Shih gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base; 3618e3ec7017SPing-Ke Shih gain_base += DIG_GAIN; 3619e3ec7017SPing-Ke Shih 3620e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n", 3621e3ec7017SPing-Ke Shih msg, i, gain_arr[i]); 3622e3ec7017SPing-Ke Shih } 3623e3ec7017SPing-Ke Shih } 3624e3ec7017SPing-Ke Shih 3625e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev) 3626e3ec7017SPing-Ke Shih { 3627e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3628e3ec7017SPing-Ke Shih u32 tmp; 3629e3ec7017SPing-Ke Shih u8 i; 3630e3ec7017SPing-Ke Shih 3631d264edb1SJohnson Lin if (!rtwdev->hal.support_igi) 3632d264edb1SJohnson Lin return; 3633d264edb1SJohnson Lin 3634e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW, 3635e3ec7017SPing-Ke Shih B_PATH0_IB_PKPW_MSK); 3636e3ec7017SPing-Ke Shih dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT); 3637e3ec7017SPing-Ke Shih dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK, 3638e3ec7017SPing-Ke Shih B_PATH0_IB_PBK_MSK); 3639e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n", 3640e3ec7017SPing-Ke Shih dig->ib_pkpwr, dig->ib_pbk); 3641e3ec7017SPing-Ke Shih 3642e3ec7017SPing-Ke Shih for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++) 3643e3ec7017SPing-Ke Shih rtw89_phy_dig_read_gain_table(rtwdev, i); 3644e3ec7017SPing-Ke Shih } 3645e3ec7017SPing-Ke Shih 3646e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22; 3647e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104}; 3648e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88}; 3649e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16}; 3650e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528}; 3651e3ec7017SPing-Ke Shih 3652e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev) 3653e3ec7017SPing-Ke Shih { 3654e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info; 3655e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3656e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3657e3ec7017SPing-Ke Shih 3658e3ec7017SPing-Ke Shih if (is_linked) { 3659e3ec7017SPing-Ke Shih dig->igi_rssi = ch_info->rssi_min >> 1; 3660e3ec7017SPing-Ke Shih } else { 3661e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n"); 3662e3ec7017SPing-Ke Shih dig->igi_rssi = rssi_nolink; 3663e3ec7017SPing-Ke Shih } 3664e3ec7017SPing-Ke Shih } 3665e3ec7017SPing-Ke Shih 3666e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev) 3667e3ec7017SPing-Ke Shih { 3668e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3669cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 3670e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3671e3ec7017SPing-Ke Shih const u16 *fa_th_src = NULL; 3672e3ec7017SPing-Ke Shih 3673cbb145b9SZong-Zhe Yang switch (chan->band_type) { 3674e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 3675e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_g; 3676e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_g; 3677e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_2g : fa_th_nolink; 3678e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = false; 3679e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 3680e3ec7017SPing-Ke Shih break; 3681e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 3682e3ec7017SPing-Ke Shih default: 3683e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_a; 3684e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_a; 3685e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_5g : fa_th_nolink; 3686e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = true; 3687e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 3688e3ec7017SPing-Ke Shih break; 3689e3ec7017SPing-Ke Shih } 3690e3ec7017SPing-Ke Shih memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th)); 3691e3ec7017SPing-Ke Shih memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th)); 3692e3ec7017SPing-Ke Shih } 3693e3ec7017SPing-Ke Shih 3694e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20; 3695e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a; 3696e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max; 3697e3ec7017SPing-Ke Shih 3698e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev) 3699e3ec7017SPing-Ke Shih { 3700e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3701e3ec7017SPing-Ke Shih 3702e3ec7017SPing-Ke Shih dig->cur_gaincode.lna_idx = LNA_IDX_MAX; 3703e3ec7017SPing-Ke Shih dig->cur_gaincode.tia_idx = TIA_IDX_MAX; 3704e3ec7017SPing-Ke Shih dig->cur_gaincode.rxb_idx = RXB_IDX_MAX; 3705e3ec7017SPing-Ke Shih dig->force_gaincode.lna_idx = LNA_IDX_MAX; 3706e3ec7017SPing-Ke Shih dig->force_gaincode.tia_idx = TIA_IDX_MAX; 3707e3ec7017SPing-Ke Shih dig->force_gaincode.rxb_idx = RXB_IDX_MAX; 3708e3ec7017SPing-Ke Shih 3709e3ec7017SPing-Ke Shih dig->dyn_igi_max = igi_max_performance_mode; 3710e3ec7017SPing-Ke Shih dig->dyn_igi_min = dynamic_igi_min; 3711e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dynamic_pd_threshold_max; 3712e3ec7017SPing-Ke Shih dig->pd_low_th_ofst = pd_low_th_offset; 3713e3ec7017SPing-Ke Shih dig->is_linked_pre = false; 3714e3ec7017SPing-Ke Shih } 3715e3ec7017SPing-Ke Shih 3716e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev) 3717e3ec7017SPing-Ke Shih { 3718e3ec7017SPing-Ke Shih rtw89_phy_dig_update_gain_para(rtwdev); 3719e3ec7017SPing-Ke Shih rtw89_phy_dig_reset(rtwdev); 3720e3ec7017SPing-Ke Shih } 3721e3ec7017SPing-Ke Shih 3722e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 3723e3ec7017SPing-Ke Shih { 3724e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3725e3ec7017SPing-Ke Shih u8 lna_idx; 3726e3ec7017SPing-Ke Shih 3727e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 3728e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX6; 3729e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[1]) 3730e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX5; 3731e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[2]) 3732e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX4; 3733e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[3]) 3734e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX3; 3735e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[4]) 3736e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX2; 3737e3ec7017SPing-Ke Shih else 3738e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX1; 3739e3ec7017SPing-Ke Shih 3740e3ec7017SPing-Ke Shih return lna_idx; 3741e3ec7017SPing-Ke Shih } 3742e3ec7017SPing-Ke Shih 3743e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 3744e3ec7017SPing-Ke Shih { 3745e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3746e3ec7017SPing-Ke Shih u8 tia_idx; 3747e3ec7017SPing-Ke Shih 3748e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 3749e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX1; 3750e3ec7017SPing-Ke Shih else 3751e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX0; 3752e3ec7017SPing-Ke Shih 3753e3ec7017SPing-Ke Shih return tia_idx; 3754e3ec7017SPing-Ke Shih } 3755e3ec7017SPing-Ke Shih 3756e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110 3757e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10 3758e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 3759e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 3760e3ec7017SPing-Ke Shih { 3761e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3762e3ec7017SPing-Ke Shih s8 lna_gain = dig->lna_gain[set->lna_idx]; 3763e3ec7017SPing-Ke Shih s8 tia_gain = dig->tia_gain[set->tia_idx]; 3764e3ec7017SPing-Ke Shih s32 wb_rssi = rssi + lna_gain + tia_gain; 3765e3ec7017SPing-Ke Shih s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE; 3766e3ec7017SPing-Ke Shih u8 rxb_idx; 3767e3ec7017SPing-Ke Shih 3768e3ec7017SPing-Ke Shih rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi; 3769e3ec7017SPing-Ke Shih rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX); 3770e3ec7017SPing-Ke Shih 3771e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n", 3772e3ec7017SPing-Ke Shih wb_rssi, rxb_idx_tmp); 3773e3ec7017SPing-Ke Shih 3774e3ec7017SPing-Ke Shih return rxb_idx; 3775e3ec7017SPing-Ke Shih } 3776e3ec7017SPing-Ke Shih 3777e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 3778e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 3779e3ec7017SPing-Ke Shih { 3780e3ec7017SPing-Ke Shih set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi); 3781e3ec7017SPing-Ke Shih set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi); 3782e3ec7017SPing-Ke Shih set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set); 3783e3ec7017SPing-Ke Shih 3784e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3785e3ec7017SPing-Ke Shih "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n", 3786e3ec7017SPing-Ke Shih rssi, set->lna_idx, set->tia_idx, set->rxb_idx); 3787e3ec7017SPing-Ke Shih } 3788e3ec7017SPing-Ke Shih 3789e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25 3790e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2 3791e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev) 3792e3ec7017SPing-Ke Shih { 3793e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3794e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3795e3ec7017SPing-Ke Shih enum rtw89_dig_noisy_level noisy_lv; 3796e3ec7017SPing-Ke Shih u8 igi_offset = dig->fa_rssi_ofst; 3797e3ec7017SPing-Ke Shih u16 fa_ratio = 0; 3798e3ec7017SPing-Ke Shih 3799e3ec7017SPing-Ke Shih fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil; 3800e3ec7017SPing-Ke Shih 3801e3ec7017SPing-Ke Shih if (fa_ratio < dig->fa_th[0]) 3802e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL0; 3803e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[1]) 3804e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL1; 3805e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[2]) 3806e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL2; 3807e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[3]) 3808e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL3; 3809e3ec7017SPing-Ke Shih else 3810e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX; 3811e3ec7017SPing-Ke Shih 3812e3ec7017SPing-Ke Shih if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2) 3813e3ec7017SPing-Ke Shih igi_offset = 0; 3814e3ec7017SPing-Ke Shih else 3815e3ec7017SPing-Ke Shih igi_offset += noisy_lv * IGI_OFFSET_MUL; 3816e3ec7017SPing-Ke Shih 3817e3ec7017SPing-Ke Shih igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX); 3818e3ec7017SPing-Ke Shih dig->fa_rssi_ofst = igi_offset; 3819e3ec7017SPing-Ke Shih 3820e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3821e3ec7017SPing-Ke Shih "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n", 3822e3ec7017SPing-Ke Shih dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]); 3823e3ec7017SPing-Ke Shih 3824e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3825e3ec7017SPing-Ke Shih "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n", 3826e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil, 3827e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil, 3828e3ec7017SPing-Ke Shih noisy_lv, igi_offset); 3829e3ec7017SPing-Ke Shih } 3830e3ec7017SPing-Ke Shih 3831e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx) 3832e3ec7017SPing-Ke Shih { 383387deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 383487deaad9SEric Huang 383587deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr, 383687deaad9SEric Huang dig_regs->p0_lna_init.mask, lna_idx); 383787deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr, 383887deaad9SEric Huang dig_regs->p1_lna_init.mask, lna_idx); 3839e3ec7017SPing-Ke Shih } 3840e3ec7017SPing-Ke Shih 3841e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx) 3842e3ec7017SPing-Ke Shih { 384387deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 384487deaad9SEric Huang 384587deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr, 384687deaad9SEric Huang dig_regs->p0_tia_init.mask, tia_idx); 384787deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr, 384887deaad9SEric Huang dig_regs->p1_tia_init.mask, tia_idx); 3849e3ec7017SPing-Ke Shih } 3850e3ec7017SPing-Ke Shih 3851e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx) 3852e3ec7017SPing-Ke Shih { 385387deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 385487deaad9SEric Huang 385587deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr, 385687deaad9SEric Huang dig_regs->p0_rxb_init.mask, rxb_idx); 385787deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr, 385887deaad9SEric Huang dig_regs->p1_rxb_init.mask, rxb_idx); 3859e3ec7017SPing-Ke Shih } 3860e3ec7017SPing-Ke Shih 3861e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev, 3862e3ec7017SPing-Ke Shih const struct rtw89_agc_gaincode_set set) 3863e3ec7017SPing-Ke Shih { 3864e3ec7017SPing-Ke Shih rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx); 3865e3ec7017SPing-Ke Shih rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx); 3866e3ec7017SPing-Ke Shih rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx); 3867e3ec7017SPing-Ke Shih 3868e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n", 3869e3ec7017SPing-Ke Shih set.lna_idx, set.tia_idx, set.rxb_idx); 3870e3ec7017SPing-Ke Shih } 3871e3ec7017SPing-Ke Shih 3872e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev, 3873e3ec7017SPing-Ke Shih bool enable) 3874e3ec7017SPing-Ke Shih { 387587deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 3876e3ec7017SPing-Ke Shih 387787deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr, 387887deaad9SEric Huang dig_regs->p0_p20_pagcugc_en.mask, enable); 387987deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr, 388087deaad9SEric Huang dig_regs->p0_s20_pagcugc_en.mask, enable); 388187deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr, 388287deaad9SEric Huang dig_regs->p1_p20_pagcugc_en.mask, enable); 388387deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr, 388487deaad9SEric Huang dig_regs->p1_s20_pagcugc_en.mask, enable); 3885e3ec7017SPing-Ke Shih 3886e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable); 3887e3ec7017SPing-Ke Shih } 3888e3ec7017SPing-Ke Shih 3889bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev) 3890bed4045fSJohnson Lin { 3891bed4045fSJohnson Lin struct rtw89_dig_info *dig = &rtwdev->dig; 3892bed4045fSJohnson Lin 3893d264edb1SJohnson Lin if (!rtwdev->hal.support_igi) 3894d264edb1SJohnson Lin return; 3895d264edb1SJohnson Lin 3896bed4045fSJohnson Lin if (dig->force_gaincode_idx_en) { 3897bed4045fSJohnson Lin rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 3898bed4045fSJohnson Lin rtw89_debug(rtwdev, RTW89_DBG_DIG, 3899bed4045fSJohnson Lin "Force gaincode index enabled.\n"); 3900bed4045fSJohnson Lin } else { 3901bed4045fSJohnson Lin rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi, 3902bed4045fSJohnson Lin &dig->cur_gaincode); 3903bed4045fSJohnson Lin rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode); 3904bed4045fSJohnson Lin } 3905bed4045fSJohnson Lin } 3906bed4045fSJohnson Lin 3907e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi, 3908e3ec7017SPing-Ke Shih bool enable) 3909e3ec7017SPing-Ke Shih { 3910cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 391187deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 3912cbb145b9SZong-Zhe Yang enum rtw89_bandwidth cbw = chan->band_width; 3913e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3914e3ec7017SPing-Ke Shih u8 final_rssi = 0, under_region = dig->pd_low_th_ofst; 39151c2423deSJohnson Lin u8 ofdm_cca_th; 39161c2423deSJohnson Lin s8 cck_cca_th; 39171c2423deSJohnson Lin u32 pd_val = 0; 3918e3ec7017SPing-Ke Shih 3919e3ec7017SPing-Ke Shih under_region += PD_TH_SB_FLTR_CMP_VAL; 3920e3ec7017SPing-Ke Shih 3921e3ec7017SPing-Ke Shih switch (cbw) { 3922e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 3923e3ec7017SPing-Ke Shih under_region += PD_TH_BW40_CMP_VAL; 3924e3ec7017SPing-Ke Shih break; 3925e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 3926e3ec7017SPing-Ke Shih under_region += PD_TH_BW80_CMP_VAL; 3927e3ec7017SPing-Ke Shih break; 39281c2423deSJohnson Lin case RTW89_CHANNEL_WIDTH_160: 39291c2423deSJohnson Lin under_region += PD_TH_BW160_CMP_VAL; 39301c2423deSJohnson Lin break; 3931e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 3932e3ec7017SPing-Ke Shih fallthrough; 3933e3ec7017SPing-Ke Shih default: 3934e3ec7017SPing-Ke Shih under_region += PD_TH_BW20_CMP_VAL; 3935e3ec7017SPing-Ke Shih break; 3936e3ec7017SPing-Ke Shih } 3937e3ec7017SPing-Ke Shih 3938e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dig->igi_rssi; 3939e3ec7017SPing-Ke Shih 3940e3ec7017SPing-Ke Shih final_rssi = min_t(u8, rssi, dig->igi_rssi); 39411c2423deSJohnson Lin ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region, 3942e3ec7017SPing-Ke Shih PD_TH_MAX_RSSI + under_region); 3943e3ec7017SPing-Ke Shih 3944e3ec7017SPing-Ke Shih if (enable) { 39451c2423deSJohnson Lin pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1; 3946e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 39471c2423deSJohnson Lin "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n", 39481c2423deSJohnson Lin final_rssi, ofdm_cca_th, under_region, pd_val); 3949e3ec7017SPing-Ke Shih } else { 3950e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3951c51ed740SColin Ian King "Dynamic PD th disabled, Set PD_low_bd=0\n"); 3952e3ec7017SPing-Ke Shih } 3953e3ec7017SPing-Ke Shih 395487deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 395587deaad9SEric Huang dig_regs->pd_lower_bound_mask, pd_val); 395687deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 395787deaad9SEric Huang dig_regs->pd_spatial_reuse_en, enable); 39581c2423deSJohnson Lin 39591c2423deSJohnson Lin if (!rtwdev->hal.support_cckpd) 39601c2423deSJohnson Lin return; 39611c2423deSJohnson Lin 39621c2423deSJohnson Lin cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI); 39631c2423deSJohnson Lin pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX); 39641c2423deSJohnson Lin 39651c2423deSJohnson Lin rtw89_debug(rtwdev, RTW89_DBG_DIG, 39661c2423deSJohnson Lin "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n", 39671c2423deSJohnson Lin final_rssi, cck_cca_th, under_region, pd_val); 39681c2423deSJohnson Lin 39691c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1, 39701c2423deSJohnson Lin B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable); 39711c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1, 39721c2423deSJohnson Lin B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val); 3973e3ec7017SPing-Ke Shih } 3974e3ec7017SPing-Ke Shih 3975e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev) 3976e3ec7017SPing-Ke Shih { 3977e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3978e3ec7017SPing-Ke Shih 3979e3ec7017SPing-Ke Shih dig->bypass_dig = false; 3980e3ec7017SPing-Ke Shih rtw89_phy_dig_para_reset(rtwdev); 3981e3ec7017SPing-Ke Shih rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 3982e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false); 3983e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 3984e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 3985e3ec7017SPing-Ke Shih } 3986e3ec7017SPing-Ke Shih 3987e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10 3988e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev) 3989e3ec7017SPing-Ke Shih { 3990e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3991e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3992e3ec7017SPing-Ke Shih 3993e3ec7017SPing-Ke Shih if (unlikely(dig->bypass_dig)) { 3994e3ec7017SPing-Ke Shih dig->bypass_dig = false; 3995e3ec7017SPing-Ke Shih return; 3996e3ec7017SPing-Ke Shih } 3997e3ec7017SPing-Ke Shih 3998e3ec7017SPing-Ke Shih if (!dig->is_linked_pre && is_linked) { 3999e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n"); 4000e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 4001e3ec7017SPing-Ke Shih } else if (dig->is_linked_pre && !is_linked) { 4002e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n"); 4003e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 4004e3ec7017SPing-Ke Shih } 4005e3ec7017SPing-Ke Shih dig->is_linked_pre = is_linked; 4006e3ec7017SPing-Ke Shih 4007e3ec7017SPing-Ke Shih rtw89_phy_dig_igi_offset_by_env(rtwdev); 4008e3ec7017SPing-Ke Shih rtw89_phy_dig_update_rssi_info(rtwdev); 4009e3ec7017SPing-Ke Shih 4010e3ec7017SPing-Ke Shih dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ? 4011e3ec7017SPing-Ke Shih dig->igi_rssi - IGI_RSSI_MIN : 0; 4012e3ec7017SPing-Ke Shih dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX; 4013e3ec7017SPing-Ke Shih dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst; 4014e3ec7017SPing-Ke Shih 4015e3ec7017SPing-Ke Shih dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min, 4016e3ec7017SPing-Ke Shih dig->dyn_igi_max); 4017e3ec7017SPing-Ke Shih 4018e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 4019e3ec7017SPing-Ke Shih "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n", 4020e3ec7017SPing-Ke Shih dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min, 4021e3ec7017SPing-Ke Shih dig->igi_fa_rssi); 4022e3ec7017SPing-Ke Shih 4023bed4045fSJohnson Lin rtw89_phy_dig_config_igi(rtwdev); 4024e3ec7017SPing-Ke Shih 4025e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en); 4026e3ec7017SPing-Ke Shih 4027e3ec7017SPing-Ke Shih if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max) 4028e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true); 4029e3ec7017SPing-Ke Shih else 4030e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 4031e3ec7017SPing-Ke Shih } 4032e3ec7017SPing-Ke Shih 40337dbdf655SPing-Ke Shih static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta) 40347dbdf655SPing-Ke Shih { 40357dbdf655SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 40367dbdf655SPing-Ke Shih struct rtw89_dev *rtwdev = rtwsta->rtwdev; 40377dbdf655SPing-Ke Shih struct rtw89_vif *rtwvif = rtwsta->rtwvif; 40387dbdf655SPing-Ke Shih struct rtw89_hal *hal = &rtwdev->hal; 40397dbdf655SPing-Ke Shih bool *done = data; 40407dbdf655SPing-Ke Shih u8 rssi_a, rssi_b; 40417dbdf655SPing-Ke Shih u32 candidate; 40427dbdf655SPing-Ke Shih 40437dbdf655SPing-Ke Shih if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls) 40447dbdf655SPing-Ke Shih return; 40457dbdf655SPing-Ke Shih 40467dbdf655SPing-Ke Shih if (*done) 40477dbdf655SPing-Ke Shih return; 40487dbdf655SPing-Ke Shih 40497dbdf655SPing-Ke Shih *done = true; 40507dbdf655SPing-Ke Shih 40517dbdf655SPing-Ke Shih rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]); 40527dbdf655SPing-Ke Shih rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]); 40537dbdf655SPing-Ke Shih 40547dbdf655SPing-Ke Shih if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH) 40557dbdf655SPing-Ke Shih candidate = RF_A; 40567dbdf655SPing-Ke Shih else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH) 40577dbdf655SPing-Ke Shih candidate = RF_B; 40587dbdf655SPing-Ke Shih else 40597dbdf655SPing-Ke Shih return; 40607dbdf655SPing-Ke Shih 40617dbdf655SPing-Ke Shih if (hal->antenna_tx == candidate) 40627dbdf655SPing-Ke Shih return; 40637dbdf655SPing-Ke Shih 40647dbdf655SPing-Ke Shih hal->antenna_tx = candidate; 40657dbdf655SPing-Ke Shih rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta); 40667dbdf655SPing-Ke Shih 40677dbdf655SPing-Ke Shih if (hal->antenna_tx == RF_A) { 40687dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12); 40697dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11); 40707dbdf655SPing-Ke Shih } else if (hal->antenna_tx == RF_B) { 40717dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11); 40727dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12); 40737dbdf655SPing-Ke Shih } 40747dbdf655SPing-Ke Shih } 40757dbdf655SPing-Ke Shih 40767dbdf655SPing-Ke Shih void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev) 40777dbdf655SPing-Ke Shih { 40787dbdf655SPing-Ke Shih struct rtw89_hal *hal = &rtwdev->hal; 40797dbdf655SPing-Ke Shih bool done = false; 40807dbdf655SPing-Ke Shih 40817dbdf655SPing-Ke Shih if (!hal->tx_path_diversity) 40827dbdf655SPing-Ke Shih return; 40837dbdf655SPing-Ke Shih 40847dbdf655SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 40857dbdf655SPing-Ke Shih rtw89_phy_tx_path_div_sta_iter, 40867dbdf655SPing-Ke Shih &done); 40877dbdf655SPing-Ke Shih } 40887dbdf655SPing-Ke Shih 4089e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev) 4090e3ec7017SPing-Ke Shih { 4091e3ec7017SPing-Ke Shih rtw89_phy_ccx_top_setting_init(rtwdev); 4092e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_setting_init(rtwdev); 4093e3ec7017SPing-Ke Shih } 4094e3ec7017SPing-Ke Shih 4095e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev) 4096e3ec7017SPing-Ke Shih { 4097e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 4098e3ec7017SPing-Ke Shih 4099e3ec7017SPing-Ke Shih rtw89_phy_stat_init(rtwdev); 4100e3ec7017SPing-Ke Shih 4101e3ec7017SPing-Ke Shih rtw89_chip_bb_sethw(rtwdev); 4102e3ec7017SPing-Ke Shih 4103e3ec7017SPing-Ke Shih rtw89_phy_env_monitor_init(rtwdev); 4104eb4e52b3SPo Hao Huang rtw89_physts_parsing_init(rtwdev); 4105e3ec7017SPing-Ke Shih rtw89_phy_dig_init(rtwdev); 4106e3ec7017SPing-Ke Shih rtw89_phy_cfo_init(rtwdev); 410729136c95SEric Huang rtw89_phy_ul_tb_info_init(rtwdev); 4108e3ec7017SPing-Ke Shih 4109e3ec7017SPing-Ke Shih rtw89_phy_init_rf_nctl(rtwdev); 4110e3ec7017SPing-Ke Shih rtw89_chip_rfk_init(rtwdev); 4111e3ec7017SPing-Ke Shih rtw89_load_txpwr_table(rtwdev, chip->byr_table); 4112e3ec7017SPing-Ke Shih rtw89_chip_set_txpwr_ctrl(rtwdev); 4113e3ec7017SPing-Ke Shih rtw89_chip_power_trim(rtwdev); 4114cd89a471SPing-Ke Shih rtw89_chip_cfg_txrx_path(rtwdev); 4115e3ec7017SPing-Ke Shih } 4116e3ec7017SPing-Ke Shih 4117e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 4118e3ec7017SPing-Ke Shih { 4119e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx = RTW89_PHY_0; 4120e3ec7017SPing-Ke Shih u8 bss_color; 4121e3ec7017SPing-Ke Shih 4122f276e20bSJohannes Berg if (!vif->bss_conf.he_support || !vif->cfg.assoc) 4123e3ec7017SPing-Ke Shih return; 4124e3ec7017SPing-Ke Shih 4125e3ec7017SPing-Ke Shih bss_color = vif->bss_conf.he_bss_color.color; 4126e3ec7017SPing-Ke Shih 4127e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_VLD0, 0x1, 4128e3ec7017SPing-Ke Shih phy_idx); 4129e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_TGT, bss_color, 4130e3ec7017SPing-Ke Shih phy_idx); 4131e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_STAID, 4132f276e20bSJohannes Berg vif->cfg.aid, phy_idx); 4133e3ec7017SPing-Ke Shih } 4134db7fa61aSZong-Zhe Yang 4135db7fa61aSZong-Zhe Yang static void 4136db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4137db7fa61aSZong-Zhe Yang { 4138db7fa61aSZong-Zhe Yang rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data); 4139db7fa61aSZong-Zhe Yang } 4140db7fa61aSZong-Zhe Yang 4141db7fa61aSZong-Zhe Yang static void 4142db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4143db7fa61aSZong-Zhe Yang { 4144db7fa61aSZong-Zhe Yang rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data); 4145db7fa61aSZong-Zhe Yang } 4146db7fa61aSZong-Zhe Yang 4147db7fa61aSZong-Zhe Yang static void 4148db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4149db7fa61aSZong-Zhe Yang { 4150db7fa61aSZong-Zhe Yang rtw89_phy_write32_set(rtwdev, def->addr, def->mask); 4151db7fa61aSZong-Zhe Yang } 4152db7fa61aSZong-Zhe Yang 4153db7fa61aSZong-Zhe Yang static void 4154db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4155db7fa61aSZong-Zhe Yang { 4156db7fa61aSZong-Zhe Yang rtw89_phy_write32_clr(rtwdev, def->addr, def->mask); 4157db7fa61aSZong-Zhe Yang } 4158db7fa61aSZong-Zhe Yang 4159db7fa61aSZong-Zhe Yang static void 4160db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4161db7fa61aSZong-Zhe Yang { 4162db7fa61aSZong-Zhe Yang udelay(def->data); 4163db7fa61aSZong-Zhe Yang } 4164db7fa61aSZong-Zhe Yang 4165db7fa61aSZong-Zhe Yang static void 4166db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = { 4167db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WRF] = _rfk_write_rf, 4168db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WM] = _rfk_write32_mask, 4169db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WS] = _rfk_write32_set, 4170db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WC] = _rfk_write32_clr, 4171db7fa61aSZong-Zhe Yang [RTW89_RFK_F_DELAY] = _rfk_delay, 4172db7fa61aSZong-Zhe Yang }; 4173db7fa61aSZong-Zhe Yang 4174db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM); 4175db7fa61aSZong-Zhe Yang 4176db7fa61aSZong-Zhe Yang void 4177db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl) 4178db7fa61aSZong-Zhe Yang { 4179db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *p = tbl->defs; 4180db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *end = tbl->defs + tbl->size; 4181db7fa61aSZong-Zhe Yang 4182db7fa61aSZong-Zhe Yang for (; p < end; p++) 4183db7fa61aSZong-Zhe Yang _rfk_handler[p->flag](rtwdev, p); 4184db7fa61aSZong-Zhe Yang } 4185db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser); 4186c7845551SPing-Ke Shih 4187c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4 4188c7845551SPing-Ke Shih 4189c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = { 4190c7845551SPing-Ke Shih {0xD934, 0xff0000}, 4191c7845551SPing-Ke Shih {0xD934, 0xff000000}, 4192c7845551SPing-Ke Shih {0xD938, 0xff}, 4193c7845551SPing-Ke Shih {0xD934, 0xff00}, 4194c7845551SPing-Ke Shih }; 4195c7845551SPing-Ke Shih 4196c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = { 4197c7845551SPing-Ke Shih {0xD930, 0xff0000}, 4198c7845551SPing-Ke Shih {0xD930, 0xff000000}, 4199c7845551SPing-Ke Shih {0xD934, 0xff}, 4200c7845551SPing-Ke Shih {0xD930, 0xff00}, 4201c7845551SPing-Ke Shih }; 4202c7845551SPing-Ke Shih 4203c7845551SPing-Ke Shih static 4204c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev, 4205c7845551SPing-Ke Shih enum rtw89_mac_idx mac_idx, 4206c7845551SPing-Ke Shih enum rtw89_tssi_bandedge_cfg bandedge_cfg, 4207c7845551SPing-Ke Shih u32 val) 4208c7845551SPing-Ke Shih { 4209c7845551SPing-Ke Shih const struct rtw89_reg_def *regs; 4210c7845551SPing-Ke Shih u32 reg; 4211c7845551SPing-Ke Shih int i; 4212c7845551SPing-Ke Shih 4213c7845551SPing-Ke Shih if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 4214c7845551SPing-Ke Shih regs = rtw89_tssi_fastmode_regs_flat; 4215c7845551SPing-Ke Shih else 4216c7845551SPing-Ke Shih regs = rtw89_tssi_fastmode_regs_level; 4217c7845551SPing-Ke Shih 4218c7845551SPing-Ke Shih for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) { 4219c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx); 4220c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, regs[i].mask, val); 4221c7845551SPing-Ke Shih } 4222c7845551SPing-Ke Shih } 4223c7845551SPing-Ke Shih 4224c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = { 4225c7845551SPing-Ke Shih {0xD91C, 0xff000000}, 4226c7845551SPing-Ke Shih {0xD920, 0xff}, 4227c7845551SPing-Ke Shih {0xD920, 0xff00}, 4228c7845551SPing-Ke Shih {0xD920, 0xff0000}, 4229c7845551SPing-Ke Shih {0xD920, 0xff000000}, 4230c7845551SPing-Ke Shih {0xD924, 0xff}, 4231c7845551SPing-Ke Shih {0xD924, 0xff00}, 4232c7845551SPing-Ke Shih {0xD914, 0xff000000}, 4233c7845551SPing-Ke Shih {0xD918, 0xff}, 4234c7845551SPing-Ke Shih {0xD918, 0xff00}, 4235c7845551SPing-Ke Shih {0xD918, 0xff0000}, 4236c7845551SPing-Ke Shih {0xD918, 0xff000000}, 4237c7845551SPing-Ke Shih {0xD91C, 0xff}, 4238c7845551SPing-Ke Shih {0xD91C, 0xff00}, 4239c7845551SPing-Ke Shih {0xD91C, 0xff0000}, 4240c7845551SPing-Ke Shih }; 4241c7845551SPing-Ke Shih 4242c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = { 4243c7845551SPing-Ke Shih {0xD910, 0xff}, 4244c7845551SPing-Ke Shih {0xD910, 0xff00}, 4245c7845551SPing-Ke Shih {0xD910, 0xff0000}, 4246c7845551SPing-Ke Shih {0xD910, 0xff000000}, 4247c7845551SPing-Ke Shih {0xD914, 0xff}, 4248c7845551SPing-Ke Shih {0xD914, 0xff00}, 4249c7845551SPing-Ke Shih {0xD914, 0xff0000}, 4250c7845551SPing-Ke Shih {0xD908, 0xff}, 4251c7845551SPing-Ke Shih {0xD908, 0xff00}, 4252c7845551SPing-Ke Shih {0xD908, 0xff0000}, 4253c7845551SPing-Ke Shih {0xD908, 0xff000000}, 4254c7845551SPing-Ke Shih {0xD90C, 0xff}, 4255c7845551SPing-Ke Shih {0xD90C, 0xff00}, 4256c7845551SPing-Ke Shih {0xD90C, 0xff0000}, 4257c7845551SPing-Ke Shih {0xD90C, 0xff000000}, 4258c7845551SPing-Ke Shih }; 4259c7845551SPing-Ke Shih 4260c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev, 4261c7845551SPing-Ke Shih enum rtw89_mac_idx mac_idx, 4262c7845551SPing-Ke Shih enum rtw89_tssi_bandedge_cfg bandedge_cfg) 4263c7845551SPing-Ke Shih { 4264c7845551SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 4265c7845551SPing-Ke Shih const struct rtw89_reg_def *regs; 4266c7845551SPing-Ke Shih const u32 *data; 4267c7845551SPing-Ke Shih u32 reg; 4268c7845551SPing-Ke Shih int i; 4269c7845551SPing-Ke Shih 4270c7845551SPing-Ke Shih if (bandedge_cfg >= RTW89_TSSI_CFG_NUM) 4271c7845551SPing-Ke Shih return; 4272c7845551SPing-Ke Shih 4273c7845551SPing-Ke Shih if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 4274c7845551SPing-Ke Shih regs = rtw89_tssi_bandedge_regs_flat; 4275c7845551SPing-Ke Shih else 4276c7845551SPing-Ke Shih regs = rtw89_tssi_bandedge_regs_level; 4277c7845551SPing-Ke Shih 4278c7845551SPing-Ke Shih data = chip->tssi_dbw_table->data[bandedge_cfg]; 4279c7845551SPing-Ke Shih 4280c7845551SPing-Ke Shih for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) { 4281c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx); 4282c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]); 4283c7845551SPing-Ke Shih } 4284c7845551SPing-Ke Shih 4285c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx); 4286c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg); 4287c7845551SPing-Ke Shih 4288c7845551SPing-Ke Shih rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg, 4289c7845551SPing-Ke Shih data[RTW89_TSSI_SBW20]); 4290c7845551SPing-Ke Shih } 4291c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg); 4292