Path: blob/main/sys/dev/ath/ath_hal/ar5416/ar2133.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*/18#include "opt_ah.h"1920#include "ah.h"21#include "ah_internal.h"2223#include "ah_eeprom_v14.h"2425#include "ar5416/ar5416.h"26#include "ar5416/ar5416reg.h"27#include "ar5416/ar5416phy.h"2829#define N(a) (sizeof(a)/sizeof(a[0]))3031struct ar2133State {32RF_HAL_FUNCS base; /* public state, must be first */33uint16_t pcdacTable[1];3435uint32_t *Bank0Data;36uint32_t *Bank1Data;37uint32_t *Bank2Data;38uint32_t *Bank3Data;39uint32_t *Bank6Data;40uint32_t *Bank7Data;4142/* NB: Bank*Data storage follows */43};44#define AR2133(ah) ((struct ar2133State *) AH5212(ah)->ah_rfHal)4546#define ar5416ModifyRfBuffer ar5212ModifyRfBuffer /*XXX*/4748void ar5416ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,49uint32_t numBits, uint32_t firstBit, uint32_t column);5051static void52ar2133WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,53int writes)54{55(void) ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_bb_rfgain,56freqIndex, writes);57}5859/*60* Fix on 2.4 GHz band for orientation sensitivity issue by increasing61* rf_pwd_icsyndiv.62*63* Theoretical Rules:64* if 2 GHz band65* if forceBiasAuto66* if synth_freq < 241267* bias = 068* else if 2412 <= synth_freq <= 242269* bias = 170* else // synth_freq > 242271* bias = 272* else if forceBias > 073* bias = forceBias & 774* else75* no change, use value from ini file76* else77* no change, invalid band78*79* 1st Mod:80* 2422 also uses value of 281* <approved>82*83* 2nd Mod:84* Less than 2412 uses value of 0, 2412 and above uses value of 285*/86static void87ar2133ForceBias(struct ath_hal *ah, uint16_t synth_freq)88{89uint32_t tmp_reg;90int reg_writes = 0;91uint32_t new_bias = 0;92struct ar2133State *priv = AR2133(ah);9394/* XXX this is a bit of a silly check for 2.4ghz channels -adrian */95if (synth_freq >= 3000)96return;9798if (synth_freq < 2412)99new_bias = 0;100else if (synth_freq < 2422)101new_bias = 1;102else103new_bias = 2;104105/* pre-reverse this field */106tmp_reg = ath_hal_reverseBits(new_bias, 3);107108HALDEBUG(ah, HAL_DEBUG_ANY, "%s: Force rf_pwd_icsyndiv to %1d on %4d\n",109__func__, new_bias, synth_freq);110111/* swizzle rf_pwd_icsyndiv */112ar5416ModifyRfBuffer(priv->Bank6Data, tmp_reg, 3, 181, 3);113114/* write Bank 6 with new params */115ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank6, priv->Bank6Data, reg_writes);116}117118/*119* Take the MHz channel value and set the Channel value120*121* ASSUMES: Writes enabled to analog bus122*/123static HAL_BOOL124ar2133SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)125{126uint32_t channelSel = 0;127uint32_t bModeSynth = 0;128uint32_t aModeRefSel = 0;129uint32_t reg32 = 0;130uint16_t freq;131CHAN_CENTERS centers;132133OS_MARK(ah, AH_MARK_SETCHANNEL, chan->ic_freq);134135ar5416GetChannelCenters(ah, chan, ¢ers);136freq = centers.synth_center;137138if (freq < 4800) {139uint32_t txctl;140141if (((freq - 2192) % 5) == 0) {142channelSel = ((freq - 672) * 2 - 3040)/10;143bModeSynth = 0;144} else if (((freq - 2224) % 5) == 0) {145channelSel = ((freq - 704) * 2 - 3040) / 10;146bModeSynth = 1;147} else {148HALDEBUG(ah, HAL_DEBUG_ANY,149"%s: invalid channel %u MHz\n", __func__, freq);150return AH_FALSE;151}152153channelSel = (channelSel << 2) & 0xff;154channelSel = ath_hal_reverseBits(channelSel, 8);155156txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);157if (freq == 2484) {158/* Enable channel spreading for channel 14 */159OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,160txctl | AR_PHY_CCK_TX_CTRL_JAPAN);161} else {162OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,163txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);164}165/*166* Handle programming the RF synth for odd frequencies in the167* 4.9->5GHz range. This matches the programming from the168* later model 802.11abg RF synths.169*170* This interoperates on the quarter rate channels with the171* AR5112 and later RF synths. Please note that the synthesiser172* isn't able to completely accurately represent these frequencies173* (as the resolution in this reference is 2.5MHz) and thus it will174* be slightly "off centre." This matches the same slightly175* incorrect * centre frequency behaviour that the AR5112 and later176* channel selection code has.177*178* This is disabled because it hasn't been tested for regulatory179* compliance and neither have the NICs which would use it.180* So if you enable this code, you must first ensure that you've181* re-certified the NICs in question beforehand or you will be182* violating your local regulatory rules and breaking the law.183*/184#if 0185} else if (((freq % 5) == 2) && (freq <= 5435)) {186freq = freq - 2;187channelSel = ath_hal_reverseBits(188(uint32_t) (((freq - 4800) * 10) / 25 + 1), 8);189/* XXX what about for Howl/Sowl? */190aModeRefSel = ath_hal_reverseBits(0, 2);191#endif192} else if ((freq % 20) == 0 && freq >= 5120) {193channelSel = ath_hal_reverseBits(((freq - 4800) / 20 << 2), 8);194if (AR_SREV_HOWL(ah) || AR_SREV_SOWL_10_OR_LATER(ah))195aModeRefSel = ath_hal_reverseBits(3, 2);196else197aModeRefSel = ath_hal_reverseBits(1, 2);198} else if ((freq % 10) == 0) {199channelSel = ath_hal_reverseBits(((freq - 4800) / 10 << 1), 8);200if (AR_SREV_HOWL(ah) || AR_SREV_SOWL_10_OR_LATER(ah))201aModeRefSel = ath_hal_reverseBits(2, 2);202else203aModeRefSel = ath_hal_reverseBits(1, 2);204} else if ((freq % 5) == 0) {205channelSel = ath_hal_reverseBits((freq - 4800) / 5, 8);206aModeRefSel = ath_hal_reverseBits(1, 2);207} else {208HALDEBUG(ah, HAL_DEBUG_UNMASKABLE,209"%s: invalid channel %u MHz\n",210__func__, freq);211return AH_FALSE;212}213214/* Workaround for hw bug - AR5416 specific */215if (AR_SREV_OWL(ah) && ah->ah_config.ah_ar5416_biasadj)216ar2133ForceBias(ah, freq);217218reg32 = (channelSel << 8) | (aModeRefSel << 2) | (bModeSynth << 1) |219(1 << 5) | 0x1;220221OS_REG_WRITE(ah, AR_PHY(0x37), reg32);222223AH_PRIVATE(ah)->ah_curchan = chan;224return AH_TRUE;225226}227228/*229* Return a reference to the requested RF Bank.230*/231static uint32_t *232ar2133GetRfBank(struct ath_hal *ah, int bank)233{234struct ar2133State *priv = AR2133(ah);235236HALASSERT(priv != AH_NULL);237switch (bank) {238case 1: return priv->Bank1Data;239case 2: return priv->Bank2Data;240case 3: return priv->Bank3Data;241case 6: return priv->Bank6Data;242case 7: return priv->Bank7Data;243}244HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",245__func__, bank);246return AH_NULL;247}248249/*250* Reads EEPROM header info from device structure and programs251* all rf registers252*253* REQUIRES: Access to the analog rf device254*/255static HAL_BOOL256ar2133SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,257uint16_t modesIndex, uint16_t *rfXpdGain)258{259struct ar2133State *priv = AR2133(ah);260int writes;261262HALASSERT(priv);263264/* Setup Bank 0 Write */265ath_hal_ini_bank_setup(priv->Bank0Data, &AH5416(ah)->ah_ini_bank0, 1);266267/* Setup Bank 1 Write */268ath_hal_ini_bank_setup(priv->Bank1Data, &AH5416(ah)->ah_ini_bank1, 1);269270/* Setup Bank 2 Write */271ath_hal_ini_bank_setup(priv->Bank2Data, &AH5416(ah)->ah_ini_bank2, 1);272273/* Setup Bank 3 Write */274ath_hal_ini_bank_setup(priv->Bank3Data, &AH5416(ah)->ah_ini_bank3, modesIndex);275276/* Setup Bank 6 Write */277ath_hal_ini_bank_setup(priv->Bank6Data, &AH5416(ah)->ah_ini_bank6, modesIndex);278279/* Only the 5 or 2 GHz OB/DB need to be set for a mode */280if (IEEE80211_IS_CHAN_2GHZ(chan)) {281HALDEBUG(ah, HAL_DEBUG_EEPROM, "%s: 2ghz: OB_2:%d, DB_2:%d\n",282__func__,283ath_hal_eepromGet(ah, AR_EEP_OB_2, AH_NULL),284ath_hal_eepromGet(ah, AR_EEP_DB_2, AH_NULL));285ar5416ModifyRfBuffer(priv->Bank6Data,286ath_hal_eepromGet(ah, AR_EEP_OB_2, AH_NULL), 3, 197, 0);287ar5416ModifyRfBuffer(priv->Bank6Data,288ath_hal_eepromGet(ah, AR_EEP_DB_2, AH_NULL), 3, 194, 0);289} else {290HALDEBUG(ah, HAL_DEBUG_EEPROM, "%s: 5ghz: OB_5:%d, DB_5:%d\n",291__func__,292ath_hal_eepromGet(ah, AR_EEP_OB_5, AH_NULL),293ath_hal_eepromGet(ah, AR_EEP_DB_5, AH_NULL));294ar5416ModifyRfBuffer(priv->Bank6Data,295ath_hal_eepromGet(ah, AR_EEP_OB_5, AH_NULL), 3, 203, 0);296ar5416ModifyRfBuffer(priv->Bank6Data,297ath_hal_eepromGet(ah, AR_EEP_DB_5, AH_NULL), 3, 200, 0);298}299/* Setup Bank 7 Setup */300ath_hal_ini_bank_setup(priv->Bank7Data, &AH5416(ah)->ah_ini_bank7, 1);301302/* Write Analog registers */303writes = ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank0,304priv->Bank0Data, 0);305writes = ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank1,306priv->Bank1Data, writes);307writes = ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank2,308priv->Bank2Data, writes);309writes = ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank3,310priv->Bank3Data, writes);311writes = ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank6,312priv->Bank6Data, writes);313(void) ath_hal_ini_bank_write(ah, &AH5416(ah)->ah_ini_bank7,314priv->Bank7Data, writes);315316return AH_TRUE;317#undef RF_BANK_SETUP318}319320/*321* Read the transmit power levels from the structures taken from EEPROM322* Interpolate read transmit power values for this channel323* Organize the transmit power values into a table for writing into the hardware324*/325326static HAL_BOOL327ar2133SetPowerTable(struct ath_hal *ah, int16_t *pPowerMin, int16_t *pPowerMax,328const struct ieee80211_channel *chan, uint16_t *rfXpdGain)329{330return AH_TRUE;331}332333#if 0334static int16_t335ar2133GetMinPower(struct ath_hal *ah, EXPN_DATA_PER_CHANNEL_5112 *data)336{337int i, minIndex;338int16_t minGain,minPwr,minPcdac,retVal;339340/* Assume NUM_POINTS_XPD0 > 0 */341minGain = data->pDataPerXPD[0].xpd_gain;342for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {343if (data->pDataPerXPD[i].xpd_gain < minGain) {344minIndex = i;345minGain = data->pDataPerXPD[i].xpd_gain;346}347}348minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];349minPcdac = data->pDataPerXPD[minIndex].pcdac[0];350for (i=1; i<NUM_POINTS_XPD0; i++) {351if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {352minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];353minPcdac = data->pDataPerXPD[minIndex].pcdac[i];354}355}356retVal = minPwr - (minPcdac*2);357return(retVal);358}359#endif360361static HAL_BOOL362ar2133GetChannelMaxMinPower(struct ath_hal *ah,363const struct ieee80211_channel *chan,364int16_t *maxPow, int16_t *minPow)365{366#if 0367struct ath_hal_5212 *ahp = AH5212(ah);368int numChannels=0,i,last;369int totalD, totalF,totalMin;370EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;371EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;372373*maxPow = 0;374if (IS_CHAN_A(chan)) {375powerArray = ahp->ah_modePowerArray5112;376data = powerArray[headerInfo11A].pDataPerChannel;377numChannels = powerArray[headerInfo11A].numChannels;378} else if (IS_CHAN_G(chan) || IS_CHAN_108G(chan)) {379/* XXX - is this correct? Should we also use the same power for turbo G? */380powerArray = ahp->ah_modePowerArray5112;381data = powerArray[headerInfo11G].pDataPerChannel;382numChannels = powerArray[headerInfo11G].numChannels;383} else if (IS_CHAN_B(chan)) {384powerArray = ahp->ah_modePowerArray5112;385data = powerArray[headerInfo11B].pDataPerChannel;386numChannels = powerArray[headerInfo11B].numChannels;387} else {388return (AH_TRUE);389}390/* Make sure the channel is in the range of the TP values391* (freq piers)392*/393if ((numChannels < 1) ||394(chan->channel < data[0].channelValue) ||395(chan->channel > data[numChannels-1].channelValue))396return(AH_FALSE);397398/* Linearly interpolate the power value now */399for (last=0,i=0;400(i<numChannels) && (chan->channel > data[i].channelValue);401last=i++);402totalD = data[i].channelValue - data[last].channelValue;403if (totalD > 0) {404totalF = data[i].maxPower_t4 - data[last].maxPower_t4;405*maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);406407totalMin = ar2133GetMinPower(ah,&data[i]) - ar2133GetMinPower(ah, &data[last]);408*minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) + ar2133GetMinPower(ah, &data[last])*totalD)/totalD);409return (AH_TRUE);410} else {411if (chan->channel == data[i].channelValue) {412*maxPow = data[i].maxPower_t4;413*minPow = ar2133GetMinPower(ah, &data[i]);414return(AH_TRUE);415} else416return(AH_FALSE);417}418#else419// XXX TODO: actually go implement for 11n chips!420*maxPow = *minPow = 0;421return AH_FALSE;422#endif423}424425/*426* The ordering of nfarray is thus:427*428* nfarray[0]: Chain 0 ctl429* nfarray[1]: Chain 1 ctl430* nfarray[2]: Chain 2 ctl431* nfarray[3]: Chain 0 ext432* nfarray[4]: Chain 1 ext433* nfarray[5]: Chain 2 ext434*/435static void436ar2133GetNoiseFloor(struct ath_hal *ah, int16_t nfarray[])437{438struct ath_hal_5416 *ahp = AH5416(ah);439int16_t nf;440441/*442* Blank nf array - some chips may only443* have one or two RX chainmasks enabled.444*/445nfarray[0] = nfarray[1] = nfarray[2] = 0;446nfarray[3] = nfarray[4] = nfarray[5] = 0;447448switch (ahp->ah_rx_chainmask) {449case 0x7:450nf = MS(OS_REG_READ(ah, AR_PHY_CH2_CCA), AR_PHY_CH2_MINCCA_PWR);451if (nf & 0x100)452nf = 0 - ((nf ^ 0x1ff) + 1);453HALDEBUG(ah, HAL_DEBUG_NFCAL,454"NF calibrated [ctl] [chain 2] is %d\n", nf);455nfarray[2] = nf;456457nf = MS(OS_REG_READ(ah, AR_PHY_CH2_EXT_CCA), AR_PHY_CH2_EXT_MINCCA_PWR);458if (nf & 0x100)459nf = 0 - ((nf ^ 0x1ff) + 1);460HALDEBUG(ah, HAL_DEBUG_NFCAL,461"NF calibrated [ext] [chain 2] is %d\n", nf);462nfarray[5] = nf;463/* fall thru... */464case 0x3:465case 0x5:466nf = MS(OS_REG_READ(ah, AR_PHY_CH1_CCA), AR_PHY_CH1_MINCCA_PWR);467if (nf & 0x100)468nf = 0 - ((nf ^ 0x1ff) + 1);469HALDEBUG(ah, HAL_DEBUG_NFCAL,470"NF calibrated [ctl] [chain 1] is %d\n", nf);471nfarray[1] = nf;472473nf = MS(OS_REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR_PHY_CH1_EXT_MINCCA_PWR);474if (nf & 0x100)475nf = 0 - ((nf ^ 0x1ff) + 1);476HALDEBUG(ah, HAL_DEBUG_NFCAL,477"NF calibrated [ext] [chain 1] is %d\n", nf);478nfarray[4] = nf;479/* fall thru... */480case 0x1:481nf = MS(OS_REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);482if (nf & 0x100)483nf = 0 - ((nf ^ 0x1ff) + 1);484HALDEBUG(ah, HAL_DEBUG_NFCAL,485"NF calibrated [ctl] [chain 0] is %d\n", nf);486nfarray[0] = nf;487488nf = MS(OS_REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);489if (nf & 0x100)490nf = 0 - ((nf ^ 0x1ff) + 1);491HALDEBUG(ah, HAL_DEBUG_NFCAL,492"NF calibrated [ext] [chain 0] is %d\n", nf);493nfarray[3] = nf;494495break;496}497}498499/*500* Adjust NF based on statistical values for 5GHz frequencies.501* Stubbed:Not used by Fowl502*/503static int16_t504ar2133GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)505{506return 0;507}508509/*510* Free memory for analog bank scratch buffers511*/512static void513ar2133RfDetach(struct ath_hal *ah)514{515struct ath_hal_5212 *ahp = AH5212(ah);516517HALASSERT(ahp->ah_rfHal != AH_NULL);518ath_hal_free(ahp->ah_rfHal);519ahp->ah_rfHal = AH_NULL;520}521522/*523* Allocate memory for analog bank scratch buffers524* Scratch Buffer will be reinitialized every reset so no need to zero now525*/526HAL_BOOL527ar2133RfAttach(struct ath_hal *ah, HAL_STATUS *status)528{529struct ath_hal_5212 *ahp = AH5212(ah);530struct ar2133State *priv;531uint32_t *bankData;532533HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: attach AR2133 radio\n", __func__);534535HALASSERT(ahp->ah_rfHal == AH_NULL);536priv = ath_hal_malloc(sizeof(struct ar2133State)537+ AH5416(ah)->ah_ini_bank0.rows * sizeof(uint32_t)538+ AH5416(ah)->ah_ini_bank1.rows * sizeof(uint32_t)539+ AH5416(ah)->ah_ini_bank2.rows * sizeof(uint32_t)540+ AH5416(ah)->ah_ini_bank3.rows * sizeof(uint32_t)541+ AH5416(ah)->ah_ini_bank6.rows * sizeof(uint32_t)542+ AH5416(ah)->ah_ini_bank7.rows * sizeof(uint32_t)543);544if (priv == AH_NULL) {545HALDEBUG(ah, HAL_DEBUG_ANY,546"%s: cannot allocate private state\n", __func__);547*status = HAL_ENOMEM; /* XXX */548return AH_FALSE;549}550priv->base.rfDetach = ar2133RfDetach;551priv->base.writeRegs = ar2133WriteRegs;552priv->base.getRfBank = ar2133GetRfBank;553priv->base.setChannel = ar2133SetChannel;554priv->base.setRfRegs = ar2133SetRfRegs;555priv->base.setPowerTable = ar2133SetPowerTable;556priv->base.getChannelMaxMinPower = ar2133GetChannelMaxMinPower;557priv->base.getNfAdjust = ar2133GetNfAdjust;558559bankData = (uint32_t *) &priv[1];560priv->Bank0Data = bankData, bankData += AH5416(ah)->ah_ini_bank0.rows;561priv->Bank1Data = bankData, bankData += AH5416(ah)->ah_ini_bank1.rows;562priv->Bank2Data = bankData, bankData += AH5416(ah)->ah_ini_bank2.rows;563priv->Bank3Data = bankData, bankData += AH5416(ah)->ah_ini_bank3.rows;564priv->Bank6Data = bankData, bankData += AH5416(ah)->ah_ini_bank6.rows;565priv->Bank7Data = bankData, bankData += AH5416(ah)->ah_ini_bank7.rows;566567ahp->ah_pcdacTable = priv->pcdacTable;568ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);569ahp->ah_rfHal = &priv->base;570/*571* Set noise floor adjust method; we arrange a572* direct call instead of thunking.573*/574AH_PRIVATE(ah)->ah_getNfAdjust = priv->base.getNfAdjust;575AH_PRIVATE(ah)->ah_getNoiseFloor = ar2133GetNoiseFloor;576577return AH_TRUE;578}579580static HAL_BOOL581ar2133Probe(struct ath_hal *ah)582{583return (AR_SREV_OWL(ah) || AR_SREV_HOWL(ah) || AR_SREV_SOWL(ah));584}585586AH_RF(RF2133, ar2133Probe, ar2133RfAttach);587588589