linux/drivers/staging/r8188eu/hal/odm_HWConfig.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2007 - 2011 Realtek Corporation. */
   3
   4#include "../include/drv_types.h"
   5
   6#define READ_AND_CONFIG     READ_AND_CONFIG_MP
   7
   8#define READ_AND_CONFIG_MP(ic, txt) (ODM_ReadAndConfig##txt##ic(dm_odm))
   9
  10static u8 odm_QueryRxPwrPercentage(s8 AntPower)
  11{
  12        if ((AntPower <= -100) || (AntPower >= 20))
  13                return  0;
  14        else if (AntPower >= 0)
  15                return  100;
  16        else
  17                return 100 + AntPower;
  18}
  19
  20static s32 odm_SignalScaleMapping(struct odm_dm_struct *dm_odm, s32 CurrSig)
  21{
  22        s32 RetSig = 0;
  23
  24        if (CurrSig >= 51 && CurrSig <= 100)
  25                RetSig = 100;
  26        else if (CurrSig >= 41 && CurrSig <= 50)
  27                RetSig = 80 + ((CurrSig - 40) * 2);
  28        else if (CurrSig >= 31 && CurrSig <= 40)
  29                RetSig = 66 + (CurrSig - 30);
  30        else if (CurrSig >= 21 && CurrSig <= 30)
  31                RetSig = 54 + (CurrSig - 20);
  32        else if (CurrSig >= 10 && CurrSig <= 20)
  33                RetSig = 42 + (((CurrSig - 10) * 2) / 3);
  34        else if (CurrSig >= 5 && CurrSig <= 9)
  35                RetSig = 22 + (((CurrSig - 5) * 3) / 2);
  36        else if (CurrSig >= 1 && CurrSig <= 4)
  37                RetSig = 6 + (((CurrSig - 1) * 3) / 2);
  38        else
  39                RetSig = CurrSig;
  40
  41        return RetSig;
  42}
  43
  44static u8 odm_evm_db_to_percentage(s8 value)
  45{
  46        /*  -33dB~0dB to 0%~99% */
  47        s8 ret_val = clamp(-value, 0, 33) * 3;
  48
  49        if (ret_val == 99)
  50                ret_val = 100;
  51
  52        return ret_val;
  53}
  54
  55static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
  56                        struct phy_info *pPhyInfo,
  57                        u8 *pPhyStatus,
  58                        struct odm_per_pkt_info *pPktinfo,
  59                        struct adapter *adapt)
  60{
  61        u8 i, Max_spatial_stream;
  62        s8 rx_pwr[4], rx_pwr_all = 0;
  63        u8 EVM, PWDB_ALL = 0;
  64        u8 RSSI, total_rssi = 0;
  65        u8 isCCKrate = 0;
  66        u8 rf_rx_num = 0;
  67        u8 cck_highpwr = 0;
  68        u8 LNA_idx, VGA_idx;
  69
  70        struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
  71
  72        isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
  73
  74        pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = -1;
  75        pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
  76
  77        if (isCCKrate) {
  78                u8 cck_agc_rpt;
  79
  80                dm_odm->PhyDbgInfo.NumQryPhyStatusCCK++;
  81                /*  (1)Hardware does not provide RSSI for CCK */
  82                /*  (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
  83
  84                cck_highpwr = dm_odm->bCckHighPower;
  85
  86                cck_agc_rpt =  pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
  87
  88                /* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
  89                /* The RSSI formula should be modified according to the gain table */
  90                /* In 88E, cck_highpwr is always set to 1 */
  91                LNA_idx = ((cck_agc_rpt & 0xE0) >> 5);
  92                VGA_idx = (cck_agc_rpt & 0x1F);
  93                switch (LNA_idx) {
  94                case 7:
  95                        if (VGA_idx <= 27)
  96                                rx_pwr_all = -100 + 2 * (27 - VGA_idx); /* VGA_idx = 27~2 */
  97                        else
  98                                rx_pwr_all = -100;
  99                        break;
 100                case 6:
 101                        rx_pwr_all = -48 + 2 * (2 - VGA_idx); /* VGA_idx = 2~0 */
 102                        break;
 103                case 5:
 104                        rx_pwr_all = -42 + 2 * (7 - VGA_idx); /* VGA_idx = 7~5 */
 105                        break;
 106                case 4:
 107                        rx_pwr_all = -36 + 2 * (7 - VGA_idx); /* VGA_idx = 7~4 */
 108                        break;
 109                case 3:
 110                        rx_pwr_all = -24 + 2 * (7 - VGA_idx); /* VGA_idx = 7~0 */
 111                        break;
 112                case 2:
 113                        if (cck_highpwr)
 114                                rx_pwr_all = -12 + 2 * (5 - VGA_idx); /* VGA_idx = 5~0 */
 115                        else
 116                                rx_pwr_all = -6 + 2 * (5 - VGA_idx);
 117                        break;
 118                case 1:
 119                                rx_pwr_all = 8 - 2 * VGA_idx;
 120                        break;
 121                case 0:
 122                                rx_pwr_all = 14 - 2 * VGA_idx;
 123                        break;
 124                default:
 125                        break;
 126                }
 127                rx_pwr_all += 6;
 128                PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
 129                if (!cck_highpwr) {
 130                        if (PWDB_ALL >= 80)
 131                                PWDB_ALL = ((PWDB_ALL - 80) << 1) + ((PWDB_ALL - 80) >> 1) + 80;
 132                        else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
 133                                PWDB_ALL += 3;
 134                        if (PWDB_ALL > 100)
 135                                PWDB_ALL = 100;
 136                }
 137
 138                pPhyInfo->RxPWDBAll = PWDB_ALL;
 139                pPhyInfo->recvpower = rx_pwr_all;
 140                /*  (3) Get Signal Quality (EVM) */
 141                if (pPktinfo->bPacketMatchBSSID) {
 142                        u8 SQ, SQ_rpt;
 143
 144                        if (pPhyInfo->RxPWDBAll > 40) {
 145                                SQ = 100;
 146                        } else {
 147                                SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
 148
 149                                if (SQ_rpt > 64)
 150                                        SQ = 0;
 151                                else if (SQ_rpt < 20)
 152                                        SQ = 100;
 153                                else
 154                                        SQ = ((64 - SQ_rpt) * 100) / 44;
 155                        }
 156                        pPhyInfo->SignalQuality = SQ;
 157                        pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = SQ;
 158                        pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
 159                }
 160        } else { /* is OFDM rate */
 161                dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
 162
 163                /*  (1)Get RSSI for HT rate */
 164
 165                for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
 166                        /*  2008/01/30 MH we will judge RF RX path now. */
 167                        if (dm_odm->RFPathRxEnable & BIT(i))
 168                                rf_rx_num++;
 169
 170                        rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F) * 2) - 110;
 171                        if (i == RF_PATH_A)
 172                                adapt->signal_strength = rx_pwr[i];
 173
 174                        pPhyInfo->RxPwr[i] = rx_pwr[i];
 175
 176                        /* Translate DBM to percentage. */
 177                        RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
 178                        total_rssi += RSSI;
 179
 180                        pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
 181
 182                        /* Get Rx snr value in DB */
 183                        pPhyInfo->RxSNR[i] = (s32)(pPhyStaRpt->path_rxsnr[i] / 2);
 184                        dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i] / 2);
 185                }
 186                /*  (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive) */
 187                rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f) - 110;
 188
 189                PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
 190
 191                pPhyInfo->RxPWDBAll = PWDB_ALL;
 192                pPhyInfo->RxPower = rx_pwr_all;
 193                pPhyInfo->recvpower = rx_pwr_all;
 194
 195                /*  (3)EVM of HT rate */
 196                if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
 197                        Max_spatial_stream = 2; /* both spatial stream make sense */
 198                else
 199                        Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
 200
 201                for (i = 0; i < Max_spatial_stream; i++) {
 202                        /*  Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
 203                        /*  fill most significant bit to "zero" when doing shifting operation which may change a negative */
 204                        /*  value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore. */
 205                        EVM = odm_evm_db_to_percentage((pPhyStaRpt->stream_rxevm[i]));  /* dbm */
 206
 207                        if (pPktinfo->bPacketMatchBSSID) {
 208                                if (i == RF_PATH_A) /*  Fill value in RFD, Get the first spatial stream only */
 209                                        pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
 210                                pPhyInfo->RxMIMOSignalQuality[i] = (u8)(EVM & 0xff);
 211                        }
 212                }
 213        }
 214        /* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
 215        /* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
 216        if (isCCKrate) {
 217                pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
 218        } else {
 219                if (rf_rx_num != 0)
 220                        pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, total_rssi /= rf_rx_num));
 221        }
 222
 223        /* For 88E HW Antenna Diversity */
 224        dm_odm->DM_FatTable.antsel_rx_keep_0 = pPhyStaRpt->ant_sel;
 225        dm_odm->DM_FatTable.antsel_rx_keep_1 = pPhyStaRpt->ant_sel_b;
 226        dm_odm->DM_FatTable.antsel_rx_keep_2 = pPhyStaRpt->antsel_rx_keep_2;
 227}
 228
 229static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
 230                                  struct phy_info *pPhyInfo,
 231                                  struct odm_per_pkt_info *pPktinfo)
 232{
 233        s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
 234        s32 UndecoratedSmoothedOFDM, RSSI_Ave;
 235        u8 isCCKrate = 0;
 236        u8 RSSI_max, RSSI_min, i;
 237        u32 OFDM_pkt = 0;
 238        u32 Weighting = 0;
 239        struct sta_info *pEntry;
 240        u8 antsel_tr_mux;
 241        struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
 242
 243        if (pPktinfo->StationID == 0xFF)
 244                return;
 245        pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
 246        if (!IS_STA_VALID(pEntry))
 247                return;
 248        if ((!pPktinfo->bPacketMatchBSSID))
 249                return;
 250
 251        isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
 252
 253        /* Smart Antenna Debug Message------------------  */
 254        if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
 255                if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
 256                        antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2 << 2) |
 257                                        (pDM_FatTable->antsel_rx_keep_1 << 1) | pDM_FatTable->antsel_rx_keep_0;
 258                        ODM_AntselStatistics_88E(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
 259                }
 260        }
 261
 262        /* Smart Antenna Debug Message------------------ */
 263
 264        UndecoratedSmoothedCCK =  pEntry->rssi_stat.UndecoratedSmoothedCCK;
 265        UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
 266        UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
 267
 268        if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
 269                if (!isCCKrate) { /* ofdm rate */
 270                        if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
 271                                RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
 272                        } else {
 273                                if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
 274                                        RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
 275                                        RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
 276                                } else {
 277                                        RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
 278                                        RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
 279                                }
 280                                if ((RSSI_max - RSSI_min) < 3)
 281                                        RSSI_Ave = RSSI_max;
 282                                else if ((RSSI_max - RSSI_min) < 6)
 283                                        RSSI_Ave = RSSI_max - 1;
 284                                else if ((RSSI_max - RSSI_min) < 10)
 285                                        RSSI_Ave = RSSI_max - 2;
 286                                else
 287                                        RSSI_Ave = RSSI_max - 3;
 288                        }
 289
 290                        /* 1 Process OFDM RSSI */
 291                        if (UndecoratedSmoothedOFDM <= 0) {     /*  initialize */
 292                                UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
 293                        } else {
 294                                if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
 295                                        UndecoratedSmoothedOFDM =
 296                                                        (((UndecoratedSmoothedOFDM) * (Rx_Smooth_Factor - 1)) +
 297                                                        (RSSI_Ave)) / (Rx_Smooth_Factor);
 298                                        UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
 299                                } else {
 300                                        UndecoratedSmoothedOFDM =
 301                                                        (((UndecoratedSmoothedOFDM) * (Rx_Smooth_Factor - 1)) +
 302                                                        (RSSI_Ave)) / (Rx_Smooth_Factor);
 303                                }
 304                        }
 305
 306                        pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap << 1) | BIT(0);
 307
 308                } else {
 309                        RSSI_Ave = pPhyInfo->RxPWDBAll;
 310
 311                        /* 1 Process CCK RSSI */
 312                        if (UndecoratedSmoothedCCK <= 0) {      /*  initialize */
 313                                UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
 314                        } else {
 315                                if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
 316                                        UndecoratedSmoothedCCK =
 317                                                        ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor - 1)) +
 318                                                        pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
 319                                        UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
 320                                } else {
 321                                        UndecoratedSmoothedCCK =
 322                                                        ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor - 1)) +
 323                                                        pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
 324                                }
 325                        }
 326                        pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap << 1;
 327                }
 328                /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
 329                if (pEntry->rssi_stat.ValidBit >= 64)
 330                        pEntry->rssi_stat.ValidBit = 64;
 331                else
 332                        pEntry->rssi_stat.ValidBit++;
 333
 334                for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
 335                        OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap >> i) & BIT(0);
 336
 337                if (pEntry->rssi_stat.ValidBit == 64) {
 338                        Weighting = ((OFDM_pkt << 4) > 64) ? 64 : (OFDM_pkt << 4);
 339                        UndecoratedSmoothedPWDB = (Weighting * UndecoratedSmoothedOFDM + (64 - Weighting) * UndecoratedSmoothedCCK) >> 6;
 340                } else {
 341                        if (pEntry->rssi_stat.ValidBit != 0)
 342                                UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
 343                                                          (pEntry->rssi_stat.ValidBit - OFDM_pkt) *
 344                                                          UndecoratedSmoothedCCK) / pEntry->rssi_stat.ValidBit;
 345                        else
 346                                UndecoratedSmoothedPWDB = 0;
 347                }
 348                pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
 349                pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
 350                pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
 351        }
 352}
 353
 354/*  Endianness before calling this API */
 355void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
 356                        struct phy_info *pPhyInfo,
 357                        u8 *pPhyStatus,
 358                        struct odm_per_pkt_info *pPktinfo,
 359                        struct adapter *adapt)
 360{
 361        odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus, pPktinfo, adapt);
 362        odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
 363}
 364
 365enum HAL_STATUS ODM_ConfigRFWithHeaderFile(struct odm_dm_struct *dm_odm,
 366                                           enum rf_radio_path content,
 367                                           enum rf_radio_path rfpath)
 368{
 369        if (rfpath == RF_PATH_A)
 370                READ_AND_CONFIG(8188E, _RadioA_1T_);
 371
 372        return HAL_STATUS_SUCCESS;
 373}
 374
 375enum HAL_STATUS ODM_ConfigBBWithHeaderFile(struct odm_dm_struct *dm_odm,
 376                                           enum odm_bb_config_type config_tp)
 377{
 378        if (config_tp == CONFIG_BB_PHY_REG) {
 379                READ_AND_CONFIG(8188E, _PHY_REG_1T_);
 380        } else if (config_tp == CONFIG_BB_AGC_TAB) {
 381                READ_AND_CONFIG(8188E, _AGC_TAB_1T_);
 382        } else if (config_tp == CONFIG_BB_PHY_REG_PG) {
 383                READ_AND_CONFIG(8188E, _PHY_REG_PG_);
 384        }
 385
 386        return HAL_STATUS_SUCCESS;
 387}
 388
 389enum HAL_STATUS ODM_ConfigMACWithHeaderFile(struct odm_dm_struct *dm_odm)
 390{
 391        u8 result = HAL_STATUS_SUCCESS;
 392        result = READ_AND_CONFIG(8188E, _MAC_REG_);
 393        return result;
 394}
 395