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