]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/ath9k/calib.c
ath9k_hw: clean up and fix initial noise floor calibration
[karo-tx-linux.git] / drivers / net / wireless / ath / ath9k / calib.c
1 /*
2  * Copyright (c) 2008-2009 Atheros Communications Inc.
3  *
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
17 #include "hw.h"
18 #include "hw-ops.h"
19
20 /* Common calibration code */
21
22 /* 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
29 static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
30 {
31         if (nf > ATH9K_NF_TOO_LOW) {
32                 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);
37                 return false;
38         }
39         return true;
40 }
41
42 static 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
65 static 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) {
77                         h[i].invalidNFcount--;
78                         h[i].privNF = nfarray[i];
79                 } else {
80                         h[i].privNF =
81                                 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
82                 }
83         }
84 }
85
86 static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
87                                    enum ieee80211_band band,
88                                    int16_t *nft)
89 {
90         switch (band) {
91         case IEEE80211_BAND_5GHZ:
92                 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
93                 break;
94         case IEEE80211_BAND_2GHZ:
95                 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
96                 break;
97         default:
98                 BUG_ON(1);
99                 return false;
100         }
101
102         return true;
103 }
104
105 void ath9k_hw_reset_calibration(struct ath_hw *ah,
106                                 struct ath9k_cal_list *currCal)
107 {
108         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++) {
115                 ah->meas0.sign[i] = 0;
116                 ah->meas1.sign[i] = 0;
117                 ah->meas2.sign[i] = 0;
118                 ah->meas3.sign[i] = 0;
119         }
120
121         ah->cal_samples = 0;
122 }
123
124 /* This is done for the currently configured channel */
125 bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
126 {
127         struct ath_common *common = ath9k_hw_common(ah);
128         struct ieee80211_conf *conf = &common->hw->conf;
129         struct ath9k_cal_list *currCal = ah->cal_list_curr;
130
131         if (!ah->curchan)
132                 return true;
133
134         if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
135                 return true;
136
137         if (currCal == NULL)
138                 return true;
139
140         if (currCal->calState != CAL_DONE) {
141                 ath_print(common, ATH_DBG_CALIBRATE,
142                           "Calibration state incorrect, %d\n",
143                           currCal->calState);
144                 return true;
145         }
146
147         if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
148                 return true;
149
150         ath_print(common, ATH_DBG_CALIBRATE,
151                   "Resetting Cal %d state for channel %u\n",
152                   currCal->calData->calType, conf->channel->center_freq);
153
154         ah->curchan->CalValid &= ~currCal->calData->calType;
155         currCal->calState = CAL_WAITING;
156
157         return false;
158 }
159 EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
160
161 void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
162 {
163         REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
164                     AR_PHY_AGC_CONTROL_ENABLE_NF);
165
166         if (update)
167                 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
168                     AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
169         else
170                 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
171                     AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
172
173         REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
174 }
175
176 void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
177 {
178         struct ath9k_nfcal_hist *h;
179         unsigned i, j;
180         int32_t val;
181         u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
182         struct ath_common *common = ath9k_hw_common(ah);
183
184         h = ah->nfCalHist;
185
186         for (i = 0; i < NUM_NF_READINGS; i++) {
187                 if (chainmask & (1 << i)) {
188                         val = REG_READ(ah, ah->nf_regs[i]);
189                         val &= 0xFFFFFE00;
190                         val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
191                         REG_WRITE(ah, ah->nf_regs[i], val);
192                 }
193         }
194
195         /*
196          * Load software filtered NF value into baseband internal minCCApwr
197          * variable.
198          */
199         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
200                     AR_PHY_AGC_CONTROL_ENABLE_NF);
201         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
202                     AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
203         REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
204
205         /*
206          * Wait for load to complete, should be fast, a few 10s of us.
207          * The max delay was changed from an original 250us to 10000us
208          * since 250us often results in NF load timeout and causes deaf
209          * condition during stress testing 12/12/2009
210          */
211         for (j = 0; j < 1000; j++) {
212                 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
213                      AR_PHY_AGC_CONTROL_NF) == 0)
214                         break;
215                 udelay(10);
216         }
217
218         /*
219          * We timed out waiting for the noisefloor to load, probably due to an
220          * in-progress rx. Simply return here and allow the load plenty of time
221          * to complete before the next calibration interval.  We need to avoid
222          * trying to load -50 (which happens below) while the previous load is
223          * still in progress as this can cause rx deafness. Instead by returning
224          * here, the baseband nf cal will just be capped by our present
225          * noisefloor until the next calibration timer.
226          */
227         if (j == 1000) {
228                 ath_print(common, ATH_DBG_ANY, "Timeout while waiting for nf "
229                           "to load: AR_PHY_AGC_CONTROL=0x%x\n",
230                           REG_READ(ah, AR_PHY_AGC_CONTROL));
231                 return;
232         }
233
234         /*
235          * Restore maxCCAPower register parameter again so that we're not capped
236          * by the median we just loaded.  This will be initial (and max) value
237          * of next noise floor calibration the baseband does.
238          */
239         ENABLE_REGWRITE_BUFFER(ah);
240         for (i = 0; i < NUM_NF_READINGS; i++) {
241                 if (chainmask & (1 << i)) {
242                         val = REG_READ(ah, ah->nf_regs[i]);
243                         val &= 0xFFFFFE00;
244                         val |= (((u32) (-50) << 1) & 0x1ff);
245                         REG_WRITE(ah, ah->nf_regs[i], val);
246                 }
247         }
248         REGWRITE_BUFFER_FLUSH(ah);
249         DISABLE_REGWRITE_BUFFER(ah);
250 }
251
252
253 static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
254 {
255         struct ath_common *common = ath9k_hw_common(ah);
256         struct ath_nf_limits *limit;
257         int i;
258
259         if (IS_CHAN_2GHZ(ah->curchan))
260                 limit = &ah->nf_2g;
261         else
262                 limit = &ah->nf_5g;
263
264         for (i = 0; i < NUM_NF_READINGS; i++) {
265                 if (!nf[i])
266                         continue;
267
268                 ath_print(common, ATH_DBG_CALIBRATE,
269                           "NF calibrated [%s] [chain %d] is %d\n",
270                           (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
271
272                 if (nf[i] > limit->max) {
273                         ath_print(common, ATH_DBG_CALIBRATE,
274                                   "NF[%d] (%d) > MAX (%d), correcting to MAX",
275                                   i, nf[i], limit->max);
276                         nf[i] = limit->max;
277                 } else if (nf[i] < limit->min) {
278                         ath_print(common, ATH_DBG_CALIBRATE,
279                                   "NF[%d] (%d) < MIN (%d), correcting to NOM",
280                                   i, nf[i], limit->min);
281                         nf[i] = limit->nominal;
282                 }
283         }
284 }
285
286 int16_t ath9k_hw_getnf(struct ath_hw *ah,
287                        struct ath9k_channel *chan)
288 {
289         struct ath_common *common = ath9k_hw_common(ah);
290         int16_t nf, nfThresh;
291         int16_t nfarray[NUM_NF_READINGS] = { 0 };
292         struct ath9k_nfcal_hist *h;
293         struct ieee80211_channel *c = chan->chan;
294
295         chan->channelFlags &= (~CHANNEL_CW_INT);
296         if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
297                 ath_print(common, ATH_DBG_CALIBRATE,
298                           "NF did not complete in calibration window\n");
299                 nf = 0;
300                 chan->rawNoiseFloor = nf;
301                 return chan->rawNoiseFloor;
302         } else {
303                 ath9k_hw_do_getnf(ah, nfarray);
304                 ath9k_hw_nf_sanitize(ah, nfarray);
305                 nf = nfarray[0];
306                 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
307                     && nf > nfThresh) {
308                         ath_print(common, ATH_DBG_CALIBRATE,
309                                   "noise floor failed detected; "
310                                   "detected %d, threshold %d\n",
311                                   nf, nfThresh);
312                         chan->channelFlags |= CHANNEL_CW_INT;
313                 }
314         }
315
316         h = ah->nfCalHist;
317
318         ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
319         chan->rawNoiseFloor = h[0].privNF;
320
321         return chan->rawNoiseFloor;
322 }
323
324 void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
325 {
326         struct ath_nf_limits *limit;
327         int i, j;
328
329         if (!ah->curchan || IS_CHAN_2GHZ(ah->curchan))
330                 limit = &ah->nf_2g;
331         else
332                 limit = &ah->nf_5g;
333
334         for (i = 0; i < NUM_NF_READINGS; i++) {
335                 ah->nfCalHist[i].currIndex = 0;
336                 ah->nfCalHist[i].privNF = limit->nominal;
337                 ah->nfCalHist[i].invalidNFcount =
338                         AR_PHY_CCA_FILTERWINDOW_LENGTH;
339                 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
340                         ah->nfCalHist[i].nfCalBuffer[j] = limit->nominal;
341                 }
342         }
343 }
344
345 s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
346 {
347         s16 nf;
348
349         if (chan->rawNoiseFloor == 0)
350                 nf = -96;
351         else
352                 nf = chan->rawNoiseFloor;
353
354         if (!ath9k_hw_nf_in_range(ah, nf))
355                 nf = ATH_DEFAULT_NOISE_FLOOR;
356
357         return nf;
358 }
359 EXPORT_SYMBOL(ath9k_hw_getchan_noise);