xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar9002/ar9287_olc.c (revision 572ff6f6e8b95055988f178b6ba12ce77bb5b3c2)
1*572ff6f6SMatthew Dillon /*
2*572ff6f6SMatthew Dillon  * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.
3*572ff6f6SMatthew Dillon  *
4*572ff6f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5*572ff6f6SMatthew Dillon  * modification, are permitted provided that the following conditions
6*572ff6f6SMatthew Dillon  * are met:
7*572ff6f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8*572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
9*572ff6f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10*572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11*572ff6f6SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12*572ff6f6SMatthew Dillon  *
13*572ff6f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*572ff6f6SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*572ff6f6SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*572ff6f6SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*572ff6f6SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*572ff6f6SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*572ff6f6SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*572ff6f6SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*572ff6f6SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*572ff6f6SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*572ff6f6SMatthew Dillon  * SUCH DAMAGE.
24*572ff6f6SMatthew Dillon  *
25*572ff6f6SMatthew Dillon  * $FreeBSD$
26*572ff6f6SMatthew Dillon  */
27*572ff6f6SMatthew Dillon #include "opt_ah.h"
28*572ff6f6SMatthew Dillon 
29*572ff6f6SMatthew Dillon #include "ah.h"
30*572ff6f6SMatthew Dillon #include "ah_internal.h"
31*572ff6f6SMatthew Dillon 
32*572ff6f6SMatthew Dillon #include "ah_eeprom_v14.h"
33*572ff6f6SMatthew Dillon #include "ah_eeprom_9287.h"
34*572ff6f6SMatthew Dillon 
35*572ff6f6SMatthew Dillon #include "ar9002/ar9280.h"
36*572ff6f6SMatthew Dillon #include "ar5416/ar5416reg.h"
37*572ff6f6SMatthew Dillon #include "ar5416/ar5416phy.h"
38*572ff6f6SMatthew Dillon #include "ar9002/ar9002phy.h"
39*572ff6f6SMatthew Dillon 
40*572ff6f6SMatthew Dillon #include "ar9002/ar9287phy.h"
41*572ff6f6SMatthew Dillon #include "ar9002/ar9287an.h"
42*572ff6f6SMatthew Dillon #include "ar9002/ar9287_olc.h"
43*572ff6f6SMatthew Dillon 
44*572ff6f6SMatthew Dillon void
ar9287olcInit(struct ath_hal * ah)45*572ff6f6SMatthew Dillon ar9287olcInit(struct ath_hal *ah)
46*572ff6f6SMatthew Dillon {
47*572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_PHY_TX_PWRCTRL9,
48*572ff6f6SMatthew Dillon 	    AR_PHY_TX_PWRCTRL9_RES_DC_REMOVAL);
49*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9287_AN_TXPC0,
50*572ff6f6SMatthew Dillon 	    AR9287_AN_TXPC0_TXPCMODE,
51*572ff6f6SMatthew Dillon 	    AR9287_AN_TXPC0_TXPCMODE_TEMPSENSE);
52*572ff6f6SMatthew Dillon 	OS_DELAY(100);
53*572ff6f6SMatthew Dillon }
54*572ff6f6SMatthew Dillon 
55*572ff6f6SMatthew Dillon /*
56*572ff6f6SMatthew Dillon  * Run temperature compensation calibration.
57*572ff6f6SMatthew Dillon  *
58*572ff6f6SMatthew Dillon  * The TX gain table is adjusted depending upon the difference
59*572ff6f6SMatthew Dillon  * between the initial PDADC value and the currently read
60*572ff6f6SMatthew Dillon  * average TX power sample value. This value is only valid if
61*572ff6f6SMatthew Dillon  * frames have been transmitted, so currPDADC will be 0 if
62*572ff6f6SMatthew Dillon  * no frames have yet been transmitted.
63*572ff6f6SMatthew Dillon  */
64*572ff6f6SMatthew Dillon void
ar9287olcTemperatureCompensation(struct ath_hal * ah)65*572ff6f6SMatthew Dillon ar9287olcTemperatureCompensation(struct ath_hal *ah)
66*572ff6f6SMatthew Dillon {
67*572ff6f6SMatthew Dillon 	uint32_t rddata;
68*572ff6f6SMatthew Dillon 	int32_t delta, currPDADC, slope;
69*572ff6f6SMatthew Dillon 
70*572ff6f6SMatthew Dillon 	rddata = OS_REG_READ(ah, AR_PHY_TX_PWRCTRL4);
71*572ff6f6SMatthew Dillon 	currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
72*572ff6f6SMatthew Dillon 
73*572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_PERCAL, "%s: initPDADC=%d, currPDADC=%d\n",
74*572ff6f6SMatthew Dillon 	     __func__, AH5416(ah)->initPDADC, currPDADC);
75*572ff6f6SMatthew Dillon 
76*572ff6f6SMatthew Dillon 	if (AH5416(ah)->initPDADC == 0 || currPDADC == 0) {
77*572ff6f6SMatthew Dillon 		/*
78*572ff6f6SMatthew Dillon 		 * Zero value indicates that no frames have been transmitted
79*572ff6f6SMatthew Dillon 		 * yet, can't do temperature compensation until frames are
80*572ff6f6SMatthew Dillon 		 * transmitted.
81*572ff6f6SMatthew Dillon 		 */
82*572ff6f6SMatthew Dillon 		return;
83*572ff6f6SMatthew Dillon 	} else {
84*572ff6f6SMatthew Dillon 		int8_t val;
85*572ff6f6SMatthew Dillon 		(void) (ath_hal_eepromGet(ah, AR_EEP_TEMPSENSE_SLOPE, &val));
86*572ff6f6SMatthew Dillon 		slope = val;
87*572ff6f6SMatthew Dillon 
88*572ff6f6SMatthew Dillon 		if (slope == 0) { /* to avoid divide by zero case */
89*572ff6f6SMatthew Dillon 			delta = 0;
90*572ff6f6SMatthew Dillon 		} else {
91*572ff6f6SMatthew Dillon 			delta = ((currPDADC - AH5416(ah)->initPDADC)*4) / slope;
92*572ff6f6SMatthew Dillon 		}
93*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_CH0_TX_PWRCTRL11,
94*572ff6f6SMatthew Dillon 		    AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
95*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_CH1_TX_PWRCTRL11,
96*572ff6f6SMatthew Dillon 		    AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
97*572ff6f6SMatthew Dillon 
98*572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_PERCAL, "%s: delta=%d\n", __func__, delta);
99*572ff6f6SMatthew Dillon 	}
100*572ff6f6SMatthew Dillon }
101*572ff6f6SMatthew Dillon 
102*572ff6f6SMatthew Dillon void
ar9287olcGetTxGainIndex(struct ath_hal * ah,const struct ieee80211_channel * chan,struct cal_data_op_loop_ar9287 * pRawDatasetOpLoop,uint8_t * pCalChans,uint16_t availPiers,int8_t * pPwr)103*572ff6f6SMatthew Dillon ar9287olcGetTxGainIndex(struct ath_hal *ah,
104*572ff6f6SMatthew Dillon     const struct ieee80211_channel *chan,
105*572ff6f6SMatthew Dillon     struct cal_data_op_loop_ar9287 *pRawDatasetOpLoop,
106*572ff6f6SMatthew Dillon     uint8_t *pCalChans,  uint16_t availPiers, int8_t *pPwr)
107*572ff6f6SMatthew Dillon {
108*572ff6f6SMatthew Dillon         uint16_t idxL = 0, idxR = 0, numPiers;
109*572ff6f6SMatthew Dillon         HAL_BOOL match;
110*572ff6f6SMatthew Dillon         CHAN_CENTERS centers;
111*572ff6f6SMatthew Dillon 
112*572ff6f6SMatthew Dillon         ar5416GetChannelCenters(ah, chan, &centers);
113*572ff6f6SMatthew Dillon 
114*572ff6f6SMatthew Dillon         for (numPiers = 0; numPiers < availPiers; numPiers++) {
115*572ff6f6SMatthew Dillon                 if (pCalChans[numPiers] == AR5416_BCHAN_UNUSED)
116*572ff6f6SMatthew Dillon                         break;
117*572ff6f6SMatthew Dillon         }
118*572ff6f6SMatthew Dillon 
119*572ff6f6SMatthew Dillon         match = ath_ee_getLowerUpperIndex(
120*572ff6f6SMatthew Dillon                 (uint8_t)FREQ2FBIN(centers.synth_center, IEEE80211_IS_CHAN_2GHZ(chan)),
121*572ff6f6SMatthew Dillon                 pCalChans, numPiers, &idxL, &idxR);
122*572ff6f6SMatthew Dillon 
123*572ff6f6SMatthew Dillon         if (match) {
124*572ff6f6SMatthew Dillon                 *pPwr = (int8_t) pRawDatasetOpLoop[idxL].pwrPdg[0][0];
125*572ff6f6SMatthew Dillon         } else {
126*572ff6f6SMatthew Dillon                 *pPwr = ((int8_t) pRawDatasetOpLoop[idxL].pwrPdg[0][0] +
127*572ff6f6SMatthew Dillon                          (int8_t) pRawDatasetOpLoop[idxR].pwrPdg[0][0])/2;
128*572ff6f6SMatthew Dillon         }
129*572ff6f6SMatthew Dillon }
130*572ff6f6SMatthew Dillon 
131*572ff6f6SMatthew Dillon void
ar9287olcSetPDADCs(struct ath_hal * ah,int32_t txPower,uint16_t chain)132*572ff6f6SMatthew Dillon ar9287olcSetPDADCs(struct ath_hal *ah, int32_t txPower,
133*572ff6f6SMatthew Dillon     uint16_t chain)
134*572ff6f6SMatthew Dillon {
135*572ff6f6SMatthew Dillon         uint32_t tmpVal;
136*572ff6f6SMatthew Dillon         uint32_t a;
137*572ff6f6SMatthew Dillon 
138*572ff6f6SMatthew Dillon         /* Enable OLPC for chain 0 */
139*572ff6f6SMatthew Dillon 
140*572ff6f6SMatthew Dillon         tmpVal = OS_REG_READ(ah, 0xa270);
141*572ff6f6SMatthew Dillon         tmpVal = tmpVal & 0xFCFFFFFF;
142*572ff6f6SMatthew Dillon         tmpVal = tmpVal | (0x3 << 24);
143*572ff6f6SMatthew Dillon         OS_REG_WRITE(ah, 0xa270, tmpVal);
144*572ff6f6SMatthew Dillon 
145*572ff6f6SMatthew Dillon         /* Enable OLPC for chain 1 */
146*572ff6f6SMatthew Dillon 
147*572ff6f6SMatthew Dillon         tmpVal = OS_REG_READ(ah, 0xb270);
148*572ff6f6SMatthew Dillon         tmpVal = tmpVal & 0xFCFFFFFF;
149*572ff6f6SMatthew Dillon         tmpVal = tmpVal | (0x3 << 24);
150*572ff6f6SMatthew Dillon         OS_REG_WRITE(ah, 0xb270, tmpVal);
151*572ff6f6SMatthew Dillon 
152*572ff6f6SMatthew Dillon         /* Write the OLPC ref power for chain 0 */
153*572ff6f6SMatthew Dillon 
154*572ff6f6SMatthew Dillon         if (chain == 0) {
155*572ff6f6SMatthew Dillon                 tmpVal = OS_REG_READ(ah, 0xa398);
156*572ff6f6SMatthew Dillon                 tmpVal = tmpVal & 0xff00ffff;
157*572ff6f6SMatthew Dillon                 a = (txPower)&0xff;
158*572ff6f6SMatthew Dillon                 tmpVal = tmpVal | (a << 16);
159*572ff6f6SMatthew Dillon                 OS_REG_WRITE(ah, 0xa398, tmpVal);
160*572ff6f6SMatthew Dillon         }
161*572ff6f6SMatthew Dillon 
162*572ff6f6SMatthew Dillon         /* Write the OLPC ref power for chain 1 */
163*572ff6f6SMatthew Dillon 
164*572ff6f6SMatthew Dillon         if (chain == 1) {
165*572ff6f6SMatthew Dillon                 tmpVal = OS_REG_READ(ah, 0xb398);
166*572ff6f6SMatthew Dillon                 tmpVal = tmpVal & 0xff00ffff;
167*572ff6f6SMatthew Dillon                 a = (txPower)&0xff;
168*572ff6f6SMatthew Dillon                 tmpVal = tmpVal | (a << 16);
169*572ff6f6SMatthew Dillon                 OS_REG_WRITE(ah, 0xb398, tmpVal);
170*572ff6f6SMatthew Dillon         }
171*572ff6f6SMatthew Dillon }
172