blob: 9c46b54d2a988c89f6588699015244e61f955942 [file] [log] [blame]
Sujithf1dc5602008-10-29 10:16:30 +05301/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 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"
Sujithf1dc5602008-10-29 10:16:30 +053018
Sujithf1dc5602008-10-29 10:16:30 +053019/* We can tune this as we go by monitoring really low values */
20#define ATH9K_NF_TOO_LOW -60
21
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 */
25
Sujithcbe61d82009-02-09 13:27:12 +053026static bool ath9k_hw_nf_in_range(struct ath_hw *ah, s16 nf)
Sujithf1dc5602008-10-29 10:16:30 +053027{
28 if (nf > ATH9K_NF_TOO_LOW) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -070029 ath_print(ath9k_hw_common(ah), 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);
Sujithf1dc5602008-10-29 10:16:30 +053034 return false;
35 }
36 return true;
37}
38
39static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
40{
41 int16_t nfval;
42 int16_t sort[ATH9K_NF_CAL_HIST_MAX];
43 int i, j;
44
45 for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
46 sort[i] = nfCalBuffer[i];
47
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]) {
51 nfval = sort[j];
52 sort[j] = sort[j - 1];
53 sort[j - 1] = nfval;
54 }
55 }
56 }
57 nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
58
59 return nfval;
60}
61
62static void ath9k_hw_update_nfcal_hist_buffer(struct ath9k_nfcal_hist *h,
63 int16_t *nfarray)
64{
65 int i;
66
67 for (i = 0; i < NUM_NF_READINGS; i++) {
68 h[i].nfCalBuffer[h[i].currIndex] = nfarray[i];
69
70 if (++h[i].currIndex >= ATH9K_NF_CAL_HIST_MAX)
71 h[i].currIndex = 0;
72
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;
77 } else {
78 h[i].invalidNFcount--;
79 h[i].privNF = nfarray[i];
80 }
81 } else {
82 h[i].privNF =
83 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
84 }
85 }
86 return;
87}
88
Sujithcbe61d82009-02-09 13:27:12 +053089static void ath9k_hw_do_getnf(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +053090 int16_t nfarray[NUM_NF_READINGS])
91{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -070092 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +053093 int16_t nf;
94
95 if (AR_SREV_9280_10_OR_LATER(ah))
96 nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
97 else
98 nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
99
100 if (nf & 0x100)
101 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700102 ath_print(common, ATH_DBG_CALIBRATE,
103 "NF calibrated [ctl] [chain 0] is %d\n", nf);
Sujithf1dc5602008-10-29 10:16:30 +0530104 nfarray[0] = nf;
105
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530106 if (!AR_SREV_9285(ah)) {
107 if (AR_SREV_9280_10_OR_LATER(ah))
108 nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
109 AR9280_PHY_CH1_MINCCA_PWR);
110 else
111 nf = MS(REG_READ(ah, AR_PHY_CH1_CCA),
112 AR_PHY_CH1_MINCCA_PWR);
Sujithf1dc5602008-10-29 10:16:30 +0530113
Sujithf1dc5602008-10-29 10:16:30 +0530114 if (nf & 0x100)
115 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700116 ath_print(common, ATH_DBG_CALIBRATE,
117 "NF calibrated [ctl] [chain 1] is %d\n", nf);
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530118 nfarray[1] = nf;
119
Vivek Natarajanac88b6e2009-07-23 10:59:57 +0530120 if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530121 nf = MS(REG_READ(ah, AR_PHY_CH2_CCA),
122 AR_PHY_CH2_MINCCA_PWR);
123 if (nf & 0x100)
124 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700125 ath_print(common, ATH_DBG_CALIBRATE,
126 "NF calibrated [ctl] [chain 2] is %d\n", nf);
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530127 nfarray[2] = nf;
128 }
Sujithf1dc5602008-10-29 10:16:30 +0530129 }
130
131 if (AR_SREV_9280_10_OR_LATER(ah))
132 nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
133 AR9280_PHY_EXT_MINCCA_PWR);
134 else
135 nf = MS(REG_READ(ah, AR_PHY_EXT_CCA),
136 AR_PHY_EXT_MINCCA_PWR);
137
138 if (nf & 0x100)
139 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700140 ath_print(common, ATH_DBG_CALIBRATE,
141 "NF calibrated [ext] [chain 0] is %d\n", nf);
Sujithf1dc5602008-10-29 10:16:30 +0530142 nfarray[3] = nf;
143
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530144 if (!AR_SREV_9285(ah)) {
145 if (AR_SREV_9280_10_OR_LATER(ah))
146 nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
147 AR9280_PHY_CH1_EXT_MINCCA_PWR);
148 else
149 nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA),
150 AR_PHY_CH1_EXT_MINCCA_PWR);
Sujithf1dc5602008-10-29 10:16:30 +0530151
Sujithf1dc5602008-10-29 10:16:30 +0530152 if (nf & 0x100)
153 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700154 ath_print(common, ATH_DBG_CALIBRATE,
155 "NF calibrated [ext] [chain 1] is %d\n", nf);
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530156 nfarray[4] = nf;
157
Vivek Natarajanac88b6e2009-07-23 10:59:57 +0530158 if (!AR_SREV_9280(ah) && !AR_SREV_9287(ah)) {
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530159 nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA),
160 AR_PHY_CH2_EXT_MINCCA_PWR);
161 if (nf & 0x100)
162 nf = 0 - ((nf ^ 0x1ff) + 1);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700163 ath_print(common, ATH_DBG_CALIBRATE,
164 "NF calibrated [ext] [chain 2] is %d\n", nf);
Senthil Balasubramanian793c5922009-01-26 20:28:14 +0530165 nfarray[5] = nf;
166 }
Sujithf1dc5602008-10-29 10:16:30 +0530167 }
168}
169
Sujithcbe61d82009-02-09 13:27:12 +0530170static bool getNoiseFloorThresh(struct ath_hw *ah,
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800171 enum ieee80211_band band,
Sujithf1dc5602008-10-29 10:16:30 +0530172 int16_t *nft)
173{
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800174 switch (band) {
175 case IEEE80211_BAND_5GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530176 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_5);
Sujithf1dc5602008-10-29 10:16:30 +0530177 break;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800178 case IEEE80211_BAND_2GHZ:
Sujithf74df6f2009-02-09 13:27:24 +0530179 *nft = (int8_t)ah->eep_ops->get_eeprom(ah, EEP_NFTHRESH_2);
Sujithf1dc5602008-10-29 10:16:30 +0530180 break;
181 default:
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800182 BUG_ON(1);
Sujithf1dc5602008-10-29 10:16:30 +0530183 return false;
184 }
185
186 return true;
187}
188
Sujithcbe61d82009-02-09 13:27:12 +0530189static void ath9k_hw_setup_calibration(struct ath_hw *ah,
Sujithcbfe9462009-04-13 21:56:56 +0530190 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530191{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700192 struct ath_common *common = ath9k_hw_common(ah);
193
Sujithf1dc5602008-10-29 10:16:30 +0530194 REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
195 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
196 currCal->calData->calCountMax);
197
198 switch (currCal->calData->calType) {
199 case IQ_MISMATCH_CAL:
200 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700201 ath_print(common, ATH_DBG_CALIBRATE,
202 "starting IQ Mismatch Calibration\n");
Sujithf1dc5602008-10-29 10:16:30 +0530203 break;
204 case ADC_GAIN_CAL:
205 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700206 ath_print(common, ATH_DBG_CALIBRATE,
207 "starting ADC Gain Calibration\n");
Sujithf1dc5602008-10-29 10:16:30 +0530208 break;
209 case ADC_DC_CAL:
210 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700211 ath_print(common, ATH_DBG_CALIBRATE,
212 "starting ADC DC Calibration\n");
Sujithf1dc5602008-10-29 10:16:30 +0530213 break;
214 case ADC_DC_INIT_CAL:
215 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_INIT);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700216 ath_print(common, ATH_DBG_CALIBRATE,
217 "starting Init ADC DC Calibration\n");
Sujithf1dc5602008-10-29 10:16:30 +0530218 break;
219 }
220
221 REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
222 AR_PHY_TIMING_CTRL4_DO_CAL);
223}
224
Sujithcbe61d82009-02-09 13:27:12 +0530225static void ath9k_hw_reset_calibration(struct ath_hw *ah,
Sujithcbfe9462009-04-13 21:56:56 +0530226 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530227{
Sujithf1dc5602008-10-29 10:16:30 +0530228 int i;
229
230 ath9k_hw_setup_calibration(ah, currCal);
231
232 currCal->calState = CAL_RUNNING;
233
234 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530235 ah->meas0.sign[i] = 0;
236 ah->meas1.sign[i] = 0;
237 ah->meas2.sign[i] = 0;
238 ah->meas3.sign[i] = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530239 }
240
Sujith2660b812009-02-09 13:27:26 +0530241 ah->cal_samples = 0;
Sujithf1dc5602008-10-29 10:16:30 +0530242}
243
Sujith379f0442009-04-13 21:56:48 +0530244static bool ath9k_hw_per_calibration(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530245 struct ath9k_channel *ichan,
246 u8 rxchainmask,
Sujithcbfe9462009-04-13 21:56:56 +0530247 struct ath9k_cal_list *currCal)
Sujithf1dc5602008-10-29 10:16:30 +0530248{
Sujith379f0442009-04-13 21:56:48 +0530249 bool iscaldone = false;
Sujithf1dc5602008-10-29 10:16:30 +0530250
251 if (currCal->calState == CAL_RUNNING) {
252 if (!(REG_READ(ah, AR_PHY_TIMING_CTRL4(0)) &
253 AR_PHY_TIMING_CTRL4_DO_CAL)) {
254
255 currCal->calData->calCollect(ah);
Sujith2660b812009-02-09 13:27:26 +0530256 ah->cal_samples++;
Sujithf1dc5602008-10-29 10:16:30 +0530257
Sujith2660b812009-02-09 13:27:26 +0530258 if (ah->cal_samples >= currCal->calData->calNumSamples) {
Sujithf1dc5602008-10-29 10:16:30 +0530259 int i, numChains = 0;
260 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
261 if (rxchainmask & (1 << i))
262 numChains++;
263 }
264
265 currCal->calData->calPostProc(ah, numChains);
266 ichan->CalValid |= currCal->calData->calType;
267 currCal->calState = CAL_DONE;
Sujith379f0442009-04-13 21:56:48 +0530268 iscaldone = true;
Sujithf1dc5602008-10-29 10:16:30 +0530269 } else {
270 ath9k_hw_setup_calibration(ah, currCal);
271 }
272 }
273 } else if (!(ichan->CalValid & currCal->calData->calType)) {
274 ath9k_hw_reset_calibration(ah, currCal);
275 }
Sujith379f0442009-04-13 21:56:48 +0530276
277 return iscaldone;
Sujithf1dc5602008-10-29 10:16:30 +0530278}
279
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800280/* Assumes you are talking about the currently configured channel */
Sujithcbe61d82009-02-09 13:27:12 +0530281static bool ath9k_hw_iscal_supported(struct ath_hw *ah,
Sujithcbfe9462009-04-13 21:56:56 +0530282 enum ath9k_cal_types calType)
Sujithf1dc5602008-10-29 10:16:30 +0530283{
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -0700284 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Sujithf1dc5602008-10-29 10:16:30 +0530285
Sujith2660b812009-02-09 13:27:26 +0530286 switch (calType & ah->supp_cals) {
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800287 case IQ_MISMATCH_CAL: /* Both 2 GHz and 5 GHz support OFDM */
288 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530289 case ADC_GAIN_CAL:
290 case ADC_DC_CAL:
Sujitha451aa62009-04-13 21:56:43 +0530291 if (!(conf->channel->band == IEEE80211_BAND_2GHZ &&
292 conf_is_ht20(conf)))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800293 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530294 break;
295 }
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800296 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530297}
298
Sujithcbe61d82009-02-09 13:27:12 +0530299static void ath9k_hw_iqcal_collect(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530300{
Sujithf1dc5602008-10-29 10:16:30 +0530301 int i;
302
303 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530304 ah->totalPowerMeasI[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530305 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
Sujith2660b812009-02-09 13:27:26 +0530306 ah->totalPowerMeasQ[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530307 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
Sujith2660b812009-02-09 13:27:26 +0530308 ah->totalIqCorrMeas[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530309 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700310 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
311 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
312 ah->cal_samples, i, ah->totalPowerMeasI[i],
313 ah->totalPowerMeasQ[i],
314 ah->totalIqCorrMeas[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530315 }
316}
317
Sujithcbe61d82009-02-09 13:27:12 +0530318static void ath9k_hw_adc_gaincal_collect(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530319{
Sujithf1dc5602008-10-29 10:16:30 +0530320 int i;
321
322 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530323 ah->totalAdcIOddPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530324 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
Sujith2660b812009-02-09 13:27:26 +0530325 ah->totalAdcIEvenPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530326 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
Sujith2660b812009-02-09 13:27:26 +0530327 ah->totalAdcQOddPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530328 REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
Sujith2660b812009-02-09 13:27:26 +0530329 ah->totalAdcQEvenPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530330 REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
331
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700332 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
333 "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
334 "oddq=0x%08x; evenq=0x%08x;\n",
335 ah->cal_samples, i,
336 ah->totalAdcIOddPhase[i],
337 ah->totalAdcIEvenPhase[i],
338 ah->totalAdcQOddPhase[i],
339 ah->totalAdcQEvenPhase[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530340 }
341}
342
Sujithcbe61d82009-02-09 13:27:12 +0530343static void ath9k_hw_adc_dccal_collect(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530344{
Sujithf1dc5602008-10-29 10:16:30 +0530345 int i;
346
347 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530348 ah->totalAdcDcOffsetIOddPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530349 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
Sujith2660b812009-02-09 13:27:26 +0530350 ah->totalAdcDcOffsetIEvenPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530351 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
Sujith2660b812009-02-09 13:27:26 +0530352 ah->totalAdcDcOffsetQOddPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530353 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
Sujith2660b812009-02-09 13:27:26 +0530354 ah->totalAdcDcOffsetQEvenPhase[i] +=
Sujithf1dc5602008-10-29 10:16:30 +0530355 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
356
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700357 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
358 "%d: Chn %d oddi=0x%08x; eveni=0x%08x; "
359 "oddq=0x%08x; evenq=0x%08x;\n",
360 ah->cal_samples, i,
361 ah->totalAdcDcOffsetIOddPhase[i],
362 ah->totalAdcDcOffsetIEvenPhase[i],
363 ah->totalAdcDcOffsetQOddPhase[i],
364 ah->totalAdcDcOffsetQEvenPhase[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530365 }
366}
367
Sujithcbe61d82009-02-09 13:27:12 +0530368static void ath9k_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
Sujithf1dc5602008-10-29 10:16:30 +0530369{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700370 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530371 u32 powerMeasQ, powerMeasI, iqCorrMeas;
372 u32 qCoffDenom, iCoffDenom;
373 int32_t qCoff, iCoff;
374 int iqCorrNeg, i;
375
376 for (i = 0; i < numChains; i++) {
Sujith2660b812009-02-09 13:27:26 +0530377 powerMeasI = ah->totalPowerMeasI[i];
378 powerMeasQ = ah->totalPowerMeasQ[i];
379 iqCorrMeas = ah->totalIqCorrMeas[i];
Sujithf1dc5602008-10-29 10:16:30 +0530380
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700381 ath_print(common, ATH_DBG_CALIBRATE,
382 "Starting IQ Cal and Correction for Chain %d\n",
383 i);
Sujithf1dc5602008-10-29 10:16:30 +0530384
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700385 ath_print(common, ATH_DBG_CALIBRATE,
386 "Orignal: Chn %diq_corr_meas = 0x%08x\n",
387 i, ah->totalIqCorrMeas[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530388
389 iqCorrNeg = 0;
390
391 if (iqCorrMeas > 0x80000000) {
392 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
393 iqCorrNeg = 1;
394 }
395
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700396 ath_print(common, ATH_DBG_CALIBRATE,
397 "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
398 ath_print(common, ATH_DBG_CALIBRATE,
399 "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
400 ath_print(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
401 iqCorrNeg);
Sujithf1dc5602008-10-29 10:16:30 +0530402
403 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
404 qCoffDenom = powerMeasQ / 64;
405
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530406 if ((powerMeasQ != 0) && (iCoffDenom != 0) &&
407 (qCoffDenom != 0)) {
Sujithf1dc5602008-10-29 10:16:30 +0530408 iCoff = iqCorrMeas / iCoffDenom;
409 qCoff = powerMeasI / qCoffDenom - 64;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700410 ath_print(common, ATH_DBG_CALIBRATE,
411 "Chn %d iCoff = 0x%08x\n", i, iCoff);
412 ath_print(common, ATH_DBG_CALIBRATE,
413 "Chn %d qCoff = 0x%08x\n", i, qCoff);
Sujithf1dc5602008-10-29 10:16:30 +0530414
415 iCoff = iCoff & 0x3f;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700416 ath_print(common, ATH_DBG_CALIBRATE,
417 "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
Sujithf1dc5602008-10-29 10:16:30 +0530418 if (iqCorrNeg == 0x0)
419 iCoff = 0x40 - iCoff;
420
421 if (qCoff > 15)
422 qCoff = 15;
423 else if (qCoff <= -16)
424 qCoff = 16;
425
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700426 ath_print(common, ATH_DBG_CALIBRATE,
427 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
428 i, iCoff, qCoff);
Sujithf1dc5602008-10-29 10:16:30 +0530429
430 REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
431 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
432 iCoff);
433 REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
434 AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
435 qCoff);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700436 ath_print(common, ATH_DBG_CALIBRATE,
437 "IQ Cal and Correction done for Chain %d\n",
438 i);
Sujithf1dc5602008-10-29 10:16:30 +0530439 }
440 }
441
442 REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
443 AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
444}
445
Sujithcbe61d82009-02-09 13:27:12 +0530446static void ath9k_hw_adc_gaincal_calibrate(struct ath_hw *ah, u8 numChains)
Sujithf1dc5602008-10-29 10:16:30 +0530447{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700448 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530449 u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset, qEvenMeasOffset;
450 u32 qGainMismatch, iGainMismatch, val, i;
451
452 for (i = 0; i < numChains; i++) {
Sujith2660b812009-02-09 13:27:26 +0530453 iOddMeasOffset = ah->totalAdcIOddPhase[i];
454 iEvenMeasOffset = ah->totalAdcIEvenPhase[i];
455 qOddMeasOffset = ah->totalAdcQOddPhase[i];
456 qEvenMeasOffset = ah->totalAdcQEvenPhase[i];
Sujithf1dc5602008-10-29 10:16:30 +0530457
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700458 ath_print(common, ATH_DBG_CALIBRATE,
459 "Starting ADC Gain Cal for Chain %d\n", i);
Sujithf1dc5602008-10-29 10:16:30 +0530460
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700461 ath_print(common, ATH_DBG_CALIBRATE,
462 "Chn %d pwr_meas_odd_i = 0x%08x\n", i,
463 iOddMeasOffset);
464 ath_print(common, ATH_DBG_CALIBRATE,
465 "Chn %d pwr_meas_even_i = 0x%08x\n", i,
466 iEvenMeasOffset);
467 ath_print(common, ATH_DBG_CALIBRATE,
468 "Chn %d pwr_meas_odd_q = 0x%08x\n", i,
469 qOddMeasOffset);
470 ath_print(common, ATH_DBG_CALIBRATE,
471 "Chn %d pwr_meas_even_q = 0x%08x\n", i,
472 qEvenMeasOffset);
Sujithf1dc5602008-10-29 10:16:30 +0530473
474 if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
475 iGainMismatch =
476 ((iEvenMeasOffset * 32) /
477 iOddMeasOffset) & 0x3f;
478 qGainMismatch =
479 ((qOddMeasOffset * 32) /
480 qEvenMeasOffset) & 0x3f;
481
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700482 ath_print(common, ATH_DBG_CALIBRATE,
483 "Chn %d gain_mismatch_i = 0x%08x\n", i,
484 iGainMismatch);
485 ath_print(common, ATH_DBG_CALIBRATE,
486 "Chn %d gain_mismatch_q = 0x%08x\n", i,
487 qGainMismatch);
Sujithf1dc5602008-10-29 10:16:30 +0530488
489 val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
490 val &= 0xfffff000;
491 val |= (qGainMismatch) | (iGainMismatch << 6);
492 REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
493
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700494 ath_print(common, ATH_DBG_CALIBRATE,
495 "ADC Gain Cal done for Chain %d\n", i);
Sujithf1dc5602008-10-29 10:16:30 +0530496 }
497 }
498
499 REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
500 REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
501 AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
502}
503
Sujithcbe61d82009-02-09 13:27:12 +0530504static void ath9k_hw_adc_dccal_calibrate(struct ath_hw *ah, u8 numChains)
Sujithf1dc5602008-10-29 10:16:30 +0530505{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700506 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530507 u32 iOddMeasOffset, iEvenMeasOffset, val, i;
508 int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
Sujithcbfe9462009-04-13 21:56:56 +0530509 const struct ath9k_percal_data *calData =
Sujith2660b812009-02-09 13:27:26 +0530510 ah->cal_list_curr->calData;
Sujithf1dc5602008-10-29 10:16:30 +0530511 u32 numSamples =
512 (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
513
514 for (i = 0; i < numChains; i++) {
Sujith2660b812009-02-09 13:27:26 +0530515 iOddMeasOffset = ah->totalAdcDcOffsetIOddPhase[i];
516 iEvenMeasOffset = ah->totalAdcDcOffsetIEvenPhase[i];
517 qOddMeasOffset = ah->totalAdcDcOffsetQOddPhase[i];
518 qEvenMeasOffset = ah->totalAdcDcOffsetQEvenPhase[i];
Sujithf1dc5602008-10-29 10:16:30 +0530519
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700520 ath_print(common, ATH_DBG_CALIBRATE,
521 "Starting ADC DC Offset Cal for Chain %d\n", i);
Sujithf1dc5602008-10-29 10:16:30 +0530522
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700523 ath_print(common, ATH_DBG_CALIBRATE,
524 "Chn %d pwr_meas_odd_i = %d\n", i,
525 iOddMeasOffset);
526 ath_print(common, ATH_DBG_CALIBRATE,
527 "Chn %d pwr_meas_even_i = %d\n", i,
528 iEvenMeasOffset);
529 ath_print(common, ATH_DBG_CALIBRATE,
530 "Chn %d pwr_meas_odd_q = %d\n", i,
531 qOddMeasOffset);
532 ath_print(common, ATH_DBG_CALIBRATE,
533 "Chn %d pwr_meas_even_q = %d\n", i,
534 qEvenMeasOffset);
Sujithf1dc5602008-10-29 10:16:30 +0530535
536 iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
537 numSamples) & 0x1ff;
538 qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
539 numSamples) & 0x1ff;
540
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700541 ath_print(common, ATH_DBG_CALIBRATE,
542 "Chn %d dc_offset_mismatch_i = 0x%08x\n", i,
543 iDcMismatch);
544 ath_print(common, ATH_DBG_CALIBRATE,
545 "Chn %d dc_offset_mismatch_q = 0x%08x\n", i,
546 qDcMismatch);
Sujithf1dc5602008-10-29 10:16:30 +0530547
548 val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
549 val &= 0xc0000fff;
550 val |= (qDcMismatch << 12) | (iDcMismatch << 21);
551 REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
552
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700553 ath_print(common, ATH_DBG_CALIBRATE,
554 "ADC DC Offset Cal done for Chain %d\n", i);
Sujithf1dc5602008-10-29 10:16:30 +0530555 }
556
557 REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
558 REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
559 AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
560}
561
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800562/* This is done for the currently configured channel */
Sujithcbe61d82009-02-09 13:27:12 +0530563bool ath9k_hw_reset_calvalid(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530564{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700565 struct ath_common *common = ath9k_hw_common(ah);
566 struct ieee80211_conf *conf = &common->hw->conf;
Sujithcbfe9462009-04-13 21:56:56 +0530567 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Sujithf1dc5602008-10-29 10:16:30 +0530568
Sujith2660b812009-02-09 13:27:26 +0530569 if (!ah->curchan)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800570 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530571
572 if (!AR_SREV_9100(ah) && !AR_SREV_9160_10_OR_LATER(ah))
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800573 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530574
575 if (currCal == NULL)
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800576 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530577
578 if (currCal->calState != CAL_DONE) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700579 ath_print(common, ATH_DBG_CALIBRATE,
580 "Calibration state incorrect, %d\n",
581 currCal->calState);
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800582 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530583 }
584
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800585 if (!ath9k_hw_iscal_supported(ah, currCal->calData->calType))
586 return true;
Sujithf1dc5602008-10-29 10:16:30 +0530587
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700588 ath_print(common, ATH_DBG_CALIBRATE,
589 "Resetting Cal %d state for channel %u\n",
590 currCal->calData->calType, conf->channel->center_freq);
Sujithf1dc5602008-10-29 10:16:30 +0530591
Sujith2660b812009-02-09 13:27:26 +0530592 ah->curchan->CalValid &= ~currCal->calData->calType;
Sujithf1dc5602008-10-29 10:16:30 +0530593 currCal->calState = CAL_WAITING;
594
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -0800595 return false;
Sujithf1dc5602008-10-29 10:16:30 +0530596}
597
Sujithcbe61d82009-02-09 13:27:12 +0530598void ath9k_hw_start_nfcal(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530599{
600 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
601 AR_PHY_AGC_CONTROL_ENABLE_NF);
602 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
603 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
604 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
605}
606
Sujithcbe61d82009-02-09 13:27:12 +0530607void ath9k_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530608{
609 struct ath9k_nfcal_hist *h;
610 int i, j;
611 int32_t val;
612 const u32 ar5416_cca_regs[6] = {
613 AR_PHY_CCA,
614 AR_PHY_CH1_CCA,
615 AR_PHY_CH2_CCA,
616 AR_PHY_EXT_CCA,
617 AR_PHY_CH1_EXT_CCA,
618 AR_PHY_CH2_EXT_CCA
619 };
Senthil Balasubramaniance143bb2009-09-17 09:27:33 +0530620 u8 chainmask, rx_chain_status;
Sujithf1dc5602008-10-29 10:16:30 +0530621
Senthil Balasubramaniance143bb2009-09-17 09:27:33 +0530622 rx_chain_status = REG_READ(ah, AR_PHY_RX_CHAINMASK);
Sujith5dad40c2009-01-23 11:20:55 +0530623 if (AR_SREV_9285(ah))
624 chainmask = 0x9;
Senthil Balasubramaniance143bb2009-09-17 09:27:33 +0530625 else if (AR_SREV_9280(ah) || AR_SREV_9287(ah)) {
626 if ((rx_chain_status & 0x2) || (rx_chain_status & 0x4))
627 chainmask = 0x1B;
628 else
629 chainmask = 0x09;
630 } else {
631 if (rx_chain_status & 0x4)
632 chainmask = 0x3F;
633 else if (rx_chain_status & 0x2)
634 chainmask = 0x1B;
635 else
636 chainmask = 0x09;
637 }
Sujithf1dc5602008-10-29 10:16:30 +0530638
Sujithf1dc5602008-10-29 10:16:30 +0530639 h = ah->nfCalHist;
Sujithf1dc5602008-10-29 10:16:30 +0530640
641 for (i = 0; i < NUM_NF_READINGS; i++) {
642 if (chainmask & (1 << i)) {
643 val = REG_READ(ah, ar5416_cca_regs[i]);
644 val &= 0xFFFFFE00;
645 val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
646 REG_WRITE(ah, ar5416_cca_regs[i], val);
647 }
648 }
649
650 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
651 AR_PHY_AGC_CONTROL_ENABLE_NF);
652 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
653 AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
654 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
655
656 for (j = 0; j < 1000; j++) {
657 if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
658 AR_PHY_AGC_CONTROL_NF) == 0)
659 break;
660 udelay(10);
661 }
662
663 for (i = 0; i < NUM_NF_READINGS; i++) {
664 if (chainmask & (1 << i)) {
665 val = REG_READ(ah, ar5416_cca_regs[i]);
666 val &= 0xFFFFFE00;
667 val |= (((u32) (-50) << 1) & 0x1ff);
668 REG_WRITE(ah, ar5416_cca_regs[i], val);
669 }
670 }
671}
672
Sujithcbe61d82009-02-09 13:27:12 +0530673int16_t ath9k_hw_getnf(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530674 struct ath9k_channel *chan)
675{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700676 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530677 int16_t nf, nfThresh;
678 int16_t nfarray[NUM_NF_READINGS] = { 0 };
679 struct ath9k_nfcal_hist *h;
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800680 struct ieee80211_channel *c = chan->chan;
Sujithf1dc5602008-10-29 10:16:30 +0530681
682 chan->channelFlags &= (~CHANNEL_CW_INT);
683 if (REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700684 ath_print(common, ATH_DBG_CALIBRATE,
685 "NF did not complete in calibration window\n");
Sujithf1dc5602008-10-29 10:16:30 +0530686 nf = 0;
687 chan->rawNoiseFloor = nf;
688 return chan->rawNoiseFloor;
689 } else {
690 ath9k_hw_do_getnf(ah, nfarray);
691 nf = nfarray[0];
Luis R. Rodriguez76061ab2008-12-23 15:58:41 -0800692 if (getNoiseFloorThresh(ah, c->band, &nfThresh)
Sujithf1dc5602008-10-29 10:16:30 +0530693 && nf > nfThresh) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700694 ath_print(common, ATH_DBG_CALIBRATE,
695 "noise floor failed detected; "
696 "detected %d, threshold %d\n",
697 nf, nfThresh);
Sujithf1dc5602008-10-29 10:16:30 +0530698 chan->channelFlags |= CHANNEL_CW_INT;
699 }
700 }
701
Sujithf1dc5602008-10-29 10:16:30 +0530702 h = ah->nfCalHist;
Sujithf1dc5602008-10-29 10:16:30 +0530703
704 ath9k_hw_update_nfcal_hist_buffer(h, nfarray);
705 chan->rawNoiseFloor = h[0].privNF;
706
707 return chan->rawNoiseFloor;
708}
709
Sujithcbe61d82009-02-09 13:27:12 +0530710void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530711{
712 int i, j;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400713 s16 noise_floor;
714
715 if (AR_SREV_9280(ah))
716 noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
717 else if (AR_SREV_9285(ah))
718 noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
Vivek Natarajan6170cd52009-09-17 09:24:24 +0530719 else if (AR_SREV_9287(ah))
720 noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400721 else
722 noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
Sujithf1dc5602008-10-29 10:16:30 +0530723
724 for (i = 0; i < NUM_NF_READINGS; i++) {
725 ah->nfCalHist[i].currIndex = 0;
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400726 ah->nfCalHist[i].privNF = noise_floor;
Sujithf1dc5602008-10-29 10:16:30 +0530727 ah->nfCalHist[i].invalidNFcount =
728 AR_PHY_CCA_FILTERWINDOW_LENGTH;
729 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
Senthil Balasubramaniana59b5a52009-07-14 20:17:07 -0400730 ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
Sujithf1dc5602008-10-29 10:16:30 +0530731 }
732 }
Sujithf1dc5602008-10-29 10:16:30 +0530733}
734
Sujithcbe61d82009-02-09 13:27:12 +0530735s16 ath9k_hw_getchan_noise(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +0530736{
Sujithf1dc5602008-10-29 10:16:30 +0530737 s16 nf;
738
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800739 if (chan->rawNoiseFloor == 0)
Luis R. Rodrigueze56db712008-12-23 15:58:47 -0800740 nf = -96;
741 else
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -0800742 nf = chan->rawNoiseFloor;
Sujithf1dc5602008-10-29 10:16:30 +0530743
744 if (!ath9k_hw_nf_in_range(ah, nf))
745 nf = ATH_DEFAULT_NOISE_FLOOR;
746
747 return nf;
748}
749
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530750static void ath9k_olc_temp_compensation_9287(struct ath_hw *ah)
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530751{
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530752 u32 rddata;
753 int32_t delta, currPDADC, slope;
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530754
755 rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530756 currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
757
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530758 if (ah->initPDADC == 0 || currPDADC == 0) {
759 /*
760 * Zero value indicates that no frames have been transmitted yet,
761 * can't do temperature compensation until frames are transmitted.
762 */
763 return;
764 } else {
765 slope = ah->eep_ops->get_eeprom(ah, EEP_TEMPSENSE_SLOPE);
766
767 if (slope == 0) { /* to avoid divide by zero case */
768 delta = 0;
769 } else {
770 delta = ((currPDADC - ah->initPDADC)*4) / slope;
771 }
772 REG_RMW_FIELD(ah, AR_PHY_CH0_TX_PWRCTRL11,
773 AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
774 REG_RMW_FIELD(ah, AR_PHY_CH1_TX_PWRCTRL11,
775 AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
776 }
777}
778
779static void ath9k_olc_temp_compensation(struct ath_hw *ah)
780{
781 u32 rddata, i;
782 int delta, currPDADC, regval;
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530783
Vivek Natarajandb91f2e2009-08-14 11:27:16 +0530784 if (OLC_FOR_AR9287_10_LATER) {
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530785 ath9k_olc_temp_compensation_9287(ah);
786 } else {
787 rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
788 currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
789
Vivek Natarajandb91f2e2009-08-14 11:27:16 +0530790 if (ah->initPDADC == 0 || currPDADC == 0) {
791 return;
792 } else {
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530793 if (ah->eep_ops->get_eeprom(ah, EEP_DAC_HPWR_5G))
794 delta = (currPDADC - ah->initPDADC + 4) / 8;
Vivek Natarajandb91f2e2009-08-14 11:27:16 +0530795 else
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530796 delta = (currPDADC - ah->initPDADC + 5) / 10;
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530797
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530798 if (delta != ah->PDADCdelta) {
799 ah->PDADCdelta = delta;
800 for (i = 1; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
801 regval = ah->originalGain[i] - delta;
802 if (regval < 0)
803 regval = 0;
Vivek Natarajandb91f2e2009-08-14 11:27:16 +0530804
Vivek Natarajan0b98eaa2009-09-18 15:03:42 +0530805 REG_RMW_FIELD(ah,
806 AR_PHY_TX_GAIN_TBL1 + i * 4,
807 AR_PHY_TX_GAIN, regval);
808 }
Vivek Natarajandb91f2e2009-08-14 11:27:16 +0530809 }
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +0530810 }
811 }
812}
813
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400814static void ath9k_hw_9271_pa_cal(struct ath_hw *ah)
815{
816 u32 regVal;
817 unsigned int i;
818 u32 regList [][2] = {
819 { 0x786c, 0 },
820 { 0x7854, 0 },
821 { 0x7820, 0 },
822 { 0x7824, 0 },
823 { 0x7868, 0 },
824 { 0x783c, 0 },
825 { 0x7838, 0 } ,
826 { 0x7828, 0 } ,
827 };
828
829 for (i = 0; i < ARRAY_SIZE(regList); i++)
830 regList[i][1] = REG_READ(ah, regList[i][0]);
831
832 regVal = REG_READ(ah, 0x7834);
833 regVal &= (~(0x1));
834 REG_WRITE(ah, 0x7834, regVal);
835 regVal = REG_READ(ah, 0x9808);
836 regVal |= (0x1 << 27);
837 REG_WRITE(ah, 0x9808, regVal);
838
839 /* 786c,b23,1, pwddac=1 */
840 REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
841 /* 7854, b5,1, pdrxtxbb=1 */
842 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
843 /* 7854, b7,1, pdv2i=1 */
844 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
845 /* 7854, b8,1, pddacinterface=1 */
846 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
847 /* 7824,b12,0, offcal=0 */
848 REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
849 /* 7838, b1,0, pwddb=0 */
850 REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
851 /* 7820,b11,0, enpacal=0 */
852 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
853 /* 7820,b25,1, pdpadrv1=0 */
854 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
855 /* 7820,b24,0, pdpadrv2=0 */
856 REG_RMW_FIELD(ah, AR9285_AN_RF2G1,AR9285_AN_RF2G1_PDPADRV2,0);
857 /* 7820,b23,0, pdpaout=0 */
858 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
859 /* 783c,b14-16,7, padrvgn2tab_0=7 */
860 REG_RMW_FIELD(ah, AR9285_AN_RF2G8,AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
861 /*
862 * 7838,b29-31,0, padrvgn1tab_0=0
863 * does not matter since we turn it off
864 */
865 REG_RMW_FIELD(ah, AR9285_AN_RF2G7,AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
866
867 REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9271_AN_RF2G3_CCOMP, 0xfff);
868
869 /* Set:
870 * localmode=1,bmode=1,bmoderxtx=1,synthon=1,
871 * txon=1,paon=1,oscon=1,synthon_force=1
872 */
873 REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
874 udelay(30);
875 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9271_AN_RF2G6_OFFS, 0);
876
877 /* find off_6_1; */
878 for (i = 6; i >= 0; i--) {
879 regVal = REG_READ(ah, 0x7834);
880 regVal |= (1 << (20 + i));
881 REG_WRITE(ah, 0x7834, regVal);
882 udelay(1);
883 //regVal = REG_READ(ah, 0x7834);
884 regVal &= (~(0x1 << (20 + i)));
885 regVal |= (MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9)
886 << (20 + i));
887 REG_WRITE(ah, 0x7834, regVal);
888 }
889
890 /* Empirical offset correction */
891#if 0
892 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9271_AN_RF2G6_OFFS, 0x20);
893#endif
894
895 regVal = REG_READ(ah, 0x7834);
896 regVal |= 0x1;
897 REG_WRITE(ah, 0x7834, regVal);
898 regVal = REG_READ(ah, 0x9808);
899 regVal &= (~(0x1 << 27));
900 REG_WRITE(ah, 0x9808, regVal);
901
902 for (i = 0; i < ARRAY_SIZE(regList); i++)
903 REG_WRITE(ah, regList[i][0], regList[i][1]);
904}
905
Sujitha13883b2009-08-26 08:39:40 +0530906static inline void ath9k_hw_9285_pa_cal(struct ath_hw *ah, bool is_reset)
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530907{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700908 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530909 u32 regVal;
910 int i, offset, offs_6_1, offs_0;
911 u32 ccomp_org, reg_field;
912 u32 regList[][2] = {
913 { 0x786c, 0 },
914 { 0x7854, 0 },
915 { 0x7820, 0 },
916 { 0x7824, 0 },
917 { 0x7868, 0 },
918 { 0x783c, 0 },
919 { 0x7838, 0 },
920 };
921
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700922 ath_print(common, ATH_DBG_CALIBRATE, "Running PA Calibration\n");
Sujitha13883b2009-08-26 08:39:40 +0530923
Sujith20caf0d2009-08-26 08:39:52 +0530924 /* PA CAL is not needed for high power solution */
925 if (ah->eep_ops->get_eeprom(ah, EEP_TXGAIN_TYPE) ==
926 AR5416_EEP_TXGAIN_HIGH_POWER)
927 return;
928
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530929 if (AR_SREV_9285_11(ah)) {
930 REG_WRITE(ah, AR9285_AN_TOP4, (AR9285_AN_TOP4_DEFAULT | 0x14));
931 udelay(10);
932 }
933
934 for (i = 0; i < ARRAY_SIZE(regList); i++)
935 regList[i][1] = REG_READ(ah, regList[i][0]);
936
937 regVal = REG_READ(ah, 0x7834);
938 regVal &= (~(0x1));
939 REG_WRITE(ah, 0x7834, regVal);
940 regVal = REG_READ(ah, 0x9808);
941 regVal |= (0x1 << 27);
942 REG_WRITE(ah, 0x9808, regVal);
943
944 REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
945 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
946 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
947 REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
948 REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
949 REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
950 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
Sujith0abb0962009-08-26 08:39:50 +0530951 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530952 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);
953 REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
954 REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
955 REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
956 ccomp_org = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);
Sujith0abb0962009-08-26 08:39:50 +0530957 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 0xf);
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530958
959 REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
960 udelay(30);
961 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);
962 REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);
963
964 for (i = 6; i > 0; i--) {
965 regVal = REG_READ(ah, 0x7834);
966 regVal |= (1 << (19 + i));
967 REG_WRITE(ah, 0x7834, regVal);
968 udelay(1);
Sujithedbf51f2009-09-17 09:28:41 +0530969 regVal = REG_READ(ah, 0x7834);
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530970 regVal &= (~(0x1 << (19 + i)));
971 reg_field = MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);
972 regVal |= (reg_field << (19 + i));
973 REG_WRITE(ah, 0x7834, regVal);
974 }
975
976 REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);
977 udelay(1);
978 reg_field = MS(REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);
979 REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);
980 offs_6_1 = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);
981 offs_0 = MS(REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);
982
983 offset = (offs_6_1<<1) | offs_0;
984 offset = offset - 0;
985 offs_6_1 = offset>>1;
986 offs_0 = offset & 1;
987
Sujitha13883b2009-08-26 08:39:40 +0530988 if ((!is_reset) && (ah->pacal_info.prev_offset == offset)) {
989 if (ah->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
990 ah->pacal_info.max_skipcount =
991 2 * ah->pacal_info.max_skipcount;
992 ah->pacal_info.skipcount = ah->pacal_info.max_skipcount;
993 } else {
994 ah->pacal_info.max_skipcount = 1;
995 ah->pacal_info.skipcount = 0;
996 ah->pacal_info.prev_offset = offset;
997 }
998
Senthil Balasubramaniane7594072008-12-08 19:43:48 +0530999 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);
1000 REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);
1001
1002 regVal = REG_READ(ah, 0x7834);
1003 regVal |= 0x1;
1004 REG_WRITE(ah, 0x7834, regVal);
1005 regVal = REG_READ(ah, 0x9808);
1006 regVal &= (~(0x1 << 27));
1007 REG_WRITE(ah, 0x9808, regVal);
1008
1009 for (i = 0; i < ARRAY_SIZE(regList); i++)
1010 REG_WRITE(ah, regList[i][0], regList[i][1]);
1011
1012 REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);
1013
1014 if (AR_SREV_9285_11(ah))
1015 REG_WRITE(ah, AR9285_AN_TOP4, AR9285_AN_TOP4_DEFAULT);
1016
1017}
1018
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301019bool ath9k_hw_calibrate(struct ath_hw *ah, struct ath9k_channel *chan,
Sujith379f0442009-04-13 21:56:48 +05301020 u8 rxchainmask, bool longcal)
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301021{
Sujith379f0442009-04-13 21:56:48 +05301022 bool iscaldone = true;
Sujithcbfe9462009-04-13 21:56:56 +05301023 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301024
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301025 if (currCal &&
1026 (currCal->calState == CAL_RUNNING ||
1027 currCal->calState == CAL_WAITING)) {
Sujith379f0442009-04-13 21:56:48 +05301028 iscaldone = ath9k_hw_per_calibration(ah, chan,
1029 rxchainmask, currCal);
1030 if (iscaldone) {
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301031 ah->cal_list_curr = currCal = currCal->calNext;
1032
1033 if (currCal->calState == CAL_WAITING) {
Sujith379f0442009-04-13 21:56:48 +05301034 iscaldone = false;
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301035 ath9k_hw_reset_calibration(ah, currCal);
1036 }
1037 }
1038 }
1039
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001040 /* Do NF cal only at longer intervals */
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301041 if (longcal) {
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001042 /* Do periodic PAOffset Cal */
1043 if (AR_SREV_9271(ah))
1044 ath9k_hw_9271_pa_cal(ah);
Sujitha13883b2009-08-26 08:39:40 +05301045 else if (AR_SREV_9285_11_OR_LATER(ah)) {
1046 if (!ah->pacal_info.skipcount)
1047 ath9k_hw_9285_pa_cal(ah, false);
1048 else
1049 ah->pacal_info.skipcount--;
1050 }
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301051
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05301052 if (OLC_FOR_AR9280_20_LATER || OLC_FOR_AR9287_10_LATER)
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301053 ath9k_olc_temp_compensation(ah);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001054
1055 /* Get the value from the previous NF cal and update history buffer */
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301056 ath9k_hw_getnf(ah, chan);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001057
1058 /*
1059 * Load the NF from history buffer of the current channel.
1060 * NF is slow time-variant, so it is OK to use a historical value.
1061 */
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301062 ath9k_hw_loadnf(ah, ah->curchan);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001063
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301064 ath9k_hw_start_nfcal(ah);
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301065 }
1066
Sujith379f0442009-04-13 21:56:48 +05301067 return iscaldone;
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301068}
1069
Luis R. Rodriguez15cc0f12009-03-09 22:09:42 -04001070static bool ar9285_clc(struct ath_hw *ah, struct ath9k_channel *chan)
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301071{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001072 struct ath_common *common = ath9k_hw_common(ah);
1073
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301074 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
Sujithdb2f63f2009-04-13 21:56:41 +05301075 if (IS_CHAN_HT20(chan)) {
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301076 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
1077 REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
1078 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
1079 AR_PHY_AGC_CONTROL_FLTR_CAL);
1080 REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
1081 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
1082 if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1083 AR_PHY_AGC_CONTROL_CAL, 0, AH_WAIT_TIMEOUT)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001084 ath_print(common, ATH_DBG_CALIBRATE, "offset "
1085 "calibration failed to complete in "
1086 "1ms; noisy ??\n");
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301087 return false;
1088 }
1089 REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
1090 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
1091 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
1092 }
1093 REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
1094 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
1095 REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
1096 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
1097 if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
1098 0, AH_WAIT_TIMEOUT)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001099 ath_print(common, ATH_DBG_CALIBRATE, "offset calibration "
1100 "failed to complete in 1ms; noisy ??\n");
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301101 return false;
1102 }
1103
1104 REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
1105 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
1106 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
1107
1108 return true;
1109}
1110
Sujith04d19dd2009-04-13 21:56:59 +05301111bool ath9k_hw_init_cal(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +05301112{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001113 struct ath_common *common = ath9k_hw_common(ah);
1114
Gabor Juhosf9dd6b52009-05-06 09:47:30 +02001115 if (AR_SREV_9285_12_OR_LATER(ah)) {
Senthil Balasubramanian4e845162009-03-06 11:24:10 +05301116 if (!ar9285_clc(ah, chan))
1117 return false;
Sujith04d19dd2009-04-13 21:56:59 +05301118 } else {
1119 if (AR_SREV_9280_10_OR_LATER(ah)) {
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05301120 if (!AR_SREV_9287_10_OR_LATER(ah))
1121 REG_CLR_BIT(ah, AR_PHY_ADC_CTL,
1122 AR_PHY_ADC_CTL_OFF_PWDADC);
1123 REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
1124 AR_PHY_AGC_CONTROL_FLTR_CAL);
Sujith04d19dd2009-04-13 21:56:59 +05301125 }
Sujithedf7c062009-02-12 10:06:49 +05301126
Sujith04d19dd2009-04-13 21:56:59 +05301127 /* Calibrate the AGC */
Sujithedf7c062009-02-12 10:06:49 +05301128 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
Sujith04d19dd2009-04-13 21:56:59 +05301129 REG_READ(ah, AR_PHY_AGC_CONTROL) |
1130 AR_PHY_AGC_CONTROL_CAL);
Sujithedf7c062009-02-12 10:06:49 +05301131
Sujith04d19dd2009-04-13 21:56:59 +05301132 /* Poll for offset calibration complete */
1133 if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
1134 0, AH_WAIT_TIMEOUT)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001135 ath_print(common, ATH_DBG_CALIBRATE,
1136 "offset calibration failed to "
1137 "complete in 1ms; noisy environment?\n");
Sujithedf7c062009-02-12 10:06:49 +05301138 return false;
1139 }
1140
Sujith04d19dd2009-04-13 21:56:59 +05301141 if (AR_SREV_9280_10_OR_LATER(ah)) {
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05301142 if (!AR_SREV_9287_10_OR_LATER(ah))
1143 REG_SET_BIT(ah, AR_PHY_ADC_CTL,
1144 AR_PHY_ADC_CTL_OFF_PWDADC);
1145 REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
1146 AR_PHY_AGC_CONTROL_FLTR_CAL);
Sujith04d19dd2009-04-13 21:56:59 +05301147 }
Sujithedf7c062009-02-12 10:06:49 +05301148 }
1149
1150 /* Do PA Calibration */
Gabor Juhosf9dd6b52009-05-06 09:47:30 +02001151 if (AR_SREV_9285_11_OR_LATER(ah))
Sujitha13883b2009-08-26 08:39:40 +05301152 ath9k_hw_9285_pa_cal(ah, true);
Senthil Balasubramaniane7594072008-12-08 19:43:48 +05301153
Sujith04d19dd2009-04-13 21:56:59 +05301154 /* Do NF Calibration after DC offset and other calibrations */
Sujithf1dc5602008-10-29 10:16:30 +05301155 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
Sujith04d19dd2009-04-13 21:56:59 +05301156 REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_NF);
Sujithf1dc5602008-10-29 10:16:30 +05301157
Sujith2660b812009-02-09 13:27:26 +05301158 ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
Sujithf1dc5602008-10-29 10:16:30 +05301159
Sujith04d19dd2009-04-13 21:56:59 +05301160 /* Enable IQ, ADC Gain and ADC DC offset CALs */
Sujithf1dc5602008-10-29 10:16:30 +05301161 if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -08001162 if (ath9k_hw_iscal_supported(ah, ADC_GAIN_CAL)) {
Sujith2660b812009-02-09 13:27:26 +05301163 INIT_CAL(&ah->adcgain_caldata);
1164 INSERT_CAL(ah, &ah->adcgain_caldata);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001165 ath_print(common, ATH_DBG_CALIBRATE,
1166 "enabling ADC Gain Calibration.\n");
Sujithf1dc5602008-10-29 10:16:30 +05301167 }
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -08001168 if (ath9k_hw_iscal_supported(ah, ADC_DC_CAL)) {
Sujith2660b812009-02-09 13:27:26 +05301169 INIT_CAL(&ah->adcdc_caldata);
1170 INSERT_CAL(ah, &ah->adcdc_caldata);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001171 ath_print(common, ATH_DBG_CALIBRATE,
1172 "enabling ADC DC Calibration.\n");
Sujithf1dc5602008-10-29 10:16:30 +05301173 }
Luis R. Rodriguezc9e27d92008-12-23 15:58:42 -08001174 if (ath9k_hw_iscal_supported(ah, IQ_MISMATCH_CAL)) {
Sujith2660b812009-02-09 13:27:26 +05301175 INIT_CAL(&ah->iq_caldata);
1176 INSERT_CAL(ah, &ah->iq_caldata);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001177 ath_print(common, ATH_DBG_CALIBRATE,
1178 "enabling IQ Calibration.\n");
Sujithf1dc5602008-10-29 10:16:30 +05301179 }
1180
Sujith2660b812009-02-09 13:27:26 +05301181 ah->cal_list_curr = ah->cal_list;
Sujithf1dc5602008-10-29 10:16:30 +05301182
Sujith2660b812009-02-09 13:27:26 +05301183 if (ah->cal_list_curr)
1184 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
Sujithf1dc5602008-10-29 10:16:30 +05301185 }
1186
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -08001187 chan->CalValid = 0;
Sujithf1dc5602008-10-29 10:16:30 +05301188
1189 return true;
1190}
1191
Sujithcbfe9462009-04-13 21:56:56 +05301192const struct ath9k_percal_data iq_cal_multi_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301193 IQ_MISMATCH_CAL,
1194 MAX_CAL_SAMPLES,
1195 PER_MIN_LOG_COUNT,
1196 ath9k_hw_iqcal_collect,
1197 ath9k_hw_iqcalibrate
1198};
Sujithcbfe9462009-04-13 21:56:56 +05301199const struct ath9k_percal_data iq_cal_single_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301200 IQ_MISMATCH_CAL,
1201 MIN_CAL_SAMPLES,
1202 PER_MAX_LOG_COUNT,
1203 ath9k_hw_iqcal_collect,
1204 ath9k_hw_iqcalibrate
1205};
Sujithcbfe9462009-04-13 21:56:56 +05301206const struct ath9k_percal_data adc_gain_cal_multi_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301207 ADC_GAIN_CAL,
1208 MAX_CAL_SAMPLES,
1209 PER_MIN_LOG_COUNT,
1210 ath9k_hw_adc_gaincal_collect,
1211 ath9k_hw_adc_gaincal_calibrate
1212};
Sujithcbfe9462009-04-13 21:56:56 +05301213const struct ath9k_percal_data adc_gain_cal_single_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301214 ADC_GAIN_CAL,
1215 MIN_CAL_SAMPLES,
1216 PER_MAX_LOG_COUNT,
1217 ath9k_hw_adc_gaincal_collect,
1218 ath9k_hw_adc_gaincal_calibrate
1219};
Sujithcbfe9462009-04-13 21:56:56 +05301220const struct ath9k_percal_data adc_dc_cal_multi_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301221 ADC_DC_CAL,
1222 MAX_CAL_SAMPLES,
1223 PER_MIN_LOG_COUNT,
1224 ath9k_hw_adc_dccal_collect,
1225 ath9k_hw_adc_dccal_calibrate
1226};
Sujithcbfe9462009-04-13 21:56:56 +05301227const struct ath9k_percal_data adc_dc_cal_single_sample = {
Sujithf1dc5602008-10-29 10:16:30 +05301228 ADC_DC_CAL,
1229 MIN_CAL_SAMPLES,
1230 PER_MAX_LOG_COUNT,
1231 ath9k_hw_adc_dccal_collect,
1232 ath9k_hw_adc_dccal_calibrate
1233};
Sujithcbfe9462009-04-13 21:56:56 +05301234const struct ath9k_percal_data adc_init_dc_cal = {
Sujithf1dc5602008-10-29 10:16:30 +05301235 ADC_DC_INIT_CAL,
1236 MIN_CAL_SAMPLES,
1237 INIT_LOG_COUNT,
1238 ath9k_hw_adc_dccal_collect,
1239 ath9k_hw_adc_dccal_calibrate
1240};