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