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