xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar9002/ar9285_reset.c (revision 572ff6f6e8b95055988f178b6ba12ce77bb5b3c2)
1*572ff6f6SMatthew Dillon /*
2*572ff6f6SMatthew Dillon  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3*572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4*572ff6f6SMatthew Dillon  *
5*572ff6f6SMatthew Dillon  * Permission to use, copy, modify, and/or distribute this software for any
6*572ff6f6SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7*572ff6f6SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8*572ff6f6SMatthew Dillon  *
9*572ff6f6SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*572ff6f6SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*572ff6f6SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*572ff6f6SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*572ff6f6SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*572ff6f6SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*572ff6f6SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*572ff6f6SMatthew Dillon  *
17*572ff6f6SMatthew Dillon  * $FreeBSD$
18*572ff6f6SMatthew Dillon  */
19*572ff6f6SMatthew Dillon 
20*572ff6f6SMatthew Dillon /*
21*572ff6f6SMatthew Dillon  * This is almost the same as ar5416_reset.c but uses the v4k EEPROM and
22*572ff6f6SMatthew Dillon  * supports only 2Ghz operation.
23*572ff6f6SMatthew Dillon  */
24*572ff6f6SMatthew Dillon 
25*572ff6f6SMatthew Dillon #include "opt_ah.h"
26*572ff6f6SMatthew Dillon 
27*572ff6f6SMatthew Dillon #include "ah.h"
28*572ff6f6SMatthew Dillon #include "ah_internal.h"
29*572ff6f6SMatthew Dillon #include "ah_devid.h"
30*572ff6f6SMatthew Dillon 
31*572ff6f6SMatthew Dillon #include "ah_eeprom_v14.h"
32*572ff6f6SMatthew Dillon #include "ah_eeprom_v4k.h"
33*572ff6f6SMatthew Dillon 
34*572ff6f6SMatthew Dillon #include "ar9002/ar9285.h"
35*572ff6f6SMatthew Dillon #include "ar5416/ar5416.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 #include "ar9002/ar9285phy.h"
40*572ff6f6SMatthew Dillon #include "ar9002/ar9285an.h"
41*572ff6f6SMatthew Dillon #include "ar9002/ar9285_diversity.h"
42*572ff6f6SMatthew Dillon 
43*572ff6f6SMatthew Dillon /* Eeprom versioning macros. Returns true if the version is equal or newer than the ver specified */
44*572ff6f6SMatthew Dillon #define	EEP_MINOR(_ah) \
45*572ff6f6SMatthew Dillon 	(AH_PRIVATE(_ah)->ah_eeversion & AR5416_EEP_VER_MINOR_MASK)
46*572ff6f6SMatthew Dillon #define IS_EEP_MINOR_V2(_ah)	(EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_2)
47*572ff6f6SMatthew Dillon #define IS_EEP_MINOR_V3(_ah)	(EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_3)
48*572ff6f6SMatthew Dillon 
49*572ff6f6SMatthew Dillon /* Additional Time delay to wait after activiting the Base band */
50*572ff6f6SMatthew Dillon #define BASE_ACTIVATE_DELAY	100	/* 100 usec */
51*572ff6f6SMatthew Dillon #define PLL_SETTLE_DELAY	300	/* 300 usec */
52*572ff6f6SMatthew Dillon #define RTC_PLL_SETTLE_DELAY    1000    /* 1 ms     */
53*572ff6f6SMatthew Dillon 
54*572ff6f6SMatthew Dillon static HAL_BOOL ar9285SetPowerPerRateTable(struct ath_hal *ah,
55*572ff6f6SMatthew Dillon 	struct ar5416eeprom_4k *pEepData,
56*572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan, int16_t *ratesArray,
57*572ff6f6SMatthew Dillon 	uint16_t cfgCtl, uint16_t AntennaReduction,
58*572ff6f6SMatthew Dillon 	uint16_t twiceMaxRegulatoryPower,
59*572ff6f6SMatthew Dillon 	uint16_t powerLimit);
60*572ff6f6SMatthew Dillon static HAL_BOOL ar9285SetPowerCalTable(struct ath_hal *ah,
61*572ff6f6SMatthew Dillon 	struct ar5416eeprom_4k *pEepData,
62*572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan,
63*572ff6f6SMatthew Dillon 	int16_t *pTxPowerIndexOffset);
64*572ff6f6SMatthew Dillon static void ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,
65*572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan, CAL_DATA_PER_FREQ_4K *pRawDataSet,
66*572ff6f6SMatthew Dillon 	uint8_t * bChans, uint16_t availPiers,
67*572ff6f6SMatthew Dillon 	uint16_t tPdGainOverlap, int16_t *pMinCalPower,
68*572ff6f6SMatthew Dillon 	uint16_t * pPdGainBoundaries, uint8_t * pPDADCValues,
69*572ff6f6SMatthew Dillon 	uint16_t numXpdGains);
70*572ff6f6SMatthew Dillon 
71*572ff6f6SMatthew Dillon HAL_BOOL
ar9285SetTransmitPower(struct ath_hal * ah,const struct ieee80211_channel * chan,uint16_t * rfXpdGain)72*572ff6f6SMatthew Dillon ar9285SetTransmitPower(struct ath_hal *ah,
73*572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan, uint16_t *rfXpdGain)
74*572ff6f6SMatthew Dillon {
75*572ff6f6SMatthew Dillon #define POW_SM(_r, _s)     (((_r) & 0x3f) << (_s))
76*572ff6f6SMatthew Dillon #define N(a)            (sizeof (a) / sizeof (a[0]))
77*572ff6f6SMatthew Dillon 
78*572ff6f6SMatthew Dillon     MODAL_EEP4K_HEADER	*pModal;
79*572ff6f6SMatthew Dillon     struct ath_hal_5212 *ahp = AH5212(ah);
80*572ff6f6SMatthew Dillon     int16_t		txPowerIndexOffset = 0;
81*572ff6f6SMatthew Dillon     int			i;
82*572ff6f6SMatthew Dillon 
83*572ff6f6SMatthew Dillon     uint16_t		cfgCtl;
84*572ff6f6SMatthew Dillon     uint16_t		powerLimit;
85*572ff6f6SMatthew Dillon     uint16_t		twiceAntennaReduction;
86*572ff6f6SMatthew Dillon     uint16_t		twiceMaxRegulatoryPower;
87*572ff6f6SMatthew Dillon     int16_t		maxPower;
88*572ff6f6SMatthew Dillon     HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
89*572ff6f6SMatthew Dillon     struct ar5416eeprom_4k *pEepData = &ee->ee_base;
90*572ff6f6SMatthew Dillon 
91*572ff6f6SMatthew Dillon     HALASSERT(AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER14_1);
92*572ff6f6SMatthew Dillon 
93*572ff6f6SMatthew Dillon     AH5416(ah)->ah_ht40PowerIncForPdadc = 2;
94*572ff6f6SMatthew Dillon 
95*572ff6f6SMatthew Dillon     /* Setup info for the actual eeprom */
96*572ff6f6SMatthew Dillon     OS_MEMZERO(AH5416(ah)->ah_ratesArray, sizeof(AH5416(ah)->ah_ratesArray));
97*572ff6f6SMatthew Dillon     cfgCtl = ath_hal_getctl(ah, chan);
98*572ff6f6SMatthew Dillon     powerLimit = chan->ic_maxregpower * 2;
99*572ff6f6SMatthew Dillon     twiceAntennaReduction = chan->ic_maxantgain;
100*572ff6f6SMatthew Dillon     twiceMaxRegulatoryPower = AH_MIN(MAX_RATE_POWER, AH_PRIVATE(ah)->ah_powerLimit);
101*572ff6f6SMatthew Dillon     pModal = &pEepData->modalHeader;
102*572ff6f6SMatthew Dillon     HALDEBUG(ah, HAL_DEBUG_RESET, "%s Channel=%u CfgCtl=%u\n",
103*572ff6f6SMatthew Dillon 	__func__,chan->ic_freq, cfgCtl );
104*572ff6f6SMatthew Dillon 
105*572ff6f6SMatthew Dillon     if (IS_EEP_MINOR_V2(ah)) {
106*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
107*572ff6f6SMatthew Dillon     }
108*572ff6f6SMatthew Dillon 
109*572ff6f6SMatthew Dillon     if (!ar9285SetPowerPerRateTable(ah, pEepData,  chan,
110*572ff6f6SMatthew Dillon                                     &AH5416(ah)->ah_ratesArray[0],cfgCtl,
111*572ff6f6SMatthew Dillon                                     twiceAntennaReduction,
112*572ff6f6SMatthew Dillon 				    twiceMaxRegulatoryPower, powerLimit)) {
113*572ff6f6SMatthew Dillon         HALDEBUG(ah, HAL_DEBUG_ANY,
114*572ff6f6SMatthew Dillon 	    "%s: unable to set tx power per rate table\n", __func__);
115*572ff6f6SMatthew Dillon         return AH_FALSE;
116*572ff6f6SMatthew Dillon     }
117*572ff6f6SMatthew Dillon 
118*572ff6f6SMatthew Dillon     if (!ar9285SetPowerCalTable(ah,  pEepData, chan, &txPowerIndexOffset)) {
119*572ff6f6SMatthew Dillon         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set power table\n",
120*572ff6f6SMatthew Dillon 	    __func__);
121*572ff6f6SMatthew Dillon         return AH_FALSE;
122*572ff6f6SMatthew Dillon     }
123*572ff6f6SMatthew Dillon 
124*572ff6f6SMatthew Dillon     maxPower = AH_MAX(AH5416(ah)->ah_ratesArray[rate6mb],
125*572ff6f6SMatthew Dillon       AH5416(ah)->ah_ratesArray[rateHt20_0]);
126*572ff6f6SMatthew Dillon     maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rate1l]);
127*572ff6f6SMatthew Dillon 
128*572ff6f6SMatthew Dillon     if (IEEE80211_IS_CHAN_HT40(chan)) {
129*572ff6f6SMatthew Dillon         maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rateHt40_0]);
130*572ff6f6SMatthew Dillon     }
131*572ff6f6SMatthew Dillon 
132*572ff6f6SMatthew Dillon     ahp->ah_tx6PowerInHalfDbm = maxPower;
133*572ff6f6SMatthew Dillon     AH_PRIVATE(ah)->ah_maxPowerLevel = maxPower;
134*572ff6f6SMatthew Dillon     ahp->ah_txPowerIndexOffset = txPowerIndexOffset;
135*572ff6f6SMatthew Dillon 
136*572ff6f6SMatthew Dillon     /*
137*572ff6f6SMatthew Dillon      * txPowerIndexOffset is set by the SetPowerTable() call -
138*572ff6f6SMatthew Dillon      *  adjust the rate table (0 offset if rates EEPROM not loaded)
139*572ff6f6SMatthew Dillon      */
140*572ff6f6SMatthew Dillon     for (i = 0; i < N(AH5416(ah)->ah_ratesArray); i++) {
141*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[i] = (int16_t)(txPowerIndexOffset + AH5416(ah)->ah_ratesArray[i]);
142*572ff6f6SMatthew Dillon 	/* -5 dBm offset for Merlin and later; this includes Kite */
143*572ff6f6SMatthew Dillon 	AH5416(ah)->ah_ratesArray[i] -= AR5416_PWR_TABLE_OFFSET_DB * 2;
144*572ff6f6SMatthew Dillon         if (AH5416(ah)->ah_ratesArray[i] > AR5416_MAX_RATE_POWER)
145*572ff6f6SMatthew Dillon             AH5416(ah)->ah_ratesArray[i] = AR5416_MAX_RATE_POWER;
146*572ff6f6SMatthew Dillon 	if (AH5416(ah)->ah_ratesArray[i] < 0)
147*572ff6f6SMatthew Dillon 		AH5416(ah)->ah_ratesArray[i] = 0;
148*572ff6f6SMatthew Dillon     }
149*572ff6f6SMatthew Dillon 
150*572ff6f6SMatthew Dillon #ifdef AH_EEPROM_DUMP
151*572ff6f6SMatthew Dillon     ar5416PrintPowerPerRate(ah, AH5416(ah)->ah_ratesArray);
152*572ff6f6SMatthew Dillon #endif
153*572ff6f6SMatthew Dillon 
154*572ff6f6SMatthew Dillon     /*
155*572ff6f6SMatthew Dillon      * Adjust the HT40 power to meet the correct target TX power
156*572ff6f6SMatthew Dillon      * for 40MHz mode, based on TX power curves that are established
157*572ff6f6SMatthew Dillon      * for 20MHz mode.
158*572ff6f6SMatthew Dillon      *
159*572ff6f6SMatthew Dillon      * XXX handle overflow/too high power level?
160*572ff6f6SMatthew Dillon      */
161*572ff6f6SMatthew Dillon     if (IEEE80211_IS_CHAN_HT40(chan)) {
162*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_0] +=
163*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
164*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_1] +=
165*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
166*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_2] +=
167*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
168*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_3] +=
169*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
170*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_4] +=
171*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
172*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_5] +=
173*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
174*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_6] +=
175*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
176*572ff6f6SMatthew Dillon         AH5416(ah)->ah_ratesArray[rateHt40_7] +=
177*572ff6f6SMatthew Dillon           AH5416(ah)->ah_ht40PowerIncForPdadc;
178*572ff6f6SMatthew Dillon     }
179*572ff6f6SMatthew Dillon 
180*572ff6f6SMatthew Dillon     /* Write the TX power rate registers */
181*572ff6f6SMatthew Dillon     ar5416WriteTxPowerRateRegisters(ah, chan, AH5416(ah)->ah_ratesArray);
182*572ff6f6SMatthew Dillon 
183*572ff6f6SMatthew Dillon     return AH_TRUE;
184*572ff6f6SMatthew Dillon #undef POW_SM
185*572ff6f6SMatthew Dillon #undef N
186*572ff6f6SMatthew Dillon }
187*572ff6f6SMatthew Dillon 
188*572ff6f6SMatthew Dillon static void
ar9285SetBoardGain(struct ath_hal * ah,const MODAL_EEP4K_HEADER * pModal,const struct ar5416eeprom_4k * eep,uint8_t txRxAttenLocal)189*572ff6f6SMatthew Dillon ar9285SetBoardGain(struct ath_hal *ah, const MODAL_EEP4K_HEADER *pModal,
190*572ff6f6SMatthew Dillon     const struct ar5416eeprom_4k *eep, uint8_t txRxAttenLocal)
191*572ff6f6SMatthew Dillon {
192*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0,
193*572ff6f6SMatthew Dillon 		  pModal->antCtrlChain[0]);
194*572ff6f6SMatthew Dillon 
195*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4_CHAIN(0),
196*572ff6f6SMatthew Dillon 		  (OS_REG_READ(ah, AR_PHY_TIMING_CTRL4_CHAIN(0)) &
197*572ff6f6SMatthew Dillon 		   ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
198*572ff6f6SMatthew Dillon 		     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
199*572ff6f6SMatthew Dillon 		  SM(pModal->iqCalICh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
200*572ff6f6SMatthew Dillon 		  SM(pModal->iqCalQCh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
201*572ff6f6SMatthew Dillon 
202*572ff6f6SMatthew Dillon 	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
203*572ff6f6SMatthew Dillon 	    AR5416_EEP_MINOR_VER_3) {
204*572ff6f6SMatthew Dillon 		txRxAttenLocal = pModal->txRxAttenCh[0];
205*572ff6f6SMatthew Dillon 
206*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
207*572ff6f6SMatthew Dillon 		    AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN, pModal->bswMargin[0]);
208*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
209*572ff6f6SMatthew Dillon 		    AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
210*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
211*572ff6f6SMatthew Dillon 		    AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN, pModal->xatten2Margin[0]);
212*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
213*572ff6f6SMatthew Dillon 		    AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
214*572ff6f6SMatthew Dillon 
215*572ff6f6SMatthew Dillon 		/* Set the block 1 value to block 0 value */
216*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
217*572ff6f6SMatthew Dillon 		      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
218*572ff6f6SMatthew Dillon 		      pModal->bswMargin[0]);
219*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
220*572ff6f6SMatthew Dillon 		      AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
221*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
222*572ff6f6SMatthew Dillon 		      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
223*572ff6f6SMatthew Dillon 		      pModal->xatten2Margin[0]);
224*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
225*572ff6f6SMatthew Dillon 		      AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
226*572ff6f6SMatthew Dillon 	}
227*572ff6f6SMatthew Dillon 
228*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,
229*572ff6f6SMatthew Dillon 		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
230*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,
231*572ff6f6SMatthew Dillon 		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
232*572ff6f6SMatthew Dillon 
233*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
234*572ff6f6SMatthew Dillon 		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
235*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
236*572ff6f6SMatthew Dillon 		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
237*572ff6f6SMatthew Dillon }
238*572ff6f6SMatthew Dillon 
239*572ff6f6SMatthew Dillon /*
240*572ff6f6SMatthew Dillon  * Read EEPROM header info and program the device for correct operation
241*572ff6f6SMatthew Dillon  * given the channel value.
242*572ff6f6SMatthew Dillon  */
243*572ff6f6SMatthew Dillon HAL_BOOL
ar9285SetBoardValues(struct ath_hal * ah,const struct ieee80211_channel * chan)244*572ff6f6SMatthew Dillon ar9285SetBoardValues(struct ath_hal *ah, const struct ieee80211_channel *chan)
245*572ff6f6SMatthew Dillon {
246*572ff6f6SMatthew Dillon 	const HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
247*572ff6f6SMatthew Dillon 	const struct ar5416eeprom_4k *eep = &ee->ee_base;
248*572ff6f6SMatthew Dillon 	const MODAL_EEP4K_HEADER *pModal;
249*572ff6f6SMatthew Dillon 	uint8_t txRxAttenLocal;
250*572ff6f6SMatthew Dillon 	uint8_t ob[5], db1[5], db2[5];
251*572ff6f6SMatthew Dillon 
252*572ff6f6SMatthew Dillon 	pModal = &eep->modalHeader;
253*572ff6f6SMatthew Dillon 	txRxAttenLocal = 23;
254*572ff6f6SMatthew Dillon 
255*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY_SWITCH_COM, pModal->antCtrlCommon);
256*572ff6f6SMatthew Dillon 
257*572ff6f6SMatthew Dillon 	/* Single chain for 4K EEPROM*/
258*572ff6f6SMatthew Dillon 	ar9285SetBoardGain(ah, pModal, eep, txRxAttenLocal);
259*572ff6f6SMatthew Dillon 
260*572ff6f6SMatthew Dillon 	/* Initialize Ant Diversity settings if supported */
261*572ff6f6SMatthew Dillon 	(void) ar9285SetAntennaSwitch(ah, AH5212(ah)->ah_antControl);
262*572ff6f6SMatthew Dillon 
263*572ff6f6SMatthew Dillon 	/* Configure TX power calibration */
264*572ff6f6SMatthew Dillon 	if (pModal->version >= 2) {
265*572ff6f6SMatthew Dillon 		ob[0] = pModal->ob_0;
266*572ff6f6SMatthew Dillon 		ob[1] = pModal->ob_1;
267*572ff6f6SMatthew Dillon 		ob[2] = pModal->ob_2;
268*572ff6f6SMatthew Dillon 		ob[3] = pModal->ob_3;
269*572ff6f6SMatthew Dillon 		ob[4] = pModal->ob_4;
270*572ff6f6SMatthew Dillon 
271*572ff6f6SMatthew Dillon 		db1[0] = pModal->db1_0;
272*572ff6f6SMatthew Dillon 		db1[1] = pModal->db1_1;
273*572ff6f6SMatthew Dillon 		db1[2] = pModal->db1_2;
274*572ff6f6SMatthew Dillon 		db1[3] = pModal->db1_3;
275*572ff6f6SMatthew Dillon 		db1[4] = pModal->db1_4;
276*572ff6f6SMatthew Dillon 
277*572ff6f6SMatthew Dillon 		db2[0] = pModal->db2_0;
278*572ff6f6SMatthew Dillon 		db2[1] = pModal->db2_1;
279*572ff6f6SMatthew Dillon 		db2[2] = pModal->db2_2;
280*572ff6f6SMatthew Dillon 		db2[3] = pModal->db2_3;
281*572ff6f6SMatthew Dillon 		db2[4] = pModal->db2_4;
282*572ff6f6SMatthew Dillon 	} else if (pModal->version == 1) {
283*572ff6f6SMatthew Dillon 		ob[0] = pModal->ob_0;
284*572ff6f6SMatthew Dillon 		ob[1] = ob[2] = ob[3] = ob[4] = pModal->ob_1;
285*572ff6f6SMatthew Dillon 		db1[0] = pModal->db1_0;
286*572ff6f6SMatthew Dillon 		db1[1] = db1[2] = db1[3] = db1[4] = pModal->db1_1;
287*572ff6f6SMatthew Dillon 		db2[0] = pModal->db2_0;
288*572ff6f6SMatthew Dillon 		db2[1] = db2[2] = db2[3] = db2[4] = pModal->db2_1;
289*572ff6f6SMatthew Dillon 	} else {
290*572ff6f6SMatthew Dillon 		int i;
291*572ff6f6SMatthew Dillon 
292*572ff6f6SMatthew Dillon 		for (i = 0; i < 5; i++) {
293*572ff6f6SMatthew Dillon 			ob[i] = pModal->ob_0;
294*572ff6f6SMatthew Dillon 			db1[i] = pModal->db1_0;
295*572ff6f6SMatthew Dillon 			db2[i] = pModal->db1_0;
296*572ff6f6SMatthew Dillon 		}
297*572ff6f6SMatthew Dillon 	}
298*572ff6f6SMatthew Dillon 
299*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_0, ob[0]);
300*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_1, ob[1]);
301*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_2, ob[2]);
302*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_3, ob[3]);
303*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_4, ob[4]);
304*572ff6f6SMatthew Dillon 
305*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_0, db1[0]);
306*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_1, db1[1]);
307*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_2, db1[2]);
308*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_3, db1[3]);
309*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_4, db1[4]);
310*572ff6f6SMatthew Dillon 
311*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_0, db2[0]);
312*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_1, db2[1]);
313*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_2, db2[2]);
314*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_3, db2[3]);
315*572ff6f6SMatthew Dillon 	OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_4, db2[4]);
316*572ff6f6SMatthew Dillon 
317*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
318*572ff6f6SMatthew Dillon 		      pModal->switchSettling);
319*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
320*572ff6f6SMatthew Dillon 		      pModal->adcDesiredSize);
321*572ff6f6SMatthew Dillon 
322*572ff6f6SMatthew Dillon 	OS_REG_WRITE(ah, AR_PHY_RF_CTL4,
323*572ff6f6SMatthew Dillon 		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF) |
324*572ff6f6SMatthew Dillon 		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF) |
325*572ff6f6SMatthew Dillon 		  SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON)  |
326*572ff6f6SMatthew Dillon 		  SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));
327*572ff6f6SMatthew Dillon 
328*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
329*572ff6f6SMatthew Dillon 		      pModal->txEndToRxOn);
330*572ff6f6SMatthew Dillon 
331*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
332*572ff6f6SMatthew Dillon 		      pModal->thresh62);
333*572ff6f6SMatthew Dillon 	OS_REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0, AR_PHY_EXT_CCA0_THRESH62,
334*572ff6f6SMatthew Dillon 		      pModal->thresh62);
335*572ff6f6SMatthew Dillon 
336*572ff6f6SMatthew Dillon 	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
337*572ff6f6SMatthew Dillon 	    AR5416_EEP_MINOR_VER_2) {
338*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_DATA_START,
339*572ff6f6SMatthew Dillon 		    pModal->txFrameToDataStart);
340*572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_PA_ON,
341*572ff6f6SMatthew Dillon 		    pModal->txFrameToPaOn);
342*572ff6f6SMatthew Dillon 	}
343*572ff6f6SMatthew Dillon 
344*572ff6f6SMatthew Dillon 	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
345*572ff6f6SMatthew Dillon 	    AR5416_EEP_MINOR_VER_3) {
346*572ff6f6SMatthew Dillon 		if (IEEE80211_IS_CHAN_HT40(chan))
347*572ff6f6SMatthew Dillon 			OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING,
348*572ff6f6SMatthew Dillon 			    AR_PHY_SETTLING_SWITCH, pModal->swSettleHt40);
349*572ff6f6SMatthew Dillon 	}
350*572ff6f6SMatthew Dillon 
351*572ff6f6SMatthew Dillon 	/*
352*572ff6f6SMatthew Dillon 	 * Program the CCK TX gain factor appropriately if needed.
353*572ff6f6SMatthew Dillon 	 * The AR9285/AR9271 has a non-constant PA tx gain behaviour
354*572ff6f6SMatthew Dillon 	 * for CCK versus OFDM rates; other chips deal with this
355*572ff6f6SMatthew Dillon 	 * differently.
356*572ff6f6SMatthew Dillon 	 *
357*572ff6f6SMatthew Dillon 	 * The mask/shift/multiply hackery is done so place the same
358*572ff6f6SMatthew Dillon 	 * value (bb_desired_scale) into multiple 5-bit fields.
359*572ff6f6SMatthew Dillon 	 * For example, AR_PHY_TX_PWRCTRL9 has bb_desired_scale written
360*572ff6f6SMatthew Dillon 	 * to three fields: (0..4), (5..9) and (10..14).
361*572ff6f6SMatthew Dillon 	 */
362*572ff6f6SMatthew Dillon 	if (AR_SREV_9271(ah) || AR_SREV_KITE(ah)) {
363*572ff6f6SMatthew Dillon 		uint8_t bb_desired_scale = (pModal->bb_scale_smrt_antenna & EEP_4K_BB_DESIRED_SCALE_MASK);
364*572ff6f6SMatthew Dillon 		if ((eep->baseEepHeader.txGainType == 0) && (bb_desired_scale != 0)) {
365*572ff6f6SMatthew Dillon 			ath_hal_printf(ah, "[ath]: adjusting cck tx gain factor\n");
366*572ff6f6SMatthew Dillon 			uint32_t pwrctrl, mask, clr;
367*572ff6f6SMatthew Dillon 
368*572ff6f6SMatthew Dillon 			mask = (1<<0) | (1<<5) | (1<<10) | (1<<15) | (1<<20) | (1<<25);
369*572ff6f6SMatthew Dillon 			pwrctrl = mask * bb_desired_scale;
370*572ff6f6SMatthew Dillon 			clr = mask * 0x1f;
371*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL8, pwrctrl, clr);
372*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL10, pwrctrl, clr);
373*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL12, pwrctrl, clr);
374*572ff6f6SMatthew Dillon 
375*572ff6f6SMatthew Dillon 			mask = (1<<0) | (1<<5) | (1<<15);
376*572ff6f6SMatthew Dillon 			pwrctrl = mask * bb_desired_scale;
377*572ff6f6SMatthew Dillon 			clr = mask * 0x1f;
378*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL9, pwrctrl, clr);
379*572ff6f6SMatthew Dillon 
380*572ff6f6SMatthew Dillon 			mask = (1<<0) | (1<<5);
381*572ff6f6SMatthew Dillon 			pwrctrl = mask * bb_desired_scale;
382*572ff6f6SMatthew Dillon 			clr = mask * 0x1f;
383*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL11, pwrctrl, clr);
384*572ff6f6SMatthew Dillon 			OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL13, pwrctrl, clr);
385*572ff6f6SMatthew Dillon 		}
386*572ff6f6SMatthew Dillon 	}
387*572ff6f6SMatthew Dillon 
388*572ff6f6SMatthew Dillon 	return AH_TRUE;
389*572ff6f6SMatthew Dillon }
390*572ff6f6SMatthew Dillon 
391*572ff6f6SMatthew Dillon /*
392*572ff6f6SMatthew Dillon  * Helper functions common for AP/CB/XB
393*572ff6f6SMatthew Dillon  */
394*572ff6f6SMatthew Dillon 
395*572ff6f6SMatthew Dillon static HAL_BOOL
ar9285SetPowerPerRateTable(struct ath_hal * ah,struct ar5416eeprom_4k * pEepData,const struct ieee80211_channel * chan,int16_t * ratesArray,uint16_t cfgCtl,uint16_t AntennaReduction,uint16_t twiceMaxRegulatoryPower,uint16_t powerLimit)396*572ff6f6SMatthew Dillon ar9285SetPowerPerRateTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,
397*572ff6f6SMatthew Dillon                            const struct ieee80211_channel *chan,
398*572ff6f6SMatthew Dillon                            int16_t *ratesArray, uint16_t cfgCtl,
399*572ff6f6SMatthew Dillon                            uint16_t AntennaReduction,
400*572ff6f6SMatthew Dillon                            uint16_t twiceMaxRegulatoryPower,
401*572ff6f6SMatthew Dillon                            uint16_t powerLimit)
402*572ff6f6SMatthew Dillon {
403*572ff6f6SMatthew Dillon #define	N(a)	(sizeof(a)/sizeof(a[0]))
404*572ff6f6SMatthew Dillon /* Local defines to distinguish between extension and control CTL's */
405*572ff6f6SMatthew Dillon #define EXT_ADDITIVE (0x8000)
406*572ff6f6SMatthew Dillon #define CTL_11G_EXT (CTL_11G | EXT_ADDITIVE)
407*572ff6f6SMatthew Dillon #define CTL_11B_EXT (CTL_11B | EXT_ADDITIVE)
408*572ff6f6SMatthew Dillon 
409*572ff6f6SMatthew Dillon 	uint16_t twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
410*572ff6f6SMatthew Dillon 	int i;
411*572ff6f6SMatthew Dillon 	int16_t  twiceLargestAntenna;
412*572ff6f6SMatthew Dillon 	CAL_CTL_DATA_4K *rep;
413*572ff6f6SMatthew Dillon 	CAL_TARGET_POWER_LEG targetPowerOfdm, targetPowerCck = {0, {0, 0, 0, 0}};
414*572ff6f6SMatthew Dillon 	CAL_TARGET_POWER_LEG targetPowerOfdmExt = {0, {0, 0, 0, 0}}, targetPowerCckExt = {0, {0, 0, 0, 0}};
415*572ff6f6SMatthew Dillon 	CAL_TARGET_POWER_HT  targetPowerHt20, targetPowerHt40 = {0, {0, 0, 0, 0}};
416*572ff6f6SMatthew Dillon 	int16_t scaledPower, minCtlPower;
417*572ff6f6SMatthew Dillon 
418*572ff6f6SMatthew Dillon #define SUB_NUM_CTL_MODES_AT_2G_40 3   /* excluding HT40, EXT-OFDM, EXT-CCK */
419*572ff6f6SMatthew Dillon 	static const uint16_t ctlModesFor11g[] = {
420*572ff6f6SMatthew Dillon 	   CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT, CTL_2GHT40
421*572ff6f6SMatthew Dillon 	};
422*572ff6f6SMatthew Dillon 	const uint16_t *pCtlMode;
423*572ff6f6SMatthew Dillon 	uint16_t numCtlModes, ctlMode, freq;
424*572ff6f6SMatthew Dillon 	CHAN_CENTERS centers;
425*572ff6f6SMatthew Dillon 
426*572ff6f6SMatthew Dillon 	ar5416GetChannelCenters(ah,  chan, &centers);
427*572ff6f6SMatthew Dillon 
428*572ff6f6SMatthew Dillon 	/* Compute TxPower reduction due to Antenna Gain */
429*572ff6f6SMatthew Dillon 
430*572ff6f6SMatthew Dillon 	twiceLargestAntenna = pEepData->modalHeader.antennaGainCh[0];
431*572ff6f6SMatthew Dillon 	twiceLargestAntenna = (int16_t)AH_MIN((AntennaReduction) - twiceLargestAntenna, 0);
432*572ff6f6SMatthew Dillon 
433*572ff6f6SMatthew Dillon 	/* XXX setup for 5212 use (really used?) */
434*572ff6f6SMatthew Dillon 	ath_hal_eepromSet(ah, AR_EEP_ANTGAINMAX_2, twiceLargestAntenna);
435*572ff6f6SMatthew Dillon 
436*572ff6f6SMatthew Dillon 	/*
437*572ff6f6SMatthew Dillon 	 * scaledPower is the minimum of the user input power level and
438*572ff6f6SMatthew Dillon 	 * the regulatory allowed power level
439*572ff6f6SMatthew Dillon 	 */
440*572ff6f6SMatthew Dillon 	scaledPower = AH_MIN(powerLimit, twiceMaxRegulatoryPower + twiceLargestAntenna);
441*572ff6f6SMatthew Dillon 
442*572ff6f6SMatthew Dillon 	/* Get target powers from EEPROM - our baseline for TX Power */
443*572ff6f6SMatthew Dillon 	/* Setup for CTL modes */
444*572ff6f6SMatthew Dillon 	numCtlModes = N(ctlModesFor11g) - SUB_NUM_CTL_MODES_AT_2G_40; /* CTL_11B, CTL_11G, CTL_2GHT20 */
445*572ff6f6SMatthew Dillon 	pCtlMode = ctlModesFor11g;
446*572ff6f6SMatthew Dillon 
447*572ff6f6SMatthew Dillon 	ar5416GetTargetPowersLeg(ah,  chan, pEepData->calTargetPowerCck,
448*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCck, 4, AH_FALSE);
449*572ff6f6SMatthew Dillon 	ar5416GetTargetPowersLeg(ah,  chan, pEepData->calTargetPower2G,
450*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdm, 4, AH_FALSE);
451*572ff6f6SMatthew Dillon 	ar5416GetTargetPowers(ah,  chan, pEepData->calTargetPower2GHT20,
452*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerHt20, 8, AH_FALSE);
453*572ff6f6SMatthew Dillon 
454*572ff6f6SMatthew Dillon 	if (IEEE80211_IS_CHAN_HT40(chan)) {
455*572ff6f6SMatthew Dillon 		numCtlModes = N(ctlModesFor11g);    /* All 2G CTL's */
456*572ff6f6SMatthew Dillon 
457*572ff6f6SMatthew Dillon 		ar5416GetTargetPowers(ah,  chan, pEepData->calTargetPower2GHT40,
458*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_40_TARGET_POWERS, &targetPowerHt40, 8, AH_TRUE);
459*572ff6f6SMatthew Dillon 		/* Get target powers for extension channels */
460*572ff6f6SMatthew Dillon 		ar5416GetTargetPowersLeg(ah,  chan, pEepData->calTargetPowerCck,
461*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCckExt, 4, AH_TRUE);
462*572ff6f6SMatthew Dillon 		ar5416GetTargetPowersLeg(ah,  chan, pEepData->calTargetPower2G,
463*572ff6f6SMatthew Dillon 			AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdmExt, 4, AH_TRUE);
464*572ff6f6SMatthew Dillon 	}
465*572ff6f6SMatthew Dillon 
466*572ff6f6SMatthew Dillon 	/*
467*572ff6f6SMatthew Dillon 	 * For MIMO, need to apply regulatory caps individually across dynamically
468*572ff6f6SMatthew Dillon 	 * running modes: CCK, OFDM, HT20, HT40
469*572ff6f6SMatthew Dillon 	 *
470*572ff6f6SMatthew Dillon 	 * The outer loop walks through each possible applicable runtime mode.
471*572ff6f6SMatthew Dillon 	 * The inner loop walks through each ctlIndex entry in EEPROM.
472*572ff6f6SMatthew Dillon 	 * The ctl value is encoded as [7:4] == test group, [3:0] == test mode.
473*572ff6f6SMatthew Dillon 	 *
474*572ff6f6SMatthew Dillon 	 */
475*572ff6f6SMatthew Dillon 	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
476*572ff6f6SMatthew Dillon 		HAL_BOOL isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
477*572ff6f6SMatthew Dillon 		    (pCtlMode[ctlMode] == CTL_2GHT40);
478*572ff6f6SMatthew Dillon 		if (isHt40CtlMode) {
479*572ff6f6SMatthew Dillon 			freq = centers.ctl_center;
480*572ff6f6SMatthew Dillon 		} else if (pCtlMode[ctlMode] & EXT_ADDITIVE) {
481*572ff6f6SMatthew Dillon 			freq = centers.ext_center;
482*572ff6f6SMatthew Dillon 		} else {
483*572ff6f6SMatthew Dillon 			freq = centers.ctl_center;
484*572ff6f6SMatthew Dillon 		}
485*572ff6f6SMatthew Dillon 
486*572ff6f6SMatthew Dillon 		/* walk through each CTL index stored in EEPROM */
487*572ff6f6SMatthew Dillon 		for (i = 0; (i < AR5416_4K_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
488*572ff6f6SMatthew Dillon 			uint16_t twiceMinEdgePower;
489*572ff6f6SMatthew Dillon 
490*572ff6f6SMatthew Dillon 			/* compare test group from regulatory channel list with test mode from pCtlMode list */
491*572ff6f6SMatthew Dillon 			if ((((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) == pEepData->ctlIndex[i]) ||
492*572ff6f6SMatthew Dillon 				(((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) ==
493*572ff6f6SMatthew Dillon 				 ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
494*572ff6f6SMatthew Dillon 				rep = &(pEepData->ctlData[i]);
495*572ff6f6SMatthew Dillon 				twiceMinEdgePower = ar5416GetMaxEdgePower(freq,
496*572ff6f6SMatthew Dillon 							rep->ctlEdges[
497*572ff6f6SMatthew Dillon 							  owl_get_ntxchains(AH5416(ah)->ah_tx_chainmask) - 1], AH_TRUE);
498*572ff6f6SMatthew Dillon 				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
499*572ff6f6SMatthew Dillon 					/* Find the minimum of all CTL edge powers that apply to this channel */
500*572ff6f6SMatthew Dillon 					twiceMaxEdgePower = AH_MIN(twiceMaxEdgePower, twiceMinEdgePower);
501*572ff6f6SMatthew Dillon 				} else {
502*572ff6f6SMatthew Dillon 					/* specific */
503*572ff6f6SMatthew Dillon 					twiceMaxEdgePower = twiceMinEdgePower;
504*572ff6f6SMatthew Dillon 					break;
505*572ff6f6SMatthew Dillon 				}
506*572ff6f6SMatthew Dillon 			}
507*572ff6f6SMatthew Dillon 		}
508*572ff6f6SMatthew Dillon 		minCtlPower = (uint8_t)AH_MIN(twiceMaxEdgePower, scaledPower);
509*572ff6f6SMatthew Dillon 		/* Apply ctl mode to correct target power set */
510*572ff6f6SMatthew Dillon 		switch(pCtlMode[ctlMode]) {
511*572ff6f6SMatthew Dillon 		case CTL_11B:
512*572ff6f6SMatthew Dillon 			for (i = 0; i < N(targetPowerCck.tPow2x); i++) {
513*572ff6f6SMatthew Dillon 				targetPowerCck.tPow2x[i] = (uint8_t)AH_MIN(targetPowerCck.tPow2x[i], minCtlPower);
514*572ff6f6SMatthew Dillon 			}
515*572ff6f6SMatthew Dillon 			break;
516*572ff6f6SMatthew Dillon 		case CTL_11A:
517*572ff6f6SMatthew Dillon 		case CTL_11G:
518*572ff6f6SMatthew Dillon 			for (i = 0; i < N(targetPowerOfdm.tPow2x); i++) {
519*572ff6f6SMatthew Dillon 				targetPowerOfdm.tPow2x[i] = (uint8_t)AH_MIN(targetPowerOfdm.tPow2x[i], minCtlPower);
520*572ff6f6SMatthew Dillon 			}
521*572ff6f6SMatthew Dillon 			break;
522*572ff6f6SMatthew Dillon 		case CTL_5GHT20:
523*572ff6f6SMatthew Dillon 		case CTL_2GHT20:
524*572ff6f6SMatthew Dillon 			for (i = 0; i < N(targetPowerHt20.tPow2x); i++) {
525*572ff6f6SMatthew Dillon 				targetPowerHt20.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt20.tPow2x[i], minCtlPower);
526*572ff6f6SMatthew Dillon 			}
527*572ff6f6SMatthew Dillon 			break;
528*572ff6f6SMatthew Dillon 		case CTL_11B_EXT:
529*572ff6f6SMatthew Dillon 			targetPowerCckExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerCckExt.tPow2x[0], minCtlPower);
530*572ff6f6SMatthew Dillon 			break;
531*572ff6f6SMatthew Dillon 		case CTL_11G_EXT:
532*572ff6f6SMatthew Dillon 			targetPowerOfdmExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerOfdmExt.tPow2x[0], minCtlPower);
533*572ff6f6SMatthew Dillon 			break;
534*572ff6f6SMatthew Dillon 		case CTL_5GHT40:
535*572ff6f6SMatthew Dillon 		case CTL_2GHT40:
536*572ff6f6SMatthew Dillon 			for (i = 0; i < N(targetPowerHt40.tPow2x); i++) {
537*572ff6f6SMatthew Dillon 				targetPowerHt40.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt40.tPow2x[i], minCtlPower);
538*572ff6f6SMatthew Dillon 			}
539*572ff6f6SMatthew Dillon 			break;
540*572ff6f6SMatthew Dillon 		default:
541*572ff6f6SMatthew Dillon 			return AH_FALSE;
542*572ff6f6SMatthew Dillon 			break;
543*572ff6f6SMatthew Dillon 		}
544*572ff6f6SMatthew Dillon 	} /* end ctl mode checking */
545*572ff6f6SMatthew Dillon 
546*572ff6f6SMatthew Dillon         /* Set rates Array from collected data */
547*572ff6f6SMatthew Dillon 	ar5416SetRatesArrayFromTargetPower(ah, chan, ratesArray,
548*572ff6f6SMatthew Dillon 	    &targetPowerCck,
549*572ff6f6SMatthew Dillon 	    &targetPowerCckExt,
550*572ff6f6SMatthew Dillon 	    &targetPowerOfdm,
551*572ff6f6SMatthew Dillon 	    &targetPowerOfdmExt,
552*572ff6f6SMatthew Dillon 	    &targetPowerHt20,
553*572ff6f6SMatthew Dillon 	    &targetPowerHt40);
554*572ff6f6SMatthew Dillon 
555*572ff6f6SMatthew Dillon 	return AH_TRUE;
556*572ff6f6SMatthew Dillon #undef EXT_ADDITIVE
557*572ff6f6SMatthew Dillon #undef CTL_11G_EXT
558*572ff6f6SMatthew Dillon #undef CTL_11B_EXT
559*572ff6f6SMatthew Dillon #undef SUB_NUM_CTL_MODES_AT_2G_40
560*572ff6f6SMatthew Dillon #undef N
561*572ff6f6SMatthew Dillon }
562*572ff6f6SMatthew Dillon 
563*572ff6f6SMatthew Dillon static HAL_BOOL
ar9285SetPowerCalTable(struct ath_hal * ah,struct ar5416eeprom_4k * pEepData,const struct ieee80211_channel * chan,int16_t * pTxPowerIndexOffset)564*572ff6f6SMatthew Dillon ar9285SetPowerCalTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,
565*572ff6f6SMatthew Dillon 	const struct ieee80211_channel *chan, int16_t *pTxPowerIndexOffset)
566*572ff6f6SMatthew Dillon {
567*572ff6f6SMatthew Dillon     CAL_DATA_PER_FREQ_4K *pRawDataset;
568*572ff6f6SMatthew Dillon     uint8_t  *pCalBChans = AH_NULL;
569*572ff6f6SMatthew Dillon     uint16_t pdGainOverlap_t2;
570*572ff6f6SMatthew Dillon     static uint8_t  pdadcValues[AR5416_NUM_PDADC_VALUES];
571*572ff6f6SMatthew Dillon     uint16_t gainBoundaries[AR5416_PD_GAINS_IN_MASK];
572*572ff6f6SMatthew Dillon     uint16_t numPiers, i;
573*572ff6f6SMatthew Dillon     int16_t  tMinCalPower;
574*572ff6f6SMatthew Dillon     uint16_t numXpdGain, xpdMask;
575*572ff6f6SMatthew Dillon     uint16_t xpdGainValues[4];	/* v4k eeprom has 2; the other two stay 0 */
576*572ff6f6SMatthew Dillon     uint32_t regChainOffset;
577*572ff6f6SMatthew Dillon 
578*572ff6f6SMatthew Dillon     OS_MEMZERO(xpdGainValues, sizeof(xpdGainValues));
579*572ff6f6SMatthew Dillon 
580*572ff6f6SMatthew Dillon     xpdMask = pEepData->modalHeader.xpdGain;
581*572ff6f6SMatthew Dillon 
582*572ff6f6SMatthew Dillon     if (IS_EEP_MINOR_V2(ah)) {
583*572ff6f6SMatthew Dillon         pdGainOverlap_t2 = pEepData->modalHeader.pdGainOverlap;
584*572ff6f6SMatthew Dillon     } else {
585*572ff6f6SMatthew Dillon     	pdGainOverlap_t2 = (uint16_t)(MS(OS_REG_READ(ah, AR_PHY_TPCRG5), AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
586*572ff6f6SMatthew Dillon     }
587*572ff6f6SMatthew Dillon 
588*572ff6f6SMatthew Dillon     pCalBChans = pEepData->calFreqPier2G;
589*572ff6f6SMatthew Dillon     numPiers = AR5416_4K_NUM_2G_CAL_PIERS;
590*572ff6f6SMatthew Dillon     numXpdGain = 0;
591*572ff6f6SMatthew Dillon 
592*572ff6f6SMatthew Dillon     /* Calculate the value of xpdgains from the xpdGain Mask */
593*572ff6f6SMatthew Dillon     for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
594*572ff6f6SMatthew Dillon         if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
595*572ff6f6SMatthew Dillon             if (numXpdGain >= AR5416_4K_NUM_PD_GAINS) {
596*572ff6f6SMatthew Dillon                 HALASSERT(0);
597*572ff6f6SMatthew Dillon                 break;
598*572ff6f6SMatthew Dillon             }
599*572ff6f6SMatthew Dillon             xpdGainValues[numXpdGain] = (uint16_t)(AR5416_PD_GAINS_IN_MASK - i);
600*572ff6f6SMatthew Dillon             numXpdGain++;
601*572ff6f6SMatthew Dillon         }
602*572ff6f6SMatthew Dillon     }
603*572ff6f6SMatthew Dillon 
604*572ff6f6SMatthew Dillon     /* Write the detector gain biases and their number */
605*572ff6f6SMatthew Dillon     ar5416WriteDetectorGainBiases(ah, numXpdGain, xpdGainValues);
606*572ff6f6SMatthew Dillon 
607*572ff6f6SMatthew Dillon     for (i = 0; i < AR5416_MAX_CHAINS; i++) {
608*572ff6f6SMatthew Dillon 	regChainOffset = ar5416GetRegChainOffset(ah, i);
609*572ff6f6SMatthew Dillon         if (pEepData->baseEepHeader.txMask & (1 << i)) {
610*572ff6f6SMatthew Dillon             pRawDataset = pEepData->calPierData2G[i];
611*572ff6f6SMatthew Dillon 
612*572ff6f6SMatthew Dillon             ar9285GetGainBoundariesAndPdadcs(ah,  chan, pRawDataset,
613*572ff6f6SMatthew Dillon                                              pCalBChans, numPiers,
614*572ff6f6SMatthew Dillon                                              pdGainOverlap_t2,
615*572ff6f6SMatthew Dillon                                              &tMinCalPower, gainBoundaries,
616*572ff6f6SMatthew Dillon                                              pdadcValues, numXpdGain);
617*572ff6f6SMatthew Dillon 
618*572ff6f6SMatthew Dillon             if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
619*572ff6f6SMatthew Dillon                 /*
620*572ff6f6SMatthew Dillon                  * Note the pdadc table may not start at 0 dBm power, could be
621*572ff6f6SMatthew Dillon                  * negative or greater than 0.  Need to offset the power
622*572ff6f6SMatthew Dillon                  * values by the amount of minPower for griffin
623*572ff6f6SMatthew Dillon                  */
624*572ff6f6SMatthew Dillon 		ar5416SetGainBoundariesClosedLoop(ah, i, pdGainOverlap_t2, gainBoundaries);
625*572ff6f6SMatthew Dillon             }
626*572ff6f6SMatthew Dillon 
627*572ff6f6SMatthew Dillon             /* Write the power values into the baseband power table */
628*572ff6f6SMatthew Dillon 	    ar5416WritePdadcValues(ah, i, pdadcValues);
629*572ff6f6SMatthew Dillon         }
630*572ff6f6SMatthew Dillon     }
631*572ff6f6SMatthew Dillon     *pTxPowerIndexOffset = 0;
632*572ff6f6SMatthew Dillon 
633*572ff6f6SMatthew Dillon     return AH_TRUE;
634*572ff6f6SMatthew Dillon }
635*572ff6f6SMatthew Dillon 
636*572ff6f6SMatthew Dillon static void
ar9285GetGainBoundariesAndPdadcs(struct ath_hal * ah,const struct ieee80211_channel * chan,CAL_DATA_PER_FREQ_4K * pRawDataSet,uint8_t * bChans,uint16_t availPiers,uint16_t tPdGainOverlap,int16_t * pMinCalPower,uint16_t * pPdGainBoundaries,uint8_t * pPDADCValues,uint16_t numXpdGains)637*572ff6f6SMatthew Dillon ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,
638*572ff6f6SMatthew Dillon                                  const struct ieee80211_channel *chan,
639*572ff6f6SMatthew Dillon 				 CAL_DATA_PER_FREQ_4K *pRawDataSet,
640*572ff6f6SMatthew Dillon                                  uint8_t * bChans,  uint16_t availPiers,
641*572ff6f6SMatthew Dillon                                  uint16_t tPdGainOverlap, int16_t *pMinCalPower, uint16_t * pPdGainBoundaries,
642*572ff6f6SMatthew Dillon                                  uint8_t * pPDADCValues, uint16_t numXpdGains)
643*572ff6f6SMatthew Dillon {
644*572ff6f6SMatthew Dillon 
645*572ff6f6SMatthew Dillon     int       i, j, k;
646*572ff6f6SMatthew Dillon     int16_t   ss;         /* potentially -ve index for taking care of pdGainOverlap */
647*572ff6f6SMatthew Dillon     uint16_t  idxL, idxR, numPiers; /* Pier indexes */
648*572ff6f6SMatthew Dillon 
649*572ff6f6SMatthew Dillon     /* filled out Vpd table for all pdGains (chanL) */
650*572ff6f6SMatthew Dillon     static uint8_t   vpdTableL[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
651*572ff6f6SMatthew Dillon 
652*572ff6f6SMatthew Dillon     /* filled out Vpd table for all pdGains (chanR) */
653*572ff6f6SMatthew Dillon     static uint8_t   vpdTableR[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
654*572ff6f6SMatthew Dillon 
655*572ff6f6SMatthew Dillon     /* filled out Vpd table for all pdGains (interpolated) */
656*572ff6f6SMatthew Dillon     static uint8_t   vpdTableI[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
657*572ff6f6SMatthew Dillon 
658*572ff6f6SMatthew Dillon     uint8_t   *pVpdL, *pVpdR, *pPwrL, *pPwrR;
659*572ff6f6SMatthew Dillon     uint8_t   minPwrT4[AR5416_4K_NUM_PD_GAINS];
660*572ff6f6SMatthew Dillon     uint8_t   maxPwrT4[AR5416_4K_NUM_PD_GAINS];
661*572ff6f6SMatthew Dillon     int16_t   vpdStep;
662*572ff6f6SMatthew Dillon     int16_t   tmpVal;
663*572ff6f6SMatthew Dillon     uint16_t  sizeCurrVpdTable, maxIndex, tgtIndex;
664*572ff6f6SMatthew Dillon     HAL_BOOL    match;
665*572ff6f6SMatthew Dillon     int16_t  minDelta = 0;
666*572ff6f6SMatthew Dillon     CHAN_CENTERS centers;
667*572ff6f6SMatthew Dillon 
668*572ff6f6SMatthew Dillon     ar5416GetChannelCenters(ah, chan, &centers);
669*572ff6f6SMatthew Dillon 
670*572ff6f6SMatthew Dillon     /* Trim numPiers for the number of populated channel Piers */
671*572ff6f6SMatthew Dillon     for (numPiers = 0; numPiers < availPiers; numPiers++) {
672*572ff6f6SMatthew Dillon         if (bChans[numPiers] == AR5416_BCHAN_UNUSED) {
673*572ff6f6SMatthew Dillon             break;
674*572ff6f6SMatthew Dillon         }
675*572ff6f6SMatthew Dillon     }
676*572ff6f6SMatthew Dillon 
677*572ff6f6SMatthew Dillon     /* Find pier indexes around the current channel */
678*572ff6f6SMatthew Dillon     match = ath_ee_getLowerUpperIndex((uint8_t)FREQ2FBIN(centers.synth_center,
679*572ff6f6SMatthew Dillon       IEEE80211_IS_CHAN_2GHZ(chan)), bChans, numPiers, &idxL, &idxR);
680*572ff6f6SMatthew Dillon 
681*572ff6f6SMatthew Dillon     if (match) {
682*572ff6f6SMatthew Dillon         /* Directly fill both vpd tables from the matching index */
683*572ff6f6SMatthew Dillon         for (i = 0; i < numXpdGains; i++) {
684*572ff6f6SMatthew Dillon             minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
685*572ff6f6SMatthew Dillon             maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
686*572ff6f6SMatthew Dillon             ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i],
687*572ff6f6SMatthew Dillon 			       pRawDataSet[idxL].pwrPdg[i],
688*572ff6f6SMatthew Dillon                                pRawDataSet[idxL].vpdPdg[i],
689*572ff6f6SMatthew Dillon 			       AR5416_PD_GAIN_ICEPTS, vpdTableI[i]);
690*572ff6f6SMatthew Dillon         }
691*572ff6f6SMatthew Dillon     } else {
692*572ff6f6SMatthew Dillon         for (i = 0; i < numXpdGains; i++) {
693*572ff6f6SMatthew Dillon             pVpdL = pRawDataSet[idxL].vpdPdg[i];
694*572ff6f6SMatthew Dillon             pPwrL = pRawDataSet[idxL].pwrPdg[i];
695*572ff6f6SMatthew Dillon             pVpdR = pRawDataSet[idxR].vpdPdg[i];
696*572ff6f6SMatthew Dillon             pPwrR = pRawDataSet[idxR].pwrPdg[i];
697*572ff6f6SMatthew Dillon 
698*572ff6f6SMatthew Dillon             /* Start Vpd interpolation from the max of the minimum powers */
699*572ff6f6SMatthew Dillon             minPwrT4[i] = AH_MAX(pPwrL[0], pPwrR[0]);
700*572ff6f6SMatthew Dillon 
701*572ff6f6SMatthew Dillon             /* End Vpd interpolation from the min of the max powers */
702*572ff6f6SMatthew Dillon             maxPwrT4[i] = AH_MIN(pPwrL[AR5416_PD_GAIN_ICEPTS - 1], pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);
703*572ff6f6SMatthew Dillon             HALASSERT(maxPwrT4[i] > minPwrT4[i]);
704*572ff6f6SMatthew Dillon 
705*572ff6f6SMatthew Dillon             /* Fill pier Vpds */
706*572ff6f6SMatthew Dillon             ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrL, pVpdL,
707*572ff6f6SMatthew Dillon 			       AR5416_PD_GAIN_ICEPTS, vpdTableL[i]);
708*572ff6f6SMatthew Dillon             ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrR, pVpdR,
709*572ff6f6SMatthew Dillon 			       AR5416_PD_GAIN_ICEPTS, vpdTableR[i]);
710*572ff6f6SMatthew Dillon 
711*572ff6f6SMatthew Dillon             /* Interpolate the final vpd */
712*572ff6f6SMatthew Dillon             for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
713*572ff6f6SMatthew Dillon                 vpdTableI[i][j] = (uint8_t)(ath_ee_interpolate((uint16_t)FREQ2FBIN(centers.synth_center,
714*572ff6f6SMatthew Dillon                     IEEE80211_IS_CHAN_2GHZ(chan)),
715*572ff6f6SMatthew Dillon                     bChans[idxL], bChans[idxR], vpdTableL[i][j], vpdTableR[i][j]));
716*572ff6f6SMatthew Dillon             }
717*572ff6f6SMatthew Dillon         }
718*572ff6f6SMatthew Dillon     }
719*572ff6f6SMatthew Dillon     *pMinCalPower = (int16_t)(minPwrT4[0] / 2);
720*572ff6f6SMatthew Dillon 
721*572ff6f6SMatthew Dillon     k = 0; /* index for the final table */
722*572ff6f6SMatthew Dillon     for (i = 0; i < numXpdGains; i++) {
723*572ff6f6SMatthew Dillon         if (i == (numXpdGains - 1)) {
724*572ff6f6SMatthew Dillon             pPdGainBoundaries[i] = (uint16_t)(maxPwrT4[i] / 2);
725*572ff6f6SMatthew Dillon         } else {
726*572ff6f6SMatthew Dillon             pPdGainBoundaries[i] = (uint16_t)((maxPwrT4[i] + minPwrT4[i+1]) / 4);
727*572ff6f6SMatthew Dillon         }
728*572ff6f6SMatthew Dillon 
729*572ff6f6SMatthew Dillon         pPdGainBoundaries[i] = (uint16_t)AH_MIN(AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);
730*572ff6f6SMatthew Dillon 
731*572ff6f6SMatthew Dillon 	/* NB: only applies to owl 1.0 */
732*572ff6f6SMatthew Dillon         if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah) ) {
733*572ff6f6SMatthew Dillon 	    /*
734*572ff6f6SMatthew Dillon              * fix the gain delta, but get a delta that can be applied to min to
735*572ff6f6SMatthew Dillon              * keep the upper power values accurate, don't think max needs to
736*572ff6f6SMatthew Dillon              * be adjusted because should not be at that area of the table?
737*572ff6f6SMatthew Dillon 	     */
738*572ff6f6SMatthew Dillon             minDelta = pPdGainBoundaries[0] - 23;
739*572ff6f6SMatthew Dillon             pPdGainBoundaries[0] = 23;
740*572ff6f6SMatthew Dillon         }
741*572ff6f6SMatthew Dillon         else {
742*572ff6f6SMatthew Dillon             minDelta = 0;
743*572ff6f6SMatthew Dillon         }
744*572ff6f6SMatthew Dillon 
745*572ff6f6SMatthew Dillon         /* Find starting index for this pdGain */
746*572ff6f6SMatthew Dillon         if (i == 0) {
747*572ff6f6SMatthew Dillon             if (AR_SREV_MERLIN_20_OR_LATER(ah))
748*572ff6f6SMatthew Dillon                 ss = (int16_t)(0 - (minPwrT4[i] / 2));
749*572ff6f6SMatthew Dillon             else
750*572ff6f6SMatthew Dillon                 ss = 0; /* for the first pdGain, start from index 0 */
751*572ff6f6SMatthew Dillon         } else {
752*572ff6f6SMatthew Dillon 	    /* need overlap entries extrapolated below. */
753*572ff6f6SMatthew Dillon             ss = (int16_t)((pPdGainBoundaries[i-1] - (minPwrT4[i] / 2)) - tPdGainOverlap + 1 + minDelta);
754*572ff6f6SMatthew Dillon         }
755*572ff6f6SMatthew Dillon         vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
756*572ff6f6SMatthew Dillon         vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
757*572ff6f6SMatthew Dillon         /*
758*572ff6f6SMatthew Dillon          *-ve ss indicates need to extrapolate data below for this pdGain
759*572ff6f6SMatthew Dillon          */
760*572ff6f6SMatthew Dillon         while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
761*572ff6f6SMatthew Dillon             tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
762*572ff6f6SMatthew Dillon             pPDADCValues[k++] = (uint8_t)((tmpVal < 0) ? 0 : tmpVal);
763*572ff6f6SMatthew Dillon             ss++;
764*572ff6f6SMatthew Dillon         }
765*572ff6f6SMatthew Dillon 
766*572ff6f6SMatthew Dillon         sizeCurrVpdTable = (uint8_t)((maxPwrT4[i] - minPwrT4[i]) / 2 +1);
767*572ff6f6SMatthew Dillon         tgtIndex = (uint8_t)(pPdGainBoundaries[i] + tPdGainOverlap - (minPwrT4[i] / 2));
768*572ff6f6SMatthew Dillon         maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
769*572ff6f6SMatthew Dillon 
770*572ff6f6SMatthew Dillon         while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
771*572ff6f6SMatthew Dillon             pPDADCValues[k++] = vpdTableI[i][ss++];
772*572ff6f6SMatthew Dillon         }
773*572ff6f6SMatthew Dillon 
774*572ff6f6SMatthew Dillon         vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] - vpdTableI[i][sizeCurrVpdTable - 2]);
775*572ff6f6SMatthew Dillon         vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
776*572ff6f6SMatthew Dillon         /*
777*572ff6f6SMatthew Dillon          * for last gain, pdGainBoundary == Pmax_t2, so will
778*572ff6f6SMatthew Dillon          * have to extrapolate
779*572ff6f6SMatthew Dillon          */
780*572ff6f6SMatthew Dillon         if (tgtIndex >= maxIndex) {  /* need to extrapolate above */
781*572ff6f6SMatthew Dillon             while ((ss <= tgtIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
782*572ff6f6SMatthew Dillon                 tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
783*572ff6f6SMatthew Dillon                           (ss - maxIndex +1) * vpdStep));
784*572ff6f6SMatthew Dillon                 pPDADCValues[k++] = (uint8_t)((tmpVal > 255) ? 255 : tmpVal);
785*572ff6f6SMatthew Dillon                 ss++;
786*572ff6f6SMatthew Dillon             }
787*572ff6f6SMatthew Dillon         }               /* extrapolated above */
788*572ff6f6SMatthew Dillon     }                   /* for all pdGainUsed */
789*572ff6f6SMatthew Dillon 
790*572ff6f6SMatthew Dillon     /* Fill out pdGainBoundaries - only up to 2 allowed here, but hardware allows up to 4 */
791*572ff6f6SMatthew Dillon     while (i < AR5416_PD_GAINS_IN_MASK) {
792*572ff6f6SMatthew Dillon         pPdGainBoundaries[i] = AR5416_4K_EEP_PD_GAIN_BOUNDARY_DEFAULT;
793*572ff6f6SMatthew Dillon         i++;
794*572ff6f6SMatthew Dillon     }
795*572ff6f6SMatthew Dillon 
796*572ff6f6SMatthew Dillon     while (k < AR5416_NUM_PDADC_VALUES) {
797*572ff6f6SMatthew Dillon         pPDADCValues[k] = pPDADCValues[k-1];
798*572ff6f6SMatthew Dillon         k++;
799*572ff6f6SMatthew Dillon     }
800*572ff6f6SMatthew Dillon     return;
801*572ff6f6SMatthew Dillon }
802