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 5e3ec7017SPing-Ke Shih #include "debug.h" 6e3ec7017SPing-Ke Shih #include "fw.h" 7e3ec7017SPing-Ke Shih #include "phy.h" 8e3ec7017SPing-Ke Shih #include "ps.h" 9e3ec7017SPing-Ke Shih #include "reg.h" 10e3ec7017SPing-Ke Shih #include "sar.h" 11e3ec7017SPing-Ke Shih #include "coex.h" 12e3ec7017SPing-Ke Shih 13e3ec7017SPing-Ke Shih static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev, 14e3ec7017SPing-Ke Shih const struct rtw89_ra_report *report) 15e3ec7017SPing-Ke Shih { 16e3ec7017SPing-Ke Shih const struct rate_info *txrate = &report->txrate; 17e3ec7017SPing-Ke Shih u32 bit_rate = report->bit_rate; 18e3ec7017SPing-Ke Shih u8 mcs; 19e3ec7017SPing-Ke Shih 20e3ec7017SPing-Ke Shih /* lower than ofdm, do not aggregate */ 21e3ec7017SPing-Ke Shih if (bit_rate < 550) 22e3ec7017SPing-Ke Shih return 1; 23e3ec7017SPing-Ke Shih 24e3ec7017SPing-Ke Shih /* prevent hardware rate fallback to G mode rate */ 25e3ec7017SPing-Ke Shih if (txrate->flags & RATE_INFO_FLAGS_MCS) 26e3ec7017SPing-Ke Shih mcs = txrate->mcs & 0x07; 27e3ec7017SPing-Ke Shih else if (txrate->flags & (RATE_INFO_FLAGS_VHT_MCS | RATE_INFO_FLAGS_HE_MCS)) 28e3ec7017SPing-Ke Shih mcs = txrate->mcs; 29e3ec7017SPing-Ke Shih else 30e3ec7017SPing-Ke Shih mcs = 0; 31e3ec7017SPing-Ke Shih 32e3ec7017SPing-Ke Shih if (mcs <= 2) 33e3ec7017SPing-Ke Shih return 1; 34e3ec7017SPing-Ke Shih 35e3ec7017SPing-Ke Shih /* lower than 20M vht 2ss mcs8, make it small */ 36e3ec7017SPing-Ke Shih if (bit_rate < 1800) 37e3ec7017SPing-Ke Shih return 1200; 38e3ec7017SPing-Ke Shih 39e3ec7017SPing-Ke Shih /* lower than 40M vht 2ss mcs9, make it medium */ 40e3ec7017SPing-Ke Shih if (bit_rate < 4000) 41e3ec7017SPing-Ke Shih return 2600; 42e3ec7017SPing-Ke Shih 43e3ec7017SPing-Ke Shih /* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */ 44e3ec7017SPing-Ke Shih if (bit_rate < 7000) 45e3ec7017SPing-Ke Shih return 3500; 46e3ec7017SPing-Ke Shih 47e3ec7017SPing-Ke Shih return rtwdev->chip->max_amsdu_limit; 48e3ec7017SPing-Ke Shih } 49e3ec7017SPing-Ke Shih 50e3ec7017SPing-Ke Shih static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap) 51e3ec7017SPing-Ke Shih { 52e3ec7017SPing-Ke Shih u64 ra_mask = 0; 53e3ec7017SPing-Ke Shih u8 mcs_cap; 54e3ec7017SPing-Ke Shih int i, nss; 55e3ec7017SPing-Ke Shih 56e3ec7017SPing-Ke Shih for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) { 57e3ec7017SPing-Ke Shih mcs_cap = mcs_map & 0x3; 58e3ec7017SPing-Ke Shih switch (mcs_cap) { 59e3ec7017SPing-Ke Shih case 2: 60e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss; 61e3ec7017SPing-Ke Shih break; 62e3ec7017SPing-Ke Shih case 1: 63e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss; 64e3ec7017SPing-Ke Shih break; 65e3ec7017SPing-Ke Shih case 0: 66e3ec7017SPing-Ke Shih ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss; 67e3ec7017SPing-Ke Shih break; 68e3ec7017SPing-Ke Shih default: 69e3ec7017SPing-Ke Shih break; 70e3ec7017SPing-Ke Shih } 71e3ec7017SPing-Ke Shih } 72e3ec7017SPing-Ke Shih 73e3ec7017SPing-Ke Shih return ra_mask; 74e3ec7017SPing-Ke Shih } 75e3ec7017SPing-Ke Shih 76e3ec7017SPing-Ke Shih static u64 get_he_ra_mask(struct ieee80211_sta *sta) 77e3ec7017SPing-Ke Shih { 78e3ec7017SPing-Ke Shih struct ieee80211_sta_he_cap cap = sta->he_cap; 79e3ec7017SPing-Ke Shih u16 mcs_map; 80e3ec7017SPing-Ke Shih 81e3ec7017SPing-Ke Shih switch (sta->bandwidth) { 82e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_160: 83e3ec7017SPing-Ke Shih if (cap.he_cap_elem.phy_cap_info[0] & 84e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) 85e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80); 86e3ec7017SPing-Ke Shih else 87e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160); 88e3ec7017SPing-Ke Shih break; 89e3ec7017SPing-Ke Shih default: 90e3ec7017SPing-Ke Shih mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80); 91e3ec7017SPing-Ke Shih } 92e3ec7017SPing-Ke Shih 93e3ec7017SPing-Ke Shih /* MCS11, MCS9, MCS7 */ 94e3ec7017SPing-Ke Shih return get_mcs_ra_mask(mcs_map, 11, 2); 95e3ec7017SPing-Ke Shih } 96e3ec7017SPing-Ke Shih 97e3ec7017SPing-Ke Shih #define RA_FLOOR_TABLE_SIZE 7 98e3ec7017SPing-Ke Shih #define RA_FLOOR_UP_GAP 3 99e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi, 100e3ec7017SPing-Ke Shih u8 ratr_state) 101e3ec7017SPing-Ke Shih { 102e3ec7017SPing-Ke Shih u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100}; 103e3ec7017SPing-Ke Shih u8 rssi_lv = 0; 104e3ec7017SPing-Ke Shih u8 i; 105e3ec7017SPing-Ke Shih 106e3ec7017SPing-Ke Shih rssi >>= 1; 107e3ec7017SPing-Ke Shih for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 108e3ec7017SPing-Ke Shih if (i >= ratr_state) 109e3ec7017SPing-Ke Shih rssi_lv_t[i] += RA_FLOOR_UP_GAP; 110e3ec7017SPing-Ke Shih if (rssi < rssi_lv_t[i]) { 111e3ec7017SPing-Ke Shih rssi_lv = i; 112e3ec7017SPing-Ke Shih break; 113e3ec7017SPing-Ke Shih } 114e3ec7017SPing-Ke Shih } 115e3ec7017SPing-Ke Shih if (rssi_lv == 0) 116e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 117e3ec7017SPing-Ke Shih else if (rssi_lv == 1) 118e3ec7017SPing-Ke Shih return 0xfffffffffffffff0ULL; 119e3ec7017SPing-Ke Shih else if (rssi_lv == 2) 1203c2c2e2eSChien-Hsun Liao return 0xffffffffffffefe0ULL; 121e3ec7017SPing-Ke Shih else if (rssi_lv == 3) 1223c2c2e2eSChien-Hsun Liao return 0xffffffffffffcfc0ULL; 123e3ec7017SPing-Ke Shih else if (rssi_lv == 4) 1243c2c2e2eSChien-Hsun Liao return 0xffffffffffff8f80ULL; 125e3ec7017SPing-Ke Shih else if (rssi_lv >= 5) 1263c2c2e2eSChien-Hsun Liao return 0xffffffffffff0f00ULL; 127e3ec7017SPing-Ke Shih 128e3ec7017SPing-Ke Shih return 0xffffffffffffffffULL; 129e3ec7017SPing-Ke Shih } 130e3ec7017SPing-Ke Shih 1313c2c2e2eSChien-Hsun Liao static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak) 1323c2c2e2eSChien-Hsun Liao { 1333c2c2e2eSChien-Hsun Liao if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0) 1343c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1353c2c2e2eSChien-Hsun Liao 1363c2c2e2eSChien-Hsun Liao if (ra_mask == 0) 1373c2c2e2eSChien-Hsun Liao ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 1383c2c2e2eSChien-Hsun Liao 1393c2c2e2eSChien-Hsun Liao return ra_mask; 1403c2c2e2eSChien-Hsun Liao } 1413c2c2e2eSChien-Hsun Liao 142e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta) 143e3ec7017SPing-Ke Shih { 144e3ec7017SPing-Ke Shih struct rtw89_hal *hal = &rtwdev->hal; 145e3ec7017SPing-Ke Shih struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta); 146e3ec7017SPing-Ke Shih struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 147e3ec7017SPing-Ke Shih enum nl80211_band band; 148e3ec7017SPing-Ke Shih u64 cfg_mask; 149e3ec7017SPing-Ke Shih 150e3ec7017SPing-Ke Shih if (!rtwsta->use_cfg_mask) 151e3ec7017SPing-Ke Shih return -1; 152e3ec7017SPing-Ke Shih 153e3ec7017SPing-Ke Shih switch (hal->current_band_type) { 154e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 155e3ec7017SPing-Ke Shih band = NL80211_BAND_2GHZ; 156e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy, 157e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES); 158e3ec7017SPing-Ke Shih break; 159e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 160e3ec7017SPing-Ke Shih band = NL80211_BAND_5GHZ; 161e3ec7017SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy, 162e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES); 163e3ec7017SPing-Ke Shih break; 164f76b3276SPing-Ke Shih case RTW89_BAND_6G: 165f76b3276SPing-Ke Shih band = NL80211_BAND_6GHZ; 166f76b3276SPing-Ke Shih cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy, 167f76b3276SPing-Ke Shih RA_MASK_OFDM_RATES); 168f76b3276SPing-Ke Shih break; 169e3ec7017SPing-Ke Shih default: 170e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unhandled band type %d\n", hal->current_band_type); 171e3ec7017SPing-Ke Shih return -1; 172e3ec7017SPing-Ke Shih } 173e3ec7017SPing-Ke Shih 174e3ec7017SPing-Ke Shih if (sta->he_cap.has_he) { 175e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0], 176e3ec7017SPing-Ke Shih RA_MASK_HE_1SS_RATES); 177e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1], 178e3ec7017SPing-Ke Shih RA_MASK_HE_2SS_RATES); 179e3ec7017SPing-Ke Shih } else if (sta->vht_cap.vht_supported) { 180e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0], 181e3ec7017SPing-Ke Shih RA_MASK_VHT_1SS_RATES); 182e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1], 183e3ec7017SPing-Ke Shih RA_MASK_VHT_2SS_RATES); 184e3ec7017SPing-Ke Shih } else if (sta->ht_cap.ht_supported) { 185e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0], 186e3ec7017SPing-Ke Shih RA_MASK_HT_1SS_RATES); 187e3ec7017SPing-Ke Shih cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1], 188e3ec7017SPing-Ke Shih RA_MASK_HT_2SS_RATES); 189e3ec7017SPing-Ke Shih } 190e3ec7017SPing-Ke Shih 191e3ec7017SPing-Ke Shih return cfg_mask; 192e3ec7017SPing-Ke Shih } 193e3ec7017SPing-Ke Shih 194e3ec7017SPing-Ke Shih static const u64 195e3ec7017SPing-Ke Shih rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES, 196e3ec7017SPing-Ke Shih RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES}; 197e3ec7017SPing-Ke Shih static const u64 198e3ec7017SPing-Ke Shih rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES, 199e3ec7017SPing-Ke Shih RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES}; 200e3ec7017SPing-Ke Shih static const u64 201e3ec7017SPing-Ke Shih rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES, 202e3ec7017SPing-Ke Shih RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES}; 203e3ec7017SPing-Ke Shih 204e3ec7017SPing-Ke Shih static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev, 205e3ec7017SPing-Ke Shih struct ieee80211_sta *sta, bool csi) 206e3ec7017SPing-Ke Shih { 207e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 208e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = rtwsta->rtwvif; 209e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern; 210e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 211e3ec7017SPing-Ke Shih const u64 *high_rate_masks = rtw89_ra_mask_ht_rates; 212e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi); 213e3ec7017SPing-Ke Shih u64 ra_mask = 0; 2143c2c2e2eSChien-Hsun Liao u64 ra_mask_bak; 215e3ec7017SPing-Ke Shih u8 mode = 0; 216e3ec7017SPing-Ke Shih u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY; 217e3ec7017SPing-Ke Shih u8 bw_mode = 0; 218e3ec7017SPing-Ke Shih u8 stbc_en = 0; 219e3ec7017SPing-Ke Shih u8 ldpc_en = 0; 220e3ec7017SPing-Ke Shih u8 i; 221e3ec7017SPing-Ke Shih bool sgi = false; 222e3ec7017SPing-Ke Shih 223e3ec7017SPing-Ke Shih memset(ra, 0, sizeof(*ra)); 224e3ec7017SPing-Ke Shih /* Set the ra mask from sta's capability */ 225e3ec7017SPing-Ke Shih if (sta->he_cap.has_he) { 226e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HE; 227e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HE; 228e3ec7017SPing-Ke Shih ra_mask |= get_he_ra_mask(sta); 229e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_he_rates; 230e3ec7017SPing-Ke Shih if (sta->he_cap.he_cap_elem.phy_cap_info[2] & 231e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ) 232e3ec7017SPing-Ke Shih stbc_en = 1; 233e3ec7017SPing-Ke Shih if (sta->he_cap.he_cap_elem.phy_cap_info[1] & 234e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD) 235e3ec7017SPing-Ke Shih ldpc_en = 1; 236e3ec7017SPing-Ke Shih } else if (sta->vht_cap.vht_supported) { 237e3ec7017SPing-Ke Shih u16 mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.rx_mcs_map); 238e3ec7017SPing-Ke Shih 239e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_VHT; 240e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_VHT; 241e3ec7017SPing-Ke Shih /* MCS9, MCS8, MCS7 */ 242e3ec7017SPing-Ke Shih ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1); 243e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_vht_rates; 244e3ec7017SPing-Ke Shih if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK) 245e3ec7017SPing-Ke Shih stbc_en = 1; 246e3ec7017SPing-Ke Shih if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC) 247e3ec7017SPing-Ke Shih ldpc_en = 1; 248e3ec7017SPing-Ke Shih } else if (sta->ht_cap.ht_supported) { 249e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_HT; 250e3ec7017SPing-Ke Shih csi_mode = RTW89_RA_RPT_MODE_HT; 251e3ec7017SPing-Ke Shih ra_mask |= ((u64)sta->ht_cap.mcs.rx_mask[3] << 48) | 252e3ec7017SPing-Ke Shih ((u64)sta->ht_cap.mcs.rx_mask[2] << 36) | 253e3ec7017SPing-Ke Shih (sta->ht_cap.mcs.rx_mask[1] << 24) | 254e3ec7017SPing-Ke Shih (sta->ht_cap.mcs.rx_mask[0] << 12); 255e3ec7017SPing-Ke Shih high_rate_masks = rtw89_ra_mask_ht_rates; 256e3ec7017SPing-Ke Shih if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC) 257e3ec7017SPing-Ke Shih stbc_en = 1; 258e3ec7017SPing-Ke Shih if (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING) 259e3ec7017SPing-Ke Shih ldpc_en = 1; 260e3ec7017SPing-Ke Shih } 261e3ec7017SPing-Ke Shih 262f76b3276SPing-Ke Shih switch (rtwdev->hal.current_band_type) { 263f76b3276SPing-Ke Shih case RTW89_BAND_2G: 2643c2c2e2eSChien-Hsun Liao ra_mask |= sta->supp_rates[NL80211_BAND_2GHZ]; 265e3ec7017SPing-Ke Shih if (sta->supp_rates[NL80211_BAND_2GHZ] <= 0xf) 266e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_CCK; 267e3ec7017SPing-Ke Shih else 268e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM; 269f76b3276SPing-Ke Shih break; 270f76b3276SPing-Ke Shih case RTW89_BAND_5G: 2713c2c2e2eSChien-Hsun Liao ra_mask |= (u64)sta->supp_rates[NL80211_BAND_5GHZ] << 4; 272e3ec7017SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 273f76b3276SPing-Ke Shih break; 274f76b3276SPing-Ke Shih case RTW89_BAND_6G: 275f76b3276SPing-Ke Shih ra_mask |= (u64)sta->supp_rates[NL80211_BAND_6GHZ] << 4; 276f76b3276SPing-Ke Shih mode |= RTW89_RA_MODE_OFDM; 277f76b3276SPing-Ke Shih break; 278f76b3276SPing-Ke Shih default: 279f76b3276SPing-Ke Shih rtw89_err(rtwdev, "Unknown band type\n"); 280f76b3276SPing-Ke Shih break; 281e3ec7017SPing-Ke Shih } 282e3ec7017SPing-Ke Shih 2833c2c2e2eSChien-Hsun Liao ra_mask_bak = ra_mask; 2843c2c2e2eSChien-Hsun Liao 285e3ec7017SPing-Ke Shih if (mode >= RTW89_RA_MODE_HT) { 2863c2c2e2eSChien-Hsun Liao u64 mask = 0; 287e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->hal.tx_nss; i++) 2883c2c2e2eSChien-Hsun Liao mask |= high_rate_masks[i]; 289e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_OFDM) 2903c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBOFDM_RATES; 291e3ec7017SPing-Ke Shih if (mode & RTW89_RA_MODE_CCK) 2923c2c2e2eSChien-Hsun Liao mask |= RA_MASK_SUBCCK_RATES; 2933c2c2e2eSChien-Hsun Liao ra_mask &= mask; 294e3ec7017SPing-Ke Shih } else if (mode & RTW89_RA_MODE_OFDM) { 2953c2c2e2eSChien-Hsun Liao ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES); 296e3ec7017SPing-Ke Shih } 297e3ec7017SPing-Ke Shih 2983c2c2e2eSChien-Hsun Liao if (mode != RTW89_RA_MODE_CCK) 299e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0); 3003c2c2e2eSChien-Hsun Liao 3013c2c2e2eSChien-Hsun Liao ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak); 302e3ec7017SPing-Ke Shih ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 303e3ec7017SPing-Ke Shih 304e3ec7017SPing-Ke Shih switch (sta->bandwidth) { 305167044afSPing-Ke Shih case IEEE80211_STA_RX_BW_160: 306167044afSPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_160; 307167044afSPing-Ke Shih sgi = sta->vht_cap.vht_supported && 308167044afSPing-Ke Shih (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160); 309167044afSPing-Ke Shih break; 310e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_80: 311e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_80; 312e3ec7017SPing-Ke Shih sgi = sta->vht_cap.vht_supported && 313e3ec7017SPing-Ke Shih (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80); 314e3ec7017SPing-Ke Shih break; 315e3ec7017SPing-Ke Shih case IEEE80211_STA_RX_BW_40: 316e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_40; 317e3ec7017SPing-Ke Shih sgi = sta->ht_cap.ht_supported && 318e3ec7017SPing-Ke Shih (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40); 319e3ec7017SPing-Ke Shih break; 320e3ec7017SPing-Ke Shih default: 321e3ec7017SPing-Ke Shih bw_mode = RTW89_CHANNEL_WIDTH_20; 322e3ec7017SPing-Ke Shih sgi = sta->ht_cap.ht_supported && 323e3ec7017SPing-Ke Shih (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20); 324e3ec7017SPing-Ke Shih break; 325e3ec7017SPing-Ke Shih } 326e3ec7017SPing-Ke Shih 327e3ec7017SPing-Ke Shih if (sta->he_cap.he_cap_elem.phy_cap_info[3] & 328e3ec7017SPing-Ke Shih IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM) 329e3ec7017SPing-Ke Shih ra->dcm_cap = 1; 330e3ec7017SPing-Ke Shih 331e3ec7017SPing-Ke Shih if (rate_pattern->enable) { 332e3ec7017SPing-Ke Shih ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta); 333e3ec7017SPing-Ke Shih ra_mask &= rate_pattern->ra_mask; 334e3ec7017SPing-Ke Shih mode = rate_pattern->ra_mode; 335e3ec7017SPing-Ke Shih } 336e3ec7017SPing-Ke Shih 337e3ec7017SPing-Ke Shih ra->bw_cap = bw_mode; 338e3ec7017SPing-Ke Shih ra->mode_ctrl = mode; 339e3ec7017SPing-Ke Shih ra->macid = rtwsta->mac_id; 340e3ec7017SPing-Ke Shih ra->stbc_cap = stbc_en; 341e3ec7017SPing-Ke Shih ra->ldpc_cap = ldpc_en; 342e3ec7017SPing-Ke Shih ra->ss_num = min(sta->rx_nss, rtwdev->hal.tx_nss) - 1; 343e3ec7017SPing-Ke Shih ra->en_sgi = sgi; 344e3ec7017SPing-Ke Shih ra->ra_mask = ra_mask; 345e3ec7017SPing-Ke Shih 346e3ec7017SPing-Ke Shih if (!csi) 347e3ec7017SPing-Ke Shih return; 348e3ec7017SPing-Ke Shih 349e3ec7017SPing-Ke Shih ra->fixed_csi_rate_en = false; 350e3ec7017SPing-Ke Shih ra->ra_csi_rate_en = true; 351e3ec7017SPing-Ke Shih ra->cr_tbl_sel = false; 352e3ec7017SPing-Ke Shih ra->band_num = rtwvif->phy_idx; 353e3ec7017SPing-Ke Shih ra->csi_bw = bw_mode; 354e3ec7017SPing-Ke Shih ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32; 355e3ec7017SPing-Ke Shih ra->csi_mcs_ss_idx = 5; 356e3ec7017SPing-Ke Shih ra->csi_mode = csi_mode; 357e3ec7017SPing-Ke Shih } 358e3ec7017SPing-Ke Shih 359e3ec7017SPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta) 360e3ec7017SPing-Ke Shih { 361e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 362e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 363e3ec7017SPing-Ke Shih 364e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, false); 365e3ec7017SPing-Ke Shih ra->upd_mask = 1; 366e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 367e3ec7017SPing-Ke Shih "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d", 368e3ec7017SPing-Ke Shih ra->macid, 369e3ec7017SPing-Ke Shih ra->bw_cap, 370e3ec7017SPing-Ke Shih ra->ss_num, 371e3ec7017SPing-Ke Shih ra->en_sgi, 372e3ec7017SPing-Ke Shih ra->giltf); 373e3ec7017SPing-Ke Shih 374e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, false); 375e3ec7017SPing-Ke Shih } 376e3ec7017SPing-Ke Shih 377e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next, 378e3ec7017SPing-Ke Shih u16 rate_base, u64 ra_mask, u8 ra_mode, 379e3ec7017SPing-Ke Shih u32 rate_ctrl, u32 ctrl_skip, bool force) 380e3ec7017SPing-Ke Shih { 381e3ec7017SPing-Ke Shih u8 n, c; 382e3ec7017SPing-Ke Shih 383e3ec7017SPing-Ke Shih if (rate_ctrl == ctrl_skip) 384e3ec7017SPing-Ke Shih return true; 385e3ec7017SPing-Ke Shih 386e3ec7017SPing-Ke Shih n = hweight32(rate_ctrl); 387e3ec7017SPing-Ke Shih if (n == 0) 388e3ec7017SPing-Ke Shih return true; 389e3ec7017SPing-Ke Shih 390e3ec7017SPing-Ke Shih if (force && n != 1) 391e3ec7017SPing-Ke Shih return false; 392e3ec7017SPing-Ke Shih 393e3ec7017SPing-Ke Shih if (next->enable) 394e3ec7017SPing-Ke Shih return false; 395e3ec7017SPing-Ke Shih 396e3ec7017SPing-Ke Shih c = __fls(rate_ctrl); 397e3ec7017SPing-Ke Shih next->rate = rate_base + c; 398e3ec7017SPing-Ke Shih next->ra_mode = ra_mode; 399e3ec7017SPing-Ke Shih next->ra_mask = ra_mask; 400e3ec7017SPing-Ke Shih next->enable = true; 401e3ec7017SPing-Ke Shih 402e3ec7017SPing-Ke Shih return true; 403e3ec7017SPing-Ke Shih } 404e3ec7017SPing-Ke Shih 405e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev, 406e3ec7017SPing-Ke Shih struct ieee80211_vif *vif, 407e3ec7017SPing-Ke Shih const struct cfg80211_bitrate_mask *mask) 408e3ec7017SPing-Ke Shih { 409e3ec7017SPing-Ke Shih struct ieee80211_supported_band *sband; 410e3ec7017SPing-Ke Shih struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 411e3ec7017SPing-Ke Shih struct rtw89_phy_rate_pattern next_pattern = {0}; 412e3ec7017SPing-Ke Shih static const u16 hw_rate_he[] = {RTW89_HW_RATE_HE_NSS1_MCS0, 413e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS2_MCS0, 414e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS3_MCS0, 415e3ec7017SPing-Ke Shih RTW89_HW_RATE_HE_NSS4_MCS0}; 416e3ec7017SPing-Ke Shih static const u16 hw_rate_vht[] = {RTW89_HW_RATE_VHT_NSS1_MCS0, 417e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS2_MCS0, 418e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS3_MCS0, 419e3ec7017SPing-Ke Shih RTW89_HW_RATE_VHT_NSS4_MCS0}; 420e3ec7017SPing-Ke Shih static const u16 hw_rate_ht[] = {RTW89_HW_RATE_MCS0, 421e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS8, 422e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS16, 423e3ec7017SPing-Ke Shih RTW89_HW_RATE_MCS24}; 424e3ec7017SPing-Ke Shih u8 band = rtwdev->hal.current_band_type; 425e3ec7017SPing-Ke Shih u8 tx_nss = rtwdev->hal.tx_nss; 426e3ec7017SPing-Ke Shih u8 i; 427e3ec7017SPing-Ke Shih 428e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 429e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_he[i], 430e3ec7017SPing-Ke Shih RA_MASK_HE_RATES, RTW89_RA_MODE_HE, 431e3ec7017SPing-Ke Shih mask->control[band].he_mcs[i], 432e3ec7017SPing-Ke Shih 0, true)) 433e3ec7017SPing-Ke Shih goto out; 434e3ec7017SPing-Ke Shih 435e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 436e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i], 437e3ec7017SPing-Ke Shih RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT, 438e3ec7017SPing-Ke Shih mask->control[band].vht_mcs[i], 439e3ec7017SPing-Ke Shih 0, true)) 440e3ec7017SPing-Ke Shih goto out; 441e3ec7017SPing-Ke Shih 442e3ec7017SPing-Ke Shih for (i = 0; i < tx_nss; i++) 443e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i], 444e3ec7017SPing-Ke Shih RA_MASK_HT_RATES, RTW89_RA_MODE_HT, 445e3ec7017SPing-Ke Shih mask->control[band].ht_mcs[i], 446e3ec7017SPing-Ke Shih 0, true)) 447e3ec7017SPing-Ke Shih goto out; 448e3ec7017SPing-Ke Shih 449e3ec7017SPing-Ke Shih /* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and 450e3ec7017SPing-Ke Shih * require at least one basic rate for ieee80211_set_bitrate_mask, 451e3ec7017SPing-Ke Shih * so the decision just depends on if all bitrates are set or not. 452e3ec7017SPing-Ke Shih */ 453e3ec7017SPing-Ke Shih sband = rtwdev->hw->wiphy->bands[band]; 454e3ec7017SPing-Ke Shih if (band == RTW89_BAND_2G) { 455e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1, 456e3ec7017SPing-Ke Shih RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES, 457e3ec7017SPing-Ke Shih RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM, 458e3ec7017SPing-Ke Shih mask->control[band].legacy, 459e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 460e3ec7017SPing-Ke Shih goto out; 461e3ec7017SPing-Ke Shih } else { 462e3ec7017SPing-Ke Shih if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6, 463e3ec7017SPing-Ke Shih RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM, 464e3ec7017SPing-Ke Shih mask->control[band].legacy, 465e3ec7017SPing-Ke Shih BIT(sband->n_bitrates) - 1, false)) 466e3ec7017SPing-Ke Shih goto out; 467e3ec7017SPing-Ke Shih } 468e3ec7017SPing-Ke Shih 469e3ec7017SPing-Ke Shih if (!next_pattern.enable) 470e3ec7017SPing-Ke Shih goto out; 471e3ec7017SPing-Ke Shih 472e3ec7017SPing-Ke Shih rtwvif->rate_pattern = next_pattern; 473e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 474e3ec7017SPing-Ke Shih "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n", 475e3ec7017SPing-Ke Shih next_pattern.rate, 476e3ec7017SPing-Ke Shih next_pattern.ra_mask, 477e3ec7017SPing-Ke Shih next_pattern.ra_mode); 478e3ec7017SPing-Ke Shih return; 479e3ec7017SPing-Ke Shih 480e3ec7017SPing-Ke Shih out: 481e3ec7017SPing-Ke Shih rtwvif->rate_pattern.enable = false; 482e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n"); 483e3ec7017SPing-Ke Shih } 484e3ec7017SPing-Ke Shih 485e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta) 486e3ec7017SPing-Ke Shih { 487e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = (struct rtw89_dev *)data; 488e3ec7017SPing-Ke Shih 489e3ec7017SPing-Ke Shih rtw89_phy_ra_updata_sta(rtwdev, sta); 490e3ec7017SPing-Ke Shih } 491e3ec7017SPing-Ke Shih 492e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev) 493e3ec7017SPing-Ke Shih { 494e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 495e3ec7017SPing-Ke Shih rtw89_phy_ra_updata_sta_iter, 496e3ec7017SPing-Ke Shih rtwdev); 497e3ec7017SPing-Ke Shih } 498e3ec7017SPing-Ke Shih 499e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta) 500e3ec7017SPing-Ke Shih { 501e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 502e3ec7017SPing-Ke Shih struct rtw89_ra_info *ra = &rtwsta->ra; 503e3ec7017SPing-Ke Shih u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR; 504e3ec7017SPing-Ke Shih bool csi = rtw89_sta_has_beamformer_cap(sta); 505e3ec7017SPing-Ke Shih 506e3ec7017SPing-Ke Shih rtw89_phy_ra_sta_update(rtwdev, sta, csi); 507e3ec7017SPing-Ke Shih 508e3ec7017SPing-Ke Shih if (rssi > 40) 509e3ec7017SPing-Ke Shih ra->init_rate_lv = 1; 510e3ec7017SPing-Ke Shih else if (rssi > 20) 511e3ec7017SPing-Ke Shih ra->init_rate_lv = 2; 512e3ec7017SPing-Ke Shih else if (rssi > 1) 513e3ec7017SPing-Ke Shih ra->init_rate_lv = 3; 514e3ec7017SPing-Ke Shih else 515e3ec7017SPing-Ke Shih ra->init_rate_lv = 0; 516e3ec7017SPing-Ke Shih ra->upd_all = 1; 517e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 518e3ec7017SPing-Ke Shih "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d", 519e3ec7017SPing-Ke Shih ra->macid, 520e3ec7017SPing-Ke Shih ra->mode_ctrl, 521e3ec7017SPing-Ke Shih ra->bw_cap, 522e3ec7017SPing-Ke Shih ra->ss_num, 523e3ec7017SPing-Ke Shih ra->init_rate_lv); 524e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RA, 525e3ec7017SPing-Ke Shih "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d", 526e3ec7017SPing-Ke Shih ra->dcm_cap, 527e3ec7017SPing-Ke Shih ra->er_cap, 528e3ec7017SPing-Ke Shih ra->ldpc_cap, 529e3ec7017SPing-Ke Shih ra->stbc_cap, 530e3ec7017SPing-Ke Shih ra->en_sgi, 531e3ec7017SPing-Ke Shih ra->giltf); 532e3ec7017SPing-Ke Shih 533e3ec7017SPing-Ke Shih rtw89_fw_h2c_ra(rtwdev, ra, csi); 534e3ec7017SPing-Ke Shih } 535e3ec7017SPing-Ke Shih 536e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev, 537e3ec7017SPing-Ke Shih struct rtw89_channel_params *param, 538e3ec7017SPing-Ke Shih enum rtw89_bandwidth dbw) 539e3ec7017SPing-Ke Shih { 540e3ec7017SPing-Ke Shih enum rtw89_bandwidth cbw = param->bandwidth; 541e3ec7017SPing-Ke Shih u8 pri_ch = param->primary_chan; 542e3ec7017SPing-Ke Shih u8 central_ch = param->center_chan; 543e3ec7017SPing-Ke Shih u8 txsc_idx = 0; 544e3ec7017SPing-Ke Shih u8 tmp = 0; 545e3ec7017SPing-Ke Shih 546e3ec7017SPing-Ke Shih if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20) 547e3ec7017SPing-Ke Shih return txsc_idx; 548e3ec7017SPing-Ke Shih 549e3ec7017SPing-Ke Shih switch (cbw) { 550e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 551e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 1 : 2; 552e3ec7017SPing-Ke Shih break; 553e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 554e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 555e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 556e3ec7017SPing-Ke Shih txsc_idx = (pri_ch - central_ch) >> 1; 557e3ec7017SPing-Ke Shih else 558e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 1; 559e3ec7017SPing-Ke Shih } else { 560e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 9 : 10; 561e3ec7017SPing-Ke Shih } 562e3ec7017SPing-Ke Shih break; 563e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_160: 564e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 565e3ec7017SPing-Ke Shih tmp = (pri_ch - central_ch) >> 1; 566e3ec7017SPing-Ke Shih else 567e3ec7017SPing-Ke Shih tmp = ((central_ch - pri_ch) >> 1) + 1; 568e3ec7017SPing-Ke Shih 569e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 570e3ec7017SPing-Ke Shih txsc_idx = tmp; 571e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 572e3ec7017SPing-Ke Shih if (tmp == 1 || tmp == 3) 573e3ec7017SPing-Ke Shih txsc_idx = 9; 574e3ec7017SPing-Ke Shih else if (tmp == 5 || tmp == 7) 575e3ec7017SPing-Ke Shih txsc_idx = 11; 576e3ec7017SPing-Ke Shih else if (tmp == 2 || tmp == 4) 577e3ec7017SPing-Ke Shih txsc_idx = 10; 578e3ec7017SPing-Ke Shih else if (tmp == 6 || tmp == 8) 579e3ec7017SPing-Ke Shih txsc_idx = 12; 580e3ec7017SPing-Ke Shih else 581e3ec7017SPing-Ke Shih return 0xff; 582e3ec7017SPing-Ke Shih } else { 583e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 13 : 14; 584e3ec7017SPing-Ke Shih } 585e3ec7017SPing-Ke Shih break; 586e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80_80: 587e3ec7017SPing-Ke Shih if (dbw == RTW89_CHANNEL_WIDTH_20) { 588e3ec7017SPing-Ke Shih if (pri_ch > central_ch) 589e3ec7017SPing-Ke Shih txsc_idx = (10 - (pri_ch - central_ch)) >> 1; 590e3ec7017SPing-Ke Shih else 591e3ec7017SPing-Ke Shih txsc_idx = ((central_ch - pri_ch) >> 1) + 5; 592e3ec7017SPing-Ke Shih } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 593e3ec7017SPing-Ke Shih txsc_idx = pri_ch > central_ch ? 10 : 12; 594e3ec7017SPing-Ke Shih } else { 595e3ec7017SPing-Ke Shih txsc_idx = 14; 596e3ec7017SPing-Ke Shih } 597e3ec7017SPing-Ke Shih break; 598e3ec7017SPing-Ke Shih default: 599e3ec7017SPing-Ke Shih break; 600e3ec7017SPing-Ke Shih } 601e3ec7017SPing-Ke Shih 602e3ec7017SPing-Ke Shih return txsc_idx; 603e3ec7017SPing-Ke Shih } 604861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc); 605e3ec7017SPing-Ke Shih 606e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 607e3ec7017SPing-Ke Shih u32 addr, u32 mask) 608e3ec7017SPing-Ke Shih { 609e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 610e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 611e3ec7017SPing-Ke Shih u32 val, direct_addr; 612e3ec7017SPing-Ke Shih 613e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 614e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 615e3ec7017SPing-Ke Shih return INV_RF_DATA; 616e3ec7017SPing-Ke Shih } 617e3ec7017SPing-Ke Shih 618e3ec7017SPing-Ke Shih addr &= 0xff; 619e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 620e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 621e3ec7017SPing-Ke Shih 622e3ec7017SPing-Ke Shih val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask); 623e3ec7017SPing-Ke Shih 624e3ec7017SPing-Ke Shih return val; 625e3ec7017SPing-Ke Shih } 626e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf); 627e3ec7017SPing-Ke Shih 628e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 629e3ec7017SPing-Ke Shih u32 addr, u32 mask, u32 data) 630e3ec7017SPing-Ke Shih { 631e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 632e3ec7017SPing-Ke Shih const u32 *base_addr = chip->rf_base_addr; 633e3ec7017SPing-Ke Shih u32 direct_addr; 634e3ec7017SPing-Ke Shih 635e3ec7017SPing-Ke Shih if (rf_path >= rtwdev->chip->rf_path_num) { 636e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 637e3ec7017SPing-Ke Shih return false; 638e3ec7017SPing-Ke Shih } 639e3ec7017SPing-Ke Shih 640e3ec7017SPing-Ke Shih addr &= 0xff; 641e3ec7017SPing-Ke Shih direct_addr = base_addr[rf_path] + (addr << 2); 642e3ec7017SPing-Ke Shih mask &= RFREG_MASK; 643e3ec7017SPing-Ke Shih 644e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data); 645e3ec7017SPing-Ke Shih 646e3ec7017SPing-Ke Shih /* delay to ensure writing properly */ 647e3ec7017SPing-Ke Shih udelay(1); 648e3ec7017SPing-Ke Shih 649e3ec7017SPing-Ke Shih return true; 650e3ec7017SPing-Ke Shih } 651e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf); 652e3ec7017SPing-Ke Shih 653e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev, 654e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx) 655e3ec7017SPing-Ke Shih { 656e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 657e3ec7017SPing-Ke Shih 658e3ec7017SPing-Ke Shih chip->ops->bb_reset(rtwdev, phy_idx); 659e3ec7017SPing-Ke Shih } 660e3ec7017SPing-Ke Shih 661e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev, 662e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 663e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 664e3ec7017SPing-Ke Shih void *extra_data) 665e3ec7017SPing-Ke Shih { 666e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) 667e3ec7017SPing-Ke Shih mdelay(50); 668e3ec7017SPing-Ke Shih else if (reg->addr == 0xfd) 669e3ec7017SPing-Ke Shih mdelay(5); 670e3ec7017SPing-Ke Shih else if (reg->addr == 0xfc) 671e3ec7017SPing-Ke Shih mdelay(1); 672e3ec7017SPing-Ke Shih else if (reg->addr == 0xfb) 673e3ec7017SPing-Ke Shih udelay(50); 674e3ec7017SPing-Ke Shih else if (reg->addr == 0xfa) 675e3ec7017SPing-Ke Shih udelay(5); 676e3ec7017SPing-Ke Shih else if (reg->addr == 0xf9) 677e3ec7017SPing-Ke Shih udelay(1); 678e3ec7017SPing-Ke Shih else 679e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, reg->addr, reg->data); 680e3ec7017SPing-Ke Shih } 681e3ec7017SPing-Ke Shih 682e3ec7017SPing-Ke Shih static void 683e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev, 684e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 685e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 686e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 687e3ec7017SPing-Ke Shih { 688e3ec7017SPing-Ke Shih u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE; 689e3ec7017SPing-Ke Shih u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE; 690e3ec7017SPing-Ke Shih 69130101812SPing-Ke Shih if (page >= RTW89_H2C_RF_PAGE_NUM) { 69230101812SPing-Ke Shih rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d", 69330101812SPing-Ke Shih rf_path, info->curr_idx); 69430101812SPing-Ke Shih return; 69530101812SPing-Ke Shih } 69630101812SPing-Ke Shih 697e3ec7017SPing-Ke Shih info->rtw89_phy_config_rf_h2c[page][idx] = 698e3ec7017SPing-Ke Shih cpu_to_le32((reg->addr << 20) | reg->data); 699e3ec7017SPing-Ke Shih info->curr_idx++; 700e3ec7017SPing-Ke Shih } 701e3ec7017SPing-Ke Shih 702e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev, 703e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *info) 704e3ec7017SPing-Ke Shih { 70530101812SPing-Ke Shih u16 remain = info->curr_idx; 70630101812SPing-Ke Shih u16 len = 0; 707e3ec7017SPing-Ke Shih u8 i; 708e3ec7017SPing-Ke Shih int ret = 0; 709e3ec7017SPing-Ke Shih 71030101812SPing-Ke Shih if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) { 711e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, 71230101812SPing-Ke Shih "rf reg h2c total len %d larger than %d\n", 71330101812SPing-Ke Shih remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE); 71430101812SPing-Ke Shih ret = -EINVAL; 71530101812SPing-Ke Shih goto out; 716e3ec7017SPing-Ke Shih } 717e3ec7017SPing-Ke Shih 71830101812SPing-Ke Shih for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) { 71930101812SPing-Ke Shih len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain; 72030101812SPing-Ke Shih ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i); 721e3ec7017SPing-Ke Shih if (ret) 72230101812SPing-Ke Shih goto out; 723e3ec7017SPing-Ke Shih } 72430101812SPing-Ke Shih out: 725e3ec7017SPing-Ke Shih info->curr_idx = 0; 726e3ec7017SPing-Ke Shih 72730101812SPing-Ke Shih return ret; 728e3ec7017SPing-Ke Shih } 729e3ec7017SPing-Ke Shih 730e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev, 731e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 732e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 733e3ec7017SPing-Ke Shih void *extra_data) 734e3ec7017SPing-Ke Shih { 735e3ec7017SPing-Ke Shih if (reg->addr == 0xfe) { 736e3ec7017SPing-Ke Shih mdelay(50); 737e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfd) { 738e3ec7017SPing-Ke Shih mdelay(5); 739e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfc) { 740e3ec7017SPing-Ke Shih mdelay(1); 741e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfb) { 742e3ec7017SPing-Ke Shih udelay(50); 743e3ec7017SPing-Ke Shih } else if (reg->addr == 0xfa) { 744e3ec7017SPing-Ke Shih udelay(5); 745e3ec7017SPing-Ke Shih } else if (reg->addr == 0xf9) { 746e3ec7017SPing-Ke Shih udelay(1); 747e3ec7017SPing-Ke Shih } else { 748e3ec7017SPing-Ke Shih rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data); 749e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 750e3ec7017SPing-Ke Shih (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 751e3ec7017SPing-Ke Shih } 752e3ec7017SPing-Ke Shih } 753e3ec7017SPing-Ke Shih 754e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev, 755e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 756e3ec7017SPing-Ke Shih u32 *headline_size, u32 *headline_idx, 757e3ec7017SPing-Ke Shih u8 rfe, u8 cv) 758e3ec7017SPing-Ke Shih { 759e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 760e3ec7017SPing-Ke Shih u32 headline; 761e3ec7017SPing-Ke Shih u32 compare, target; 762e3ec7017SPing-Ke Shih u8 rfe_para, cv_para; 763e3ec7017SPing-Ke Shih u8 cv_max = 0; 764e3ec7017SPing-Ke Shih bool case_matched = false; 765e3ec7017SPing-Ke Shih u32 i; 766e3ec7017SPing-Ke Shih 767e3ec7017SPing-Ke Shih for (i = 0; i < table->n_regs; i++) { 768e3ec7017SPing-Ke Shih reg = &table->regs[i]; 769e3ec7017SPing-Ke Shih headline = get_phy_headline(reg->addr); 770e3ec7017SPing-Ke Shih if (headline != PHY_HEADLINE_VALID) 771e3ec7017SPing-Ke Shih break; 772e3ec7017SPing-Ke Shih } 773e3ec7017SPing-Ke Shih *headline_size = i; 774e3ec7017SPing-Ke Shih if (*headline_size == 0) 775e3ec7017SPing-Ke Shih return 0; 776e3ec7017SPing-Ke Shih 777e3ec7017SPing-Ke Shih /* case 1: RFE match, CV match */ 778e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, cv); 779e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 780e3ec7017SPing-Ke Shih reg = &table->regs[i]; 781e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 782e3ec7017SPing-Ke Shih if (target == compare) { 783e3ec7017SPing-Ke Shih *headline_idx = i; 784e3ec7017SPing-Ke Shih return 0; 785e3ec7017SPing-Ke Shih } 786e3ec7017SPing-Ke Shih } 787e3ec7017SPing-Ke Shih 788e3ec7017SPing-Ke Shih /* case 2: RFE match, CV don't care */ 789e3ec7017SPing-Ke Shih compare = get_phy_compare(rfe, PHY_COND_DONT_CARE); 790e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 791e3ec7017SPing-Ke Shih reg = &table->regs[i]; 792e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 793e3ec7017SPing-Ke Shih if (target == compare) { 794e3ec7017SPing-Ke Shih *headline_idx = i; 795e3ec7017SPing-Ke Shih return 0; 796e3ec7017SPing-Ke Shih } 797e3ec7017SPing-Ke Shih } 798e3ec7017SPing-Ke Shih 799e3ec7017SPing-Ke Shih /* case 3: RFE match, CV max in table */ 800e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 801e3ec7017SPing-Ke Shih reg = &table->regs[i]; 802e3ec7017SPing-Ke Shih rfe_para = get_phy_cond_rfe(reg->addr); 803e3ec7017SPing-Ke Shih cv_para = get_phy_cond_cv(reg->addr); 804e3ec7017SPing-Ke Shih if (rfe_para == rfe) { 805e3ec7017SPing-Ke Shih if (cv_para >= cv_max) { 806e3ec7017SPing-Ke Shih cv_max = cv_para; 807e3ec7017SPing-Ke Shih *headline_idx = i; 808e3ec7017SPing-Ke Shih case_matched = true; 809e3ec7017SPing-Ke Shih } 810e3ec7017SPing-Ke Shih } 811e3ec7017SPing-Ke Shih } 812e3ec7017SPing-Ke Shih 813e3ec7017SPing-Ke Shih if (case_matched) 814e3ec7017SPing-Ke Shih return 0; 815e3ec7017SPing-Ke Shih 816e3ec7017SPing-Ke Shih /* case 4: RFE don't care, CV max in table */ 817e3ec7017SPing-Ke Shih for (i = 0; i < *headline_size; i++) { 818e3ec7017SPing-Ke Shih reg = &table->regs[i]; 819e3ec7017SPing-Ke Shih rfe_para = get_phy_cond_rfe(reg->addr); 820e3ec7017SPing-Ke Shih cv_para = get_phy_cond_cv(reg->addr); 821e3ec7017SPing-Ke Shih if (rfe_para == PHY_COND_DONT_CARE) { 822e3ec7017SPing-Ke Shih if (cv_para >= cv_max) { 823e3ec7017SPing-Ke Shih cv_max = cv_para; 824e3ec7017SPing-Ke Shih *headline_idx = i; 825e3ec7017SPing-Ke Shih case_matched = true; 826e3ec7017SPing-Ke Shih } 827e3ec7017SPing-Ke Shih } 828e3ec7017SPing-Ke Shih } 829e3ec7017SPing-Ke Shih 830e3ec7017SPing-Ke Shih if (case_matched) 831e3ec7017SPing-Ke Shih return 0; 832e3ec7017SPing-Ke Shih 833e3ec7017SPing-Ke Shih return -EINVAL; 834e3ec7017SPing-Ke Shih } 835e3ec7017SPing-Ke Shih 836e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev, 837e3ec7017SPing-Ke Shih const struct rtw89_phy_table *table, 838e3ec7017SPing-Ke Shih void (*config)(struct rtw89_dev *rtwdev, 839e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg, 840e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path, 841e3ec7017SPing-Ke Shih void *data), 842e3ec7017SPing-Ke Shih void *extra_data) 843e3ec7017SPing-Ke Shih { 844e3ec7017SPing-Ke Shih const struct rtw89_reg2_def *reg; 845e3ec7017SPing-Ke Shih enum rtw89_rf_path rf_path = table->rf_path; 846e3ec7017SPing-Ke Shih u8 rfe = rtwdev->efuse.rfe_type; 847e3ec7017SPing-Ke Shih u8 cv = rtwdev->hal.cv; 848e3ec7017SPing-Ke Shih u32 i; 849e3ec7017SPing-Ke Shih u32 headline_size = 0, headline_idx = 0; 850e3ec7017SPing-Ke Shih u32 target = 0, cfg_target; 851e3ec7017SPing-Ke Shih u8 cond; 852e3ec7017SPing-Ke Shih bool is_matched = true; 853e3ec7017SPing-Ke Shih bool target_found = false; 854e3ec7017SPing-Ke Shih int ret; 855e3ec7017SPing-Ke Shih 856e3ec7017SPing-Ke Shih ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size, 857e3ec7017SPing-Ke Shih &headline_idx, rfe, cv); 858e3ec7017SPing-Ke Shih if (ret) { 859e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv); 860e3ec7017SPing-Ke Shih return; 861e3ec7017SPing-Ke Shih } 862e3ec7017SPing-Ke Shih 863e3ec7017SPing-Ke Shih cfg_target = get_phy_target(table->regs[headline_idx].addr); 864e3ec7017SPing-Ke Shih for (i = headline_size; i < table->n_regs; i++) { 865e3ec7017SPing-Ke Shih reg = &table->regs[i]; 866e3ec7017SPing-Ke Shih cond = get_phy_cond(reg->addr); 867e3ec7017SPing-Ke Shih switch (cond) { 868e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_IF: 869e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELIF: 870e3ec7017SPing-Ke Shih target = get_phy_target(reg->addr); 871e3ec7017SPing-Ke Shih break; 872e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_ELSE: 873e3ec7017SPing-Ke Shih is_matched = false; 874e3ec7017SPing-Ke Shih if (!target_found) { 875e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "failed to load CR %x/%x\n", 876e3ec7017SPing-Ke Shih reg->addr, reg->data); 877e3ec7017SPing-Ke Shih return; 878e3ec7017SPing-Ke Shih } 879e3ec7017SPing-Ke Shih break; 880e3ec7017SPing-Ke Shih case PHY_COND_BRANCH_END: 881e3ec7017SPing-Ke Shih is_matched = true; 882e3ec7017SPing-Ke Shih target_found = false; 883e3ec7017SPing-Ke Shih break; 884e3ec7017SPing-Ke Shih case PHY_COND_CHECK: 885e3ec7017SPing-Ke Shih if (target_found) { 886e3ec7017SPing-Ke Shih is_matched = false; 887e3ec7017SPing-Ke Shih break; 888e3ec7017SPing-Ke Shih } 889e3ec7017SPing-Ke Shih 890e3ec7017SPing-Ke Shih if (target == cfg_target) { 891e3ec7017SPing-Ke Shih is_matched = true; 892e3ec7017SPing-Ke Shih target_found = true; 893e3ec7017SPing-Ke Shih } else { 894e3ec7017SPing-Ke Shih is_matched = false; 895e3ec7017SPing-Ke Shih target_found = false; 896e3ec7017SPing-Ke Shih } 897e3ec7017SPing-Ke Shih break; 898e3ec7017SPing-Ke Shih default: 899e3ec7017SPing-Ke Shih if (is_matched) 900e3ec7017SPing-Ke Shih config(rtwdev, reg, rf_path, extra_data); 901e3ec7017SPing-Ke Shih break; 902e3ec7017SPing-Ke Shih } 903e3ec7017SPing-Ke Shih } 904e3ec7017SPing-Ke Shih } 905e3ec7017SPing-Ke Shih 906e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev) 907e3ec7017SPing-Ke Shih { 908e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 909e3ec7017SPing-Ke Shih const struct rtw89_phy_table *bb_table = chip->bb_table; 910e3ec7017SPing-Ke Shih 911e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL); 912e3ec7017SPing-Ke Shih rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0); 913e3ec7017SPing-Ke Shih rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0); 914e3ec7017SPing-Ke Shih } 915e3ec7017SPing-Ke Shih 916e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev) 917e3ec7017SPing-Ke Shih { 918e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, 0x8080, 0x4); 919e3ec7017SPing-Ke Shih udelay(1); 920e3ec7017SPing-Ke Shih return rtw89_phy_read32(rtwdev, 0x8080); 921e3ec7017SPing-Ke Shih } 922e3ec7017SPing-Ke Shih 923e3ec7017SPing-Ke Shih void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev) 924e3ec7017SPing-Ke Shih { 925e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 926e3ec7017SPing-Ke Shih const struct rtw89_phy_table *rf_table; 927e3ec7017SPing-Ke Shih struct rtw89_fw_h2c_rf_reg_info *rf_reg_info; 928e3ec7017SPing-Ke Shih u8 path; 929e3ec7017SPing-Ke Shih 930e3ec7017SPing-Ke Shih rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL); 931e3ec7017SPing-Ke Shih if (!rf_reg_info) 932e3ec7017SPing-Ke Shih return; 933e3ec7017SPing-Ke Shih 934e3ec7017SPing-Ke Shih for (path = RF_PATH_A; path < chip->rf_path_num; path++) { 935e3ec7017SPing-Ke Shih rf_reg_info->rf_path = path; 936e3ec7017SPing-Ke Shih rf_table = chip->rf_table[path]; 937e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, rf_table, rtw89_phy_config_rf_reg, 938e3ec7017SPing-Ke Shih (void *)rf_reg_info); 939e3ec7017SPing-Ke Shih if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info)) 940e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n", 941e3ec7017SPing-Ke Shih path); 942e3ec7017SPing-Ke Shih } 943e3ec7017SPing-Ke Shih kfree(rf_reg_info); 944e3ec7017SPing-Ke Shih } 945e3ec7017SPing-Ke Shih 946e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev) 947e3ec7017SPing-Ke Shih { 948e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 949e3ec7017SPing-Ke Shih const struct rtw89_phy_table *nctl_table; 950e3ec7017SPing-Ke Shih u32 val; 951e3ec7017SPing-Ke Shih int ret; 952e3ec7017SPing-Ke Shih 953e3ec7017SPing-Ke Shih /* IQK/DPK clock & reset */ 954e3ec7017SPing-Ke Shih rtw89_phy_write32_set(rtwdev, 0x0c60, 0x3); 955e3ec7017SPing-Ke Shih rtw89_phy_write32_set(rtwdev, 0x0c6c, 0x1); 956e3ec7017SPing-Ke Shih rtw89_phy_write32_set(rtwdev, 0x58ac, 0x8000000); 957e3ec7017SPing-Ke Shih rtw89_phy_write32_set(rtwdev, 0x78ac, 0x8000000); 958e3ec7017SPing-Ke Shih 959e3ec7017SPing-Ke Shih /* check 0x8080 */ 960e3ec7017SPing-Ke Shih rtw89_phy_write32(rtwdev, 0x8000, 0x8); 961e3ec7017SPing-Ke Shih 962e3ec7017SPing-Ke Shih ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10, 963e3ec7017SPing-Ke Shih 1000, false, rtwdev); 964e3ec7017SPing-Ke Shih if (ret) 965e3ec7017SPing-Ke Shih rtw89_err(rtwdev, "failed to poll nctl block\n"); 966e3ec7017SPing-Ke Shih 967e3ec7017SPing-Ke Shih nctl_table = chip->nctl_table; 968e3ec7017SPing-Ke Shih rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL); 969e3ec7017SPing-Ke Shih } 970e3ec7017SPing-Ke Shih 971e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr) 972e3ec7017SPing-Ke Shih { 973e3ec7017SPing-Ke Shih u32 phy_page = addr >> 8; 974e3ec7017SPing-Ke Shih u32 ofst = 0; 975e3ec7017SPing-Ke Shih 976e3ec7017SPing-Ke Shih switch (phy_page) { 977e3ec7017SPing-Ke Shih case 0x6: 978e3ec7017SPing-Ke Shih case 0x7: 979e3ec7017SPing-Ke Shih case 0x8: 980e3ec7017SPing-Ke Shih case 0x9: 981e3ec7017SPing-Ke Shih case 0xa: 982e3ec7017SPing-Ke Shih case 0xb: 983e3ec7017SPing-Ke Shih case 0xc: 984e3ec7017SPing-Ke Shih case 0xd: 985e3ec7017SPing-Ke Shih case 0x19: 986e3ec7017SPing-Ke Shih case 0x1a: 987e3ec7017SPing-Ke Shih case 0x1b: 988e3ec7017SPing-Ke Shih ofst = 0x2000; 989e3ec7017SPing-Ke Shih break; 990e3ec7017SPing-Ke Shih default: 991e3ec7017SPing-Ke Shih /* warning case */ 992e3ec7017SPing-Ke Shih ofst = 0; 993e3ec7017SPing-Ke Shih break; 994e3ec7017SPing-Ke Shih } 995e3ec7017SPing-Ke Shih 996e3ec7017SPing-Ke Shih if (phy_page >= 0x40 && phy_page <= 0x4f) 997e3ec7017SPing-Ke Shih ofst = 0x2000; 998e3ec7017SPing-Ke Shih 999e3ec7017SPing-Ke Shih return ofst; 1000e3ec7017SPing-Ke Shih } 1001e3ec7017SPing-Ke Shih 1002e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1003e3ec7017SPing-Ke Shih u32 data, enum rtw89_phy_idx phy_idx) 1004e3ec7017SPing-Ke Shih { 1005e3ec7017SPing-Ke Shih if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 1006e3ec7017SPing-Ke Shih addr += rtw89_phy0_phy1_offset(rtwdev, addr); 1007e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, addr, mask, data); 1008e3ec7017SPing-Ke Shih } 1009861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx); 1010e3ec7017SPing-Ke Shih 1011e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1012e3ec7017SPing-Ke Shih u32 val) 1013e3ec7017SPing-Ke Shih { 1014e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0); 1015e3ec7017SPing-Ke Shih 1016e3ec7017SPing-Ke Shih if (!rtwdev->dbcc_en) 1017e3ec7017SPing-Ke Shih return; 1018e3ec7017SPing-Ke Shih 1019e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1); 1020e3ec7017SPing-Ke Shih } 1021e3ec7017SPing-Ke Shih 1022e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev, 1023e3ec7017SPing-Ke Shih const struct rtw89_phy_reg3_tbl *tbl) 1024e3ec7017SPing-Ke Shih { 1025e3ec7017SPing-Ke Shih const struct rtw89_reg3_def *reg3; 1026e3ec7017SPing-Ke Shih int i; 1027e3ec7017SPing-Ke Shih 1028e3ec7017SPing-Ke Shih for (i = 0; i < tbl->size; i++) { 1029e3ec7017SPing-Ke Shih reg3 = &tbl->reg3[i]; 1030e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data); 1031e3ec7017SPing-Ke Shih } 1032e3ec7017SPing-Ke Shih } 1033861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl); 1034e3ec7017SPing-Ke Shih 1035e3ec7017SPing-Ke Shih const u8 rtw89_rs_idx_max[] = { 1036e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = RTW89_RATE_CCK_MAX, 1037e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = RTW89_RATE_OFDM_MAX, 1038e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = RTW89_RATE_MCS_MAX, 1039e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_MAX, 1040e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_MAX, 1041e3ec7017SPing-Ke Shih }; 1042861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_rs_idx_max); 1043e3ec7017SPing-Ke Shih 1044e3ec7017SPing-Ke Shih const u8 rtw89_rs_nss_max[] = { 1045e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = 1, 1046e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = 1, 1047e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = RTW89_NSS_MAX, 1048e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_MAX, 1049e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = 1, 1050e3ec7017SPing-Ke Shih }; 1051861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_rs_nss_max); 1052e3ec7017SPing-Ke Shih 1053e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = { 1054e3ec7017SPing-Ke Shih [RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck), 1055e3ec7017SPing-Ke Shih [RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm), 1056e3ec7017SPing-Ke Shih [RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs), 1057e3ec7017SPing-Ke Shih [RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm), 1058e3ec7017SPing-Ke Shih [RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset), 1059e3ec7017SPing-Ke Shih }; 1060e3ec7017SPing-Ke Shih 1061e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs]) 1062e3ec7017SPing-Ke Shih #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_max[rs] + (idx)) 1063e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \ 1064e3ec7017SPing-Ke Shih ((nss) < rtw89_rs_nss_max[rs] && (idx) < rtw89_rs_idx_max[rs]) 1065e3ec7017SPing-Ke Shih 1066e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev, 1067e3ec7017SPing-Ke Shih const struct rtw89_txpwr_table *tbl) 1068e3ec7017SPing-Ke Shih { 1069e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data; 1070e3ec7017SPing-Ke Shih const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size; 1071e3ec7017SPing-Ke Shih s8 *byr; 1072e3ec7017SPing-Ke Shih u32 data; 1073e3ec7017SPing-Ke Shih u8 i, idx; 1074e3ec7017SPing-Ke Shih 1075e3ec7017SPing-Ke Shih for (; cfg < end; cfg++) { 1076e3ec7017SPing-Ke Shih byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]); 1077e3ec7017SPing-Ke Shih data = cfg->data; 1078e3ec7017SPing-Ke Shih 1079e3ec7017SPing-Ke Shih for (i = 0; i < cfg->len; i++, data >>= 8) { 1080e3ec7017SPing-Ke Shih idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i)); 1081e3ec7017SPing-Ke Shih byr[idx] = (s8)(data & 0xff); 1082e3ec7017SPing-Ke Shih } 1083e3ec7017SPing-Ke Shih } 1084e3ec7017SPing-Ke Shih } 1085861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate); 1086e3ec7017SPing-Ke Shih 1087e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf) \ 1088e3ec7017SPing-Ke Shih ({ \ 1089e3ec7017SPing-Ke Shih const struct rtw89_chip_info *__c = (rtwdev)->chip; \ 1090e3ec7017SPing-Ke Shih (txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac); \ 1091e3ec7017SPing-Ke Shih }) 1092e3ec7017SPing-Ke Shih 1093e3ec7017SPing-Ke Shih s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, 1094e3ec7017SPing-Ke Shih const struct rtw89_rate_desc *rate_desc) 1095e3ec7017SPing-Ke Shih { 1096e3ec7017SPing-Ke Shih enum rtw89_band band = rtwdev->hal.current_band_type; 1097e3ec7017SPing-Ke Shih s8 *byr; 1098e3ec7017SPing-Ke Shih u8 idx; 1099e3ec7017SPing-Ke Shih 1100e3ec7017SPing-Ke Shih if (rate_desc->rs == RTW89_RS_CCK) 1101e3ec7017SPing-Ke Shih band = RTW89_BAND_2G; 1102e3ec7017SPing-Ke Shih 1103e3ec7017SPing-Ke Shih if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) { 1104e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 1105e3ec7017SPing-Ke Shih "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n", 1106e3ec7017SPing-Ke Shih rate_desc->rs, rate_desc->nss, rate_desc->idx); 1107e3ec7017SPing-Ke Shih 1108e3ec7017SPing-Ke Shih return 0; 1109e3ec7017SPing-Ke Shih } 1110e3ec7017SPing-Ke Shih 1111e3ec7017SPing-Ke Shih byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]); 1112e3ec7017SPing-Ke Shih idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx); 1113e3ec7017SPing-Ke Shih 1114e3ec7017SPing-Ke Shih return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]); 1115e3ec7017SPing-Ke Shih } 1116861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_byrate); 1117e3ec7017SPing-Ke Shih 1118ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g) 1119e3ec7017SPing-Ke Shih { 1120ac74f016SZong-Zhe Yang switch (channel_6g) { 1121ac74f016SZong-Zhe Yang case 1 ... 29: 1122ac74f016SZong-Zhe Yang return (channel_6g - 1) / 2; 1123ac74f016SZong-Zhe Yang case 33 ... 61: 1124ac74f016SZong-Zhe Yang return (channel_6g - 3) / 2; 1125ac74f016SZong-Zhe Yang case 65 ... 93: 1126ac74f016SZong-Zhe Yang return (channel_6g - 5) / 2; 1127ac74f016SZong-Zhe Yang case 97 ... 125: 1128ac74f016SZong-Zhe Yang return (channel_6g - 7) / 2; 1129ac74f016SZong-Zhe Yang case 129 ... 157: 1130ac74f016SZong-Zhe Yang return (channel_6g - 9) / 2; 1131ac74f016SZong-Zhe Yang case 161 ... 189: 1132ac74f016SZong-Zhe Yang return (channel_6g - 11) / 2; 1133ac74f016SZong-Zhe Yang case 193 ... 221: 1134ac74f016SZong-Zhe Yang return (channel_6g - 13) / 2; 1135ac74f016SZong-Zhe Yang case 225 ... 253: 1136ac74f016SZong-Zhe Yang return (channel_6g - 15) / 2; 1137ac74f016SZong-Zhe Yang default: 1138ac74f016SZong-Zhe Yang rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g); 1139ac74f016SZong-Zhe Yang return 0; 1140ac74f016SZong-Zhe Yang } 1141ac74f016SZong-Zhe Yang } 1142ac74f016SZong-Zhe Yang 1143ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel) 1144ac74f016SZong-Zhe Yang { 1145ac74f016SZong-Zhe Yang if (band == RTW89_BAND_6G) 1146ac74f016SZong-Zhe Yang return rtw89_channel_6g_to_idx(rtwdev, channel); 1147ac74f016SZong-Zhe Yang 1148e3ec7017SPing-Ke Shih switch (channel) { 1149e3ec7017SPing-Ke Shih case 1 ... 14: 1150e3ec7017SPing-Ke Shih return channel - 1; 1151e3ec7017SPing-Ke Shih case 36 ... 64: 1152e3ec7017SPing-Ke Shih return (channel - 36) / 2; 1153e3ec7017SPing-Ke Shih case 100 ... 144: 1154e3ec7017SPing-Ke Shih return ((channel - 100) / 2) + 15; 1155e3ec7017SPing-Ke Shih case 149 ... 177: 1156e3ec7017SPing-Ke Shih return ((channel - 149) / 2) + 38; 1157e3ec7017SPing-Ke Shih default: 1158e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown channel: %d\n", channel); 1159e3ec7017SPing-Ke Shih return 0; 1160e3ec7017SPing-Ke Shih } 1161e3ec7017SPing-Ke Shih } 1162e3ec7017SPing-Ke Shih 1163e3ec7017SPing-Ke Shih s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, 1164e3ec7017SPing-Ke Shih u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch) 1165e3ec7017SPing-Ke Shih { 1166e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1167e3ec7017SPing-Ke Shih u8 band = rtwdev->hal.current_band_type; 1168ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1169e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 1170e3ec7017SPing-Ke Shih s8 lmt = 0, sar; 1171e3ec7017SPing-Ke Shih 1172e3ec7017SPing-Ke Shih switch (band) { 1173e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 1174e3ec7017SPing-Ke Shih lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf][regd][ch_idx]; 117554257714SZong-Zhe Yang if (!lmt) 117654257714SZong-Zhe Yang lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf] 117754257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1178e3ec7017SPing-Ke Shih break; 1179e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 1180e3ec7017SPing-Ke Shih lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf][regd][ch_idx]; 118154257714SZong-Zhe Yang if (!lmt) 118254257714SZong-Zhe Yang lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf] 118354257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1184e3ec7017SPing-Ke Shih break; 1185ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 1186ac74f016SZong-Zhe Yang lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf][regd][ch_idx]; 1187ac74f016SZong-Zhe Yang if (!lmt) 1188ac74f016SZong-Zhe Yang lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf] 1189ac74f016SZong-Zhe Yang [RTW89_WW][ch_idx]; 1190ac74f016SZong-Zhe Yang break; 1191e3ec7017SPing-Ke Shih default: 1192e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1193e3ec7017SPing-Ke Shih return 0; 1194e3ec7017SPing-Ke Shih } 1195e3ec7017SPing-Ke Shih 1196e3ec7017SPing-Ke Shih lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt); 1197e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1198e3ec7017SPing-Ke Shih 1199e3ec7017SPing-Ke Shih return min(lmt, sar); 1200e3ec7017SPing-Ke Shih } 1201861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit); 1202e3ec7017SPing-Ke Shih 1203e3ec7017SPing-Ke Shih #define __fill_txpwr_limit_nonbf_bf(ptr, bw, ntx, rs, ch) \ 1204e3ec7017SPing-Ke Shih do { \ 1205e3ec7017SPing-Ke Shih u8 __i; \ 1206e3ec7017SPing-Ke Shih for (__i = 0; __i < RTW89_BF_NUM; __i++) \ 1207e3ec7017SPing-Ke Shih ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev, \ 1208e3ec7017SPing-Ke Shih bw, ntx, \ 1209e3ec7017SPing-Ke Shih rs, __i, \ 1210e3ec7017SPing-Ke Shih (ch)); \ 1211e3ec7017SPing-Ke Shih } while (0) 1212e3ec7017SPing-Ke Shih 1213e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev, 1214e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 1215e3ec7017SPing-Ke Shih u8 ntx, u8 ch) 1216e3ec7017SPing-Ke Shih { 1217e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, RTW89_CHANNEL_WIDTH_20, 1218e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 1219e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, RTW89_CHANNEL_WIDTH_40, 1220e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 1221e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20, 1222e3ec7017SPing-Ke Shih ntx, RTW89_RS_OFDM, ch); 1223e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20, 1224e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1225e3ec7017SPing-Ke Shih } 1226e3ec7017SPing-Ke Shih 1227e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev, 1228e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 122994b70cafSZong-Zhe Yang u8 ntx, u8 ch, u8 pri_ch) 1230e3ec7017SPing-Ke Shih { 1231e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, RTW89_CHANNEL_WIDTH_20, 1232e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch - 2); 1233e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, RTW89_CHANNEL_WIDTH_40, 1234e3ec7017SPing-Ke Shih ntx, RTW89_RS_CCK, ch); 1235e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20, 123694b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 1237e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20, 1238e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 1239e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20, 1240e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 1241e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40, 1242e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1243e3ec7017SPing-Ke Shih } 1244e3ec7017SPing-Ke Shih 1245e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev, 1246e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 124794b70cafSZong-Zhe Yang u8 ntx, u8 ch, u8 pri_ch) 1248e3ec7017SPing-Ke Shih { 1249e3ec7017SPing-Ke Shih s8 val_0p5_n[RTW89_BF_NUM]; 1250e3ec7017SPing-Ke Shih s8 val_0p5_p[RTW89_BF_NUM]; 1251e3ec7017SPing-Ke Shih u8 i; 1252e3ec7017SPing-Ke Shih 1253e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20, 125494b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 1255e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20, 1256e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 6); 1257e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20, 1258e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 2); 1259e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], RTW89_CHANNEL_WIDTH_20, 1260e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 2); 1261e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], RTW89_CHANNEL_WIDTH_20, 1262e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 6); 1263e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40, 1264e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 1265e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], RTW89_CHANNEL_WIDTH_40, 1266e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 1267e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], RTW89_CHANNEL_WIDTH_80, 1268e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch); 1269e3ec7017SPing-Ke Shih 1270e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(val_0p5_n, RTW89_CHANNEL_WIDTH_40, 1271e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch - 4); 1272e3ec7017SPing-Ke Shih __fill_txpwr_limit_nonbf_bf(val_0p5_p, RTW89_CHANNEL_WIDTH_40, 1273e3ec7017SPing-Ke Shih ntx, RTW89_RS_MCS, ch + 4); 1274e3ec7017SPing-Ke Shih 1275e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_BF_NUM; i++) 1276e3ec7017SPing-Ke Shih lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 1277e3ec7017SPing-Ke Shih } 1278e3ec7017SPing-Ke Shih 127994b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev, 128094b70cafSZong-Zhe Yang struct rtw89_txpwr_limit *lmt, 128194b70cafSZong-Zhe Yang u8 ntx, u8 ch, u8 pri_ch) 128294b70cafSZong-Zhe Yang { 128394b70cafSZong-Zhe Yang s8 val_0p5_n[RTW89_BF_NUM]; 128494b70cafSZong-Zhe Yang s8 val_0p5_p[RTW89_BF_NUM]; 128594b70cafSZong-Zhe Yang s8 val_2p5_n[RTW89_BF_NUM]; 128694b70cafSZong-Zhe Yang s8 val_2p5_p[RTW89_BF_NUM]; 128794b70cafSZong-Zhe Yang u8 i; 128894b70cafSZong-Zhe Yang 128994b70cafSZong-Zhe Yang /* fill ofdm section */ 129094b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20, 129194b70cafSZong-Zhe Yang ntx, RTW89_RS_OFDM, pri_ch); 129294b70cafSZong-Zhe Yang 129394b70cafSZong-Zhe Yang /* fill mcs 20m section */ 129494b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20, 129594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 14); 129694b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20, 129794b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 10); 129894b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], RTW89_CHANNEL_WIDTH_20, 129994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 6); 130094b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], RTW89_CHANNEL_WIDTH_20, 130194b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 2); 130294b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], RTW89_CHANNEL_WIDTH_20, 130394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 2); 130494b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], RTW89_CHANNEL_WIDTH_20, 130594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 6); 130694b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], RTW89_CHANNEL_WIDTH_20, 130794b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 10); 130894b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], RTW89_CHANNEL_WIDTH_20, 130994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 14); 131094b70cafSZong-Zhe Yang 131194b70cafSZong-Zhe Yang /* fill mcs 40m section */ 131294b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40, 131394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 12); 131494b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], RTW89_CHANNEL_WIDTH_40, 131594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 131694b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], RTW89_CHANNEL_WIDTH_40, 131794b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 131894b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], RTW89_CHANNEL_WIDTH_40, 131994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 12); 132094b70cafSZong-Zhe Yang 132194b70cafSZong-Zhe Yang /* fill mcs 80m section */ 132294b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], RTW89_CHANNEL_WIDTH_80, 132394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 132494b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], RTW89_CHANNEL_WIDTH_80, 132594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 132694b70cafSZong-Zhe Yang 132794b70cafSZong-Zhe Yang /* fill mcs 160m section */ 132894b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, RTW89_CHANNEL_WIDTH_160, 132994b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch); 133094b70cafSZong-Zhe Yang 133194b70cafSZong-Zhe Yang /* fill mcs 40m 0p5 section */ 133294b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_n, RTW89_CHANNEL_WIDTH_40, 133394b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 4); 133494b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_0p5_p, RTW89_CHANNEL_WIDTH_40, 133594b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 4); 133694b70cafSZong-Zhe Yang 133794b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 133894b70cafSZong-Zhe Yang lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 133994b70cafSZong-Zhe Yang 134094b70cafSZong-Zhe Yang /* fill mcs 40m 2p5 section */ 134194b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_n, RTW89_CHANNEL_WIDTH_40, 134294b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch - 8); 134394b70cafSZong-Zhe Yang __fill_txpwr_limit_nonbf_bf(val_2p5_p, RTW89_CHANNEL_WIDTH_40, 134494b70cafSZong-Zhe Yang ntx, RTW89_RS_MCS, ch + 8); 134594b70cafSZong-Zhe Yang 134694b70cafSZong-Zhe Yang for (i = 0; i < RTW89_BF_NUM; i++) 134794b70cafSZong-Zhe Yang lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]); 134894b70cafSZong-Zhe Yang } 134994b70cafSZong-Zhe Yang 1350e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev, 1351e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit *lmt, 1352e3ec7017SPing-Ke Shih u8 ntx) 1353e3ec7017SPing-Ke Shih { 135494b70cafSZong-Zhe Yang u8 pri_ch = rtwdev->hal.current_primary_channel; 1355e3ec7017SPing-Ke Shih u8 ch = rtwdev->hal.current_channel; 1356e3ec7017SPing-Ke Shih u8 bw = rtwdev->hal.current_band_width; 1357e3ec7017SPing-Ke Shih 1358e3ec7017SPing-Ke Shih memset(lmt, 0, sizeof(*lmt)); 1359e3ec7017SPing-Ke Shih 1360e3ec7017SPing-Ke Shih switch (bw) { 1361e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 1362e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, ntx, ch); 1363e3ec7017SPing-Ke Shih break; 1364e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 136594b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, ntx, ch, pri_ch); 1366e3ec7017SPing-Ke Shih break; 1367e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 136894b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, ntx, ch, pri_ch); 136994b70cafSZong-Zhe Yang break; 137094b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 137194b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, ntx, ch, pri_ch); 1372e3ec7017SPing-Ke Shih break; 1373e3ec7017SPing-Ke Shih } 1374e3ec7017SPing-Ke Shih } 1375861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_fill_txpwr_limit); 1376e3ec7017SPing-Ke Shih 1377e3ec7017SPing-Ke Shih static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, 1378e3ec7017SPing-Ke Shih u8 ru, u8 ntx, u8 ch) 1379e3ec7017SPing-Ke Shih { 1380e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 1381e3ec7017SPing-Ke Shih u8 band = rtwdev->hal.current_band_type; 1382ac74f016SZong-Zhe Yang u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1383e3ec7017SPing-Ke Shih u8 regd = rtw89_regd_get(rtwdev, band); 1384e3ec7017SPing-Ke Shih s8 lmt_ru = 0, sar; 1385e3ec7017SPing-Ke Shih 1386e3ec7017SPing-Ke Shih switch (band) { 1387e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 1388e3ec7017SPing-Ke Shih lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx][regd][ch_idx]; 138954257714SZong-Zhe Yang if (!lmt_ru) 139054257714SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx] 139154257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1392e3ec7017SPing-Ke Shih break; 1393e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 1394e3ec7017SPing-Ke Shih lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx][regd][ch_idx]; 139554257714SZong-Zhe Yang if (!lmt_ru) 139654257714SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx] 139754257714SZong-Zhe Yang [RTW89_WW][ch_idx]; 1398e3ec7017SPing-Ke Shih break; 1399ac74f016SZong-Zhe Yang case RTW89_BAND_6G: 1400ac74f016SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx][regd][ch_idx]; 1401ac74f016SZong-Zhe Yang if (!lmt_ru) 1402ac74f016SZong-Zhe Yang lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx] 1403ac74f016SZong-Zhe Yang [RTW89_WW][ch_idx]; 1404ac74f016SZong-Zhe Yang break; 1405e3ec7017SPing-Ke Shih default: 1406e3ec7017SPing-Ke Shih rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1407e3ec7017SPing-Ke Shih return 0; 1408e3ec7017SPing-Ke Shih } 1409e3ec7017SPing-Ke Shih 1410e3ec7017SPing-Ke Shih lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru); 1411e3ec7017SPing-Ke Shih sar = rtw89_query_sar(rtwdev); 1412e3ec7017SPing-Ke Shih 1413e3ec7017SPing-Ke Shih return min(lmt_ru, sar); 1414e3ec7017SPing-Ke Shih } 1415e3ec7017SPing-Ke Shih 1416e3ec7017SPing-Ke Shih static void 1417e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev, 1418e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 1419e3ec7017SPing-Ke Shih u8 ntx, u8 ch) 1420e3ec7017SPing-Ke Shih { 1421e3ec7017SPing-Ke Shih lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1422e3ec7017SPing-Ke Shih ntx, ch); 1423e3ec7017SPing-Ke Shih lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1424e3ec7017SPing-Ke Shih ntx, ch); 1425e3ec7017SPing-Ke Shih lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1426e3ec7017SPing-Ke Shih ntx, ch); 1427e3ec7017SPing-Ke Shih } 1428e3ec7017SPing-Ke Shih 1429e3ec7017SPing-Ke Shih static void 1430e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev, 1431e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 1432e3ec7017SPing-Ke Shih u8 ntx, u8 ch) 1433e3ec7017SPing-Ke Shih { 1434e3ec7017SPing-Ke Shih lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1435e3ec7017SPing-Ke Shih ntx, ch - 2); 1436e3ec7017SPing-Ke Shih lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1437e3ec7017SPing-Ke Shih ntx, ch + 2); 1438e3ec7017SPing-Ke Shih lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1439e3ec7017SPing-Ke Shih ntx, ch - 2); 1440e3ec7017SPing-Ke Shih lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1441e3ec7017SPing-Ke Shih ntx, ch + 2); 1442e3ec7017SPing-Ke Shih lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1443e3ec7017SPing-Ke Shih ntx, ch - 2); 1444e3ec7017SPing-Ke Shih lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1445e3ec7017SPing-Ke Shih ntx, ch + 2); 1446e3ec7017SPing-Ke Shih } 1447e3ec7017SPing-Ke Shih 1448e3ec7017SPing-Ke Shih static void 1449e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev, 1450e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 1451e3ec7017SPing-Ke Shih u8 ntx, u8 ch) 1452e3ec7017SPing-Ke Shih { 1453e3ec7017SPing-Ke Shih lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1454e3ec7017SPing-Ke Shih ntx, ch - 6); 1455e3ec7017SPing-Ke Shih lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1456e3ec7017SPing-Ke Shih ntx, ch - 2); 1457e3ec7017SPing-Ke Shih lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1458e3ec7017SPing-Ke Shih ntx, ch + 2); 1459e3ec7017SPing-Ke Shih lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26, 1460e3ec7017SPing-Ke Shih ntx, ch + 6); 1461e3ec7017SPing-Ke Shih lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1462e3ec7017SPing-Ke Shih ntx, ch - 6); 1463e3ec7017SPing-Ke Shih lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1464e3ec7017SPing-Ke Shih ntx, ch - 2); 1465e3ec7017SPing-Ke Shih lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1466e3ec7017SPing-Ke Shih ntx, ch + 2); 1467e3ec7017SPing-Ke Shih lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52, 1468e3ec7017SPing-Ke Shih ntx, ch + 6); 1469e3ec7017SPing-Ke Shih lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1470e3ec7017SPing-Ke Shih ntx, ch - 6); 1471e3ec7017SPing-Ke Shih lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1472e3ec7017SPing-Ke Shih ntx, ch - 2); 1473e3ec7017SPing-Ke Shih lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1474e3ec7017SPing-Ke Shih ntx, ch + 2); 1475e3ec7017SPing-Ke Shih lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106, 1476e3ec7017SPing-Ke Shih ntx, ch + 6); 1477e3ec7017SPing-Ke Shih } 1478e3ec7017SPing-Ke Shih 147994b70cafSZong-Zhe Yang static void 148094b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev, 148194b70cafSZong-Zhe Yang struct rtw89_txpwr_limit_ru *lmt_ru, 148294b70cafSZong-Zhe Yang u8 ntx, u8 ch) 148394b70cafSZong-Zhe Yang { 148494b70cafSZong-Zhe Yang static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 }; 148594b70cafSZong-Zhe Yang int i; 148694b70cafSZong-Zhe Yang 148794b70cafSZong-Zhe Yang static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM); 148894b70cafSZong-Zhe Yang for (i = 0; i < RTW89_RU_SEC_NUM; i++) { 148994b70cafSZong-Zhe Yang lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, 149094b70cafSZong-Zhe Yang RTW89_RU26, 149194b70cafSZong-Zhe Yang ntx, 149294b70cafSZong-Zhe Yang ch + ofst[i]); 149394b70cafSZong-Zhe Yang lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, 149494b70cafSZong-Zhe Yang RTW89_RU52, 149594b70cafSZong-Zhe Yang ntx, 149694b70cafSZong-Zhe Yang ch + ofst[i]); 149794b70cafSZong-Zhe Yang lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, 149894b70cafSZong-Zhe Yang RTW89_RU106, 149994b70cafSZong-Zhe Yang ntx, 150094b70cafSZong-Zhe Yang ch + ofst[i]); 150194b70cafSZong-Zhe Yang } 150294b70cafSZong-Zhe Yang } 150394b70cafSZong-Zhe Yang 1504e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev, 1505e3ec7017SPing-Ke Shih struct rtw89_txpwr_limit_ru *lmt_ru, 1506e3ec7017SPing-Ke Shih u8 ntx) 1507e3ec7017SPing-Ke Shih { 1508e3ec7017SPing-Ke Shih u8 ch = rtwdev->hal.current_channel; 1509e3ec7017SPing-Ke Shih u8 bw = rtwdev->hal.current_band_width; 1510e3ec7017SPing-Ke Shih 1511e3ec7017SPing-Ke Shih memset(lmt_ru, 0, sizeof(*lmt_ru)); 1512e3ec7017SPing-Ke Shih 1513e3ec7017SPing-Ke Shih switch (bw) { 1514e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 1515e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, ntx, ch); 1516e3ec7017SPing-Ke Shih break; 1517e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 1518e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, ntx, ch); 1519e3ec7017SPing-Ke Shih break; 1520e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 1521e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, ntx, ch); 1522e3ec7017SPing-Ke Shih break; 152394b70cafSZong-Zhe Yang case RTW89_CHANNEL_WIDTH_160: 152494b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, ntx, ch); 152594b70cafSZong-Zhe Yang break; 1526e3ec7017SPing-Ke Shih } 1527e3ec7017SPing-Ke Shih } 1528861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_fill_txpwr_limit_ru); 1529e3ec7017SPing-Ke Shih 1530e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data { 1531e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 1532e3ec7017SPing-Ke Shih struct sk_buff *c2h; 1533e3ec7017SPing-Ke Shih }; 1534e3ec7017SPing-Ke Shih 1535e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta) 1536e3ec7017SPing-Ke Shih { 1537e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data; 1538e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = ra_data->rtwdev; 1539e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 1540e3ec7017SPing-Ke Shih struct rtw89_ra_report *ra_report = &rtwsta->ra_report; 1541e3ec7017SPing-Ke Shih struct sk_buff *c2h = ra_data->c2h; 1542e3ec7017SPing-Ke Shih u8 mode, rate, bw, giltf, mac_id; 1543e3ec7017SPing-Ke Shih 1544e3ec7017SPing-Ke Shih mac_id = RTW89_GET_PHY_C2H_RA_RPT_MACID(c2h->data); 1545e3ec7017SPing-Ke Shih if (mac_id != rtwsta->mac_id) 1546e3ec7017SPing-Ke Shih return; 1547e3ec7017SPing-Ke Shih 1548e3ec7017SPing-Ke Shih memset(ra_report, 0, sizeof(*ra_report)); 1549e3ec7017SPing-Ke Shih 1550e3ec7017SPing-Ke Shih rate = RTW89_GET_PHY_C2H_RA_RPT_MCSNSS(c2h->data); 1551e3ec7017SPing-Ke Shih bw = RTW89_GET_PHY_C2H_RA_RPT_BW(c2h->data); 1552e3ec7017SPing-Ke Shih giltf = RTW89_GET_PHY_C2H_RA_RPT_GILTF(c2h->data); 1553e3ec7017SPing-Ke Shih mode = RTW89_GET_PHY_C2H_RA_RPT_MD_SEL(c2h->data); 1554e3ec7017SPing-Ke Shih 1555e3ec7017SPing-Ke Shih switch (mode) { 1556e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_LEGACY: 1557e3ec7017SPing-Ke Shih ra_report->txrate.legacy = rtw89_ra_report_to_bitrate(rtwdev, rate); 1558e3ec7017SPing-Ke Shih break; 1559e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HT: 1560e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS; 1561e3ec7017SPing-Ke Shih if (rtwdev->fw.old_ht_ra_format) 1562e3ec7017SPing-Ke Shih rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate), 1563e3ec7017SPing-Ke Shih FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate)); 1564e3ec7017SPing-Ke Shih else 1565e3ec7017SPing-Ke Shih rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate); 1566e3ec7017SPing-Ke Shih ra_report->txrate.mcs = rate; 1567e3ec7017SPing-Ke Shih if (giltf) 1568e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 1569e3ec7017SPing-Ke Shih break; 1570e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_VHT: 1571e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS; 1572e3ec7017SPing-Ke Shih ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate); 1573e3ec7017SPing-Ke Shih ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1; 1574e3ec7017SPing-Ke Shih if (giltf) 1575e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 1576e3ec7017SPing-Ke Shih break; 1577e3ec7017SPing-Ke Shih case RTW89_RA_RPT_MODE_HE: 1578e3ec7017SPing-Ke Shih ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS; 1579e3ec7017SPing-Ke Shih ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate); 1580e3ec7017SPing-Ke Shih ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1; 1581e3ec7017SPing-Ke Shih if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08) 1582e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8; 1583e3ec7017SPing-Ke Shih else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16) 1584e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6; 1585e3ec7017SPing-Ke Shih else 1586e3ec7017SPing-Ke Shih ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2; 1587e3ec7017SPing-Ke Shih break; 1588e3ec7017SPing-Ke Shih } 1589e3ec7017SPing-Ke Shih 1590167044afSPing-Ke Shih ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw); 1591e3ec7017SPing-Ke Shih ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate); 1592e3ec7017SPing-Ke Shih ra_report->hw_rate = FIELD_PREP(RTW89_HW_RATE_MASK_MOD, mode) | 1593e3ec7017SPing-Ke Shih FIELD_PREP(RTW89_HW_RATE_MASK_VAL, rate); 1594e3ec7017SPing-Ke Shih sta->max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report); 1595e3ec7017SPing-Ke Shih rtwsta->max_agg_wait = sta->max_rc_amsdu_len / 1500 - 1; 1596e3ec7017SPing-Ke Shih } 1597e3ec7017SPing-Ke Shih 1598e3ec7017SPing-Ke Shih static void 1599e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 1600e3ec7017SPing-Ke Shih { 1601e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data ra_data; 1602e3ec7017SPing-Ke Shih 1603e3ec7017SPing-Ke Shih ra_data.rtwdev = rtwdev; 1604e3ec7017SPing-Ke Shih ra_data.c2h = c2h; 1605e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 1606e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt_iter, 1607e3ec7017SPing-Ke Shih &ra_data); 1608e3ec7017SPing-Ke Shih } 1609e3ec7017SPing-Ke Shih 1610e3ec7017SPing-Ke Shih static 1611e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev, 1612e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = { 1613e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt, 1614e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL, 1615e3ec7017SPing-Ke Shih [RTW89_PHY_C2H_FUNC_TXSTS] = NULL, 1616e3ec7017SPing-Ke Shih }; 1617e3ec7017SPing-Ke Shih 1618e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb, 1619e3ec7017SPing-Ke Shih u32 len, u8 class, u8 func) 1620e3ec7017SPing-Ke Shih { 1621e3ec7017SPing-Ke Shih void (*handler)(struct rtw89_dev *rtwdev, 1622e3ec7017SPing-Ke Shih struct sk_buff *c2h, u32 len) = NULL; 1623e3ec7017SPing-Ke Shih 1624e3ec7017SPing-Ke Shih switch (class) { 1625e3ec7017SPing-Ke Shih case RTW89_PHY_C2H_CLASS_RA: 1626e3ec7017SPing-Ke Shih if (func < RTW89_PHY_C2H_FUNC_RA_MAX) 1627e3ec7017SPing-Ke Shih handler = rtw89_phy_c2h_ra_handler[func]; 1628e3ec7017SPing-Ke Shih break; 1629e3ec7017SPing-Ke Shih default: 1630e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d not support\n", class); 1631e3ec7017SPing-Ke Shih return; 1632e3ec7017SPing-Ke Shih } 1633e3ec7017SPing-Ke Shih if (!handler) { 1634e3ec7017SPing-Ke Shih rtw89_info(rtwdev, "c2h class %d func %d not support\n", class, 1635e3ec7017SPing-Ke Shih func); 1636e3ec7017SPing-Ke Shih return; 1637e3ec7017SPing-Ke Shih } 1638e3ec7017SPing-Ke Shih handler(rtwdev, skb, len); 1639e3ec7017SPing-Ke Shih } 1640e3ec7017SPing-Ke Shih 1641e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo) 1642e3ec7017SPing-Ke Shih { 1643e3ec7017SPing-Ke Shih u32 reg_mask; 1644e3ec7017SPing-Ke Shih 1645e3ec7017SPing-Ke Shih if (sc_xo) 1646e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XO_MASK; 1647e3ec7017SPing-Ke Shih else 1648e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XI_MASK; 1649e3ec7017SPing-Ke Shih 1650e3ec7017SPing-Ke Shih return (u8)rtw89_read32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask); 1651e3ec7017SPing-Ke Shih } 1652e3ec7017SPing-Ke Shih 1653e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo, 1654e3ec7017SPing-Ke Shih u8 val) 1655e3ec7017SPing-Ke Shih { 1656e3ec7017SPing-Ke Shih u32 reg_mask; 1657e3ec7017SPing-Ke Shih 1658e3ec7017SPing-Ke Shih if (sc_xo) 1659e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XO_MASK; 1660e3ec7017SPing-Ke Shih else 1661e3ec7017SPing-Ke Shih reg_mask = B_AX_XTAL_SC_XI_MASK; 1662e3ec7017SPing-Ke Shih 1663e3ec7017SPing-Ke Shih rtw89_write32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask, val); 1664e3ec7017SPing-Ke Shih } 1665e3ec7017SPing-Ke Shih 1666e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev, 1667e3ec7017SPing-Ke Shih u8 crystal_cap, bool force) 1668e3ec7017SPing-Ke Shih { 1669e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1670e3ec7017SPing-Ke Shih u8 sc_xi_val, sc_xo_val; 1671e3ec7017SPing-Ke Shih 1672e3ec7017SPing-Ke Shih if (!force && cfo->crystal_cap == crystal_cap) 1673e3ec7017SPing-Ke Shih return; 1674e3ec7017SPing-Ke Shih crystal_cap = clamp_t(u8, crystal_cap, 0, 127); 1675e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap); 1676e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap); 1677e3ec7017SPing-Ke Shih sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true); 1678e3ec7017SPing-Ke Shih sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false); 1679e3ec7017SPing-Ke Shih cfo->crystal_cap = sc_xi_val; 1680e3ec7017SPing-Ke Shih cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap); 1681e3ec7017SPing-Ke Shih 1682e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val); 1683e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val); 1684e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n", 1685e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 1686e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n"); 1687e3ec7017SPing-Ke Shih } 1688e3ec7017SPing-Ke Shih 1689e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev) 1690e3ec7017SPing-Ke Shih { 1691e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1692e3ec7017SPing-Ke Shih u8 cap; 1693e3ec7017SPing-Ke Shih 1694e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK; 1695e3ec7017SPing-Ke Shih cfo->is_adjust = false; 1696e3ec7017SPing-Ke Shih if (cfo->crystal_cap == cfo->def_x_cap) 1697e3ec7017SPing-Ke Shih return; 1698e3ec7017SPing-Ke Shih cap = cfo->crystal_cap; 1699e3ec7017SPing-Ke Shih cap += (cap > cfo->def_x_cap ? -1 : 1); 1700e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false); 1701e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1702e3ec7017SPing-Ke Shih "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap, 1703e3ec7017SPing-Ke Shih cfo->def_x_cap); 1704e3ec7017SPing-Ke Shih } 1705e3ec7017SPing-Ke Shih 1706e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo) 1707e3ec7017SPing-Ke Shih { 1708*b7379148SYuan-Han Zhang const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp; 1709e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 1710e3ec7017SPing-Ke Shih s32 cfo_avg_312; 1711*b7379148SYuan-Han Zhang s32 dcfo_comp_val; 1712*b7379148SYuan-Han Zhang u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft; 1713e3ec7017SPing-Ke Shih int sign; 1714e3ec7017SPing-Ke Shih 1715e3ec7017SPing-Ke Shih if (!is_linked) { 1716e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n", 1717e3ec7017SPing-Ke Shih is_linked); 1718e3ec7017SPing-Ke Shih return; 1719e3ec7017SPing-Ke Shih } 1720e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo); 1721e3ec7017SPing-Ke Shih if (curr_cfo == 0) 1722e3ec7017SPing-Ke Shih return; 1723*b7379148SYuan-Han Zhang dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO); 1724e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 1725*b7379148SYuan-Han Zhang cfo_avg_312 = (curr_cfo << dcfo_comp_sft) / 5 + sign * dcfo_comp_val; 1726e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: avg_cfo=%d\n", cfo_avg_312); 1727e3ec7017SPing-Ke Shih if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV) 1728e3ec7017SPing-Ke Shih cfo_avg_312 = -cfo_avg_312; 1729*b7379148SYuan-Han Zhang rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask, 1730e3ec7017SPing-Ke Shih cfo_avg_312); 1731e3ec7017SPing-Ke Shih } 1732e3ec7017SPing-Ke Shih 1733e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev) 1734e3ec7017SPing-Ke Shih { 1735e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1); 1736e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8); 1737e3ec7017SPing-Ke Shih rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK); 1738e3ec7017SPing-Ke Shih } 1739e3ec7017SPing-Ke Shih 1740e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev) 1741e3ec7017SPing-Ke Shih { 1742e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1743e3ec7017SPing-Ke Shih struct rtw89_efuse *efuse = &rtwdev->efuse; 1744e3ec7017SPing-Ke Shih 1745e3ec7017SPing-Ke Shih cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK; 1746e3ec7017SPing-Ke Shih cfo->crystal_cap = cfo->crystal_cap_default; 1747e3ec7017SPing-Ke Shih cfo->def_x_cap = cfo->crystal_cap; 1748a9e06f2eSYi-Tang Chiu cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f); 1749a9e06f2eSYi-Tang Chiu cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1); 1750e3ec7017SPing-Ke Shih cfo->is_adjust = false; 1751a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 1752e3ec7017SPing-Ke Shih cfo->x_cap_ofst = 0; 1753a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 1754e3ec7017SPing-Ke Shih cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE; 1755e3ec7017SPing-Ke Shih cfo->apply_compensation = false; 1756e3ec7017SPing-Ke Shih cfo->residual_cfo_acc = 0; 1757e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n", 1758e3ec7017SPing-Ke Shih cfo->crystal_cap_default); 1759e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true); 1760e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1); 1761e3ec7017SPing-Ke Shih rtw89_dcfo_comp_init(rtwdev); 1762e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = 2000; 1763e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 1764e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 1765e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 1766e3ec7017SPing-Ke Shih } 1767e3ec7017SPing-Ke Shih 1768e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev, 1769e3ec7017SPing-Ke Shih s32 curr_cfo) 1770e3ec7017SPing-Ke Shih { 1771e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1772e3ec7017SPing-Ke Shih s8 crystal_cap = cfo->crystal_cap; 1773e3ec7017SPing-Ke Shih s32 cfo_abs = abs(curr_cfo); 1774e3ec7017SPing-Ke Shih int sign; 1775e3ec7017SPing-Ke Shih 1776e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 1777e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_ENABLE_TH) 1778e3ec7017SPing-Ke Shih cfo->is_adjust = true; 1779e3ec7017SPing-Ke Shih } else { 1780e3ec7017SPing-Ke Shih if (cfo_abs < CFO_TRK_STOP_TH) 1781e3ec7017SPing-Ke Shih cfo->is_adjust = false; 1782e3ec7017SPing-Ke Shih } 1783e3ec7017SPing-Ke Shih if (!cfo->is_adjust) { 1784e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n"); 1785e3ec7017SPing-Ke Shih return; 1786e3ec7017SPing-Ke Shih } 1787e3ec7017SPing-Ke Shih sign = curr_cfo > 0 ? 1 : -1; 1788e3ec7017SPing-Ke Shih if (cfo_abs > CFO_TRK_STOP_TH_4) 1789e3ec7017SPing-Ke Shih crystal_cap += 7 * sign; 1790e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_3) 1791e3ec7017SPing-Ke Shih crystal_cap += 5 * sign; 1792e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_2) 1793e3ec7017SPing-Ke Shih crystal_cap += 3 * sign; 1794e3ec7017SPing-Ke Shih else if (cfo_abs > CFO_TRK_STOP_TH_1) 1795e3ec7017SPing-Ke Shih crystal_cap += 1 * sign; 1796e3ec7017SPing-Ke Shih else 1797e3ec7017SPing-Ke Shih return; 1798e3ec7017SPing-Ke Shih rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false); 1799e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1800e3ec7017SPing-Ke Shih "X_cap{Curr,Default}={0x%x,0x%x}\n", 1801e3ec7017SPing-Ke Shih cfo->crystal_cap, cfo->def_x_cap); 1802e3ec7017SPing-Ke Shih } 1803e3ec7017SPing-Ke Shih 1804e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev) 1805e3ec7017SPing-Ke Shih { 1806e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1807e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 1808e3ec7017SPing-Ke Shih s32 cfo_cnt_all = 0; 1809e3ec7017SPing-Ke Shih s32 cfo_all_avg = 0; 1810e3ec7017SPing-Ke Shih u8 i; 1811e3ec7017SPing-Ke Shih 1812e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc != 1) 1813e3ec7017SPing-Ke Shih return 0; 1814e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n"); 1815e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 1816e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 1817e3ec7017SPing-Ke Shih continue; 1818e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 1819e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 1820e3ec7017SPing-Ke Shih cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all); 1821e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 1822e3ec7017SPing-Ke Shih } 1823e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1824e3ec7017SPing-Ke Shih "CFO track for macid = %d\n", i); 1825e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1826e3ec7017SPing-Ke Shih "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n", 1827e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_all_avg); 1828e3ec7017SPing-Ke Shih return cfo_all_avg; 1829e3ec7017SPing-Ke Shih } 1830e3ec7017SPing-Ke Shih 1831e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev) 1832e3ec7017SPing-Ke Shih { 1833e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1834e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 1835e3ec7017SPing-Ke Shih s32 target_cfo = 0; 1836e3ec7017SPing-Ke Shih s32 cfo_khz_all = 0; 1837e3ec7017SPing-Ke Shih s32 cfo_khz_all_tp_wgt = 0; 1838e3ec7017SPing-Ke Shih s32 cfo_avg = 0; 1839e3ec7017SPing-Ke Shih s32 max_cfo_lb = BIT(31); 1840e3ec7017SPing-Ke Shih s32 min_cfo_ub = GENMASK(30, 0); 1841e3ec7017SPing-Ke Shih u16 cfo_cnt_all = 0; 1842e3ec7017SPing-Ke Shih u8 active_entry_cnt = 0; 1843e3ec7017SPing-Ke Shih u8 sta_cnt = 0; 1844e3ec7017SPing-Ke Shih u32 tp_all = 0; 1845e3ec7017SPing-Ke Shih u8 i; 1846e3ec7017SPing-Ke Shih u8 cfo_tol = 0; 1847e3ec7017SPing-Ke Shih 1848e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n"); 1849e3ec7017SPing-Ke Shih if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) { 1850e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n"); 1851e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 1852e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 1853e3ec7017SPing-Ke Shih continue; 1854e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_tail[i]; 1855e3ec7017SPing-Ke Shih cfo_cnt_all += cfo->cfo_cnt[i]; 1856e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all); 1857e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1858e3ec7017SPing-Ke Shih "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n", 1859e3ec7017SPing-Ke Shih cfo_khz_all, cfo_cnt_all, cfo_avg); 1860e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 1861e3ec7017SPing-Ke Shih } 1862e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) { 1863e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n"); 1864e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 1865e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] == 0) 1866e3ec7017SPing-Ke Shih continue; 1867e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 1868e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 1869e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 1870e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1871e3ec7017SPing-Ke Shih "Macid=%d, cfo_avg=%d\n", i, 1872e3ec7017SPing-Ke Shih cfo->cfo_avg[i]); 1873e3ec7017SPing-Ke Shih } 1874e3ec7017SPing-Ke Shih sta_cnt = rtwdev->total_sta_assoc; 1875e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt); 1876e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1877e3ec7017SPing-Ke Shih "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n", 1878e3ec7017SPing-Ke Shih cfo_khz_all, sta_cnt, cfo_avg); 1879e3ec7017SPing-Ke Shih target_cfo = cfo_avg; 1880e3ec7017SPing-Ke Shih } else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) { 1881e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n"); 1882e3ec7017SPing-Ke Shih cfo_tol = cfo->sta_cfo_tolerance; 1883e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 1884e3ec7017SPing-Ke Shih sta_cnt++; 1885e3ec7017SPing-Ke Shih if (cfo->cfo_cnt[i] != 0) { 1886e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 1887e3ec7017SPing-Ke Shih (s32)cfo->cfo_cnt[i]); 1888e3ec7017SPing-Ke Shih active_entry_cnt++; 1889e3ec7017SPing-Ke Shih } else { 1890e3ec7017SPing-Ke Shih cfo->cfo_avg[i] = cfo->pre_cfo_avg[i]; 1891e3ec7017SPing-Ke Shih } 1892e3ec7017SPing-Ke Shih max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb); 1893e3ec7017SPing-Ke Shih min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub); 1894e3ec7017SPing-Ke Shih cfo_khz_all += cfo->cfo_avg[i]; 1895e3ec7017SPing-Ke Shih /* need tp for each entry */ 1896e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1897e3ec7017SPing-Ke Shih "[%d] cfo_avg=%d, tp=tbd\n", 1898e3ec7017SPing-Ke Shih i, cfo->cfo_avg[i]); 1899e3ec7017SPing-Ke Shih if (sta_cnt >= rtwdev->total_sta_assoc) 1900e3ec7017SPing-Ke Shih break; 1901e3ec7017SPing-Ke Shih } 1902e3ec7017SPing-Ke Shih tp_all = stats->rx_throughput; /* need tp for each entry */ 1903e3ec7017SPing-Ke Shih cfo_avg = phy_div(cfo_khz_all_tp_wgt, (s32)tp_all); 1904e3ec7017SPing-Ke Shih 1905e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n", 1906e3ec7017SPing-Ke Shih sta_cnt); 1907e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n", 1908e3ec7017SPing-Ke Shih active_entry_cnt); 1909e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1910e3ec7017SPing-Ke Shih "Msta cfo with tp_wgt=%d, avg_cfo=%d\n", 1911e3ec7017SPing-Ke Shih cfo_khz_all_tp_wgt, cfo_avg); 1912e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n", 1913e3ec7017SPing-Ke Shih max_cfo_lb, min_cfo_ub); 1914e3ec7017SPing-Ke Shih if (max_cfo_lb <= min_cfo_ub) { 1915e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1916e3ec7017SPing-Ke Shih "cfo win_size=%d\n", 1917e3ec7017SPing-Ke Shih min_cfo_ub - max_cfo_lb); 1918e3ec7017SPing-Ke Shih target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub); 1919e3ec7017SPing-Ke Shih } else { 1920e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 1921c51ed740SColin Ian King "No intersection of cfo tolerance windows\n"); 1922e3ec7017SPing-Ke Shih target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt); 1923e3ec7017SPing-Ke Shih } 1924e3ec7017SPing-Ke Shih for (i = 0; i < CFO_TRACK_MAX_USER; i++) 1925e3ec7017SPing-Ke Shih cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 1926e3ec7017SPing-Ke Shih } 1927e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo); 1928e3ec7017SPing-Ke Shih return target_cfo; 1929e3ec7017SPing-Ke Shih } 1930e3ec7017SPing-Ke Shih 1931e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev) 1932e3ec7017SPing-Ke Shih { 1933e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1934e3ec7017SPing-Ke Shih 1935e3ec7017SPing-Ke Shih memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail)); 1936e3ec7017SPing-Ke Shih memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt)); 1937e3ec7017SPing-Ke Shih cfo->packet_count = 0; 1938e3ec7017SPing-Ke Shih cfo->packet_count_pre = 0; 1939e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = 0; 1940e3ec7017SPing-Ke Shih } 1941e3ec7017SPing-Ke Shih 1942e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev) 1943e3ec7017SPing-Ke Shih { 1944e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 1945e3ec7017SPing-Ke Shih s32 new_cfo = 0; 1946e3ec7017SPing-Ke Shih bool x_cap_update = false; 1947e3ec7017SPing-Ke Shih u8 pre_x_cap = cfo->crystal_cap; 1948e3ec7017SPing-Ke Shih 1949e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n", 1950e3ec7017SPing-Ke Shih rtwdev->total_sta_assoc); 1951e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 0) { 1952e3ec7017SPing-Ke Shih rtw89_phy_cfo_reset(rtwdev); 1953e3ec7017SPing-Ke Shih return; 1954e3ec7017SPing-Ke Shih } 1955e3ec7017SPing-Ke Shih if (cfo->packet_count == 0) { 1956e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n"); 1957e3ec7017SPing-Ke Shih return; 1958e3ec7017SPing-Ke Shih } 1959e3ec7017SPing-Ke Shih if (cfo->packet_count == cfo->packet_count_pre) { 1960e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n"); 1961e3ec7017SPing-Ke Shih return; 1962e3ec7017SPing-Ke Shih } 1963e3ec7017SPing-Ke Shih if (rtwdev->total_sta_assoc == 1) 1964e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_average_cfo_calc(rtwdev); 1965e3ec7017SPing-Ke Shih else 1966e3ec7017SPing-Ke Shih new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev); 1967e3ec7017SPing-Ke Shih if (new_cfo == 0) { 1968e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n"); 1969e3ec7017SPing-Ke Shih return; 1970e3ec7017SPing-Ke Shih } 1971a9e06f2eSYi-Tang Chiu if (cfo->divergence_lock_en) { 1972a9e06f2eSYi-Tang Chiu cfo->lock_cnt++; 1973a9e06f2eSYi-Tang Chiu if (cfo->lock_cnt > CFO_PERIOD_CNT) { 1974a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = false; 1975a9e06f2eSYi-Tang Chiu cfo->lock_cnt = 0; 1976a9e06f2eSYi-Tang Chiu } else { 1977a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 1978a9e06f2eSYi-Tang Chiu } 1979a9e06f2eSYi-Tang Chiu return; 1980a9e06f2eSYi-Tang Chiu } 1981a9e06f2eSYi-Tang Chiu if (cfo->crystal_cap >= cfo->x_cap_ub || 1982a9e06f2eSYi-Tang Chiu cfo->crystal_cap <= cfo->x_cap_lb) { 1983a9e06f2eSYi-Tang Chiu cfo->divergence_lock_en = true; 1984a9e06f2eSYi-Tang Chiu rtw89_phy_cfo_reset(rtwdev); 1985a9e06f2eSYi-Tang Chiu return; 1986a9e06f2eSYi-Tang Chiu } 1987a9e06f2eSYi-Tang Chiu 1988e3ec7017SPing-Ke Shih rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo); 1989e3ec7017SPing-Ke Shih cfo->cfo_avg_pre = new_cfo; 19901646ce8fSYe Guojin x_cap_update = cfo->crystal_cap != pre_x_cap; 1991e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update); 1992e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n", 1993e3ec7017SPing-Ke Shih cfo->def_x_cap, pre_x_cap, cfo->crystal_cap, 1994e3ec7017SPing-Ke Shih cfo->x_cap_ofst); 1995e3ec7017SPing-Ke Shih if (x_cap_update) { 1996e3ec7017SPing-Ke Shih if (new_cfo > 0) 1997e3ec7017SPing-Ke Shih new_cfo -= CFO_SW_COMP_FINE_TUNE; 1998e3ec7017SPing-Ke Shih else 1999e3ec7017SPing-Ke Shih new_cfo += CFO_SW_COMP_FINE_TUNE; 2000e3ec7017SPing-Ke Shih } 2001e3ec7017SPing-Ke Shih rtw89_dcfo_comp(rtwdev, new_cfo); 2002e3ec7017SPing-Ke Shih rtw89_phy_cfo_statistics_reset(rtwdev); 2003e3ec7017SPing-Ke Shih } 2004e3ec7017SPing-Ke Shih 2005e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work) 2006e3ec7017SPing-Ke Shih { 2007e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 2008e3ec7017SPing-Ke Shih cfo_track_work.work); 2009e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2010e3ec7017SPing-Ke Shih 2011e3ec7017SPing-Ke Shih mutex_lock(&rtwdev->mutex); 2012e3ec7017SPing-Ke Shih if (!cfo->cfo_trig_by_timer_en) 2013e3ec7017SPing-Ke Shih goto out; 2014e3ec7017SPing-Ke Shih rtw89_leave_ps_mode(rtwdev); 2015e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2016e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2017e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2018e3ec7017SPing-Ke Shih out: 2019e3ec7017SPing-Ke Shih mutex_unlock(&rtwdev->mutex); 2020e3ec7017SPing-Ke Shih } 2021e3ec7017SPing-Ke Shih 2022e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev) 2023e3ec7017SPing-Ke Shih { 2024e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2025e3ec7017SPing-Ke Shih 2026e3ec7017SPing-Ke Shih ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 2027e3ec7017SPing-Ke Shih msecs_to_jiffies(cfo->cfo_timer_ms)); 2028e3ec7017SPing-Ke Shih } 2029e3ec7017SPing-Ke Shih 2030e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev) 2031e3ec7017SPing-Ke Shih { 2032e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2033e3ec7017SPing-Ke Shih struct rtw89_traffic_stats *stats = &rtwdev->stats; 2034e3ec7017SPing-Ke Shih 2035e3ec7017SPing-Ke Shih switch (cfo->phy_cfo_status) { 2036e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_NORMAL: 2037e3ec7017SPing-Ke Shih if (stats->tx_throughput >= CFO_TP_UPPER) { 2038e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE; 2039e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = true; 2040e3ec7017SPing-Ke Shih cfo->cfo_timer_ms = CFO_COMP_PERIOD; 2041e3ec7017SPing-Ke Shih rtw89_phy_cfo_start_work(rtwdev); 2042e3ec7017SPing-Ke Shih } 2043e3ec7017SPing-Ke Shih break; 2044e3ec7017SPing-Ke Shih case RTW89_PHY_DCFO_STATE_ENHANCE: 2045e3ec7017SPing-Ke Shih if (cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT) { 2046e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2047e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2048e3ec7017SPing-Ke Shih } 2049e3ec7017SPing-Ke Shih if (cfo->cfo_trig_by_timer_en == 1) 2050e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt++; 2051e3ec7017SPing-Ke Shih if (stats->tx_throughput <= CFO_TP_LOWER) { 2052e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2053e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2054e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en = false; 2055e3ec7017SPing-Ke Shih } 2056e3ec7017SPing-Ke Shih break; 2057e3ec7017SPing-Ke Shih default: 2058e3ec7017SPing-Ke Shih cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 2059e3ec7017SPing-Ke Shih cfo->phy_cfo_trk_cnt = 0; 2060e3ec7017SPing-Ke Shih break; 2061e3ec7017SPing-Ke Shih } 2062e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_CFO, 2063e3ec7017SPing-Ke Shih "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n", 2064e3ec7017SPing-Ke Shih stats->tx_throughput, cfo->phy_cfo_status, 2065e3ec7017SPing-Ke Shih cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt, 2066e3ec7017SPing-Ke Shih ewma_thermal_read(&rtwdev->phystat.avg_thermal[0])); 2067e3ec7017SPing-Ke Shih if (cfo->cfo_trig_by_timer_en) 2068e3ec7017SPing-Ke Shih return; 2069e3ec7017SPing-Ke Shih rtw89_phy_cfo_dm(rtwdev); 2070e3ec7017SPing-Ke Shih } 2071e3ec7017SPing-Ke Shih 2072e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val, 2073e3ec7017SPing-Ke Shih struct rtw89_rx_phy_ppdu *phy_ppdu) 2074e3ec7017SPing-Ke Shih { 2075e3ec7017SPing-Ke Shih struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 2076e3ec7017SPing-Ke Shih u8 macid = phy_ppdu->mac_id; 2077e3ec7017SPing-Ke Shih 2078e3ec7017SPing-Ke Shih cfo->cfo_tail[macid] += cfo_val; 2079e3ec7017SPing-Ke Shih cfo->cfo_cnt[macid]++; 2080e3ec7017SPing-Ke Shih cfo->packet_count++; 2081e3ec7017SPing-Ke Shih } 2082e3ec7017SPing-Ke Shih 2083e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev) 2084e3ec7017SPing-Ke Shih { 2085e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2086e3ec7017SPing-Ke Shih int i; 2087e3ec7017SPing-Ke Shih u8 th; 2088e3ec7017SPing-Ke Shih 2089e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) { 2090e3ec7017SPing-Ke Shih th = rtw89_chip_get_thermal(rtwdev, i); 2091e3ec7017SPing-Ke Shih if (th) 2092e3ec7017SPing-Ke Shih ewma_thermal_add(&phystat->avg_thermal[i], th); 2093e3ec7017SPing-Ke Shih 2094e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, 2095e3ec7017SPing-Ke Shih "path(%d) thermal cur=%u avg=%ld", i, th, 2096e3ec7017SPing-Ke Shih ewma_thermal_read(&phystat->avg_thermal[i])); 2097e3ec7017SPing-Ke Shih } 2098e3ec7017SPing-Ke Shih } 2099e3ec7017SPing-Ke Shih 2100e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data { 2101e3ec7017SPing-Ke Shih struct rtw89_dev *rtwdev; 2102e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info; 2103e3ec7017SPing-Ke Shih bool rssi_changed; 2104e3ec7017SPing-Ke Shih }; 2105e3ec7017SPing-Ke Shih 2106e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data, 2107e3ec7017SPing-Ke Shih struct ieee80211_sta *sta) 2108e3ec7017SPing-Ke Shih { 2109e3ec7017SPing-Ke Shih struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2110e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data *rssi_data = 2111e3ec7017SPing-Ke Shih (struct rtw89_phy_iter_rssi_data *)data; 2112e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info; 2113e3ec7017SPing-Ke Shih unsigned long rssi_curr; 2114e3ec7017SPing-Ke Shih 2115e3ec7017SPing-Ke Shih rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi); 2116e3ec7017SPing-Ke Shih 2117e3ec7017SPing-Ke Shih if (rssi_curr < ch_info->rssi_min) { 2118e3ec7017SPing-Ke Shih ch_info->rssi_min = rssi_curr; 2119e3ec7017SPing-Ke Shih ch_info->rssi_min_macid = rtwsta->mac_id; 2120e3ec7017SPing-Ke Shih } 2121e3ec7017SPing-Ke Shih 2122e3ec7017SPing-Ke Shih if (rtwsta->prev_rssi == 0) { 2123e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 2124e3ec7017SPing-Ke Shih } else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) { 2125e3ec7017SPing-Ke Shih rtwsta->prev_rssi = rssi_curr; 2126e3ec7017SPing-Ke Shih rssi_data->rssi_changed = true; 2127e3ec7017SPing-Ke Shih } 2128e3ec7017SPing-Ke Shih } 2129e3ec7017SPing-Ke Shih 2130e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev) 2131e3ec7017SPing-Ke Shih { 2132e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data rssi_data = {0}; 2133e3ec7017SPing-Ke Shih 2134e3ec7017SPing-Ke Shih rssi_data.rtwdev = rtwdev; 2135e3ec7017SPing-Ke Shih rssi_data.ch_info = &rtwdev->ch_info; 2136e3ec7017SPing-Ke Shih rssi_data.ch_info->rssi_min = U8_MAX; 2137e3ec7017SPing-Ke Shih ieee80211_iterate_stations_atomic(rtwdev->hw, 2138e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update_iter, 2139e3ec7017SPing-Ke Shih &rssi_data); 2140e3ec7017SPing-Ke Shih if (rssi_data.rssi_changed) 2141e3ec7017SPing-Ke Shih rtw89_btc_ntfy_wl_sta(rtwdev); 2142e3ec7017SPing-Ke Shih } 2143e3ec7017SPing-Ke Shih 2144e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev) 2145e3ec7017SPing-Ke Shih { 2146e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2147e3ec7017SPing-Ke Shih int i; 2148e3ec7017SPing-Ke Shih 2149e3ec7017SPing-Ke Shih for (i = 0; i < rtwdev->chip->rf_path_num; i++) 2150e3ec7017SPing-Ke Shih ewma_thermal_init(&phystat->avg_thermal[i]); 2151e3ec7017SPing-Ke Shih 2152e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 2153e3ec7017SPing-Ke Shih 2154e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 2155e3ec7017SPing-Ke Shih memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat)); 2156e3ec7017SPing-Ke Shih } 2157e3ec7017SPing-Ke Shih 2158e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev) 2159e3ec7017SPing-Ke Shih { 2160e3ec7017SPing-Ke Shih struct rtw89_phy_stat *phystat = &rtwdev->phystat; 2161e3ec7017SPing-Ke Shih 2162e3ec7017SPing-Ke Shih rtw89_phy_stat_thermal_update(rtwdev); 2163e3ec7017SPing-Ke Shih rtw89_phy_stat_rssi_update(rtwdev); 2164e3ec7017SPing-Ke Shih 2165e3ec7017SPing-Ke Shih phystat->last_pkt_stat = phystat->cur_pkt_stat; 2166e3ec7017SPing-Ke Shih memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 2167e3ec7017SPing-Ke Shih } 2168e3ec7017SPing-Ke Shih 2169e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us) 2170e3ec7017SPing-Ke Shih { 2171e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2172e3ec7017SPing-Ke Shih 2173e3ec7017SPing-Ke Shih return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 2174e3ec7017SPing-Ke Shih } 2175e3ec7017SPing-Ke Shih 2176e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx) 2177e3ec7017SPing-Ke Shih { 2178e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2179e3ec7017SPing-Ke Shih 2180e3ec7017SPing-Ke Shih return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 2181e3ec7017SPing-Ke Shih } 2182e3ec7017SPing-Ke Shih 2183e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev) 2184e3ec7017SPing-Ke Shih { 2185e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2186e3ec7017SPing-Ke Shih 2187e3ec7017SPing-Ke Shih env->ccx_manual_ctrl = false; 2188e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 2189e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 2190e3ec7017SPing-Ke Shih env->ccx_rpt_stamp = 0; 2191e3ec7017SPing-Ke Shih env->ccx_period = 0; 2192e3ec7017SPing-Ke Shih env->ccx_unit_idx = RTW89_CCX_32_US; 2193e3ec7017SPing-Ke Shih env->ccx_trigger_time = 0; 2194e3ec7017SPing-Ke Shih env->ccx_edcca_opt_bw_idx = RTW89_CCX_EDCCA_BW20_0; 2195e3ec7017SPing-Ke Shih 2196e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1); 2197e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1); 2198e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 2199e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK, 2200e3ec7017SPing-Ke Shih RTW89_CCX_EDCCA_BW20_0); 2201e3ec7017SPing-Ke Shih } 2202e3ec7017SPing-Ke Shih 2203e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report, 2204e3ec7017SPing-Ke Shih u16 score) 2205e3ec7017SPing-Ke Shih { 2206e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2207e3ec7017SPing-Ke Shih u32 numer = 0; 2208e3ec7017SPing-Ke Shih u16 ret = 0; 2209e3ec7017SPing-Ke Shih 2210e3ec7017SPing-Ke Shih numer = report * score + (env->ccx_period >> 1); 2211e3ec7017SPing-Ke Shih if (env->ccx_period) 2212e3ec7017SPing-Ke Shih ret = numer / env->ccx_period; 2213e3ec7017SPing-Ke Shih 2214e3ec7017SPing-Ke Shih return ret >= score ? score - 1 : ret; 2215e3ec7017SPing-Ke Shih } 2216e3ec7017SPing-Ke Shih 2217e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev, 2218e3ec7017SPing-Ke Shih u16 time_ms, u32 *period, 2219e3ec7017SPing-Ke Shih u32 *unit_idx) 2220e3ec7017SPing-Ke Shih { 2221e3ec7017SPing-Ke Shih u32 idx; 2222e3ec7017SPing-Ke Shih u8 quotient; 2223e3ec7017SPing-Ke Shih 2224e3ec7017SPing-Ke Shih if (time_ms >= CCX_MAX_PERIOD) 2225e3ec7017SPing-Ke Shih time_ms = CCX_MAX_PERIOD; 2226e3ec7017SPing-Ke Shih 2227e3ec7017SPing-Ke Shih quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD; 2228e3ec7017SPing-Ke Shih 2229e3ec7017SPing-Ke Shih if (quotient < 4) 2230e3ec7017SPing-Ke Shih idx = RTW89_CCX_4_US; 2231e3ec7017SPing-Ke Shih else if (quotient < 8) 2232e3ec7017SPing-Ke Shih idx = RTW89_CCX_8_US; 2233e3ec7017SPing-Ke Shih else if (quotient < 16) 2234e3ec7017SPing-Ke Shih idx = RTW89_CCX_16_US; 2235e3ec7017SPing-Ke Shih else 2236e3ec7017SPing-Ke Shih idx = RTW89_CCX_32_US; 2237e3ec7017SPing-Ke Shih 2238e3ec7017SPing-Ke Shih *unit_idx = idx; 2239e3ec7017SPing-Ke Shih *period = (time_ms * MS_TO_4US_RATIO) >> idx; 2240e3ec7017SPing-Ke Shih 2241e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2242e3ec7017SPing-Ke Shih "[Trigger Time] period:%d, unit_idx:%d\n", 2243e3ec7017SPing-Ke Shih *period, *unit_idx); 2244e3ec7017SPing-Ke Shih } 2245e3ec7017SPing-Ke Shih 2246e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev) 2247e3ec7017SPing-Ke Shih { 2248e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2249e3ec7017SPing-Ke Shih 2250e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2251e3ec7017SPing-Ke Shih "lv:(%d)->(0)\n", env->ccx_rac_lv); 2252e3ec7017SPing-Ke Shih 2253e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 2254e3ec7017SPing-Ke Shih env->ccx_rac_lv = RTW89_RAC_RELEASE; 2255e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 2256e3ec7017SPing-Ke Shih } 2257e3ec7017SPing-Ke Shih 2258e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev, 2259e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 2260e3ec7017SPing-Ke Shih { 2261e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2262e3ec7017SPing-Ke Shih bool is_update = env->ifs_clm_app != para->ifs_clm_app; 2263e3ec7017SPing-Ke Shih u8 i = 0; 2264e3ec7017SPing-Ke Shih u16 *ifs_th_l = env->ifs_clm_th_l; 2265e3ec7017SPing-Ke Shih u16 *ifs_th_h = env->ifs_clm_th_h; 2266e3ec7017SPing-Ke Shih u32 ifs_th0_us = 0, ifs_th_times = 0; 2267e3ec7017SPing-Ke Shih u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0}; 2268e3ec7017SPing-Ke Shih 2269e3ec7017SPing-Ke Shih if (!is_update) 2270e3ec7017SPing-Ke Shih goto ifs_update_finished; 2271e3ec7017SPing-Ke Shih 2272e3ec7017SPing-Ke Shih switch (para->ifs_clm_app) { 2273e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_INIT: 2274e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_BACKGROUND: 2275e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_ACS: 2276e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG: 2277e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DIG: 2278e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_TDMA_DIG: 2279e3ec7017SPing-Ke Shih ifs_th0_us = IFS_CLM_TH0_UPPER; 2280e3ec7017SPing-Ke Shih ifs_th_times = IFS_CLM_TH_MUL; 2281e3ec7017SPing-Ke Shih break; 2282e3ec7017SPing-Ke Shih case RTW89_IFS_CLM_DBG_MANUAL: 2283e3ec7017SPing-Ke Shih ifs_th0_us = para->ifs_clm_manual_th0; 2284e3ec7017SPing-Ke Shih ifs_th_times = para->ifs_clm_manual_th_times; 2285e3ec7017SPing-Ke Shih break; 2286e3ec7017SPing-Ke Shih default: 2287e3ec7017SPing-Ke Shih break; 2288e3ec7017SPing-Ke Shih } 2289e3ec7017SPing-Ke Shih 2290e3ec7017SPing-Ke Shih /* Set sampling threshold for 4 different regions, unit in idx_cnt. 2291e3ec7017SPing-Ke Shih * low[i] = high[i-1] + 1 2292e3ec7017SPing-Ke Shih * high[i] = high[i-1] * ifs_th_times 2293e3ec7017SPing-Ke Shih */ 2294e3ec7017SPing-Ke Shih ifs_th_l[IFS_CLM_TH_START_IDX] = 0; 2295e3ec7017SPing-Ke Shih ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us; 2296e3ec7017SPing-Ke Shih ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev, 2297e3ec7017SPing-Ke Shih ifs_th0_us); 2298e3ec7017SPing-Ke Shih for (i = 1; i < RTW89_IFS_CLM_NUM; i++) { 2299e3ec7017SPing-Ke Shih ifs_th_l[i] = ifs_th_h[i - 1] + 1; 2300e3ec7017SPing-Ke Shih ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times; 2301e3ec7017SPing-Ke Shih ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]); 2302e3ec7017SPing-Ke Shih } 2303e3ec7017SPing-Ke Shih 2304e3ec7017SPing-Ke Shih ifs_update_finished: 2305e3ec7017SPing-Ke Shih if (!is_update) 2306e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2307e3ec7017SPing-Ke Shih "No need to update IFS_TH\n"); 2308e3ec7017SPing-Ke Shih 2309e3ec7017SPing-Ke Shih return is_update; 2310e3ec7017SPing-Ke Shih } 2311e3ec7017SPing-Ke Shih 2312e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev) 2313e3ec7017SPing-Ke Shih { 2314e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2315e3ec7017SPing-Ke Shih u8 i = 0; 2316e3ec7017SPing-Ke Shih 2317e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK, 2318e3ec7017SPing-Ke Shih env->ifs_clm_th_l[0]); 2319e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK, 2320e3ec7017SPing-Ke Shih env->ifs_clm_th_l[1]); 2321e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK, 2322e3ec7017SPing-Ke Shih env->ifs_clm_th_l[2]); 2323e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK, 2324e3ec7017SPing-Ke Shih env->ifs_clm_th_l[3]); 2325e3ec7017SPing-Ke Shih 2326e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK, 2327e3ec7017SPing-Ke Shih env->ifs_clm_th_h[0]); 2328e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK, 2329e3ec7017SPing-Ke Shih env->ifs_clm_th_h[1]); 2330e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK, 2331e3ec7017SPing-Ke Shih env->ifs_clm_th_h[2]); 2332e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK, 2333e3ec7017SPing-Ke Shih env->ifs_clm_th_h[3]); 2334e3ec7017SPing-Ke Shih 2335e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 2336e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2337e3ec7017SPing-Ke Shih "Update IFS_T%d_th{low, high} : {%d, %d}\n", 2338e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]); 2339e3ec7017SPing-Ke Shih } 2340e3ec7017SPing-Ke Shih 2341e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev) 2342e3ec7017SPing-Ke Shih { 2343e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2344e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 2345e3ec7017SPing-Ke Shih 2346e3ec7017SPing-Ke Shih env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 2347e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = 0; 2348e3ec7017SPing-Ke Shih 2349e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_INIT; 2350e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, ¶)) 2351e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 2352e3ec7017SPing-Ke Shih 2353e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN, 2354e3ec7017SPing-Ke Shih true); 2355e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true); 2356e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true); 2357e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true); 2358e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true); 2359e3ec7017SPing-Ke Shih } 2360e3ec7017SPing-Ke Shih 2361e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev, 2362e3ec7017SPing-Ke Shih enum rtw89_env_racing_lv level) 2363e3ec7017SPing-Ke Shih { 2364e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2365e3ec7017SPing-Ke Shih int ret = 0; 2366e3ec7017SPing-Ke Shih 2367e3ec7017SPing-Ke Shih if (level >= RTW89_RAC_MAX_NUM) { 2368e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2369e3ec7017SPing-Ke Shih "[WARNING] Wrong LV=%d\n", level); 2370e3ec7017SPing-Ke Shih return -EINVAL; 2371e3ec7017SPing-Ke Shih } 2372e3ec7017SPing-Ke Shih 2373e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2374e3ec7017SPing-Ke Shih "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing, 2375e3ec7017SPing-Ke Shih env->ccx_rac_lv, level); 2376e3ec7017SPing-Ke Shih 2377e3ec7017SPing-Ke Shih if (env->ccx_ongoing) { 2378e3ec7017SPing-Ke Shih if (level <= env->ccx_rac_lv) 2379e3ec7017SPing-Ke Shih ret = -EINVAL; 2380e3ec7017SPing-Ke Shih else 2381e3ec7017SPing-Ke Shih env->ccx_ongoing = false; 2382e3ec7017SPing-Ke Shih } 2383e3ec7017SPing-Ke Shih 2384e3ec7017SPing-Ke Shih if (ret == 0) 2385e3ec7017SPing-Ke Shih env->ccx_rac_lv = level; 2386e3ec7017SPing-Ke Shih 2387e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n", 2388e3ec7017SPing-Ke Shih !ret); 2389e3ec7017SPing-Ke Shih 2390e3ec7017SPing-Ke Shih return ret; 2391e3ec7017SPing-Ke Shih } 2392e3ec7017SPing-Ke Shih 2393e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev) 2394e3ec7017SPing-Ke Shih { 2395e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2396e3ec7017SPing-Ke Shih 2397e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0); 2398e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0); 2399e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1); 2400e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1); 2401e3ec7017SPing-Ke Shih 2402e3ec7017SPing-Ke Shih env->ccx_rpt_stamp++; 2403e3ec7017SPing-Ke Shih env->ccx_ongoing = true; 2404e3ec7017SPing-Ke Shih } 2405e3ec7017SPing-Ke Shih 2406e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev) 2407e3ec7017SPing-Ke Shih { 2408e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2409e3ec7017SPing-Ke Shih u8 i = 0; 2410e3ec7017SPing-Ke Shih u32 res = 0; 2411e3ec7017SPing-Ke Shih 2412e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio = 2413e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT); 2414e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca_ratio = 2415e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca, 2416e3ec7017SPing-Ke Shih PERCENT); 2417e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio = 2418e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT); 2419e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_ratio = 2420e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT); 2421e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio = 2422e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa, 2423e3ec7017SPing-Ke Shih PERCENT); 2424e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio = 2425e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa, 2426e3ec7017SPing-Ke Shih PERCENT); 2427e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil = 2428e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL); 2429e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_fa_permil = 2430e3ec7017SPing-Ke Shih rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL); 2431e3ec7017SPing-Ke Shih 2432e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) { 2433e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) { 2434e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD; 2435e3ec7017SPing-Ke Shih } else { 2436e3ec7017SPing-Ke Shih env->ifs_clm_ifs_avg[i] = 2437e3ec7017SPing-Ke Shih rtw89_phy_ccx_idx_to_us(rtwdev, 2438e3ec7017SPing-Ke Shih env->ifs_clm_avg[i]); 2439e3ec7017SPing-Ke Shih } 2440e3ec7017SPing-Ke Shih 2441e3ec7017SPing-Ke Shih res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]); 2442e3ec7017SPing-Ke Shih res += env->ifs_clm_his[i] >> 1; 2443e3ec7017SPing-Ke Shih if (env->ifs_clm_his[i]) 2444e3ec7017SPing-Ke Shih res /= env->ifs_clm_his[i]; 2445e3ec7017SPing-Ke Shih else 2446e3ec7017SPing-Ke Shih res = 0; 2447e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i] = res; 2448e3ec7017SPing-Ke Shih } 2449e3ec7017SPing-Ke Shih 2450e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2451e3ec7017SPing-Ke Shih "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n", 2452e3ec7017SPing-Ke Shih env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio); 2453e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2454e3ec7017SPing-Ke Shih "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n", 2455e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio); 2456e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2457e3ec7017SPing-Ke Shih "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n", 2458e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil); 2459e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2460e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n", 2461e3ec7017SPing-Ke Shih env->ifs_clm_cck_cca_excl_fa_ratio, 2462e3ec7017SPing-Ke Shih env->ifs_clm_ofdm_cca_excl_fa_ratio); 2463e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2464e3ec7017SPing-Ke Shih "Time:[his, ifs_avg(us), cca_avg(us)]\n"); 2465e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 2466e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n", 2467e3ec7017SPing-Ke Shih i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i], 2468e3ec7017SPing-Ke Shih env->ifs_clm_cca_avg[i]); 2469e3ec7017SPing-Ke Shih } 2470e3ec7017SPing-Ke Shih 2471e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev) 2472e3ec7017SPing-Ke Shih { 2473e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2474e3ec7017SPing-Ke Shih u8 i = 0; 2475e3ec7017SPing-Ke Shih 2476e3ec7017SPing-Ke Shih if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) { 2477e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2478e3ec7017SPing-Ke Shih "Get IFS_CLM report Fail\n"); 2479e3ec7017SPing-Ke Shih return false; 2480e3ec7017SPing-Ke Shih } 2481e3ec7017SPing-Ke Shih 2482e3ec7017SPing-Ke Shih env->ifs_clm_tx = 2483e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 2484e3ec7017SPing-Ke Shih B_IFS_CLM_TX_CNT_MSK); 2485e3ec7017SPing-Ke Shih env->ifs_clm_edcca_excl_cca = 2486e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT, 2487e3ec7017SPing-Ke Shih B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK); 2488e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa = 2489e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 2490e3ec7017SPing-Ke Shih B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK); 2491e3ec7017SPing-Ke Shih env->ifs_clm_ofdmcca_excl_fa = 2492e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA, 2493e3ec7017SPing-Ke Shih B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK); 2494e3ec7017SPing-Ke Shih env->ifs_clm_cckfa = 2495e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 2496e3ec7017SPing-Ke Shih B_IFS_CLM_CCK_FA_MSK); 2497e3ec7017SPing-Ke Shih env->ifs_clm_ofdmfa = 2498e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA, 2499e3ec7017SPing-Ke Shih B_IFS_CLM_OFDM_FA_MSK); 2500e3ec7017SPing-Ke Shih 2501e3ec7017SPing-Ke Shih env->ifs_clm_his[0] = 2502e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK); 2503e3ec7017SPing-Ke Shih env->ifs_clm_his[1] = 2504e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK); 2505e3ec7017SPing-Ke Shih env->ifs_clm_his[2] = 2506e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK); 2507e3ec7017SPing-Ke Shih env->ifs_clm_his[3] = 2508e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK); 2509e3ec7017SPing-Ke Shih 2510e3ec7017SPing-Ke Shih env->ifs_clm_avg[0] = 2511e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK); 2512e3ec7017SPing-Ke Shih env->ifs_clm_avg[1] = 2513e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK); 2514e3ec7017SPing-Ke Shih env->ifs_clm_avg[2] = 2515e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK); 2516e3ec7017SPing-Ke Shih env->ifs_clm_avg[3] = 2517e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK); 2518e3ec7017SPing-Ke Shih 2519e3ec7017SPing-Ke Shih env->ifs_clm_cca[0] = 2520e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK); 2521e3ec7017SPing-Ke Shih env->ifs_clm_cca[1] = 2522e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK); 2523e3ec7017SPing-Ke Shih env->ifs_clm_cca[2] = 2524e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK); 2525e3ec7017SPing-Ke Shih env->ifs_clm_cca[3] = 2526e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK); 2527e3ec7017SPing-Ke Shih 2528e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs = 2529e3ec7017SPing-Ke Shih rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK); 2530e3ec7017SPing-Ke Shih 2531e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n", 2532e3ec7017SPing-Ke Shih env->ifs_clm_total_ifs); 2533e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2534e3ec7017SPing-Ke Shih "{Tx, EDCCA_exclu_cca} = {%d, %d}\n", 2535e3ec7017SPing-Ke Shih env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca); 2536e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2537e3ec7017SPing-Ke Shih "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n", 2538e3ec7017SPing-Ke Shih env->ifs_clm_cckfa, env->ifs_clm_ofdmfa); 2539e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2540e3ec7017SPing-Ke Shih "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n", 2541e3ec7017SPing-Ke Shih env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa); 2542e3ec7017SPing-Ke Shih 2543e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n"); 2544e3ec7017SPing-Ke Shih for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 2545e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2546e3ec7017SPing-Ke Shih "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i], 2547e3ec7017SPing-Ke Shih env->ifs_clm_avg[i], env->ifs_clm_cca[i]); 2548e3ec7017SPing-Ke Shih 2549e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_get_utility(rtwdev); 2550e3ec7017SPing-Ke Shih 2551e3ec7017SPing-Ke Shih return true; 2552e3ec7017SPing-Ke Shih } 2553e3ec7017SPing-Ke Shih 2554e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev, 2555e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info *para) 2556e3ec7017SPing-Ke Shih { 2557e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2558e3ec7017SPing-Ke Shih u32 period = 0; 2559e3ec7017SPing-Ke Shih u32 unit_idx = 0; 2560e3ec7017SPing-Ke Shih 2561e3ec7017SPing-Ke Shih if (para->mntr_time == 0) { 2562e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2563e3ec7017SPing-Ke Shih "[WARN] MNTR_TIME is 0\n"); 2564e3ec7017SPing-Ke Shih return -EINVAL; 2565e3ec7017SPing-Ke Shih } 2566e3ec7017SPing-Ke Shih 2567e3ec7017SPing-Ke Shih if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv)) 2568e3ec7017SPing-Ke Shih return -EINVAL; 2569e3ec7017SPing-Ke Shih 2570e3ec7017SPing-Ke Shih if (para->mntr_time != env->ifs_clm_mntr_time) { 2571e3ec7017SPing-Ke Shih rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time, 2572e3ec7017SPing-Ke Shih &period, &unit_idx); 2573e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 2574e3ec7017SPing-Ke Shih B_IFS_CLM_PERIOD_MSK, period); 2575e3ec7017SPing-Ke Shih rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, 2576e3ec7017SPing-Ke Shih B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx); 2577e3ec7017SPing-Ke Shih 2578e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2579e3ec7017SPing-Ke Shih "Update IFS-CLM time ((%d)) -> ((%d))\n", 2580e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time, para->mntr_time); 2581e3ec7017SPing-Ke Shih 2582e3ec7017SPing-Ke Shih env->ifs_clm_mntr_time = para->mntr_time; 2583e3ec7017SPing-Ke Shih env->ccx_period = (u16)period; 2584e3ec7017SPing-Ke Shih env->ccx_unit_idx = (u8)unit_idx; 2585e3ec7017SPing-Ke Shih } 2586e3ec7017SPing-Ke Shih 2587e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) { 2588e3ec7017SPing-Ke Shih env->ifs_clm_app = para->ifs_clm_app; 2589e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_set_th_reg(rtwdev); 2590e3ec7017SPing-Ke Shih } 2591e3ec7017SPing-Ke Shih 2592e3ec7017SPing-Ke Shih return 0; 2593e3ec7017SPing-Ke Shih } 2594e3ec7017SPing-Ke Shih 2595e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev) 2596e3ec7017SPing-Ke Shih { 2597e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2598e3ec7017SPing-Ke Shih struct rtw89_ccx_para_info para = {0}; 2599e3ec7017SPing-Ke Shih u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL; 2600e3ec7017SPing-Ke Shih 2601e3ec7017SPing-Ke Shih env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL; 2602e3ec7017SPing-Ke Shih if (env->ccx_manual_ctrl) { 2603e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2604e3ec7017SPing-Ke Shih "CCX in manual ctrl\n"); 2605e3ec7017SPing-Ke Shih return; 2606e3ec7017SPing-Ke Shih } 2607e3ec7017SPing-Ke Shih 2608e3ec7017SPing-Ke Shih /* only ifs_clm for now */ 2609e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_get_result(rtwdev)) 2610e3ec7017SPing-Ke Shih env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM; 2611e3ec7017SPing-Ke Shih 2612e3ec7017SPing-Ke Shih rtw89_phy_ccx_racing_release(rtwdev); 2613e3ec7017SPing-Ke Shih para.mntr_time = 1900; 2614e3ec7017SPing-Ke Shih para.rac_lv = RTW89_RAC_LV_1; 2615e3ec7017SPing-Ke Shih para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 2616e3ec7017SPing-Ke Shih 2617e3ec7017SPing-Ke Shih if (rtw89_phy_ifs_clm_set(rtwdev, ¶) == 0) 2618e3ec7017SPing-Ke Shih chk_result |= RTW89_PHY_ENV_MON_IFS_CLM; 2619e3ec7017SPing-Ke Shih if (chk_result) 2620e3ec7017SPing-Ke Shih rtw89_phy_ccx_trigger(rtwdev); 2621e3ec7017SPing-Ke Shih 2622e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 2623e3ec7017SPing-Ke Shih "get_result=0x%x, chk_result:0x%x\n", 2624e3ec7017SPing-Ke Shih env->ccx_watchdog_result, chk_result); 2625e3ec7017SPing-Ke Shih } 2626e3ec7017SPing-Ke Shih 2627eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page) 2628eb4e52b3SPo Hao Huang { 2629eb4e52b3SPo Hao Huang if (*ie_page > RTW89_PHYSTS_BITMAP_NUM || 2630eb4e52b3SPo Hao Huang *ie_page == RTW89_RSVD_9) 2631eb4e52b3SPo Hao Huang return false; 2632eb4e52b3SPo Hao Huang else if (*ie_page > RTW89_RSVD_9) 2633eb4e52b3SPo Hao Huang *ie_page -= 1; 2634eb4e52b3SPo Hao Huang 2635eb4e52b3SPo Hao Huang return true; 2636eb4e52b3SPo Hao Huang } 2637eb4e52b3SPo Hao Huang 2638eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page) 2639eb4e52b3SPo Hao Huang { 2640eb4e52b3SPo Hao Huang static const u8 ie_page_shift = 2; 2641eb4e52b3SPo Hao Huang 2642eb4e52b3SPo Hao Huang return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift); 2643eb4e52b3SPo Hao Huang } 2644eb4e52b3SPo Hao Huang 2645eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev, 2646eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page) 2647eb4e52b3SPo Hao Huang { 2648eb4e52b3SPo Hao Huang u32 addr; 2649eb4e52b3SPo Hao Huang 2650eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 2651eb4e52b3SPo Hao Huang return 0; 2652eb4e52b3SPo Hao Huang 2653eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 2654eb4e52b3SPo Hao Huang 2655eb4e52b3SPo Hao Huang return rtw89_phy_read32(rtwdev, addr); 2656eb4e52b3SPo Hao Huang } 2657eb4e52b3SPo Hao Huang 2658eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev, 2659eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap ie_page, 2660eb4e52b3SPo Hao Huang u32 val) 2661eb4e52b3SPo Hao Huang { 2662eb4e52b3SPo Hao Huang const struct rtw89_chip_info *chip = rtwdev->chip; 2663eb4e52b3SPo Hao Huang u32 addr; 2664eb4e52b3SPo Hao Huang 2665eb4e52b3SPo Hao Huang if (!rtw89_physts_ie_page_valid(&ie_page)) 2666eb4e52b3SPo Hao Huang return; 2667eb4e52b3SPo Hao Huang 2668eb4e52b3SPo Hao Huang if (chip->chip_id == RTL8852A) 2669eb4e52b3SPo Hao Huang val &= B_PHY_STS_BITMAP_MSK_52A; 2670eb4e52b3SPo Hao Huang 2671eb4e52b3SPo Hao Huang addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 2672eb4e52b3SPo Hao Huang rtw89_phy_write32(rtwdev, addr, val); 2673eb4e52b3SPo Hao Huang } 2674eb4e52b3SPo Hao Huang 2675eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev, 2676eb4e52b3SPo Hao Huang enum rtw89_phy_status_bitmap bitmap, 2677eb4e52b3SPo Hao Huang enum rtw89_phy_status_ie_type ie, 2678eb4e52b3SPo Hao Huang bool enable) 2679eb4e52b3SPo Hao Huang { 2680eb4e52b3SPo Hao Huang u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap); 2681eb4e52b3SPo Hao Huang 2682eb4e52b3SPo Hao Huang if (enable) 2683eb4e52b3SPo Hao Huang val |= BIT(ie); 2684eb4e52b3SPo Hao Huang else 2685eb4e52b3SPo Hao Huang val &= ~BIT(ie); 2686eb4e52b3SPo Hao Huang 2687eb4e52b3SPo Hao Huang rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val); 2688eb4e52b3SPo Hao Huang } 2689eb4e52b3SPo Hao Huang 2690eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev, 2691eb4e52b3SPo Hao Huang bool enable, 2692eb4e52b3SPo Hao Huang enum rtw89_phy_idx phy_idx) 2693eb4e52b3SPo Hao Huang { 2694eb4e52b3SPo Hao Huang if (enable) { 2695eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 2696eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 2697eb4e52b3SPo Hao Huang rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM, 2698eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 2699eb4e52b3SPo Hao Huang } else { 2700eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 2701eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_FAIL); 2702eb4e52b3SPo Hao Huang rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM, 2703eb4e52b3SPo Hao Huang B_STS_DIS_TRIG_BY_BRK); 2704eb4e52b3SPo Hao Huang } 2705eb4e52b3SPo Hao Huang } 2706eb4e52b3SPo Hao Huang 2707eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev) 2708eb4e52b3SPo Hao Huang { 2709eb4e52b3SPo Hao Huang const struct rtw89_chip_info *chip = rtwdev->chip; 2710eb4e52b3SPo Hao Huang u8 i; 2711eb4e52b3SPo Hao Huang 2712eb4e52b3SPo Hao Huang if (chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV) 2713eb4e52b3SPo Hao Huang rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0); 2714eb4e52b3SPo Hao Huang 2715eb4e52b3SPo Hao Huang for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) { 2716eb4e52b3SPo Hao Huang if (i >= RTW89_CCK_PKT) 2717eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 2718eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE09_FTR_0, 2719eb4e52b3SPo Hao Huang true); 2720eb4e52b3SPo Hao Huang if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) || 2721eb4e52b3SPo Hao Huang (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT)) 2722eb4e52b3SPo Hao Huang continue; 2723eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, i, 2724eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE24_OFDM_TD_PATH_A, 2725eb4e52b3SPo Hao Huang true); 2726eb4e52b3SPo Hao Huang } 2727eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT, 2728eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 2729eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT, 2730eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE13_DL_MU_DEF, true); 2731eb4e52b3SPo Hao Huang 2732eb4e52b3SPo Hao Huang /* force IE01 for channel index, only channel field is valid */ 2733eb4e52b3SPo Hao Huang rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT, 2734eb4e52b3SPo Hao Huang RTW89_PHYSTS_IE01_CMN_OFDM, true); 2735eb4e52b3SPo Hao Huang } 2736eb4e52b3SPo Hao Huang 2737e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type) 2738e3ec7017SPing-Ke Shih { 2739e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 2740e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2741e3ec7017SPing-Ke Shih const struct rtw89_phy_dig_gain_cfg *cfg; 2742e3ec7017SPing-Ke Shih const char *msg; 2743e3ec7017SPing-Ke Shih u8 i; 2744e3ec7017SPing-Ke Shih s8 gain_base; 2745e3ec7017SPing-Ke Shih s8 *gain_arr; 2746e3ec7017SPing-Ke Shih u32 tmp; 2747e3ec7017SPing-Ke Shih 2748e3ec7017SPing-Ke Shih switch (type) { 2749e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_G: 2750e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_g; 2751e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 2752e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_g; 2753e3ec7017SPing-Ke Shih msg = "lna_gain_g"; 2754e3ec7017SPing-Ke Shih break; 2755e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_G: 2756e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_g; 2757e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_G; 2758e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_g; 2759e3ec7017SPing-Ke Shih msg = "tia_gain_g"; 2760e3ec7017SPing-Ke Shih break; 2761e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_LNA_A: 2762e3ec7017SPing-Ke Shih gain_arr = dig->lna_gain_a; 2763e3ec7017SPing-Ke Shih gain_base = LNA0_GAIN; 2764e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_lna_a; 2765e3ec7017SPing-Ke Shih msg = "lna_gain_a"; 2766e3ec7017SPing-Ke Shih break; 2767e3ec7017SPing-Ke Shih case RTW89_DIG_GAIN_TIA_A: 2768e3ec7017SPing-Ke Shih gain_arr = dig->tia_gain_a; 2769e3ec7017SPing-Ke Shih gain_base = TIA0_GAIN_A; 2770e3ec7017SPing-Ke Shih cfg = chip->dig_table->cfg_tia_a; 2771e3ec7017SPing-Ke Shih msg = "tia_gain_a"; 2772e3ec7017SPing-Ke Shih break; 2773e3ec7017SPing-Ke Shih default: 2774e3ec7017SPing-Ke Shih return; 2775e3ec7017SPing-Ke Shih } 2776e3ec7017SPing-Ke Shih 2777e3ec7017SPing-Ke Shih for (i = 0; i < cfg->size; i++) { 2778e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr, 2779e3ec7017SPing-Ke Shih cfg->table[i].mask); 2780e3ec7017SPing-Ke Shih tmp >>= DIG_GAIN_SHIFT; 2781e3ec7017SPing-Ke Shih gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base; 2782e3ec7017SPing-Ke Shih gain_base += DIG_GAIN; 2783e3ec7017SPing-Ke Shih 2784e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n", 2785e3ec7017SPing-Ke Shih msg, i, gain_arr[i]); 2786e3ec7017SPing-Ke Shih } 2787e3ec7017SPing-Ke Shih } 2788e3ec7017SPing-Ke Shih 2789e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev) 2790e3ec7017SPing-Ke Shih { 2791e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2792e3ec7017SPing-Ke Shih u32 tmp; 2793e3ec7017SPing-Ke Shih u8 i; 2794e3ec7017SPing-Ke Shih 2795e3ec7017SPing-Ke Shih tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW, 2796e3ec7017SPing-Ke Shih B_PATH0_IB_PKPW_MSK); 2797e3ec7017SPing-Ke Shih dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT); 2798e3ec7017SPing-Ke Shih dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK, 2799e3ec7017SPing-Ke Shih B_PATH0_IB_PBK_MSK); 2800e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n", 2801e3ec7017SPing-Ke Shih dig->ib_pkpwr, dig->ib_pbk); 2802e3ec7017SPing-Ke Shih 2803e3ec7017SPing-Ke Shih for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++) 2804e3ec7017SPing-Ke Shih rtw89_phy_dig_read_gain_table(rtwdev, i); 2805e3ec7017SPing-Ke Shih } 2806e3ec7017SPing-Ke Shih 2807e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22; 2808e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104}; 2809e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88}; 2810e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16}; 2811e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528}; 2812e3ec7017SPing-Ke Shih 2813e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev) 2814e3ec7017SPing-Ke Shih { 2815e3ec7017SPing-Ke Shih struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info; 2816e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2817e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 2818e3ec7017SPing-Ke Shih 2819e3ec7017SPing-Ke Shih if (is_linked) { 2820e3ec7017SPing-Ke Shih dig->igi_rssi = ch_info->rssi_min >> 1; 2821e3ec7017SPing-Ke Shih } else { 2822e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n"); 2823e3ec7017SPing-Ke Shih dig->igi_rssi = rssi_nolink; 2824e3ec7017SPing-Ke Shih } 2825e3ec7017SPing-Ke Shih } 2826e3ec7017SPing-Ke Shih 2827e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev) 2828e3ec7017SPing-Ke Shih { 2829e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2830e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 2831e3ec7017SPing-Ke Shih const u16 *fa_th_src = NULL; 2832e3ec7017SPing-Ke Shih 2833e3ec7017SPing-Ke Shih switch (rtwdev->hal.current_band_type) { 2834e3ec7017SPing-Ke Shih case RTW89_BAND_2G: 2835e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_g; 2836e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_g; 2837e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_2g : fa_th_nolink; 2838e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = false; 2839e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 2840e3ec7017SPing-Ke Shih break; 2841e3ec7017SPing-Ke Shih case RTW89_BAND_5G: 2842e3ec7017SPing-Ke Shih default: 2843e3ec7017SPing-Ke Shih dig->lna_gain = dig->lna_gain_a; 2844e3ec7017SPing-Ke Shih dig->tia_gain = dig->tia_gain_a; 2845e3ec7017SPing-Ke Shih fa_th_src = is_linked ? fa_th_5g : fa_th_nolink; 2846e3ec7017SPing-Ke Shih dig->force_gaincode_idx_en = true; 2847e3ec7017SPing-Ke Shih dig->dyn_pd_th_en = true; 2848e3ec7017SPing-Ke Shih break; 2849e3ec7017SPing-Ke Shih } 2850e3ec7017SPing-Ke Shih memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th)); 2851e3ec7017SPing-Ke Shih memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th)); 2852e3ec7017SPing-Ke Shih } 2853e3ec7017SPing-Ke Shih 2854e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20; 2855e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a; 2856e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max; 2857e3ec7017SPing-Ke Shih 2858e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev) 2859e3ec7017SPing-Ke Shih { 2860e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2861e3ec7017SPing-Ke Shih 2862e3ec7017SPing-Ke Shih dig->cur_gaincode.lna_idx = LNA_IDX_MAX; 2863e3ec7017SPing-Ke Shih dig->cur_gaincode.tia_idx = TIA_IDX_MAX; 2864e3ec7017SPing-Ke Shih dig->cur_gaincode.rxb_idx = RXB_IDX_MAX; 2865e3ec7017SPing-Ke Shih dig->force_gaincode.lna_idx = LNA_IDX_MAX; 2866e3ec7017SPing-Ke Shih dig->force_gaincode.tia_idx = TIA_IDX_MAX; 2867e3ec7017SPing-Ke Shih dig->force_gaincode.rxb_idx = RXB_IDX_MAX; 2868e3ec7017SPing-Ke Shih 2869e3ec7017SPing-Ke Shih dig->dyn_igi_max = igi_max_performance_mode; 2870e3ec7017SPing-Ke Shih dig->dyn_igi_min = dynamic_igi_min; 2871e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dynamic_pd_threshold_max; 2872e3ec7017SPing-Ke Shih dig->pd_low_th_ofst = pd_low_th_offset; 2873e3ec7017SPing-Ke Shih dig->is_linked_pre = false; 2874e3ec7017SPing-Ke Shih } 2875e3ec7017SPing-Ke Shih 2876e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev) 2877e3ec7017SPing-Ke Shih { 2878e3ec7017SPing-Ke Shih rtw89_phy_dig_update_gain_para(rtwdev); 2879e3ec7017SPing-Ke Shih rtw89_phy_dig_reset(rtwdev); 2880e3ec7017SPing-Ke Shih } 2881e3ec7017SPing-Ke Shih 2882e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 2883e3ec7017SPing-Ke Shih { 2884e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2885e3ec7017SPing-Ke Shih u8 lna_idx; 2886e3ec7017SPing-Ke Shih 2887e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 2888e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX6; 2889e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[1]) 2890e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX5; 2891e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[2]) 2892e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX4; 2893e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[3]) 2894e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX3; 2895e3ec7017SPing-Ke Shih else if (rssi < dig->igi_rssi_th[4]) 2896e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX2; 2897e3ec7017SPing-Ke Shih else 2898e3ec7017SPing-Ke Shih lna_idx = RTW89_DIG_GAIN_LNA_IDX1; 2899e3ec7017SPing-Ke Shih 2900e3ec7017SPing-Ke Shih return lna_idx; 2901e3ec7017SPing-Ke Shih } 2902e3ec7017SPing-Ke Shih 2903e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 2904e3ec7017SPing-Ke Shih { 2905e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2906e3ec7017SPing-Ke Shih u8 tia_idx; 2907e3ec7017SPing-Ke Shih 2908e3ec7017SPing-Ke Shih if (rssi < dig->igi_rssi_th[0]) 2909e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX1; 2910e3ec7017SPing-Ke Shih else 2911e3ec7017SPing-Ke Shih tia_idx = RTW89_DIG_GAIN_TIA_IDX0; 2912e3ec7017SPing-Ke Shih 2913e3ec7017SPing-Ke Shih return tia_idx; 2914e3ec7017SPing-Ke Shih } 2915e3ec7017SPing-Ke Shih 2916e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110 2917e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10 2918e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 2919e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 2920e3ec7017SPing-Ke Shih { 2921e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2922e3ec7017SPing-Ke Shih s8 lna_gain = dig->lna_gain[set->lna_idx]; 2923e3ec7017SPing-Ke Shih s8 tia_gain = dig->tia_gain[set->tia_idx]; 2924e3ec7017SPing-Ke Shih s32 wb_rssi = rssi + lna_gain + tia_gain; 2925e3ec7017SPing-Ke Shih s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE; 2926e3ec7017SPing-Ke Shih u8 rxb_idx; 2927e3ec7017SPing-Ke Shih 2928e3ec7017SPing-Ke Shih rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi; 2929e3ec7017SPing-Ke Shih rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX); 2930e3ec7017SPing-Ke Shih 2931e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n", 2932e3ec7017SPing-Ke Shih wb_rssi, rxb_idx_tmp); 2933e3ec7017SPing-Ke Shih 2934e3ec7017SPing-Ke Shih return rxb_idx; 2935e3ec7017SPing-Ke Shih } 2936e3ec7017SPing-Ke Shih 2937e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 2938e3ec7017SPing-Ke Shih struct rtw89_agc_gaincode_set *set) 2939e3ec7017SPing-Ke Shih { 2940e3ec7017SPing-Ke Shih set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi); 2941e3ec7017SPing-Ke Shih set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi); 2942e3ec7017SPing-Ke Shih set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set); 2943e3ec7017SPing-Ke Shih 2944e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 2945e3ec7017SPing-Ke Shih "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n", 2946e3ec7017SPing-Ke Shih rssi, set->lna_idx, set->tia_idx, set->rxb_idx); 2947e3ec7017SPing-Ke Shih } 2948e3ec7017SPing-Ke Shih 2949e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25 2950e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2 2951e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev) 2952e3ec7017SPing-Ke Shih { 2953e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 2954e3ec7017SPing-Ke Shih struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 2955e3ec7017SPing-Ke Shih enum rtw89_dig_noisy_level noisy_lv; 2956e3ec7017SPing-Ke Shih u8 igi_offset = dig->fa_rssi_ofst; 2957e3ec7017SPing-Ke Shih u16 fa_ratio = 0; 2958e3ec7017SPing-Ke Shih 2959e3ec7017SPing-Ke Shih fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil; 2960e3ec7017SPing-Ke Shih 2961e3ec7017SPing-Ke Shih if (fa_ratio < dig->fa_th[0]) 2962e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL0; 2963e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[1]) 2964e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL1; 2965e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[2]) 2966e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL2; 2967e3ec7017SPing-Ke Shih else if (fa_ratio < dig->fa_th[3]) 2968e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL3; 2969e3ec7017SPing-Ke Shih else 2970e3ec7017SPing-Ke Shih noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX; 2971e3ec7017SPing-Ke Shih 2972e3ec7017SPing-Ke Shih if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2) 2973e3ec7017SPing-Ke Shih igi_offset = 0; 2974e3ec7017SPing-Ke Shih else 2975e3ec7017SPing-Ke Shih igi_offset += noisy_lv * IGI_OFFSET_MUL; 2976e3ec7017SPing-Ke Shih 2977e3ec7017SPing-Ke Shih igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX); 2978e3ec7017SPing-Ke Shih dig->fa_rssi_ofst = igi_offset; 2979e3ec7017SPing-Ke Shih 2980e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 2981e3ec7017SPing-Ke Shih "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n", 2982e3ec7017SPing-Ke Shih dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]); 2983e3ec7017SPing-Ke Shih 2984e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 2985e3ec7017SPing-Ke Shih "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n", 2986e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil, 2987e3ec7017SPing-Ke Shih env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil, 2988e3ec7017SPing-Ke Shih noisy_lv, igi_offset); 2989e3ec7017SPing-Ke Shih } 2990e3ec7017SPing-Ke Shih 2991e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx) 2992e3ec7017SPing-Ke Shih { 2993e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH0_LNA_INIT, 2994e3ec7017SPing-Ke Shih B_PATH0_LNA_INIT_IDX_MSK, lna_idx); 2995e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH1_LNA_INIT, 2996e3ec7017SPing-Ke Shih B_PATH1_LNA_INIT_IDX_MSK, lna_idx); 2997e3ec7017SPing-Ke Shih } 2998e3ec7017SPing-Ke Shih 2999e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx) 3000e3ec7017SPing-Ke Shih { 3001e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH0_TIA_INIT, 3002e3ec7017SPing-Ke Shih B_PATH0_TIA_INIT_IDX_MSK, tia_idx); 3003e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH1_TIA_INIT, 3004e3ec7017SPing-Ke Shih B_PATH1_TIA_INIT_IDX_MSK, tia_idx); 3005e3ec7017SPing-Ke Shih } 3006e3ec7017SPing-Ke Shih 3007e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx) 3008e3ec7017SPing-Ke Shih { 3009e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH0_RXB_INIT, 3010e3ec7017SPing-Ke Shih B_PATH0_RXB_INIT_IDX_MSK, rxb_idx); 3011e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_PATH1_RXB_INIT, 3012e3ec7017SPing-Ke Shih B_PATH1_RXB_INIT_IDX_MSK, rxb_idx); 3013e3ec7017SPing-Ke Shih } 3014e3ec7017SPing-Ke Shih 3015e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev, 3016e3ec7017SPing-Ke Shih const struct rtw89_agc_gaincode_set set) 3017e3ec7017SPing-Ke Shih { 3018e3ec7017SPing-Ke Shih rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx); 3019e3ec7017SPing-Ke Shih rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx); 3020e3ec7017SPing-Ke Shih rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx); 3021e3ec7017SPing-Ke Shih 3022e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n", 3023e3ec7017SPing-Ke Shih set.lna_idx, set.tia_idx, set.rxb_idx); 3024e3ec7017SPing-Ke Shih } 3025e3ec7017SPing-Ke Shih 3026e3ec7017SPing-Ke Shih static const struct rtw89_reg_def sdagc_config[4] = { 3027e3ec7017SPing-Ke Shih {R_PATH0_P20_FOLLOW_BY_PAGCUGC, B_PATH0_P20_FOLLOW_BY_PAGCUGC_EN_MSK}, 3028e3ec7017SPing-Ke Shih {R_PATH0_S20_FOLLOW_BY_PAGCUGC, B_PATH0_S20_FOLLOW_BY_PAGCUGC_EN_MSK}, 3029e3ec7017SPing-Ke Shih {R_PATH1_P20_FOLLOW_BY_PAGCUGC, B_PATH1_P20_FOLLOW_BY_PAGCUGC_EN_MSK}, 3030e3ec7017SPing-Ke Shih {R_PATH1_S20_FOLLOW_BY_PAGCUGC, B_PATH1_S20_FOLLOW_BY_PAGCUGC_EN_MSK}, 3031e3ec7017SPing-Ke Shih }; 3032e3ec7017SPing-Ke Shih 3033e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev, 3034e3ec7017SPing-Ke Shih bool enable) 3035e3ec7017SPing-Ke Shih { 3036e3ec7017SPing-Ke Shih u8 i = 0; 3037e3ec7017SPing-Ke Shih 3038e3ec7017SPing-Ke Shih for (i = 0; i < ARRAY_SIZE(sdagc_config); i++) 3039e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, sdagc_config[i].addr, 3040e3ec7017SPing-Ke Shih sdagc_config[i].mask, enable); 3041e3ec7017SPing-Ke Shih 3042e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable); 3043e3ec7017SPing-Ke Shih } 3044e3ec7017SPing-Ke Shih 3045e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi, 3046e3ec7017SPing-Ke Shih bool enable) 3047e3ec7017SPing-Ke Shih { 3048e3ec7017SPing-Ke Shih enum rtw89_bandwidth cbw = rtwdev->hal.current_band_width; 3049e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3050e3ec7017SPing-Ke Shih u8 final_rssi = 0, under_region = dig->pd_low_th_ofst; 30511c2423deSJohnson Lin u8 ofdm_cca_th; 30521c2423deSJohnson Lin s8 cck_cca_th; 30531c2423deSJohnson Lin u32 pd_val = 0; 3054e3ec7017SPing-Ke Shih 3055e3ec7017SPing-Ke Shih under_region += PD_TH_SB_FLTR_CMP_VAL; 3056e3ec7017SPing-Ke Shih 3057e3ec7017SPing-Ke Shih switch (cbw) { 3058e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_40: 3059e3ec7017SPing-Ke Shih under_region += PD_TH_BW40_CMP_VAL; 3060e3ec7017SPing-Ke Shih break; 3061e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_80: 3062e3ec7017SPing-Ke Shih under_region += PD_TH_BW80_CMP_VAL; 3063e3ec7017SPing-Ke Shih break; 30641c2423deSJohnson Lin case RTW89_CHANNEL_WIDTH_160: 30651c2423deSJohnson Lin under_region += PD_TH_BW160_CMP_VAL; 30661c2423deSJohnson Lin break; 3067e3ec7017SPing-Ke Shih case RTW89_CHANNEL_WIDTH_20: 3068e3ec7017SPing-Ke Shih fallthrough; 3069e3ec7017SPing-Ke Shih default: 3070e3ec7017SPing-Ke Shih under_region += PD_TH_BW20_CMP_VAL; 3071e3ec7017SPing-Ke Shih break; 3072e3ec7017SPing-Ke Shih } 3073e3ec7017SPing-Ke Shih 3074e3ec7017SPing-Ke Shih dig->dyn_pd_th_max = dig->igi_rssi; 3075e3ec7017SPing-Ke Shih 3076e3ec7017SPing-Ke Shih final_rssi = min_t(u8, rssi, dig->igi_rssi); 30771c2423deSJohnson Lin ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region, 3078e3ec7017SPing-Ke Shih PD_TH_MAX_RSSI + under_region); 3079e3ec7017SPing-Ke Shih 3080e3ec7017SPing-Ke Shih if (enable) { 30811c2423deSJohnson Lin pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1; 3082e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 30831c2423deSJohnson Lin "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n", 30841c2423deSJohnson Lin final_rssi, ofdm_cca_th, under_region, pd_val); 3085e3ec7017SPing-Ke Shih } else { 3086e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3087c51ed740SColin Ian King "Dynamic PD th disabled, Set PD_low_bd=0\n"); 3088e3ec7017SPing-Ke Shih } 3089e3ec7017SPing-Ke Shih 3090e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_SEG0R_PD, B_SEG0R_PD_LOWER_BOUND_MSK, 30911c2423deSJohnson Lin pd_val); 3092e3ec7017SPing-Ke Shih rtw89_phy_write32_mask(rtwdev, R_SEG0R_PD, 3093e3ec7017SPing-Ke Shih B_SEG0R_PD_SPATIAL_REUSE_EN_MSK, enable); 30941c2423deSJohnson Lin 30951c2423deSJohnson Lin if (!rtwdev->hal.support_cckpd) 30961c2423deSJohnson Lin return; 30971c2423deSJohnson Lin 30981c2423deSJohnson Lin cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI); 30991c2423deSJohnson Lin pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX); 31001c2423deSJohnson Lin 31011c2423deSJohnson Lin rtw89_debug(rtwdev, RTW89_DBG_DIG, 31021c2423deSJohnson Lin "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n", 31031c2423deSJohnson Lin final_rssi, cck_cca_th, under_region, pd_val); 31041c2423deSJohnson Lin 31051c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1, 31061c2423deSJohnson Lin B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable); 31071c2423deSJohnson Lin rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1, 31081c2423deSJohnson Lin B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val); 3109e3ec7017SPing-Ke Shih } 3110e3ec7017SPing-Ke Shih 3111e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev) 3112e3ec7017SPing-Ke Shih { 3113e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3114e3ec7017SPing-Ke Shih 3115e3ec7017SPing-Ke Shih dig->bypass_dig = false; 3116e3ec7017SPing-Ke Shih rtw89_phy_dig_para_reset(rtwdev); 3117e3ec7017SPing-Ke Shih rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 3118e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false); 3119e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 3120e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 3121e3ec7017SPing-Ke Shih } 3122e3ec7017SPing-Ke Shih 3123e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10 3124e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev) 3125e3ec7017SPing-Ke Shih { 3126e3ec7017SPing-Ke Shih struct rtw89_dig_info *dig = &rtwdev->dig; 3127e3ec7017SPing-Ke Shih bool is_linked = rtwdev->total_sta_assoc > 0; 3128e3ec7017SPing-Ke Shih 3129e3ec7017SPing-Ke Shih if (unlikely(dig->bypass_dig)) { 3130e3ec7017SPing-Ke Shih dig->bypass_dig = false; 3131e3ec7017SPing-Ke Shih return; 3132e3ec7017SPing-Ke Shih } 3133e3ec7017SPing-Ke Shih 3134e3ec7017SPing-Ke Shih if (!dig->is_linked_pre && is_linked) { 3135e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n"); 3136e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 3137e3ec7017SPing-Ke Shih } else if (dig->is_linked_pre && !is_linked) { 3138e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n"); 3139e3ec7017SPing-Ke Shih rtw89_phy_dig_update_para(rtwdev); 3140e3ec7017SPing-Ke Shih } 3141e3ec7017SPing-Ke Shih dig->is_linked_pre = is_linked; 3142e3ec7017SPing-Ke Shih 3143e3ec7017SPing-Ke Shih rtw89_phy_dig_igi_offset_by_env(rtwdev); 3144e3ec7017SPing-Ke Shih rtw89_phy_dig_update_rssi_info(rtwdev); 3145e3ec7017SPing-Ke Shih 3146e3ec7017SPing-Ke Shih dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ? 3147e3ec7017SPing-Ke Shih dig->igi_rssi - IGI_RSSI_MIN : 0; 3148e3ec7017SPing-Ke Shih dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX; 3149e3ec7017SPing-Ke Shih dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst; 3150e3ec7017SPing-Ke Shih 3151e3ec7017SPing-Ke Shih dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min, 3152e3ec7017SPing-Ke Shih dig->dyn_igi_max); 3153e3ec7017SPing-Ke Shih 3154e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3155e3ec7017SPing-Ke Shih "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n", 3156e3ec7017SPing-Ke Shih dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min, 3157e3ec7017SPing-Ke Shih dig->igi_fa_rssi); 3158e3ec7017SPing-Ke Shih 3159e3ec7017SPing-Ke Shih if (dig->force_gaincode_idx_en) { 3160e3ec7017SPing-Ke Shih rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 3161e3ec7017SPing-Ke Shih rtw89_debug(rtwdev, RTW89_DBG_DIG, 3162e3ec7017SPing-Ke Shih "Force gaincode index enabled.\n"); 3163e3ec7017SPing-Ke Shih } else { 3164e3ec7017SPing-Ke Shih rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi, 3165e3ec7017SPing-Ke Shih &dig->cur_gaincode); 3166e3ec7017SPing-Ke Shih rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode); 3167e3ec7017SPing-Ke Shih } 3168e3ec7017SPing-Ke Shih 3169e3ec7017SPing-Ke Shih rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en); 3170e3ec7017SPing-Ke Shih 3171e3ec7017SPing-Ke Shih if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max) 3172e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true); 3173e3ec7017SPing-Ke Shih else 3174e3ec7017SPing-Ke Shih rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 3175e3ec7017SPing-Ke Shih } 3176e3ec7017SPing-Ke Shih 3177e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev) 3178e3ec7017SPing-Ke Shih { 3179e3ec7017SPing-Ke Shih rtw89_phy_ccx_top_setting_init(rtwdev); 3180e3ec7017SPing-Ke Shih rtw89_phy_ifs_clm_setting_init(rtwdev); 3181e3ec7017SPing-Ke Shih } 3182e3ec7017SPing-Ke Shih 3183e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev) 3184e3ec7017SPing-Ke Shih { 3185e3ec7017SPing-Ke Shih const struct rtw89_chip_info *chip = rtwdev->chip; 3186e3ec7017SPing-Ke Shih 3187e3ec7017SPing-Ke Shih rtw89_phy_stat_init(rtwdev); 3188e3ec7017SPing-Ke Shih 3189e3ec7017SPing-Ke Shih rtw89_chip_bb_sethw(rtwdev); 3190e3ec7017SPing-Ke Shih 3191e3ec7017SPing-Ke Shih rtw89_phy_env_monitor_init(rtwdev); 3192eb4e52b3SPo Hao Huang rtw89_physts_parsing_init(rtwdev); 3193e3ec7017SPing-Ke Shih rtw89_phy_dig_init(rtwdev); 3194e3ec7017SPing-Ke Shih rtw89_phy_cfo_init(rtwdev); 3195e3ec7017SPing-Ke Shih 3196e3ec7017SPing-Ke Shih rtw89_phy_init_rf_nctl(rtwdev); 3197e3ec7017SPing-Ke Shih rtw89_chip_rfk_init(rtwdev); 3198e3ec7017SPing-Ke Shih rtw89_load_txpwr_table(rtwdev, chip->byr_table); 3199e3ec7017SPing-Ke Shih rtw89_chip_set_txpwr_ctrl(rtwdev); 3200e3ec7017SPing-Ke Shih rtw89_chip_power_trim(rtwdev); 3201e3ec7017SPing-Ke Shih } 3202e3ec7017SPing-Ke Shih 3203e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 3204e3ec7017SPing-Ke Shih { 3205e3ec7017SPing-Ke Shih enum rtw89_phy_idx phy_idx = RTW89_PHY_0; 3206e3ec7017SPing-Ke Shih u8 bss_color; 3207e3ec7017SPing-Ke Shih 3208e3ec7017SPing-Ke Shih if (!vif->bss_conf.he_support || !vif->bss_conf.assoc) 3209e3ec7017SPing-Ke Shih return; 3210e3ec7017SPing-Ke Shih 3211e3ec7017SPing-Ke Shih bss_color = vif->bss_conf.he_bss_color.color; 3212e3ec7017SPing-Ke Shih 3213e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_VLD0, 0x1, 3214e3ec7017SPing-Ke Shih phy_idx); 3215e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_TGT, bss_color, 3216e3ec7017SPing-Ke Shih phy_idx); 3217e3ec7017SPing-Ke Shih rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_STAID, 3218e3ec7017SPing-Ke Shih vif->bss_conf.aid, phy_idx); 3219e3ec7017SPing-Ke Shih } 3220db7fa61aSZong-Zhe Yang 3221db7fa61aSZong-Zhe Yang static void 3222db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 3223db7fa61aSZong-Zhe Yang { 3224db7fa61aSZong-Zhe Yang rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data); 3225db7fa61aSZong-Zhe Yang } 3226db7fa61aSZong-Zhe Yang 3227db7fa61aSZong-Zhe Yang static void 3228db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 3229db7fa61aSZong-Zhe Yang { 3230db7fa61aSZong-Zhe Yang rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data); 3231db7fa61aSZong-Zhe Yang } 3232db7fa61aSZong-Zhe Yang 3233db7fa61aSZong-Zhe Yang static void 3234db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 3235db7fa61aSZong-Zhe Yang { 3236db7fa61aSZong-Zhe Yang rtw89_phy_write32_set(rtwdev, def->addr, def->mask); 3237db7fa61aSZong-Zhe Yang } 3238db7fa61aSZong-Zhe Yang 3239db7fa61aSZong-Zhe Yang static void 3240db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 3241db7fa61aSZong-Zhe Yang { 3242db7fa61aSZong-Zhe Yang rtw89_phy_write32_clr(rtwdev, def->addr, def->mask); 3243db7fa61aSZong-Zhe Yang } 3244db7fa61aSZong-Zhe Yang 3245db7fa61aSZong-Zhe Yang static void 3246db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 3247db7fa61aSZong-Zhe Yang { 3248db7fa61aSZong-Zhe Yang udelay(def->data); 3249db7fa61aSZong-Zhe Yang } 3250db7fa61aSZong-Zhe Yang 3251db7fa61aSZong-Zhe Yang static void 3252db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = { 3253db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WRF] = _rfk_write_rf, 3254db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WM] = _rfk_write32_mask, 3255db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WS] = _rfk_write32_set, 3256db7fa61aSZong-Zhe Yang [RTW89_RFK_F_WC] = _rfk_write32_clr, 3257db7fa61aSZong-Zhe Yang [RTW89_RFK_F_DELAY] = _rfk_delay, 3258db7fa61aSZong-Zhe Yang }; 3259db7fa61aSZong-Zhe Yang 3260db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM); 3261db7fa61aSZong-Zhe Yang 3262db7fa61aSZong-Zhe Yang void 3263db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl) 3264db7fa61aSZong-Zhe Yang { 3265db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *p = tbl->defs; 3266db7fa61aSZong-Zhe Yang const struct rtw89_reg5_def *end = tbl->defs + tbl->size; 3267db7fa61aSZong-Zhe Yang 3268db7fa61aSZong-Zhe Yang for (; p < end; p++) 3269db7fa61aSZong-Zhe Yang _rfk_handler[p->flag](rtwdev, p); 3270db7fa61aSZong-Zhe Yang } 3271db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser); 3272