2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Copyright (c) 2002-2008 Atheros Communications, Inc.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * This is almost the same as ar5416_reset.c but uses the v4k EEPROM and
24 * supports only 2Ghz operation.
30 #include "ah_internal.h"
33 #include "ah_eeprom_v14.h"
34 #include "ah_eeprom_v4k.h"
36 #include "ar9002/ar9285.h"
37 #include "ar5416/ar5416.h"
38 #include "ar5416/ar5416reg.h"
39 #include "ar5416/ar5416phy.h"
40 #include "ar9002/ar9002phy.h"
41 #include "ar9002/ar9285phy.h"
42 #include "ar9002/ar9285an.h"
43 #include "ar9002/ar9285_diversity.h"
45 /* Eeprom versioning macros. Returns true if the version is equal or newer than the ver specified */
46 #define EEP_MINOR(_ah) \
47 (AH_PRIVATE(_ah)->ah_eeversion & AR5416_EEP_VER_MINOR_MASK)
48 #define IS_EEP_MINOR_V2(_ah) (EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_2)
49 #define IS_EEP_MINOR_V3(_ah) (EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_3)
51 /* Additional Time delay to wait after activiting the Base band */
52 #define BASE_ACTIVATE_DELAY 100 /* 100 usec */
53 #define PLL_SETTLE_DELAY 300 /* 300 usec */
54 #define RTC_PLL_SETTLE_DELAY 1000 /* 1 ms */
56 static HAL_BOOL ar9285SetPowerPerRateTable(struct ath_hal *ah,
57 struct ar5416eeprom_4k *pEepData,
58 const struct ieee80211_channel *chan, int16_t *ratesArray,
59 uint16_t cfgCtl, uint16_t AntennaReduction,
60 uint16_t twiceMaxRegulatoryPower,
62 static HAL_BOOL ar9285SetPowerCalTable(struct ath_hal *ah,
63 struct ar5416eeprom_4k *pEepData,
64 const struct ieee80211_channel *chan,
65 int16_t *pTxPowerIndexOffset);
66 static void ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,
67 const struct ieee80211_channel *chan, CAL_DATA_PER_FREQ_4K *pRawDataSet,
68 uint8_t * bChans, uint16_t availPiers,
69 uint16_t tPdGainOverlap, int16_t *pMinCalPower,
70 uint16_t * pPdGainBoundaries, uint8_t * pPDADCValues,
71 uint16_t numXpdGains);
74 ar9285SetTransmitPower(struct ath_hal *ah,
75 const struct ieee80211_channel *chan, uint16_t *rfXpdGain)
77 #define POW_SM(_r, _s) (((_r) & 0x3f) << (_s))
78 #define N(a) (sizeof (a) / sizeof (a[0]))
80 MODAL_EEP4K_HEADER *pModal;
81 struct ath_hal_5212 *ahp = AH5212(ah);
82 int16_t txPowerIndexOffset = 0;
87 uint16_t twiceAntennaReduction;
88 uint16_t twiceMaxRegulatoryPower;
90 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
91 struct ar5416eeprom_4k *pEepData = &ee->ee_base;
93 HALASSERT(AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER14_1);
95 AH5416(ah)->ah_ht40PowerIncForPdadc = 2;
97 /* Setup info for the actual eeprom */
98 OS_MEMZERO(AH5416(ah)->ah_ratesArray, sizeof(AH5416(ah)->ah_ratesArray));
99 cfgCtl = ath_hal_getctl(ah, chan);
100 powerLimit = chan->ic_maxregpower * 2;
101 twiceAntennaReduction = chan->ic_maxantgain;
102 twiceMaxRegulatoryPower = AH_MIN(MAX_RATE_POWER, AH_PRIVATE(ah)->ah_powerLimit);
103 pModal = &pEepData->modalHeader;
104 HALDEBUG(ah, HAL_DEBUG_RESET, "%s Channel=%u CfgCtl=%u\n",
105 __func__,chan->ic_freq, cfgCtl );
107 if (IS_EEP_MINOR_V2(ah)) {
108 AH5416(ah)->ah_ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
111 if (!ar9285SetPowerPerRateTable(ah, pEepData, chan,
112 &AH5416(ah)->ah_ratesArray[0],cfgCtl,
113 twiceAntennaReduction,
114 twiceMaxRegulatoryPower, powerLimit)) {
115 HALDEBUG(ah, HAL_DEBUG_ANY,
116 "%s: unable to set tx power per rate table\n", __func__);
120 if (!ar9285SetPowerCalTable(ah, pEepData, chan, &txPowerIndexOffset)) {
121 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set power table\n",
126 maxPower = AH_MAX(AH5416(ah)->ah_ratesArray[rate6mb],
127 AH5416(ah)->ah_ratesArray[rateHt20_0]);
128 maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rate1l]);
130 if (IEEE80211_IS_CHAN_HT40(chan)) {
131 maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rateHt40_0]);
134 ahp->ah_tx6PowerInHalfDbm = maxPower;
135 AH_PRIVATE(ah)->ah_maxPowerLevel = maxPower;
136 ahp->ah_txPowerIndexOffset = txPowerIndexOffset;
139 * txPowerIndexOffset is set by the SetPowerTable() call -
140 * adjust the rate table (0 offset if rates EEPROM not loaded)
142 for (i = 0; i < N(AH5416(ah)->ah_ratesArray); i++) {
143 AH5416(ah)->ah_ratesArray[i] = (int16_t)(txPowerIndexOffset + AH5416(ah)->ah_ratesArray[i]);
144 /* -5 dBm offset for Merlin and later; this includes Kite */
145 AH5416(ah)->ah_ratesArray[i] -= AR5416_PWR_TABLE_OFFSET_DB * 2;
146 if (AH5416(ah)->ah_ratesArray[i] > AR5416_MAX_RATE_POWER)
147 AH5416(ah)->ah_ratesArray[i] = AR5416_MAX_RATE_POWER;
148 if (AH5416(ah)->ah_ratesArray[i] < 0)
149 AH5416(ah)->ah_ratesArray[i] = 0;
152 #ifdef AH_EEPROM_DUMP
153 ar5416PrintPowerPerRate(ah, AH5416(ah)->ah_ratesArray);
157 * Adjust the HT40 power to meet the correct target TX power
158 * for 40MHz mode, based on TX power curves that are established
161 * XXX handle overflow/too high power level?
163 if (IEEE80211_IS_CHAN_HT40(chan)) {
164 AH5416(ah)->ah_ratesArray[rateHt40_0] +=
165 AH5416(ah)->ah_ht40PowerIncForPdadc;
166 AH5416(ah)->ah_ratesArray[rateHt40_1] +=
167 AH5416(ah)->ah_ht40PowerIncForPdadc;
168 AH5416(ah)->ah_ratesArray[rateHt40_2] +=
169 AH5416(ah)->ah_ht40PowerIncForPdadc;
170 AH5416(ah)->ah_ratesArray[rateHt40_3] +=
171 AH5416(ah)->ah_ht40PowerIncForPdadc;
172 AH5416(ah)->ah_ratesArray[rateHt40_4] +=
173 AH5416(ah)->ah_ht40PowerIncForPdadc;
174 AH5416(ah)->ah_ratesArray[rateHt40_5] +=
175 AH5416(ah)->ah_ht40PowerIncForPdadc;
176 AH5416(ah)->ah_ratesArray[rateHt40_6] +=
177 AH5416(ah)->ah_ht40PowerIncForPdadc;
178 AH5416(ah)->ah_ratesArray[rateHt40_7] +=
179 AH5416(ah)->ah_ht40PowerIncForPdadc;
182 /* Write the TX power rate registers */
183 ar5416WriteTxPowerRateRegisters(ah, chan, AH5416(ah)->ah_ratesArray);
191 ar9285SetBoardGain(struct ath_hal *ah, const MODAL_EEP4K_HEADER *pModal,
192 const struct ar5416eeprom_4k *eep, uint8_t txRxAttenLocal)
194 OS_REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0,
195 pModal->antCtrlChain[0]);
197 OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4_CHAIN(0),
198 (OS_REG_READ(ah, AR_PHY_TIMING_CTRL4_CHAIN(0)) &
199 ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
200 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
201 SM(pModal->iqCalICh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
202 SM(pModal->iqCalQCh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
204 if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
205 AR5416_EEP_MINOR_VER_3) {
206 txRxAttenLocal = pModal->txRxAttenCh[0];
208 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
209 AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN, pModal->bswMargin[0]);
210 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
211 AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
212 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
213 AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN, pModal->xatten2Margin[0]);
214 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
215 AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
217 /* Set the block 1 value to block 0 value */
218 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
219 AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
220 pModal->bswMargin[0]);
221 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
222 AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
223 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
224 AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
225 pModal->xatten2Margin[0]);
226 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
227 AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
230 OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,
231 AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
232 OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,
233 AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
235 OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
236 AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
237 OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
238 AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
242 * Read EEPROM header info and program the device for correct operation
243 * given the channel value.
246 ar9285SetBoardValues(struct ath_hal *ah, const struct ieee80211_channel *chan)
248 const HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
249 const struct ar5416eeprom_4k *eep = &ee->ee_base;
250 const MODAL_EEP4K_HEADER *pModal;
251 uint8_t txRxAttenLocal;
252 uint8_t ob[5], db1[5], db2[5];
254 pModal = &eep->modalHeader;
257 OS_REG_WRITE(ah, AR_PHY_SWITCH_COM, pModal->antCtrlCommon);
259 /* Single chain for 4K EEPROM*/
260 ar9285SetBoardGain(ah, pModal, eep, txRxAttenLocal);
262 /* Initialize Ant Diversity settings if supported */
263 (void) ar9285SetAntennaSwitch(ah, AH5212(ah)->ah_antControl);
265 /* Configure TX power calibration */
266 if (pModal->version >= 2) {
267 ob[0] = pModal->ob_0;
268 ob[1] = pModal->ob_1;
269 ob[2] = pModal->ob_2;
270 ob[3] = pModal->ob_3;
271 ob[4] = pModal->ob_4;
273 db1[0] = pModal->db1_0;
274 db1[1] = pModal->db1_1;
275 db1[2] = pModal->db1_2;
276 db1[3] = pModal->db1_3;
277 db1[4] = pModal->db1_4;
279 db2[0] = pModal->db2_0;
280 db2[1] = pModal->db2_1;
281 db2[2] = pModal->db2_2;
282 db2[3] = pModal->db2_3;
283 db2[4] = pModal->db2_4;
284 } else if (pModal->version == 1) {
285 ob[0] = pModal->ob_0;
286 ob[1] = ob[2] = ob[3] = ob[4] = pModal->ob_1;
287 db1[0] = pModal->db1_0;
288 db1[1] = db1[2] = db1[3] = db1[4] = pModal->db1_1;
289 db2[0] = pModal->db2_0;
290 db2[1] = db2[2] = db2[3] = db2[4] = pModal->db2_1;
294 for (i = 0; i < 5; i++) {
295 ob[i] = pModal->ob_0;
296 db1[i] = pModal->db1_0;
297 db2[i] = pModal->db1_0;
301 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_0, ob[0]);
302 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_1, ob[1]);
303 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_2, ob[2]);
304 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_3, ob[3]);
305 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_4, ob[4]);
307 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_0, db1[0]);
308 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_1, db1[1]);
309 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_2, db1[2]);
310 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_3, db1[3]);
311 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_4, db1[4]);
313 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_0, db2[0]);
314 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_1, db2[1]);
315 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_2, db2[2]);
316 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_3, db2[3]);
317 OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_4, db2[4]);
319 OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
320 pModal->switchSettling);
321 OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
322 pModal->adcDesiredSize);
324 OS_REG_WRITE(ah, AR_PHY_RF_CTL4,
325 SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF) |
326 SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF) |
327 SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON) |
328 SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));
330 OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
331 pModal->txEndToRxOn);
333 OS_REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
335 OS_REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0, AR_PHY_EXT_CCA0_THRESH62,
338 if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
339 AR5416_EEP_MINOR_VER_2) {
340 OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_DATA_START,
341 pModal->txFrameToDataStart);
342 OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_PA_ON,
343 pModal->txFrameToPaOn);
346 if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
347 AR5416_EEP_MINOR_VER_3) {
348 if (IEEE80211_IS_CHAN_HT40(chan))
349 OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING,
350 AR_PHY_SETTLING_SWITCH, pModal->swSettleHt40);
354 * Program the CCK TX gain factor appropriately if needed.
355 * The AR9285/AR9271 has a non-constant PA tx gain behaviour
356 * for CCK versus OFDM rates; other chips deal with this
359 * The mask/shift/multiply hackery is done so place the same
360 * value (bb_desired_scale) into multiple 5-bit fields.
361 * For example, AR_PHY_TX_PWRCTRL9 has bb_desired_scale written
362 * to three fields: (0..4), (5..9) and (10..14).
364 if (AR_SREV_9271(ah) || AR_SREV_KITE(ah)) {
365 uint8_t bb_desired_scale = (pModal->bb_scale_smrt_antenna & EEP_4K_BB_DESIRED_SCALE_MASK);
366 if ((eep->baseEepHeader.txGainType == 0) && (bb_desired_scale != 0)) {
367 ath_hal_printf(ah, "[ath]: adjusting cck tx gain factor\n");
368 uint32_t pwrctrl, mask, clr;
370 mask = (1<<0) | (1<<5) | (1<<10) | (1<<15) | (1<<20) | (1<<25);
371 pwrctrl = mask * bb_desired_scale;
373 OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL8, pwrctrl, clr);
374 OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL10, pwrctrl, clr);
375 OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL12, pwrctrl, clr);
377 mask = (1<<0) | (1<<5) | (1<<15);
378 pwrctrl = mask * bb_desired_scale;
380 OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL9, pwrctrl, clr);
382 mask = (1<<0) | (1<<5);
383 pwrctrl = mask * bb_desired_scale;
385 OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL11, pwrctrl, clr);
386 OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL13, pwrctrl, clr);
394 * Helper functions common for AP/CB/XB
398 ar9285SetPowerPerRateTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,
399 const struct ieee80211_channel *chan,
400 int16_t *ratesArray, uint16_t cfgCtl,
401 uint16_t AntennaReduction,
402 uint16_t twiceMaxRegulatoryPower,
405 #define N(a) (sizeof(a)/sizeof(a[0]))
406 /* Local defines to distinguish between extension and control CTL's */
407 #define EXT_ADDITIVE (0x8000)
408 #define CTL_11G_EXT (CTL_11G | EXT_ADDITIVE)
409 #define CTL_11B_EXT (CTL_11B | EXT_ADDITIVE)
411 uint16_t twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
413 int16_t twiceLargestAntenna;
414 CAL_CTL_DATA_4K *rep;
415 CAL_TARGET_POWER_LEG targetPowerOfdm, targetPowerCck = {0, {0, 0, 0, 0}};
416 CAL_TARGET_POWER_LEG targetPowerOfdmExt = {0, {0, 0, 0, 0}}, targetPowerCckExt = {0, {0, 0, 0, 0}};
417 CAL_TARGET_POWER_HT targetPowerHt20, targetPowerHt40 = {0, {0, 0, 0, 0}};
418 int16_t scaledPower, minCtlPower;
420 #define SUB_NUM_CTL_MODES_AT_2G_40 3 /* excluding HT40, EXT-OFDM, EXT-CCK */
421 static const uint16_t ctlModesFor11g[] = {
422 CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT, CTL_2GHT40
424 const uint16_t *pCtlMode;
425 uint16_t numCtlModes, ctlMode, freq;
426 CHAN_CENTERS centers;
428 ar5416GetChannelCenters(ah, chan, ¢ers);
430 /* Compute TxPower reduction due to Antenna Gain */
432 twiceLargestAntenna = pEepData->modalHeader.antennaGainCh[0];
433 twiceLargestAntenna = (int16_t)AH_MIN((AntennaReduction) - twiceLargestAntenna, 0);
435 /* XXX setup for 5212 use (really used?) */
436 ath_hal_eepromSet(ah, AR_EEP_ANTGAINMAX_2, twiceLargestAntenna);
439 * scaledPower is the minimum of the user input power level and
440 * the regulatory allowed power level
442 scaledPower = AH_MIN(powerLimit, twiceMaxRegulatoryPower + twiceLargestAntenna);
444 /* Get target powers from EEPROM - our baseline for TX Power */
445 /* Setup for CTL modes */
446 numCtlModes = N(ctlModesFor11g) - SUB_NUM_CTL_MODES_AT_2G_40; /* CTL_11B, CTL_11G, CTL_2GHT20 */
447 pCtlMode = ctlModesFor11g;
449 ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPowerCck,
450 AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCck, 4, AH_FALSE);
451 ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPower2G,
452 AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdm, 4, AH_FALSE);
453 ar5416GetTargetPowers(ah, chan, pEepData->calTargetPower2GHT20,
454 AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerHt20, 8, AH_FALSE);
456 if (IEEE80211_IS_CHAN_HT40(chan)) {
457 numCtlModes = N(ctlModesFor11g); /* All 2G CTL's */
459 ar5416GetTargetPowers(ah, chan, pEepData->calTargetPower2GHT40,
460 AR5416_4K_NUM_2G_40_TARGET_POWERS, &targetPowerHt40, 8, AH_TRUE);
461 /* Get target powers for extension channels */
462 ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPowerCck,
463 AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCckExt, 4, AH_TRUE);
464 ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPower2G,
465 AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdmExt, 4, AH_TRUE);
469 * For MIMO, need to apply regulatory caps individually across dynamically
470 * running modes: CCK, OFDM, HT20, HT40
472 * The outer loop walks through each possible applicable runtime mode.
473 * The inner loop walks through each ctlIndex entry in EEPROM.
474 * The ctl value is encoded as [7:4] == test group, [3:0] == test mode.
477 for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
478 HAL_BOOL isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
479 (pCtlMode[ctlMode] == CTL_2GHT40);
481 freq = centers.ctl_center;
482 } else if (pCtlMode[ctlMode] & EXT_ADDITIVE) {
483 freq = centers.ext_center;
485 freq = centers.ctl_center;
488 /* walk through each CTL index stored in EEPROM */
489 for (i = 0; (i < AR5416_4K_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
490 uint16_t twiceMinEdgePower;
492 /* compare test group from regulatory channel list with test mode from pCtlMode list */
493 if ((((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) == pEepData->ctlIndex[i]) ||
494 (((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) ==
495 ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
496 rep = &(pEepData->ctlData[i]);
497 twiceMinEdgePower = ar5416GetMaxEdgePower(freq,
499 owl_get_ntxchains(AH5416(ah)->ah_tx_chainmask) - 1], AH_TRUE);
500 if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
501 /* Find the minimum of all CTL edge powers that apply to this channel */
502 twiceMaxEdgePower = AH_MIN(twiceMaxEdgePower, twiceMinEdgePower);
505 twiceMaxEdgePower = twiceMinEdgePower;
510 minCtlPower = (uint8_t)AH_MIN(twiceMaxEdgePower, scaledPower);
511 /* Apply ctl mode to correct target power set */
512 switch(pCtlMode[ctlMode]) {
514 for (i = 0; i < N(targetPowerCck.tPow2x); i++) {
515 targetPowerCck.tPow2x[i] = (uint8_t)AH_MIN(targetPowerCck.tPow2x[i], minCtlPower);
520 for (i = 0; i < N(targetPowerOfdm.tPow2x); i++) {
521 targetPowerOfdm.tPow2x[i] = (uint8_t)AH_MIN(targetPowerOfdm.tPow2x[i], minCtlPower);
526 for (i = 0; i < N(targetPowerHt20.tPow2x); i++) {
527 targetPowerHt20.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt20.tPow2x[i], minCtlPower);
531 targetPowerCckExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerCckExt.tPow2x[0], minCtlPower);
534 targetPowerOfdmExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerOfdmExt.tPow2x[0], minCtlPower);
538 for (i = 0; i < N(targetPowerHt40.tPow2x); i++) {
539 targetPowerHt40.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt40.tPow2x[i], minCtlPower);
546 } /* end ctl mode checking */
548 /* Set rates Array from collected data */
549 ar5416SetRatesArrayFromTargetPower(ah, chan, ratesArray,
561 #undef SUB_NUM_CTL_MODES_AT_2G_40
566 ar9285SetPowerCalTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,
567 const struct ieee80211_channel *chan, int16_t *pTxPowerIndexOffset)
569 CAL_DATA_PER_FREQ_4K *pRawDataset;
570 uint8_t *pCalBChans = AH_NULL;
571 uint16_t pdGainOverlap_t2;
572 static uint8_t pdadcValues[AR5416_NUM_PDADC_VALUES];
573 uint16_t gainBoundaries[AR5416_PD_GAINS_IN_MASK];
574 uint16_t numPiers, i;
575 int16_t tMinCalPower;
576 uint16_t numXpdGain, xpdMask;
577 uint16_t xpdGainValues[4]; /* v4k eeprom has 2; the other two stay 0 */
578 uint32_t regChainOffset;
580 OS_MEMZERO(xpdGainValues, sizeof(xpdGainValues));
582 xpdMask = pEepData->modalHeader.xpdGain;
584 if (IS_EEP_MINOR_V2(ah)) {
585 pdGainOverlap_t2 = pEepData->modalHeader.pdGainOverlap;
587 pdGainOverlap_t2 = (uint16_t)(MS(OS_REG_READ(ah, AR_PHY_TPCRG5), AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
590 pCalBChans = pEepData->calFreqPier2G;
591 numPiers = AR5416_4K_NUM_2G_CAL_PIERS;
594 /* Calculate the value of xpdgains from the xpdGain Mask */
595 for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
596 if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
597 if (numXpdGain >= AR5416_4K_NUM_PD_GAINS) {
601 xpdGainValues[numXpdGain] = (uint16_t)(AR5416_PD_GAINS_IN_MASK - i);
606 /* Write the detector gain biases and their number */
607 ar5416WriteDetectorGainBiases(ah, numXpdGain, xpdGainValues);
609 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
610 regChainOffset = ar5416GetRegChainOffset(ah, i);
611 if (pEepData->baseEepHeader.txMask & (1 << i)) {
612 pRawDataset = pEepData->calPierData2G[i];
614 ar9285GetGainBoundariesAndPdadcs(ah, chan, pRawDataset,
615 pCalBChans, numPiers,
617 &tMinCalPower, gainBoundaries,
618 pdadcValues, numXpdGain);
620 if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
622 * Note the pdadc table may not start at 0 dBm power, could be
623 * negative or greater than 0. Need to offset the power
624 * values by the amount of minPower for griffin
626 ar5416SetGainBoundariesClosedLoop(ah, i, pdGainOverlap_t2, gainBoundaries);
629 /* Write the power values into the baseband power table */
630 ar5416WritePdadcValues(ah, i, pdadcValues);
633 *pTxPowerIndexOffset = 0;
639 ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,
640 const struct ieee80211_channel *chan,
641 CAL_DATA_PER_FREQ_4K *pRawDataSet,
642 uint8_t * bChans, uint16_t availPiers,
643 uint16_t tPdGainOverlap, int16_t *pMinCalPower, uint16_t * pPdGainBoundaries,
644 uint8_t * pPDADCValues, uint16_t numXpdGains)
648 int16_t ss; /* potentially -ve index for taking care of pdGainOverlap */
649 uint16_t idxL, idxR, numPiers; /* Pier indexes */
651 /* filled out Vpd table for all pdGains (chanL) */
652 static uint8_t vpdTableL[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
654 /* filled out Vpd table for all pdGains (chanR) */
655 static uint8_t vpdTableR[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
657 /* filled out Vpd table for all pdGains (interpolated) */
658 static uint8_t vpdTableI[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];
660 uint8_t *pVpdL, *pVpdR, *pPwrL, *pPwrR;
661 uint8_t minPwrT4[AR5416_4K_NUM_PD_GAINS];
662 uint8_t maxPwrT4[AR5416_4K_NUM_PD_GAINS];
665 uint16_t sizeCurrVpdTable, maxIndex, tgtIndex;
667 int16_t minDelta = 0;
668 CHAN_CENTERS centers;
670 ar5416GetChannelCenters(ah, chan, ¢ers);
672 /* Trim numPiers for the number of populated channel Piers */
673 for (numPiers = 0; numPiers < availPiers; numPiers++) {
674 if (bChans[numPiers] == AR5416_BCHAN_UNUSED) {
679 /* Find pier indexes around the current channel */
680 match = ath_ee_getLowerUpperIndex((uint8_t)FREQ2FBIN(centers.synth_center,
681 IEEE80211_IS_CHAN_2GHZ(chan)), bChans, numPiers, &idxL, &idxR);
684 /* Directly fill both vpd tables from the matching index */
685 for (i = 0; i < numXpdGains; i++) {
686 minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
687 maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
688 ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i],
689 pRawDataSet[idxL].pwrPdg[i],
690 pRawDataSet[idxL].vpdPdg[i],
691 AR5416_PD_GAIN_ICEPTS, vpdTableI[i]);
694 for (i = 0; i < numXpdGains; i++) {
695 pVpdL = pRawDataSet[idxL].vpdPdg[i];
696 pPwrL = pRawDataSet[idxL].pwrPdg[i];
697 pVpdR = pRawDataSet[idxR].vpdPdg[i];
698 pPwrR = pRawDataSet[idxR].pwrPdg[i];
700 /* Start Vpd interpolation from the max of the minimum powers */
701 minPwrT4[i] = AH_MAX(pPwrL[0], pPwrR[0]);
703 /* End Vpd interpolation from the min of the max powers */
704 maxPwrT4[i] = AH_MIN(pPwrL[AR5416_PD_GAIN_ICEPTS - 1], pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);
705 HALASSERT(maxPwrT4[i] > minPwrT4[i]);
708 ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrL, pVpdL,
709 AR5416_PD_GAIN_ICEPTS, vpdTableL[i]);
710 ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrR, pVpdR,
711 AR5416_PD_GAIN_ICEPTS, vpdTableR[i]);
713 /* Interpolate the final vpd */
714 for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
715 vpdTableI[i][j] = (uint8_t)(ath_ee_interpolate((uint16_t)FREQ2FBIN(centers.synth_center,
716 IEEE80211_IS_CHAN_2GHZ(chan)),
717 bChans[idxL], bChans[idxR], vpdTableL[i][j], vpdTableR[i][j]));
721 *pMinCalPower = (int16_t)(minPwrT4[0] / 2);
723 k = 0; /* index for the final table */
724 for (i = 0; i < numXpdGains; i++) {
725 if (i == (numXpdGains - 1)) {
726 pPdGainBoundaries[i] = (uint16_t)(maxPwrT4[i] / 2);
728 pPdGainBoundaries[i] = (uint16_t)((maxPwrT4[i] + minPwrT4[i+1]) / 4);
731 pPdGainBoundaries[i] = (uint16_t)AH_MIN(AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);
733 /* NB: only applies to owl 1.0 */
734 if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah) ) {
736 * fix the gain delta, but get a delta that can be applied to min to
737 * keep the upper power values accurate, don't think max needs to
738 * be adjusted because should not be at that area of the table?
740 minDelta = pPdGainBoundaries[0] - 23;
741 pPdGainBoundaries[0] = 23;
747 /* Find starting index for this pdGain */
749 if (AR_SREV_MERLIN_20_OR_LATER(ah))
750 ss = (int16_t)(0 - (minPwrT4[i] / 2));
752 ss = 0; /* for the first pdGain, start from index 0 */
754 /* need overlap entries extrapolated below. */
755 ss = (int16_t)((pPdGainBoundaries[i-1] - (minPwrT4[i] / 2)) - tPdGainOverlap + 1 + minDelta);
757 vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
758 vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
760 *-ve ss indicates need to extrapolate data below for this pdGain
762 while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
763 tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
764 pPDADCValues[k++] = (uint8_t)((tmpVal < 0) ? 0 : tmpVal);
768 sizeCurrVpdTable = (uint8_t)((maxPwrT4[i] - minPwrT4[i]) / 2 +1);
769 tgtIndex = (uint8_t)(pPdGainBoundaries[i] + tPdGainOverlap - (minPwrT4[i] / 2));
770 maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
772 while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
773 pPDADCValues[k++] = vpdTableI[i][ss++];
776 vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] - vpdTableI[i][sizeCurrVpdTable - 2]);
777 vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
779 * for last gain, pdGainBoundary == Pmax_t2, so will
780 * have to extrapolate
782 if (tgtIndex >= maxIndex) { /* need to extrapolate above */
783 while ((ss <= tgtIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
784 tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
785 (ss - maxIndex +1) * vpdStep));
786 pPDADCValues[k++] = (uint8_t)((tmpVal > 255) ? 255 : tmpVal);
789 } /* extrapolated above */
790 } /* for all pdGainUsed */
792 /* Fill out pdGainBoundaries - only up to 2 allowed here, but hardware allows up to 4 */
793 while (i < AR5416_PD_GAINS_IN_MASK) {
794 pPdGainBoundaries[i] = AR5416_4K_EEP_PD_GAIN_BOUNDARY_DEFAULT;
798 while (k < AR5416_NUM_PDADC_VALUES) {
799 pPDADCValues[k] = pPDADCValues[k-1];