ar5416_cal.c revision 218068
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 218068 2011-01-29 14:27:20Z 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
40static uint16_t ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan);
41static 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 */
63static OS_INLINE HAL_BOOL
64ar5416IsCalSupp(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 */
85static void
86ar5416SetupMeasurement(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 */
123static void
124ar5416ResetMeasurement(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 */
143static HAL_BOOL
144ar5416RunInitCals(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 */
189HAL_BOOL
190ar5416InitCal(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 */
314HAL_BOOL
315ar5416ResetCalValid(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 */
362static void
363ar5416DoCalibration(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 */
419HAL_BOOL
420ar5416PerCalibrationN(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 */
494HAL_BOOL
495ar5416PerCalibration(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
514static HAL_BOOL
515ar5416GetEepromNoiseFloorThresh(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
531static void
532ar5416StartNFCal(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
539static void
540ar5416LoadNF(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		if (chainmask & (1 << i)) {
577			int16_t nf_val;
578
579			if (h)
580				nf_val = h[i].privNF;
581			else
582				nf_val = default_nf;
583
584			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
585			val &= 0xFFFFFE00;
586			val |= (((uint32_t) nf_val << 1) & 0x1ff);
587			HALDEBUG(ah, HAL_DEBUG_NFCAL, "[%d: %d]", i, nf_val);
588			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
589		}
590	}
591	HALDEBUG(ah, HAL_DEBUG_NFCAL, "\n");
592
593	/* Load software filtered NF value into baseband internal minCCApwr variable. */
594	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
595	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
596	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
597
598	/* Wait for load to complete, should be fast, a few 10s of us. */
599	if (! ar5212WaitNFCalComplete(ah, 1000)) {
600		/*
601		 * We timed out waiting for the noisefloor to load, probably due to an
602		 * in-progress rx. Simply return here and allow the load plenty of time
603		 * to complete before the next calibration interval.  We need to avoid
604		 * trying to load -50 (which happens below) while the previous load is
605		 * still in progress as this can cause rx deafness. Instead by returning
606		 * here, the baseband nf cal will just be capped by our present
607		 * noisefloor until the next calibration timer.
608		 */
609		HALDEBUG(ah, HAL_DEBUG_ANY, "Timeout while waiting for nf "
610		    "to load: AR_PHY_AGC_CONTROL=0x%x\n",
611		    OS_REG_READ(ah, AR_PHY_AGC_CONTROL));
612		return;
613	}
614
615	/*
616	 * Restore maxCCAPower register parameter again so that we're not capped
617	 * by the median we just loaded.  This will be initial (and max) value
618	 * of next noise floor calibration the baseband does.
619	 */
620	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
621		if (chainmask & (1 << i)) {
622			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
623			val &= 0xFFFFFE00;
624			val |= (((uint32_t)(-50) << 1) & 0x1ff);
625			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
626		}
627}
628
629/*
630 * This just initialises the "good" values for AR5416 which
631 * may not be right; it'lll be overridden by ar5416SanitizeNF()
632 * to nominal values.
633 */
634void
635ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
636{
637	int i, j;
638
639	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
640		h[i].currIndex = 0;
641		h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
642		h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
643		for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
644			h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
645	}
646}
647
648/*
649 * Update the noise floor buffer as a ring buffer
650 */
651static void
652ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
653{
654	int i;
655
656	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
657		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
658
659		if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
660			h[i].currIndex = 0;
661		if (h[i].invalidNFcount > 0) {
662			if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
663			    nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
664				h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
665			} else {
666				h[i].invalidNFcount--;
667				h[i].privNF = nfarray[i];
668			}
669		} else {
670			h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
671		}
672	}
673}
674
675static uint16_t
676ar5416GetDefaultNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
677{
678        struct ar5416NfLimits *limit;
679
680        if (!chan || IEEE80211_IS_CHAN_2GHZ(chan))
681                limit = &AH5416(ah)->nf_2g;
682        else
683                limit = &AH5416(ah)->nf_5g;
684
685        return limit->nominal;
686}
687
688static void
689ar5416SanitizeNF(struct ath_hal *ah, int16_t *nf)
690{
691
692        struct ar5416NfLimits *limit;
693        int i;
694
695        if (IEEE80211_IS_CHAN_2GHZ(AH_PRIVATE(ah)->ah_curchan))
696                limit = &AH5416(ah)->nf_2g;
697        else
698                limit = &AH5416(ah)->nf_5g;
699
700        for (i = 0; i < AR5416_NUM_NF_READINGS; i++) {
701                if (!nf[i])
702                        continue;
703
704                if (nf[i] > limit->max) {
705                        HALDEBUG(ah, HAL_DEBUG_NFCAL,
706                                  "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
707                                  i, nf[i], limit->max);
708                        nf[i] = limit->max;
709                } else if (nf[i] < limit->min) {
710                        HALDEBUG(ah, HAL_DEBUG_NFCAL,
711                                  "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
712                                  i, nf[i], limit->min);
713                        nf[i] = limit->nominal;
714                }
715        }
716}
717
718
719/*
720 * Read the NF and check it against the noise floor threshhold
721 */
722static int16_t
723ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
724{
725	int16_t nf, nfThresh;
726
727	if (ar5212IsNFCalInProgress(ah)) {
728		HALDEBUG(ah, HAL_DEBUG_ANY,
729		    "%s: NF didn't complete in calibration window\n", __func__);
730		nf = 0;
731	} else {
732		/* Finished NF cal, check against threshold */
733		int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
734		HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
735
736		/* TODO - enhance for multiple chains and ext ch */
737		ath_hal_getNoiseFloor(ah, nfarray);
738		nf = nfarray[0];
739		ar5416SanitizeNF(ah, nfarray);
740		if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
741			if (nf > nfThresh) {
742				HALDEBUG(ah, HAL_DEBUG_ANY,
743				    "%s: noise floor failed detected; "
744				    "detected %d, threshold %d\n", __func__,
745				    nf, nfThresh);
746				/*
747				 * NB: Don't discriminate 2.4 vs 5Ghz, if this
748				 *     happens it indicates a problem regardless
749				 *     of the band.
750				 */
751				chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
752				nf = 0;
753			}
754		} else {
755			nf = 0;
756		}
757		ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
758		ichan->rawNoiseFloor = nf;
759	}
760	return nf;
761}
762