Deleted Added
sdiff udiff text old ( 185380 ) new ( 185406 )
full compact
1/*
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * $Id: ar5416_cal.c,v 1.7 2008/11/11 17:43:23 sam Exp $
18 */
19#include "opt_ah.h"
20
21#ifdef AH_SUPPORT_AR5416
22
23#include "ah.h"
24#include "ah_internal.h"
25#include "ah_devid.h"
26
27#include "ah_eeprom_v14.h"
28
29#include "ar5416/ar5416.h"
30#include "ar5416/ar5416reg.h"
31#include "ar5416/ar5416phy.h"
32#ifdef AH_SUPPORT_AR9280
33#include "ar5416/ar9280.h"
34#endif
35
36/* Owl specific stuff */
37#define NUM_NOISEFLOOR_READINGS 6 /* 3 chains * (ctl + ext) */
38
39static void ar5416StartNFCal(struct ath_hal *ah);
40static void ar5416LoadNF(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *);
41static int16_t ar5416GetNf(struct ath_hal *, HAL_CHANNEL_INTERNAL *);
42
43/*
44 * Determine if calibration is supported by device and channel flags
45 */
46static OS_INLINE HAL_BOOL
47ar5416IsCalSupp(struct ath_hal *ah, HAL_CHANNEL *chan, HAL_CAL_TYPE calType)
48{
49 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
50
51 switch (calType & cal->suppCals) {
52 case IQ_MISMATCH_CAL:
53 /* Run IQ Mismatch for non-CCK only */
54 return !IS_CHAN_B(chan);
55 case ADC_GAIN_CAL:
56 case ADC_DC_CAL:
57 /* Run ADC Gain Cal for non-CCK & non 2GHz-HT20 only */
58 return !IS_CHAN_B(chan) &&
59 !(IS_CHAN_2GHZ(chan) && IS_CHAN_HT20(chan));
60 }
61 return AH_FALSE;
62}
63
64/*
65 * Setup HW to collect samples used for current cal
66 */
67static void
68ar5416SetupMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
69{
70 /* Start calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
71 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
72 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
73 currCal->calData->calCountMax);
74
75 /* Select calibration to run */
76 switch (currCal->calData->calType) {
77 case IQ_MISMATCH_CAL:
78 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
79 HALDEBUG(ah, HAL_DEBUG_PERCAL,
80 "%s: start IQ Mismatch calibration\n", __func__);
81 break;
82 case ADC_GAIN_CAL:
83 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
84 HALDEBUG(ah, HAL_DEBUG_PERCAL,
85 "%s: start ADC Gain calibration\n", __func__);
86 break;
87 case ADC_DC_CAL:
88 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
89 HALDEBUG(ah, HAL_DEBUG_PERCAL,
90 "%s: start ADC DC calibration\n", __func__);
91 break;
92 case ADC_DC_INIT_CAL:
93 OS_REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
94 HALDEBUG(ah, HAL_DEBUG_PERCAL,
95 "%s: start Init ADC DC calibration\n", __func__);
96 break;
97 }
98 /* Kick-off cal */
99 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL);
100}
101
102/*
103 * Initialize shared data structures and prepare a cal to be run.
104 */
105static void
106ar5416ResetMeasurement(struct ath_hal *ah, HAL_CAL_LIST *currCal)
107{
108 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
109
110 /* Reset data structures shared between different calibrations */
111 OS_MEMZERO(cal->caldata, sizeof(cal->caldata));
112 cal->calSamples = 0;
113
114 /* Setup HW for new calibration */
115 ar5416SetupMeasurement(ah, currCal);
116
117 /* Change SW state to RUNNING for this calibration */
118 currCal->calState = CAL_RUNNING;
119}
120
121#if 0
122/*
123 * Run non-periodic calibrations.
124 */
125static HAL_BOOL
126ar5416RunInitCals(struct ath_hal *ah, int init_cal_count)
127{
128 struct ath_hal_5416 *ahp = AH5416(ah);
129 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
130 HAL_CHANNEL_INTERNAL ichan; /* XXX bogus */
131 HAL_CAL_LIST *curCal = ahp->ah_cal_curr;
132 HAL_BOOL isCalDone;
133 int i;
134
135 if (curCal == AH_NULL)
136 return AH_FALSE;
137
138 ichan.calValid = 0;
139 for (i = 0; i < init_cal_count; i++) {
140 /* Reset this Cal */
141 ar5416ResetMeasurement(ah, curCal);
142 /* Poll for offset calibration complete */
143 if (!ath_hal_wait(ah, AR_PHY_TIMING_CTRL4, AR_PHY_TIMING_CTRL4_DO_CAL, 0)) {
144 HALDEBUG(ah, HAL_DEBUG_ANY,
145 "%s: Cal %d failed to finish in 100ms.\n",
146 __func__, curCal->calData->calType);
147 /* Re-initialize list pointers for periodic cals */
148 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
149 return AH_FALSE;
150 }
151 /* Run this cal */
152 ar5416DoCalibration(ah, &ichan, ahp->ah_rxchainmask,
153 curCal, &isCalDone);
154 if (!isCalDone)
155 HALDEBUG(ah, HAL_DEBUG_ANY,
156 "%s: init cal %d did not complete.\n",
157 __func__, curCal->calData->calType);
158 if (curCal->calNext != AH_NULL)
159 curCal = curCal->calNext;
160 }
161
162 /* Re-initialize list pointers for periodic cals */
163 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
164 return AH_TRUE;
165}
166#endif
167
168/*
169 * Initialize Calibration infrastructure.
170 */
171HAL_BOOL
172ar5416InitCal(struct ath_hal *ah, HAL_CHANNEL *chan)
173{
174 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
175 HAL_CHANNEL_INTERNAL *ichan;
176
177 ichan = ath_hal_checkchannel(ah, chan);
178 HALASSERT(ichan != AH_NULL);
179
180 if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
181 /* Enable Rx Filter Cal */
182 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
183 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
184 AR_PHY_AGC_CONTROL_FLTR_CAL);
185
186 /* Clear the carrier leak cal bit */
187 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
188
189 /* kick off the cal */
190 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
191
192 /* Poll for offset calibration complete */
193 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
194 HALDEBUG(ah, HAL_DEBUG_ANY,
195 "%s: offset calibration failed to complete in 1ms; "
196 "noisy environment?\n", __func__);
197 return AH_FALSE;
198 }
199
200 /* Set the cl cal bit and rerun the cal a 2nd time */
201 /* Enable Rx Filter Cal */
202 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
203 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
204 AR_PHY_AGC_CONTROL_FLTR_CAL);
205
206 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
207 }
208
209 /* Calibrate the AGC */
210 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
211
212 /* Poll for offset calibration complete */
213 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
214 HALDEBUG(ah, HAL_DEBUG_ANY,
215 "%s: offset calibration did not complete in 1ms; "
216 "noisy environment?\n", __func__);
217 return AH_FALSE;
218 }
219
220 /*
221 * Do NF calibration after DC offset and other CALs.
222 * Per system engineers, noise floor value can sometimes be 20 dB
223 * higher than normal value if DC offset and noise floor cal are
224 * triggered at the same time.
225 */
226 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
227
228 /* Initialize list pointers */
229 cal->cal_list = cal->cal_last = cal->cal_curr = AH_NULL;
230
231 /*
232 * Enable IQ, ADC Gain, ADC DC Offset Cals
233 */
234 if (AR_SREV_SOWL_10_OR_LATER(ah)) {
235 /* Setup all non-periodic, init time only calibrations */
236 /* XXX: Init DC Offset not working yet */
237#if 0
238 if (ar5416IsCalSupp(ah, chan, ADC_DC_INIT_CAL)) {
239 INIT_CAL(&cal->adcDcCalInitData);
240 INSERT_CAL(cal, &cal->adcDcCalInitData);
241 }
242 /* Initialize current pointer to first element in list */
243 cal->cal_curr = cal->cal_list;
244
245 if (cal->ah_cal_curr != AH_NULL && !ar5416RunInitCals(ah, 0))
246 return AH_FALSE;
247#endif
248 }
249
250 /* If Cals are supported, add them to list via INIT/INSERT_CAL */
251 if (ar5416IsCalSupp(ah, chan, ADC_GAIN_CAL)) {
252 INIT_CAL(&cal->adcGainCalData);
253 INSERT_CAL(cal, &cal->adcGainCalData);
254 HALDEBUG(ah, HAL_DEBUG_PERCAL,
255 "%s: enable ADC Gain Calibration.\n", __func__);
256 }
257 if (ar5416IsCalSupp(ah, chan, ADC_DC_CAL)) {
258 INIT_CAL(&cal->adcDcCalData);
259 INSERT_CAL(cal, &cal->adcDcCalData);
260 HALDEBUG(ah, HAL_DEBUG_PERCAL,
261 "%s: enable ADC DC Calibration.\n", __func__);
262 }
263 if (ar5416IsCalSupp(ah, chan, IQ_MISMATCH_CAL)) {
264 INIT_CAL(&cal->iqCalData);
265 INSERT_CAL(cal, &cal->iqCalData);
266 HALDEBUG(ah, HAL_DEBUG_PERCAL,
267 "%s: enable IQ Calibration.\n", __func__);
268 }
269 /* Initialize current pointer to first element in list */
270 cal->cal_curr = cal->cal_list;
271
272 /* Kick off measurements for the first cal */
273 if (cal->cal_curr != AH_NULL)
274 ar5416ResetMeasurement(ah, cal->cal_curr);
275
276 /* Mark all calibrations on this channel as being invalid */
277 ichan->calValid = 0;
278
279 return AH_TRUE;
280}
281
282/*
283 * Entry point for upper layers to restart current cal.
284 * Reset the calibration valid bit in channel.
285 */
286HAL_BOOL
287ar5416ResetCalValid(struct ath_hal *ah, HAL_CHANNEL *chan)
288{
289 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
290 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
291 HAL_CAL_LIST *currCal = cal->cal_curr;
292
293 if (!AR_SREV_SOWL_10_OR_LATER(ah))
294 return AH_FALSE;
295 if (currCal == AH_NULL)
296 return AH_FALSE;
297 if (ichan == AH_NULL) {
298 HALDEBUG(ah, HAL_DEBUG_ANY,
299 "%s: invalid channel %u/0x%x; no mapping\n",
300 __func__, chan->channel, chan->channelFlags);
301 return AH_FALSE;
302 }
303 /*
304 * Expected that this calibration has run before, post-reset.
305 * Current state should be done
306 */
307 if (currCal->calState != CAL_DONE) {
308 HALDEBUG(ah, HAL_DEBUG_ANY,
309 "%s: Calibration state incorrect, %d\n",
310 __func__, currCal->calState);
311 return AH_FALSE;
312 }
313
314 /* Verify Cal is supported on this channel */
315 if (!ar5416IsCalSupp(ah, chan, currCal->calData->calType))
316 return AH_FALSE;
317
318 HALDEBUG(ah, HAL_DEBUG_PERCAL,
319 "%s: Resetting Cal %d state for channel %u/0x%x\n",
320 __func__, currCal->calData->calType, chan->channel,
321 chan->channelFlags);
322
323 /* Disable cal validity in channel */
324 ichan->calValid &= ~currCal->calData->calType;
325 currCal->calState = CAL_WAITING;
326
327 return AH_TRUE;
328}
329
330/*
331 * Recalibrate the lower PHY chips to account for temperature/environment
332 * changes.
333 */
334static void
335ar5416DoCalibration(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan,
336 uint8_t rxchainmask, HAL_CAL_LIST *currCal, HAL_BOOL *isCalDone)
337{
338 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
339
340 /* Cal is assumed not done until explicitly set below */
341 *isCalDone = AH_FALSE;
342
343 HALDEBUG(ah, HAL_DEBUG_PERCAL,
344 "%s: %s Calibration, state %d, calValid 0x%x\n",
345 __func__, currCal->calData->calName, currCal->calState,
346 ichan->calValid);
347
348 /* Calibration in progress. */
349 if (currCal->calState == CAL_RUNNING) {
350 /* Check to see if it has finished. */
351 if (!(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_CAL)) {
352 HALDEBUG(ah, HAL_DEBUG_PERCAL,
353 "%s: sample %d of %d finished\n",
354 __func__, cal->calSamples,
355 currCal->calData->calNumSamples);
356 /*
357 * Collect measurements for active chains.
358 */
359 currCal->calData->calCollect(ah);
360 if (++cal->calSamples >= currCal->calData->calNumSamples) {
361 int i, numChains = 0;
362 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
363 if (rxchainmask & (1 << i))
364 numChains++;
365 }
366 /*
367 * Process accumulated data
368 */
369 currCal->calData->calPostProc(ah, numChains);
370
371 /* Calibration has finished. */
372 ichan->calValid |= currCal->calData->calType;
373 currCal->calState = CAL_DONE;
374 *isCalDone = AH_TRUE;
375 } else {
376 /*
377 * Set-up to collect of another sub-sample.
378 */
379 ar5416SetupMeasurement(ah, currCal);
380 }
381 }
382 } else if (!(ichan->calValid & currCal->calData->calType)) {
383 /* If current cal is marked invalid in channel, kick it off */
384 ar5416ResetMeasurement(ah, currCal);
385 }
386}
387
388/*
389 * Internal interface to schedule periodic calibration work.
390 */
391HAL_BOOL
392ar5416PerCalibrationN(struct ath_hal *ah, HAL_CHANNEL *chan,
393 u_int rxchainmask, HAL_BOOL longcal, HAL_BOOL *isCalDone)
394{
395 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
396 HAL_CAL_LIST *currCal = cal->cal_curr;
397 HAL_CHANNEL_INTERNAL *ichan;
398
399 OS_MARK(ah, AH_MARK_PERCAL, chan->channel);
400
401 *isCalDone = AH_TRUE;
402
403 /* Invalid channel check */
404 ichan = ath_hal_checkchannel(ah, chan);
405 if (ichan == AH_NULL) {
406 HALDEBUG(ah, HAL_DEBUG_ANY,
407 "%s: invalid channel %u/0x%x; no mapping\n",
408 __func__, chan->channel, chan->channelFlags);
409 return AH_FALSE;
410 }
411
412 /*
413 * For given calibration:
414 * 1. Call generic cal routine
415 * 2. When this cal is done (isCalDone) if we have more cals waiting
416 * (eg after reset), mask this to upper layers by not propagating
417 * isCalDone if it is set to TRUE.
418 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
419 * to be run.
420 */
421 if (currCal != AH_NULL &&
422 (currCal->calState == CAL_RUNNING ||
423 currCal->calState == CAL_WAITING)) {
424 ar5416DoCalibration(ah, ichan, rxchainmask, currCal, isCalDone);
425 if (*isCalDone == AH_TRUE) {
426 cal->cal_curr = currCal = currCal->calNext;
427 if (currCal->calState == CAL_WAITING) {
428 *isCalDone = AH_FALSE;
429 ar5416ResetMeasurement(ah, currCal);
430 }
431 }
432 }
433
434 /* Do NF cal only at longer intervals */
435 if (longcal) {
436 /*
437 * Get the value from the previous NF cal
438 * and update the history buffer.
439 */
440 ar5416GetNf(ah, ichan);
441
442 /*
443 * Load the NF from history buffer of the current channel.
444 * NF is slow time-variant, so it is OK to use a
445 * historical value.
446 */
447 ar5416LoadNF(ah, AH_PRIVATE(ah)->ah_curchan);
448
449 /* start NF calibration, without updating BB NF register*/
450 ar5416StartNFCal(ah);
451
452 if ((ichan->channelFlags & CHANNEL_CW_INT) != 0) {
453 /* report up and clear internal state */
454 chan->channelFlags |= CHANNEL_CW_INT;
455 ichan->channelFlags &= ~CHANNEL_CW_INT;
456 }
457 }
458 return AH_TRUE;
459}
460
461/*
462 * Recalibrate the lower PHY chips to account for temperature/environment
463 * changes.
464 */
465HAL_BOOL
466ar5416PerCalibration(struct ath_hal *ah, HAL_CHANNEL *chan, HAL_BOOL *isIQdone)
467{
468 struct ath_hal_5416 *ahp = AH5416(ah);
469 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
470 HAL_CAL_LIST *curCal = cal->cal_curr;
471
472 if (curCal != AH_NULL && curCal->calData->calType == IQ_MISMATCH_CAL) {
473 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
474 AH_TRUE, isIQdone);
475 } else {
476 HAL_BOOL isCalDone;
477
478 *isIQdone = AH_FALSE;
479 return ar5416PerCalibrationN(ah, chan, ahp->ah_rx_chainmask,
480 AH_TRUE, &isCalDone);
481 }
482}
483
484static HAL_BOOL
485ar5416GetEepromNoiseFloorThresh(struct ath_hal *ah,
486 const HAL_CHANNEL_INTERNAL *chan, int16_t *nft)
487{
488 switch (chan->channelFlags & CHANNEL_ALL_NOTURBO) {
489 case CHANNEL_A:
490 case CHANNEL_A_HT20:
491 case CHANNEL_A_HT40PLUS:
492 case CHANNEL_A_HT40MINUS:
493 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_5, nft);
494 break;
495 case CHANNEL_B:
496 case CHANNEL_G:
497 case CHANNEL_G_HT20:
498 case CHANNEL_G_HT40PLUS:
499 case CHANNEL_G_HT40MINUS:
500 ath_hal_eepromGet(ah, AR_EEP_NFTHRESH_2, nft);
501 break;
502 default:
503 HALDEBUG(ah, HAL_DEBUG_ANY,
504 "%s: invalid channel flags 0x%x\n",
505 __func__, chan->channelFlags);
506 return AH_FALSE;
507 }
508 return AH_TRUE;
509}
510
511static void
512ar5416StartNFCal(struct ath_hal *ah)
513{
514 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
515 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
516 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
517}
518
519static void
520ar5416LoadNF(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan)
521{
522 static const uint32_t ar5416_cca_regs[] = {
523 AR_PHY_CCA,
524 AR_PHY_CH1_CCA,
525 AR_PHY_CH2_CCA,
526 AR_PHY_EXT_CCA,
527 AR_PHY_CH1_EXT_CCA,
528 AR_PHY_CH2_EXT_CCA
529 };
530 struct ar5212NfCalHist *h;
531 int i, j;
532 int32_t val;
533 uint8_t chainmask;
534
535 /*
536 * Force NF calibration for all chains.
537 */
538 if (AR_SREV_KITE(ah)) {
539 /* Kite has only one chain */
540 chainmask = 0x9;
541 } else if (AR_SREV_MERLIN(ah)) {
542 /* Merlin has only two chains */
543 chainmask = 0x1B;
544 } else {
545 chainmask = 0x3F;
546 }
547
548 /*
549 * Write filtered NF values into maxCCApwr register parameter
550 * so we can load below.
551 */
552 h = AH5416(ah)->ah_cal.nfCalHist;
553 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
554 if (chainmask & (1 << i)) {
555 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
556 val &= 0xFFFFFE00;
557 val |= (((uint32_t)(h[i].privNF) << 1) & 0x1ff);
558 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
559 }
560
561 /* Load software filtered NF value into baseband internal minCCApwr variable. */
562 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
563 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
564 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
565
566 /* Wait for load to complete, should be fast, a few 10s of us. */
567 for (j = 0; j < 1000; j++) {
568 if ((OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) == 0)
569 break;
570 OS_DELAY(10);
571 }
572
573 /*
574 * Restore maxCCAPower register parameter again so that we're not capped
575 * by the median we just loaded. This will be initial (and max) value
576 * of next noise floor calibration the baseband does.
577 */
578 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++)
579 if (chainmask & (1 << i)) {
580 val = OS_REG_READ(ah, ar5416_cca_regs[i]);
581 val &= 0xFFFFFE00;
582 val |= (((uint32_t)(-50) << 1) & 0x1ff);
583 OS_REG_WRITE(ah, ar5416_cca_regs[i], val);
584 }
585}
586
587void
588ar5416InitNfHistBuff(struct ar5212NfCalHist *h)
589{
590 int i, j;
591
592 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
593 h[i].currIndex = 0;
594 h[i].privNF = AR5416_CCA_MAX_GOOD_VALUE;
595 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
596 for (j = 0; j < AR512_NF_CAL_HIST_MAX; j ++)
597 h[i].nfCalBuffer[j] = AR5416_CCA_MAX_GOOD_VALUE;
598 }
599}
600
601/*
602 * Update the noise floor buffer as a ring buffer
603 */
604static void
605ar5416UpdateNFHistBuff(struct ar5212NfCalHist *h, int16_t *nfarray)
606{
607 int i;
608
609 for (i = 0; i < AR5416_NUM_NF_READINGS; i ++) {
610 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
611
612 if (++h[i].currIndex >= AR512_NF_CAL_HIST_MAX)
613 h[i].currIndex = 0;
614 if (h[i].invalidNFcount > 0) {
615 if (nfarray[i] < AR5416_CCA_MIN_BAD_VALUE ||
616 nfarray[i] > AR5416_CCA_MAX_HIGH_VALUE) {
617 h[i].invalidNFcount = AR512_NF_CAL_HIST_MAX;
618 } else {
619 h[i].invalidNFcount--;
620 h[i].privNF = nfarray[i];
621 }
622 } else {
623 h[i].privNF = ar5212GetNfHistMid(h[i].nfCalBuffer);
624 }
625 }
626}
627
628/*
629 * Read the NF and check it against the noise floor threshhold
630 */
631static int16_t
632ar5416GetNf(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan)
633{
634 int16_t nf, nfThresh;
635
636 if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
637 HALDEBUG(ah, HAL_DEBUG_ANY,
638 "%s: NF didn't complete in calibration window\n", __func__);
639 nf = 0;
640 } else {
641 /* Finished NF cal, check against threshold */
642 int16_t nfarray[NUM_NOISEFLOOR_READINGS] = { 0 };
643
644 /* TODO - enhance for multiple chains and ext ch */
645 ath_hal_getNoiseFloor(ah, nfarray);
646 nf = nfarray[0];
647 if (ar5416GetEepromNoiseFloorThresh(ah, chan, &nfThresh)) {
648 if (nf > nfThresh) {
649 HALDEBUG(ah, HAL_DEBUG_ANY,
650 "%s: noise floor failed detected; "
651 "detected %d, threshold %d\n", __func__,
652 nf, nfThresh);
653 /*
654 * NB: Don't discriminate 2.4 vs 5Ghz, if this
655 * happens it indicates a problem regardless
656 * of the band.
657 */
658 chan->channelFlags |= CHANNEL_CW_INT;
659 nf = 0;
660 }
661 } else {
662 nf = 0;
663 }
664 ar5416UpdateNFHistBuff(AH5416(ah)->ah_cal.nfCalHist, nfarray);
665 chan->rawNoiseFloor = nf;
666 }
667 return nf;
668}
669#endif /* AH_SUPPORT_AR5416 */