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 /* We can tune this as we go by monitoring really low values */
23 #define ATH9K_NF_TOO_LOW -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 void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
53 for (i = 0; i < NUM_NF_READINGS; i++) {
54 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
56 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
59 if (h[i].invalidNFcount > 0) {
60 h[i].invalidNFcount--;
61 h[i].privNF = nfarray[i];
64 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
69 static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
70 enum ieee80211_band band,
74 case IEEE80211_BAND_5GHZ:
75 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
77 case IEEE80211_BAND_2GHZ:
78 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
88 void ath9k_hw_reset_calibration(struct ath_hw *ah,
89 struct ath9k_cal_list *currCal)
93 ath9k_hw_setup_calibration(ah, currCal);
95 currCal->calState = CAL_RUNNING;
97 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
98 ah->meas0.sign[i] = 0;
99 ah->meas1.sign[i] = 0;
100 ah->meas2.sign[i] = 0;
101 ah->meas3.sign[i] = 0;
107 static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
108 struct ath9k_channel *chan)
110 struct ath_nf_limits *limit;
112 if (!chan || IS_CHAN_2GHZ(chan))
117 return limit->nominal;
120 /* This is done for the currently configured channel */
121 bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
123 struct ath_common *common = ath9k_hw_common(ah);
124 struct ieee80211_conf *conf = &common->hw->conf;
125 struct ath9k_cal_list *currCal = ah->cal_list_curr;
130 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
136 if (currCal->calState != CAL_DONE) {
137 ath_print(common, ATH_DBG_CALIBRATE,
138 "Calibration state incorrect, %d\n",
143 if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
146 ath_print(common, ATH_DBG_CALIBRATE,
147 "Resetting Cal %d state for channel %u\n",
148 currCal->calData->calType, conf->channel->center_freq);
150 ah->caldata->CalValid &= ~currCal->calData->calType;
151 currCal->calState = CAL_WAITING;
155 EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
157 void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
160 ah->caldata->nfcal_pending = true;
162 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
163 AR_PHY_AGC_CONTROL_ENABLE_NF);
166 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
167 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
169 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
170 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
172 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
175 void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
177 struct ath9k_nfcal_hist *h = NULL;
180 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
181 struct ath_common *common = ath9k_hw_common(ah);
182 s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
185 h = ah->caldata->nfCalHist;
187 for (i = 0; i < NUM_NF_READINGS; i++) {
188 if (chainmask & (1 << i)) {
196 val = REG_READ(ah, ah->nf_regs[i]);
198 val |= (((u32) nfval << 1) & 0x1ff);
199 REG_WRITE(ah, ah->nf_regs[i], val);
204 * Load software filtered NF value into baseband internal minCCApwr
207 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
208 AR_PHY_AGC_CONTROL_ENABLE_NF);
209 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
210 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
211 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
214 * Wait for load to complete, should be fast, a few 10s of us.
215 * The max delay was changed from an original 250us to 10000us
216 * since 250us often results in NF load timeout and causes deaf
217 * condition during stress testing 12/12/2009
219 for (j = 0; j < 1000; j++) {
220 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
221 AR_PHY_AGC_CONTROL_NF) == 0)
227 * We timed out waiting for the noisefloor to load, probably due to an
228 * in-progress rx. Simply return here and allow the load plenty of time
229 * to complete before the next calibration interval. We need to avoid
230 * trying to load -50 (which happens below) while the previous load is
231 * still in progress as this can cause rx deafness. Instead by returning
232 * here, the baseband nf cal will just be capped by our present
233 * noisefloor until the next calibration timer.
236 ath_print(common, ATH_DBG_ANY, "Timeout while waiting for nf "
237 "to load: AR_PHY_AGC_CONTROL=0x%x\n",
238 REG_READ(ah, AR_PHY_AGC_CONTROL));
243 * Restore maxCCAPower register parameter again so that we're not capped
244 * by the median we just loaded. This will be initial (and max) value
245 * of next noise floor calibration the baseband does.
247 ENABLE_REGWRITE_BUFFER(ah);
248 for (i = 0; i < NUM_NF_READINGS; i++) {
249 if (chainmask & (1 << i)) {
250 val = REG_READ(ah, ah->nf_regs[i]);
252 val |= (((u32) (-50) << 1) & 0x1ff);
253 REG_WRITE(ah, ah->nf_regs[i], val);
256 REGWRITE_BUFFER_FLUSH(ah);
257 DISABLE_REGWRITE_BUFFER(ah);
261 static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
263 struct ath_common *common = ath9k_hw_common(ah);
264 struct ath_nf_limits *limit;
267 if (IS_CHAN_2GHZ(ah->curchan))
272 for (i = 0; i < NUM_NF_READINGS; i++) {
276 ath_print(common, ATH_DBG_CALIBRATE,
277 "NF calibrated [%s] [chain %d] is %d\n",
278 (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
280 if (nf[i] > limit->max) {
281 ath_print(common, ATH_DBG_CALIBRATE,
282 "NF[%d] (%d) > MAX (%d), correcting to MAX",
283 i, nf[i], limit->max);
285 } else if (nf[i] < limit->min) {
286 ath_print(common, ATH_DBG_CALIBRATE,
287 "NF[%d] (%d) < MIN (%d), correcting to NOM",
288 i, nf[i], limit->min);
289 nf[i] = limit->nominal;
294 bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
296 struct ath_common *common = ath9k_hw_common(ah);
297 int16_t nf, nfThresh;
298 int16_t nfarray[NUM_NF_READINGS] = { 0 };
299 struct ath9k_nfcal_hist *h;
300 struct ieee80211_channel *c = chan->chan;
301 struct ath9k_hw_cal_data *caldata = ah->caldata;
306 chan->channelFlags &= (~CHANNEL_CW_INT);
307 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
308 ath_print(common, ATH_DBG_CALIBRATE,
309 "NF did not complete in calibration window\n");
311 caldata->rawNoiseFloor = nf;
314 ath9k_hw_do_getnf(ah, nfarray);
315 ath9k_hw_nf_sanitize(ah, nfarray);
317 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
319 ath_print(common, ATH_DBG_CALIBRATE,
320 "noise floor failed detected; "
321 "detected %d, threshold %d\n",
323 chan->channelFlags |= CHANNEL_CW_INT;
327 h = caldata->nfCalHist;
328 caldata->nfcal_pending = false;
329 ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
330 caldata->rawNoiseFloor = h[0].privNF;
334 void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
335 struct ath9k_channel *chan)
337 struct ath9k_nfcal_hist *h;
344 h = ah->caldata->nfCalHist;
345 default_nf = ath9k_hw_get_default_nf(ah, chan);
346 for (i = 0; i < NUM_NF_READINGS; i++) {
348 h[i].privNF = default_nf;
349 h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
350 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
351 h[i].nfCalBuffer[j] = default_nf;
356 s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
358 if (!ah->caldata || !ah->caldata->rawNoiseFloor)
359 return ath9k_hw_get_default_nf(ah, chan);
361 return ah->caldata->rawNoiseFloor;
363 EXPORT_SYMBOL(ath9k_hw_getchan_noise);