Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5210 / ar5210_reset.c
1 /*
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2004 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/ar5210/ar5210_reset.c 202161 2010-01-12 17:59:58Z gavin $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24
25 #include "ar5210/ar5210.h"
26 #include "ar5210/ar5210reg.h"
27 #include "ar5210/ar5210phy.h"
28
29 #include "ah_eeprom_v1.h"
30
31 typedef struct {
32         uint32_t        Offset;
33         uint32_t        Value;
34 } REGISTER_VAL;
35
36 static const REGISTER_VAL ar5k0007_init[] = {
37 #include "ar5210/ar5k_0007.ini"
38 };
39
40 /* Default Power Settings for channels outside of EEPROM range */
41 static const uint8_t ar5k0007_pwrSettings[17] = {
42 /*      gain delta                      pc dac */
43 /* 54  48  36  24  18  12   9   54  48  36  24  18  12   9   6  ob  db    */
44     9,  9,  0,  0,  0,  0,  0,   2,  2,  6,  6,  6,  6,  6,  6,  2,  2
45 };
46
47 /*
48  * The delay, in usecs, between writing AR_RC with a reset
49  * request and waiting for the chip to settle.  If this is
50  * too short then the chip does not come out of sleep state.
51  * Note this value was empirically derived and may be dependent
52  * on the host machine (don't know--the problem was identified
53  * on an IBM 570e laptop; 10us delays worked on other systems).
54  */
55 #define AR_RC_SETTLE_TIME       20000
56
57 static HAL_BOOL ar5210SetResetReg(struct ath_hal *,
58                 uint32_t resetMask, u_int delay);
59 static HAL_BOOL ar5210SetChannel(struct ath_hal *, struct ieee80211_channel *);
60 static void ar5210SetOperatingMode(struct ath_hal *, int opmode);
61
62 /*
63  * Places the device in and out of reset and then places sane
64  * values in the registers based on EEPROM config, initialization
65  * vectors (as determined by the mode), and station configuration
66  *
67  * bChannelChange is used to preserve DMA/PCU registers across
68  * a HW Reset during channel change.
69  */
70 HAL_BOOL
71 ar5210Reset(struct ath_hal *ah, HAL_OPMODE opmode,
72         struct ieee80211_channel *chan, HAL_BOOL bChannelChange,
73         HAL_STATUS *status)
74 {
75 #define N(a)    (sizeof (a) /sizeof (a[0]))
76 #define FAIL(_code)     do { ecode = _code; goto bad; } while (0)
77         struct ath_hal_5210 *ahp = AH5210(ah);
78         const HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
79         HAL_CHANNEL_INTERNAL *ichan;
80         HAL_STATUS ecode;
81         uint32_t ledstate;
82         int i, q;
83
84         HALDEBUG(ah, HAL_DEBUG_RESET,
85             "%s: opmode %u channel %u/0x%x %s channel\n", __func__,
86             opmode, chan->ic_freq, chan->ic_flags,
87             bChannelChange ? "change" : "same");
88
89         if (!IEEE80211_IS_CHAN_5GHZ(chan)) {
90                 /* Only 11a mode */
91                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: channel not 5GHz\n", __func__);
92                 FAIL(HAL_EINVAL);
93         }
94         /*
95          * Map public channel to private.
96          */
97         ichan = ath_hal_checkchannel(ah, chan);
98         if (ichan == AH_NULL) {
99                 HALDEBUG(ah, HAL_DEBUG_ANY,
100                     "%s: invalid channel %u/0x%x; no mapping\n",
101                     __func__, chan->ic_freq, chan->ic_flags);
102                 FAIL(HAL_EINVAL);
103         }
104         switch (opmode) {
105         case HAL_M_STA:
106         case HAL_M_IBSS:
107         case HAL_M_HOSTAP:
108         case HAL_M_MONITOR:
109                 break;
110         default:
111                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid operating mode %u\n",
112                     __func__, opmode);
113                 FAIL(HAL_EINVAL);
114                 break;
115         }
116
117         ledstate = OS_REG_READ(ah, AR_PCICFG) &
118                 (AR_PCICFG_LED_PEND | AR_PCICFG_LED_ACT);
119
120         if (!ar5210ChipReset(ah, chan)) {
121                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n",
122                     __func__);
123                 FAIL(HAL_EIO);
124         }
125
126         OS_REG_WRITE(ah, AR_STA_ID0, LE_READ_4(ahp->ah_macaddr));
127         OS_REG_WRITE(ah, AR_STA_ID1, LE_READ_2(ahp->ah_macaddr + 4));
128         ar5210SetOperatingMode(ah, opmode);
129
130         switch (opmode) {
131         case HAL_M_HOSTAP:
132                 OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);
133                 OS_REG_WRITE(ah, AR_PCICFG,
134                         AR_PCICFG_LED_ACT | AR_PCICFG_LED_BCTL);
135                 break;
136         case HAL_M_IBSS:
137                 OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG | AR_BCR_BCMD);
138                 OS_REG_WRITE(ah, AR_PCICFG,
139                         AR_PCICFG_CLKRUNEN | AR_PCICFG_LED_PEND | AR_PCICFG_LED_BCTL);
140                 break;
141         case HAL_M_STA:
142                 OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);
143                 OS_REG_WRITE(ah, AR_PCICFG,
144                         AR_PCICFG_CLKRUNEN | AR_PCICFG_LED_PEND | AR_PCICFG_LED_BCTL);
145                 break;
146         case HAL_M_MONITOR:
147                 OS_REG_WRITE(ah, AR_BCR, INIT_BCON_CNTRL_REG);
148                 OS_REG_WRITE(ah, AR_PCICFG,
149                         AR_PCICFG_LED_ACT | AR_PCICFG_LED_BCTL);
150                 break;
151         }
152
153         /* Restore previous led state */
154         OS_REG_WRITE(ah, AR_PCICFG, OS_REG_READ(ah, AR_PCICFG) | ledstate);
155
156         OS_REG_WRITE(ah, AR_BSS_ID0, LE_READ_4(ahp->ah_bssid));
157         OS_REG_WRITE(ah, AR_BSS_ID1, LE_READ_2(ahp->ah_bssid + 4));
158
159         OS_REG_WRITE(ah, AR_TXDP0, 0);
160         OS_REG_WRITE(ah, AR_TXDP1, 0);
161         OS_REG_WRITE(ah, AR_RXDP, 0);
162
163         /*
164          * Initialize interrupt state.
165          */
166         (void) OS_REG_READ(ah, AR_ISR);         /* cleared on read */
167         OS_REG_WRITE(ah, AR_IMR, 0);
168         OS_REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
169         ahp->ah_maskReg = 0;
170
171         (void) OS_REG_READ(ah, AR_BSR);         /* cleared on read */
172         OS_REG_WRITE(ah, AR_TXCFG, AR_DMASIZE_128B);
173         OS_REG_WRITE(ah, AR_RXCFG, AR_DMASIZE_128B);
174
175         OS_REG_WRITE(ah, AR_TOPS, 8);           /* timeout prescale */
176         OS_REG_WRITE(ah, AR_RXNOFRM, 8);        /* RX no frame timeout */
177         OS_REG_WRITE(ah, AR_RPGTO, 0);          /* RX frame gap timeout */
178         OS_REG_WRITE(ah, AR_TXNOFRM, 0);        /* TX no frame timeout */
179
180         OS_REG_WRITE(ah, AR_SFR, 0);
181         OS_REG_WRITE(ah, AR_MIBC, 0);           /* unfreeze ctrs + clr state */
182         OS_REG_WRITE(ah, AR_RSSI_THR, ahp->ah_rssiThr);
183         OS_REG_WRITE(ah, AR_CFP_DUR, 0);
184
185         ar5210SetRxFilter(ah, 0);               /* nothing for now */
186         OS_REG_WRITE(ah, AR_MCAST_FIL0, 0);     /* multicast filter */
187         OS_REG_WRITE(ah, AR_MCAST_FIL1, 0);     /* XXX was 2 */
188
189         OS_REG_WRITE(ah, AR_TX_MASK0, 0);
190         OS_REG_WRITE(ah, AR_TX_MASK1, 0);
191         OS_REG_WRITE(ah, AR_CLR_TMASK, 1);
192         OS_REG_WRITE(ah, AR_TRIG_LEV, 1);       /* minimum */
193
194         OS_REG_WRITE(ah, AR_DIAG_SW, 0);
195
196         OS_REG_WRITE(ah, AR_CFP_PERIOD, 0);
197         OS_REG_WRITE(ah, AR_TIMER0, 0);         /* next beacon time */
198         OS_REG_WRITE(ah, AR_TSF_L32, 0);        /* local clock */
199         OS_REG_WRITE(ah, AR_TIMER1, ~0);        /* next DMA beacon alert */
200         OS_REG_WRITE(ah, AR_TIMER2, ~0);        /* next SW beacon alert */
201         OS_REG_WRITE(ah, AR_TIMER3, 1);         /* next ATIM window */
202
203         /* Write the INI values for PHYreg initialization */
204         for (i = 0; i < N(ar5k0007_init); i++) {
205                 uint32_t reg = ar5k0007_init[i].Offset;
206                 /* On channel change, don't reset the PCU registers */
207                 if (!(bChannelChange && (0x8000 <= reg && reg < 0x9000)))
208                         OS_REG_WRITE(ah, reg, ar5k0007_init[i].Value);
209         }
210
211         /* Setup the transmit power values for cards since 0x0[0-2]05 */
212         if (!ar5210SetTransmitPower(ah, chan)) {
213                 HALDEBUG(ah, HAL_DEBUG_ANY,
214                     "%s: error init'ing transmit power\n", __func__);
215                 FAIL(HAL_EIO);
216         }
217
218         OS_REG_WRITE(ah, AR_PHY(10),
219                 (OS_REG_READ(ah, AR_PHY(10)) & 0xFFFF00FF) |
220                 (ee->ee_xlnaOn << 8));
221         OS_REG_WRITE(ah, AR_PHY(13),
222                 (ee->ee_xpaOff << 24) | (ee->ee_xpaOff << 16) |
223                 (ee->ee_xpaOn << 8) | ee->ee_xpaOn);
224         OS_REG_WRITE(ah, AR_PHY(17),
225                 (OS_REG_READ(ah, AR_PHY(17)) & 0xFFFFC07F) |
226                 ((ee->ee_antenna >> 1) & 0x3F80));
227         OS_REG_WRITE(ah, AR_PHY(18),
228                 (OS_REG_READ(ah, AR_PHY(18)) & 0xFFFC0FFF) |
229                 ((ee->ee_antenna << 10) & 0x3F000));
230         OS_REG_WRITE(ah, AR_PHY(25),
231                 (OS_REG_READ(ah, AR_PHY(25)) & 0xFFF80FFF) |
232                 ((ee->ee_thresh62 << 12) & 0x7F000));
233         OS_REG_WRITE(ah, AR_PHY(68),
234                 (OS_REG_READ(ah, AR_PHY(68)) & 0xFFFFFFFC) |
235                 (ee->ee_antenna & 0x3));
236
237         if (!ar5210SetChannel(ah, chan)) {
238                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unable to set channel\n",
239                     __func__);
240                 FAIL(HAL_EIO);
241         }
242         if (bChannelChange && !IEEE80211_IS_CHAN_DFS(chan)) 
243                 chan->ic_state &= ~IEEE80211_CHANSTATE_CWINT;
244
245         /* Activate the PHY */
246         OS_REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ENABLE);
247
248         OS_DELAY(1000);         /* Wait a bit (1 msec) */
249
250         /* calibrate the HW and poll the bit going to 0 for completion */
251         OS_REG_WRITE(ah, AR_PHY_AGCCTL,
252                 OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_CAL);
253         (void) ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_CAL, 0);
254
255         /* Perform noise floor calibration and set status */
256         if (!ar5210CalNoiseFloor(ah, ichan)) {
257                 chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
258                 HALDEBUG(ah, HAL_DEBUG_ANY,
259                     "%s: noise floor calibration failed\n", __func__);
260                 FAIL(HAL_EIO);
261         }
262
263         for (q = 0; q < HAL_NUM_TX_QUEUES; q++)
264                 ar5210ResetTxQueue(ah, q);
265
266         if (AH_PRIVATE(ah)->ah_rfkillEnabled)
267                 ar5210EnableRfKill(ah);
268
269         /*
270          * Writing to AR_BEACON will start timers. Hence it should be
271          * the last register to be written. Do not reset tsf, do not
272          * enable beacons at this point, but preserve other values
273          * like beaconInterval.
274          */
275         OS_REG_WRITE(ah, AR_BEACON,
276                 (OS_REG_READ(ah, AR_BEACON) &
277                         ~(AR_BEACON_EN | AR_BEACON_RESET_TSF)));
278
279         /* Restore user-specified slot time and timeouts */
280         if (ahp->ah_sifstime != (u_int) -1)
281                 ar5210SetSifsTime(ah, ahp->ah_sifstime);
282         if (ahp->ah_slottime != (u_int) -1)
283                 ar5210SetSlotTime(ah, ahp->ah_slottime);
284         if (ahp->ah_acktimeout != (u_int) -1)
285                 ar5210SetAckTimeout(ah, ahp->ah_acktimeout);
286         if (ahp->ah_ctstimeout != (u_int) -1)
287                 ar5210SetCTSTimeout(ah, ahp->ah_ctstimeout);
288         if (AH_PRIVATE(ah)->ah_diagreg != 0)
289                 OS_REG_WRITE(ah, AR_DIAG_SW, AH_PRIVATE(ah)->ah_diagreg);
290
291         AH_PRIVATE(ah)->ah_opmode = opmode;     /* record operating mode */
292
293         HALDEBUG(ah, HAL_DEBUG_RESET, "%s: done\n", __func__);
294
295         return AH_TRUE;
296 bad:
297         if (status != AH_NULL)
298                 *status = ecode;
299         return AH_FALSE;
300 #undef FAIL
301 #undef N
302 }
303
304 static void
305 ar5210SetOperatingMode(struct ath_hal *ah, int opmode)
306 {
307         struct ath_hal_5210 *ahp = AH5210(ah);
308         uint32_t val;
309
310         val = OS_REG_READ(ah, AR_STA_ID1) & 0xffff;
311         switch (opmode) {
312         case HAL_M_HOSTAP:
313                 OS_REG_WRITE(ah, AR_STA_ID1, val
314                         | AR_STA_ID1_AP
315                         | AR_STA_ID1_NO_PSPOLL
316                         | AR_STA_ID1_DESC_ANTENNA
317                         | ahp->ah_staId1Defaults);
318                 break;
319         case HAL_M_IBSS:
320                 OS_REG_WRITE(ah, AR_STA_ID1, val
321                         | AR_STA_ID1_ADHOC
322                         | AR_STA_ID1_NO_PSPOLL
323                         | AR_STA_ID1_DESC_ANTENNA
324                         | ahp->ah_staId1Defaults);
325                 break;
326         case HAL_M_STA:
327                 OS_REG_WRITE(ah, AR_STA_ID1, val
328                         | AR_STA_ID1_NO_PSPOLL
329                         | AR_STA_ID1_PWR_SV
330                         | ahp->ah_staId1Defaults);
331                 break;
332         case HAL_M_MONITOR:
333                 OS_REG_WRITE(ah, AR_STA_ID1, val
334                         | AR_STA_ID1_NO_PSPOLL
335                         | ahp->ah_staId1Defaults);
336                 break;
337         }
338 }
339
340 void
341 ar5210SetPCUConfig(struct ath_hal *ah)
342 {
343         ar5210SetOperatingMode(ah, AH_PRIVATE(ah)->ah_opmode);
344 }
345
346 /*
347  * Places the PHY and Radio chips into reset.  A full reset
348  * must be called to leave this state.  The PCI/MAC/PCU are
349  * not placed into reset as we must receive interrupt to
350  * re-enable the hardware.
351  */
352 HAL_BOOL
353 ar5210PhyDisable(struct ath_hal *ah)
354 {
355         return ar5210SetResetReg(ah, AR_RC_RPHY, 10);
356 }
357
358 /*
359  * Places all of hardware into reset
360  */
361 HAL_BOOL
362 ar5210Disable(struct ath_hal *ah)
363 {
364 #define AR_RC_HW (AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC)
365         if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
366                 return AH_FALSE;
367
368         /*
369          * Reset the HW - PCI must be reset after the rest of the
370          * device has been reset
371          */
372         if (!ar5210SetResetReg(ah, AR_RC_HW, AR_RC_SETTLE_TIME))
373                 return AH_FALSE;
374         OS_DELAY(1000);
375         (void) ar5210SetResetReg(ah, AR_RC_HW | AR_RC_RPCI, AR_RC_SETTLE_TIME);
376         OS_DELAY(2100);   /* 8245 @ 96Mhz hangs with 2000us. */
377
378         return AH_TRUE;
379 #undef AR_RC_HW
380 }
381
382 /*
383  * Places the hardware into reset and then pulls it out of reset
384  */
385 HAL_BOOL
386 ar5210ChipReset(struct ath_hal *ah, struct ieee80211_channel *chan)
387 {
388 #define AR_RC_HW (AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC)
389
390         HALDEBUG(ah, HAL_DEBUG_RESET, "%s turbo %s\n", __func__,
391                 chan && IEEE80211_IS_CHAN_TURBO(chan) ?
392                 "enabled" : "disabled");
393
394         if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
395                 return AH_FALSE;
396
397         /* Place chip in turbo before reset to cleanly reset clocks */
398         OS_REG_WRITE(ah, AR_PHY_FRCTL,
399                 chan && IEEE80211_IS_CHAN_TURBO(chan) ? AR_PHY_TURBO_MODE : 0);
400
401         /*
402          * Reset the HW.
403          * PCI must be reset after the rest of the device has been reset.
404          */
405         if (!ar5210SetResetReg(ah, AR_RC_HW, AR_RC_SETTLE_TIME))
406                 return AH_FALSE;
407         OS_DELAY(1000);
408         if (!ar5210SetResetReg(ah, AR_RC_HW | AR_RC_RPCI, AR_RC_SETTLE_TIME))
409                 return AH_FALSE;
410         OS_DELAY(2100);   /* 8245 @ 96Mhz hangs with 2000us. */
411
412         /*
413          * Bring out of sleep mode (AGAIN)
414          *
415          * WARNING WARNING WARNING
416          *
417          * There is a problem with the chip where it doesn't always indicate
418          * that it's awake, so initializePowerUp() will fail.
419          */
420         if (!ar5210SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE))
421                 return AH_FALSE;
422
423         /* Clear warm reset reg */
424         return ar5210SetResetReg(ah, 0, 10);
425 #undef AR_RC_HW
426 }
427
428 enum {
429         FIRPWR_M        = 0x03fc0000,
430         FIRPWR_S        = 18,
431         KCOARSEHIGH_M   = 0x003f8000,
432         KCOARSEHIGH_S   = 15,
433         KCOARSELOW_M    = 0x00007f80,
434         KCOARSELOW_S    = 7,
435         ADCSAT_ICOUNT_M = 0x0001f800,
436         ADCSAT_ICOUNT_S = 11,
437         ADCSAT_THRESH_M = 0x000007e0,
438         ADCSAT_THRESH_S = 5
439 };
440
441 /*
442  * Recalibrate the lower PHY chips to account for temperature/environment
443  * changes.
444  */
445 HAL_BOOL
446 ar5210PerCalibrationN(struct ath_hal *ah,
447         struct ieee80211_channel *chan, u_int chainMask,
448         HAL_BOOL longCal, HAL_BOOL *isCalDone)
449 {
450         uint32_t regBeacon;
451         uint32_t reg9858, reg985c, reg9868;
452         HAL_CHANNEL_INTERNAL *ichan;
453
454         ichan = ath_hal_checkchannel(ah, chan);
455         if (ichan == AH_NULL)
456                 return AH_FALSE;
457         /* Disable tx and rx */
458         OS_REG_WRITE(ah, AR_DIAG_SW,
459                 OS_REG_READ(ah, AR_DIAG_SW) | (AR_DIAG_SW_DIS_TX | AR_DIAG_SW_DIS_RX));
460
461         /* Disable Beacon Enable */
462         regBeacon = OS_REG_READ(ah, AR_BEACON);
463         OS_REG_WRITE(ah, AR_BEACON, regBeacon & ~AR_BEACON_EN);
464
465         /* Delay 4ms to ensure that all tx and rx activity has ceased */
466         OS_DELAY(4000);
467
468         /* Disable AGC to radio traffic */
469         OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) | 0x08000000);
470         /* Wait for the AGC traffic to cease. */
471         OS_DELAY(10);
472
473         /* Change Channel to relock synth */
474         if (!ar5210SetChannel(ah, chan))
475                 return AH_FALSE;
476
477         /* wait for the synthesizer lock to stabilize */
478         OS_DELAY(1000);
479
480         /* Re-enable AGC to radio traffic */
481         OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) & (~0x08000000));
482
483         /*
484          * Configure the AGC so that it is highly unlikely (if not
485          * impossible) for it to send any gain changes to the analog
486          * chip.  We store off the current values so that they can
487          * be rewritten below. Setting the following values:
488          * firpwr        = -1
489          * Kcoursehigh   = -1
490          * Kcourselow    = -127
491          * ADCsat_icount = 2
492          * ADCsat_thresh = 12
493          */
494         reg9858 = OS_REG_READ(ah, 0x9858);
495         reg985c = OS_REG_READ(ah, 0x985c);
496         reg9868 = OS_REG_READ(ah, 0x9868);
497
498         OS_REG_WRITE(ah, 0x9858, (reg9858 & ~FIRPWR_M) |
499                                          ((-1 << FIRPWR_S) & FIRPWR_M));
500         OS_REG_WRITE(ah, 0x985c,
501                  (reg985c & ~(KCOARSEHIGH_M | KCOARSELOW_M)) |
502                  ((-1 << KCOARSEHIGH_S) & KCOARSEHIGH_M) |
503                  ((-127 << KCOARSELOW_S) & KCOARSELOW_M));
504         OS_REG_WRITE(ah, 0x9868,
505                  (reg9868 & ~(ADCSAT_ICOUNT_M | ADCSAT_THRESH_M)) |
506                  ((2 << ADCSAT_ICOUNT_S) & ADCSAT_ICOUNT_M) |
507                  ((12 << ADCSAT_THRESH_S) & ADCSAT_THRESH_M));
508
509         /* Wait for AGC changes to be enacted */
510         OS_DELAY(20);
511
512         /*
513          * We disable RF mix/gain stages for the PGA to avoid a
514          * race condition that will occur with receiving a frame
515          * and performing the AGC calibration.  This will be
516          * re-enabled at the end of offset cal.  We turn off AGC
517          * writes during this write as it will go over the analog bus.
518          */
519         OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) | 0x08000000);
520         OS_DELAY(10);            /* wait for the AGC traffic to cease */
521         OS_REG_WRITE(ah, 0x98D4, 0x21);
522         OS_REG_WRITE(ah, 0x9808, OS_REG_READ(ah, 0x9808) & (~0x08000000));
523
524         /* wait to make sure that additional AGC traffic has quiesced */
525         OS_DELAY(1000);
526
527         /* AGC calibration (this was added to make the NF threshold check work) */
528         OS_REG_WRITE(ah, AR_PHY_AGCCTL,
529                  OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_CAL);
530         if (!ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_CAL, 0)) {
531                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: AGC calibration timeout\n",
532                     __func__);
533         }
534
535         /* Rewrite our AGC values we stored off earlier (return AGC to normal operation) */
536         OS_REG_WRITE(ah, 0x9858, reg9858);
537         OS_REG_WRITE(ah, 0x985c, reg985c);
538         OS_REG_WRITE(ah, 0x9868, reg9868);
539
540         /* Perform noise floor and set status */
541         if (!ar5210CalNoiseFloor(ah, ichan)) {
542                 /*
543                  * Delay 5ms before retrying the noise floor -
544                  * just to make sure.  We're in an error
545                  * condition here
546                  */
547                 HALDEBUG(ah, HAL_DEBUG_NFCAL | HAL_DEBUG_PERCAL,
548                     "%s: Performing 2nd Noise Cal\n", __func__);
549                 OS_DELAY(5000);
550                 if (!ar5210CalNoiseFloor(ah, ichan))
551                         chan->ic_state |= IEEE80211_CHANSTATE_CWINT;
552         }
553
554         /* Clear tx and rx disable bit */
555         OS_REG_WRITE(ah, AR_DIAG_SW,
556                  OS_REG_READ(ah, AR_DIAG_SW) & ~(AR_DIAG_SW_DIS_TX | AR_DIAG_SW_DIS_RX));
557
558         /* Re-enable Beacons */
559         OS_REG_WRITE(ah, AR_BEACON, regBeacon);
560
561         *isCalDone = AH_TRUE;
562
563         return AH_TRUE;
564 }
565
566 HAL_BOOL
567 ar5210PerCalibration(struct ath_hal *ah, struct ieee80211_channel *chan,
568         HAL_BOOL *isIQdone)
569 {
570         return ar5210PerCalibrationN(ah,  chan, 0x1, AH_TRUE, isIQdone);
571 }
572
573 HAL_BOOL
574 ar5210ResetCalValid(struct ath_hal *ah, const struct ieee80211_channel *chan)
575 {
576         return AH_TRUE;
577 }
578
579 /*
580  * Writes the given reset bit mask into the reset register
581  */
582 static HAL_BOOL
583 ar5210SetResetReg(struct ath_hal *ah, uint32_t resetMask, u_int delay)
584 {
585         uint32_t mask = resetMask ? resetMask : ~0;
586         HAL_BOOL rt;
587
588         OS_REG_WRITE(ah, AR_RC, resetMask);
589         /* need to wait at least 128 clocks when reseting PCI before read */
590         OS_DELAY(delay);
591
592         resetMask &= AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC;
593         mask &= AR_RC_RPCU | AR_RC_RDMA | AR_RC_RPHY | AR_RC_RMAC;
594         rt = ath_hal_wait(ah, AR_RC, mask, resetMask);
595         if ((resetMask & AR_RC_RMAC) == 0) {
596                 if (isBigEndian()) {
597                         /*
598                          * Set CFG, little-endian for register
599                          * and descriptor accesses.
600                          */
601                         mask = INIT_CONFIG_STATUS |
602                                 AR_CFG_SWTD | AR_CFG_SWRD | AR_CFG_SWRG;
603                         OS_REG_WRITE(ah, AR_CFG, LE_READ_4(&mask));
604                 } else
605                         OS_REG_WRITE(ah, AR_CFG, INIT_CONFIG_STATUS);
606         }
607         return rt;
608 }
609
610
611 /*
612  * Returns: the pcdac value
613  */
614 static uint8_t
615 getPcdac(struct ath_hal *ah, const struct tpcMap *pRD, uint8_t dBm)
616 {
617         int32_t  i;
618         int useNextEntry = AH_FALSE;
619         uint32_t interp;
620
621         for (i = AR_TP_SCALING_ENTRIES - 1; i >= 0; i--) {
622                 /* Check for exact entry */
623                 if (dBm == AR_I2DBM(i)) {
624                         if (pRD->pcdac[i] != 63)
625                                 return pRD->pcdac[i];
626                         useNextEntry = AH_TRUE;
627                 } else if (dBm + 1 == AR_I2DBM(i) && i > 0) {
628                         /* Interpolate for between entry with a logish scale */
629                         if (pRD->pcdac[i] != 63 && pRD->pcdac[i-1] != 63) {
630                                 interp = (350 * (pRD->pcdac[i] - pRD->pcdac[i-1])) + 999;
631                                 interp = (interp / 1000) + pRD->pcdac[i-1];
632                                 return interp;
633                         }
634                         useNextEntry = AH_TRUE;
635                 } else if (useNextEntry == AH_TRUE) {
636                         /* Grab the next lowest */
637                         if (pRD->pcdac[i] != 63)
638                                 return pRD->pcdac[i];
639                 }
640         }
641
642         /* Return the lowest Entry if we haven't returned */
643         for (i = 0; i < AR_TP_SCALING_ENTRIES; i++)
644                 if (pRD->pcdac[i] != 63)
645                         return pRD->pcdac[i];
646
647         /* No value to return from table */
648 #ifdef AH_DEBUG
649         ath_hal_printf(ah, "%s: empty transmit power table?\n", __func__);
650 #endif
651         return 1;
652 }
653
654 /*
655  * Find or interpolates the gainF value from the table ptr.
656  */
657 static uint8_t
658 getGainF(struct ath_hal *ah, const struct tpcMap *pRD,
659         uint8_t pcdac, uint8_t *dBm)
660 {
661         uint32_t interp;
662         int low, high, i;
663
664         low = high = -1;
665
666         for (i = 0; i < AR_TP_SCALING_ENTRIES; i++) {
667                 if(pRD->pcdac[i] == 63)
668                         continue;
669                 if (pcdac == pRD->pcdac[i]) {
670                         *dBm = AR_I2DBM(i);
671                         return pRD->gainF[i];  /* Exact Match */
672                 }
673                 if (pcdac > pRD->pcdac[i])
674                         low = i;
675                 if (pcdac < pRD->pcdac[i]) {
676                         high = i;
677                         if (low == -1) {
678                                 *dBm = AR_I2DBM(i);
679                                 /* PCDAC is lower than lowest setting */
680                                 return pRD->gainF[i];
681                         }
682                         break;
683                 }
684         }
685         if (i >= AR_TP_SCALING_ENTRIES && low == -1) {
686                 /* No settings were found */
687 #ifdef AH_DEBUG
688                 ath_hal_printf(ah,
689                         "%s: no valid entries in the pcdac table: %d\n",
690                         __func__, pcdac);
691 #endif
692                 return 63;
693         }
694         if (i >= AR_TP_SCALING_ENTRIES) {
695                 /* PCDAC setting was above the max setting in the table */
696                 *dBm = AR_I2DBM(low);
697                 return pRD->gainF[low];
698         }
699         /* Only exact if table has no missing entries */
700         *dBm = (low + high) + 3;
701
702         /*
703          * Perform interpolation between low and high values to find gainF
704          * linearly scale the pcdac between low and high
705          */
706         interp = ((pcdac - pRD->pcdac[low]) * 1000) /
707                   (pRD->pcdac[high] - pRD->pcdac[low]);
708         /*
709          * Multiply the scale ratio by the gainF difference
710          * (plus a rnd up factor)
711          */
712         interp = ((interp * (pRD->gainF[high] - pRD->gainF[low])) + 999) / 1000;
713
714         /* Add ratioed gain_f to low gain_f value */
715         return interp + pRD->gainF[low];
716 }
717
718 HAL_BOOL
719 ar5210SetTxPowerLimit(struct ath_hal *ah, uint32_t limit)
720 {
721         AH_PRIVATE(ah)->ah_powerLimit = AH_MIN(limit, AR5210_MAX_RATE_POWER);
722         /* XXX flush to h/w */
723         return AH_TRUE;
724 }
725
726 /*
727  * Get TXPower values and set them in the radio
728  */
729 static HAL_BOOL
730 setupPowerSettings(struct ath_hal *ah, const struct ieee80211_channel *chan,
731         uint8_t cp[17])
732 {
733         uint16_t freq = ath_hal_gethwchannel(ah, chan);
734         const HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
735         uint8_t gainFRD, gainF36, gainF48, gainF54;
736         uint8_t dBmRD, dBm36, dBm48, dBm54, dontcare;
737         uint32_t rd, group;
738         const struct tpcMap  *pRD;
739
740         /* Set OB/DB Values regardless of channel */
741         cp[15] = (ee->ee_biasCurrents >> 4) & 0x7;
742         cp[16] = ee->ee_biasCurrents & 0x7;
743
744         if (freq < 5170 || freq > 5320) {
745                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u\n",
746                     __func__, freq);
747                 return AH_FALSE;
748         }
749
750         HALASSERT(ee->ee_version >= AR_EEPROM_VER1 &&
751             ee->ee_version < AR_EEPROM_VER3);
752
753         /* Match regulatory domain */
754         for (rd = 0; rd < AR_REG_DOMAINS_MAX; rd++)
755                 if (AH_PRIVATE(ah)->ah_currentRD == ee->ee_regDomain[rd])
756                         break;
757         if (rd == AR_REG_DOMAINS_MAX) {
758 #ifdef AH_DEBUG
759                 ath_hal_printf(ah,
760                         "%s: no calibrated regulatory domain matches the "
761                         "current regularly domain (0x%0x)\n", __func__, 
762                         AH_PRIVATE(ah)->ah_currentRD);
763 #endif
764                 return AH_FALSE;
765         }
766         group = ((freq - 5170) / 10);
767
768         if (group > 11) {
769                 /* Pull 5.29 into the 5.27 group */
770                 group--;
771         }
772
773         /* Integer divide will set group from 0 to 4 */
774         group = group / 3;
775         pRD   = &ee->ee_tpc[group];
776
777         /* Set PC DAC Values */
778         cp[14] = pRD->regdmn[rd];
779         cp[9]  = AH_MIN(pRD->regdmn[rd], pRD->rate36);
780         cp[8]  = AH_MIN(pRD->regdmn[rd], pRD->rate48);
781         cp[7]  = AH_MIN(pRD->regdmn[rd], pRD->rate54);
782
783         /* Find Corresponding gainF values for RD, 36, 48, 54 */
784         gainFRD = getGainF(ah, pRD, pRD->regdmn[rd], &dBmRD);
785         gainF36 = getGainF(ah, pRD, cp[9], &dBm36);
786         gainF48 = getGainF(ah, pRD, cp[8], &dBm48);
787         gainF54 = getGainF(ah, pRD, cp[7], &dBm54);
788
789         /* Power Scale if requested */
790         if (AH_PRIVATE(ah)->ah_tpScale != HAL_TP_SCALE_MAX) {
791                 static const uint16_t tpcScaleReductionTable[5] =
792                         { 0, 3, 6, 9, AR5210_MAX_RATE_POWER };
793                 uint16_t tpScale;
794
795                 tpScale = tpcScaleReductionTable[AH_PRIVATE(ah)->ah_tpScale];
796                 if (dBmRD < tpScale+3)
797                         dBmRD = 3;              /* min */
798                 else
799                         dBmRD -= tpScale;
800                 cp[14]  = getPcdac(ah, pRD, dBmRD);
801                 gainFRD = getGainF(ah, pRD, cp[14], &dontcare);
802                 dBm36   = AH_MIN(dBm36, dBmRD);
803                 cp[9]   = getPcdac(ah, pRD, dBm36);
804                 gainF36 = getGainF(ah, pRD, cp[9], &dontcare);
805                 dBm48   = AH_MIN(dBm48, dBmRD);
806                 cp[8]   = getPcdac(ah, pRD, dBm48);
807                 gainF48 = getGainF(ah, pRD, cp[8], &dontcare);
808                 dBm54   = AH_MIN(dBm54, dBmRD);
809                 cp[7]   = getPcdac(ah, pRD, dBm54);
810                 gainF54 = getGainF(ah, pRD, cp[7], &dontcare);
811         }
812         /* Record current dBm at rate 6 */
813         AH_PRIVATE(ah)->ah_maxPowerLevel = 2*dBmRD;
814
815         cp[13] = cp[12] = cp[11] = cp[10] = cp[14];
816
817         /* Set GainF Values */
818         cp[0] = gainFRD - gainF54;
819         cp[1] = gainFRD - gainF48;
820         cp[2] = gainFRD - gainF36;
821         /* 9, 12, 18, 24 have no gain_delta from 6 */
822         cp[3] = cp[4] = cp[5] = cp[6] = 0;
823         return AH_TRUE;
824 }
825
826 /*
827  * Places the device in and out of reset and then places sane
828  * values in the registers based on EEPROM config, initialization
829  * vectors (as determined by the mode), and station configuration
830  */
831 HAL_BOOL
832 ar5210SetTransmitPower(struct ath_hal *ah, const struct ieee80211_channel *chan)
833 {
834 #define N(a)    (sizeof (a) / sizeof (a[0]))
835         static const uint32_t pwr_regs_start[17] = {
836                 0x00000000, 0x00000000, 0x00000000,
837                 0x00000000, 0x00000000, 0xf0000000,
838                 0xcc000000, 0x00000000, 0x00000000,
839                 0x00000000, 0x0a000000, 0x000000e2,
840                 0x0a000020, 0x01000002, 0x01000018,
841                 0x40000000, 0x00000418
842         };
843         uint16_t i;
844         uint8_t cp[sizeof(ar5k0007_pwrSettings)];
845         uint32_t pwr_regs[17];
846
847         OS_MEMCPY(pwr_regs, pwr_regs_start, sizeof(pwr_regs));
848         OS_MEMCPY(cp, ar5k0007_pwrSettings, sizeof(cp));
849
850         /* Check the EEPROM tx power calibration settings */
851         if (!setupPowerSettings(ah, chan, cp)) {
852 #ifdef AH_DEBUG
853                 ath_hal_printf(ah, "%s: unable to setup power settings\n",
854                         __func__);
855 #endif
856                 return AH_FALSE;
857         }
858         if (cp[15] < 1 || cp[15] > 5) {
859 #ifdef AH_DEBUG
860                 ath_hal_printf(ah, "%s: OB out of range (%u)\n",
861                         __func__, cp[15]);
862 #endif
863                 return AH_FALSE;
864         }
865         if (cp[16] < 1 || cp[16] > 5) {
866 #ifdef AH_DEBUG
867                 ath_hal_printf(ah, "%s: DB out of range (%u)\n",
868                         __func__, cp[16]);
869 #endif
870                 return AH_FALSE;
871         }
872
873         /* reverse bits of the transmit power array */
874         for (i = 0; i < 7; i++)
875                 cp[i] = ath_hal_reverseBits(cp[i], 5);
876         for (i = 7; i < 15; i++)
877                 cp[i] = ath_hal_reverseBits(cp[i], 6);
878
879         /* merge transmit power values into the register - quite gross */
880         pwr_regs[0] |= ((cp[1] << 5) & 0xE0) | (cp[0] & 0x1F);
881         pwr_regs[1] |= ((cp[3] << 7) & 0x80) | ((cp[2] << 2) & 0x7C) | 
882                         ((cp[1] >> 3) & 0x03);
883         pwr_regs[2] |= ((cp[4] << 4) & 0xF0) | ((cp[3] >> 1) & 0x0F);
884         pwr_regs[3] |= ((cp[6] << 6) & 0xC0) | ((cp[5] << 1) & 0x3E) |
885                        ((cp[4] >> 4) & 0x01);
886         pwr_regs[4] |= ((cp[7] << 3) & 0xF8) | ((cp[6] >> 2) & 0x07);
887         pwr_regs[5] |= ((cp[9] << 7) & 0x80) | ((cp[8] << 1) & 0x7E) |
888                         ((cp[7] >> 5) & 0x01);
889         pwr_regs[6] |= ((cp[10] << 5) & 0xE0) | ((cp[9] >> 1) & 0x1F);
890         pwr_regs[7] |= ((cp[11] << 3) & 0xF8) | ((cp[10] >> 3) & 0x07);
891         pwr_regs[8] |= ((cp[12] << 1) & 0x7E) | ((cp[11] >> 5) & 0x01);
892         pwr_regs[9] |= ((cp[13] << 5) & 0xE0);
893         pwr_regs[10] |= ((cp[14] << 3) & 0xF8) | ((cp[13] >> 3) & 0x07);
894         pwr_regs[11] |= ((cp[14] >> 5) & 0x01);
895
896         /* Set OB */
897         pwr_regs[8] |=  (ath_hal_reverseBits(cp[15], 3) << 7) & 0x80;
898         pwr_regs[9] |=  (ath_hal_reverseBits(cp[15], 3) >> 1) & 0x03;
899
900         /* Set DB */
901         pwr_regs[9] |=  (ath_hal_reverseBits(cp[16], 3) << 2) & 0x1C;
902
903         /* Write the registers */
904         for (i = 0; i < N(pwr_regs)-1; i++)
905                 OS_REG_WRITE(ah, 0x0000989c, pwr_regs[i]);
906         /* last write is a flush */
907         OS_REG_WRITE(ah, 0x000098d4, pwr_regs[i]);
908
909         return AH_TRUE;
910 #undef N
911 }
912
913 /*
914  * Takes the MHz channel value and sets the Channel value
915  *
916  * ASSUMES: Writes enabled to analog bus before AGC is active
917  *   or by disabling the AGC.
918  */
919 static HAL_BOOL
920 ar5210SetChannel(struct ath_hal *ah, struct ieee80211_channel *chan)
921 {
922         uint16_t freq = ath_hal_gethwchannel(ah, chan);
923         uint32_t data;
924
925         /* Set the Channel */
926         data = ath_hal_reverseBits((freq - 5120)/10, 5);
927         data = (data << 1) | 0x41;
928         OS_REG_WRITE(ah, AR_PHY(0x27), data);
929         OS_REG_WRITE(ah, AR_PHY(0x30), 0);
930         AH_PRIVATE(ah)->ah_curchan = chan;
931         return AH_TRUE;
932 }
933
934 int16_t
935 ar5210GetNoiseFloor(struct ath_hal *ah)
936 {
937         int16_t nf;
938
939         nf = (OS_REG_READ(ah, AR_PHY(25)) >> 19) & 0x1ff;
940         if (nf & 0x100)
941                 nf = 0 - ((nf ^ 0x1ff) + 1);
942         return nf;
943 }
944
945 #define NORMAL_NF_THRESH (-72)
946 /*
947  * Peform the noisefloor calibration and check for
948  * any constant channel interference
949  *
950  * Returns: TRUE for a successful noise floor calibration; else FALSE
951  */
952 HAL_BOOL
953 ar5210CalNoiseFloor(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *ichan)
954 {
955         int32_t nf, nfLoops;
956
957         /* Calibrate the noise floor */
958         OS_REG_WRITE(ah, AR_PHY_AGCCTL,
959                 OS_REG_READ(ah, AR_PHY_AGCCTL) | AR_PHY_AGC_NF);
960
961         /* Do not read noise floor until it has done the first update */
962         if (!ath_hal_wait(ah, AR_PHY_AGCCTL, AR_PHY_AGC_NF, 0)) {
963 #ifdef ATH_HAL_DEBUG
964                 ath_hal_printf(ah, " -PHY NF Reg state: 0x%x\n",
965                         OS_REG_READ(ah, AR_PHY_AGCCTL));
966                 ath_hal_printf(ah, " -MAC Reset Reg state: 0x%x\n",
967                         OS_REG_READ(ah, AR_RC));
968                 ath_hal_printf(ah, " -PHY Active Reg state: 0x%x\n",
969                         OS_REG_READ(ah, AR_PHY_ACTIVE));
970 #endif /* ATH_HAL_DEBUG */
971                 return AH_FALSE;
972         }
973
974         nf = 0;
975         /* Keep checking until the floor is below the threshold or the nf is done */
976         for (nfLoops = 0; ((nfLoops < 21) && (nf > NORMAL_NF_THRESH)); nfLoops++) {
977                 OS_DELAY(1000); /* Sleep for 1 ms */
978                 nf = ar5210GetNoiseFloor(ah);
979         }
980
981         if (nf > NORMAL_NF_THRESH) {
982                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: Bad noise cal %d\n",
983                     __func__, nf);
984                 ichan->rawNoiseFloor = 0;
985                 return AH_FALSE;
986         }
987         ichan->rawNoiseFloor = nf;
988         return AH_TRUE;
989 }
990
991 /*
992  * Adjust NF based on statistical values for 5GHz frequencies.
993  */
994 int16_t
995 ar5210GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)
996 {
997         return 0;
998 }
999
1000 HAL_RFGAIN
1001 ar5210GetRfgain(struct ath_hal *ah)
1002 {
1003         return HAL_RFGAIN_INACTIVE;
1004 }