ar5416_cal.c revision 211207
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 211207 2010-08-12 06:08:36Z 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	/* Invalid channel check */
411	ichan = ath_hal_checkchannel(ah, chan);
412	if (ichan == AH_NULL) {
413		HALDEBUG(ah, HAL_DEBUG_ANY,
414		    "%s: invalid channel %u/0x%x; no mapping\n",
415		    __func__, chan->ic_freq, chan->ic_flags);
416		return AH_FALSE;
417	}
418
419	/*
420	 * For given calibration:
421	 * 1. Call generic cal routine
422	 * 2. When this cal is done (isCalDone) if we have more cals waiting
423	 *    (eg after reset), mask this to upper layers by not propagating
424	 *    isCalDone if it is set to TRUE.
425	 *    Instead, change isCalDone to FALSE and setup the waiting cal(s)
426	 *    to be run.
427	 */
428	if (currCal != AH_NULL &&
429	    (currCal->calState == CAL_RUNNING ||
430	     currCal->calState == CAL_WAITING)) {
431		ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
432		if (*isCalDone == AH_TRUE) {
433			cal->cal_curr = currCal = currCal->calNext;
434			if (currCal->calState == CAL_WAITING) {
435				*isCalDone = AH_FALSE;
436				ar5416ResetMeasurement(ah, currCal);
437			}
438		}
439	}
440
441	/* Do NF cal only at longer intervals */
442	if (longcal) {
443		/*
444		 * Get the value from the previous NF cal
445		 * and update the history buffer.
446		 */
447		ar5416GetNf(ah, chan);
448
449		/*
450		 * Load the NF from history buffer of the current channel.
451		 * NF is slow time-variant, so it is OK to use a
452		 * historical value.
453		 */
454		ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
455
456		/* start NF calibration, without updating BB NF register*/
457		ar5416StartNFCal(ah);
458	}
459	return AH_TRUE;
460}
461
462/*
463 * Recalibrate the lower PHY chips to account for temperature/environment
464 * changes.
465 */
466HAL_BOOL
467ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
468	HAL_BOOL *isIQdone)
469{
470	struct ath_hal_5416 *ahp = AH5416(ah);
471	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
472	HAL_CAL_LIST *curCal = cal->cal_curr;
473
474	if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
475		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
476		    AH_TRUE, isIQdone);
477	} else {
478		HAL_BOOL isCalDone;
479
480		*isIQdone = AH_FALSE;
481		return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
482		    AH_TRUE, &isCalDone);
483	}
484}
485
486static HAL_BOOL
487ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
488	const struct ieee80211_channel *chan, int16_t *nft)
489{
490	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
491		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
492		return AH_TRUE;
493	}
494	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
495		ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
496		return AH_TRUE;
497	}
498	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
499	    __func__, chan->ic_flags);
500	return AH_FALSE;
501}
502
503static void
504ar5416StartNFCal(struct ath_hal *ah)
505{
506	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
507	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
508	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
509}
510
511static void
512ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
513{
514	static const uint32_t ar5416_cca_regs[] = {
515		AR_PHY_CCA,
516		AR_PHY_CH1_CCA,
517		AR_PHY_CH2_CCA,
518		AR_PHY_EXT_CCA,
519		AR_PHY_CH1_EXT_CCA,
520		AR_PHY_CH2_EXT_CCA
521	};
522	struct ar5212NfCalHist *h;
523	int i, j;
524	int32_t val;
525	uint8_t chainmask;
526
527	/*
528	 * Force NF calibration for all chains.
529	 */
530	if (AR_SREV_KITE(ah)) {
531		/* Kite has only one chain */
532		chainmask = 0x9;
533	} else if (AR_SREV_MERLIN(ah)) {
534		/* Merlin has only two chains */
535		chainmask = 0x1B;
536	} else {
537		chainmask = 0x3F;
538	}
539
540	/*
541	 * Write filtered NF values into maxCCApwr register parameter
542	 * so we can load below.
543	 */
544	h = AH5416(ah)->ah_cal.nfCalHist;
545	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
546		if (chainmask & (1 << i)) {
547			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
548			val &= 0xFFFFFE00;
549			val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
550			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
551		}
552
553	/* Load software filtered NF value into baseband internal minCCApwr variable. */
554	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
555	OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
556	OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
557
558	/* Wait for load to complete, should be fast, a few 10s of us. */
559	for (j = 0; j < 1000; j++) {
560		if ((OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) == 0)
561			break;
562		OS_DELAY(10);
563	}
564
565	/*
566	 * Restore maxCCAPower register parameter again so that we're not capped
567	 * by the median we just loaded.  This will be initial (and max) value
568	 * of next noise floor calibration the baseband does.
569	 */
570	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
571		if (chainmask & (1 << i)) {
572			val = OS_REG_READ(ah, ar5416_cca_regs[i]);
573			val &= 0xFFFFFE00;
574			val |= (((uint32_t)(-50) << 1) & 0x1ff);
575			OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
576		}
577}
578
579void
580ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
581{
582	int i, j;
583
584	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
585		h[i].currIndex = 0;
586		h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
587		h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
588		for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
589			h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
590	}
591}
592
593/*
594 * Update the noise floor buffer as a ring buffer
595 */
596static void
597ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
598{
599	int i;
600
601	for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
602		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
603
604		if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
605			h[i].currIndex = 0;
606		if (h[i].invalidNFcount > 0) {
607			if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
608			    nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
609				h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
610			} else {
611				h[i].invalidNFcount--;
612				h[i].privNF = nfarray[i];
613			}
614		} else {
615			h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
616		}
617	}
618}
619
620/*
621 * Read the NF and check it against the noise floor threshhold
622 */
623static int16_t
624ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
625{
626	int16_t nf, nfThresh;
627
628	if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
629		HALDEBUG(ah, HAL_DEBUG_ANY,
630		    "%s: NF didn't complete in calibration window\n", __func__);
631		nf = 0;
632	} else {
633		/* Finished NF cal, check against threshold */
634		int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
635		HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
636
637		/* TODO - enhance for multiple chains and ext ch */
638		ath_hal_getNoiseFloor(ah, nfarray);
639		nf = nfarray[0];
640		if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
641			if (nf > nfThresh) {
642				HALDEBUG(ah, HAL_DEBUG_ANY,
643				    "%s: noise floor failed detected; "
644				    "detected %d, threshold %d\n", __func__,
645				    nf, nfThresh);
646				/*
647				 * NB: Don't discriminate 2.4 vs 5Ghz, if this
648				 *     happens it indicates a problem regardless
649				 *     of the band.
650				 */
651				chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
652				nf = 0;
653			}
654		} else {
655			nf = 0;
656		}
657		ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
658		ichan->rawNoiseFloor = nf;
659	}
660	return nf;
661}
662