xref: /freebsd/sys/dev/ath/ath_hal/ar5416/ar5416_cal.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * Copyright (c) 2002-2008 Atheros Communications, Inc.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * $FreeBSD$
20  */
21 #include "opt_ah.h"
22 
23 #include "ah.h"
24 #include "ah_internal.h"
25 #include "ah_devid.h"
26 
27 #include "ah_eeprom_v14.h"
28 
29 #include "ar5212/ar5212.h"	/* for NF cal related declarations */
30 
31 #include "ar5416/ar5416.h"
32 #include "ar5416/ar5416reg.h"
33 #include "ar5416/ar5416phy.h"
34 
35 /* Owl specific stuff */
36 #define NUM_NOISEFLOOR_READINGS 6       /* 3 chains * (ctl + ext) */
37 
38 static void ar5416StartNFCal(struct ath_hal *ah);
39 static HAL_BOOL ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *);
40 static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *);
41 
42 static uint16_t ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan);
43 static void ar5416SanitizeNF(struct ath_hal *ah, int16_t *nf);
44 
45 /*
46  * Determine if calibration is supported by device and channel flags
47  */
48 
49 /*
50  * ADC GAIN/DC offset calibration is for calibrating two ADCs that
51  * are acting as one by interleaving incoming symbols. This isn't
52  * relevant for 2.4GHz 20MHz wide modes because, as far as I can tell,
53  * the secondary ADC is never enabled. It is enabled however for
54  * 5GHz modes.
55  *
56  * It hasn't been confirmed whether doing this calibration is needed
57  * at all in the above modes and/or whether it's actually harmful.
58  * So for now, let's leave it enabled and just remember to get
59  * confirmation that it needs to be clarified.
60  *
61  * See US Patent No: US 7,541,952 B1:
62  *  " Method and Apparatus for Offset and Gain Compensation for
63  *    Analog-to-Digital Converters."
64  */
65 static OS_INLINE HAL_BOOL
66 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
67 	HAL_CAL_TYPE calType)
68 {
69 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
70 
71 	switch (calType & cal->suppCals) {
72 	case IQ_MISMATCH_CAL:
73 		/* Run IQ Mismatch for non-CCK only */
74 		return !IEEE80211_IS_CHAN_B(chan);
75 	case ADC_GAIN_CAL:
76 	case ADC_DC_CAL:
77 		/*
78 		 * Run ADC Gain Cal for either 5ghz any or 2ghz HT40.
79 		 *
80 		 * Don't run ADC calibrations for 5ghz fast clock mode
81 		 * in HT20 - only one ADC is used.
82 		 */
83 		if (IEEE80211_IS_CHAN_HT20(chan) &&
84 		    (IS_5GHZ_FAST_CLOCK_EN(ah, chan)))
85 			return AH_FALSE;
86 		if (IEEE80211_IS_CHAN_5GHZ(chan))
87 			return AH_TRUE;
88 		if (IEEE80211_IS_CHAN_HT40(chan))
89 			return AH_TRUE;
90 		return AH_FALSE;
91 	}
92 	return AH_FALSE;
93 }
94 
95 /*
96  * Setup HW to collect samples used for current cal
97  */
98 static void
99 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
100 {
101 	/* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
102 	OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
103 	    AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
104 	    currCal->calData->calCountMax);
105 
106 	/* Select calibration to run */
107 	switch (currCal->calData->calType) {
108 	case IQ_MISMATCH_CAL:
109 		OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
110 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
111 		    "%s: start IQ Mismatch calibration\n", __func__);
112 		break;
113 	case ADC_GAIN_CAL:
114 		OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
115 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
116 		    "%s: start ADC Gain calibration\n", __func__);
117 		break;
118 	case ADC_DC_CAL:
119 		OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
120 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
121 		    "%s: start ADC DC calibration\n", __func__);
122 		break;
123 	case ADC_DC_INIT_CAL:
124 		OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
125 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
126 		    "%s: start Init ADC DC calibration\n", __func__);
127 		break;
128 	}
129 	/* Kick-off cal */
130 	OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
131 }
132 
133 /*
134  * Initialize shared data structures and prepare a cal to be run.
135  */
136 static void
137 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
138 {
139 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
140 
141 	/* Reset data structures shared between different calibrations */
142 	OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
143 	cal->calSamples = 0;
144 
145 	/* Setup HW for new calibration */
146 	ar5416SetupMeasurement(ah, currCal);
147 
148 	/* Change SW state to RUNNING for this calibration */
149 	currCal->calState = CAL_RUNNING;
150 }
151 
152 #if 0
153 /*
154  * Run non-periodic calibrations.
155  */
156 static HAL_BOOL
157 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
158 {
159 	struct ath_hal_5416 *ahp = AH5416(ah);
160 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
161 	HAL_CHANNEL_INTERNAL ichan;	/* XXX bogus */
162 	HAL_CAL_LIST *curCal = ahp->ah_cal_curr;
163 	HAL_BOOL isCalDone;
164 	int i;
165 
166 	if (curCal == AH_NULL)
167 		return AH_FALSE;
168 
169 	ichan.calValid = 0;
170 	for (i = 0; i < init_cal_count; i++) {
171 		/* Reset this Cal */
172 		ar5416ResetMeasurement(ah, curCal);
173 		/* Poll for offset calibration complete */
174 		if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
175 			HALDEBUG(ah, HAL_DEBUG_ANY,
176 			    "%s: Cal %d failed to finish in 100ms.\n",
177 			    __func__, curCal->calData->calType);
178 			/* Re-initialize list pointers for periodic cals */
179 			cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
180 			return AH_FALSE;
181 		}
182 		/* Run this cal */
183 		ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
184 		    curCal, &isCalDone);
185 		if (!isCalDone)
186 			HALDEBUG(ah, HAL_DEBUG_ANY,
187 			    "%s: init cal %d did not complete.\n",
188 			    __func__, curCal->calData->calType);
189 		if (curCal->calNext != AH_NULL)
190 			curCal = curCal->calNext;
191 	}
192 
193 	/* Re-initialize list pointers for periodic cals */
194 	cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
195 	return AH_TRUE;
196 }
197 #endif
198 
199 
200 /*
201  * AGC calibration for the AR5416, AR9130, AR9160, AR9280.
202  */
203 HAL_BOOL
204 ar5416InitCalHardware(struct ath_hal *ah, const struct ieee80211_channel *chan)
205 {
206 
207 	if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
208 		/* Disable ADC */
209 		OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL,
210 		    AR_PHY_ADC_CTL_OFF_PWDADC);
211 
212 		/* Enable Rx Filter Cal */
213 		OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
214 		    AR_PHY_AGC_CONTROL_FLTR_CAL);
215 	}
216 
217 	/* Calibrate the AGC */
218 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
219 
220 	/* Poll for offset calibration complete */
221 	if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
222 		HALDEBUG(ah, HAL_DEBUG_ANY,
223 		    "%s: offset calibration did not complete in 1ms; "
224 		    "noisy environment?\n", __func__);
225 		return AH_FALSE;
226 	}
227 
228 	if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
229 		/* Enable ADC */
230 		OS_REG_SET_BIT(ah, AR_PHY_ADC_CTL,
231 		    AR_PHY_ADC_CTL_OFF_PWDADC);
232 
233 		/* Disable Rx Filter Cal */
234 		OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
235 		    AR_PHY_AGC_CONTROL_FLTR_CAL);
236 	}
237 
238 	return AH_TRUE;
239 }
240 
241 /*
242  * Initialize Calibration infrastructure.
243  */
244 #define	MAX_CAL_CHECK		32
245 HAL_BOOL
246 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
247 {
248 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
249 	HAL_CHANNEL_INTERNAL *ichan;
250 
251 	ichan = ath_hal_checkchannel(ah, chan);
252 	HALASSERT(ichan != AH_NULL);
253 
254 	/* Do initial chipset-specific calibration */
255 	if (! AH5416(ah)->ah_cal_initcal(ah, chan)) {
256 		HALDEBUG(ah, HAL_DEBUG_ANY,
257 		    "%s: initial chipset calibration did "
258 		    "not complete in time; noisy environment?\n", __func__);
259 		return AH_FALSE;
260 	}
261 
262 	/* If there's PA Cal, do it */
263 	if (AH5416(ah)->ah_cal_pacal)
264 		AH5416(ah)->ah_cal_pacal(ah, AH_TRUE);
265 
266 	/*
267 	 * Do NF calibration after DC offset and other CALs.
268 	 * Per system engineers, noise floor value can sometimes be 20 dB
269 	 * higher than normal value if DC offset and noise floor cal are
270 	 * triggered at the same time.
271 	 */
272 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
273 
274 	/*
275 	 * This may take a while to run; make sure subsequent
276 	 * calibration routines check that this has completed
277 	 * before reading the value and triggering a subsequent
278 	 * calibration.
279 	 */
280 
281 	/* Initialize list pointers */
282 	cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
283 
284 	/*
285 	 * Enable IQ, ADC Gain, ADC DC Offset Cals
286 	 */
287 	if (AR_SREV_HOWL(ah) || AR_SREV_SOWL_10_OR_LATER(ah)) {
288 		/* Setup all non-periodic, init time only calibrations */
289 		/* XXX: Init DC Offset not working yet */
290 #if 0
291 		if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
292 			INIT_CAL(&cal->adcDcCalInitData);
293 			INSERT_CAL(cal, &cal->adcDcCalInitData);
294 		}
295 		/* Initialize current pointer to first element in list */
296 		cal->cal_curr = cal->cal_list;
297 
298 		if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
299 			return AH_FALSE;
300 #endif
301 	}
302 
303 	/* If Cals are supported, add them to list via INIT/INSERT_CAL */
304 	if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
305 		INIT_CAL(&cal->adcGainCalData);
306 		INSERT_CAL(cal, &cal->adcGainCalData);
307 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
308 		    "%s: enable ADC Gain Calibration.\n", __func__);
309 	}
310 	if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
311 		INIT_CAL(&cal->adcDcCalData);
312 		INSERT_CAL(cal, &cal->adcDcCalData);
313 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
314 		    "%s: enable ADC DC Calibration.\n", __func__);
315 	}
316 	if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
317 		INIT_CAL(&cal->iqCalData);
318 		INSERT_CAL(cal, &cal->iqCalData);
319 		HALDEBUG(ah, HAL_DEBUG_PERCAL,
320 		    "%s: enable IQ Calibration.\n", __func__);
321 	}
322 	/* Initialize current pointer to first element in list */
323 	cal->cal_curr = cal->cal_list;
324 
325 	/* Kick off measurements for the first cal */
326 	if (cal->cal_curr != AH_NULL)
327 		ar5416ResetMeasurement(ah, cal->cal_curr);
328 
329 	/* Mark all calibrations on this channel as being invalid */
330 	ichan->calValid = 0;
331 
332 	return AH_TRUE;
333 #undef	MAX_CAL_CHECK
334 }
335 
336 /*
337  * Entry point for upper layers to restart current cal.
338  * Reset the calibration valid bit in channel.
339  */
340 HAL_BOOL
341 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
342 {
343 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
344 	HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
345 	HAL_CAL_LIST *currCal = cal->cal_curr;
346 
347 	if (!AR_SREV_SOWL_10_OR_LATER(ah))
348 		return AH_FALSE;
349 	if (currCal == AH_NULL)
350 		return AH_FALSE;
351 	if (ichan == AH_NULL) {
352 		HALDEBUG(ah, HAL_DEBUG_ANY,
353 		    "%s: invalid channel %u/0x%x; no mapping\n",
354 		    __func__, chan->ic_freq, chan->ic_flags);
355 		return AH_FALSE;
356 	}
357 	/*
358 	 * Expected that this calibration has run before, post-reset.
359 	 * Current state should be done
360 	 */
361 	if (currCal->calState != CAL_DONE) {
362 		HALDEBUG(ah, HAL_DEBUG_ANY,
363 		    "%s: Calibration state incorrect, %d\n",
364 		    __func__, currCal->calState);
365 		return AH_FALSE;
366 	}
367 
368 	/* Verify Cal is supported on this channel */
369 	if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
370 		return AH_FALSE;
371 
372 	HALDEBUG(ah, HAL_DEBUG_PERCAL,
373 	    "%s: Resetting Cal %d state for channel %u/0x%x\n",
374 	    __func__, currCal->calData->calType, chan->ic_freq,
375 	    chan->ic_flags);
376 
377 	/* Disable cal validity in channel */
378 	ichan->calValid &= ~currCal->calData->calType;
379 	currCal->calState = CAL_WAITING;
380 
381 	return AH_TRUE;
382 }
383 
384 /*
385  * Recalibrate the lower PHY chips to account for temperature/environment
386  * changes.
387  */
388 static void
389 ar5416DoCalibration(struct ath_hal *ah,  HAL_CHANNEL_INTERNAL *ichan,
390 	uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
391 {
392 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
393 
394 	/* Cal is assumed not done until explicitly set below */
395 	*isCalDone = AH_FALSE;
396 
397 	HALDEBUG(ah, HAL_DEBUG_PERCAL,
398 	    "%s: %s Calibration, state %d, calValid 0x%x\n",
399 	    __func__, currCal->calData->calName, currCal->calState,
400 	    ichan->calValid);
401 
402 	/* Calibration in progress. */
403 	if (currCal->calState == CAL_RUNNING) {
404 		/* Check to see if it has finished. */
405 		if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
406 			HALDEBUG(ah, HAL_DEBUG_PERCAL,
407 			    "%s: sample %d of %d finished\n",
408 			    __func__, cal->calSamples,
409 			    currCal->calData->calNumSamples);
410 			/*
411 			 * Collect measurements for active chains.
412 			 */
413 			currCal->calData->calCollect(ah);
414 			if (++cal->calSamples >= currCal->calData->calNumSamples) {
415 				int i, numChains = 0;
416 				for (i = 0; i < AR5416_MAX_CHAINS; i++) {
417 					if (rxchainmask & (1 << i))
418 						numChains++;
419 				}
420 				/*
421 				 * Process accumulated data
422 				 */
423 				currCal->calData->calPostProc(ah, numChains);
424 
425 				/* Calibration has finished. */
426 				ichan->calValid |= currCal->calData->calType;
427 				currCal->calState = CAL_DONE;
428 				*isCalDone = AH_TRUE;
429 			} else {
430 				/*
431 				 * Set-up to collect of another sub-sample.
432 				 */
433 				ar5416SetupMeasurement(ah, currCal);
434 			}
435 		}
436 	} else if (!(ichan->calValid & currCal->calData->calType)) {
437 		/* If current cal is marked invalid in channel, kick it off */
438 		ar5416ResetMeasurement(ah, currCal);
439 	}
440 }
441 
442 /*
443  * Internal interface to schedule periodic calibration work.
444  */
445 HAL_BOOL
446 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
447 	u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
448 {
449 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
450 	HAL_CAL_LIST *currCal = cal->cal_curr;
451 	HAL_CHANNEL_INTERNAL *ichan;
452 	int r;
453 
454 	OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
455 
456 	*isCalDone = AH_TRUE;
457 
458 	/*
459 	 * Since ath_hal calls the PerCal method with rxchainmask=0x1;
460 	 * override it with the current chainmask. The upper levels currently
461 	 * doesn't know about the chainmask.
462 	 */
463 	rxchainmask = AH5416(ah)->ah_rx_chainmask;
464 
465 	/* Invalid channel check */
466 	ichan = ath_hal_checkchannel(ah, chan);
467 	if (ichan == AH_NULL) {
468 		HALDEBUG(ah, HAL_DEBUG_ANY,
469 		    "%s: invalid channel %u/0x%x; no mapping\n",
470 		    __func__, chan->ic_freq, chan->ic_flags);
471 		return AH_FALSE;
472 	}
473 
474 	/*
475 	 * For given calibration:
476 	 * 1. Call generic cal routine
477 	 * 2. When this cal is done (isCalDone) if we have more cals waiting
478 	 *    (eg after reset), mask this to upper layers by not propagating
479 	 *    isCalDone if it is set to TRUE.
480 	 *    Instead, change isCalDone to FALSE and setup the waiting cal(s)
481 	 *    to be run.
482 	 */
483 	if (currCal != AH_NULL &&
484 	    (currCal->calState == CAL_RUNNING ||
485 	     currCal->calState == CAL_WAITING)) {
486 		ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
487 		if (*isCalDone == AH_TRUE) {
488 			cal->cal_curr = currCal = currCal->calNext;
489 			if (currCal->calState == CAL_WAITING) {
490 				*isCalDone = AH_FALSE;
491 				ar5416ResetMeasurement(ah, currCal);
492 			}
493 		}
494 	}
495 
496 	/* Do NF cal only at longer intervals */
497 	if (longcal) {
498 		/* Do PA calibration if the chipset supports */
499 		if (AH5416(ah)->ah_cal_pacal)
500 			AH5416(ah)->ah_cal_pacal(ah, AH_FALSE);
501 
502 		/* Do open-loop temperature compensation if the chipset needs it */
503 		if (ath_hal_eepromGetFlag(ah, AR_EEP_OL_PWRCTRL))
504 			AH5416(ah)->ah_olcTempCompensation(ah);
505 
506 		/*
507 		 * Get the value from the previous NF cal
508 		 * and update the history buffer.
509 		 */
510 		r = ar5416GetNf(ah, chan);
511 		if (r == 0 || r == -1) {
512 			/* NF calibration result isn't valid */
513 			HALDEBUG(ah, HAL_DEBUG_UNMASKABLE, "%s: NF calibration"
514 			    " didn't finish; delaying CCA\n", __func__);
515 		} else {
516 			int ret;
517 			/*
518 			 * NF calibration result is valid.
519 			 *
520 			 * Load the NF from history buffer of the current channel.
521 			 * NF is slow time-variant, so it is OK to use a
522 			 * historical value.
523 			 */
524 			ret = ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
525 
526 			/* start NF calibration, without updating BB NF register*/
527 			ar5416StartNFCal(ah);
528 
529 			/*
530 			 * If we failed calibration then tell the driver
531 			 * we failed and it should do a full chip reset
532 			 */
533 			if (! ret)
534 				return AH_FALSE;
535 		}
536 	}
537 	return AH_TRUE;
538 }
539 
540 /*
541  * Recalibrate the lower PHY chips to account for temperature/environment
542  * changes.
543  */
544 HAL_BOOL
545 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
546 	HAL_BOOL *isIQdone)
547 {
548 	struct ath_hal_5416 *ahp = AH5416(ah);
549 	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
550 	HAL_CAL_LIST *curCal = cal->cal_curr;
551 
552 	if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
553 		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
554 		    AH_TRUE, isIQdone);
555 	} else {
556 		HAL_BOOL isCalDone;
557 
558 		*isIQdone = AH_FALSE;
559 		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
560 		    AH_TRUE, &isCalDone);
561 	}
562 }
563 
564 static HAL_BOOL
565 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
566 	const struct ieee80211_channel *chan, int16_t *nft)
567 {
568 	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
569 		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
570 		return AH_TRUE;
571 	}
572 	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
573 		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
574 		return AH_TRUE;
575 	}
576 	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
577 	    __func__, chan->ic_flags);
578 	return AH_FALSE;
579 }
580 
581 static void
582 ar5416StartNFCal(struct ath_hal *ah)
583 {
584 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
585 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
586 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
587 }
588 
589 static HAL_BOOL
590 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
591 {
592 	static const uint32_t ar5416_cca_regs[] = {
593 		AR_PHY_CCA,
594 		AR_PHY_CH1_CCA,
595 		AR_PHY_CH2_CCA,
596 		AR_PHY_EXT_CCA,
597 		AR_PHY_CH1_EXT_CCA,
598 		AR_PHY_CH2_EXT_CCA
599 	};
600 	struct ar5212NfCalHist *h;
601 	int i;
602 	int32_t val;
603 	uint8_t chainmask;
604 	int16_t default_nf = ar5416GetDefaultNF(ah, chan);
605 
606 	/*
607 	 * Force NF calibration for all chains.
608 	 */
609 	if (AR_SREV_KITE(ah)) {
610 		/* Kite has only one chain */
611 		chainmask = 0x9;
612 	} else if (AR_SREV_MERLIN(ah) || AR_SREV_KIWI(ah)) {
613 		/* Merlin/Kiwi has only two chains */
614 		chainmask = 0x1B;
615 	} else {
616 		chainmask = 0x3F;
617 	}
618 
619 	/*
620 	 * Write filtered NF values into maxCCApwr register parameter
621 	 * so we can load below.
622 	 */
623 	h = AH5416(ah)->ah_cal.nfCalHist;
624 	HALDEBUG(ah, HAL_DEBUG_NFCAL, "CCA: ");
625 	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
626 
627 		/* Don't write to EXT radio CCA registers unless in HT/40 mode */
628 		/* XXX this check should really be cleaner! */
629 		if (i > 2 && !IEEE80211_IS_CHAN_HT40(chan))
630 			continue;
631 
632 		if (chainmask & (1 << i)) {
633 			int16_t nf_val;
634 
635 			if (h)
636 				nf_val = h[i].privNF;
637 			else
638 				nf_val = default_nf;
639 
640 			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
641 			val &= 0xFFFFFE00;
642 			val |= (((uint32_t) nf_val << 1) & 0x1ff);
643 			HALDEBUG(ah, HAL_DEBUG_NFCAL, "[%d: %d]", i, nf_val);
644 			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
645 		}
646 	}
647 	HALDEBUG(ah, HAL_DEBUG_NFCAL, "\n");
648 
649 	/* Load software filtered NF value into baseband internal minCCApwr variable. */
650 	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
651 	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
652 	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
653 
654 	/* Wait for load to complete, should be fast, a few 10s of us. */
655 	if (! ar5212WaitNFCalComplete(ah, 1000)) {
656 		/*
657 		 * We timed out waiting for the noisefloor to load, probably due to an
658 		 * in-progress rx. Simply return here and allow the load plenty of time
659 		 * to complete before the next calibration interval.  We need to avoid
660 		 * trying to load -50 (which happens below) while the previous load is
661 		 * still in progress as this can cause rx deafness. Instead by returning
662 		 * here, the baseband nf cal will just be capped by our present
663 		 * noisefloor until the next calibration timer.
664 		 */
665 		HALDEBUG(ah, HAL_DEBUG_UNMASKABLE, "Timeout while waiting for "
666 		    "nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
667 		    OS_REG_READ(ah, AR_PHY_AGC_CONTROL));
668 		return AH_FALSE;
669 	}
670 
671 	/*
672 	 * Restore maxCCAPower register parameter again so that we're not capped
673 	 * by the median we just loaded.  This will be initial (and max) value
674 	 * of next noise floor calibration the baseband does.
675 	 */
676 	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
677 
678 		/* Don't write to EXT radio CCA registers unless in HT/40 mode */
679 		/* XXX this check should really be cleaner! */
680 		if (i > 2 && !IEEE80211_IS_CHAN_HT40(chan))
681 			continue;
682 
683 		if (chainmask & (1 << i)) {
684 			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
685 			val &= 0xFFFFFE00;
686 			val |= (((uint32_t)(-50) << 1) & 0x1ff);
687 			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
688 		}
689 	}
690 	return AH_TRUE;
691 }
692 
693 /*
694  * This just initialises the "good" values for AR5416 which
695  * may not be right; it'lll be overridden by ar5416SanitizeNF()
696  * to nominal values.
697  */
698 void
699 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
700 {
701 	int i, j;
702 
703 	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
704 		h[i].currIndex = 0;
705 		h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
706 		h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
707 		for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
708 			h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
709 	}
710 }
711 
712 /*
713  * Update the noise floor buffer as a ring buffer
714  */
715 static void
716 ar5416UpdateNFHistBuff(struct ath_hal *ah, struct ar5212NfCalHist *h,
717     int16_t *nfarray)
718 {
719 	int i;
720 
721 	/* XXX TODO: don't record nfarray[] entries for inactive chains */
722 	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
723 		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
724 
725 		if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
726 			h[i].currIndex = 0;
727 		if (h[i].invalidNFcount > 0) {
728 			if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
729 			    nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
730 				h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
731 			} else {
732 				h[i].invalidNFcount--;
733 				h[i].privNF = nfarray[i];
734 			}
735 		} else {
736 			h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
737 		}
738 	}
739 }
740 
741 static uint16_t
742 ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
743 {
744         struct ar5416NfLimits *limit;
745 
746         if (!chan || IEEE80211_IS_CHAN_2GHZ(chan))
747                 limit = &AH5416(ah)->nf_2g;
748         else
749                 limit = &AH5416(ah)->nf_5g;
750 
751         return limit->nominal;
752 }
753 
754 static void
755 ar5416SanitizeNF(struct ath_hal *ah, int16_t *nf)
756 {
757 
758         struct ar5416NfLimits *limit;
759         int i;
760 
761         if (IEEE80211_IS_CHAN_2GHZ(AH_PRIVATE(ah)->ah_curchan))
762                 limit = &AH5416(ah)->nf_2g;
763         else
764                 limit = &AH5416(ah)->nf_5g;
765 
766         for (i = 0; i < AR5416_NUM_NF_READINGS; i++) {
767                 if (!nf[i])
768                         continue;
769 
770                 if (nf[i] > limit->max) {
771                         HALDEBUG(ah, HAL_DEBUG_NFCAL,
772                                   "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
773                                   i, nf[i], limit->max);
774                         nf[i] = limit->max;
775                 } else if (nf[i] < limit->min) {
776                         HALDEBUG(ah, HAL_DEBUG_NFCAL,
777                                   "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
778                                   i, nf[i], limit->min);
779                         nf[i] = limit->nominal;
780                 }
781         }
782 }
783 
784 
785 /*
786  * Read the NF and check it against the noise floor threshold
787  *
788  * Return 0 if the NF calibration hadn't finished, 0 if it was
789  * invalid, or > 0 for a valid NF reading.
790  */
791 static int16_t
792 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
793 {
794 	int16_t nf, nfThresh;
795 	int i;
796 	int retval = 0;
797 
798 	if (ar5212IsNFCalInProgress(ah)) {
799 		HALDEBUG(ah, HAL_DEBUG_ANY,
800 		    "%s: NF didn't complete in calibration window\n", __func__);
801 		nf = 0;
802 		retval = -1;	/* NF didn't finish */
803 	} else {
804 		/* Finished NF cal, check against threshold */
805 		int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
806 		HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
807 
808 		/* TODO - enhance for multiple chains and ext ch */
809 		ath_hal_getNoiseFloor(ah, nfarray);
810 		nf = nfarray[0];
811 		ar5416SanitizeNF(ah, nfarray);
812 		if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
813 			if (nf > nfThresh) {
814 				HALDEBUG(ah, HAL_DEBUG_UNMASKABLE,
815 				    "%s: noise floor failed detected; "
816 				    "detected %d, threshold %d\n", __func__,
817 				    nf, nfThresh);
818 				/*
819 				 * NB: Don't discriminate 2.4 vs 5Ghz, if this
820 				 *     happens it indicates a problem regardless
821 				 *     of the band.
822 				 */
823 				chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
824 				nf = 0;
825 				retval = 0;
826 			}
827 		} else {
828 			nf = 0;
829 			retval = 0;
830 		}
831 		/* Update MIMO channel statistics, regardless of validity or not (for now) */
832 		for (i = 0; i < 3; i++) {
833 			ichan->noiseFloorCtl[i] = nfarray[i];
834 			ichan->noiseFloorExt[i] = nfarray[i + 3];
835 		}
836 		ichan->privFlags |= CHANNEL_MIMO_NF_VALID;
837 
838 		ar5416UpdateNFHistBuff(ah, AH5416(ah)->ah_cal.nfCalHist, nfarray);
839 		ichan->rawNoiseFloor = nf;
840 		retval = nf;
841 	}
842 	return retval;
843 }
844