Path: blob/main/sys/dev/ath/ath_hal/ar9002/ar9285_reset.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2002-2009 Sam Leffler, Errno Consulting4* Copyright (c) 2002-2008 Atheros Communications, Inc.5*6* Permission to use, copy, modify, and/or distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/1819/*20* This is almost the same as ar5416_reset.c but uses the v4k EEPROM and21* supports only 2Ghz operation.22*/2324#include "opt_ah.h"2526#include "ah.h"27#include "ah_internal.h"28#include "ah_devid.h"2930#include "ah_eeprom_v14.h"31#include "ah_eeprom_v4k.h"3233#include "ar9002/ar9285.h"34#include "ar5416/ar5416.h"35#include "ar5416/ar5416reg.h"36#include "ar5416/ar5416phy.h"37#include "ar9002/ar9002phy.h"38#include "ar9002/ar9285phy.h"39#include "ar9002/ar9285an.h"40#include "ar9002/ar9285_diversity.h"4142/* Eeprom versioning macros. Returns true if the version is equal or newer than the ver specified */43#define EEP_MINOR(_ah) \44(AH_PRIVATE(_ah)->ah_eeversion & AR5416_EEP_VER_MINOR_MASK)45#define IS_EEP_MINOR_V2(_ah) (EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_2)46#define IS_EEP_MINOR_V3(_ah) (EEP_MINOR(_ah) >= AR5416_EEP_MINOR_VER_3)4748/* Additional Time delay to wait after activiting the Base band */49#define BASE_ACTIVATE_DELAY 100 /* 100 usec */50#define PLL_SETTLE_DELAY 300 /* 300 usec */51#define RTC_PLL_SETTLE_DELAY 1000 /* 1 ms */5253static HAL_BOOL ar9285SetPowerPerRateTable(struct ath_hal *ah,54struct ar5416eeprom_4k *pEepData,55const struct ieee80211_channel *chan, int16_t *ratesArray,56uint16_t cfgCtl, uint16_t AntennaReduction,57uint16_t twiceMaxRegulatoryPower,58uint16_t powerLimit);59static HAL_BOOL ar9285SetPowerCalTable(struct ath_hal *ah,60struct ar5416eeprom_4k *pEepData,61const struct ieee80211_channel *chan,62int16_t *pTxPowerIndexOffset);63static void ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,64const struct ieee80211_channel *chan, CAL_DATA_PER_FREQ_4K *pRawDataSet,65uint8_t * bChans, uint16_t availPiers,66uint16_t tPdGainOverlap, int16_t *pMinCalPower,67uint16_t * pPdGainBoundaries, uint8_t * pPDADCValues,68uint16_t numXpdGains);6970HAL_BOOL71ar9285SetTransmitPower(struct ath_hal *ah,72const struct ieee80211_channel *chan, uint16_t *rfXpdGain)73{74#define POW_SM(_r, _s) (((_r) & 0x3f) << (_s))75#define N(a) (sizeof (a) / sizeof (a[0]))7677MODAL_EEP4K_HEADER *pModal;78struct ath_hal_5212 *ahp = AH5212(ah);79int16_t txPowerIndexOffset = 0;80int i;8182uint16_t cfgCtl;83uint16_t powerLimit;84uint16_t twiceAntennaReduction;85uint16_t twiceMaxRegulatoryPower;86int16_t maxPower;87HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;88struct ar5416eeprom_4k *pEepData = &ee->ee_base;8990HALASSERT(AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER14_1);9192AH5416(ah)->ah_ht40PowerIncForPdadc = 2;9394/* Setup info for the actual eeprom */95OS_MEMZERO(AH5416(ah)->ah_ratesArray, sizeof(AH5416(ah)->ah_ratesArray));96cfgCtl = ath_hal_getctl(ah, chan);97powerLimit = chan->ic_maxregpower * 2;98twiceAntennaReduction = chan->ic_maxantgain;99twiceMaxRegulatoryPower = AH_MIN(MAX_RATE_POWER, AH_PRIVATE(ah)->ah_powerLimit);100pModal = &pEepData->modalHeader;101HALDEBUG(ah, HAL_DEBUG_RESET, "%s Channel=%u CfgCtl=%u\n",102__func__,chan->ic_freq, cfgCtl );103104if (IS_EEP_MINOR_V2(ah)) {105AH5416(ah)->ah_ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;106}107108if (!ar9285SetPowerPerRateTable(ah, pEepData, chan,109&AH5416(ah)->ah_ratesArray[0],cfgCtl,110twiceAntennaReduction,111twiceMaxRegulatoryPower, powerLimit)) {112HALDEBUG(ah, HAL_DEBUG_ANY,113"%s: unable to set tx power per rate table\n", __func__);114return AH_FALSE;115}116117if (!ar9285SetPowerCalTable(ah, pEepData, chan, &txPowerIndexOffset)) {118HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set power table\n",119__func__);120return AH_FALSE;121}122123maxPower = AH_MAX(AH5416(ah)->ah_ratesArray[rate6mb],124AH5416(ah)->ah_ratesArray[rateHt20_0]);125maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rate1l]);126127if (IEEE80211_IS_CHAN_HT40(chan)) {128maxPower = AH_MAX(maxPower, AH5416(ah)->ah_ratesArray[rateHt40_0]);129}130131ahp->ah_tx6PowerInHalfDbm = maxPower;132AH_PRIVATE(ah)->ah_maxPowerLevel = maxPower;133ahp->ah_txPowerIndexOffset = txPowerIndexOffset;134135/*136* txPowerIndexOffset is set by the SetPowerTable() call -137* adjust the rate table (0 offset if rates EEPROM not loaded)138*/139for (i = 0; i < N(AH5416(ah)->ah_ratesArray); i++) {140AH5416(ah)->ah_ratesArray[i] = (int16_t)(txPowerIndexOffset + AH5416(ah)->ah_ratesArray[i]);141/* -5 dBm offset for Merlin and later; this includes Kite */142AH5416(ah)->ah_ratesArray[i] -= AR5416_PWR_TABLE_OFFSET_DB * 2;143if (AH5416(ah)->ah_ratesArray[i] > AR5416_MAX_RATE_POWER)144AH5416(ah)->ah_ratesArray[i] = AR5416_MAX_RATE_POWER;145if (AH5416(ah)->ah_ratesArray[i] < 0)146AH5416(ah)->ah_ratesArray[i] = 0;147}148149#ifdef AH_EEPROM_DUMP150ar5416PrintPowerPerRate(ah, AH5416(ah)->ah_ratesArray);151#endif152153/*154* Adjust the HT40 power to meet the correct target TX power155* for 40MHz mode, based on TX power curves that are established156* for 20MHz mode.157*158* XXX handle overflow/too high power level?159*/160if (IEEE80211_IS_CHAN_HT40(chan)) {161AH5416(ah)->ah_ratesArray[rateHt40_0] +=162AH5416(ah)->ah_ht40PowerIncForPdadc;163AH5416(ah)->ah_ratesArray[rateHt40_1] +=164AH5416(ah)->ah_ht40PowerIncForPdadc;165AH5416(ah)->ah_ratesArray[rateHt40_2] +=166AH5416(ah)->ah_ht40PowerIncForPdadc;167AH5416(ah)->ah_ratesArray[rateHt40_3] +=168AH5416(ah)->ah_ht40PowerIncForPdadc;169AH5416(ah)->ah_ratesArray[rateHt40_4] +=170AH5416(ah)->ah_ht40PowerIncForPdadc;171AH5416(ah)->ah_ratesArray[rateHt40_5] +=172AH5416(ah)->ah_ht40PowerIncForPdadc;173AH5416(ah)->ah_ratesArray[rateHt40_6] +=174AH5416(ah)->ah_ht40PowerIncForPdadc;175AH5416(ah)->ah_ratesArray[rateHt40_7] +=176AH5416(ah)->ah_ht40PowerIncForPdadc;177}178179/* Write the TX power rate registers */180ar5416WriteTxPowerRateRegisters(ah, chan, AH5416(ah)->ah_ratesArray);181182return AH_TRUE;183#undef POW_SM184#undef N185}186187static void188ar9285SetBoardGain(struct ath_hal *ah, const MODAL_EEP4K_HEADER *pModal,189const struct ar5416eeprom_4k *eep, uint8_t txRxAttenLocal)190{191OS_REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0,192pModal->antCtrlChain[0]);193194OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4_CHAIN(0),195(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4_CHAIN(0)) &196~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |197AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |198SM(pModal->iqCalICh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |199SM(pModal->iqCalQCh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));200201if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=202AR5416_EEP_MINOR_VER_3) {203txRxAttenLocal = pModal->txRxAttenCh[0];204205OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,206AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN, pModal->bswMargin[0]);207OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,208AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);209OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,210AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN, pModal->xatten2Margin[0]);211OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,212AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);213214/* Set the block 1 value to block 0 value */215OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,216AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,217pModal->bswMargin[0]);218OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,219AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);220OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,221AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,222pModal->xatten2Margin[0]);223OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,224AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);225}226227OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,228AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);229OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,230AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);231232OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,233AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);234OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,235AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);236}237238/*239* Read EEPROM header info and program the device for correct operation240* given the channel value.241*/242HAL_BOOL243ar9285SetBoardValues(struct ath_hal *ah, const struct ieee80211_channel *chan)244{245const HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;246const struct ar5416eeprom_4k *eep = &ee->ee_base;247const MODAL_EEP4K_HEADER *pModal;248uint8_t txRxAttenLocal;249uint8_t ob[5], db1[5], db2[5];250251pModal = &eep->modalHeader;252txRxAttenLocal = 23;253254OS_REG_WRITE(ah, AR_PHY_SWITCH_COM, pModal->antCtrlCommon);255256/* Single chain for 4K EEPROM*/257ar9285SetBoardGain(ah, pModal, eep, txRxAttenLocal);258259/* Initialize Ant Diversity settings if supported */260(void) ar9285SetAntennaSwitch(ah, AH5212(ah)->ah_antControl);261262/* Configure TX power calibration */263if (pModal->version >= 2) {264ob[0] = pModal->ob_0;265ob[1] = pModal->ob_1;266ob[2] = pModal->ob_2;267ob[3] = pModal->ob_3;268ob[4] = pModal->ob_4;269270db1[0] = pModal->db1_0;271db1[1] = pModal->db1_1;272db1[2] = pModal->db1_2;273db1[3] = pModal->db1_3;274db1[4] = pModal->db1_4;275276db2[0] = pModal->db2_0;277db2[1] = pModal->db2_1;278db2[2] = pModal->db2_2;279db2[3] = pModal->db2_3;280db2[4] = pModal->db2_4;281} else if (pModal->version == 1) {282ob[0] = pModal->ob_0;283ob[1] = ob[2] = ob[3] = ob[4] = pModal->ob_1;284db1[0] = pModal->db1_0;285db1[1] = db1[2] = db1[3] = db1[4] = pModal->db1_1;286db2[0] = pModal->db2_0;287db2[1] = db2[2] = db2[3] = db2[4] = pModal->db2_1;288} else {289int i;290291for (i = 0; i < 5; i++) {292ob[i] = pModal->ob_0;293db1[i] = pModal->db1_0;294db2[i] = pModal->db1_0;295}296}297298OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_0, ob[0]);299OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_1, ob[1]);300OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_2, ob[2]);301OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_3, ob[3]);302OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_OB_4, ob[4]);303304OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_0, db1[0]);305OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_1, db1[1]);306OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_DB1_2, db1[2]);307OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_3, db1[3]);308OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB1_4, db1[4]);309310OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_0, db2[0]);311OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_1, db2[1]);312OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_2, db2[2]);313OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_3, db2[3]);314OS_A_REG_RMW_FIELD(ah, AR9285_AN_RF2G4, AR9285_AN_RF2G4_DB2_4, db2[4]);315316OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,317pModal->switchSettling);318OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,319pModal->adcDesiredSize);320321OS_REG_WRITE(ah, AR_PHY_RF_CTL4,322SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF) |323SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF) |324SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON) |325SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));326327OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,328pModal->txEndToRxOn);329330OS_REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,331pModal->thresh62);332OS_REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0, AR_PHY_EXT_CCA0_THRESH62,333pModal->thresh62);334335if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=336AR5416_EEP_MINOR_VER_2) {337OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_DATA_START,338pModal->txFrameToDataStart);339OS_REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_FRAME_TO_PA_ON,340pModal->txFrameToPaOn);341}342343if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=344AR5416_EEP_MINOR_VER_3) {345if (IEEE80211_IS_CHAN_HT40(chan))346OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING,347AR_PHY_SETTLING_SWITCH, pModal->swSettleHt40);348}349350/*351* Program the CCK TX gain factor appropriately if needed.352* The AR9285/AR9271 has a non-constant PA tx gain behaviour353* for CCK versus OFDM rates; other chips deal with this354* differently.355*356* The mask/shift/multiply hackery is done so place the same357* value (bb_desired_scale) into multiple 5-bit fields.358* For example, AR_PHY_TX_PWRCTRL9 has bb_desired_scale written359* to three fields: (0..4), (5..9) and (10..14).360*/361if (AR_SREV_9271(ah) || AR_SREV_KITE(ah)) {362uint8_t bb_desired_scale = (pModal->bb_scale_smrt_antenna & EEP_4K_BB_DESIRED_SCALE_MASK);363if ((eep->baseEepHeader.txGainType == 0) && (bb_desired_scale != 0)) {364ath_hal_printf(ah, "[ath]: adjusting cck tx gain factor\n");365uint32_t pwrctrl, mask, clr;366367mask = (1<<0) | (1<<5) | (1<<10) | (1<<15) | (1<<20) | (1<<25);368pwrctrl = mask * bb_desired_scale;369clr = mask * 0x1f;370OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL8, pwrctrl, clr);371OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL10, pwrctrl, clr);372OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL12, pwrctrl, clr);373374mask = (1<<0) | (1<<5) | (1<<15);375pwrctrl = mask * bb_desired_scale;376clr = mask * 0x1f;377OS_REG_RMW(ah, AR_PHY_TX_PWRCTRL9, pwrctrl, clr);378379mask = (1<<0) | (1<<5);380pwrctrl = mask * bb_desired_scale;381clr = mask * 0x1f;382OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL11, pwrctrl, clr);383OS_REG_RMW(ah, AR_PHY_CH0_TX_PWRCTRL13, pwrctrl, clr);384}385}386387return AH_TRUE;388}389390/*391* Helper functions common for AP/CB/XB392*/393394static HAL_BOOL395ar9285SetPowerPerRateTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,396const struct ieee80211_channel *chan,397int16_t *ratesArray, uint16_t cfgCtl,398uint16_t AntennaReduction,399uint16_t twiceMaxRegulatoryPower,400uint16_t powerLimit)401{402#define N(a) (sizeof(a)/sizeof(a[0]))403/* Local defines to distinguish between extension and control CTL's */404#define EXT_ADDITIVE (0x8000)405#define CTL_11G_EXT (CTL_11G | EXT_ADDITIVE)406#define CTL_11B_EXT (CTL_11B | EXT_ADDITIVE)407408uint16_t twiceMaxEdgePower = AR5416_MAX_RATE_POWER;409int i;410int16_t twiceLargestAntenna;411CAL_CTL_DATA_4K *rep;412CAL_TARGET_POWER_LEG targetPowerOfdm, targetPowerCck = {0, {0, 0, 0, 0}};413CAL_TARGET_POWER_LEG targetPowerOfdmExt = {0, {0, 0, 0, 0}}, targetPowerCckExt = {0, {0, 0, 0, 0}};414CAL_TARGET_POWER_HT targetPowerHt20, targetPowerHt40 = {0, {0, 0, 0, 0}};415int16_t scaledPower, minCtlPower;416417#define SUB_NUM_CTL_MODES_AT_2G_40 3 /* excluding HT40, EXT-OFDM, EXT-CCK */418static const uint16_t ctlModesFor11g[] = {419CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT, CTL_2GHT40420};421const uint16_t *pCtlMode;422uint16_t numCtlModes, ctlMode, freq;423CHAN_CENTERS centers;424425ar5416GetChannelCenters(ah, chan, ¢ers);426427/* Compute TxPower reduction due to Antenna Gain */428429twiceLargestAntenna = pEepData->modalHeader.antennaGainCh[0];430twiceLargestAntenna = (int16_t)AH_MIN((AntennaReduction) - twiceLargestAntenna, 0);431432/* XXX setup for 5212 use (really used?) */433ath_hal_eepromSet(ah, AR_EEP_ANTGAINMAX_2, twiceLargestAntenna);434435/*436* scaledPower is the minimum of the user input power level and437* the regulatory allowed power level438*/439scaledPower = AH_MIN(powerLimit, twiceMaxRegulatoryPower + twiceLargestAntenna);440441/* Get target powers from EEPROM - our baseline for TX Power */442/* Setup for CTL modes */443numCtlModes = N(ctlModesFor11g) - SUB_NUM_CTL_MODES_AT_2G_40; /* CTL_11B, CTL_11G, CTL_2GHT20 */444pCtlMode = ctlModesFor11g;445446ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPowerCck,447AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCck, 4, AH_FALSE);448ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPower2G,449AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdm, 4, AH_FALSE);450ar5416GetTargetPowers(ah, chan, pEepData->calTargetPower2GHT20,451AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerHt20, 8, AH_FALSE);452453if (IEEE80211_IS_CHAN_HT40(chan)) {454numCtlModes = N(ctlModesFor11g); /* All 2G CTL's */455456ar5416GetTargetPowers(ah, chan, pEepData->calTargetPower2GHT40,457AR5416_4K_NUM_2G_40_TARGET_POWERS, &targetPowerHt40, 8, AH_TRUE);458/* Get target powers for extension channels */459ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPowerCck,460AR5416_4K_NUM_2G_CCK_TARGET_POWERS, &targetPowerCckExt, 4, AH_TRUE);461ar5416GetTargetPowersLeg(ah, chan, pEepData->calTargetPower2G,462AR5416_4K_NUM_2G_20_TARGET_POWERS, &targetPowerOfdmExt, 4, AH_TRUE);463}464465/*466* For MIMO, need to apply regulatory caps individually across dynamically467* running modes: CCK, OFDM, HT20, HT40468*469* The outer loop walks through each possible applicable runtime mode.470* The inner loop walks through each ctlIndex entry in EEPROM.471* The ctl value is encoded as [7:4] == test group, [3:0] == test mode.472*473*/474for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {475HAL_BOOL isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||476(pCtlMode[ctlMode] == CTL_2GHT40);477if (isHt40CtlMode) {478freq = centers.ctl_center;479} else if (pCtlMode[ctlMode] & EXT_ADDITIVE) {480freq = centers.ext_center;481} else {482freq = centers.ctl_center;483}484485/* walk through each CTL index stored in EEPROM */486for (i = 0; (i < AR5416_4K_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {487uint16_t twiceMinEdgePower;488489/* compare test group from regulatory channel list with test mode from pCtlMode list */490if ((((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) == pEepData->ctlIndex[i]) ||491(((cfgCtl & ~CTL_MODE_M) | (pCtlMode[ctlMode] & CTL_MODE_M)) ==492((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {493rep = &(pEepData->ctlData[i]);494twiceMinEdgePower = ar5416GetMaxEdgePower(freq,495rep->ctlEdges[496owl_get_ntxchains(AH5416(ah)->ah_tx_chainmask) - 1], AH_TRUE);497if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {498/* Find the minimum of all CTL edge powers that apply to this channel */499twiceMaxEdgePower = AH_MIN(twiceMaxEdgePower, twiceMinEdgePower);500} else {501/* specific */502twiceMaxEdgePower = twiceMinEdgePower;503break;504}505}506}507minCtlPower = (uint8_t)AH_MIN(twiceMaxEdgePower, scaledPower);508/* Apply ctl mode to correct target power set */509switch(pCtlMode[ctlMode]) {510case CTL_11B:511for (i = 0; i < N(targetPowerCck.tPow2x); i++) {512targetPowerCck.tPow2x[i] = (uint8_t)AH_MIN(targetPowerCck.tPow2x[i], minCtlPower);513}514break;515case CTL_11A:516case CTL_11G:517for (i = 0; i < N(targetPowerOfdm.tPow2x); i++) {518targetPowerOfdm.tPow2x[i] = (uint8_t)AH_MIN(targetPowerOfdm.tPow2x[i], minCtlPower);519}520break;521case CTL_5GHT20:522case CTL_2GHT20:523for (i = 0; i < N(targetPowerHt20.tPow2x); i++) {524targetPowerHt20.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt20.tPow2x[i], minCtlPower);525}526break;527case CTL_11B_EXT:528targetPowerCckExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerCckExt.tPow2x[0], minCtlPower);529break;530case CTL_11G_EXT:531targetPowerOfdmExt.tPow2x[0] = (uint8_t)AH_MIN(targetPowerOfdmExt.tPow2x[0], minCtlPower);532break;533case CTL_5GHT40:534case CTL_2GHT40:535for (i = 0; i < N(targetPowerHt40.tPow2x); i++) {536targetPowerHt40.tPow2x[i] = (uint8_t)AH_MIN(targetPowerHt40.tPow2x[i], minCtlPower);537}538break;539default:540return AH_FALSE;541break;542}543} /* end ctl mode checking */544545/* Set rates Array from collected data */546ar5416SetRatesArrayFromTargetPower(ah, chan, ratesArray,547&targetPowerCck,548&targetPowerCckExt,549&targetPowerOfdm,550&targetPowerOfdmExt,551&targetPowerHt20,552&targetPowerHt40);553554return AH_TRUE;555#undef EXT_ADDITIVE556#undef CTL_11G_EXT557#undef CTL_11B_EXT558#undef SUB_NUM_CTL_MODES_AT_2G_40559#undef N560}561562static HAL_BOOL563ar9285SetPowerCalTable(struct ath_hal *ah, struct ar5416eeprom_4k *pEepData,564const struct ieee80211_channel *chan, int16_t *pTxPowerIndexOffset)565{566CAL_DATA_PER_FREQ_4K *pRawDataset;567uint8_t *pCalBChans = AH_NULL;568uint16_t pdGainOverlap_t2;569static uint8_t pdadcValues[AR5416_NUM_PDADC_VALUES];570uint16_t gainBoundaries[AR5416_PD_GAINS_IN_MASK];571uint16_t numPiers, i;572int16_t tMinCalPower;573uint16_t numXpdGain, xpdMask;574uint16_t xpdGainValues[4]; /* v4k eeprom has 2; the other two stay 0 */575uint32_t regChainOffset;576577OS_MEMZERO(xpdGainValues, sizeof(xpdGainValues));578579xpdMask = pEepData->modalHeader.xpdGain;580581if (IS_EEP_MINOR_V2(ah)) {582pdGainOverlap_t2 = pEepData->modalHeader.pdGainOverlap;583} else {584pdGainOverlap_t2 = (uint16_t)(MS(OS_REG_READ(ah, AR_PHY_TPCRG5), AR_PHY_TPCRG5_PD_GAIN_OVERLAP));585}586587pCalBChans = pEepData->calFreqPier2G;588numPiers = AR5416_4K_NUM_2G_CAL_PIERS;589numXpdGain = 0;590591/* Calculate the value of xpdgains from the xpdGain Mask */592for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {593if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {594if (numXpdGain >= AR5416_4K_NUM_PD_GAINS) {595HALASSERT(0);596break;597}598xpdGainValues[numXpdGain] = (uint16_t)(AR5416_PD_GAINS_IN_MASK - i);599numXpdGain++;600}601}602603/* Write the detector gain biases and their number */604ar5416WriteDetectorGainBiases(ah, numXpdGain, xpdGainValues);605606for (i = 0; i < AR5416_MAX_CHAINS; i++) {607regChainOffset = ar5416GetRegChainOffset(ah, i);608if (pEepData->baseEepHeader.txMask & (1 << i)) {609pRawDataset = pEepData->calPierData2G[i];610611ar9285GetGainBoundariesAndPdadcs(ah, chan, pRawDataset,612pCalBChans, numPiers,613pdGainOverlap_t2,614&tMinCalPower, gainBoundaries,615pdadcValues, numXpdGain);616617if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {618/*619* Note the pdadc table may not start at 0 dBm power, could be620* negative or greater than 0. Need to offset the power621* values by the amount of minPower for griffin622*/623ar5416SetGainBoundariesClosedLoop(ah, i, pdGainOverlap_t2, gainBoundaries);624}625626/* Write the power values into the baseband power table */627ar5416WritePdadcValues(ah, i, pdadcValues);628}629}630*pTxPowerIndexOffset = 0;631632return AH_TRUE;633}634635static void636ar9285GetGainBoundariesAndPdadcs(struct ath_hal *ah,637const struct ieee80211_channel *chan,638CAL_DATA_PER_FREQ_4K *pRawDataSet,639uint8_t * bChans, uint16_t availPiers,640uint16_t tPdGainOverlap, int16_t *pMinCalPower, uint16_t * pPdGainBoundaries,641uint8_t * pPDADCValues, uint16_t numXpdGains)642{643644int i, j, k;645int16_t ss; /* potentially -ve index for taking care of pdGainOverlap */646uint16_t idxL, idxR, numPiers; /* Pier indexes */647648/* filled out Vpd table for all pdGains (chanL) */649static uint8_t vpdTableL[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];650651/* filled out Vpd table for all pdGains (chanR) */652static uint8_t vpdTableR[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];653654/* filled out Vpd table for all pdGains (interpolated) */655static uint8_t vpdTableI[AR5416_4K_NUM_PD_GAINS][AR5416_MAX_PWR_RANGE_IN_HALF_DB];656657uint8_t *pVpdL, *pVpdR, *pPwrL, *pPwrR;658uint8_t minPwrT4[AR5416_4K_NUM_PD_GAINS];659uint8_t maxPwrT4[AR5416_4K_NUM_PD_GAINS];660int16_t vpdStep;661int16_t tmpVal;662uint16_t sizeCurrVpdTable, maxIndex, tgtIndex;663HAL_BOOL match;664int16_t minDelta = 0;665CHAN_CENTERS centers;666667ar5416GetChannelCenters(ah, chan, ¢ers);668669/* Trim numPiers for the number of populated channel Piers */670for (numPiers = 0; numPiers < availPiers; numPiers++) {671if (bChans[numPiers] == AR5416_BCHAN_UNUSED) {672break;673}674}675676/* Find pier indexes around the current channel */677match = ath_ee_getLowerUpperIndex((uint8_t)FREQ2FBIN(centers.synth_center,678IEEE80211_IS_CHAN_2GHZ(chan)), bChans, numPiers, &idxL, &idxR);679680if (match) {681/* Directly fill both vpd tables from the matching index */682for (i = 0; i < numXpdGains; i++) {683minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];684maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];685ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i],686pRawDataSet[idxL].pwrPdg[i],687pRawDataSet[idxL].vpdPdg[i],688AR5416_PD_GAIN_ICEPTS, vpdTableI[i]);689}690} else {691for (i = 0; i < numXpdGains; i++) {692pVpdL = pRawDataSet[idxL].vpdPdg[i];693pPwrL = pRawDataSet[idxL].pwrPdg[i];694pVpdR = pRawDataSet[idxR].vpdPdg[i];695pPwrR = pRawDataSet[idxR].pwrPdg[i];696697/* Start Vpd interpolation from the max of the minimum powers */698minPwrT4[i] = AH_MAX(pPwrL[0], pPwrR[0]);699700/* End Vpd interpolation from the min of the max powers */701maxPwrT4[i] = AH_MIN(pPwrL[AR5416_PD_GAIN_ICEPTS - 1], pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);702HALASSERT(maxPwrT4[i] > minPwrT4[i]);703704/* Fill pier Vpds */705ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrL, pVpdL,706AR5416_PD_GAIN_ICEPTS, vpdTableL[i]);707ath_ee_FillVpdTable(minPwrT4[i], maxPwrT4[i], pPwrR, pVpdR,708AR5416_PD_GAIN_ICEPTS, vpdTableR[i]);709710/* Interpolate the final vpd */711for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {712vpdTableI[i][j] = (uint8_t)(ath_ee_interpolate((uint16_t)FREQ2FBIN(centers.synth_center,713IEEE80211_IS_CHAN_2GHZ(chan)),714bChans[idxL], bChans[idxR], vpdTableL[i][j], vpdTableR[i][j]));715}716}717}718*pMinCalPower = (int16_t)(minPwrT4[0] / 2);719720k = 0; /* index for the final table */721for (i = 0; i < numXpdGains; i++) {722if (i == (numXpdGains - 1)) {723pPdGainBoundaries[i] = (uint16_t)(maxPwrT4[i] / 2);724} else {725pPdGainBoundaries[i] = (uint16_t)((maxPwrT4[i] + minPwrT4[i+1]) / 4);726}727728pPdGainBoundaries[i] = (uint16_t)AH_MIN(AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);729730/* NB: only applies to owl 1.0 */731if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah) ) {732/*733* fix the gain delta, but get a delta that can be applied to min to734* keep the upper power values accurate, don't think max needs to735* be adjusted because should not be at that area of the table?736*/737minDelta = pPdGainBoundaries[0] - 23;738pPdGainBoundaries[0] = 23;739}740else {741minDelta = 0;742}743744/* Find starting index for this pdGain */745if (i == 0) {746if (AR_SREV_MERLIN_20_OR_LATER(ah))747ss = (int16_t)(0 - (minPwrT4[i] / 2));748else749ss = 0; /* for the first pdGain, start from index 0 */750} else {751/* need overlap entries extrapolated below. */752ss = (int16_t)((pPdGainBoundaries[i-1] - (minPwrT4[i] / 2)) - tPdGainOverlap + 1 + minDelta);753}754vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);755vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);756/*757*-ve ss indicates need to extrapolate data below for this pdGain758*/759while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {760tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);761pPDADCValues[k++] = (uint8_t)((tmpVal < 0) ? 0 : tmpVal);762ss++;763}764765sizeCurrVpdTable = (uint8_t)((maxPwrT4[i] - minPwrT4[i]) / 2 +1);766tgtIndex = (uint8_t)(pPdGainBoundaries[i] + tPdGainOverlap - (minPwrT4[i] / 2));767maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;768769while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {770pPDADCValues[k++] = vpdTableI[i][ss++];771}772773vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] - vpdTableI[i][sizeCurrVpdTable - 2]);774vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);775/*776* for last gain, pdGainBoundary == Pmax_t2, so will777* have to extrapolate778*/779if (tgtIndex >= maxIndex) { /* need to extrapolate above */780while ((ss <= tgtIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {781tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +782(ss - maxIndex +1) * vpdStep));783pPDADCValues[k++] = (uint8_t)((tmpVal > 255) ? 255 : tmpVal);784ss++;785}786} /* extrapolated above */787} /* for all pdGainUsed */788789/* Fill out pdGainBoundaries - only up to 2 allowed here, but hardware allows up to 4 */790while (i < AR5416_PD_GAINS_IN_MASK) {791pPdGainBoundaries[i] = AR5416_4K_EEP_PD_GAIN_BOUNDARY_DEFAULT;792i++;793}794795while (k < AR5416_NUM_PDADC_VALUES) {796pPDADCValues[k] = pPDADCValues[k-1];797k++;798}799return;800}801802803