2 * Copyright (c) 2008-2009 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.
20 /* Common calibration code */
22 #define ATH9K_NF_TOO_HIGH -60
24 static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
27 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
30 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
31 sort[i] = nfCalBuffer[i];
33 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
34 for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
35 if (sort[j] > sort[j - 1]) {
37 sort[j] = sort[j - 1];
42 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
47 static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
48 struct ath9k_channel *chan)
50 struct ath_nf_limits *limit;
52 if (!chan || IS_CHAN_2GHZ(chan))
60 static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
61 struct ath9k_channel *chan)
63 return ath9k_hw_get_nf_limits(ah, chan)->nominal;
67 static void ath9k_hw_update_nfcal_hist_buffer(struct ath_hw *ah,
68 struct ath9k_hw_cal_data *cal,
71 struct ath_common *common = ath9k_hw_common(ah);
72 struct ath_nf_limits *limit;
73 struct ath9k_nfcal_hist *h;
74 bool high_nf_mid = false;
78 limit = ath9k_hw_get_nf_limits(ah, ah->curchan);
80 for (i = 0; i < NUM_NF_READINGS; i++) {
81 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
83 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
86 if (h[i].invalidNFcount > 0) {
87 h[i].invalidNFcount--;
88 h[i].privNF = nfarray[i];
91 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
97 if (h[i].privNF > limit->max) {
100 ath_print(common, ATH_DBG_CALIBRATE,
101 "NFmid[%d] (%d) > MAX (%d), %s\n",
102 i, h[i].privNF, limit->max,
103 (cal->nfcal_interference ?
104 "not corrected (due to interference)" :
105 "correcting to MAX"));
108 * Normally we limit the average noise floor by the
109 * hardware specific maximum here. However if we have
110 * encountered stuck beacons because of interference,
111 * we bypass this limit here in order to better deal
112 * with our environment.
114 if (!cal->nfcal_interference)
115 h[i].privNF = limit->max;
120 * If the noise floor seems normal for all chains, assume that
121 * there is no significant interference in the environment anymore.
122 * Re-enable the enforcement of the NF maximum again.
125 cal->nfcal_interference = false;
128 static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
129 enum ieee80211_band band,
133 case IEEE80211_BAND_5GHZ:
134 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
136 case IEEE80211_BAND_2GHZ:
137 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
147 void ath9k_hw_reset_calibration(struct ath_hw *ah,
148 struct ath9k_cal_list *currCal)
152 ath9k_hw_setup_calibration(ah, currCal);
154 currCal->calState = CAL_RUNNING;
156 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
157 ah->meas0.sign[i] = 0;
158 ah->meas1.sign[i] = 0;
159 ah->meas2.sign[i] = 0;
160 ah->meas3.sign[i] = 0;
166 /* This is done for the currently configured channel */
167 bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
169 struct ath_common *common = ath9k_hw_common(ah);
170 struct ieee80211_conf *conf = &common->hw->conf;
171 struct ath9k_cal_list *currCal = ah->cal_list_curr;
176 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
182 if (currCal->calState != CAL_DONE) {
183 ath_print(common, ATH_DBG_CALIBRATE,
184 "Calibration state incorrect, %d\n",
189 if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
192 ath_print(common, ATH_DBG_CALIBRATE,
193 "Resetting Cal %d state for channel %u\n",
194 currCal->calData->calType, conf->channel->center_freq);
196 ah->caldata->CalValid &= ~currCal->calData->calType;
197 currCal->calState = CAL_WAITING;
201 EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
203 void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
206 ah->caldata->nfcal_pending = true;
208 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
209 AR_PHY_AGC_CONTROL_ENABLE_NF);
212 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
213 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
215 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
216 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
218 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
221 void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
223 struct ath9k_nfcal_hist *h = NULL;
226 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
227 struct ath_common *common = ath9k_hw_common(ah);
228 s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
231 h = ah->caldata->nfCalHist;
233 for (i = 0; i < NUM_NF_READINGS; i++) {
234 if (chainmask & (1 << i)) {
242 val = REG_READ(ah, ah->nf_regs[i]);
244 val |= (((u32) nfval << 1) & 0x1ff);
245 REG_WRITE(ah, ah->nf_regs[i], val);
250 * Load software filtered NF value into baseband internal minCCApwr
253 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
254 AR_PHY_AGC_CONTROL_ENABLE_NF);
255 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
256 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
257 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
260 * Wait for load to complete, should be fast, a few 10s of us.
261 * The max delay was changed from an original 250us to 10000us
262 * since 250us often results in NF load timeout and causes deaf
263 * condition during stress testing 12/12/2009
265 for (j = 0; j < 1000; j++) {
266 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
267 AR_PHY_AGC_CONTROL_NF) == 0)
273 * We timed out waiting for the noisefloor to load, probably due to an
274 * in-progress rx. Simply return here and allow the load plenty of time
275 * to complete before the next calibration interval. We need to avoid
276 * trying to load -50 (which happens below) while the previous load is
277 * still in progress as this can cause rx deafness. Instead by returning
278 * here, the baseband nf cal will just be capped by our present
279 * noisefloor until the next calibration timer.
282 ath_print(common, ATH_DBG_ANY, "Timeout while waiting for nf "
283 "to load: AR_PHY_AGC_CONTROL=0x%x\n",
284 REG_READ(ah, AR_PHY_AGC_CONTROL));
289 * Restore maxCCAPower register parameter again so that we're not capped
290 * by the median we just loaded. This will be initial (and max) value
291 * of next noise floor calibration the baseband does.
293 ENABLE_REGWRITE_BUFFER(ah);
294 for (i = 0; i < NUM_NF_READINGS; i++) {
295 if (chainmask & (1 << i)) {
296 val = REG_READ(ah, ah->nf_regs[i]);
298 val |= (((u32) (-50) << 1) & 0x1ff);
299 REG_WRITE(ah, ah->nf_regs[i], val);
302 REGWRITE_BUFFER_FLUSH(ah);
303 DISABLE_REGWRITE_BUFFER(ah);
307 static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
309 struct ath_common *common = ath9k_hw_common(ah);
310 struct ath_nf_limits *limit;
313 if (IS_CHAN_2GHZ(ah->curchan))
318 for (i = 0; i < NUM_NF_READINGS; i++) {
322 ath_print(common, ATH_DBG_CALIBRATE,
323 "NF calibrated [%s] [chain %d] is %d\n",
324 (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
326 if (nf[i] > ATH9K_NF_TOO_HIGH) {
327 ath_print(common, ATH_DBG_CALIBRATE,
328 "NF[%d] (%d) > MAX (%d), correcting to MAX",
329 i, nf[i], ATH9K_NF_TOO_HIGH);
331 } else if (nf[i] < limit->min) {
332 ath_print(common, ATH_DBG_CALIBRATE,
333 "NF[%d] (%d) < MIN (%d), correcting to NOM",
334 i, nf[i], limit->min);
335 nf[i] = limit->nominal;
340 bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
342 struct ath_common *common = ath9k_hw_common(ah);
343 int16_t nf, nfThresh;
344 int16_t nfarray[NUM_NF_READINGS] = { 0 };
345 struct ath9k_nfcal_hist *h;
346 struct ieee80211_channel *c = chan->chan;
347 struct ath9k_hw_cal_data *caldata = ah->caldata;
352 chan->channelFlags &= (~CHANNEL_CW_INT);
353 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
354 ath_print(common, ATH_DBG_CALIBRATE,
355 "NF did not complete in calibration window\n");
357 caldata->rawNoiseFloor = nf;
360 ath9k_hw_do_getnf(ah, nfarray);
361 ath9k_hw_nf_sanitize(ah, nfarray);
363 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
365 ath_print(common, ATH_DBG_CALIBRATE,
366 "noise floor failed detected; "
367 "detected %d, threshold %d\n",
369 chan->channelFlags |= CHANNEL_CW_INT;
373 h = caldata->nfCalHist;
374 caldata->nfcal_pending = false;
375 ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
376 caldata->rawNoiseFloor = h[0].privNF;
380 void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
381 struct ath9k_channel *chan)
383 struct ath9k_nfcal_hist *h;
390 h = ah->caldata->nfCalHist;
391 default_nf = ath9k_hw_get_default_nf(ah, chan);
392 for (i = 0; i < NUM_NF_READINGS; i++) {
394 h[i].privNF = default_nf;
395 h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
396 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
397 h[i].nfCalBuffer[j] = default_nf;
402 s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
404 if (!ah->caldata || !ah->caldata->rawNoiseFloor)
405 return ath9k_hw_get_default_nf(ah, chan);
407 return ah->caldata->rawNoiseFloor;
409 EXPORT_SYMBOL(ath9k_hw_getchan_noise);
411 void ath9k_hw_bstuck_nfcal(struct ath_hw *ah)
413 struct ath9k_hw_cal_data *caldata = ah->caldata;
415 if (unlikely(!caldata))
419 * If beacons are stuck, the most likely cause is interference.
420 * Triggering a noise floor calibration at this point helps the
421 * hardware adapt to a noisy environment much faster.
422 * To ensure that we recover from stuck beacons quickly, let
423 * the baseband update the internal NF value itself, similar to
424 * what is being done after a full reset.
426 if (!caldata->nfcal_pending)
427 ath9k_hw_start_nfcal(ah, true);
428 else if (!(REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
429 ath9k_hw_getnf(ah, ah->curchan);
431 caldata->nfcal_interference = true;
433 EXPORT_SYMBOL(ath9k_hw_bstuck_nfcal);