Update ath_hal from FreeBSD.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5416 / ar5416_attach.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4  *
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.
8  *
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.
16  *
17  * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_attach.c 203930 2010-02-15 17:49:49Z rpaulo $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_devid.h"
25
26 #include "ah_eeprom_v14.h"
27
28 #include "ar5416/ar5416.h"
29 #include "ar5416/ar5416reg.h"
30 #include "ar5416/ar5416phy.h"
31
32 #include "ar5416/ar5416.ini"
33
34 static void ar5416ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore);
35 static void ar5416WriteIni(struct ath_hal *ah,
36             const struct ieee80211_channel *chan);
37 static void ar5416SpurMitigate(struct ath_hal *ah,
38             const struct ieee80211_channel *chan);
39
40 static void
41 ar5416AniSetup(struct ath_hal *ah)
42 {
43         static const struct ar5212AniParams aniparams = {
44                 .maxNoiseImmunityLevel  = 4,    /* levels 0..4 */
45                 .totalSizeDesired       = { -55, -55, -55, -55, -62 },
46                 .coarseHigh             = { -14, -14, -14, -14, -12 },
47                 .coarseLow              = { -64, -64, -64, -64, -70 },
48                 .firpwr                 = { -78, -78, -78, -78, -80 },
49                 .maxSpurImmunityLevel   = 2,
50                 .cycPwrThr1             = { 2, 4, 6 },
51                 .maxFirstepLevel        = 2,    /* levels 0..2 */
52                 .firstep                = { 0, 4, 8 },
53                 .ofdmTrigHigh           = 500,
54                 .ofdmTrigLow            = 200,
55                 .cckTrigHigh            = 200,
56                 .cckTrigLow             = 100,
57                 .rssiThrHigh            = 40,
58                 .rssiThrLow             = 7,
59                 .period                 = 100,
60         };
61         /* NB: ANI is not enabled yet */
62         ar5212AniAttach(ah, &aniparams, &aniparams, AH_FALSE);
63 }
64
65 /*
66  * Attach for an AR5416 part.
67  */
68 void
69 ar5416InitState(struct ath_hal_5416 *ahp5416, uint16_t devid, HAL_SOFTC sc,
70         HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
71 {
72         struct ath_hal_5212 *ahp;
73         struct ath_hal *ah;
74
75         ahp = &ahp5416->ah_5212;
76         ar5212InitState(ahp, devid, sc, st, sh, status);
77         ah = &ahp->ah_priv.h;
78
79         /* override 5212 methods for our needs */
80         ah->ah_magic                    = AR5416_MAGIC;
81         ah->ah_getRateTable             = ar5416GetRateTable;
82         ah->ah_detach                   = ar5416Detach;
83
84         /* Reset functions */
85         ah->ah_reset                    = ar5416Reset;
86         ah->ah_phyDisable               = ar5416PhyDisable;
87         ah->ah_disable                  = ar5416Disable;
88         ah->ah_configPCIE               = ar5416ConfigPCIE;
89         ah->ah_perCalibration           = ar5416PerCalibration;
90         ah->ah_perCalibrationN          = ar5416PerCalibrationN,
91         ah->ah_resetCalValid            = ar5416ResetCalValid,
92         ah->ah_setTxPowerLimit          = ar5416SetTxPowerLimit;
93         ah->ah_setTxPower               = ar5416SetTransmitPower;
94         ah->ah_setBoardValues           = ar5416SetBoardValues;
95
96         /* Transmit functions */
97         ah->ah_stopTxDma                = ar5416StopTxDma;
98         ah->ah_setupTxDesc              = ar5416SetupTxDesc;
99         ah->ah_setupXTxDesc             = ar5416SetupXTxDesc;
100         ah->ah_fillTxDesc               = ar5416FillTxDesc;
101         ah->ah_procTxDesc               = ar5416ProcTxDesc;
102
103         /* Receive Functions */
104         ah->ah_startPcuReceive          = ar5416StartPcuReceive;
105         ah->ah_stopPcuReceive           = ar5416StopPcuReceive;
106         ah->ah_setupRxDesc              = ar5416SetupRxDesc;
107         ah->ah_procRxDesc               = ar5416ProcRxDesc;
108         ah->ah_rxMonitor                = ar5416AniPoll,
109         ah->ah_procMibEvent             = ar5416ProcessMibIntr,
110
111         /* Misc Functions */
112         ah->ah_getDiagState             = ar5416GetDiagState;
113         ah->ah_setLedState              = ar5416SetLedState;
114         ah->ah_gpioCfgOutput            = ar5416GpioCfgOutput;
115         ah->ah_gpioCfgInput             = ar5416GpioCfgInput;
116         ah->ah_gpioGet                  = ar5416GpioGet;
117         ah->ah_gpioSet                  = ar5416GpioSet;
118         ah->ah_gpioSetIntr              = ar5416GpioSetIntr;
119         ah->ah_resetTsf                 = ar5416ResetTsf;
120         ah->ah_getRfGain                = ar5416GetRfgain;
121         ah->ah_setAntennaSwitch         = ar5416SetAntennaSwitch;
122         ah->ah_setDecompMask            = ar5416SetDecompMask;
123         ah->ah_setCoverageClass         = ar5416SetCoverageClass;
124
125         ah->ah_resetKeyCacheEntry       = ar5416ResetKeyCacheEntry;
126         ah->ah_setKeyCacheEntry         = ar5416SetKeyCacheEntry;
127
128         /* Power Management Functions */
129         ah->ah_setPowerMode             = ar5416SetPowerMode;
130
131         /* Beacon Management Functions */
132         ah->ah_setBeaconTimers          = ar5416SetBeaconTimers;
133         ah->ah_beaconInit               = ar5416BeaconInit;
134         ah->ah_setStationBeaconTimers   = ar5416SetStaBeaconTimers;
135         ah->ah_resetStationBeaconTimers = ar5416ResetStaBeaconTimers;
136
137         /* XXX 802.11n Functions */
138 #if 0
139         ah->ah_chainTxDesc              = ar5416ChainTxDesc;
140         ah->ah_setupFirstTxDesc         = ar5416SetupFirstTxDesc;
141         ah->ah_setupLastTxDesc          = ar5416SetupLastTxDesc;
142         ah->ah_set11nRateScenario       = ar5416Set11nRateScenario;
143         ah->ah_set11nAggrMiddle         = ar5416Set11nAggrMiddle;
144         ah->ah_clr11nAggr               = ar5416Clr11nAggr;
145         ah->ah_set11nBurstDuration      = ar5416Set11nBurstDuration;
146         ah->ah_get11nExtBusy            = ar5416Get11nExtBusy;
147         ah->ah_set11nMac2040            = ar5416Set11nMac2040;
148         ah->ah_get11nRxClear            = ar5416Get11nRxClear;
149         ah->ah_set11nRxClear            = ar5416Set11nRxClear;
150 #endif
151
152         /* Interrupt functions */
153         ah->ah_isInterruptPending       = ar5416IsInterruptPending;
154         ah->ah_getPendingInterrupts     = ar5416GetPendingInterrupts;
155         ah->ah_setInterrupts            = ar5416SetInterrupts;
156
157         ahp->ah_priv.ah_getWirelessModes= ar5416GetWirelessModes;
158         ahp->ah_priv.ah_eepromRead      = ar5416EepromRead;
159 #ifdef AH_SUPPORT_WRITE_EEPROM
160         ahp->ah_priv.ah_eepromWrite     = ar5416EepromWrite;
161 #endif
162         ahp->ah_priv.ah_getChipPowerLimits = ar5416GetChipPowerLimits;
163
164         AH5416(ah)->ah_writeIni         = ar5416WriteIni;
165         AH5416(ah)->ah_spurMitigate     = ar5416SpurMitigate;
166         /*
167          * Start by setting all Owl devices to 2x2
168          */
169         AH5416(ah)->ah_rx_chainmask = AR5416_DEFAULT_RXCHAINMASK;
170         AH5416(ah)->ah_tx_chainmask = AR5416_DEFAULT_TXCHAINMASK;
171 }
172
173 uint32_t
174 ar5416GetRadioRev(struct ath_hal *ah)
175 {
176         uint32_t val;
177         int i;
178
179         /* Read Radio Chip Rev Extract */
180         OS_REG_WRITE(ah, AR_PHY(0x36), 0x00007058);
181         for (i = 0; i < 8; i++)
182                 OS_REG_WRITE(ah, AR_PHY(0x20), 0x00010000);
183         val = (OS_REG_READ(ah, AR_PHY(256)) >> 24) & 0xff;
184         val = ((val & 0xf0) >> 4) | ((val & 0x0f) << 4);
185         return ath_hal_reverseBits(val, 8);
186 }
187
188 /*
189  * Attach for an AR5416 part.
190  */
191 static struct ath_hal *
192 ar5416Attach(uint16_t devid, HAL_SOFTC sc,
193         HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
194 {
195         struct ath_hal_5416 *ahp5416;
196         struct ath_hal_5212 *ahp;
197         struct ath_hal *ah;
198         uint32_t val;
199         HAL_STATUS ecode;
200         HAL_BOOL rfStatus;
201
202         HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
203             __func__, sc, (void*) st, (void*) sh);
204
205         /* NB: memory is returned zero'd */
206         ahp5416 = ath_hal_malloc(sizeof (struct ath_hal_5416) +
207                 /* extra space for Owl 2.1/2.2 WAR */
208                 sizeof(ar5416Addac)
209         );
210         if (ahp5416 == AH_NULL) {
211                 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
212                     "%s: cannot allocate memory for state block\n", __func__);
213                 *status = HAL_ENOMEM;
214                 return AH_NULL;
215         }
216         ar5416InitState(ahp5416, devid, sc, st, sh, status);
217         ahp = &ahp5416->ah_5212;
218         ah = &ahp->ah_priv.h;
219
220         if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON)) {
221                 /* reset chip */
222                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't reset chip\n", __func__);
223                 ecode = HAL_EIO;
224                 goto bad;
225         }
226
227         if (!ar5416SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
228                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't wakeup chip\n", __func__);
229                 ecode = HAL_EIO;
230                 goto bad;
231         }
232         /* Read Revisions from Chips before taking out of reset */
233         val = OS_REG_READ(ah, AR_SREV) & AR_SREV_ID;
234         AH_PRIVATE(ah)->ah_macVersion = val >> AR_SREV_ID_S;
235         AH_PRIVATE(ah)->ah_macRev = val & AR_SREV_REVISION;
236         AH_PRIVATE(ah)->ah_ispcie = (devid == AR5416_DEVID_PCIE);
237
238         /* setup common ini data; rf backends handle remainder */
239         HAL_INI_INIT(&ahp->ah_ini_modes, ar5416Modes, 6);
240         HAL_INI_INIT(&ahp->ah_ini_common, ar5416Common, 2);
241
242         HAL_INI_INIT(&AH5416(ah)->ah_ini_bb_rfgain, ar5416BB_RfGain, 3);
243         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank0, ar5416Bank0, 2);
244         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank1, ar5416Bank1, 2);
245         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank2, ar5416Bank2, 2);
246         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank3, ar5416Bank3, 3);
247         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank6, ar5416Bank6, 3);
248         HAL_INI_INIT(&AH5416(ah)->ah_ini_bank7, ar5416Bank7, 2);
249         HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar5416Addac, 2);
250
251         if (!IS_5416V2_2(ah)) {         /* Owl 2.1/2.0 */
252                 struct ini {
253                         uint32_t        *data;          /* NB: !const */
254                         int             rows, cols;
255                 };
256                 /* override CLKDRV value */
257                 OS_MEMCPY(&AH5416(ah)[1], ar5416Addac, sizeof(ar5416Addac));
258                 AH5416(ah)->ah_ini_addac.data = (uint32_t *) &AH5416(ah)[1];
259                 HAL_INI_VAL((struct ini *)&AH5416(ah)->ah_ini_addac, 31, 1) = 0;
260         }
261
262         HAL_INI_INIT(&AH5416(ah)->ah_ini_pcieserdes, ar5416PciePhy, 2);
263         ar5416AttachPCIE(ah);
264
265         ecode = ath_hal_v14EepromAttach(ah);
266         if (ecode != HAL_OK)
267                 goto bad;
268
269         if (!ar5416ChipReset(ah, AH_NULL)) {    /* reset chip */
270                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n",
271                     __func__);
272                 ecode = HAL_EIO;
273                 goto bad;
274         }
275
276         AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
277
278         if (!ar5212ChipTest(ah)) {
279                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
280                     __func__);
281                 ecode = HAL_ESELFTEST;
282                 goto bad;
283         }
284
285         /*
286          * Set correct Baseband to analog shift
287          * setting to access analog chips.
288          */
289         OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
290
291         /* Read Radio Chip Rev Extract */
292         AH_PRIVATE(ah)->ah_analog5GhzRev = ar5212GetRadioRev(ah);
293         switch (AH_PRIVATE(ah)->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR) {
294         case AR_RAD5122_SREV_MAJOR:     /* Fowl: 5G/2x2 */
295         case AR_RAD2122_SREV_MAJOR:     /* Fowl: 2+5G/2x2 */
296         case AR_RAD2133_SREV_MAJOR:     /* Fowl: 2G/3x3 */
297         case AR_RAD5133_SREV_MAJOR:     /* Fowl: 2+5G/3x3 */
298                 break;
299         default:
300                 if (AH_PRIVATE(ah)->ah_analog5GhzRev == 0) {
301                         /*
302                          * When RF_Silen is used the analog chip is reset.
303                          * So when the system boots with radio switch off
304                          * the RF chip rev reads back as zero and we need
305                          * to use the mac+phy revs to set the radio rev.
306                          */
307                         AH_PRIVATE(ah)->ah_analog5GhzRev =
308                                 AR_RAD5133_SREV_MAJOR;
309                         break;
310                 }
311                 /* NB: silently accept anything in release code per Atheros */
312 #ifdef AH_DEBUG
313                 HALDEBUG(ah, HAL_DEBUG_ANY,
314                     "%s: 5G Radio Chip Rev 0x%02X is not supported by "
315                     "this driver\n", __func__,
316                     AH_PRIVATE(ah)->ah_analog5GhzRev);
317                 ecode = HAL_ENOTSUPP;
318                 goto bad;
319 #endif
320         }
321
322         /*
323          * Got everything we need now to setup the capabilities.
324          */
325         if (!ar5416FillCapabilityInfo(ah)) {
326                 ecode = HAL_EEREAD;
327                 goto bad;
328         }
329
330         ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
331         if (ecode != HAL_OK) {
332                 HALDEBUG(ah, HAL_DEBUG_ANY,
333                     "%s: error getting mac address from EEPROM\n", __func__);
334                 goto bad;
335         }
336         /* XXX How about the serial number ? */
337         /* Read Reg Domain */
338         AH_PRIVATE(ah)->ah_currentRD =
339             ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, AH_NULL);
340
341         /*
342          * ah_miscMode is populated by ar5416FillCapabilityInfo()
343          * starting from griffin. Set here to make sure that
344          * AR_MISC_MODE_MIC_NEW_LOC_ENABLE is set before a GTK is
345          * placed into hardware.
346          */
347         if (ahp->ah_miscMode != 0)
348                 OS_REG_WRITE(ah, AR_MISC_MODE, ahp->ah_miscMode);
349
350         rfStatus = ar2133RfAttach(ah, &ecode);
351         if (!rfStatus) {
352                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
353                     __func__, ecode);
354                 goto bad;
355         }
356
357         ar5416AniSetup(ah);                     /* Anti Noise Immunity */
358         ar5416InitNfHistBuff(AH5416(ah)->ah_cal.nfCalHist);
359
360         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
361
362         return ah;
363 bad:
364         if (ahp)
365                 ar5416Detach((struct ath_hal *) ahp);
366         if (status)
367                 *status = ecode;
368         return AH_NULL;
369 }
370
371 void
372 ar5416Detach(struct ath_hal *ah)
373 {
374         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
375
376         HALASSERT(ah != AH_NULL);
377         HALASSERT(ah->ah_magic == AR5416_MAGIC);
378
379         ar5416AniDetach(ah);
380         ar5212RfDetach(ah);
381         ah->ah_disable(ah);
382         ar5416SetPowerMode(ah, HAL_PM_FULL_SLEEP, AH_TRUE);
383         ath_hal_eepromDetach(ah);
384         ath_hal_free(ah);
385 }
386
387 void
388 ar5416AttachPCIE(struct ath_hal *ah)
389 {
390         if (AH_PRIVATE(ah)->ah_ispcie)
391                 ath_hal_configPCIE(ah, AH_FALSE);
392         else
393                 ath_hal_disablePCIE(ah);
394 }
395
396 static void
397 ar5416ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore)
398 {
399         if (AH_PRIVATE(ah)->ah_ispcie && !restore) {
400                 ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_pcieserdes, 1, 0);
401                 OS_DELAY(1000);
402                 OS_REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
403                 OS_REG_WRITE(ah, AR_WA, AR_WA_DEFAULT);
404         }
405 }
406
407 static void
408 ar5416WriteIni(struct ath_hal *ah, const struct ieee80211_channel *chan)
409 {
410         u_int modesIndex, freqIndex;
411         int regWrites = 0;
412
413         /* Setup the indices for the next set of register array writes */
414         /* XXX Ignore 11n dynamic mode on the AR5416 for the moment */
415         if (IEEE80211_IS_CHAN_2GHZ(chan)) {
416                 freqIndex = 2;
417                 if (IEEE80211_IS_CHAN_HT40(chan))
418                         modesIndex = 3;
419                 else if (IEEE80211_IS_CHAN_108G(chan))
420                         modesIndex = 5;
421                 else
422                         modesIndex = 4;
423         } else {
424                 freqIndex = 1;
425                 if (IEEE80211_IS_CHAN_HT40(chan) ||
426                     IEEE80211_IS_CHAN_TURBO(chan))
427                         modesIndex = 2;
428                 else
429                         modesIndex = 1;
430         }
431
432         /* Set correct Baseband to analog shift setting to access analog chips. */
433         OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
434
435         /*
436          * Write addac shifts
437          */
438         OS_REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
439 #if 0
440         /* NB: only required for Sowl */
441         ar5416EepromSetAddac(ah, chan);
442 #endif
443         regWrites = ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_addac, 1,
444             regWrites);
445         OS_REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
446
447         regWrites = ath_hal_ini_write(ah, &AH5212(ah)->ah_ini_modes,
448             modesIndex, regWrites);
449         regWrites = ath_hal_ini_write(ah, &AH5212(ah)->ah_ini_common,
450             1, regWrites);
451
452         /* XXX updated regWrites? */
453         AH5212(ah)->ah_rfHal->writeRegs(ah, modesIndex, freqIndex, regWrites);
454 }
455
456 /*
457  * Convert to baseband spur frequency given input channel frequency 
458  * and compute register settings below.
459  */
460
461 static void
462 ar5416SpurMitigate(struct ath_hal *ah, const struct ieee80211_channel *chan)
463 {
464     uint16_t freq = ath_hal_gethwchannel(ah, chan);
465     static const int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
466                 AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60 };
467     static const int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
468                 AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60 };
469     static const int inc[4] = { 0, 100, 0, 0 };
470
471     int bb_spur = AR_NO_SPUR;
472     int bin, cur_bin;
473     int spur_freq_sd;
474     int spur_delta_phase;
475     int denominator;
476     int upper, lower, cur_vit_mask;
477     int tmp, new;
478     int i;
479
480     int8_t mask_m[123];
481     int8_t mask_p[123];
482     int8_t mask_amt;
483     int tmp_mask;
484     int cur_bb_spur;
485     HAL_BOOL is2GHz = IEEE80211_IS_CHAN_2GHZ(chan);
486
487     OS_MEMZERO(mask_m, sizeof(mask_m));
488     OS_MEMZERO(mask_p, sizeof(mask_p));
489
490     /*
491      * Need to verify range +/- 9.5 for static ht20, otherwise spur
492      * is out-of-band and can be ignored.
493      */
494     /* XXX ath9k changes */
495     for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
496         cur_bb_spur = ath_hal_getSpurChan(ah, i, is2GHz);
497         if (AR_NO_SPUR == cur_bb_spur)
498             break;
499         cur_bb_spur = cur_bb_spur - (freq * 10);
500         if ((cur_bb_spur > -95) && (cur_bb_spur < 95)) {
501             bb_spur = cur_bb_spur;
502             break;
503         }
504     }
505     if (AR_NO_SPUR == bb_spur)
506         return;
507
508     bin = bb_spur * 32;
509
510     tmp = OS_REG_READ(ah, AR_PHY_TIMING_CTRL4_CHAIN(0));
511     new = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
512         AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
513         AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
514         AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
515
516     OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4_CHAIN(0), new);
517
518     new = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
519         AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
520         AR_PHY_SPUR_REG_MASK_RATE_SELECT |
521         AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
522         SM(AR5416_SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
523     OS_REG_WRITE(ah, AR_PHY_SPUR_REG, new);
524     /*
525      * Should offset bb_spur by +/- 10 MHz for dynamic 2040 MHz
526      * config, no offset for HT20.
527      * spur_delta_phase = bb_spur/40 * 2**21 for static ht20,
528      * /80 for dyn2040.
529      */
530     spur_delta_phase = ((bb_spur * 524288) / 100) &
531         AR_PHY_TIMING11_SPUR_DELTA_PHASE;
532     /*
533      * in 11A mode the denominator of spur_freq_sd should be 40 and
534      * it should be 44 in 11G
535      */
536     denominator = IEEE80211_IS_CHAN_2GHZ(chan) ? 440 : 400;
537     spur_freq_sd = ((bb_spur * 2048) / denominator) & 0x3ff;
538
539     new = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
540         SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
541         SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
542     OS_REG_WRITE(ah, AR_PHY_TIMING11, new);
543
544
545     /*
546      * ============================================
547      * pilot mask 1 [31:0] = +6..-26, no 0 bin
548      * pilot mask 2 [19:0] = +26..+7
549      *
550      * channel mask 1 [31:0] = +6..-26, no 0 bin
551      * channel mask 2 [19:0] = +26..+7
552      */
553     //cur_bin = -26;
554     cur_bin = -6000;
555     upper = bin + 100;
556     lower = bin - 100;
557
558     for (i = 0; i < 4; i++) {
559         int pilot_mask = 0;
560         int chan_mask  = 0;
561         int bp         = 0;
562         for (bp = 0; bp < 30; bp++) {
563             if ((cur_bin > lower) && (cur_bin < upper)) {
564                 pilot_mask = pilot_mask | 0x1 << bp;
565                 chan_mask  = chan_mask | 0x1 << bp;
566             }
567             cur_bin += 100;
568         }
569         cur_bin += inc[i];
570         OS_REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
571         OS_REG_WRITE(ah, chan_mask_reg[i], chan_mask);
572     }
573
574     /* =================================================
575      * viterbi mask 1 based on channel magnitude
576      * four levels 0-3
577      *  - mask (-27 to 27) (reg 64,0x9900 to 67,0x990c)
578      *      [1 2 2 1] for -9.6 or [1 2 1] for +16
579      *  - enable_mask_ppm, all bins move with freq
580      *
581      *  - mask_select,    8 bits for rates (reg 67,0x990c)
582      *  - mask_rate_cntl, 8 bits for rates (reg 67,0x990c)
583      *      choose which mask to use mask or mask2
584      */
585
586     /*
587      * viterbi mask 2  2nd set for per data rate puncturing
588      * four levels 0-3
589      *  - mask_select, 8 bits for rates (reg 67)
590      *  - mask (-27 to 27) (reg 98,0x9988 to 101,0x9994)
591      *      [1 2 2 1] for -9.6 or [1 2 1] for +16
592      */
593     cur_vit_mask = 6100;
594     upper        = bin + 120;
595     lower        = bin - 120;
596
597     for (i = 0; i < 123; i++) {
598         if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
599             if ((abs(cur_vit_mask - bin)) < 75) {
600                 mask_amt = 1;
601             } else {
602                 mask_amt = 0;
603             }
604             if (cur_vit_mask < 0) {
605                 mask_m[abs(cur_vit_mask / 100)] = mask_amt;
606             } else {
607                 mask_p[cur_vit_mask / 100] = mask_amt;
608             }
609         }
610         cur_vit_mask -= 100;
611     }
612
613     tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
614           | (mask_m[48] << 26) | (mask_m[49] << 24)
615           | (mask_m[50] << 22) | (mask_m[51] << 20)
616           | (mask_m[52] << 18) | (mask_m[53] << 16)
617           | (mask_m[54] << 14) | (mask_m[55] << 12)
618           | (mask_m[56] << 10) | (mask_m[57] <<  8)
619           | (mask_m[58] <<  6) | (mask_m[59] <<  4)
620           | (mask_m[60] <<  2) | (mask_m[61] <<  0);
621     OS_REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
622     OS_REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
623
624     tmp_mask =             (mask_m[31] << 28)
625           | (mask_m[32] << 26) | (mask_m[33] << 24)
626           | (mask_m[34] << 22) | (mask_m[35] << 20)
627           | (mask_m[36] << 18) | (mask_m[37] << 16)
628           | (mask_m[48] << 14) | (mask_m[39] << 12)
629           | (mask_m[40] << 10) | (mask_m[41] <<  8)
630           | (mask_m[42] <<  6) | (mask_m[43] <<  4)
631           | (mask_m[44] <<  2) | (mask_m[45] <<  0);
632     OS_REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
633     OS_REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
634
635     tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
636           | (mask_m[18] << 26) | (mask_m[18] << 24)
637           | (mask_m[20] << 22) | (mask_m[20] << 20)
638           | (mask_m[22] << 18) | (mask_m[22] << 16)
639           | (mask_m[24] << 14) | (mask_m[24] << 12)
640           | (mask_m[25] << 10) | (mask_m[26] <<  8)
641           | (mask_m[27] <<  6) | (mask_m[28] <<  4)
642           | (mask_m[29] <<  2) | (mask_m[30] <<  0);
643     OS_REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
644     OS_REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
645
646     tmp_mask = (mask_m[ 0] << 30) | (mask_m[ 1] << 28)
647           | (mask_m[ 2] << 26) | (mask_m[ 3] << 24)
648           | (mask_m[ 4] << 22) | (mask_m[ 5] << 20)
649           | (mask_m[ 6] << 18) | (mask_m[ 7] << 16)
650           | (mask_m[ 8] << 14) | (mask_m[ 9] << 12)
651           | (mask_m[10] << 10) | (mask_m[11] <<  8)
652           | (mask_m[12] <<  6) | (mask_m[13] <<  4)
653           | (mask_m[14] <<  2) | (mask_m[15] <<  0);
654     OS_REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
655     OS_REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
656
657     tmp_mask =             (mask_p[15] << 28)
658           | (mask_p[14] << 26) | (mask_p[13] << 24)
659           | (mask_p[12] << 22) | (mask_p[11] << 20)
660           | (mask_p[10] << 18) | (mask_p[ 9] << 16)
661           | (mask_p[ 8] << 14) | (mask_p[ 7] << 12)
662           | (mask_p[ 6] << 10) | (mask_p[ 5] <<  8)
663           | (mask_p[ 4] <<  6) | (mask_p[ 3] <<  4)
664           | (mask_p[ 2] <<  2) | (mask_p[ 1] <<  0);
665     OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
666     OS_REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
667
668     tmp_mask =             (mask_p[30] << 28)
669           | (mask_p[29] << 26) | (mask_p[28] << 24)
670           | (mask_p[27] << 22) | (mask_p[26] << 20)
671           | (mask_p[25] << 18) | (mask_p[24] << 16)
672           | (mask_p[23] << 14) | (mask_p[22] << 12)
673           | (mask_p[21] << 10) | (mask_p[20] <<  8)
674           | (mask_p[19] <<  6) | (mask_p[18] <<  4)
675           | (mask_p[17] <<  2) | (mask_p[16] <<  0);
676     OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
677     OS_REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
678
679     tmp_mask =             (mask_p[45] << 28)
680           | (mask_p[44] << 26) | (mask_p[43] << 24)
681           | (mask_p[42] << 22) | (mask_p[41] << 20)
682           | (mask_p[40] << 18) | (mask_p[39] << 16)
683           | (mask_p[38] << 14) | (mask_p[37] << 12)
684           | (mask_p[36] << 10) | (mask_p[35] <<  8)
685           | (mask_p[34] <<  6) | (mask_p[33] <<  4)
686           | (mask_p[32] <<  2) | (mask_p[31] <<  0);
687     OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
688     OS_REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
689
690     tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
691           | (mask_p[59] << 26) | (mask_p[58] << 24)
692           | (mask_p[57] << 22) | (mask_p[56] << 20)
693           | (mask_p[55] << 18) | (mask_p[54] << 16)
694           | (mask_p[53] << 14) | (mask_p[52] << 12)
695           | (mask_p[51] << 10) | (mask_p[50] <<  8)
696           | (mask_p[49] <<  6) | (mask_p[48] <<  4)
697           | (mask_p[47] <<  2) | (mask_p[46] <<  0);
698     OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
699     OS_REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
700 }
701
702 /*
703  * Fill all software cached or static hardware state information.
704  * Return failure if capabilities are to come from EEPROM and
705  * cannot be read.
706  */
707 HAL_BOOL
708 ar5416FillCapabilityInfo(struct ath_hal *ah)
709 {
710         struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
711         HAL_CAPABILITIES *pCap = &ahpriv->ah_caps;
712         uint16_t val;
713         
714         /* Construct wireless mode from EEPROM */
715         pCap->halWirelessModes = 0;
716         if (ath_hal_eepromGetFlag(ah, AR_EEP_AMODE)) {
717                 pCap->halWirelessModes |= HAL_MODE_11A
718                                        |  HAL_MODE_11NA_HT20
719                                        |  HAL_MODE_11NA_HT40PLUS
720                                        |  HAL_MODE_11NA_HT40MINUS
721                                        ;
722         }
723         if (ath_hal_eepromGetFlag(ah, AR_EEP_GMODE)) {
724                 pCap->halWirelessModes |= HAL_MODE_11G
725                                        |  HAL_MODE_11NG_HT20
726                                        |  HAL_MODE_11NG_HT40PLUS
727                                        |  HAL_MODE_11NG_HT40MINUS
728                                        ;
729                 pCap->halWirelessModes |= HAL_MODE_11A
730                                        |  HAL_MODE_11NA_HT20
731                                        |  HAL_MODE_11NA_HT40PLUS
732                                        |  HAL_MODE_11NA_HT40MINUS
733                                        ;
734         }
735
736         pCap->halLow2GhzChan = 2312;
737         pCap->halHigh2GhzChan = 2732;
738
739         pCap->halLow5GhzChan = 4915;
740         pCap->halHigh5GhzChan = 6100;
741
742         pCap->halCipherCkipSupport = AH_FALSE;
743         pCap->halCipherTkipSupport = AH_TRUE;
744         pCap->halCipherAesCcmSupport = ath_hal_eepromGetFlag(ah, AR_EEP_AES);
745
746         pCap->halMicCkipSupport    = AH_FALSE;
747         pCap->halMicTkipSupport    = AH_TRUE;
748         pCap->halMicAesCcmSupport  = ath_hal_eepromGetFlag(ah, AR_EEP_AES);
749         /*
750          * Starting with Griffin TX+RX mic keys can be combined
751          * in one key cache slot.
752          */
753         pCap->halTkipMicTxRxKeySupport = AH_TRUE;
754         pCap->halChanSpreadSupport = AH_TRUE;
755         pCap->halSleepAfterBeaconBroken = AH_TRUE;
756
757         pCap->halCompressSupport = AH_FALSE;
758         pCap->halBurstSupport = AH_TRUE;
759         pCap->halFastFramesSupport = AH_FALSE;  /* XXX? */
760         pCap->halChapTuningSupport = AH_TRUE;
761         pCap->halTurboPrimeSupport = AH_TRUE;
762
763         pCap->halTurboGSupport = pCap->halWirelessModes & HAL_MODE_108G;
764
765         pCap->halPSPollBroken = AH_TRUE;        /* XXX fixed in later revs? */
766         pCap->halVEOLSupport = AH_TRUE;
767         pCap->halBssIdMaskSupport = AH_TRUE;
768         pCap->halMcastKeySrchSupport = AH_FALSE;
769         pCap->halTsfAddSupport = AH_TRUE;
770
771         if (ath_hal_eepromGet(ah, AR_EEP_MAXQCU, &val) == HAL_OK)
772                 pCap->halTotalQueues = val;
773         else
774                 pCap->halTotalQueues = HAL_NUM_TX_QUEUES;
775
776         if (ath_hal_eepromGet(ah, AR_EEP_KCENTRIES, &val) == HAL_OK)
777                 pCap->halKeyCacheSize = val;
778         else
779                 pCap->halKeyCacheSize = AR5416_KEYTABLE_SIZE;
780
781         /* XXX not needed */
782         pCap->halChanHalfRate = AH_FALSE;       /* XXX ? */
783         pCap->halChanQuarterRate = AH_FALSE;    /* XXX ? */
784
785         pCap->halTstampPrecision = 32;
786         pCap->halHwPhyCounterSupport = AH_TRUE;
787         pCap->halIntrMask = HAL_INT_COMMON
788                         | HAL_INT_RX
789                         | HAL_INT_TX
790                         | HAL_INT_FATAL
791                         | HAL_INT_BNR
792                         | HAL_INT_BMISC
793                         | HAL_INT_DTIMSYNC
794                         | HAL_INT_TSFOOR
795                         | HAL_INT_CST
796                         | HAL_INT_GTT
797                         ;
798
799         pCap->halFastCCSupport = AH_TRUE;
800         pCap->halNumGpioPins = 6;
801         pCap->halWowSupport = AH_FALSE;
802         pCap->halWowMatchPatternExact = AH_FALSE;
803         pCap->halBtCoexSupport = AH_FALSE;      /* XXX need support */
804         pCap->halAutoSleepSupport = AH_FALSE;
805 #if 0   /* XXX not yet */
806         pCap->halNumAntCfg2GHz = ar5416GetNumAntConfig(ahp, HAL_FREQ_BAND_2GHZ);
807         pCap->halNumAntCfg5GHz = ar5416GetNumAntConfig(ahp, HAL_FREQ_BAND_5GHZ);
808 #endif
809         pCap->halHTSupport = AH_TRUE;
810         pCap->halTxChainMask = ath_hal_eepromGet(ah, AR_EEP_TXMASK, AH_NULL);
811         /* XXX CB71 uses GPIO 0 to indicate 3 rx chains */
812         pCap->halRxChainMask = ath_hal_eepromGet(ah, AR_EEP_RXMASK, AH_NULL);
813         pCap->halRtsAggrLimit = 8*1024;         /* Owl 2.0 limit */
814         pCap->halMbssidAggrSupport = AH_TRUE;
815         pCap->halForcePpmSupport = AH_TRUE;
816         pCap->halEnhancedPmSupport = AH_TRUE;
817         pCap->halBssidMatchSupport = AH_TRUE;
818
819         if (ath_hal_eepromGetFlag(ah, AR_EEP_RFKILL) &&
820             ath_hal_eepromGet(ah, AR_EEP_RFSILENT, &ahpriv->ah_rfsilent) == HAL_OK) {
821                 /* NB: enabled by default */
822                 ahpriv->ah_rfkillEnabled = AH_TRUE;
823                 pCap->halRfSilentSupport = AH_TRUE;
824         }
825
826         ahpriv->ah_rxornIsFatal = AH_FALSE;
827
828         return AH_TRUE;
829 }
830
831 static const char*
832 ar5416Probe(uint16_t vendorid, uint16_t devid)
833 {
834         if (vendorid == ATHEROS_VENDOR_ID &&
835             (devid == AR5416_DEVID_PCI || devid == AR5416_DEVID_PCIE))
836                 return "Atheros 5416";
837         return AH_NULL;
838 }
839 AH_CHIP(AR5416, ar5416Probe, ar5416Attach);