2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
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.
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.
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_cal.c 203882 2010-02-14 16:26:32Z rpaulo $
23 #include "ah_internal.h"
26 #include "ah_eeprom_v14.h"
28 #include "ar5416/ar5416.h"
29 #include "ar5416/ar5416reg.h"
30 #include "ar5416/ar5416phy.h"
32 /* Owl specific stuff */
33 #define NUM_NOISEFLOOR_READINGS 6 /* 3 chains * (ctl + ext) */
35 static void ar5416StartNFCal(struct ath_hal *ah);
36 static void ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *);
37 static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *);
40 * Determine if calibration is supported by device and channel flags
42 static OS_INLINE HAL_BOOL
43 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
46 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
48 switch (calType & cal->suppCals) {
50 /* Run IQ Mismatch for non-CCK only */
51 return !IEEE80211_IS_CHAN_B(chan);
54 /* Run ADC Gain Cal for non-CCK & non 2GHz-HT20 only */
55 return !IEEE80211_IS_CHAN_B(chan) &&
56 !(IEEE80211_IS_CHAN_2GHZ(chan) && IEEE80211_IS_CHAN_HT20(chan));
62 * Setup HW to collect samples used for current cal
65 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
67 /* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
68 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
69 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
70 currCal->calData->calCountMax);
72 /* Select calibration to run */
73 switch (currCal->calData->calType) {
75 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
76 HALDEBUG(ah, HAL_DEBUG_PERCAL,
77 "%s: start IQ Mismatch calibration\n", __func__);
80 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
81 HALDEBUG(ah, HAL_DEBUG_PERCAL,
82 "%s: start ADC Gain calibration\n", __func__);
85 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
86 HALDEBUG(ah, HAL_DEBUG_PERCAL,
87 "%s: start ADC DC calibration\n", __func__);
90 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
91 HALDEBUG(ah, HAL_DEBUG_PERCAL,
92 "%s: start Init ADC DC calibration\n", __func__);
96 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
100 * Initialize shared data structures and prepare a cal to be run.
103 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
105 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
107 /* Reset data structures shared between different calibrations */
108 OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
111 /* Setup HW for new calibration */
112 ar5416SetupMeasurement(ah, currCal);
114 /* Change SW state to RUNNING for this calibration */
115 currCal->calState = CAL_RUNNING;
120 * Run non-periodic calibrations.
123 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
125 struct ath_hal_5416 *ahp = AH5416(ah);
126 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
127 HAL_CHANNEL_INTERNAL ichan; /* XXX bogus */
128 HAL_CAL_LIST *curCal = ahp->ah_cal_curr;
132 if (curCal == AH_NULL)
136 for (i = 0; i < init_cal_count; i++) {
138 ar5416ResetMeasurement(ah, curCal);
139 /* Poll for offset calibration complete */
140 if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
141 HALDEBUG(ah, HAL_DEBUG_ANY,
142 "%s: Cal %d failed to finish in 100ms.\n",
143 __func__, curCal->calData->calType);
144 /* Re-initialize list pointers for periodic cals */
145 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
149 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
152 HALDEBUG(ah, HAL_DEBUG_ANY,
153 "%s: init cal %d did not complete.\n",
154 __func__, curCal->calData->calType);
155 if (curCal->calNext != AH_NULL)
156 curCal = curCal->calNext;
159 /* Re-initialize list pointers for periodic cals */
160 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
166 * Initialize Calibration infrastructure.
169 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
171 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
172 HAL_CHANNEL_INTERNAL *ichan;
174 ichan = ath_hal_checkchannel(ah, chan);
175 HALASSERT(ichan != AH_NULL);
177 if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
178 /* Enable Rx Filter Cal */
179 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
180 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
181 AR_PHY_AGC_CONTROL_FLTR_CAL);
183 /* Clear the carrier leak cal bit */
184 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
186 /* kick off the cal */
187 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
189 /* Poll for offset calibration complete */
190 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
191 HALDEBUG(ah, HAL_DEBUG_ANY,
192 "%s: offset calibration failed to complete in 1ms; "
193 "noisy environment?\n", __func__);
197 /* Set the cl cal bit and rerun the cal a 2nd time */
198 /* Enable Rx Filter Cal */
199 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
200 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
201 AR_PHY_AGC_CONTROL_FLTR_CAL);
203 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
206 /* Calibrate the AGC */
207 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
209 /* Poll for offset calibration complete */
210 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
211 HALDEBUG(ah, HAL_DEBUG_ANY,
212 "%s: offset calibration did not complete in 1ms; "
213 "noisy environment?\n", __func__);
218 * Do NF calibration after DC offset and other CALs.
219 * Per system engineers, noise floor value can sometimes be 20 dB
220 * higher than normal value if DC offset and noise floor cal are
221 * triggered at the same time.
223 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
225 /* Initialize list pointers */
226 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
229 * Enable IQ, ADC Gain, ADC DC Offset Cals
231 if (AR_SREV_SOWL_10_OR_LATER(ah)) {
232 /* Setup all non-periodic, init time only calibrations */
233 /* XXX: Init DC Offset not working yet */
235 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
236 INIT_CAL(&cal->adcDcCalInitData);
237 INSERT_CAL(cal, &cal->adcDcCalInitData);
239 /* Initialize current pointer to first element in list */
240 cal->cal_curr = cal->cal_list;
242 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
247 /* If Cals are supported, add them to list via INIT/INSERT_CAL */
248 if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
249 INIT_CAL(&cal->adcGainCalData);
250 INSERT_CAL(cal, &cal->adcGainCalData);
251 HALDEBUG(ah, HAL_DEBUG_PERCAL,
252 "%s: enable ADC Gain Calibration.\n", __func__);
254 if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
255 INIT_CAL(&cal->adcDcCalData);
256 INSERT_CAL(cal, &cal->adcDcCalData);
257 HALDEBUG(ah, HAL_DEBUG_PERCAL,
258 "%s: enable ADC DC Calibration.\n", __func__);
260 if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
261 INIT_CAL(&cal->iqCalData);
262 INSERT_CAL(cal, &cal->iqCalData);
263 HALDEBUG(ah, HAL_DEBUG_PERCAL,
264 "%s: enable IQ Calibration.\n", __func__);
266 /* Initialize current pointer to first element in list */
267 cal->cal_curr = cal->cal_list;
269 /* Kick off measurements for the first cal */
270 if (cal->cal_curr != AH_NULL)
271 ar5416ResetMeasurement(ah, cal->cal_curr);
273 /* Mark all calibrations on this channel as being invalid */
280 * Entry point for upper layers to restart current cal.
281 * Reset the calibration valid bit in channel.
284 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
286 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
287 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
288 HAL_CAL_LIST *currCal = cal->cal_curr;
290 if (!AR_SREV_SOWL_10_OR_LATER(ah))
292 if (currCal == AH_NULL)
294 if (ichan == AH_NULL) {
295 HALDEBUG(ah, HAL_DEBUG_ANY,
296 "%s: invalid channel %u/0x%x; no mapping\n",
297 __func__, chan->ic_freq, chan->ic_flags);
301 * Expected that this calibration has run before, post-reset.
302 * Current state should be done
304 if (currCal->calState != CAL_DONE) {
305 HALDEBUG(ah, HAL_DEBUG_ANY,
306 "%s: Calibration state incorrect, %d\n",
307 __func__, currCal->calState);
311 /* Verify Cal is supported on this channel */
312 if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
315 HALDEBUG(ah, HAL_DEBUG_PERCAL,
316 "%s: Resetting Cal %d state for channel %u/0x%x\n",
317 __func__, currCal->calData->calType, chan->ic_freq,
320 /* Disable cal validity in channel */
321 ichan->calValid &= ~currCal->calData->calType;
322 currCal->calState = CAL_WAITING;
328 * Recalibrate the lower PHY chips to account for temperature/environment
332 ar5416DoCalibration(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan,
333 uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
335 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
337 /* Cal is assumed not done until explicitly set below */
338 *isCalDone = AH_FALSE;
340 HALDEBUG(ah, HAL_DEBUG_PERCAL,
341 "%s: %s Calibration, state %d, calValid 0x%x\n",
342 __func__, currCal->calData->calName, currCal->calState,
345 /* Calibration in progress. */
346 if (currCal->calState == CAL_RUNNING) {
347 /* Check to see if it has finished. */
348 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
349 HALDEBUG(ah, HAL_DEBUG_PERCAL,
350 "%s: sample %d of %d finished\n",
351 __func__, cal->calSamples,
352 currCal->calData->calNumSamples);
354 * Collect measurements for active chains.
356 currCal->calData->calCollect(ah);
357 if (++cal->calSamples >= currCal->calData->calNumSamples) {
358 int i, numChains = 0;
359 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
360 if (rxchainmask & (1 << i))
364 * Process accumulated data
366 currCal->calData->calPostProc(ah, numChains);
368 /* Calibration has finished. */
369 ichan->calValid |= currCal->calData->calType;
370 currCal->calState = CAL_DONE;
371 *isCalDone = AH_TRUE;
374 * Set-up to collect of another sub-sample.
376 ar5416SetupMeasurement(ah, currCal);
379 } else if (!(ichan->calValid & currCal->calData->calType)) {
380 /* If current cal is marked invalid in channel, kick it off */
381 ar5416ResetMeasurement(ah, currCal);
386 * Internal interface to schedule periodic calibration work.
389 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
390 u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
392 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
393 HAL_CAL_LIST *currCal = cal->cal_curr;
394 HAL_CHANNEL_INTERNAL *ichan;
396 OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
398 *isCalDone = AH_TRUE;
400 /* Invalid channel check */
401 ichan = ath_hal_checkchannel(ah, chan);
402 if (ichan == AH_NULL) {
403 HALDEBUG(ah, HAL_DEBUG_ANY,
404 "%s: invalid channel %u/0x%x; no mapping\n",
405 __func__, chan->ic_freq, chan->ic_flags);
410 * For given calibration:
411 * 1. Call generic cal routine
412 * 2. When this cal is done (isCalDone) if we have more cals waiting
413 * (eg after reset), mask this to upper layers by not propagating
414 * isCalDone if it is set to TRUE.
415 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
418 if (currCal != AH_NULL &&
419 (currCal->calState == CAL_RUNNING ||
420 currCal->calState == CAL_WAITING)) {
421 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
422 if (*isCalDone == AH_TRUE) {
423 cal->cal_curr = currCal = currCal->calNext;
424 if (currCal->calState == CAL_WAITING) {
425 *isCalDone = AH_FALSE;
426 ar5416ResetMeasurement(ah, currCal);
431 /* Do NF cal only at longer intervals */
434 * Get the value from the previous NF cal
435 * and update the history buffer.
437 ar5416GetNf(ah, chan);
440 * Load the NF from history buffer of the current channel.
441 * NF is slow time-variant, so it is OK to use a
444 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
446 /* start NF calibration, without updating BB NF register*/
447 ar5416StartNFCal(ah);
453 * Recalibrate the lower PHY chips to account for temperature/environment
457 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
460 struct ath_hal_5416 *ahp = AH5416(ah);
461 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
462 HAL_CAL_LIST *curCal = cal->cal_curr;
464 if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
465 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
470 *isIQdone = AH_FALSE;
471 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
472 AH_TRUE, &isCalDone);
477 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
478 const struct ieee80211_channel *chan, int16_t *nft)
480 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
481 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
484 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
485 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
488 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
489 __func__, chan->ic_flags);
494 ar5416StartNFCal(struct ath_hal *ah)
496 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
497 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
498 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
502 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
504 static const uint32_t ar5416_cca_regs[] = {
512 struct ar5212NfCalHist *h;
518 * Force NF calibration for all chains.
520 if (AR_SREV_KITE(ah)) {
521 /* Kite has only one chain */
523 } else if (AR_SREV_MERLIN(ah)) {
524 /* Merlin has only two chains */
531 * Write filtered NF values into maxCCApwr register parameter
532 * so we can load below.
534 h = AH5416(ah)->ah_cal.nfCalHist;
535 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
536 if (chainmask & (1 << i)) {
537 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
539 val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
540 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
543 /* Load software filtered NF value into baseband internal minCCApwr variable. */
544 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
545 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
546 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
548 /* Wait for load to complete, should be fast, a few 10s of us. */
549 for (j = 0; j < 1000; j++) {
550 if ((OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) == 0)
556 * Restore maxCCAPower register parameter again so that we're not capped
557 * by the median we just loaded. This will be initial (and max) value
558 * of next noise floor calibration the baseband does.
560 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
561 if (chainmask & (1 << i)) {
562 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
564 val |= (((uint32_t)(-50) << 1) & 0x1ff);
565 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
570 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
574 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
576 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
577 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
578 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
579 h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
584 * Update the noise floor buffer as a ring buffer
587 ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
591 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
592 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
594 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
596 if (h[i].invalidNFcount > 0) {
597 if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
598 nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
599 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
601 h[i].invalidNFcount--;
602 h[i].privNF = nfarray[i];
605 h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
611 * Read the NF and check it against the noise floor threshhold
614 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
616 int16_t nf, nfThresh;
618 if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
619 HALDEBUG(ah, HAL_DEBUG_ANY,
620 "%s: NF didn't complete in calibration window\n", __func__);
623 /* Finished NF cal, check against threshold */
624 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
625 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
627 /* TODO - enhance for multiple chains and ext ch */
628 ath_hal_getNoiseFloor(ah, nfarray);
630 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
632 HALDEBUG(ah, HAL_DEBUG_ANY,
633 "%s: noise floor failed detected; "
634 "detected %d, threshold %d\n", __func__,
637 * NB: Don't discriminate 2.4 vs 5Ghz, if this
638 * happens it indicates a problem regardless
641 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
647 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
648 ichan->rawNoiseFloor = nf;