1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
8 #include "odm_precomp.h"
10 #define READ_AND_CONFIG_MP(ic, txt) (ODM_ReadAndConfig_MP_##ic##txt(pDM_Odm))
11 #define READ_AND_CONFIG READ_AND_CONFIG_MP
13 static u8 odm_query_rx_pwr_percentage(s8 ant_power)
15 if ((ant_power <= -100) || (ant_power >= 20))
17 else if (ant_power >= 0)
20 return 100 + ant_power;
24 s32 odm_signal_scale_mapping(struct dm_odm_t *dm_odm, s32 curr_sig)
28 if (dm_odm->SupportInterface == ODM_ITRF_SDIO) {
29 if (curr_sig >= 51 && curr_sig <= 100)
31 else if (curr_sig >= 41 && curr_sig <= 50)
32 ret_sig = 80 + ((curr_sig - 40)*2);
33 else if (curr_sig >= 31 && curr_sig <= 40)
34 ret_sig = 66 + (curr_sig - 30);
35 else if (curr_sig >= 21 && curr_sig <= 30)
36 ret_sig = 54 + (curr_sig - 20);
37 else if (curr_sig >= 10 && curr_sig <= 20)
38 ret_sig = 42 + (((curr_sig - 10) * 2) / 3);
39 else if (curr_sig >= 5 && curr_sig <= 9)
40 ret_sig = 22 + (((curr_sig - 5) * 3) / 2);
41 else if (curr_sig >= 1 && curr_sig <= 4)
42 ret_sig = 6 + (((curr_sig - 1) * 3) / 2);
50 static u8 odm_evm_db_to_percentage(s8 value)
53 /* -33dB~0dB to 0%~99% */
65 ret_val = 0 - ret_val;
74 static s8 odm_cck_rssi(u8 lna_idx, u8 vga_idx)
79 /* 46 53 73 95 201301231630 */
80 /* 46 53 77 99 201301241630 */
83 rx_pwr_all = -34 - (2 * vga_idx);
86 rx_pwr_all = -14 - (2 * vga_idx);
89 rx_pwr_all = 6 - (2 * vga_idx);
92 rx_pwr_all = 16 - (2 * vga_idx);
95 /* rx_pwr_all = -53+(2*(31-VGA_idx)); */
101 static void odm_rx_phy_status_parsing(struct dm_odm_t *dm_odm,
102 struct odm_phy_info *phy_info,
104 struct odm_packet_info *pkt_info)
107 s8 rx_pwr[4], rx_pwr_all = 0;
108 u8 evm, pwdb_all = 0, pwdb_all_bt;
109 u8 rssi, total_rssi = 0;
110 bool is_cck_rate = false;
113 struct phy_status_rpt_8192cd_t *phy_sta_rpt = (struct phy_status_rpt_8192cd_t *)phy_status;
115 is_cck_rate = pkt_info->data_rate <= DESC_RATE11M;
116 phy_info->rx_mimo_signal_quality[RF_PATH_A] = -1;
117 phy_info->rx_mimo_signal_quality[RF_PATH_B] = -1;
123 dm_odm->PhyDbgInfo.NumQryPhyStatusCCK++;
126 * (1)Hardware does not provide RSSI for CCK/
127 * (2)PWDB, Average PWDB calculated by
128 * hardware (for rate adaptive)
131 cck_agc_rpt = phy_sta_rpt->cck_agc_rpt_ofdm_cfosho_a;
134 * 2011.11.28 LukeLee: 88E use different LNA & VGA gain table
135 * The RSSI formula should be modified according to the gain table
137 lna_idx = ((cck_agc_rpt & 0xE0)>>5);
138 vga_idx = (cck_agc_rpt & 0x1F);
139 rx_pwr_all = odm_cck_rssi(lna_idx, vga_idx);
140 pwdb_all = odm_query_rx_pwr_percentage(rx_pwr_all);
144 phy_info->rx_pwd_ba11 = pwdb_all;
145 phy_info->bt_rx_rssi_percentage = pwdb_all;
146 phy_info->recv_signal_power = rx_pwr_all;
148 /* (3) Get Signal Quality (EVM) */
150 /* if (pPktinfo->bPacketMatchBSSID) */
154 if (phy_info->rx_pwd_ba11 > 40 && !dm_odm->bInHctTest)
157 sq_rpt = phy_sta_rpt->cck_sig_qual_ofdm_pwdb_all;
161 else if (sq_rpt < 20)
164 sq = ((64-sq_rpt) * 100) / 44;
168 phy_info->signal_quality = sq;
169 phy_info->rx_mimo_signal_quality[RF_PATH_A] = sq;
170 phy_info->rx_mimo_signal_quality[RF_PATH_B] = -1;
172 } else { /* is OFDM rate */
173 dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
176 * (1)Get RSSI for HT rate
179 for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
180 /* 2008/01/30 MH we will judge RF RX path now. */
181 if (dm_odm->RFPathRxEnable & BIT(i))
186 rx_pwr[i] = ((phy_sta_rpt->path_agc[i].gain & 0x3F) * 2) - 110;
188 phy_info->rx_pwr[i] = rx_pwr[i];
190 /* Translate DBM to percentage. */
191 rssi = odm_query_rx_pwr_percentage(rx_pwr[i]);
194 phy_info->rx_mimo_signal_strength[i] = (u8)rssi;
196 /* Get Rx snr value in DB */
197 phy_info->rx_snr[i] = dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(phy_sta_rpt->path_rxsnr[i]/2);
201 * (2)PWDB, Average PWDB calculated by hardware (for rate adaptive)
203 rx_pwr_all = ((phy_sta_rpt->cck_sig_qual_ofdm_pwdb_all >> 1) & 0x7f) - 110;
205 pwdb_all_bt = pwdb_all = odm_query_rx_pwr_percentage(rx_pwr_all);
207 phy_info->rx_pwd_ba11 = pwdb_all;
208 phy_info->bt_rx_rssi_percentage = pwdb_all_bt;
209 phy_info->rx_power = rx_pwr_all;
210 phy_info->recv_signal_power = rx_pwr_all;
215 * Only spatial stream 1 makes sense
217 * Do not use shift operation like "rx_evmX >>= 1"
218 * because the compiler of free build environment
219 * fill most significant bit to "zero" when doing
220 * shifting operation which may change a negative
221 * value to positive one, then the dbm value (which
222 * is supposed to be negative) is not correct
225 evm = odm_evm_db_to_percentage(phy_sta_rpt->stream_rxevm[0]); /* dbm */
227 /* Fill value in RFD, Get the first spatial stream only */
228 phy_info->signal_quality = (u8)(evm & 0xff);
230 phy_info->rx_mimo_signal_quality[RF_PATH_A] = (u8)(evm & 0xff);
232 odm_parsing_cfo(dm_odm, pkt_info, phy_sta_rpt->path_cfotail);
236 * UI BSS List signal strength(in percentage), make it good
237 * looking, from 0~100.
238 * It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
241 phy_info->signal_strength = (u8)(odm_signal_scale_mapping(dm_odm, pwdb_all));
243 if (rf_rx_num != 0) {
244 phy_info->signal_strength = (u8)(odm_signal_scale_mapping(dm_odm, total_rssi /= rf_rx_num));
249 static void odm_Process_RSSIForDM(
250 struct dm_odm_t *pDM_Odm, struct odm_phy_info *pPhyInfo, struct odm_packet_info *pPktinfo
254 s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK, UndecoratedSmoothedOFDM, RSSI_Ave;
256 u8 RSSI_max, RSSI_min, i;
262 if (pPktinfo->station_id == 0xFF)
265 pEntry = pDM_Odm->pODM_StaInfo[pPktinfo->station_id];
267 if (!IS_STA_VALID(pEntry))
270 if ((!pPktinfo->bssid_match))
273 if (pPktinfo->is_beacon)
274 pDM_Odm->PhyDbgInfo.NumQryBeaconPkt++;
276 isCCKrate = ((pPktinfo->data_rate <= DESC_RATE11M)) ? true : false;
277 pDM_Odm->RxRate = pPktinfo->data_rate;
279 /* Statistic for antenna/path diversity------------------ */
280 if (pDM_Odm->SupportAbility & ODM_BB_ANT_DIV) {
284 /* Smart Antenna Debug Message------------------ */
286 UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
287 UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
288 UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
290 if (pPktinfo->to_self || pPktinfo->is_beacon) {
292 if (!isCCKrate) { /* ofdm rate */
293 if (pPhyInfo->rx_mimo_signal_strength[RF_PATH_B] == 0) {
294 RSSI_Ave = pPhyInfo->rx_mimo_signal_strength[RF_PATH_A];
295 pDM_Odm->RSSI_A = pPhyInfo->rx_mimo_signal_strength[RF_PATH_A];
298 pDM_Odm->RSSI_A = pPhyInfo->rx_mimo_signal_strength[RF_PATH_A];
299 pDM_Odm->RSSI_B = pPhyInfo->rx_mimo_signal_strength[RF_PATH_B];
302 pPhyInfo->rx_mimo_signal_strength[RF_PATH_A] >
303 pPhyInfo->rx_mimo_signal_strength[RF_PATH_B]
305 RSSI_max = pPhyInfo->rx_mimo_signal_strength[RF_PATH_A];
306 RSSI_min = pPhyInfo->rx_mimo_signal_strength[RF_PATH_B];
308 RSSI_max = pPhyInfo->rx_mimo_signal_strength[RF_PATH_B];
309 RSSI_min = pPhyInfo->rx_mimo_signal_strength[RF_PATH_A];
312 if ((RSSI_max-RSSI_min) < 3)
314 else if ((RSSI_max-RSSI_min) < 6)
315 RSSI_Ave = RSSI_max - 1;
316 else if ((RSSI_max-RSSI_min) < 10)
317 RSSI_Ave = RSSI_max - 2;
319 RSSI_Ave = RSSI_max - 3;
322 /* 1 Process OFDM RSSI */
323 if (UndecoratedSmoothedOFDM <= 0) /* initialize */
324 UndecoratedSmoothedOFDM = pPhyInfo->rx_pwd_ba11;
326 if (pPhyInfo->rx_pwd_ba11 > (u32)UndecoratedSmoothedOFDM) {
327 UndecoratedSmoothedOFDM =
328 ((UndecoratedSmoothedOFDM*(Rx_Smooth_Factor-1)) +
329 RSSI_Ave)/Rx_Smooth_Factor;
330 UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
332 UndecoratedSmoothedOFDM =
333 ((UndecoratedSmoothedOFDM*(Rx_Smooth_Factor-1)) +
334 RSSI_Ave)/Rx_Smooth_Factor;
338 pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT0;
341 RSSI_Ave = pPhyInfo->rx_pwd_ba11;
342 pDM_Odm->RSSI_A = (u8) pPhyInfo->rx_pwd_ba11;
345 /* 1 Process CCK RSSI */
346 if (UndecoratedSmoothedCCK <= 0) /* initialize */
347 UndecoratedSmoothedCCK = pPhyInfo->rx_pwd_ba11;
349 if (pPhyInfo->rx_pwd_ba11 > (u32)UndecoratedSmoothedCCK) {
350 UndecoratedSmoothedCCK =
351 ((UndecoratedSmoothedCCK*(Rx_Smooth_Factor-1)) +
352 pPhyInfo->rx_pwd_ba11)/Rx_Smooth_Factor;
353 UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
355 UndecoratedSmoothedCCK =
356 ((UndecoratedSmoothedCCK*(Rx_Smooth_Factor-1)) +
357 pPhyInfo->rx_pwd_ba11)/Rx_Smooth_Factor;
360 pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
365 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
366 if (pEntry->rssi_stat.ValidBit >= 64)
367 pEntry->rssi_stat.ValidBit = 64;
369 pEntry->rssi_stat.ValidBit++;
371 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
372 OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i)&BIT0;
374 if (pEntry->rssi_stat.ValidBit == 64) {
375 Weighting = ((OFDM_pkt<<4) > 64)?64:(OFDM_pkt<<4);
376 UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
378 if (pEntry->rssi_stat.ValidBit != 0)
379 UndecoratedSmoothedPWDB = (OFDM_pkt*UndecoratedSmoothedOFDM+(pEntry->rssi_stat.ValidBit-OFDM_pkt)*UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
381 UndecoratedSmoothedPWDB = 0;
384 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
385 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
386 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
394 /* Endianness before calling this API */
396 void odm_phy_status_query(struct dm_odm_t *dm_odm, struct odm_phy_info *phy_info,
397 u8 *phy_status, struct odm_packet_info *pkt_info)
400 odm_rx_phy_status_parsing(dm_odm, phy_info, phy_status, pkt_info);
402 if (!dm_odm->RSSI_test)
403 odm_Process_RSSIForDM(dm_odm, phy_info, pkt_info);
407 /* If you want to add a new IC, Please follow below template and generate a new one. */
411 enum hal_status ODM_ConfigRFWithHeaderFile(
412 struct dm_odm_t *pDM_Odm,
413 enum ODM_RF_Config_Type ConfigType,
417 if (ConfigType == CONFIG_RF_RADIO)
418 READ_AND_CONFIG(8723B, _RadioA);
419 else if (ConfigType == CONFIG_RF_TXPWR_LMT)
420 READ_AND_CONFIG(8723B, _TXPWR_LMT);
422 return HAL_STATUS_SUCCESS;
425 enum hal_status ODM_ConfigRFWithTxPwrTrackHeaderFile(struct dm_odm_t *pDM_Odm)
427 if (pDM_Odm->SupportInterface == ODM_ITRF_SDIO)
428 READ_AND_CONFIG(8723B, _TxPowerTrack_SDIO);
430 return HAL_STATUS_SUCCESS;
433 enum hal_status ODM_ConfigBBWithHeaderFile(
434 struct dm_odm_t *pDM_Odm, enum ODM_BB_Config_Type ConfigType
437 if (ConfigType == CONFIG_BB_PHY_REG)
438 READ_AND_CONFIG(8723B, _PHY_REG);
439 else if (ConfigType == CONFIG_BB_AGC_TAB)
440 READ_AND_CONFIG(8723B, _AGC_TAB);
441 else if (ConfigType == CONFIG_BB_PHY_REG_PG)
442 READ_AND_CONFIG(8723B, _PHY_REG_PG);
444 return HAL_STATUS_SUCCESS;