blob: 23b3a6c578009051c333e70f619952bd3130b3bf [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 Perches226afe62010-12-02 19:12:37 -080055 ath_dbg(common, ATH_DBG_CALIBRATE,
56 "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 Perches226afe62010-12-02 19:12:37 -080067 ath_dbg(common, ATH_DBG_CALIBRATE,
68 "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
162 /* Do NF cal only at longer intervals */
163 if (longcal) {
164 /*
Felix Fietkau93697462010-07-30 21:02:10 +0200165 * Get the value from the previous NF cal and update
166 * history buffer.
167 */
168 ath9k_hw_getnf(ah, chan);
169
170 /*
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400171 * Load the NF from history buffer of the current channel.
172 * NF is slow time-variant, so it is OK to use a historical
173 * value.
174 */
175 ath9k_hw_loadnf(ah, ah->curchan);
176
177 /* start NF calibration, without updating BB NF register */
Felix Fietkau00c86592010-07-30 21:02:09 +0200178 ath9k_hw_start_nfcal(ah, false);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400179 }
180
181 return iscaldone;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400182}
183
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400184static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
185{
186 int i;
187
188 /* Accumulate IQ cal measures for active chains */
189 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Vasanthakumar Thiagarajan5d48ae72011-04-19 19:29:16 +0530190 if (ah->txchainmask & BIT(i)) {
191 ah->totalPowerMeasI[i] +=
192 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
193 ah->totalPowerMeasQ[i] +=
194 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
195 ah->totalIqCorrMeas[i] +=
196 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
197 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
198 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
199 ah->cal_samples, i, ah->totalPowerMeasI[i],
200 ah->totalPowerMeasQ[i],
201 ah->totalIqCorrMeas[i]);
202 }
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400203 }
204}
205
206static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
207{
208 struct ath_common *common = ath9k_hw_common(ah);
209 u32 powerMeasQ, powerMeasI, iqCorrMeas;
210 u32 qCoffDenom, iCoffDenom;
211 int32_t qCoff, iCoff;
212 int iqCorrNeg, i;
Joe Perches07b2fa52010-11-20 18:38:53 -0800213 static const u_int32_t offset_array[3] = {
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400214 AR_PHY_RX_IQCAL_CORR_B0,
215 AR_PHY_RX_IQCAL_CORR_B1,
216 AR_PHY_RX_IQCAL_CORR_B2,
217 };
218
219 for (i = 0; i < numChains; i++) {
220 powerMeasI = ah->totalPowerMeasI[i];
221 powerMeasQ = ah->totalPowerMeasQ[i];
222 iqCorrMeas = ah->totalIqCorrMeas[i];
223
Joe Perches226afe62010-12-02 19:12:37 -0800224 ath_dbg(common, ATH_DBG_CALIBRATE,
225 "Starting IQ Cal and Correction for Chain %d\n",
226 i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400227
Joe Perches226afe62010-12-02 19:12:37 -0800228 ath_dbg(common, ATH_DBG_CALIBRATE,
Nikolay Martynova9b2ce02011-12-02 22:39:14 -0500229 "Original: Chn %d iq_corr_meas = 0x%08x\n",
Joe Perches226afe62010-12-02 19:12:37 -0800230 i, ah->totalIqCorrMeas[i]);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400231
232 iqCorrNeg = 0;
233
234 if (iqCorrMeas > 0x80000000) {
235 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
236 iqCorrNeg = 1;
237 }
238
Joe Perches226afe62010-12-02 19:12:37 -0800239 ath_dbg(common, ATH_DBG_CALIBRATE,
240 "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
241 ath_dbg(common, ATH_DBG_CALIBRATE,
242 "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
243 ath_dbg(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
244 iqCorrNeg);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400245
246 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
247 qCoffDenom = powerMeasQ / 64;
248
249 if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
250 iCoff = iqCorrMeas / iCoffDenom;
251 qCoff = powerMeasI / qCoffDenom - 64;
Joe Perches226afe62010-12-02 19:12:37 -0800252 ath_dbg(common, ATH_DBG_CALIBRATE,
253 "Chn %d iCoff = 0x%08x\n", i, iCoff);
254 ath_dbg(common, ATH_DBG_CALIBRATE,
255 "Chn %d qCoff = 0x%08x\n", i, qCoff);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400256
257 /* Force bounds on iCoff */
258 if (iCoff >= 63)
259 iCoff = 63;
260 else if (iCoff <= -63)
261 iCoff = -63;
262
263 /* Negate iCoff if iqCorrNeg == 0 */
264 if (iqCorrNeg == 0x0)
265 iCoff = -iCoff;
266
267 /* Force bounds on qCoff */
268 if (qCoff >= 63)
269 qCoff = 63;
270 else if (qCoff <= -63)
271 qCoff = -63;
272
273 iCoff = iCoff & 0x7f;
274 qCoff = qCoff & 0x7f;
275
Joe Perches226afe62010-12-02 19:12:37 -0800276 ath_dbg(common, ATH_DBG_CALIBRATE,
277 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
278 i, iCoff, qCoff);
279 ath_dbg(common, ATH_DBG_CALIBRATE,
280 "Register offset (0x%04x) before update = 0x%x\n",
281 offset_array[i],
282 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400283
284 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 Perches226afe62010-12-02 19:12:37 -0800290 ath_dbg(common, ATH_DBG_CALIBRATE,
291 "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]));
295 ath_dbg(common, ATH_DBG_CALIBRATE,
296 "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 Perches226afe62010-12-02 19:12:37 -0800301 ath_dbg(common, ATH_DBG_CALIBRATE,
302 "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 Perches226afe62010-12-02 19:12:37 -0800308 ath_dbg(common, ATH_DBG_CALIBRATE,
309 "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;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400326}
327
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400328/*
329 * solve 4x4 linear equation used in loopback iq cal.
330 */
331static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
332 s32 sin_2phi_1,
333 s32 cos_2phi_1,
334 s32 sin_2phi_2,
335 s32 cos_2phi_2,
336 s32 mag_a0_d0,
337 s32 phs_a0_d0,
338 s32 mag_a1_d0,
339 s32 phs_a1_d0,
340 s32 solved_eq[])
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -0400341{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400342 s32 f1 = cos_2phi_1 - cos_2phi_2,
343 f3 = sin_2phi_1 - sin_2phi_2,
344 f2;
345 s32 mag_tx, phs_tx, mag_rx, phs_rx;
346 const s32 result_shift = 1 << 15;
347 struct ath_common *common = ath9k_hw_common(ah);
348
349 f2 = (f1 * f1 + f3 * f3) / result_shift;
350
351 if (!f2) {
Joe Perches226afe62010-12-02 19:12:37 -0800352 ath_dbg(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400353 return false;
354 }
355
356 /* mag mismatch, tx */
357 mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
358 /* phs mismatch, tx */
359 phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
360
361 mag_tx = (mag_tx / f2);
362 phs_tx = (phs_tx / f2);
363
364 /* mag mismatch, rx */
365 mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
366 result_shift;
367 /* phs mismatch, rx */
368 phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
369 result_shift;
370
371 solved_eq[0] = mag_tx;
372 solved_eq[1] = phs_tx;
373 solved_eq[2] = mag_rx;
374 solved_eq[3] = phs_rx;
375
376 return true;
377}
378
379static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
380{
381 s32 abs_i = abs(in_re),
382 abs_q = abs(in_im),
383 max_abs, min_abs;
384
385 if (abs_i > abs_q) {
386 max_abs = abs_i;
387 min_abs = abs_q;
388 } else {
389 max_abs = abs_q;
390 min_abs = abs_i;
391 }
392
393 return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
394}
395
396#define DELPT 32
397
398static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
399 s32 chain_idx,
400 const s32 iq_res[],
401 s32 iqc_coeff[])
402{
403 s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
404 i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
405 i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
406 i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
407 s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
408 phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
409 sin_2phi_1, cos_2phi_1,
410 sin_2phi_2, cos_2phi_2;
411 s32 mag_tx, phs_tx, mag_rx, phs_rx;
412 s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
413 q_q_coff, q_i_coff;
414 const s32 res_scale = 1 << 15;
415 const s32 delpt_shift = 1 << 8;
416 s32 mag1, mag2;
417 struct ath_common *common = ath9k_hw_common(ah);
418
419 i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
420 i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
421 iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
422
423 if (i2_m_q2_a0_d0 > 0x800)
424 i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
425
426 if (i2_p_q2_a0_d0 > 0x800)
427 i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
428
429 if (iq_corr_a0_d0 > 0x800)
430 iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
431
432 i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
433 i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
434 iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
435
436 if (i2_m_q2_a0_d1 > 0x800)
437 i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
438
439 if (i2_p_q2_a0_d1 > 0x800)
440 i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
441
442 if (iq_corr_a0_d1 > 0x800)
443 iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
444
445 i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
446 i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
447 iq_corr_a1_d0 = iq_res[4] & 0xfff;
448
449 if (i2_m_q2_a1_d0 > 0x800)
450 i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
451
452 if (i2_p_q2_a1_d0 > 0x800)
453 i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
454
455 if (iq_corr_a1_d0 > 0x800)
456 iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
457
458 i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
459 i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
460 iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
461
462 if (i2_m_q2_a1_d1 > 0x800)
463 i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
464
465 if (i2_p_q2_a1_d1 > 0x800)
466 i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
467
468 if (iq_corr_a1_d1 > 0x800)
469 iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
470
471 if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
472 (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800473 ath_dbg(common, ATH_DBG_CALIBRATE,
474 "Divide by 0:\n"
475 "a0_d0=%d\n"
476 "a0_d1=%d\n"
477 "a2_d0=%d\n"
478 "a1_d1=%d\n",
479 i2_p_q2_a0_d0, i2_p_q2_a0_d1,
480 i2_p_q2_a1_d0, i2_p_q2_a1_d1);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400481 return false;
482 }
483
484 mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
485 phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
486
487 mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
488 phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
489
490 mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
491 phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
492
493 mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
494 phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
495
496 /* w/o analog phase shift */
497 sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
498 /* w/o analog phase shift */
499 cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
500 /* w/ analog phase shift */
501 sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
502 /* w/ analog phase shift */
503 cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
504
505 /*
506 * force sin^2 + cos^2 = 1;
507 * find magnitude by approximation
508 */
509 mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
510 mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
511
512 if ((mag1 == 0) || (mag2 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800513 ath_dbg(common, ATH_DBG_CALIBRATE,
514 "Divide by 0: mag1=%d, mag2=%d\n",
515 mag1, mag2);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400516 return false;
517 }
518
519 /* normalization sin and cos by mag */
520 sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
521 cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
522 sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
523 cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
524
525 /* calculate IQ mismatch */
526 if (!ar9003_hw_solve_iq_cal(ah,
527 sin_2phi_1, cos_2phi_1,
528 sin_2phi_2, cos_2phi_2,
529 mag_a0_d0, phs_a0_d0,
530 mag_a1_d0,
531 phs_a1_d0, solved_eq)) {
Joe Perches226afe62010-12-02 19:12:37 -0800532 ath_dbg(common, ATH_DBG_CALIBRATE,
533 "Call to ar9003_hw_solve_iq_cal() failed.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400534 return false;
535 }
536
537 mag_tx = solved_eq[0];
538 phs_tx = solved_eq[1];
539 mag_rx = solved_eq[2];
540 phs_rx = solved_eq[3];
541
Joe Perches226afe62010-12-02 19:12:37 -0800542 ath_dbg(common, ATH_DBG_CALIBRATE,
543 "chain %d: mag mismatch=%d phase mismatch=%d\n",
544 chain_idx, mag_tx/res_scale, phs_tx/res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400545
546 if (res_scale == mag_tx) {
Joe Perches226afe62010-12-02 19:12:37 -0800547 ath_dbg(common, ATH_DBG_CALIBRATE,
548 "Divide by 0: mag_tx=%d, res_scale=%d\n",
549 mag_tx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400550 return false;
551 }
552
553 /* calculate and quantize Tx IQ correction factor */
554 mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
555 phs_corr_tx = -phs_tx;
556
557 q_q_coff = (mag_corr_tx * 128 / res_scale);
558 q_i_coff = (phs_corr_tx * 256 / res_scale);
559
Joe Perches226afe62010-12-02 19:12:37 -0800560 ath_dbg(common, ATH_DBG_CALIBRATE,
561 "tx chain %d: mag corr=%d phase corr=%d\n",
562 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400563
564 if (q_i_coff < -63)
565 q_i_coff = -63;
566 if (q_i_coff > 63)
567 q_i_coff = 63;
568 if (q_q_coff < -63)
569 q_q_coff = -63;
570 if (q_q_coff > 63)
571 q_q_coff = 63;
572
573 iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
574
Joe Perches226afe62010-12-02 19:12:37 -0800575 ath_dbg(common, ATH_DBG_CALIBRATE,
576 "tx chain %d: iq corr coeff=%x\n",
577 chain_idx, iqc_coeff[0]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400578
579 if (-mag_rx == res_scale) {
Joe Perches226afe62010-12-02 19:12:37 -0800580 ath_dbg(common, ATH_DBG_CALIBRATE,
581 "Divide by 0: mag_rx=%d, res_scale=%d\n",
582 mag_rx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400583 return false;
584 }
585
586 /* calculate and quantize Rx IQ correction factors */
587 mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
588 phs_corr_rx = -phs_rx;
589
590 q_q_coff = (mag_corr_rx * 128 / res_scale);
591 q_i_coff = (phs_corr_rx * 256 / res_scale);
592
Joe Perches226afe62010-12-02 19:12:37 -0800593 ath_dbg(common, ATH_DBG_CALIBRATE,
594 "rx chain %d: mag corr=%d phase corr=%d\n",
595 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400596
597 if (q_i_coff < -63)
598 q_i_coff = -63;
599 if (q_i_coff > 63)
600 q_i_coff = 63;
601 if (q_q_coff < -63)
602 q_q_coff = -63;
603 if (q_q_coff > 63)
604 q_q_coff = 63;
605
606 iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
607
Joe Perches226afe62010-12-02 19:12:37 -0800608 ath_dbg(common, ATH_DBG_CALIBRATE,
609 "rx chain %d: iq corr coeff=%x\n",
610 chain_idx, iqc_coeff[1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400611
612 return true;
613}
614
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530615static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
616 int max_delta)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800617{
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530618 int mp_max = -64, max_idx = 0;
619 int mp_min = 63, min_idx = 0;
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530620 int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800621
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530622 /* find min/max mismatch across all calibrated gains */
623 for (i = 0; i < nmeasurement; i++) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530624 if (mp_coeff[i] > mp_max) {
625 mp_max = mp_coeff[i];
626 max_idx = i;
627 } else if (mp_coeff[i] < mp_min) {
628 mp_min = mp_coeff[i];
629 min_idx = i;
630 }
631 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800632
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530633 /* find average (exclude max abs value) */
634 for (i = 0; i < nmeasurement; i++) {
635 if ((abs(mp_coeff[i]) < abs(mp_max)) ||
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530636 (abs(mp_coeff[i]) < abs(mp_min))) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530637 mp_avg += mp_coeff[i];
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530638 mp_count++;
639 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530640 }
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530641
642 /*
643 * finding mean magnitude/phase if possible, otherwise
644 * just use the last value as the mean
645 */
646 if (mp_count)
647 mp_avg /= mp_count;
648 else
649 mp_avg = mp_coeff[nmeasurement - 1];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800650
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530651 /* detect outlier */
652 if (abs(mp_max - mp_min) > max_delta) {
653 if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
654 outlier_idx = max_idx;
655 else
656 outlier_idx = min_idx;
Rajkumar Manoharane9c10462011-09-15 19:02:25 +0530657
658 mp_coeff[outlier_idx] = mp_avg;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530659 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800660}
661
662static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
663 u8 num_chains,
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530664 struct coeff *coeff,
665 bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800666{
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800667 int i, im, nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800668 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530669 struct ath9k_hw_cal_data *caldata = ah->caldata;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800670
671 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
672 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
673 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
674 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
675 if (!AR_SREV_9485(ah)) {
676 tx_corr_coeff[i * 2][1] =
677 tx_corr_coeff[(i * 2) + 1][1] =
678 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
679
680 tx_corr_coeff[i * 2][2] =
681 tx_corr_coeff[(i * 2) + 1][2] =
682 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
683 }
684 }
685
686 /* Load the average of 2 passes */
687 for (i = 0; i < num_chains; i++) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530688 nmeasurement = REG_READ_FIELD(ah,
689 AR_PHY_TX_IQCAL_STATUS_B0,
690 AR_PHY_CALIBRATED_GAINS_0);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800691
692 if (nmeasurement > MAX_MEASUREMENT)
693 nmeasurement = MAX_MEASUREMENT;
694
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530695 /* detect outlier only if nmeasurement > 1 */
696 if (nmeasurement > 1) {
697 /* Detect magnitude outlier */
698 ar9003_hw_detect_outlier(coeff->mag_coeff[i],
699 nmeasurement, MAX_MAG_DELTA);
700
701 /* Detect phase outlier */
702 ar9003_hw_detect_outlier(coeff->phs_coeff[i],
703 nmeasurement, MAX_PHS_DELTA);
704 }
705
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800706 for (im = 0; im < nmeasurement; im++) {
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800707
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530708 coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
709 ((coeff->phs_coeff[i][im] & 0x7f) << 7);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800710
711 if ((im % 2) == 0)
712 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
713 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
714 coeff->iqc_coeff[0]);
715 else
716 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
717 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
718 coeff->iqc_coeff[0]);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530719
720 if (caldata)
721 caldata->tx_corr_coeff[im][i] =
722 coeff->iqc_coeff[0];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800723 }
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530724 if (caldata)
725 caldata->num_measures[i] = nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800726 }
727
728 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
729 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
730 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
731 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530732
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530733 if (caldata)
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530734 caldata->done_txiqcal_once = is_reusable;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800735
736 return;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800737}
738
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530739static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
Vasanthakumar Thiagarajan0b2084b2010-12-15 07:30:50 -0800740{
741 struct ath_common *common = ath9k_hw_common(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800742 u8 tx_gain_forced;
743
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800744 tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
745 AR_PHY_TXGAIN_FORCE);
746 if (tx_gain_forced)
747 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
748 AR_PHY_TXGAIN_FORCE, 0);
749
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530750 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
751 AR_PHY_TX_IQCAL_START_DO_CAL, 1);
752
753 if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
754 AR_PHY_TX_IQCAL_START_DO_CAL, 0,
755 AH_WAIT_TIMEOUT)) {
756 ath_dbg(common, ATH_DBG_CALIBRATE,
757 "Tx IQ Cal is not completed.\n");
758 return false;
759 }
760 return true;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800761}
762
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530763static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah, bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800764{
765 struct ath_common *common = ath9k_hw_common(ah);
766 const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530767 AR_PHY_TX_IQCAL_STATUS_B0,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800768 AR_PHY_TX_IQCAL_STATUS_B1,
769 AR_PHY_TX_IQCAL_STATUS_B2,
770 };
771 const u_int32_t chan_info_tab[] = {
772 AR_PHY_CHAN_INFO_TAB_0,
773 AR_PHY_CHAN_INFO_TAB_1,
774 AR_PHY_CHAN_INFO_TAB_2,
775 };
776 struct coeff coeff;
777 s32 iq_res[6];
778 u8 num_chains = 0;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530779 int i, im, j;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800780 int nmeasurement;
781
782 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
783 if (ah->txchainmask & (1 << i))
784 num_chains++;
785 }
786
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530787 for (i = 0; i < num_chains; i++) {
788 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++) {
795 ath_dbg(common, ATH_DBG_CALIBRATE,
796 "Doing Tx IQ Cal for chain %d.\n", i);
797
798 if (REG_READ(ah, txiqcal_status[i]) &
799 AR_PHY_TX_IQCAL_STATUS_FAILED) {
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800800 ath_dbg(common, ATH_DBG_CALIBRATE,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800801 "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
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530827 ath_dbg(common, ATH_DBG_CALIBRATE,
Mohammed Shafi Shajakhane170d182011-11-22 20:15:56 +0530828 "IQ_RES[%d]=0x%x "
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530829 "IQ_RES[%d]=0x%x\n",
830 idx, iq_res[idx], idx + 1,
831 iq_res[idx + 1]);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800832 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530833
834 if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
835 coeff.iqc_coeff)) {
836 ath_dbg(common, ATH_DBG_CALIBRATE,
837 "Failed in calculation of \
838 IQ correction.\n");
839 goto tx_iqcal_fail;
840 }
841
842 coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
843 coeff.phs_coeff[i][im] =
844 (coeff.iqc_coeff[0] >> 7) & 0x7f;
845
846 if (coeff.mag_coeff[i][im] > 63)
847 coeff.mag_coeff[i][im] -= 128;
848 if (coeff.phs_coeff[i][im] > 63)
849 coeff.phs_coeff[i][im] -= 128;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800850 }
851 }
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530852 ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains,
853 &coeff, is_reusable);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800854
855 return;
856
857tx_iqcal_fail:
858 ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
859 return;
860}
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530861
862static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
863{
864 struct ath9k_hw_cal_data *caldata = ah->caldata;
865 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
866 int i, im;
867
868 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
869 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
870 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
871 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
872 if (!AR_SREV_9485(ah)) {
873 tx_corr_coeff[i * 2][1] =
874 tx_corr_coeff[(i * 2) + 1][1] =
875 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
876
877 tx_corr_coeff[i * 2][2] =
878 tx_corr_coeff[(i * 2) + 1][2] =
879 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
880 }
881 }
882
883 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
884 if (!(ah->txchainmask & (1 << i)))
885 continue;
886
887 for (im = 0; im < caldata->num_measures[i]; im++) {
888 if ((im % 2) == 0)
889 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
890 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
891 caldata->tx_corr_coeff[im][i]);
892 else
893 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
894 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
895 caldata->tx_corr_coeff[im][i]);
896 }
897 }
898
899 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
900 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
901 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
902 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
903}
904
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530905static bool ar9003_hw_rtt_restore(struct ath_hw *ah, struct ath9k_channel *chan)
906{
907 struct ath9k_rtt_hist *hist;
908 u32 *table;
909 int i;
910 bool restore;
911
Rajkumar Manoharan91ae4d02011-10-20 14:29:38 +0530912 if (!ah->caldata)
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530913 return false;
914
915 hist = &ah->caldata->rtt_hist;
Rajkumar Manoharan91ae4d02011-10-20 14:29:38 +0530916 if (!hist->num_readings)
917 return false;
918
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530919 ar9003_hw_rtt_enable(ah);
Rajkumar Manoharan91ae4d02011-10-20 14:29:38 +0530920 ar9003_hw_rtt_set_mask(ah, 0x00);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530921 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
922 if (!(ah->rxchainmask & (1 << i)))
923 continue;
924 table = &hist->table[i][hist->num_readings][0];
925 ar9003_hw_rtt_load_hist(ah, i, table);
926 }
927 restore = ar9003_hw_rtt_force_restore(ah);
928 ar9003_hw_rtt_disable(ah);
929
930 return restore;
931}
932
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400933static bool ar9003_hw_init_cal(struct ath_hw *ah,
934 struct ath9k_channel *chan)
935{
936 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530937 struct ath9k_hw_cal_data *caldata = ah->caldata;
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +0530938 struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530939 bool txiqcal_done = false, txclcal_done = false;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530940 bool is_reusable = true, status = true;
941 bool run_rtt_cal = false, run_agc_cal;
942 bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +0530943 bool mci = !!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530944 u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
945 AR_PHY_AGC_CONTROL_FLTR_CAL |
946 AR_PHY_AGC_CONTROL_PKDET_CAL;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530947 int i, j;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530948 u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
949 AR_PHY_CL_TAB_1,
950 AR_PHY_CL_TAB_2 };
951
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530952 if (rtt) {
953 if (!ar9003_hw_rtt_restore(ah, chan))
954 run_rtt_cal = true;
955
956 ath_dbg(common, ATH_DBG_CALIBRATE, "RTT restore %s\n",
957 run_rtt_cal ? "failed" : "succeed");
958 }
959 run_agc_cal = run_rtt_cal;
960
961 if (run_rtt_cal) {
962 ar9003_hw_rtt_enable(ah);
963 ar9003_hw_rtt_set_mask(ah, 0x00);
964 ar9003_hw_rtt_clear_hist(ah);
965 }
966
967 if (rtt && !run_rtt_cal) {
968 agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
969 agc_supp_cals &= agc_ctrl;
970 agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
971 AR_PHY_AGC_CONTROL_FLTR_CAL |
972 AR_PHY_AGC_CONTROL_PKDET_CAL);
973 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
974 }
975
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530976 if (ah->enabled_cals & TX_CL_CAL) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530977 if (caldata && caldata->done_txclcal_once)
978 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
979 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530980 else {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530981 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
982 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530983 run_agc_cal = true;
984 }
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530985 }
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400986
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530987 if (!(ah->enabled_cals & TX_IQ_CAL))
988 goto skip_tx_iqcal;
989
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -0400990 /* Do Tx IQ Calibration */
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530991 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
992 AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
993 DELPT);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800994
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530995 /*
996 * For AR9485 or later chips, TxIQ cal runs as part of
997 * AGC calibration
998 */
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530999 if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301000 if (caldata && !caldata->done_txiqcal_once)
1001 REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1002 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
1003 else
1004 REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1005 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301006 txiqcal_done = run_agc_cal = true;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301007 goto skip_tx_iqcal;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301008 } else if (caldata && !caldata->done_txiqcal_once)
1009 run_agc_cal = true;
1010
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +05301011 if (mci && IS_CHAN_2GHZ(chan) &&
1012 (mci_hw->bt_state == MCI_BT_AWAKE) &&
1013 run_agc_cal &&
1014 !(mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL)) {
1015
1016 u32 pld[4] = {0, 0, 0, 0};
1017
1018 /* send CAL_REQ only when BT is AWAKE. */
1019 ath_dbg(common, ATH_DBG_MCI, "MCI send WLAN_CAL_REQ 0x%x\n",
1020 mci_hw->wlan_cal_seq);
1021 MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_REQ);
1022 pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_seq++;
1023 ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
1024
1025 /* Wait BT_CAL_GRANT for 50ms */
1026 ath_dbg(common, ATH_DBG_MCI, "MCI wait for BT_CAL_GRANT");
1027
1028 if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_GRANT, 0, 50000))
1029 ath_dbg(common, ATH_DBG_MCI, "MCI got BT_CAL_GRANT");
1030 else {
1031 is_reusable = false;
1032 ath_dbg(common, ATH_DBG_MCI, "\nMCI BT is not responding");
1033 }
1034 }
1035
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301036 txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
1037 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1038 udelay(5);
1039 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -04001040
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301041skip_tx_iqcal:
Rajkumar Manoharana126ff52011-10-13 11:00:42 +05301042 if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301043 /* Calibrate the AGC */
1044 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
1045 REG_READ(ah, AR_PHY_AGC_CONTROL) |
1046 AR_PHY_AGC_CONTROL_CAL);
1047
1048 /* Poll for offset calibration complete */
1049 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1050 AR_PHY_AGC_CONTROL_CAL,
1051 0, AH_WAIT_TIMEOUT);
1052 }
Mohammed Shafi Shajakhan3ebfcdc2011-11-30 10:41:26 +05301053
1054 if (mci && IS_CHAN_2GHZ(chan) &&
1055 (mci_hw->bt_state == MCI_BT_AWAKE) &&
1056 run_agc_cal &&
1057 !(mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL)) {
1058
1059 u32 pld[4] = {0, 0, 0, 0};
1060
1061 ath_dbg(common, ATH_DBG_MCI, "MCI Send WLAN_CAL_DONE 0x%x\n",
1062 mci_hw->wlan_cal_done);
1063 MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_DONE);
1064 pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_done++;
1065 ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
1066 }
1067
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301068 if (rtt && !run_rtt_cal) {
1069 agc_ctrl |= agc_supp_cals;
1070 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1071 }
1072
1073 if (!status) {
1074 if (run_rtt_cal)
1075 ar9003_hw_rtt_disable(ah);
1076
Joe Perches226afe62010-12-02 19:12:37 -08001077 ath_dbg(common, ATH_DBG_CALIBRATE,
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301078 "offset calibration failed to complete in 1ms;"
1079 "noisy environment?\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001080 return false;
1081 }
1082
Rajkumar Manoharan3782c692011-04-24 21:34:39 +05301083 if (txiqcal_done)
Rajkumar Manoharan34013522011-10-13 11:00:36 +05301084 ar9003_hw_tx_iq_cal_post_proc(ah, is_reusable);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301085 else if (caldata && caldata->done_txiqcal_once)
1086 ar9003_hw_tx_iq_cal_reload(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -08001087
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301088#define CL_TAB_ENTRY(reg_base) (reg_base + (4 * j))
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301089 if (caldata && (ah->enabled_cals & TX_CL_CAL)) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301090 txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
1091 AR_PHY_AGC_CONTROL_CLC_SUCCESS);
1092 if (caldata->done_txclcal_once) {
1093 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1094 if (!(ah->txchainmask & (1 << i)))
1095 continue;
1096 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1097 REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
1098 caldata->tx_clcal[i][j]);
1099 }
1100 } else if (is_reusable && txclcal_done) {
1101 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1102 if (!(ah->txchainmask & (1 << i)))
1103 continue;
1104 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1105 caldata->tx_clcal[i][j] =
1106 REG_READ(ah,
1107 CL_TAB_ENTRY(cl_idx[i]));
1108 }
1109 caldata->done_txclcal_once = true;
1110 }
1111 }
1112#undef CL_TAB_ENTRY
1113
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301114 if (run_rtt_cal && caldata) {
1115 struct ath9k_rtt_hist *hist = &caldata->rtt_hist;
1116 if (is_reusable && (hist->num_readings < RTT_HIST_MAX)) {
1117 u32 *table;
1118
Rajkumar Manoharan91ae4d02011-10-20 14:29:38 +05301119 hist->num_readings++;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301120 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1121 if (!(ah->rxchainmask & (1 << i)))
1122 continue;
1123 table = &hist->table[i][hist->num_readings][0];
1124 ar9003_hw_rtt_fill_hist(ah, i, table);
1125 }
1126 }
1127
1128 ar9003_hw_rtt_disable(ah);
1129 }
1130
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001131 /* Initialize list pointers */
1132 ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
Felix Fietkau64978272010-10-03 19:07:16 +02001133 ah->supp_cals = IQ_MISMATCH_CAL;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001134
Felix Fietkau64978272010-10-03 19:07:16 +02001135 if (ah->supp_cals & IQ_MISMATCH_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001136 INIT_CAL(&ah->iq_caldata);
1137 INSERT_CAL(ah, &ah->iq_caldata);
Joe Perches226afe62010-12-02 19:12:37 -08001138 ath_dbg(common, ATH_DBG_CALIBRATE,
1139 "enabling IQ Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001140 }
1141
Felix Fietkau64978272010-10-03 19:07:16 +02001142 if (ah->supp_cals & TEMP_COMP_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001143 INIT_CAL(&ah->tempCompCalData);
1144 INSERT_CAL(ah, &ah->tempCompCalData);
Joe Perches226afe62010-12-02 19:12:37 -08001145 ath_dbg(common, ATH_DBG_CALIBRATE,
1146 "enabling Temperature Compensation Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001147 }
1148
1149 /* Initialize current pointer to first element in list */
1150 ah->cal_list_curr = ah->cal_list;
1151
1152 if (ah->cal_list_curr)
1153 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1154
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301155 if (caldata)
1156 caldata->CalValid = 0;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001157
1158 return true;
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -04001159}
1160
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001161void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
1162{
1163 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1164 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1165
1166 priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
1167 priv_ops->init_cal = ar9003_hw_init_cal;
1168 priv_ops->setup_calibration = ar9003_hw_setup_calibration;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001169
1170 ops->calibrate = ar9003_hw_calibrate;
1171}