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.
19 #include "ar9002_phy.h"
21 /* Common calibration code */
23 /* We can tune this as we go by monitoring really low values */
24 #define ATH9K_NF_TOO_LOW -60
26 /* AR5416 may return very high value (like -31 dBm), in those cases the nf
27 * is incorrect and we should use the static NF value. Later we can try to
28 * find out why they are reporting these values */
30 static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
32 if (nf > ATH9K_NF_TOO_LOW) {
33 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
34 "noise floor value detected (%d) is "
35 "lower than what we think is a "
36 "reasonable value (%d)\n",
37 nf, ATH9K_NF_TOO_LOW);
43 static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
46 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
49 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
50 sort[i] = nfCalBuffer[i];
52 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
53 for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
54 if (sort[j] > sort[j - 1]) {
56 sort[j] = sort[j - 1];
61 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
66 static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
71 for (i = 0; i < NUM_NF_READINGS; i++) {
72 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
74 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
77 if (h[i].invalidNFcount > 0) {
78 if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE ||
79 nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
80 h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
82 h[i].invalidNFcount--;
83 h[i].privNF = nfarray[i];
87 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
93 static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
94 enum ieee80211_band band,
98 case IEEE80211_BAND_5GHZ:
99 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
101 case IEEE80211_BAND_2GHZ:
102 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
112 void ath9k_hw_reset_calibration(struct ath_hw *ah,
113 struct ath9k_cal_list *currCal)
117 ath9k_hw_setup_calibration(ah, currCal);
119 currCal->calState = CAL_RUNNING;
121 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
122 ah->meas0.sign[i] = 0;
123 ah->meas1.sign[i] = 0;
124 ah->meas2.sign[i] = 0;
125 ah->meas3.sign[i] = 0;
131 /* This is done for the currently configured channel */
132 bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
134 struct ath_common *common = ath9k_hw_common(ah);
135 struct ieee80211_conf *conf = &common->hw->conf;
136 struct ath9k_cal_list *currCal = ah->cal_list_curr;
141 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
147 if (currCal->calState != CAL_DONE) {
148 ath_print(common, ATH_DBG_CALIBRATE,
149 "Calibration state incorrect, %d\n",
154 if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
157 ath_print(common, ATH_DBG_CALIBRATE,
158 "Resetting Cal %d state for channel %u\n",
159 currCal->calData->calType, conf->channel->center_freq);
161 ah->curchan->CalValid &= ~currCal->calData->calType;
162 currCal->calState = CAL_WAITING;
166 EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
168 void ath9k_hw_start_nfcal(struct ath_hw *ah)
170 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
171 AR_PHY_AGC_CONTROL_ENABLE_NF);
172 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
173 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
174 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
177 void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
179 struct ath9k_nfcal_hist *h;
182 const u32 ar5416_cca_regs[6] = {
190 u8 chainmask, rx_chain_status;
192 rx_chain_status = REG_READ(ah, AR_PHY_RX_CHAINMASK);
193 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
195 else if (AR_SREV_9280(ah) || AR_SREV_9287(ah)) {
196 if ((rx_chain_status & 0x2) || (rx_chain_status & 0x4))
201 if (rx_chain_status & 0x4)
203 else if (rx_chain_status & 0x2)
211 for (i = 0; i < NUM_NF_READINGS; i++) {
212 if (chainmask & (1 << i)) {
213 val = REG_READ(ah, ar5416_cca_regs[i]);
215 val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
216 REG_WRITE(ah, ar5416_cca_regs[i], val);
220 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
221 AR_PHY_AGC_CONTROL_ENABLE_NF);
222 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
223 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
224 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
226 for (j = 0; j < 5; j++) {
227 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
228 AR_PHY_AGC_CONTROL_NF) == 0)
233 for (i = 0; i < NUM_NF_READINGS; i++) {
234 if (chainmask & (1 << i)) {
235 val = REG_READ(ah, ar5416_cca_regs[i]);
237 val |= (((u32) (-50) << 1) & 0x1ff);
238 REG_WRITE(ah, ar5416_cca_regs[i], val);
243 int16_t ath9k_hw_getnf(struct ath_hw *ah,
244 struct ath9k_channel *chan)
246 struct ath_common *common = ath9k_hw_common(ah);
247 int16_t nf, nfThresh;
248 int16_t nfarray[NUM_NF_READINGS] = { 0 };
249 struct ath9k_nfcal_hist *h;
250 struct ieee80211_channel *c = chan->chan;
252 chan->channelFlags &= (~CHANNEL_CW_INT);
253 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
254 ath_print(common, ATH_DBG_CALIBRATE,
255 "NF did not complete in calibration window\n");
257 chan->rawNoiseFloor = nf;
258 return chan->rawNoiseFloor;
260 ath9k_hw_do_getnf(ah, nfarray);
262 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
264 ath_print(common, ATH_DBG_CALIBRATE,
265 "noise floor failed detected; "
266 "detected %d, threshold %d\n",
268 chan->channelFlags |= CHANNEL_CW_INT;
274 ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
275 chan->rawNoiseFloor = h[0].privNF;
277 return chan->rawNoiseFloor;
280 void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
285 if (AR_SREV_9280(ah))
286 noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
287 else if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
288 noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
289 else if (AR_SREV_9287(ah))
290 noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
292 noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
294 for (i = 0; i < NUM_NF_READINGS; i++) {
295 ah->nfCalHist[i].currIndex = 0;
296 ah->nfCalHist[i].privNF = noise_floor;
297 ah->nfCalHist[i].invalidNFcount =
298 AR_PHY_CCA_FILTERWINDOW_LENGTH;
299 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
300 ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
305 s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
309 if (chan->rawNoiseFloor == 0)
312 nf = chan->rawNoiseFloor;
314 if (!ath9k_hw_nf_in_range(ah, nf))
315 nf = ATH_DEFAULT_NOISE_FLOOR;
319 EXPORT_SYMBOL(ath9k_hw_getchan_noise);