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