calib.c 11.7 KB
Newer Older
Sujith's avatar
Sujith committed
1
/*
2
 * Copyright (c) 2008-2011 Atheros Communications Inc.
Sujith's avatar
Sujith committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

17
#include "hw.h"
18
#include "hw-ops.h"
Sujith's avatar
Sujith committed
19

20
21
/* Common calibration code */

22
#define ATH9K_NF_TOO_HIGH	-60
Sujith's avatar
Sujith committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
{
	int16_t nfval;
	int16_t sort[ATH9K_NF_CAL_HIST_MAX];
	int i, j;

	for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
		sort[i] = nfCalBuffer[i];

	for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
		for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
			if (sort[j] > sort[j - 1]) {
				nfval = sort[j];
				sort[j] = sort[j - 1];
				sort[j - 1] = nfval;
			}
		}
	}
	nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];

	return nfval;
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
static struct ath_nf_limits *ath9k_hw_get_nf_limits(struct ath_hw *ah,
						    struct ath9k_channel *chan)
{
	struct ath_nf_limits *limit;

	if (!chan || IS_CHAN_2GHZ(chan))
		limit = &ah->nf_2g;
	else
		limit = &ah->nf_5g;

	return limit;
}

static s16 ath9k_hw_get_default_nf(struct ath_hw *ah,
				   struct ath9k_channel *chan)
{
	return ath9k_hw_get_nf_limits(ah, chan)->nominal;
}

66
67
68
69
70
71
72
73
74
75
76
77
78
s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
{
	s8 noise = ATH_DEFAULT_NOISE_FLOOR;

	if (chan && chan->noisefloor) {
		s8 delta = chan->noisefloor -
			   ath9k_hw_get_default_nf(ah, chan);
		if (delta > 0)
			noise += delta;
	}
	return noise;
}
EXPORT_SYMBOL(ath9k_hw_getchan_noise);
79
80

static void ath9k_hw_update_nfcal_hist_buffer(struct ath_hw *ah,
81
					      struct ath9k_hw_cal_data *cal,
Sujith's avatar
Sujith committed
82
83
					      int16_t *nfarray)
{
84
	struct ath_common *common = ath9k_hw_common(ah);
85
	struct ieee80211_conf *conf = &common->hw->conf;
86
	struct ath_nf_limits *limit;
87
88
	struct ath9k_nfcal_hist *h;
	bool high_nf_mid = false;
89
	u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
Sujith's avatar
Sujith committed
90
91
	int i;

92
	h = cal->nfCalHist;
93
94
	limit = ath9k_hw_get_nf_limits(ah, ah->curchan);

Sujith's avatar
Sujith committed
95
	for (i = 0; i < NUM_NF_READINGS; i++) {
96
97
98
99
		if (!(chainmask & (1 << i)) ||
		    ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf)))
			continue;

Sujith's avatar
Sujith committed
100
101
102
103
104
105
		h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];

		if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
			h[i].currIndex = 0;

		if (h[i].invalidNFcount > 0) {
106
107
			h[i].invalidNFcount--;
			h[i].privNF = nfarray[i];
Sujith's avatar
Sujith committed
108
109
110
111
		} else {
			h[i].privNF =
				ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
		}
112

113
114
115
116
117
118
		if (!h[i].privNF)
			continue;

		if (h[i].privNF > limit->max) {
			high_nf_mid = true;

119
120
121
122
123
124
			ath_dbg(common, ATH_DBG_CALIBRATE,
				"NFmid[%d] (%d) > MAX (%d), %s\n",
				i, h[i].privNF, limit->max,
				(cal->nfcal_interference ?
				 "not corrected (due to interference)" :
				 "correcting to MAX"));
125
126
127
128
129
130
131
132
133
134
135

			/*
			 * Normally we limit the average noise floor by the
			 * hardware specific maximum here. However if we have
			 * encountered stuck beacons because of interference,
			 * we bypass this limit here in order to better deal
			 * with our environment.
			 */
			if (!cal->nfcal_interference)
				h[i].privNF = limit->max;
		}
Sujith's avatar
Sujith committed
136
	}
137
138
139
140
141
142
143
144

	/*
	 * If the noise floor seems normal for all chains, assume that
	 * there is no significant interference in the environment anymore.
	 * Re-enable the enforcement of the NF maximum again.
	 */
	if (!high_nf_mid)
		cal->nfcal_interference = false;
Sujith's avatar
Sujith committed
145
146
}

147
148
149
static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
				   enum ieee80211_band band,
				   int16_t *nft)
Sujith's avatar
Sujith committed
150
{
151
152
	switch (band) {
	case IEEE80211_BAND_5GHZ:
Sujith's avatar
Sujith committed
153
		*nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
Sujith's avatar
Sujith committed
154
		break;
155
	case IEEE80211_BAND_2GHZ:
Sujith's avatar
Sujith committed
156
		*nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
Sujith's avatar
Sujith committed
157
158
		break;
	default:
159
		BUG_ON(1);
Sujith's avatar
Sujith committed
160
161
162
163
164
165
		return false;
	}

	return true;
}

166
167
void ath9k_hw_reset_calibration(struct ath_hw *ah,
				struct ath9k_cal_list *currCal)
Sujith's avatar
Sujith committed
168
169
170
171
172
173
174
175
{
	int i;

	ath9k_hw_setup_calibration(ah, currCal);

	currCal->calState = CAL_RUNNING;

	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
176
177
178
179
		ah->meas0.sign[i] = 0;
		ah->meas1.sign[i] = 0;
		ah->meas2.sign[i] = 0;
		ah->meas3.sign[i] = 0;
Sujith's avatar
Sujith committed
180
181
	}

182
	ah->cal_samples = 0;
Sujith's avatar
Sujith committed
183
184
}

185
/* This is done for the currently configured channel */
186
bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
Sujith's avatar
Sujith committed
187
{
188
189
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_conf *conf = &common->hw->conf;
190
	struct ath9k_cal_list *currCal = ah->cal_list_curr;
Sujith's avatar
Sujith committed
191

192
	if (!ah->caldata)
193
		return true;
Sujith's avatar
Sujith committed
194
195

	if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
196
		return true;
Sujith's avatar
Sujith committed
197
198

	if (currCal == NULL)
199
		return true;
Sujith's avatar
Sujith committed
200
201

	if (currCal->calState != CAL_DONE) {
202
203
204
		ath_dbg(common, ATH_DBG_CALIBRATE,
			"Calibration state incorrect, %d\n",
			currCal->calState);
205
		return true;
Sujith's avatar
Sujith committed
206
207
	}

208
	if (!(ah->supp_cals & currCal->calData->calType))
209
		return true;
Sujith's avatar
Sujith committed
210

211
212
213
	ath_dbg(common, ATH_DBG_CALIBRATE,
		"Resetting Cal %d state for channel %u\n",
		currCal->calData->calType, conf->channel->center_freq);
Sujith's avatar
Sujith committed
214

215
	ah->caldata->CalValid &= ~currCal->calData->calType;
Sujith's avatar
Sujith committed
216
217
	currCal->calState = CAL_WAITING;

218
	return false;
Sujith's avatar
Sujith committed
219
}
220
EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
Sujith's avatar
Sujith committed
221

222
void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
Sujith's avatar
Sujith committed
223
{
224
225
226
	if (ah->caldata)
		ah->caldata->nfcal_pending = true;

Sujith's avatar
Sujith committed
227
228
	REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_ENABLE_NF);
229
230
231

	if (update)
		REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
Sujith's avatar
Sujith committed
232
		    AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
233
234
235
236
	else
		REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_NO_UPDATE_NF);

Sujith's avatar
Sujith committed
237
238
239
	REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
}

240
241
void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
{
242
	struct ath9k_nfcal_hist *h = NULL;
243
244
	unsigned i, j;
	int32_t val;
245
	u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
246
	struct ath_common *common = ath9k_hw_common(ah);
247
	struct ieee80211_conf *conf = &common->hw->conf;
248
	s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
249

250
251
	if (ah->caldata)
		h = ah->caldata->nfCalHist;
252
253
254

	for (i = 0; i < NUM_NF_READINGS; i++) {
		if (chainmask & (1 << i)) {
255
256
			s16 nfval;

257
258
259
			if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
				continue;

260
261
262
263
264
			if (h)
				nfval = h[i].privNF;
			else
				nfval = default_nf;

265
266
			val = REG_READ(ah, ah->nf_regs[i]);
			val &= 0xFFFFFE00;
267
			val |= (((u32) nfval << 1) & 0x1ff);
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
			REG_WRITE(ah, ah->nf_regs[i], val);
		}
	}

	/*
	 * Load software filtered NF value into baseband internal minCCApwr
	 * variable.
	 */
	REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_ENABLE_NF);
	REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
	REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);

	/*
	 * Wait for load to complete, should be fast, a few 10s of us.
	 * The max delay was changed from an original 250us to 10000us
	 * since 250us often results in NF load timeout and causes deaf
	 * condition during stress testing 12/12/2009
	 */
288
	for (j = 0; j < 10000; j++) {
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
		     AR_PHY_AGC_CONTROL_NF) == 0)
			break;
		udelay(10);
	}

	/*
	 * We timed out waiting for the noisefloor to load, probably due to an
	 * in-progress rx. Simply return here and allow the load plenty of time
	 * to complete before the next calibration interval.  We need to avoid
	 * trying to load -50 (which happens below) while the previous load is
	 * still in progress as this can cause rx deafness. Instead by returning
	 * here, the baseband nf cal will just be capped by our present
	 * noisefloor until the next calibration timer.
	 */
304
	if (j == 10000) {
305
306
307
		ath_dbg(common, ATH_DBG_ANY,
			"Timeout while waiting for nf to load: AR_PHY_AGC_CONTROL=0x%x\n",
			REG_READ(ah, AR_PHY_AGC_CONTROL));
308
309
310
311
312
313
314
315
316
317
318
		return;
	}

	/*
	 * Restore maxCCAPower register parameter again so that we're not capped
	 * by the median we just loaded.  This will be initial (and max) value
	 * of next noise floor calibration the baseband does.
	 */
	ENABLE_REGWRITE_BUFFER(ah);
	for (i = 0; i < NUM_NF_READINGS; i++) {
		if (chainmask & (1 << i)) {
319
320
321
			if ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf))
				continue;

322
323
324
325
326
327
328
329
330
331
			val = REG_READ(ah, ah->nf_regs[i]);
			val &= 0xFFFFFE00;
			val |= (((u32) (-50) << 1) & 0x1ff);
			REG_WRITE(ah, ah->nf_regs[i], val);
		}
	}
	REGWRITE_BUFFER_FLUSH(ah);
}


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath_nf_limits *limit;
	int i;

	if (IS_CHAN_2GHZ(ah->curchan))
		limit = &ah->nf_2g;
	else
		limit = &ah->nf_5g;

	for (i = 0; i < NUM_NF_READINGS; i++) {
		if (!nf[i])
			continue;

347
348
349
		ath_dbg(common, ATH_DBG_CALIBRATE,
			"NF calibrated [%s] [chain %d] is %d\n",
			(i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
350

351
		if (nf[i] > ATH9K_NF_TOO_HIGH) {
352
353
354
			ath_dbg(common, ATH_DBG_CALIBRATE,
				"NF[%d] (%d) > MAX (%d), correcting to MAX\n",
				i, nf[i], ATH9K_NF_TOO_HIGH);
355
356
			nf[i] = limit->max;
		} else if (nf[i] < limit->min) {
357
358
359
			ath_dbg(common, ATH_DBG_CALIBRATE,
				"NF[%d] (%d) < MIN (%d), correcting to NOM\n",
				i, nf[i], limit->min);
360
361
362
363
364
			nf[i] = limit->nominal;
		}
	}
}

365
bool ath9k_hw_getnf(struct ath_hw *ah, struct ath9k_channel *chan)
Sujith's avatar
Sujith committed
366
{
367
	struct ath_common *common = ath9k_hw_common(ah);
Sujith's avatar
Sujith committed
368
369
370
	int16_t nf, nfThresh;
	int16_t nfarray[NUM_NF_READINGS] = { 0 };
	struct ath9k_nfcal_hist *h;
371
	struct ieee80211_channel *c = chan->chan;
372
373
	struct ath9k_hw_cal_data *caldata = ah->caldata;

Sujith's avatar
Sujith committed
374
375
	chan->channelFlags &= (~CHANNEL_CW_INT);
	if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
376
377
		ath_dbg(common, ATH_DBG_CALIBRATE,
			"NF did not complete in calibration window\n");
378
		return false;
379
380
381
382
383
384
385
	}

	ath9k_hw_do_getnf(ah, nfarray);
	ath9k_hw_nf_sanitize(ah, nfarray);
	nf = nfarray[0];
	if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
	    && nf > nfThresh) {
386
387
388
		ath_dbg(common, ATH_DBG_CALIBRATE,
			"noise floor failed detected; detected %d, threshold %d\n",
			nf, nfThresh);
389
390
391
392
393
		chan->channelFlags |= CHANNEL_CW_INT;
	}

	if (!caldata) {
		chan->noisefloor = nf;
394
		ah->noise = ath9k_hw_getchan_noise(ah, chan);
395
		return false;
Sujith's avatar
Sujith committed
396
397
	}

398
	h = caldata->nfCalHist;
399
	caldata->nfcal_pending = false;
400
	ath9k_hw_update_nfcal_hist_buffer(ah, caldata, nfarray);
401
	chan->noisefloor = h[0].privNF;
402
	ah->noise = ath9k_hw_getchan_noise(ah, chan);
403
	return true;
Sujith's avatar
Sujith committed
404
405
}

406
407
void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
				  struct ath9k_channel *chan)
Sujith's avatar
Sujith committed
408
{
409
410
	struct ath9k_nfcal_hist *h;
	s16 default_nf;
Sujith's avatar
Sujith committed
411
	int i, j;
412

413
414
	ah->caldata->channel = chan->channel;
	ah->caldata->channelFlags = chan->channelFlags & ~CHANNEL_CW_INT;
415
416
	h = ah->caldata->nfCalHist;
	default_nf = ath9k_hw_get_default_nf(ah, chan);
Sujith's avatar
Sujith committed
417
	for (i = 0; i < NUM_NF_READINGS; i++) {
418
419
420
		h[i].currIndex = 0;
		h[i].privNF = default_nf;
		h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
Sujith's avatar
Sujith committed
421
		for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
422
			h[i].nfCalBuffer[j] = default_nf;
Sujith's avatar
Sujith committed
423
424
425
426
		}
	}
}

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451

void ath9k_hw_bstuck_nfcal(struct ath_hw *ah)
{
	struct ath9k_hw_cal_data *caldata = ah->caldata;

	if (unlikely(!caldata))
		return;

	/*
	 * If beacons are stuck, the most likely cause is interference.
	 * Triggering a noise floor calibration at this point helps the
	 * hardware adapt to a noisy environment much faster.
	 * To ensure that we recover from stuck beacons quickly, let
	 * the baseband update the internal NF value itself, similar to
	 * what is being done after a full reset.
	 */
	if (!caldata->nfcal_pending)
		ath9k_hw_start_nfcal(ah, true);
	else if (!(REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
		ath9k_hw_getnf(ah, ah->curchan);

	caldata->nfcal_interference = true;
}
EXPORT_SYMBOL(ath9k_hw_bstuck_nfcal);