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