No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / isc / atheros_hal / dist / ar5416 / ar5416_cal_iq.c
blob229d095ddc08a594731ca276771c51ccd73e85c0
1 /*
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id: ar5416_cal_iq.c,v 1.1.1.1 2008/12/11 04:46:48 alc Exp $
19 #include "opt_ah.h"
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.h"
25 #include "ar5416/ar5416.h"
26 #include "ar5416/ar5416reg.h"
27 #include "ar5416/ar5416phy.h"
29 /* IQ Cal aliases */
30 #define totalPowerMeasI(i) caldata[0][i].u
31 #define totalPowerMeasQ(i) caldata[1][i].u
32 #define totalIqCorrMeas(i) caldata[2][i].s
35 * Collect data from HW to later perform IQ Mismatch Calibration
37 void
38 ar5416IQCalCollect(struct ath_hal *ah)
40 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
41 int i;
43 /*
44 * Accumulate IQ cal measures for active chains
46 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
47 cal->totalPowerMeasI(i) +=
48 OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
49 cal->totalPowerMeasQ(i) +=
50 OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
51 cal->totalIqCorrMeas(i) += (int32_t)
52 OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
53 HALDEBUG(ah, HAL_DEBUG_PERCAL,
54 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
55 cal->calSamples, i, cal->totalPowerMeasI(i),
56 cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));
61 * Use HW data to do IQ Mismatch Calibration
63 void
64 ar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)
66 struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
67 int i;
69 for (i = 0; i < numChains; i++) {
70 uint32_t powerMeasI = cal->totalPowerMeasI(i);
71 uint32_t powerMeasQ = cal->totalPowerMeasQ(i);
72 uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);
73 uint32_t qCoffDenom, iCoffDenom;
74 int iqCorrNeg;
76 HALDEBUG(ah, HAL_DEBUG_PERCAL,
77 "Start IQ Cal and Correction for Chain %d\n", i);
78 HALDEBUG(ah, HAL_DEBUG_PERCAL,
79 "Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);
81 iqCorrNeg = 0;
82 /* iqCorrMeas is always negative. */
83 if (iqCorrMeas > 0x80000000) {
84 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
85 iqCorrNeg = 1;
88 HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",
89 powerMeasI);
90 HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",
91 powerMeasQ);
92 HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",
93 iqCorrNeg);
95 iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;
96 qCoffDenom = powerMeasQ / 64;
97 /* Protect against divide-by-0 */
98 if (powerMeasQ != 0) {
99 /* IQ corr_meas is already negated if iqcorr_neg == 1 */
100 int32_t iCoff = iqCorrMeas/iCoffDenom;
101 int32_t qCoff = powerMeasI/qCoffDenom - 64;
103 HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",
104 iCoff);
105 HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",
106 qCoff);
108 /* Negate iCoff if iqCorrNeg == 0 */
109 iCoff = iCoff & 0x3f;
110 HALDEBUG(ah, HAL_DEBUG_PERCAL,
111 "New: iCoff = 0x%08x\n", iCoff);
113 if (iqCorrNeg == 0x0)
114 iCoff = 0x40 - iCoff;
115 if (qCoff > 15)
116 qCoff = 15;
117 else if (qCoff <= -16)
118 qCoff = 16;
119 HALDEBUG(ah, HAL_DEBUG_PERCAL,
120 " : iCoff = 0x%x qCoff = 0x%x\n", iCoff, qCoff);
122 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
123 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
124 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
125 AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
126 HALDEBUG(ah, HAL_DEBUG_PERCAL,
127 "IQ Cal and Correction done for Chain %d\n", i);
130 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
131 AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);