ar5416_cal.c revision 211211
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 211211 2010-08-12 06:20:54Z adrian $
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
36static void ar5416StartNFCal(struct ath_hal *ah);
37static void ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *);
38static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *);
39
40/*
41 * Determine if calibration is supported by device and channel flags
42 */
43static OS_INLINE HAL_BOOL
44ar5416IsCalSupp(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 */
65static void
66ar5416SetupMeasurement(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 */
103static void
104ar5416ResetMeasurement(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 */
123static HAL_BOOL
124ar5416RunInitCals(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 */
169HAL_BOOL
170ar5416InitCal(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	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
225	/*
226	 * Try to make sure the above NF cal completes, just so
227	 * it doesn't clash with subsequent percals -adrian
228	 */
229	if (! ar5212WaitNFCalComplete(ah, 10000)) {
230		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: initial NF calibration did "
231		    "not complete in time; noisy environment?\n", __func__);
232		return AH_FALSE;
233	}
234
235	/* Initialize list pointers */
236	cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
237
238	/*
239	 * Enable IQ, ADC Gain, ADC DC Offset Cals
240	 */
241	if (AR_SREV_SOWL_10_OR_LATER(ah)) {
242		/* Setup all non-periodic, init time only calibrations */
243		/* XXX: Init DC Offset not working yet */
244#if 0
245		if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
246			INIT_CAL(&cal->adcDcCalInitData);
247			INSERT_CAL(cal, &cal->adcDcCalInitData);
248		}
249		/* Initialize current pointer to first element in list */
250		cal->cal_curr = cal->cal_list;
251
252		if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
253			return AH_FALSE;
254#endif
255	}
256
257	/* If Cals are supported, add them to list via INIT/INSERT_CAL */
258	if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
259		INIT_CAL(&cal->adcGainCalData);
260		INSERT_CAL(cal, &cal->adcGainCalData);
261		HALDEBUG(ah, HAL_DEBUG_PERCAL,
262		    "%s: enable ADC Gain Calibration.\n", __func__);
263	}
264	if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
265		INIT_CAL(&cal->adcDcCalData);
266		INSERT_CAL(cal, &cal->adcDcCalData);
267		HALDEBUG(ah, HAL_DEBUG_PERCAL,
268		    "%s: enable ADC DC Calibration.\n", __func__);
269	}
270	if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
271		INIT_CAL(&cal->iqCalData);
272		INSERT_CAL(cal, &cal->iqCalData);
273		HALDEBUG(ah, HAL_DEBUG_PERCAL,
274		    "%s: enable IQ Calibration.\n", __func__);
275	}
276	/* Initialize current pointer to first element in list */
277	cal->cal_curr = cal->cal_list;
278
279	/* Kick off measurements for the first cal */
280	if (cal->cal_curr != AH_NULL)
281		ar5416ResetMeasurement(ah, cal->cal_curr);
282
283	/* Mark all calibrations on this channel as being invalid */
284	ichan->calValid = 0;
285
286	return AH_TRUE;
287}
288
289/*
290 * Entry point for upper layers to restart current cal.
291 * Reset the calibration valid bit in channel.
292 */
293HAL_BOOL
294ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
295{
296	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
297	HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
298	HAL_CAL_LIST *currCal = cal->cal_curr;
299
300	if (!AR_SREV_SOWL_10_OR_LATER(ah))
301		return AH_FALSE;
302	if (currCal == AH_NULL)
303		return AH_FALSE;
304	if (ichan == AH_NULL) {
305		HALDEBUG(ah, HAL_DEBUG_ANY,
306		    "%s: invalid channel %u/0x%x; no mapping\n",
307		    __func__, chan->ic_freq, chan->ic_flags);
308		return AH_FALSE;
309	}
310	/*
311	 * Expected that this calibration has run before, post-reset.
312	 * Current state should be done
313	 */
314	if (currCal->calState != CAL_DONE) {
315		HALDEBUG(ah, HAL_DEBUG_ANY,
316		    "%s: Calibration state incorrect, %d\n",
317		    __func__, currCal->calState);
318		return AH_FALSE;
319	}
320
321	/* Verify Cal is supported on this channel */
322	if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
323		return AH_FALSE;
324
325	HALDEBUG(ah, HAL_DEBUG_PERCAL,
326	    "%s: Resetting Cal %d state for channel %u/0x%x\n",
327	    __func__, currCal->calData->calType, chan->ic_freq,
328	    chan->ic_flags);
329
330	/* Disable cal validity in channel */
331	ichan->calValid &= ~currCal->calData->calType;
332	currCal->calState = CAL_WAITING;
333
334	return AH_TRUE;
335}
336
337/*
338 * Recalibrate the lower PHY chips to account for temperature/environment
339 * changes.
340 */
341static void
342ar5416DoCalibration(struct ath_hal *ah,  HAL_CHANNEL_INTERNAL *ichan,
343	uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
344{
345	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
346
347	/* Cal is assumed not done until explicitly set below */
348	*isCalDone = AH_FALSE;
349
350	HALDEBUG(ah, HAL_DEBUG_PERCAL,
351	    "%s: %s Calibration, state %d, calValid 0x%x\n",
352	    __func__, currCal->calData->calName, currCal->calState,
353	    ichan->calValid);
354
355	/* Calibration in progress. */
356	if (currCal->calState == CAL_RUNNING) {
357		/* Check to see if it has finished. */
358		if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
359			HALDEBUG(ah, HAL_DEBUG_PERCAL,
360			    "%s: sample %d of %d finished\n",
361			    __func__, cal->calSamples,
362			    currCal->calData->calNumSamples);
363			/*
364			 * Collect measurements for active chains.
365			 */
366			currCal->calData->calCollect(ah);
367			if (++cal->calSamples >= currCal->calData->calNumSamples) {
368				int i, numChains = 0;
369				for (i = 0; i < AR5416_MAX_CHAINS; i++) {
370					if (rxchainmask & (1 << i))
371						numChains++;
372				}
373				/*
374				 * Process accumulated data
375				 */
376				currCal->calData->calPostProc(ah, numChains);
377
378				/* Calibration has finished. */
379				ichan->calValid |= currCal->calData->calType;
380				currCal->calState = CAL_DONE;
381				*isCalDone = AH_TRUE;
382			} else {
383				/*
384				 * Set-up to collect of another sub-sample.
385				 */
386				ar5416SetupMeasurement(ah, currCal);
387			}
388		}
389	} else if (!(ichan->calValid & currCal->calData->calType)) {
390		/* If current cal is marked invalid in channel, kick it off */
391		ar5416ResetMeasurement(ah, currCal);
392	}
393}
394
395/*
396 * Internal interface to schedule periodic calibration work.
397 */
398HAL_BOOL
399ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
400	u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
401{
402	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
403	HAL_CAL_LIST *currCal = cal->cal_curr;
404	HAL_CHANNEL_INTERNAL *ichan;
405
406	OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
407
408	*isCalDone = AH_TRUE;
409
410	/*
411	 * Since ath_hal calls the PerCal method with rxchainmask=0x1;
412	 * override it with the current chainmask. The upper levels currently
413	 * doesn't know about the chainmask.
414	 */
415	rxchainmask = AH5416(ah)->ah_rx_chainmask;
416
417	/* Invalid channel check */
418	ichan = ath_hal_checkchannel(ah, chan);
419	if (ichan == AH_NULL) {
420		HALDEBUG(ah, HAL_DEBUG_ANY,
421		    "%s: invalid channel %u/0x%x; no mapping\n",
422		    __func__, chan->ic_freq, chan->ic_flags);
423		return AH_FALSE;
424	}
425
426	/*
427	 * For given calibration:
428	 * 1. Call generic cal routine
429	 * 2. When this cal is done (isCalDone) if we have more cals waiting
430	 *    (eg after reset), mask this to upper layers by not propagating
431	 *    isCalDone if it is set to TRUE.
432	 *    Instead, change isCalDone to FALSE and setup the waiting cal(s)
433	 *    to be run.
434	 */
435	if (currCal != AH_NULL &&
436	    (currCal->calState == CAL_RUNNING ||
437	     currCal->calState == CAL_WAITING)) {
438		ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
439		if (*isCalDone == AH_TRUE) {
440			cal->cal_curr = currCal = currCal->calNext;
441			if (currCal->calState == CAL_WAITING) {
442				*isCalDone = AH_FALSE;
443				ar5416ResetMeasurement(ah, currCal);
444			}
445		}
446	}
447
448	/* Do NF cal only at longer intervals */
449	if (longcal) {
450		/*
451		 * Get the value from the previous NF cal
452		 * and update the history buffer.
453		 */
454		ar5416GetNf(ah, chan);
455
456		/*
457		 * Load the NF from history buffer of the current channel.
458		 * NF is slow time-variant, so it is OK to use a
459		 * historical value.
460		 */
461		ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
462
463		/* start NF calibration, without updating BB NF register*/
464		ar5416StartNFCal(ah);
465	}
466	return AH_TRUE;
467}
468
469/*
470 * Recalibrate the lower PHY chips to account for temperature/environment
471 * changes.
472 */
473HAL_BOOL
474ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
475	HAL_BOOL *isIQdone)
476{
477	struct ath_hal_5416 *ahp = AH5416(ah);
478	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
479	HAL_CAL_LIST *curCal = cal->cal_curr;
480
481	if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
482		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
483		    AH_TRUE, isIQdone);
484	} else {
485		HAL_BOOL isCalDone;
486
487		*isIQdone = AH_FALSE;
488		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
489		    AH_TRUE, &isCalDone);
490	}
491}
492
493static HAL_BOOL
494ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
495	const struct ieee80211_channel *chan, int16_t *nft)
496{
497	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
498		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
499		return AH_TRUE;
500	}
501	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
502		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
503		return AH_TRUE;
504	}
505	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
506	    __func__, chan->ic_flags);
507	return AH_FALSE;
508}
509
510static void
511ar5416StartNFCal(struct ath_hal *ah)
512{
513	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
514	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
515	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
516}
517
518static void
519ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
520{
521	static const uint32_t ar5416_cca_regs[] = {
522		AR_PHY_CCA,
523		AR_PHY_CH1_CCA,
524		AR_PHY_CH2_CCA,
525		AR_PHY_EXT_CCA,
526		AR_PHY_CH1_EXT_CCA,
527		AR_PHY_CH2_EXT_CCA
528	};
529	struct ar5212NfCalHist *h;
530	int i;
531	int32_t val;
532	uint8_t chainmask;
533
534	/*
535	 * Force NF calibration for all chains.
536	 */
537	if (AR_SREV_KITE(ah)) {
538		/* Kite has only one chain */
539		chainmask = 0x9;
540	} else if (AR_SREV_MERLIN(ah)) {
541		/* Merlin has only two chains */
542		chainmask = 0x1B;
543	} else {
544		chainmask = 0x3F;
545	}
546
547	/*
548	 * Write filtered NF values into maxCCApwr register parameter
549	 * so we can load below.
550	 */
551	h = AH5416(ah)->ah_cal.nfCalHist;
552	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
553		if (chainmask & (1 << i)) {
554			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
555			val &= 0xFFFFFE00;
556			val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
557			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
558		}
559
560	/* Load software filtered NF value into baseband internal minCCApwr variable. */
561	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
562	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
563	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
564
565	if (! ar5212WaitNFCalComplete(ah, 1000)) {
566		/*
567		 * We timed out waiting for the noisefloor to load, probably due to an
568		 * in-progress rx. Simply return here and allow the load plenty of time
569		 * to complete before the next calibration interval.  We need to avoid
570		 * trying to load -50 (which happens below) while the previous load is
571		 * still in progress as this can cause rx deafness. Instead by returning
572		 * here, the baseband nf cal will just be capped by our present
573		 * noisefloor until the next calibration timer.
574		 */
575		HALDEBUG(ah, HAL_DEBUG_ANY, "Timeout while waiting for nf "
576		    "to load: AR_PHY_AGC_CONTROL=0x%x\n",
577		    OS_REG_READ(ah, AR_PHY_AGC_CONTROL));
578		    return;
579	}
580
581	/*
582	 * Restore maxCCAPower register parameter again so that we're not capped
583	 * by the median we just loaded.  This will be initial (and max) value
584	 * of next noise floor calibration the baseband does.
585	 */
586	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
587		if (chainmask & (1 << i)) {
588			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
589			val &= 0xFFFFFE00;
590			val |= (((uint32_t)(-50) << 1) & 0x1ff);
591			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
592		}
593}
594
595void
596ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
597{
598	int i, j;
599
600	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
601		h[i].currIndex = 0;
602		h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
603		h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
604		for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
605			h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
606	}
607}
608
609/*
610 * Update the noise floor buffer as a ring buffer
611 */
612static void
613ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
614{
615	int i;
616
617	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
618		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
619
620		if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
621			h[i].currIndex = 0;
622		if (h[i].invalidNFcount > 0) {
623			if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
624			    nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
625				h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
626			} else {
627				h[i].invalidNFcount--;
628				h[i].privNF = nfarray[i];
629			}
630		} else {
631			h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
632		}
633	}
634}
635
636/*
637 * Read the NF and check it against the noise floor threshhold
638 */
639static int16_t
640ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
641{
642	int16_t nf, nfThresh;
643
644	if (ar5212IsNFCalInProgress(ah)) {
645		HALDEBUG(ah, HAL_DEBUG_ANY,
646		    "%s: NF didn't complete in calibration window\n", __func__);
647		nf = 0;
648	} else {
649		/* Finished NF cal, check against threshold */
650		int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
651		HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
652
653		/* TODO - enhance for multiple chains and ext ch */
654		ath_hal_getNoiseFloor(ah, nfarray);
655		nf = nfarray[0];
656		if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
657			if (nf > nfThresh) {
658				HALDEBUG(ah, HAL_DEBUG_ANY,
659				    "%s: noise floor failed detected; "
660				    "detected %d, threshold %d\n", __func__,
661				    nf, nfThresh);
662				/*
663				 * NB: Don't discriminate 2.4 vs 5Ghz, if this
664				 *     happens it indicates a problem regardless
665				 *     of the band.
666				 */
667				chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
668				nf = 0;
669			}
670		} else {
671			nf = 0;
672		}
673		ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
674		ichan->rawNoiseFloor = nf;
675	}
676	return nf;
677}
678