blob: cc29ef78d1bfe1b8144944979a85721e3b141efd [file] [log] [blame]
Sujithf1dc5602008-10-29 10:16:30 +05301/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 Atheros Communications Inc.
Sujithf1dc5602008-10-29 10:16:30 +05303 *
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.
7 *
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.
15 */
16
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -070017#include "hw.h"
Felix Fietkau641d9922010-04-15 17:38:49 -040018#include "hw-ops.h"
Sujithf1dc5602008-10-29 10:16:30 +053019
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040020/* Common calibration code */
21
Sujithf1dc5602008-10-29 10:16:30 +053022/* We can tune this as we go by monitoring really low values */
23#define ATH9K_NF_TOO_LOW -60
24
25/* AR5416 may return very high value (like -31 dBm), in those cases the nf
26 * is incorrect and we should use the static NF value. Later we can try to
27 * find out why they are reporting these values */
28
Sujithcbe61d82009-02-09 13:27:12 +053029static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
Sujithf1dc5602008-10-29 10:16:30 +053030{
31 if (nf > ATH9K_NF_TOO_LOW) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -070032 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
33 "noise floor value detected (%d) is "
34 "lower than what we think is a "
35 "reasonable value (%d)\n",
36 nf, ATH9K_NF_TOO_LOW);
Sujithf1dc5602008-10-29 10:16:30 +053037 return false;
38 }
39 return true;
40}
41
42static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
43{
44 int16_t nfval;
45 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
46 int i, j;
47
48 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
49 sort[i] = nfCalBuffer[i];
50
51 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
52 for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
53 if (sort[j] > sort[j - 1]) {
54 nfval = sort[j];
55 sort[j] = sort[j - 1];
56 sort[j - 1] = nfval;
57 }
58 }
59 }
60 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
61
62 return nfval;
63}
64
65static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
66 int16_t *nfarray)
67{
68 int i;
69
70 for (i = 0; i < NUM_NF_READINGS; i++) {
71 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
72
73 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
74 h[i].currIndex = 0;
75
76 if (h[i].invalidNFcount > 0) {
Felix Fietkauf2552e22010-07-02 00:09:50 +020077 h[i].invalidNFcount--;
78 h[i].privNF = nfarray[i];
Sujithf1dc5602008-10-29 10:16:30 +053079 } else {
80 h[i].privNF =
81 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
82 }
83 }
Sujithf1dc5602008-10-29 10:16:30 +053084}
85
Luis R. Rodriguezb43d59f2010-04-15 17:38:58 -040086static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
87 enum ieee80211_band band,
88 int16_t *nft)
Sujithf1dc5602008-10-29 10:16:30 +053089{
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -080090 switch (band) {
91 case IEEE80211_BAND_5GHZ:
Sujithf74df6f2009-02-09 13:27:24 +053092 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
Sujithf1dc5602008-10-29 10:16:30 +053093 break;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -080094 case IEEE80211_BAND_2GHZ:
Sujithf74df6f2009-02-09 13:27:24 +053095 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
Sujithf1dc5602008-10-29 10:16:30 +053096 break;
97 default:
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -080098 BUG_ON(1);
Sujithf1dc5602008-10-29 10:16:30 +053099 return false;
100 }
101
102 return true;
103}
104
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400105void ath9k_hw_reset_calibration(struct ath_hw *ah,
106 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530107{
Sujithf1dc5602008-10-29 10:16:30 +0530108 int i;
109
110 ath9k_hw_setup_calibration(ah, currCal);
111
112 currCal->calState = CAL_RUNNING;
113
114 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530115 ah->meas0.sign[i] = 0;
116 ah->meas1.sign[i] = 0;
117 ah->meas2.sign[i] = 0;
118 ah->meas3.sign[i] = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530119 }
120
Sujith2660b812009-02-09 13:27:26 +0530121 ah->cal_samples = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530122}
123
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800124/* This is done for the currently configured channel */
Sujithcbe61d82009-02-09 13:27:12 +0530125bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530126{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700127 struct ath_common *common = ath9k_hw_common(ah);
128 struct ieee80211_conf *conf = &common->hw->conf;
Sujithcbfe9462009-04-13 21:56:56 +0530129 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Sujithf1dc5602008-10-29 10:16:30 +0530130
Sujith2660b812009-02-09 13:27:26 +0530131 if (!ah->curchan)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800132 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530133
134 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800135 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530136
137 if (currCal == NULL)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800138 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530139
140 if (currCal->calState != CAL_DONE) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700141 ath_print(common, ATH_DBG_CALIBRATE,
142 "Calibration state incorrect, %d\n",
143 currCal->calState);
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800144 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530145 }
146
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800147 if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
148 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530149
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700150 ath_print(common, ATH_DBG_CALIBRATE,
151 "Resetting Cal %d state for channel %u\n",
152 currCal->calData->calType, conf->channel->center_freq);
Sujithf1dc5602008-10-29 10:16:30 +0530153
Sujith2660b812009-02-09 13:27:26 +0530154 ah->curchan->CalValid &= ~currCal->calData->calType;
Sujithf1dc5602008-10-29 10:16:30 +0530155 currCal->calState = CAL_WAITING;
156
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800157 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530158}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400159EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
Sujithf1dc5602008-10-29 10:16:30 +0530160
Sujithcbe61d82009-02-09 13:27:12 +0530161void ath9k_hw_start_nfcal(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530162{
163 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
164 AR_PHY_AGC_CONTROL_ENABLE_NF);
165 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
166 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
167 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
168}
169
Felix Fietkauf2552e22010-07-02 00:09:50 +0200170static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
171{
172 struct ath_common *common = ath9k_hw_common(ah);
173 struct ath_nf_limits *limit;
174 int i;
175
176 if (IS_CHAN_2GHZ(ah->curchan))
177 limit = &ah->nf_2g;
178 else
179 limit = &ah->nf_5g;
180
181 for (i = 0; i < NUM_NF_READINGS; i++) {
182 if (!nf[i])
183 continue;
184
Felix Fietkau54bd5002010-07-02 00:09:51 +0200185 ath_print(common, ATH_DBG_CALIBRATE,
186 "NF calibrated [%s] [chain %d] is %d\n",
187 (i > 3 ? "ext" : "ctl"), i % 3, nf[i]);
188
Felix Fietkauf2552e22010-07-02 00:09:50 +0200189 if (nf[i] > limit->max) {
190 ath_print(common, ATH_DBG_CALIBRATE,
191 "NF[%d] (%d) > MAX (%d), correcting to MAX",
192 i, nf[i], limit->max);
193 nf[i] = limit->max;
194 } else if (nf[i] < limit->min) {
195 ath_print(common, ATH_DBG_CALIBRATE,
196 "NF[%d] (%d) < MIN (%d), correcting to NOM",
197 i, nf[i], limit->min);
198 nf[i] = limit->nominal;
199 }
200 }
201}
202
Sujithcbe61d82009-02-09 13:27:12 +0530203int16_t ath9k_hw_getnf(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530204 struct ath9k_channel *chan)
205{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700206 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530207 int16_t nf, nfThresh;
208 int16_t nfarray[NUM_NF_READINGS] = { 0 };
209 struct ath9k_nfcal_hist *h;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800210 struct ieee80211_channel *c = chan->chan;
Sujithf1dc5602008-10-29 10:16:30 +0530211
212 chan->channelFlags &= (~CHANNEL_CW_INT);
213 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700214 ath_print(common, ATH_DBG_CALIBRATE,
215 "NF did not complete in calibration window\n");
Sujithf1dc5602008-10-29 10:16:30 +0530216 nf = 0;
217 chan->rawNoiseFloor = nf;
218 return chan->rawNoiseFloor;
219 } else {
220 ath9k_hw_do_getnf(ah, nfarray);
Felix Fietkauf2552e22010-07-02 00:09:50 +0200221 ath9k_hw_nf_sanitize(ah, nfarray);
Sujithf1dc5602008-10-29 10:16:30 +0530222 nf = nfarray[0];
Luis R. Rodriguezb43d59f2010-04-15 17:38:58 -0400223 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
Sujithf1dc5602008-10-29 10:16:30 +0530224 && nf > nfThresh) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700225 ath_print(common, ATH_DBG_CALIBRATE,
226 "noise floor failed detected; "
227 "detected %d, threshold %d\n",
228 nf, nfThresh);
Sujithf1dc5602008-10-29 10:16:30 +0530229 chan->channelFlags |= CHANNEL_CW_INT;
230 }
231 }
232
Sujithf1dc5602008-10-29 10:16:30 +0530233 h = ah->nfCalHist;
Sujithf1dc5602008-10-29 10:16:30 +0530234
235 ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
236 chan->rawNoiseFloor = h[0].privNF;
237
238 return chan->rawNoiseFloor;
239}
240
Sujithcbe61d82009-02-09 13:27:12 +0530241void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530242{
Felix Fietkauf2552e22010-07-02 00:09:50 +0200243 struct ath_nf_limits *limit;
Sujithf1dc5602008-10-29 10:16:30 +0530244 int i, j;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400245
Felix Fietkauf2552e22010-07-02 00:09:50 +0200246 if (!ah->curchan || IS_CHAN_2GHZ(ah->curchan))
247 limit = &ah->nf_2g;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400248 else
Felix Fietkauf2552e22010-07-02 00:09:50 +0200249 limit = &ah->nf_5g;
Sujithf1dc5602008-10-29 10:16:30 +0530250
251 for (i = 0; i < NUM_NF_READINGS; i++) {
252 ah->nfCalHist[i].currIndex = 0;
Felix Fietkauf2552e22010-07-02 00:09:50 +0200253 ah->nfCalHist[i].privNF = limit->nominal;
Sujithf1dc5602008-10-29 10:16:30 +0530254 ah->nfCalHist[i].invalidNFcount =
255 AR_PHY_CCA_FILTERWINDOW_LENGTH;
256 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
Felix Fietkauf2552e22010-07-02 00:09:50 +0200257 ah->nfCalHist[i].nfCalBuffer[j] = limit->nominal;
Sujithf1dc5602008-10-29 10:16:30 +0530258 }
259 }
Sujithf1dc5602008-10-29 10:16:30 +0530260}
261
Sujithcbe61d82009-02-09 13:27:12 +0530262s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530263{
Sujithf1dc5602008-10-29 10:16:30 +0530264 s16 nf;
265
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800266 if (chan->rawNoiseFloor == 0)
Luis R. Rodrigueze56db712008-12-23 15:58:47 -0800267 nf = -96;
268 else
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800269 nf = chan->rawNoiseFloor;
Sujithf1dc5602008-10-29 10:16:30 +0530270
271 if (!ath9k_hw_nf_in_range(ah, nf))
272 nf = ATH_DEFAULT_NOISE_FLOOR;
273
274 return nf;
275}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400276EXPORT_SYMBOL(ath9k_hw_getchan_noise);