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