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