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  * $Id: ar5416_cal_adcgain.c,v 1.1.1.1 2008/12/11 04:46:48 alc Exp $
18  */
19 #include "opt_ah.h"
20 
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.h"
24 
25 #include "ar5416/ar5416.h"
26 #include "ar5416/ar5416reg.h"
27 #include "ar5416/ar5416phy.h"
28 
29 /* Adc Gain Cal aliases */
30 #define   totalAdcIOddPhase(i)          caldata[0][i].u
31 #define   totalAdcIEvenPhase(i)         caldata[1][i].u
32 #define   totalAdcQOddPhase(i)          caldata[2][i].u
33 #define   totalAdcQEvenPhase(i)         caldata[3][i].u
34 
35 /*
36  * Collect data from HW to later perform ADC Gain Calibration
37  */
38 void
ar5416AdcGainCalCollect(struct ath_hal * ah)39 ar5416AdcGainCalCollect(struct ath_hal *ah)
40 {
41           struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
42           int i;
43 
44           /*
45           * Accumulate ADC Gain cal measures for active chains
46           */
47           for (i = 0; i < AR5416_MAX_CHAINS; i++) {
48                     cal->totalAdcIOddPhase(i) +=
49                         OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
50                     cal->totalAdcIEvenPhase(i) +=
51                         OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
52                     cal->totalAdcQOddPhase(i) +=
53                         OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
54                     cal->totalAdcQEvenPhase(i) +=
55                         OS_REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
56 
57                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
58                         "%d: Chn %d oddi=0x%08x; eveni=0x%08x; oddq=0x%08x; evenq=0x%08x;\n",
59                         cal->calSamples, i, cal->totalAdcIOddPhase(i),
60                         cal->totalAdcIEvenPhase(i), cal->totalAdcQOddPhase(i),
61                         cal->totalAdcQEvenPhase(i));
62           }
63 }
64 
65 /*
66  * Use HW data to do ADC Gain Calibration
67  */
68 void
ar5416AdcGainCalibration(struct ath_hal * ah,uint8_t numChains)69 ar5416AdcGainCalibration(struct ath_hal *ah, uint8_t numChains)
70 {
71           struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
72           uint32_t i;
73 
74           for (i = 0; i < numChains; i++) {
75                     uint32_t iOddMeasOffset  = cal->totalAdcIOddPhase(i);
76                     uint32_t iEvenMeasOffset = cal->totalAdcIEvenPhase(i);
77                     uint32_t qOddMeasOffset  = cal->totalAdcQOddPhase(i);
78                     uint32_t qEvenMeasOffset = cal->totalAdcQEvenPhase(i);
79 
80                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
81                         "Start ADC Gain Cal for Chain %d\n", i);
82                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
83                         "  pwr_meas_odd_i = 0x%08x\n", iOddMeasOffset);
84                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
85                         "  pwr_meas_even_i = 0x%08x\n", iEvenMeasOffset);
86                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
87                         "  pwr_meas_odd_q = 0x%08x\n", qOddMeasOffset);
88                     HALDEBUG(ah, HAL_DEBUG_PERCAL,
89                         "  pwr_meas_even_q = 0x%08x\n", qEvenMeasOffset);
90 
91                     if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
92                               uint32_t iGainMismatch =
93                                   ((iEvenMeasOffset*32)/iOddMeasOffset) & 0x3f;
94                               uint32_t qGainMismatch =
95                                   ((qOddMeasOffset*32)/qEvenMeasOffset) & 0x3f;
96                               uint32_t val;
97 
98                               HALDEBUG(ah, HAL_DEBUG_PERCAL,
99                                   " gain_mismatch_i = 0x%08x\n",
100                                   iGainMismatch);
101                               HALDEBUG(ah, HAL_DEBUG_PERCAL,
102                                   " gain_mismatch_q = 0x%08x\n",
103                                   qGainMismatch);
104 
105                               val = OS_REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
106                               val &= 0xfffff000;
107                               val |= (qGainMismatch) | (iGainMismatch << 6);
108                               OS_REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
109 
110                               HALDEBUG(ah,  HAL_DEBUG_PERCAL,
111                                   "ADC Gain Cal done for Chain %d\n", i);
112                     }
113           }
114           OS_REG_SET_BIT(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
115               AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
116 }
117