kernel - Misc atheros updates
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5416 / ar5416_cal_iq.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4  *
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.
8  *
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.
16  *
17  * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_cal_iq.c 203158 2010-01-29 10:07:17Z rpaulo $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_devid.h"
25
26 #include "ar5416/ar5416.h"
27 #include "ar5416/ar5416reg.h"
28 #include "ar5416/ar5416phy.h"
29
30 /* IQ Cal aliases */
31 #define totalPowerMeasI(i)      caldata[0][i].u
32 #define totalPowerMeasQ(i)      caldata[1][i].u
33 #define totalIqCorrMeas(i)      caldata[2][i].s
34
35 /*
36  * Collect data from HW to later perform IQ Mismatch Calibration
37  */
38 void
39 ar5416IQCalCollect(struct ath_hal *ah)
40 {
41         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
42         int i;
43
44         /* 
45          * Accumulate IQ cal measures for active chains
46          */
47         for (i = 0; i < AR5416_MAX_CHAINS; i++) {
48                 cal->totalPowerMeasI(i) +=
49                     OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
50                 cal->totalPowerMeasQ(i) +=
51                     OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
52                 cal->totalIqCorrMeas(i) += (int32_t)
53                     OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
54                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
55                     "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
56                     cal->calSamples, i, cal->totalPowerMeasI(i),
57                     cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));
58         }
59 }
60
61 /*
62  * Use HW data to do IQ Mismatch Calibration
63  */
64 void
65 ar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)
66 {
67         struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
68         int i;
69
70         for (i = 0; i < numChains; i++) {
71                 uint32_t powerMeasI = cal->totalPowerMeasI(i);
72                 uint32_t powerMeasQ = cal->totalPowerMeasQ(i);
73                 uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);
74                 uint32_t qCoffDenom, iCoffDenom;
75                 int iqCorrNeg;
76
77                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
78                     "Start IQ Cal and Correction for Chain %d\n", i);
79                 HALDEBUG(ah, HAL_DEBUG_PERCAL,
80                     "Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);
81
82                 iqCorrNeg = 0;
83                 /* iqCorrMeas is always negative. */ 
84                 if (iqCorrMeas > 0x80000000)  {
85                         iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
86                         iqCorrNeg = 1;
87                 }
88
89                 HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",
90                     powerMeasI);
91                 HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",
92                     powerMeasQ);
93                 HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",
94                     iqCorrNeg);
95
96                 iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;
97                 qCoffDenom = powerMeasQ / 64;
98                 /* Protect against divide-by-0 */
99                 if (powerMeasQ != 0) {
100                         /* IQ corr_meas is already negated if iqcorr_neg == 1 */
101                         int32_t iCoff = iqCorrMeas/iCoffDenom;
102                         int32_t qCoff = powerMeasI/qCoffDenom - 64;
103
104                         HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",
105                             iCoff);
106                         HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",
107                             qCoff);
108          
109                         /* Negate iCoff if iqCorrNeg == 0 */
110                         iCoff = iCoff & 0x3f;
111                         HALDEBUG(ah, HAL_DEBUG_PERCAL,
112                             "New:  iCoff = 0x%08x\n", iCoff);
113
114                         if (iqCorrNeg == 0x0)
115                                 iCoff = 0x40 - iCoff;
116                         if (qCoff > 15)
117                                 qCoff = 15;
118                         else if (qCoff <= -16)
119                                 qCoff = -16;
120                         HALDEBUG(ah, HAL_DEBUG_PERCAL,
121                             " : iCoff = 0x%x  qCoff = 0x%x\n", iCoff, qCoff);
122
123                         OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
124                             AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
125                         OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
126                             AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
127                         HALDEBUG(ah, HAL_DEBUG_PERCAL,
128                             "IQ Cal and Correction done for Chain %d\n", i);
129                 }
130         }
131         OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
132             AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
133 }