blob: 2f4b48e6fb036648af5eb1cdf35bd217b97dc67f [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
Felix Fietkau2292ca62010-08-02 15:53:13 +020023#define ATH9K_NF_TOO_HIGH -60
Sujithf1dc5602008-10-29 10:16:30 +053024
Sujithf1dc5602008-10-29 10:16:30 +053025static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
26{
27 int16_t nfval;
28 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
29 int i, j;
30
31 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
32 sort[i] = nfCalBuffer[i];
33
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]) {
37 nfval = sort[j];
38 sort[j] = sort[j - 1];
39 sort[j - 1] = nfval;
40 }
41 }
42 }
43 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
44
45 return nfval;
46}
47
Felix Fietkau2292ca62010-08-02 15:53:13 +020048static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
49 struct ath9k_channel *chan)
50{
51 struct ath_nf_limits *limit;
52
53 if (!chan || IS_CHAN_2GHZ(chan))
54 limit = &ah->nf_2g;
55 else
56 limit = &ah->nf_5g;
57
58 return limit;
59}
60
61static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
62 struct ath9k_channel *chan)
63{
64 return ath9k_hw_get_nf_limits(ah, chan)->nominal;
65}
66
Felix Fietkauf23fba42011-07-28 14:08:56 +020067s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
68{
69 s8 noise = ATH_DEFAULT_NOISE_FLOOR;
70
71 if (chan && chan->noisefloor) {
72 s8 delta = chan->noisefloor -
73 ath9k_hw_get_default_nf(ah, chan);
74 if (delta > 0)
75 noise += delta;
76 }
77 return noise;
78}
79EXPORT_SYMBOL(ath9k_hw_getchan_noise);
Felix Fietkau2292ca62010-08-02 15:53:13 +020080
81static void ath9k_hw_update_nfcal_hist_buffer(struct ath_hw *ah,
Felix Fietkau70cf1532010-08-02 15:53:14 +020082 struct ath9k_hw_cal_data *cal,
Sujithf1dc5602008-10-29 10:16:30 +053083 int16_t *nfarray)
84{
Felix Fietkau70cf1532010-08-02 15:53:14 +020085 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau2292ca62010-08-02 15:53:13 +020086 struct ath_nf_limits *limit;
Felix Fietkau70cf1532010-08-02 15:53:14 +020087 struct ath9k_nfcal_hist *h;
88 bool high_nf_mid = false;
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053089 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
Sujithf1dc5602008-10-29 10:16:30 +053090 int i;
91
Felix Fietkau70cf1532010-08-02 15:53:14 +020092 h = cal->nfCalHist;
Felix Fietkau2292ca62010-08-02 15:53:13 +020093 limit = ath9k_hw_get_nf_limits(ah, ah->curchan);
94
Sujithf1dc5602008-10-29 10:16:30 +053095 for (i = 0; i < NUM_NF_READINGS; i++) {
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053096 if (!(chainmask & (1 << i)) ||
Rajkumar Manoharan6b3d3482011-08-13 10:28:16 +053097 ((i >= AR5416_MAX_CHAINS) && !IS_CHAN_HT40(ah->curchan)))
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +053098 continue;
99
Sujithf1dc5602008-10-29 10:16:30 +0530100 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
101
102 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
103 h[i].currIndex = 0;
104
105 if (h[i].invalidNFcount > 0) {
Felix Fietkauf2552e22010-07-02 00:09:50 +0200106 h[i].invalidNFcount--;
107 h[i].privNF = nfarray[i];
Sujithf1dc5602008-10-29 10:16:30 +0530108 } else {
109 h[i].privNF =
110 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
111 }
Felix Fietkau2292ca62010-08-02 15:53:13 +0200112
Felix Fietkau70cf1532010-08-02 15:53:14 +0200113 if (!h[i].privNF)
114 continue;
115
116 if (h[i].privNF > limit->max) {
117 high_nf_mid = true;
118
Joe Perchesd2182b62011-12-15 14:55:53 -0800119 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800120 "NFmid[%d] (%d) > MAX (%d), %s\n",
121 i, h[i].privNF, limit->max,
122 (cal->nfcal_interference ?
123 "not corrected (due to interference)" :
124 "correcting to MAX"));
Felix Fietkau70cf1532010-08-02 15:53:14 +0200125
126 /*
127 * Normally we limit the average noise floor by the
128 * hardware specific maximum here. However if we have
129 * encountered stuck beacons because of interference,
130 * we bypass this limit here in order to better deal
131 * with our environment.
132 */
133 if (!cal->nfcal_interference)
134 h[i].privNF = limit->max;
135 }
Sujithf1dc5602008-10-29 10:16:30 +0530136 }
Felix Fietkau70cf1532010-08-02 15:53:14 +0200137
138 /*
139 * If the noise floor seems normal for all chains, assume that
140 * there is no significant interference in the environment anymore.
141 * Re-enable the enforcement of the NF maximum again.
142 */
143 if (!high_nf_mid)
144 cal->nfcal_interference = false;
Sujithf1dc5602008-10-29 10:16:30 +0530145}
146
Luis R. Rodriguezb43d59f2010-04-15 17:38:58 -0400147static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
148 enum ieee80211_band band,
149 int16_t *nft)
Sujithf1dc5602008-10-29 10:16:30 +0530150{
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800151 switch (band) {
152 case IEEE80211_BAND_5GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530153 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
Sujithf1dc5602008-10-29 10:16:30 +0530154 break;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800155 case IEEE80211_BAND_2GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530156 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
Sujithf1dc5602008-10-29 10:16:30 +0530157 break;
158 default:
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800159 BUG_ON(1);
Sujithf1dc5602008-10-29 10:16:30 +0530160 return false;
161 }
162
163 return true;
164}
165
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400166void ath9k_hw_reset_calibration(struct ath_hw *ah,
167 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530168{
Sujithf1dc5602008-10-29 10:16:30 +0530169 int i;
170
171 ath9k_hw_setup_calibration(ah, currCal);
172
173 currCal->calState = CAL_RUNNING;
174
175 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530176 ah->meas0.sign[i] = 0;
177 ah->meas1.sign[i] = 0;
178 ah->meas2.sign[i] = 0;
179 ah->meas3.sign[i] = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530180 }
181
Sujith2660b812009-02-09 13:27:26 +0530182 ah->cal_samples = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530183}
184
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800185/* This is done for the currently configured channel */
Sujithcbe61d82009-02-09 13:27:12 +0530186bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530187{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700188 struct ath_common *common = ath9k_hw_common(ah);
189 struct ieee80211_conf *conf = &common->hw->conf;
Sujithcbfe9462009-04-13 21:56:56 +0530190 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Sujithf1dc5602008-10-29 10:16:30 +0530191
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200192 if (!ah->caldata)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800193 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530194
195 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800196 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530197
198 if (currCal == NULL)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800199 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530200
201 if (currCal->calState != CAL_DONE) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800202 ath_dbg(common, CALIBRATE, "Calibration state incorrect, %d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800203 currCal->calState);
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800204 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530205 }
206
Felix Fietkau64978272010-10-03 19:07:16 +0200207 if (!(ah->supp_cals & currCal->calData->calType))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800208 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530209
Joe Perchesd2182b62011-12-15 14:55:53 -0800210 ath_dbg(common, CALIBRATE, "Resetting Cal %d state for channel %u\n",
Joe Perches226afe62010-12-02 19:12:37 -0800211 currCal->calData->calType, conf->channel->center_freq);
Sujithf1dc5602008-10-29 10:16:30 +0530212
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200213 ah->caldata->CalValid &= ~currCal->calData->calType;
Sujithf1dc5602008-10-29 10:16:30 +0530214 currCal->calState = CAL_WAITING;
215
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800216 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530217}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400218EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
Sujithf1dc5602008-10-29 10:16:30 +0530219
Felix Fietkau00c86592010-07-30 21:02:09 +0200220void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
Sujithf1dc5602008-10-29 10:16:30 +0530221{
Felix Fietkau4254bc12010-07-31 00:12:01 +0200222 if (ah->caldata)
223 ah->caldata->nfcal_pending = true;
224
Sujithf1dc5602008-10-29 10:16:30 +0530225 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
226 AR_PHY_AGC_CONTROL_ENABLE_NF);
Felix Fietkau00c86592010-07-30 21:02:09 +0200227
228 if (update)
229 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
Sujithf1dc5602008-10-29 10:16:30 +0530230 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
Felix Fietkau00c86592010-07-30 21:02:09 +0200231 else
232 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
233 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
234
Sujithf1dc5602008-10-29 10:16:30 +0530235 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
236}
237
Felix Fietkaubbacee12010-07-11 15:44:42 +0200238void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
239{
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200240 struct ath9k_nfcal_hist *h = NULL;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200241 unsigned i, j;
242 int32_t val;
Felix Fietkau487f0e02010-07-23 04:31:56 +0200243 u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200244 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530245 struct ieee80211_conf *conf = &common->hw->conf;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200246 s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200247
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200248 if (ah->caldata)
249 h = ah->caldata->nfCalHist;
Felix Fietkaubbacee12010-07-11 15:44:42 +0200250
251 for (i = 0; i < NUM_NF_READINGS; i++) {
252 if (chainmask & (1 << i)) {
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200253 s16 nfval;
254
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530255 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
256 continue;
257
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200258 if (h)
259 nfval = h[i].privNF;
260 else
261 nfval = default_nf;
262
Felix Fietkaubbacee12010-07-11 15:44:42 +0200263 val = REG_READ(ah, ah->nf_regs[i]);
264 val &= 0xFFFFFE00;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200265 val |= (((u32) nfval << 1) & 0x1ff);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200266 REG_WRITE(ah, ah->nf_regs[i], val);
267 }
268 }
269
270 /*
271 * Load software filtered NF value into baseband internal minCCApwr
272 * variable.
273 */
274 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
275 AR_PHY_AGC_CONTROL_ENABLE_NF);
276 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
277 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
278 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
279
280 /*
281 * Wait for load to complete, should be fast, a few 10s of us.
282 * The max delay was changed from an original 250us to 10000us
283 * since 250us often results in NF load timeout and causes deaf
284 * condition during stress testing 12/12/2009
285 */
Vivek Natarajan23952ec2011-03-10 11:05:43 +0530286 for (j = 0; j < 10000; j++) {
Felix Fietkaubbacee12010-07-11 15:44:42 +0200287 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
288 AR_PHY_AGC_CONTROL_NF) == 0)
289 break;
290 udelay(10);
291 }
292
293 /*
294 * We timed out waiting for the noisefloor to load, probably due to an
295 * in-progress rx. Simply return here and allow the load plenty of time
296 * to complete before the next calibration interval. We need to avoid
297 * trying to load -50 (which happens below) while the previous load is
298 * still in progress as this can cause rx deafness. Instead by returning
299 * here, the baseband nf cal will just be capped by our present
300 * noisefloor until the next calibration timer.
301 */
Vivek Natarajan23952ec2011-03-10 11:05:43 +0530302 if (j == 10000) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800303 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800304 "Timeout while waiting for nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
305 REG_READ(ah, AR_PHY_AGC_CONTROL));
Felix Fietkaubbacee12010-07-11 15:44:42 +0200306 return;
307 }
308
309 /*
310 * Restore maxCCAPower register parameter again so that we're not capped
311 * by the median we just loaded. This will be initial (and max) value
312 * of next noise floor calibration the baseband does.
313 */
314 ENABLE_REGWRITE_BUFFER(ah);
315 for (i = 0; i < NUM_NF_READINGS; i++) {
316 if (chainmask & (1 << i)) {
Rajkumar Manoharan28ef6452011-05-04 19:37:17 +0530317 if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
318 continue;
319
Felix Fietkaubbacee12010-07-11 15:44:42 +0200320 val = REG_READ(ah, ah->nf_regs[i]);
321 val &= 0xFFFFFE00;
322 val |= (((u32) (-50) << 1) & 0x1ff);
323 REG_WRITE(ah, ah->nf_regs[i], val);
324 }
325 }
326 REGWRITE_BUFFER_FLUSH(ah);
Felix Fietkaubbacee12010-07-11 15:44:42 +0200327}
328
329
Felix Fietkauf2552e22010-07-02 00:09:50 +0200330static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
331{
332 struct ath_common *common = ath9k_hw_common(ah);
333 struct ath_nf_limits *limit;
334 int i;
335
336 if (IS_CHAN_2GHZ(ah->curchan))
337 limit = &ah->nf_2g;
338 else
339 limit = &ah->nf_5g;
340
341 for (i = 0; i < NUM_NF_READINGS; i++) {
342 if (!nf[i])
343 continue;
344
Joe Perchesd2182b62011-12-15 14:55:53 -0800345 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800346 "NF calibrated [%s] [chain %d] is %d\n",
347 (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
Felix Fietkau54bd5002010-07-02 00:09:51 +0200348
Felix Fietkau2292ca62010-08-02 15:53:13 +0200349 if (nf[i] > ATH9K_NF_TOO_HIGH) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800350 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800351 "NF[%d] (%d) > MAX (%d), correcting to MAX\n",
352 i, nf[i], ATH9K_NF_TOO_HIGH);
Felix Fietkauf2552e22010-07-02 00:09:50 +0200353 nf[i] = limit->max;
354 } else if (nf[i] < limit->min) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800355 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800356 "NF[%d] (%d) < MIN (%d), correcting to NOM\n",
357 i, nf[i], limit->min);
Felix Fietkauf2552e22010-07-02 00:09:50 +0200358 nf[i] = limit->nominal;
359 }
360 }
361}
362
Felix Fietkau4254bc12010-07-31 00:12:01 +0200363bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530364{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700365 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530366 int16_t nf, nfThresh;
367 int16_t nfarray[NUM_NF_READINGS] = { 0 };
368 struct ath9k_nfcal_hist *h;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800369 struct ieee80211_channel *c = chan->chan;
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200370 struct ath9k_hw_cal_data *caldata = ah->caldata;
371
Sujithf1dc5602008-10-29 10:16:30 +0530372 chan->channelFlags &= (~CHANNEL_CW_INT);
373 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800374 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800375 "NF did not complete in calibration window\n");
Felix Fietkau4254bc12010-07-31 00:12:01 +0200376 return false;
Felix Fietkaud9891c72010-09-29 17:15:27 +0200377 }
378
379 ath9k_hw_do_getnf(ah, nfarray);
380 ath9k_hw_nf_sanitize(ah, nfarray);
381 nf = nfarray[0];
382 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
383 && nf > nfThresh) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800384 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800385 "noise floor failed detected; detected %d, threshold %d\n",
386 nf, nfThresh);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200387 chan->channelFlags |= CHANNEL_CW_INT;
388 }
389
390 if (!caldata) {
391 chan->noisefloor = nf;
Felix Fietkauf23fba42011-07-28 14:08:56 +0200392 ah->noise = ath9k_hw_getchan_noise(ah, chan);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200393 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530394 }
395
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200396 h = caldata->nfCalHist;
Felix Fietkau4254bc12010-07-31 00:12:01 +0200397 caldata->nfcal_pending = false;
Felix Fietkau70cf1532010-08-02 15:53:14 +0200398 ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
Felix Fietkaud9891c72010-09-29 17:15:27 +0200399 chan->noisefloor = h[0].privNF;
Felix Fietkauf23fba42011-07-28 14:08:56 +0200400 ah->noise = ath9k_hw_getchan_noise(ah, chan);
Felix Fietkau4254bc12010-07-31 00:12:01 +0200401 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530402}
Rajkumar Manoharan1a19f772012-01-09 15:37:53 +0530403EXPORT_SYMBOL(ath9k_hw_getnf);
Sujithf1dc5602008-10-29 10:16:30 +0530404
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200405void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
406 struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530407{
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200408 struct ath9k_nfcal_hist *h;
409 s16 default_nf;
Sujithf1dc5602008-10-29 10:16:30 +0530410 int i, j;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400411
Rajkumar Manoharanbdd196a2011-01-15 01:01:22 +0530412 ah->caldata->channel = chan->channel;
413 ah->caldata->channelFlags = chan->channelFlags & ~CHANNEL_CW_INT;
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