Remove empty DragonFly CVS IDs.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5210 / ar5210_attach.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_attach.c 192397 2009-05-19 17:35:15Z sam $
18  */
19 #include "opt_ah.h"
20
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.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 static  HAL_BOOL ar5210GetChannelEdges(struct ath_hal *,
32                 uint16_t flags, uint16_t *low, uint16_t *high);
33 static  HAL_BOOL ar5210GetChipPowerLimits(struct ath_hal *ah,
34                 struct ieee80211_channel *chan);
35
36 static void ar5210ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore);
37 static void ar5210DisablePCIE(struct ath_hal *ah);
38
39 static const struct ath_hal_private ar5210hal = {{
40         .ah_magic                       = AR5210_MAGIC,
41
42         .ah_getRateTable                = ar5210GetRateTable,
43         .ah_detach                      = ar5210Detach,
44
45         /* Reset Functions */
46         .ah_reset                       = ar5210Reset,
47         .ah_phyDisable                  = ar5210PhyDisable,
48         .ah_disable                     = ar5210Disable,
49         .ah_configPCIE                  = ar5210ConfigPCIE,
50         .ah_disablePCIE                 = ar5210DisablePCIE,
51         .ah_setPCUConfig                = ar5210SetPCUConfig,
52         .ah_perCalibration              = ar5210PerCalibration,
53         .ah_perCalibrationN             = ar5210PerCalibrationN,
54         .ah_resetCalValid               = ar5210ResetCalValid,
55         .ah_setTxPowerLimit             = ar5210SetTxPowerLimit,
56         .ah_getChanNoise                = ath_hal_getChanNoise,
57
58         /* Transmit functions */
59         .ah_updateTxTrigLevel           = ar5210UpdateTxTrigLevel,
60         .ah_setupTxQueue                = ar5210SetupTxQueue,
61         .ah_setTxQueueProps             = ar5210SetTxQueueProps,
62         .ah_getTxQueueProps             = ar5210GetTxQueueProps,
63         .ah_releaseTxQueue              = ar5210ReleaseTxQueue,
64         .ah_resetTxQueue                = ar5210ResetTxQueue,
65         .ah_getTxDP                     = ar5210GetTxDP,
66         .ah_setTxDP                     = ar5210SetTxDP,
67         .ah_numTxPending                = ar5210NumTxPending,
68         .ah_startTxDma                  = ar5210StartTxDma,
69         .ah_stopTxDma                   = ar5210StopTxDma,
70         .ah_setupTxDesc                 = ar5210SetupTxDesc,
71         .ah_setupXTxDesc                = ar5210SetupXTxDesc,
72         .ah_fillTxDesc                  = ar5210FillTxDesc,
73         .ah_procTxDesc                  = ar5210ProcTxDesc,
74         .ah_getTxIntrQueue              = ar5210GetTxIntrQueue,
75         .ah_reqTxIntrDesc               = ar5210IntrReqTxDesc,
76
77         /* RX Functions */
78         .ah_getRxDP                     = ar5210GetRxDP,
79         .ah_setRxDP                     = ar5210SetRxDP,
80         .ah_enableReceive               = ar5210EnableReceive,
81         .ah_stopDmaReceive              = ar5210StopDmaReceive,
82         .ah_startPcuReceive             = ar5210StartPcuReceive,
83         .ah_stopPcuReceive              = ar5210StopPcuReceive,
84         .ah_setMulticastFilter          = ar5210SetMulticastFilter,
85         .ah_setMulticastFilterIndex     = ar5210SetMulticastFilterIndex,
86         .ah_clrMulticastFilterIndex     = ar5210ClrMulticastFilterIndex,
87         .ah_getRxFilter                 = ar5210GetRxFilter,
88         .ah_setRxFilter                 = ar5210SetRxFilter,
89         .ah_setupRxDesc                 = ar5210SetupRxDesc,
90         .ah_procRxDesc                  = ar5210ProcRxDesc,
91         .ah_rxMonitor                   = ar5210AniPoll,
92         .ah_procMibEvent                = ar5210MibEvent,
93
94         /* Misc Functions */
95         .ah_getCapability               = ar5210GetCapability,
96         .ah_setCapability               = ar5210SetCapability,
97         .ah_getDiagState                = ar5210GetDiagState,
98         .ah_getMacAddress               = ar5210GetMacAddress,
99         .ah_setMacAddress               = ar5210SetMacAddress,
100         .ah_getBssIdMask                = ar5210GetBssIdMask,
101         .ah_setBssIdMask                = ar5210SetBssIdMask,
102         .ah_setRegulatoryDomain         = ar5210SetRegulatoryDomain,
103         .ah_setLedState                 = ar5210SetLedState,
104         .ah_writeAssocid                = ar5210WriteAssocid,
105         .ah_gpioCfgInput                = ar5210GpioCfgInput,
106         .ah_gpioCfgOutput               = ar5210GpioCfgOutput,
107         .ah_gpioGet                     = ar5210GpioGet,
108         .ah_gpioSet                     = ar5210GpioSet,
109         .ah_gpioSetIntr                 = ar5210Gpio0SetIntr,
110         .ah_getTsf32                    = ar5210GetTsf32,
111         .ah_getTsf64                    = ar5210GetTsf64,
112         .ah_resetTsf                    = ar5210ResetTsf,
113         .ah_detectCardPresent           = ar5210DetectCardPresent,
114         .ah_updateMibCounters           = ar5210UpdateMibCounters,
115         .ah_getRfGain                   = ar5210GetRfgain,
116         .ah_getDefAntenna               = ar5210GetDefAntenna,
117         .ah_setDefAntenna               = ar5210SetDefAntenna,
118         .ah_getAntennaSwitch            = ar5210GetAntennaSwitch,
119         .ah_setAntennaSwitch            = ar5210SetAntennaSwitch,
120         .ah_setSifsTime                 = ar5210SetSifsTime,
121         .ah_getSifsTime                 = ar5210GetSifsTime,
122         .ah_setSlotTime                 = ar5210SetSlotTime,
123         .ah_getSlotTime                 = ar5210GetSlotTime,
124         .ah_setAckTimeout               = ar5210SetAckTimeout,
125         .ah_getAckTimeout               = ar5210GetAckTimeout,
126         .ah_setAckCTSRate               = ar5210SetAckCTSRate,
127         .ah_getAckCTSRate               = ar5210GetAckCTSRate,
128         .ah_setCTSTimeout               = ar5210SetCTSTimeout,
129         .ah_getCTSTimeout               = ar5210GetCTSTimeout,
130         .ah_setDecompMask               = ar5210SetDecompMask,
131         .ah_setCoverageClass            = ar5210SetCoverageClass,
132
133         /* Key Cache Functions */
134         .ah_getKeyCacheSize             = ar5210GetKeyCacheSize,
135         .ah_resetKeyCacheEntry          = ar5210ResetKeyCacheEntry,
136         .ah_isKeyCacheEntryValid        = ar5210IsKeyCacheEntryValid,
137         .ah_setKeyCacheEntry            = ar5210SetKeyCacheEntry,
138         .ah_setKeyCacheEntryMac         = ar5210SetKeyCacheEntryMac,
139
140         /* Power Management Functions */
141         .ah_setPowerMode                = ar5210SetPowerMode,
142         .ah_getPowerMode                = ar5210GetPowerMode,
143
144         /* Beacon Functions */
145         .ah_setBeaconTimers             = ar5210SetBeaconTimers,
146         .ah_beaconInit                  = ar5210BeaconInit,
147         .ah_setStationBeaconTimers      = ar5210SetStaBeaconTimers,
148         .ah_resetStationBeaconTimers    = ar5210ResetStaBeaconTimers,
149
150         /* Interrupt Functions */
151         .ah_isInterruptPending          = ar5210IsInterruptPending,
152         .ah_getPendingInterrupts        = ar5210GetPendingInterrupts,
153         .ah_getInterrupts               = ar5210GetInterrupts,
154         .ah_setInterrupts               = ar5210SetInterrupts },
155
156         .ah_getChannelEdges             = ar5210GetChannelEdges,
157         .ah_getWirelessModes            = ar5210GetWirelessModes,
158         .ah_eepromRead                  = ar5210EepromRead,
159 #ifdef AH_SUPPORT_WRITE_EEPROM
160         .ah_eepromWrite                 = ar5210EepromWrite,
161 #endif
162         .ah_getChipPowerLimits          = ar5210GetChipPowerLimits,
163 };
164
165 static HAL_BOOL ar5210FillCapabilityInfo(struct ath_hal *ah);
166
167 /*
168  * Attach for an AR5210 part.
169  */
170 static struct ath_hal *
171 ar5210Attach(uint16_t devid, HAL_SOFTC sc, HAL_BUS_TAG st, HAL_BUS_HANDLE sh,
172         HAL_STATUS *status)
173 {
174         struct ath_hal_5210 *ahp;
175         struct ath_hal *ah;
176         uint32_t revid, pcicfg;
177         uint16_t eeval;
178         HAL_STATUS ecode;
179         int i;
180
181         HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH,
182             "%s: devid 0x%x sc %p st %p sh %p\n", __func__, devid,
183             sc, (void*) st, (void*) sh);
184
185         /* NB: memory is returned zero'd */
186         ahp = ath_hal_malloc(sizeof (struct ath_hal_5210));
187         if (ahp == AH_NULL) {
188                 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
189                     "%s: no memory for state block\n", __func__);
190                 ecode = HAL_ENOMEM;
191                 goto bad;
192         }
193         ah = &ahp->ah_priv.h;
194         /* set initial values */
195         OS_MEMCPY(&ahp->ah_priv, &ar5210hal, sizeof(struct ath_hal_private));
196         ah->ah_sc = sc;
197         ah->ah_st = st;
198         ah->ah_sh = sh;
199
200         ah->ah_devid = devid;                   /* NB: for AH_DEBUG_ALQ */
201         AH_PRIVATE(ah)->ah_devid = devid;
202         AH_PRIVATE(ah)->ah_subvendorid = 0;     /* XXX */
203
204         AH_PRIVATE(ah)->ah_powerLimit = AR5210_MAX_RATE_POWER;
205         AH_PRIVATE(ah)->ah_tpScale = HAL_TP_SCALE_MAX;  /* no scaling */
206
207         ahp->ah_powerMode = HAL_PM_UNDEFINED;
208         ahp->ah_staId1Defaults = 0;
209         ahp->ah_rssiThr = INIT_RSSI_THR;
210         ahp->ah_sifstime = (u_int) -1;
211         ahp->ah_slottime = (u_int) -1;
212         ahp->ah_acktimeout = (u_int) -1;
213         ahp->ah_ctstimeout = (u_int) -1;
214
215         if (!ar5210ChipReset(ah, AH_NULL)) {    /* reset chip */
216                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n",
217                     __func__);
218                 ecode = HAL_EIO;
219                 goto bad;
220         }
221
222         /* Read Revisions from Chips */
223         AH_PRIVATE(ah)->ah_macVersion = 1;
224         AH_PRIVATE(ah)->ah_macRev = OS_REG_READ(ah, AR_SREV) & 0xff;
225         AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIPID);
226         AH_PRIVATE(ah)->ah_analog2GhzRev = 0;
227
228         /* Read Radio Chip Rev Extract */
229         OS_REG_WRITE(ah, (AR_PHY_BASE + (0x34 << 2)), 0x00001c16);
230         for (i = 0; i < 4; i++)
231                 OS_REG_WRITE(ah, (AR_PHY_BASE + (0x20 << 2)), 0x00010000);
232         revid = (OS_REG_READ(ah, AR_PHY_BASE + (256 << 2)) >> 28) & 0xf;
233
234         /* Chip labelling is 1 greater than revision register for AR5110 */
235         AH_PRIVATE(ah)->ah_analog5GhzRev = ath_hal_reverseBits(revid, 4) + 1;
236
237         /*
238          * Read all the settings from the EEPROM and stash
239          * ones we'll use later.
240          */
241         pcicfg = OS_REG_READ(ah, AR_PCICFG);
242         OS_REG_WRITE(ah, AR_PCICFG, pcicfg | AR_PCICFG_EEPROMSEL);
243         ecode = ath_hal_v1EepromAttach(ah);
244         if (ecode != HAL_OK) {
245                 goto eebad;
246         }
247         ecode = ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, &eeval);
248         if (ecode != HAL_OK) {
249                 HALDEBUG(ah, HAL_DEBUG_ANY,
250                     "%s: cannot read regulatory domain from EEPROM\n",
251                     __func__);
252                 goto eebad;
253         }
254         AH_PRIVATE(ah)->ah_currentRD = eeval;
255         ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
256         if (ecode != HAL_OK) {
257                 HALDEBUG(ah, HAL_DEBUG_ANY,
258                     "%s: error getting mac address from EEPROM\n", __func__);
259                 goto eebad;
260         }
261         OS_REG_WRITE(ah, AR_PCICFG, pcicfg);    /* disable EEPROM access */
262
263         AH_PRIVATE(ah)->ah_getNfAdjust = ar5210GetNfAdjust;
264
265         /*
266          * Got everything we need now to setup the capabilities.
267          */
268         (void) ar5210FillCapabilityInfo(ah);
269
270         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
271
272         return ah;
273 eebad:
274         OS_REG_WRITE(ah, AR_PCICFG, pcicfg);    /* disable EEPROM access */
275 bad:
276         if (ahp)
277                 ath_hal_free(ahp);
278         if (status)
279                 *status = ecode;
280         return AH_NULL;
281 }
282
283 void
284 ar5210Detach(struct ath_hal *ah)
285 {
286         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
287
288         HALASSERT(ah != AH_NULL);
289         HALASSERT(ah->ah_magic == AR5210_MAGIC);
290
291         ath_hal_eepromDetach(ah);
292         ath_hal_free(ah);
293 }
294
295 /*
296  * Store the channel edges for the requested operational mode
297  */
298 static HAL_BOOL
299 ar5210GetChannelEdges(struct ath_hal *ah,
300         uint16_t flags, uint16_t *low, uint16_t *high)
301 {
302         if (flags & IEEE80211_CHAN_5GHZ) {
303                 *low = 5120;
304                 *high = 5430;
305                 return AH_TRUE;
306         } else {
307                 return AH_FALSE;
308         }
309 }
310
311 static HAL_BOOL
312 ar5210GetChipPowerLimits(struct ath_hal *ah, struct ieee80211_channel *chan)
313 {
314         /* XXX fill in, this is just a placeholder */
315         HALDEBUG(ah, HAL_DEBUG_ATTACH,
316             "%s: no min/max power for %u/0x%x\n",
317             __func__, chan->ic_freq, chan->ic_flags);
318         chan->ic_maxpower = AR5210_MAX_RATE_POWER;
319         chan->ic_minpower = 0;
320         return AH_TRUE;
321 }
322
323 static void
324 ar5210ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore)
325 {
326 }
327
328 static void
329 ar5210DisablePCIE(struct ath_hal *ah)
330 {
331 }
332
333 /*
334  * Fill all software cached or static hardware state information.
335  */
336 static HAL_BOOL
337 ar5210FillCapabilityInfo(struct ath_hal *ah)
338 {
339         struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
340         HAL_CAPABILITIES *pCap = &ahpriv->ah_caps;
341
342         pCap->halWirelessModes |= HAL_MODE_11A;
343
344         pCap->halLow5GhzChan = 5120;
345         pCap->halHigh5GhzChan = 5430;
346
347         pCap->halSleepAfterBeaconBroken = AH_TRUE;
348         pCap->halPSPollBroken = AH_FALSE;
349
350         pCap->halTotalQueues = HAL_NUM_TX_QUEUES;
351         pCap->halKeyCacheSize = 64;
352
353         /* XXX not needed */
354         pCap->halChanHalfRate = AH_FALSE;
355         pCap->halChanQuarterRate = AH_FALSE;
356
357         if (ath_hal_eepromGetFlag(ah, AR_EEP_RFKILL)) {
358                 /*
359                  * Setup initial rfsilent settings based on the EEPROM
360                  * contents.  Pin 0, polarity 0 is fixed; record this
361                  * using the EEPROM format found in later parts.
362                  */
363                 ahpriv->ah_rfsilent = SM(0, AR_EEPROM_RFSILENT_GPIO_SEL)
364                                     | SM(0, AR_EEPROM_RFSILENT_POLARITY);
365                 ahpriv->ah_rfkillEnabled = AH_TRUE;
366                 pCap->halRfSilentSupport = AH_TRUE;
367         }
368
369         pCap->halTstampPrecision = 15;          /* NB: s/w extended from 13 */
370         pCap->halIntrMask = (HAL_INT_COMMON - HAL_INT_BNR)
371                         | HAL_INT_RX
372                         | HAL_INT_TX
373                         | HAL_INT_FATAL
374                         ;
375
376         ahpriv->ah_rxornIsFatal = AH_TRUE;
377         return AH_TRUE;
378 }
379
380 static const char*
381 ar5210Probe(uint16_t vendorid, uint16_t devid)
382 {
383         if (vendorid == ATHEROS_VENDOR_ID &&
384             (devid == AR5210_PROD || devid == AR5210_DEFAULT))
385                 return "Atheros 5210";
386         return AH_NULL;
387 }
388 AH_CHIP(AR5210, ar5210Probe, ar5210Attach);