]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/ath/ath_hal/ar5416/ar5416_cal.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / ath / ath_hal / ar5416 / ar5416_cal.c
1 /*
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD$
18  */
19 #include "opt_ah.h"
20
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.h"
24
25 #include "ah_eeprom_v14.h"
26
27 #include "ar5416/ar5416.h"
28 #include "ar5416/ar5416reg.h"
29 #include "ar5416/ar5416phy.h"
30
31 /* Owl specific stuff */
32 #define NUM_NOISEFLOOR_READINGS 6       /* 3 chains * (ctl + ext) */
33
34 static void ar5416StartNFCal(struct ath_hal *ah);
35 static void ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *);
36 static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *);
37
38 /*
39  * Determine if calibration is supported by device and channel flags
40  */
41 static OS_INLINE HAL_BOOL
42 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
43         HAL_CAL_TYPE calType) 
44 {
45         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
46
47         switch (calType & cal->suppCals) {
48         case IQ_MISMATCH_CAL:
49                 /* Run IQ Mismatch for non-CCK only */
50                 return !IEEE80211_IS_CHAN_B(chan);
51         case ADC_GAIN_CAL:
52         case ADC_DC_CAL:
53                 /* Run ADC Gain Cal for non-CCK & non 2GHz-HT20 only */
54                 return !IEEE80211_IS_CHAN_B(chan) &&
55                     !(IEEE80211_IS_CHAN_2GHZ(chan) && IEEE80211_IS_CHAN_HT20(chan));
56         }
57         return AH_FALSE;
58 }
59
60 /*
61  * Setup HW to collect samples used for current cal
62  */
63 static void
64 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
65 {
66         /* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
67         OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
68             AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
69             currCal->calData->calCountMax);
70
71         /* Select calibration to run */
72         switch (currCal->calData->calType) {
73         case IQ_MISMATCH_CAL:
74                 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
75                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
76                     "%s: start IQ Mismatch calibration\n", __func__);
77                 break;
78         case ADC_GAIN_CAL:
79                 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
80                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
81                     "%s: start ADC Gain calibration\n", __func__);
82                 break;
83         case ADC_DC_CAL:
84                 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
85                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
86                     "%s: start ADC DC calibration\n", __func__);
87                 break;
88         case ADC_DC_INIT_CAL:
89                 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
90                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
91                     "%s: start Init ADC DC calibration\n", __func__);
92                 break;
93         }
94         /* Kick-off cal */
95         OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
96 }
97
98 /*
99  * Initialize shared data structures and prepare a cal to be run.
100  */
101 static void
102 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
103 {
104         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
105
106         /* Reset data structures shared between different calibrations */
107         OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
108         cal->calSamples = 0;
109
110         /* Setup HW for new calibration */
111         ar5416SetupMeasurement(ah, currCal);
112
113         /* Change SW state to RUNNING for this calibration */
114         currCal->calState = CAL_RUNNING;
115 }
116
117 #if 0
118 /*
119  * Run non-periodic calibrations.
120  */
121 static HAL_BOOL
122 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
123 {
124         struct ath_hal_5416 *ahp = AH5416(ah);
125         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
126         HAL_CHANNEL_INTERNAL ichan;     /* XXX bogus */
127         HAL_CAL_LIST *curCal = ahp->ah_cal_curr;
128         HAL_BOOL isCalDone;
129         int i;
130
131         if (curCal == AH_NULL)
132                 return AH_FALSE;
133
134         ichan.calValid = 0;
135         for (i = 0; i < init_cal_count; i++) {
136                 /* Reset this Cal */
137                 ar5416ResetMeasurement(ah, curCal);
138                 /* Poll for offset calibration complete */
139                 if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
140                         HALDEBUG(ah, HAL_DEBUG_ANY,
141                             "%s: Cal %d failed to finish in 100ms.\n",
142                             __func__, curCal->calData->calType);
143                         /* Re-initialize list pointers for periodic cals */
144                         cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
145                         return AH_FALSE;
146                 }
147                 /* Run this cal */
148                 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
149                     curCal, &isCalDone);
150                 if (!isCalDone)
151                         HALDEBUG(ah, HAL_DEBUG_ANY,
152                             "%s: init cal %d did not complete.\n",
153                             __func__, curCal->calData->calType);
154                 if (curCal->calNext != AH_NULL)
155                         curCal = curCal->calNext;
156         }
157
158         /* Re-initialize list pointers for periodic cals */
159         cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
160         return AH_TRUE;
161 }
162 #endif
163
164 /*
165  * Initialize Calibration infrastructure.
166  */
167 HAL_BOOL
168 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
169 {
170         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
171         HAL_CHANNEL_INTERNAL *ichan;
172
173         ichan = ath_hal_checkchannel(ah, chan);
174         HALASSERT(ichan != AH_NULL);
175
176         if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
177                 /* Enable Rx Filter Cal */
178                 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
179                 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
180                     AR_PHY_AGC_CONTROL_FLTR_CAL);
181
182                 /* Clear the carrier leak cal bit */
183                 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
184
185                 /* kick off the cal */
186                 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
187
188                 /* Poll for offset calibration complete */
189                 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
190                         HALDEBUG(ah, HAL_DEBUG_ANY,
191                             "%s: offset calibration failed to complete in 1ms; "
192                             "noisy environment?\n", __func__);
193                         return AH_FALSE;
194                 }
195
196                 /* Set the cl cal bit and rerun the cal a 2nd time */
197                 /* Enable Rx Filter Cal */
198                 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
199                 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
200                     AR_PHY_AGC_CONTROL_FLTR_CAL);
201
202                 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
203         }       
204
205         /* Calibrate the AGC */
206         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
207
208         /* Poll for offset calibration complete */
209         if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
210                 HALDEBUG(ah, HAL_DEBUG_ANY,
211                     "%s: offset calibration did not complete in 1ms; "
212                     "noisy environment?\n", __func__);
213                 return AH_FALSE;
214         }
215
216         /* 
217          * Do NF calibration after DC offset and other CALs.
218          * Per system engineers, noise floor value can sometimes be 20 dB
219          * higher than normal value if DC offset and noise floor cal are
220          * triggered at the same time.
221          */
222         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
223
224         /* Initialize list pointers */
225         cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
226
227         /*
228          * Enable IQ, ADC Gain, ADC DC Offset Cals
229          */
230         if (AR_SREV_SOWL_10_OR_LATER(ah)) {
231                 /* Setup all non-periodic, init time only calibrations */
232                 /* XXX: Init DC Offset not working yet */
233 #if 0
234                 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
235                         INIT_CAL(&cal->adcDcCalInitData);
236                         INSERT_CAL(cal, &cal->adcDcCalInitData);
237                 }
238                 /* Initialize current pointer to first element in list */
239                 cal->cal_curr = cal->cal_list;
240
241                 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
242                         return AH_FALSE;
243 #endif
244         }
245
246         /* If Cals are supported, add them to list via INIT/INSERT_CAL */
247         if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
248                 INIT_CAL(&cal->adcGainCalData);
249                 INSERT_CAL(cal, &cal->adcGainCalData);
250                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
251                     "%s: enable ADC Gain Calibration.\n", __func__);
252         }
253         if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
254                 INIT_CAL(&cal->adcDcCalData);
255                 INSERT_CAL(cal, &cal->adcDcCalData);
256                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
257                     "%s: enable ADC DC Calibration.\n", __func__);
258         }
259         if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
260                 INIT_CAL(&cal->iqCalData);
261                 INSERT_CAL(cal, &cal->iqCalData);
262                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
263                     "%s: enable IQ Calibration.\n", __func__);
264         }
265         /* Initialize current pointer to first element in list */
266         cal->cal_curr = cal->cal_list;
267
268         /* Kick off measurements for the first cal */
269         if (cal->cal_curr != AH_NULL)
270                 ar5416ResetMeasurement(ah, cal->cal_curr);
271
272         /* Mark all calibrations on this channel as being invalid */
273         ichan->calValid = 0;
274
275         return AH_TRUE;
276 }
277
278 /*
279  * Entry point for upper layers to restart current cal.
280  * Reset the calibration valid bit in channel.
281  */
282 HAL_BOOL
283 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
284 {
285         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
286         HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
287         HAL_CAL_LIST *currCal = cal->cal_curr;
288
289         if (!AR_SREV_SOWL_10_OR_LATER(ah))
290                 return AH_FALSE;
291         if (currCal == AH_NULL)
292                 return AH_FALSE;
293         if (ichan == AH_NULL) {
294                 HALDEBUG(ah, HAL_DEBUG_ANY,
295                     "%s: invalid channel %u/0x%x; no mapping\n",
296                     __func__, chan->ic_freq, chan->ic_flags);
297                 return AH_FALSE;
298         }
299         /*
300          * Expected that this calibration has run before, post-reset.
301          * Current state should be done
302          */
303         if (currCal->calState != CAL_DONE) {
304                 HALDEBUG(ah, HAL_DEBUG_ANY,
305                     "%s: Calibration state incorrect, %d\n",
306                     __func__, currCal->calState);
307                 return AH_FALSE;
308         }
309
310         /* Verify Cal is supported on this channel */
311         if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
312                 return AH_FALSE;
313
314         HALDEBUG(ah, HAL_DEBUG_PERCAL,
315             "%s: Resetting Cal %d state for channel %u/0x%x\n",
316             __func__, currCal->calData->calType, chan->ic_freq,
317             chan->ic_flags);
318
319         /* Disable cal validity in channel */
320         ichan->calValid &= ~currCal->calData->calType;
321         currCal->calState = CAL_WAITING;
322
323         return AH_TRUE;
324 }
325
326 /*
327  * Recalibrate the lower PHY chips to account for temperature/environment
328  * changes.
329  */
330 static void
331 ar5416DoCalibration(struct ath_hal *ah,  HAL_CHANNEL_INTERNAL *ichan,
332         uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
333 {
334         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
335
336         /* Cal is assumed not done until explicitly set below */
337         *isCalDone = AH_FALSE;
338
339         HALDEBUG(ah, HAL_DEBUG_PERCAL,
340             "%s: %s Calibration, state %d, calValid 0x%x\n",
341             __func__, currCal->calData->calName, currCal->calState,
342             ichan->calValid);
343
344         /* Calibration in progress. */
345         if (currCal->calState == CAL_RUNNING) {
346                 /* Check to see if it has finished. */
347                 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
348                         HALDEBUG(ah, HAL_DEBUG_PERCAL,
349                             "%s: sample %d of %d finished\n",
350                             __func__, cal->calSamples,
351                             currCal->calData->calNumSamples);
352                         /* 
353                          * Collect measurements for active chains.
354                          */
355                         currCal->calData->calCollect(ah);
356                         if (++cal->calSamples >= currCal->calData->calNumSamples) {
357                                 int i, numChains = 0;
358                                 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
359                                         if (rxchainmask & (1 << i))
360                                                 numChains++;
361                                 }
362                                 /* 
363                                  * Process accumulated data
364                                  */
365                                 currCal->calData->calPostProc(ah, numChains);
366
367                                 /* Calibration has finished. */
368                                 ichan->calValid |= currCal->calData->calType;
369                                 currCal->calState = CAL_DONE;
370                                 *isCalDone = AH_TRUE;
371                         } else {
372                                 /*
373                                  * Set-up to collect of another sub-sample.
374                                  */
375                                 ar5416SetupMeasurement(ah, currCal);
376                         }
377                 }
378         } else if (!(ichan->calValid & currCal->calData->calType)) {
379                 /* If current cal is marked invalid in channel, kick it off */
380                 ar5416ResetMeasurement(ah, currCal);
381         }
382 }
383
384 /*
385  * Internal interface to schedule periodic calibration work.
386  */
387 HAL_BOOL
388 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
389         u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
390 {
391         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
392         HAL_CAL_LIST *currCal = cal->cal_curr;
393         HAL_CHANNEL_INTERNAL *ichan;
394
395         OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
396
397         *isCalDone = AH_TRUE;
398
399         /* Invalid channel check */
400         ichan = ath_hal_checkchannel(ah, chan);
401         if (ichan == AH_NULL) {
402                 HALDEBUG(ah, HAL_DEBUG_ANY,
403                     "%s: invalid channel %u/0x%x; no mapping\n",
404                     __func__, chan->ic_freq, chan->ic_flags);
405                 return AH_FALSE;
406         }
407
408         /*
409          * For given calibration:
410          * 1. Call generic cal routine
411          * 2. When this cal is done (isCalDone) if we have more cals waiting
412          *    (eg after reset), mask this to upper layers by not propagating
413          *    isCalDone if it is set to TRUE.
414          *    Instead, change isCalDone to FALSE and setup the waiting cal(s)
415          *    to be run.
416          */
417         if (currCal != AH_NULL &&
418             (currCal->calState == CAL_RUNNING ||
419              currCal->calState == CAL_WAITING)) {
420                 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
421                 if (*isCalDone == AH_TRUE) {
422                         cal->cal_curr = currCal = currCal->calNext;
423                         if (currCal->calState == CAL_WAITING) {
424                                 *isCalDone = AH_FALSE;
425                                 ar5416ResetMeasurement(ah, currCal);
426                         }
427                 }
428         }
429
430         /* Do NF cal only at longer intervals */
431         if (longcal) {
432                 /*
433                  * Get the value from the previous NF cal
434                  * and update the history buffer.
435                  */
436                 ar5416GetNf(ah, chan);
437
438                 /* 
439                  * Load the NF from history buffer of the current channel.
440                  * NF is slow time-variant, so it is OK to use a
441                  * historical value.
442                  */
443                 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
444
445                 /* start NF calibration, without updating BB NF register*/
446                 ar5416StartNFCal(ah);
447         }
448         return AH_TRUE;
449 }
450
451 /*
452  * Recalibrate the lower PHY chips to account for temperature/environment
453  * changes.
454  */
455 HAL_BOOL
456 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
457         HAL_BOOL *isIQdone)
458 {
459         struct ath_hal_5416 *ahp = AH5416(ah);
460         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
461         HAL_CAL_LIST *curCal = cal->cal_curr;
462
463         if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
464                 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
465                     AH_TRUE, isIQdone);
466         } else {
467                 HAL_BOOL isCalDone;
468
469                 *isIQdone = AH_FALSE;
470                 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
471                     AH_TRUE, &isCalDone);
472         }
473 }
474
475 static HAL_BOOL
476 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
477         const struct ieee80211_channel *chan, int16_t *nft)
478 {
479         if (IEEE80211_IS_CHAN_5GHZ(chan)) {
480                 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
481                 return AH_TRUE;
482         }
483         if (IEEE80211_IS_CHAN_2GHZ(chan)) {
484                 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
485                 return AH_TRUE;
486         }
487         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
488             __func__, chan->ic_flags);
489         return AH_FALSE;
490 }
491
492 static void
493 ar5416StartNFCal(struct ath_hal *ah)
494 {
495         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
496         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
497         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
498 }
499
500 static void
501 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
502 {
503         static const uint32_t ar5416_cca_regs[] = {
504                 AR_PHY_CCA,
505                 AR_PHY_CH1_CCA,
506                 AR_PHY_CH2_CCA,
507                 AR_PHY_EXT_CCA,
508                 AR_PHY_CH1_EXT_CCA,
509                 AR_PHY_CH2_EXT_CCA
510         };
511         struct ar5212NfCalHist *h;
512         int i, j;
513         int32_t val;
514         uint8_t chainmask;
515
516         /*
517          * Force NF calibration for all chains.
518          */
519         if (AR_SREV_KITE(ah)) {
520                 /* Kite has only one chain */
521                 chainmask = 0x9;
522         } else if (AR_SREV_MERLIN(ah)) {
523                 /* Merlin has only two chains */
524                 chainmask = 0x1B;
525         } else {
526                 chainmask = 0x3F;
527         }
528
529         /*
530          * Write filtered NF values into maxCCApwr register parameter
531          * so we can load below.
532          */
533         h = AH5416(ah)->ah_cal.nfCalHist;
534         for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
535                 if (chainmask & (1 << i)) { 
536                         val = OS_REG_READ(ah, ar5416_cca_regs[i]);
537                         val &= 0xFFFFFE00;
538                         val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
539                         OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
540                 }
541
542         /* Load software filtered NF value into baseband internal minCCApwr variable. */
543         OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
544         OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
545         OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
546
547         /* Wait for load to complete, should be fast, a few 10s of us. */
548         for (j = 0; j < 1000; j++) {
549                 if ((OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) == 0)
550                         break;
551                 OS_DELAY(10);
552         }
553
554         /*
555          * Restore maxCCAPower register parameter again so that we're not capped
556          * by the median we just loaded.  This will be initial (and max) value
557          * of next noise floor calibration the baseband does.  
558          */
559         for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
560                 if (chainmask & (1 << i)) {     
561                         val = OS_REG_READ(ah, ar5416_cca_regs[i]);
562                         val &= 0xFFFFFE00;
563                         val |= (((uint32_t)(-50) << 1) & 0x1ff);
564                         OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
565                 }
566 }
567
568 void
569 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
570 {
571         int i, j;
572
573         for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
574                 h[i].currIndex = 0;
575                 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
576                 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
577                 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
578                         h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
579         }
580 }
581
582 /*
583  * Update the noise floor buffer as a ring buffer
584  */
585 static void
586 ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
587 {
588         int i;
589
590         for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
591                 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
592
593                 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
594                         h[i].currIndex = 0;
595                 if (h[i].invalidNFcount > 0) {
596                         if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
597                             nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
598                                 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
599                         } else {
600                                 h[i].invalidNFcount--;
601                                 h[i].privNF = nfarray[i];
602                         }
603                 } else {
604                         h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
605                 }
606         }
607 }   
608
609 /*
610  * Read the NF and check it against the noise floor threshhold
611  */
612 static int16_t
613 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
614 {
615         int16_t nf, nfThresh;
616
617         if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
618                 HALDEBUG(ah, HAL_DEBUG_ANY,
619                     "%s: NF didn't complete in calibration window\n", __func__);
620                 nf = 0;
621         } else {
622                 /* Finished NF cal, check against threshold */
623                 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
624                 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
625                         
626                 /* TODO - enhance for multiple chains and ext ch */
627                 ath_hal_getNoiseFloor(ah, nfarray);
628                 nf = nfarray[0];
629                 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
630                         if (nf > nfThresh) {
631                                 HALDEBUG(ah, HAL_DEBUG_ANY,
632                                     "%s: noise floor failed detected; "
633                                     "detected %d, threshold %d\n", __func__,
634                                     nf, nfThresh);
635                                 /*
636                                  * NB: Don't discriminate 2.4 vs 5Ghz, if this
637                                  *     happens it indicates a problem regardless
638                                  *     of the band.
639                                  */
640                                 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
641                                 nf = 0;
642                         }
643                 } else {
644                         nf = 0;
645                 }
646                 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
647                 ichan->rawNoiseFloor = nf;
648         }
649         return nf;
650 }