Remove empty DragonFly CVS IDs.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5416 / ar5416_cal.c
blob1e7dffe5948dc0732fc878bf4e0a2da2987ac4c5
1 /*
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
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.
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.
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_cal.c 203882 2010-02-14 16:26:32Z rpaulo $
19 #include "opt_ah.h"
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.h"
25 #include "ah_eeprom_v14.h"
27 #include "ar5212/ar5212.h" /* for NF cal related declarations */
29 #include "ar5416/ar5416.h"
30 #include "ar5416/ar5416reg.h"
31 #include "ar5416/ar5416phy.h"
33 /* Owl specific stuff */
34 #define NUM_NOISEFLOOR_READINGS 6 /* 3 chains * (ctl + ext) */
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 *);
41 * Determine if calibration is supported by device and channel flags
43 static OS_INLINE HAL_BOOL
44 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
45 HAL_CAL_TYPE calType)
47 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
49 switch (calType & cal->suppCals) {
50 case IQ_MISMATCH_CAL:
51 /* Run IQ Mismatch for non-CCK only */
52 return !IEEE80211_IS_CHAN_B(chan);
53 case ADC_GAIN_CAL:
54 case ADC_DC_CAL:
55 /* Run ADC Gain Cal for non-CCK & non 2GHz-HT20 only */
56 return !IEEE80211_IS_CHAN_B(chan) &&
57 !(IEEE80211_IS_CHAN_2GHZ(chan) && IEEE80211_IS_CHAN_HT20(chan));
59 return AH_FALSE;
63 * Setup HW to collect samples used for current cal
65 static void
66 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
68 /* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
69 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
70 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
71 currCal->calData->calCountMax);
73 /* Select calibration to run */
74 switch (currCal->calData->calType) {
75 case IQ_MISMATCH_CAL:
76 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
77 HALDEBUG(ah, HAL_DEBUG_PERCAL,
78 "%s: start IQ Mismatch calibration\n", __func__);
79 break;
80 case ADC_GAIN_CAL:
81 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
82 HALDEBUG(ah, HAL_DEBUG_PERCAL,
83 "%s: start ADC Gain calibration\n", __func__);
84 break;
85 case ADC_DC_CAL:
86 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
87 HALDEBUG(ah, HAL_DEBUG_PERCAL,
88 "%s: start ADC DC calibration\n", __func__);
89 break;
90 case ADC_DC_INIT_CAL:
91 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
92 HALDEBUG(ah, HAL_DEBUG_PERCAL,
93 "%s: start Init ADC DC calibration\n", __func__);
94 break;
96 /* Kick-off cal */
97 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
101 * Initialize shared data structures and prepare a cal to be run.
103 static void
104 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
106 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
108 /* Reset data structures shared between different calibrations */
109 OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
110 cal->calSamples = 0;
112 /* Setup HW for new calibration */
113 ar5416SetupMeasurement(ah, currCal);
115 /* Change SW state to RUNNING for this calibration */
116 currCal->calState = CAL_RUNNING;
119 #if 0
121 * Run non-periodic calibrations.
123 static HAL_BOOL
124 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
126 struct ath_hal_5416 *ahp = AH5416(ah);
127 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
128 HAL_CHANNEL_INTERNAL ichan; /* XXX bogus */
129 HAL_CAL_LIST *curCal = ahp->ah_cal_curr;
130 HAL_BOOL isCalDone;
131 int i;
133 if (curCal == AH_NULL)
134 return AH_FALSE;
136 ichan.calValid = 0;
137 for (i = 0; i < init_cal_count; i++) {
138 /* Reset this Cal */
139 ar5416ResetMeasurement(ah, curCal);
140 /* Poll for offset calibration complete */
141 if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
142 HALDEBUG(ah, HAL_DEBUG_ANY,
143 "%s: Cal %d failed to finish in 100ms.\n",
144 __func__, curCal->calData->calType);
145 /* Re-initialize list pointers for periodic cals */
146 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
147 return AH_FALSE;
149 /* Run this cal */
150 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
151 curCal, &isCalDone);
152 if (!isCalDone)
153 HALDEBUG(ah, HAL_DEBUG_ANY,
154 "%s: init cal %d did not complete.\n",
155 __func__, curCal->calData->calType);
156 if (curCal->calNext != AH_NULL)
157 curCal = curCal->calNext;
160 /* Re-initialize list pointers for periodic cals */
161 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
162 return AH_TRUE;
164 #endif
167 * Initialize Calibration infrastructure.
169 HAL_BOOL
170 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
172 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
173 HAL_CHANNEL_INTERNAL *ichan;
175 ichan = ath_hal_checkchannel(ah, chan);
176 HALASSERT(ichan != AH_NULL);
178 if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
179 /* Enable Rx Filter Cal */
180 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
181 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
182 AR_PHY_AGC_CONTROL_FLTR_CAL);
184 /* Clear the carrier leak cal bit */
185 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
187 /* kick off the cal */
188 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
190 /* Poll for offset calibration complete */
191 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
192 HALDEBUG(ah, HAL_DEBUG_ANY,
193 "%s: offset calibration failed to complete in 1ms; "
194 "noisy environment?\n", __func__);
195 return AH_FALSE;
198 /* Set the cl cal bit and rerun the cal a 2nd time */
199 /* Enable Rx Filter Cal */
200 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
201 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
202 AR_PHY_AGC_CONTROL_FLTR_CAL);
204 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
207 /* Calibrate the AGC */
208 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
210 /* Poll for offset calibration complete */
211 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
212 HALDEBUG(ah, HAL_DEBUG_ANY,
213 "%s: offset calibration did not complete in 1ms; "
214 "noisy environment?\n", __func__);
215 return AH_FALSE;
219 * Do NF calibration after DC offset and other CALs.
220 * Per system engineers, noise floor value can sometimes be 20 dB
221 * higher than normal value if DC offset and noise floor cal are
222 * triggered at the same time.
224 /* XXX this actually kicks off a NF calibration -adrian */
225 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
227 * Try to make sure the above NF cal completes, just so
228 * it doesn't clash with subsequent percals -adrian
230 if (! ar5212WaitNFCalComplete(ah, 10000)) {
231 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: initial NF calibration did "
232 "not complete in time; noisy environment?\n", __func__);
233 return AH_FALSE;
236 /* Initialize list pointers */
237 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
240 * Enable IQ, ADC Gain, ADC DC Offset Cals
242 if (AR_SREV_SOWL_10_OR_LATER(ah)) {
243 /* Setup all non-periodic, init time only calibrations */
244 /* XXX: Init DC Offset not working yet */
245 #if 0
246 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
247 INIT_CAL(&cal->adcDcCalInitData);
248 INSERT_CAL(cal, &cal->adcDcCalInitData);
250 /* Initialize current pointer to first element in list */
251 cal->cal_curr = cal->cal_list;
253 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
254 return AH_FALSE;
255 #endif
258 /* If Cals are supported, add them to list via INIT/INSERT_CAL */
259 if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
260 INIT_CAL(&cal->adcGainCalData);
261 INSERT_CAL(cal, &cal->adcGainCalData);
262 HALDEBUG(ah, HAL_DEBUG_PERCAL,
263 "%s: enable ADC Gain Calibration.\n", __func__);
265 if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
266 INIT_CAL(&cal->adcDcCalData);
267 INSERT_CAL(cal, &cal->adcDcCalData);
268 HALDEBUG(ah, HAL_DEBUG_PERCAL,
269 "%s: enable ADC DC Calibration.\n", __func__);
271 if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
272 INIT_CAL(&cal->iqCalData);
273 INSERT_CAL(cal, &cal->iqCalData);
274 HALDEBUG(ah, HAL_DEBUG_PERCAL,
275 "%s: enable IQ Calibration.\n", __func__);
277 /* Initialize current pointer to first element in list */
278 cal->cal_curr = cal->cal_list;
280 /* Kick off measurements for the first cal */
281 if (cal->cal_curr != AH_NULL)
282 ar5416ResetMeasurement(ah, cal->cal_curr);
284 /* Mark all calibrations on this channel as being invalid */
285 ichan->calValid = 0;
287 return AH_TRUE;
291 * Entry point for upper layers to restart current cal.
292 * Reset the calibration valid bit in channel.
294 HAL_BOOL
295 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
297 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
298 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
299 HAL_CAL_LIST *currCal = cal->cal_curr;
301 if (!AR_SREV_SOWL_10_OR_LATER(ah))
302 return AH_FALSE;
303 if (currCal == AH_NULL)
304 return AH_FALSE;
305 if (ichan == AH_NULL) {
306 HALDEBUG(ah, HAL_DEBUG_ANY,
307 "%s: invalid channel %u/0x%x; no mapping\n",
308 __func__, chan->ic_freq, chan->ic_flags);
309 return AH_FALSE;
312 * Expected that this calibration has run before, post-reset.
313 * Current state should be done
315 if (currCal->calState != CAL_DONE) {
316 HALDEBUG(ah, HAL_DEBUG_ANY,
317 "%s: Calibration state incorrect, %d\n",
318 __func__, currCal->calState);
319 return AH_FALSE;
322 /* Verify Cal is supported on this channel */
323 if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
324 return AH_FALSE;
326 HALDEBUG(ah, HAL_DEBUG_PERCAL,
327 "%s: Resetting Cal %d state for channel %u/0x%x\n",
328 __func__, currCal->calData->calType, chan->ic_freq,
329 chan->ic_flags);
331 /* Disable cal validity in channel */
332 ichan->calValid &= ~currCal->calData->calType;
333 currCal->calState = CAL_WAITING;
335 return AH_TRUE;
339 * Recalibrate the lower PHY chips to account for temperature/environment
340 * changes.
342 static void
343 ar5416DoCalibration(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan,
344 uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
346 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
348 /* Cal is assumed not done until explicitly set below */
349 *isCalDone = AH_FALSE;
351 HALDEBUG(ah, HAL_DEBUG_PERCAL,
352 "%s: %s Calibration, state %d, calValid 0x%x\n",
353 __func__, currCal->calData->calName, currCal->calState,
354 ichan->calValid);
356 /* Calibration in progress. */
357 if (currCal->calState == CAL_RUNNING) {
358 /* Check to see if it has finished. */
359 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
360 HALDEBUG(ah, HAL_DEBUG_PERCAL,
361 "%s: sample %d of %d finished\n",
362 __func__, cal->calSamples,
363 currCal->calData->calNumSamples);
365 * Collect measurements for active chains.
367 currCal->calData->calCollect(ah);
368 if (++cal->calSamples >= currCal->calData->calNumSamples) {
369 int i, numChains = 0;
370 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
371 if (rxchainmask & (1 << i))
372 numChains++;
375 * Process accumulated data
377 currCal->calData->calPostProc(ah, numChains);
379 /* Calibration has finished. */
380 ichan->calValid |= currCal->calData->calType;
381 currCal->calState = CAL_DONE;
382 *isCalDone = AH_TRUE;
383 } else {
385 * Set-up to collect of another sub-sample.
387 ar5416SetupMeasurement(ah, currCal);
390 } else if (!(ichan->calValid & currCal->calData->calType)) {
391 /* If current cal is marked invalid in channel, kick it off */
392 ar5416ResetMeasurement(ah, currCal);
397 * Internal interface to schedule periodic calibration work.
399 HAL_BOOL
400 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
401 u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
403 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
404 HAL_CAL_LIST *currCal = cal->cal_curr;
405 HAL_CHANNEL_INTERNAL *ichan;
407 OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
409 *isCalDone = AH_TRUE;
412 * Since ath_hal calls the PerCal method with rxchainmask=0x1;
413 * override it with the current chainmask. The upper levels currently
414 * doesn't know about the chainmask.
416 rxchainmask = AH5416(ah)->ah_rx_chainmask;
418 /* Invalid channel check */
419 ichan = ath_hal_checkchannel(ah, chan);
420 if (ichan == AH_NULL) {
421 HALDEBUG(ah, HAL_DEBUG_ANY,
422 "%s: invalid channel %u/0x%x; no mapping\n",
423 __func__, chan->ic_freq, chan->ic_flags);
424 return AH_FALSE;
428 * For given calibration:
429 * 1. Call generic cal routine
430 * 2. When this cal is done (isCalDone) if we have more cals waiting
431 * (eg after reset), mask this to upper layers by not propagating
432 * isCalDone if it is set to TRUE.
433 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
434 * to be run.
436 if (currCal != AH_NULL &&
437 (currCal->calState == CAL_RUNNING ||
438 currCal->calState == CAL_WAITING)) {
439 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
440 if (*isCalDone == AH_TRUE) {
441 cal->cal_curr = currCal = currCal->calNext;
442 if (currCal->calState == CAL_WAITING) {
443 *isCalDone = AH_FALSE;
444 ar5416ResetMeasurement(ah, currCal);
449 /* Do NF cal only at longer intervals */
450 if (longcal) {
452 * Get the value from the previous NF cal
453 * and update the history buffer.
455 ar5416GetNf(ah, chan);
458 * Load the NF from history buffer of the current channel.
459 * NF is slow time-variant, so it is OK to use a
460 * historical value.
462 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
464 /* start NF calibration, without updating BB NF register*/
465 ar5416StartNFCal(ah);
467 return AH_TRUE;
471 * Recalibrate the lower PHY chips to account for temperature/environment
472 * changes.
474 HAL_BOOL
475 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
476 HAL_BOOL *isIQdone)
478 struct ath_hal_5416 *ahp = AH5416(ah);
479 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
480 HAL_CAL_LIST *curCal = cal->cal_curr;
482 if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
483 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
484 AH_TRUE, isIQdone);
485 } else {
486 HAL_BOOL isCalDone;
488 *isIQdone = AH_FALSE;
489 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
490 AH_TRUE, &isCalDone);
494 static HAL_BOOL
495 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
496 const struct ieee80211_channel *chan, int16_t *nft)
498 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
499 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
500 return AH_TRUE;
502 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
503 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
504 return AH_TRUE;
506 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
507 __func__, chan->ic_flags);
508 return AH_FALSE;
511 static void
512 ar5416StartNFCal(struct ath_hal *ah)
514 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
515 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
516 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
519 static void
520 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
522 static const uint32_t ar5416_cca_regs[] = {
523 AR_PHY_CCA,
524 AR_PHY_CH1_CCA,
525 AR_PHY_CH2_CCA,
526 AR_PHY_EXT_CCA,
527 AR_PHY_CH1_EXT_CCA,
528 AR_PHY_CH2_EXT_CCA
530 struct ar5212NfCalHist *h;
531 int i;
532 int32_t val;
533 uint8_t chainmask;
536 * Force NF calibration for all chains.
538 if (AR_SREV_KITE(ah)) {
539 /* Kite has only one chain */
540 chainmask = 0x9;
541 } else if (AR_SREV_MERLIN(ah)) {
542 /* Merlin has only two chains */
543 chainmask = 0x1B;
544 } else {
545 chainmask = 0x3F;
549 * Write filtered NF values into maxCCApwr register parameter
550 * so we can load below.
552 h = AH5416(ah)->ah_cal.nfCalHist;
553 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
554 if (chainmask & (1 << i)) {
555 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
556 val &= 0xFFFFFE00;
557 val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
558 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
561 /* Load software filtered NF value into baseband internal minCCApwr variable. */
562 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
563 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
564 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
566 /* Wait for load to complete, should be fast, a few 10s of us. */
567 if (! ar5212WaitNFCalComplete(ah, 1000)) {
569 * We timed out waiting for the noisefloor to load, probably due to an
570 * in-progress rx. Simply return here and allow the load plenty of time
571 * to complete before the next calibration interval. We need to avoid
572 * trying to load -50 (which happens below) while the previous load is
573 * still in progress as this can cause rx deafness. Instead by returning
574 * here, the baseband nf cal will just be capped by our present
575 * noisefloor until the next calibration timer.
577 HALDEBUG(ah, HAL_DEBUG_ANY, "Timeout while waiting for nf "
578 "to load: AR_PHY_AGC_CONTROL=0x%x\n",
579 OS_REG_READ(ah, AR_PHY_AGC_CONTROL));
580 return;
584 * Restore maxCCAPower register parameter again so that we're not capped
585 * by the median we just loaded. This will be initial (and max) value
586 * of next noise floor calibration the baseband does.
588 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
589 if (chainmask & (1 << i)) {
590 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
591 val &= 0xFFFFFE00;
592 val |= (((uint32_t)(-50) << 1) & 0x1ff);
593 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
597 void
598 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
600 int i, j;
602 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
603 h[i].currIndex = 0;
604 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
605 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
606 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
607 h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
612 * Update the noise floor buffer as a ring buffer
614 static void
615 ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
617 int i;
619 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
620 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
622 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
623 h[i].currIndex = 0;
624 if (h[i].invalidNFcount > 0) {
625 if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
626 nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
627 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
628 } else {
629 h[i].invalidNFcount--;
630 h[i].privNF = nfarray[i];
632 } else {
633 h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
639 * Read the NF and check it against the noise floor threshhold
641 static int16_t
642 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
644 int16_t nf, nfThresh;
646 if (ar5212IsNFCalInProgress(ah)) {
647 HALDEBUG(ah, HAL_DEBUG_ANY,
648 "%s: NF didn't complete in calibration window\n", __func__);
649 nf = 0;
650 } else {
651 /* Finished NF cal, check against threshold */
652 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
653 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
655 /* TODO - enhance for multiple chains and ext ch */
656 ath_hal_getNoiseFloor(ah, nfarray);
657 nf = nfarray[0];
658 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
659 if (nf > nfThresh) {
660 HALDEBUG(ah, HAL_DEBUG_ANY,
661 "%s: noise floor failed detected; "
662 "detected %d, threshold %d\n", __func__,
663 nf, nfThresh);
665 * NB: Don't discriminate 2.4 vs 5Ghz, if this
666 * happens it indicates a problem regardless
667 * of the band.
669 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
670 nf = 0;
672 } else {
673 nf = 0;
675 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
676 ichan->rawNoiseFloor = nf;
678 return nf;