blob: f3448a032e6f377cb52154975369af7cb21aa316 [file] [log] [blame]
Sujithf1dc5602008-10-29 10:16:30 +05301/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2008-2011 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"
Paul Gortmakeree40fa02011-05-27 16:14:23 -040019#include <linux/export.h>
Sujithf1dc5602008-10-29 10:16:30 +053020
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040021/* Common calibration code */
22
Sujithf1dc5602008-10-29 10:16:30 +053023
Sujithf1dc5602008-10-29 10:16:30 +053024static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
25{
26 int16_t nfval;
27 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
28 int i, j;
29
30 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
31 sort[i] = nfCalBuffer[i];
32
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]) {
36 nfval = sort[j];
37 sort[j] = sort[j - 1];
38 sort[j - 1] = nfval;
39 }
40 }
41 }
42 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
43
44 return nfval;
45}
46
Felix Fietkau2292ca62010-08-02 15:53:13 +020047static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
48 struct ath9k_channel *chan)
49{
50 struct ath_nf_limits *limit;
51
52 if (!chan || IS_CHAN_2GHZ(chan))
53 limit = &ah->nf_2g;
54 else
55 limit = &ah->nf_5g;
56
57 return limit;
58}
59
60static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
61 struct ath9k_channel *chan)
62{
63 return ath9k_hw_get_nf_limits(ah, chan)->nominal;
64}
65
Felix Fietkauf23fba42011-07-28 14:08:56 +020066s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
67{
68 s8 noise = ATH_DEFAULT_NOISE_FLOOR;
69
70 if (chan && chan->noisefloor) {
71 s8 delta = chan->noisefloor -
72 ath9k_hw_get_default_nf(ah, chan);
73 if (delta > 0)
74 noise += delta;
75 }
76 return noise;
77}
78EXPORT_SYMBOL(ath9k_hw_getchan_noise);
Felix Fietkau2292ca62010-08-02 15:53:13 +020079
80static void ath9k_hw_update_nfcal_hist_buffer(struct ath_hw *ah,
Felix Fietkau70cf1532010-08-02 15:53:14 +020081 struct ath9k_hw_cal_data *cal,
Sujithf1dc5602008-10-29 10:16:30 +053082 int16_t *nfarray)
83{
Felix Fietkau70cf1532010-08-02 15:53:14 +020084 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau2292ca62010-08-02 15:53:13 +020085 struct ath_nf_limits *limit;
Felix Fietkau70cf1532010-08-02 15:53:14 +020086 struct ath9k_nfcal_hist *h;
87 bool high_nf_mid = false;
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053088 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
Sujithf1dc5602008-10-29 10:16:30 +053089 int i;
90
Felix Fietkau70cf1532010-08-02 15:53:14 +020091 h = cal->nfCalHist;
Felix Fietkau2292ca62010-08-02 15:53:13 +020092 limit = ath9k_hw_get_nf_limits(ah, ah->curchan);
93
Sujithf1dc5602008-10-29 10:16:30 +053094 for (i = 0; i < NUM_NF_READINGS; i++) {
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053095 if (!(chainmask & (1 << i)) ||
Rajkumar Manoharan6b3d3482011-08-13 10:28:16 +053096 ((i >= AR5416_MAX_CHAINS) && !IS_CHAN_HT40(ah->curchan)))
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053097 continue;
98
Sujithf1dc5602008-10-29 10:16:30 +053099 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
100
101 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
102 h[i].currIndex = 0;
103
104 if (h[i].invalidNFcount > 0) {
Felix Fietkauf2552e22010-07-02 00:09:50 +0200105 h[i].invalidNFcount--;
106 h[i].privNF = nfarray[i];
Sujithf1dc5602008-10-29 10:16:30 +0530107 } else {
108 h[i].privNF =
109 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
110 }
Felix Fietkau2292ca62010-08-02 15:53:13 +0200111
Felix Fietkau70cf1532010-08-02 15:53:14 +0200112 if (!h[i].privNF)
113 continue;
114
115 if (h[i].privNF > limit->max) {
116 high_nf_mid = true;
117
Joe Perchesd2182b62011-12-15 14:55:53 -0800118 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800119 "NFmid[%d] (%d) > MAX (%d), %s\n",
120 i, h[i].privNF, limit->max,
121 (cal->nfcal_interference ?
122 "not corrected (due to interference)" :
123 "correcting to MAX"));
Felix Fietkau70cf1532010-08-02 15:53:14 +0200124
125 /*
126 * Normally we limit the average noise floor by the
127 * hardware specific maximum here. However if we have
128 * encountered stuck beacons because of interference,
129 * we bypass this limit here in order to better deal
130 * with our environment.
131 */
132 if (!cal->nfcal_interference)
133 h[i].privNF = limit->max;
134 }
Sujithf1dc5602008-10-29 10:16:30 +0530135 }
Felix Fietkau70cf1532010-08-02 15:53:14 +0200136
137 /*
138 * If the noise floor seems normal for all chains, assume that
139 * there is no significant interference in the environment anymore.
140 * Re-enable the enforcement of the NF maximum again.
141 */
142 if (!high_nf_mid)
143 cal->nfcal_interference = false;
Sujithf1dc5602008-10-29 10:16:30 +0530144}
145
Luis R. Rodriguezb43d59f2010-04-15 17:38:58 -0400146static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
147 enum ieee80211_band band,
148 int16_t *nft)
Sujithf1dc5602008-10-29 10:16:30 +0530149{
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800150 switch (band) {
151 case IEEE80211_BAND_5GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530152 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
Sujithf1dc5602008-10-29 10:16:30 +0530153 break;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800154 case IEEE80211_BAND_2GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530155 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
Sujithf1dc5602008-10-29 10:16:30 +0530156 break;
157 default:
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800158 BUG_ON(1);
Sujithf1dc5602008-10-29 10:16:30 +0530159 return false;
160 }
161
162 return true;
163}
164
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400165void ath9k_hw_reset_calibration(struct ath_hw *ah,
166 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530167{
Sujithf1dc5602008-10-29 10:16:30 +0530168 int i;
169
170 ath9k_hw_setup_calibration(ah, currCal);
171
172 currCal->calState = CAL_RUNNING;
173
174 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530175 ah->meas0.sign[i] = 0;
176 ah->meas1.sign[i] = 0;
177 ah->meas2.sign[i] = 0;
178 ah->meas3.sign[i] = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530179 }
180
Sujith2660b812009-02-09 13:27:26 +0530181 ah->cal_samples = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530182}
183
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800184/* This is done for the currently configured channel */
Sujithcbe61d82009-02-09 13:27:12 +0530185bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530186{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700187 struct ath_common *common = ath9k_hw_common(ah);
188 struct ieee80211_conf *conf = &common->hw->conf;
Sujithcbfe9462009-04-13 21:56:56 +0530189 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Sujithf1dc5602008-10-29 10:16:30 +0530190
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200191 if (!ah->caldata)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800192 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530193
194 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800195 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530196
197 if (currCal == NULL)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800198 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530199
200 if (currCal->calState != CAL_DONE) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800201 ath_dbg(common, CALIBRATE, "Calibration state incorrect, %d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800202 currCal->calState);
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800203 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530204 }
205
Felix Fietkau64978272010-10-03 19:07:16 +0200206 if (!(ah->supp_cals & currCal->calData->calType))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800207 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530208
Joe Perchesd2182b62011-12-15 14:55:53 -0800209 ath_dbg(common, CALIBRATE, "Resetting Cal %d state for channel %u\n",
Joe Perches226afe62010-12-02 19:12:37 -0800210 currCal->calData->calType, conf->channel->center_freq);
Sujithf1dc5602008-10-29 10:16:30 +0530211
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200212 ah->caldata->CalValid &= ~currCal->calData->calType;
Sujithf1dc5602008-10-29 10:16:30 +0530213 currCal->calState = CAL_WAITING;
214
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800215 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530216}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400217EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
Sujithf1dc5602008-10-29 10:16:30 +0530218
Felix Fietkau00c86592010-07-30 21:02:09 +0200219void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
Sujithf1dc5602008-10-29 10:16:30 +0530220{
Felix Fietkau4254bc12010-07-31 00:12:01 +0200221 if (ah->caldata)
222 ah->caldata->nfcal_pending = true;
223
Sujithf1dc5602008-10-29 10:16:30 +0530224 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
225 AR_PHY_AGC_CONTROL_ENABLE_NF);
Felix Fietkau00c86592010-07-30 21:02:09 +0200226
227 if (update)
228 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
Sujithf1dc5602008-10-29 10:16:30 +0530229 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
Felix Fietkau00c86592010-07-30 21:02:09 +0200230 else
231 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
232 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
233
Sujithf1dc5602008-10-29 10:16:30 +0530234 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
235}
236
Felix Fietkaubbacee12010-07-11 15:44:42 +0200237void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
238{
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200239 struct ath9k_nfcal_hist *h = NULL;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200240 unsigned i, j;
241 int32_t val;
Felix Fietkau487f0e02010-07-23 04:31:56 +0200242 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200243 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530244 struct ieee80211_conf *conf = &common->hw->conf;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200245 s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200246
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200247 if (ah->caldata)
248 h = ah->caldata->nfCalHist;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200249
250 for (i = 0; i < NUM_NF_READINGS; i++) {
251 if (chainmask & (1 << i)) {
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200252 s16 nfval;
253
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530254 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
255 continue;
256
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200257 if (h)
258 nfval = h[i].privNF;
259 else
260 nfval = default_nf;
261
Felix Fietkaubbacee12010-07-11 15:44:42 +0200262 val = REG_READ(ah, ah->nf_regs[i]);
263 val &= 0xFFFFFE00;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200264 val |= (((u32) nfval << 1) & 0x1ff);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200265 REG_WRITE(ah, ah->nf_regs[i], val);
266 }
267 }
268
269 /*
270 * Load software filtered NF value into baseband internal minCCApwr
271 * variable.
272 */
273 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
274 AR_PHY_AGC_CONTROL_ENABLE_NF);
275 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
276 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
277 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
278
279 /*
280 * Wait for load to complete, should be fast, a few 10s of us.
281 * The max delay was changed from an original 250us to 10000us
282 * since 250us often results in NF load timeout and causes deaf
283 * condition during stress testing 12/12/2009
284 */
Vivek Natarajan23952ec2011-03-10 11:05:43 +0530285 for (j = 0; j < 10000; j++) {
Felix Fietkaubbacee12010-07-11 15:44:42 +0200286 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
287 AR_PHY_AGC_CONTROL_NF) == 0)
288 break;
289 udelay(10);
290 }
291
292 /*
293 * We timed out waiting for the noisefloor to load, probably due to an
294 * in-progress rx. Simply return here and allow the load plenty of time
295 * to complete before the next calibration interval. We need to avoid
296 * trying to load -50 (which happens below) while the previous load is
297 * still in progress as this can cause rx deafness. Instead by returning
298 * here, the baseband nf cal will just be capped by our present
299 * noisefloor until the next calibration timer.
300 */
Vivek Natarajan23952ec2011-03-10 11:05:43 +0530301 if (j == 10000) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800302 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800303 "Timeout while waiting for nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
304 REG_READ(ah, AR_PHY_AGC_CONTROL));
Felix Fietkaubbacee12010-07-11 15:44:42 +0200305 return;
306 }
307
308 /*
309 * Restore maxCCAPower register parameter again so that we're not capped
310 * by the median we just loaded. This will be initial (and max) value
311 * of next noise floor calibration the baseband does.
312 */
313 ENABLE_REGWRITE_BUFFER(ah);
314 for (i = 0; i < NUM_NF_READINGS; i++) {
315 if (chainmask & (1 << i)) {
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530316 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
317 continue;
318
Felix Fietkaubbacee12010-07-11 15:44:42 +0200319 val = REG_READ(ah, ah->nf_regs[i]);
320 val &= 0xFFFFFE00;
321 val |= (((u32) (-50) << 1) & 0x1ff);
322 REG_WRITE(ah, ah->nf_regs[i], val);
323 }
324 }
325 REGWRITE_BUFFER_FLUSH(ah);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200326}
327
328
Felix Fietkauf2552e22010-07-02 00:09:50 +0200329static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
330{
331 struct ath_common *common = ath9k_hw_common(ah);
332 struct ath_nf_limits *limit;
333 int i;
334
335 if (IS_CHAN_2GHZ(ah->curchan))
336 limit = &ah->nf_2g;
337 else
338 limit = &ah->nf_5g;
339
340 for (i = 0; i < NUM_NF_READINGS; i++) {
341 if (!nf[i])
342 continue;
343
Joe Perchesd2182b62011-12-15 14:55:53 -0800344 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800345 "NF calibrated [%s] [chain %d] is %d\n",
346 (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
Felix Fietkau54bd5002010-07-02 00:09:51 +0200347
Rajkumar Manoharan2ee0a072012-03-15 06:08:04 +0530348 if (nf[i] > limit->max) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800349 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800350 "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
Rajkumar Manoharan2ee0a072012-03-15 06:08:04 +0530351 i, nf[i], limit->max);
Felix Fietkauf2552e22010-07-02 00:09:50 +0200352 nf[i] = limit->max;
353 } else if (nf[i] < limit->min) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800354 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800355 "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
356 i, nf[i], limit->min);
Felix Fietkauf2552e22010-07-02 00:09:50 +0200357 nf[i] = limit->nominal;
358 }
359 }
360}
361
Felix Fietkau4254bc12010-07-31 00:12:01 +0200362bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530363{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700364 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530365 int16_t nf, nfThresh;
366 int16_t nfarray[NUM_NF_READINGS] = { 0 };
367 struct ath9k_nfcal_hist *h;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800368 struct ieee80211_channel *c = chan->chan;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200369 struct ath9k_hw_cal_data *caldata = ah->caldata;
370
Sujithf1dc5602008-10-29 10:16:30 +0530371 chan->channelFlags &= (~CHANNEL_CW_INT);
372 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800373 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800374 "NF did not complete in calibration window\n");
Felix Fietkau4254bc12010-07-31 00:12:01 +0200375 return false;
Felix Fietkaud9891c72010-09-29 17:15:27 +0200376 }
377
378 ath9k_hw_do_getnf(ah, nfarray);
379 ath9k_hw_nf_sanitize(ah, nfarray);
380 nf = nfarray[0];
381 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
382 && nf > nfThresh) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800383 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800384 "noise floor failed detected; detected %d, threshold %d\n",
385 nf, nfThresh);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200386 chan->channelFlags |= CHANNEL_CW_INT;
387 }
388
389 if (!caldata) {
390 chan->noisefloor = nf;
Felix Fietkauf23fba42011-07-28 14:08:56 +0200391 ah->noise = ath9k_hw_getchan_noise(ah, chan);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200392 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530393 }
394
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200395 h = caldata->nfCalHist;
Felix Fietkau4254bc12010-07-31 00:12:01 +0200396 caldata->nfcal_pending = false;
Felix Fietkau70cf1532010-08-02 15:53:14 +0200397 ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200398 chan->noisefloor = h[0].privNF;
Felix Fietkauf23fba42011-07-28 14:08:56 +0200399 ah->noise = ath9k_hw_getchan_noise(ah, chan);
Felix Fietkau4254bc12010-07-31 00:12:01 +0200400 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530401}
Rajkumar Manoharan1a19f772012-01-09 15:37:53 +0530402EXPORT_SYMBOL(ath9k_hw_getnf);
Sujithf1dc5602008-10-29 10:16:30 +0530403
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200404void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
405 struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530406{
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200407 struct ath9k_nfcal_hist *h;
408 s16 default_nf;
Sujithf1dc5602008-10-29 10:16:30 +0530409 int i, j;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400410
Rajkumar Manoharanbdd196a2011-01-15 01:01:22 +0530411 ah->caldata->channel = chan->channel;
412 ah->caldata->channelFlags = chan->channelFlags & ~CHANNEL_CW_INT;
Rajkumar Manoharan77d84832012-10-12 14:07:24 +0530413 ah->caldata->chanmode = chan->chanmode;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200414 h = ah->caldata->nfCalHist;
415 default_nf = ath9k_hw_get_default_nf(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +0530416 for (i = 0; i < NUM_NF_READINGS; i++) {
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200417 h[i].currIndex = 0;
418 h[i].privNF = default_nf;
419 h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
Sujithf1dc5602008-10-29 10:16:30 +0530420 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200421 h[i].nfCalBuffer[j] = default_nf;
Sujithf1dc5602008-10-29 10:16:30 +0530422 }
423 }
Sujithf1dc5602008-10-29 10:16:30 +0530424}
425
Felix Fietkau70cf1532010-08-02 15:53:14 +0200426
427void ath9k_hw_bstuck_nfcal(struct ath_hw *ah)
428{
429 struct ath9k_hw_cal_data *caldata = ah->caldata;
430
431 if (unlikely(!caldata))
432 return;
433
434 /*
435 * If beacons are stuck, the most likely cause is interference.
436 * Triggering a noise floor calibration at this point helps the
437 * hardware adapt to a noisy environment much faster.
438 * To ensure that we recover from stuck beacons quickly, let
439 * the baseband update the internal NF value itself, similar to
440 * what is being done after a full reset.
441 */
442 if (!caldata->nfcal_pending)
443 ath9k_hw_start_nfcal(ah, true);
444 else if (!(REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
445 ath9k_hw_getnf(ah, ah->curchan);
446
447 caldata->nfcal_interference = true;
448}
449EXPORT_SYMBOL(ath9k_hw_bstuck_nfcal);
450