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