blob: 598b847e4e5abf42e0fcfc122eee68ff66ca8537 [file] [log] [blame]
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2010-2011 Atheros Communications Inc.
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04003 *
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
17#include "hw.h"
18#include "hw-ops.h"
19#include "ar9003_phy.h"
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +053020#include "ar9003_rtt.h"
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +053021#include "ar9003_mci.h"
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040022
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +053023#define MAX_MEASUREMENT MAX_IQCAL_MEASUREMENT
Rajkumar Manoharan3782c692011-04-24 21:34:39 +053024#define MAX_MAG_DELTA 11
25#define MAX_PHS_DELTA 10
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -080026
27struct coeff {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +053028 int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
29 int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -080030 int iqc_coeff[2];
31};
32
Felix Fietkau64978272010-10-03 19:07:16 +020033enum ar9003_cal_types {
34 IQ_MISMATCH_CAL = BIT(0),
35 TEMP_COMP_CAL = BIT(1),
36};
37
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040038static void ar9003_hw_setup_calibration(struct ath_hw *ah,
39 struct ath9k_cal_list *currCal)
40{
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040041 struct ath_common *common = ath9k_hw_common(ah);
42
43 /* Select calibration to run */
44 switch (currCal->calData->calType) {
45 case IQ_MISMATCH_CAL:
46 /*
47 * Start calibration with
48 * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
49 */
50 REG_RMW_FIELD(ah, AR_PHY_TIMING4,
51 AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
52 currCal->calData->calCountMax);
53 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
54
Joe Perchesd2182b62011-12-15 14:55:53 -080055 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -080056 "starting IQ Mismatch Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040057
58 /* Kick-off cal */
59 REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
60 break;
61 case TEMP_COMP_CAL:
62 REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
63 AR_PHY_65NM_CH0_THERM_LOCAL, 1);
64 REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
65 AR_PHY_65NM_CH0_THERM_START, 1);
66
Joe Perchesd2182b62011-12-15 14:55:53 -080067 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -080068 "starting Temperature Compensation Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040069 break;
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040070 }
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040071}
72
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040073/*
74 * Generic calibration routine.
75 * Recalibrate the lower PHY chips to account for temperature/environment
76 * changes.
77 */
78static bool ar9003_hw_per_calibration(struct ath_hw *ah,
79 struct ath9k_channel *ichan,
80 u8 rxchainmask,
81 struct ath9k_cal_list *currCal)
82{
Felix Fietkau20bd2a02010-07-31 00:12:00 +020083 struct ath9k_hw_cal_data *caldata = ah->caldata;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040084 /* Cal is assumed not done until explicitly set below */
85 bool iscaldone = false;
86
87 /* Calibration in progress. */
88 if (currCal->calState == CAL_RUNNING) {
89 /* Check to see if it has finished. */
90 if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
91 /*
92 * Accumulate cal measures for active chains
93 */
94 currCal->calData->calCollect(ah);
95 ah->cal_samples++;
96
97 if (ah->cal_samples >=
98 currCal->calData->calNumSamples) {
99 unsigned int i, numChains = 0;
100 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
101 if (rxchainmask & (1 << i))
102 numChains++;
103 }
104
105 /*
106 * Process accumulated data
107 */
108 currCal->calData->calPostProc(ah, numChains);
109
110 /* Calibration has finished. */
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200111 caldata->CalValid |= currCal->calData->calType;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400112 currCal->calState = CAL_DONE;
113 iscaldone = true;
114 } else {
115 /*
116 * Set-up collection of another sub-sample until we
117 * get desired number
118 */
119 ar9003_hw_setup_calibration(ah, currCal);
120 }
121 }
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200122 } else if (!(caldata->CalValid & currCal->calData->calType)) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400123 /* If current cal is marked invalid in channel, kick it off */
124 ath9k_hw_reset_calibration(ah, currCal);
125 }
126
127 return iscaldone;
128}
129
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400130static bool ar9003_hw_calibrate(struct ath_hw *ah,
131 struct ath9k_channel *chan,
132 u8 rxchainmask,
133 bool longcal)
134{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400135 bool iscaldone = true;
136 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400137
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400138 /*
139 * For given calibration:
140 * 1. Call generic cal routine
141 * 2. When this cal is done (isCalDone) if we have more cals waiting
142 * (eg after reset), mask this to upper layers by not propagating
143 * isCalDone if it is set to TRUE.
144 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
145 * to be run.
146 */
147 if (currCal &&
148 (currCal->calState == CAL_RUNNING ||
149 currCal->calState == CAL_WAITING)) {
150 iscaldone = ar9003_hw_per_calibration(ah, chan,
151 rxchainmask, currCal);
152 if (iscaldone) {
153 ah->cal_list_curr = currCal = currCal->calNext;
154
155 if (currCal->calState == CAL_WAITING) {
156 iscaldone = false;
157 ath9k_hw_reset_calibration(ah, currCal);
158 }
159 }
160 }
161
Rajkumar Manoharan54717e52012-07-01 19:53:52 +0530162 /*
163 * Do NF cal only at longer intervals. Get the value from
164 * the previous NF cal and update history buffer.
165 */
166 if (longcal && ath9k_hw_getnf(ah, chan)) {
Felix Fietkau93697462010-07-30 21:02:10 +0200167 /*
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400168 * Load the NF from history buffer of the current channel.
169 * NF is slow time-variant, so it is OK to use a historical
170 * value.
171 */
172 ath9k_hw_loadnf(ah, ah->curchan);
173
174 /* start NF calibration, without updating BB NF register */
Felix Fietkau00c86592010-07-30 21:02:09 +0200175 ath9k_hw_start_nfcal(ah, false);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400176 }
177
178 return iscaldone;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400179}
180
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400181static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
182{
183 int i;
184
185 /* Accumulate IQ cal measures for active chains */
186 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Vasanthakumar Thiagarajan5d48ae72011-04-19 19:29:16 +0530187 if (ah->txchainmask & BIT(i)) {
188 ah->totalPowerMeasI[i] +=
189 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
190 ah->totalPowerMeasQ[i] +=
191 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
192 ah->totalIqCorrMeas[i] +=
193 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
Joe Perchesd2182b62011-12-15 14:55:53 -0800194 ath_dbg(ath9k_hw_common(ah), CALIBRATE,
Vasanthakumar Thiagarajan5d48ae72011-04-19 19:29:16 +0530195 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
196 ah->cal_samples, i, ah->totalPowerMeasI[i],
197 ah->totalPowerMeasQ[i],
198 ah->totalIqCorrMeas[i]);
199 }
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400200 }
201}
202
203static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
204{
205 struct ath_common *common = ath9k_hw_common(ah);
206 u32 powerMeasQ, powerMeasI, iqCorrMeas;
207 u32 qCoffDenom, iCoffDenom;
208 int32_t qCoff, iCoff;
209 int iqCorrNeg, i;
Joe Perches07b2fa52010-11-20 18:38:53 -0800210 static const u_int32_t offset_array[3] = {
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400211 AR_PHY_RX_IQCAL_CORR_B0,
212 AR_PHY_RX_IQCAL_CORR_B1,
213 AR_PHY_RX_IQCAL_CORR_B2,
214 };
215
216 for (i = 0; i < numChains; i++) {
217 powerMeasI = ah->totalPowerMeasI[i];
218 powerMeasQ = ah->totalPowerMeasQ[i];
219 iqCorrMeas = ah->totalIqCorrMeas[i];
220
Joe Perchesd2182b62011-12-15 14:55:53 -0800221 ath_dbg(common, CALIBRATE,
222 "Starting IQ Cal and Correction for Chain %d\n", i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400223
Joe Perchesd2182b62011-12-15 14:55:53 -0800224 ath_dbg(common, CALIBRATE,
Nikolay Martynova9b2ce02011-12-02 22:39:14 -0500225 "Original: Chn %d iq_corr_meas = 0x%08x\n",
Joe Perches226afe62010-12-02 19:12:37 -0800226 i, ah->totalIqCorrMeas[i]);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400227
228 iqCorrNeg = 0;
229
230 if (iqCorrMeas > 0x80000000) {
231 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
232 iqCorrNeg = 1;
233 }
234
Joe Perchesd2182b62011-12-15 14:55:53 -0800235 ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_i = 0x%08x\n",
236 i, powerMeasI);
237 ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_q = 0x%08x\n",
238 i, powerMeasQ);
239 ath_dbg(common, CALIBRATE, "iqCorrNeg is 0x%08x\n", iqCorrNeg);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400240
241 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
242 qCoffDenom = powerMeasQ / 64;
243
244 if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
245 iCoff = iqCorrMeas / iCoffDenom;
246 qCoff = powerMeasI / qCoffDenom - 64;
Joe Perchesd2182b62011-12-15 14:55:53 -0800247 ath_dbg(common, CALIBRATE, "Chn %d iCoff = 0x%08x\n",
248 i, iCoff);
249 ath_dbg(common, CALIBRATE, "Chn %d qCoff = 0x%08x\n",
250 i, qCoff);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400251
252 /* Force bounds on iCoff */
253 if (iCoff >= 63)
254 iCoff = 63;
255 else if (iCoff <= -63)
256 iCoff = -63;
257
258 /* Negate iCoff if iqCorrNeg == 0 */
259 if (iqCorrNeg == 0x0)
260 iCoff = -iCoff;
261
262 /* Force bounds on qCoff */
263 if (qCoff >= 63)
264 qCoff = 63;
265 else if (qCoff <= -63)
266 qCoff = -63;
267
268 iCoff = iCoff & 0x7f;
269 qCoff = qCoff & 0x7f;
270
Joe Perchesd2182b62011-12-15 14:55:53 -0800271 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800272 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
273 i, iCoff, qCoff);
Joe Perchesd2182b62011-12-15 14:55:53 -0800274 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800275 "Register offset (0x%04x) before update = 0x%x\n",
276 offset_array[i],
277 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400278
Rajkumar Manoharan6f37ff92012-10-15 15:29:51 +0530279 if (AR_SREV_9565(ah) &&
280 (iCoff == 63 || qCoff == 63 ||
281 iCoff == -63 || qCoff == -63))
282 return;
283
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400284 REG_RMW_FIELD(ah, offset_array[i],
285 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
286 iCoff);
287 REG_RMW_FIELD(ah, offset_array[i],
288 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
289 qCoff);
Joe Perchesd2182b62011-12-15 14:55:53 -0800290 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800291 "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
292 offset_array[i],
293 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
294 REG_READ(ah, offset_array[i]));
Joe Perchesd2182b62011-12-15 14:55:53 -0800295 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800296 "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
297 offset_array[i],
298 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
299 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400300
Joe Perchesd2182b62011-12-15 14:55:53 -0800301 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800302 "IQ Cal and Correction done for Chain %d\n", i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400303 }
304 }
305
306 REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
307 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
Joe Perchesd2182b62011-12-15 14:55:53 -0800308 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800309 "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
310 (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
311 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
312 REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400313}
314
315static const struct ath9k_percal_data iq_cal_single_sample = {
316 IQ_MISMATCH_CAL,
317 MIN_CAL_SAMPLES,
318 PER_MAX_LOG_COUNT,
319 ar9003_hw_iqcal_collect,
320 ar9003_hw_iqcalibrate
321};
322
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400323static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
324{
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400325 ah->iq_caldata.calData = &iq_cal_single_sample;
Sujith Manoharanb6869292013-01-03 12:21:21 +0530326
327 if (AR_SREV_9300_20_OR_LATER(ah)) {
328 ah->enabled_cals |= TX_IQ_CAL;
329 if (AR_SREV_9485_OR_LATER(ah))
330 ah->enabled_cals |= TX_IQ_ON_AGC_CAL;
331 }
332
333 ah->supp_cals = IQ_MISMATCH_CAL;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400334}
335
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400336/*
337 * solve 4x4 linear equation used in loopback iq cal.
338 */
339static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
340 s32 sin_2phi_1,
341 s32 cos_2phi_1,
342 s32 sin_2phi_2,
343 s32 cos_2phi_2,
344 s32 mag_a0_d0,
345 s32 phs_a0_d0,
346 s32 mag_a1_d0,
347 s32 phs_a1_d0,
348 s32 solved_eq[])
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -0400349{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400350 s32 f1 = cos_2phi_1 - cos_2phi_2,
351 f3 = sin_2phi_1 - sin_2phi_2,
352 f2;
353 s32 mag_tx, phs_tx, mag_rx, phs_rx;
354 const s32 result_shift = 1 << 15;
355 struct ath_common *common = ath9k_hw_common(ah);
356
357 f2 = (f1 * f1 + f3 * f3) / result_shift;
358
359 if (!f2) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800360 ath_dbg(common, CALIBRATE, "Divide by 0\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400361 return false;
362 }
363
364 /* mag mismatch, tx */
365 mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
366 /* phs mismatch, tx */
367 phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
368
369 mag_tx = (mag_tx / f2);
370 phs_tx = (phs_tx / f2);
371
372 /* mag mismatch, rx */
373 mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
374 result_shift;
375 /* phs mismatch, rx */
376 phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
377 result_shift;
378
379 solved_eq[0] = mag_tx;
380 solved_eq[1] = phs_tx;
381 solved_eq[2] = mag_rx;
382 solved_eq[3] = phs_rx;
383
384 return true;
385}
386
387static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
388{
389 s32 abs_i = abs(in_re),
390 abs_q = abs(in_im),
391 max_abs, min_abs;
392
393 if (abs_i > abs_q) {
394 max_abs = abs_i;
395 min_abs = abs_q;
396 } else {
397 max_abs = abs_q;
398 min_abs = abs_i;
399 }
400
401 return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
402}
403
404#define DELPT 32
405
406static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
407 s32 chain_idx,
408 const s32 iq_res[],
409 s32 iqc_coeff[])
410{
411 s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
412 i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
413 i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
414 i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
415 s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
416 phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
417 sin_2phi_1, cos_2phi_1,
418 sin_2phi_2, cos_2phi_2;
419 s32 mag_tx, phs_tx, mag_rx, phs_rx;
420 s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
421 q_q_coff, q_i_coff;
422 const s32 res_scale = 1 << 15;
423 const s32 delpt_shift = 1 << 8;
424 s32 mag1, mag2;
425 struct ath_common *common = ath9k_hw_common(ah);
426
427 i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
428 i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
429 iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
430
431 if (i2_m_q2_a0_d0 > 0x800)
432 i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
433
434 if (i2_p_q2_a0_d0 > 0x800)
435 i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
436
437 if (iq_corr_a0_d0 > 0x800)
438 iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
439
440 i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
441 i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
442 iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
443
444 if (i2_m_q2_a0_d1 > 0x800)
445 i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
446
447 if (i2_p_q2_a0_d1 > 0x800)
448 i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
449
450 if (iq_corr_a0_d1 > 0x800)
451 iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
452
453 i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
454 i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
455 iq_corr_a1_d0 = iq_res[4] & 0xfff;
456
457 if (i2_m_q2_a1_d0 > 0x800)
458 i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
459
460 if (i2_p_q2_a1_d0 > 0x800)
461 i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
462
463 if (iq_corr_a1_d0 > 0x800)
464 iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
465
466 i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
467 i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
468 iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
469
470 if (i2_m_q2_a1_d1 > 0x800)
471 i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
472
473 if (i2_p_q2_a1_d1 > 0x800)
474 i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
475
476 if (iq_corr_a1_d1 > 0x800)
477 iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
478
479 if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
480 (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800481 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800482 "Divide by 0:\n"
483 "a0_d0=%d\n"
484 "a0_d1=%d\n"
485 "a2_d0=%d\n"
486 "a1_d1=%d\n",
487 i2_p_q2_a0_d0, i2_p_q2_a0_d1,
488 i2_p_q2_a1_d0, i2_p_q2_a1_d1);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400489 return false;
490 }
491
492 mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
493 phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
494
495 mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
496 phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
497
498 mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
499 phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
500
501 mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
502 phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
503
504 /* w/o analog phase shift */
505 sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
506 /* w/o analog phase shift */
507 cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
508 /* w/ analog phase shift */
509 sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
510 /* w/ analog phase shift */
511 cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
512
513 /*
514 * force sin^2 + cos^2 = 1;
515 * find magnitude by approximation
516 */
517 mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
518 mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
519
520 if ((mag1 == 0) || (mag2 == 0)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800521 ath_dbg(common, CALIBRATE, "Divide by 0: mag1=%d, mag2=%d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800522 mag1, mag2);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400523 return false;
524 }
525
526 /* normalization sin and cos by mag */
527 sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
528 cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
529 sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
530 cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
531
532 /* calculate IQ mismatch */
533 if (!ar9003_hw_solve_iq_cal(ah,
534 sin_2phi_1, cos_2phi_1,
535 sin_2phi_2, cos_2phi_2,
536 mag_a0_d0, phs_a0_d0,
537 mag_a1_d0,
538 phs_a1_d0, solved_eq)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800539 ath_dbg(common, CALIBRATE,
540 "Call to ar9003_hw_solve_iq_cal() failed\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400541 return false;
542 }
543
544 mag_tx = solved_eq[0];
545 phs_tx = solved_eq[1];
546 mag_rx = solved_eq[2];
547 phs_rx = solved_eq[3];
548
Joe Perchesd2182b62011-12-15 14:55:53 -0800549 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800550 "chain %d: mag mismatch=%d phase mismatch=%d\n",
551 chain_idx, mag_tx/res_scale, phs_tx/res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400552
553 if (res_scale == mag_tx) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800554 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800555 "Divide by 0: mag_tx=%d, res_scale=%d\n",
556 mag_tx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400557 return false;
558 }
559
560 /* calculate and quantize Tx IQ correction factor */
561 mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
562 phs_corr_tx = -phs_tx;
563
564 q_q_coff = (mag_corr_tx * 128 / res_scale);
565 q_i_coff = (phs_corr_tx * 256 / res_scale);
566
Joe Perchesd2182b62011-12-15 14:55:53 -0800567 ath_dbg(common, CALIBRATE, "tx chain %d: mag corr=%d phase corr=%d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800568 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400569
570 if (q_i_coff < -63)
571 q_i_coff = -63;
572 if (q_i_coff > 63)
573 q_i_coff = 63;
574 if (q_q_coff < -63)
575 q_q_coff = -63;
576 if (q_q_coff > 63)
577 q_q_coff = 63;
578
579 iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
580
Joe Perchesd2182b62011-12-15 14:55:53 -0800581 ath_dbg(common, CALIBRATE, "tx chain %d: iq corr coeff=%x\n",
Joe Perches226afe62010-12-02 19:12:37 -0800582 chain_idx, iqc_coeff[0]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400583
584 if (-mag_rx == res_scale) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800585 ath_dbg(common, CALIBRATE,
Joe Perches226afe62010-12-02 19:12:37 -0800586 "Divide by 0: mag_rx=%d, res_scale=%d\n",
587 mag_rx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400588 return false;
589 }
590
591 /* calculate and quantize Rx IQ correction factors */
592 mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
593 phs_corr_rx = -phs_rx;
594
595 q_q_coff = (mag_corr_rx * 128 / res_scale);
596 q_i_coff = (phs_corr_rx * 256 / res_scale);
597
Joe Perchesd2182b62011-12-15 14:55:53 -0800598 ath_dbg(common, CALIBRATE, "rx chain %d: mag corr=%d phase corr=%d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800599 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400600
601 if (q_i_coff < -63)
602 q_i_coff = -63;
603 if (q_i_coff > 63)
604 q_i_coff = 63;
605 if (q_q_coff < -63)
606 q_q_coff = -63;
607 if (q_q_coff > 63)
608 q_q_coff = 63;
609
610 iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
611
Joe Perchesd2182b62011-12-15 14:55:53 -0800612 ath_dbg(common, CALIBRATE, "rx chain %d: iq corr coeff=%x\n",
Joe Perches226afe62010-12-02 19:12:37 -0800613 chain_idx, iqc_coeff[1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400614
615 return true;
616}
617
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530618static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
619 int max_delta)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800620{
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530621 int mp_max = -64, max_idx = 0;
622 int mp_min = 63, min_idx = 0;
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530623 int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800624
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530625 /* find min/max mismatch across all calibrated gains */
626 for (i = 0; i < nmeasurement; i++) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530627 if (mp_coeff[i] > mp_max) {
628 mp_max = mp_coeff[i];
629 max_idx = i;
630 } else if (mp_coeff[i] < mp_min) {
631 mp_min = mp_coeff[i];
632 min_idx = i;
633 }
634 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800635
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530636 /* find average (exclude max abs value) */
637 for (i = 0; i < nmeasurement; i++) {
638 if ((abs(mp_coeff[i]) < abs(mp_max)) ||
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530639 (abs(mp_coeff[i]) < abs(mp_min))) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530640 mp_avg += mp_coeff[i];
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530641 mp_count++;
642 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530643 }
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530644
645 /*
646 * finding mean magnitude/phase if possible, otherwise
647 * just use the last value as the mean
648 */
649 if (mp_count)
650 mp_avg /= mp_count;
651 else
652 mp_avg = mp_coeff[nmeasurement - 1];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800653
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530654 /* detect outlier */
655 if (abs(mp_max - mp_min) > max_delta) {
656 if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
657 outlier_idx = max_idx;
658 else
659 outlier_idx = min_idx;
Rajkumar Manoharane9c10462011-09-15 19:02:25 +0530660
661 mp_coeff[outlier_idx] = mp_avg;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530662 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800663}
664
665static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530666 struct coeff *coeff,
667 bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800668{
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800669 int i, im, nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800670 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530671 struct ath9k_hw_cal_data *caldata = ah->caldata;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800672
673 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
674 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
675 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
676 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
677 if (!AR_SREV_9485(ah)) {
678 tx_corr_coeff[i * 2][1] =
679 tx_corr_coeff[(i * 2) + 1][1] =
680 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
681
682 tx_corr_coeff[i * 2][2] =
683 tx_corr_coeff[(i * 2) + 1][2] =
684 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
685 }
686 }
687
688 /* Load the average of 2 passes */
Rajkumar Manoharan4fb71752012-06-04 16:28:14 +0530689 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
690 if (!(ah->txchainmask & (1 << i)))
691 continue;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530692 nmeasurement = REG_READ_FIELD(ah,
693 AR_PHY_TX_IQCAL_STATUS_B0,
694 AR_PHY_CALIBRATED_GAINS_0);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800695
696 if (nmeasurement > MAX_MEASUREMENT)
697 nmeasurement = MAX_MEASUREMENT;
698
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530699 /* detect outlier only if nmeasurement > 1 */
700 if (nmeasurement > 1) {
701 /* Detect magnitude outlier */
702 ar9003_hw_detect_outlier(coeff->mag_coeff[i],
703 nmeasurement, MAX_MAG_DELTA);
704
705 /* Detect phase outlier */
706 ar9003_hw_detect_outlier(coeff->phs_coeff[i],
707 nmeasurement, MAX_PHS_DELTA);
708 }
709
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800710 for (im = 0; im < nmeasurement; im++) {
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800711
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530712 coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
713 ((coeff->phs_coeff[i][im] & 0x7f) << 7);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800714
715 if ((im % 2) == 0)
716 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
717 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
718 coeff->iqc_coeff[0]);
719 else
720 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
721 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
722 coeff->iqc_coeff[0]);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530723
724 if (caldata)
725 caldata->tx_corr_coeff[im][i] =
726 coeff->iqc_coeff[0];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800727 }
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530728 if (caldata)
729 caldata->num_measures[i] = nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800730 }
731
732 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
733 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
734 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
735 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530736
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530737 if (caldata)
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530738 caldata->done_txiqcal_once = is_reusable;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800739
740 return;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800741}
742
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530743static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
Vasanthakumar Thiagarajan0b2084b2010-12-15 07:30:50 -0800744{
745 struct ath_common *common = ath9k_hw_common(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800746 u8 tx_gain_forced;
747
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800748 tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
749 AR_PHY_TXGAIN_FORCE);
750 if (tx_gain_forced)
751 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
752 AR_PHY_TXGAIN_FORCE, 0);
753
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530754 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
755 AR_PHY_TX_IQCAL_START_DO_CAL, 1);
756
757 if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
758 AR_PHY_TX_IQCAL_START_DO_CAL, 0,
759 AH_WAIT_TIMEOUT)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800760 ath_dbg(common, CALIBRATE, "Tx IQ Cal is not completed\n");
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530761 return false;
762 }
763 return true;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800764}
765
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530766static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah, bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800767{
768 struct ath_common *common = ath9k_hw_common(ah);
769 const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530770 AR_PHY_TX_IQCAL_STATUS_B0,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800771 AR_PHY_TX_IQCAL_STATUS_B1,
772 AR_PHY_TX_IQCAL_STATUS_B2,
773 };
774 const u_int32_t chan_info_tab[] = {
775 AR_PHY_CHAN_INFO_TAB_0,
776 AR_PHY_CHAN_INFO_TAB_1,
777 AR_PHY_CHAN_INFO_TAB_2,
778 };
779 struct coeff coeff;
780 s32 iq_res[6];
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530781 int i, im, j;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800782 int nmeasurement;
783
784 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
Rajkumar Manoharan4fb71752012-06-04 16:28:14 +0530785 if (!(ah->txchainmask & (1 << i)))
786 continue;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800787
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530788 nmeasurement = REG_READ_FIELD(ah,
789 AR_PHY_TX_IQCAL_STATUS_B0,
790 AR_PHY_CALIBRATED_GAINS_0);
791 if (nmeasurement > MAX_MEASUREMENT)
792 nmeasurement = MAX_MEASUREMENT;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800793
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530794 for (im = 0; im < nmeasurement; im++) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800795 ath_dbg(common, CALIBRATE,
796 "Doing Tx IQ Cal for chain %d\n", i);
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530797
798 if (REG_READ(ah, txiqcal_status[i]) &
799 AR_PHY_TX_IQCAL_STATUS_FAILED) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800800 ath_dbg(common, CALIBRATE,
801 "Tx IQ Cal failed for chain %d\n", i);
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530802 goto tx_iqcal_fail;
803 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800804
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530805 for (j = 0; j < 3; j++) {
806 u32 idx = 2 * j, offset = 4 * (3 * im + j);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800807
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530808 REG_RMW_FIELD(ah,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800809 AR_PHY_CHAN_INFO_MEMORY,
810 AR_PHY_CHAN_INFO_TAB_S2_READ,
811 0);
812
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530813 /* 32 bits */
814 iq_res[idx] = REG_READ(ah,
815 chan_info_tab[i] +
816 offset);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800817
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530818 REG_RMW_FIELD(ah,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800819 AR_PHY_CHAN_INFO_MEMORY,
820 AR_PHY_CHAN_INFO_TAB_S2_READ,
821 1);
822
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530823 /* 16 bits */
824 iq_res[idx + 1] = 0xffff & REG_READ(ah,
825 chan_info_tab[i] + offset);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800826
Joe Perchesd2182b62011-12-15 14:55:53 -0800827 ath_dbg(common, CALIBRATE,
828 "IQ_RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530829 idx, iq_res[idx], idx + 1,
830 iq_res[idx + 1]);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800831 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530832
833 if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
834 coeff.iqc_coeff)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800835 ath_dbg(common, CALIBRATE,
836 "Failed in calculation of IQ correction\n");
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530837 goto tx_iqcal_fail;
838 }
839
840 coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
841 coeff.phs_coeff[i][im] =
842 (coeff.iqc_coeff[0] >> 7) & 0x7f;
843
844 if (coeff.mag_coeff[i][im] > 63)
845 coeff.mag_coeff[i][im] -= 128;
846 if (coeff.phs_coeff[i][im] > 63)
847 coeff.phs_coeff[i][im] -= 128;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800848 }
849 }
Rajkumar Manoharan4fb71752012-06-04 16:28:14 +0530850 ar9003_hw_tx_iqcal_load_avg_2_passes(ah, &coeff, is_reusable);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800851
852 return;
853
854tx_iqcal_fail:
Joe Perchesd2182b62011-12-15 14:55:53 -0800855 ath_dbg(common, CALIBRATE, "Tx IQ Cal failed\n");
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800856 return;
857}
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530858
859static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
860{
861 struct ath9k_hw_cal_data *caldata = ah->caldata;
862 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
863 int i, im;
864
865 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
866 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
867 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
868 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
869 if (!AR_SREV_9485(ah)) {
870 tx_corr_coeff[i * 2][1] =
871 tx_corr_coeff[(i * 2) + 1][1] =
872 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
873
874 tx_corr_coeff[i * 2][2] =
875 tx_corr_coeff[(i * 2) + 1][2] =
876 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
877 }
878 }
879
880 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
881 if (!(ah->txchainmask & (1 << i)))
882 continue;
883
884 for (im = 0; im < caldata->num_measures[i]; im++) {
885 if ((im % 2) == 0)
886 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
887 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
888 caldata->tx_corr_coeff[im][i]);
889 else
890 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
891 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
892 caldata->tx_corr_coeff[im][i]);
893 }
894 }
895
896 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
897 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
898 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
899 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
900}
901
Rajkumar Manoharan0b6eb362012-11-09 14:51:30 +0530902static void ar9003_hw_manual_peak_cal(struct ath_hw *ah, u8 chain, bool is_2g)
903{
904 int offset[8], total = 0, test;
905 int agc_out, i;
906
907 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
908 AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0x1);
909 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
910 AR_PHY_65NM_RXRF_GAINSTAGES_LNAON_CALDC, 0x0);
911 if (is_2g)
912 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
913 AR_PHY_65NM_RXRF_GAINSTAGES_LNA2G_GAIN_OVR, 0x0);
914 else
915 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
916 AR_PHY_65NM_RXRF_GAINSTAGES_LNA5G_GAIN_OVR, 0x0);
917
918 REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
919 AR_PHY_65NM_RXTX2_RXON_OVR, 0x1);
920 REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
921 AR_PHY_65NM_RXTX2_RXON, 0x0);
922
923 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
924 AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE, 0x1);
925 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
926 AR_PHY_65NM_RXRF_AGC_AGC_ON_OVR, 0x1);
927 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
928 AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0x1);
929 if (is_2g)
930 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
931 AR_PHY_65NM_RXRF_AGC_AGC2G_DBDAC_OVR, 0x0);
932 else
933 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
934 AR_PHY_65NM_RXRF_AGC_AGC5G_DBDAC_OVR, 0x0);
935
936 for (i = 6; i > 0; i--) {
937 offset[i] = BIT(i - 1);
938 test = total + offset[i];
939
940 if (is_2g)
941 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
942 AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR,
943 test);
944 else
945 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
946 AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR,
947 test);
948 udelay(100);
949 agc_out = REG_READ_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
950 AR_PHY_65NM_RXRF_AGC_AGC_OUT);
951 offset[i] = (agc_out) ? 0 : 1;
952 total += (offset[i] << (i - 1));
953 }
954
955 if (is_2g)
956 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
957 AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR, total);
958 else
959 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
960 AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR, total);
961
962 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
963 AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0);
964 REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
965 AR_PHY_65NM_RXTX2_RXON_OVR, 0);
966 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
967 AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0);
968}
969
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400970static bool ar9003_hw_init_cal(struct ath_hw *ah,
971 struct ath9k_channel *chan)
972{
973 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530974 struct ath9k_hw_cal_data *caldata = ah->caldata;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530975 bool txiqcal_done = false, txclcal_done = false;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530976 bool is_reusable = true, status = true;
977 bool run_rtt_cal = false, run_agc_cal;
978 bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
979 u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
980 AR_PHY_AGC_CONTROL_FLTR_CAL |
981 AR_PHY_AGC_CONTROL_PKDET_CAL;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530982 int i, j;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530983 u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
984 AR_PHY_CL_TAB_1,
985 AR_PHY_CL_TAB_2 };
986
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530987 if (rtt) {
988 if (!ar9003_hw_rtt_restore(ah, chan))
989 run_rtt_cal = true;
990
Sujith Manoharan8a905552012-05-04 13:23:59 +0530991 if (run_rtt_cal)
992 ath_dbg(common, CALIBRATE, "RTT calibration to be done\n");
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530993 }
Sujith Manoharan8a905552012-05-04 13:23:59 +0530994
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530995 run_agc_cal = run_rtt_cal;
996
997 if (run_rtt_cal) {
998 ar9003_hw_rtt_enable(ah);
999 ar9003_hw_rtt_set_mask(ah, 0x00);
1000 ar9003_hw_rtt_clear_hist(ah);
1001 }
1002
1003 if (rtt && !run_rtt_cal) {
1004 agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
1005 agc_supp_cals &= agc_ctrl;
1006 agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
1007 AR_PHY_AGC_CONTROL_FLTR_CAL |
1008 AR_PHY_AGC_CONTROL_PKDET_CAL);
1009 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1010 }
1011
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301012 if (ah->enabled_cals & TX_CL_CAL) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301013 if (caldata && caldata->done_txclcal_once)
1014 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
1015 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301016 else {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301017 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
1018 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301019 run_agc_cal = true;
1020 }
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301021 }
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001022
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301023 if (!(ah->enabled_cals & TX_IQ_CAL))
1024 goto skip_tx_iqcal;
1025
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -04001026 /* Do Tx IQ Calibration */
Rajkumar Manoharan3782c692011-04-24 21:34:39 +05301027 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
1028 AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
1029 DELPT);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -08001030
Rajkumar Manoharan3782c692011-04-24 21:34:39 +05301031 /*
1032 * For AR9485 or later chips, TxIQ cal runs as part of
1033 * AGC calibration
1034 */
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301035 if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301036 if (caldata && !caldata->done_txiqcal_once)
1037 REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1038 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
1039 else
1040 REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1041 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301042 txiqcal_done = run_agc_cal = true;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301043 goto skip_tx_iqcal;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301044 } else if (caldata && !caldata->done_txiqcal_once)
1045 run_agc_cal = true;
1046
Sujith Manoharan5955b2b2012-06-04 16:27:30 +05301047 if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
Sujith Manoharanf2f408e2012-02-22 12:41:06 +05301048 ar9003_mci_init_cal_req(ah, &is_reusable);
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +05301049
Felix Fietkaue5d821a2012-04-19 21:18:27 +02001050 if (!(IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan))) {
1051 txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
1052 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1053 udelay(5);
1054 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
1055 }
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -04001056
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301057skip_tx_iqcal:
Rajkumar Manoharana126ff52011-10-13 11:00:42 +05301058 if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301059 /* Calibrate the AGC */
1060 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
1061 REG_READ(ah, AR_PHY_AGC_CONTROL) |
1062 AR_PHY_AGC_CONTROL_CAL);
1063
1064 /* Poll for offset calibration complete */
1065 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1066 AR_PHY_AGC_CONTROL_CAL,
1067 0, AH_WAIT_TIMEOUT);
Rajkumar Manoharan0b6eb362012-11-09 14:51:30 +05301068 if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
1069 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1070 if (!(ah->rxchainmask & (1 << i)))
1071 continue;
1072 ar9003_hw_manual_peak_cal(ah, i,
1073 IS_CHAN_2GHZ(chan));
1074 }
1075 }
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301076 }
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +05301077
Sujith Manoharan5955b2b2012-06-04 16:27:30 +05301078 if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
Sujith Manoharanf2f408e2012-02-22 12:41:06 +05301079 ar9003_mci_init_cal_done(ah);
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +05301080
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301081 if (rtt && !run_rtt_cal) {
1082 agc_ctrl |= agc_supp_cals;
1083 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1084 }
1085
1086 if (!status) {
1087 if (run_rtt_cal)
1088 ar9003_hw_rtt_disable(ah);
1089
Joe Perchesd2182b62011-12-15 14:55:53 -08001090 ath_dbg(common, CALIBRATE,
1091 "offset calibration failed to complete in 1ms; noisy environment?\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001092 return false;
1093 }
1094
Rajkumar Manoharan3782c692011-04-24 21:34:39 +05301095 if (txiqcal_done)
Rajkumar Manoharan34013522011-10-13 11:00:36 +05301096 ar9003_hw_tx_iq_cal_post_proc(ah, is_reusable);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301097 else if (caldata && caldata->done_txiqcal_once)
1098 ar9003_hw_tx_iq_cal_reload(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -08001099
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301100#define CL_TAB_ENTRY(reg_base) (reg_base + (4 * j))
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301101 if (caldata && (ah->enabled_cals & TX_CL_CAL)) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301102 txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
1103 AR_PHY_AGC_CONTROL_CLC_SUCCESS);
1104 if (caldata->done_txclcal_once) {
1105 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1106 if (!(ah->txchainmask & (1 << i)))
1107 continue;
1108 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1109 REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
1110 caldata->tx_clcal[i][j]);
1111 }
1112 } else if (is_reusable && txclcal_done) {
1113 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1114 if (!(ah->txchainmask & (1 << i)))
1115 continue;
1116 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1117 caldata->tx_clcal[i][j] =
1118 REG_READ(ah,
1119 CL_TAB_ENTRY(cl_idx[i]));
1120 }
1121 caldata->done_txclcal_once = true;
1122 }
1123 }
1124#undef CL_TAB_ENTRY
1125
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301126 if (run_rtt_cal && caldata) {
Sujith Manoharan8a905552012-05-04 13:23:59 +05301127 if (is_reusable) {
1128 if (!ath9k_hw_rfbus_req(ah))
1129 ath_err(ath9k_hw_common(ah),
1130 "Could not stop baseband\n");
1131 else
1132 ar9003_hw_rtt_fill_hist(ah);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301133
Sujith Manoharan8a905552012-05-04 13:23:59 +05301134 ath9k_hw_rfbus_done(ah);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301135 }
1136
1137 ar9003_hw_rtt_disable(ah);
1138 }
1139
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001140 /* Initialize list pointers */
1141 ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
1142
Sujith Manoharanb6869292013-01-03 12:21:21 +05301143 INIT_CAL(&ah->iq_caldata);
1144 INSERT_CAL(ah, &ah->iq_caldata);
1145 ath_dbg(common, CALIBRATE, "enabling IQ Calibration\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001146
Felix Fietkau64978272010-10-03 19:07:16 +02001147 if (ah->supp_cals & TEMP_COMP_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001148 INIT_CAL(&ah->tempCompCalData);
1149 INSERT_CAL(ah, &ah->tempCompCalData);
Joe Perchesd2182b62011-12-15 14:55:53 -08001150 ath_dbg(common, CALIBRATE,
1151 "enabling Temperature Compensation Calibration\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001152 }
1153
1154 /* Initialize current pointer to first element in list */
1155 ah->cal_list_curr = ah->cal_list;
1156
1157 if (ah->cal_list_curr)
1158 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1159
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301160 if (caldata)
1161 caldata->CalValid = 0;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001162
1163 return true;
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -04001164}
1165
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001166void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
1167{
1168 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1169 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1170
1171 priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
1172 priv_ops->init_cal = ar9003_hw_init_cal;
1173 priv_ops->setup_calibration = ar9003_hw_setup_calibration;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001174
1175 ops->calibrate = ar9003_hw_calibrate;
1176}