1e3ec7017SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2e3ec7017SPing-Ke Shih /* Copyright(c) 2019-2020 Realtek Corporation 3e3ec7017SPing-Ke Shih */ 4e3ec7017SPing-Ke Shih 529136c95SEric Huang #include "coex.h" 6e3ec7017SPing-Ke Shih #include "debug.h" 7e3ec7017SPing-Ke Shih #include "fw.h" 88379fa61SYuan-Han Zhang #include "mac.h" 9e3ec7017SPing-Ke Shih #include "phy.h" 10e3ec7017SPing-Ke Shih #include "ps.h" 11e3ec7017SPing-Ke Shih #include "reg.h" 12e3ec7017SPing-Ke Shih #include "sar.h" 13e3715859SEric Huang #include "txrx.h" 1429136c95SEric Huang #include "util.h" 15e3ec7017SPing-Ke Shih 16e3ec7017SPing-Ke Shih static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev, 17e3ec7017SPing-Ke Shih const struct rtw89_ra_report *report) 18e3ec7017SPing-Ke Shih { 19e3ec7017SPing-Ke Shih u32 bit_rate = report->bit_rate; 20e3ec7017SPing-Ke Shih 21e3ec7017SPing-Ke Shih /* lower than ofdm, do not aggregate */ 22e3ec7017SPing-Ke Shih if (bit_rate < 550) 23e3ec7017SPing-Ke Shih return 1; 24e3ec7017SPing-Ke Shih 250d466f05SPing-Ke Shih /* avoid AMSDU for legacy rate */ 260d466f05SPing-Ke Shih if (report->might_fallback_legacy) 27e3ec7017SPing-Ke Shih return 1; 28e3ec7017SPing-Ke Shih 29e3ec7017SPing-Ke Shih /* lower than 20M vht 2ss mcs8, make it small */ 30e3ec7017SPing-Ke Shih if (bit_rate < 1800) 31e3ec7017SPing-Ke Shih return 1200; 32e3ec7017SPing-Ke Shih 33e3ec7017SPing-Ke Shih /* lower than 40M vht 2ss mcs9, make it medium */ 34e3ec7017SPing-Ke Shih if (bit_rate < 4000) 35e3ec7017SPing-Ke Shih return 2600; 36e3ec7017SPing-Ke Shih 37e3ec7017SPing-Ke Shih /* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */ 38e3ec7017SPing-Ke Shih if (bit_rate < 7000) 39e3ec7017SPing-Ke Shih return 3500; 40e3ec7017SPing-Ke Shih 41e3ec7017SPing-Ke Shih return rtwdev->chip->max_amsdu_limit; 42e3ec7017SPing-Ke Shih } 43e3ec7017SPing-Ke Shih 44e3ec7017SPing-Ke Shih static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap) 45e3ec7017SPing-Ke Shih { 46e3ec7017SPing-Ke Shih u64 ra_mask = 0; 47e3ec7017SPing-Ke Shih u8 mcs_cap; 48e3ec7017SPing-Ke Shih int i, nss; 49e3ec7017SPing-Ke Shih 50e3ec7017SPing-Ke Shih for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) { 51e3ec7017SPing-Ke Shih mcs_cap = mcs_map & 0x3; 52e3ec7017SPing-Ke Shih switch (mcs_cap) { 53e3ec7017SPing-Ke Shih case 2: 54e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss; 55e3ec7017SPing-Ke Shih break; 56e3ec7017SPing-Ke Shih case 1: 57e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss; 58e3ec7017SPing-Ke Shih break; 59e3ec7017SPing-Ke Shih case 0: 60e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss; 61e3ec7017SPing-Ke Shih break; 62e3ec7017SPing-Ke Shih default: 63e3ec7017SPing-Ke Shih break; 64e3ec7017SPing-Ke Shih } 65e3ec7017SPing-Ke Shih } 66e3ec7017SPing-Ke Shih 67e3ec7017SPing-Ke Shih return ra_mask; 68e3ec7017SPing-Ke Shih } 69e3ec7017SPing-Ke Shih 70e3ec7017SPing-Ke Shih static u64 get_he_ra_mask(struct ieee80211_sta *sta) 71e3ec7017SPing-Ke Shih { 72046d2e7cSSriram R struct ieee80211_sta_he_cap cap = sta->deflink.he_cap; 73e3ec7017SPing-Ke Shih u16 mcs_map; 74e3ec7017SPing-Ke Shih 75046d2e7cSSriram R switch (sta->deflink.bandwidth) { 76e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_160: 77e3ec7017SPing-Ke Shih if (cap.he_cap_elem.phy_cap_info[0] & 78e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) 79e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80); 80e3ec7017SPing-Ke Shih else 81e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160); 82e3ec7017SPing-Ke Shih break; 83e3ec7017SPing-Ke Shih default: 84e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80); 85e3ec7017SPing-Ke Shih } 86e3ec7017SPing-Ke Shih 87e3ec7017SPing-Ke Shih /* MCS11, MCS9, MCS7 */ 88e3ec7017SPing-Ke Shih return get_mcs_ra_mask(mcs_map, 11, 2); 89e3ec7017SPing-Ke Shih } 90e3ec7017SPing-Ke Shih 91e3ec7017SPing-Ke Shih #define RA_FLOOR_TABLE_SIZE 7 92e3ec7017SPing-Ke Shih #define RA_FLOOR_UP_GAP 3 93e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi, 94e3ec7017SPing-Ke Shih u8 ratr_state) 95e3ec7017SPing-Ke Shih { 96e3ec7017SPing-Ke Shih u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100}; 97e3ec7017SPing-Ke Shih u8 rssi_lv = 0; 98e3ec7017SPing-Ke Shih u8 i; 99e3ec7017SPing-Ke Shih 100e3ec7017SPing-Ke Shih rssi >>= 1; 101e3ec7017SPing-Ke Shih for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 102e3ec7017SPing-Ke Shih if (i >= ratr_state) 103e3ec7017SPing-Ke Shih rssi_lv_t[i] += RA_FLOOR_UP_GAP; 104e3ec7017SPing-Ke Shih if (rssi < rssi_lv_t[i]) { 105e3ec7017SPing-Ke Shih rssi_lv = i; 106e3ec7017SPing-Ke Shih break; 107e3ec7017SPing-Ke Shih } 108e3ec7017SPing-Ke Shih } 109e3ec7017SPing-Ke Shih if (rssi_lv == 0) 110e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 111e3ec7017SPing-Ke Shih else if (rssi_lv == 1) 112e3ec7017SPing-Ke Shih return 0xfffffffffffffff0ULL; 113e3ec7017SPing-Ke Shih else if (rssi_lv == 2) 1143c2c2e2eSChien-Hsun Liao return 0xffffffffffffefe0ULL; 115e3ec7017SPing-Ke Shih else if (rssi_lv == 3) 1163c2c2e2eSChien-Hsun Liao return 0xffffffffffffcfc0ULL; 117e3ec7017SPing-Ke Shih else if (rssi_lv == 4) 1183c2c2e2eSChien-Hsun Liao return 0xffffffffffff8f80ULL; 119e3ec7017SPing-Ke Shih else if (rssi_lv >= 5) 1203c2c2e2eSChien-Hsun Liao return 0xffffffffffff0f00ULL; 121e3ec7017SPing-Ke Shih 122e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 123e3ec7017SPing-Ke Shih } 124e3ec7017SPing-Ke Shih 1253c2c2e2eSChien-Hsun Liao static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak) 1263c2c2e2eSChien-Hsun Liao { 1273c2c2e2eSChien-Hsun Liao if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0) 1283c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1293c2c2e2eSChien-Hsun Liao 1303c2c2e2eSChien-Hsun Liao if (ra_mask == 0) 1313c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1323c2c2e2eSChien-Hsun Liao 1333c2c2e2eSChien-Hsun Liao return ra_mask; 1343c2c2e2eSChien-Hsun Liao } 1353c2c2e2eSChien-Hsun Liao 136e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta) 137e3ec7017SPing-Ke Shih { 138e3ec7017SPing-Ke Shih struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta); 139cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 140e3ec7017SPing-Ke Shih struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 141e3ec7017SPing-Ke Shih enum nl80211_band band; 142e3ec7017SPing-Ke Shih u64 cfg_mask; 143e3ec7017SPing-Ke Shih 144e3ec7017SPing-Ke Shih if (!rtwsta->use_cfg_mask) 145e3ec7017SPing-Ke Shih return -1; 146e3ec7017SPing-Ke Shih 147cbb145b9SZong-Zhe Yang switch (chan->band_type) { 148e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 149e3ec7017SPing-Ke Shih band = NL80211_BAND_2GHZ; 150e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy, 151e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES); 152e3ec7017SPing-Ke Shih break; 153e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 154e3ec7017SPing-Ke Shih band = NL80211_BAND_5GHZ; 155e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy, 156e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES); 157e3ec7017SPing-Ke Shih break; 158f76b3276SPing-Ke Shih case RTW89_BAND_6G: 159f76b3276SPing-Ke Shih band = NL80211_BAND_6GHZ; 160f76b3276SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy, 161f76b3276SPing-Ke Shih RA_MASK_OFDM_RATES); 162f76b3276SPing-Ke Shih break; 163e3ec7017SPing-Ke Shih default: 164cbb145b9SZong-Zhe Yang rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type); 165e3ec7017SPing-Ke Shih return -1; 166e3ec7017SPing-Ke Shih } 167e3ec7017SPing-Ke Shih 168046d2e7cSSriram R if (sta->deflink.he_cap.has_he) { 169e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0], 170e3ec7017SPing-Ke Shih RA_MASK_HE_1SS_RATES); 171e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1], 172e3ec7017SPing-Ke Shih RA_MASK_HE_2SS_RATES); 173046d2e7cSSriram R } else if (sta->deflink.vht_cap.vht_supported) { 174e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0], 175e3ec7017SPing-Ke Shih RA_MASK_VHT_1SS_RATES); 176e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1], 177e3ec7017SPing-Ke Shih RA_MASK_VHT_2SS_RATES); 178046d2e7cSSriram R } else if (sta->deflink.ht_cap.ht_supported) { 179e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0], 180e3ec7017SPing-Ke Shih RA_MASK_HT_1SS_RATES); 181e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1], 182e3ec7017SPing-Ke Shih RA_MASK_HT_2SS_RATES); 183e3ec7017SPing-Ke Shih } 184e3ec7017SPing-Ke Shih 185e3ec7017SPing-Ke Shih return cfg_mask; 186e3ec7017SPing-Ke Shih } 187e3ec7017SPing-Ke Shih 188e3ec7017SPing-Ke Shih static const u64 189e3ec7017SPing-Ke Shih rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES, 190e3ec7017SPing-Ke Shih RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES}; 191e3ec7017SPing-Ke Shih static const u64 192e3ec7017SPing-Ke Shih rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES, 193e3ec7017SPing-Ke Shih RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES}; 194e3ec7017SPing-Ke Shih static const u64 195e3ec7017SPing-Ke Shih rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES, 196e3ec7017SPing-Ke Shih RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES}; 197e3ec7017SPing-Ke Shih 1980891b366SKuan-Chung Chen static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev, 1990891b366SKuan-Chung Chen struct rtw89_sta *rtwsta, 2000891b366SKuan-Chung Chen bool *fix_giltf_en, u8 *fix_giltf) 2010891b366SKuan-Chung Chen { 2020891b366SKuan-Chung Chen const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 2030891b366SKuan-Chung Chen struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 2040891b366SKuan-Chung Chen u8 band = chan->band_type; 2050891b366SKuan-Chung Chen enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 2060891b366SKuan-Chung Chen u8 he_gi = mask->control[nl_band].he_gi; 2070891b366SKuan-Chung Chen u8 he_ltf = mask->control[nl_band].he_ltf; 2080891b366SKuan-Chung Chen 2090891b366SKuan-Chung Chen if (!rtwsta->use_cfg_mask) 2100891b366SKuan-Chung Chen return; 2110891b366SKuan-Chung Chen 2120891b366SKuan-Chung Chen if (he_ltf == 2 && he_gi == 2) { 2130891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_LGI_4XHE32; 2140891b366SKuan-Chung Chen } else if (he_ltf == 2 && he_gi == 0) { 2150891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_SGI_4XHE08; 2160891b366SKuan-Chung Chen } else if (he_ltf == 1 && he_gi == 1) { 2170891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_2XHE16; 2180891b366SKuan-Chung Chen } else if (he_ltf == 1 && he_gi == 0) { 2190891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_2XHE08; 2200891b366SKuan-Chung Chen } else if (he_ltf == 0 && he_gi == 1) { 2210891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_1XHE16; 2220891b366SKuan-Chung Chen } else if (he_ltf == 0 && he_gi == 0) { 2230891b366SKuan-Chung Chen *fix_giltf = RTW89_GILTF_1XHE08; 2240891b366SKuan-Chung Chen } else { 2250891b366SKuan-Chung Chen *fix_giltf_en = false; 2260891b366SKuan-Chung Chen return; 2270891b366SKuan-Chung Chen } 2280891b366SKuan-Chung Chen 2290891b366SKuan-Chung Chen *fix_giltf_en = true; 2300891b366SKuan-Chung Chen } 2310891b366SKuan-Chung Chen 232e3ec7017SPing-Ke Shih static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev, 233e3ec7017SPing-Ke Shih struct ieee80211_sta *sta, bool csi) 234e3ec7017SPing-Ke Shih { 235e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 236e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = rtwsta->rtwvif; 237e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern; 238e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 239cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 2403788c599SDian-Syuan Yang struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif); 241e3ec7017SPing-Ke Shih const u64 *high_rate_masks = rtw89_ra_mask_ht_rates; 242e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi); 243e3ec7017SPing-Ke Shih u64 ra_mask = 0; 2443c2c2e2eSChien-Hsun Liao u64 ra_mask_bak; 245e3ec7017SPing-Ke Shih u8 mode = 0; 246e3ec7017SPing-Ke Shih u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY; 247e3ec7017SPing-Ke Shih u8 bw_mode = 0; 248e3ec7017SPing-Ke Shih u8 stbc_en = 0; 249e3ec7017SPing-Ke Shih u8 ldpc_en = 0; 2500891b366SKuan-Chung Chen u8 fix_giltf = 0; 251e3ec7017SPing-Ke Shih u8 i; 252e3ec7017SPing-Ke Shih bool sgi = false; 2530891b366SKuan-Chung Chen bool fix_giltf_en = false; 254e3ec7017SPing-Ke Shih 255e3ec7017SPing-Ke Shih memset(ra, 0, sizeof(*ra)); 256e3ec7017SPing-Ke Shih /* Set the ra mask from sta's capability */ 257046d2e7cSSriram R if (sta->deflink.he_cap.has_he) { 258e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HE; 259e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HE; 260e3ec7017SPing-Ke Shih ra_mask |= get_he_ra_mask(sta); 261e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_he_rates; 262046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] & 263e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ) 264e3ec7017SPing-Ke Shih stbc_en = 1; 265046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] & 266e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD) 267e3ec7017SPing-Ke Shih ldpc_en = 1; 2680891b366SKuan-Chung Chen rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, &fix_giltf_en, &fix_giltf); 269046d2e7cSSriram R } else if (sta->deflink.vht_cap.vht_supported) { 270046d2e7cSSriram R u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map); 271e3ec7017SPing-Ke Shih 272e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_VHT; 273e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_VHT; 274e3ec7017SPing-Ke Shih /* MCS9, MCS8, MCS7 */ 275e3ec7017SPing-Ke Shih ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1); 276e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_vht_rates; 277046d2e7cSSriram R if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK) 278e3ec7017SPing-Ke Shih stbc_en = 1; 279046d2e7cSSriram R if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC) 280e3ec7017SPing-Ke Shih ldpc_en = 1; 281046d2e7cSSriram R } else if (sta->deflink.ht_cap.ht_supported) { 282e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HT; 283e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HT; 284046d2e7cSSriram R ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) | 285046d2e7cSSriram R ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) | 286046d2e7cSSriram R (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) | 287046d2e7cSSriram R (sta->deflink.ht_cap.mcs.rx_mask[0] << 12); 288e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_ht_rates; 289046d2e7cSSriram R if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC) 290e3ec7017SPing-Ke Shih stbc_en = 1; 291046d2e7cSSriram R if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING) 292e3ec7017SPing-Ke Shih ldpc_en = 1; 293e3ec7017SPing-Ke Shih } 294e3ec7017SPing-Ke Shih 295cbb145b9SZong-Zhe Yang switch (chan->band_type) { 296f76b3276SPing-Ke Shih case RTW89_BAND_2G: 297046d2e7cSSriram R ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ]; 2983788c599SDian-Syuan Yang if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf) 299e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_CCK; 3003788c599SDian-Syuan Yang if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0) 3013788c599SDian-Syuan Yang mode |= RTW89_RA_MODE_OFDM; 302f76b3276SPing-Ke Shih break; 303f76b3276SPing-Ke Shih case RTW89_BAND_5G: 304046d2e7cSSriram R ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4; 305e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 306f76b3276SPing-Ke Shih break; 307f76b3276SPing-Ke Shih case RTW89_BAND_6G: 308046d2e7cSSriram R ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4; 309f76b3276SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 310f76b3276SPing-Ke Shih break; 311f76b3276SPing-Ke Shih default: 312f76b3276SPing-Ke Shih rtw89_err(rtwdev, "Unknown band type\n"); 313f76b3276SPing-Ke Shih break; 314e3ec7017SPing-Ke Shih } 315e3ec7017SPing-Ke Shih 3163c2c2e2eSChien-Hsun Liao ra_mask_bak = ra_mask; 3173c2c2e2eSChien-Hsun Liao 318e3ec7017SPing-Ke Shih if (mode >= RTW89_RA_MODE_HT) { 3193c2c2e2eSChien-Hsun Liao u64 mask = 0; 320e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->hal.tx_nss; i++) 3213c2c2e2eSChien-Hsun Liao mask |= high_rate_masks[i]; 322e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_OFDM) 3233c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBOFDM_RATES; 324e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_CCK) 3253c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBCCK_RATES; 3263c2c2e2eSChien-Hsun Liao ra_mask &= mask; 327e3ec7017SPing-Ke Shih } else if (mode & RTW89_RA_MODE_OFDM) { 3283c2c2e2eSChien-Hsun Liao ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES); 329e3ec7017SPing-Ke Shih } 330e3ec7017SPing-Ke Shih 3313c2c2e2eSChien-Hsun Liao if (mode != RTW89_RA_MODE_CCK) 332e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0); 3333c2c2e2eSChien-Hsun Liao 3343c2c2e2eSChien-Hsun Liao ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak); 335e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 336e3ec7017SPing-Ke Shih 337046d2e7cSSriram R switch (sta->deflink.bandwidth) { 338167044afSPing-Ke Shih case IEEE80211_STA_RX_BW_160: 339167044afSPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_160; 340046d2e7cSSriram R sgi = sta->deflink.vht_cap.vht_supported && 341046d2e7cSSriram R (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160); 342167044afSPing-Ke Shih break; 343e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_80: 344e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_80; 345046d2e7cSSriram R sgi = sta->deflink.vht_cap.vht_supported && 346046d2e7cSSriram R (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80); 347e3ec7017SPing-Ke Shih break; 348e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_40: 349e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_40; 350046d2e7cSSriram R sgi = sta->deflink.ht_cap.ht_supported && 351046d2e7cSSriram R (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40); 352e3ec7017SPing-Ke Shih break; 353e3ec7017SPing-Ke Shih default: 354e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_20; 355046d2e7cSSriram R sgi = sta->deflink.ht_cap.ht_supported && 356046d2e7cSSriram R (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20); 357e3ec7017SPing-Ke Shih break; 358e3ec7017SPing-Ke Shih } 359e3ec7017SPing-Ke Shih 360046d2e7cSSriram R if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] & 361e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM) 362e3ec7017SPing-Ke Shih ra->dcm_cap = 1; 363e3ec7017SPing-Ke Shih 3643788c599SDian-Syuan Yang if (rate_pattern->enable && !vif->p2p) { 365e3ec7017SPing-Ke Shih ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 366e3ec7017SPing-Ke Shih ra_mask &= rate_pattern->ra_mask; 367e3ec7017SPing-Ke Shih mode = rate_pattern->ra_mode; 368e3ec7017SPing-Ke Shih } 369e3ec7017SPing-Ke Shih 370e3ec7017SPing-Ke Shih ra->bw_cap = bw_mode; 37125ed1a17SPing-Ke Shih ra->er_cap = rtwsta->er_cap; 372e3ec7017SPing-Ke Shih ra->mode_ctrl = mode; 373e3ec7017SPing-Ke Shih ra->macid = rtwsta->mac_id; 374e3ec7017SPing-Ke Shih ra->stbc_cap = stbc_en; 375e3ec7017SPing-Ke Shih ra->ldpc_cap = ldpc_en; 376046d2e7cSSriram R ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1; 377e3ec7017SPing-Ke Shih ra->en_sgi = sgi; 378e3ec7017SPing-Ke Shih ra->ra_mask = ra_mask; 3790891b366SKuan-Chung Chen ra->fix_giltf_en = fix_giltf_en; 3800891b366SKuan-Chung Chen ra->fix_giltf = fix_giltf; 381e3ec7017SPing-Ke Shih 382e3ec7017SPing-Ke Shih if (!csi) 383e3ec7017SPing-Ke Shih return; 384e3ec7017SPing-Ke Shih 385e3ec7017SPing-Ke Shih ra->fixed_csi_rate_en = false; 386e3ec7017SPing-Ke Shih ra->ra_csi_rate_en = true; 387e3ec7017SPing-Ke Shih ra->cr_tbl_sel = false; 388e3ec7017SPing-Ke Shih ra->band_num = rtwvif->phy_idx; 389e3ec7017SPing-Ke Shih ra->csi_bw = bw_mode; 390e3ec7017SPing-Ke Shih ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32; 391e3ec7017SPing-Ke Shih ra->csi_mcs_ss_idx = 5; 392e3ec7017SPing-Ke Shih ra->csi_mode = csi_mode; 393e3ec7017SPing-Ke Shih } 394e3ec7017SPing-Ke Shih 3959d9a9edcSPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta, 3969d9a9edcSPing-Ke Shih u32 changed) 397e3ec7017SPing-Ke Shih { 398e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 399e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 400e3ec7017SPing-Ke Shih 401e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, false); 4029d9a9edcSPing-Ke Shih 4039d9a9edcSPing-Ke Shih if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) 404e3ec7017SPing-Ke Shih ra->upd_mask = 1; 4059d9a9edcSPing-Ke Shih if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED)) 4069d9a9edcSPing-Ke Shih ra->upd_bw_nss_mask = 1; 4079d9a9edcSPing-Ke Shih 408e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 409e3ec7017SPing-Ke Shih "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d", 410e3ec7017SPing-Ke Shih ra->macid, 411e3ec7017SPing-Ke Shih ra->bw_cap, 412e3ec7017SPing-Ke Shih ra->ss_num, 413e3ec7017SPing-Ke Shih ra->en_sgi, 414e3ec7017SPing-Ke Shih ra->giltf); 415e3ec7017SPing-Ke Shih 416e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, false); 417e3ec7017SPing-Ke Shih } 418e3ec7017SPing-Ke Shih 419e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next, 420e3ec7017SPing-Ke Shih u16 rate_base, u64 ra_mask, u8 ra_mode, 421e3ec7017SPing-Ke Shih u32 rate_ctrl, u32 ctrl_skip, bool force) 422e3ec7017SPing-Ke Shih { 423e3ec7017SPing-Ke Shih u8 n, c; 424e3ec7017SPing-Ke Shih 425e3ec7017SPing-Ke Shih if (rate_ctrl == ctrl_skip) 426e3ec7017SPing-Ke Shih return true; 427e3ec7017SPing-Ke Shih 428e3ec7017SPing-Ke Shih n = hweight32(rate_ctrl); 429e3ec7017SPing-Ke Shih if (n == 0) 430e3ec7017SPing-Ke Shih return true; 431e3ec7017SPing-Ke Shih 432e3ec7017SPing-Ke Shih if (force && n != 1) 433e3ec7017SPing-Ke Shih return false; 434e3ec7017SPing-Ke Shih 435e3ec7017SPing-Ke Shih if (next->enable) 436e3ec7017SPing-Ke Shih return false; 437e3ec7017SPing-Ke Shih 438e3ec7017SPing-Ke Shih c = __fls(rate_ctrl); 439e3ec7017SPing-Ke Shih next->rate = rate_base + c; 440e3ec7017SPing-Ke Shih next->ra_mode = ra_mode; 441e3ec7017SPing-Ke Shih next->ra_mask = ra_mask; 442e3ec7017SPing-Ke Shih next->enable = true; 443e3ec7017SPing-Ke Shih 444e3ec7017SPing-Ke Shih return true; 445e3ec7017SPing-Ke Shih } 446e3ec7017SPing-Ke Shih 4472ef14155SZong-Zhe Yang #define RTW89_HW_RATE_BY_CHIP_GEN(rate) \ 4482ef14155SZong-Zhe Yang { \ 4492ef14155SZong-Zhe Yang [RTW89_CHIP_AX] = RTW89_HW_RATE_ ## rate, \ 4502ef14155SZong-Zhe Yang [RTW89_CHIP_BE] = RTW89_HW_RATE_V1_ ## rate, \ 4512ef14155SZong-Zhe Yang } 4522ef14155SZong-Zhe Yang 453e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev, 454e3ec7017SPing-Ke Shih struct ieee80211_vif *vif, 455e3ec7017SPing-Ke Shih const struct cfg80211_bitrate_mask *mask) 456e3ec7017SPing-Ke Shih { 457e3ec7017SPing-Ke Shih struct ieee80211_supported_band *sband; 458e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 459e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern next_pattern = {0}; 460cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 4612ef14155SZong-Zhe Yang static const u16 hw_rate_he[][RTW89_CHIP_GEN_NUM] = { 4622ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS1_MCS0), 4632ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS2_MCS0), 4642ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS3_MCS0), 4652ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS4_MCS0), 4662ef14155SZong-Zhe Yang }; 4672ef14155SZong-Zhe Yang static const u16 hw_rate_vht[][RTW89_CHIP_GEN_NUM] = { 4682ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS1_MCS0), 4692ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS2_MCS0), 4702ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS3_MCS0), 4712ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS4_MCS0), 4722ef14155SZong-Zhe Yang }; 4732ef14155SZong-Zhe Yang static const u16 hw_rate_ht[][RTW89_CHIP_GEN_NUM] = { 4742ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(MCS0), 4752ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(MCS8), 4762ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(MCS16), 4772ef14155SZong-Zhe Yang RTW89_HW_RATE_BY_CHIP_GEN(MCS24), 4782ef14155SZong-Zhe Yang }; 479cbb145b9SZong-Zhe Yang u8 band = chan->band_type; 480a06d2dd7SZong-Zhe Yang enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 4812ef14155SZong-Zhe Yang enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen; 482e3ec7017SPing-Ke Shih u8 tx_nss = rtwdev->hal.tx_nss; 483e3ec7017SPing-Ke Shih u8 i; 484e3ec7017SPing-Ke Shih 485e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 4862ef14155SZong-Zhe Yang if (!__check_rate_pattern(&next_pattern, hw_rate_he[i][chip_gen], 487e3ec7017SPing-Ke Shih RA_MASK_HE_RATES, RTW89_RA_MODE_HE, 488a06d2dd7SZong-Zhe Yang mask->control[nl_band].he_mcs[i], 489e3ec7017SPing-Ke Shih 0, true)) 490e3ec7017SPing-Ke Shih goto out; 491e3ec7017SPing-Ke Shih 492e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 4932ef14155SZong-Zhe Yang if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i][chip_gen], 494e3ec7017SPing-Ke Shih RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT, 495a06d2dd7SZong-Zhe Yang mask->control[nl_band].vht_mcs[i], 496e3ec7017SPing-Ke Shih 0, true)) 497e3ec7017SPing-Ke Shih goto out; 498e3ec7017SPing-Ke Shih 499e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 5002ef14155SZong-Zhe Yang if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i][chip_gen], 501e3ec7017SPing-Ke Shih RA_MASK_HT_RATES, RTW89_RA_MODE_HT, 502a06d2dd7SZong-Zhe Yang mask->control[nl_band].ht_mcs[i], 503e3ec7017SPing-Ke Shih 0, true)) 504e3ec7017SPing-Ke Shih goto out; 505e3ec7017SPing-Ke Shih 506e3ec7017SPing-Ke Shih /* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and 507e3ec7017SPing-Ke Shih * require at least one basic rate for ieee80211_set_bitrate_mask, 508e3ec7017SPing-Ke Shih * so the decision just depends on if all bitrates are set or not. 509e3ec7017SPing-Ke Shih */ 510a06d2dd7SZong-Zhe Yang sband = rtwdev->hw->wiphy->bands[nl_band]; 511e3ec7017SPing-Ke Shih if (band == RTW89_BAND_2G) { 512e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1, 513e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES, 514e3ec7017SPing-Ke Shih RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM, 515a06d2dd7SZong-Zhe Yang mask->control[nl_band].legacy, 516e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 517e3ec7017SPing-Ke Shih goto out; 518e3ec7017SPing-Ke Shih } else { 519e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6, 520e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM, 521a06d2dd7SZong-Zhe Yang mask->control[nl_band].legacy, 522e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 523e3ec7017SPing-Ke Shih goto out; 524e3ec7017SPing-Ke Shih } 525e3ec7017SPing-Ke Shih 526e3ec7017SPing-Ke Shih if (!next_pattern.enable) 527e3ec7017SPing-Ke Shih goto out; 528e3ec7017SPing-Ke Shih 529e3ec7017SPing-Ke Shih rtwvif->rate_pattern = next_pattern; 530e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 531e3ec7017SPing-Ke Shih "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n", 532e3ec7017SPing-Ke Shih next_pattern.rate, 533e3ec7017SPing-Ke Shih next_pattern.ra_mask, 534e3ec7017SPing-Ke Shih next_pattern.ra_mode); 535e3ec7017SPing-Ke Shih return; 536e3ec7017SPing-Ke Shih 537e3ec7017SPing-Ke Shih out: 538e3ec7017SPing-Ke Shih rtwvif->rate_pattern.enable = false; 539e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n"); 540e3ec7017SPing-Ke Shih } 541e3ec7017SPing-Ke Shih 542e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta) 543e3ec7017SPing-Ke Shih { 544e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = (struct rtw89_dev *)data; 545e3ec7017SPing-Ke Shih 5469d9a9edcSPing-Ke Shih rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED); 547e3ec7017SPing-Ke Shih } 548e3ec7017SPing-Ke Shih 549e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev) 550e3ec7017SPing-Ke Shih { 551e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 552e3ec7017SPing-Ke Shih rtw89_phy_ra_updata_sta_iter, 553e3ec7017SPing-Ke Shih rtwdev); 554e3ec7017SPing-Ke Shih } 555e3ec7017SPing-Ke Shih 556e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta) 557e3ec7017SPing-Ke Shih { 558e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 559e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 560e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR; 561e3ec7017SPing-Ke Shih bool csi = rtw89_sta_has_beamformer_cap(sta); 562e3ec7017SPing-Ke Shih 563e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, csi); 564e3ec7017SPing-Ke Shih 565e3ec7017SPing-Ke Shih if (rssi > 40) 566e3ec7017SPing-Ke Shih ra->init_rate_lv = 1; 567e3ec7017SPing-Ke Shih else if (rssi > 20) 568e3ec7017SPing-Ke Shih ra->init_rate_lv = 2; 569e3ec7017SPing-Ke Shih else if (rssi > 1) 570e3ec7017SPing-Ke Shih ra->init_rate_lv = 3; 571e3ec7017SPing-Ke Shih else 572e3ec7017SPing-Ke Shih ra->init_rate_lv = 0; 573e3ec7017SPing-Ke Shih ra->upd_all = 1; 574e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 575e3ec7017SPing-Ke Shih "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d", 576e3ec7017SPing-Ke Shih ra->macid, 577e3ec7017SPing-Ke Shih ra->mode_ctrl, 578e3ec7017SPing-Ke Shih ra->bw_cap, 579e3ec7017SPing-Ke Shih ra->ss_num, 580e3ec7017SPing-Ke Shih ra->init_rate_lv); 581e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 582e3ec7017SPing-Ke Shih "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d", 583e3ec7017SPing-Ke Shih ra->dcm_cap, 584e3ec7017SPing-Ke Shih ra->er_cap, 585e3ec7017SPing-Ke Shih ra->ldpc_cap, 586e3ec7017SPing-Ke Shih ra->stbc_cap, 587e3ec7017SPing-Ke Shih ra->en_sgi, 588e3ec7017SPing-Ke Shih ra->giltf); 589e3ec7017SPing-Ke Shih 590e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, csi); 591e3ec7017SPing-Ke Shih } 592e3ec7017SPing-Ke Shih 593e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev, 5943e5831caSZong-Zhe Yang const struct rtw89_chan *chan, 595e3ec7017SPing-Ke Shih enum rtw89_bandwidth dbw) 596e3ec7017SPing-Ke Shih { 5973e5831caSZong-Zhe Yang enum rtw89_bandwidth cbw = chan->band_width; 5983e5831caSZong-Zhe Yang u8 pri_ch = chan->primary_channel; 5993e5831caSZong-Zhe Yang u8 central_ch = chan->channel; 600e3ec7017SPing-Ke Shih u8 txsc_idx = 0; 601e3ec7017SPing-Ke Shih u8 tmp = 0; 602e3ec7017SPing-Ke Shih 603e3ec7017SPing-Ke Shih if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20) 604e3ec7017SPing-Ke Shih return txsc_idx; 605e3ec7017SPing-Ke Shih 606e3ec7017SPing-Ke Shih switch (cbw) { 607e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 608e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 1 : 2; 609e3ec7017SPing-Ke Shih break; 610e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 611e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 612e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 613e3ec7017SPing-Ke Shih txsc_idx = (pri_ch - central_ch) >> 1; 614e3ec7017SPing-Ke Shih else 615e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 1; 616e3ec7017SPing-Ke Shih } else { 617e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 9 : 10; 618e3ec7017SPing-Ke Shih } 619e3ec7017SPing-Ke Shih break; 620e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_160: 621e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 622e3ec7017SPing-Ke Shih tmp = (pri_ch - central_ch) >> 1; 623e3ec7017SPing-Ke Shih else 624e3ec7017SPing-Ke Shih tmp = ((central_ch - pri_ch) >> 1) + 1; 625e3ec7017SPing-Ke Shih 626e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 627e3ec7017SPing-Ke Shih txsc_idx = tmp; 628e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 629e3ec7017SPing-Ke Shih if (tmp == 1 || tmp == 3) 630e3ec7017SPing-Ke Shih txsc_idx = 9; 631e3ec7017SPing-Ke Shih else if (tmp == 5 || tmp == 7) 632e3ec7017SPing-Ke Shih txsc_idx = 11; 633e3ec7017SPing-Ke Shih else if (tmp == 2 || tmp == 4) 634e3ec7017SPing-Ke Shih txsc_idx = 10; 635e3ec7017SPing-Ke Shih else if (tmp == 6 || tmp == 8) 636e3ec7017SPing-Ke Shih txsc_idx = 12; 637e3ec7017SPing-Ke Shih else 638e3ec7017SPing-Ke Shih return 0xff; 639e3ec7017SPing-Ke Shih } else { 640e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 13 : 14; 641e3ec7017SPing-Ke Shih } 642e3ec7017SPing-Ke Shih break; 643e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80_80: 644e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 645e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 646e3ec7017SPing-Ke Shih txsc_idx = (10 - (pri_ch - central_ch)) >> 1; 647e3ec7017SPing-Ke Shih else 648e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 5; 649e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 650e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 10 : 12; 651e3ec7017SPing-Ke Shih } else { 652e3ec7017SPing-Ke Shih txsc_idx = 14; 653e3ec7017SPing-Ke Shih } 654e3ec7017SPing-Ke Shih break; 655e3ec7017SPing-Ke Shih default: 656e3ec7017SPing-Ke Shih break; 657e3ec7017SPing-Ke Shih } 658e3ec7017SPing-Ke Shih 659e3ec7017SPing-Ke Shih return txsc_idx; 660e3ec7017SPing-Ke Shih } 661861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc); 662e3ec7017SPing-Ke Shih 66384d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev) 66484d0e33eSChung-Hsuan Hung { 66584d0e33eSChung-Hsuan Hung return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) || 66684d0e33eSChung-Hsuan Hung !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1); 66784d0e33eSChung-Hsuan Hung } 66884d0e33eSChung-Hsuan Hung 669e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 670e3ec7017SPing-Ke Shih u32 addr, u32 mask) 671e3ec7017SPing-Ke Shih { 672e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 673e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 674e3ec7017SPing-Ke Shih u32 val, direct_addr; 675e3ec7017SPing-Ke Shih 676e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 677e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 678e3ec7017SPing-Ke Shih return INV_RF_DATA; 679e3ec7017SPing-Ke Shih } 680e3ec7017SPing-Ke Shih 681e3ec7017SPing-Ke Shih addr &= 0xff; 682e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 683e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 684e3ec7017SPing-Ke Shih 685e3ec7017SPing-Ke Shih val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask); 686e3ec7017SPing-Ke Shih 687e3ec7017SPing-Ke Shih return val; 688e3ec7017SPing-Ke Shih } 689e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf); 690e3ec7017SPing-Ke Shih 69184d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev, 69284d0e33eSChung-Hsuan Hung enum rtw89_rf_path rf_path, u32 addr, u32 mask) 69384d0e33eSChung-Hsuan Hung { 69484d0e33eSChung-Hsuan Hung bool busy; 69584d0e33eSChung-Hsuan Hung bool done; 69684d0e33eSChung-Hsuan Hung u32 val; 69784d0e33eSChung-Hsuan Hung int ret; 69884d0e33eSChung-Hsuan Hung 69984d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 70084d0e33eSChung-Hsuan Hung 1, 30, false, rtwdev); 70184d0e33eSChung-Hsuan Hung if (ret) { 70284d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "read rf busy swsi\n"); 70384d0e33eSChung-Hsuan Hung return INV_RF_DATA; 70484d0e33eSChung-Hsuan Hung } 70584d0e33eSChung-Hsuan Hung 70684d0e33eSChung-Hsuan Hung mask &= RFREG_MASK; 70784d0e33eSChung-Hsuan Hung 70884d0e33eSChung-Hsuan Hung val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) | 70984d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr); 71084d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val); 71184d0e33eSChung-Hsuan Hung udelay(2); 71284d0e33eSChung-Hsuan Hung 71384d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1, 71484d0e33eSChung-Hsuan Hung 30, false, rtwdev, R_SWSI_V1, 71584d0e33eSChung-Hsuan Hung B_SWSI_R_DATA_DONE_V1); 71684d0e33eSChung-Hsuan Hung if (ret) { 71784d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "read swsi busy\n"); 71884d0e33eSChung-Hsuan Hung return INV_RF_DATA; 71984d0e33eSChung-Hsuan Hung } 72084d0e33eSChung-Hsuan Hung 72184d0e33eSChung-Hsuan Hung return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask); 72284d0e33eSChung-Hsuan Hung } 72384d0e33eSChung-Hsuan Hung 72484d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 72584d0e33eSChung-Hsuan Hung u32 addr, u32 mask) 72684d0e33eSChung-Hsuan Hung { 72784d0e33eSChung-Hsuan Hung bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 72884d0e33eSChung-Hsuan Hung 72984d0e33eSChung-Hsuan Hung if (rf_path >= rtwdev->chip->rf_path_num) { 73084d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 73184d0e33eSChung-Hsuan Hung return INV_RF_DATA; 73284d0e33eSChung-Hsuan Hung } 73384d0e33eSChung-Hsuan Hung 73484d0e33eSChung-Hsuan Hung if (ad_sel) 73584d0e33eSChung-Hsuan Hung return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask); 73684d0e33eSChung-Hsuan Hung else 73784d0e33eSChung-Hsuan Hung return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask); 73884d0e33eSChung-Hsuan Hung } 73984d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1); 74084d0e33eSChung-Hsuan Hung 741e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 742e3ec7017SPing-Ke Shih u32 addr, u32 mask, u32 data) 743e3ec7017SPing-Ke Shih { 744e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 745e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 746e3ec7017SPing-Ke Shih u32 direct_addr; 747e3ec7017SPing-Ke Shih 748e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 749e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 750e3ec7017SPing-Ke Shih return false; 751e3ec7017SPing-Ke Shih } 752e3ec7017SPing-Ke Shih 753e3ec7017SPing-Ke Shih addr &= 0xff; 754e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 755e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 756e3ec7017SPing-Ke Shih 757e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data); 758e3ec7017SPing-Ke Shih 759e3ec7017SPing-Ke Shih /* delay to ensure writing properly */ 760e3ec7017SPing-Ke Shih udelay(1); 761e3ec7017SPing-Ke Shih 762e3ec7017SPing-Ke Shih return true; 763e3ec7017SPing-Ke Shih } 764e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf); 765e3ec7017SPing-Ke Shih 76684d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev, 76784d0e33eSChung-Hsuan Hung enum rtw89_rf_path rf_path, u32 addr, u32 mask, 76884d0e33eSChung-Hsuan Hung u32 data) 76984d0e33eSChung-Hsuan Hung { 77084d0e33eSChung-Hsuan Hung u8 bit_shift; 77184d0e33eSChung-Hsuan Hung u32 val; 77284d0e33eSChung-Hsuan Hung bool busy, b_msk_en = false; 77384d0e33eSChung-Hsuan Hung int ret; 77484d0e33eSChung-Hsuan Hung 77584d0e33eSChung-Hsuan Hung ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 77684d0e33eSChung-Hsuan Hung 1, 30, false, rtwdev); 77784d0e33eSChung-Hsuan Hung if (ret) { 77884d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "write rf busy swsi\n"); 77984d0e33eSChung-Hsuan Hung return false; 78084d0e33eSChung-Hsuan Hung } 78184d0e33eSChung-Hsuan Hung 78284d0e33eSChung-Hsuan Hung data &= RFREG_MASK; 78384d0e33eSChung-Hsuan Hung mask &= RFREG_MASK; 78484d0e33eSChung-Hsuan Hung 78584d0e33eSChung-Hsuan Hung if (mask != RFREG_MASK) { 78684d0e33eSChung-Hsuan Hung b_msk_en = true; 78784d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK, 78884d0e33eSChung-Hsuan Hung mask); 78984d0e33eSChung-Hsuan Hung bit_shift = __ffs(mask); 79084d0e33eSChung-Hsuan Hung data = (data << bit_shift) & RFREG_MASK; 79184d0e33eSChung-Hsuan Hung } 79284d0e33eSChung-Hsuan Hung 79384d0e33eSChung-Hsuan Hung val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) | 79484d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) | 79584d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) | 79684d0e33eSChung-Hsuan Hung FIELD_PREP(B_SWSI_DATA_VAL_V1, data); 79784d0e33eSChung-Hsuan Hung 79884d0e33eSChung-Hsuan Hung rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val); 79984d0e33eSChung-Hsuan Hung 80084d0e33eSChung-Hsuan Hung return true; 80184d0e33eSChung-Hsuan Hung } 80284d0e33eSChung-Hsuan Hung 80384d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 80484d0e33eSChung-Hsuan Hung u32 addr, u32 mask, u32 data) 80584d0e33eSChung-Hsuan Hung { 80684d0e33eSChung-Hsuan Hung bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 80784d0e33eSChung-Hsuan Hung 80884d0e33eSChung-Hsuan Hung if (rf_path >= rtwdev->chip->rf_path_num) { 80984d0e33eSChung-Hsuan Hung rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 81084d0e33eSChung-Hsuan Hung return false; 81184d0e33eSChung-Hsuan Hung } 81284d0e33eSChung-Hsuan Hung 81384d0e33eSChung-Hsuan Hung if (ad_sel) 81484d0e33eSChung-Hsuan Hung return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data); 81584d0e33eSChung-Hsuan Hung else 81684d0e33eSChung-Hsuan Hung return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data); 81784d0e33eSChung-Hsuan Hung } 81884d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1); 81984d0e33eSChung-Hsuan Hung 820d9112042SChih-Kang Chang static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev) 821d9112042SChih-Kang Chang { 822d9112042SChih-Kang Chang return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1; 823d9112042SChih-Kang Chang } 824d9112042SChih-Kang Chang 825e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev, 826e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx) 827e3ec7017SPing-Ke Shih { 828e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 829e3ec7017SPing-Ke Shih 830e3ec7017SPing-Ke Shih chip->ops->bb_reset(rtwdev, phy_idx); 831e3ec7017SPing-Ke Shih } 832e3ec7017SPing-Ke Shih 833e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev, 834e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 835e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 836e3ec7017SPing-Ke Shih void *extra_data) 837e3ec7017SPing-Ke Shih { 838e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) 839e3ec7017SPing-Ke Shih mdelay(50); 840e3ec7017SPing-Ke Shih else if (reg->addr == 0xfd) 841e3ec7017SPing-Ke Shih mdelay(5); 842e3ec7017SPing-Ke Shih else if (reg->addr == 0xfc) 843e3ec7017SPing-Ke Shih mdelay(1); 844e3ec7017SPing-Ke Shih else if (reg->addr == 0xfb) 845e3ec7017SPing-Ke Shih udelay(50); 846e3ec7017SPing-Ke Shih else if (reg->addr == 0xfa) 847e3ec7017SPing-Ke Shih udelay(5); 848e3ec7017SPing-Ke Shih else if (reg->addr == 0xf9) 849e3ec7017SPing-Ke Shih udelay(1); 850e3ec7017SPing-Ke Shih else 851e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, reg->addr, reg->data); 852e3ec7017SPing-Ke Shih } 853e3ec7017SPing-Ke Shih 854e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg { 855e885871eSZong-Zhe Yang u32 addr; 856e885871eSZong-Zhe Yang struct { 857e885871eSZong-Zhe Yang union { 858e885871eSZong-Zhe Yang u8 type; 859e885871eSZong-Zhe Yang struct { 860e885871eSZong-Zhe Yang u8 rxsc_start:4; 861e885871eSZong-Zhe Yang u8 bw:4; 862e885871eSZong-Zhe Yang }; 863e885871eSZong-Zhe Yang }; 864e885871eSZong-Zhe Yang u8 path; 865e885871eSZong-Zhe Yang u8 gain_band; 866e885871eSZong-Zhe Yang u8 cfg_type; 867e885871eSZong-Zhe Yang }; 868e885871eSZong-Zhe Yang } __packed; 869e885871eSZong-Zhe Yang 870e885871eSZong-Zhe Yang static void 871e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev, 872e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 873e885871eSZong-Zhe Yang { 874e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 875e885871eSZong-Zhe Yang u8 type = arg.type; 876e885871eSZong-Zhe Yang u8 path = arg.path; 877e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 878e885871eSZong-Zhe Yang int i; 879e885871eSZong-Zhe Yang 880e885871eSZong-Zhe Yang switch (type) { 881e885871eSZong-Zhe Yang case 0: 882e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 883e885871eSZong-Zhe Yang gain->lna_gain[gband][path][i] = data & 0xff; 884e885871eSZong-Zhe Yang break; 885e885871eSZong-Zhe Yang case 1: 886e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 887e885871eSZong-Zhe Yang gain->lna_gain[gband][path][i] = data & 0xff; 888e885871eSZong-Zhe Yang break; 889e885871eSZong-Zhe Yang case 2: 890e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) 891e885871eSZong-Zhe Yang gain->tia_gain[gband][path][i] = data & 0xff; 892e885871eSZong-Zhe Yang break; 893e885871eSZong-Zhe Yang default: 894e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 895e885871eSZong-Zhe Yang "bb gain error {0x%x:0x%x} with unknown type: %d\n", 896e885871eSZong-Zhe Yang arg.addr, data, type); 897e885871eSZong-Zhe Yang break; 898e885871eSZong-Zhe Yang } 899e885871eSZong-Zhe Yang } 900e885871eSZong-Zhe Yang 901e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx { 902e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_FULL = 0, 903e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_20 = 1, 904e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_20_1 = 5, 905e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_40 = 9, 906e885871eSZong-Zhe Yang RTW89_BB_RXSC_START_IDX_80 = 13, 907e885871eSZong-Zhe Yang }; 908e885871eSZong-Zhe Yang 909e885871eSZong-Zhe Yang static void 910e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev, 911e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 912e885871eSZong-Zhe Yang { 913e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 914e885871eSZong-Zhe Yang u8 rxsc_start = arg.rxsc_start; 915e885871eSZong-Zhe Yang u8 bw = arg.bw; 916e885871eSZong-Zhe Yang u8 path = arg.path; 917e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 918e885871eSZong-Zhe Yang u8 rxsc; 919e885871eSZong-Zhe Yang s8 ofst; 920e885871eSZong-Zhe Yang int i; 921e885871eSZong-Zhe Yang 922e885871eSZong-Zhe Yang switch (bw) { 923e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_20: 924e885871eSZong-Zhe Yang gain->rpl_ofst_20[gband][path] = (s8)data; 925e885871eSZong-Zhe Yang break; 926e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_40: 927e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 928e885871eSZong-Zhe Yang gain->rpl_ofst_40[gband][path][0] = (s8)data; 929e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 930e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 931e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 932e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 933e885871eSZong-Zhe Yang gain->rpl_ofst_40[gband][path][rxsc] = ofst; 934e885871eSZong-Zhe Yang } 935e885871eSZong-Zhe Yang } 936e885871eSZong-Zhe Yang break; 937e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_80: 938e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 939e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][0] = (s8)data; 940e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 941e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 942e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 943e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 944e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][rxsc] = ofst; 945e885871eSZong-Zhe Yang } 946e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 947e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 948e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 949e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 950e885871eSZong-Zhe Yang gain->rpl_ofst_80[gband][path][rxsc] = ofst; 951e885871eSZong-Zhe Yang } 952e885871eSZong-Zhe Yang } 953e885871eSZong-Zhe Yang break; 954e885871eSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 955e885871eSZong-Zhe Yang if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 956e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][0] = (s8)data; 957e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 958e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 959e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 960e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 961e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 962e885871eSZong-Zhe Yang } 963e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) { 964e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 965e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i; 966e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 967e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 968e885871eSZong-Zhe Yang } 969e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 970e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) { 971e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 972e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 973e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 974e885871eSZong-Zhe Yang } 975e885871eSZong-Zhe Yang } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) { 976e885871eSZong-Zhe Yang for (i = 0; i < 2; i++, data >>= 8) { 977e885871eSZong-Zhe Yang rxsc = RTW89_BB_RXSC_START_IDX_80 + i; 978e885871eSZong-Zhe Yang ofst = (s8)(data & 0xff); 979e885871eSZong-Zhe Yang gain->rpl_ofst_160[gband][path][rxsc] = ofst; 980e885871eSZong-Zhe Yang } 981e885871eSZong-Zhe Yang } 982e885871eSZong-Zhe Yang break; 983e885871eSZong-Zhe Yang default: 984e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 985e885871eSZong-Zhe Yang "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n", 986e885871eSZong-Zhe Yang arg.addr, data, bw); 987e885871eSZong-Zhe Yang break; 988e885871eSZong-Zhe Yang } 989e885871eSZong-Zhe Yang } 990e885871eSZong-Zhe Yang 991e885871eSZong-Zhe Yang static void 992e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev, 993e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 994e885871eSZong-Zhe Yang { 995e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 996e885871eSZong-Zhe Yang u8 type = arg.type; 997e885871eSZong-Zhe Yang u8 path = arg.path; 998e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 999e885871eSZong-Zhe Yang int i; 1000e885871eSZong-Zhe Yang 1001e885871eSZong-Zhe Yang switch (type) { 1002e885871eSZong-Zhe Yang case 0: 1003e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 1004e885871eSZong-Zhe Yang gain->lna_gain_bypass[gband][path][i] = data & 0xff; 1005e885871eSZong-Zhe Yang break; 1006e885871eSZong-Zhe Yang case 1: 1007e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 1008e885871eSZong-Zhe Yang gain->lna_gain_bypass[gband][path][i] = data & 0xff; 1009e885871eSZong-Zhe Yang break; 1010e885871eSZong-Zhe Yang default: 1011e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 1012e885871eSZong-Zhe Yang "bb gain bypass {0x%x:0x%x} with unknown type: %d\n", 1013e885871eSZong-Zhe Yang arg.addr, data, type); 1014e885871eSZong-Zhe Yang break; 1015e885871eSZong-Zhe Yang } 1016e885871eSZong-Zhe Yang } 1017e885871eSZong-Zhe Yang 1018e885871eSZong-Zhe Yang static void 1019e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev, 1020e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg, u32 data) 1021e885871eSZong-Zhe Yang { 1022e885871eSZong-Zhe Yang struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain; 1023e885871eSZong-Zhe Yang u8 type = arg.type; 1024e885871eSZong-Zhe Yang u8 path = arg.path; 1025e885871eSZong-Zhe Yang u8 gband = arg.gain_band; 1026e885871eSZong-Zhe Yang int i; 1027e885871eSZong-Zhe Yang 1028e885871eSZong-Zhe Yang switch (type) { 1029e885871eSZong-Zhe Yang case 0: 1030e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 1031e885871eSZong-Zhe Yang gain->lna_op1db[gband][path][i] = data & 0xff; 1032e885871eSZong-Zhe Yang break; 1033e885871eSZong-Zhe Yang case 1: 1034e885871eSZong-Zhe Yang for (i = 4; i < 7; i++, data >>= 8) 1035e885871eSZong-Zhe Yang gain->lna_op1db[gband][path][i] = data & 0xff; 1036e885871eSZong-Zhe Yang break; 1037e885871eSZong-Zhe Yang case 2: 1038e885871eSZong-Zhe Yang for (i = 0; i < 4; i++, data >>= 8) 1039e885871eSZong-Zhe Yang gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1040e885871eSZong-Zhe Yang break; 1041e885871eSZong-Zhe Yang case 3: 1042e885871eSZong-Zhe Yang for (i = 4; i < 8; i++, data >>= 8) 1043e885871eSZong-Zhe Yang gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1044e885871eSZong-Zhe Yang break; 1045e885871eSZong-Zhe Yang default: 1046e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 1047e885871eSZong-Zhe Yang "bb gain op1db {0x%x:0x%x} with unknown type: %d\n", 1048e885871eSZong-Zhe Yang arg.addr, data, type); 1049e885871eSZong-Zhe Yang break; 1050e885871eSZong-Zhe Yang } 1051e885871eSZong-Zhe Yang } 1052e885871eSZong-Zhe Yang 1053e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev, 1054e885871eSZong-Zhe Yang const struct rtw89_reg2_def *reg, 1055e885871eSZong-Zhe Yang enum rtw89_rf_path rf_path, 1056e885871eSZong-Zhe Yang void *extra_data) 1057e885871eSZong-Zhe Yang { 1058e885871eSZong-Zhe Yang const struct rtw89_chip_info *chip = rtwdev->chip; 1059e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr }; 1060c6a9d360SPing-Ke Shih struct rtw89_efuse *efuse = &rtwdev->efuse; 1061e885871eSZong-Zhe Yang 1062e885871eSZong-Zhe Yang if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR) 1063e885871eSZong-Zhe Yang return; 1064e885871eSZong-Zhe Yang 1065e885871eSZong-Zhe Yang if (arg.path >= chip->rf_path_num) 1066e885871eSZong-Zhe Yang return; 1067e885871eSZong-Zhe Yang 1068e885871eSZong-Zhe Yang if (arg.addr >= 0xf9 && arg.addr <= 0xfe) { 1069e885871eSZong-Zhe Yang rtw89_warn(rtwdev, "bb gain table with flow ctrl\n"); 1070e885871eSZong-Zhe Yang return; 1071e885871eSZong-Zhe Yang } 1072e885871eSZong-Zhe Yang 1073e885871eSZong-Zhe Yang switch (arg.cfg_type) { 1074e885871eSZong-Zhe Yang case 0: 1075e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data); 1076e885871eSZong-Zhe Yang break; 1077e885871eSZong-Zhe Yang case 1: 1078e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data); 1079e885871eSZong-Zhe Yang break; 1080e885871eSZong-Zhe Yang case 2: 1081e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data); 1082e885871eSZong-Zhe Yang break; 1083e885871eSZong-Zhe Yang case 3: 1084e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data); 1085e885871eSZong-Zhe Yang break; 1086c6a9d360SPing-Ke Shih case 4: 1087c6a9d360SPing-Ke Shih /* This cfg_type is only used by rfe_type >= 50 with eFEM */ 1088c6a9d360SPing-Ke Shih if (efuse->rfe_type < 50) 1089c6a9d360SPing-Ke Shih break; 1090c6a9d360SPing-Ke Shih fallthrough; 1091e885871eSZong-Zhe Yang default: 1092e885871eSZong-Zhe Yang rtw89_warn(rtwdev, 1093e885871eSZong-Zhe Yang "bb gain {0x%x:0x%x} with unknown cfg type: %d\n", 1094e885871eSZong-Zhe Yang arg.addr, reg->data, arg.cfg_type); 1095e885871eSZong-Zhe Yang break; 1096e885871eSZong-Zhe Yang } 1097e885871eSZong-Zhe Yang } 1098e885871eSZong-Zhe Yang 1099e3ec7017SPing-Ke Shih static void 1100e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev, 1101e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1102e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1103e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 1104e3ec7017SPing-Ke Shih { 1105e3ec7017SPing-Ke Shih u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE; 1106e3ec7017SPing-Ke Shih u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE; 1107e3ec7017SPing-Ke Shih 110830101812SPing-Ke Shih if (page >= RTW89_H2C_RF_PAGE_NUM) { 110930101812SPing-Ke Shih rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d", 111030101812SPing-Ke Shih rf_path, info->curr_idx); 111130101812SPing-Ke Shih return; 111230101812SPing-Ke Shih } 111330101812SPing-Ke Shih 1114e3ec7017SPing-Ke Shih info->rtw89_phy_config_rf_h2c[page][idx] = 1115e3ec7017SPing-Ke Shih cpu_to_le32((reg->addr << 20) | reg->data); 1116e3ec7017SPing-Ke Shih info->curr_idx++; 1117e3ec7017SPing-Ke Shih } 1118e3ec7017SPing-Ke Shih 1119e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev, 1120e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 1121e3ec7017SPing-Ke Shih { 112230101812SPing-Ke Shih u16 remain = info->curr_idx; 112330101812SPing-Ke Shih u16 len = 0; 1124e3ec7017SPing-Ke Shih u8 i; 1125e3ec7017SPing-Ke Shih int ret = 0; 1126e3ec7017SPing-Ke Shih 112730101812SPing-Ke Shih if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) { 1128e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, 112930101812SPing-Ke Shih "rf reg h2c total len %d larger than %d\n", 113030101812SPing-Ke Shih remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE); 113130101812SPing-Ke Shih ret = -EINVAL; 113230101812SPing-Ke Shih goto out; 1133e3ec7017SPing-Ke Shih } 1134e3ec7017SPing-Ke Shih 113530101812SPing-Ke Shih for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) { 113630101812SPing-Ke Shih len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain; 113730101812SPing-Ke Shih ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i); 1138e3ec7017SPing-Ke Shih if (ret) 113930101812SPing-Ke Shih goto out; 1140e3ec7017SPing-Ke Shih } 114130101812SPing-Ke Shih out: 1142e3ec7017SPing-Ke Shih info->curr_idx = 0; 1143e3ec7017SPing-Ke Shih 114430101812SPing-Ke Shih return ret; 1145e3ec7017SPing-Ke Shih } 1146e3ec7017SPing-Ke Shih 1147d9112042SChih-Kang Chang static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev, 1148d9112042SChih-Kang Chang const struct rtw89_reg2_def *reg, 1149d9112042SChih-Kang Chang enum rtw89_rf_path rf_path, 1150d9112042SChih-Kang Chang void *extra_data) 1151d9112042SChih-Kang Chang { 1152d9112042SChih-Kang Chang u32 addr = reg->addr; 1153d9112042SChih-Kang Chang 1154d9112042SChih-Kang Chang if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb || 1155d9112042SChih-Kang Chang addr == 0xfa || addr == 0xf9) 1156d9112042SChih-Kang Chang return; 1157d9112042SChih-Kang Chang 1158d9112042SChih-Kang Chang if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100) 1159d9112042SChih-Kang Chang return; 1160d9112042SChih-Kang Chang 1161d9112042SChih-Kang Chang rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1162d9112042SChih-Kang Chang (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1163d9112042SChih-Kang Chang } 1164d9112042SChih-Kang Chang 1165e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev, 1166e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1167e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1168e3ec7017SPing-Ke Shih void *extra_data) 1169e3ec7017SPing-Ke Shih { 1170e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) { 1171e3ec7017SPing-Ke Shih mdelay(50); 1172e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfd) { 1173e3ec7017SPing-Ke Shih mdelay(5); 1174e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfc) { 1175e3ec7017SPing-Ke Shih mdelay(1); 1176e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfb) { 1177e3ec7017SPing-Ke Shih udelay(50); 1178e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfa) { 1179e3ec7017SPing-Ke Shih udelay(5); 1180e3ec7017SPing-Ke Shih } else if (reg->addr == 0xf9) { 1181e3ec7017SPing-Ke Shih udelay(1); 1182e3ec7017SPing-Ke Shih } else { 1183e3ec7017SPing-Ke Shih rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data); 1184e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1185e3ec7017SPing-Ke Shih (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1186e3ec7017SPing-Ke Shih } 1187e3ec7017SPing-Ke Shih } 1188e3ec7017SPing-Ke Shih 11892a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev, 11902a5f2b32SPing-Ke Shih const struct rtw89_reg2_def *reg, 11912a5f2b32SPing-Ke Shih enum rtw89_rf_path rf_path, 11922a5f2b32SPing-Ke Shih void *extra_data) 11932a5f2b32SPing-Ke Shih { 11942a5f2b32SPing-Ke Shih rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data); 11952a5f2b32SPing-Ke Shih 11962a5f2b32SPing-Ke Shih if (reg->addr < 0x100) 11972a5f2b32SPing-Ke Shih return; 11982a5f2b32SPing-Ke Shih 11992a5f2b32SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 12002a5f2b32SPing-Ke Shih (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 12012a5f2b32SPing-Ke Shih } 12022a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1); 12032a5f2b32SPing-Ke Shih 1204e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev, 1205e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 1206e3ec7017SPing-Ke Shih u32 *headline_size, u32 *headline_idx, 1207e3ec7017SPing-Ke Shih u8 rfe, u8 cv) 1208e3ec7017SPing-Ke Shih { 1209e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 1210e3ec7017SPing-Ke Shih u32 headline; 1211e3ec7017SPing-Ke Shih u32 compare, target; 1212e3ec7017SPing-Ke Shih u8 rfe_para, cv_para; 1213e3ec7017SPing-Ke Shih u8 cv_max = 0; 1214e3ec7017SPing-Ke Shih bool case_matched = false; 1215e3ec7017SPing-Ke Shih u32 i; 1216e3ec7017SPing-Ke Shih 1217e3ec7017SPing-Ke Shih for (i = 0; i < table->n_regs; i++) { 1218e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1219e3ec7017SPing-Ke Shih headline = get_phy_headline(reg->addr); 1220e3ec7017SPing-Ke Shih if (headline != PHY_HEADLINE_VALID) 1221e3ec7017SPing-Ke Shih break; 1222e3ec7017SPing-Ke Shih } 1223e3ec7017SPing-Ke Shih *headline_size = i; 1224e3ec7017SPing-Ke Shih if (*headline_size == 0) 1225e3ec7017SPing-Ke Shih return 0; 1226e3ec7017SPing-Ke Shih 1227e3ec7017SPing-Ke Shih /* case 1: RFE match, CV match */ 1228e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, cv); 1229e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1230e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1231e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1232e3ec7017SPing-Ke Shih if (target == compare) { 1233e3ec7017SPing-Ke Shih *headline_idx = i; 1234e3ec7017SPing-Ke Shih return 0; 1235e3ec7017SPing-Ke Shih } 1236e3ec7017SPing-Ke Shih } 1237e3ec7017SPing-Ke Shih 1238e3ec7017SPing-Ke Shih /* case 2: RFE match, CV don't care */ 1239e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, PHY_COND_DONT_CARE); 1240e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1241e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1242e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1243e3ec7017SPing-Ke Shih if (target == compare) { 1244e3ec7017SPing-Ke Shih *headline_idx = i; 1245e3ec7017SPing-Ke Shih return 0; 1246e3ec7017SPing-Ke Shih } 1247e3ec7017SPing-Ke Shih } 1248e3ec7017SPing-Ke Shih 1249e3ec7017SPing-Ke Shih /* case 3: RFE match, CV max in table */ 1250e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1251e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1252e3ec7017SPing-Ke Shih rfe_para = get_phy_cond_rfe(reg->addr); 1253e3ec7017SPing-Ke Shih cv_para = get_phy_cond_cv(reg->addr); 1254e3ec7017SPing-Ke Shih if (rfe_para == rfe) { 1255e3ec7017SPing-Ke Shih if (cv_para >= cv_max) { 1256e3ec7017SPing-Ke Shih cv_max = cv_para; 1257e3ec7017SPing-Ke Shih *headline_idx = i; 1258e3ec7017SPing-Ke Shih case_matched = true; 1259e3ec7017SPing-Ke Shih } 1260e3ec7017SPing-Ke Shih } 1261e3ec7017SPing-Ke Shih } 1262e3ec7017SPing-Ke Shih 1263e3ec7017SPing-Ke Shih if (case_matched) 1264e3ec7017SPing-Ke Shih return 0; 1265e3ec7017SPing-Ke Shih 1266e3ec7017SPing-Ke Shih /* case 4: RFE don't care, CV max in table */ 1267e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 1268e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1269e3ec7017SPing-Ke Shih rfe_para = get_phy_cond_rfe(reg->addr); 1270e3ec7017SPing-Ke Shih cv_para = get_phy_cond_cv(reg->addr); 1271e3ec7017SPing-Ke Shih if (rfe_para == PHY_COND_DONT_CARE) { 1272e3ec7017SPing-Ke Shih if (cv_para >= cv_max) { 1273e3ec7017SPing-Ke Shih cv_max = cv_para; 1274e3ec7017SPing-Ke Shih *headline_idx = i; 1275e3ec7017SPing-Ke Shih case_matched = true; 1276e3ec7017SPing-Ke Shih } 1277e3ec7017SPing-Ke Shih } 1278e3ec7017SPing-Ke Shih } 1279e3ec7017SPing-Ke Shih 1280e3ec7017SPing-Ke Shih if (case_matched) 1281e3ec7017SPing-Ke Shih return 0; 1282e3ec7017SPing-Ke Shih 1283e3ec7017SPing-Ke Shih return -EINVAL; 1284e3ec7017SPing-Ke Shih } 1285e3ec7017SPing-Ke Shih 1286e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev, 1287e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 1288e3ec7017SPing-Ke Shih void (*config)(struct rtw89_dev *rtwdev, 1289e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 1290e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 1291e3ec7017SPing-Ke Shih void *data), 1292e3ec7017SPing-Ke Shih void *extra_data) 1293e3ec7017SPing-Ke Shih { 1294e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 1295e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path = table->rf_path; 1296e3ec7017SPing-Ke Shih u8 rfe = rtwdev->efuse.rfe_type; 1297e3ec7017SPing-Ke Shih u8 cv = rtwdev->hal.cv; 1298e3ec7017SPing-Ke Shih u32 i; 1299e3ec7017SPing-Ke Shih u32 headline_size = 0, headline_idx = 0; 1300e3ec7017SPing-Ke Shih u32 target = 0, cfg_target; 1301e3ec7017SPing-Ke Shih u8 cond; 1302e3ec7017SPing-Ke Shih bool is_matched = true; 1303e3ec7017SPing-Ke Shih bool target_found = false; 1304e3ec7017SPing-Ke Shih int ret; 1305e3ec7017SPing-Ke Shih 1306e3ec7017SPing-Ke Shih ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size, 1307e3ec7017SPing-Ke Shih &headline_idx, rfe, cv); 1308e3ec7017SPing-Ke Shih if (ret) { 1309e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv); 1310e3ec7017SPing-Ke Shih return; 1311e3ec7017SPing-Ke Shih } 1312e3ec7017SPing-Ke Shih 1313e3ec7017SPing-Ke Shih cfg_target = get_phy_target(table->regs[headline_idx].addr); 1314e3ec7017SPing-Ke Shih for (i = headline_size; i < table->n_regs; i++) { 1315e3ec7017SPing-Ke Shih reg = &table->regs[i]; 1316e3ec7017SPing-Ke Shih cond = get_phy_cond(reg->addr); 1317e3ec7017SPing-Ke Shih switch (cond) { 1318e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_IF: 1319e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELIF: 1320e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 1321e3ec7017SPing-Ke Shih break; 1322e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELSE: 1323e3ec7017SPing-Ke Shih is_matched = false; 1324e3ec7017SPing-Ke Shih if (!target_found) { 1325e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "failed to load CR %x/%x\n", 1326e3ec7017SPing-Ke Shih reg->addr, reg->data); 1327e3ec7017SPing-Ke Shih return; 1328e3ec7017SPing-Ke Shih } 1329e3ec7017SPing-Ke Shih break; 1330e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_END: 1331e3ec7017SPing-Ke Shih is_matched = true; 1332e3ec7017SPing-Ke Shih target_found = false; 1333e3ec7017SPing-Ke Shih break; 1334e3ec7017SPing-Ke Shih case PHY_COND_CHECK: 1335e3ec7017SPing-Ke Shih if (target_found) { 1336e3ec7017SPing-Ke Shih is_matched = false; 1337e3ec7017SPing-Ke Shih break; 1338e3ec7017SPing-Ke Shih } 1339e3ec7017SPing-Ke Shih 1340e3ec7017SPing-Ke Shih if (target == cfg_target) { 1341e3ec7017SPing-Ke Shih is_matched = true; 1342e3ec7017SPing-Ke Shih target_found = true; 1343e3ec7017SPing-Ke Shih } else { 1344e3ec7017SPing-Ke Shih is_matched = false; 1345e3ec7017SPing-Ke Shih target_found = false; 1346e3ec7017SPing-Ke Shih } 1347e3ec7017SPing-Ke Shih break; 1348e3ec7017SPing-Ke Shih default: 1349e3ec7017SPing-Ke Shih if (is_matched) 1350e3ec7017SPing-Ke Shih config(rtwdev, reg, rf_path, extra_data); 1351e3ec7017SPing-Ke Shih break; 1352e3ec7017SPing-Ke Shih } 1353e3ec7017SPing-Ke Shih } 1354e3ec7017SPing-Ke Shih } 1355e3ec7017SPing-Ke Shih 1356e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev) 1357e3ec7017SPing-Ke Shih { 1358e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1359e3ec7017SPing-Ke Shih const struct rtw89_phy_table *bb_table = chip->bb_table; 1360e885871eSZong-Zhe Yang const struct rtw89_phy_table *bb_gain_table = chip->bb_gain_table; 1361e3ec7017SPing-Ke Shih 1362e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL); 1363e3ec7017SPing-Ke Shih rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0); 1364e885871eSZong-Zhe Yang if (bb_gain_table) 1365e885871eSZong-Zhe Yang rtw89_phy_init_reg(rtwdev, bb_gain_table, 1366e885871eSZong-Zhe Yang rtw89_phy_config_bb_gain, NULL); 1367e3ec7017SPing-Ke Shih rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0); 1368e3ec7017SPing-Ke Shih } 1369e3ec7017SPing-Ke Shih 1370e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev) 1371e3ec7017SPing-Ke Shih { 1372e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, 0x8080, 0x4); 1373e3ec7017SPing-Ke Shih udelay(1); 1374e3ec7017SPing-Ke Shih return rtw89_phy_read32(rtwdev, 0x8080); 1375e3ec7017SPing-Ke Shih } 1376e3ec7017SPing-Ke Shih 1377d9112042SChih-Kang Chang void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio) 1378e3ec7017SPing-Ke Shih { 13792a5f2b32SPing-Ke Shih void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg, 13802a5f2b32SPing-Ke Shih enum rtw89_rf_path rf_path, void *data); 1381e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1382e3ec7017SPing-Ke Shih const struct rtw89_phy_table *rf_table; 1383e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *rf_reg_info; 1384e3ec7017SPing-Ke Shih u8 path; 1385e3ec7017SPing-Ke Shih 1386e3ec7017SPing-Ke Shih rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL); 1387e3ec7017SPing-Ke Shih if (!rf_reg_info) 1388e3ec7017SPing-Ke Shih return; 1389e3ec7017SPing-Ke Shih 1390e3ec7017SPing-Ke Shih for (path = RF_PATH_A; path < chip->rf_path_num; path++) { 1391e3ec7017SPing-Ke Shih rf_table = chip->rf_table[path]; 13922a5f2b32SPing-Ke Shih rf_reg_info->rf_path = rf_table->rf_path; 1393d9112042SChih-Kang Chang if (noio) 1394d9112042SChih-Kang Chang config = rtw89_phy_config_rf_reg_noio; 1395d9112042SChih-Kang Chang else 1396d9112042SChih-Kang Chang config = rf_table->config ? rf_table->config : 1397d9112042SChih-Kang Chang rtw89_phy_config_rf_reg; 13982a5f2b32SPing-Ke Shih rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info); 1399e3ec7017SPing-Ke Shih if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info)) 1400e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n", 14012a5f2b32SPing-Ke Shih rf_reg_info->rf_path); 1402e3ec7017SPing-Ke Shih } 1403e3ec7017SPing-Ke Shih kfree(rf_reg_info); 1404e3ec7017SPing-Ke Shih } 1405e3ec7017SPing-Ke Shih 1406e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev) 1407e3ec7017SPing-Ke Shih { 1408e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1409e3ec7017SPing-Ke Shih const struct rtw89_phy_table *nctl_table; 1410e3ec7017SPing-Ke Shih u32 val; 1411e3ec7017SPing-Ke Shih int ret; 1412e3ec7017SPing-Ke Shih 1413e3ec7017SPing-Ke Shih /* IQK/DPK clock & reset */ 1414d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3); 1415d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1); 1416d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000); 1417a24be8bbSPing-Ke Shih if (chip->chip_id != RTL8851B) 1418d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000); 1419d0c820ccSPing-Ke Shih if (chip->chip_id == RTL8852B) 1420d0c820ccSPing-Ke Shih rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2); 1421e3ec7017SPing-Ke Shih 1422e3ec7017SPing-Ke Shih /* check 0x8080 */ 1423d0c820ccSPing-Ke Shih rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8); 1424e3ec7017SPing-Ke Shih 1425e3ec7017SPing-Ke Shih ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10, 1426e3ec7017SPing-Ke Shih 1000, false, rtwdev); 1427e3ec7017SPing-Ke Shih if (ret) 1428e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "failed to poll nctl block\n"); 1429e3ec7017SPing-Ke Shih 1430e3ec7017SPing-Ke Shih nctl_table = chip->nctl_table; 1431e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL); 1432a24be8bbSPing-Ke Shih 1433a24be8bbSPing-Ke Shih if (chip->nctl_post_table) 1434a24be8bbSPing-Ke Shih rtw89_rfk_parser(rtwdev, chip->nctl_post_table); 1435e3ec7017SPing-Ke Shih } 1436e3ec7017SPing-Ke Shih 1437e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr) 1438e3ec7017SPing-Ke Shih { 1439e3ec7017SPing-Ke Shih u32 phy_page = addr >> 8; 1440e3ec7017SPing-Ke Shih u32 ofst = 0; 1441e3ec7017SPing-Ke Shih 1442e3ec7017SPing-Ke Shih switch (phy_page) { 1443e3ec7017SPing-Ke Shih case 0x6: 1444e3ec7017SPing-Ke Shih case 0x7: 1445e3ec7017SPing-Ke Shih case 0x8: 1446e3ec7017SPing-Ke Shih case 0x9: 1447e3ec7017SPing-Ke Shih case 0xa: 1448e3ec7017SPing-Ke Shih case 0xb: 1449e3ec7017SPing-Ke Shih case 0xc: 1450e3ec7017SPing-Ke Shih case 0xd: 1451e3ec7017SPing-Ke Shih case 0x19: 1452e3ec7017SPing-Ke Shih case 0x1a: 1453e3ec7017SPing-Ke Shih case 0x1b: 1454e3ec7017SPing-Ke Shih ofst = 0x2000; 1455e3ec7017SPing-Ke Shih break; 1456e3ec7017SPing-Ke Shih default: 1457e3ec7017SPing-Ke Shih /* warning case */ 1458e3ec7017SPing-Ke Shih ofst = 0; 1459e3ec7017SPing-Ke Shih break; 1460e3ec7017SPing-Ke Shih } 1461e3ec7017SPing-Ke Shih 1462e3ec7017SPing-Ke Shih if (phy_page >= 0x40 && phy_page <= 0x4f) 1463e3ec7017SPing-Ke Shih ofst = 0x2000; 1464e3ec7017SPing-Ke Shih 1465e3ec7017SPing-Ke Shih return ofst; 1466e3ec7017SPing-Ke Shih } 1467e3ec7017SPing-Ke Shih 1468e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1469e3ec7017SPing-Ke Shih u32 data, enum rtw89_phy_idx phy_idx) 1470e3ec7017SPing-Ke Shih { 1471e3ec7017SPing-Ke Shih if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 1472e3ec7017SPing-Ke Shih addr += rtw89_phy0_phy1_offset(rtwdev, addr); 1473e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, addr, mask, data); 1474e3ec7017SPing-Ke Shih } 1475861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx); 1476e3ec7017SPing-Ke Shih 14776b069898SPing-Ke Shih u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 14786b069898SPing-Ke Shih enum rtw89_phy_idx phy_idx) 14796b069898SPing-Ke Shih { 14806b069898SPing-Ke Shih if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 14816b069898SPing-Ke Shih addr += rtw89_phy0_phy1_offset(rtwdev, addr); 14826b069898SPing-Ke Shih return rtw89_phy_read32_mask(rtwdev, addr, mask); 14836b069898SPing-Ke Shih } 14846b069898SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read32_idx); 14856b069898SPing-Ke Shih 1486e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1487e3ec7017SPing-Ke Shih u32 val) 1488e3ec7017SPing-Ke Shih { 1489e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0); 1490e3ec7017SPing-Ke Shih 1491e3ec7017SPing-Ke Shih if (!rtwdev->dbcc_en) 1492e3ec7017SPing-Ke Shih return; 1493e3ec7017SPing-Ke Shih 1494e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1); 1495e3ec7017SPing-Ke Shih } 1496e3ec7017SPing-Ke Shih 1497e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev, 1498e3ec7017SPing-Ke Shih const struct rtw89_phy_reg3_tbl *tbl) 1499e3ec7017SPing-Ke Shih { 1500e3ec7017SPing-Ke Shih const struct rtw89_reg3_def *reg3; 1501e3ec7017SPing-Ke Shih int i; 1502e3ec7017SPing-Ke Shih 1503e3ec7017SPing-Ke Shih for (i = 0; i < tbl->size; i++) { 1504e3ec7017SPing-Ke Shih reg3 = &tbl->reg3[i]; 1505e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data); 1506e3ec7017SPing-Ke Shih } 1507e3ec7017SPing-Ke Shih } 1508861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl); 1509e3ec7017SPing-Ke Shih 1510b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_idx_num[] = { 1511b4a283fbSZong-Zhe Yang [RTW89_RS_CCK] = RTW89_RATE_CCK_NUM, 1512b4a283fbSZong-Zhe Yang [RTW89_RS_OFDM] = RTW89_RATE_OFDM_NUM, 1513b4a283fbSZong-Zhe Yang [RTW89_RS_MCS] = RTW89_RATE_MCS_NUM, 1514b4a283fbSZong-Zhe Yang [RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_NUM, 1515b4a283fbSZong-Zhe Yang [RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_NUM, 1516e3ec7017SPing-Ke Shih }; 1517e3ec7017SPing-Ke Shih 1518b4a283fbSZong-Zhe Yang static const u8 rtw89_rs_nss_num[] = { 1519e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = 1, 1520e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = 1, 1521b4a283fbSZong-Zhe Yang [RTW89_RS_MCS] = RTW89_NSS_NUM, 1522b4a283fbSZong-Zhe Yang [RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_NUM, 1523e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = 1, 1524e3ec7017SPing-Ke Shih }; 1525e3ec7017SPing-Ke Shih 1526e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = { 1527e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck), 1528e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm), 1529e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs), 1530e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm), 1531e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset), 1532e3ec7017SPing-Ke Shih }; 1533e3ec7017SPing-Ke Shih 1534e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs]) 1535b4a283fbSZong-Zhe Yang #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_num[rs] + (idx)) 1536e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \ 1537b4a283fbSZong-Zhe Yang ((nss) < rtw89_rs_nss_num[rs] && (idx) < rtw89_rs_idx_num[rs]) 1538e3ec7017SPing-Ke Shih 1539e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev, 1540e3ec7017SPing-Ke Shih const struct rtw89_txpwr_table *tbl) 1541e3ec7017SPing-Ke Shih { 1542e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data; 1543e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size; 1544e3ec7017SPing-Ke Shih s8 *byr; 1545e3ec7017SPing-Ke Shih u32 data; 1546e3ec7017SPing-Ke Shih u8 i, idx; 1547e3ec7017SPing-Ke Shih 1548e3ec7017SPing-Ke Shih for (; cfg < end; cfg++) { 1549e3ec7017SPing-Ke Shih byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]); 1550e3ec7017SPing-Ke Shih data = cfg->data; 1551e3ec7017SPing-Ke Shih 1552e3ec7017SPing-Ke Shih for (i = 0; i < cfg->len; i++, data >>= 8) { 1553e3ec7017SPing-Ke Shih idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i)); 1554e3ec7017SPing-Ke Shih byr[idx] = (s8)(data & 0xff); 1555e3ec7017SPing-Ke Shih } 1556e3ec7017SPing-Ke Shih } 1557e3ec7017SPing-Ke Shih } 1558861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate); 1559e3ec7017SPing-Ke Shih 1560e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf) \ 1561e3ec7017SPing-Ke Shih ({ \ 1562e3ec7017SPing-Ke Shih const struct rtw89_chip_info *__c = (rtwdev)->chip; \ 1563e3ec7017SPing-Ke Shih (txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac); \ 1564e3ec7017SPing-Ke Shih }) 1565e3ec7017SPing-Ke Shih 15669b43bd1aSZong-Zhe Yang static 156707ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band, 1568e3ec7017SPing-Ke Shih const struct rtw89_rate_desc *rate_desc) 1569e3ec7017SPing-Ke Shih { 1570e3ec7017SPing-Ke Shih s8 *byr; 1571e3ec7017SPing-Ke Shih u8 idx; 1572e3ec7017SPing-Ke Shih 1573e3ec7017SPing-Ke Shih if (rate_desc->rs == RTW89_RS_CCK) 1574e3ec7017SPing-Ke Shih band = RTW89_BAND_2G; 1575e3ec7017SPing-Ke Shih 1576e3ec7017SPing-Ke Shih if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) { 1577e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 1578e3ec7017SPing-Ke Shih "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n", 1579e3ec7017SPing-Ke Shih rate_desc->rs, rate_desc->nss, rate_desc->idx); 1580e3ec7017SPing-Ke Shih 1581e3ec7017SPing-Ke Shih return 0; 1582e3ec7017SPing-Ke Shih } 1583e3ec7017SPing-Ke Shih 1584e3ec7017SPing-Ke Shih byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]); 1585e3ec7017SPing-Ke Shih idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx); 1586e3ec7017SPing-Ke Shih 1587e3ec7017SPing-Ke Shih return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]); 1588e3ec7017SPing-Ke Shih } 1589e3ec7017SPing-Ke Shih 1590ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g) 1591e3ec7017SPing-Ke Shih { 1592ac74f016SZong-Zhe Yang switch (channel_6g) { 1593ac74f016SZong-Zhe Yang case 1 ... 29: 1594ac74f016SZong-Zhe Yang return (channel_6g - 1) / 2; 1595ac74f016SZong-Zhe Yang case 33 ... 61: 1596ac74f016SZong-Zhe Yang return (channel_6g - 3) / 2; 1597ac74f016SZong-Zhe Yang case 65 ... 93: 1598ac74f016SZong-Zhe Yang return (channel_6g - 5) / 2; 1599ac74f016SZong-Zhe Yang case 97 ... 125: 1600ac74f016SZong-Zhe Yang return (channel_6g - 7) / 2; 1601ac74f016SZong-Zhe Yang case 129 ... 157: 1602ac74f016SZong-Zhe Yang return (channel_6g - 9) / 2; 1603ac74f016SZong-Zhe Yang case 161 ... 189: 1604ac74f016SZong-Zhe Yang return (channel_6g - 11) / 2; 1605ac74f016SZong-Zhe Yang case 193 ... 221: 1606ac74f016SZong-Zhe Yang return (channel_6g - 13) / 2; 1607ac74f016SZong-Zhe Yang case 225 ... 253: 1608ac74f016SZong-Zhe Yang return (channel_6g - 15) / 2; 1609ac74f016SZong-Zhe Yang default: 1610ac74f016SZong-Zhe Yang rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g); 1611ac74f016SZong-Zhe Yang return 0; 1612ac74f016SZong-Zhe Yang } 1613ac74f016SZong-Zhe Yang } 1614ac74f016SZong-Zhe Yang 1615ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel) 1616ac74f016SZong-Zhe Yang { 1617ac74f016SZong-Zhe Yang if (band == RTW89_BAND_6G) 1618ac74f016SZong-Zhe Yang return rtw89_channel_6g_to_idx(rtwdev, channel); 1619ac74f016SZong-Zhe Yang 1620e3ec7017SPing-Ke Shih switch (channel) { 1621e3ec7017SPing-Ke Shih case 1 ... 14: 1622e3ec7017SPing-Ke Shih return channel - 1; 1623e3ec7017SPing-Ke Shih case 36 ... 64: 1624e3ec7017SPing-Ke Shih return (channel - 36) / 2; 1625e3ec7017SPing-Ke Shih case 100 ... 144: 1626e3ec7017SPing-Ke Shih return ((channel - 100) / 2) + 15; 1627e3ec7017SPing-Ke Shih case 149 ... 177: 1628e3ec7017SPing-Ke Shih return ((channel - 149) / 2) + 38; 1629e3ec7017SPing-Ke Shih default: 1630e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown channel: %d\n", channel); 1631e3ec7017SPing-Ke Shih return 0; 1632e3ec7017SPing-Ke Shih } 1633e3ec7017SPing-Ke Shih } 1634e3ec7017SPing-Ke Shih 163507ef5f2fSZong-Zhe Yang s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band, 1636e3ec7017SPing-Ke Shih u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch) 1637e3ec7017SPing-Ke Shih { 16385395482aSZong-Zhe Yang const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms; 16395395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz; 16405395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz; 16415395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz; 16422a8ec45fSZong-Zhe Yang struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory; 1643ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1644e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 16452a8ec45fSZong-Zhe Yang u8 reg6 = regulatory->reg_6ghz_power; 1646e3ec7017SPing-Ke Shih s8 lmt = 0, sar; 1647e3ec7017SPing-Ke Shih 1648e3ec7017SPing-Ke Shih switch (band) { 1649e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 16505395482aSZong-Zhe Yang lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx]; 16515395482aSZong-Zhe Yang if (lmt) 16525395482aSZong-Zhe Yang break; 16535395482aSZong-Zhe Yang 16545395482aSZong-Zhe Yang lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx]; 1655e3ec7017SPing-Ke Shih break; 1656e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 16575395482aSZong-Zhe Yang lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx]; 16585395482aSZong-Zhe Yang if (lmt) 16595395482aSZong-Zhe Yang break; 16605395482aSZong-Zhe Yang 16615395482aSZong-Zhe Yang lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx]; 1662e3ec7017SPing-Ke Shih break; 1663ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 16642a8ec45fSZong-Zhe Yang lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][regd][reg6][ch_idx]; 16655395482aSZong-Zhe Yang if (lmt) 16665395482aSZong-Zhe Yang break; 16675395482aSZong-Zhe Yang 16682a8ec45fSZong-Zhe Yang lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][RTW89_WW] 16692a8ec45fSZong-Zhe Yang [RTW89_REG_6GHZ_POWER_DFLT] 16702a8ec45fSZong-Zhe Yang [ch_idx]; 1671ac74f016SZong-Zhe Yang break; 1672e3ec7017SPing-Ke Shih default: 1673e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1674e3ec7017SPing-Ke Shih return 0; 1675e3ec7017SPing-Ke Shih } 1676e3ec7017SPing-Ke Shih 1677e3ec7017SPing-Ke Shih lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt); 1678e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1679e3ec7017SPing-Ke Shih 1680e3ec7017SPing-Ke Shih return min(lmt, sar); 1681e3ec7017SPing-Ke Shih } 1682861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit); 1683e3ec7017SPing-Ke Shih 168407ef5f2fSZong-Zhe Yang #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch) \ 1685e3ec7017SPing-Ke Shih do { \ 1686e3ec7017SPing-Ke Shih u8 __i; \ 1687e3ec7017SPing-Ke Shih for (__i = 0; __i < RTW89_BF_NUM; __i++) \ 1688e3ec7017SPing-Ke Shih ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev, \ 168907ef5f2fSZong-Zhe Yang band, \ 1690e3ec7017SPing-Ke Shih bw, ntx, \ 1691e3ec7017SPing-Ke Shih rs, __i, \ 1692e3ec7017SPing-Ke Shih (ch)); \ 1693e3ec7017SPing-Ke Shih } while (0) 1694e3ec7017SPing-Ke Shih 1695e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev, 1696e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 169707ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1698e3ec7017SPing-Ke Shih { 169907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 1700e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 170107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 1702e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 170307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 1704e3ec7017SPing-Ke Shih ntx, RTW89_RS_OFDM, ch); 170507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 170607ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1707e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1708e3ec7017SPing-Ke Shih } 1709e3ec7017SPing-Ke Shih 1710e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev, 1711e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 171207ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 1713e3ec7017SPing-Ke Shih { 171407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 1715e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch - 2); 171607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 1717e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 171807ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 171994b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 172007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 172107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1722e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 172307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 172407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1725e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 172607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 172707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1728e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1729e3ec7017SPing-Ke Shih } 1730e3ec7017SPing-Ke Shih 1731e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev, 1732e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 173307ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 1734e3ec7017SPing-Ke Shih { 1735e3ec7017SPing-Ke Shih s8 val_0p5_n[RTW89_BF_NUM]; 1736e3ec7017SPing-Ke Shih s8 val_0p5_p[RTW89_BF_NUM]; 1737e3ec7017SPing-Ke Shih u8 i; 1738e3ec7017SPing-Ke Shih 173907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 174094b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 174107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 174207ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1743e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 6); 174407ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 174507ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1746e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 174707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 174807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1749e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 175007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 175107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 1752e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 6); 175307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 175407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1755e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 175607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 175707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 1758e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 175907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 176007ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 1761e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1762e3ec7017SPing-Ke Shih 176307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 1764e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 176507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 1766e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 1767e3ec7017SPing-Ke Shih 1768e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_BF_NUM; i++) 1769e3ec7017SPing-Ke Shih lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 1770e3ec7017SPing-Ke Shih } 1771e3ec7017SPing-Ke Shih 177294b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev, 177394b70cafSZong-Zhe Yang struct rtw89_txpwr_limit *lmt, 177407ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch, u8 pri_ch) 177594b70cafSZong-Zhe Yang { 177694b70cafSZong-Zhe Yang s8 val_0p5_n[RTW89_BF_NUM]; 177794b70cafSZong-Zhe Yang s8 val_0p5_p[RTW89_BF_NUM]; 177894b70cafSZong-Zhe Yang s8 val_2p5_n[RTW89_BF_NUM]; 177994b70cafSZong-Zhe Yang s8 val_2p5_p[RTW89_BF_NUM]; 178094b70cafSZong-Zhe Yang u8 i; 178194b70cafSZong-Zhe Yang 178294b70cafSZong-Zhe Yang /* fill ofdm section */ 178307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 178494b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 178594b70cafSZong-Zhe Yang 178694b70cafSZong-Zhe Yang /* fill mcs 20m section */ 178707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 178807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 178994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 14); 179007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 179107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 179294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 10); 179307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 179407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 179594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 6); 179607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 179707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 179894b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 2); 179907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band, 180007ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 180194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 2); 180207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band, 180307ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 180494b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 6); 180507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band, 180607ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 180794b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 10); 180807ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band, 180907ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_20, 181094b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 14); 181194b70cafSZong-Zhe Yang 181294b70cafSZong-Zhe Yang /* fill mcs 40m section */ 181307ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 181407ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 181594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 12); 181607ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 181707ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 181894b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 181907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band, 182007ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 182194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 182207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band, 182307ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_40, 182494b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 12); 182594b70cafSZong-Zhe Yang 182694b70cafSZong-Zhe Yang /* fill mcs 80m section */ 182707ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 182807ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 182994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 183007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band, 183107ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_80, 183294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 183394b70cafSZong-Zhe Yang 183494b70cafSZong-Zhe Yang /* fill mcs 160m section */ 183507ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band, 183607ef5f2fSZong-Zhe Yang RTW89_CHANNEL_WIDTH_160, 183794b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch); 183894b70cafSZong-Zhe Yang 183994b70cafSZong-Zhe Yang /* fill mcs 40m 0p5 section */ 184007ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 184194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 184207ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 184394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 184494b70cafSZong-Zhe Yang 184594b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 184694b70cafSZong-Zhe Yang lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 184794b70cafSZong-Zhe Yang 184894b70cafSZong-Zhe Yang /* fill mcs 40m 2p5 section */ 184907ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40, 185094b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 185107ef5f2fSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40, 185294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 185394b70cafSZong-Zhe Yang 185494b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 185594b70cafSZong-Zhe Yang lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]); 185694b70cafSZong-Zhe Yang } 185794b70cafSZong-Zhe Yang 18589b43bd1aSZong-Zhe Yang static 1859e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev, 186007ef5f2fSZong-Zhe Yang const struct rtw89_chan *chan, 1861e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 1862e3ec7017SPing-Ke Shih u8 ntx) 1863e3ec7017SPing-Ke Shih { 186407ef5f2fSZong-Zhe Yang u8 band = chan->band_type; 1865cbb145b9SZong-Zhe Yang u8 pri_ch = chan->primary_channel; 1866cbb145b9SZong-Zhe Yang u8 ch = chan->channel; 1867cbb145b9SZong-Zhe Yang u8 bw = chan->band_width; 1868e3ec7017SPing-Ke Shih 1869e3ec7017SPing-Ke Shih memset(lmt, 0, sizeof(*lmt)); 1870e3ec7017SPing-Ke Shih 1871e3ec7017SPing-Ke Shih switch (bw) { 1872e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 187307ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch); 1874e3ec7017SPing-Ke Shih break; 1875e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 187607ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch, 187707ef5f2fSZong-Zhe Yang pri_ch); 1878e3ec7017SPing-Ke Shih break; 1879e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 188007ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch, 188107ef5f2fSZong-Zhe Yang pri_ch); 188294b70cafSZong-Zhe Yang break; 188394b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 188407ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch, 188507ef5f2fSZong-Zhe Yang pri_ch); 1886e3ec7017SPing-Ke Shih break; 1887e3ec7017SPing-Ke Shih } 1888e3ec7017SPing-Ke Shih } 1889e3ec7017SPing-Ke Shih 189007ef5f2fSZong-Zhe Yang static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band, 1891e3ec7017SPing-Ke Shih u8 ru, u8 ntx, u8 ch) 1892e3ec7017SPing-Ke Shih { 18935395482aSZong-Zhe Yang const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms; 18945395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz; 18955395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz; 18965395482aSZong-Zhe Yang const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz; 1897dad142c3SZong-Zhe Yang struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory; 1898ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1899e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 1900dad142c3SZong-Zhe Yang u8 reg6 = regulatory->reg_6ghz_power; 1901e3ec7017SPing-Ke Shih s8 lmt_ru = 0, sar; 1902e3ec7017SPing-Ke Shih 1903e3ec7017SPing-Ke Shih switch (band) { 1904e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 19055395482aSZong-Zhe Yang lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][regd][ch_idx]; 19065395482aSZong-Zhe Yang if (lmt_ru) 19075395482aSZong-Zhe Yang break; 19085395482aSZong-Zhe Yang 19095395482aSZong-Zhe Yang lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx]; 1910e3ec7017SPing-Ke Shih break; 1911e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 19125395482aSZong-Zhe Yang lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][regd][ch_idx]; 19135395482aSZong-Zhe Yang if (lmt_ru) 19145395482aSZong-Zhe Yang break; 19155395482aSZong-Zhe Yang 19165395482aSZong-Zhe Yang lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx]; 1917e3ec7017SPing-Ke Shih break; 1918ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 1919dad142c3SZong-Zhe Yang lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][regd][reg6][ch_idx]; 19205395482aSZong-Zhe Yang if (lmt_ru) 19215395482aSZong-Zhe Yang break; 19225395482aSZong-Zhe Yang 1923dad142c3SZong-Zhe Yang lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][RTW89_WW] 1924dad142c3SZong-Zhe Yang [RTW89_REG_6GHZ_POWER_DFLT] 1925dad142c3SZong-Zhe Yang [ch_idx]; 1926ac74f016SZong-Zhe Yang break; 1927e3ec7017SPing-Ke Shih default: 1928e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1929e3ec7017SPing-Ke Shih return 0; 1930e3ec7017SPing-Ke Shih } 1931e3ec7017SPing-Ke Shih 1932e3ec7017SPing-Ke Shih lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru); 1933e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1934e3ec7017SPing-Ke Shih 1935e3ec7017SPing-Ke Shih return min(lmt_ru, sar); 1936e3ec7017SPing-Ke Shih } 1937e3ec7017SPing-Ke Shih 1938e3ec7017SPing-Ke Shih static void 1939e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev, 1940e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 194107ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1942e3ec7017SPing-Ke Shih { 194307ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 194407ef5f2fSZong-Zhe Yang RTW89_RU26, 1945e3ec7017SPing-Ke Shih ntx, ch); 194607ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 194707ef5f2fSZong-Zhe Yang RTW89_RU52, 1948e3ec7017SPing-Ke Shih ntx, ch); 194907ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 195007ef5f2fSZong-Zhe Yang RTW89_RU106, 1951e3ec7017SPing-Ke Shih ntx, ch); 1952e3ec7017SPing-Ke Shih } 1953e3ec7017SPing-Ke Shih 1954e3ec7017SPing-Ke Shih static void 1955e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev, 1956e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 195707ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1958e3ec7017SPing-Ke Shih { 195907ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196007ef5f2fSZong-Zhe Yang RTW89_RU26, 1961e3ec7017SPing-Ke Shih ntx, ch - 2); 196207ef5f2fSZong-Zhe Yang lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196307ef5f2fSZong-Zhe Yang RTW89_RU26, 1964e3ec7017SPing-Ke Shih ntx, ch + 2); 196507ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196607ef5f2fSZong-Zhe Yang RTW89_RU52, 1967e3ec7017SPing-Ke Shih ntx, ch - 2); 196807ef5f2fSZong-Zhe Yang lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 196907ef5f2fSZong-Zhe Yang RTW89_RU52, 1970e3ec7017SPing-Ke Shih ntx, ch + 2); 197107ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197207ef5f2fSZong-Zhe Yang RTW89_RU106, 1973e3ec7017SPing-Ke Shih ntx, ch - 2); 197407ef5f2fSZong-Zhe Yang lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 197507ef5f2fSZong-Zhe Yang RTW89_RU106, 1976e3ec7017SPing-Ke Shih ntx, ch + 2); 1977e3ec7017SPing-Ke Shih } 1978e3ec7017SPing-Ke Shih 1979e3ec7017SPing-Ke Shih static void 1980e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev, 1981e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 198207ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 1983e3ec7017SPing-Ke Shih { 198407ef5f2fSZong-Zhe Yang lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 198507ef5f2fSZong-Zhe Yang RTW89_RU26, 1986e3ec7017SPing-Ke Shih ntx, ch - 6); 198707ef5f2fSZong-Zhe Yang lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 198807ef5f2fSZong-Zhe Yang RTW89_RU26, 1989e3ec7017SPing-Ke Shih ntx, ch - 2); 199007ef5f2fSZong-Zhe Yang lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 199107ef5f2fSZong-Zhe Yang RTW89_RU26, 1992e3ec7017SPing-Ke Shih ntx, ch + 2); 199307ef5f2fSZong-Zhe Yang lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 199407ef5f2fSZong-Zhe Yang RTW89_RU26, 1995e3ec7017SPing-Ke Shih ntx, ch + 6); 199607ef5f2fSZong-Zhe Yang lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 199707ef5f2fSZong-Zhe Yang RTW89_RU52, 1998e3ec7017SPing-Ke Shih ntx, ch - 6); 199907ef5f2fSZong-Zhe Yang lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 200007ef5f2fSZong-Zhe Yang RTW89_RU52, 2001e3ec7017SPing-Ke Shih ntx, ch - 2); 200207ef5f2fSZong-Zhe Yang lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 200307ef5f2fSZong-Zhe Yang RTW89_RU52, 2004e3ec7017SPing-Ke Shih ntx, ch + 2); 200507ef5f2fSZong-Zhe Yang lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 200607ef5f2fSZong-Zhe Yang RTW89_RU52, 2007e3ec7017SPing-Ke Shih ntx, ch + 6); 200807ef5f2fSZong-Zhe Yang lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 200907ef5f2fSZong-Zhe Yang RTW89_RU106, 2010e3ec7017SPing-Ke Shih ntx, ch - 6); 201107ef5f2fSZong-Zhe Yang lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 201207ef5f2fSZong-Zhe Yang RTW89_RU106, 2013e3ec7017SPing-Ke Shih ntx, ch - 2); 201407ef5f2fSZong-Zhe Yang lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 201507ef5f2fSZong-Zhe Yang RTW89_RU106, 2016e3ec7017SPing-Ke Shih ntx, ch + 2); 201707ef5f2fSZong-Zhe Yang lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 201807ef5f2fSZong-Zhe Yang RTW89_RU106, 2019e3ec7017SPing-Ke Shih ntx, ch + 6); 2020e3ec7017SPing-Ke Shih } 2021e3ec7017SPing-Ke Shih 202294b70cafSZong-Zhe Yang static void 202394b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev, 202494b70cafSZong-Zhe Yang struct rtw89_txpwr_limit_ru *lmt_ru, 202507ef5f2fSZong-Zhe Yang u8 band, u8 ntx, u8 ch) 202694b70cafSZong-Zhe Yang { 202794b70cafSZong-Zhe Yang static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 }; 202894b70cafSZong-Zhe Yang int i; 202994b70cafSZong-Zhe Yang 203094b70cafSZong-Zhe Yang static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM); 203194b70cafSZong-Zhe Yang for (i = 0; i < RTW89_RU_SEC_NUM; i++) { 203207ef5f2fSZong-Zhe Yang lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 203394b70cafSZong-Zhe Yang RTW89_RU26, 203494b70cafSZong-Zhe Yang ntx, 203594b70cafSZong-Zhe Yang ch + ofst[i]); 203607ef5f2fSZong-Zhe Yang lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 203794b70cafSZong-Zhe Yang RTW89_RU52, 203894b70cafSZong-Zhe Yang ntx, 203994b70cafSZong-Zhe Yang ch + ofst[i]); 204007ef5f2fSZong-Zhe Yang lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 204194b70cafSZong-Zhe Yang RTW89_RU106, 204294b70cafSZong-Zhe Yang ntx, 204394b70cafSZong-Zhe Yang ch + ofst[i]); 204494b70cafSZong-Zhe Yang } 204594b70cafSZong-Zhe Yang } 204694b70cafSZong-Zhe Yang 20479b43bd1aSZong-Zhe Yang static 2048e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev, 204907ef5f2fSZong-Zhe Yang const struct rtw89_chan *chan, 2050e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 2051e3ec7017SPing-Ke Shih u8 ntx) 2052e3ec7017SPing-Ke Shih { 205307ef5f2fSZong-Zhe Yang u8 band = chan->band_type; 2054cbb145b9SZong-Zhe Yang u8 ch = chan->channel; 2055cbb145b9SZong-Zhe Yang u8 bw = chan->band_width; 2056e3ec7017SPing-Ke Shih 2057e3ec7017SPing-Ke Shih memset(lmt_ru, 0, sizeof(*lmt_ru)); 2058e3ec7017SPing-Ke Shih 2059e3ec7017SPing-Ke Shih switch (bw) { 2060e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 206107ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx, 206207ef5f2fSZong-Zhe Yang ch); 2063e3ec7017SPing-Ke Shih break; 2064e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 206507ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx, 206607ef5f2fSZong-Zhe Yang ch); 2067e3ec7017SPing-Ke Shih break; 2068e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 206907ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx, 207007ef5f2fSZong-Zhe Yang ch); 2071e3ec7017SPing-Ke Shih break; 207294b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 207307ef5f2fSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx, 207407ef5f2fSZong-Zhe Yang ch); 207594b70cafSZong-Zhe Yang break; 2076e3ec7017SPing-Ke Shih } 2077e3ec7017SPing-Ke Shih } 20789b43bd1aSZong-Zhe Yang 20799b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev, 20809b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 20819b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 20829b43bd1aSZong-Zhe Yang { 2083ddf9a2eaSZong-Zhe Yang u8 max_nss_num = rtwdev->chip->rf_path_num; 20849b43bd1aSZong-Zhe Yang static const u8 rs[] = { 20859b43bd1aSZong-Zhe Yang RTW89_RS_CCK, 20869b43bd1aSZong-Zhe Yang RTW89_RS_OFDM, 20879b43bd1aSZong-Zhe Yang RTW89_RS_MCS, 20889b43bd1aSZong-Zhe Yang RTW89_RS_HEDCM, 20899b43bd1aSZong-Zhe Yang }; 20909b43bd1aSZong-Zhe Yang struct rtw89_rate_desc cur; 20919b43bd1aSZong-Zhe Yang u8 band = chan->band_type; 20929b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 20939b43bd1aSZong-Zhe Yang u32 addr, val; 20949b43bd1aSZong-Zhe Yang s8 v[4] = {}; 20959b43bd1aSZong-Zhe Yang u8 i; 20969b43bd1aSZong-Zhe Yang 20979b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 20989b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr byrate with ch=%d\n", ch); 20999b43bd1aSZong-Zhe Yang 2100b4a283fbSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_CCK] % 4); 2101b4a283fbSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_OFDM] % 4); 2102b4a283fbSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_MCS] % 4); 2103b4a283fbSZong-Zhe Yang BUILD_BUG_ON(rtw89_rs_idx_num[RTW89_RS_HEDCM] % 4); 21049b43bd1aSZong-Zhe Yang 21059b43bd1aSZong-Zhe Yang addr = R_AX_PWR_BY_RATE; 2106ddf9a2eaSZong-Zhe Yang for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) { 21079b43bd1aSZong-Zhe Yang for (i = 0; i < ARRAY_SIZE(rs); i++) { 2108b4a283fbSZong-Zhe Yang if (cur.nss >= rtw89_rs_nss_num[rs[i]]) 21099b43bd1aSZong-Zhe Yang continue; 21109b43bd1aSZong-Zhe Yang 21119b43bd1aSZong-Zhe Yang cur.rs = rs[i]; 2112b4a283fbSZong-Zhe Yang for (cur.idx = 0; cur.idx < rtw89_rs_idx_num[rs[i]]; 21139b43bd1aSZong-Zhe Yang cur.idx++) { 21149b43bd1aSZong-Zhe Yang v[cur.idx % 4] = 21159b43bd1aSZong-Zhe Yang rtw89_phy_read_txpwr_byrate(rtwdev, 21169b43bd1aSZong-Zhe Yang band, 21179b43bd1aSZong-Zhe Yang &cur); 21189b43bd1aSZong-Zhe Yang 21199b43bd1aSZong-Zhe Yang if ((cur.idx + 1) % 4) 21209b43bd1aSZong-Zhe Yang continue; 21219b43bd1aSZong-Zhe Yang 21229b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), v[0]) | 21239b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), v[1]) | 21249b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), v[2]) | 21259b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), v[3]); 21269b43bd1aSZong-Zhe Yang 21279b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, 21289b43bd1aSZong-Zhe Yang val); 21299b43bd1aSZong-Zhe Yang addr += 4; 21309b43bd1aSZong-Zhe Yang } 21319b43bd1aSZong-Zhe Yang } 21329b43bd1aSZong-Zhe Yang } 21339b43bd1aSZong-Zhe Yang } 21349b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate); 21359b43bd1aSZong-Zhe Yang 21369b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev, 21379b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 21389b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 21399b43bd1aSZong-Zhe Yang { 21409b43bd1aSZong-Zhe Yang struct rtw89_rate_desc desc = { 21419b43bd1aSZong-Zhe Yang .nss = RTW89_NSS_1, 21429b43bd1aSZong-Zhe Yang .rs = RTW89_RS_OFFSET, 21439b43bd1aSZong-Zhe Yang }; 21449b43bd1aSZong-Zhe Yang u8 band = chan->band_type; 2145b4a283fbSZong-Zhe Yang s8 v[RTW89_RATE_OFFSET_NUM] = {}; 21469b43bd1aSZong-Zhe Yang u32 val; 21479b43bd1aSZong-Zhe Yang 21489b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n"); 21499b43bd1aSZong-Zhe Yang 2150b4a283fbSZong-Zhe Yang for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_NUM; desc.idx++) 21519b43bd1aSZong-Zhe Yang v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc); 21529b43bd1aSZong-Zhe Yang 2153b4a283fbSZong-Zhe Yang BUILD_BUG_ON(RTW89_RATE_OFFSET_NUM != 5); 21549b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(3, 0), v[0]) | 21559b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(7, 4), v[1]) | 21569b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(11, 8), v[2]) | 21579b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 12), v[3]) | 21589b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(19, 16), v[4]); 21599b43bd1aSZong-Zhe Yang 21609b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL, 21619b43bd1aSZong-Zhe Yang GENMASK(19, 0), val); 21629b43bd1aSZong-Zhe Yang } 21639b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset); 21649b43bd1aSZong-Zhe Yang 21659b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev, 21669b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 21679b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 21689b43bd1aSZong-Zhe Yang { 2169ddf9a2eaSZong-Zhe Yang u8 max_ntx_num = rtwdev->chip->rf_path_num; 21709b43bd1aSZong-Zhe Yang struct rtw89_txpwr_limit lmt; 21719b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 21729b43bd1aSZong-Zhe Yang u8 bw = chan->band_width; 21739b43bd1aSZong-Zhe Yang const s8 *ptr; 21749b43bd1aSZong-Zhe Yang u32 addr, val; 21759b43bd1aSZong-Zhe Yang u8 i, j; 21769b43bd1aSZong-Zhe Yang 21779b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 21789b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw); 21799b43bd1aSZong-Zhe Yang 21809b43bd1aSZong-Zhe Yang BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) != 21819b43bd1aSZong-Zhe Yang RTW89_TXPWR_LMT_PAGE_SIZE); 21829b43bd1aSZong-Zhe Yang 21839b43bd1aSZong-Zhe Yang addr = R_AX_PWR_LMT; 2184ddf9a2eaSZong-Zhe Yang for (i = 0; i < max_ntx_num; i++) { 21859b43bd1aSZong-Zhe Yang rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i); 21869b43bd1aSZong-Zhe Yang 21879b43bd1aSZong-Zhe Yang ptr = (s8 *)&lmt; 21889b43bd1aSZong-Zhe Yang for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE; 21899b43bd1aSZong-Zhe Yang j += 4, addr += 4, ptr += 4) { 21909b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 21919b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), ptr[1]) | 21929b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), ptr[2]) | 21939b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), ptr[3]); 21949b43bd1aSZong-Zhe Yang 21959b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 21969b43bd1aSZong-Zhe Yang } 21979b43bd1aSZong-Zhe Yang } 21989b43bd1aSZong-Zhe Yang } 21999b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit); 22009b43bd1aSZong-Zhe Yang 22019b43bd1aSZong-Zhe Yang void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev, 22029b43bd1aSZong-Zhe Yang const struct rtw89_chan *chan, 22039b43bd1aSZong-Zhe Yang enum rtw89_phy_idx phy_idx) 22049b43bd1aSZong-Zhe Yang { 2205ddf9a2eaSZong-Zhe Yang u8 max_ntx_num = rtwdev->chip->rf_path_num; 22069b43bd1aSZong-Zhe Yang struct rtw89_txpwr_limit_ru lmt_ru; 22079b43bd1aSZong-Zhe Yang u8 ch = chan->channel; 22089b43bd1aSZong-Zhe Yang u8 bw = chan->band_width; 22099b43bd1aSZong-Zhe Yang const s8 *ptr; 22109b43bd1aSZong-Zhe Yang u32 addr, val; 22119b43bd1aSZong-Zhe Yang u8 i, j; 22129b43bd1aSZong-Zhe Yang 22139b43bd1aSZong-Zhe Yang rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 22149b43bd1aSZong-Zhe Yang "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw); 22159b43bd1aSZong-Zhe Yang 22169b43bd1aSZong-Zhe Yang BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) != 22179b43bd1aSZong-Zhe Yang RTW89_TXPWR_LMT_RU_PAGE_SIZE); 22189b43bd1aSZong-Zhe Yang 22199b43bd1aSZong-Zhe Yang addr = R_AX_PWR_RU_LMT; 2220ddf9a2eaSZong-Zhe Yang for (i = 0; i < max_ntx_num; i++) { 22219b43bd1aSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i); 22229b43bd1aSZong-Zhe Yang 22239b43bd1aSZong-Zhe Yang ptr = (s8 *)&lmt_ru; 22249b43bd1aSZong-Zhe Yang for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE; 22259b43bd1aSZong-Zhe Yang j += 4, addr += 4, ptr += 4) { 22269b43bd1aSZong-Zhe Yang val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 22279b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(15, 8), ptr[1]) | 22289b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(23, 16), ptr[2]) | 22299b43bd1aSZong-Zhe Yang FIELD_PREP(GENMASK(31, 24), ptr[3]); 22309b43bd1aSZong-Zhe Yang 22319b43bd1aSZong-Zhe Yang rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 22329b43bd1aSZong-Zhe Yang } 22339b43bd1aSZong-Zhe Yang } 22349b43bd1aSZong-Zhe Yang } 22359b43bd1aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru); 2236e3ec7017SPing-Ke Shih 2237e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data { 2238e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 2239e3ec7017SPing-Ke Shih struct sk_buff *c2h; 2240e3ec7017SPing-Ke Shih }; 2241e3ec7017SPing-Ke Shih 2242e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta) 2243e3ec7017SPing-Ke Shih { 2244e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data; 2245e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = ra_data->rtwdev; 2246e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 224757cafeb1SPing-Ke Shih const struct rtw89_c2h_ra_rpt *c2h = 224857cafeb1SPing-Ke Shih (const struct rtw89_c2h_ra_rpt *)ra_data->c2h->data; 2249e3ec7017SPing-Ke Shih struct rtw89_ra_report *ra_report = &rtwsta->ra_report; 22505c152231SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 22515c152231SPing-Ke Shih bool format_v1 = chip->chip_gen == RTW89_CHIP_BE; 2252e3ec7017SPing-Ke Shih u8 mode, rate, bw, giltf, mac_id; 22539a3a593cSPing-Ke Shih u16 legacy_bitrate; 22549a3a593cSPing-Ke Shih bool valid; 22550d466f05SPing-Ke Shih u8 mcs = 0; 22565c152231SPing-Ke Shih u8 t; 2257e3ec7017SPing-Ke Shih 225857cafeb1SPing-Ke Shih mac_id = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MACID); 2259e3ec7017SPing-Ke Shih if (mac_id != rtwsta->mac_id) 2260e3ec7017SPing-Ke Shih return; 2261e3ec7017SPing-Ke Shih 226257cafeb1SPing-Ke Shih rate = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MCSNSS); 226357cafeb1SPing-Ke Shih bw = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW); 226457cafeb1SPing-Ke Shih giltf = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_GILTF); 226557cafeb1SPing-Ke Shih mode = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL); 2266e3ec7017SPing-Ke Shih 22675c152231SPing-Ke Shih if (format_v1) { 22685c152231SPing-Ke Shih t = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MCSNSS_B7); 22695c152231SPing-Ke Shih rate |= u8_encode_bits(t, BIT(7)); 22705c152231SPing-Ke Shih t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW_B2); 22715c152231SPing-Ke Shih bw |= u8_encode_bits(t, BIT(2)); 22725c152231SPing-Ke Shih t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL_B2); 22735c152231SPing-Ke Shih mode |= u8_encode_bits(t, BIT(2)); 22745c152231SPing-Ke Shih } 22755c152231SPing-Ke Shih 22769a3a593cSPing-Ke Shih if (mode == RTW89_RA_RPT_MODE_LEGACY) { 22779a3a593cSPing-Ke Shih valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate); 22789a3a593cSPing-Ke Shih if (!valid) 22799a3a593cSPing-Ke Shih return; 22809a3a593cSPing-Ke Shih } 22819a3a593cSPing-Ke Shih 22820d466f05SPing-Ke Shih memset(&ra_report->txrate, 0, sizeof(ra_report->txrate)); 22839a3a593cSPing-Ke Shih 2284e3ec7017SPing-Ke Shih switch (mode) { 2285e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_LEGACY: 22869a3a593cSPing-Ke Shih ra_report->txrate.legacy = legacy_bitrate; 2287e3ec7017SPing-Ke Shih break; 2288e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HT: 2289e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS; 229011fe4ccdSZong-Zhe Yang if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw)) 2291e3ec7017SPing-Ke Shih rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate), 2292e3ec7017SPing-Ke Shih FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate)); 2293e3ec7017SPing-Ke Shih else 2294e3ec7017SPing-Ke Shih rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate); 2295e3ec7017SPing-Ke Shih ra_report->txrate.mcs = rate; 2296e3ec7017SPing-Ke Shih if (giltf) 2297e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 22980d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs & 0x07; 2299e3ec7017SPing-Ke Shih break; 2300e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_VHT: 2301e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS; 23025c152231SPing-Ke Shih ra_report->txrate.mcs = format_v1 ? 23035c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) : 23045c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS); 23055c152231SPing-Ke Shih ra_report->txrate.nss = format_v1 ? 23065c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 : 23075c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1; 2308e3ec7017SPing-Ke Shih if (giltf) 2309e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 23100d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs; 2311e3ec7017SPing-Ke Shih break; 2312e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HE: 2313e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS; 23145c152231SPing-Ke Shih ra_report->txrate.mcs = format_v1 ? 23155c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) : 23165c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS); 23175c152231SPing-Ke Shih ra_report->txrate.nss = format_v1 ? 23185c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 : 23195c152231SPing-Ke Shih u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1; 2320e3ec7017SPing-Ke Shih if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08) 2321e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8; 2322e3ec7017SPing-Ke Shih else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16) 2323e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6; 2324e3ec7017SPing-Ke Shih else 2325e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2; 23260d466f05SPing-Ke Shih mcs = ra_report->txrate.mcs; 2327e3ec7017SPing-Ke Shih break; 2328e3ec7017SPing-Ke Shih } 2329e3ec7017SPing-Ke Shih 2330167044afSPing-Ke Shih ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw); 2331e3ec7017SPing-Ke Shih ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate); 23325c152231SPing-Ke Shih ra_report->hw_rate = format_v1 ? 23335c152231SPing-Ke Shih u16_encode_bits(mode, RTW89_HW_RATE_V1_MASK_MOD) | 23345c152231SPing-Ke Shih u16_encode_bits(rate, RTW89_HW_RATE_V1_MASK_VAL) : 23355c152231SPing-Ke Shih u16_encode_bits(mode, RTW89_HW_RATE_MASK_MOD) | 23365c152231SPing-Ke Shih u16_encode_bits(rate, RTW89_HW_RATE_MASK_VAL); 23370d466f05SPing-Ke Shih ra_report->might_fallback_legacy = mcs <= 2; 23384c51541dSBenjamin Berg sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report); 23394c51541dSBenjamin Berg rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1; 2340e3ec7017SPing-Ke Shih } 2341e3ec7017SPing-Ke Shih 2342e3ec7017SPing-Ke Shih static void 2343e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2344e3ec7017SPing-Ke Shih { 2345e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data ra_data; 2346e3ec7017SPing-Ke Shih 2347e3ec7017SPing-Ke Shih ra_data.rtwdev = rtwdev; 2348e3ec7017SPing-Ke Shih ra_data.c2h = c2h; 2349e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 2350e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt_iter, 2351e3ec7017SPing-Ke Shih &ra_data); 2352e3ec7017SPing-Ke Shih } 2353e3ec7017SPing-Ke Shih 2354e3ec7017SPing-Ke Shih static 2355e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev, 2356e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = { 2357e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt, 2358e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL, 2359e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_TXSTS] = NULL, 2360e3ec7017SPing-Ke Shih }; 2361e3ec7017SPing-Ke Shih 2362e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb, 2363e3ec7017SPing-Ke Shih u32 len, u8 class, u8 func) 2364e3ec7017SPing-Ke Shih { 2365e3ec7017SPing-Ke Shih void (*handler)(struct rtw89_dev *rtwdev, 2366e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = NULL; 2367e3ec7017SPing-Ke Shih 2368e3ec7017SPing-Ke Shih switch (class) { 2369e3ec7017SPing-Ke Shih case RTW89_PHY_C2H_CLASS_RA: 2370e3ec7017SPing-Ke Shih if (func < RTW89_PHY_C2H_FUNC_RA_MAX) 2371e3ec7017SPing-Ke Shih handler = rtw89_phy_c2h_ra_handler[func]; 2372e3ec7017SPing-Ke Shih break; 23733b66519bSPing-Ke Shih case RTW89_PHY_C2H_CLASS_DM: 23743b66519bSPing-Ke Shih if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY) 23753b66519bSPing-Ke Shih return; 23763b66519bSPing-Ke Shih fallthrough; 2377e3ec7017SPing-Ke Shih default: 2378e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d not support\n", class); 2379e3ec7017SPing-Ke Shih return; 2380e3ec7017SPing-Ke Shih } 2381e3ec7017SPing-Ke Shih if (!handler) { 2382e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d func %d not support\n", class, 2383e3ec7017SPing-Ke Shih func); 2384e3ec7017SPing-Ke Shih return; 2385e3ec7017SPing-Ke Shih } 2386e3ec7017SPing-Ke Shih handler(rtwdev, skb, len); 2387e3ec7017SPing-Ke Shih } 2388e3ec7017SPing-Ke Shih 2389e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo) 2390e3ec7017SPing-Ke Shih { 23910789881aSChia-Yuan Li const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info; 2392e3ec7017SPing-Ke Shih u32 reg_mask; 2393e3ec7017SPing-Ke Shih 2394e3ec7017SPing-Ke Shih if (sc_xo) 23950789881aSChia-Yuan Li reg_mask = xtal->sc_xo_mask; 2396e3ec7017SPing-Ke Shih else 23970789881aSChia-Yuan Li reg_mask = xtal->sc_xi_mask; 2398e3ec7017SPing-Ke Shih 23990789881aSChia-Yuan Li return (u8)rtw89_read32_mask(rtwdev, xtal->xcap_reg, reg_mask); 2400e3ec7017SPing-Ke Shih } 2401e3ec7017SPing-Ke Shih 2402e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo, 2403e3ec7017SPing-Ke Shih u8 val) 2404e3ec7017SPing-Ke Shih { 24050789881aSChia-Yuan Li const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info; 2406e3ec7017SPing-Ke Shih u32 reg_mask; 2407e3ec7017SPing-Ke Shih 2408e3ec7017SPing-Ke Shih if (sc_xo) 24090789881aSChia-Yuan Li reg_mask = xtal->sc_xo_mask; 2410e3ec7017SPing-Ke Shih else 24110789881aSChia-Yuan Li reg_mask = xtal->sc_xi_mask; 2412e3ec7017SPing-Ke Shih 24130789881aSChia-Yuan Li rtw89_write32_mask(rtwdev, xtal->xcap_reg, reg_mask, val); 2414e3ec7017SPing-Ke Shih } 2415e3ec7017SPing-Ke Shih 2416e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev, 2417e3ec7017SPing-Ke Shih u8 crystal_cap, bool force) 2418e3ec7017SPing-Ke Shih { 2419e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 24208379fa61SYuan-Han Zhang const struct rtw89_chip_info *chip = rtwdev->chip; 2421e3ec7017SPing-Ke Shih u8 sc_xi_val, sc_xo_val; 2422e3ec7017SPing-Ke Shih 2423e3ec7017SPing-Ke Shih if (!force && cfo->crystal_cap == crystal_cap) 2424e3ec7017SPing-Ke Shih return; 2425e3ec7017SPing-Ke Shih crystal_cap = clamp_t(u8, crystal_cap, 0, 127); 24260789881aSChia-Yuan Li if (chip->chip_id == RTL8852A || chip->chip_id == RTL8851B) { 2427e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap); 2428e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap); 2429e3ec7017SPing-Ke Shih sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true); 2430e3ec7017SPing-Ke Shih sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false); 24318379fa61SYuan-Han Zhang } else { 24328379fa61SYuan-Han Zhang rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, 24338379fa61SYuan-Han Zhang crystal_cap, XTAL_SC_XO_MASK); 24348379fa61SYuan-Han Zhang rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, 24358379fa61SYuan-Han Zhang crystal_cap, XTAL_SC_XI_MASK); 24368379fa61SYuan-Han Zhang rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val); 24378379fa61SYuan-Han Zhang rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val); 24388379fa61SYuan-Han Zhang } 2439e3ec7017SPing-Ke Shih cfo->crystal_cap = sc_xi_val; 2440e3ec7017SPing-Ke Shih cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap); 2441e3ec7017SPing-Ke Shih 2442e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val); 2443e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val); 2444e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n", 2445e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 2446e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n"); 2447e3ec7017SPing-Ke Shih } 2448e3ec7017SPing-Ke Shih 2449e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev) 2450e3ec7017SPing-Ke Shih { 2451e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2452e3ec7017SPing-Ke Shih u8 cap; 2453e3ec7017SPing-Ke Shih 2454e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK; 2455e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2456e3ec7017SPing-Ke Shih if (cfo->crystal_cap == cfo->def_x_cap) 2457e3ec7017SPing-Ke Shih return; 2458e3ec7017SPing-Ke Shih cap = cfo->crystal_cap; 2459e3ec7017SPing-Ke Shih cap += (cap > cfo->def_x_cap ? -1 : 1); 2460e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false); 2461e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2462e3ec7017SPing-Ke Shih "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap, 2463e3ec7017SPing-Ke Shih cfo->def_x_cap); 2464e3ec7017SPing-Ke Shih } 2465e3ec7017SPing-Ke Shih 2466e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo) 2467e3ec7017SPing-Ke Shih { 2468b7379148SYuan-Han Zhang const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp; 2469e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 2470e3ec7017SPing-Ke Shih s32 cfo_avg_312; 2471b7379148SYuan-Han Zhang s32 dcfo_comp_val; 2472e3ec7017SPing-Ke Shih int sign; 2473e3ec7017SPing-Ke Shih 2474e3ec7017SPing-Ke Shih if (!is_linked) { 2475e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n", 2476e3ec7017SPing-Ke Shih is_linked); 2477e3ec7017SPing-Ke Shih return; 2478e3ec7017SPing-Ke Shih } 2479e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo); 2480e3ec7017SPing-Ke Shih if (curr_cfo == 0) 2481e3ec7017SPing-Ke Shih return; 2482b7379148SYuan-Han Zhang dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO); 2483e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 24849f9882dbSEric Huang cfo_avg_312 = curr_cfo / 625 + sign * dcfo_comp_val; 24859f9882dbSEric Huang rtw89_debug(rtwdev, RTW89_DBG_CFO, "avg_cfo_312=%d step\n", cfo_avg_312); 2486e3ec7017SPing-Ke Shih if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV) 2487e3ec7017SPing-Ke Shih cfo_avg_312 = -cfo_avg_312; 2488b7379148SYuan-Han Zhang rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask, 2489e3ec7017SPing-Ke Shih cfo_avg_312); 2490e3ec7017SPing-Ke Shih } 2491e3ec7017SPing-Ke Shih 2492e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev) 2493e3ec7017SPing-Ke Shih { 24949f9882dbSEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 24959f9882dbSEric Huang 2496e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1); 2497e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8); 24989f9882dbSEric Huang 24999f9882dbSEric Huang if (chip->cfo_hw_comp) 25009f9882dbSEric Huang rtw89_write32_mask(rtwdev, R_AX_PWR_UL_CTRL2, 25019f9882dbSEric Huang B_AX_PWR_UL_CFO_MASK, 0x6); 25029f9882dbSEric Huang else 2503e3ec7017SPing-Ke Shih rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK); 2504e3ec7017SPing-Ke Shih } 2505e3ec7017SPing-Ke Shih 2506e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev) 2507e3ec7017SPing-Ke Shih { 2508e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2509e3ec7017SPing-Ke Shih struct rtw89_efuse *efuse = &rtwdev->efuse; 2510e3ec7017SPing-Ke Shih 2511e3ec7017SPing-Ke Shih cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK; 2512e3ec7017SPing-Ke Shih cfo->crystal_cap = cfo->crystal_cap_default; 2513e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap; 2514a9e06f2eSYi-Tang Chiu cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f); 2515a9e06f2eSYi-Tang Chiu cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1); 2516e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2517a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 2518e3ec7017SPing-Ke Shih cfo->x_cap_ofst = 0; 2519a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 2520e3ec7017SPing-Ke Shih cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE; 2521e3ec7017SPing-Ke Shih cfo->apply_compensation = false; 2522e3ec7017SPing-Ke Shih cfo->residual_cfo_acc = 0; 2523e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n", 2524e3ec7017SPing-Ke Shih cfo->crystal_cap_default); 2525e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true); 2526e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1); 2527e3ec7017SPing-Ke Shih rtw89_dcfo_comp_init(rtwdev); 2528e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = 2000; 2529e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2530e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2531e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2532bc013052SEric Huang cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE; 2533e3ec7017SPing-Ke Shih } 2534e3ec7017SPing-Ke Shih 2535e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev, 2536e3ec7017SPing-Ke Shih s32 curr_cfo) 2537e3ec7017SPing-Ke Shih { 2538e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2539e3ec7017SPing-Ke Shih s8 crystal_cap = cfo->crystal_cap; 2540e3ec7017SPing-Ke Shih s32 cfo_abs = abs(curr_cfo); 2541e3ec7017SPing-Ke Shih int sign; 2542e3ec7017SPing-Ke Shih 2543e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 2544e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_ENABLE_TH) 2545e3ec7017SPing-Ke Shih cfo->is_adjust = true; 2546e3ec7017SPing-Ke Shih } else { 2547e3ec7017SPing-Ke Shih if (cfo_abs < CFO_TRK_STOP_TH) 2548e3ec7017SPing-Ke Shih cfo->is_adjust = false; 2549e3ec7017SPing-Ke Shih } 2550e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 2551e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n"); 2552e3ec7017SPing-Ke Shih return; 2553e3ec7017SPing-Ke Shih } 2554e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 2555e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_STOP_TH_4) 2556e3ec7017SPing-Ke Shih crystal_cap += 7 * sign; 2557e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_3) 2558e3ec7017SPing-Ke Shih crystal_cap += 5 * sign; 2559e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_2) 2560e3ec7017SPing-Ke Shih crystal_cap += 3 * sign; 2561e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_1) 2562e3ec7017SPing-Ke Shih crystal_cap += 1 * sign; 2563e3ec7017SPing-Ke Shih else 2564e3ec7017SPing-Ke Shih return; 2565e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false); 2566e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2567e3ec7017SPing-Ke Shih "X_cap{Curr,Default}={0x%x,0x%x}\n", 2568e3ec7017SPing-Ke Shih cfo->crystal_cap, cfo->def_x_cap); 2569e3ec7017SPing-Ke Shih } 2570e3ec7017SPing-Ke Shih 2571e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev) 2572e3ec7017SPing-Ke Shih { 25739f9882dbSEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 2574e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2575e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 2576e3ec7017SPing-Ke Shih s32 cfo_cnt_all = 0; 2577e3ec7017SPing-Ke Shih s32 cfo_all_avg = 0; 2578e3ec7017SPing-Ke Shih u8 i; 2579e3ec7017SPing-Ke Shih 2580e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc != 1) 2581e3ec7017SPing-Ke Shih return 0; 2582e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n"); 2583e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2584e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2585e3ec7017SPing-Ke Shih continue; 2586e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 2587e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 2588e3ec7017SPing-Ke Shih cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all); 2589e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 25909f9882dbSEric Huang cfo->dcfo_avg = phy_div(cfo_khz_all << chip->dcfo_comp_sft, 25919f9882dbSEric Huang cfo_cnt_all); 2592e3ec7017SPing-Ke Shih } 2593e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2594e3ec7017SPing-Ke Shih "CFO track for macid = %d\n", i); 2595e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2596e3ec7017SPing-Ke Shih "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n", 2597e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_all_avg); 2598e3ec7017SPing-Ke Shih return cfo_all_avg; 2599e3ec7017SPing-Ke Shih } 2600e3ec7017SPing-Ke Shih 2601e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev) 2602e3ec7017SPing-Ke Shih { 2603e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2604e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 2605e3ec7017SPing-Ke Shih s32 target_cfo = 0; 2606e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 2607e3ec7017SPing-Ke Shih s32 cfo_khz_all_tp_wgt = 0; 2608e3ec7017SPing-Ke Shih s32 cfo_avg = 0; 2609e3ec7017SPing-Ke Shih s32 max_cfo_lb = BIT(31); 2610e3ec7017SPing-Ke Shih s32 min_cfo_ub = GENMASK(30, 0); 2611e3ec7017SPing-Ke Shih u16 cfo_cnt_all = 0; 2612e3ec7017SPing-Ke Shih u8 active_entry_cnt = 0; 2613e3ec7017SPing-Ke Shih u8 sta_cnt = 0; 2614e3ec7017SPing-Ke Shih u32 tp_all = 0; 2615e3ec7017SPing-Ke Shih u8 i; 2616e3ec7017SPing-Ke Shih u8 cfo_tol = 0; 2617e3ec7017SPing-Ke Shih 2618e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n"); 2619e3ec7017SPing-Ke Shih if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) { 2620e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n"); 2621e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2622e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2623e3ec7017SPing-Ke Shih continue; 2624e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 2625e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 2626e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all); 2627e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2628e3ec7017SPing-Ke Shih "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n", 2629e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_avg); 2630e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 2631e3ec7017SPing-Ke Shih } 2632e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) { 2633e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n"); 2634e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2635e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 2636e3ec7017SPing-Ke Shih continue; 2637e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 2638e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 2639e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 2640e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2641e3ec7017SPing-Ke Shih "Macid=%d, cfo_avg=%d\n", i, 2642e3ec7017SPing-Ke Shih cfo->cfo_avg[i]); 2643e3ec7017SPing-Ke Shih } 2644e3ec7017SPing-Ke Shih sta_cnt = rtwdev->total_sta_assoc; 2645e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt); 2646e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2647e3ec7017SPing-Ke Shih "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n", 2648e3ec7017SPing-Ke Shih cfo_khz_all, sta_cnt, cfo_avg); 2649e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 2650e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) { 2651e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n"); 2652e3ec7017SPing-Ke Shih cfo_tol = cfo->sta_cfo_tolerance; 2653e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 2654e3ec7017SPing-Ke Shih sta_cnt++; 2655e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] != 0) { 2656e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 2657e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 2658e3ec7017SPing-Ke Shih active_entry_cnt++; 2659e3ec7017SPing-Ke Shih } else { 2660e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = cfo->pre_cfo_avg[i]; 2661e3ec7017SPing-Ke Shih } 2662e3ec7017SPing-Ke Shih max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb); 2663e3ec7017SPing-Ke Shih min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub); 2664e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 2665e3ec7017SPing-Ke Shih /* need tp for each entry */ 2666e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2667e3ec7017SPing-Ke Shih "[%d] cfo_avg=%d, tp=tbd\n", 2668e3ec7017SPing-Ke Shih i, cfo->cfo_avg[i]); 2669e3ec7017SPing-Ke Shih if (sta_cnt >= rtwdev->total_sta_assoc) 2670e3ec7017SPing-Ke Shih break; 2671e3ec7017SPing-Ke Shih } 2672e3ec7017SPing-Ke Shih tp_all = stats->rx_throughput; /* need tp for each entry */ 2673e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all_tp_wgt, (s32)tp_all); 2674e3ec7017SPing-Ke Shih 2675e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n", 2676e3ec7017SPing-Ke Shih sta_cnt); 2677e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n", 2678e3ec7017SPing-Ke Shih active_entry_cnt); 2679e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2680e3ec7017SPing-Ke Shih "Msta cfo with tp_wgt=%d, avg_cfo=%d\n", 2681e3ec7017SPing-Ke Shih cfo_khz_all_tp_wgt, cfo_avg); 2682e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n", 2683e3ec7017SPing-Ke Shih max_cfo_lb, min_cfo_ub); 2684e3ec7017SPing-Ke Shih if (max_cfo_lb <= min_cfo_ub) { 2685e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2686e3ec7017SPing-Ke Shih "cfo win_size=%d\n", 2687e3ec7017SPing-Ke Shih min_cfo_ub - max_cfo_lb); 2688e3ec7017SPing-Ke Shih target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub); 2689e3ec7017SPing-Ke Shih } else { 2690e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2691c51ed740SColin Ian King "No intersection of cfo tolerance windows\n"); 2692e3ec7017SPing-Ke Shih target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt); 2693e3ec7017SPing-Ke Shih } 2694e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) 2695e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 2696e3ec7017SPing-Ke Shih } 2697e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo); 2698e3ec7017SPing-Ke Shih return target_cfo; 2699e3ec7017SPing-Ke Shih } 2700e3ec7017SPing-Ke Shih 2701e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev) 2702e3ec7017SPing-Ke Shih { 2703e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2704e3ec7017SPing-Ke Shih 2705e3ec7017SPing-Ke Shih memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail)); 2706e3ec7017SPing-Ke Shih memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt)); 2707e3ec7017SPing-Ke Shih cfo->packet_count = 0; 2708e3ec7017SPing-Ke Shih cfo->packet_count_pre = 0; 2709e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = 0; 2710e3ec7017SPing-Ke Shih } 2711e3ec7017SPing-Ke Shih 2712e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev) 2713e3ec7017SPing-Ke Shih { 2714e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2715e3ec7017SPing-Ke Shih s32 new_cfo = 0; 2716e3ec7017SPing-Ke Shih bool x_cap_update = false; 2717e3ec7017SPing-Ke Shih u8 pre_x_cap = cfo->crystal_cap; 27189f9882dbSEric Huang u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft; 2719e3ec7017SPing-Ke Shih 27209f9882dbSEric Huang cfo->dcfo_avg = 0; 2721e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n", 2722e3ec7017SPing-Ke Shih rtwdev->total_sta_assoc); 2723e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 0) { 2724e3ec7017SPing-Ke Shih rtw89_phy_cfo_reset(rtwdev); 2725e3ec7017SPing-Ke Shih return; 2726e3ec7017SPing-Ke Shih } 2727e3ec7017SPing-Ke Shih if (cfo->packet_count == 0) { 2728e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n"); 2729e3ec7017SPing-Ke Shih return; 2730e3ec7017SPing-Ke Shih } 2731e3ec7017SPing-Ke Shih if (cfo->packet_count == cfo->packet_count_pre) { 2732e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n"); 2733e3ec7017SPing-Ke Shih return; 2734e3ec7017SPing-Ke Shih } 2735e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 1) 2736e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_average_cfo_calc(rtwdev); 2737e3ec7017SPing-Ke Shih else 2738e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev); 2739e3ec7017SPing-Ke Shih if (new_cfo == 0) { 2740e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n"); 2741e3ec7017SPing-Ke Shih return; 2742e3ec7017SPing-Ke Shih } 2743a9e06f2eSYi-Tang Chiu if (cfo->divergence_lock_en) { 2744a9e06f2eSYi-Tang Chiu cfo->lock_cnt++; 2745a9e06f2eSYi-Tang Chiu if (cfo->lock_cnt > CFO_PERIOD_CNT) { 2746a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 2747a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 2748a9e06f2eSYi-Tang Chiu } else { 2749a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 2750a9e06f2eSYi-Tang Chiu } 2751a9e06f2eSYi-Tang Chiu return; 2752a9e06f2eSYi-Tang Chiu } 2753a9e06f2eSYi-Tang Chiu if (cfo->crystal_cap >= cfo->x_cap_ub || 2754a9e06f2eSYi-Tang Chiu cfo->crystal_cap <= cfo->x_cap_lb) { 2755a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = true; 2756a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 2757a9e06f2eSYi-Tang Chiu return; 2758a9e06f2eSYi-Tang Chiu } 2759a9e06f2eSYi-Tang Chiu 2760e3ec7017SPing-Ke Shih rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo); 2761e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = new_cfo; 27629f9882dbSEric Huang cfo->dcfo_avg_pre = cfo->dcfo_avg; 27631646ce8fSYe Guojin x_cap_update = cfo->crystal_cap != pre_x_cap; 2764e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update); 2765e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n", 2766e3ec7017SPing-Ke Shih cfo->def_x_cap, pre_x_cap, cfo->crystal_cap, 2767e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 2768e3ec7017SPing-Ke Shih if (x_cap_update) { 27699f9882dbSEric Huang if (cfo->dcfo_avg > 0) 27709f9882dbSEric Huang cfo->dcfo_avg -= CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft; 2771e3ec7017SPing-Ke Shih else 27729f9882dbSEric Huang cfo->dcfo_avg += CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft; 2773e3ec7017SPing-Ke Shih } 27749f9882dbSEric Huang rtw89_dcfo_comp(rtwdev, cfo->dcfo_avg); 2775e3ec7017SPing-Ke Shih rtw89_phy_cfo_statistics_reset(rtwdev); 2776e3ec7017SPing-Ke Shih } 2777e3ec7017SPing-Ke Shih 2778e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work) 2779e3ec7017SPing-Ke Shih { 2780e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 2781e3ec7017SPing-Ke Shih cfo_track_work.work); 2782e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2783e3ec7017SPing-Ke Shih 2784e3ec7017SPing-Ke Shih mutex_lock(&rtwdev->mutex); 2785e3ec7017SPing-Ke Shih if (!cfo->cfo_trig_by_timer_en) 2786e3ec7017SPing-Ke Shih goto out; 2787e3ec7017SPing-Ke Shih rtw89_leave_ps_mode(rtwdev); 2788e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2789e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2790e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2791e3ec7017SPing-Ke Shih out: 2792e3ec7017SPing-Ke Shih mutex_unlock(&rtwdev->mutex); 2793e3ec7017SPing-Ke Shih } 2794e3ec7017SPing-Ke Shih 2795e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev) 2796e3ec7017SPing-Ke Shih { 2797e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2798e3ec7017SPing-Ke Shih 2799e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2800e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2801e3ec7017SPing-Ke Shih } 2802e3ec7017SPing-Ke Shih 2803e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev) 2804e3ec7017SPing-Ke Shih { 2805e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2806e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 2807bc013052SEric Huang bool is_ul_ofdma = false, ofdma_acc_en = false; 2808bc013052SEric Huang 2809bc013052SEric Huang if (stats->rx_tf_periodic > CFO_TF_CNT_TH) 2810bc013052SEric Huang is_ul_ofdma = true; 2811bc013052SEric Huang if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE && 2812bc013052SEric Huang is_ul_ofdma) 2813bc013052SEric Huang ofdma_acc_en = true; 2814e3ec7017SPing-Ke Shih 2815e3ec7017SPing-Ke Shih switch (cfo->phy_cfo_status) { 2816e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_NORMAL: 2817e3ec7017SPing-Ke Shih if (stats->tx_throughput >= CFO_TP_UPPER) { 2818e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE; 2819e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = true; 2820e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = CFO_COMP_PERIOD; 2821e3ec7017SPing-Ke Shih rtw89_phy_cfo_start_work(rtwdev); 2822e3ec7017SPing-Ke Shih } 2823e3ec7017SPing-Ke Shih break; 2824e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_ENHANCE: 2825bc013052SEric Huang if (stats->tx_throughput <= CFO_TP_LOWER) 2826bc013052SEric Huang cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2827bc013052SEric Huang else if (ofdma_acc_en && 2828bc013052SEric Huang cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT) 2829bc013052SEric Huang cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD; 2830bc013052SEric Huang else 2831bc013052SEric Huang cfo->phy_cfo_trk_cnt++; 2832bc013052SEric Huang 2833bc013052SEric Huang if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) { 2834e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2835e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2836e3ec7017SPing-Ke Shih } 2837bc013052SEric Huang break; 2838bc013052SEric Huang case RTW89_PHY_DCFO_STATE_HOLD: 2839e3ec7017SPing-Ke Shih if (stats->tx_throughput <= CFO_TP_LOWER) { 2840e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2841e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2842e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2843bc013052SEric Huang } else { 2844bc013052SEric Huang cfo->phy_cfo_trk_cnt++; 2845e3ec7017SPing-Ke Shih } 2846e3ec7017SPing-Ke Shih break; 2847e3ec7017SPing-Ke Shih default: 2848e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2849e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2850e3ec7017SPing-Ke Shih break; 2851e3ec7017SPing-Ke Shih } 2852e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2853e3ec7017SPing-Ke Shih "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n", 2854e3ec7017SPing-Ke Shih stats->tx_throughput, cfo->phy_cfo_status, 2855e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt, 2856e3ec7017SPing-Ke Shih ewma_thermal_read(&rtwdev->phystat.avg_thermal[0])); 2857e3ec7017SPing-Ke Shih if (cfo->cfo_trig_by_timer_en) 2858e3ec7017SPing-Ke Shih return; 2859e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2860e3ec7017SPing-Ke Shih } 2861e3ec7017SPing-Ke Shih 2862e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val, 2863e3ec7017SPing-Ke Shih struct rtw89_rx_phy_ppdu *phy_ppdu) 2864e3ec7017SPing-Ke Shih { 2865e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2866e3ec7017SPing-Ke Shih u8 macid = phy_ppdu->mac_id; 2867e3ec7017SPing-Ke Shih 286897df8587SPing-Ke Shih if (macid >= CFO_TRACK_MAX_USER) { 286997df8587SPing-Ke Shih rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid); 287097df8587SPing-Ke Shih return; 287197df8587SPing-Ke Shih } 287297df8587SPing-Ke Shih 2873e3ec7017SPing-Ke Shih cfo->cfo_tail[macid] += cfo_val; 2874e3ec7017SPing-Ke Shih cfo->cfo_cnt[macid]++; 2875e3ec7017SPing-Ke Shih cfo->packet_count++; 2876e3ec7017SPing-Ke Shih } 2877e3ec7017SPing-Ke Shih 287829136c95SEric Huang void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 287929136c95SEric Huang { 288029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 288129136c95SEric Huang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 288229136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 288329136c95SEric Huang 288429136c95SEric Huang if (!chip->support_ul_tb_ctrl) 288529136c95SEric Huang return; 288629136c95SEric Huang 288729136c95SEric Huang rtwvif->def_tri_idx = 288829136c95SEric Huang rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG); 288929136c95SEric Huang 289029136c95SEric Huang if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV) 289129136c95SEric Huang rtwvif->dyn_tb_bedge_en = false; 289229136c95SEric Huang else if (chan->band_type >= RTW89_BAND_5G && 289329136c95SEric Huang chan->band_width >= RTW89_CHANNEL_WIDTH_40) 289429136c95SEric Huang rtwvif->dyn_tb_bedge_en = true; 289529136c95SEric Huang else 289629136c95SEric Huang rtwvif->dyn_tb_bedge_en = false; 289729136c95SEric Huang 289829136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 289929136c95SEric Huang "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n", 290029136c95SEric Huang ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx); 290129136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 290229136c95SEric Huang "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n", 290329136c95SEric Huang rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en); 290429136c95SEric Huang } 290529136c95SEric Huang 290629136c95SEric Huang struct rtw89_phy_ul_tb_check_data { 290729136c95SEric Huang bool valid; 290829136c95SEric Huang bool high_tf_client; 290929136c95SEric Huang bool low_tf_client; 291029136c95SEric Huang bool dyn_tb_bedge_en; 291129136c95SEric Huang u8 def_tri_idx; 291229136c95SEric Huang }; 291329136c95SEric Huang 291429136c95SEric Huang static 291529136c95SEric Huang void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev, 291629136c95SEric Huang struct rtw89_vif *rtwvif, 291729136c95SEric Huang struct rtw89_phy_ul_tb_check_data *ul_tb_data) 291829136c95SEric Huang { 291929136c95SEric Huang struct rtw89_traffic_stats *stats = &rtwdev->stats; 292029136c95SEric Huang struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 292129136c95SEric Huang 292229136c95SEric Huang if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION) 292329136c95SEric Huang return; 292429136c95SEric Huang 292529136c95SEric Huang if (!vif->cfg.assoc) 292629136c95SEric Huang return; 292729136c95SEric Huang 292829136c95SEric Huang if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH) 292929136c95SEric Huang ul_tb_data->high_tf_client = true; 293029136c95SEric Huang else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH) 293129136c95SEric Huang ul_tb_data->low_tf_client = true; 293229136c95SEric Huang 293329136c95SEric Huang ul_tb_data->valid = true; 293429136c95SEric Huang ul_tb_data->def_tri_idx = rtwvif->def_tri_idx; 293529136c95SEric Huang ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en; 293629136c95SEric Huang } 293729136c95SEric Huang 293829136c95SEric Huang void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev) 293929136c95SEric Huang { 294029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 294129136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 294229136c95SEric Huang struct rtw89_phy_ul_tb_check_data ul_tb_data = {}; 294329136c95SEric Huang struct rtw89_vif *rtwvif; 294429136c95SEric Huang 294529136c95SEric Huang if (!chip->support_ul_tb_ctrl) 294629136c95SEric Huang return; 294729136c95SEric Huang 294829136c95SEric Huang if (rtwdev->total_sta_assoc != 1) 294929136c95SEric Huang return; 295029136c95SEric Huang 295129136c95SEric Huang rtw89_for_each_rtwvif(rtwdev, rtwvif) 295229136c95SEric Huang rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data); 295329136c95SEric Huang 295429136c95SEric Huang if (!ul_tb_data.valid) 295529136c95SEric Huang return; 295629136c95SEric Huang 295729136c95SEric Huang if (ul_tb_data.dyn_tb_bedge_en) { 295829136c95SEric Huang if (ul_tb_data.high_tf_client) { 295929136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0); 296029136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 296129136c95SEric Huang "[ULTB] Turn off if_bandedge\n"); 296229136c95SEric Huang } else if (ul_tb_data.low_tf_client) { 296329136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 296429136c95SEric Huang ul_tb_info->def_if_bandedge); 296529136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 296629136c95SEric Huang "[ULTB] Set to default if_bandedge = %d\n", 296729136c95SEric Huang ul_tb_info->def_if_bandedge); 296829136c95SEric Huang } 296929136c95SEric Huang } 297029136c95SEric Huang 297129136c95SEric Huang if (ul_tb_info->dyn_tb_tri_en) { 297229136c95SEric Huang if (ul_tb_data.high_tf_client) { 297329136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 297429136c95SEric Huang B_TXSHAPE_TRIANGULAR_CFG, 0); 297529136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 297629136c95SEric Huang "[ULTB] Turn off Tx triangle\n"); 297729136c95SEric Huang } else if (ul_tb_data.low_tf_client) { 297829136c95SEric Huang rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 297929136c95SEric Huang B_TXSHAPE_TRIANGULAR_CFG, 298029136c95SEric Huang ul_tb_data.def_tri_idx); 298129136c95SEric Huang rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 298229136c95SEric Huang "[ULTB] Set to default tx_shap_idx = %d\n", 298329136c95SEric Huang ul_tb_data.def_tri_idx); 298429136c95SEric Huang } 298529136c95SEric Huang } 298629136c95SEric Huang } 298729136c95SEric Huang 298829136c95SEric Huang static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev) 298929136c95SEric Huang { 299029136c95SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 299129136c95SEric Huang struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 299229136c95SEric Huang 299329136c95SEric Huang if (!chip->support_ul_tb_ctrl) 299429136c95SEric Huang return; 299529136c95SEric Huang 299629136c95SEric Huang ul_tb_info->dyn_tb_tri_en = true; 299729136c95SEric Huang ul_tb_info->def_if_bandedge = 299829136c95SEric Huang rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN); 299929136c95SEric Huang } 300029136c95SEric Huang 3001e3715859SEric Huang static 3002e3715859SEric Huang void rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats *antdiv_sts) 3003e3715859SEric Huang { 3004e3715859SEric Huang ewma_rssi_init(&antdiv_sts->cck_rssi_avg); 3005e3715859SEric Huang ewma_rssi_init(&antdiv_sts->ofdm_rssi_avg); 3006e3715859SEric Huang ewma_rssi_init(&antdiv_sts->non_legacy_rssi_avg); 3007e3715859SEric Huang antdiv_sts->pkt_cnt_cck = 0; 3008e3715859SEric Huang antdiv_sts->pkt_cnt_ofdm = 0; 3009e3715859SEric Huang antdiv_sts->pkt_cnt_non_legacy = 0; 30105feecb40SEric Huang antdiv_sts->evm = 0; 3011e3715859SEric Huang } 3012e3715859SEric Huang 3013e3715859SEric Huang static void rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev *rtwdev, 3014e3715859SEric Huang struct rtw89_rx_phy_ppdu *phy_ppdu, 3015e3715859SEric Huang struct rtw89_antdiv_stats *stats) 3016e3715859SEric Huang { 3017*023d2f14SPing-Ke Shih if (rtw89_get_data_rate_mode(rtwdev, phy_ppdu->rate) == DATA_RATE_MODE_NON_HT) { 3018e3715859SEric Huang if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) { 3019e3715859SEric Huang ewma_rssi_add(&stats->cck_rssi_avg, phy_ppdu->rssi_avg); 3020e3715859SEric Huang stats->pkt_cnt_cck++; 3021e3715859SEric Huang } else { 3022e3715859SEric Huang ewma_rssi_add(&stats->ofdm_rssi_avg, phy_ppdu->rssi_avg); 3023e3715859SEric Huang stats->pkt_cnt_ofdm++; 30245feecb40SEric Huang stats->evm += phy_ppdu->ofdm.evm_min; 3025e3715859SEric Huang } 3026e3715859SEric Huang } else { 3027e3715859SEric Huang ewma_rssi_add(&stats->non_legacy_rssi_avg, phy_ppdu->rssi_avg); 3028e3715859SEric Huang stats->pkt_cnt_non_legacy++; 30295feecb40SEric Huang stats->evm += phy_ppdu->ofdm.evm_min; 3030e3715859SEric Huang } 3031e3715859SEric Huang } 3032e3715859SEric Huang 3033e3715859SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats *stats) 3034e3715859SEric Huang { 3035e3715859SEric Huang if (stats->pkt_cnt_non_legacy >= stats->pkt_cnt_cck && 3036e3715859SEric Huang stats->pkt_cnt_non_legacy >= stats->pkt_cnt_ofdm) 3037e3715859SEric Huang return ewma_rssi_read(&stats->non_legacy_rssi_avg); 3038e3715859SEric Huang else if (stats->pkt_cnt_ofdm >= stats->pkt_cnt_cck && 3039e3715859SEric Huang stats->pkt_cnt_ofdm >= stats->pkt_cnt_non_legacy) 3040e3715859SEric Huang return ewma_rssi_read(&stats->ofdm_rssi_avg); 3041e3715859SEric Huang else 3042e3715859SEric Huang return ewma_rssi_read(&stats->cck_rssi_avg); 3043e3715859SEric Huang } 3044e3715859SEric Huang 30455feecb40SEric Huang static u8 rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats *stats) 30465feecb40SEric Huang { 30475feecb40SEric Huang return phy_div(stats->evm, stats->pkt_cnt_non_legacy + stats->pkt_cnt_ofdm); 30485feecb40SEric Huang } 30495feecb40SEric Huang 3050e3715859SEric Huang void rtw89_phy_antdiv_parse(struct rtw89_dev *rtwdev, 3051e3715859SEric Huang struct rtw89_rx_phy_ppdu *phy_ppdu) 3052e3715859SEric Huang { 3053e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 3054e3715859SEric Huang struct rtw89_hal *hal = &rtwdev->hal; 3055e3715859SEric Huang 3056e3715859SEric Huang if (!hal->ant_diversity || hal->ant_diversity_fixed) 3057e3715859SEric Huang return; 3058e3715859SEric Huang 3059e3715859SEric Huang rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->target_stats); 3060e3715859SEric Huang 3061e3715859SEric Huang if (!antdiv->get_stats) 3062e3715859SEric Huang return; 3063e3715859SEric Huang 3064e3715859SEric Huang if (hal->antenna_rx == RF_A) 3065e3715859SEric Huang rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->main_stats); 3066e3715859SEric Huang else if (hal->antenna_rx == RF_B) 3067e3715859SEric Huang rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->aux_stats); 3068e3715859SEric Huang } 3069e3715859SEric Huang 3070a90c613dSEric Huang static void rtw89_phy_antdiv_reg_init(struct rtw89_dev *rtwdev) 3071a90c613dSEric Huang { 3072a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_ANT_TRAIN_EN, 3073a90c613dSEric Huang 0x0, RTW89_PHY_0); 3074a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_TX_ANT_SEL, 3075a90c613dSEric Huang 0x0, RTW89_PHY_0); 3076a90c613dSEric Huang 3077a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_TRSW_TX_EXTEND, 3078a90c613dSEric Huang 0x0, RTW89_PHY_0); 3079a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_HW_ANTSW_DIS_BY_GNT_BT, 3080a90c613dSEric Huang 0x0, RTW89_PHY_0); 3081a90c613dSEric Huang 3082a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_BT_FORCE_ANTIDX_EN, 3083a90c613dSEric Huang 0x0, RTW89_PHY_0); 3084a90c613dSEric Huang 3085a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_RFSW_CTRL_ANT0_BASE, B_RFSW_CTRL_ANT_MAPPING, 3086a90c613dSEric Huang 0x0100, RTW89_PHY_0); 3087a90c613dSEric Huang 3088a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_BTG_TRX, 3089a90c613dSEric Huang 0x1, RTW89_PHY_0); 3090a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_HW_CTRL, 3091a90c613dSEric Huang 0x0, RTW89_PHY_0); 3092a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_2G, 3093a90c613dSEric Huang 0x0, RTW89_PHY_0); 3094a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_5G, 3095a90c613dSEric Huang 0x0, RTW89_PHY_0); 3096a90c613dSEric Huang } 3097a90c613dSEric Huang 3098e3715859SEric Huang static void rtw89_phy_antdiv_sts_reset(struct rtw89_dev *rtwdev) 3099e3715859SEric Huang { 3100e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 3101e3715859SEric Huang 3102e3715859SEric Huang rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats); 3103e3715859SEric Huang rtw89_phy_antdiv_sts_instance_reset(&antdiv->main_stats); 3104e3715859SEric Huang rtw89_phy_antdiv_sts_instance_reset(&antdiv->aux_stats); 3105e3715859SEric Huang } 3106e3715859SEric Huang 3107a90c613dSEric Huang static void rtw89_phy_antdiv_init(struct rtw89_dev *rtwdev) 3108a90c613dSEric Huang { 3109e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 3110a90c613dSEric Huang struct rtw89_hal *hal = &rtwdev->hal; 3111a90c613dSEric Huang 3112a90c613dSEric Huang if (!hal->ant_diversity) 3113a90c613dSEric Huang return; 3114a90c613dSEric Huang 3115e3715859SEric Huang antdiv->get_stats = false; 3116e3715859SEric Huang antdiv->rssi_pre = 0; 3117e3715859SEric Huang rtw89_phy_antdiv_sts_reset(rtwdev); 3118a90c613dSEric Huang rtw89_phy_antdiv_reg_init(rtwdev); 3119a90c613dSEric Huang } 3120a90c613dSEric Huang 3121e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev) 3122e3ec7017SPing-Ke Shih { 3123e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 3124e3ec7017SPing-Ke Shih int i; 3125e3ec7017SPing-Ke Shih u8 th; 3126e3ec7017SPing-Ke Shih 3127e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) { 3128e3ec7017SPing-Ke Shih th = rtw89_chip_get_thermal(rtwdev, i); 3129e3ec7017SPing-Ke Shih if (th) 3130e3ec7017SPing-Ke Shih ewma_thermal_add(&phystat->avg_thermal[i], th); 3131e3ec7017SPing-Ke Shih 3132e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, 3133e3ec7017SPing-Ke Shih "path(%d) thermal cur=%u avg=%ld", i, th, 3134e3ec7017SPing-Ke Shih ewma_thermal_read(&phystat->avg_thermal[i])); 3135e3ec7017SPing-Ke Shih } 3136e3ec7017SPing-Ke Shih } 3137e3ec7017SPing-Ke Shih 3138e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data { 3139e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 3140e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info; 3141e3ec7017SPing-Ke Shih bool rssi_changed; 3142e3ec7017SPing-Ke Shih }; 3143e3ec7017SPing-Ke Shih 3144e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data, 3145e3ec7017SPing-Ke Shih struct ieee80211_sta *sta) 3146e3ec7017SPing-Ke Shih { 3147e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 3148e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data *rssi_data = 3149e3ec7017SPing-Ke Shih (struct rtw89_phy_iter_rssi_data *)data; 3150e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info; 3151e3ec7017SPing-Ke Shih unsigned long rssi_curr; 3152e3ec7017SPing-Ke Shih 3153e3ec7017SPing-Ke Shih rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi); 3154e3ec7017SPing-Ke Shih 3155e3ec7017SPing-Ke Shih if (rssi_curr < ch_info->rssi_min) { 3156e3ec7017SPing-Ke Shih ch_info->rssi_min = rssi_curr; 3157e3ec7017SPing-Ke Shih ch_info->rssi_min_macid = rtwsta->mac_id; 3158e3ec7017SPing-Ke Shih } 3159e3ec7017SPing-Ke Shih 3160e3ec7017SPing-Ke Shih if (rtwsta->prev_rssi == 0) { 3161e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 3162e3ec7017SPing-Ke Shih } else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) { 3163e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 3164e3ec7017SPing-Ke Shih rssi_data->rssi_changed = true; 3165e3ec7017SPing-Ke Shih } 3166e3ec7017SPing-Ke Shih } 3167e3ec7017SPing-Ke Shih 3168e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev) 3169e3ec7017SPing-Ke Shih { 3170e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data rssi_data = {0}; 3171e3ec7017SPing-Ke Shih 3172e3ec7017SPing-Ke Shih rssi_data.rtwdev = rtwdev; 3173e3ec7017SPing-Ke Shih rssi_data.ch_info = &rtwdev->ch_info; 3174e3ec7017SPing-Ke Shih rssi_data.ch_info->rssi_min = U8_MAX; 3175e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 3176e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update_iter, 3177e3ec7017SPing-Ke Shih &rssi_data); 3178e3ec7017SPing-Ke Shih if (rssi_data.rssi_changed) 3179e3ec7017SPing-Ke Shih rtw89_btc_ntfy_wl_sta(rtwdev); 3180e3ec7017SPing-Ke Shih } 3181e3ec7017SPing-Ke Shih 3182e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev) 3183e3ec7017SPing-Ke Shih { 3184e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 3185e3ec7017SPing-Ke Shih int i; 3186e3ec7017SPing-Ke Shih 3187e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) 3188e3ec7017SPing-Ke Shih ewma_thermal_init(&phystat->avg_thermal[i]); 3189e3ec7017SPing-Ke Shih 3190e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 3191e3ec7017SPing-Ke Shih 3192e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 3193e3ec7017SPing-Ke Shih memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat)); 3194e3ec7017SPing-Ke Shih } 3195e3ec7017SPing-Ke Shih 3196e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev) 3197e3ec7017SPing-Ke Shih { 3198e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 3199e3ec7017SPing-Ke Shih 3200e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 3201e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update(rtwdev); 3202e3ec7017SPing-Ke Shih 3203e3ec7017SPing-Ke Shih phystat->last_pkt_stat = phystat->cur_pkt_stat; 3204e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 3205e3ec7017SPing-Ke Shih } 3206e3ec7017SPing-Ke Shih 3207e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us) 3208e3ec7017SPing-Ke Shih { 3209e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3210e3ec7017SPing-Ke Shih 3211e3ec7017SPing-Ke Shih return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 3212e3ec7017SPing-Ke Shih } 3213e3ec7017SPing-Ke Shih 3214e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx) 3215e3ec7017SPing-Ke Shih { 3216e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3217e3ec7017SPing-Ke Shih 3218e3ec7017SPing-Ke Shih return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 3219e3ec7017SPing-Ke Shih } 3220e3ec7017SPing-Ke Shih 3221e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev) 3222e3ec7017SPing-Ke Shih { 3223e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3224e3ec7017SPing-Ke Shih 3225e3ec7017SPing-Ke Shih env->ccx_manual_ctrl = false; 3226e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3227e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 3228e3ec7017SPing-Ke Shih env->ccx_period = 0; 3229e3ec7017SPing-Ke Shih env->ccx_unit_idx = RTW89_CCX_32_US; 3230e3ec7017SPing-Ke Shih 3231e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1); 3232e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1); 3233e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 3234e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK, 3235e3ec7017SPing-Ke Shih RTW89_CCX_EDCCA_BW20_0); 3236e3ec7017SPing-Ke Shih } 3237e3ec7017SPing-Ke Shih 3238e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report, 3239e3ec7017SPing-Ke Shih u16 score) 3240e3ec7017SPing-Ke Shih { 3241e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3242e3ec7017SPing-Ke Shih u32 numer = 0; 3243e3ec7017SPing-Ke Shih u16 ret = 0; 3244e3ec7017SPing-Ke Shih 3245e3ec7017SPing-Ke Shih numer = report * score + (env->ccx_period >> 1); 3246e3ec7017SPing-Ke Shih if (env->ccx_period) 3247e3ec7017SPing-Ke Shih ret = numer / env->ccx_period; 3248e3ec7017SPing-Ke Shih 3249e3ec7017SPing-Ke Shih return ret >= score ? score - 1 : ret; 3250e3ec7017SPing-Ke Shih } 3251e3ec7017SPing-Ke Shih 3252e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev, 3253e3ec7017SPing-Ke Shih u16 time_ms, u32 *period, 3254e3ec7017SPing-Ke Shih u32 *unit_idx) 3255e3ec7017SPing-Ke Shih { 3256e3ec7017SPing-Ke Shih u32 idx; 3257e3ec7017SPing-Ke Shih u8 quotient; 3258e3ec7017SPing-Ke Shih 3259e3ec7017SPing-Ke Shih if (time_ms >= CCX_MAX_PERIOD) 3260e3ec7017SPing-Ke Shih time_ms = CCX_MAX_PERIOD; 3261e3ec7017SPing-Ke Shih 3262e3ec7017SPing-Ke Shih quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD; 3263e3ec7017SPing-Ke Shih 3264e3ec7017SPing-Ke Shih if (quotient < 4) 3265e3ec7017SPing-Ke Shih idx = RTW89_CCX_4_US; 3266e3ec7017SPing-Ke Shih else if (quotient < 8) 3267e3ec7017SPing-Ke Shih idx = RTW89_CCX_8_US; 3268e3ec7017SPing-Ke Shih else if (quotient < 16) 3269e3ec7017SPing-Ke Shih idx = RTW89_CCX_16_US; 3270e3ec7017SPing-Ke Shih else 3271e3ec7017SPing-Ke Shih idx = RTW89_CCX_32_US; 3272e3ec7017SPing-Ke Shih 3273e3ec7017SPing-Ke Shih *unit_idx = idx; 3274e3ec7017SPing-Ke Shih *period = (time_ms * MS_TO_4US_RATIO) >> idx; 3275e3ec7017SPing-Ke Shih 3276e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3277e3ec7017SPing-Ke Shih "[Trigger Time] period:%d, unit_idx:%d\n", 3278e3ec7017SPing-Ke Shih *period, *unit_idx); 3279e3ec7017SPing-Ke Shih } 3280e3ec7017SPing-Ke Shih 3281e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev) 3282e3ec7017SPing-Ke Shih { 3283e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3284e3ec7017SPing-Ke Shih 3285e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3286e3ec7017SPing-Ke Shih "lv:(%d)->(0)\n", env->ccx_rac_lv); 3287e3ec7017SPing-Ke Shih 3288e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3289e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 3290e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3291e3ec7017SPing-Ke Shih } 3292e3ec7017SPing-Ke Shih 3293e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev, 3294e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 3295e3ec7017SPing-Ke Shih { 3296e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3297e3ec7017SPing-Ke Shih bool is_update = env->ifs_clm_app != para->ifs_clm_app; 3298e3ec7017SPing-Ke Shih u8 i = 0; 3299e3ec7017SPing-Ke Shih u16 *ifs_th_l = env->ifs_clm_th_l; 3300e3ec7017SPing-Ke Shih u16 *ifs_th_h = env->ifs_clm_th_h; 3301e3ec7017SPing-Ke Shih u32 ifs_th0_us = 0, ifs_th_times = 0; 3302e3ec7017SPing-Ke Shih u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0}; 3303e3ec7017SPing-Ke Shih 3304e3ec7017SPing-Ke Shih if (!is_update) 3305e3ec7017SPing-Ke Shih goto ifs_update_finished; 3306e3ec7017SPing-Ke Shih 3307e3ec7017SPing-Ke Shih switch (para->ifs_clm_app) { 3308e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_INIT: 3309e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_BACKGROUND: 3310e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_ACS: 3311e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG: 3312e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DIG: 3313e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_TDMA_DIG: 3314e3ec7017SPing-Ke Shih ifs_th0_us = IFS_CLM_TH0_UPPER; 3315e3ec7017SPing-Ke Shih ifs_th_times = IFS_CLM_TH_MUL; 3316e3ec7017SPing-Ke Shih break; 3317e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG_MANUAL: 3318e3ec7017SPing-Ke Shih ifs_th0_us = para->ifs_clm_manual_th0; 3319e3ec7017SPing-Ke Shih ifs_th_times = para->ifs_clm_manual_th_times; 3320e3ec7017SPing-Ke Shih break; 3321e3ec7017SPing-Ke Shih default: 3322e3ec7017SPing-Ke Shih break; 3323e3ec7017SPing-Ke Shih } 3324e3ec7017SPing-Ke Shih 3325e3ec7017SPing-Ke Shih /* Set sampling threshold for 4 different regions, unit in idx_cnt. 3326e3ec7017SPing-Ke Shih * low[i] = high[i-1] + 1 3327e3ec7017SPing-Ke Shih * high[i] = high[i-1] * ifs_th_times 3328e3ec7017SPing-Ke Shih */ 3329e3ec7017SPing-Ke Shih ifs_th_l[IFS_CLM_TH_START_IDX] = 0; 3330e3ec7017SPing-Ke Shih ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us; 3331e3ec7017SPing-Ke Shih ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev, 3332e3ec7017SPing-Ke Shih ifs_th0_us); 3333e3ec7017SPing-Ke Shih for (i = 1; i < RTW89_IFS_CLM_NUM; i++) { 3334e3ec7017SPing-Ke Shih ifs_th_l[i] = ifs_th_h[i - 1] + 1; 3335e3ec7017SPing-Ke Shih ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times; 3336e3ec7017SPing-Ke Shih ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]); 3337e3ec7017SPing-Ke Shih } 3338e3ec7017SPing-Ke Shih 3339e3ec7017SPing-Ke Shih ifs_update_finished: 3340e3ec7017SPing-Ke Shih if (!is_update) 3341e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3342e3ec7017SPing-Ke Shih "No need to update IFS_TH\n"); 3343e3ec7017SPing-Ke Shih 3344e3ec7017SPing-Ke Shih return is_update; 3345e3ec7017SPing-Ke Shih } 3346e3ec7017SPing-Ke Shih 3347e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev) 3348e3ec7017SPing-Ke Shih { 3349e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3350e3ec7017SPing-Ke Shih u8 i = 0; 3351e3ec7017SPing-Ke Shih 3352e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK, 3353e3ec7017SPing-Ke Shih env->ifs_clm_th_l[0]); 3354e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK, 3355e3ec7017SPing-Ke Shih env->ifs_clm_th_l[1]); 3356e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK, 3357e3ec7017SPing-Ke Shih env->ifs_clm_th_l[2]); 3358e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK, 3359e3ec7017SPing-Ke Shih env->ifs_clm_th_l[3]); 3360e3ec7017SPing-Ke Shih 3361e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK, 3362e3ec7017SPing-Ke Shih env->ifs_clm_th_h[0]); 3363e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK, 3364e3ec7017SPing-Ke Shih env->ifs_clm_th_h[1]); 3365e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK, 3366e3ec7017SPing-Ke Shih env->ifs_clm_th_h[2]); 3367e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK, 3368e3ec7017SPing-Ke Shih env->ifs_clm_th_h[3]); 3369e3ec7017SPing-Ke Shih 3370e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3371e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3372e3ec7017SPing-Ke Shih "Update IFS_T%d_th{low, high} : {%d, %d}\n", 3373e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]); 3374e3ec7017SPing-Ke Shih } 3375e3ec7017SPing-Ke Shih 3376e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev) 3377e3ec7017SPing-Ke Shih { 3378e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3379e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 3380e3ec7017SPing-Ke Shih 3381e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3382e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = 0; 3383e3ec7017SPing-Ke Shih 3384e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_INIT; 3385e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, ¶)) 3386e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 3387e3ec7017SPing-Ke Shih 3388e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN, 3389e3ec7017SPing-Ke Shih true); 3390e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true); 3391e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true); 3392e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true); 3393e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true); 3394e3ec7017SPing-Ke Shih } 3395e3ec7017SPing-Ke Shih 3396e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev, 3397e3ec7017SPing-Ke Shih enum rtw89_env_racing_lv level) 3398e3ec7017SPing-Ke Shih { 3399e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3400e3ec7017SPing-Ke Shih int ret = 0; 3401e3ec7017SPing-Ke Shih 3402e3ec7017SPing-Ke Shih if (level >= RTW89_RAC_MAX_NUM) { 3403e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3404e3ec7017SPing-Ke Shih "[WARNING] Wrong LV=%d\n", level); 3405e3ec7017SPing-Ke Shih return -EINVAL; 3406e3ec7017SPing-Ke Shih } 3407e3ec7017SPing-Ke Shih 3408e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3409e3ec7017SPing-Ke Shih "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing, 3410e3ec7017SPing-Ke Shih env->ccx_rac_lv, level); 3411e3ec7017SPing-Ke Shih 3412e3ec7017SPing-Ke Shih if (env->ccx_ongoing) { 3413e3ec7017SPing-Ke Shih if (level <= env->ccx_rac_lv) 3414e3ec7017SPing-Ke Shih ret = -EINVAL; 3415e3ec7017SPing-Ke Shih else 3416e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 3417e3ec7017SPing-Ke Shih } 3418e3ec7017SPing-Ke Shih 3419e3ec7017SPing-Ke Shih if (ret == 0) 3420e3ec7017SPing-Ke Shih env->ccx_rac_lv = level; 3421e3ec7017SPing-Ke Shih 3422e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n", 3423e3ec7017SPing-Ke Shih !ret); 3424e3ec7017SPing-Ke Shih 3425e3ec7017SPing-Ke Shih return ret; 3426e3ec7017SPing-Ke Shih } 3427e3ec7017SPing-Ke Shih 3428e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev) 3429e3ec7017SPing-Ke Shih { 3430e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3431e3ec7017SPing-Ke Shih 3432e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0); 3433e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0); 3434e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1); 3435e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 3436e3ec7017SPing-Ke Shih 3437e3ec7017SPing-Ke Shih env->ccx_ongoing = true; 3438e3ec7017SPing-Ke Shih } 3439e3ec7017SPing-Ke Shih 3440e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev) 3441e3ec7017SPing-Ke Shih { 3442e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3443e3ec7017SPing-Ke Shih u8 i = 0; 3444e3ec7017SPing-Ke Shih u32 res = 0; 3445e3ec7017SPing-Ke Shih 3446e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio = 3447e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT); 3448e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca_ratio = 3449e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca, 3450e3ec7017SPing-Ke Shih PERCENT); 3451e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio = 3452e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT); 3453e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_ratio = 3454e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT); 3455e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio = 3456e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa, 3457e3ec7017SPing-Ke Shih PERCENT); 3458e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio = 3459e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa, 3460e3ec7017SPing-Ke Shih PERCENT); 3461e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil = 3462e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL); 3463e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_permil = 3464e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL); 3465e3ec7017SPing-Ke Shih 3466e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) { 3467e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) { 3468e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD; 3469e3ec7017SPing-Ke Shih } else { 3470e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = 3471e3ec7017SPing-Ke Shih rtw89_phy_ccx_idx_to_us(rtwdev, 3472e3ec7017SPing-Ke Shih env->ifs_clm_avg[i]); 3473e3ec7017SPing-Ke Shih } 3474e3ec7017SPing-Ke Shih 3475e3ec7017SPing-Ke Shih res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]); 3476e3ec7017SPing-Ke Shih res += env->ifs_clm_his[i] >> 1; 3477e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i]) 3478e3ec7017SPing-Ke Shih res /= env->ifs_clm_his[i]; 3479e3ec7017SPing-Ke Shih else 3480e3ec7017SPing-Ke Shih res = 0; 3481e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i] = res; 3482e3ec7017SPing-Ke Shih } 3483e3ec7017SPing-Ke Shih 3484e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3485e3ec7017SPing-Ke Shih "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n", 3486e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio); 3487e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3488e3ec7017SPing-Ke Shih "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n", 3489e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio); 3490e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3491e3ec7017SPing-Ke Shih "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n", 3492e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil); 3493e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3494e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n", 3495e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio, 3496e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio); 3497e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3498e3ec7017SPing-Ke Shih "Time:[his, ifs_avg(us), cca_avg(us)]\n"); 3499e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3500e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n", 3501e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i], 3502e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i]); 3503e3ec7017SPing-Ke Shih } 3504e3ec7017SPing-Ke Shih 3505e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev) 3506e3ec7017SPing-Ke Shih { 3507e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3508e3ec7017SPing-Ke Shih u8 i = 0; 3509e3ec7017SPing-Ke Shih 3510e3ec7017SPing-Ke Shih if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) { 3511e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3512e3ec7017SPing-Ke Shih "Get IFS_CLM report Fail\n"); 3513e3ec7017SPing-Ke Shih return false; 3514e3ec7017SPing-Ke Shih } 3515e3ec7017SPing-Ke Shih 3516e3ec7017SPing-Ke Shih env->ifs_clm_tx = 3517e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 3518e3ec7017SPing-Ke Shih B_IFS_CLM_TX_CNT_MSK); 3519e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca = 3520e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 3521e3ec7017SPing-Ke Shih B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK); 3522e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa = 3523e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 3524e3ec7017SPing-Ke Shih B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK); 3525e3ec7017SPing-Ke Shih env->ifs_clm_ofdmcca_excl_fa = 3526e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 3527e3ec7017SPing-Ke Shih B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK); 3528e3ec7017SPing-Ke Shih env->ifs_clm_cckfa = 3529e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 3530e3ec7017SPing-Ke Shih B_IFS_CLM_CCK_FA_MSK); 3531e3ec7017SPing-Ke Shih env->ifs_clm_ofdmfa = 3532e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 3533e3ec7017SPing-Ke Shih B_IFS_CLM_OFDM_FA_MSK); 3534e3ec7017SPing-Ke Shih 3535e3ec7017SPing-Ke Shih env->ifs_clm_his[0] = 3536e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK); 3537e3ec7017SPing-Ke Shih env->ifs_clm_his[1] = 3538e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK); 3539e3ec7017SPing-Ke Shih env->ifs_clm_his[2] = 3540e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK); 3541e3ec7017SPing-Ke Shih env->ifs_clm_his[3] = 3542e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK); 3543e3ec7017SPing-Ke Shih 3544e3ec7017SPing-Ke Shih env->ifs_clm_avg[0] = 3545e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK); 3546e3ec7017SPing-Ke Shih env->ifs_clm_avg[1] = 3547e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK); 3548e3ec7017SPing-Ke Shih env->ifs_clm_avg[2] = 3549e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK); 3550e3ec7017SPing-Ke Shih env->ifs_clm_avg[3] = 3551e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK); 3552e3ec7017SPing-Ke Shih 3553e3ec7017SPing-Ke Shih env->ifs_clm_cca[0] = 3554e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK); 3555e3ec7017SPing-Ke Shih env->ifs_clm_cca[1] = 3556e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK); 3557e3ec7017SPing-Ke Shih env->ifs_clm_cca[2] = 3558e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK); 3559e3ec7017SPing-Ke Shih env->ifs_clm_cca[3] = 3560e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK); 3561e3ec7017SPing-Ke Shih 3562e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs = 3563e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK); 3564e3ec7017SPing-Ke Shih 3565e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n", 3566e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs); 3567e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3568e3ec7017SPing-Ke Shih "{Tx, EDCCA_exclu_cca} = {%d, %d}\n", 3569e3ec7017SPing-Ke Shih env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca); 3570e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3571e3ec7017SPing-Ke Shih "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n", 3572e3ec7017SPing-Ke Shih env->ifs_clm_cckfa, env->ifs_clm_ofdmfa); 3573e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3574e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n", 3575e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa); 3576e3ec7017SPing-Ke Shih 3577e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n"); 3578e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 3579e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3580e3ec7017SPing-Ke Shih "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i], 3581e3ec7017SPing-Ke Shih env->ifs_clm_avg[i], env->ifs_clm_cca[i]); 3582e3ec7017SPing-Ke Shih 3583e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_get_utility(rtwdev); 3584e3ec7017SPing-Ke Shih 3585e3ec7017SPing-Ke Shih return true; 3586e3ec7017SPing-Ke Shih } 3587e3ec7017SPing-Ke Shih 3588e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev, 3589e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 3590e3ec7017SPing-Ke Shih { 3591e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3592e3ec7017SPing-Ke Shih u32 period = 0; 3593e3ec7017SPing-Ke Shih u32 unit_idx = 0; 3594e3ec7017SPing-Ke Shih 3595e3ec7017SPing-Ke Shih if (para->mntr_time == 0) { 3596e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3597e3ec7017SPing-Ke Shih "[WARN] MNTR_TIME is 0\n"); 3598e3ec7017SPing-Ke Shih return -EINVAL; 3599e3ec7017SPing-Ke Shih } 3600e3ec7017SPing-Ke Shih 3601e3ec7017SPing-Ke Shih if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv)) 3602e3ec7017SPing-Ke Shih return -EINVAL; 3603e3ec7017SPing-Ke Shih 3604e3ec7017SPing-Ke Shih if (para->mntr_time != env->ifs_clm_mntr_time) { 3605e3ec7017SPing-Ke Shih rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time, 3606e3ec7017SPing-Ke Shih &period, &unit_idx); 3607e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 3608e3ec7017SPing-Ke Shih B_IFS_CLM_PERIOD_MSK, period); 3609e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 3610e3ec7017SPing-Ke Shih B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx); 3611e3ec7017SPing-Ke Shih 3612e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3613e3ec7017SPing-Ke Shih "Update IFS-CLM time ((%d)) -> ((%d))\n", 3614e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time, para->mntr_time); 3615e3ec7017SPing-Ke Shih 3616e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = para->mntr_time; 3617e3ec7017SPing-Ke Shih env->ccx_period = (u16)period; 3618e3ec7017SPing-Ke Shih env->ccx_unit_idx = (u8)unit_idx; 3619e3ec7017SPing-Ke Shih } 3620e3ec7017SPing-Ke Shih 3621e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) { 3622e3ec7017SPing-Ke Shih env->ifs_clm_app = para->ifs_clm_app; 3623e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 3624e3ec7017SPing-Ke Shih } 3625e3ec7017SPing-Ke Shih 3626e3ec7017SPing-Ke Shih return 0; 3627e3ec7017SPing-Ke Shih } 3628e3ec7017SPing-Ke Shih 3629e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev) 3630e3ec7017SPing-Ke Shih { 3631e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3632e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 3633e3ec7017SPing-Ke Shih u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL; 3634e3ec7017SPing-Ke Shih 3635e3ec7017SPing-Ke Shih env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL; 3636e3ec7017SPing-Ke Shih if (env->ccx_manual_ctrl) { 3637e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3638e3ec7017SPing-Ke Shih "CCX in manual ctrl\n"); 3639e3ec7017SPing-Ke Shih return; 3640e3ec7017SPing-Ke Shih } 3641e3ec7017SPing-Ke Shih 3642e3ec7017SPing-Ke Shih /* only ifs_clm for now */ 3643e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_get_result(rtwdev)) 3644e3ec7017SPing-Ke Shih env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM; 3645e3ec7017SPing-Ke Shih 3646e3ec7017SPing-Ke Shih rtw89_phy_ccx_racing_release(rtwdev); 3647e3ec7017SPing-Ke Shih para.mntr_time = 1900; 3648e3ec7017SPing-Ke Shih para.rac_lv = RTW89_RAC_LV_1; 3649e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 3650e3ec7017SPing-Ke Shih 3651e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_set(rtwdev, ¶) == 0) 3652e3ec7017SPing-Ke Shih chk_result |= RTW89_PHY_ENV_MON_IFS_CLM; 3653e3ec7017SPing-Ke Shih if (chk_result) 3654e3ec7017SPing-Ke Shih rtw89_phy_ccx_trigger(rtwdev); 3655e3ec7017SPing-Ke Shih 3656e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 3657e3ec7017SPing-Ke Shih "get_result=0x%x, chk_result:0x%x\n", 3658e3ec7017SPing-Ke Shih env->ccx_watchdog_result, chk_result); 3659e3ec7017SPing-Ke Shih } 3660e3ec7017SPing-Ke Shih 3661eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page) 3662eb4e52b3SPo Hao Huang { 36639e2f177dSZong-Zhe Yang if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM || 3664eb4e52b3SPo Hao Huang *ie_page == RTW89_RSVD_9) 3665eb4e52b3SPo Hao Huang return false; 3666eb4e52b3SPo Hao Huang else if (*ie_page > RTW89_RSVD_9) 3667eb4e52b3SPo Hao Huang *ie_page -= 1; 3668eb4e52b3SPo Hao Huang 3669eb4e52b3SPo Hao Huang return true; 3670eb4e52b3SPo Hao Huang } 3671eb4e52b3SPo Hao Huang 3672eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page) 3673eb4e52b3SPo Hao Huang { 3674eb4e52b3SPo Hao Huang static const u8 ie_page_shift = 2; 3675eb4e52b3SPo Hao Huang 3676eb4e52b3SPo Hao Huang return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift); 3677eb4e52b3SPo Hao Huang } 3678eb4e52b3SPo Hao Huang 3679eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev, 3680eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page) 3681eb4e52b3SPo Hao Huang { 3682eb4e52b3SPo Hao Huang u32 addr; 3683eb4e52b3SPo Hao Huang 3684eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 3685eb4e52b3SPo Hao Huang return 0; 3686eb4e52b3SPo Hao Huang 3687eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 3688eb4e52b3SPo Hao Huang 3689eb4e52b3SPo Hao Huang return rtw89_phy_read32(rtwdev, addr); 3690eb4e52b3SPo Hao Huang } 3691eb4e52b3SPo Hao Huang 3692eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev, 3693eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page, 3694eb4e52b3SPo Hao Huang u32 val) 3695eb4e52b3SPo Hao Huang { 3696eb4e52b3SPo Hao Huang const struct rtw89_chip_info *chip = rtwdev->chip; 3697eb4e52b3SPo Hao Huang u32 addr; 3698eb4e52b3SPo Hao Huang 3699eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 3700eb4e52b3SPo Hao Huang return; 3701eb4e52b3SPo Hao Huang 3702eb4e52b3SPo Hao Huang if (chip->chip_id == RTL8852A) 3703eb4e52b3SPo Hao Huang val &= B_PHY_STS_BITMAP_MSK_52A; 3704eb4e52b3SPo Hao Huang 3705eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 3706eb4e52b3SPo Hao Huang rtw89_phy_write32(rtwdev, addr, val); 3707eb4e52b3SPo Hao Huang } 3708eb4e52b3SPo Hao Huang 3709eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev, 3710eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap bitmap, 3711eb4e52b3SPo Hao Huang enum rtw89_phy_status_ie_type ie, 3712eb4e52b3SPo Hao Huang bool enable) 3713eb4e52b3SPo Hao Huang { 3714eb4e52b3SPo Hao Huang u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap); 3715eb4e52b3SPo Hao Huang 3716eb4e52b3SPo Hao Huang if (enable) 3717eb4e52b3SPo Hao Huang val |= BIT(ie); 3718eb4e52b3SPo Hao Huang else 3719eb4e52b3SPo Hao Huang val &= ~BIT(ie); 3720eb4e52b3SPo Hao Huang 3721eb4e52b3SPo Hao Huang rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val); 3722eb4e52b3SPo Hao Huang } 3723eb4e52b3SPo Hao Huang 3724eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev, 3725eb4e52b3SPo Hao Huang bool enable, 3726eb4e52b3SPo Hao Huang enum rtw89_phy_idx phy_idx) 3727eb4e52b3SPo Hao Huang { 3728eb4e52b3SPo Hao Huang if (enable) { 3729eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 3730eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 3731eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 3732eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 3733eb4e52b3SPo Hao Huang } else { 3734eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 3735eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 3736eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 3737eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 3738eb4e52b3SPo Hao Huang } 3739eb4e52b3SPo Hao Huang } 3740eb4e52b3SPo Hao Huang 3741eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev) 3742eb4e52b3SPo Hao Huang { 3743eb4e52b3SPo Hao Huang u8 i; 3744eb4e52b3SPo Hao Huang 3745eb4e52b3SPo Hao Huang rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0); 3746eb4e52b3SPo Hao Huang 3747eb4e52b3SPo Hao Huang for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) { 3748eb4e52b3SPo Hao Huang if (i >= RTW89_CCK_PKT) 3749eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 3750eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE09_FTR_0, 3751eb4e52b3SPo Hao Huang true); 3752eb4e52b3SPo Hao Huang if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) || 3753eb4e52b3SPo Hao Huang (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT)) 3754eb4e52b3SPo Hao Huang continue; 3755eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 3756eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE24_OFDM_TD_PATH_A, 3757eb4e52b3SPo Hao Huang true); 3758eb4e52b3SPo Hao Huang } 3759eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT, 3760eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 3761eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT, 3762eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 3763eb4e52b3SPo Hao Huang 3764eb4e52b3SPo Hao Huang /* force IE01 for channel index, only channel field is valid */ 3765eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT, 3766eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE01_CMN_OFDM, true); 3767eb4e52b3SPo Hao Huang } 3768eb4e52b3SPo Hao Huang 3769e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type) 3770e3ec7017SPing-Ke Shih { 3771e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 3772e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3773e3ec7017SPing-Ke Shih const struct rtw89_phy_dig_gain_cfg *cfg; 3774e3ec7017SPing-Ke Shih const char *msg; 3775e3ec7017SPing-Ke Shih u8 i; 3776e3ec7017SPing-Ke Shih s8 gain_base; 3777e3ec7017SPing-Ke Shih s8 *gain_arr; 3778e3ec7017SPing-Ke Shih u32 tmp; 3779e3ec7017SPing-Ke Shih 3780e3ec7017SPing-Ke Shih switch (type) { 3781e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_G: 3782e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_g; 3783e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 3784e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_g; 3785e3ec7017SPing-Ke Shih msg = "lna_gain_g"; 3786e3ec7017SPing-Ke Shih break; 3787e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_G: 3788e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_g; 3789e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_G; 3790e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_g; 3791e3ec7017SPing-Ke Shih msg = "tia_gain_g"; 3792e3ec7017SPing-Ke Shih break; 3793e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_A: 3794e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_a; 3795e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 3796e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_a; 3797e3ec7017SPing-Ke Shih msg = "lna_gain_a"; 3798e3ec7017SPing-Ke Shih break; 3799e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_A: 3800e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_a; 3801e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_A; 3802e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_a; 3803e3ec7017SPing-Ke Shih msg = "tia_gain_a"; 3804e3ec7017SPing-Ke Shih break; 3805e3ec7017SPing-Ke Shih default: 3806e3ec7017SPing-Ke Shih return; 3807e3ec7017SPing-Ke Shih } 3808e3ec7017SPing-Ke Shih 3809e3ec7017SPing-Ke Shih for (i = 0; i < cfg->size; i++) { 3810e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr, 3811e3ec7017SPing-Ke Shih cfg->table[i].mask); 3812e3ec7017SPing-Ke Shih tmp >>= DIG_GAIN_SHIFT; 3813e3ec7017SPing-Ke Shih gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base; 3814e3ec7017SPing-Ke Shih gain_base += DIG_GAIN; 3815e3ec7017SPing-Ke Shih 3816e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n", 3817e3ec7017SPing-Ke Shih msg, i, gain_arr[i]); 3818e3ec7017SPing-Ke Shih } 3819e3ec7017SPing-Ke Shih } 3820e3ec7017SPing-Ke Shih 3821e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev) 3822e3ec7017SPing-Ke Shih { 3823e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3824e3ec7017SPing-Ke Shih u32 tmp; 3825e3ec7017SPing-Ke Shih u8 i; 3826e3ec7017SPing-Ke Shih 3827d264edb1SJohnson Lin if (!rtwdev->hal.support_igi) 3828d264edb1SJohnson Lin return; 3829d264edb1SJohnson Lin 3830e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW, 3831e3ec7017SPing-Ke Shih B_PATH0_IB_PKPW_MSK); 3832e3ec7017SPing-Ke Shih dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT); 3833e3ec7017SPing-Ke Shih dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK, 3834e3ec7017SPing-Ke Shih B_PATH0_IB_PBK_MSK); 3835e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n", 3836e3ec7017SPing-Ke Shih dig->ib_pkpwr, dig->ib_pbk); 3837e3ec7017SPing-Ke Shih 3838e3ec7017SPing-Ke Shih for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++) 3839e3ec7017SPing-Ke Shih rtw89_phy_dig_read_gain_table(rtwdev, i); 3840e3ec7017SPing-Ke Shih } 3841e3ec7017SPing-Ke Shih 3842e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22; 3843e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104}; 3844e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88}; 3845e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16}; 3846e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528}; 3847e3ec7017SPing-Ke Shih 3848e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev) 3849e3ec7017SPing-Ke Shih { 3850e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info; 3851e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3852e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3853e3ec7017SPing-Ke Shih 3854e3ec7017SPing-Ke Shih if (is_linked) { 3855e3ec7017SPing-Ke Shih dig->igi_rssi = ch_info->rssi_min >> 1; 3856e3ec7017SPing-Ke Shih } else { 3857e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n"); 3858e3ec7017SPing-Ke Shih dig->igi_rssi = rssi_nolink; 3859e3ec7017SPing-Ke Shih } 3860e3ec7017SPing-Ke Shih } 3861e3ec7017SPing-Ke Shih 3862e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev) 3863e3ec7017SPing-Ke Shih { 3864e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3865cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 3866e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3867e3ec7017SPing-Ke Shih const u16 *fa_th_src = NULL; 3868e3ec7017SPing-Ke Shih 3869cbb145b9SZong-Zhe Yang switch (chan->band_type) { 3870e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 3871e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_g; 3872e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_g; 3873e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_2g : fa_th_nolink; 3874e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = false; 3875e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 3876e3ec7017SPing-Ke Shih break; 3877e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 3878e3ec7017SPing-Ke Shih default: 3879e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_a; 3880e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_a; 3881e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_5g : fa_th_nolink; 3882e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = true; 3883e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 3884e3ec7017SPing-Ke Shih break; 3885e3ec7017SPing-Ke Shih } 3886e3ec7017SPing-Ke Shih memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th)); 3887e3ec7017SPing-Ke Shih memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th)); 3888e3ec7017SPing-Ke Shih } 3889e3ec7017SPing-Ke Shih 3890e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20; 3891e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a; 3892e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max; 3893e3ec7017SPing-Ke Shih 3894e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev) 3895e3ec7017SPing-Ke Shih { 3896e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3897e3ec7017SPing-Ke Shih 3898e3ec7017SPing-Ke Shih dig->cur_gaincode.lna_idx = LNA_IDX_MAX; 3899e3ec7017SPing-Ke Shih dig->cur_gaincode.tia_idx = TIA_IDX_MAX; 3900e3ec7017SPing-Ke Shih dig->cur_gaincode.rxb_idx = RXB_IDX_MAX; 3901e3ec7017SPing-Ke Shih dig->force_gaincode.lna_idx = LNA_IDX_MAX; 3902e3ec7017SPing-Ke Shih dig->force_gaincode.tia_idx = TIA_IDX_MAX; 3903e3ec7017SPing-Ke Shih dig->force_gaincode.rxb_idx = RXB_IDX_MAX; 3904e3ec7017SPing-Ke Shih 3905e3ec7017SPing-Ke Shih dig->dyn_igi_max = igi_max_performance_mode; 3906e3ec7017SPing-Ke Shih dig->dyn_igi_min = dynamic_igi_min; 3907e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dynamic_pd_threshold_max; 3908e3ec7017SPing-Ke Shih dig->pd_low_th_ofst = pd_low_th_offset; 3909e3ec7017SPing-Ke Shih dig->is_linked_pre = false; 3910e3ec7017SPing-Ke Shih } 3911e3ec7017SPing-Ke Shih 3912e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev) 3913e3ec7017SPing-Ke Shih { 3914e3ec7017SPing-Ke Shih rtw89_phy_dig_update_gain_para(rtwdev); 3915e3ec7017SPing-Ke Shih rtw89_phy_dig_reset(rtwdev); 3916e3ec7017SPing-Ke Shih } 3917e3ec7017SPing-Ke Shih 3918e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 3919e3ec7017SPing-Ke Shih { 3920e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3921e3ec7017SPing-Ke Shih u8 lna_idx; 3922e3ec7017SPing-Ke Shih 3923e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 3924e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX6; 3925e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[1]) 3926e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX5; 3927e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[2]) 3928e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX4; 3929e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[3]) 3930e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX3; 3931e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[4]) 3932e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX2; 3933e3ec7017SPing-Ke Shih else 3934e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX1; 3935e3ec7017SPing-Ke Shih 3936e3ec7017SPing-Ke Shih return lna_idx; 3937e3ec7017SPing-Ke Shih } 3938e3ec7017SPing-Ke Shih 3939e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 3940e3ec7017SPing-Ke Shih { 3941e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3942e3ec7017SPing-Ke Shih u8 tia_idx; 3943e3ec7017SPing-Ke Shih 3944e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 3945e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX1; 3946e3ec7017SPing-Ke Shih else 3947e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX0; 3948e3ec7017SPing-Ke Shih 3949e3ec7017SPing-Ke Shih return tia_idx; 3950e3ec7017SPing-Ke Shih } 3951e3ec7017SPing-Ke Shih 3952e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110 3953e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10 3954e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 3955e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 3956e3ec7017SPing-Ke Shih { 3957e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3958e3ec7017SPing-Ke Shih s8 lna_gain = dig->lna_gain[set->lna_idx]; 3959e3ec7017SPing-Ke Shih s8 tia_gain = dig->tia_gain[set->tia_idx]; 3960e3ec7017SPing-Ke Shih s32 wb_rssi = rssi + lna_gain + tia_gain; 3961e3ec7017SPing-Ke Shih s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE; 3962e3ec7017SPing-Ke Shih u8 rxb_idx; 3963e3ec7017SPing-Ke Shih 3964e3ec7017SPing-Ke Shih rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi; 3965e3ec7017SPing-Ke Shih rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX); 3966e3ec7017SPing-Ke Shih 3967e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n", 3968e3ec7017SPing-Ke Shih wb_rssi, rxb_idx_tmp); 3969e3ec7017SPing-Ke Shih 3970e3ec7017SPing-Ke Shih return rxb_idx; 3971e3ec7017SPing-Ke Shih } 3972e3ec7017SPing-Ke Shih 3973e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 3974e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 3975e3ec7017SPing-Ke Shih { 3976e3ec7017SPing-Ke Shih set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi); 3977e3ec7017SPing-Ke Shih set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi); 3978e3ec7017SPing-Ke Shih set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set); 3979e3ec7017SPing-Ke Shih 3980e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3981e3ec7017SPing-Ke Shih "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n", 3982e3ec7017SPing-Ke Shih rssi, set->lna_idx, set->tia_idx, set->rxb_idx); 3983e3ec7017SPing-Ke Shih } 3984e3ec7017SPing-Ke Shih 3985e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25 3986e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2 3987e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev) 3988e3ec7017SPing-Ke Shih { 3989e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3990e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 3991e3ec7017SPing-Ke Shih enum rtw89_dig_noisy_level noisy_lv; 3992e3ec7017SPing-Ke Shih u8 igi_offset = dig->fa_rssi_ofst; 3993e3ec7017SPing-Ke Shih u16 fa_ratio = 0; 3994e3ec7017SPing-Ke Shih 3995e3ec7017SPing-Ke Shih fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil; 3996e3ec7017SPing-Ke Shih 3997e3ec7017SPing-Ke Shih if (fa_ratio < dig->fa_th[0]) 3998e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL0; 3999e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[1]) 4000e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL1; 4001e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[2]) 4002e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL2; 4003e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[3]) 4004e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL3; 4005e3ec7017SPing-Ke Shih else 4006e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX; 4007e3ec7017SPing-Ke Shih 4008e3ec7017SPing-Ke Shih if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2) 4009e3ec7017SPing-Ke Shih igi_offset = 0; 4010e3ec7017SPing-Ke Shih else 4011e3ec7017SPing-Ke Shih igi_offset += noisy_lv * IGI_OFFSET_MUL; 4012e3ec7017SPing-Ke Shih 4013e3ec7017SPing-Ke Shih igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX); 4014e3ec7017SPing-Ke Shih dig->fa_rssi_ofst = igi_offset; 4015e3ec7017SPing-Ke Shih 4016e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 4017e3ec7017SPing-Ke Shih "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n", 4018e3ec7017SPing-Ke Shih dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]); 4019e3ec7017SPing-Ke Shih 4020e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 4021e3ec7017SPing-Ke Shih "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n", 4022e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil, 4023e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil, 4024e3ec7017SPing-Ke Shih noisy_lv, igi_offset); 4025e3ec7017SPing-Ke Shih } 4026e3ec7017SPing-Ke Shih 4027e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx) 4028e3ec7017SPing-Ke Shih { 402987deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 403087deaad9SEric Huang 403187deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr, 403287deaad9SEric Huang dig_regs->p0_lna_init.mask, lna_idx); 403387deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr, 403487deaad9SEric Huang dig_regs->p1_lna_init.mask, lna_idx); 4035e3ec7017SPing-Ke Shih } 4036e3ec7017SPing-Ke Shih 4037e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx) 4038e3ec7017SPing-Ke Shih { 403987deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 404087deaad9SEric Huang 404187deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr, 404287deaad9SEric Huang dig_regs->p0_tia_init.mask, tia_idx); 404387deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr, 404487deaad9SEric Huang dig_regs->p1_tia_init.mask, tia_idx); 4045e3ec7017SPing-Ke Shih } 4046e3ec7017SPing-Ke Shih 4047e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx) 4048e3ec7017SPing-Ke Shih { 404987deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 405087deaad9SEric Huang 405187deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr, 405287deaad9SEric Huang dig_regs->p0_rxb_init.mask, rxb_idx); 405387deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr, 405487deaad9SEric Huang dig_regs->p1_rxb_init.mask, rxb_idx); 4055e3ec7017SPing-Ke Shih } 4056e3ec7017SPing-Ke Shih 4057e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev, 4058e3ec7017SPing-Ke Shih const struct rtw89_agc_gaincode_set set) 4059e3ec7017SPing-Ke Shih { 4060e3ec7017SPing-Ke Shih rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx); 4061e3ec7017SPing-Ke Shih rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx); 4062e3ec7017SPing-Ke Shih rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx); 4063e3ec7017SPing-Ke Shih 4064e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n", 4065e3ec7017SPing-Ke Shih set.lna_idx, set.tia_idx, set.rxb_idx); 4066e3ec7017SPing-Ke Shih } 4067e3ec7017SPing-Ke Shih 4068e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev, 4069e3ec7017SPing-Ke Shih bool enable) 4070e3ec7017SPing-Ke Shih { 407187deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 4072e3ec7017SPing-Ke Shih 407387deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr, 407487deaad9SEric Huang dig_regs->p0_p20_pagcugc_en.mask, enable); 407587deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr, 407687deaad9SEric Huang dig_regs->p0_s20_pagcugc_en.mask, enable); 407787deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr, 407887deaad9SEric Huang dig_regs->p1_p20_pagcugc_en.mask, enable); 407987deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr, 408087deaad9SEric Huang dig_regs->p1_s20_pagcugc_en.mask, enable); 4081e3ec7017SPing-Ke Shih 4082e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable); 4083e3ec7017SPing-Ke Shih } 4084e3ec7017SPing-Ke Shih 4085bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev) 4086bed4045fSJohnson Lin { 4087bed4045fSJohnson Lin struct rtw89_dig_info *dig = &rtwdev->dig; 4088bed4045fSJohnson Lin 4089d264edb1SJohnson Lin if (!rtwdev->hal.support_igi) 4090d264edb1SJohnson Lin return; 4091d264edb1SJohnson Lin 4092bed4045fSJohnson Lin if (dig->force_gaincode_idx_en) { 4093bed4045fSJohnson Lin rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 4094bed4045fSJohnson Lin rtw89_debug(rtwdev, RTW89_DBG_DIG, 4095bed4045fSJohnson Lin "Force gaincode index enabled.\n"); 4096bed4045fSJohnson Lin } else { 4097bed4045fSJohnson Lin rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi, 4098bed4045fSJohnson Lin &dig->cur_gaincode); 4099bed4045fSJohnson Lin rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode); 4100bed4045fSJohnson Lin } 4101bed4045fSJohnson Lin } 4102bed4045fSJohnson Lin 4103e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi, 4104e3ec7017SPing-Ke Shih bool enable) 4105e3ec7017SPing-Ke Shih { 4106cbb145b9SZong-Zhe Yang const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 410787deaad9SEric Huang const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 4108cbb145b9SZong-Zhe Yang enum rtw89_bandwidth cbw = chan->band_width; 4109e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 4110e3ec7017SPing-Ke Shih u8 final_rssi = 0, under_region = dig->pd_low_th_ofst; 41111c2423deSJohnson Lin u8 ofdm_cca_th; 41121c2423deSJohnson Lin s8 cck_cca_th; 41131c2423deSJohnson Lin u32 pd_val = 0; 4114e3ec7017SPing-Ke Shih 4115e3ec7017SPing-Ke Shih under_region += PD_TH_SB_FLTR_CMP_VAL; 4116e3ec7017SPing-Ke Shih 4117e3ec7017SPing-Ke Shih switch (cbw) { 4118e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 4119e3ec7017SPing-Ke Shih under_region += PD_TH_BW40_CMP_VAL; 4120e3ec7017SPing-Ke Shih break; 4121e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 4122e3ec7017SPing-Ke Shih under_region += PD_TH_BW80_CMP_VAL; 4123e3ec7017SPing-Ke Shih break; 41241c2423deSJohnson Lin case RTW89_CHANNEL_WIDTH_160: 41251c2423deSJohnson Lin under_region += PD_TH_BW160_CMP_VAL; 41261c2423deSJohnson Lin break; 4127e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 4128e3ec7017SPing-Ke Shih fallthrough; 4129e3ec7017SPing-Ke Shih default: 4130e3ec7017SPing-Ke Shih under_region += PD_TH_BW20_CMP_VAL; 4131e3ec7017SPing-Ke Shih break; 4132e3ec7017SPing-Ke Shih } 4133e3ec7017SPing-Ke Shih 4134e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dig->igi_rssi; 4135e3ec7017SPing-Ke Shih 4136e3ec7017SPing-Ke Shih final_rssi = min_t(u8, rssi, dig->igi_rssi); 41371c2423deSJohnson Lin ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region, 4138e3ec7017SPing-Ke Shih PD_TH_MAX_RSSI + under_region); 4139e3ec7017SPing-Ke Shih 4140e3ec7017SPing-Ke Shih if (enable) { 41411c2423deSJohnson Lin pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1; 4142e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 41431c2423deSJohnson Lin "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n", 41441c2423deSJohnson Lin final_rssi, ofdm_cca_th, under_region, pd_val); 4145e3ec7017SPing-Ke Shih } else { 4146e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 4147c51ed740SColin Ian King "Dynamic PD th disabled, Set PD_low_bd=0\n"); 4148e3ec7017SPing-Ke Shih } 4149e3ec7017SPing-Ke Shih 415087deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 415187deaad9SEric Huang dig_regs->pd_lower_bound_mask, pd_val); 415287deaad9SEric Huang rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 415387deaad9SEric Huang dig_regs->pd_spatial_reuse_en, enable); 41541c2423deSJohnson Lin 41551c2423deSJohnson Lin if (!rtwdev->hal.support_cckpd) 41561c2423deSJohnson Lin return; 41571c2423deSJohnson Lin 41581c2423deSJohnson Lin cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI); 41591c2423deSJohnson Lin pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX); 41601c2423deSJohnson Lin 41611c2423deSJohnson Lin rtw89_debug(rtwdev, RTW89_DBG_DIG, 41621c2423deSJohnson Lin "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n", 41631c2423deSJohnson Lin final_rssi, cck_cca_th, under_region, pd_val); 41641c2423deSJohnson Lin 41651c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1, 41661c2423deSJohnson Lin B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable); 41671c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1, 41681c2423deSJohnson Lin B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val); 4169e3ec7017SPing-Ke Shih } 4170e3ec7017SPing-Ke Shih 4171e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev) 4172e3ec7017SPing-Ke Shih { 4173e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 4174e3ec7017SPing-Ke Shih 4175e3ec7017SPing-Ke Shih dig->bypass_dig = false; 4176e3ec7017SPing-Ke Shih rtw89_phy_dig_para_reset(rtwdev); 4177e3ec7017SPing-Ke Shih rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 4178e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false); 4179e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 4180e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 4181e3ec7017SPing-Ke Shih } 4182e3ec7017SPing-Ke Shih 4183e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10 4184e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev) 4185e3ec7017SPing-Ke Shih { 4186e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 4187e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 4188e3ec7017SPing-Ke Shih 4189e3ec7017SPing-Ke Shih if (unlikely(dig->bypass_dig)) { 4190e3ec7017SPing-Ke Shih dig->bypass_dig = false; 4191e3ec7017SPing-Ke Shih return; 4192e3ec7017SPing-Ke Shih } 4193e3ec7017SPing-Ke Shih 4194e3ec7017SPing-Ke Shih if (!dig->is_linked_pre && is_linked) { 4195e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n"); 4196e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 4197e3ec7017SPing-Ke Shih } else if (dig->is_linked_pre && !is_linked) { 4198e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n"); 4199e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 4200e3ec7017SPing-Ke Shih } 4201e3ec7017SPing-Ke Shih dig->is_linked_pre = is_linked; 4202e3ec7017SPing-Ke Shih 4203e3ec7017SPing-Ke Shih rtw89_phy_dig_igi_offset_by_env(rtwdev); 4204e3ec7017SPing-Ke Shih rtw89_phy_dig_update_rssi_info(rtwdev); 4205e3ec7017SPing-Ke Shih 4206e3ec7017SPing-Ke Shih dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ? 4207e3ec7017SPing-Ke Shih dig->igi_rssi - IGI_RSSI_MIN : 0; 4208e3ec7017SPing-Ke Shih dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX; 4209e3ec7017SPing-Ke Shih dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst; 4210e3ec7017SPing-Ke Shih 4211e3ec7017SPing-Ke Shih dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min, 4212e3ec7017SPing-Ke Shih dig->dyn_igi_max); 4213e3ec7017SPing-Ke Shih 4214e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 4215e3ec7017SPing-Ke Shih "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n", 4216e3ec7017SPing-Ke Shih dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min, 4217e3ec7017SPing-Ke Shih dig->igi_fa_rssi); 4218e3ec7017SPing-Ke Shih 4219bed4045fSJohnson Lin rtw89_phy_dig_config_igi(rtwdev); 4220e3ec7017SPing-Ke Shih 4221e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en); 4222e3ec7017SPing-Ke Shih 4223e3ec7017SPing-Ke Shih if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max) 4224e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true); 4225e3ec7017SPing-Ke Shih else 4226e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 4227e3ec7017SPing-Ke Shih } 4228e3ec7017SPing-Ke Shih 42297dbdf655SPing-Ke Shih static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta) 42307dbdf655SPing-Ke Shih { 42317dbdf655SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 42327dbdf655SPing-Ke Shih struct rtw89_dev *rtwdev = rtwsta->rtwdev; 42337dbdf655SPing-Ke Shih struct rtw89_vif *rtwvif = rtwsta->rtwvif; 42347dbdf655SPing-Ke Shih struct rtw89_hal *hal = &rtwdev->hal; 42357dbdf655SPing-Ke Shih bool *done = data; 42367dbdf655SPing-Ke Shih u8 rssi_a, rssi_b; 42377dbdf655SPing-Ke Shih u32 candidate; 42387dbdf655SPing-Ke Shih 42397dbdf655SPing-Ke Shih if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls) 42407dbdf655SPing-Ke Shih return; 42417dbdf655SPing-Ke Shih 42427dbdf655SPing-Ke Shih if (*done) 42437dbdf655SPing-Ke Shih return; 42447dbdf655SPing-Ke Shih 42457dbdf655SPing-Ke Shih *done = true; 42467dbdf655SPing-Ke Shih 42477dbdf655SPing-Ke Shih rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]); 42487dbdf655SPing-Ke Shih rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]); 42497dbdf655SPing-Ke Shih 42507dbdf655SPing-Ke Shih if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH) 42517dbdf655SPing-Ke Shih candidate = RF_A; 42527dbdf655SPing-Ke Shih else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH) 42537dbdf655SPing-Ke Shih candidate = RF_B; 42547dbdf655SPing-Ke Shih else 42557dbdf655SPing-Ke Shih return; 42567dbdf655SPing-Ke Shih 42577dbdf655SPing-Ke Shih if (hal->antenna_tx == candidate) 42587dbdf655SPing-Ke Shih return; 42597dbdf655SPing-Ke Shih 42607dbdf655SPing-Ke Shih hal->antenna_tx = candidate; 42617dbdf655SPing-Ke Shih rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta); 42627dbdf655SPing-Ke Shih 42637dbdf655SPing-Ke Shih if (hal->antenna_tx == RF_A) { 42647dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12); 42657dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11); 42667dbdf655SPing-Ke Shih } else if (hal->antenna_tx == RF_B) { 42677dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11); 42687dbdf655SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12); 42697dbdf655SPing-Ke Shih } 42707dbdf655SPing-Ke Shih } 42717dbdf655SPing-Ke Shih 42727dbdf655SPing-Ke Shih void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev) 42737dbdf655SPing-Ke Shih { 42747dbdf655SPing-Ke Shih struct rtw89_hal *hal = &rtwdev->hal; 42757dbdf655SPing-Ke Shih bool done = false; 42767dbdf655SPing-Ke Shih 42777dbdf655SPing-Ke Shih if (!hal->tx_path_diversity) 42787dbdf655SPing-Ke Shih return; 42797dbdf655SPing-Ke Shih 42807dbdf655SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 42817dbdf655SPing-Ke Shih rtw89_phy_tx_path_div_sta_iter, 42827dbdf655SPing-Ke Shih &done); 42837dbdf655SPing-Ke Shih } 42847dbdf655SPing-Ke Shih 4285a90c613dSEric Huang #define ANTDIV_MAIN 0 4286a90c613dSEric Huang #define ANTDIV_AUX 1 4287a90c613dSEric Huang 4288a90c613dSEric Huang static void rtw89_phy_antdiv_set_ant(struct rtw89_dev *rtwdev) 4289a90c613dSEric Huang { 4290a90c613dSEric Huang struct rtw89_hal *hal = &rtwdev->hal; 4291a90c613dSEric Huang u8 default_ant, optional_ant; 4292a90c613dSEric Huang 4293a90c613dSEric Huang if (!hal->ant_diversity || hal->antenna_tx == 0) 4294a90c613dSEric Huang return; 4295a90c613dSEric Huang 4296a90c613dSEric Huang if (hal->antenna_tx == RF_B) { 4297a90c613dSEric Huang default_ant = ANTDIV_AUX; 4298a90c613dSEric Huang optional_ant = ANTDIV_MAIN; 4299a90c613dSEric Huang } else { 4300a90c613dSEric Huang default_ant = ANTDIV_MAIN; 4301a90c613dSEric Huang optional_ant = ANTDIV_AUX; 4302a90c613dSEric Huang } 4303a90c613dSEric Huang 4304a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_CGCS_CTRL, 4305a90c613dSEric Huang default_ant, RTW89_PHY_0); 4306a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ORI, 4307a90c613dSEric Huang default_ant, RTW89_PHY_0); 4308a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ALT, 4309a90c613dSEric Huang optional_ant, RTW89_PHY_0); 4310a90c613dSEric Huang rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_TX_ORI, 4311a90c613dSEric Huang default_ant, RTW89_PHY_0); 4312a90c613dSEric Huang } 4313a90c613dSEric Huang 4314e3715859SEric Huang static void rtw89_phy_swap_hal_antenna(struct rtw89_dev *rtwdev) 4315e3715859SEric Huang { 4316e3715859SEric Huang struct rtw89_hal *hal = &rtwdev->hal; 4317e3715859SEric Huang 4318e3715859SEric Huang hal->antenna_rx = hal->antenna_rx == RF_A ? RF_B : RF_A; 4319e3715859SEric Huang hal->antenna_tx = hal->antenna_rx; 4320e3715859SEric Huang } 4321e3715859SEric Huang 4322e3715859SEric Huang static void rtw89_phy_antdiv_decision_state(struct rtw89_dev *rtwdev) 4323e3715859SEric Huang { 4324e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4325e3715859SEric Huang struct rtw89_hal *hal = &rtwdev->hal; 4326e3715859SEric Huang bool no_change = false; 4327e3715859SEric Huang u8 main_rssi, aux_rssi; 43285feecb40SEric Huang u8 main_evm, aux_evm; 4329e3715859SEric Huang u32 candidate; 4330e3715859SEric Huang 4331e3715859SEric Huang antdiv->get_stats = false; 4332e3715859SEric Huang antdiv->training_count = 0; 4333e3715859SEric Huang 4334e3715859SEric Huang main_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->main_stats); 43355feecb40SEric Huang main_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->main_stats); 4336e3715859SEric Huang aux_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->aux_stats); 43375feecb40SEric Huang aux_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->aux_stats); 4338e3715859SEric Huang 43395feecb40SEric Huang if (main_evm > aux_evm + ANTDIV_EVM_DIFF_TH) 43405feecb40SEric Huang candidate = RF_A; 43415feecb40SEric Huang else if (aux_evm > main_evm + ANTDIV_EVM_DIFF_TH) 43425feecb40SEric Huang candidate = RF_B; 43435feecb40SEric Huang else if (main_rssi > aux_rssi + RTW89_TX_DIV_RSSI_RAW_TH) 4344e3715859SEric Huang candidate = RF_A; 4345e3715859SEric Huang else if (aux_rssi > main_rssi + RTW89_TX_DIV_RSSI_RAW_TH) 4346e3715859SEric Huang candidate = RF_B; 4347e3715859SEric Huang else 4348e3715859SEric Huang no_change = true; 4349e3715859SEric Huang 4350e3715859SEric Huang if (no_change) { 4351e3715859SEric Huang /* swap back from training antenna to original */ 4352e3715859SEric Huang rtw89_phy_swap_hal_antenna(rtwdev); 4353e3715859SEric Huang return; 4354e3715859SEric Huang } 4355e3715859SEric Huang 4356e3715859SEric Huang hal->antenna_tx = candidate; 4357e3715859SEric Huang hal->antenna_rx = candidate; 4358e3715859SEric Huang } 4359e3715859SEric Huang 4360e3715859SEric Huang static void rtw89_phy_antdiv_training_state(struct rtw89_dev *rtwdev) 4361e3715859SEric Huang { 4362e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4363e3715859SEric Huang u64 state_period; 4364e3715859SEric Huang 4365e3715859SEric Huang if (antdiv->training_count % 2 == 0) { 4366e3715859SEric Huang if (antdiv->training_count == 0) 4367e3715859SEric Huang rtw89_phy_antdiv_sts_reset(rtwdev); 4368e3715859SEric Huang 4369e3715859SEric Huang antdiv->get_stats = true; 4370e3715859SEric Huang state_period = msecs_to_jiffies(ANTDIV_TRAINNING_INTVL); 4371e3715859SEric Huang } else { 4372e3715859SEric Huang antdiv->get_stats = false; 4373e3715859SEric Huang state_period = msecs_to_jiffies(ANTDIV_DELAY); 4374e3715859SEric Huang 4375e3715859SEric Huang rtw89_phy_swap_hal_antenna(rtwdev); 4376e3715859SEric Huang rtw89_phy_antdiv_set_ant(rtwdev); 4377e3715859SEric Huang } 4378e3715859SEric Huang 4379e3715859SEric Huang antdiv->training_count++; 4380e3715859SEric Huang ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 4381e3715859SEric Huang state_period); 4382e3715859SEric Huang } 4383e3715859SEric Huang 4384e3715859SEric Huang void rtw89_phy_antdiv_work(struct work_struct *work) 4385e3715859SEric Huang { 4386e3715859SEric Huang struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 4387e3715859SEric Huang antdiv_work.work); 4388e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4389e3715859SEric Huang 4390e3715859SEric Huang mutex_lock(&rtwdev->mutex); 4391e3715859SEric Huang 4392e3715859SEric Huang if (antdiv->training_count <= ANTDIV_TRAINNING_CNT) { 4393e3715859SEric Huang rtw89_phy_antdiv_training_state(rtwdev); 4394e3715859SEric Huang } else { 4395e3715859SEric Huang rtw89_phy_antdiv_decision_state(rtwdev); 4396e3715859SEric Huang rtw89_phy_antdiv_set_ant(rtwdev); 4397e3715859SEric Huang } 4398e3715859SEric Huang 4399e3715859SEric Huang mutex_unlock(&rtwdev->mutex); 4400e3715859SEric Huang } 4401e3715859SEric Huang 4402e3715859SEric Huang void rtw89_phy_antdiv_track(struct rtw89_dev *rtwdev) 4403e3715859SEric Huang { 4404e3715859SEric Huang struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4405e3715859SEric Huang struct rtw89_hal *hal = &rtwdev->hal; 4406e3715859SEric Huang u8 rssi, rssi_pre; 4407e3715859SEric Huang 4408e3715859SEric Huang if (!hal->ant_diversity || hal->ant_diversity_fixed) 4409e3715859SEric Huang return; 4410e3715859SEric Huang 4411e3715859SEric Huang rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->target_stats); 4412e3715859SEric Huang rssi_pre = antdiv->rssi_pre; 4413e3715859SEric Huang antdiv->rssi_pre = rssi; 4414e3715859SEric Huang rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats); 4415e3715859SEric Huang 4416e3715859SEric Huang if (abs((int)rssi - (int)rssi_pre) < ANTDIV_RSSI_DIFF_TH) 4417e3715859SEric Huang return; 4418e3715859SEric Huang 4419e3715859SEric Huang antdiv->training_count = 0; 4420e3715859SEric Huang ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 0); 4421e3715859SEric Huang } 4422e3715859SEric Huang 4423e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev) 4424e3ec7017SPing-Ke Shih { 4425e3ec7017SPing-Ke Shih rtw89_phy_ccx_top_setting_init(rtwdev); 4426e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_setting_init(rtwdev); 4427e3ec7017SPing-Ke Shih } 4428e3ec7017SPing-Ke Shih 4429e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev) 4430e3ec7017SPing-Ke Shih { 4431e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 4432e3ec7017SPing-Ke Shih 4433e3ec7017SPing-Ke Shih rtw89_phy_stat_init(rtwdev); 4434e3ec7017SPing-Ke Shih 4435e3ec7017SPing-Ke Shih rtw89_chip_bb_sethw(rtwdev); 4436e3ec7017SPing-Ke Shih 4437e3ec7017SPing-Ke Shih rtw89_phy_env_monitor_init(rtwdev); 4438eb4e52b3SPo Hao Huang rtw89_physts_parsing_init(rtwdev); 4439e3ec7017SPing-Ke Shih rtw89_phy_dig_init(rtwdev); 4440e3ec7017SPing-Ke Shih rtw89_phy_cfo_init(rtwdev); 444129136c95SEric Huang rtw89_phy_ul_tb_info_init(rtwdev); 4442a90c613dSEric Huang rtw89_phy_antdiv_init(rtwdev); 4443f03bd042SPing-Ke Shih rtw89_chip_rfe_gpio(rtwdev); 4444a90c613dSEric Huang rtw89_phy_antdiv_set_ant(rtwdev); 4445e3ec7017SPing-Ke Shih 4446e3ec7017SPing-Ke Shih rtw89_phy_init_rf_nctl(rtwdev); 4447e3ec7017SPing-Ke Shih rtw89_chip_rfk_init(rtwdev); 4448e3ec7017SPing-Ke Shih rtw89_load_txpwr_table(rtwdev, chip->byr_table); 4449e3ec7017SPing-Ke Shih rtw89_chip_set_txpwr_ctrl(rtwdev); 4450e3ec7017SPing-Ke Shih rtw89_chip_power_trim(rtwdev); 4451cd89a471SPing-Ke Shih rtw89_chip_cfg_txrx_path(rtwdev); 4452e3ec7017SPing-Ke Shih } 4453e3ec7017SPing-Ke Shih 4454e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 4455e3ec7017SPing-Ke Shih { 4456a48f4fd0SEric Huang const struct rtw89_chip_info *chip = rtwdev->chip; 4457e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx = RTW89_PHY_0; 4458e3ec7017SPing-Ke Shih u8 bss_color; 4459e3ec7017SPing-Ke Shih 4460f276e20bSJohannes Berg if (!vif->bss_conf.he_support || !vif->cfg.assoc) 4461e3ec7017SPing-Ke Shih return; 4462e3ec7017SPing-Ke Shih 4463e3ec7017SPing-Ke Shih bss_color = vif->bss_conf.he_bss_color.color; 4464e3ec7017SPing-Ke Shih 4465a48f4fd0SEric Huang rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_VLD0, 0x1, 4466e3ec7017SPing-Ke Shih phy_idx); 4467a48f4fd0SEric Huang rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT, 4468a48f4fd0SEric Huang bss_color, phy_idx); 4469a48f4fd0SEric Huang rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID, 4470f276e20bSJohannes Berg vif->cfg.aid, phy_idx); 4471e3ec7017SPing-Ke Shih } 4472db7fa61aSZong-Zhe Yang 4473db7fa61aSZong-Zhe Yang static void 4474db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4475db7fa61aSZong-Zhe Yang { 4476db7fa61aSZong-Zhe Yang rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data); 4477db7fa61aSZong-Zhe Yang } 4478db7fa61aSZong-Zhe Yang 4479db7fa61aSZong-Zhe Yang static void 4480db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4481db7fa61aSZong-Zhe Yang { 4482db7fa61aSZong-Zhe Yang rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data); 4483db7fa61aSZong-Zhe Yang } 4484db7fa61aSZong-Zhe Yang 4485db7fa61aSZong-Zhe Yang static void 4486db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4487db7fa61aSZong-Zhe Yang { 4488db7fa61aSZong-Zhe Yang rtw89_phy_write32_set(rtwdev, def->addr, def->mask); 4489db7fa61aSZong-Zhe Yang } 4490db7fa61aSZong-Zhe Yang 4491db7fa61aSZong-Zhe Yang static void 4492db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4493db7fa61aSZong-Zhe Yang { 4494db7fa61aSZong-Zhe Yang rtw89_phy_write32_clr(rtwdev, def->addr, def->mask); 4495db7fa61aSZong-Zhe Yang } 4496db7fa61aSZong-Zhe Yang 4497db7fa61aSZong-Zhe Yang static void 4498db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 4499db7fa61aSZong-Zhe Yang { 4500db7fa61aSZong-Zhe Yang udelay(def->data); 4501db7fa61aSZong-Zhe Yang } 4502db7fa61aSZong-Zhe Yang 4503db7fa61aSZong-Zhe Yang static void 4504db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = { 4505db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WRF] = _rfk_write_rf, 4506db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WM] = _rfk_write32_mask, 4507db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WS] = _rfk_write32_set, 4508db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WC] = _rfk_write32_clr, 4509db7fa61aSZong-Zhe Yang [RTW89_RFK_F_DELAY] = _rfk_delay, 4510db7fa61aSZong-Zhe Yang }; 4511db7fa61aSZong-Zhe Yang 4512db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM); 4513db7fa61aSZong-Zhe Yang 4514db7fa61aSZong-Zhe Yang void 4515db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl) 4516db7fa61aSZong-Zhe Yang { 4517db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *p = tbl->defs; 4518db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *end = tbl->defs + tbl->size; 4519db7fa61aSZong-Zhe Yang 4520db7fa61aSZong-Zhe Yang for (; p < end; p++) 4521db7fa61aSZong-Zhe Yang _rfk_handler[p->flag](rtwdev, p); 4522db7fa61aSZong-Zhe Yang } 4523db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser); 4524c7845551SPing-Ke Shih 4525c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4 4526c7845551SPing-Ke Shih 4527c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = { 4528c7845551SPing-Ke Shih {0xD934, 0xff0000}, 4529c7845551SPing-Ke Shih {0xD934, 0xff000000}, 4530c7845551SPing-Ke Shih {0xD938, 0xff}, 4531c7845551SPing-Ke Shih {0xD934, 0xff00}, 4532c7845551SPing-Ke Shih }; 4533c7845551SPing-Ke Shih 4534c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = { 4535c7845551SPing-Ke Shih {0xD930, 0xff0000}, 4536c7845551SPing-Ke Shih {0xD930, 0xff000000}, 4537c7845551SPing-Ke Shih {0xD934, 0xff}, 4538c7845551SPing-Ke Shih {0xD930, 0xff00}, 4539c7845551SPing-Ke Shih }; 4540c7845551SPing-Ke Shih 4541c7845551SPing-Ke Shih static 4542c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev, 4543c7845551SPing-Ke Shih enum rtw89_mac_idx mac_idx, 4544c7845551SPing-Ke Shih enum rtw89_tssi_bandedge_cfg bandedge_cfg, 4545c7845551SPing-Ke Shih u32 val) 4546c7845551SPing-Ke Shih { 4547c7845551SPing-Ke Shih const struct rtw89_reg_def *regs; 4548c7845551SPing-Ke Shih u32 reg; 4549c7845551SPing-Ke Shih int i; 4550c7845551SPing-Ke Shih 4551c7845551SPing-Ke Shih if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 4552c7845551SPing-Ke Shih regs = rtw89_tssi_fastmode_regs_flat; 4553c7845551SPing-Ke Shih else 4554c7845551SPing-Ke Shih regs = rtw89_tssi_fastmode_regs_level; 4555c7845551SPing-Ke Shih 4556c7845551SPing-Ke Shih for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) { 4557c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx); 4558c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, regs[i].mask, val); 4559c7845551SPing-Ke Shih } 4560c7845551SPing-Ke Shih } 4561c7845551SPing-Ke Shih 4562c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = { 4563c7845551SPing-Ke Shih {0xD91C, 0xff000000}, 4564c7845551SPing-Ke Shih {0xD920, 0xff}, 4565c7845551SPing-Ke Shih {0xD920, 0xff00}, 4566c7845551SPing-Ke Shih {0xD920, 0xff0000}, 4567c7845551SPing-Ke Shih {0xD920, 0xff000000}, 4568c7845551SPing-Ke Shih {0xD924, 0xff}, 4569c7845551SPing-Ke Shih {0xD924, 0xff00}, 4570c7845551SPing-Ke Shih {0xD914, 0xff000000}, 4571c7845551SPing-Ke Shih {0xD918, 0xff}, 4572c7845551SPing-Ke Shih {0xD918, 0xff00}, 4573c7845551SPing-Ke Shih {0xD918, 0xff0000}, 4574c7845551SPing-Ke Shih {0xD918, 0xff000000}, 4575c7845551SPing-Ke Shih {0xD91C, 0xff}, 4576c7845551SPing-Ke Shih {0xD91C, 0xff00}, 4577c7845551SPing-Ke Shih {0xD91C, 0xff0000}, 4578c7845551SPing-Ke Shih }; 4579c7845551SPing-Ke Shih 4580c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = { 4581c7845551SPing-Ke Shih {0xD910, 0xff}, 4582c7845551SPing-Ke Shih {0xD910, 0xff00}, 4583c7845551SPing-Ke Shih {0xD910, 0xff0000}, 4584c7845551SPing-Ke Shih {0xD910, 0xff000000}, 4585c7845551SPing-Ke Shih {0xD914, 0xff}, 4586c7845551SPing-Ke Shih {0xD914, 0xff00}, 4587c7845551SPing-Ke Shih {0xD914, 0xff0000}, 4588c7845551SPing-Ke Shih {0xD908, 0xff}, 4589c7845551SPing-Ke Shih {0xD908, 0xff00}, 4590c7845551SPing-Ke Shih {0xD908, 0xff0000}, 4591c7845551SPing-Ke Shih {0xD908, 0xff000000}, 4592c7845551SPing-Ke Shih {0xD90C, 0xff}, 4593c7845551SPing-Ke Shih {0xD90C, 0xff00}, 4594c7845551SPing-Ke Shih {0xD90C, 0xff0000}, 4595c7845551SPing-Ke Shih {0xD90C, 0xff000000}, 4596c7845551SPing-Ke Shih }; 4597c7845551SPing-Ke Shih 4598c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev, 4599c7845551SPing-Ke Shih enum rtw89_mac_idx mac_idx, 4600c7845551SPing-Ke Shih enum rtw89_tssi_bandedge_cfg bandedge_cfg) 4601c7845551SPing-Ke Shih { 4602c7845551SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 4603c7845551SPing-Ke Shih const struct rtw89_reg_def *regs; 4604c7845551SPing-Ke Shih const u32 *data; 4605c7845551SPing-Ke Shih u32 reg; 4606c7845551SPing-Ke Shih int i; 4607c7845551SPing-Ke Shih 4608c7845551SPing-Ke Shih if (bandedge_cfg >= RTW89_TSSI_CFG_NUM) 4609c7845551SPing-Ke Shih return; 4610c7845551SPing-Ke Shih 4611c7845551SPing-Ke Shih if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 4612c7845551SPing-Ke Shih regs = rtw89_tssi_bandedge_regs_flat; 4613c7845551SPing-Ke Shih else 4614c7845551SPing-Ke Shih regs = rtw89_tssi_bandedge_regs_level; 4615c7845551SPing-Ke Shih 4616c7845551SPing-Ke Shih data = chip->tssi_dbw_table->data[bandedge_cfg]; 4617c7845551SPing-Ke Shih 4618c7845551SPing-Ke Shih for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) { 4619c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx); 4620c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]); 4621c7845551SPing-Ke Shih } 4622c7845551SPing-Ke Shih 4623c7845551SPing-Ke Shih reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx); 4624c7845551SPing-Ke Shih rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg); 4625c7845551SPing-Ke Shih 4626c7845551SPing-Ke Shih rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg, 4627c7845551SPing-Ke Shih data[RTW89_TSSI_SBW20]); 4628c7845551SPing-Ke Shih } 4629c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg); 4630bb9040b3SPo-Hao Huang 4631bb9040b3SPo-Hao Huang static 4632bb9040b3SPo-Hao Huang const u8 rtw89_ch_base_table[16] = {1, 0xff, 4633bb9040b3SPo-Hao Huang 36, 100, 132, 149, 0xff, 4634bb9040b3SPo-Hao Huang 1, 33, 65, 97, 129, 161, 193, 225, 0xff}; 4635bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_2G 0 4636bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_FIRST 2 4637bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_5G_LAST 5 4638bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_FIRST 7 4639bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_6G_LAST 14 4640bb9040b3SPo-Hao Huang 4641bb9040b3SPo-Hao Huang #define RTW89_CH_BASE_IDX_MASK GENMASK(7, 4) 4642bb9040b3SPo-Hao Huang #define RTW89_CH_OFFSET_MASK GENMASK(3, 0) 4643bb9040b3SPo-Hao Huang 4644bb9040b3SPo-Hao Huang u8 rtw89_encode_chan_idx(struct rtw89_dev *rtwdev, u8 central_ch, u8 band) 4645bb9040b3SPo-Hao Huang { 4646bb9040b3SPo-Hao Huang u8 chan_idx; 4647bb9040b3SPo-Hao Huang u8 last, first; 4648bb9040b3SPo-Hao Huang u8 idx; 4649bb9040b3SPo-Hao Huang 4650bb9040b3SPo-Hao Huang switch (band) { 4651bb9040b3SPo-Hao Huang case RTW89_BAND_2G: 4652bb9040b3SPo-Hao Huang chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, RTW89_CH_BASE_IDX_2G) | 4653bb9040b3SPo-Hao Huang FIELD_PREP(RTW89_CH_OFFSET_MASK, central_ch); 4654bb9040b3SPo-Hao Huang return chan_idx; 4655bb9040b3SPo-Hao Huang case RTW89_BAND_5G: 4656bb9040b3SPo-Hao Huang first = RTW89_CH_BASE_IDX_5G_FIRST; 4657bb9040b3SPo-Hao Huang last = RTW89_CH_BASE_IDX_5G_LAST; 4658bb9040b3SPo-Hao Huang break; 4659bb9040b3SPo-Hao Huang case RTW89_BAND_6G: 4660bb9040b3SPo-Hao Huang first = RTW89_CH_BASE_IDX_6G_FIRST; 4661bb9040b3SPo-Hao Huang last = RTW89_CH_BASE_IDX_6G_LAST; 4662bb9040b3SPo-Hao Huang break; 4663bb9040b3SPo-Hao Huang default: 4664bb9040b3SPo-Hao Huang rtw89_warn(rtwdev, "Unsupported band %d\n", band); 4665bb9040b3SPo-Hao Huang return 0; 4666bb9040b3SPo-Hao Huang } 4667bb9040b3SPo-Hao Huang 4668bb9040b3SPo-Hao Huang for (idx = last; idx >= first; idx--) 4669bb9040b3SPo-Hao Huang if (central_ch >= rtw89_ch_base_table[idx]) 4670bb9040b3SPo-Hao Huang break; 4671bb9040b3SPo-Hao Huang 4672bb9040b3SPo-Hao Huang if (idx < first) { 4673bb9040b3SPo-Hao Huang rtw89_warn(rtwdev, "Unknown band %d channel %d\n", band, central_ch); 4674bb9040b3SPo-Hao Huang return 0; 4675bb9040b3SPo-Hao Huang } 4676bb9040b3SPo-Hao Huang 4677bb9040b3SPo-Hao Huang chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, idx) | 4678bb9040b3SPo-Hao Huang FIELD_PREP(RTW89_CH_OFFSET_MASK, 4679bb9040b3SPo-Hao Huang (central_ch - rtw89_ch_base_table[idx]) >> 1); 4680bb9040b3SPo-Hao Huang return chan_idx; 4681bb9040b3SPo-Hao Huang } 4682bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_encode_chan_idx); 4683bb9040b3SPo-Hao Huang 4684bb9040b3SPo-Hao Huang void rtw89_decode_chan_idx(struct rtw89_dev *rtwdev, u8 chan_idx, 4685bb9040b3SPo-Hao Huang u8 *ch, enum nl80211_band *band) 4686bb9040b3SPo-Hao Huang { 4687bb9040b3SPo-Hao Huang u8 idx, offset; 4688bb9040b3SPo-Hao Huang 4689bb9040b3SPo-Hao Huang idx = FIELD_GET(RTW89_CH_BASE_IDX_MASK, chan_idx); 4690bb9040b3SPo-Hao Huang offset = FIELD_GET(RTW89_CH_OFFSET_MASK, chan_idx); 4691bb9040b3SPo-Hao Huang 4692bb9040b3SPo-Hao Huang if (idx == RTW89_CH_BASE_IDX_2G) { 4693bb9040b3SPo-Hao Huang *band = NL80211_BAND_2GHZ; 4694bb9040b3SPo-Hao Huang *ch = offset; 4695bb9040b3SPo-Hao Huang return; 4696bb9040b3SPo-Hao Huang } 4697bb9040b3SPo-Hao Huang 4698bb9040b3SPo-Hao Huang *band = idx <= RTW89_CH_BASE_IDX_5G_LAST ? NL80211_BAND_5GHZ : NL80211_BAND_6GHZ; 4699bb9040b3SPo-Hao Huang *ch = rtw89_ch_base_table[idx] + (offset << 1); 4700bb9040b3SPo-Hao Huang } 4701bb9040b3SPo-Hao Huang EXPORT_SYMBOL(rtw89_decode_chan_idx); 4702280c4447SChih-Kang Chang 4703280c4447SChih-Kang Chang #define EDCCA_DEFAULT 249 4704280c4447SChih-Kang Chang void rtw89_phy_config_edcca(struct rtw89_dev *rtwdev, bool scan) 4705280c4447SChih-Kang Chang { 4706280c4447SChih-Kang Chang u32 reg = rtwdev->chip->edcca_lvl_reg; 4707280c4447SChih-Kang Chang struct rtw89_hal *hal = &rtwdev->hal; 4708280c4447SChih-Kang Chang u32 val; 4709280c4447SChih-Kang Chang 4710280c4447SChih-Kang Chang if (scan) { 4711280c4447SChih-Kang Chang hal->edcca_bak = rtw89_phy_read32(rtwdev, reg); 4712280c4447SChih-Kang Chang val = hal->edcca_bak; 4713280c4447SChih-Kang Chang u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_A_MSK); 4714280c4447SChih-Kang Chang u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_EDCCA_LVL_P_MSK); 4715280c4447SChih-Kang Chang u32p_replace_bits(&val, EDCCA_DEFAULT, B_SEG0R_PPDU_LVL_MSK); 4716280c4447SChih-Kang Chang rtw89_phy_write32(rtwdev, reg, val); 4717280c4447SChih-Kang Chang } else { 4718280c4447SChih-Kang Chang rtw89_phy_write32(rtwdev, reg, hal->edcca_bak); 4719280c4447SChih-Kang Chang } 4720280c4447SChih-Kang Chang } 4721