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