kernel: Use NELEM() where we can.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5212 / ar5212_misc.c
1 /*
2  * Copyright (c) 2002-2009 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/ar5212/ar5212_misc.c 188866 2009-02-20 22:06:58Z sam $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_devid.h"
25 #ifdef AH_DEBUG
26 #include "ah_desc.h"                    /* NB: for HAL_PHYERR* */
27 #endif
28
29 #include "ar5212/ar5212.h"
30 #include "ar5212/ar5212reg.h"
31 #include "ar5212/ar5212phy.h"
32
33 #include "ah_eeprom_v3.h"
34
35 #define AR_NUM_GPIO     6               /* 6 GPIO pins */
36 #define AR_GPIOD_MASK   0x0000002F      /* GPIO data reg r/w mask */
37
38 void
39 ar5212GetMacAddress(struct ath_hal *ah, uint8_t *mac)
40 {
41         struct ath_hal_5212 *ahp = AH5212(ah);
42
43         OS_MEMCPY(mac, ahp->ah_macaddr, IEEE80211_ADDR_LEN);
44 }
45
46 HAL_BOOL
47 ar5212SetMacAddress(struct ath_hal *ah, const uint8_t *mac)
48 {
49         struct ath_hal_5212 *ahp = AH5212(ah);
50
51         OS_MEMCPY(ahp->ah_macaddr, mac, IEEE80211_ADDR_LEN);
52         return AH_TRUE;
53 }
54
55 void
56 ar5212GetBssIdMask(struct ath_hal *ah, uint8_t *mask)
57 {
58         struct ath_hal_5212 *ahp = AH5212(ah);
59
60         OS_MEMCPY(mask, ahp->ah_bssidmask, IEEE80211_ADDR_LEN);
61 }
62
63 HAL_BOOL
64 ar5212SetBssIdMask(struct ath_hal *ah, const uint8_t *mask)
65 {
66         struct ath_hal_5212 *ahp = AH5212(ah);
67
68         /* save it since it must be rewritten on reset */
69         OS_MEMCPY(ahp->ah_bssidmask, mask, IEEE80211_ADDR_LEN);
70
71         OS_REG_WRITE(ah, AR_BSSMSKL, LE_READ_4(ahp->ah_bssidmask));
72         OS_REG_WRITE(ah, AR_BSSMSKU, LE_READ_2(ahp->ah_bssidmask + 4));
73         return AH_TRUE;
74 }
75
76 /*
77  * Attempt to change the cards operating regulatory domain to the given value
78  */
79 HAL_BOOL
80 ar5212SetRegulatoryDomain(struct ath_hal *ah,
81         uint16_t regDomain, HAL_STATUS *status)
82 {
83         HAL_STATUS ecode;
84
85         if (AH_PRIVATE(ah)->ah_currentRD == regDomain) {
86                 ecode = HAL_EINVAL;
87                 goto bad;
88         }
89         if (ath_hal_eepromGetFlag(ah, AR_EEP_WRITEPROTECT)) {
90                 ecode = HAL_EEWRITE;
91                 goto bad;
92         }
93 #ifdef AH_SUPPORT_WRITE_REGDOMAIN
94         if (ath_hal_eepromWrite(ah, AR_EEPROM_REG_DOMAIN, regDomain)) {
95                 HALDEBUG(ah, HAL_DEBUG_ANY,
96                     "%s: set regulatory domain to %u (0x%x)\n",
97                     __func__, regDomain, regDomain);
98                 AH_PRIVATE(ah)->ah_currentRD = regDomain;
99                 return AH_TRUE;
100         }
101 #endif
102         ecode = HAL_EIO;
103 bad:
104         if (status)
105                 *status = ecode;
106         return AH_FALSE;
107 }
108
109 /*
110  * Return the wireless modes (a,b,g,t) supported by hardware.
111  *
112  * This value is what is actually supported by the hardware
113  * and is unaffected by regulatory/country code settings.
114  */
115 u_int
116 ar5212GetWirelessModes(struct ath_hal *ah)
117 {
118         u_int mode = 0;
119
120         if (ath_hal_eepromGetFlag(ah, AR_EEP_AMODE)) {
121                 mode = HAL_MODE_11A;
122                 if (!ath_hal_eepromGetFlag(ah, AR_EEP_TURBO5DISABLE))
123                         mode |= HAL_MODE_TURBO | HAL_MODE_108A;
124                 if (AH_PRIVATE(ah)->ah_caps.halChanHalfRate)
125                         mode |= HAL_MODE_11A_HALF_RATE;
126                 if (AH_PRIVATE(ah)->ah_caps.halChanQuarterRate)
127                         mode |= HAL_MODE_11A_QUARTER_RATE;
128         }
129         if (ath_hal_eepromGetFlag(ah, AR_EEP_BMODE))
130                 mode |= HAL_MODE_11B;
131         if (ath_hal_eepromGetFlag(ah, AR_EEP_GMODE) &&
132             AH_PRIVATE(ah)->ah_subvendorid != AR_SUBVENDOR_ID_NOG) {
133                 mode |= HAL_MODE_11G;
134                 if (!ath_hal_eepromGetFlag(ah, AR_EEP_TURBO2DISABLE))
135                         mode |= HAL_MODE_108G;
136                 if (AH_PRIVATE(ah)->ah_caps.halChanHalfRate)
137                         mode |= HAL_MODE_11G_HALF_RATE;
138                 if (AH_PRIVATE(ah)->ah_caps.halChanQuarterRate)
139                         mode |= HAL_MODE_11G_QUARTER_RATE;
140         }
141         return mode;
142 }
143
144 /*
145  * Set the interrupt and GPIO values so the ISR can disable RF
146  * on a switch signal.  Assumes GPIO port and interrupt polarity
147  * are set prior to call.
148  */
149 void
150 ar5212EnableRfKill(struct ath_hal *ah)
151 {
152         uint16_t rfsilent = AH_PRIVATE(ah)->ah_rfsilent;
153         int select = MS(rfsilent, AR_EEPROM_RFSILENT_GPIO_SEL);
154         int polarity = MS(rfsilent, AR_EEPROM_RFSILENT_POLARITY);
155
156         /*
157          * Configure the desired GPIO port for input
158          * and enable baseband rf silence.
159          */
160         ath_hal_gpioCfgInput(ah, select);
161         OS_REG_SET_BIT(ah, AR_PHY(0), 0x00002000);
162         /*
163          * If radio disable switch connection to GPIO bit x is enabled
164          * program GPIO interrupt.
165          * If rfkill bit on eeprom is 1, setupeeprommap routine has already
166          * verified that it is a later version of eeprom, it has a place for
167          * rfkill bit and it is set to 1, indicating that GPIO bit x hardware
168          * connection is present.
169          */
170         ath_hal_gpioSetIntr(ah, select,
171             (ath_hal_gpioGet(ah, select) == polarity ? !polarity : polarity));
172 }
173
174 /*
175  * Change the LED blinking pattern to correspond to the connectivity
176  */
177 void
178 ar5212SetLedState(struct ath_hal *ah, HAL_LED_STATE state)
179 {
180         static const uint32_t ledbits[8] = {
181                 AR_PCICFG_LEDCTL_NONE,  /* HAL_LED_INIT */
182                 AR_PCICFG_LEDCTL_PEND,  /* HAL_LED_SCAN */
183                 AR_PCICFG_LEDCTL_PEND,  /* HAL_LED_AUTH */
184                 AR_PCICFG_LEDCTL_ASSOC, /* HAL_LED_ASSOC*/
185                 AR_PCICFG_LEDCTL_ASSOC, /* HAL_LED_RUN */
186                 AR_PCICFG_LEDCTL_NONE,
187                 AR_PCICFG_LEDCTL_NONE,
188                 AR_PCICFG_LEDCTL_NONE,
189         };
190         uint32_t bits;
191
192         bits = OS_REG_READ(ah, AR_PCICFG);
193         if (IS_2417(ah)) {
194                 /*
195                  * Enable LED for Nala. There is a bit marked reserved
196                  * that must be set and we also turn on the power led.
197                  * Because we mark s/w LED control setting the control
198                  * status bits below is meangless (the driver must flash
199                  * the LED(s) using the GPIO lines).
200                  */
201                 bits = (bits &~ AR_PCICFG_LEDMODE)
202                      | SM(AR_PCICFG_LEDMODE_POWON, AR_PCICFG_LEDMODE)
203 #if 0
204                      | SM(AR_PCICFG_LEDMODE_NETON, AR_PCICFG_LEDMODE)
205 #endif
206                      | 0x08000000;
207         }
208         bits = (bits &~ AR_PCICFG_LEDCTL)
209              | SM(ledbits[state & 0x7], AR_PCICFG_LEDCTL);
210         OS_REG_WRITE(ah, AR_PCICFG, bits);
211 }
212
213 /*
214  * Change association related fields programmed into the hardware.
215  * Writing a valid BSSID to the hardware effectively enables the hardware
216  * to synchronize its TSF to the correct beacons and receive frames coming
217  * from that BSSID. It is called by the SME JOIN operation.
218  */
219 void
220 ar5212WriteAssocid(struct ath_hal *ah, const uint8_t *bssid, uint16_t assocId)
221 {
222         struct ath_hal_5212 *ahp = AH5212(ah);
223
224         /* XXX save bssid for possible re-use on reset */
225         OS_MEMCPY(ahp->ah_bssid, bssid, IEEE80211_ADDR_LEN);
226         OS_REG_WRITE(ah, AR_BSS_ID0, LE_READ_4(ahp->ah_bssid));
227         OS_REG_WRITE(ah, AR_BSS_ID1, LE_READ_2(ahp->ah_bssid+4) |
228                                      ((assocId & 0x3fff)<<AR_BSS_ID1_AID_S));
229 }
230
231 /*
232  * Get the current hardware tsf for stamlme
233  */
234 uint64_t
235 ar5212GetTsf64(struct ath_hal *ah)
236 {
237         uint32_t low1, low2, u32;
238
239         /* sync multi-word read */
240         low1 = OS_REG_READ(ah, AR_TSF_L32);
241         u32 = OS_REG_READ(ah, AR_TSF_U32);
242         low2 = OS_REG_READ(ah, AR_TSF_L32);
243         if (low2 < low1) {      /* roll over */
244                 /*
245                  * If we are not preempted this will work.  If we are
246                  * then we re-reading AR_TSF_U32 does no good as the
247                  * low bits will be meaningless.  Likewise reading
248                  * L32, U32, U32, then comparing the last two reads
249                  * to check for rollover doesn't help if preempted--so
250                  * we take this approach as it costs one less PCI read
251                  * which can be noticeable when doing things like
252                  * timestamping packets in monitor mode.
253                  */
254                 u32++;
255         }
256         return (((uint64_t) u32) << 32) | ((uint64_t) low2);
257 }
258
259 /*
260  * Get the current hardware tsf for stamlme
261  */
262 uint32_t
263 ar5212GetTsf32(struct ath_hal *ah)
264 {
265         return OS_REG_READ(ah, AR_TSF_L32);
266 }
267
268 /*
269  * Reset the current hardware tsf for stamlme.
270  */
271 void
272 ar5212ResetTsf(struct ath_hal *ah)
273 {
274
275         uint32_t val = OS_REG_READ(ah, AR_BEACON);
276
277         OS_REG_WRITE(ah, AR_BEACON, val | AR_BEACON_RESET_TSF);
278         /*
279          * When resetting the TSF, write twice to the
280          * corresponding register; each write to the RESET_TSF bit toggles
281          * the internal signal to cause a reset of the TSF - but if the signal
282          * is left high, it will reset the TSF on the next chip reset also!
283          * writing the bit an even number of times fixes this issue
284          */
285         OS_REG_WRITE(ah, AR_BEACON, val | AR_BEACON_RESET_TSF);
286 }
287
288 /*
289  * Set or clear hardware basic rate bit
290  * Set hardware basic rate set if basic rate is found
291  * and basic rate is equal or less than 2Mbps
292  */
293 void
294 ar5212SetBasicRate(struct ath_hal *ah, HAL_RATE_SET *rs)
295 {
296         const struct ieee80211_channel *chan = AH_PRIVATE(ah)->ah_curchan;
297         uint32_t reg;
298         uint8_t xset;
299         int i;
300
301         if (chan == AH_NULL || !IEEE80211_IS_CHAN_CCK(chan))
302                 return;
303         xset = 0;
304         for (i = 0; i < rs->rs_count; i++) {
305                 uint8_t rset = rs->rs_rates[i];
306                 /* Basic rate defined? */
307                 if ((rset & 0x80) && (rset &= 0x7f) >= xset)
308                         xset = rset;
309         }
310         /*
311          * Set the h/w bit to reflect whether or not the basic
312          * rate is found to be equal or less than 2Mbps.
313          */
314         reg = OS_REG_READ(ah, AR_STA_ID1);
315         if (xset && xset/2 <= 2)
316                 OS_REG_WRITE(ah, AR_STA_ID1, reg | AR_STA_ID1_BASE_RATE_11B);
317         else
318                 OS_REG_WRITE(ah, AR_STA_ID1, reg &~ AR_STA_ID1_BASE_RATE_11B);
319 }
320
321 /*
322  * Grab a semi-random value from hardware registers - may not
323  * change often
324  */
325 uint32_t
326 ar5212GetRandomSeed(struct ath_hal *ah)
327 {
328         uint32_t nf;
329
330         nf = (OS_REG_READ(ah, AR_PHY(25)) >> 19) & 0x1ff;
331         if (nf & 0x100)
332                 nf = 0 - ((nf ^ 0x1ff) + 1);
333         return (OS_REG_READ(ah, AR_TSF_U32) ^
334                 OS_REG_READ(ah, AR_TSF_L32) ^ nf);
335 }
336
337 /*
338  * Detect if our card is present
339  */
340 HAL_BOOL
341 ar5212DetectCardPresent(struct ath_hal *ah)
342 {
343         uint16_t macVersion, macRev;
344         uint32_t v;
345
346         /*
347          * Read the Silicon Revision register and compare that
348          * to what we read at attach time.  If the same, we say
349          * a card/device is present.
350          */
351         v = OS_REG_READ(ah, AR_SREV) & AR_SREV_ID;
352         macVersion = v >> AR_SREV_ID_S;
353         macRev = v & AR_SREV_REVISION;
354         return (AH_PRIVATE(ah)->ah_macVersion == macVersion &&
355                 AH_PRIVATE(ah)->ah_macRev == macRev);
356 }
357
358 void
359 ar5212EnableMibCounters(struct ath_hal *ah)
360 {
361         /* NB: this just resets the mib counter machinery */
362         OS_REG_WRITE(ah, AR_MIBC,
363             ~(AR_MIBC_COW | AR_MIBC_FMC | AR_MIBC_CMC | AR_MIBC_MCS) & 0x0f);
364 }
365
366 void 
367 ar5212DisableMibCounters(struct ath_hal *ah)
368 {
369         OS_REG_WRITE(ah, AR_MIBC,  AR_MIBC | AR_MIBC_CMC);
370 }
371
372 /*
373  * Update MIB Counters
374  */
375 void
376 ar5212UpdateMibCounters(struct ath_hal *ah, HAL_MIB_STATS* stats)
377 {
378         stats->ackrcv_bad += OS_REG_READ(ah, AR_ACK_FAIL);
379         stats->rts_bad    += OS_REG_READ(ah, AR_RTS_FAIL);
380         stats->fcs_bad    += OS_REG_READ(ah, AR_FCS_FAIL);
381         stats->rts_good   += OS_REG_READ(ah, AR_RTS_OK);
382         stats->beacons    += OS_REG_READ(ah, AR_BEACON_CNT);
383 }
384
385 /*
386  * Detect if the HW supports spreading a CCK signal on channel 14
387  */
388 HAL_BOOL
389 ar5212IsJapanChannelSpreadSupported(struct ath_hal *ah)
390 {
391         return AH_TRUE;
392 }
393
394 /*
395  * Get the rssi of frame curently being received.
396  */
397 uint32_t
398 ar5212GetCurRssi(struct ath_hal *ah)
399 {
400         return (OS_REG_READ(ah, AR_PHY_CURRENT_RSSI) & 0xff);
401 }
402
403 u_int
404 ar5212GetDefAntenna(struct ath_hal *ah)
405 {   
406         return (OS_REG_READ(ah, AR_DEF_ANTENNA) & 0x7);
407 }   
408
409 void
410 ar5212SetDefAntenna(struct ath_hal *ah, u_int antenna)
411 {
412         OS_REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
413 }
414
415 HAL_ANT_SETTING
416 ar5212GetAntennaSwitch(struct ath_hal *ah)
417 {
418         return AH5212(ah)->ah_antControl;
419 }
420
421 HAL_BOOL
422 ar5212SetAntennaSwitch(struct ath_hal *ah, HAL_ANT_SETTING setting)
423 {
424         struct ath_hal_5212 *ahp = AH5212(ah);
425         const struct ieee80211_channel *chan = AH_PRIVATE(ah)->ah_curchan;
426
427         if (!ahp->ah_phyPowerOn || chan == AH_NULL) {
428                 /* PHY powered off, just stash settings */
429                 ahp->ah_antControl = setting;
430                 ahp->ah_diversity = (setting == HAL_ANT_VARIABLE);
431                 return AH_TRUE;
432         }
433         return ar5212SetAntennaSwitchInternal(ah, setting, chan);
434 }
435
436 HAL_BOOL
437 ar5212IsSleepAfterBeaconBroken(struct ath_hal *ah)
438 {
439         return AH_TRUE;
440 }
441
442 HAL_BOOL
443 ar5212SetSifsTime(struct ath_hal *ah, u_int us)
444 {
445         struct ath_hal_5212 *ahp = AH5212(ah);
446
447         if (us > ath_hal_mac_usec(ah, 0xffff)) {
448                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad SIFS time %u\n",
449                     __func__, us);
450                 ahp->ah_sifstime = (u_int) -1;  /* restore default handling */
451                 return AH_FALSE;
452         } else {
453                 /* convert to system clocks */
454                 OS_REG_WRITE(ah, AR_D_GBL_IFS_SIFS, ath_hal_mac_clks(ah, us-2));
455                 ahp->ah_slottime = us;
456                 return AH_TRUE;
457         }
458 }
459
460 u_int
461 ar5212GetSifsTime(struct ath_hal *ah)
462 {
463         u_int clks = OS_REG_READ(ah, AR_D_GBL_IFS_SIFS) & 0xffff;
464         return ath_hal_mac_usec(ah, clks)+2;    /* convert from system clocks */
465 }
466
467 HAL_BOOL
468 ar5212SetSlotTime(struct ath_hal *ah, u_int us)
469 {
470         struct ath_hal_5212 *ahp = AH5212(ah);
471
472         if (us < HAL_SLOT_TIME_6 || us > ath_hal_mac_usec(ah, 0xffff)) {
473                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad slot time %u\n",
474                     __func__, us);
475                 ahp->ah_slottime = (u_int) -1;  /* restore default handling */
476                 return AH_FALSE;
477         } else {
478                 /* convert to system clocks */
479                 OS_REG_WRITE(ah, AR_D_GBL_IFS_SLOT, ath_hal_mac_clks(ah, us));
480                 ahp->ah_slottime = us;
481                 return AH_TRUE;
482         }
483 }
484
485 u_int
486 ar5212GetSlotTime(struct ath_hal *ah)
487 {
488         u_int clks = OS_REG_READ(ah, AR_D_GBL_IFS_SLOT) & 0xffff;
489         return ath_hal_mac_usec(ah, clks);      /* convert from system clocks */
490 }
491
492 HAL_BOOL
493 ar5212SetAckTimeout(struct ath_hal *ah, u_int us)
494 {
495         struct ath_hal_5212 *ahp = AH5212(ah);
496
497         if (us > ath_hal_mac_usec(ah, MS(0xffffffff, AR_TIME_OUT_ACK))) {
498                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad ack timeout %u\n",
499                     __func__, us);
500                 ahp->ah_acktimeout = (u_int) -1; /* restore default handling */
501                 return AH_FALSE;
502         } else {
503                 /* convert to system clocks */
504                 OS_REG_RMW_FIELD(ah, AR_TIME_OUT,
505                         AR_TIME_OUT_ACK, ath_hal_mac_clks(ah, us));
506                 ahp->ah_acktimeout = us;
507                 return AH_TRUE;
508         }
509 }
510
511 u_int
512 ar5212GetAckTimeout(struct ath_hal *ah)
513 {
514         u_int clks = MS(OS_REG_READ(ah, AR_TIME_OUT), AR_TIME_OUT_ACK);
515         return ath_hal_mac_usec(ah, clks);      /* convert from system clocks */
516 }
517
518 u_int
519 ar5212GetAckCTSRate(struct ath_hal *ah)
520 {
521         return ((AH5212(ah)->ah_staId1Defaults & AR_STA_ID1_ACKCTS_6MB) == 0);
522 }
523
524 HAL_BOOL
525 ar5212SetAckCTSRate(struct ath_hal *ah, u_int high)
526 {
527         struct ath_hal_5212 *ahp = AH5212(ah);
528
529         if (high) {
530                 OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_ACKCTS_6MB);
531                 ahp->ah_staId1Defaults &= ~AR_STA_ID1_ACKCTS_6MB;
532         } else {
533                 OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_ACKCTS_6MB);
534                 ahp->ah_staId1Defaults |= AR_STA_ID1_ACKCTS_6MB;
535         }
536         return AH_TRUE;
537 }
538
539 HAL_BOOL
540 ar5212SetCTSTimeout(struct ath_hal *ah, u_int us)
541 {
542         struct ath_hal_5212 *ahp = AH5212(ah);
543
544         if (us > ath_hal_mac_usec(ah, MS(0xffffffff, AR_TIME_OUT_CTS))) {
545                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad cts timeout %u\n",
546                     __func__, us);
547                 ahp->ah_ctstimeout = (u_int) -1; /* restore default handling */
548                 return AH_FALSE;
549         } else {
550                 /* convert to system clocks */
551                 OS_REG_RMW_FIELD(ah, AR_TIME_OUT,
552                         AR_TIME_OUT_CTS, ath_hal_mac_clks(ah, us));
553                 ahp->ah_ctstimeout = us;
554                 return AH_TRUE;
555         }
556 }
557
558 u_int
559 ar5212GetCTSTimeout(struct ath_hal *ah)
560 {
561         u_int clks = MS(OS_REG_READ(ah, AR_TIME_OUT), AR_TIME_OUT_CTS);
562         return ath_hal_mac_usec(ah, clks);      /* convert from system clocks */
563 }
564
565 /* Setup decompression for given key index */
566 HAL_BOOL
567 ar5212SetDecompMask(struct ath_hal *ah, uint16_t keyidx, int en)
568 {
569         struct ath_hal_5212 *ahp = AH5212(ah);
570
571         if (keyidx >= HAL_DECOMP_MASK_SIZE)
572                 return HAL_EINVAL; 
573         OS_REG_WRITE(ah, AR_DCM_A, keyidx);
574         OS_REG_WRITE(ah, AR_DCM_D, en ? AR_DCM_D_EN : 0);
575         ahp->ah_decompMask[keyidx] = en;
576
577         return AH_TRUE;
578 }
579
580 /* Setup coverage class */
581 void
582 ar5212SetCoverageClass(struct ath_hal *ah, uint8_t coverageclass, int now)
583 {
584         uint32_t slot, timeout, eifs;
585         u_int clkRate;
586
587         AH_PRIVATE(ah)->ah_coverageClass = coverageclass;
588
589         if (now) {
590                 if (AH_PRIVATE(ah)->ah_coverageClass == 0)
591                         return;
592
593                 /* Don't apply coverage class to non A channels */
594                 if (!IEEE80211_IS_CHAN_A(AH_PRIVATE(ah)->ah_curchan))
595                         return;
596
597                 /* Get core clock rate */
598                 clkRate = ath_hal_mac_clks(ah, 1);
599
600                 /* Compute EIFS */
601                 slot = coverageclass * 3 * clkRate;
602                 eifs = coverageclass * 6 * clkRate;
603                 if (IEEE80211_IS_CHAN_HALF(AH_PRIVATE(ah)->ah_curchan)) {
604                         slot += IFS_SLOT_HALF_RATE;
605                         eifs += IFS_EIFS_HALF_RATE;
606                 } else if (IEEE80211_IS_CHAN_QUARTER(AH_PRIVATE(ah)->ah_curchan)) {
607                         slot += IFS_SLOT_QUARTER_RATE;
608                         eifs += IFS_EIFS_QUARTER_RATE;
609                 } else { /* full rate */
610                         slot += IFS_SLOT_FULL_RATE;
611                         eifs += IFS_EIFS_FULL_RATE;
612                 }
613
614                 /*
615                  * Add additional time for air propagation for ACK and CTS
616                  * timeouts. This value is in core clocks.
617                  */
618                 timeout = ACK_CTS_TIMEOUT_11A + (coverageclass * 3 * clkRate);
619         
620                 /*
621                  * Write the values: slot, eifs, ack/cts timeouts.
622                  */
623                 OS_REG_WRITE(ah, AR_D_GBL_IFS_SLOT, slot);
624                 OS_REG_WRITE(ah, AR_D_GBL_IFS_EIFS, eifs);
625                 OS_REG_WRITE(ah, AR_TIME_OUT,
626                           SM(timeout, AR_TIME_OUT_CTS)
627                         | SM(timeout, AR_TIME_OUT_ACK));
628         }
629 }
630
631 void
632 ar5212SetPCUConfig(struct ath_hal *ah)
633 {
634         ar5212SetOperatingMode(ah, AH_PRIVATE(ah)->ah_opmode);
635 }
636
637 /*
638  * Return whether an external 32KHz crystal should be used
639  * to reduce power consumption when sleeping.  We do so if
640  * the crystal is present (obtained from EEPROM) and if we
641  * are not running as an AP and are configured to use it.
642  */
643 HAL_BOOL
644 ar5212Use32KHzclock(struct ath_hal *ah, HAL_OPMODE opmode)
645 {
646         if (opmode != HAL_M_HOSTAP) {
647                 struct ath_hal_5212 *ahp = AH5212(ah);
648                 return ath_hal_eepromGetFlag(ah, AR_EEP_32KHZCRYSTAL) &&
649                        (ahp->ah_enable32kHzClock == USE_32KHZ ||
650                         ahp->ah_enable32kHzClock == AUTO_32KHZ);
651         } else
652                 return AH_FALSE;
653 }
654
655 /*
656  * If 32KHz clock exists, use it to lower power consumption during sleep
657  *
658  * Note: If clock is set to 32 KHz, delays on accessing certain
659  *       baseband registers (27-31, 124-127) are required.
660  */
661 void
662 ar5212SetupClock(struct ath_hal *ah, HAL_OPMODE opmode)
663 {
664         if (ar5212Use32KHzclock(ah, opmode)) {
665                 /*
666                  * Enable clocks to be turned OFF in BB during sleep
667                  * and also enable turning OFF 32MHz/40MHz Refclk
668                  * from A2.
669                  */
670                 OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_CONTROL, 0x1f);
671                 OS_REG_WRITE(ah, AR_PHY_REFCLKPD,
672                     IS_RAD5112_ANY(ah) || IS_5413(ah) ? 0x14 : 0x18);
673                 OS_REG_RMW_FIELD(ah, AR_USEC, AR_USEC_USEC32, 1);
674                 OS_REG_WRITE(ah, AR_TSF_PARM, 61);  /* 32 KHz TSF incr */
675                 OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_SEL, 1);
676
677                 if (IS_2413(ah) || IS_5413(ah) || IS_2417(ah)) {
678                         OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_LIMIT,   0x26);
679                         OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL,        0x0d);
680                         OS_REG_WRITE(ah, AR_PHY_M_SLEEP,           0x07);
681                         OS_REG_WRITE(ah, AR_PHY_REFCLKDLY,         0x3f);
682                         /* # Set sleep clock rate to 32 KHz. */
683                         OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_RATE_IND, 0x2);
684                 } else {
685                         OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_LIMIT,   0x0a);
686                         OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL,        0x0c);
687                         OS_REG_WRITE(ah, AR_PHY_M_SLEEP,           0x03);
688                         OS_REG_WRITE(ah, AR_PHY_REFCLKDLY,         0x20);
689                         OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_RATE_IND, 0x3);
690                 }
691         } else {
692                 OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_RATE_IND, 0x0);
693                 OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_SEL, 0);
694
695                 OS_REG_WRITE(ah, AR_TSF_PARM, 1);       /* 32MHz TSF inc */
696
697                 OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_CONTROL, 0x1f);
698                 OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_LIMIT,   0x7f);
699
700                 if (IS_2417(ah))
701                         OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL, 0x0a);
702                 else if (IS_HB63(ah))
703                         OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL, 0x32);
704                 else
705                         OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL, 0x0e);
706                 OS_REG_WRITE(ah, AR_PHY_M_SLEEP,           0x0c);
707                 OS_REG_WRITE(ah, AR_PHY_REFCLKDLY,         0xff);
708                 OS_REG_WRITE(ah, AR_PHY_REFCLKPD,
709                     IS_RAD5112_ANY(ah) || IS_5413(ah) || IS_2417(ah) ? 0x14 : 0x18);
710                 OS_REG_RMW_FIELD(ah, AR_USEC, AR_USEC_USEC32,
711                     IS_RAD5112_ANY(ah) || IS_5413(ah) ? 39 : 31);
712         }
713 }
714
715 /*
716  * If 32KHz clock exists, turn it off and turn back on the 32Mhz
717  */
718 void
719 ar5212RestoreClock(struct ath_hal *ah, HAL_OPMODE opmode)
720 {
721         if (ar5212Use32KHzclock(ah, opmode)) {
722                 /* # Set sleep clock rate back to 32 MHz. */
723                 OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_RATE_IND, 0);
724                 OS_REG_RMW_FIELD(ah, AR_PCICFG, AR_PCICFG_SCLK_SEL, 0);
725
726                 OS_REG_WRITE(ah, AR_TSF_PARM, 1);       /* 32 MHz TSF incr */
727                 OS_REG_RMW_FIELD(ah, AR_USEC, AR_USEC_USEC32,
728                     IS_RAD5112_ANY(ah) || IS_5413(ah) ? 39 : 31);
729
730                 /*
731                  * Restore BB registers to power-on defaults
732                  */
733                 OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_CONTROL, 0x1f);
734                 OS_REG_WRITE(ah, AR_PHY_SLEEP_CTR_LIMIT,   0x7f);
735                 OS_REG_WRITE(ah, AR_PHY_SLEEP_SCAL,        0x0e);
736                 OS_REG_WRITE(ah, AR_PHY_M_SLEEP,           0x0c);
737                 OS_REG_WRITE(ah, AR_PHY_REFCLKDLY,         0xff);
738                 OS_REG_WRITE(ah, AR_PHY_REFCLKPD,
739                     IS_RAD5112_ANY(ah) || IS_5413(ah) ?  0x14 : 0x18);
740         }
741 }
742
743 /*
744  * Adjust NF based on statistical values for 5GHz frequencies.
745  * Default method: this may be overridden by the rf backend.
746  */
747 int16_t
748 ar5212GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)
749 {
750         static const struct {
751                 uint16_t freqLow;
752                 int16_t   adjust;
753         } adjustDef[] = {
754                 { 5790, 11 },   /* NB: ordered high -> low */
755                 { 5730, 10 },
756                 { 5690,  9 },
757                 { 5660,  8 },
758                 { 5610,  7 },
759                 { 5530,  5 },
760                 { 5450,  4 },
761                 { 5379,  2 },
762                 { 5209,  0 },
763                 { 3000,  1 },
764                 {    0,  0 },
765         };
766         int i;
767
768         for (i = 0; c->channel <= adjustDef[i].freqLow; i++)
769                 ;
770         return adjustDef[i].adjust;
771 }
772
773 HAL_STATUS
774 ar5212GetCapability(struct ath_hal *ah, HAL_CAPABILITY_TYPE type,
775         uint32_t capability, uint32_t *result)
776 {
777 #define MACVERSION(ah)  AH_PRIVATE(ah)->ah_macVersion
778         struct ath_hal_5212 *ahp = AH5212(ah);
779         const HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
780         const struct ar5212AniState *ani;
781
782         switch (type) {
783         case HAL_CAP_CIPHER:            /* cipher handled in hardware */
784                 switch (capability) {
785                 case HAL_CIPHER_AES_CCM:
786                         return pCap->halCipherAesCcmSupport ?
787                                 HAL_OK : HAL_ENOTSUPP;
788                 case HAL_CIPHER_AES_OCB:
789                 case HAL_CIPHER_TKIP:
790                 case HAL_CIPHER_WEP:
791                 case HAL_CIPHER_MIC:
792                 case HAL_CIPHER_CLR:
793                         return HAL_OK;
794                 default:
795                         return HAL_ENOTSUPP;
796                 }
797         case HAL_CAP_TKIP_MIC:          /* handle TKIP MIC in hardware */
798                 switch (capability) {
799                 case 0:                 /* hardware capability */
800                         return HAL_OK;
801                 case 1:
802                         return (ahp->ah_staId1Defaults &
803                             AR_STA_ID1_CRPT_MIC_ENABLE) ?  HAL_OK : HAL_ENXIO;
804                 }
805                 return HAL_EINVAL;
806         case HAL_CAP_TKIP_SPLIT:        /* hardware TKIP uses split keys */
807                 switch (capability) {
808                 case 0:                 /* hardware capability */
809                         return pCap->halTkipMicTxRxKeySupport ?
810                                 HAL_ENXIO : HAL_OK;
811                 case 1:                 /* current setting */
812                         return (ahp->ah_miscMode &
813                             AR_MISC_MODE_MIC_NEW_LOC_ENABLE) ? HAL_ENXIO : HAL_OK;
814                 }
815                 return HAL_EINVAL;
816         case HAL_CAP_WME_TKIPMIC:       /* hardware can do TKIP MIC w/ WMM */
817                 /* XXX move to capability bit */
818                 return MACVERSION(ah) > AR_SREV_VERSION_VENICE ||
819                     (MACVERSION(ah) == AR_SREV_VERSION_VENICE &&
820                      AH_PRIVATE(ah)->ah_macRev >= 8) ? HAL_OK : HAL_ENOTSUPP;
821         case HAL_CAP_DIVERSITY:         /* hardware supports fast diversity */
822                 switch (capability) {
823                 case 0:                 /* hardware capability */
824                         return HAL_OK;
825                 case 1:                 /* current setting */
826                         return ahp->ah_diversity ? HAL_OK : HAL_ENXIO;
827                 }
828                 return HAL_EINVAL;
829         case HAL_CAP_DIAG:
830                 *result = AH_PRIVATE(ah)->ah_diagreg;
831                 return HAL_OK;
832         case HAL_CAP_TPC:
833                 switch (capability) {
834                 case 0:                 /* hardware capability */
835                         return HAL_OK;
836                 case 1:
837                         return ahp->ah_tpcEnabled ? HAL_OK : HAL_ENXIO;
838                 }
839                 return HAL_OK;
840         case HAL_CAP_PHYDIAG:           /* radar pulse detection capability */
841                 switch (capability) {
842                 case HAL_CAP_RADAR:
843                         return ath_hal_eepromGetFlag(ah, AR_EEP_AMODE) ?
844                             HAL_OK: HAL_ENXIO;
845                 case HAL_CAP_AR:
846                         return (ath_hal_eepromGetFlag(ah, AR_EEP_GMODE) ||
847                             ath_hal_eepromGetFlag(ah, AR_EEP_BMODE)) ?
848                                HAL_OK: HAL_ENXIO;
849                 }
850                 return HAL_ENXIO;
851         case HAL_CAP_MCAST_KEYSRCH:     /* multicast frame keycache search */
852                 switch (capability) {
853                 case 0:                 /* hardware capability */
854                         return HAL_OK;
855                 case 1:
856                         return (ahp->ah_staId1Defaults &
857                             AR_STA_ID1_MCAST_KSRCH) ? HAL_OK : HAL_ENXIO;
858                 }
859                 return HAL_EINVAL;
860         case HAL_CAP_TSF_ADJUST:        /* hardware has beacon tsf adjust */
861                 switch (capability) {
862                 case 0:                 /* hardware capability */
863                         return pCap->halTsfAddSupport ? HAL_OK : HAL_ENOTSUPP;
864                 case 1:
865                         return (ahp->ah_miscMode & AR_MISC_MODE_TX_ADD_TSF) ?
866                                 HAL_OK : HAL_ENXIO;
867                 }
868                 return HAL_EINVAL;
869         case HAL_CAP_TPC_ACK:
870                 *result = MS(ahp->ah_macTPC, AR_TPC_ACK);
871                 return HAL_OK;
872         case HAL_CAP_TPC_CTS:
873                 *result = MS(ahp->ah_macTPC, AR_TPC_CTS);
874                 return HAL_OK;
875         case HAL_CAP_INTMIT:            /* interference mitigation */
876                 switch (capability) {
877                 case 0:                 /* hardware capability */
878                         return HAL_OK;
879                 case 1:
880                         return (ahp->ah_procPhyErr & HAL_ANI_ENA) ?
881                                 HAL_OK : HAL_ENXIO;
882                 case 2:                 /* HAL_ANI_NOISE_IMMUNITY_LEVEL */
883                 case 3:                 /* HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION */
884                 case 4:                 /* HAL_ANI_CCK_WEAK_SIGNAL_THR */
885                 case 5:                 /* HAL_ANI_FIRSTEP_LEVEL */
886                 case 6:                 /* HAL_ANI_SPUR_IMMUNITY_LEVEL */
887                         ani = ar5212AniGetCurrentState(ah);
888                         if (ani == AH_NULL)
889                                 return HAL_ENXIO;
890                         switch (capability) {
891                         case 2: *result = ani->noiseImmunityLevel; break;
892                         case 3: *result = !ani->ofdmWeakSigDetectOff; break;
893                         case 4: *result = ani->cckWeakSigThreshold; break;
894                         case 5: *result = ani->firstepLevel; break;
895                         case 6: *result = ani->spurImmunityLevel; break;
896                         }
897                         return HAL_OK;
898                 }
899                 return HAL_EINVAL;
900         default:
901                 return ath_hal_getcapability(ah, type, capability, result);
902         }
903 #undef MACVERSION
904 }
905
906 HAL_BOOL
907 ar5212SetCapability(struct ath_hal *ah, HAL_CAPABILITY_TYPE type,
908         uint32_t capability, uint32_t setting, HAL_STATUS *status)
909 {
910         struct ath_hal_5212 *ahp = AH5212(ah);
911         const HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
912         uint32_t v;
913
914         switch (type) {
915         case HAL_CAP_TKIP_MIC:          /* handle TKIP MIC in hardware */
916                 if (setting)
917                         ahp->ah_staId1Defaults |= AR_STA_ID1_CRPT_MIC_ENABLE;
918                 else
919                         ahp->ah_staId1Defaults &= ~AR_STA_ID1_CRPT_MIC_ENABLE;
920                 return AH_TRUE;
921         case HAL_CAP_TKIP_SPLIT:        /* hardware TKIP uses split keys */
922                 if (!pCap->halTkipMicTxRxKeySupport)
923                         return AH_FALSE;
924                 /* NB: true =>'s use split key cache layout */
925                 if (setting)
926                         ahp->ah_miscMode &= ~AR_MISC_MODE_MIC_NEW_LOC_ENABLE;
927                 else
928                         ahp->ah_miscMode |= AR_MISC_MODE_MIC_NEW_LOC_ENABLE;
929                 /* NB: write here so keys can be setup w/o a reset */
930                 OS_REG_WRITE(ah, AR_MISC_MODE, ahp->ah_miscMode);
931                 return AH_TRUE;
932         case HAL_CAP_DIVERSITY:
933                 if (ahp->ah_phyPowerOn) {
934                         v = OS_REG_READ(ah, AR_PHY_CCK_DETECT);
935                         if (setting)
936                                 v |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
937                         else
938                                 v &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
939                         OS_REG_WRITE(ah, AR_PHY_CCK_DETECT, v);
940                 }
941                 ahp->ah_diversity = (setting != 0);
942                 return AH_TRUE;
943         case HAL_CAP_DIAG:              /* hardware diagnostic support */
944                 /*
945                  * NB: could split this up into virtual capabilities,
946                  *     (e.g. 1 => ACK, 2 => CTS, etc.) but it hardly
947                  *     seems worth the additional complexity.
948                  */
949                 AH_PRIVATE(ah)->ah_diagreg = setting;
950                 OS_REG_WRITE(ah, AR_DIAG_SW, AH_PRIVATE(ah)->ah_diagreg);
951                 return AH_TRUE;
952         case HAL_CAP_TPC:
953                 ahp->ah_tpcEnabled = (setting != 0);
954                 return AH_TRUE;
955         case HAL_CAP_MCAST_KEYSRCH:     /* multicast frame keycache search */
956                 if (setting)
957                         ahp->ah_staId1Defaults |= AR_STA_ID1_MCAST_KSRCH;
958                 else
959                         ahp->ah_staId1Defaults &= ~AR_STA_ID1_MCAST_KSRCH;
960                 return AH_TRUE;
961         case HAL_CAP_TPC_ACK:
962         case HAL_CAP_TPC_CTS:
963                 setting += ahp->ah_txPowerIndexOffset;
964                 if (setting > 63)
965                         setting = 63;
966                 if (type == HAL_CAP_TPC_ACK) {
967                         ahp->ah_macTPC &= AR_TPC_ACK;
968                         ahp->ah_macTPC |= MS(setting, AR_TPC_ACK);
969                 } else {
970                         ahp->ah_macTPC &= AR_TPC_CTS;
971                         ahp->ah_macTPC |= MS(setting, AR_TPC_CTS);
972                 }
973                 OS_REG_WRITE(ah, AR_TPC, ahp->ah_macTPC);
974                 return AH_TRUE;
975         case HAL_CAP_INTMIT: {          /* interference mitigation */
976                 static const HAL_ANI_CMD cmds[] = {
977                         HAL_ANI_PRESENT,
978                         HAL_ANI_MODE,
979                         HAL_ANI_NOISE_IMMUNITY_LEVEL,
980                         HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION,
981                         HAL_ANI_CCK_WEAK_SIGNAL_THR,
982                         HAL_ANI_FIRSTEP_LEVEL,
983                         HAL_ANI_SPUR_IMMUNITY_LEVEL,
984                 };
985                 return capability < NELEM(cmds) ?
986                         ar5212AniControl(ah, cmds[capability], setting) :
987                         AH_FALSE;
988         }
989         case HAL_CAP_TSF_ADJUST:        /* hardware has beacon tsf adjust */
990                 if (pCap->halTsfAddSupport) {
991                         if (setting)
992                                 ahp->ah_miscMode |= AR_MISC_MODE_TX_ADD_TSF;
993                         else
994                                 ahp->ah_miscMode &= ~AR_MISC_MODE_TX_ADD_TSF;
995                         return AH_TRUE;
996                 }
997                 /* fall thru... */
998         default:
999                 return ath_hal_setcapability(ah, type, capability,
1000                                 setting, status);
1001         }
1002 }
1003
1004 HAL_BOOL
1005 ar5212GetDiagState(struct ath_hal *ah, int request,
1006         const void *args, uint32_t argsize,
1007         void **result, uint32_t *resultsize)
1008 {
1009         struct ath_hal_5212 *ahp = AH5212(ah);
1010
1011         (void) ahp;
1012         if (ath_hal_getdiagstate(ah, request, args, argsize, result, resultsize))
1013                 return AH_TRUE;
1014         switch (request) {
1015         case HAL_DIAG_EEPROM:
1016         case HAL_DIAG_EEPROM_EXP_11A:
1017         case HAL_DIAG_EEPROM_EXP_11B:
1018         case HAL_DIAG_EEPROM_EXP_11G:
1019         case HAL_DIAG_RFGAIN:
1020                 return ath_hal_eepromDiag(ah, request,
1021                     args, argsize, result, resultsize);
1022         case HAL_DIAG_RFGAIN_CURSTEP:
1023                 *result = __DECONST(void *, ahp->ah_gainValues.currStep);
1024                 *resultsize = (*result == AH_NULL) ?
1025                         0 : sizeof(GAIN_OPTIMIZATION_STEP);
1026                 return AH_TRUE;
1027         case HAL_DIAG_PCDAC:
1028                 *result = ahp->ah_pcdacTable;
1029                 *resultsize = ahp->ah_pcdacTableSize;
1030                 return AH_TRUE;
1031         case HAL_DIAG_TXRATES:
1032                 *result = &ahp->ah_ratesArray[0];
1033                 *resultsize = sizeof(ahp->ah_ratesArray);
1034                 return AH_TRUE;
1035         case HAL_DIAG_ANI_CURRENT:
1036                 *result = ar5212AniGetCurrentState(ah);
1037                 *resultsize = (*result == AH_NULL) ?
1038                         0 : sizeof(struct ar5212AniState);
1039                 return AH_TRUE;
1040         case HAL_DIAG_ANI_STATS:
1041                 *result = ar5212AniGetCurrentStats(ah);
1042                 *resultsize = (*result == AH_NULL) ?
1043                         0 : sizeof(struct ar5212Stats);
1044                 return AH_TRUE;
1045         case HAL_DIAG_ANI_CMD:
1046                 if (argsize != 2*sizeof(uint32_t))
1047                         return AH_FALSE;
1048                 ar5212AniControl(ah, ((const uint32_t *)args)[0],
1049                         ((const uint32_t *)args)[1]);
1050                 return AH_TRUE;
1051         case HAL_DIAG_ANI_PARAMS:
1052                 /*
1053                  * NB: We assume struct ar5212AniParams is identical
1054                  * to HAL_ANI_PARAMS; if they diverge then we'll need
1055                  * to handle it here
1056                  */
1057                 if (argsize == 0 && args == AH_NULL) {
1058                         struct ar5212AniState *aniState =
1059                             ar5212AniGetCurrentState(ah);
1060                         if (aniState == AH_NULL)
1061                                 return AH_FALSE;
1062                         *result = __DECONST(void *, aniState->params);
1063                         *resultsize = sizeof(struct ar5212AniParams);
1064                         return AH_TRUE;
1065                 } else {
1066                         if (argsize != sizeof(struct ar5212AniParams))
1067                                 return AH_FALSE;
1068                         return ar5212AniSetParams(ah, args, args);
1069                 }
1070         }
1071         return AH_FALSE;
1072 }
1073
1074 /*
1075  * Check whether there's an in-progress NF completion.
1076  *
1077  * Returns AH_TRUE if there's a in-progress NF calibration, AH_FALSE
1078  * otherwise.
1079  */
1080 HAL_BOOL
1081 ar5212IsNFCalInProgress(struct ath_hal *ah)
1082 {
1083         if (OS_REG_READ(ah, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF)
1084                 return AH_TRUE;
1085         return AH_FALSE;
1086 }
1087
1088 /*
1089  * Wait for an in-progress NF calibration to complete.
1090  *
1091  * The completion function waits "i" times 10uS.
1092  * It returns AH_TRUE if the NF calibration completed (or was never
1093  * in progress); AH_FALSE if it was still in progress after "i" checks.
1094  */
1095 HAL_BOOL
1096 ar5212WaitNFCalComplete(struct ath_hal *ah, int i)
1097 {
1098         int j;
1099         if (i <= 0)
1100                 i = 1;    /* it should run at least once */
1101         for (j = 0; j < i; j++) {
1102                 if (! ar5212IsNFCalInProgress(ah))
1103                         return AH_TRUE;
1104                 OS_DELAY(10);
1105         }
1106         return AH_FALSE;
1107 }