2 * Copyright (c) 2008 Atheros Communications Inc.
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.
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.
19 /* We can tune this as we go by monitoring really low values */
20 #define ATH9K_NF_TOO_LOW -60
22 /* AR5416 may return very high value (like -31 dBm), in those cases the nf
23 * is incorrect and we should use the static NF value. Later we can try to
24 * find out why they are reporting these values */
26 static bool ath9k_hw_nf_in_range(struct ath_hw
*ah
, s16 nf
)
28 if (nf
> ATH9K_NF_TOO_LOW
) {
29 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
30 "noise floor value detected (%d) is "
31 "lower than what we think is a "
32 "reasonable value (%d)\n",
33 nf
, ATH9K_NF_TOO_LOW
);
39 static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer
)
42 int16_t sort
[ATH9K_NF_CAL_HIST_MAX
];
45 for (i
= 0; i
< ATH9K_NF_CAL_HIST_MAX
; i
++)
46 sort
[i
] = nfCalBuffer
[i
];
48 for (i
= 0; i
< ATH9K_NF_CAL_HIST_MAX
- 1; i
++) {
49 for (j
= 1; j
< ATH9K_NF_CAL_HIST_MAX
- i
; j
++) {
50 if (sort
[j
] > sort
[j
- 1]) {
52 sort
[j
] = sort
[j
- 1];
57 nfval
= sort
[(ATH9K_NF_CAL_HIST_MAX
- 1) >> 1];
62 static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist
*h
,
67 for (i
= 0; i
< NUM_NF_READINGS
; i
++) {
68 h
[i
].nfCalBuffer
[h
[i
].currIndex
] = nfarray
[i
];
70 if (++h
[i
].currIndex
>= ATH9K_NF_CAL_HIST_MAX
)
73 if (h
[i
].invalidNFcount
> 0) {
74 if (nfarray
[i
] < AR_PHY_CCA_MIN_BAD_VALUE
||
75 nfarray
[i
] > AR_PHY_CCA_MAX_HIGH_VALUE
) {
76 h
[i
].invalidNFcount
= ATH9K_NF_CAL_HIST_MAX
;
78 h
[i
].invalidNFcount
--;
79 h
[i
].privNF
= nfarray
[i
];
83 ath9k_hw_get_nf_hist_mid(h
[i
].nfCalBuffer
);
89 static void ath9k_hw_do_getnf(struct ath_hw
*ah
,
90 int16_t nfarray
[NUM_NF_READINGS
])
94 if (AR_SREV_9280_10_OR_LATER(ah
))
95 nf
= MS(REG_READ(ah
, AR_PHY_CCA
), AR9280_PHY_MINCCA_PWR
);
97 nf
= MS(REG_READ(ah
, AR_PHY_CCA
), AR_PHY_MINCCA_PWR
);
100 nf
= 0 - ((nf
^ 0x1ff) + 1);
101 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
102 "NF calibrated [ctl] [chain 0] is %d\n", nf
);
105 if (!AR_SREV_9285(ah
)) {
106 if (AR_SREV_9280_10_OR_LATER(ah
))
107 nf
= MS(REG_READ(ah
, AR_PHY_CH1_CCA
),
108 AR9280_PHY_CH1_MINCCA_PWR
);
110 nf
= MS(REG_READ(ah
, AR_PHY_CH1_CCA
),
111 AR_PHY_CH1_MINCCA_PWR
);
114 nf
= 0 - ((nf
^ 0x1ff) + 1);
115 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
116 "NF calibrated [ctl] [chain 1] is %d\n", nf
);
119 if (!AR_SREV_9280(ah
)) {
120 nf
= MS(REG_READ(ah
, AR_PHY_CH2_CCA
),
121 AR_PHY_CH2_MINCCA_PWR
);
123 nf
= 0 - ((nf
^ 0x1ff) + 1);
124 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
125 "NF calibrated [ctl] [chain 2] is %d\n", nf
);
130 if (AR_SREV_9280_10_OR_LATER(ah
))
131 nf
= MS(REG_READ(ah
, AR_PHY_EXT_CCA
),
132 AR9280_PHY_EXT_MINCCA_PWR
);
134 nf
= MS(REG_READ(ah
, AR_PHY_EXT_CCA
),
135 AR_PHY_EXT_MINCCA_PWR
);
138 nf
= 0 - ((nf
^ 0x1ff) + 1);
139 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
140 "NF calibrated [ext] [chain 0] is %d\n", nf
);
143 if (!AR_SREV_9285(ah
)) {
144 if (AR_SREV_9280_10_OR_LATER(ah
))
145 nf
= MS(REG_READ(ah
, AR_PHY_CH1_EXT_CCA
),
146 AR9280_PHY_CH1_EXT_MINCCA_PWR
);
148 nf
= MS(REG_READ(ah
, AR_PHY_CH1_EXT_CCA
),
149 AR_PHY_CH1_EXT_MINCCA_PWR
);
152 nf
= 0 - ((nf
^ 0x1ff) + 1);
153 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
154 "NF calibrated [ext] [chain 1] is %d\n", nf
);
157 if (!AR_SREV_9280(ah
)) {
158 nf
= MS(REG_READ(ah
, AR_PHY_CH2_EXT_CCA
),
159 AR_PHY_CH2_EXT_MINCCA_PWR
);
161 nf
= 0 - ((nf
^ 0x1ff) + 1);
162 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
163 "NF calibrated [ext] [chain 2] is %d\n", nf
);
169 static bool getNoiseFloorThresh(struct ath_hw
*ah
,
170 enum ieee80211_band band
,
174 case IEEE80211_BAND_5GHZ
:
175 *nft
= (int8_t)ah
->eep_ops
->get_eeprom(ah
, EEP_NFTHRESH_5
);
177 case IEEE80211_BAND_2GHZ
:
178 *nft
= (int8_t)ah
->eep_ops
->get_eeprom(ah
, EEP_NFTHRESH_2
);
188 static void ath9k_hw_setup_calibration(struct ath_hw
*ah
,
189 struct hal_cal_list
*currCal
)
191 REG_RMW_FIELD(ah
, AR_PHY_TIMING_CTRL4(0),
192 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX
,
193 currCal
->calData
->calCountMax
);
195 switch (currCal
->calData
->calType
) {
196 case IQ_MISMATCH_CAL
:
197 REG_WRITE(ah
, AR_PHY_CALMODE
, AR_PHY_CALMODE_IQ
);
198 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
199 "starting IQ Mismatch Calibration\n");
202 REG_WRITE(ah
, AR_PHY_CALMODE
, AR_PHY_CALMODE_ADC_GAIN
);
203 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
204 "starting ADC Gain Calibration\n");
207 REG_WRITE(ah
, AR_PHY_CALMODE
, AR_PHY_CALMODE_ADC_DC_PER
);
208 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
209 "starting ADC DC Calibration\n");
211 case ADC_DC_INIT_CAL
:
212 REG_WRITE(ah
, AR_PHY_CALMODE
, AR_PHY_CALMODE_ADC_DC_INIT
);
213 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
214 "starting Init ADC DC Calibration\n");
218 REG_SET_BIT(ah
, AR_PHY_TIMING_CTRL4(0),
219 AR_PHY_TIMING_CTRL4_DO_CAL
);
222 static void ath9k_hw_reset_calibration(struct ath_hw
*ah
,
223 struct hal_cal_list
*currCal
)
227 ath9k_hw_setup_calibration(ah
, currCal
);
229 currCal
->calState
= CAL_RUNNING
;
231 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
232 ah
->meas0
.sign
[i
] = 0;
233 ah
->meas1
.sign
[i
] = 0;
234 ah
->meas2
.sign
[i
] = 0;
235 ah
->meas3
.sign
[i
] = 0;
241 static void ath9k_hw_per_calibration(struct ath_hw
*ah
,
242 struct ath9k_channel
*ichan
,
244 struct hal_cal_list
*currCal
,
249 if (currCal
->calState
== CAL_RUNNING
) {
250 if (!(REG_READ(ah
, AR_PHY_TIMING_CTRL4(0)) &
251 AR_PHY_TIMING_CTRL4_DO_CAL
)) {
253 currCal
->calData
->calCollect(ah
);
256 if (ah
->cal_samples
>= currCal
->calData
->calNumSamples
) {
257 int i
, numChains
= 0;
258 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
259 if (rxchainmask
& (1 << i
))
263 currCal
->calData
->calPostProc(ah
, numChains
);
264 ichan
->CalValid
|= currCal
->calData
->calType
;
265 currCal
->calState
= CAL_DONE
;
268 ath9k_hw_setup_calibration(ah
, currCal
);
271 } else if (!(ichan
->CalValid
& currCal
->calData
->calType
)) {
272 ath9k_hw_reset_calibration(ah
, currCal
);
276 /* Assumes you are talking about the currently configured channel */
277 static bool ath9k_hw_iscal_supported(struct ath_hw
*ah
,
278 enum hal_cal_types calType
)
280 struct ieee80211_conf
*conf
= &ah
->ah_sc
->hw
->conf
;
282 switch (calType
& ah
->supp_cals
) {
283 case IQ_MISMATCH_CAL
: /* Both 2 GHz and 5 GHz support OFDM */
287 if (conf
->channel
->band
== IEEE80211_BAND_5GHZ
&&
295 static void ath9k_hw_iqcal_collect(struct ath_hw
*ah
)
299 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
300 ah
->totalPowerMeasI
[i
] +=
301 REG_READ(ah
, AR_PHY_CAL_MEAS_0(i
));
302 ah
->totalPowerMeasQ
[i
] +=
303 REG_READ(ah
, AR_PHY_CAL_MEAS_1(i
));
304 ah
->totalIqCorrMeas
[i
] +=
305 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_2(i
));
306 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
307 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
308 ah
->cal_samples
, i
, ah
->totalPowerMeasI
[i
],
309 ah
->totalPowerMeasQ
[i
],
310 ah
->totalIqCorrMeas
[i
]);
314 static void ath9k_hw_adc_gaincal_collect(struct ath_hw
*ah
)
318 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
319 ah
->totalAdcIOddPhase
[i
] +=
320 REG_READ(ah
, AR_PHY_CAL_MEAS_0(i
));
321 ah
->totalAdcIEvenPhase
[i
] +=
322 REG_READ(ah
, AR_PHY_CAL_MEAS_1(i
));
323 ah
->totalAdcQOddPhase
[i
] +=
324 REG_READ(ah
, AR_PHY_CAL_MEAS_2(i
));
325 ah
->totalAdcQEvenPhase
[i
] +=
326 REG_READ(ah
, AR_PHY_CAL_MEAS_3(i
));
328 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
329 "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
330 "oddq=0x%08x; evenq=0x%08x;\n",
332 ah
->totalAdcIOddPhase
[i
],
333 ah
->totalAdcIEvenPhase
[i
],
334 ah
->totalAdcQOddPhase
[i
],
335 ah
->totalAdcQEvenPhase
[i
]);
339 static void ath9k_hw_adc_dccal_collect(struct ath_hw
*ah
)
343 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
344 ah
->totalAdcDcOffsetIOddPhase
[i
] +=
345 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_0(i
));
346 ah
->totalAdcDcOffsetIEvenPhase
[i
] +=
347 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_1(i
));
348 ah
->totalAdcDcOffsetQOddPhase
[i
] +=
349 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_2(i
));
350 ah
->totalAdcDcOffsetQEvenPhase
[i
] +=
351 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_3(i
));
353 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
354 "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
355 "oddq=0x%08x; evenq=0x%08x;\n",
357 ah
->totalAdcDcOffsetIOddPhase
[i
],
358 ah
->totalAdcDcOffsetIEvenPhase
[i
],
359 ah
->totalAdcDcOffsetQOddPhase
[i
],
360 ah
->totalAdcDcOffsetQEvenPhase
[i
]);
364 static void ath9k_hw_iqcalibrate(struct ath_hw
*ah
, u8 numChains
)
366 u32 powerMeasQ
, powerMeasI
, iqCorrMeas
;
367 u32 qCoffDenom
, iCoffDenom
;
368 int32_t qCoff
, iCoff
;
371 for (i
= 0; i
< numChains
; i
++) {
372 powerMeasI
= ah
->totalPowerMeasI
[i
];
373 powerMeasQ
= ah
->totalPowerMeasQ
[i
];
374 iqCorrMeas
= ah
->totalIqCorrMeas
[i
];
376 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
377 "Starting IQ Cal and Correction for Chain %d\n",
380 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
381 "Orignal: Chn %diq_corr_meas = 0x%08x\n",
382 i
, ah
->totalIqCorrMeas
[i
]);
386 if (iqCorrMeas
> 0x80000000) {
387 iqCorrMeas
= (0xffffffff - iqCorrMeas
) + 1;
391 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
392 "Chn %d pwr_meas_i = 0x%08x\n", i
, powerMeasI
);
393 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
394 "Chn %d pwr_meas_q = 0x%08x\n", i
, powerMeasQ
);
395 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
, "iqCorrNeg is 0x%08x\n",
398 iCoffDenom
= (powerMeasI
/ 2 + powerMeasQ
/ 2) / 128;
399 qCoffDenom
= powerMeasQ
/ 64;
401 if (powerMeasQ
!= 0) {
402 iCoff
= iqCorrMeas
/ iCoffDenom
;
403 qCoff
= powerMeasI
/ qCoffDenom
- 64;
404 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
405 "Chn %d iCoff = 0x%08x\n", i
, iCoff
);
406 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
407 "Chn %d qCoff = 0x%08x\n", i
, qCoff
);
409 iCoff
= iCoff
& 0x3f;
410 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
411 "New: Chn %d iCoff = 0x%08x\n", i
, iCoff
);
412 if (iqCorrNeg
== 0x0)
413 iCoff
= 0x40 - iCoff
;
417 else if (qCoff
<= -16)
420 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
421 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
424 REG_RMW_FIELD(ah
, AR_PHY_TIMING_CTRL4(i
),
425 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF
,
427 REG_RMW_FIELD(ah
, AR_PHY_TIMING_CTRL4(i
),
428 AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF
,
430 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
431 "IQ Cal and Correction done for Chain %d\n",
436 REG_SET_BIT(ah
, AR_PHY_TIMING_CTRL4(0),
437 AR_PHY_TIMING_CTRL4_IQCORR_ENABLE
);
440 static void ath9k_hw_adc_gaincal_calibrate(struct ath_hw
*ah
, u8 numChains
)
442 u32 iOddMeasOffset
, iEvenMeasOffset
, qOddMeasOffset
, qEvenMeasOffset
;
443 u32 qGainMismatch
, iGainMismatch
, val
, i
;
445 for (i
= 0; i
< numChains
; i
++) {
446 iOddMeasOffset
= ah
->totalAdcIOddPhase
[i
];
447 iEvenMeasOffset
= ah
->totalAdcIEvenPhase
[i
];
448 qOddMeasOffset
= ah
->totalAdcQOddPhase
[i
];
449 qEvenMeasOffset
= ah
->totalAdcQEvenPhase
[i
];
451 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
452 "Starting ADC Gain Cal for Chain %d\n", i
);
454 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
455 "Chn %d pwr_meas_odd_i = 0x%08x\n", i
,
457 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
458 "Chn %d pwr_meas_even_i = 0x%08x\n", i
,
460 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
461 "Chn %d pwr_meas_odd_q = 0x%08x\n", i
,
463 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
464 "Chn %d pwr_meas_even_q = 0x%08x\n", i
,
467 if (iOddMeasOffset
!= 0 && qEvenMeasOffset
!= 0) {
469 ((iEvenMeasOffset
* 32) /
470 iOddMeasOffset
) & 0x3f;
472 ((qOddMeasOffset
* 32) /
473 qEvenMeasOffset
) & 0x3f;
475 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
476 "Chn %d gain_mismatch_i = 0x%08x\n", i
,
478 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
479 "Chn %d gain_mismatch_q = 0x%08x\n", i
,
482 val
= REG_READ(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(i
));
484 val
|= (qGainMismatch
) | (iGainMismatch
<< 6);
485 REG_WRITE(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(i
), val
);
487 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
488 "ADC Gain Cal done for Chain %d\n", i
);
492 REG_WRITE(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
493 REG_READ(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
494 AR_PHY_NEW_ADC_GAIN_CORR_ENABLE
);
497 static void ath9k_hw_adc_dccal_calibrate(struct ath_hw
*ah
, u8 numChains
)
499 u32 iOddMeasOffset
, iEvenMeasOffset
, val
, i
;
500 int32_t qOddMeasOffset
, qEvenMeasOffset
, qDcMismatch
, iDcMismatch
;
501 const struct hal_percal_data
*calData
=
502 ah
->cal_list_curr
->calData
;
504 (1 << (calData
->calCountMax
+ 5)) * calData
->calNumSamples
;
506 for (i
= 0; i
< numChains
; i
++) {
507 iOddMeasOffset
= ah
->totalAdcDcOffsetIOddPhase
[i
];
508 iEvenMeasOffset
= ah
->totalAdcDcOffsetIEvenPhase
[i
];
509 qOddMeasOffset
= ah
->totalAdcDcOffsetQOddPhase
[i
];
510 qEvenMeasOffset
= ah
->totalAdcDcOffsetQEvenPhase
[i
];
512 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
513 "Starting ADC DC Offset Cal for Chain %d\n", i
);
515 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
516 "Chn %d pwr_meas_odd_i = %d\n", i
,
518 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
519 "Chn %d pwr_meas_even_i = %d\n", i
,
521 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
522 "Chn %d pwr_meas_odd_q = %d\n", i
,
524 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
525 "Chn %d pwr_meas_even_q = %d\n", i
,
528 iDcMismatch
= (((iEvenMeasOffset
- iOddMeasOffset
) * 2) /
530 qDcMismatch
= (((qOddMeasOffset
- qEvenMeasOffset
) * 2) /
533 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
534 "Chn %d dc_offset_mismatch_i = 0x%08x\n", i
,
536 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
537 "Chn %d dc_offset_mismatch_q = 0x%08x\n", i
,
540 val
= REG_READ(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(i
));
542 val
|= (qDcMismatch
<< 12) | (iDcMismatch
<< 21);
543 REG_WRITE(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(i
), val
);
545 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
546 "ADC DC Offset Cal done for Chain %d\n", i
);
549 REG_WRITE(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
550 REG_READ(ah
, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
551 AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE
);
554 /* This is done for the currently configured channel */
555 bool ath9k_hw_reset_calvalid(struct ath_hw
*ah
)
557 struct ieee80211_conf
*conf
= &ah
->ah_sc
->hw
->conf
;
558 struct hal_cal_list
*currCal
= ah
->cal_list_curr
;
563 if (!AR_SREV_9100(ah
) && !AR_SREV_9160_10_OR_LATER(ah
))
569 if (currCal
->calState
!= CAL_DONE
) {
570 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
571 "Calibration state incorrect, %d\n",
576 if (!ath9k_hw_iscal_supported(ah
, currCal
->calData
->calType
))
579 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
580 "Resetting Cal %d state for channel %u\n",
581 currCal
->calData
->calType
, conf
->channel
->center_freq
);
583 ah
->curchan
->CalValid
&= ~currCal
->calData
->calType
;
584 currCal
->calState
= CAL_WAITING
;
589 void ath9k_hw_start_nfcal(struct ath_hw
*ah
)
591 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
,
592 AR_PHY_AGC_CONTROL_ENABLE_NF
);
593 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
,
594 AR_PHY_AGC_CONTROL_NO_UPDATE_NF
);
595 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_NF
);
598 void ath9k_hw_loadnf(struct ath_hw
*ah
, struct ath9k_channel
*chan
)
600 struct ath9k_nfcal_hist
*h
;
603 const u32 ar5416_cca_regs
[6] = {
613 if (AR_SREV_9285(ah
))
615 else if (AR_SREV_9280(ah
))
622 for (i
= 0; i
< NUM_NF_READINGS
; i
++) {
623 if (chainmask
& (1 << i
)) {
624 val
= REG_READ(ah
, ar5416_cca_regs
[i
]);
626 val
|= (((u32
) (h
[i
].privNF
) << 1) & 0x1ff);
627 REG_WRITE(ah
, ar5416_cca_regs
[i
], val
);
631 REG_CLR_BIT(ah
, AR_PHY_AGC_CONTROL
,
632 AR_PHY_AGC_CONTROL_ENABLE_NF
);
633 REG_CLR_BIT(ah
, AR_PHY_AGC_CONTROL
,
634 AR_PHY_AGC_CONTROL_NO_UPDATE_NF
);
635 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_NF
);
637 for (j
= 0; j
< 1000; j
++) {
638 if ((REG_READ(ah
, AR_PHY_AGC_CONTROL
) &
639 AR_PHY_AGC_CONTROL_NF
) == 0)
644 for (i
= 0; i
< NUM_NF_READINGS
; i
++) {
645 if (chainmask
& (1 << i
)) {
646 val
= REG_READ(ah
, ar5416_cca_regs
[i
]);
648 val
|= (((u32
) (-50) << 1) & 0x1ff);
649 REG_WRITE(ah
, ar5416_cca_regs
[i
], val
);
654 int16_t ath9k_hw_getnf(struct ath_hw
*ah
,
655 struct ath9k_channel
*chan
)
657 int16_t nf
, nfThresh
;
658 int16_t nfarray
[NUM_NF_READINGS
] = { 0 };
659 struct ath9k_nfcal_hist
*h
;
660 struct ieee80211_channel
*c
= chan
->chan
;
662 chan
->channelFlags
&= (~CHANNEL_CW_INT
);
663 if (REG_READ(ah
, AR_PHY_AGC_CONTROL
) & AR_PHY_AGC_CONTROL_NF
) {
664 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
665 "NF did not complete in calibration window\n");
667 chan
->rawNoiseFloor
= nf
;
668 return chan
->rawNoiseFloor
;
670 ath9k_hw_do_getnf(ah
, nfarray
);
672 if (getNoiseFloorThresh(ah
, c
->band
, &nfThresh
)
674 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
675 "noise floor failed detected; "
676 "detected %d, threshold %d\n",
678 chan
->channelFlags
|= CHANNEL_CW_INT
;
684 ath9k_hw_update_nfcal_hist_buffer(h
, nfarray
);
685 chan
->rawNoiseFloor
= h
[0].privNF
;
687 return chan
->rawNoiseFloor
;
690 void ath9k_init_nfcal_hist_buffer(struct ath_hw
*ah
)
694 for (i
= 0; i
< NUM_NF_READINGS
; i
++) {
695 ah
->nfCalHist
[i
].currIndex
= 0;
696 ah
->nfCalHist
[i
].privNF
= AR_PHY_CCA_MAX_GOOD_VALUE
;
697 ah
->nfCalHist
[i
].invalidNFcount
=
698 AR_PHY_CCA_FILTERWINDOW_LENGTH
;
699 for (j
= 0; j
< ATH9K_NF_CAL_HIST_MAX
; j
++) {
700 ah
->nfCalHist
[i
].nfCalBuffer
[j
] =
701 AR_PHY_CCA_MAX_GOOD_VALUE
;
706 s16
ath9k_hw_getchan_noise(struct ath_hw
*ah
, struct ath9k_channel
*chan
)
710 if (chan
->rawNoiseFloor
== 0)
713 nf
= chan
->rawNoiseFloor
;
715 if (!ath9k_hw_nf_in_range(ah
, nf
))
716 nf
= ATH_DEFAULT_NOISE_FLOOR
;
721 static void ath9k_olc_temp_compensation(struct ath_hw
*ah
)
724 int delta
, currPDADC
, regval
;
726 rddata
= REG_READ(ah
, AR_PHY_TX_PWRCTRL4
);
728 currPDADC
= MS(rddata
, AR_PHY_TX_PWRCTRL_PD_AVG_OUT
);
730 if (ah
->eep_ops
->get_eeprom(ah
, EEP_DAC_HPWR_5G
))
731 delta
= (currPDADC
- ah
->initPDADC
+ 4) / 8;
733 delta
= (currPDADC
- ah
->initPDADC
+ 5) / 10;
735 if (delta
!= ah
->PDADCdelta
) {
736 ah
->PDADCdelta
= delta
;
737 for (i
= 1; i
< AR9280_TX_GAIN_TABLE_SIZE
; i
++) {
738 regval
= ah
->originalGain
[i
] - delta
;
742 REG_RMW_FIELD(ah
, AR_PHY_TX_GAIN_TBL1
+ i
* 4,
743 AR_PHY_TX_GAIN
, regval
);
748 bool ath9k_hw_calibrate(struct ath_hw
*ah
, struct ath9k_channel
*chan
,
749 u8 rxchainmask
, bool longcal
,
752 struct hal_cal_list
*currCal
= ah
->cal_list_curr
;
757 (currCal
->calState
== CAL_RUNNING
||
758 currCal
->calState
== CAL_WAITING
)) {
759 ath9k_hw_per_calibration(ah
, chan
, rxchainmask
, currCal
,
762 ah
->cal_list_curr
= currCal
= currCal
->calNext
;
764 if (currCal
->calState
== CAL_WAITING
) {
766 ath9k_hw_reset_calibration(ah
, currCal
);
772 if (OLC_FOR_AR9280_20_LATER
)
773 ath9k_olc_temp_compensation(ah
);
774 ath9k_hw_getnf(ah
, chan
);
775 ath9k_hw_loadnf(ah
, ah
->curchan
);
776 ath9k_hw_start_nfcal(ah
);
778 if (chan
->channelFlags
& CHANNEL_CW_INT
)
779 chan
->channelFlags
&= ~CHANNEL_CW_INT
;
785 static inline void ath9k_hw_9285_pa_cal(struct ath_hw
*ah
)
789 int i
, offset
, offs_6_1
, offs_0
;
790 u32 ccomp_org
, reg_field
;
801 if (AR_SREV_9285_11(ah
)) {
802 REG_WRITE(ah
, AR9285_AN_TOP4
, (AR9285_AN_TOP4_DEFAULT
| 0x14));
806 for (i
= 0; i
< ARRAY_SIZE(regList
); i
++)
807 regList
[i
][1] = REG_READ(ah
, regList
[i
][0]);
809 regVal
= REG_READ(ah
, 0x7834);
811 REG_WRITE(ah
, 0x7834, regVal
);
812 regVal
= REG_READ(ah
, 0x9808);
813 regVal
|= (0x1 << 27);
814 REG_WRITE(ah
, 0x9808, regVal
);
816 REG_RMW_FIELD(ah
, AR9285_AN_TOP3
, AR9285_AN_TOP3_PWDDAC
, 1);
817 REG_RMW_FIELD(ah
, AR9285_AN_RXTXBB1
, AR9285_AN_RXTXBB1_PDRXTXBB1
, 1);
818 REG_RMW_FIELD(ah
, AR9285_AN_RXTXBB1
, AR9285_AN_RXTXBB1_PDV2I
, 1);
819 REG_RMW_FIELD(ah
, AR9285_AN_RXTXBB1
, AR9285_AN_RXTXBB1_PDDACIF
, 1);
820 REG_RMW_FIELD(ah
, AR9285_AN_RF2G2
, AR9285_AN_RF2G2_OFFCAL
, 0);
821 REG_RMW_FIELD(ah
, AR9285_AN_RF2G7
, AR9285_AN_RF2G7_PWDDB
, 0);
822 REG_RMW_FIELD(ah
, AR9285_AN_RF2G1
, AR9285_AN_RF2G1_ENPACAL
, 0);
823 REG_RMW_FIELD(ah
, AR9285_AN_RF2G1
, AR9285_AN_RF2G1_PDPADRV1
, 1);
824 REG_RMW_FIELD(ah
, AR9285_AN_RF2G1
, AR9285_AN_RF2G1_PDPADRV2
, 0);
825 REG_RMW_FIELD(ah
, AR9285_AN_RF2G1
, AR9285_AN_RF2G1_PDPAOUT
, 0);
826 REG_RMW_FIELD(ah
, AR9285_AN_RF2G8
, AR9285_AN_RF2G8_PADRVGN2TAB0
, 7);
827 REG_RMW_FIELD(ah
, AR9285_AN_RF2G7
, AR9285_AN_RF2G7_PADRVGN2TAB0
, 0);
828 ccomp_org
= MS(REG_READ(ah
, AR9285_AN_RF2G6
), AR9285_AN_RF2G6_CCOMP
);
829 REG_RMW_FIELD(ah
, AR9285_AN_RF2G6
, AR9285_AN_RF2G6_CCOMP
, 7);
831 REG_WRITE(ah
, AR9285_AN_TOP2
, 0xca0358a0);
833 REG_RMW_FIELD(ah
, AR9285_AN_RF2G6
, AR9285_AN_RF2G6_OFFS
, 0);
834 REG_RMW_FIELD(ah
, AR9285_AN_RF2G3
, AR9285_AN_RF2G3_PDVCCOMP
, 0);
836 for (i
= 6; i
> 0; i
--) {
837 regVal
= REG_READ(ah
, 0x7834);
838 regVal
|= (1 << (19 + i
));
839 REG_WRITE(ah
, 0x7834, regVal
);
841 regVal
= REG_READ(ah
, 0x7834);
842 regVal
&= (~(0x1 << (19 + i
)));
843 reg_field
= MS(REG_READ(ah
, 0x7840), AR9285_AN_RXTXBB1_SPARE9
);
844 regVal
|= (reg_field
<< (19 + i
));
845 REG_WRITE(ah
, 0x7834, regVal
);
848 REG_RMW_FIELD(ah
, AR9285_AN_RF2G3
, AR9285_AN_RF2G3_PDVCCOMP
, 1);
850 reg_field
= MS(REG_READ(ah
, AR9285_AN_RF2G9
), AR9285_AN_RXTXBB1_SPARE9
);
851 REG_RMW_FIELD(ah
, AR9285_AN_RF2G3
, AR9285_AN_RF2G3_PDVCCOMP
, reg_field
);
852 offs_6_1
= MS(REG_READ(ah
, AR9285_AN_RF2G6
), AR9285_AN_RF2G6_OFFS
);
853 offs_0
= MS(REG_READ(ah
, AR9285_AN_RF2G3
), AR9285_AN_RF2G3_PDVCCOMP
);
855 offset
= (offs_6_1
<<1) | offs_0
;
857 offs_6_1
= offset
>>1;
860 REG_RMW_FIELD(ah
, AR9285_AN_RF2G6
, AR9285_AN_RF2G6_OFFS
, offs_6_1
);
861 REG_RMW_FIELD(ah
, AR9285_AN_RF2G3
, AR9285_AN_RF2G3_PDVCCOMP
, offs_0
);
863 regVal
= REG_READ(ah
, 0x7834);
865 REG_WRITE(ah
, 0x7834, regVal
);
866 regVal
= REG_READ(ah
, 0x9808);
867 regVal
&= (~(0x1 << 27));
868 REG_WRITE(ah
, 0x9808, regVal
);
870 for (i
= 0; i
< ARRAY_SIZE(regList
); i
++)
871 REG_WRITE(ah
, regList
[i
][0], regList
[i
][1]);
873 REG_RMW_FIELD(ah
, AR9285_AN_RF2G6
, AR9285_AN_RF2G6_CCOMP
, ccomp_org
);
875 if (AR_SREV_9285_11(ah
))
876 REG_WRITE(ah
, AR9285_AN_TOP4
, AR9285_AN_TOP4_DEFAULT
);
880 bool ath9k_hw_init_cal(struct ath_hw
*ah
,
881 struct ath9k_channel
*chan
)
883 if (AR_SREV_9280_10_OR_LATER(ah
)) {
884 REG_CLR_BIT(ah
, AR_PHY_ADC_CTL
, AR_PHY_ADC_CTL_OFF_PWDADC
);
885 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_FLTR_CAL
);
886 REG_CLR_BIT(ah
, AR_PHY_CL_CAL_CTL
, AR_PHY_CL_CAL_ENABLE
);
888 /* Kick off the cal */
889 REG_WRITE(ah
, AR_PHY_AGC_CONTROL
,
890 REG_READ(ah
, AR_PHY_AGC_CONTROL
) |
891 AR_PHY_AGC_CONTROL_CAL
);
893 if (!ath9k_hw_wait(ah
, AR_PHY_AGC_CONTROL
,
894 AR_PHY_AGC_CONTROL_CAL
, 0,
896 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
897 "offset calibration failed to complete in 1ms; "
898 "noisy environment?\n");
902 REG_CLR_BIT(ah
, AR_PHY_ADC_CTL
, AR_PHY_ADC_CTL_OFF_PWDADC
);
903 REG_SET_BIT(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_FLTR_CAL
);
904 REG_SET_BIT(ah
, AR_PHY_CL_CAL_CTL
, AR_PHY_CL_CAL_ENABLE
);
907 /* Calibrate the AGC */
908 REG_WRITE(ah
, AR_PHY_AGC_CONTROL
,
909 REG_READ(ah
, AR_PHY_AGC_CONTROL
) |
910 AR_PHY_AGC_CONTROL_CAL
);
912 if (!ath9k_hw_wait(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_CAL
,
913 0, AH_WAIT_TIMEOUT
)) {
914 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
915 "offset calibration failed to complete in 1ms; "
916 "noisy environment?\n");
920 if (AR_SREV_9280_10_OR_LATER(ah
)) {
921 REG_SET_BIT(ah
, AR_PHY_ADC_CTL
, AR_PHY_ADC_CTL_OFF_PWDADC
);
922 REG_CLR_BIT(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_FLTR_CAL
);
925 /* Do PA Calibration */
926 if (AR_SREV_9285(ah
) && AR_SREV_9285_11_OR_LATER(ah
))
927 ath9k_hw_9285_pa_cal(ah
);
929 /* Do NF Calibration */
930 REG_WRITE(ah
, AR_PHY_AGC_CONTROL
,
931 REG_READ(ah
, AR_PHY_AGC_CONTROL
) |
932 AR_PHY_AGC_CONTROL_NF
);
934 ah
->cal_list
= ah
->cal_list_last
= ah
->cal_list_curr
= NULL
;
936 if (AR_SREV_9100(ah
) || AR_SREV_9160_10_OR_LATER(ah
)) {
937 if (ath9k_hw_iscal_supported(ah
, ADC_GAIN_CAL
)) {
938 INIT_CAL(&ah
->adcgain_caldata
);
939 INSERT_CAL(ah
, &ah
->adcgain_caldata
);
940 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
941 "enabling ADC Gain Calibration.\n");
943 if (ath9k_hw_iscal_supported(ah
, ADC_DC_CAL
)) {
944 INIT_CAL(&ah
->adcdc_caldata
);
945 INSERT_CAL(ah
, &ah
->adcdc_caldata
);
946 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
947 "enabling ADC DC Calibration.\n");
949 if (ath9k_hw_iscal_supported(ah
, IQ_MISMATCH_CAL
)) {
950 INIT_CAL(&ah
->iq_caldata
);
951 INSERT_CAL(ah
, &ah
->iq_caldata
);
952 DPRINTF(ah
->ah_sc
, ATH_DBG_CALIBRATE
,
953 "enabling IQ Calibration.\n");
956 ah
->cal_list_curr
= ah
->cal_list
;
958 if (ah
->cal_list_curr
)
959 ath9k_hw_reset_calibration(ah
, ah
->cal_list_curr
);
967 const struct hal_percal_data iq_cal_multi_sample
= {
971 ath9k_hw_iqcal_collect
,
974 const struct hal_percal_data iq_cal_single_sample
= {
978 ath9k_hw_iqcal_collect
,
981 const struct hal_percal_data adc_gain_cal_multi_sample
= {
985 ath9k_hw_adc_gaincal_collect
,
986 ath9k_hw_adc_gaincal_calibrate
988 const struct hal_percal_data adc_gain_cal_single_sample
= {
992 ath9k_hw_adc_gaincal_collect
,
993 ath9k_hw_adc_gaincal_calibrate
995 const struct hal_percal_data adc_dc_cal_multi_sample
= {
999 ath9k_hw_adc_dccal_collect
,
1000 ath9k_hw_adc_dccal_calibrate
1002 const struct hal_percal_data adc_dc_cal_single_sample
= {
1006 ath9k_hw_adc_dccal_collect
,
1007 ath9k_hw_adc_dccal_calibrate
1009 const struct hal_percal_data adc_init_dc_cal
= {
1013 ath9k_hw_adc_dccal_collect
,
1014 ath9k_hw_adc_dccal_calibrate