Path: blob/main/sys/dev/ath/ath_hal/ar5210/ar5210_reset.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2002-2009 Sam Leffler, Errno Consulting4* Copyright (c) 2002-2004 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 "ar5210/ar5210.h"24#include "ar5210/ar5210reg.h"25#include "ar5210/ar5210phy.h"2627#include "ah_eeprom_v1.h"2829typedef struct {30uint32_t Offset;31uint32_t Value;32} REGISTER_VAL;3334static const REGISTER_VAL ar5k0007_init[] = {35#include "ar5210/ar5k_0007.ini"36};3738/* Default Power Settings for channels outside of EEPROM range */39static const uint8_t ar5k0007_pwrSettings[17] = {40/* gain delta pc dac */41/* 54 48 36 24 18 12 9 54 48 36 24 18 12 9 6 ob db */429, 9, 0, 0, 0, 0, 0, 2, 2, 6, 6, 6, 6, 6, 6, 2, 243};4445/*46* The delay, in usecs, between writing AR_RC with a reset47* request and waiting for the chip to settle. If this is48* too short then the chip does not come out of sleep state.49* Note this value was empirically derived and may be dependent50* on the host machine (don't know--the problem was identified51* on an IBM 570e laptop; 10us delays worked on other systems).52*/53#define AR_RC_SETTLE_TIME 200005455static HAL_BOOL ar5210SetResetReg(struct ath_hal *,56uint32_t resetMask, u_int delay);57static HAL_BOOL ar5210SetChannel(struct ath_hal *, struct ieee80211_channel *);58static void ar5210SetOperatingMode(struct ath_hal *, int opmode);5960/*61* Places the device in and out of reset and then places sane62* values in the registers based on EEPROM config, initialization63* vectors (as determined by the mode), and station configuration64*65* bChannelChange is used to preserve DMA/PCU registers across66* a HW Reset during channel change.67*/68HAL_BOOL69ar5210Reset(struct ath_hal *ah, HAL_OPMODE opmode,70struct ieee80211_channel *chan, HAL_BOOL bChannelChange,71HAL_RESET_TYPE resetType,72HAL_STATUS *status)73{74#define N(a) (sizeof (a) /sizeof (a[0]))75#define FAIL(_code) do { ecode = _code; goto bad; } while (0)76struct ath_hal_5210 *ahp = AH5210(ah);77const HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;78HAL_CHANNEL_INTERNAL *ichan;79HAL_STATUS ecode;80uint32_t ledstate;81int i, q;8283HALDEBUG(ah, HAL_DEBUG_RESET,84"%s: opmode %u channel %u/0x%x %s channel\n", __func__,85opmode, chan->ic_freq, chan->ic_flags,86bChannelChange ? "change" : "same");8788if (!IEEE80211_IS_CHAN_5GHZ(chan)) {89/* Only 11a mode */90HALDEBUG(ah, HAL_DEBUG_ANY, "%s: channel not 5GHz\n", __func__);91FAIL(HAL_EINVAL);92}93/*94* Map public channel to private.95*/96ichan = ath_hal_checkchannel(ah, chan);97if (ichan == AH_NULL) {98HALDEBUG(ah, HAL_DEBUG_ANY,99"%s: invalid channel %u/0x%x; no mapping\n",100__func__, chan->ic_freq, chan->ic_flags);101FAIL(HAL_EINVAL);102}103switch (opmode) {104case HAL_M_STA:105case HAL_M_IBSS:106case HAL_M_HOSTAP:107case HAL_M_MONITOR:108break;109default:110HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid operating mode %u\n",111__func__, opmode);112FAIL(HAL_EINVAL);113break;114}115116ledstate = OS_REG_READ(ah, AR_PCICFG) &117(AR_PCICFG_LED_PEND | AR_PCICFG_LED_ACT);118119if (!ar5210ChipReset(ah, chan)) {120HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n",121__func__);122FAIL(HAL_EIO);123}124125OS_REG_WRITE(ah, AR_STA_ID0, LE_READ_4(ahp->ah_macaddr));126OS_REG_WRITE(ah, AR_STA_ID1, LE_READ_2(ahp->ah_macaddr + 4));127ar5210SetOperatingMode(ah, opmode);128129switch (opmode) {130case HAL_M_HOSTAP:131OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);132OS_REG_WRITE(ah, AR_PCICFG,133AR_PCICFG_LED_ACT | AR_PCICFG_LED_BCTL);134break;135case HAL_M_IBSS:136OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG | AR_BCR_BCMD);137OS_REG_WRITE(ah, AR_PCICFG,138AR_PCICFG_CLKRUNEN | AR_PCICFG_LED_PEND | AR_PCICFG_LED_BCTL);139break;140case HAL_M_STA:141OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);142OS_REG_WRITE(ah, AR_PCICFG,143AR_PCICFG_CLKRUNEN | AR_PCICFG_LED_PEND | AR_PCICFG_LED_BCTL);144break;145case HAL_M_MONITOR:146OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);147OS_REG_WRITE(ah, AR_PCICFG,148AR_PCICFG_LED_ACT | AR_PCICFG_LED_BCTL);149break;150}151152/* Restore previous led state */153OS_REG_WRITE(ah, AR_PCICFG, OS_REG_READ(ah, AR_PCICFG) | ledstate);154155#if 0156OS_REG_WRITE(ah, AR_BSS_ID0, LE_READ_4(ahp->ah_bssid));157OS_REG_WRITE(ah, AR_BSS_ID1, LE_READ_2(ahp->ah_bssid + 4));158#endif159/* BSSID, association id, ps-poll */160ar5210WriteAssocid(ah, ahp->ah_bssid, ahp->ah_associd);161162OS_REG_WRITE(ah, AR_TXDP0, 0);163OS_REG_WRITE(ah, AR_TXDP1, 0);164OS_REG_WRITE(ah, AR_RXDP, 0);165166/*167* Initialize interrupt state.168*/169(void) OS_REG_READ(ah, AR_ISR); /* cleared on read */170OS_REG_WRITE(ah, AR_IMR, 0);171OS_REG_WRITE(ah, AR_IER, AR_IER_DISABLE);172ahp->ah_maskReg = 0;173174(void) OS_REG_READ(ah, AR_BSR); /* cleared on read */175OS_REG_WRITE(ah, AR_TXCFG, AR_DMASIZE_128B);176OS_REG_WRITE(ah, AR_RXCFG, AR_DMASIZE_128B);177178OS_REG_WRITE(ah, AR_TOPS, 8); /* timeout prescale */179OS_REG_WRITE(ah, AR_RXNOFRM, 8); /* RX no frame timeout */180OS_REG_WRITE(ah, AR_RPGTO, 0); /* RX frame gap timeout */181OS_REG_WRITE(ah, AR_TXNOFRM, 0); /* TX no frame timeout */182183OS_REG_WRITE(ah, AR_SFR, 0);184OS_REG_WRITE(ah, AR_MIBC, 0); /* unfreeze ctrs + clr state */185OS_REG_WRITE(ah, AR_RSSI_THR, ahp->ah_rssiThr);186OS_REG_WRITE(ah, AR_CFP_DUR, 0);187188ar5210SetRxFilter(ah, 0); /* nothing for now */189OS_REG_WRITE(ah, AR_MCAST_FIL0, 0); /* multicast filter */190OS_REG_WRITE(ah, AR_MCAST_FIL1, 0); /* XXX was 2 */191192OS_REG_WRITE(ah, AR_TX_MASK0, 0);193OS_REG_WRITE(ah, AR_TX_MASK1, 0);194OS_REG_WRITE(ah, AR_CLR_TMASK, 1);195OS_REG_WRITE(ah, AR_TRIG_LEV, 1); /* minimum */196197ar5210UpdateDiagReg(ah, 0);198199OS_REG_WRITE(ah, AR_CFP_PERIOD, 0);200OS_REG_WRITE(ah, AR_TIMER0, 0); /* next beacon time */201OS_REG_WRITE(ah, AR_TSF_L32, 0); /* local clock */202OS_REG_WRITE(ah, AR_TIMER1, ~0); /* next DMA beacon alert */203OS_REG_WRITE(ah, AR_TIMER2, ~0); /* next SW beacon alert */204OS_REG_WRITE(ah, AR_TIMER3, 1); /* next ATIM window */205206/* Write the INI values for PHYreg initialization */207for (i = 0; i < N(ar5k0007_init); i++) {208uint32_t reg = ar5k0007_init[i].Offset;209/* On channel change, don't reset the PCU registers */210if (!(bChannelChange && (0x8000 <= reg && reg < 0x9000)))211OS_REG_WRITE(ah, reg, ar5k0007_init[i].Value);212}213214/* Setup the transmit power values for cards since 0x0[0-2]05 */215if (!ar5210SetTransmitPower(ah, chan)) {216HALDEBUG(ah, HAL_DEBUG_ANY,217"%s: error init'ing transmit power\n", __func__);218FAIL(HAL_EIO);219}220221OS_REG_WRITE(ah, AR_PHY(10),222(OS_REG_READ(ah, AR_PHY(10)) & 0xFFFF00FF) |223(ee->ee_xlnaOn << 8));224OS_REG_WRITE(ah, AR_PHY(13),225(ee->ee_xpaOff << 24) | (ee->ee_xpaOff << 16) |226(ee->ee_xpaOn << 8) | ee->ee_xpaOn);227OS_REG_WRITE(ah, AR_PHY(17),228(OS_REG_READ(ah, AR_PHY(17)) & 0xFFFFC07F) |229((ee->ee_antenna >> 1) & 0x3F80));230OS_REG_WRITE(ah, AR_PHY(18),231(OS_REG_READ(ah, AR_PHY(18)) & 0xFFFC0FFF) |232((ee->ee_antenna << 10) & 0x3F000));233OS_REG_WRITE(ah, AR_PHY(25),234(OS_REG_READ(ah, AR_PHY(25)) & 0xFFF80FFF) |235((ee->ee_thresh62 << 12) & 0x7F000));236OS_REG_WRITE(ah, AR_PHY(68),237(OS_REG_READ(ah, AR_PHY(68)) & 0xFFFFFFFC) |238(ee->ee_antenna & 0x3));239240if (!ar5210SetChannel(ah, chan)) {241HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set channel\n",242__func__);243FAIL(HAL_EIO);244}245if (bChannelChange && !IEEE80211_IS_CHAN_DFS(chan))246chan->ic_state &= ~IEEE80211_CHANSTATE_CWINT;247248/* Activate the PHY */249OS_REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ENABLE);250251OS_DELAY(1000); /* Wait a bit (1 msec) */252253/* calibrate the HW and poll the bit going to 0 for completion */254OS_REG_WRITE(ah, AR_PHY_AGCCTL,255OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_CAL);256(void) ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_CAL, 0);257258/* Perform noise floor calibration and set status */259if (!ar5210CalNoiseFloor(ah, ichan)) {260chan->ic_state |= IEEE80211_CHANSTATE_CWINT;261HALDEBUG(ah, HAL_DEBUG_ANY,262"%s: noise floor calibration failed\n", __func__);263FAIL(HAL_EIO);264}265266for (q = 0; q < HAL_NUM_TX_QUEUES; q++)267ar5210ResetTxQueue(ah, q);268269if (AH_PRIVATE(ah)->ah_rfkillEnabled)270ar5210EnableRfKill(ah);271272/*273* Writing to AR_BEACON will start timers. Hence it should be274* the last register to be written. Do not reset tsf, do not275* enable beacons at this point, but preserve other values276* like beaconInterval.277*/278OS_REG_WRITE(ah, AR_BEACON,279(OS_REG_READ(ah, AR_BEACON) &280~(AR_BEACON_EN | AR_BEACON_RESET_TSF)));281282/* Restore user-specified slot time and timeouts */283if (ahp->ah_sifstime != (u_int) -1)284ar5210SetSifsTime(ah, ahp->ah_sifstime);285if (ahp->ah_slottime != (u_int) -1)286ar5210SetSlotTime(ah, ahp->ah_slottime);287if (ahp->ah_acktimeout != (u_int) -1)288ar5210SetAckTimeout(ah, ahp->ah_acktimeout);289if (ahp->ah_ctstimeout != (u_int) -1)290ar5210SetCTSTimeout(ah, ahp->ah_ctstimeout);291if (AH_PRIVATE(ah)->ah_diagreg != 0)292ar5210UpdateDiagReg(ah, AH_PRIVATE(ah)->ah_diagreg);293294AH_PRIVATE(ah)->ah_opmode = opmode; /* record operating mode */295296HALDEBUG(ah, HAL_DEBUG_RESET, "%s: done\n", __func__);297298return AH_TRUE;299bad:300if (status != AH_NULL)301*status = ecode;302return AH_FALSE;303#undef FAIL304#undef N305}306307static void308ar5210SetOperatingMode(struct ath_hal *ah, int opmode)309{310struct ath_hal_5210 *ahp = AH5210(ah);311uint32_t val;312313val = OS_REG_READ(ah, AR_STA_ID1) & 0xffff;314switch (opmode) {315case HAL_M_HOSTAP:316OS_REG_WRITE(ah, AR_STA_ID1, val317| AR_STA_ID1_AP318| AR_STA_ID1_NO_PSPOLL319| AR_STA_ID1_DESC_ANTENNA320| ahp->ah_staId1Defaults);321break;322case HAL_M_IBSS:323OS_REG_WRITE(ah, AR_STA_ID1, val324| AR_STA_ID1_ADHOC325| AR_STA_ID1_NO_PSPOLL326| AR_STA_ID1_DESC_ANTENNA327| ahp->ah_staId1Defaults);328break;329case HAL_M_STA:330OS_REG_WRITE(ah, AR_STA_ID1, val331| AR_STA_ID1_NO_PSPOLL332| AR_STA_ID1_PWR_SV333| ahp->ah_staId1Defaults);334break;335case HAL_M_MONITOR:336OS_REG_WRITE(ah, AR_STA_ID1, val337| AR_STA_ID1_NO_PSPOLL338| ahp->ah_staId1Defaults);339break;340}341}342343void344ar5210SetPCUConfig(struct ath_hal *ah)345{346ar5210SetOperatingMode(ah, AH_PRIVATE(ah)->ah_opmode);347}348349/*350* Places the PHY and Radio chips into reset. A full reset351* must be called to leave this state. The PCI/MAC/PCU are352* not placed into reset as we must receive interrupt to353* re-enable the hardware.354*/355HAL_BOOL356ar5210PhyDisable(struct ath_hal *ah)357{358return ar5210SetResetReg(ah, AR_RC_RPHY, 10);359}360361/*362* Places all of hardware into reset363*/364HAL_BOOL365ar5210Disable(struct ath_hal *ah)366{367#define AR_RC_HW (AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC)368if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))369return AH_FALSE;370371/*372* Reset the HW - PCI must be reset after the rest of the373* device has been reset374*/375if (!ar5210SetResetReg(ah, AR_RC_HW, AR_RC_SETTLE_TIME))376return AH_FALSE;377OS_DELAY(1000);378(void) ar5210SetResetReg(ah, AR_RC_HW | AR_RC_RPCI, AR_RC_SETTLE_TIME);379OS_DELAY(2100); /* 8245 @ 96Mhz hangs with 2000us. */380381return AH_TRUE;382#undef AR_RC_HW383}384385/*386* Places the hardware into reset and then pulls it out of reset387*/388HAL_BOOL389ar5210ChipReset(struct ath_hal *ah, struct ieee80211_channel *chan)390{391#define AR_RC_HW (AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC)392393HALDEBUG(ah, HAL_DEBUG_RESET, "%s turbo %s\n", __func__,394chan && IEEE80211_IS_CHAN_TURBO(chan) ?395"enabled" : "disabled");396397if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))398return AH_FALSE;399400/* Place chip in turbo before reset to cleanly reset clocks */401OS_REG_WRITE(ah, AR_PHY_FRCTL,402chan && IEEE80211_IS_CHAN_TURBO(chan) ? AR_PHY_TURBO_MODE : 0);403404/*405* Reset the HW.406* PCI must be reset after the rest of the device has been reset.407*/408if (!ar5210SetResetReg(ah, AR_RC_HW, AR_RC_SETTLE_TIME))409return AH_FALSE;410OS_DELAY(1000);411if (!ar5210SetResetReg(ah, AR_RC_HW | AR_RC_RPCI, AR_RC_SETTLE_TIME))412return AH_FALSE;413OS_DELAY(2100); /* 8245 @ 96Mhz hangs with 2000us. */414415/*416* Bring out of sleep mode (AGAIN)417*418* WARNING WARNING WARNING419*420* There is a problem with the chip where it doesn't always indicate421* that it's awake, so initializePowerUp() will fail.422*/423if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))424return AH_FALSE;425426/* Clear warm reset reg */427return ar5210SetResetReg(ah, 0, 10);428#undef AR_RC_HW429}430431enum {432FIRPWR_M = 0x03fc0000,433FIRPWR_S = 18,434KCOARSEHIGH_M = 0x003f8000,435KCOARSEHIGH_S = 15,436KCOARSELOW_M = 0x00007f80,437KCOARSELOW_S = 7,438ADCSAT_ICOUNT_M = 0x0001f800,439ADCSAT_ICOUNT_S = 11,440ADCSAT_THRESH_M = 0x000007e0,441ADCSAT_THRESH_S = 5442};443444/*445* Recalibrate the lower PHY chips to account for temperature/environment446* changes.447*/448HAL_BOOL449ar5210PerCalibrationN(struct ath_hal *ah,450struct ieee80211_channel *chan, u_int chainMask,451HAL_BOOL longCal, HAL_BOOL *isCalDone)452{453uint32_t regBeacon;454uint32_t reg9858, reg985c, reg9868;455HAL_CHANNEL_INTERNAL *ichan;456457ichan = ath_hal_checkchannel(ah, chan);458if (ichan == AH_NULL)459return AH_FALSE;460/* Disable tx and rx */461ar5210UpdateDiagReg(ah,462OS_REG_READ(ah, AR_DIAG_SW) | (AR_DIAG_SW_DIS_TX | AR_DIAG_SW_DIS_RX));463464/* Disable Beacon Enable */465regBeacon = OS_REG_READ(ah, AR_BEACON);466OS_REG_WRITE(ah, AR_BEACON, regBeacon & ~AR_BEACON_EN);467468/* Delay 4ms to ensure that all tx and rx activity has ceased */469OS_DELAY(4000);470471/* Disable AGC to radio traffic */472OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) | 0x08000000);473/* Wait for the AGC traffic to cease. */474OS_DELAY(10);475476/* Change Channel to relock synth */477if (!ar5210SetChannel(ah, chan))478return AH_FALSE;479480/* wait for the synthesizer lock to stabilize */481OS_DELAY(1000);482483/* Re-enable AGC to radio traffic */484OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) & (~0x08000000));485486/*487* Configure the AGC so that it is highly unlikely (if not488* impossible) for it to send any gain changes to the analog489* chip. We store off the current values so that they can490* be rewritten below. Setting the following values:491* firpwr = -1492* Kcoursehigh = -1493* Kcourselow = -127494* ADCsat_icount = 2495* ADCsat_thresh = 12496*/497reg9858 = OS_REG_READ(ah, 0x9858);498reg985c = OS_REG_READ(ah, 0x985c);499reg9868 = OS_REG_READ(ah, 0x9868);500501OS_REG_WRITE(ah, 0x9858, (reg9858 & ~FIRPWR_M) |502((-1 << FIRPWR_S) & FIRPWR_M));503OS_REG_WRITE(ah, 0x985c,504(reg985c & ~(KCOARSEHIGH_M | KCOARSELOW_M)) |505((-1 << KCOARSEHIGH_S) & KCOARSEHIGH_M) |506((-127 << KCOARSELOW_S) & KCOARSELOW_M));507OS_REG_WRITE(ah, 0x9868,508(reg9868 & ~(ADCSAT_ICOUNT_M | ADCSAT_THRESH_M)) |509((2 << ADCSAT_ICOUNT_S) & ADCSAT_ICOUNT_M) |510((12 << ADCSAT_THRESH_S) & ADCSAT_THRESH_M));511512/* Wait for AGC changes to be enacted */513OS_DELAY(20);514515/*516* We disable RF mix/gain stages for the PGA to avoid a517* race condition that will occur with receiving a frame518* and performing the AGC calibration. This will be519* re-enabled at the end of offset cal. We turn off AGC520* writes during this write as it will go over the analog bus.521*/522OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) | 0x08000000);523OS_DELAY(10); /* wait for the AGC traffic to cease */524OS_REG_WRITE(ah, 0x98D4, 0x21);525OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) & (~0x08000000));526527/* wait to make sure that additional AGC traffic has quiesced */528OS_DELAY(1000);529530/* AGC calibration (this was added to make the NF threshold check work) */531OS_REG_WRITE(ah, AR_PHY_AGCCTL,532OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_CAL);533if (!ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_CAL, 0)) {534HALDEBUG(ah, HAL_DEBUG_ANY, "%s: AGC calibration timeout\n",535__func__);536}537538/* Rewrite our AGC values we stored off earlier (return AGC to normal operation) */539OS_REG_WRITE(ah, 0x9858, reg9858);540OS_REG_WRITE(ah, 0x985c, reg985c);541OS_REG_WRITE(ah, 0x9868, reg9868);542543/* Perform noise floor and set status */544if (!ar5210CalNoiseFloor(ah, ichan)) {545/*546* Delay 5ms before retrying the noise floor -547* just to make sure. We're in an error548* condition here549*/550HALDEBUG(ah, HAL_DEBUG_NFCAL | HAL_DEBUG_PERCAL,551"%s: Performing 2nd Noise Cal\n", __func__);552OS_DELAY(5000);553if (!ar5210CalNoiseFloor(ah, ichan))554chan->ic_state |= IEEE80211_CHANSTATE_CWINT;555}556557/* Clear tx and rx disable bit */558ar5210UpdateDiagReg(ah,559OS_REG_READ(ah, AR_DIAG_SW) & ~(AR_DIAG_SW_DIS_TX | AR_DIAG_SW_DIS_RX));560561/* Re-enable Beacons */562OS_REG_WRITE(ah, AR_BEACON, regBeacon);563564*isCalDone = AH_TRUE;565566return AH_TRUE;567}568569HAL_BOOL570ar5210PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,571HAL_BOOL *isIQdone)572{573return ar5210PerCalibrationN(ah, chan, 0x1, AH_TRUE, isIQdone);574}575576HAL_BOOL577ar5210ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)578{579return AH_TRUE;580}581582/*583* Writes the given reset bit mask into the reset register584*/585static HAL_BOOL586ar5210SetResetReg(struct ath_hal *ah, uint32_t resetMask, u_int delay)587{588uint32_t mask = resetMask ? resetMask : ~0;589HAL_BOOL rt;590591OS_REG_WRITE(ah, AR_RC, resetMask);592/* need to wait at least 128 clocks when reseting PCI before read */593OS_DELAY(delay);594595resetMask &= AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC;596mask &= AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC;597rt = ath_hal_wait(ah, AR_RC, mask, resetMask);598if ((resetMask & AR_RC_RMAC) == 0) {599if (isBigEndian()) {600/*601* Set CFG, little-endian for descriptor accesses.602*/603mask = INIT_CONFIG_STATUS | AR_CFG_SWTD | AR_CFG_SWRD;604OS_REG_WRITE(ah, AR_CFG, mask);605} else606OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);607}608return rt;609}610611/*612* Returns: the pcdac value613*/614static uint8_t615getPcdac(struct ath_hal *ah, const struct tpcMap *pRD, uint8_t dBm)616{617int32_t i;618int useNextEntry = AH_FALSE;619uint32_t interp;620621for (i = AR_TP_SCALING_ENTRIES - 1; i >= 0; i--) {622/* Check for exact entry */623if (dBm == AR_I2DBM(i)) {624if (pRD->pcdac[i] != 63)625return pRD->pcdac[i];626useNextEntry = AH_TRUE;627} else if (dBm + 1 == AR_I2DBM(i) && i > 0) {628/* Interpolate for between entry with a logish scale */629if (pRD->pcdac[i] != 63 && pRD->pcdac[i-1] != 63) {630interp = (350 * (pRD->pcdac[i] - pRD->pcdac[i-1])) + 999;631interp = (interp / 1000) + pRD->pcdac[i-1];632return interp;633}634useNextEntry = AH_TRUE;635} else if (useNextEntry == AH_TRUE) {636/* Grab the next lowest */637if (pRD->pcdac[i] != 63)638return pRD->pcdac[i];639}640}641642/* Return the lowest Entry if we haven't returned */643for (i = 0; i < AR_TP_SCALING_ENTRIES; i++)644if (pRD->pcdac[i] != 63)645return pRD->pcdac[i];646647/* No value to return from table */648#ifdef AH_DEBUG649ath_hal_printf(ah, "%s: empty transmit power table?\n", __func__);650#endif651return 1;652}653654/*655* Find or interpolates the gainF value from the table ptr.656*/657static uint8_t658getGainF(struct ath_hal *ah, const struct tpcMap *pRD,659uint8_t pcdac, uint8_t *dBm)660{661uint32_t interp;662int low, high, i;663664low = high = -1;665666for (i = 0; i < AR_TP_SCALING_ENTRIES; i++) {667if(pRD->pcdac[i] == 63)668continue;669if (pcdac == pRD->pcdac[i]) {670*dBm = AR_I2DBM(i);671return pRD->gainF[i]; /* Exact Match */672}673if (pcdac > pRD->pcdac[i])674low = i;675if (pcdac < pRD->pcdac[i]) {676high = i;677if (low == -1) {678*dBm = AR_I2DBM(i);679/* PCDAC is lower than lowest setting */680return pRD->gainF[i];681}682break;683}684}685if (i >= AR_TP_SCALING_ENTRIES && low == -1) {686/* No settings were found */687#ifdef AH_DEBUG688ath_hal_printf(ah,689"%s: no valid entries in the pcdac table: %d\n",690__func__, pcdac);691#endif692return 63;693}694if (i >= AR_TP_SCALING_ENTRIES) {695/* PCDAC setting was above the max setting in the table */696*dBm = AR_I2DBM(low);697return pRD->gainF[low];698}699/* Only exact if table has no missing entries */700*dBm = (low + high) + 3;701702/*703* Perform interpolation between low and high values to find gainF704* linearly scale the pcdac between low and high705*/706interp = ((pcdac - pRD->pcdac[low]) * 1000) /707(pRD->pcdac[high] - pRD->pcdac[low]);708/*709* Multiply the scale ratio by the gainF difference710* (plus a rnd up factor)711*/712interp = ((interp * (pRD->gainF[high] - pRD->gainF[low])) + 999) / 1000;713714/* Add ratioed gain_f to low gain_f value */715return interp + pRD->gainF[low];716}717718HAL_BOOL719ar5210SetTxPowerLimit(struct ath_hal *ah, uint32_t limit)720{721AH_PRIVATE(ah)->ah_powerLimit = AH_MIN(limit, AR5210_MAX_RATE_POWER);722/* XXX flush to h/w */723return AH_TRUE;724}725726/*727* Get TXPower values and set them in the radio728*/729static HAL_BOOL730setupPowerSettings(struct ath_hal *ah, const struct ieee80211_channel *chan,731uint8_t cp[17])732{733uint16_t freq = ath_hal_gethwchannel(ah, chan);734const HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;735uint8_t gainFRD, gainF36, gainF48, gainF54;736uint8_t dBmRD, dBm36, dBm48, dBm54, dontcare;737uint32_t rd, group;738const struct tpcMap *pRD;739740/* Set OB/DB Values regardless of channel */741cp[15] = (ee->ee_biasCurrents >> 4) & 0x7;742cp[16] = ee->ee_biasCurrents & 0x7;743744if (freq < 5170 || freq > 5320) {745HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u\n",746__func__, freq);747return AH_FALSE;748}749750HALASSERT(ee->ee_version >= AR_EEPROM_VER1 &&751ee->ee_version < AR_EEPROM_VER3);752753/* Match regulatory domain */754for (rd = 0; rd < AR_REG_DOMAINS_MAX; rd++)755if (AH_PRIVATE(ah)->ah_currentRD == ee->ee_regDomain[rd])756break;757if (rd == AR_REG_DOMAINS_MAX) {758#ifdef AH_DEBUG759ath_hal_printf(ah,760"%s: no calibrated regulatory domain matches the "761"current regularly domain (0x%0x)\n", __func__,762AH_PRIVATE(ah)->ah_currentRD);763#endif764return AH_FALSE;765}766group = ((freq - 5170) / 10);767768if (group > 11) {769/* Pull 5.29 into the 5.27 group */770group--;771}772773/* Integer divide will set group from 0 to 4 */774group = group / 3;775pRD = &ee->ee_tpc[group];776777/* Set PC DAC Values */778cp[14] = pRD->regdmn[rd];779cp[9] = AH_MIN(pRD->regdmn[rd], pRD->rate36);780cp[8] = AH_MIN(pRD->regdmn[rd], pRD->rate48);781cp[7] = AH_MIN(pRD->regdmn[rd], pRD->rate54);782783/* Find Corresponding gainF values for RD, 36, 48, 54 */784gainFRD = getGainF(ah, pRD, pRD->regdmn[rd], &dBmRD);785gainF36 = getGainF(ah, pRD, cp[9], &dBm36);786gainF48 = getGainF(ah, pRD, cp[8], &dBm48);787gainF54 = getGainF(ah, pRD, cp[7], &dBm54);788789/* Power Scale if requested */790if (AH_PRIVATE(ah)->ah_tpScale != HAL_TP_SCALE_MAX) {791static const uint16_t tpcScaleReductionTable[5] =792{ 0, 3, 6, 9, AR5210_MAX_RATE_POWER };793uint16_t tpScale;794795tpScale = tpcScaleReductionTable[AH_PRIVATE(ah)->ah_tpScale];796if (dBmRD < tpScale+3)797dBmRD = 3; /* min */798else799dBmRD -= tpScale;800cp[14] = getPcdac(ah, pRD, dBmRD);801gainFRD = getGainF(ah, pRD, cp[14], &dontcare);802dBm36 = AH_MIN(dBm36, dBmRD);803cp[9] = getPcdac(ah, pRD, dBm36);804gainF36 = getGainF(ah, pRD, cp[9], &dontcare);805dBm48 = AH_MIN(dBm48, dBmRD);806cp[8] = getPcdac(ah, pRD, dBm48);807gainF48 = getGainF(ah, pRD, cp[8], &dontcare);808dBm54 = AH_MIN(dBm54, dBmRD);809cp[7] = getPcdac(ah, pRD, dBm54);810gainF54 = getGainF(ah, pRD, cp[7], &dontcare);811}812/* Record current dBm at rate 6 */813AH_PRIVATE(ah)->ah_maxPowerLevel = 2*dBmRD;814815cp[13] = cp[12] = cp[11] = cp[10] = cp[14];816817/* Set GainF Values */818cp[0] = gainFRD - gainF54;819cp[1] = gainFRD - gainF48;820cp[2] = gainFRD - gainF36;821/* 9, 12, 18, 24 have no gain_delta from 6 */822cp[3] = cp[4] = cp[5] = cp[6] = 0;823return AH_TRUE;824}825826/*827* Places the device in and out of reset and then places sane828* values in the registers based on EEPROM config, initialization829* vectors (as determined by the mode), and station configuration830*/831HAL_BOOL832ar5210SetTransmitPower(struct ath_hal *ah, const struct ieee80211_channel *chan)833{834#define N(a) (sizeof (a) / sizeof (a[0]))835static const uint32_t pwr_regs_start[17] = {8360x00000000, 0x00000000, 0x00000000,8370x00000000, 0x00000000, 0xf0000000,8380xcc000000, 0x00000000, 0x00000000,8390x00000000, 0x0a000000, 0x000000e2,8400x0a000020, 0x01000002, 0x01000018,8410x40000000, 0x00000418842};843uint16_t i;844uint8_t cp[sizeof(ar5k0007_pwrSettings)];845uint32_t pwr_regs[17];846847OS_MEMCPY(pwr_regs, pwr_regs_start, sizeof(pwr_regs));848OS_MEMCPY(cp, ar5k0007_pwrSettings, sizeof(cp));849850/* Check the EEPROM tx power calibration settings */851if (!setupPowerSettings(ah, chan, cp)) {852#ifdef AH_DEBUG853ath_hal_printf(ah, "%s: unable to setup power settings\n",854__func__);855#endif856return AH_FALSE;857}858if (cp[15] < 1 || cp[15] > 5) {859#ifdef AH_DEBUG860ath_hal_printf(ah, "%s: OB out of range (%u)\n",861__func__, cp[15]);862#endif863return AH_FALSE;864}865if (cp[16] < 1 || cp[16] > 5) {866#ifdef AH_DEBUG867ath_hal_printf(ah, "%s: DB out of range (%u)\n",868__func__, cp[16]);869#endif870return AH_FALSE;871}872873/* reverse bits of the transmit power array */874for (i = 0; i < 7; i++)875cp[i] = ath_hal_reverseBits(cp[i], 5);876for (i = 7; i < 15; i++)877cp[i] = ath_hal_reverseBits(cp[i], 6);878879/* merge transmit power values into the register - quite gross */880pwr_regs[0] |= ((cp[1] << 5) & 0xE0) | (cp[0] & 0x1F);881pwr_regs[1] |= ((cp[3] << 7) & 0x80) | ((cp[2] << 2) & 0x7C) |882((cp[1] >> 3) & 0x03);883pwr_regs[2] |= ((cp[4] << 4) & 0xF0) | ((cp[3] >> 1) & 0x0F);884pwr_regs[3] |= ((cp[6] << 6) & 0xC0) | ((cp[5] << 1) & 0x3E) |885((cp[4] >> 4) & 0x01);886pwr_regs[4] |= ((cp[7] << 3) & 0xF8) | ((cp[6] >> 2) & 0x07);887pwr_regs[5] |= ((cp[9] << 7) & 0x80) | ((cp[8] << 1) & 0x7E) |888((cp[7] >> 5) & 0x01);889pwr_regs[6] |= ((cp[10] << 5) & 0xE0) | ((cp[9] >> 1) & 0x1F);890pwr_regs[7] |= ((cp[11] << 3) & 0xF8) | ((cp[10] >> 3) & 0x07);891pwr_regs[8] |= ((cp[12] << 1) & 0x7E) | ((cp[11] >> 5) & 0x01);892pwr_regs[9] |= ((cp[13] << 5) & 0xE0);893pwr_regs[10] |= ((cp[14] << 3) & 0xF8) | ((cp[13] >> 3) & 0x07);894pwr_regs[11] |= ((cp[14] >> 5) & 0x01);895896/* Set OB */897pwr_regs[8] |= (ath_hal_reverseBits(cp[15], 3) << 7) & 0x80;898pwr_regs[9] |= (ath_hal_reverseBits(cp[15], 3) >> 1) & 0x03;899900/* Set DB */901pwr_regs[9] |= (ath_hal_reverseBits(cp[16], 3) << 2) & 0x1C;902903/* Write the registers */904for (i = 0; i < N(pwr_regs)-1; i++)905OS_REG_WRITE(ah, 0x0000989c, pwr_regs[i]);906/* last write is a flush */907OS_REG_WRITE(ah, 0x000098d4, pwr_regs[i]);908909return AH_TRUE;910#undef N911}912913/*914* Takes the MHz channel value and sets the Channel value915*916* ASSUMES: Writes enabled to analog bus before AGC is active917* or by disabling the AGC.918*/919static HAL_BOOL920ar5210SetChannel(struct ath_hal *ah, struct ieee80211_channel *chan)921{922uint16_t freq = ath_hal_gethwchannel(ah, chan);923uint32_t data;924925/* Set the Channel */926data = ath_hal_reverseBits((freq - 5120)/10, 5);927data = (data << 1) | 0x41;928OS_REG_WRITE(ah, AR_PHY(0x27), data);929OS_REG_WRITE(ah, AR_PHY(0x30), 0);930AH_PRIVATE(ah)->ah_curchan = chan;931return AH_TRUE;932}933934int16_t935ar5210GetNoiseFloor(struct ath_hal *ah)936{937int16_t nf;938939nf = (OS_REG_READ(ah, AR_PHY(25)) >> 19) & 0x1ff;940if (nf & 0x100)941nf = 0 - ((nf ^ 0x1ff) + 1);942return nf;943}944945#define NORMAL_NF_THRESH (-72)946/*947* Peform the noisefloor calibration and check for948* any constant channel interference949*950* Returns: TRUE for a successful noise floor calibration; else FALSE951*/952HAL_BOOL953ar5210CalNoiseFloor(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan)954{955int32_t nf, nfLoops;956957/* Calibrate the noise floor */958OS_REG_WRITE(ah, AR_PHY_AGCCTL,959OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_NF);960961/* Do not read noise floor until it has done the first update */962if (!ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_NF, 0)) {963#ifdef ATH_HAL_DEBUG964ath_hal_printf(ah, " -PHY NF Reg state: 0x%x\n",965OS_REG_READ(ah, AR_PHY_AGCCTL));966ath_hal_printf(ah, " -MAC Reset Reg state: 0x%x\n",967OS_REG_READ(ah, AR_RC));968ath_hal_printf(ah, " -PHY Active Reg state: 0x%x\n",969OS_REG_READ(ah, AR_PHY_ACTIVE));970#endif /* ATH_HAL_DEBUG */971return AH_FALSE;972}973974nf = 0;975/* Keep checking until the floor is below the threshold or the nf is done */976for (nfLoops = 0; ((nfLoops < 21) && (nf > NORMAL_NF_THRESH)); nfLoops++) {977OS_DELAY(1000); /* Sleep for 1 ms */978nf = ar5210GetNoiseFloor(ah);979}980981if (nf > NORMAL_NF_THRESH) {982HALDEBUG(ah, HAL_DEBUG_ANY, "%s: Bad noise cal %d\n",983__func__, nf);984ichan->rawNoiseFloor = 0;985return AH_FALSE;986}987ichan->rawNoiseFloor = nf;988return AH_TRUE;989}990991/*992* Adjust NF based on statistical values for 5GHz frequencies.993*/994int16_t995ar5210GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)996{997return 0;998}9991000HAL_RFGAIN1001ar5210GetRfgain(struct ath_hal *ah)1002{1003return HAL_RFGAIN_INACTIVE;1004}100510061007