1/*
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2006 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/ar5211/ar5211_attach.c 188970 2009-02-23 23:48:17Z 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 "ar5211/ar5211.h"
26#include "ar5211/ar5211reg.h"
27#include "ar5211/ar5211phy.h"
28
29#include "ah_eeprom_v3.h"
30
31static HAL_BOOL ar5211GetChannelEdges(struct ath_hal *ah,
32 uint16_t flags, uint16_t *low, uint16_t *high);
33static HAL_BOOL ar5211GetChipPowerLimits(struct ath_hal *ah,
34 struct ieee80211_channel *chan);
35
36static const struct ath_hal_private ar5211hal = {{
37 .ah_magic = AR5211_MAGIC,
38 .ah_abi = HAL_ABI_VERSION,
39
40 .ah_getRateTable = ar5211GetRateTable,
41 .ah_detach = ar5211Detach,
42
43 /* Reset Functions */
44 .ah_reset = ar5211Reset,
45 .ah_phyDisable = ar5211PhyDisable,
46 .ah_disable = ar5211Disable,
47 .ah_setPCUConfig = ar5211SetPCUConfig,
48 .ah_perCalibration = ar5211PerCalibration,
49 .ah_perCalibrationN = ar5211PerCalibrationN,
50 .ah_resetCalValid = ar5211ResetCalValid,
51 .ah_setTxPowerLimit = ar5211SetTxPowerLimit,
52 .ah_getChanNoise = ath_hal_getChanNoise,
53
54 /* Transmit functions */
55 .ah_updateTxTrigLevel = ar5211UpdateTxTrigLevel,
56 .ah_setupTxQueue = ar5211SetupTxQueue,
57 .ah_setTxQueueProps = ar5211SetTxQueueProps,
58 .ah_getTxQueueProps = ar5211GetTxQueueProps,
59 .ah_releaseTxQueue = ar5211ReleaseTxQueue,
60 .ah_resetTxQueue = ar5211ResetTxQueue,
61 .ah_getTxDP = ar5211GetTxDP,
62 .ah_setTxDP = ar5211SetTxDP,
63 .ah_numTxPending = ar5211NumTxPending,
64 .ah_startTxDma = ar5211StartTxDma,
65 .ah_stopTxDma = ar5211StopTxDma,
66 .ah_setupTxDesc = ar5211SetupTxDesc,
67 .ah_setupXTxDesc = ar5211SetupXTxDesc,
68 .ah_fillTxDesc = ar5211FillTxDesc,
69 .ah_procTxDesc = ar5211ProcTxDesc,
70 .ah_getTxIntrQueue = ar5211GetTxIntrQueue,
71 .ah_reqTxIntrDesc = ar5211IntrReqTxDesc,
72
73 /* RX Functions */
74 .ah_getRxDP = ar5211GetRxDP,
75 .ah_setRxDP = ar5211SetRxDP,
76 .ah_enableReceive = ar5211EnableReceive,
77 .ah_stopDmaReceive = ar5211StopDmaReceive,
78 .ah_startPcuReceive = ar5211StartPcuReceive,
79 .ah_stopPcuReceive = ar5211StopPcuReceive,
80 .ah_setMulticastFilter = ar5211SetMulticastFilter,
81 .ah_setMulticastFilterIndex = ar5211SetMulticastFilterIndex,
82 .ah_clrMulticastFilterIndex = ar5211ClrMulticastFilterIndex,
83 .ah_getRxFilter = ar5211GetRxFilter,
84 .ah_setRxFilter = ar5211SetRxFilter,
85 .ah_setupRxDesc = ar5211SetupRxDesc,
86 .ah_procRxDesc = ar5211ProcRxDesc,
87 .ah_rxMonitor = ar5211AniPoll,
88 .ah_procMibEvent = ar5211MibEvent,
89
90 /* Misc Functions */
91 .ah_getCapability = ar5211GetCapability,
92 .ah_setCapability = ar5211SetCapability,
93 .ah_getDiagState = ar5211GetDiagState,
94 .ah_getMacAddress = ar5211GetMacAddress,
95 .ah_setMacAddress = ar5211SetMacAddress,
96 .ah_getBssIdMask = ar5211GetBssIdMask,
97 .ah_setBssIdMask = ar5211SetBssIdMask,
98 .ah_setRegulatoryDomain = ar5211SetRegulatoryDomain,
99 .ah_setLedState = ar5211SetLedState,
100 .ah_writeAssocid = ar5211WriteAssocid,
101 .ah_gpioCfgInput = ar5211GpioCfgInput,
102 .ah_gpioCfgOutput = ar5211GpioCfgOutput,
103 .ah_gpioGet = ar5211GpioGet,
104 .ah_gpioSet = ar5211GpioSet,
105 .ah_gpioSetIntr = ar5211GpioSetIntr,
106 .ah_getTsf32 = ar5211GetTsf32,
107 .ah_getTsf64 = ar5211GetTsf64,
108 .ah_resetTsf = ar5211ResetTsf,
109 .ah_detectCardPresent = ar5211DetectCardPresent,
110 .ah_updateMibCounters = ar5211UpdateMibCounters,
111 .ah_getRfGain = ar5211GetRfgain,
112 .ah_getDefAntenna = ar5211GetDefAntenna,
113 .ah_setDefAntenna = ar5211SetDefAntenna,
114 .ah_getAntennaSwitch = ar5211GetAntennaSwitch,
115 .ah_setAntennaSwitch = ar5211SetAntennaSwitch,
116 .ah_setSifsTime = ar5211SetSifsTime,
117 .ah_getSifsTime = ar5211GetSifsTime,
118 .ah_setSlotTime = ar5211SetSlotTime,
119 .ah_getSlotTime = ar5211GetSlotTime,
120 .ah_setAckTimeout = ar5211SetAckTimeout,
121 .ah_getAckTimeout = ar5211GetAckTimeout,
122 .ah_setAckCTSRate = ar5211SetAckCTSRate,
123 .ah_getAckCTSRate = ar5211GetAckCTSRate,
124 .ah_setCTSTimeout = ar5211SetCTSTimeout,
125 .ah_getCTSTimeout = ar5211GetCTSTimeout,
126 .ah_setDecompMask = ar5211SetDecompMask,
127 .ah_setCoverageClass = ar5211SetCoverageClass,
128
129 /* Key Cache Functions */
130 .ah_getKeyCacheSize = ar5211GetKeyCacheSize,
131 .ah_resetKeyCacheEntry = ar5211ResetKeyCacheEntry,
132 .ah_isKeyCacheEntryValid = ar5211IsKeyCacheEntryValid,
133 .ah_setKeyCacheEntry = ar5211SetKeyCacheEntry,
134 .ah_setKeyCacheEntryMac = ar5211SetKeyCacheEntryMac,
135
136 /* Power Management Functions */
137 .ah_setPowerMode = ar5211SetPowerMode,
138 .ah_getPowerMode = ar5211GetPowerMode,
139
140 /* Beacon Functions */
141 .ah_setBeaconTimers = ar5211SetBeaconTimers,
142 .ah_beaconInit = ar5211BeaconInit,
143 .ah_setStationBeaconTimers = ar5211SetStaBeaconTimers,
144 .ah_resetStationBeaconTimers = ar5211ResetStaBeaconTimers,
145
146 /* Interrupt Functions */
147 .ah_isInterruptPending = ar5211IsInterruptPending,
148 .ah_getPendingInterrupts = ar5211GetPendingInterrupts,
149 .ah_getInterrupts = ar5211GetInterrupts,
150 .ah_setInterrupts = ar5211SetInterrupts },
151
152 .ah_getChannelEdges = ar5211GetChannelEdges,
153 .ah_getWirelessModes = ar5211GetWirelessModes,
154 .ah_eepromRead = ar5211EepromRead,
155#ifdef AH_SUPPORT_WRITE_EEPROM
156 .ah_eepromWrite = ar5211EepromWrite,
157#endif
158 .ah_getChipPowerLimits = ar5211GetChipPowerLimits,
159};
160
161static HAL_BOOL ar5211ChipTest(struct ath_hal *);
162static HAL_BOOL ar5211FillCapabilityInfo(struct ath_hal *ah);
163
164/*
165 * Return the revsion id for the radio chip. This
166 * fetched via the PHY.
167 */
168static uint32_t
169ar5211GetRadioRev(struct ath_hal *ah)
170{
171 uint32_t val;
172 int i;
173
174 OS_REG_WRITE(ah, (AR_PHY_BASE + (0x34 << 2)), 0x00001c16);
175 for (i = 0; i < 8; i++)
176 OS_REG_WRITE(ah, (AR_PHY_BASE + (0x20 << 2)), 0x00010000);
177 val = (OS_REG_READ(ah, AR_PHY_BASE + (256 << 2)) >> 24) & 0xff;
178 val = ((val & 0xf0) >> 4) | ((val & 0x0f) << 4);
179 return ath_hal_reverseBits(val, 8);
180}
181
182/*
183 * Attach for an AR5211 part.
184 */
185struct ath_hal *
186ar5211Attach(uint16_t devid, HAL_SOFTC sc,
187 HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
188{
189#define N(a) (sizeof(a)/sizeof(a[0]))
190 struct ath_hal_5211 *ahp;
191 struct ath_hal *ah;
192 uint32_t val;
193 uint16_t eeval;
194 HAL_STATUS ecode;
195
196 HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
197 __func__, sc, (void*) st, (void*) sh);
198
199 /* NB: memory is returned zero'd */
200 ahp = ath_hal_malloc(sizeof (struct ath_hal_5211));
201 if (ahp == AH_NULL) {
202 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
203 "%s: cannot allocate memory for state block\n", __func__);
204 ecode = HAL_ENOMEM;
205 goto bad;
206 }
207 ah = &ahp->ah_priv.h;
208 /* set initial values */
209 OS_MEMCPY(&ahp->ah_priv, &ar5211hal, sizeof(struct ath_hal_private));
210 ah->ah_sc = sc;
211 ah->ah_st = st;
212 ah->ah_sh = sh;
213
214 ah->ah_devid = devid; /* NB: for AH_DEBUG_ALQ */
215 AH_PRIVATE(ah)->ah_devid = devid;
216 AH_PRIVATE(ah)->ah_subvendorid = 0; /* XXX */
217
218 AH_PRIVATE(ah)->ah_powerLimit = MAX_RATE_POWER;
219 AH_PRIVATE(ah)->ah_tpScale = HAL_TP_SCALE_MAX; /* no scaling */
220
221 ahp->ah_diversityControl = HAL_ANT_VARIABLE;
222 ahp->ah_staId1Defaults = 0;
223 ahp->ah_rssiThr = INIT_RSSI_THR;
224 ahp->ah_sifstime = (u_int) -1;
225 ahp->ah_slottime = (u_int) -1;
226 ahp->ah_acktimeout = (u_int) -1;
227 ahp->ah_ctstimeout = (u_int) -1;
228
229 if (!ar5211ChipReset(ah, AH_NULL)) { /* reset chip */
230 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
231 ecode = HAL_EIO;
232 goto bad;
233 }
234 if (AH_PRIVATE(ah)->ah_devid == AR5211_FPGA11B) {
235 /* set it back to OFDM mode to be able to read analog rev id */
236 OS_REG_WRITE(ah, AR5211_PHY_MODE, AR5211_PHY_MODE_OFDM);
237 OS_REG_WRITE(ah, AR_PHY_PLL_CTL, AR_PHY_PLL_CTL_44);
238 OS_DELAY(1000);
239 }
240
241 /* Read Revisions from Chips */
242 val = OS_REG_READ(ah, AR_SREV) & AR_SREV_ID_M;
243 AH_PRIVATE(ah)->ah_macVersion = val >> AR_SREV_ID_S;
244 AH_PRIVATE(ah)->ah_macRev = val & AR_SREV_REVISION_M;
245
246 if (AH_PRIVATE(ah)->ah_macVersion < AR_SREV_VERSION_MAUI_2 ||
247 AH_PRIVATE(ah)->ah_macVersion > AR_SREV_VERSION_OAHU) {
248 HALDEBUG(ah, HAL_DEBUG_ANY,
249 "%s: Mac Chip Rev 0x%x is not supported by this driver\n",
250 __func__, AH_PRIVATE(ah)->ah_macVersion);
251 ecode = HAL_ENOTSUPP;
252 goto bad;
253 }
254
255 AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
256
257 if (!ar5211ChipTest(ah)) {
258 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
259 __func__);
260 ecode = HAL_ESELFTEST;
261 goto bad;
262 }
263
264 /* Set correct Baseband to analog shift setting to access analog chips. */
265 if (AH_PRIVATE(ah)->ah_macVersion >= AR_SREV_VERSION_OAHU) {
266 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000007);
267 } else {
268 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000047);
269 }
270 OS_DELAY(2000);
271
272 /* Read Radio Chip Rev Extract */
273 AH_PRIVATE(ah)->ah_analog5GhzRev = ar5211GetRadioRev(ah);
274 if ((AH_PRIVATE(ah)->ah_analog5GhzRev & 0xf0) != RAD5_SREV_MAJOR) {
275 HALDEBUG(ah, HAL_DEBUG_ANY,
276 "%s: 5G Radio Chip Rev 0x%02X is not supported by this "
277 "driver\n", __func__, AH_PRIVATE(ah)->ah_analog5GhzRev);
278 ecode = HAL_ENOTSUPP;
279 goto bad;
280 }
281
282 val = (OS_REG_READ(ah, AR_PCICFG) & AR_PCICFG_EEPROM_SIZE_M) >>
283 AR_PCICFG_EEPROM_SIZE_S;
284 if (val != AR_PCICFG_EEPROM_SIZE_16K) {
285 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unsupported EEPROM size "
286 "%u (0x%x) found\n", __func__, val, val);
287 ecode = HAL_EESIZE;
288 goto bad;
289 }
290 ecode = ath_hal_legacyEepromAttach(ah);
291 if (ecode != HAL_OK) {
292 goto bad;
293 }
294
295 /* If Bmode and AR5211, verify 2.4 analog exists */
296 if (AH_PRIVATE(ah)->ah_macVersion >= AR_SREV_VERSION_OAHU &&
297 ath_hal_eepromGetFlag(ah, AR_EEP_BMODE)) {
298 /* Set correct Baseband to analog shift setting to access analog chips. */
299 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00004007);
300 OS_DELAY(2000);
301 AH_PRIVATE(ah)->ah_analog2GhzRev = ar5211GetRadioRev(ah);
302
303 /* Set baseband for 5GHz chip */
304 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000007);
305 OS_DELAY(2000);
306 if ((AH_PRIVATE(ah)->ah_analog2GhzRev & 0xF0) != RAD2_SREV_MAJOR) {
307 HALDEBUG(ah, HAL_DEBUG_ANY,
308 "%s: 2G Radio Chip Rev 0x%x is not supported by "
309 "this driver\n", __func__,
310 AH_PRIVATE(ah)->ah_analog2GhzRev);
311 ecode = HAL_ENOTSUPP;
312 goto bad;
313 }
314 } else {
315 ath_hal_eepromSet(ah, AR_EEP_BMODE, AH_FALSE);
316 }
317
318 ecode = ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, &eeval);
319 if (ecode != HAL_OK) {
320 HALDEBUG(ah, HAL_DEBUG_ANY,
321 "%s: cannot read regulatory domain from EEPROM\n",
322 __func__);
323 goto bad;
324 }
325 AH_PRIVATE(ah)->ah_currentRD = eeval;
326 AH_PRIVATE(ah)->ah_getNfAdjust = ar5211GetNfAdjust;
327
328 /*
329 * Got everything we need now to setup the capabilities.
330 */
331 (void) ar5211FillCapabilityInfo(ah);
332
333 /* Initialize gain ladder thermal calibration structure */
334 ar5211InitializeGainValues(ah);
335
336 ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
337 if (ecode != HAL_OK) {
338 HALDEBUG(ah, HAL_DEBUG_ANY,
339 "%s: error getting mac address from EEPROM\n", __func__);
340 goto bad;
341 }
342
343 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
344
345 return ah;
346bad:
347 if (ahp)
348 ar5211Detach((struct ath_hal *) ahp);
349 if (status)
350 *status = ecode;
351 return AH_NULL;
352#undef N
353}
354
355void
356ar5211Detach(struct ath_hal *ah)
357{
358 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
359
360 HALASSERT(ah != AH_NULL);
361 HALASSERT(ah->ah_magic == AR5211_MAGIC);
362
363 ath_hal_eepromDetach(ah);
364 ath_hal_free(ah);
365}
366
367static HAL_BOOL
368ar5211ChipTest(struct ath_hal *ah)
369{
370 uint32_t regAddr[2] = { AR_STA_ID0, AR_PHY_BASE+(8 << 2) };
371 uint32_t regHold[2];
372 uint32_t patternData[4] =
373 { 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999 };
374 int i, j;
375
376 /* Test PHY & MAC registers */
377 for (i = 0; i < 2; i++) {
378 uint32_t addr = regAddr[i];
379 uint32_t wrData, rdData;
380
381 regHold[i] = OS_REG_READ(ah, addr);
382 for (j = 0; j < 0x100; j++) {
383 wrData = (j << 16) | j;
384 OS_REG_WRITE(ah, addr, wrData);
385 rdData = OS_REG_READ(ah, addr);
386 if (rdData != wrData) {
387 HALDEBUG(ah, HAL_DEBUG_ANY,
388"%s: address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
389 __func__, addr, wrData, rdData);
390 return AH_FALSE;
391 }
392 }
393 for (j = 0; j < 4; j++) {
394 wrData = patternData[j];
395 OS_REG_WRITE(ah, addr, wrData);
396 rdData = OS_REG_READ(ah, addr);
397 if (wrData != rdData) {
398 HALDEBUG(ah, HAL_DEBUG_ANY,
399"%s: address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
400 __func__, addr, wrData, rdData);
401 return AH_FALSE;
402 }
403 }
404 OS_REG_WRITE(ah, regAddr[i], regHold[i]);
405 }
406 OS_DELAY(100);
407 return AH_TRUE;
408}
409
410/*
411 * Store the channel edges for the requested operational mode
412 */
413static HAL_BOOL
414ar5211GetChannelEdges(struct ath_hal *ah,
415 uint16_t flags, uint16_t *low, uint16_t *high)
416{
417 if (flags & IEEE80211_CHAN_5GHZ) {
418 *low = 4920;
419 *high = 6100;
420 return AH_TRUE;
421 }
422 if (flags & IEEE80211_CHAN_2GHZ &&
423 ath_hal_eepromGetFlag(ah, AR_EEP_BMODE)) {
424 *low = 2312;
425 *high = 2732;
426 return AH_TRUE;
427 }
428 return AH_FALSE;
429}
430
431static HAL_BOOL
432ar5211GetChipPowerLimits(struct ath_hal *ah, struct ieee80211_channel *chan)
433{
434 /* XXX fill in, this is just a placeholder */
435 HALDEBUG(ah, HAL_DEBUG_ATTACH,
436 "%s: no min/max power for %u/0x%x\n",
437 __func__, chan->ic_freq, chan->ic_flags);
438 chan->ic_maxpower = MAX_RATE_POWER;
439 chan->ic_minpower = 0;
440 return AH_TRUE;
441}
442
443/*
444 * Fill all software cached or static hardware state information.
445 */
446static HAL_BOOL
447ar5211FillCapabilityInfo(struct ath_hal *ah)
448{
449 struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
450 HAL_CAPABILITIES *pCap = &ahpriv->ah_caps;
451
452 /* Construct wireless mode from EEPROM */
453 pCap->halWirelessModes = 0;
454 if (ath_hal_eepromGetFlag(ah, AR_EEP_AMODE)) {
455 pCap->halWirelessModes |= HAL_MODE_11A;
456 if (!ath_hal_eepromGetFlag(ah, AR_EEP_TURBO5DISABLE))
457 pCap->halWirelessModes |= HAL_MODE_TURBO;
458 }
459 if (ath_hal_eepromGetFlag(ah, AR_EEP_BMODE))
460 pCap->halWirelessModes |= HAL_MODE_11B;
461
462 pCap->halLow2GhzChan = 2312;
463 pCap->halHigh2GhzChan = 2732;
464 pCap->halLow5GhzChan = 4920;
465 pCap->halHigh5GhzChan = 6100;
466
467 pCap->halChanSpreadSupport = AH_TRUE;
468 pCap->halSleepAfterBeaconBroken = AH_TRUE;
469 pCap->halPSPollBroken = AH_TRUE;
470 pCap->halVEOLSupport = AH_TRUE;
471
472 pCap->halTotalQueues = HAL_NUM_TX_QUEUES;
473 pCap->halKeyCacheSize = 128;
474
475 /* XXX not needed */
476 pCap->halChanHalfRate = AH_FALSE;
477 pCap->halChanQuarterRate = AH_FALSE;
478
479 if (ath_hal_eepromGetFlag(ah, AR_EEP_RFKILL) &&
480 ath_hal_eepromGet(ah, AR_EEP_RFSILENT, &ahpriv->ah_rfsilent) == HAL_OK) {
481 /* NB: enabled by default */
482 ahpriv->ah_rfkillEnabled = AH_TRUE;
483 pCap->halRfSilentSupport = AH_TRUE;
484 }
485
486 pCap->halTstampPrecision = 13;
487
488 /* XXX might be ok w/ some chip revs */
489 ahpriv->ah_rxornIsFatal = AH_TRUE;
490 return AH_TRUE;
491}
492
493static const char*
494ar5211Probe(uint16_t vendorid, uint16_t devid)
495{
496 if (vendorid == ATHEROS_VENDOR_ID) {
497 if (devid == AR5211_DEVID || devid == AR5311_DEVID ||
498 devid == AR5211_DEFAULT)
499 return "Atheros 5211";
500 if (devid == AR5211_FPGA11B)
501 return "Atheros 5211 (FPGA)";
502 }
503 return AH_NULL;
504}
505AH_CHIP(AR5211, ar5211Probe, ar5211Attach);
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2006 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/ar5211/ar5211_attach.c 188970 2009-02-23 23:48:17Z 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 "ar5211/ar5211.h"
26#include "ar5211/ar5211reg.h"
27#include "ar5211/ar5211phy.h"
28
29#include "ah_eeprom_v3.h"
30
31static HAL_BOOL ar5211GetChannelEdges(struct ath_hal *ah,
32 uint16_t flags, uint16_t *low, uint16_t *high);
33static HAL_BOOL ar5211GetChipPowerLimits(struct ath_hal *ah,
34 struct ieee80211_channel *chan);
35
36static const struct ath_hal_private ar5211hal = {{
37 .ah_magic = AR5211_MAGIC,
38 .ah_abi = HAL_ABI_VERSION,
39
40 .ah_getRateTable = ar5211GetRateTable,
41 .ah_detach = ar5211Detach,
42
43 /* Reset Functions */
44 .ah_reset = ar5211Reset,
45 .ah_phyDisable = ar5211PhyDisable,
46 .ah_disable = ar5211Disable,
47 .ah_setPCUConfig = ar5211SetPCUConfig,
48 .ah_perCalibration = ar5211PerCalibration,
49 .ah_perCalibrationN = ar5211PerCalibrationN,
50 .ah_resetCalValid = ar5211ResetCalValid,
51 .ah_setTxPowerLimit = ar5211SetTxPowerLimit,
52 .ah_getChanNoise = ath_hal_getChanNoise,
53
54 /* Transmit functions */
55 .ah_updateTxTrigLevel = ar5211UpdateTxTrigLevel,
56 .ah_setupTxQueue = ar5211SetupTxQueue,
57 .ah_setTxQueueProps = ar5211SetTxQueueProps,
58 .ah_getTxQueueProps = ar5211GetTxQueueProps,
59 .ah_releaseTxQueue = ar5211ReleaseTxQueue,
60 .ah_resetTxQueue = ar5211ResetTxQueue,
61 .ah_getTxDP = ar5211GetTxDP,
62 .ah_setTxDP = ar5211SetTxDP,
63 .ah_numTxPending = ar5211NumTxPending,
64 .ah_startTxDma = ar5211StartTxDma,
65 .ah_stopTxDma = ar5211StopTxDma,
66 .ah_setupTxDesc = ar5211SetupTxDesc,
67 .ah_setupXTxDesc = ar5211SetupXTxDesc,
68 .ah_fillTxDesc = ar5211FillTxDesc,
69 .ah_procTxDesc = ar5211ProcTxDesc,
70 .ah_getTxIntrQueue = ar5211GetTxIntrQueue,
71 .ah_reqTxIntrDesc = ar5211IntrReqTxDesc,
72
73 /* RX Functions */
74 .ah_getRxDP = ar5211GetRxDP,
75 .ah_setRxDP = ar5211SetRxDP,
76 .ah_enableReceive = ar5211EnableReceive,
77 .ah_stopDmaReceive = ar5211StopDmaReceive,
78 .ah_startPcuReceive = ar5211StartPcuReceive,
79 .ah_stopPcuReceive = ar5211StopPcuReceive,
80 .ah_setMulticastFilter = ar5211SetMulticastFilter,
81 .ah_setMulticastFilterIndex = ar5211SetMulticastFilterIndex,
82 .ah_clrMulticastFilterIndex = ar5211ClrMulticastFilterIndex,
83 .ah_getRxFilter = ar5211GetRxFilter,
84 .ah_setRxFilter = ar5211SetRxFilter,
85 .ah_setupRxDesc = ar5211SetupRxDesc,
86 .ah_procRxDesc = ar5211ProcRxDesc,
87 .ah_rxMonitor = ar5211AniPoll,
88 .ah_procMibEvent = ar5211MibEvent,
89
90 /* Misc Functions */
91 .ah_getCapability = ar5211GetCapability,
92 .ah_setCapability = ar5211SetCapability,
93 .ah_getDiagState = ar5211GetDiagState,
94 .ah_getMacAddress = ar5211GetMacAddress,
95 .ah_setMacAddress = ar5211SetMacAddress,
96 .ah_getBssIdMask = ar5211GetBssIdMask,
97 .ah_setBssIdMask = ar5211SetBssIdMask,
98 .ah_setRegulatoryDomain = ar5211SetRegulatoryDomain,
99 .ah_setLedState = ar5211SetLedState,
100 .ah_writeAssocid = ar5211WriteAssocid,
101 .ah_gpioCfgInput = ar5211GpioCfgInput,
102 .ah_gpioCfgOutput = ar5211GpioCfgOutput,
103 .ah_gpioGet = ar5211GpioGet,
104 .ah_gpioSet = ar5211GpioSet,
105 .ah_gpioSetIntr = ar5211GpioSetIntr,
106 .ah_getTsf32 = ar5211GetTsf32,
107 .ah_getTsf64 = ar5211GetTsf64,
108 .ah_resetTsf = ar5211ResetTsf,
109 .ah_detectCardPresent = ar5211DetectCardPresent,
110 .ah_updateMibCounters = ar5211UpdateMibCounters,
111 .ah_getRfGain = ar5211GetRfgain,
112 .ah_getDefAntenna = ar5211GetDefAntenna,
113 .ah_setDefAntenna = ar5211SetDefAntenna,
114 .ah_getAntennaSwitch = ar5211GetAntennaSwitch,
115 .ah_setAntennaSwitch = ar5211SetAntennaSwitch,
116 .ah_setSifsTime = ar5211SetSifsTime,
117 .ah_getSifsTime = ar5211GetSifsTime,
118 .ah_setSlotTime = ar5211SetSlotTime,
119 .ah_getSlotTime = ar5211GetSlotTime,
120 .ah_setAckTimeout = ar5211SetAckTimeout,
121 .ah_getAckTimeout = ar5211GetAckTimeout,
122 .ah_setAckCTSRate = ar5211SetAckCTSRate,
123 .ah_getAckCTSRate = ar5211GetAckCTSRate,
124 .ah_setCTSTimeout = ar5211SetCTSTimeout,
125 .ah_getCTSTimeout = ar5211GetCTSTimeout,
126 .ah_setDecompMask = ar5211SetDecompMask,
127 .ah_setCoverageClass = ar5211SetCoverageClass,
128
129 /* Key Cache Functions */
130 .ah_getKeyCacheSize = ar5211GetKeyCacheSize,
131 .ah_resetKeyCacheEntry = ar5211ResetKeyCacheEntry,
132 .ah_isKeyCacheEntryValid = ar5211IsKeyCacheEntryValid,
133 .ah_setKeyCacheEntry = ar5211SetKeyCacheEntry,
134 .ah_setKeyCacheEntryMac = ar5211SetKeyCacheEntryMac,
135
136 /* Power Management Functions */
137 .ah_setPowerMode = ar5211SetPowerMode,
138 .ah_getPowerMode = ar5211GetPowerMode,
139
140 /* Beacon Functions */
141 .ah_setBeaconTimers = ar5211SetBeaconTimers,
142 .ah_beaconInit = ar5211BeaconInit,
143 .ah_setStationBeaconTimers = ar5211SetStaBeaconTimers,
144 .ah_resetStationBeaconTimers = ar5211ResetStaBeaconTimers,
145
146 /* Interrupt Functions */
147 .ah_isInterruptPending = ar5211IsInterruptPending,
148 .ah_getPendingInterrupts = ar5211GetPendingInterrupts,
149 .ah_getInterrupts = ar5211GetInterrupts,
150 .ah_setInterrupts = ar5211SetInterrupts },
151
152 .ah_getChannelEdges = ar5211GetChannelEdges,
153 .ah_getWirelessModes = ar5211GetWirelessModes,
154 .ah_eepromRead = ar5211EepromRead,
155#ifdef AH_SUPPORT_WRITE_EEPROM
156 .ah_eepromWrite = ar5211EepromWrite,
157#endif
158 .ah_getChipPowerLimits = ar5211GetChipPowerLimits,
159};
160
161static HAL_BOOL ar5211ChipTest(struct ath_hal *);
162static HAL_BOOL ar5211FillCapabilityInfo(struct ath_hal *ah);
163
164/*
165 * Return the revsion id for the radio chip. This
166 * fetched via the PHY.
167 */
168static uint32_t
169ar5211GetRadioRev(struct ath_hal *ah)
170{
171 uint32_t val;
172 int i;
173
174 OS_REG_WRITE(ah, (AR_PHY_BASE + (0x34 << 2)), 0x00001c16);
175 for (i = 0; i < 8; i++)
176 OS_REG_WRITE(ah, (AR_PHY_BASE + (0x20 << 2)), 0x00010000);
177 val = (OS_REG_READ(ah, AR_PHY_BASE + (256 << 2)) >> 24) & 0xff;
178 val = ((val & 0xf0) >> 4) | ((val & 0x0f) << 4);
179 return ath_hal_reverseBits(val, 8);
180}
181
182/*
183 * Attach for an AR5211 part.
184 */
185struct ath_hal *
186ar5211Attach(uint16_t devid, HAL_SOFTC sc,
187 HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
188{
189#define N(a) (sizeof(a)/sizeof(a[0]))
190 struct ath_hal_5211 *ahp;
191 struct ath_hal *ah;
192 uint32_t val;
193 uint16_t eeval;
194 HAL_STATUS ecode;
195
196 HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
197 __func__, sc, (void*) st, (void*) sh);
198
199 /* NB: memory is returned zero'd */
200 ahp = ath_hal_malloc(sizeof (struct ath_hal_5211));
201 if (ahp == AH_NULL) {
202 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
203 "%s: cannot allocate memory for state block\n", __func__);
204 ecode = HAL_ENOMEM;
205 goto bad;
206 }
207 ah = &ahp->ah_priv.h;
208 /* set initial values */
209 OS_MEMCPY(&ahp->ah_priv, &ar5211hal, sizeof(struct ath_hal_private));
210 ah->ah_sc = sc;
211 ah->ah_st = st;
212 ah->ah_sh = sh;
213
214 ah->ah_devid = devid; /* NB: for AH_DEBUG_ALQ */
215 AH_PRIVATE(ah)->ah_devid = devid;
216 AH_PRIVATE(ah)->ah_subvendorid = 0; /* XXX */
217
218 AH_PRIVATE(ah)->ah_powerLimit = MAX_RATE_POWER;
219 AH_PRIVATE(ah)->ah_tpScale = HAL_TP_SCALE_MAX; /* no scaling */
220
221 ahp->ah_diversityControl = HAL_ANT_VARIABLE;
222 ahp->ah_staId1Defaults = 0;
223 ahp->ah_rssiThr = INIT_RSSI_THR;
224 ahp->ah_sifstime = (u_int) -1;
225 ahp->ah_slottime = (u_int) -1;
226 ahp->ah_acktimeout = (u_int) -1;
227 ahp->ah_ctstimeout = (u_int) -1;
228
229 if (!ar5211ChipReset(ah, AH_NULL)) { /* reset chip */
230 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
231 ecode = HAL_EIO;
232 goto bad;
233 }
234 if (AH_PRIVATE(ah)->ah_devid == AR5211_FPGA11B) {
235 /* set it back to OFDM mode to be able to read analog rev id */
236 OS_REG_WRITE(ah, AR5211_PHY_MODE, AR5211_PHY_MODE_OFDM);
237 OS_REG_WRITE(ah, AR_PHY_PLL_CTL, AR_PHY_PLL_CTL_44);
238 OS_DELAY(1000);
239 }
240
241 /* Read Revisions from Chips */
242 val = OS_REG_READ(ah, AR_SREV) & AR_SREV_ID_M;
243 AH_PRIVATE(ah)->ah_macVersion = val >> AR_SREV_ID_S;
244 AH_PRIVATE(ah)->ah_macRev = val & AR_SREV_REVISION_M;
245
246 if (AH_PRIVATE(ah)->ah_macVersion < AR_SREV_VERSION_MAUI_2 ||
247 AH_PRIVATE(ah)->ah_macVersion > AR_SREV_VERSION_OAHU) {
248 HALDEBUG(ah, HAL_DEBUG_ANY,
249 "%s: Mac Chip Rev 0x%x is not supported by this driver\n",
250 __func__, AH_PRIVATE(ah)->ah_macVersion);
251 ecode = HAL_ENOTSUPP;
252 goto bad;
253 }
254
255 AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
256
257 if (!ar5211ChipTest(ah)) {
258 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
259 __func__);
260 ecode = HAL_ESELFTEST;
261 goto bad;
262 }
263
264 /* Set correct Baseband to analog shift setting to access analog chips. */
265 if (AH_PRIVATE(ah)->ah_macVersion >= AR_SREV_VERSION_OAHU) {
266 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000007);
267 } else {
268 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000047);
269 }
270 OS_DELAY(2000);
271
272 /* Read Radio Chip Rev Extract */
273 AH_PRIVATE(ah)->ah_analog5GhzRev = ar5211GetRadioRev(ah);
274 if ((AH_PRIVATE(ah)->ah_analog5GhzRev & 0xf0) != RAD5_SREV_MAJOR) {
275 HALDEBUG(ah, HAL_DEBUG_ANY,
276 "%s: 5G Radio Chip Rev 0x%02X is not supported by this "
277 "driver\n", __func__, AH_PRIVATE(ah)->ah_analog5GhzRev);
278 ecode = HAL_ENOTSUPP;
279 goto bad;
280 }
281
282 val = (OS_REG_READ(ah, AR_PCICFG) & AR_PCICFG_EEPROM_SIZE_M) >>
283 AR_PCICFG_EEPROM_SIZE_S;
284 if (val != AR_PCICFG_EEPROM_SIZE_16K) {
285 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unsupported EEPROM size "
286 "%u (0x%x) found\n", __func__, val, val);
287 ecode = HAL_EESIZE;
288 goto bad;
289 }
290 ecode = ath_hal_legacyEepromAttach(ah);
291 if (ecode != HAL_OK) {
292 goto bad;
293 }
294
295 /* If Bmode and AR5211, verify 2.4 analog exists */
296 if (AH_PRIVATE(ah)->ah_macVersion >= AR_SREV_VERSION_OAHU &&
297 ath_hal_eepromGetFlag(ah, AR_EEP_BMODE)) {
298 /* Set correct Baseband to analog shift setting to access analog chips. */
299 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00004007);
300 OS_DELAY(2000);
301 AH_PRIVATE(ah)->ah_analog2GhzRev = ar5211GetRadioRev(ah);
302
303 /* Set baseband for 5GHz chip */
304 OS_REG_WRITE(ah, AR_PHY_BASE, 0x00000007);
305 OS_DELAY(2000);
306 if ((AH_PRIVATE(ah)->ah_analog2GhzRev & 0xF0) != RAD2_SREV_MAJOR) {
307 HALDEBUG(ah, HAL_DEBUG_ANY,
308 "%s: 2G Radio Chip Rev 0x%x is not supported by "
309 "this driver\n", __func__,
310 AH_PRIVATE(ah)->ah_analog2GhzRev);
311 ecode = HAL_ENOTSUPP;
312 goto bad;
313 }
314 } else {
315 ath_hal_eepromSet(ah, AR_EEP_BMODE, AH_FALSE);
316 }
317
318 ecode = ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, &eeval);
319 if (ecode != HAL_OK) {
320 HALDEBUG(ah, HAL_DEBUG_ANY,
321 "%s: cannot read regulatory domain from EEPROM\n",
322 __func__);
323 goto bad;
324 }
325 AH_PRIVATE(ah)->ah_currentRD = eeval;
326 AH_PRIVATE(ah)->ah_getNfAdjust = ar5211GetNfAdjust;
327
328 /*
329 * Got everything we need now to setup the capabilities.
330 */
331 (void) ar5211FillCapabilityInfo(ah);
332
333 /* Initialize gain ladder thermal calibration structure */
334 ar5211InitializeGainValues(ah);
335
336 ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
337 if (ecode != HAL_OK) {
338 HALDEBUG(ah, HAL_DEBUG_ANY,
339 "%s: error getting mac address from EEPROM\n", __func__);
340 goto bad;
341 }
342
343 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
344
345 return ah;
346bad:
347 if (ahp)
348 ar5211Detach((struct ath_hal *) ahp);
349 if (status)
350 *status = ecode;
351 return AH_NULL;
352#undef N
353}
354
355void
356ar5211Detach(struct ath_hal *ah)
357{
358 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
359
360 HALASSERT(ah != AH_NULL);
361 HALASSERT(ah->ah_magic == AR5211_MAGIC);
362
363 ath_hal_eepromDetach(ah);
364 ath_hal_free(ah);
365}
366
367static HAL_BOOL
368ar5211ChipTest(struct ath_hal *ah)
369{
370 uint32_t regAddr[2] = { AR_STA_ID0, AR_PHY_BASE+(8 << 2) };
371 uint32_t regHold[2];
372 uint32_t patternData[4] =
373 { 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999 };
374 int i, j;
375
376 /* Test PHY & MAC registers */
377 for (i = 0; i < 2; i++) {
378 uint32_t addr = regAddr[i];
379 uint32_t wrData, rdData;
380
381 regHold[i] = OS_REG_READ(ah, addr);
382 for (j = 0; j < 0x100; j++) {
383 wrData = (j << 16) | j;
384 OS_REG_WRITE(ah, addr, wrData);
385 rdData = OS_REG_READ(ah, addr);
386 if (rdData != wrData) {
387 HALDEBUG(ah, HAL_DEBUG_ANY,
388"%s: address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
389 __func__, addr, wrData, rdData);
390 return AH_FALSE;
391 }
392 }
393 for (j = 0; j < 4; j++) {
394 wrData = patternData[j];
395 OS_REG_WRITE(ah, addr, wrData);
396 rdData = OS_REG_READ(ah, addr);
397 if (wrData != rdData) {
398 HALDEBUG(ah, HAL_DEBUG_ANY,
399"%s: address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
400 __func__, addr, wrData, rdData);
401 return AH_FALSE;
402 }
403 }
404 OS_REG_WRITE(ah, regAddr[i], regHold[i]);
405 }
406 OS_DELAY(100);
407 return AH_TRUE;
408}
409
410/*
411 * Store the channel edges for the requested operational mode
412 */
413static HAL_BOOL
414ar5211GetChannelEdges(struct ath_hal *ah,
415 uint16_t flags, uint16_t *low, uint16_t *high)
416{
417 if (flags & IEEE80211_CHAN_5GHZ) {
418 *low = 4920;
419 *high = 6100;
420 return AH_TRUE;
421 }
422 if (flags & IEEE80211_CHAN_2GHZ &&
423 ath_hal_eepromGetFlag(ah, AR_EEP_BMODE)) {
424 *low = 2312;
425 *high = 2732;
426 return AH_TRUE;
427 }
428 return AH_FALSE;
429}
430
431static HAL_BOOL
432ar5211GetChipPowerLimits(struct ath_hal *ah, struct ieee80211_channel *chan)
433{
434 /* XXX fill in, this is just a placeholder */
435 HALDEBUG(ah, HAL_DEBUG_ATTACH,
436 "%s: no min/max power for %u/0x%x\n",
437 __func__, chan->ic_freq, chan->ic_flags);
438 chan->ic_maxpower = MAX_RATE_POWER;
439 chan->ic_minpower = 0;
440 return AH_TRUE;
441}
442
443/*
444 * Fill all software cached or static hardware state information.
445 */
446static HAL_BOOL
447ar5211FillCapabilityInfo(struct ath_hal *ah)
448{
449 struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
450 HAL_CAPABILITIES *pCap = &ahpriv->ah_caps;
451
452 /* Construct wireless mode from EEPROM */
453 pCap->halWirelessModes = 0;
454 if (ath_hal_eepromGetFlag(ah, AR_EEP_AMODE)) {
455 pCap->halWirelessModes |= HAL_MODE_11A;
456 if (!ath_hal_eepromGetFlag(ah, AR_EEP_TURBO5DISABLE))
457 pCap->halWirelessModes |= HAL_MODE_TURBO;
458 }
459 if (ath_hal_eepromGetFlag(ah, AR_EEP_BMODE))
460 pCap->halWirelessModes |= HAL_MODE_11B;
461
462 pCap->halLow2GhzChan = 2312;
463 pCap->halHigh2GhzChan = 2732;
464 pCap->halLow5GhzChan = 4920;
465 pCap->halHigh5GhzChan = 6100;
466
467 pCap->halChanSpreadSupport = AH_TRUE;
468 pCap->halSleepAfterBeaconBroken = AH_TRUE;
469 pCap->halPSPollBroken = AH_TRUE;
470 pCap->halVEOLSupport = AH_TRUE;
471
472 pCap->halTotalQueues = HAL_NUM_TX_QUEUES;
473 pCap->halKeyCacheSize = 128;
474
475 /* XXX not needed */
476 pCap->halChanHalfRate = AH_FALSE;
477 pCap->halChanQuarterRate = AH_FALSE;
478
479 if (ath_hal_eepromGetFlag(ah, AR_EEP_RFKILL) &&
480 ath_hal_eepromGet(ah, AR_EEP_RFSILENT, &ahpriv->ah_rfsilent) == HAL_OK) {
481 /* NB: enabled by default */
482 ahpriv->ah_rfkillEnabled = AH_TRUE;
483 pCap->halRfSilentSupport = AH_TRUE;
484 }
485
486 pCap->halTstampPrecision = 13;
487
488 /* XXX might be ok w/ some chip revs */
489 ahpriv->ah_rxornIsFatal = AH_TRUE;
490 return AH_TRUE;
491}
492
493static const char*
494ar5211Probe(uint16_t vendorid, uint16_t devid)
495{
496 if (vendorid == ATHEROS_VENDOR_ID) {
497 if (devid == AR5211_DEVID || devid == AR5311_DEVID ||
498 devid == AR5211_DEFAULT)
499 return "Atheros 5211";
500 if (devid == AR5211_FPGA11B)
501 return "Atheros 5211 (FPGA)";
502 }
503 return AH_NULL;
504}
505AH_CHIP(AR5211, ar5211Probe, ar5211Attach);