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.
22 #include "ah_internal.h"
25 #include "ah_eeprom_v14.h"
27 #include "ar5416/ar5416.h"
28 #include "ar5416/ar5416reg.h"
29 #include "ar5416/ar5416phy.h"
31 /* Owl specific stuff */
32 #define NUM_NOISEFLOOR_READINGS 6 /* 3 chains * (ctl + ext) */
34 static void ar5416StartNFCal(struct ath_hal *ah);
35 static void ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *);
36 static int16_t ar5416GetNf(struct ath_hal *, struct ieee80211_channel *);
39 * Determine if calibration is supported by device and channel flags
41 static OS_INLINE HAL_BOOL
42 ar5416IsCalSupp(struct ath_hal *ah, const struct ieee80211_channel *chan,
45 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
47 switch (calType & cal->suppCals) {
49 /* Run IQ Mismatch for non-CCK only */
50 return !IEEE80211_IS_CHAN_B(chan);
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));
61 * Setup HW to collect samples used for current cal
64 ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
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);
71 /* Select calibration to run */
72 switch (currCal->calData->calType) {
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__);
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__);
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__);
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__);
95 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
99 * Initialize shared data structures and prepare a cal to be run.
102 ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
104 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
106 /* Reset data structures shared between different calibrations */
107 OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
110 /* Setup HW for new calibration */
111 ar5416SetupMeasurement(ah, currCal);
113 /* Change SW state to RUNNING for this calibration */
114 currCal->calState = CAL_RUNNING;
119 * Run non-periodic calibrations.
122 ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
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;
131 if (curCal == AH_NULL)
135 for (i = 0; i < init_cal_count; i++) {
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;
148 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
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;
158 /* Re-initialize list pointers for periodic cals */
159 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
165 * Initialize Calibration infrastructure.
168 ar5416InitCal(struct ath_hal *ah, const struct ieee80211_channel *chan)
170 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
171 HAL_CHANNEL_INTERNAL *ichan;
173 ichan = ath_hal_checkchannel(ah, chan);
174 HALASSERT(ichan != AH_NULL);
176 if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
177 /* Enable Rx Filter Cal */
178 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
179 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
180 AR_PHY_AGC_CONTROL_FLTR_CAL);
182 /* Clear the carrier leak cal bit */
183 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
185 /* kick off the cal */
186 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
188 /* Poll for offset calibration complete */
189 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
190 HALDEBUG(ah, HAL_DEBUG_ANY,
191 "%s: offset calibration failed to complete in 1ms; "
192 "noisy environment?\n", __func__);
196 /* Set the cl cal bit and rerun the cal a 2nd time */
197 /* Enable Rx Filter Cal */
198 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
199 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
200 AR_PHY_AGC_CONTROL_FLTR_CAL);
202 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
205 /* Calibrate the AGC */
206 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
208 /* Poll for offset calibration complete */
209 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
210 HALDEBUG(ah, HAL_DEBUG_ANY,
211 "%s: offset calibration did not complete in 1ms; "
212 "noisy environment?\n", __func__);
217 * Do NF calibration after DC offset and other CALs.
218 * Per system engineers, noise floor value can sometimes be 20 dB
219 * higher than normal value if DC offset and noise floor cal are
220 * triggered at the same time.
222 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
224 /* Initialize list pointers */
225 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
228 * Enable IQ, ADC Gain, ADC DC Offset Cals
230 if (AR_SREV_SOWL_10_OR_LATER(ah)) {
231 /* Setup all non-periodic, init time only calibrations */
232 /* XXX: Init DC Offset not working yet */
234 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
235 INIT_CAL(&cal->adcDcCalInitData);
236 INSERT_CAL(cal, &cal->adcDcCalInitData);
238 /* Initialize current pointer to first element in list */
239 cal->cal_curr = cal->cal_list;
241 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
246 /* If Cals are supported, add them to list via INIT/INSERT_CAL */
247 if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
248 INIT_CAL(&cal->adcGainCalData);
249 INSERT_CAL(cal, &cal->adcGainCalData);
250 HALDEBUG(ah, HAL_DEBUG_PERCAL,
251 "%s: enable ADC Gain Calibration.\n", __func__);
253 if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
254 INIT_CAL(&cal->adcDcCalData);
255 INSERT_CAL(cal, &cal->adcDcCalData);
256 HALDEBUG(ah, HAL_DEBUG_PERCAL,
257 "%s: enable ADC DC Calibration.\n", __func__);
259 if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
260 INIT_CAL(&cal->iqCalData);
261 INSERT_CAL(cal, &cal->iqCalData);
262 HALDEBUG(ah, HAL_DEBUG_PERCAL,
263 "%s: enable IQ Calibration.\n", __func__);
265 /* Initialize current pointer to first element in list */
266 cal->cal_curr = cal->cal_list;
268 /* Kick off measurements for the first cal */
269 if (cal->cal_curr != AH_NULL)
270 ar5416ResetMeasurement(ah, cal->cal_curr);
272 /* Mark all calibrations on this channel as being invalid */
279 * Entry point for upper layers to restart current cal.
280 * Reset the calibration valid bit in channel.
283 ar5416ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
285 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
286 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
287 HAL_CAL_LIST *currCal = cal->cal_curr;
289 if (!AR_SREV_SOWL_10_OR_LATER(ah))
291 if (currCal == AH_NULL)
293 if (ichan == AH_NULL) {
294 HALDEBUG(ah, HAL_DEBUG_ANY,
295 "%s: invalid channel %u/0x%x; no mapping\n",
296 __func__, chan->ic_freq, chan->ic_flags);
300 * Expected that this calibration has run before, post-reset.
301 * Current state should be done
303 if (currCal->calState != CAL_DONE) {
304 HALDEBUG(ah, HAL_DEBUG_ANY,
305 "%s: Calibration state incorrect, %d\n",
306 __func__, currCal->calState);
310 /* Verify Cal is supported on this channel */
311 if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
314 HALDEBUG(ah, HAL_DEBUG_PERCAL,
315 "%s: Resetting Cal %d state for channel %u/0x%x\n",
316 __func__, currCal->calData->calType, chan->ic_freq,
319 /* Disable cal validity in channel */
320 ichan->calValid &= ~currCal->calData->calType;
321 currCal->calState = CAL_WAITING;
327 * Recalibrate the lower PHY chips to account for temperature/environment
331 ar5416DoCalibration(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan,
332 uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
334 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
336 /* Cal is assumed not done until explicitly set below */
337 *isCalDone = AH_FALSE;
339 HALDEBUG(ah, HAL_DEBUG_PERCAL,
340 "%s: %s Calibration, state %d, calValid 0x%x\n",
341 __func__, currCal->calData->calName, currCal->calState,
344 /* Calibration in progress. */
345 if (currCal->calState == CAL_RUNNING) {
346 /* Check to see if it has finished. */
347 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
348 HALDEBUG(ah, HAL_DEBUG_PERCAL,
349 "%s: sample %d of %d finished\n",
350 __func__, cal->calSamples,
351 currCal->calData->calNumSamples);
353 * Collect measurements for active chains.
355 currCal->calData->calCollect(ah);
356 if (++cal->calSamples >= currCal->calData->calNumSamples) {
357 int i, numChains = 0;
358 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
359 if (rxchainmask & (1 << i))
363 * Process accumulated data
365 currCal->calData->calPostProc(ah, numChains);
367 /* Calibration has finished. */
368 ichan->calValid |= currCal->calData->calType;
369 currCal->calState = CAL_DONE;
370 *isCalDone = AH_TRUE;
373 * Set-up to collect of another sub-sample.
375 ar5416SetupMeasurement(ah, currCal);
378 } else if (!(ichan->calValid & currCal->calData->calType)) {
379 /* If current cal is marked invalid in channel, kick it off */
380 ar5416ResetMeasurement(ah, currCal);
385 * Internal interface to schedule periodic calibration work.
388 ar5416PerCalibrationN(struct ath_hal *ah, struct ieee80211_channel *chan,
389 u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
391 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
392 HAL_CAL_LIST *currCal = cal->cal_curr;
393 HAL_CHANNEL_INTERNAL *ichan;
395 OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
397 *isCalDone = AH_TRUE;
399 /* Invalid channel check */
400 ichan = ath_hal_checkchannel(ah, chan);
401 if (ichan == AH_NULL) {
402 HALDEBUG(ah, HAL_DEBUG_ANY,
403 "%s: invalid channel %u/0x%x; no mapping\n",
404 __func__, chan->ic_freq, chan->ic_flags);
409 * For given calibration:
410 * 1. Call generic cal routine
411 * 2. When this cal is done (isCalDone) if we have more cals waiting
412 * (eg after reset), mask this to upper layers by not propagating
413 * isCalDone if it is set to TRUE.
414 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
417 if (currCal != AH_NULL &&
418 (currCal->calState == CAL_RUNNING ||
419 currCal->calState == CAL_WAITING)) {
420 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
421 if (*isCalDone == AH_TRUE) {
422 cal->cal_curr = currCal = currCal->calNext;
423 if (currCal->calState == CAL_WAITING) {
424 *isCalDone = AH_FALSE;
425 ar5416ResetMeasurement(ah, currCal);
430 /* Do NF cal only at longer intervals */
433 * Get the value from the previous NF cal
434 * and update the history buffer.
436 ar5416GetNf(ah, chan);
439 * Load the NF from history buffer of the current channel.
440 * NF is slow time-variant, so it is OK to use a
443 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
445 /* start NF calibration, without updating BB NF register*/
446 ar5416StartNFCal(ah);
452 * Recalibrate the lower PHY chips to account for temperature/environment
456 ar5416PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
459 struct ath_hal_5416 *ahp = AH5416(ah);
460 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
461 HAL_CAL_LIST *curCal = cal->cal_curr;
463 if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
464 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
469 *isIQdone = AH_FALSE;
470 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
471 AH_TRUE, &isCalDone);
476 ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
477 const struct ieee80211_channel *chan, int16_t *nft)
479 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
480 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
483 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
484 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
487 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
488 __func__, chan->ic_flags);
493 ar5416StartNFCal(struct ath_hal *ah)
495 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
496 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
497 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
501 ar5416LoadNF(struct ath_hal *ah, const struct ieee80211_channel *chan)
503 static const uint32_t ar5416_cca_regs[] = {
511 struct ar5212NfCalHist *h;
517 * Force NF calibration for all chains.
519 if (AR_SREV_KITE(ah)) {
520 /* Kite has only one chain */
522 } else if (AR_SREV_MERLIN(ah)) {
523 /* Merlin has only two chains */
530 * Write filtered NF values into maxCCApwr register parameter
531 * so we can load below.
533 h = AH5416(ah)->ah_cal.nfCalHist;
534 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
535 if (chainmask & (1 << i)) {
536 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
538 val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
539 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
542 /* Load software filtered NF value into baseband internal minCCApwr variable. */
543 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
544 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
545 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
547 /* Wait for load to complete, should be fast, a few 10s of us. */
548 for (j = 0; j < 1000; j++) {
549 if ((OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) == 0)
555 * Restore maxCCAPower register parameter again so that we're not capped
556 * by the median we just loaded. This will be initial (and max) value
557 * of next noise floor calibration the baseband does.
559 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
560 if (chainmask & (1 << i)) {
561 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
563 val |= (((uint32_t)(-50) << 1) & 0x1ff);
564 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
569 ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
573 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
575 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
576 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
577 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
578 h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
583 * Update the noise floor buffer as a ring buffer
586 ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
590 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
591 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
593 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
595 if (h[i].invalidNFcount > 0) {
596 if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
597 nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
598 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
600 h[i].invalidNFcount--;
601 h[i].privNF = nfarray[i];
604 h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
610 * Read the NF and check it against the noise floor threshhold
613 ar5416GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
615 int16_t nf, nfThresh;
617 if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
618 HALDEBUG(ah, HAL_DEBUG_ANY,
619 "%s: NF didn't complete in calibration window\n", __func__);
622 /* Finished NF cal, check against threshold */
623 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
624 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
626 /* TODO - enhance for multiple chains and ext ch */
627 ath_hal_getNoiseFloor(ah, nfarray);
629 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
631 HALDEBUG(ah, HAL_DEBUG_ANY,
632 "%s: noise floor failed detected; "
633 "detected %d, threshold %d\n", __func__,
636 * NB: Don't discriminate 2.4 vs 5Ghz, if this
637 * happens it indicates a problem regardless
640 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
646 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
647 ichan->rawNoiseFloor = nf;