xref: /freebsd/sys/dev/ath/ath_hal/ar5416/ar5416_cal.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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 "ar5212/ar5212.h"	/* for NF cal related declarations */
28 
29 #include "ar5416/ar5416.h"
30 #include "ar5416/ar5416reg.h"
31 #include "ar5416/ar5416phy.h"
32 
33 /* Owl specific stuff */
34 #define NUM_NOISEFLOOR_READINGS 6       /* 3 chains * (ctl + ext) */
35 
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 *);
39 
40 /*
41  * Determine if calibration is supported by device and channel flags
42  */
43 static OS_INLINE HAL_BOOL
44 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
45 	HAL_CAL_TYPE calType)
46 {
47 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
48 
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));
58 	}
59 	return AH_FALSE;
60 }
61 
62 /*
63  * Setup HW to collect samples used for current cal
64  */
65 static void
66 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
67 {
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);
72 
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;
95 	}
96 	/* Kick-off cal */
97 	OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
98 }
99 
100 /*
101  * Initialize shared data structures and prepare a cal to be run.
102  */
103 static void
104 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
105 {
106 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
107 
108 	/* Reset data structures shared between different calibrations */
109 	OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
110 	cal->calSamples = 0;
111 
112 	/* Setup HW for new calibration */
113 	ar5416SetupMeasurement(ah, currCal);
114 
115 	/* Change SW state to RUNNING for this calibration */
116 	currCal->calState = CAL_RUNNING;
117 }
118 
119 #if 0
120 /*
121  * Run non-periodic calibrations.
122  */
123 static HAL_BOOL
124 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
125 {
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;
132 
133 	if (curCal == AH_NULL)
134 		return AH_FALSE;
135 
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;
148 		}
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;
158 	}
159 
160 	/* Re-initialize list pointers for periodic cals */
161 	cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
162 	return AH_TRUE;
163 }
164 #endif
165 
166 /*
167  * Initialize Calibration infrastructure.
168  */
169 HAL_BOOL
170 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
171 {
172 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
173 	HAL_CHANNEL_INTERNAL *ichan;
174 
175 	ichan = ath_hal_checkchannel(ah, chan);
176 	HALASSERT(ichan != AH_NULL);
177 
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);
183 
184 		/* Clear the carrier leak cal bit */
185 		OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
186 
187 		/* kick off the cal */
188 		OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
189 
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;
196 		}
197 
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);
203 
204 		OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
205 	}
206 
207 	/* Calibrate the AGC */
208 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
209 
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;
216 	}
217 
218 	/*
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.
223 	 */
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);
226 	/*
227 	 * Try to make sure the above NF cal completes, just so
228 	 * it doesn't clash with subsequent percals -adrian
229 	 */
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;
234 	}
235 
236 	/* Initialize list pointers */
237 	cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
238 
239 	/*
240 	 * Enable IQ, ADC Gain, ADC DC Offset Cals
241 	 */
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);
249 		}
250 		/* Initialize current pointer to first element in list */
251 		cal->cal_curr = cal->cal_list;
252 
253 		if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
254 			return AH_FALSE;
255 #endif
256 	}
257 
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__);
264 	}
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__);
270 	}
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__);
276 	}
277 	/* Initialize current pointer to first element in list */
278 	cal->cal_curr = cal->cal_list;
279 
280 	/* Kick off measurements for the first cal */
281 	if (cal->cal_curr != AH_NULL)
282 		ar5416ResetMeasurement(ah, cal->cal_curr);
283 
284 	/* Mark all calibrations on this channel as being invalid */
285 	ichan->calValid = 0;
286 
287 	return AH_TRUE;
288 }
289 
290 /*
291  * Entry point for upper layers to restart current cal.
292  * Reset the calibration valid bit in channel.
293  */
294 HAL_BOOL
295 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
296 {
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;
300 
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;
310 	}
311 	/*
312 	 * Expected that this calibration has run before, post-reset.
313 	 * Current state should be done
314 	 */
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;
320 	}
321 
322 	/* Verify Cal is supported on this channel */
323 	if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
324 		return AH_FALSE;
325 
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);
330 
331 	/* Disable cal validity in channel */
332 	ichan->calValid &= ~currCal->calData->calType;
333 	currCal->calState = CAL_WAITING;
334 
335 	return AH_TRUE;
336 }
337 
338 /*
339  * Recalibrate the lower PHY chips to account for temperature/environment
340  * changes.
341  */
342 static void
343 ar5416DoCalibration(struct ath_hal *ah,  HAL_CHANNEL_INTERNAL *ichan,
344 	uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
345 {
346 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
347 
348 	/* Cal is assumed not done until explicitly set below */
349 	*isCalDone = AH_FALSE;
350 
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);
355 
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);
364 			/*
365 			 * Collect measurements for active chains.
366 			 */
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++;
373 				}
374 				/*
375 				 * Process accumulated data
376 				 */
377 				currCal->calData->calPostProc(ah, numChains);
378 
379 				/* Calibration has finished. */
380 				ichan->calValid |= currCal->calData->calType;
381 				currCal->calState = CAL_DONE;
382 				*isCalDone = AH_TRUE;
383 			} else {
384 				/*
385 				 * Set-up to collect of another sub-sample.
386 				 */
387 				ar5416SetupMeasurement(ah, currCal);
388 			}
389 		}
390 	} else if (!(ichan->calValid & currCal->calData->calType)) {
391 		/* If current cal is marked invalid in channel, kick it off */
392 		ar5416ResetMeasurement(ah, currCal);
393 	}
394 }
395 
396 /*
397  * Internal interface to schedule periodic calibration work.
398  */
399 HAL_BOOL
400 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
401 	u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
402 {
403 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
404 	HAL_CAL_LIST *currCal = cal->cal_curr;
405 	HAL_CHANNEL_INTERNAL *ichan;
406 
407 	OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
408 
409 	*isCalDone = AH_TRUE;
410 
411 	/*
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.
415 	 */
416 	rxchainmask = AH5416(ah)->ah_rx_chainmask;
417 
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;
425 	}
426 
427 	/*
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.
435 	 */
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);
445 			}
446 		}
447 	}
448 
449 	/* Do NF cal only at longer intervals */
450 	if (longcal) {
451 		/*
452 		 * Get the value from the previous NF cal
453 		 * and update the history buffer.
454 		 */
455 		ar5416GetNf(ah, chan);
456 
457 		/*
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.
461 		 */
462 		ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
463 
464 		/* start NF calibration, without updating BB NF register*/
465 		ar5416StartNFCal(ah);
466 	}
467 	return AH_TRUE;
468 }
469 
470 /*
471  * Recalibrate the lower PHY chips to account for temperature/environment
472  * changes.
473  */
474 HAL_BOOL
475 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
476 	HAL_BOOL *isIQdone)
477 {
478 	struct ath_hal_5416 *ahp = AH5416(ah);
479 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
480 	HAL_CAL_LIST *curCal = cal->cal_curr;
481 
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;
487 
488 		*isIQdone = AH_FALSE;
489 		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
490 		    AH_TRUE, &isCalDone);
491 	}
492 }
493 
494 static HAL_BOOL
495 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
496 	const struct ieee80211_channel *chan, int16_t *nft)
497 {
498 	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
499 		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
500 		return AH_TRUE;
501 	}
502 	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
503 		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
504 		return AH_TRUE;
505 	}
506 	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
507 	    __func__, chan->ic_flags);
508 	return AH_FALSE;
509 }
510 
511 static void
512 ar5416StartNFCal(struct ath_hal *ah)
513 {
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);
517 }
518 
519 static void
520 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
521 {
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
529 	};
530 	struct ar5212NfCalHist *h;
531 	int i;
532 	int32_t val;
533 	uint8_t chainmask;
534 
535 	/*
536 	 * Force NF calibration for all chains.
537 	 */
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;
546 	}
547 
548 	/*
549 	 * Write filtered NF values into maxCCApwr register parameter
550 	 * so we can load below.
551 	 */
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);
559 		}
560 
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);
565 
566 	/* Wait for load to complete, should be fast, a few 10s of us. */
567 	if (! ar5212WaitNFCalComplete(ah, 1000)) {
568 		/*
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.
576 		 */
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;
581 	}
582 
583 	/*
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.
587 	 */
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);
594 		}
595 }
596 
597 void
598 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
599 {
600 	int i, j;
601 
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;
608 	}
609 }
610 
611 /*
612  * Update the noise floor buffer as a ring buffer
613  */
614 static void
615 ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
616 {
617 	int i;
618 
619 	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
620 		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
621 
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];
631 			}
632 		} else {
633 			h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
634 		}
635 	}
636 }
637 
638 /*
639  * Read the NF and check it against the noise floor threshhold
640  */
641 static int16_t
642 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
643 {
644 	int16_t nf, nfThresh;
645 
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);
654 
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);
664 				/*
665 				 * NB: Don't discriminate 2.4 vs 5Ghz, if this
666 				 *     happens it indicates a problem regardless
667 				 *     of the band.
668 				 */
669 				chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
670 				nf = 0;
671 			}
672 		} else {
673 			nf = 0;
674 		}
675 		ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
676 		ichan->rawNoiseFloor = nf;
677 	}
678 	return nf;
679 }
680