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