calib.c 9.2 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2008-2009 Atheros Communications Inc.
S
Sujith 已提交
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"
S
Sujith 已提交
19

20 21
/* Common calibration code */

S
Sujith 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* We can tune this as we go by monitoring really low values */
#define ATH9K_NF_TOO_LOW	-60

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;
}

static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
					      int16_t *nfarray)
{
	int i;

	for (i = 0; i < NUM_NF_READINGS; i++) {
		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) {
60 61
			h[i].invalidNFcount--;
			h[i].privNF = nfarray[i];
S
Sujith 已提交
62 63 64 65 66 67 68
		} else {
			h[i].privNF =
				ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
		}
	}
}

69 70 71
static bool ath9k_hw_get_nf_thresh(struct ath_hw *ah,
				   enum ieee80211_band band,
				   int16_t *nft)
S
Sujith 已提交
72
{
73 74
	switch (band) {
	case IEEE80211_BAND_5GHZ:
S
Sujith 已提交
75
		*nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
S
Sujith 已提交
76
		break;
77
	case IEEE80211_BAND_2GHZ:
S
Sujith 已提交
78
		*nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
S
Sujith 已提交
79 80
		break;
	default:
81
		BUG_ON(1);
S
Sujith 已提交
82 83 84 85 86 87
		return false;
	}

	return true;
}

88 89
void ath9k_hw_reset_calibration(struct ath_hw *ah,
				struct ath9k_cal_list *currCal)
S
Sujith 已提交
90 91 92 93 94 95 96 97
{
	int i;

	ath9k_hw_setup_calibration(ah, currCal);

	currCal->calState = CAL_RUNNING;

	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
98 99 100 101
		ah->meas0.sign[i] = 0;
		ah->meas1.sign[i] = 0;
		ah->meas2.sign[i] = 0;
		ah->meas3.sign[i] = 0;
S
Sujith 已提交
102 103
	}

104
	ah->cal_samples = 0;
S
Sujith 已提交
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119
static s16 ath9k_hw_get_default_nf(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->nominal;
}

120
/* This is done for the currently configured channel */
121
bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
S
Sujith 已提交
122
{
123 124
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_conf *conf = &common->hw->conf;
125
	struct ath9k_cal_list *currCal = ah->cal_list_curr;
S
Sujith 已提交
126

127
	if (!ah->caldata)
128
		return true;
S
Sujith 已提交
129 130

	if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
131
		return true;
S
Sujith 已提交
132 133

	if (currCal == NULL)
134
		return true;
S
Sujith 已提交
135 136

	if (currCal->calState != CAL_DONE) {
137 138 139
		ath_print(common, ATH_DBG_CALIBRATE,
			  "Calibration state incorrect, %d\n",
			  currCal->calState);
140
		return true;
S
Sujith 已提交
141 142
	}

143 144
	if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
		return true;
S
Sujith 已提交
145

146 147 148
	ath_print(common, ATH_DBG_CALIBRATE,
		  "Resetting Cal %d state for channel %u\n",
		  currCal->calData->calType, conf->channel->center_freq);
S
Sujith 已提交
149

150
	ah->caldata->CalValid &= ~currCal->calData->calType;
S
Sujith 已提交
151 152
	currCal->calState = CAL_WAITING;

153
	return false;
S
Sujith 已提交
154
}
155
EXPORT_SYMBOL(ath9k_hw_reset_calvalid);
S
Sujith 已提交
156

157
void ath9k_hw_start_nfcal(struct ath_hw *ah, bool update)
S
Sujith 已提交
158 159 160
{
	REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_ENABLE_NF);
161 162 163

	if (update)
		REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
S
Sujith 已提交
164
		    AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
165 166 167 168
	else
		REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
		    AR_PHY_AGC_CONTROL_NO_UPDATE_NF);

S
Sujith 已提交
169 170 171
	REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
}

172 173
void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
{
174
	struct ath9k_nfcal_hist *h = NULL;
175 176
	unsigned i, j;
	int32_t val;
177
	u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
178
	struct ath_common *common = ath9k_hw_common(ah);
179
	s16 default_nf = ath9k_hw_get_default_nf(ah, chan);
180

181 182
	if (ah->caldata)
		h = ah->caldata->nfCalHist;
183 184 185

	for (i = 0; i < NUM_NF_READINGS; i++) {
		if (chainmask & (1 << i)) {
186 187 188 189 190 191 192
			s16 nfval;

			if (h)
				nfval = h[i].privNF;
			else
				nfval = default_nf;

193 194
			val = REG_READ(ah, ah->nf_regs[i]);
			val &= 0xFFFFFE00;
195
			val |= (((u32) nfval << 1) & 0x1ff);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
			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
	 */
	for (j = 0; j < 1000; j++) {
		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.
	 */
	if (j == 1000) {
		ath_print(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));
		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)) {
			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);
	DISABLE_REGWRITE_BUFFER(ah);
}


258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
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;

273 274
		ath_print(common, ATH_DBG_CALIBRATE,
			  "NF calibrated [%s] [chain %d] is %d\n",
275
			  (i >= 3 ? "ext" : "ctl"), i % 3, nf[i]);
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290
		if (nf[i] > limit->max) {
			ath_print(common, ATH_DBG_CALIBRATE,
				  "NF[%d] (%d) > MAX (%d), correcting to MAX",
				  i, nf[i], limit->max);
			nf[i] = limit->max;
		} else if (nf[i] < limit->min) {
			ath_print(common, ATH_DBG_CALIBRATE,
				  "NF[%d] (%d) < MIN (%d), correcting to NOM",
				  i, nf[i], limit->min);
			nf[i] = limit->nominal;
		}
	}
}

291
int16_t ath9k_hw_getnf(struct ath_hw *ah,
S
Sujith 已提交
292 293
		       struct ath9k_channel *chan)
{
294
	struct ath_common *common = ath9k_hw_common(ah);
S
Sujith 已提交
295 296 297
	int16_t nf, nfThresh;
	int16_t nfarray[NUM_NF_READINGS] = { 0 };
	struct ath9k_nfcal_hist *h;
298
	struct ieee80211_channel *c = chan->chan;
299 300 301 302
	struct ath9k_hw_cal_data *caldata = ah->caldata;

	if (!caldata)
		return ath9k_hw_get_default_nf(ah, chan);
S
Sujith 已提交
303 304 305

	chan->channelFlags &= (~CHANNEL_CW_INT);
	if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
306 307
		ath_print(common, ATH_DBG_CALIBRATE,
			  "NF did not complete in calibration window\n");
S
Sujith 已提交
308
		nf = 0;
309 310
		caldata->rawNoiseFloor = nf;
		return caldata->rawNoiseFloor;
S
Sujith 已提交
311 312
	} else {
		ath9k_hw_do_getnf(ah, nfarray);
313
		ath9k_hw_nf_sanitize(ah, nfarray);
S
Sujith 已提交
314
		nf = nfarray[0];
315
		if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
S
Sujith 已提交
316
		    && nf > nfThresh) {
317 318 319 320
			ath_print(common, ATH_DBG_CALIBRATE,
				  "noise floor failed detected; "
				  "detected %d, threshold %d\n",
				  nf, nfThresh);
S
Sujith 已提交
321 322 323 324
			chan->channelFlags |= CHANNEL_CW_INT;
		}
	}

325
	h = caldata->nfCalHist;
S
Sujith 已提交
326 327

	ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
328
	caldata->rawNoiseFloor = h[0].privNF;
S
Sujith 已提交
329

330
	return ah->caldata->rawNoiseFloor;
S
Sujith 已提交
331 332
}

333 334
void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah,
				  struct ath9k_channel *chan)
S
Sujith 已提交
335
{
336 337
	struct ath9k_nfcal_hist *h;
	s16 default_nf;
S
Sujith 已提交
338
	int i, j;
339

340 341
	if (!ah->caldata)
		return;
S
Sujith 已提交
342

343 344
	h = ah->caldata->nfCalHist;
	default_nf = ath9k_hw_get_default_nf(ah, chan);
S
Sujith 已提交
345
	for (i = 0; i < NUM_NF_READINGS; i++) {
346 347 348
		h[i].currIndex = 0;
		h[i].privNF = default_nf;
		h[i].invalidNFcount = AR_PHY_CCA_FILTERWINDOW_LENGTH;
S
Sujith 已提交
349
		for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
350
			h[i].nfCalBuffer[j] = default_nf;
S
Sujith 已提交
351 352 353 354
		}
	}
}

355
s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
S
Sujith 已提交
356
{
357 358
	if (!ah->caldata || !ah->caldata->rawNoiseFloor)
		return ath9k_hw_get_default_nf(ah, chan);
S
Sujith 已提交
359

360
	return ah->caldata->rawNoiseFloor;
S
Sujith 已提交
361
}
362
EXPORT_SYMBOL(ath9k_hw_getchan_noise);