2 * Copyright (c) 2008-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <linux/export.h>
21 /* Common calibration code */
23 #define ATH9K_NF_TOO_HIGH -60
25 static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
28 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
31 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
32 sort[i] = nfCalBuffer[i];
34 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
35 for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
36 if (sort[j] > sort[j - 1]) {
38 sort[j] = sort[j - 1];
43 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
48 static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
49 struct ath9k_channel *chan)
51 struct ath_nf_limits *limit;
53 if (!chan || IS_CHAN_2GHZ(chan))
61 static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
62 struct ath9k_channel *chan)
64 return ath9k_hw_get_nf_limits(ah, chan)->nominal;
67 s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
69 s8 noise = ATH_DEFAULT_NOISE_FLOOR;
71 if (chan && chan->noisefloor) {
72 s8 delta = chan->noisefloor -
73 ath9k_hw_get_default_nf(ah, chan);
79 EXPORT_SYMBOL(ath9k_hw_getchan_noise);
81 static void ath9k_hw_update_nfcal_hist_buffer(struct ath_hw *ah,
82 struct ath9k_hw_cal_data *cal,
85 struct ath_common *common = ath9k_hw_common(ah);
86 struct ath_nf_limits *limit;
87 struct ath9k_nfcal_hist *h;
88 bool high_nf_mid = false;
89 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
93 limit = ath9k_hw_get_nf_limits(ah, ah->curchan);
95 for (i = 0; i < NUM_NF_READINGS; i++) {
96 if (!(chainmask & (1 << i)) ||
97 ((i >= AR5416_MAX_CHAINS) && !IS_CHAN_HT40(ah->curchan)))
100 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
102 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
105 if (h[i].invalidNFcount > 0) {
106 h[i].invalidNFcount--;
107 h[i].privNF = nfarray[i];
110 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
116 if (h[i].privNF > limit->max) {
119 ath_dbg(common, ATH_DBG_CALIBRATE,
120 "NFmid[%d] (%d) > MAX (%d), %s\n",
121 i, h[i].privNF, limit->max,
122 (cal->nfcal_interference ?
123 "not corrected (due to interference)" :
124 "correcting to MAX"));
127 * Normally we limit the average noise floor by the
128 * hardware specific maximum here. However if we have
129 * encountered stuck beacons because of interference,
130 * we bypass this limit here in order to better deal
131 * with our environment.
133 if (!cal->nfcal_interference)
134 h[i].privNF = limit->max;
139 * If the noise floor seems normal for all chains, assume that
140 * there is no significant interference in the environment anymore.
141 * Re-enable the enforcement of the NF maximum again.
144 cal->nfcal_interference = false;
147 static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
148 enum ieee80211_band band,
152 case IEEE80211_BAND_5GHZ:
153 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
155 case IEEE80211_BAND_2GHZ:
156 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
166 void ath9k_hw_reset_calibration(struct ath_hw *ah,
167 struct ath9k_cal_list *currCal)
171 ath9k_hw_setup_calibration(ah, currCal);
173 currCal->calState = CAL_RUNNING;
175 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
176 ah->meas0.sign[i] = 0;
177 ah->meas1.sign[i] = 0;
178 ah->meas2.sign[i] = 0;
179 ah->meas3.sign[i] = 0;
185 /* This is done for the currently configured channel */
186 bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
188 struct ath_common *common = ath9k_hw_common(ah);
189 struct ieee80211_conf *conf = &common->hw->conf;
190 struct ath9k_cal_list *currCal = ah->cal_list_curr;
195 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
201 if (currCal->calState != CAL_DONE) {
202 ath_dbg(common, ATH_DBG_CALIBRATE,
203 "Calibration state incorrect, %d\n",
208 if (!(ah->supp_cals & currCal->calData->calType))
211 ath_dbg(common, ATH_DBG_CALIBRATE,
212 "Resetting Cal %d state for channel %u\n",
213 currCal->calData->calType, conf->channel->center_freq);
215 ah->caldata->CalValid &= ~currCal->calData->calType;
216 currCal->calState = CAL_WAITING;
220 EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
222 void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
225 ah->caldata->nfcal_pending = true;
227 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
228 AR_PHY_AGC_CONTROL_ENABLE_NF);
231 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
232 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
234 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
235 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
237 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
240 void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
242 struct ath9k_nfcal_hist *h = NULL;
245 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
246 struct ath_common *common = ath9k_hw_common(ah);
247 struct ieee80211_conf *conf = &common->hw->conf;
248 s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
251 h = ah->caldata->nfCalHist;
253 for (i = 0; i < NUM_NF_READINGS; i++) {
254 if (chainmask & (1 << i)) {
257 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
265 val = REG_READ(ah, ah->nf_regs[i]);
267 val |= (((u32) nfval << 1) & 0x1ff);
268 REG_WRITE(ah, ah->nf_regs[i], val);
273 * Load software filtered NF value into baseband internal minCCApwr
276 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
277 AR_PHY_AGC_CONTROL_ENABLE_NF);
278 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
279 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
280 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
283 * Wait for load to complete, should be fast, a few 10s of us.
284 * The max delay was changed from an original 250us to 10000us
285 * since 250us often results in NF load timeout and causes deaf
286 * condition during stress testing 12/12/2009
288 for (j = 0; j < 10000; j++) {
289 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
290 AR_PHY_AGC_CONTROL_NF) == 0)
296 * We timed out waiting for the noisefloor to load, probably due to an
297 * in-progress rx. Simply return here and allow the load plenty of time
298 * to complete before the next calibration interval. We need to avoid
299 * trying to load -50 (which happens below) while the previous load is
300 * still in progress as this can cause rx deafness. Instead by returning
301 * here, the baseband nf cal will just be capped by our present
302 * noisefloor until the next calibration timer.
305 ath_dbg(common, ATH_DBG_ANY,
306 "Timeout while waiting for nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
307 REG_READ(ah, AR_PHY_AGC_CONTROL));
312 * Restore maxCCAPower register parameter again so that we're not capped
313 * by the median we just loaded. This will be initial (and max) value
314 * of next noise floor calibration the baseband does.
316 ENABLE_REGWRITE_BUFFER(ah);
317 for (i = 0; i < NUM_NF_READINGS; i++) {
318 if (chainmask & (1 << i)) {
319 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
322 val = REG_READ(ah, ah->nf_regs[i]);
324 val |= (((u32) (-50) << 1) & 0x1ff);
325 REG_WRITE(ah, ah->nf_regs[i], val);
328 REGWRITE_BUFFER_FLUSH(ah);
332 static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
334 struct ath_common *common = ath9k_hw_common(ah);
335 struct ath_nf_limits *limit;
338 if (IS_CHAN_2GHZ(ah->curchan))
343 for (i = 0; i < NUM_NF_READINGS; i++) {
347 ath_dbg(common, ATH_DBG_CALIBRATE,
348 "NF calibrated [%s] [chain %d] is %d\n",
349 (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
351 if (nf[i] > ATH9K_NF_TOO_HIGH) {
352 ath_dbg(common, ATH_DBG_CALIBRATE,
353 "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
354 i, nf[i], ATH9K_NF_TOO_HIGH);
356 } else if (nf[i] < limit->min) {
357 ath_dbg(common, ATH_DBG_CALIBRATE,
358 "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
359 i, nf[i], limit->min);
360 nf[i] = limit->nominal;
365 bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
367 struct ath_common *common = ath9k_hw_common(ah);
368 int16_t nf, nfThresh;
369 int16_t nfarray[NUM_NF_READINGS] = { 0 };
370 struct ath9k_nfcal_hist *h;
371 struct ieee80211_channel *c = chan->chan;
372 struct ath9k_hw_cal_data *caldata = ah->caldata;
374 chan->channelFlags &= (~CHANNEL_CW_INT);
375 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
376 ath_dbg(common, ATH_DBG_CALIBRATE,
377 "NF did not complete in calibration window\n");
381 ath9k_hw_do_getnf(ah, nfarray);
382 ath9k_hw_nf_sanitize(ah, nfarray);
384 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
386 ath_dbg(common, ATH_DBG_CALIBRATE,
387 "noise floor failed detected; detected %d, threshold %d\n",
389 chan->channelFlags |= CHANNEL_CW_INT;
393 chan->noisefloor = nf;
394 ah->noise = ath9k_hw_getchan_noise(ah, chan);
398 h = caldata->nfCalHist;
399 caldata->nfcal_pending = false;
400 ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
401 chan->noisefloor = h[0].privNF;
402 ah->noise = ath9k_hw_getchan_noise(ah, chan);
406 void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
407 struct ath9k_channel *chan)
409 struct ath9k_nfcal_hist *h;
413 ah->caldata->channel = chan->channel;
414 ah->caldata->channelFlags = chan->channelFlags & ~CHANNEL_CW_INT;
415 h = ah->caldata->nfCalHist;
416 default_nf = ath9k_hw_get_default_nf(ah, chan);
417 for (i = 0; i < NUM_NF_READINGS; i++) {
419 h[i].privNF = default_nf;
420 h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
421 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
422 h[i].nfCalBuffer[j] = default_nf;
428 void ath9k_hw_bstuck_nfcal(struct ath_hw *ah)
430 struct ath9k_hw_cal_data *caldata = ah->caldata;
432 if (unlikely(!caldata))
436 * If beacons are stuck, the most likely cause is interference.
437 * Triggering a noise floor calibration at this point helps the
438 * hardware adapt to a noisy environment much faster.
439 * To ensure that we recover from stuck beacons quickly, let
440 * the baseband update the internal NF value itself, similar to
441 * what is being done after a full reset.
443 if (!caldata->nfcal_pending)
444 ath9k_hw_start_nfcal(ah, true);
445 else if (!(REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
446 ath9k_hw_getnf(ah, ah->curchan);
448 caldata->nfcal_interference = true;
450 EXPORT_SYMBOL(ath9k_hw_bstuck_nfcal);