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