ar5416_cal.c revision 219480
1/* 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * Copyright (c) 2002-2008 Atheros Communications, Inc. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * 17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_cal.c 219480 2011-03-11 11:35:36Z adrian $ 18 */ 19#include "opt_ah.h" 20 21#include "ah.h" 22#include "ah_internal.h" 23#include "ah_devid.h" 24 25#include "ah_eeprom_v14.h" 26 27#include "ar5212/ar5212.h" /* for NF cal related declarations */ 28 29#include "ar5416/ar5416.h" 30#include "ar5416/ar5416reg.h" 31#include "ar5416/ar5416phy.h" 32 33/* Owl specific stuff */ 34#define NUM_NOISEFLOOR_READINGS 6 /* 3 chains * (ctl + ext) */ 35 36static void ar5416StartNFCal(struct ath_hal *ah); 37static void ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *); 38static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *); 39 40static uint16_t ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan); 41static void ar5416SanitizeNF(struct ath_hal *ah, int16_t *nf); 42 43/* 44 * Determine if calibration is supported by device and channel flags 45 */ 46 47/* 48 * ADC GAIN/DC offset calibration is for calibrating two ADCs that 49 * are acting as one by interleaving incoming symbols. This isn't 50 * relevant for 2.4GHz 20MHz wide modes because, as far as I can tell, 51 * the secondary ADC is never enabled. It is enabled however for 52 * 5GHz modes. 53 * 54 * It hasn't been confirmed whether doing this calibration is needed 55 * at all in the above modes and/or whether it's actually harmful. 56 * So for now, let's leave it enabled and just remember to get 57 * confirmation that it needs to be clarified. 58 * 59 * See US Patent No: US 7,541,952 B1: 60 * " Method and Apparatus for Offset and Gain Compensation for 61 * Analog-to-Digital Converters." 62 */ 63static OS_INLINE HAL_BOOL 64ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan, 65 HAL_CAL_TYPE calType) 66{ 67 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 68 69 switch (calType & cal->suppCals) { 70 case IQ_MISMATCH_CAL: 71 /* Run IQ Mismatch for non-CCK only */ 72 return !IEEE80211_IS_CHAN_B(chan); 73 case ADC_GAIN_CAL: 74 case ADC_DC_CAL: 75 /* Run ADC Gain Cal for non-CCK & non 2GHz-HT20 only */ 76 return !IEEE80211_IS_CHAN_B(chan) && 77 !(IEEE80211_IS_CHAN_2GHZ(chan) && IEEE80211_IS_CHAN_HT20(chan)); 78 } 79 return AH_FALSE; 80} 81 82/* 83 * Setup HW to collect samples used for current cal 84 */ 85static void 86ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal) 87{ 88 /* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */ 89 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4, 90 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX, 91 currCal->calData->calCountMax); 92 93 /* Select calibration to run */ 94 switch (currCal->calData->calType) { 95 case IQ_MISMATCH_CAL: 96 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ); 97 HALDEBUG(ah, HAL_DEBUG_PERCAL, 98 "%s: start IQ Mismatch calibration\n", __func__); 99 break; 100 case ADC_GAIN_CAL: 101 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN); 102 HALDEBUG(ah, HAL_DEBUG_PERCAL, 103 "%s: start ADC Gain calibration\n", __func__); 104 break; 105 case ADC_DC_CAL: 106 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER); 107 HALDEBUG(ah, HAL_DEBUG_PERCAL, 108 "%s: start ADC DC calibration\n", __func__); 109 break; 110 case ADC_DC_INIT_CAL: 111 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT); 112 HALDEBUG(ah, HAL_DEBUG_PERCAL, 113 "%s: start Init ADC DC calibration\n", __func__); 114 break; 115 } 116 /* Kick-off cal */ 117 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL); 118} 119 120/* 121 * Initialize shared data structures and prepare a cal to be run. 122 */ 123static void 124ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal) 125{ 126 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 127 128 /* Reset data structures shared between different calibrations */ 129 OS_MEMZERO(cal->caldata, sizeof(cal->caldata)); 130 cal->calSamples = 0; 131 132 /* Setup HW for new calibration */ 133 ar5416SetupMeasurement(ah, currCal); 134 135 /* Change SW state to RUNNING for this calibration */ 136 currCal->calState = CAL_RUNNING; 137} 138 139#if 0 140/* 141 * Run non-periodic calibrations. 142 */ 143static HAL_BOOL 144ar5416RunInitCals(struct ath_hal *ah, int init_cal_count) 145{ 146 struct ath_hal_5416 *ahp = AH5416(ah); 147 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 148 HAL_CHANNEL_INTERNAL ichan; /* XXX bogus */ 149 HAL_CAL_LIST *curCal = ahp->ah_cal_curr; 150 HAL_BOOL isCalDone; 151 int i; 152 153 if (curCal == AH_NULL) 154 return AH_FALSE; 155 156 ichan.calValid = 0; 157 for (i = 0; i < init_cal_count; i++) { 158 /* Reset this Cal */ 159 ar5416ResetMeasurement(ah, curCal); 160 /* Poll for offset calibration complete */ 161 if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) { 162 HALDEBUG(ah, HAL_DEBUG_ANY, 163 "%s: Cal %d failed to finish in 100ms.\n", 164 __func__, curCal->calData->calType); 165 /* Re-initialize list pointers for periodic cals */ 166 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL; 167 return AH_FALSE; 168 } 169 /* Run this cal */ 170 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask, 171 curCal, &isCalDone); 172 if (!isCalDone) 173 HALDEBUG(ah, HAL_DEBUG_ANY, 174 "%s: init cal %d did not complete.\n", 175 __func__, curCal->calData->calType); 176 if (curCal->calNext != AH_NULL) 177 curCal = curCal->calNext; 178 } 179 180 /* Re-initialize list pointers for periodic cals */ 181 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL; 182 return AH_TRUE; 183} 184#endif 185 186HAL_BOOL 187ar5416InitCalHardware(struct ath_hal *ah, const struct ieee80211_channel *chan) 188{ 189 if (AR_SREV_MERLIN_10_OR_LATER(ah)) { 190 /* Enable Rx Filter Cal */ 191 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC); 192 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, 193 AR_PHY_AGC_CONTROL_FLTR_CAL); 194 195 /* Clear the carrier leak cal bit */ 196 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE); 197 198 /* kick off the cal */ 199 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL); 200 201 /* Poll for offset calibration complete */ 202 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) { 203 HALDEBUG(ah, HAL_DEBUG_ANY, 204 "%s: offset calibration failed to complete in 1ms; " 205 "noisy environment?\n", __func__); 206 return AH_FALSE; 207 } 208 209 /* Set the cl cal bit and rerun the cal a 2nd time */ 210 /* Enable Rx Filter Cal */ 211 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC); 212 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, 213 AR_PHY_AGC_CONTROL_FLTR_CAL); 214 215 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE); 216 } 217 218 /* Calibrate the AGC */ 219 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL); 220 221 /* Poll for offset calibration complete */ 222 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) { 223 HALDEBUG(ah, HAL_DEBUG_ANY, 224 "%s: offset calibration did not complete in 1ms; " 225 "noisy environment?\n", __func__); 226 return AH_FALSE; 227 } 228 229 return AH_TRUE; 230} 231 232/* 233 * Initialize Calibration infrastructure. 234 */ 235HAL_BOOL 236ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan) 237{ 238 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 239 HAL_CHANNEL_INTERNAL *ichan; 240 241 ichan = ath_hal_checkchannel(ah, chan); 242 HALASSERT(ichan != AH_NULL); 243 244 /* Do initial chipset-specific calibration */ 245 if (! AH5416(ah)->ah_cal_initcal(ah, chan)) { 246 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: initial chipset calibration did " 247 "not complete in time; noisy environment?\n", __func__); 248 return AH_FALSE; 249 } 250 251 /* If there's PA Cal, do it */ 252 if (AH5416(ah)->ah_cal_pacal) 253 AH5416(ah)->ah_cal_pacal(ah, AH_TRUE); 254 255 /* 256 * Do NF calibration after DC offset and other CALs. 257 * Per system engineers, noise floor value can sometimes be 20 dB 258 * higher than normal value if DC offset and noise floor cal are 259 * triggered at the same time. 260 */ 261 /* XXX this actually kicks off a NF calibration -adrian */ 262 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF); 263 /* 264 * Try to make sure the above NF cal completes, just so 265 * it doesn't clash with subsequent percals -adrian 266 */ 267 if (! ar5212WaitNFCalComplete(ah, 10000)) { 268 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: initial NF calibration did " 269 "not complete in time; noisy environment?\n", __func__); 270 return AH_FALSE; 271 } 272 273 /* Initialize list pointers */ 274 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL; 275 276 /* 277 * Enable IQ, ADC Gain, ADC DC Offset Cals 278 */ 279 if (AR_SREV_SOWL_10_OR_LATER(ah)) { 280 /* Setup all non-periodic, init time only calibrations */ 281 /* XXX: Init DC Offset not working yet */ 282#if 0 283 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) { 284 INIT_CAL(&cal->adcDcCalInitData); 285 INSERT_CAL(cal, &cal->adcDcCalInitData); 286 } 287 /* Initialize current pointer to first element in list */ 288 cal->cal_curr = cal->cal_list; 289 290 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0)) 291 return AH_FALSE; 292#endif 293 } 294 295 /* If Cals are supported, add them to list via INIT/INSERT_CAL */ 296 if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) { 297 INIT_CAL(&cal->adcGainCalData); 298 INSERT_CAL(cal, &cal->adcGainCalData); 299 HALDEBUG(ah, HAL_DEBUG_PERCAL, 300 "%s: enable ADC Gain Calibration.\n", __func__); 301 } 302 if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) { 303 INIT_CAL(&cal->adcDcCalData); 304 INSERT_CAL(cal, &cal->adcDcCalData); 305 HALDEBUG(ah, HAL_DEBUG_PERCAL, 306 "%s: enable ADC DC Calibration.\n", __func__); 307 } 308 if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) { 309 INIT_CAL(&cal->iqCalData); 310 INSERT_CAL(cal, &cal->iqCalData); 311 HALDEBUG(ah, HAL_DEBUG_PERCAL, 312 "%s: enable IQ Calibration.\n", __func__); 313 } 314 /* Initialize current pointer to first element in list */ 315 cal->cal_curr = cal->cal_list; 316 317 /* Kick off measurements for the first cal */ 318 if (cal->cal_curr != AH_NULL) 319 ar5416ResetMeasurement(ah, cal->cal_curr); 320 321 /* Mark all calibrations on this channel as being invalid */ 322 ichan->calValid = 0; 323 324 return AH_TRUE; 325} 326 327/* 328 * Entry point for upper layers to restart current cal. 329 * Reset the calibration valid bit in channel. 330 */ 331HAL_BOOL 332ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan) 333{ 334 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 335 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan); 336 HAL_CAL_LIST *currCal = cal->cal_curr; 337 338 if (!AR_SREV_SOWL_10_OR_LATER(ah)) 339 return AH_FALSE; 340 if (currCal == AH_NULL) 341 return AH_FALSE; 342 if (ichan == AH_NULL) { 343 HALDEBUG(ah, HAL_DEBUG_ANY, 344 "%s: invalid channel %u/0x%x; no mapping\n", 345 __func__, chan->ic_freq, chan->ic_flags); 346 return AH_FALSE; 347 } 348 /* 349 * Expected that this calibration has run before, post-reset. 350 * Current state should be done 351 */ 352 if (currCal->calState != CAL_DONE) { 353 HALDEBUG(ah, HAL_DEBUG_ANY, 354 "%s: Calibration state incorrect, %d\n", 355 __func__, currCal->calState); 356 return AH_FALSE; 357 } 358 359 /* Verify Cal is supported on this channel */ 360 if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType)) 361 return AH_FALSE; 362 363 HALDEBUG(ah, HAL_DEBUG_PERCAL, 364 "%s: Resetting Cal %d state for channel %u/0x%x\n", 365 __func__, currCal->calData->calType, chan->ic_freq, 366 chan->ic_flags); 367 368 /* Disable cal validity in channel */ 369 ichan->calValid &= ~currCal->calData->calType; 370 currCal->calState = CAL_WAITING; 371 372 return AH_TRUE; 373} 374 375/* 376 * Recalibrate the lower PHY chips to account for temperature/environment 377 * changes. 378 */ 379static void 380ar5416DoCalibration(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan, 381 uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone) 382{ 383 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 384 385 /* Cal is assumed not done until explicitly set below */ 386 *isCalDone = AH_FALSE; 387 388 HALDEBUG(ah, HAL_DEBUG_PERCAL, 389 "%s: %s Calibration, state %d, calValid 0x%x\n", 390 __func__, currCal->calData->calName, currCal->calState, 391 ichan->calValid); 392 393 /* Calibration in progress. */ 394 if (currCal->calState == CAL_RUNNING) { 395 /* Check to see if it has finished. */ 396 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) { 397 HALDEBUG(ah, HAL_DEBUG_PERCAL, 398 "%s: sample %d of %d finished\n", 399 __func__, cal->calSamples, 400 currCal->calData->calNumSamples); 401 /* 402 * Collect measurements for active chains. 403 */ 404 currCal->calData->calCollect(ah); 405 if (++cal->calSamples >= currCal->calData->calNumSamples) { 406 int i, numChains = 0; 407 for (i = 0; i < AR5416_MAX_CHAINS; i++) { 408 if (rxchainmask & (1 << i)) 409 numChains++; 410 } 411 /* 412 * Process accumulated data 413 */ 414 currCal->calData->calPostProc(ah, numChains); 415 416 /* Calibration has finished. */ 417 ichan->calValid |= currCal->calData->calType; 418 currCal->calState = CAL_DONE; 419 *isCalDone = AH_TRUE; 420 } else { 421 /* 422 * Set-up to collect of another sub-sample. 423 */ 424 ar5416SetupMeasurement(ah, currCal); 425 } 426 } 427 } else if (!(ichan->calValid & currCal->calData->calType)) { 428 /* If current cal is marked invalid in channel, kick it off */ 429 ar5416ResetMeasurement(ah, currCal); 430 } 431} 432 433/* 434 * Internal interface to schedule periodic calibration work. 435 */ 436HAL_BOOL 437ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan, 438 u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone) 439{ 440 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 441 HAL_CAL_LIST *currCal = cal->cal_curr; 442 HAL_CHANNEL_INTERNAL *ichan; 443 444 OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq); 445 446 *isCalDone = AH_TRUE; 447 448 /* 449 * Since ath_hal calls the PerCal method with rxchainmask=0x1; 450 * override it with the current chainmask. The upper levels currently 451 * doesn't know about the chainmask. 452 */ 453 rxchainmask = AH5416(ah)->ah_rx_chainmask; 454 455 /* Invalid channel check */ 456 ichan = ath_hal_checkchannel(ah, chan); 457 if (ichan == AH_NULL) { 458 HALDEBUG(ah, HAL_DEBUG_ANY, 459 "%s: invalid channel %u/0x%x; no mapping\n", 460 __func__, chan->ic_freq, chan->ic_flags); 461 return AH_FALSE; 462 } 463 464 /* 465 * For given calibration: 466 * 1. Call generic cal routine 467 * 2. When this cal is done (isCalDone) if we have more cals waiting 468 * (eg after reset), mask this to upper layers by not propagating 469 * isCalDone if it is set to TRUE. 470 * Instead, change isCalDone to FALSE and setup the waiting cal(s) 471 * to be run. 472 */ 473 if (currCal != AH_NULL && 474 (currCal->calState == CAL_RUNNING || 475 currCal->calState == CAL_WAITING)) { 476 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone); 477 if (*isCalDone == AH_TRUE) { 478 cal->cal_curr = currCal = currCal->calNext; 479 if (currCal->calState == CAL_WAITING) { 480 *isCalDone = AH_FALSE; 481 ar5416ResetMeasurement(ah, currCal); 482 } 483 } 484 } 485 486 /* Do NF cal only at longer intervals */ 487 if (longcal) { 488 /* Do PA calibration if the chipset supports */ 489 if (AH5416(ah)->ah_cal_pacal) 490 AH5416(ah)->ah_cal_pacal(ah, AH_FALSE); 491 492 /* Do temperature compensation if the chipset needs it */ 493 AH5416(ah)->ah_olcTempCompensation(ah); 494 495 /* 496 * Get the value from the previous NF cal 497 * and update the history buffer. 498 */ 499 ar5416GetNf(ah, chan); 500 501 /* 502 * Load the NF from history buffer of the current channel. 503 * NF is slow time-variant, so it is OK to use a 504 * historical value. 505 */ 506 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan); 507 508 /* start NF calibration, without updating BB NF register*/ 509 ar5416StartNFCal(ah); 510 } 511 return AH_TRUE; 512} 513 514/* 515 * Recalibrate the lower PHY chips to account for temperature/environment 516 * changes. 517 */ 518HAL_BOOL 519ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan, 520 HAL_BOOL *isIQdone) 521{ 522 struct ath_hal_5416 *ahp = AH5416(ah); 523 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal; 524 HAL_CAL_LIST *curCal = cal->cal_curr; 525 526 if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) { 527 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask, 528 AH_TRUE, isIQdone); 529 } else { 530 HAL_BOOL isCalDone; 531 532 *isIQdone = AH_FALSE; 533 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask, 534 AH_TRUE, &isCalDone); 535 } 536} 537 538static HAL_BOOL 539ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah, 540 const struct ieee80211_channel *chan, int16_t *nft) 541{ 542 if (IEEE80211_IS_CHAN_5GHZ(chan)) { 543 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft); 544 return AH_TRUE; 545 } 546 if (IEEE80211_IS_CHAN_2GHZ(chan)) { 547 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft); 548 return AH_TRUE; 549 } 550 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n", 551 __func__, chan->ic_flags); 552 return AH_FALSE; 553} 554 555static void 556ar5416StartNFCal(struct ath_hal *ah) 557{ 558 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF); 559 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF); 560 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF); 561} 562 563static void 564ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan) 565{ 566 static const uint32_t ar5416_cca_regs[] = { 567 AR_PHY_CCA, 568 AR_PHY_CH1_CCA, 569 AR_PHY_CH2_CCA, 570 AR_PHY_EXT_CCA, 571 AR_PHY_CH1_EXT_CCA, 572 AR_PHY_CH2_EXT_CCA 573 }; 574 struct ar5212NfCalHist *h; 575 int i; 576 int32_t val; 577 uint8_t chainmask; 578 int16_t default_nf = ar5416GetDefaultNF(ah, chan); 579 580 /* 581 * Force NF calibration for all chains. 582 */ 583 if (AR_SREV_KITE(ah)) { 584 /* Kite has only one chain */ 585 chainmask = 0x9; 586 } else if (AR_SREV_MERLIN(ah)) { 587 /* Merlin has only two chains */ 588 chainmask = 0x1B; 589 } else { 590 chainmask = 0x3F; 591 } 592 593 /* 594 * Write filtered NF values into maxCCApwr register parameter 595 * so we can load below. 596 */ 597 h = AH5416(ah)->ah_cal.nfCalHist; 598 HALDEBUG(ah, HAL_DEBUG_NFCAL, "CCA: "); 599 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) { 600 601 /* Don't write to EXT radio CCA registers */ 602 /* XXX this check should really be cleaner! */ 603 if (i >= 3 && !IEEE80211_IS_CHAN_HT40(chan)) 604 continue; 605 606 if (chainmask & (1 << i)) { 607 int16_t nf_val; 608 609 if (h) 610 nf_val = h[i].privNF; 611 else 612 nf_val = default_nf; 613 614 val = OS_REG_READ(ah, ar5416_cca_regs[i]); 615 val &= 0xFFFFFE00; 616 val |= (((uint32_t) nf_val << 1) & 0x1ff); 617 HALDEBUG(ah, HAL_DEBUG_NFCAL, "[%d: %d]", i, nf_val); 618 OS_REG_WRITE(ah, ar5416_cca_regs[i], val); 619 } 620 } 621 HALDEBUG(ah, HAL_DEBUG_NFCAL, "\n"); 622 623 /* Load software filtered NF value into baseband internal minCCApwr variable. */ 624 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF); 625 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF); 626 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF); 627 628 /* Wait for load to complete, should be fast, a few 10s of us. */ 629 if (! ar5212WaitNFCalComplete(ah, 1000)) { 630 /* 631 * We timed out waiting for the noisefloor to load, probably due to an 632 * in-progress rx. Simply return here and allow the load plenty of time 633 * to complete before the next calibration interval. We need to avoid 634 * trying to load -50 (which happens below) while the previous load is 635 * still in progress as this can cause rx deafness. Instead by returning 636 * here, the baseband nf cal will just be capped by our present 637 * noisefloor until the next calibration timer. 638 */ 639 HALDEBUG(ah, HAL_DEBUG_ANY, "Timeout while waiting for nf " 640 "to load: AR_PHY_AGC_CONTROL=0x%x\n", 641 OS_REG_READ(ah, AR_PHY_AGC_CONTROL)); 642 return; 643 } 644 645 /* 646 * Restore maxCCAPower register parameter again so that we're not capped 647 * by the median we just loaded. This will be initial (and max) value 648 * of next noise floor calibration the baseband does. 649 */ 650 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) 651 if (chainmask & (1 << i)) { 652 val = OS_REG_READ(ah, ar5416_cca_regs[i]); 653 val &= 0xFFFFFE00; 654 val |= (((uint32_t)(-50) << 1) & 0x1ff); 655 OS_REG_WRITE(ah, ar5416_cca_regs[i], val); 656 } 657} 658 659/* 660 * This just initialises the "good" values for AR5416 which 661 * may not be right; it'lll be overridden by ar5416SanitizeNF() 662 * to nominal values. 663 */ 664void 665ar5416InitNfHistBuff(struct ar5212NfCalHist *h) 666{ 667 int i, j; 668 669 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) { 670 h[i].currIndex = 0; 671 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE; 672 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX; 673 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++) 674 h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE; 675 } 676} 677 678/* 679 * Update the noise floor buffer as a ring buffer 680 */ 681static void 682ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray) 683{ 684 int i; 685 686 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) { 687 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i]; 688 689 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX) 690 h[i].currIndex = 0; 691 if (h[i].invalidNFcount > 0) { 692 if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE || 693 nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) { 694 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX; 695 } else { 696 h[i].invalidNFcount--; 697 h[i].privNF = nfarray[i]; 698 } 699 } else { 700 h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer); 701 } 702 } 703} 704 705static uint16_t 706ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan) 707{ 708 struct ar5416NfLimits *limit; 709 710 if (!chan || IEEE80211_IS_CHAN_2GHZ(chan)) 711 limit = &AH5416(ah)->nf_2g; 712 else 713 limit = &AH5416(ah)->nf_5g; 714 715 return limit->nominal; 716} 717 718static void 719ar5416SanitizeNF(struct ath_hal *ah, int16_t *nf) 720{ 721 722 struct ar5416NfLimits *limit; 723 int i; 724 725 if (IEEE80211_IS_CHAN_2GHZ(AH_PRIVATE(ah)->ah_curchan)) 726 limit = &AH5416(ah)->nf_2g; 727 else 728 limit = &AH5416(ah)->nf_5g; 729 730 for (i = 0; i < AR5416_NUM_NF_READINGS; i++) { 731 if (!nf[i]) 732 continue; 733 734 if (nf[i] > limit->max) { 735 HALDEBUG(ah, HAL_DEBUG_NFCAL, 736 "NF[%d] (%d) > MAX (%d), correcting to MAX\n", 737 i, nf[i], limit->max); 738 nf[i] = limit->max; 739 } else if (nf[i] < limit->min) { 740 HALDEBUG(ah, HAL_DEBUG_NFCAL, 741 "NF[%d] (%d) < MIN (%d), correcting to NOM\n", 742 i, nf[i], limit->min); 743 nf[i] = limit->nominal; 744 } 745 } 746} 747 748 749/* 750 * Read the NF and check it against the noise floor threshhold 751 */ 752static int16_t 753ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan) 754{ 755 int16_t nf, nfThresh; 756 757 if (ar5212IsNFCalInProgress(ah)) { 758 HALDEBUG(ah, HAL_DEBUG_ANY, 759 "%s: NF didn't complete in calibration window\n", __func__); 760 nf = 0; 761 } else { 762 /* Finished NF cal, check against threshold */ 763 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 }; 764 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan); 765 766 /* TODO - enhance for multiple chains and ext ch */ 767 ath_hal_getNoiseFloor(ah, nfarray); 768 nf = nfarray[0]; 769 ar5416SanitizeNF(ah, nfarray); 770 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) { 771 if (nf > nfThresh) { 772 HALDEBUG(ah, HAL_DEBUG_ANY, 773 "%s: noise floor failed detected; " 774 "detected %d, threshold %d\n", __func__, 775 nf, nfThresh); 776 /* 777 * NB: Don't discriminate 2.4 vs 5Ghz, if this 778 * happens it indicates a problem regardless 779 * of the band. 780 */ 781 chan->ic_state |= IEEE80211_CHANSTATE_CWINT; 782 nf = 0; 783 } 784 } else { 785 nf = 0; 786 } 787 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray); 788 ichan->rawNoiseFloor = nf; 789 } 790 return nf; 791} 792