2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "ah_internal.h"
25 #include "ar5212/ar5212.h"
26 #include "ar5212/ar5212reg.h"
27 #include "ar5212/ar5212phy.h"
29 #include "ah_eeprom_v3.h"
31 /* Additional Time delay to wait after activiting the Base band */
32 #define BASE_ACTIVATE_DELAY 100 /* 100 usec */
33 #define PLL_SETTLE_DELAY 300 /* 300 usec */
35 static HAL_BOOL ar5212SetResetReg(struct ath_hal *, uint32_t resetMask);
36 /* NB: public for 5312 use */
37 HAL_BOOL ar5212IsSpurChannel(struct ath_hal *,
38 const struct ieee80211_channel *);
39 HAL_BOOL ar5212ChannelChange(struct ath_hal *,
40 const struct ieee80211_channel *);
41 int16_t ar5212GetNf(struct ath_hal *, struct ieee80211_channel *);
42 HAL_BOOL ar5212SetBoardValues(struct ath_hal *,
43 const struct ieee80211_channel *);
44 void ar5212SetDeltaSlope(struct ath_hal *,
45 const struct ieee80211_channel *);
46 HAL_BOOL ar5212SetTransmitPower(struct ath_hal *ah,
47 const struct ieee80211_channel *chan, uint16_t *rfXpdGain);
48 static HAL_BOOL ar5212SetRateTable(struct ath_hal *,
49 const struct ieee80211_channel *, int16_t tpcScaleReduction,
51 HAL_BOOL commit, int16_t *minPower, int16_t *maxPower);
52 static void ar5212CorrectGainDelta(struct ath_hal *, int twiceOfdmCckDelta);
53 static void ar5212GetTargetPowers(struct ath_hal *,
54 const struct ieee80211_channel *,
55 const TRGT_POWER_INFO *pPowerInfo, uint16_t numChannels,
56 TRGT_POWER_INFO *pNewPower);
57 static uint16_t ar5212GetMaxEdgePower(uint16_t channel,
58 const RD_EDGES_POWER *pRdEdgesPower);
59 void ar5212SetRateDurationTable(struct ath_hal *,
60 const struct ieee80211_channel *);
61 void ar5212SetIFSTiming(struct ath_hal *,
62 const struct ieee80211_channel *);
64 /* NB: public for RF backend use */
65 void ar5212GetLowerUpperValues(uint16_t value,
66 uint16_t *pList, uint16_t listSize,
67 uint16_t *pLowerValue, uint16_t *pUpperValue);
68 void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
69 uint32_t numBits, uint32_t firstBit, uint32_t column);
72 write_common(struct ath_hal *ah, const HAL_INI_ARRAY *ia,
73 HAL_BOOL bChannelChange, int writes)
75 #define IS_NO_RESET_TIMER_ADDR(x) \
76 ( (((x) >= AR_BEACON) && ((x) <= AR_CFP_DUR)) || \
77 (((x) >= AR_SLEEP1) && ((x) <= AR_SLEEP3)))
78 #define V(r, c) (ia)->data[((r)*(ia)->cols) + (c)]
81 /* Write Common Array Parameters */
82 for (r = 0; r < ia->rows; r++) {
83 uint32_t reg = V(r, 0);
84 /* XXX timer/beacon setup registers? */
85 /* On channel change, don't reset the PCU registers */
86 if (!(bChannelChange && IS_NO_RESET_TIMER_ADDR(reg))) {
87 OS_REG_WRITE(ah, reg, V(r, 1));
92 #undef IS_NO_RESET_TIMER_ADDR
96 #define IS_DISABLE_FAST_ADC_CHAN(x) (((x) == 2462) || ((x) == 2467))
99 * XXX NDIS 5.x code had MAX_RESET_WAIT set to 2000 for AP code
100 * and 10 for Client code
102 #define MAX_RESET_WAIT 10
104 #define TX_QUEUEPEND_CHECK 1
105 #define TX_ENABLE_CHECK 2
106 #define RX_ENABLE_CHECK 4
109 * Places the device in and out of reset and then places sane
110 * values in the registers based on EEPROM config, initialization
111 * vectors (as determined by the mode), and station configuration
113 * bChannelChange is used to preserve DMA/PCU registers across
114 * a HW Reset during channel change.
117 ar5212Reset(struct ath_hal *ah, HAL_OPMODE opmode,
118 struct ieee80211_channel *chan,
119 HAL_BOOL bChannelChange, HAL_STATUS *status)
121 #define N(a) (sizeof (a) / sizeof (a[0]))
122 #define FAIL(_code) do { ecode = _code; goto bad; } while (0)
123 struct ath_hal_5212 *ahp = AH5212(ah);
124 HAL_CHANNEL_INTERNAL *ichan = AH_NULL;
125 const HAL_EEPROM *ee;
126 uint32_t softLedCfg, softLedState;
127 uint32_t saveFrameSeqCount, saveDefAntenna, saveLedState;
128 uint32_t macStaId1, synthDelay, txFrm2TxDStart;
129 uint16_t rfXpdGain[MAX_NUM_PDGAINS_PER_CHANNEL];
130 int16_t cckOfdmPwrDelta = 0;
131 u_int modesIndex, freqIndex;
134 uint32_t testReg, powerVal;
135 int8_t twiceAntennaGain, twiceAntennaReduction;
136 uint32_t ackTpcPow, ctsTpcPow, chirpTpcPow;
137 HAL_BOOL isBmode = AH_FALSE;
139 HALASSERT(ah->ah_magic == AR5212_MAGIC);
140 ee = AH_PRIVATE(ah)->ah_eeprom;
142 OS_MARK(ah, AH_MARK_RESET, bChannelChange);
144 /* Bring out of sleep mode */
145 if (!ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
146 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip did not wakeup\n",
152 * Map public channel to private.
154 ichan = ath_hal_checkchannel(ah, chan);
155 if (ichan == AH_NULL)
164 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid operating mode %u\n",
169 HALASSERT(AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER3);
171 SAVE_CCK(ah, chan, isBmode);
173 /* Preserve certain DMA hardware registers on a channel change */
174 if (bChannelChange) {
176 * On Venice, the TSF is almost preserved across a reset;
177 * it requires doubling writes to the RESET_TSF
178 * bit in the AR_BEACON register; it also has the quirk
179 * of the TSF going back in time on the station (station
180 * latches onto the last beacon's tsf during a reset 50%
181 * of the times); the latter is not a problem for adhoc
182 * stations since as long as the TSF is behind, it will
183 * get resynchronized on receiving the next beacon; the
184 * TSF going backwards in time could be a problem for the
185 * sleep operation (supported on infrastructure stations
186 * only) - the best and most general fix for this situation
187 * is to resynchronize the various sleep/beacon timers on
188 * the receipt of the next beacon i.e. when the TSF itself
189 * gets resynchronized to the AP's TSF - power save is
190 * needed to be temporarily disabled until that time
192 * Need to save the sequence number to restore it after
195 saveFrameSeqCount = OS_REG_READ(ah, AR_D_SEQNUM);
197 saveFrameSeqCount = 0; /* NB: silence compiler */
199 /* Blank the channel survey statistics */
200 OS_MEMZERO(&ahp->ah_chansurvey, sizeof(ahp->ah_chansurvey));
203 * XXX disable for now; this appears to sometimes cause OFDM
204 * XXX timing error floods when ani is enabled and bg scanning
207 /* If the channel change is across the same mode - perform a fast channel change */
208 if (IS_2413(ah) || IS_5413(ah)) {
210 * Fast channel change can only be used when:
211 * -channel change requested - so it's not the initial reset.
212 * -it's not a change to the current channel -
213 * often called when switching modes on a channel
214 * -the modes of the previous and requested channel are the
216 * XXX opmode shouldn't change either?
218 if (bChannelChange &&
219 (AH_PRIVATE(ah)->ah_curchan != AH_NULL) &&
220 (chan->ic_freq != AH_PRIVATE(ah)->ah_curchan->ic_freq) &&
221 ((chan->ic_flags & IEEE80211_CHAN_ALLTURBO) ==
222 (AH_PRIVATE(ah)->ah_curchan->ic_flags & IEEE80211_CHAN_ALLTURBO))) {
223 if (ar5212ChannelChange(ah, chan)) {
224 /* If ChannelChange completed - skip the rest of reset */
232 * Preserve the antenna on a channel change
234 saveDefAntenna = OS_REG_READ(ah, AR_DEF_ANTENNA);
235 if (saveDefAntenna == 0) /* XXX magic constants */
238 /* Save hardware flag before chip reset clears the register */
239 macStaId1 = OS_REG_READ(ah, AR_STA_ID1) &
240 (AR_STA_ID1_BASE_RATE_11B | AR_STA_ID1_USE_DEFANT);
242 /* Save led state from pci config register */
243 saveLedState = OS_REG_READ(ah, AR_PCICFG) &
244 (AR_PCICFG_LEDCTL | AR_PCICFG_LEDMODE | AR_PCICFG_LEDBLINK |
246 softLedCfg = OS_REG_READ(ah, AR_GPIOCR);
247 softLedState = OS_REG_READ(ah, AR_GPIODO);
249 ar5212RestoreClock(ah, opmode); /* move to refclk operation */
252 * Adjust gain parameters before reset if
253 * there's an outstanding gain updated.
255 (void) ar5212GetRfgain(ah);
257 if (!ar5212ChipReset(ah, chan)) {
258 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
262 /* Setup the indices for the next set of register array writes */
263 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
265 if (IEEE80211_IS_CHAN_108G(chan))
267 else if (IEEE80211_IS_CHAN_G(chan))
269 else if (IEEE80211_IS_CHAN_B(chan))
272 HALDEBUG(ah, HAL_DEBUG_ANY,
273 "%s: invalid channel %u/0x%x\n",
274 __func__, chan->ic_freq, chan->ic_flags);
279 if (IEEE80211_IS_CHAN_TURBO(chan))
281 else if (IEEE80211_IS_CHAN_A(chan))
284 HALDEBUG(ah, HAL_DEBUG_ANY,
285 "%s: invalid channel %u/0x%x\n",
286 __func__, chan->ic_freq, chan->ic_flags);
291 OS_MARK(ah, AH_MARK_RESET_LINE, __LINE__);
293 /* Set correct Baseband to analog shift setting to access analog chips. */
294 OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
296 regWrites = ath_hal_ini_write(ah, &ahp->ah_ini_modes, modesIndex, 0);
297 regWrites = write_common(ah, &ahp->ah_ini_common, bChannelChange,
299 #ifdef AH_RXCFG_SDMAMW_4BYTES
301 * Nala doesn't work with 128 byte bursts on pb42(hydra) (ar71xx),
302 * use 4 instead. Enabling it on all platforms would hurt performance,
303 * so we only enable it on the ones that are affected by it.
305 OS_REG_WRITE(ah, AR_RXCFG, 0);
307 ahp->ah_rfHal->writeRegs(ah, modesIndex, freqIndex, regWrites);
309 OS_MARK(ah, AH_MARK_RESET_LINE, __LINE__);
311 if (IEEE80211_IS_CHAN_HALF(chan) || IEEE80211_IS_CHAN_QUARTER(chan)) {
312 ar5212SetIFSTiming(ah, chan);
315 * Force window_length for 1/2 and 1/4 rate channels,
316 * the ini file sets this to zero otherwise.
318 OS_REG_RMW_FIELD(ah, AR_PHY_FRAME_CTL,
319 AR_PHY_FRAME_CTL_WINLEN, 3);
323 /* Overwrite INI values for revised chipsets */
324 if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_2) {
326 OS_REG_WRITE(ah, AR_PHY_ADC_CTL,
327 SM(2, AR_PHY_ADC_CTL_OFF_INBUFGAIN) |
328 SM(2, AR_PHY_ADC_CTL_ON_INBUFGAIN) |
329 AR_PHY_ADC_CTL_OFF_PWDDAC |
330 AR_PHY_ADC_CTL_OFF_PWDADC);
333 if (ichan->channel == 2484) {
334 cckOfdmPwrDelta = SCALE_OC_DELTA(
335 ee->ee_cckOfdmPwrDelta -
336 ee->ee_scaledCh14FilterCckDelta);
338 cckOfdmPwrDelta = SCALE_OC_DELTA(
339 ee->ee_cckOfdmPwrDelta);
342 if (IEEE80211_IS_CHAN_G(chan)) {
343 OS_REG_WRITE(ah, AR_PHY_TXPWRADJ,
344 SM((ee->ee_cckOfdmPwrDelta*-1),
345 AR_PHY_TXPWRADJ_CCK_GAIN_DELTA) |
346 SM((cckOfdmPwrDelta*-1),
347 AR_PHY_TXPWRADJ_CCK_PCDAC_INDEX));
349 OS_REG_WRITE(ah, AR_PHY_TXPWRADJ, 0);
352 /* Add barker RSSI thresh enable as disabled */
353 OS_REG_CLR_BIT(ah, AR_PHY_DAG_CTRLCCK,
354 AR_PHY_DAG_CTRLCCK_EN_RSSI_THR);
355 OS_REG_RMW_FIELD(ah, AR_PHY_DAG_CTRLCCK,
356 AR_PHY_DAG_CTRLCCK_RSSI_THR, 2);
358 /* Set the mute mask to the correct default */
359 OS_REG_WRITE(ah, AR_SEQ_MASK, 0x0000000F);
362 if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_3) {
363 /* Clear reg to alllow RX_CLEAR line debug */
364 OS_REG_WRITE(ah, AR_PHY_BLUETOOTH, 0);
366 if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_4) {
368 /* Enable burst prefetch for the data queues */
369 OS_REG_RMW_FIELD(ah, AR_D_FPCTL, ... );
370 /* Enable double-buffering */
371 OS_REG_CLR_BIT(ah, AR_TXCFG, AR_TXCFG_DBL_BUF_DIS);
375 /* Set ADC/DAC select values */
376 OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL, 0x0e);
378 if (IS_5413(ah) || IS_2417(ah)) {
380 if (IS_DISABLE_FAST_ADC_CHAN(ichan->channel))
382 /* As it's a clock changing register, only write when the value needs to be changed */
383 if (OS_REG_READ(ah, AR_PHY_FAST_ADC) != newReg)
384 OS_REG_WRITE(ah, AR_PHY_FAST_ADC, newReg);
387 /* Setup the transmit power values. */
388 if (!ar5212SetTransmitPower(ah, chan, rfXpdGain)) {
389 HALDEBUG(ah, HAL_DEBUG_ANY,
390 "%s: error init'ing transmit power\n", __func__);
394 /* Write the analog registers */
395 if (!ahp->ah_rfHal->setRfRegs(ah, chan, modesIndex, rfXpdGain)) {
396 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: ar5212SetRfRegs failed\n",
401 /* Write delta slope for OFDM enabled modes (A, G, Turbo) */
402 if (IEEE80211_IS_CHAN_OFDM(chan)) {
404 AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER5_3)
405 ar5212SetSpurMitigation(ah, chan);
406 ar5212SetDeltaSlope(ah, chan);
409 /* Setup board specific options for EEPROM version 3 */
410 if (!ar5212SetBoardValues(ah, chan)) {
411 HALDEBUG(ah, HAL_DEBUG_ANY,
412 "%s: error setting board options\n", __func__);
416 /* Restore certain DMA hardware registers on a channel change */
418 OS_REG_WRITE(ah, AR_D_SEQNUM, saveFrameSeqCount);
420 OS_MARK(ah, AH_MARK_RESET_LINE, __LINE__);
422 OS_REG_WRITE(ah, AR_STA_ID0, LE_READ_4(ahp->ah_macaddr));
423 OS_REG_WRITE(ah, AR_STA_ID1, LE_READ_2(ahp->ah_macaddr + 4)
425 | AR_STA_ID1_RTS_USE_DEF
426 | ahp->ah_staId1Defaults
428 ar5212SetOperatingMode(ah, opmode);
430 /* Set Venice BSSID mask according to current state */
431 OS_REG_WRITE(ah, AR_BSSMSKL, LE_READ_4(ahp->ah_bssidmask));
432 OS_REG_WRITE(ah, AR_BSSMSKU, LE_READ_2(ahp->ah_bssidmask + 4));
434 /* Restore previous led state */
435 OS_REG_WRITE(ah, AR_PCICFG, OS_REG_READ(ah, AR_PCICFG) | saveLedState);
437 /* Restore soft Led state to GPIO */
438 OS_REG_WRITE(ah, AR_GPIOCR, softLedCfg);
439 OS_REG_WRITE(ah, AR_GPIODO, softLedState);
441 /* Restore previous antenna */
442 OS_REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
444 /* then our BSSID and associate id */
445 OS_REG_WRITE(ah, AR_BSS_ID0, LE_READ_4(ahp->ah_bssid));
446 OS_REG_WRITE(ah, AR_BSS_ID1, LE_READ_2(ahp->ah_bssid + 4) |
447 (ahp->ah_assocId & 0x3fff) << AR_BSS_ID1_AID_S);
449 /* Restore bmiss rssi & count thresholds */
450 OS_REG_WRITE(ah, AR_RSSI_THR, ahp->ah_rssiThr);
452 OS_REG_WRITE(ah, AR_ISR, ~0); /* cleared on write */
454 if (!ar5212SetChannel(ah, chan))
457 OS_MARK(ah, AH_MARK_RESET_LINE, __LINE__);
459 ar5212SetCoverageClass(ah, AH_PRIVATE(ah)->ah_coverageClass, 1);
461 ar5212SetRateDurationTable(ah, chan);
463 /* Set Tx frame start to tx data start delay */
464 if (IS_RAD5112_ANY(ah) &&
465 (IEEE80211_IS_CHAN_HALF(chan) || IEEE80211_IS_CHAN_QUARTER(chan))) {
467 IEEE80211_IS_CHAN_HALF(chan) ?
468 TX_FRAME_D_START_HALF_RATE:
469 TX_FRAME_D_START_QUARTER_RATE;
470 OS_REG_RMW_FIELD(ah, AR_PHY_TX_CTL,
471 AR_PHY_TX_FRAME_TO_TX_DATA_START, txFrm2TxDStart);
475 * Setup fast diversity.
476 * Fast diversity can be enabled or disabled via regadd.txt.
477 * Default is enabled.
480 * 0x00009860 0x00009d18 (if 11a / 11g, else no change)
481 * 0x00009970 0x192bb514
482 * 0x0000a208 0xd03e4648
484 * Enable: 0x00009860 0x00009d10 (if 11a / 11g, else no change)
485 * 0x00009970 0x192fb514
486 * 0x0000a208 0xd03e6788
489 /* XXX Setup pre PHY ENABLE EAR additions */
491 * Wait for the frequency synth to settle (synth goes on
492 * via AR_PHY_ACTIVE_EN). Read the phy active delay register.
493 * Value is in 100ns increments.
495 synthDelay = OS_REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
496 if (IEEE80211_IS_CHAN_B(chan)) {
497 synthDelay = (4 * synthDelay) / 22;
502 /* Activate the PHY (includes baseband activate and synthesizer on) */
503 OS_REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
506 * There is an issue if the AP starts the calibration before
507 * the base band timeout completes. This could result in the
508 * rx_clear false triggering. As a workaround we add delay an
509 * extra BASE_ACTIVATE_DELAY usecs to ensure this condition
512 if (IEEE80211_IS_CHAN_HALF(chan)) {
513 OS_DELAY((synthDelay << 1) + BASE_ACTIVATE_DELAY);
514 } else if (IEEE80211_IS_CHAN_QUARTER(chan)) {
515 OS_DELAY((synthDelay << 2) + BASE_ACTIVATE_DELAY);
517 OS_DELAY(synthDelay + BASE_ACTIVATE_DELAY);
521 * The udelay method is not reliable with notebooks.
522 * Need to check to see if the baseband is ready
524 testReg = OS_REG_READ(ah, AR_PHY_TESTCTRL);
525 /* Selects the Tx hold */
526 OS_REG_WRITE(ah, AR_PHY_TESTCTRL, AR_PHY_TESTCTRL_TXHOLD);
529 (OS_REG_READ(ah, 0x9c24) & 0x10)) /* test if baseband not ready */ OS_DELAY(200);
530 OS_REG_WRITE(ah, AR_PHY_TESTCTRL, testReg);
532 /* Calibrate the AGC and start a NF calculation */
533 OS_REG_WRITE(ah, AR_PHY_AGC_CONTROL,
534 OS_REG_READ(ah, AR_PHY_AGC_CONTROL)
535 | AR_PHY_AGC_CONTROL_CAL
536 | AR_PHY_AGC_CONTROL_NF);
538 if (!IEEE80211_IS_CHAN_B(chan) && ahp->ah_bIQCalibration != IQ_CAL_DONE) {
539 /* Start IQ calibration w/ 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples */
540 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
541 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
542 INIT_IQCAL_LOG_COUNT_MAX);
543 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
544 AR_PHY_TIMING_CTRL4_DO_IQCAL);
545 ahp->ah_bIQCalibration = IQ_CAL_RUNNING;
547 ahp->ah_bIQCalibration = IQ_CAL_INACTIVE;
549 /* Setup compression registers */
550 ar5212SetCompRegs(ah);
552 /* Set 1:1 QCU to DCU mapping for all queues */
553 for (i = 0; i < AR_NUM_DCU; i++)
554 OS_REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
556 ahp->ah_intrTxqs = 0;
557 for (i = 0; i < AH_PRIVATE(ah)->ah_caps.halTotalQueues; i++)
558 ar5212ResetTxQueue(ah, i);
561 * Setup interrupt handling. Note that ar5212ResetTxQueue
562 * manipulates the secondary IMR's as queues are enabled
563 * and disabled. This is done with RMW ops to insure the
564 * settings we make here are preserved.
566 ahp->ah_maskReg = AR_IMR_TXOK | AR_IMR_TXERR | AR_IMR_TXURN
567 | AR_IMR_RXOK | AR_IMR_RXERR | AR_IMR_RXORN
570 if (opmode == HAL_M_HOSTAP)
571 ahp->ah_maskReg |= AR_IMR_MIB;
572 OS_REG_WRITE(ah, AR_IMR, ahp->ah_maskReg);
573 /* Enable bus errors that are OR'd to set the HIUERR bit */
574 OS_REG_WRITE(ah, AR_IMR_S2,
575 OS_REG_READ(ah, AR_IMR_S2)
576 | AR_IMR_S2_MCABT | AR_IMR_S2_SSERR | AR_IMR_S2_DPERR);
578 if (AH_PRIVATE(ah)->ah_rfkillEnabled)
579 ar5212EnableRfKill(ah);
581 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL, 0)) {
582 HALDEBUG(ah, HAL_DEBUG_ANY,
583 "%s: offset calibration failed to complete in 1ms;"
584 " noisy environment?\n", __func__);
588 * Set clocks back to 32kHz if they had been using refClk, then
589 * use an external 32kHz crystal when sleeping, if one exists.
591 ar5212SetupClock(ah, opmode);
594 * Writing to AR_BEACON will start timers. Hence it should
595 * be the last register to be written. Do not reset tsf, do
596 * not enable beacons at this point, but preserve other values
597 * like beaconInterval.
599 OS_REG_WRITE(ah, AR_BEACON,
600 (OS_REG_READ(ah, AR_BEACON) &~ (AR_BEACON_EN | AR_BEACON_RESET_TSF)));
602 /* XXX Setup post reset EAR additions */
605 if (AH_PRIVATE(ah)->ah_macVersion > AR_SREV_VERSION_VENICE ||
606 (AH_PRIVATE(ah)->ah_macVersion == AR_SREV_VERSION_VENICE &&
607 AH_PRIVATE(ah)->ah_macRev >= AR_SREV_GRIFFIN_LITE)) {
608 OS_REG_WRITE(ah, AR_QOS_CONTROL, 0x100aa); /* XXX magic */
609 OS_REG_WRITE(ah, AR_QOS_SELECT, 0x3210); /* XXX magic */
612 /* Turn on NOACK Support for QoS packets */
613 OS_REG_WRITE(ah, AR_NOACK,
614 SM(2, AR_NOACK_2BIT_VALUE) |
615 SM(5, AR_NOACK_BIT_OFFSET) |
616 SM(0, AR_NOACK_BYTE_OFFSET));
618 /* Get Antenna Gain reduction */
619 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
620 ath_hal_eepromGet(ah, AR_EEP_ANTGAINMAX_5, &twiceAntennaGain);
622 ath_hal_eepromGet(ah, AR_EEP_ANTGAINMAX_2, &twiceAntennaGain);
624 twiceAntennaReduction =
625 ath_hal_getantennareduction(ah, chan, twiceAntennaGain);
627 /* TPC for self-generated frames */
629 ackTpcPow = MS(ahp->ah_macTPC, AR_TPC_ACK);
630 if ((ackTpcPow-ahp->ah_txPowerIndexOffset) > chan->ic_maxpower)
631 ackTpcPow = chan->ic_maxpower+ahp->ah_txPowerIndexOffset;
633 if (ackTpcPow > (2*chan->ic_maxregpower - twiceAntennaReduction))
634 ackTpcPow = (2*chan->ic_maxregpower - twiceAntennaReduction)
635 + ahp->ah_txPowerIndexOffset;
637 ctsTpcPow = MS(ahp->ah_macTPC, AR_TPC_CTS);
638 if ((ctsTpcPow-ahp->ah_txPowerIndexOffset) > chan->ic_maxpower)
639 ctsTpcPow = chan->ic_maxpower+ahp->ah_txPowerIndexOffset;
641 if (ctsTpcPow > (2*chan->ic_maxregpower - twiceAntennaReduction))
642 ctsTpcPow = (2*chan->ic_maxregpower - twiceAntennaReduction)
643 + ahp->ah_txPowerIndexOffset;
645 chirpTpcPow = MS(ahp->ah_macTPC, AR_TPC_CHIRP);
646 if ((chirpTpcPow-ahp->ah_txPowerIndexOffset) > chan->ic_maxpower)
647 chirpTpcPow = chan->ic_maxpower+ahp->ah_txPowerIndexOffset;
649 if (chirpTpcPow > (2*chan->ic_maxregpower - twiceAntennaReduction))
650 chirpTpcPow = (2*chan->ic_maxregpower - twiceAntennaReduction)
651 + ahp->ah_txPowerIndexOffset;
657 if (chirpTpcPow > 63)
660 powerVal = SM(ackTpcPow, AR_TPC_ACK) |
661 SM(ctsTpcPow, AR_TPC_CTS) |
662 SM(chirpTpcPow, AR_TPC_CHIRP);
664 OS_REG_WRITE(ah, AR_TPC, powerVal);
666 /* Restore user-specified settings */
667 if (ahp->ah_miscMode != 0)
668 OS_REG_WRITE(ah, AR_MISC_MODE, ahp->ah_miscMode);
669 if (ahp->ah_sifstime != (u_int) -1)
670 ar5212SetSifsTime(ah, ahp->ah_sifstime);
671 if (ahp->ah_slottime != (u_int) -1)
672 ar5212SetSlotTime(ah, ahp->ah_slottime);
673 if (ahp->ah_acktimeout != (u_int) -1)
674 ar5212SetAckTimeout(ah, ahp->ah_acktimeout);
675 if (ahp->ah_ctstimeout != (u_int) -1)
676 ar5212SetCTSTimeout(ah, ahp->ah_ctstimeout);
677 if (AH_PRIVATE(ah)->ah_diagreg != 0)
678 OS_REG_WRITE(ah, AR_DIAG_SW, AH_PRIVATE(ah)->ah_diagreg);
680 AH_PRIVATE(ah)->ah_opmode = opmode; /* record operating mode */
684 if (bChannelChange && !IEEE80211_IS_CHAN_DFS(chan))
685 chan->ic_state &= ~IEEE80211_CHANSTATE_CWINT;
687 HALDEBUG(ah, HAL_DEBUG_RESET, "%s: done\n", __func__);
689 RESTORE_CCK(ah, chan, isBmode);
691 OS_MARK(ah, AH_MARK_RESET_DONE, 0);
695 RESTORE_CCK(ah, chan, isBmode);
697 OS_MARK(ah, AH_MARK_RESET_DONE, ecode);
698 if (status != AH_NULL)
706 * Call the rf backend to change the channel.
709 ar5212SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
711 struct ath_hal_5212 *ahp = AH5212(ah);
713 /* Change the synth */
714 if (!ahp->ah_rfHal->setChannel(ah, chan))
720 * This channel change evaluates whether the selected hardware can
721 * perform a synthesizer-only channel change (no reset). If the
722 * TX is not stopped, or the RFBus cannot be granted in the given
723 * time, the function returns false as a reset is necessary
726 ar5212ChannelChange(struct ath_hal *ah, const struct ieee80211_channel *chan)
729 uint32_t data, synthDelay, qnum;
730 uint16_t rfXpdGain[MAX_NUM_PDGAINS_PER_CHANNEL];
731 HAL_BOOL txStopped = AH_TRUE;
732 HAL_CHANNEL_INTERNAL *ichan;
735 * Map public channel to private.
737 ichan = ath_hal_checkchannel(ah, chan);
739 /* TX must be stopped or RF Bus grant will not work */
740 for (qnum = 0; qnum < AH_PRIVATE(ah)->ah_caps.halTotalQueues; qnum++) {
741 if (ar5212NumTxPending(ah, qnum)) {
742 txStopped = AH_FALSE;
749 /* Kill last Baseband Rx Frame */
750 OS_REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_REQUEST); /* Request analog bus grant */
751 for (ulCount = 0; ulCount < 100; ulCount++) {
752 if (OS_REG_READ(ah, AR_PHY_RFBUS_GNT))
759 /* Change the synth */
760 if (!ar5212SetChannel(ah, chan))
764 * Wait for the frequency synth to settle (synth goes on via PHY_ACTIVE_EN).
765 * Read the phy active delay register. Value is in 100ns increments.
767 data = OS_REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
768 if (IEEE80211_IS_CHAN_B(chan)) {
769 synthDelay = (4 * data) / 22;
771 synthDelay = data / 10;
773 OS_DELAY(synthDelay + BASE_ACTIVATE_DELAY);
775 /* Setup the transmit power values. */
776 if (!ar5212SetTransmitPower(ah, chan, rfXpdGain)) {
777 HALDEBUG(ah, HAL_DEBUG_ANY,
778 "%s: error init'ing transmit power\n", __func__);
782 /* Write delta slope for OFDM enabled modes (A, G, Turbo) */
783 if (IEEE80211_IS_CHAN_OFDM(chan)) {
785 AH_PRIVATE(ah)->ah_eeversion >= AR_EEPROM_VER5_3)
786 ar5212SetSpurMitigation(ah, chan);
787 ar5212SetDeltaSlope(ah, chan);
790 /* Release the RFBus Grant */
791 OS_REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
793 /* Start Noise Floor Cal */
794 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
799 ar5212SetOperatingMode(struct ath_hal *ah, int opmode)
803 val = OS_REG_READ(ah, AR_STA_ID1);
804 val &= ~(AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC);
807 OS_REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_STA_AP
808 | AR_STA_ID1_KSRCH_MODE);
809 OS_REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
812 OS_REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_ADHOC
813 | AR_STA_ID1_KSRCH_MODE);
814 OS_REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
818 OS_REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_KSRCH_MODE);
824 * Places the PHY and Radio chips into reset. A full reset
825 * must be called to leave this state. The PCI/MAC/PCU are
826 * not placed into reset as we must receive interrupt to
827 * re-enable the hardware.
830 ar5212PhyDisable(struct ath_hal *ah)
832 return ar5212SetResetReg(ah, AR_RC_BB);
836 * Places all of hardware into reset
839 ar5212Disable(struct ath_hal *ah)
841 if (!ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
844 * Reset the HW - PCI must be reset after the rest of the
845 * device has been reset.
847 return ar5212SetResetReg(ah, AR_RC_MAC | AR_RC_BB | AR_RC_PCI);
851 * Places the hardware into reset and then pulls it out of reset
853 * TODO: Only write the PLL if we're changing to or from CCK mode
855 * WARNING: The order of the PLL and mode registers must be correct.
858 ar5212ChipReset(struct ath_hal *ah, const struct ieee80211_channel *chan)
861 OS_MARK(ah, AH_MARK_CHIPRESET, chan ? chan->ic_freq : 0);
864 * Reset the HW - PCI must be reset after the rest of the
865 * device has been reset
867 if (!ar5212SetResetReg(ah, AR_RC_MAC | AR_RC_BB | AR_RC_PCI))
870 /* Bring out of sleep mode (AGAIN) */
871 if (!ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
874 /* Clear warm reset register */
875 if (!ar5212SetResetReg(ah, 0))
879 * Perform warm reset before the mode/PLL/turbo registers
880 * are changed in order to deactivate the radio. Mode changes
881 * with an active radio can result in corrupted shifts to the
886 * Set CCK and Turbo modes correctly.
888 if (chan != AH_NULL) { /* NB: can be null during attach */
889 uint32_t rfMode, phyPLL = 0, curPhyPLL, turbo;
891 if (IS_5413(ah)) { /* NB: =>'s 5424 also */
892 rfMode = AR_PHY_MODE_AR5112;
893 if (IEEE80211_IS_CHAN_HALF(chan))
894 rfMode |= AR_PHY_MODE_HALF;
895 else if (IEEE80211_IS_CHAN_QUARTER(chan))
896 rfMode |= AR_PHY_MODE_QUARTER;
898 if (IEEE80211_IS_CHAN_CCK(chan))
899 phyPLL = AR_PHY_PLL_CTL_44_5112;
901 phyPLL = AR_PHY_PLL_CTL_40_5413;
902 } else if (IS_RAD5111(ah)) {
903 rfMode = AR_PHY_MODE_AR5111;
904 if (IEEE80211_IS_CHAN_CCK(chan))
905 phyPLL = AR_PHY_PLL_CTL_44;
907 phyPLL = AR_PHY_PLL_CTL_40;
908 if (IEEE80211_IS_CHAN_HALF(chan))
909 phyPLL = AR_PHY_PLL_CTL_HALF;
910 else if (IEEE80211_IS_CHAN_QUARTER(chan))
911 phyPLL = AR_PHY_PLL_CTL_QUARTER;
912 } else { /* 5112, 2413, 2316, 2317 */
913 rfMode = AR_PHY_MODE_AR5112;
914 if (IEEE80211_IS_CHAN_CCK(chan))
915 phyPLL = AR_PHY_PLL_CTL_44_5112;
917 phyPLL = AR_PHY_PLL_CTL_40_5112;
918 if (IEEE80211_IS_CHAN_HALF(chan))
919 phyPLL |= AR_PHY_PLL_CTL_HALF;
920 else if (IEEE80211_IS_CHAN_QUARTER(chan))
921 phyPLL |= AR_PHY_PLL_CTL_QUARTER;
923 if (IEEE80211_IS_CHAN_G(chan))
924 rfMode |= AR_PHY_MODE_DYNAMIC;
925 else if (IEEE80211_IS_CHAN_OFDM(chan))
926 rfMode |= AR_PHY_MODE_OFDM;
928 rfMode |= AR_PHY_MODE_CCK;
929 if (IEEE80211_IS_CHAN_5GHZ(chan))
930 rfMode |= AR_PHY_MODE_RF5GHZ;
932 rfMode |= AR_PHY_MODE_RF2GHZ;
933 turbo = IEEE80211_IS_CHAN_TURBO(chan) ?
934 (AR_PHY_FC_TURBO_MODE | AR_PHY_FC_TURBO_SHORT) : 0;
935 curPhyPLL = OS_REG_READ(ah, AR_PHY_PLL_CTL);
937 * PLL, Mode, and Turbo values must be written in the correct
939 * - The PLL cannot be set to 44 unless the CCK or DYNAMIC
941 * - Turbo cannot be set at the same time as CCK or DYNAMIC
943 if (IEEE80211_IS_CHAN_CCK(chan)) {
944 OS_REG_WRITE(ah, AR_PHY_TURBO, turbo);
945 OS_REG_WRITE(ah, AR_PHY_MODE, rfMode);
946 if (curPhyPLL != phyPLL) {
947 OS_REG_WRITE(ah, AR_PHY_PLL_CTL, phyPLL);
948 /* Wait for the PLL to settle */
949 OS_DELAY(PLL_SETTLE_DELAY);
952 if (curPhyPLL != phyPLL) {
953 OS_REG_WRITE(ah, AR_PHY_PLL_CTL, phyPLL);
954 /* Wait for the PLL to settle */
955 OS_DELAY(PLL_SETTLE_DELAY);
957 OS_REG_WRITE(ah, AR_PHY_TURBO, turbo);
958 OS_REG_WRITE(ah, AR_PHY_MODE, rfMode);
965 * Recalibrate the lower PHY chips to account for temperature/environment
969 ar5212PerCalibrationN(struct ath_hal *ah,
970 struct ieee80211_channel *chan,
971 u_int chainMask, HAL_BOOL longCal, HAL_BOOL *isCalDone)
973 #define IQ_CAL_TRIES 10
974 struct ath_hal_5212 *ahp = AH5212(ah);
975 HAL_CHANNEL_INTERNAL *ichan;
976 int32_t qCoff, qCoffDenom;
977 int32_t iqCorrMeas, iCoff, iCoffDenom;
978 uint32_t powerMeasQ, powerMeasI;
979 HAL_BOOL isBmode = AH_FALSE;
981 OS_MARK(ah, AH_MARK_PERCAL, chan->ic_freq);
982 *isCalDone = AH_FALSE;
983 ichan = ath_hal_checkchannel(ah, chan);
984 if (ichan == AH_NULL) {
985 HALDEBUG(ah, HAL_DEBUG_ANY,
986 "%s: invalid channel %u/0x%x; no mapping\n",
987 __func__, chan->ic_freq, chan->ic_flags);
990 SAVE_CCK(ah, chan, isBmode);
992 if (ahp->ah_bIQCalibration == IQ_CAL_DONE ||
993 ahp->ah_bIQCalibration == IQ_CAL_INACTIVE)
994 *isCalDone = AH_TRUE;
996 /* IQ calibration in progress. Check to see if it has finished. */
997 if (ahp->ah_bIQCalibration == IQ_CAL_RUNNING &&
998 !(OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_DO_IQCAL)) {
1001 /* IQ Calibration has finished. */
1002 ahp->ah_bIQCalibration = IQ_CAL_INACTIVE;
1003 *isCalDone = AH_TRUE;
1005 /* workaround for misgated IQ Cal results */
1008 /* Read calibration results. */
1009 powerMeasI = OS_REG_READ(ah, AR_PHY_IQCAL_RES_PWR_MEAS_I);
1010 powerMeasQ = OS_REG_READ(ah, AR_PHY_IQCAL_RES_PWR_MEAS_Q);
1011 iqCorrMeas = OS_REG_READ(ah, AR_PHY_IQCAL_RES_IQ_CORR_MEAS);
1012 if (powerMeasI && powerMeasQ)
1014 /* Do we really need this??? */
1015 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
1016 AR_PHY_TIMING_CTRL4_DO_IQCAL);
1017 } while (++i < IQ_CAL_TRIES);
1019 HALDEBUG(ah, HAL_DEBUG_PERCAL,
1020 "%s: IQ cal finished: %d tries\n", __func__, i);
1021 HALDEBUG(ah, HAL_DEBUG_PERCAL,
1022 "%s: powerMeasI %u powerMeasQ %u iqCorrMeas %d\n",
1023 __func__, powerMeasI, powerMeasQ, iqCorrMeas);
1026 * Prescale these values to remove 64-bit operation
1027 * requirement at the loss of a little precision.
1029 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
1030 qCoffDenom = powerMeasQ / 128;
1032 /* Protect against divide-by-0 and loss of sign bits. */
1033 if (iCoffDenom != 0 && qCoffDenom >= 2) {
1034 iCoff = (int8_t)(-iqCorrMeas) / iCoffDenom;
1035 /* IQCORR_Q_I_COFF is a signed 6 bit number */
1038 } else if (iCoff > 31) {
1042 /* IQCORR_Q_Q_COFF is a signed 5 bit number */
1043 qCoff = (powerMeasI / qCoffDenom) - 128;
1046 } else if (qCoff > 15) {
1050 HALDEBUG(ah, HAL_DEBUG_PERCAL,
1051 "%s: iCoff %d qCoff %d\n", __func__, iCoff, qCoff);
1053 /* Write values and enable correction */
1054 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
1055 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
1056 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
1057 AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
1058 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
1059 AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
1061 ahp->ah_bIQCalibration = IQ_CAL_DONE;
1062 ichan->privFlags |= CHANNEL_IQVALID;
1063 ichan->iCoff = iCoff;
1064 ichan->qCoff = qCoff;
1066 } else if (!IEEE80211_IS_CHAN_B(chan) &&
1067 ahp->ah_bIQCalibration == IQ_CAL_DONE &&
1068 (ichan->privFlags & CHANNEL_IQVALID) == 0) {
1070 * Start IQ calibration if configured channel has changed.
1071 * Use a magic number of 15 based on default value.
1073 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
1074 AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
1075 INIT_IQCAL_LOG_COUNT_MAX);
1076 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
1077 AR_PHY_TIMING_CTRL4_DO_IQCAL);
1078 ahp->ah_bIQCalibration = IQ_CAL_RUNNING;
1083 /* Check noise floor results */
1084 ar5212GetNf(ah, chan);
1085 if (!IEEE80211_IS_CHAN_CWINT(chan)) {
1086 /* Perform cal for 5Ghz channels and any OFDM on 5112 */
1087 if (IEEE80211_IS_CHAN_5GHZ(chan) ||
1088 (IS_RAD5112(ah) && IEEE80211_IS_CHAN_OFDM(chan)))
1089 ar5212RequestRfgain(ah);
1092 RESTORE_CCK(ah, chan, isBmode);
1099 ar5212PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
1102 return ar5212PerCalibrationN(ah, chan, 0x1, AH_TRUE, isIQdone);
1106 ar5212ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
1108 HAL_CHANNEL_INTERNAL *ichan;
1110 ichan = ath_hal_checkchannel(ah, chan);
1111 if (ichan == AH_NULL) {
1112 HALDEBUG(ah, HAL_DEBUG_ANY,
1113 "%s: invalid channel %u/0x%x; no mapping\n",
1114 __func__, chan->ic_freq, chan->ic_flags);
1117 ichan->privFlags &= ~CHANNEL_IQVALID;
1121 /**************************************************************
1124 * Disables all active QCUs and ensure that the mac is in a
1128 ar5212MacStop(struct ath_hal *ah)
1132 uint32_t pendFrameCount;
1133 uint32_t macStateFlag;
1138 /* Disable Rx Operation ***********************************/
1139 OS_REG_SET_BIT(ah, AR_CR, AR_CR_RXD);
1141 /* Disable TX Operation ***********************************/
1143 ar5212SetTxdpInvalid(ah);
1145 OS_REG_SET_BIT(ah, AR_Q_TXD, AR_Q_TXD_M);
1147 /* Polling operation for completion of disable ************/
1148 macStateFlag = TX_ENABLE_CHECK | RX_ENABLE_CHECK;
1150 for (count = 0; count < MAX_RESET_WAIT; count++) {
1151 if (macStateFlag & RX_ENABLE_CHECK) {
1152 if (!OS_REG_IS_BIT_SET(ah, AR_CR, AR_CR_RXE)) {
1153 macStateFlag &= ~RX_ENABLE_CHECK;
1157 if (macStateFlag & TX_ENABLE_CHECK) {
1158 if (!OS_REG_IS_BIT_SET(ah, AR_Q_TXE, AR_Q_TXE_M)) {
1159 macStateFlag &= ~TX_ENABLE_CHECK;
1160 macStateFlag |= TX_QUEUEPEND_CHECK;
1163 if (macStateFlag & TX_QUEUEPEND_CHECK) {
1165 for (queue = 0; queue < AR_NUM_DCU; queue++) {
1166 pendFrameCount += OS_REG_READ(ah,
1167 AR_Q0_STS + (queue * 4)) &
1168 AR_Q_STS_PEND_FR_CNT;
1170 if (pendFrameCount == 0) {
1171 macStateFlag &= ~TX_QUEUEPEND_CHECK;
1174 if (macStateFlag == 0) {
1181 if (status != AH_TRUE) {
1182 HALDEBUG(ah, HAL_DEBUG_RESET,
1183 "%s:Failed to stop the MAC state 0x%x\n",
1184 __func__, macStateFlag);
1192 * Write the given reset bit mask into the reset register
1195 ar5212SetResetReg(struct ath_hal *ah, uint32_t resetMask)
1197 uint32_t mask = resetMask ? resetMask : ~0;
1200 /* Never reset the PCIE core */
1201 if (AH_PRIVATE(ah)->ah_ispcie) {
1202 resetMask &= ~AR_RC_PCI;
1205 if (resetMask & (AR_RC_MAC | AR_RC_PCI)) {
1207 * To ensure that the driver can reset the
1208 * MAC, wake up the chip
1210 rt = ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE);
1212 if (rt != AH_TRUE) {
1217 * Disable interrupts
1219 OS_REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
1220 OS_REG_READ(ah, AR_IER);
1222 if (ar5212MacStop(ah) != AH_TRUE) {
1224 * Failed to stop the MAC gracefully; let's be more forceful then
1227 /* need some delay before flush any pending MMR writes */
1229 OS_REG_READ(ah, AR_RXDP);
1231 resetMask |= AR_RC_MAC | AR_RC_BB;
1232 /* _Never_ reset PCI Express core */
1233 if (! AH_PRIVATE(ah)->ah_ispcie) {
1234 resetMask |= AR_RC_PCI;
1238 * Flush the park address of the PCI controller
1240 /* Read PCI slot information less than Hainan revision */
1241 if (AH_PRIVATE(ah)->ah_bustype == HAL_BUS_TYPE_PCI) {
1242 if (!IS_5112_REV5_UP(ah)) {
1243 #define PCI_COMMON_CONFIG_STATUS 0x06
1247 for (i = 0; i < 32; i++) {
1248 ath_hal_read_pci_config_space(ah,
1249 PCI_COMMON_CONFIG_STATUS,
1250 ®16, sizeof(reg16));
1253 #undef PCI_COMMON_CONFIG_STATUS
1258 * MAC stopped gracefully; no need to warm-reset the PCI bus
1261 resetMask &= ~AR_RC_PCI;
1263 /* need some delay before flush any pending MMR writes */
1265 OS_REG_READ(ah, AR_RXDP);
1269 (void) OS_REG_READ(ah, AR_RXDP);/* flush any pending MMR writes */
1270 OS_REG_WRITE(ah, AR_RC, resetMask);
1271 OS_DELAY(15); /* need to wait at least 128 clocks
1272 when reseting PCI before read */
1273 mask &= (AR_RC_MAC | AR_RC_BB);
1274 resetMask &= (AR_RC_MAC | AR_RC_BB);
1275 rt = ath_hal_wait(ah, AR_RC, mask, resetMask);
1276 if ((resetMask & AR_RC_MAC) == 0) {
1277 if (isBigEndian()) {
1279 * Set CFG, little-endian for descriptor accesses.
1281 mask = INIT_CONFIG_STATUS | AR_CFG_SWRD;
1282 #ifndef AH_NEED_DESC_SWAP
1283 mask |= AR_CFG_SWTD;
1285 OS_REG_WRITE(ah, AR_CFG, mask);
1287 OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
1288 if (ar5212SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
1289 (void) OS_REG_READ(ah, AR_ISR_RAC);
1292 /* track PHY power state so we don't try to r/w BB registers */
1293 AH5212(ah)->ah_phyPowerOn = ((resetMask & AR_RC_BB) == 0);
1298 ar5212GetNoiseFloor(struct ath_hal *ah)
1300 int16_t nf = (OS_REG_READ(ah, AR_PHY(25)) >> 19) & 0x1ff;
1302 nf = 0 - ((nf ^ 0x1ff) + 1);
1307 getNoiseFloorThresh(struct ath_hal *ah, const struct ieee80211_channel *chan,
1310 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
1312 HALASSERT(ah->ah_magic == AR5212_MAGIC);
1314 switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
1315 case IEEE80211_CHAN_A:
1316 *nft = ee->ee_noiseFloorThresh[headerInfo11A];
1318 case IEEE80211_CHAN_B:
1319 *nft = ee->ee_noiseFloorThresh[headerInfo11B];
1321 case IEEE80211_CHAN_G:
1322 case IEEE80211_CHAN_PUREG: /* NB: really 108G */
1323 *nft = ee->ee_noiseFloorThresh[headerInfo11G];
1326 HALDEBUG(ah, HAL_DEBUG_ANY,
1327 "%s: invalid channel flags %u/0x%x\n",
1328 __func__, chan->ic_freq, chan->ic_flags);
1335 * Setup the noise floor cal history buffer.
1338 ar5212InitNfCalHistBuffer(struct ath_hal *ah)
1340 struct ath_hal_5212 *ahp = AH5212(ah);
1343 ahp->ah_nfCalHist.first_run = 1;
1344 ahp->ah_nfCalHist.currIndex = 0;
1345 ahp->ah_nfCalHist.privNF = AR5212_CCA_MAX_GOOD_VALUE;
1346 ahp->ah_nfCalHist.invalidNFcount = AR512_NF_CAL_HIST_MAX;
1347 for (i = 0; i < AR512_NF_CAL_HIST_MAX; i ++)
1348 ahp->ah_nfCalHist.nfCalBuffer[i] = AR5212_CCA_MAX_GOOD_VALUE;
1352 * Add a noise floor value to the ring buffer.
1354 static __inline void
1355 updateNFHistBuff(struct ar5212NfCalHist *h, int16_t nf)
1357 h->nfCalBuffer[h->currIndex] = nf;
1358 if (++h->currIndex >= AR512_NF_CAL_HIST_MAX)
1363 * Return the median noise floor value in the ring buffer.
1366 ar5212GetNfHistMid(const int16_t calData[AR512_NF_CAL_HIST_MAX])
1368 int16_t sort[AR512_NF_CAL_HIST_MAX];
1371 OS_MEMCPY(sort, calData, AR512_NF_CAL_HIST_MAX*sizeof(int16_t));
1372 for (i = 0; i < AR512_NF_CAL_HIST_MAX-1; i ++) {
1373 for (j = 1; j < AR512_NF_CAL_HIST_MAX-i; j ++) {
1374 if (sort[j] > sort[j-1]) {
1375 int16_t nf = sort[j];
1376 sort[j] = sort[j-1];
1381 return sort[(AR512_NF_CAL_HIST_MAX-1)>>1];
1385 * Read the NF and check it against the noise floor threshhold
1388 ar5212GetNf(struct ath_hal *ah, struct ieee80211_channel *chan)
1390 struct ath_hal_5212 *ahp = AH5212(ah);
1391 struct ar5212NfCalHist *h = &ahp->ah_nfCalHist;
1392 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
1393 int16_t nf, nfThresh;
1396 if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
1397 HALDEBUG(ah, HAL_DEBUG_ANY,
1398 "%s: NF did not complete in calibration window\n", __func__);
1399 ichan->rawNoiseFloor = h->privNF; /* most recent value */
1400 return ichan->rawNoiseFloor;
1404 * Finished NF cal, check against threshold.
1406 nf = ar5212GetNoiseFloor(ah);
1407 if (getNoiseFloorThresh(ah, chan, &nfThresh)) {
1408 if (nf > nfThresh) {
1409 HALDEBUG(ah, HAL_DEBUG_ANY,
1410 "%s: noise floor failed detected; detected %u, "
1411 "threshold %u\n", __func__, nf, nfThresh);
1413 * NB: Don't discriminate 2.4 vs 5Ghz, if this
1414 * happens it indicates a problem regardless
1417 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
1424 * Pass through histogram and write median value as
1425 * calculated from the accrued window. We require a
1426 * full window of in-range values to be seen before we
1427 * start using the history.
1429 updateNFHistBuff(h, nf);
1431 if (nf < AR5212_CCA_MIN_BAD_VALUE ||
1432 nf > AR5212_CCA_MAX_HIGH_VALUE) {
1433 nf = AR5212_CCA_MAX_GOOD_VALUE;
1434 h->invalidNFcount = AR512_NF_CAL_HIST_MAX;
1435 } else if (--(h->invalidNFcount) == 0) {
1437 h->privNF = nf = ar5212GetNfHistMid(h->nfCalBuffer);
1439 nf = AR5212_CCA_MAX_GOOD_VALUE;
1442 h->privNF = nf = ar5212GetNfHistMid(h->nfCalBuffer);
1445 val = OS_REG_READ(ah, AR_PHY(25));
1447 val |= (((uint32_t)nf << 1) & 0x1FF);
1448 OS_REG_WRITE(ah, AR_PHY(25), val);
1449 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
1450 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
1451 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
1453 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF, 0)) {
1455 ath_hal_printf(ah, "%s: AGC not ready AGC_CONTROL 0x%x\n",
1456 __func__, OS_REG_READ(ah, AR_PHY_AGC_CONTROL));
1461 * Now load a high maxCCAPower value again so that we're
1462 * not capped by the median we just loaded
1465 val |= (((uint32_t)(-50) << 1) & 0x1FF);
1466 OS_REG_WRITE(ah, AR_PHY(25), val);
1467 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
1468 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
1469 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
1471 return (ichan->rawNoiseFloor = nf);
1475 * Set up compression configuration registers
1478 ar5212SetCompRegs(struct ath_hal *ah)
1480 struct ath_hal_5212 *ahp = AH5212(ah);
1483 /* Check if h/w supports compression */
1484 if (!AH_PRIVATE(ah)->ah_caps.halCompressSupport)
1487 OS_REG_WRITE(ah, AR_DCCFG, 1);
1489 OS_REG_WRITE(ah, AR_CCFG,
1490 (AR_COMPRESSION_WINDOW_SIZE >> 8) & AR_CCFG_WIN_M);
1492 OS_REG_WRITE(ah, AR_CCFG,
1493 OS_REG_READ(ah, AR_CCFG) | AR_CCFG_MIB_INT_EN);
1494 OS_REG_WRITE(ah, AR_CCUCFG,
1495 AR_CCUCFG_RESET_VAL | AR_CCUCFG_CATCHUP_EN);
1497 OS_REG_WRITE(ah, AR_CPCOVF, 0);
1499 /* reset decompression mask */
1500 for (i = 0; i < HAL_DECOMP_MASK_SIZE; i++) {
1501 OS_REG_WRITE(ah, AR_DCM_A, i);
1502 OS_REG_WRITE(ah, AR_DCM_D, ahp->ah_decompMask[i]);
1507 ar5212SetAntennaSwitchInternal(struct ath_hal *ah, HAL_ANT_SETTING settings,
1508 const struct ieee80211_channel *chan)
1510 #define ANT_SWITCH_TABLE1 AR_PHY(88)
1511 #define ANT_SWITCH_TABLE2 AR_PHY(89)
1512 struct ath_hal_5212 *ahp = AH5212(ah);
1513 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
1514 uint32_t antSwitchA, antSwitchB;
1517 HALASSERT(ah->ah_magic == AR5212_MAGIC);
1518 HALASSERT(ahp->ah_phyPowerOn);
1520 switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
1521 case IEEE80211_CHAN_A:
1524 case IEEE80211_CHAN_G:
1525 case IEEE80211_CHAN_PUREG: /* NB: 108G */
1528 case IEEE80211_CHAN_B:
1529 if (IS_2425(ah) || IS_2417(ah)) {
1530 /* NB: Nala/Swan: 11b is handled using 11g */
1536 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
1537 __func__, chan->ic_flags);
1541 antSwitchA = ee->ee_antennaControl[1][ix]
1542 | (ee->ee_antennaControl[2][ix] << 6)
1543 | (ee->ee_antennaControl[3][ix] << 12)
1544 | (ee->ee_antennaControl[4][ix] << 18)
1545 | (ee->ee_antennaControl[5][ix] << 24)
1547 antSwitchB = ee->ee_antennaControl[6][ix]
1548 | (ee->ee_antennaControl[7][ix] << 6)
1549 | (ee->ee_antennaControl[8][ix] << 12)
1550 | (ee->ee_antennaControl[9][ix] << 18)
1551 | (ee->ee_antennaControl[10][ix] << 24)
1554 * For fixed antenna, give the same setting for both switch banks
1557 case HAL_ANT_FIXED_A:
1558 antSwitchB = antSwitchA;
1560 case HAL_ANT_FIXED_B:
1561 antSwitchA = antSwitchB;
1563 case HAL_ANT_VARIABLE:
1566 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad antenna setting %u\n",
1567 __func__, settings);
1570 if (antSwitchB == antSwitchA) {
1571 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
1572 "%s: Setting fast diversity off.\n", __func__);
1573 OS_REG_CLR_BIT(ah,AR_PHY_CCK_DETECT,
1574 AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
1575 ahp->ah_diversity = AH_FALSE;
1577 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
1578 "%s: Setting fast diversity on.\n", __func__);
1579 OS_REG_SET_BIT(ah,AR_PHY_CCK_DETECT,
1580 AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
1581 ahp->ah_diversity = AH_TRUE;
1583 ahp->ah_antControl = settings;
1585 OS_REG_WRITE(ah, ANT_SWITCH_TABLE1, antSwitchA);
1586 OS_REG_WRITE(ah, ANT_SWITCH_TABLE2, antSwitchB);
1589 #undef ANT_SWITCH_TABLE2
1590 #undef ANT_SWITCH_TABLE1
1594 ar5212IsSpurChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
1596 uint16_t freq = ath_hal_gethwchannel(ah, chan);
1597 uint32_t clockFreq =
1598 ((IS_5413(ah) || IS_RAD5112_ANY(ah) || IS_2417(ah)) ? 40 : 32);
1599 return ( ((freq % clockFreq) != 0)
1600 && (((freq % clockFreq) < 10)
1601 || (((freq) % clockFreq) > 22)) );
1605 * Read EEPROM header info and program the device for correct operation
1606 * given the channel value.
1609 ar5212SetBoardValues(struct ath_hal *ah, const struct ieee80211_channel *chan)
1611 #define NO_FALSE_DETECT_BACKOFF 2
1612 #define CB22_FALSE_DETECT_BACKOFF 6
1613 #define AR_PHY_BIS(_ah, _reg, _mask, _val) \
1614 OS_REG_WRITE(_ah, AR_PHY(_reg), \
1615 (OS_REG_READ(_ah, AR_PHY(_reg)) & _mask) | (_val));
1616 struct ath_hal_5212 *ahp = AH5212(ah);
1617 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
1618 int arrayMode, falseDectectBackoff;
1619 int is2GHz = IEEE80211_IS_CHAN_2GHZ(chan);
1620 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
1621 int8_t adcDesiredSize, pgaDesiredSize;
1622 uint16_t switchSettling, txrxAtten, rxtxMargin;
1625 HALASSERT(ah->ah_magic == AR5212_MAGIC);
1627 switch (chan->ic_flags & IEEE80211_CHAN_ALLTURBOFULL) {
1628 case IEEE80211_CHAN_A:
1629 case IEEE80211_CHAN_ST:
1630 arrayMode = headerInfo11A;
1631 if (!IS_RAD5112_ANY(ah) && !IS_2413(ah) && !IS_5413(ah))
1632 OS_REG_RMW_FIELD(ah, AR_PHY_FRAME_CTL,
1633 AR_PHY_FRAME_CTL_TX_CLIP,
1634 ahp->ah_gainValues.currStep->paramVal[GP_TXCLIP]);
1636 case IEEE80211_CHAN_B:
1637 arrayMode = headerInfo11B;
1639 case IEEE80211_CHAN_G:
1640 case IEEE80211_CHAN_108G:
1641 arrayMode = headerInfo11G;
1644 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
1645 __func__, chan->ic_flags);
1649 /* Set the antenna register(s) correctly for the chip revision */
1650 AR_PHY_BIS(ah, 68, 0xFFFFFC06,
1651 (ee->ee_antennaControl[0][arrayMode] << 4) | 0x1);
1653 ar5212SetAntennaSwitchInternal(ah, ahp->ah_antControl, chan);
1655 /* Set the Noise Floor Thresh on ar5211 devices */
1656 OS_REG_WRITE(ah, AR_PHY(90),
1657 (ee->ee_noiseFloorThresh[arrayMode] & 0x1FF)
1660 if (ee->ee_version >= AR_EEPROM_VER5_0 && IEEE80211_IS_CHAN_TURBO(chan)) {
1661 switchSettling = ee->ee_switchSettlingTurbo[is2GHz];
1662 adcDesiredSize = ee->ee_adcDesiredSizeTurbo[is2GHz];
1663 pgaDesiredSize = ee->ee_pgaDesiredSizeTurbo[is2GHz];
1664 txrxAtten = ee->ee_txrxAttenTurbo[is2GHz];
1665 rxtxMargin = ee->ee_rxtxMarginTurbo[is2GHz];
1667 switchSettling = ee->ee_switchSettling[arrayMode];
1668 adcDesiredSize = ee->ee_adcDesiredSize[arrayMode];
1669 pgaDesiredSize = ee->ee_pgaDesiredSize[is2GHz];
1670 txrxAtten = ee->ee_txrxAtten[is2GHz];
1671 rxtxMargin = ee->ee_rxtxMargin[is2GHz];
1674 OS_REG_RMW_FIELD(ah, AR_PHY_SETTLING,
1675 AR_PHY_SETTLING_SWITCH, switchSettling);
1676 OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
1677 AR_PHY_DESIRED_SZ_ADC, adcDesiredSize);
1678 OS_REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
1679 AR_PHY_DESIRED_SZ_PGA, pgaDesiredSize);
1680 OS_REG_RMW_FIELD(ah, AR_PHY_RXGAIN,
1681 AR_PHY_RXGAIN_TXRX_ATTEN, txrxAtten);
1682 OS_REG_WRITE(ah, AR_PHY(13),
1683 (ee->ee_txEndToXPAOff[arrayMode] << 24)
1684 | (ee->ee_txEndToXPAOff[arrayMode] << 16)
1685 | (ee->ee_txFrameToXPAOn[arrayMode] << 8)
1686 | ee->ee_txFrameToXPAOn[arrayMode]);
1687 AR_PHY_BIS(ah, 10, 0xFFFF00FF,
1688 ee->ee_txEndToXLNAOn[arrayMode] << 8);
1689 AR_PHY_BIS(ah, 25, 0xFFF80FFF,
1690 (ee->ee_thresh62[arrayMode] << 12) & 0x7F000);
1693 * False detect backoff - suspected 32 MHz spur causes false
1694 * detects in OFDM, causing Tx Hangs. Decrease weak signal
1695 * sensitivity for this card.
1697 falseDectectBackoff = NO_FALSE_DETECT_BACKOFF;
1698 if (ee->ee_version < AR_EEPROM_VER3_3) {
1699 /* XXX magic number */
1700 if (AH_PRIVATE(ah)->ah_subvendorid == 0x1022 &&
1701 IEEE80211_IS_CHAN_OFDM(chan))
1702 falseDectectBackoff += CB22_FALSE_DETECT_BACKOFF;
1704 if (ar5212IsSpurChannel(ah, chan))
1705 falseDectectBackoff += ee->ee_falseDetectBackoff[arrayMode];
1707 AR_PHY_BIS(ah, 73, 0xFFFFFF01, (falseDectectBackoff << 1) & 0xFE);
1709 if (ichan->privFlags & CHANNEL_IQVALID) {
1710 iCoff = ichan->iCoff;
1711 qCoff = ichan->qCoff;
1713 iCoff = ee->ee_iqCalI[is2GHz];
1714 qCoff = ee->ee_iqCalQ[is2GHz];
1717 /* write previous IQ results */
1718 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
1719 AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
1720 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4,
1721 AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
1722 OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
1723 AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
1725 if (ee->ee_version >= AR_EEPROM_VER4_1) {
1726 if (!IEEE80211_IS_CHAN_108G(chan) || ee->ee_version >= AR_EEPROM_VER5_0)
1727 OS_REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ,
1728 AR_PHY_GAIN_2GHZ_RXTX_MARGIN, rxtxMargin);
1730 if (ee->ee_version >= AR_EEPROM_VER5_1) {
1731 /* for now always disabled */
1732 OS_REG_WRITE(ah, AR_PHY_HEAVY_CLIP_ENABLE, 0);
1737 #undef NO_FALSE_DETECT_BACKOFF
1738 #undef CB22_FALSE_DETECT_BACKOFF
1742 * Apply Spur Immunity to Boards that require it.
1743 * Applies only to OFDM RX operation.
1747 ar5212SetSpurMitigation(struct ath_hal *ah,
1748 const struct ieee80211_channel *chan)
1750 uint32_t pilotMask[2] = {0, 0}, binMagMask[4] = {0, 0, 0 , 0};
1751 uint16_t i, finalSpur, curChanAsSpur, binWidth = 0, spurDetectWidth, spurChan;
1752 int32_t spurDeltaPhase = 0, spurFreqSd = 0, spurOffset, binOffsetNumT16, curBinOffset;
1753 int16_t numBinOffsets;
1754 static const uint16_t magMapFor4[4] = {1, 2, 2, 1};
1755 static const uint16_t magMapFor3[3] = {1, 2, 1};
1756 const uint16_t *pMagMap;
1757 HAL_BOOL is2GHz = IEEE80211_IS_CHAN_2GHZ(chan);
1758 HAL_CHANNEL_INTERNAL *ichan = ath_hal_checkchannel(ah, chan);
1761 #define CHAN_TO_SPUR(_f, _freq) ( ((_freq) - ((_f) ? 2300 : 4900)) * 10 )
1763 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: no spur mitigation\n",
1768 curChanAsSpur = CHAN_TO_SPUR(is2GHz, ichan->channel);
1770 if (ichan->mainSpur) {
1771 /* Pull out the saved spur value */
1772 finalSpur = ichan->mainSpur;
1775 * Check if spur immunity should be performed for this channel
1776 * Should only be performed once per channel and then saved
1778 finalSpur = AR_NO_SPUR;
1779 spurDetectWidth = HAL_SPUR_CHAN_WIDTH;
1780 if (IEEE80211_IS_CHAN_TURBO(chan))
1781 spurDetectWidth *= 2;
1783 /* Decide if any spur affects the current channel */
1784 for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
1785 spurChan = ath_hal_getSpurChan(ah, i, is2GHz);
1786 if (spurChan == AR_NO_SPUR) {
1789 if ((curChanAsSpur - spurDetectWidth <= (spurChan & HAL_SPUR_VAL_MASK)) &&
1790 (curChanAsSpur + spurDetectWidth >= (spurChan & HAL_SPUR_VAL_MASK))) {
1791 finalSpur = spurChan & HAL_SPUR_VAL_MASK;
1795 /* Save detected spur (or no spur) for this channel */
1796 ichan->mainSpur = finalSpur;
1799 /* Write spur immunity data */
1800 if (finalSpur == AR_NO_SPUR) {
1801 /* Disable Spur Immunity Regs if they appear set */
1802 if (OS_REG_READ(ah, AR_PHY_TIMING_CTRL4) & AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER) {
1803 /* Clear Spur Delta Phase, Spur Freq, and enable bits */
1804 OS_REG_RMW_FIELD(ah, AR_PHY_MASK_CTL, AR_PHY_MASK_CTL_RATE, 0);
1805 val = OS_REG_READ(ah, AR_PHY_TIMING_CTRL4);
1806 val &= ~(AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
1807 AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
1808 AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
1809 OS_REG_WRITE(ah, AR_PHY_MASK_CTL, val);
1810 OS_REG_WRITE(ah, AR_PHY_TIMING11, 0);
1812 /* Clear pilot masks */
1813 OS_REG_WRITE(ah, AR_PHY_TIMING7, 0);
1814 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING8, AR_PHY_TIMING8_PILOT_MASK_2, 0);
1815 OS_REG_WRITE(ah, AR_PHY_TIMING9, 0);
1816 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING10, AR_PHY_TIMING10_PILOT_MASK_2, 0);
1818 /* Clear magnitude masks */
1819 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_1, 0);
1820 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_2, 0);
1821 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_3, 0);
1822 OS_REG_RMW_FIELD(ah, AR_PHY_MASK_CTL, AR_PHY_MASK_CTL_MASK_4, 0);
1823 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_1, 0);
1824 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_2, 0);
1825 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_3, 0);
1826 OS_REG_RMW_FIELD(ah, AR_PHY_BIN_MASK2_4, AR_PHY_BIN_MASK2_4_MASK_4, 0);
1829 spurOffset = finalSpur - curChanAsSpur;
1831 * Spur calculations:
1832 * spurDeltaPhase is (spurOffsetIn100KHz / chipFrequencyIn100KHz) << 21
1833 * spurFreqSd is (spurOffsetIn100KHz / sampleFrequencyIn100KHz) << 11
1835 if (IEEE80211_IS_CHAN_TURBO(chan)) {
1836 /* Chip Frequency & sampleFrequency are 80 MHz */
1837 spurDeltaPhase = (spurOffset << 16) / 25;
1838 spurFreqSd = spurDeltaPhase >> 10;
1839 binWidth = HAL_BIN_WIDTH_TURBO_100HZ;
1840 } else if (IEEE80211_IS_CHAN_G(chan)) {
1841 /* Chip Frequency is 44MHz, sampleFrequency is 40 MHz */
1842 spurFreqSd = (spurOffset << 8) / 55;
1843 spurDeltaPhase = (spurOffset << 17) / 25;
1844 binWidth = HAL_BIN_WIDTH_BASE_100HZ;
1846 HALASSERT(!IEEE80211_IS_CHAN_B(chan));
1847 /* Chip Frequency & sampleFrequency are 40 MHz */
1848 spurDeltaPhase = (spurOffset << 17) / 25;
1849 spurFreqSd = spurDeltaPhase >> 10;
1850 binWidth = HAL_BIN_WIDTH_BASE_100HZ;
1853 /* Compute Pilot Mask */
1854 binOffsetNumT16 = ((spurOffset * 1000) << 4) / binWidth;
1855 /* The spur is on a bin if it's remainder at times 16 is 0 */
1856 if (binOffsetNumT16 & 0xF) {
1858 pMagMap = magMapFor4;
1861 pMagMap = magMapFor3;
1863 for (i = 0; i < numBinOffsets; i++) {
1864 if ((binOffsetNumT16 >> 4) > HAL_MAX_BINS_ALLOWED) {
1865 HALDEBUG(ah, HAL_DEBUG_ANY,
1866 "Too man bins in spur mitigation\n");
1870 /* Get Pilot Mask values */
1871 curBinOffset = (binOffsetNumT16 >> 4) + i + 25;
1872 if ((curBinOffset >= 0) && (curBinOffset <= 32)) {
1873 if (curBinOffset <= 25)
1874 pilotMask[0] |= 1 << curBinOffset;
1875 else if (curBinOffset >= 27)
1876 pilotMask[0] |= 1 << (curBinOffset - 1);
1877 } else if ((curBinOffset >= 33) && (curBinOffset <= 52))
1878 pilotMask[1] |= 1 << (curBinOffset - 33);
1880 /* Get viterbi values */
1881 if ((curBinOffset >= -1) && (curBinOffset <= 14))
1882 binMagMask[0] |= pMagMap[i] << (curBinOffset + 1) * 2;
1883 else if ((curBinOffset >= 15) && (curBinOffset <= 30))
1884 binMagMask[1] |= pMagMap[i] << (curBinOffset - 15) * 2;
1885 else if ((curBinOffset >= 31) && (curBinOffset <= 46))
1886 binMagMask[2] |= pMagMap[i] << (curBinOffset -31) * 2;
1887 else if((curBinOffset >= 47) && (curBinOffset <= 53))
1888 binMagMask[3] |= pMagMap[i] << (curBinOffset -47) * 2;
1891 /* Write Spur Delta Phase, Spur Freq, and enable bits */
1892 OS_REG_RMW_FIELD(ah, AR_PHY_MASK_CTL, AR_PHY_MASK_CTL_RATE, 0xFF);
1893 val = OS_REG_READ(ah, AR_PHY_TIMING_CTRL4);
1894 val |= (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
1895 AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
1896 AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
1897 OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4, val);
1898 OS_REG_WRITE(ah, AR_PHY_TIMING11, AR_PHY_TIMING11_USE_SPUR_IN_AGC |
1899 SM(spurFreqSd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
1900 SM(spurDeltaPhase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
1902 /* Write pilot masks */
1903 OS_REG_WRITE(ah, AR_PHY_TIMING7, pilotMask[0]);
1904 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING8, AR_PHY_TIMING8_PILOT_MASK_2, pilotMask[1]);
1905 OS_REG_WRITE(ah, AR_PHY_TIMING9, pilotMask[0]);
1906 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING10, AR_PHY_TIMING10_PILOT_MASK_2, pilotMask[1]);
1908 /* Write magnitude masks */
1909 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_1, binMagMask[0]);
1910 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_2, binMagMask[1]);
1911 OS_REG_WRITE(ah, AR_PHY_BIN_MASK_3, binMagMask[2]);
1912 OS_REG_RMW_FIELD(ah, AR_PHY_MASK_CTL, AR_PHY_MASK_CTL_MASK_4, binMagMask[3]);
1913 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_1, binMagMask[0]);
1914 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_2, binMagMask[1]);
1915 OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_3, binMagMask[2]);
1916 OS_REG_RMW_FIELD(ah, AR_PHY_BIN_MASK2_4, AR_PHY_BIN_MASK2_4_MASK_4, binMagMask[3]);
1923 * Delta slope coefficient computation.
1924 * Required for OFDM operation.
1927 ar5212SetDeltaSlope(struct ath_hal *ah, const struct ieee80211_channel *chan)
1929 #define COEF_SCALE_S 24
1930 #define INIT_CLOCKMHZSCALED 0x64000000
1931 uint16_t freq = ath_hal_gethwchannel(ah, chan);
1932 unsigned long coef_scaled, coef_exp, coef_man, ds_coef_exp, ds_coef_man;
1933 unsigned long clockMhzScaled = INIT_CLOCKMHZSCALED;
1935 if (IEEE80211_IS_CHAN_TURBO(chan))
1936 clockMhzScaled *= 2;
1937 /* half and quarter rate can divide the scaled clock by 2 or 4 respectively */
1938 /* scale for selected channel bandwidth */
1939 if (IEEE80211_IS_CHAN_HALF(chan)) {
1940 clockMhzScaled = clockMhzScaled >> 1;
1941 } else if (IEEE80211_IS_CHAN_QUARTER(chan)) {
1942 clockMhzScaled = clockMhzScaled >> 2;
1946 * ALGO -> coef = 1e8/fcarrier*fclock/40;
1947 * scaled coef to provide precision for this floating calculation
1949 coef_scaled = clockMhzScaled / freq;
1952 * ALGO -> coef_exp = 14-floor(log2(coef));
1953 * floor(log2(x)) is the highest set bit position
1955 for (coef_exp = 31; coef_exp > 0; coef_exp--)
1956 if ((coef_scaled >> coef_exp) & 0x1)
1958 /* A coef_exp of 0 is a legal bit position but an unexpected coef_exp */
1959 HALASSERT(coef_exp);
1960 coef_exp = 14 - (coef_exp - COEF_SCALE_S);
1963 * ALGO -> coef_man = floor(coef* 2^coef_exp+0.5);
1964 * The coefficient is already shifted up for scaling
1966 coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
1967 ds_coef_man = coef_man >> (COEF_SCALE_S - coef_exp);
1968 ds_coef_exp = coef_exp - 16;
1970 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING3,
1971 AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
1972 OS_REG_RMW_FIELD(ah, AR_PHY_TIMING3,
1973 AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
1974 #undef INIT_CLOCKMHZSCALED
1979 * Set a limit on the overall output power. Used for dynamic
1980 * transmit power control and the like.
1982 * NB: limit is in units of 0.5 dbM.
1985 ar5212SetTxPowerLimit(struct ath_hal *ah, uint32_t limit)
1987 /* XXX blech, construct local writable copy */
1988 struct ieee80211_channel dummy = *AH_PRIVATE(ah)->ah_curchan;
1989 uint16_t dummyXpdGains[2];
1992 SAVE_CCK(ah, &dummy, isBmode);
1993 AH_PRIVATE(ah)->ah_powerLimit = AH_MIN(limit, MAX_RATE_POWER);
1994 return ar5212SetTransmitPower(ah, &dummy, dummyXpdGains);
1998 * Set the transmit power in the baseband for the given
1999 * operating channel and mode.
2002 ar5212SetTransmitPower(struct ath_hal *ah,
2003 const struct ieee80211_channel *chan, uint16_t *rfXpdGain)
2005 #define POW_OFDM(_r, _s) (((0 & 1)<< ((_s)+6)) | (((_r) & 0x3f) << (_s)))
2006 #define POW_CCK(_r, _s) (((_r) & 0x3f) << (_s))
2007 #define N(a) (sizeof (a) / sizeof (a[0]))
2008 static const uint16_t tpcScaleReductionTable[5] =
2009 { 0, 3, 6, 9, MAX_RATE_POWER };
2010 struct ath_hal_5212 *ahp = AH5212(ah);
2011 uint16_t freq = ath_hal_gethwchannel(ah, chan);
2012 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
2013 int16_t minPower, maxPower, tpcInDb, powerLimit;
2016 HALASSERT(ah->ah_magic == AR5212_MAGIC);
2018 OS_MEMZERO(ahp->ah_pcdacTable, ahp->ah_pcdacTableSize);
2019 OS_MEMZERO(ahp->ah_ratesArray, sizeof(ahp->ah_ratesArray));
2021 powerLimit = AH_MIN(MAX_RATE_POWER, AH_PRIVATE(ah)->ah_powerLimit);
2022 if (powerLimit >= MAX_RATE_POWER || powerLimit == 0)
2023 tpcInDb = tpcScaleReductionTable[AH_PRIVATE(ah)->ah_tpScale];
2026 if (!ar5212SetRateTable(ah, chan, tpcInDb, powerLimit,
2027 AH_TRUE, &minPower, &maxPower)) {
2028 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set rate table\n",
2032 if (!ahp->ah_rfHal->setPowerTable(ah,
2033 &minPower, &maxPower, chan, rfXpdGain)) {
2034 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set power table\n",
2040 * Adjust XR power/rate up by 2 dB to account for greater peak
2041 * to avg ratio - except in newer avg power designs
2043 if (!IS_2413(ah) && !IS_5413(ah))
2044 ahp->ah_ratesArray[15] += 4;
2046 * txPowerIndexOffset is set by the SetPowerTable() call -
2047 * adjust the rate table
2049 for (i = 0; i < N(ahp->ah_ratesArray); i++) {
2050 ahp->ah_ratesArray[i] += ahp->ah_txPowerIndexOffset;
2051 if (ahp->ah_ratesArray[i] > 63)
2052 ahp->ah_ratesArray[i] = 63;
2055 if (ee->ee_eepMap < 2) {
2057 * Correct gain deltas for 5212 G operation -
2058 * Removed with revised chipset
2060 if (AH_PRIVATE(ah)->ah_phyRev < AR_PHY_CHIP_ID_REV_2 &&
2061 IEEE80211_IS_CHAN_G(chan)) {
2062 uint16_t cckOfdmPwrDelta;
2065 cckOfdmPwrDelta = SCALE_OC_DELTA(
2066 ee->ee_cckOfdmPwrDelta -
2067 ee->ee_scaledCh14FilterCckDelta);
2069 cckOfdmPwrDelta = SCALE_OC_DELTA(
2070 ee->ee_cckOfdmPwrDelta);
2071 ar5212CorrectGainDelta(ah, cckOfdmPwrDelta);
2074 * Finally, write the power values into the
2075 * baseband power table
2077 for (i = 0; i < (PWR_TABLE_SIZE/2); i++) {
2078 OS_REG_WRITE(ah, AR_PHY_PCDAC_TX_POWER(i),
2079 ((((ahp->ah_pcdacTable[2*i + 1] << 8) | 0xff) & 0xffff) << 16)
2080 | (((ahp->ah_pcdacTable[2*i] << 8) | 0xff) & 0xffff)
2085 /* Write the OFDM power per rate set */
2086 OS_REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
2087 POW_OFDM(ahp->ah_ratesArray[3], 24)
2088 | POW_OFDM(ahp->ah_ratesArray[2], 16)
2089 | POW_OFDM(ahp->ah_ratesArray[1], 8)
2090 | POW_OFDM(ahp->ah_ratesArray[0], 0)
2092 OS_REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
2093 POW_OFDM(ahp->ah_ratesArray[7], 24)
2094 | POW_OFDM(ahp->ah_ratesArray[6], 16)
2095 | POW_OFDM(ahp->ah_ratesArray[5], 8)
2096 | POW_OFDM(ahp->ah_ratesArray[4], 0)
2099 /* Write the CCK power per rate set */
2100 OS_REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
2101 POW_CCK(ahp->ah_ratesArray[10], 24)
2102 | POW_CCK(ahp->ah_ratesArray[9], 16)
2103 | POW_CCK(ahp->ah_ratesArray[15], 8) /* XR target power */
2104 | POW_CCK(ahp->ah_ratesArray[8], 0)
2106 OS_REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
2107 POW_CCK(ahp->ah_ratesArray[14], 24)
2108 | POW_CCK(ahp->ah_ratesArray[13], 16)
2109 | POW_CCK(ahp->ah_ratesArray[12], 8)
2110 | POW_CCK(ahp->ah_ratesArray[11], 0)
2114 * Set max power to 30 dBm and, optionally,
2115 * enable TPC in tx descriptors.
2117 OS_REG_WRITE(ah, AR_PHY_POWER_TX_RATE_MAX, MAX_RATE_POWER |
2118 (ahp->ah_tpcEnabled ? AR_PHY_POWER_TX_RATE_MAX_TPC_ENABLE : 0));
2127 * Sets the transmit power in the baseband for the given
2128 * operating channel and mode.
2131 ar5212SetRateTable(struct ath_hal *ah, const struct ieee80211_channel *chan,
2132 int16_t tpcScaleReduction, int16_t powerLimit, HAL_BOOL commit,
2133 int16_t *pMinPower, int16_t *pMaxPower)
2135 struct ath_hal_5212 *ahp = AH5212(ah);
2136 uint16_t freq = ath_hal_gethwchannel(ah, chan);
2137 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
2138 uint16_t *rpow = ahp->ah_ratesArray;
2139 uint16_t twiceMaxEdgePower = MAX_RATE_POWER;
2140 uint16_t twiceMaxEdgePowerCck = MAX_RATE_POWER;
2141 uint16_t twiceMaxRDPower = MAX_RATE_POWER;
2144 int8_t twiceAntennaGain, twiceAntennaReduction;
2145 const RD_EDGES_POWER *rep;
2146 TRGT_POWER_INFO targetPowerOfdm, targetPowerCck;
2147 int16_t scaledPower, maxAvailPower = 0;
2148 int16_t r13, r9, r7, r0;
2150 HALASSERT(ah->ah_magic == AR5212_MAGIC);
2152 twiceMaxRDPower = chan->ic_maxregpower * 2;
2153 *pMaxPower = -MAX_RATE_POWER;
2154 *pMinPower = MAX_RATE_POWER;
2156 /* Get conformance test limit maximum for this channel */
2157 cfgCtl = ath_hal_getctl(ah, chan);
2158 for (i = 0; i < ee->ee_numCtls; i++) {
2159 uint16_t twiceMinEdgePower;
2161 if (ee->ee_ctl[i] == 0)
2163 if (ee->ee_ctl[i] == cfgCtl ||
2164 cfgCtl == ((ee->ee_ctl[i] & CTL_MODE_M) | SD_NO_CTL)) {
2165 rep = &ee->ee_rdEdgesPower[i * NUM_EDGES];
2166 twiceMinEdgePower = ar5212GetMaxEdgePower(freq, rep);
2167 if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
2168 /* Find the minimum of all CTL edge powers that apply to this channel */
2169 twiceMaxEdgePower = AH_MIN(twiceMaxEdgePower, twiceMinEdgePower);
2171 twiceMaxEdgePower = twiceMinEdgePower;
2177 if (IEEE80211_IS_CHAN_G(chan)) {
2178 /* Check for a CCK CTL for 11G CCK powers */
2179 cfgCtl = (cfgCtl & ~CTL_MODE_M) | CTL_11B;
2180 for (i = 0; i < ee->ee_numCtls; i++) {
2181 uint16_t twiceMinEdgePowerCck;
2183 if (ee->ee_ctl[i] == 0)
2185 if (ee->ee_ctl[i] == cfgCtl ||
2186 cfgCtl == ((ee->ee_ctl[i] & CTL_MODE_M) | SD_NO_CTL)) {
2187 rep = &ee->ee_rdEdgesPower[i * NUM_EDGES];
2188 twiceMinEdgePowerCck = ar5212GetMaxEdgePower(freq, rep);
2189 if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
2190 /* Find the minimum of all CTL edge powers that apply to this channel */
2191 twiceMaxEdgePowerCck = AH_MIN(twiceMaxEdgePowerCck, twiceMinEdgePowerCck);
2193 twiceMaxEdgePowerCck = twiceMinEdgePowerCck;
2199 /* Set the 11B cck edge power to the one found before */
2200 twiceMaxEdgePowerCck = twiceMaxEdgePower;
2203 /* Get Antenna Gain reduction */
2204 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
2205 ath_hal_eepromGet(ah, AR_EEP_ANTGAINMAX_5, &twiceAntennaGain);
2207 ath_hal_eepromGet(ah, AR_EEP_ANTGAINMAX_2, &twiceAntennaGain);
2209 twiceAntennaReduction =
2210 ath_hal_getantennareduction(ah, chan, twiceAntennaGain);
2212 if (IEEE80211_IS_CHAN_OFDM(chan)) {
2213 /* Get final OFDM target powers */
2214 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
2215 ar5212GetTargetPowers(ah, chan, ee->ee_trgtPwr_11g,
2216 ee->ee_numTargetPwr_11g, &targetPowerOfdm);
2218 ar5212GetTargetPowers(ah, chan, ee->ee_trgtPwr_11a,
2219 ee->ee_numTargetPwr_11a, &targetPowerOfdm);
2222 /* Get Maximum OFDM power */
2223 /* Minimum of target and edge powers */
2224 scaledPower = AH_MIN(twiceMaxEdgePower,
2225 twiceMaxRDPower - twiceAntennaReduction);
2228 * If turbo is set, reduce power to keep power
2229 * consumption under 2 Watts. Note that we always do
2230 * this unless specially configured. Then we limit
2231 * power only for non-AP operation.
2233 if (IEEE80211_IS_CHAN_TURBO(chan)
2234 #ifdef AH_ENABLE_AP_SUPPORT
2235 && AH_PRIVATE(ah)->ah_opmode != HAL_M_HOSTAP
2239 * If turbo is set, reduce power to keep power
2240 * consumption under 2 Watts
2242 if (ee->ee_version >= AR_EEPROM_VER3_1)
2243 scaledPower = AH_MIN(scaledPower,
2244 ee->ee_turbo2WMaxPower5);
2246 * EEPROM version 4.0 added an additional
2247 * constraint on 2.4GHz channels.
2249 if (ee->ee_version >= AR_EEPROM_VER4_0 &&
2250 IEEE80211_IS_CHAN_2GHZ(chan))
2251 scaledPower = AH_MIN(scaledPower,
2252 ee->ee_turbo2WMaxPower2);
2255 maxAvailPower = AH_MIN(scaledPower,
2256 targetPowerOfdm.twicePwr6_24);
2258 /* Reduce power by max regulatory domain allowed restrictions */
2259 scaledPower = maxAvailPower - (tpcScaleReduction * 2);
2260 scaledPower = (scaledPower < 0) ? 0 : scaledPower;
2261 scaledPower = AH_MIN(scaledPower, powerLimit);
2264 /* Set OFDM rates 9, 12, 18, 24 */
2265 r0 = rpow[0] = rpow[1] = rpow[2] = rpow[3] = rpow[4] = scaledPower;
2267 /* Set OFDM rates 36, 48, 54, XR */
2268 rpow[5] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr36);
2269 rpow[6] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr48);
2270 r7 = rpow[7] = AH_MIN(rpow[0], targetPowerOfdm.twicePwr54);
2272 if (ee->ee_version >= AR_EEPROM_VER4_0) {
2273 /* Setup XR target power from EEPROM */
2274 rpow[15] = AH_MIN(scaledPower, IEEE80211_IS_CHAN_2GHZ(chan) ?
2275 ee->ee_xrTargetPower2 : ee->ee_xrTargetPower5);
2277 /* XR uses 6mb power */
2280 ahp->ah_ofdmTxPower = *pMaxPower;
2284 r7 = AH_MIN(r0, targetPowerOfdm.twicePwr54);
2289 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
2290 "%s: MaxRD: %d TurboMax: %d MaxCTL: %d "
2291 "TPC_Reduction %d chan=%d (0x%x) maxAvailPower=%d pwr6_24=%d, maxPower=%d\n",
2292 __func__, twiceMaxRDPower, ee->ee_turbo2WMaxPower5,
2293 twiceMaxEdgePower, tpcScaleReduction * 2,
2294 chan->ic_freq, chan->ic_flags,
2295 maxAvailPower, targetPowerOfdm.twicePwr6_24, *pMaxPower);
2298 if (IEEE80211_IS_CHAN_CCK(chan)) {
2299 /* Get final CCK target powers */
2300 ar5212GetTargetPowers(ah, chan, ee->ee_trgtPwr_11b,
2301 ee->ee_numTargetPwr_11b, &targetPowerCck);
2303 /* Reduce power by max regulatory domain allowed restrictions */
2304 scaledPower = AH_MIN(twiceMaxEdgePowerCck,
2305 twiceMaxRDPower - twiceAntennaReduction);
2306 if (maxAvailPower < AH_MIN(scaledPower, targetPowerCck.twicePwr6_24))
2307 maxAvailPower = AH_MIN(scaledPower, targetPowerCck.twicePwr6_24);
2309 /* Reduce power by user selection */
2310 scaledPower = AH_MIN(scaledPower, targetPowerCck.twicePwr6_24) - (tpcScaleReduction * 2);
2311 scaledPower = (scaledPower < 0) ? 0 : scaledPower;
2312 scaledPower = AH_MIN(scaledPower, powerLimit);
2315 /* Set CCK rates 2L, 2S, 5.5L, 5.5S, 11L, 11S */
2316 rpow[8] = AH_MIN(scaledPower, targetPowerCck.twicePwr6_24);
2317 r9 = rpow[9] = AH_MIN(scaledPower, targetPowerCck.twicePwr36);
2319 rpow[11] = AH_MIN(scaledPower, targetPowerCck.twicePwr48);
2320 rpow[12] = rpow[11];
2321 r13 = rpow[13] = AH_MIN(scaledPower, targetPowerCck.twicePwr54);
2322 rpow[14] = rpow[13];
2324 r9 = AH_MIN(scaledPower, targetPowerCck.twicePwr36);
2325 r13 = AH_MIN(scaledPower, targetPowerCck.twicePwr54);
2328 /* Set min/max power based off OFDM values or initialization */
2329 if (r13 < *pMinPower)
2331 if (r9 > *pMaxPower)
2334 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
2335 "%s: cck: MaxRD: %d MaxCTL: %d "
2336 "TPC_Reduction %d chan=%d (0x%x) maxAvailPower=%d pwr6_24=%d, maxPower=%d\n",
2337 __func__, twiceMaxRDPower, twiceMaxEdgePowerCck,
2338 tpcScaleReduction * 2, chan->ic_freq, chan->ic_flags,
2339 maxAvailPower, targetPowerCck.twicePwr6_24, *pMaxPower);
2342 ahp->ah_tx6PowerInHalfDbm = *pMaxPower;
2343 AH_PRIVATE(ah)->ah_maxPowerLevel = ahp->ah_tx6PowerInHalfDbm;
2349 ar5212GetChipPowerLimits(struct ath_hal *ah, struct ieee80211_channel *chan)
2351 struct ath_hal_5212 *ahp = AH5212(ah);
2353 static const uint16_t tpcScaleReductionTable[5] =
2354 { 0, 3, 6, 9, MAX_RATE_POWER };
2355 int16_t tpcInDb, powerLimit;
2357 int16_t minPower, maxPower;
2360 * Get Pier table max and min powers.
2362 if (ahp->ah_rfHal->getChannelMaxMinPower(ah, chan, &maxPower, &minPower)) {
2363 /* NB: rf code returns 1/4 dBm units, convert */
2364 chan->ic_maxpower = maxPower / 2;
2365 chan->ic_minpower = minPower / 2;
2367 HALDEBUG(ah, HAL_DEBUG_ANY,
2368 "%s: no min/max power for %u/0x%x\n",
2369 __func__, chan->ic_freq, chan->ic_flags);
2370 chan->ic_maxpower = MAX_RATE_POWER;
2371 chan->ic_minpower = 0;
2375 * Now adjust to reflect any global scale and/or CTL's.
2376 * (XXX is that correct?)
2378 powerLimit = AH_MIN(MAX_RATE_POWER, AH_PRIVATE(ah)->ah_powerLimit);
2379 if (powerLimit >= MAX_RATE_POWER || powerLimit == 0)
2380 tpcInDb = tpcScaleReductionTable[AH_PRIVATE(ah)->ah_tpScale];
2383 if (!ar5212SetRateTable(ah, chan, tpcInDb, powerLimit,
2384 AH_FALSE, &minPower, &maxPower)) {
2385 HALDEBUG(ah, HAL_DEBUG_ANY,
2386 "%s: unable to find max/min power\n",__func__);
2389 if (maxPower < chan->ic_maxpower)
2390 chan->ic_maxpower = maxPower;
2391 if (minPower < chan->ic_minpower)
2392 chan->ic_minpower = minPower;
2393 HALDEBUG(ah, HAL_DEBUG_RESET,
2394 "Chan %d: MaxPow = %d MinPow = %d\n",
2395 chan->ic_freq, chan->ic_maxpower, chans->ic_minpower);
2401 * Correct for the gain-delta between ofdm and cck mode target
2402 * powers. Write the results to the rate table and the power table.
2405 * 1. rpow[ii] is the integer value of 2*(desired power
2406 * for the rate ii in dBm) to provide 0.5dB resolution. rate
2407 * mapping is as following :
2408 * [0..7] --> ofdm 6, 9, .. 48, 54
2409 * [8..14] --> cck 1L, 2L, 2S, .. 11L, 11S
2410 * [15] --> XR (all rates get the same power)
2411 * 2. powv[ii] is the pcdac corresponding to ii/2 dBm.
2414 ar5212CorrectGainDelta(struct ath_hal *ah, int twiceOfdmCckDelta)
2416 #define N(_a) (sizeof(_a) / sizeof(_a[0]))
2417 struct ath_hal_5212 *ahp = AH5212(ah);
2418 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
2419 int16_t ratesIndex[N(ahp->ah_ratesArray)];
2420 uint16_t ii, jj, iter;
2422 int16_t gainDeltaAdjust;
2424 HALASSERT(ah->ah_magic == AR5212_MAGIC);
2426 gainDeltaAdjust = ee->ee_cckOfdmGainDelta;
2428 /* make a local copy of desired powers as initial indices */
2429 OS_MEMCPY(ratesIndex, ahp->ah_ratesArray, sizeof(ratesIndex));
2431 /* fix only the CCK indices */
2432 for (ii = 8; ii < 15; ii++) {
2433 /* apply a gain_delta correction of -15 for CCK */
2434 ratesIndex[ii] -= gainDeltaAdjust;
2436 /* Now check for contention with all ofdm target powers */
2439 /* indicates not all ofdm rates checked forcontention yet */
2441 if (ratesIndex[ii] < 0)
2443 if (jj == 8) { /* skip CCK rates */
2447 if (ratesIndex[ii] == ahp->ah_ratesArray[jj]) {
2448 if (ahp->ah_ratesArray[jj] == 0)
2450 else if (iter > 50) {
2452 * To avoid pathological case of of
2453 * dm target powers 0 and 0.5dBm
2458 /* check with all rates again */
2464 if (ratesIndex[ii] >= PWR_TABLE_SIZE)
2465 ratesIndex[ii] = PWR_TABLE_SIZE -1;
2466 cckIndex = ahp->ah_ratesArray[ii] - twiceOfdmCckDelta;
2471 * Validate that the indexes for the powv are not
2474 HALASSERT(cckIndex < PWR_TABLE_SIZE);
2475 HALASSERT(ratesIndex[ii] < PWR_TABLE_SIZE);
2476 ahp->ah_pcdacTable[ratesIndex[ii]] =
2477 ahp->ah_pcdacTable[cckIndex];
2479 /* Override rate per power table with new values */
2480 for (ii = 8; ii < 15; ii++)
2481 ahp->ah_ratesArray[ii] = ratesIndex[ii];
2486 * Find the maximum conformance test limit for the given channel and CTL info
2489 ar5212GetMaxEdgePower(uint16_t channel, const RD_EDGES_POWER *pRdEdgesPower)
2491 /* temp array for holding edge channels */
2492 uint16_t tempChannelList[NUM_EDGES];
2493 uint16_t clo, chi, twiceMaxEdgePower;
2496 /* Get the edge power */
2497 for (i = 0; i < NUM_EDGES; i++) {
2498 if (pRdEdgesPower[i].rdEdge == 0)
2500 tempChannelList[i] = pRdEdgesPower[i].rdEdge;
2504 ar5212GetLowerUpperValues(channel, tempChannelList,
2505 numEdges, &clo, &chi);
2506 /* Get the index for the lower channel */
2507 for (i = 0; i < numEdges && clo != tempChannelList[i]; i++)
2509 /* Is lower channel ever outside the rdEdge? */
2510 HALASSERT(i != numEdges);
2512 if ((clo == chi && clo == channel) || (pRdEdgesPower[i].flag)) {
2514 * If there's an exact channel match or an inband flag set
2515 * on the lower channel use the given rdEdgePower
2517 twiceMaxEdgePower = pRdEdgesPower[i].twice_rdEdgePower;
2518 HALASSERT(twiceMaxEdgePower > 0);
2520 twiceMaxEdgePower = MAX_RATE_POWER;
2521 return twiceMaxEdgePower;
2525 * Returns interpolated or the scaled up interpolated value
2528 interpolate(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
2529 uint16_t targetLeft, uint16_t targetRight)
2534 /* to get an accurate ratio, always scale, if want to scale, then don't scale back down */
2535 if ((targetLeft * targetRight) == 0)
2538 if (srcRight != srcLeft) {
2540 * Note the ratio always need to be scaled,
2541 * since it will be a fraction.
2543 lRatio = (target - srcLeft) * EEP_SCALE / (srcRight - srcLeft);
2545 /* Return as Left target if value would be negative */
2547 } else if (lRatio > EEP_SCALE) {
2548 /* Return as Right target if Ratio is greater than 100% (SCALE) */
2551 rv = (lRatio * targetRight + (EEP_SCALE - lRatio) *
2552 targetLeft) / EEP_SCALE;
2561 * Return the four rates of target power for the given target power table
2562 * channel, and number of channels
2565 ar5212GetTargetPowers(struct ath_hal *ah, const struct ieee80211_channel *chan,
2566 const TRGT_POWER_INFO *powInfo,
2567 uint16_t numChannels, TRGT_POWER_INFO *pNewPower)
2569 uint16_t freq = ath_hal_gethwchannel(ah, chan);
2570 /* temp array for holding target power channels */
2571 uint16_t tempChannelList[NUM_TEST_FREQUENCIES];
2572 uint16_t clo, chi, ixlo, ixhi;
2575 /* Copy the target powers into the temp channel list */
2576 for (i = 0; i < numChannels; i++)
2577 tempChannelList[i] = powInfo[i].testChannel;
2579 ar5212GetLowerUpperValues(freq, tempChannelList,
2580 numChannels, &clo, &chi);
2582 /* Get the indices for the channel */
2584 for (i = 0; i < numChannels; i++) {
2585 if (clo == tempChannelList[i]) {
2588 if (chi == tempChannelList[i]) {
2595 * Get the lower and upper channels, target powers,
2596 * and interpolate between them.
2598 pNewPower->twicePwr6_24 = interpolate(freq, clo, chi,
2599 powInfo[ixlo].twicePwr6_24, powInfo[ixhi].twicePwr6_24);
2600 pNewPower->twicePwr36 = interpolate(freq, clo, chi,
2601 powInfo[ixlo].twicePwr36, powInfo[ixhi].twicePwr36);
2602 pNewPower->twicePwr48 = interpolate(freq, clo, chi,
2603 powInfo[ixlo].twicePwr48, powInfo[ixhi].twicePwr48);
2604 pNewPower->twicePwr54 = interpolate(freq, clo, chi,
2605 powInfo[ixlo].twicePwr54, powInfo[ixhi].twicePwr54);
2609 * Search a list for a specified value v that is within
2610 * EEP_DELTA of the search values. Return the closest
2611 * values in the list above and below the desired value.
2612 * EEP_DELTA is a factional value; everything is scaled
2613 * so only integer arithmetic is used.
2615 * NB: the input list is assumed to be sorted in ascending order
2618 ar5212GetLowerUpperValues(uint16_t v, uint16_t *lp, uint16_t listSize,
2619 uint16_t *vlo, uint16_t *vhi)
2621 uint32_t target = v * EEP_SCALE;
2622 uint16_t *ep = lp+listSize;
2625 * Check first and last elements for out-of-bounds conditions.
2627 if (target < (uint32_t)(lp[0] * EEP_SCALE - EEP_DELTA)) {
2628 *vlo = *vhi = lp[0];
2631 if (target > (uint32_t)(ep[-1] * EEP_SCALE + EEP_DELTA)) {
2632 *vlo = *vhi = ep[-1];
2636 /* look for value being near or between 2 values in list */
2637 for (; lp < ep; lp++) {
2639 * If value is close to the current value of the list
2640 * then target is not between values, it is one of the values
2642 if (abs(lp[0] * EEP_SCALE - target) < EEP_DELTA) {
2643 *vlo = *vhi = lp[0];
2647 * Look for value being between current value and next value
2648 * if so return these 2 values
2650 if (target < (uint32_t)(lp[1] * EEP_SCALE - EEP_DELTA)) {
2656 HALASSERT(AH_FALSE); /* should not reach here */
2660 * Perform analog "swizzling" of parameters into their location
2662 * NB: used by RF backends
2665 ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32, uint32_t numBits,
2666 uint32_t firstBit, uint32_t column)
2668 #define MAX_ANALOG_START 319 /* XXX */
2669 uint32_t tmp32, mask, arrayEntry, lastBit;
2670 int32_t bitPosition, bitsLeft;
2672 HALASSERT(column <= 3);
2673 HALASSERT(numBits <= 32);
2674 HALASSERT(firstBit + numBits <= MAX_ANALOG_START);
2676 tmp32 = ath_hal_reverseBits(reg32, numBits);
2677 arrayEntry = (firstBit - 1) / 8;
2678 bitPosition = (firstBit - 1) % 8;
2680 while (bitsLeft > 0) {
2681 lastBit = (bitPosition + bitsLeft > 8) ?
2682 8 : bitPosition + bitsLeft;
2683 mask = (((1 << lastBit) - 1) ^ ((1 << bitPosition) - 1)) <<
2685 rfBuf[arrayEntry] &= ~mask;
2686 rfBuf[arrayEntry] |= ((tmp32 << bitPosition) <<
2687 (column * 8)) & mask;
2688 bitsLeft -= 8 - bitPosition;
2689 tmp32 = tmp32 >> (8 - bitPosition);
2693 #undef MAX_ANALOG_START
2697 * Sets the rate to duration values in MAC - used for multi-
2699 * The rate duration table needs to cover all valid rate codes;
2700 * the 11g table covers all ofdm rates, while the 11b table
2701 * covers all cck rates => all valid rates get covered between
2702 * these two mode's ratetables!
2703 * But if we're turbo, the ofdm phy is replaced by the turbo phy
2704 * and cck is not valid with turbo => all rates get covered
2705 * by the turbo ratetable only
2708 ar5212SetRateDurationTable(struct ath_hal *ah,
2709 const struct ieee80211_channel *chan)
2711 const HAL_RATE_TABLE *rt;
2714 /* NB: band doesn't matter for 1/2 and 1/4 rate */
2715 if (IEEE80211_IS_CHAN_HALF(chan)) {
2716 rt = ar5212GetRateTable(ah, HAL_MODE_11A_HALF_RATE);
2717 } else if (IEEE80211_IS_CHAN_QUARTER(chan)) {
2718 rt = ar5212GetRateTable(ah, HAL_MODE_11A_QUARTER_RATE);
2720 rt = ar5212GetRateTable(ah,
2721 IEEE80211_IS_CHAN_TURBO(chan) ? HAL_MODE_TURBO : HAL_MODE_11G);
2724 for (i = 0; i < rt->rateCount; ++i)
2726 AR_RATE_DURATION(rt->info[i].rateCode),
2727 ath_hal_computetxtime(ah, rt,
2728 WLAN_CTRL_FRAME_SIZE,
2729 rt->info[i].controlRate, AH_FALSE));
2730 if (!IEEE80211_IS_CHAN_TURBO(chan)) {
2731 /* 11g Table is used to cover the CCK rates. */
2732 rt = ar5212GetRateTable(ah, HAL_MODE_11G);
2733 for (i = 0; i < rt->rateCount; ++i) {
2734 uint32_t reg = AR_RATE_DURATION(rt->info[i].rateCode);
2736 if (rt->info[i].phy != IEEE80211_T_CCK)
2739 OS_REG_WRITE(ah, reg,
2740 ath_hal_computetxtime(ah, rt,
2741 WLAN_CTRL_FRAME_SIZE,
2742 rt->info[i].controlRate, AH_FALSE));
2743 /* cck rates have short preamble option also */
2744 if (rt->info[i].shortPreamble) {
2745 reg += rt->info[i].shortPreamble << 2;
2746 OS_REG_WRITE(ah, reg,
2747 ath_hal_computetxtime(ah, rt,
2748 WLAN_CTRL_FRAME_SIZE,
2749 rt->info[i].controlRate,
2756 /* Adjust various register settings based on half/quarter rate clock setting.
2757 * This includes: +USEC, TX/RX latency,
2758 * + IFS params: slot, eifs, misc etc.
2761 ar5212SetIFSTiming(struct ath_hal *ah, const struct ieee80211_channel *chan)
2763 uint32_t txLat, rxLat, usec, slot, refClock, eifs, init_usec;
2765 HALASSERT(IEEE80211_IS_CHAN_HALF(chan) ||
2766 IEEE80211_IS_CHAN_QUARTER(chan));
2768 refClock = OS_REG_READ(ah, AR_USEC) & AR_USEC_USEC32;
2769 if (IEEE80211_IS_CHAN_HALF(chan)) {
2770 slot = IFS_SLOT_HALF_RATE;
2771 rxLat = RX_NON_FULL_RATE_LATENCY << AR5212_USEC_RX_LAT_S;
2772 txLat = TX_HALF_RATE_LATENCY << AR5212_USEC_TX_LAT_S;
2773 usec = HALF_RATE_USEC;
2774 eifs = IFS_EIFS_HALF_RATE;
2775 init_usec = INIT_USEC >> 1;
2776 } else { /* quarter rate */
2777 slot = IFS_SLOT_QUARTER_RATE;
2778 rxLat = RX_NON_FULL_RATE_LATENCY << AR5212_USEC_RX_LAT_S;
2779 txLat = TX_QUARTER_RATE_LATENCY << AR5212_USEC_TX_LAT_S;
2780 usec = QUARTER_RATE_USEC;
2781 eifs = IFS_EIFS_QUARTER_RATE;
2782 init_usec = INIT_USEC >> 2;
2785 OS_REG_WRITE(ah, AR_USEC, (usec | refClock | txLat | rxLat));
2786 OS_REG_WRITE(ah, AR_D_GBL_IFS_SLOT, slot);
2787 OS_REG_WRITE(ah, AR_D_GBL_IFS_EIFS, eifs);
2788 OS_REG_RMW_FIELD(ah, AR_D_GBL_IFS_MISC,
2789 AR_D_GBL_IFS_MISC_USEC_DURATION, init_usec);