sys/dev/netif/ath/ath_hal/ar5212/ar5212_rfgain.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting * Copyright (c) 2002-2008 Atheros Communications, Inc. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * $FreeBSD$ */ #include "opt_ah.h" #include "ah.h" #include "ah_internal.h" #include "ah_devid.h" #include "ar5212/ar5212.h" #include "ar5212/ar5212reg.h" #include "ar5212/ar5212phy.h" #include "ah_eeprom_v3.h" static const GAIN_OPTIMIZATION_LADDER gainLadder = { 9, /* numStepsInLadder */ 4, /* defaultStepNum */ { { {4, 1, 1, 1}, 6, "FG8"}, { {4, 0, 1, 1}, 4, "FG7"}, { {3, 1, 1, 1}, 3, "FG6"}, { {4, 0, 0, 1}, 1, "FG5"}, { {4, 1, 1, 0}, 0, "FG4"}, /* noJack */ { {4, 0, 1, 0}, -2, "FG3"}, /* halfJack */ { {3, 1, 1, 0}, -3, "FG2"}, /* clip3 */ { {4, 0, 0, 0}, -4, "FG1"}, /* noJack */ { {2, 1, 1, 0}, -6, "FG0"} /* clip2 */ } }; static const GAIN_OPTIMIZATION_LADDER gainLadder5112 = { 8, /* numStepsInLadder */ 1, /* defaultStepNum */ { { {3, 0,0,0, 0,0,0}, 6, "FG7"}, /* most fixed gain */ { {2, 0,0,0, 0,0,0}, 0, "FG6"}, { {1, 0,0,0, 0,0,0}, -3, "FG5"}, { {0, 0,0,0, 0,0,0}, -6, "FG4"}, { {0, 1,1,0, 0,0,0}, -8, "FG3"}, { {0, 1,1,0, 1,1,0}, -10, "FG2"}, { {0, 1,0,1, 1,1,0}, -13, "FG1"}, { {0, 1,0,1, 1,0,1}, -16, "FG0"}, /* least fixed gain */ } }; /* * Initialize the gain structure to good values */ void ar5212InitializeGainValues(struct ath_hal *ah) { struct ath_hal_5212 *ahp = AH5212(ah); GAIN_VALUES *gv = &ahp->ah_gainValues; /* initialize gain optimization values */ if (IS_RAD5112_ANY(ah)) { gv->currStepNum = gainLadder5112.defaultStepNum; gv->currStep = &gainLadder5112.optStep[gainLadder5112.defaultStepNum]; gv->active = AH_TRUE; gv->loTrig = 20; gv->hiTrig = 85; } else { gv->currStepNum = gainLadder.defaultStepNum; gv->currStep = &gainLadder.optStep[gainLadder.defaultStepNum]; gv->active = AH_TRUE; gv->loTrig = 20; gv->hiTrig = 35; } } #define MAX_ANALOG_START 319 /* XXX */ /* * Find analog bits of given parameter data and return a reversed value */ static uint32_t ar5212GetRfField(uint32_t *rfBuf, uint32_t numBits, uint32_t firstBit, uint32_t column) { uint32_t reg32 = 0, mask, arrayEntry, lastBit; uint32_t bitPosition, bitsShifted; int32_t bitsLeft; HALASSERT(column <= 3); HALASSERT(numBits <= 32); HALASSERT(firstBit + numBits <= MAX_ANALOG_START); arrayEntry = (firstBit - 1) / 8; bitPosition = (firstBit - 1) % 8; bitsLeft = numBits; bitsShifted = 0; while (bitsLeft > 0) { lastBit = (bitPosition + bitsLeft > 8) ? (8) : (bitPosition + bitsLeft); mask = (((1 << lastBit) - 1) ^ ((1 << bitPosition) - 1)) << (column * 8); reg32 |= (((rfBuf[arrayEntry] & mask) >> (column * 8)) >> bitPosition) << bitsShifted; bitsShifted += lastBit - bitPosition; bitsLeft -= (8 - bitPosition); bitPosition = 0; arrayEntry++; } reg32 = ath_hal_reverseBits(reg32, numBits); return reg32; } static HAL_BOOL ar5212InvalidGainReadback(struct ath_hal *ah, GAIN_VALUES *gv) { uint32_t gStep, g, mixOvr; uint32_t L1, L2, L3, L4; if (IS_RAD5112_ANY(ah)) { mixOvr = ar5212GetRfField(ar5212GetRfBank(ah, 7), 1, 36, 0); L1 = 0; L2 = 107; L3 = 0; L4 = 107; if (mixOvr == 1) { L2 = 83; L4 = 83; gv->hiTrig = 55; } } else { gStep = ar5212GetRfField(ar5212GetRfBank(ah, 7), 6, 37, 0); L1 = 0; L2 = (gStep == 0x3f) ? 50 : gStep + 4; L3 = (gStep != 0x3f) ? 0x40 : L1; L4 = L3 + 50; gv->loTrig = L1 + (gStep == 0x3f ? DYN_ADJ_LO_MARGIN : 0); /* never adjust if != 0x3f */ gv->hiTrig = L4 - (gStep == 0x3f ? DYN_ADJ_UP_MARGIN : -5); } g = gv->currGain; return !((g >= L1 && g<= L2) || (g >= L3 && g <= L4)); } /* * Enable the probe gain check on the next packet */ void ar5212RequestRfgain(struct ath_hal *ah) { struct ath_hal_5212 *ahp = AH5212(ah); uint32_t probePowerIndex; /* Enable the gain readback probe */ probePowerIndex = ahp->ah_ofdmTxPower + ahp->ah_txPowerIndexOffset; OS_REG_WRITE(ah, AR_PHY_PAPD_PROBE, SM(probePowerIndex, AR_PHY_PAPD_PROBE_POWERTX) | AR_PHY_PAPD_PROBE_NEXT_TX); ahp->ah_rfgainState = HAL_RFGAIN_READ_REQUESTED; } /* * Check to see if our readback gain level sits within the linear * region of our current variable attenuation window */ static HAL_BOOL ar5212IsGainAdjustNeeded(struct ath_hal *ah, const GAIN_VALUES *gv) { return (gv->currGain <= gv->loTrig || gv->currGain >= gv->hiTrig); } /* * Move the rabbit ears in the correct direction. */ static int32_t ar5212AdjustGain(struct ath_hal *ah, GAIN_VALUES *gv) { const GAIN_OPTIMIZATION_LADDER *gl; if (IS_RAD5112_ANY(ah)) gl = &gainLadder5112; else gl = &gainLadder; gv->currStep = &gl->optStep[gv->currStepNum]; if (gv->currGain >= gv->hiTrig) { if (gv->currStepNum == 0) { HALDEBUG(ah, HAL_DEBUG_ANY, "%s: Max gain limit.\n", __func__); return -1; } HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: Adding gain: currG=%d [%s] --> ", __func__, gv->currGain, gv->currStep->stepName); gv->targetGain = gv->currGain; while (gv->targetGain >= gv->hiTrig && gv->currStepNum > 0) { gv->targetGain -= 2 * (gl->optStep[--(gv->currStepNum)].stepGain - gv->currStep->stepGain); gv->currStep = &gl->optStep[gv->currStepNum]; } HALDEBUG(ah, HAL_DEBUG_RFPARAM, "targG=%d [%s]\n", gv->targetGain, gv->currStep->stepName); return 1; } if (gv->currGain <= gv->loTrig) { if (gv->currStepNum == gl->numStepsInLadder-1) { HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: Min gain limit.\n", __func__); return -2; } HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: Deducting gain: currG=%d [%s] --> ", __func__, gv->currGain, gv->currStep->stepName); gv->targetGain = gv->currGain; while (gv->targetGain <= gv->loTrig && gv->currStepNum < (gl->numStepsInLadder - 1)) { gv->targetGain -= 2 * (gl->optStep[++(gv->currStepNum)].stepGain - gv->currStep->stepGain); gv->currStep = &gl->optStep[gv->currStepNum]; } HALDEBUG(ah, HAL_DEBUG_RFPARAM, "targG=%d [%s]\n", gv->targetGain, gv->currStep->stepName); return 2; } return 0; /* caller didn't call needAdjGain first */ } /* * Read rf register to determine if gainF needs correction */ static uint32_t ar5212GetGainFCorrection(struct ath_hal *ah) { struct ath_hal_5212 *ahp = AH5212(ah); uint32_t correction; HALASSERT(IS_RADX112_REV2(ah)); correction = 0; if (ar5212GetRfField(ar5212GetRfBank(ah, 7), 1, 36, 0) == 1) { const GAIN_VALUES *gv = &ahp->ah_gainValues; uint32_t mixGain = gv->currStep->paramVal[0]; uint32_t gainStep = ar5212GetRfField(ar5212GetRfBank(ah, 7), 4, 32, 0); switch (mixGain) { case 0 : correction = 0; break; case 1 : correction = gainStep; break; case 2 : correction = 2 * gainStep - 5; break; case 3 : correction = 2 * gainStep; break; } } return correction; } /* * Exported call to check for a recent gain reading and return * the current state of the thermal calibration gain engine. */ HAL_RFGAIN ar5212GetRfgain(struct ath_hal *ah) { struct ath_hal_5212 *ahp = AH5212(ah); GAIN_VALUES *gv = &ahp->ah_gainValues; uint32_t rddata, probeType; /* NB: beware of touching the BB when PHY is powered down */ if (!gv->active || !ahp->ah_phyPowerOn) return HAL_RFGAIN_INACTIVE; if (ahp->ah_rfgainState == HAL_RFGAIN_READ_REQUESTED) { /* Caller had asked to setup a new reading. Check it. */ rddata = OS_REG_READ(ah, AR_PHY_PAPD_PROBE); if ((rddata & AR_PHY_PAPD_PROBE_NEXT_TX) == 0) { /* bit got cleared, we have a new reading. */ gv->currGain = rddata >> AR_PHY_PAPD_PROBE_GAINF_S; probeType = MS(rddata, AR_PHY_PAPD_PROBE_TYPE); if (probeType == AR_PHY_PAPD_PROBE_TYPE_CCK) { const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom; HALASSERT(IS_RAD5112_ANY(ah)); HALASSERT(ah->ah_magic == AR5212_MAGIC); if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_2) gv->currGain += ee->ee_cckOfdmGainDelta; else gv->currGain += PHY_PROBE_CCK_CORRECTION; } if (IS_RADX112_REV2(ah)) { uint32_t correct = ar5212GetGainFCorrection(ah); if (gv->currGain >= correct) gv->currGain -= correct; else gv->currGain = 0; } /* inactive by default */ ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE; if (!ar5212InvalidGainReadback(ah, gv) && ar5212IsGainAdjustNeeded(ah, gv) && ar5212AdjustGain(ah, gv) > 0) { /* * Change needed. Copy ladder info * into eeprom info. */ ahp->ah_rfgainState = HAL_RFGAIN_NEED_CHANGE; /* for ap51 */ ahp->ah_cwCalRequire = AH_TRUE; /* Request IQ recalibration for temperature chang */ ahp->ah_bIQCalibration = IQ_CAL_INACTIVE; } } } return ahp->ah_rfgainState; } |