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