Merge branch 'master' into for-davem
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / ath / ath9k / hw.c
blob5a29048db3b192c496d3a5507961fe2ed7e5be94
1 /*
2 * Copyright (c) 2008-2010 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
21 #include "hw.h"
22 #include "hw-ops.h"
23 #include "rc.h"
24 #include "ar9003_mac.h"
26 #define ATH9K_CLOCK_RATE_CCK 22
27 #define ATH9K_CLOCK_RATE_5GHZ_OFDM 40
28 #define ATH9K_CLOCK_RATE_2GHZ_OFDM 44
30 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
32 MODULE_AUTHOR("Atheros Communications");
33 MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
34 MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
35 MODULE_LICENSE("Dual BSD/GPL");
37 static int __init ath9k_init(void)
39 return 0;
41 module_init(ath9k_init);
43 static void __exit ath9k_exit(void)
45 return;
47 module_exit(ath9k_exit);
49 /* Private hardware callbacks */
51 static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
53 ath9k_hw_private_ops(ah)->init_cal_settings(ah);
56 static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
58 ath9k_hw_private_ops(ah)->init_mode_regs(ah);
61 static bool ath9k_hw_macversion_supported(struct ath_hw *ah)
63 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
65 return priv_ops->macversion_supported(ah->hw_version.macVersion);
68 static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
69 struct ath9k_channel *chan)
71 return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
74 static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
76 if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
77 return;
79 ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
82 /********************/
83 /* Helper Functions */
84 /********************/
86 static u32 ath9k_hw_mac_clks(struct ath_hw *ah, u32 usecs)
88 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
90 if (!ah->curchan) /* should really check for CCK instead */
91 return usecs *ATH9K_CLOCK_RATE_CCK;
92 if (conf->channel->band == IEEE80211_BAND_2GHZ)
93 return usecs *ATH9K_CLOCK_RATE_2GHZ_OFDM;
94 return usecs *ATH9K_CLOCK_RATE_5GHZ_OFDM;
97 static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
99 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
101 if (conf_is_ht40(conf))
102 return ath9k_hw_mac_clks(ah, usecs) * 2;
103 else
104 return ath9k_hw_mac_clks(ah, usecs);
107 bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
109 int i;
111 BUG_ON(timeout < AH_TIME_QUANTUM);
113 for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
114 if ((REG_READ(ah, reg) & mask) == val)
115 return true;
117 udelay(AH_TIME_QUANTUM);
120 ath_print(ath9k_hw_common(ah), ATH_DBG_ANY,
121 "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
122 timeout, reg, REG_READ(ah, reg), mask, val);
124 return false;
126 EXPORT_SYMBOL(ath9k_hw_wait);
128 u32 ath9k_hw_reverse_bits(u32 val, u32 n)
130 u32 retval;
131 int i;
133 for (i = 0, retval = 0; i < n; i++) {
134 retval = (retval << 1) | (val & 1);
135 val >>= 1;
137 return retval;
140 bool ath9k_get_channel_edges(struct ath_hw *ah,
141 u16 flags, u16 *low,
142 u16 *high)
144 struct ath9k_hw_capabilities *pCap = &ah->caps;
146 if (flags & CHANNEL_5GHZ) {
147 *low = pCap->low_5ghz_chan;
148 *high = pCap->high_5ghz_chan;
149 return true;
151 if ((flags & CHANNEL_2GHZ)) {
152 *low = pCap->low_2ghz_chan;
153 *high = pCap->high_2ghz_chan;
154 return true;
156 return false;
159 u16 ath9k_hw_computetxtime(struct ath_hw *ah,
160 u8 phy, int kbps,
161 u32 frameLen, u16 rateix,
162 bool shortPreamble)
164 u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
166 if (kbps == 0)
167 return 0;
169 switch (phy) {
170 case WLAN_RC_PHY_CCK:
171 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
172 if (shortPreamble)
173 phyTime >>= 1;
174 numBits = frameLen << 3;
175 txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
176 break;
177 case WLAN_RC_PHY_OFDM:
178 if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
179 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
180 numBits = OFDM_PLCP_BITS + (frameLen << 3);
181 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
182 txTime = OFDM_SIFS_TIME_QUARTER
183 + OFDM_PREAMBLE_TIME_QUARTER
184 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
185 } else if (ah->curchan &&
186 IS_CHAN_HALF_RATE(ah->curchan)) {
187 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
188 numBits = OFDM_PLCP_BITS + (frameLen << 3);
189 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
190 txTime = OFDM_SIFS_TIME_HALF +
191 OFDM_PREAMBLE_TIME_HALF
192 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
193 } else {
194 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
195 numBits = OFDM_PLCP_BITS + (frameLen << 3);
196 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
197 txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
198 + (numSymbols * OFDM_SYMBOL_TIME);
200 break;
201 default:
202 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
203 "Unknown phy %u (rate ix %u)\n", phy, rateix);
204 txTime = 0;
205 break;
208 return txTime;
210 EXPORT_SYMBOL(ath9k_hw_computetxtime);
212 void ath9k_hw_get_channel_centers(struct ath_hw *ah,
213 struct ath9k_channel *chan,
214 struct chan_centers *centers)
216 int8_t extoff;
218 if (!IS_CHAN_HT40(chan)) {
219 centers->ctl_center = centers->ext_center =
220 centers->synth_center = chan->channel;
221 return;
224 if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
225 (chan->chanmode == CHANNEL_G_HT40PLUS)) {
226 centers->synth_center =
227 chan->channel + HT40_CHANNEL_CENTER_SHIFT;
228 extoff = 1;
229 } else {
230 centers->synth_center =
231 chan->channel - HT40_CHANNEL_CENTER_SHIFT;
232 extoff = -1;
235 centers->ctl_center =
236 centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
237 /* 25 MHz spacing is supported by hw but not on upper layers */
238 centers->ext_center =
239 centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
242 /******************/
243 /* Chip Revisions */
244 /******************/
246 static void ath9k_hw_read_revisions(struct ath_hw *ah)
248 u32 val;
250 val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
252 if (val == 0xFF) {
253 val = REG_READ(ah, AR_SREV);
254 ah->hw_version.macVersion =
255 (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
256 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
257 ah->is_pciexpress = (val & AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
258 } else {
259 if (!AR_SREV_9100(ah))
260 ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
262 ah->hw_version.macRev = val & AR_SREV_REVISION;
264 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
265 ah->is_pciexpress = true;
269 /************************************/
270 /* HW Attach, Detach, Init Routines */
271 /************************************/
273 static void ath9k_hw_disablepcie(struct ath_hw *ah)
275 if (AR_SREV_9100(ah))
276 return;
278 ENABLE_REGWRITE_BUFFER(ah);
280 REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
281 REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
282 REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
283 REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
284 REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
285 REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
286 REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
287 REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
288 REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
290 REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
292 REGWRITE_BUFFER_FLUSH(ah);
293 DISABLE_REGWRITE_BUFFER(ah);
296 /* This should work for all families including legacy */
297 static bool ath9k_hw_chip_test(struct ath_hw *ah)
299 struct ath_common *common = ath9k_hw_common(ah);
300 u32 regAddr[2] = { AR_STA_ID0 };
301 u32 regHold[2];
302 u32 patternData[4] = { 0x55555555,
303 0xaaaaaaaa,
304 0x66666666,
305 0x99999999 };
306 int i, j, loop_max;
308 if (!AR_SREV_9300_20_OR_LATER(ah)) {
309 loop_max = 2;
310 regAddr[1] = AR_PHY_BASE + (8 << 2);
311 } else
312 loop_max = 1;
314 for (i = 0; i < loop_max; i++) {
315 u32 addr = regAddr[i];
316 u32 wrData, rdData;
318 regHold[i] = REG_READ(ah, addr);
319 for (j = 0; j < 0x100; j++) {
320 wrData = (j << 16) | j;
321 REG_WRITE(ah, addr, wrData);
322 rdData = REG_READ(ah, addr);
323 if (rdData != wrData) {
324 ath_print(common, ATH_DBG_FATAL,
325 "address test failed "
326 "addr: 0x%08x - wr:0x%08x != "
327 "rd:0x%08x\n",
328 addr, wrData, rdData);
329 return false;
332 for (j = 0; j < 4; j++) {
333 wrData = patternData[j];
334 REG_WRITE(ah, addr, wrData);
335 rdData = REG_READ(ah, addr);
336 if (wrData != rdData) {
337 ath_print(common, ATH_DBG_FATAL,
338 "address test failed "
339 "addr: 0x%08x - wr:0x%08x != "
340 "rd:0x%08x\n",
341 addr, wrData, rdData);
342 return false;
345 REG_WRITE(ah, regAddr[i], regHold[i]);
347 udelay(100);
349 return true;
352 static void ath9k_hw_init_config(struct ath_hw *ah)
354 int i;
356 ah->config.dma_beacon_response_time = 2;
357 ah->config.sw_beacon_response_time = 10;
358 ah->config.additional_swba_backoff = 0;
359 ah->config.ack_6mb = 0x0;
360 ah->config.cwm_ignore_extcca = 0;
361 ah->config.pcie_powersave_enable = 0;
362 ah->config.pcie_clock_req = 0;
363 ah->config.pcie_waen = 0;
364 ah->config.analog_shiftreg = 1;
365 ah->config.ofdm_trig_low = 200;
366 ah->config.ofdm_trig_high = 500;
367 ah->config.cck_trig_high = 200;
368 ah->config.cck_trig_low = 100;
371 * For now ANI is disabled for AR9003, it is still
372 * being tested.
374 if (!AR_SREV_9300_20_OR_LATER(ah))
375 ah->config.enable_ani = 1;
377 for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
378 ah->config.spurchans[i][0] = AR_NO_SPUR;
379 ah->config.spurchans[i][1] = AR_NO_SPUR;
382 if (ah->hw_version.devid != AR2427_DEVID_PCIE)
383 ah->config.ht_enable = 1;
384 else
385 ah->config.ht_enable = 0;
387 ah->config.rx_intr_mitigation = true;
390 * We need this for PCI devices only (Cardbus, PCI, miniPCI)
391 * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
392 * This means we use it for all AR5416 devices, and the few
393 * minor PCI AR9280 devices out there.
395 * Serialization is required because these devices do not handle
396 * well the case of two concurrent reads/writes due to the latency
397 * involved. During one read/write another read/write can be issued
398 * on another CPU while the previous read/write may still be working
399 * on our hardware, if we hit this case the hardware poops in a loop.
400 * We prevent this by serializing reads and writes.
402 * This issue is not present on PCI-Express devices or pre-AR5416
403 * devices (legacy, 802.11abg).
405 if (num_possible_cpus() > 1)
406 ah->config.serialize_regmode = SER_REG_MODE_AUTO;
409 static void ath9k_hw_init_defaults(struct ath_hw *ah)
411 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
413 regulatory->country_code = CTRY_DEFAULT;
414 regulatory->power_limit = MAX_RATE_POWER;
415 regulatory->tp_scale = ATH9K_TP_SCALE_MAX;
417 ah->hw_version.magic = AR5416_MAGIC;
418 ah->hw_version.subvendorid = 0;
420 ah->ah_flags = 0;
421 if (!AR_SREV_9100(ah))
422 ah->ah_flags = AH_USE_EEPROM;
424 ah->atim_window = 0;
425 ah->sta_id1_defaults = AR_STA_ID1_CRPT_MIC_ENABLE;
426 ah->beacon_interval = 100;
427 ah->enable_32kHz_clock = DONT_USE_32KHZ;
428 ah->slottime = (u32) -1;
429 ah->globaltxtimeout = (u32) -1;
430 ah->power_mode = ATH9K_PM_UNDEFINED;
433 static int ath9k_hw_init_macaddr(struct ath_hw *ah)
435 struct ath_common *common = ath9k_hw_common(ah);
436 u32 sum;
437 int i;
438 u16 eeval;
439 u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
441 sum = 0;
442 for (i = 0; i < 3; i++) {
443 eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
444 sum += eeval;
445 common->macaddr[2 * i] = eeval >> 8;
446 common->macaddr[2 * i + 1] = eeval & 0xff;
448 if (sum == 0 || sum == 0xffff * 3)
449 return -EADDRNOTAVAIL;
451 return 0;
454 static int ath9k_hw_post_init(struct ath_hw *ah)
456 int ecode;
458 if (!AR_SREV_9271(ah)) {
459 if (!ath9k_hw_chip_test(ah))
460 return -ENODEV;
463 if (!AR_SREV_9300_20_OR_LATER(ah)) {
464 ecode = ar9002_hw_rf_claim(ah);
465 if (ecode != 0)
466 return ecode;
469 ecode = ath9k_hw_eeprom_init(ah);
470 if (ecode != 0)
471 return ecode;
473 ath_print(ath9k_hw_common(ah), ATH_DBG_CONFIG,
474 "Eeprom VER: %d, REV: %d\n",
475 ah->eep_ops->get_eeprom_ver(ah),
476 ah->eep_ops->get_eeprom_rev(ah));
478 ecode = ath9k_hw_rf_alloc_ext_banks(ah);
479 if (ecode) {
480 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
481 "Failed allocating banks for "
482 "external radio\n");
483 return ecode;
486 if (!AR_SREV_9100(ah)) {
487 ath9k_hw_ani_setup(ah);
488 ath9k_hw_ani_init(ah);
491 return 0;
494 static void ath9k_hw_attach_ops(struct ath_hw *ah)
496 if (AR_SREV_9300_20_OR_LATER(ah))
497 ar9003_hw_attach_ops(ah);
498 else
499 ar9002_hw_attach_ops(ah);
502 /* Called for all hardware families */
503 static int __ath9k_hw_init(struct ath_hw *ah)
505 struct ath_common *common = ath9k_hw_common(ah);
506 int r = 0;
508 if (ah->hw_version.devid == AR5416_AR9100_DEVID)
509 ah->hw_version.macVersion = AR_SREV_VERSION_9100;
511 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
512 ath_print(common, ATH_DBG_FATAL,
513 "Couldn't reset chip\n");
514 return -EIO;
517 ath9k_hw_init_defaults(ah);
518 ath9k_hw_init_config(ah);
520 ath9k_hw_attach_ops(ah);
522 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
523 ath_print(common, ATH_DBG_FATAL, "Couldn't wakeup chip\n");
524 return -EIO;
527 if (ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
528 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
529 (AR_SREV_9280(ah) && !ah->is_pciexpress)) {
530 ah->config.serialize_regmode =
531 SER_REG_MODE_ON;
532 } else {
533 ah->config.serialize_regmode =
534 SER_REG_MODE_OFF;
538 ath_print(common, ATH_DBG_RESET, "serialize_regmode is %d\n",
539 ah->config.serialize_regmode);
541 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
542 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
543 else
544 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
546 if (!ath9k_hw_macversion_supported(ah)) {
547 ath_print(common, ATH_DBG_FATAL,
548 "Mac Chip Rev 0x%02x.%x is not supported by "
549 "this driver\n", ah->hw_version.macVersion,
550 ah->hw_version.macRev);
551 return -EOPNOTSUPP;
554 if (AR_SREV_9271(ah) || AR_SREV_9100(ah))
555 ah->is_pciexpress = false;
557 ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
558 ath9k_hw_init_cal_settings(ah);
560 ah->ani_function = ATH9K_ANI_ALL;
561 if (AR_SREV_9280_10_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
562 ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
564 ath9k_hw_init_mode_regs(ah);
566 if (ah->is_pciexpress)
567 ath9k_hw_configpcipowersave(ah, 0, 0);
568 else
569 ath9k_hw_disablepcie(ah);
571 if (!AR_SREV_9300_20_OR_LATER(ah))
572 ar9002_hw_cck_chan14_spread(ah);
574 r = ath9k_hw_post_init(ah);
575 if (r)
576 return r;
578 ath9k_hw_init_mode_gain_regs(ah);
579 r = ath9k_hw_fill_cap_info(ah);
580 if (r)
581 return r;
583 r = ath9k_hw_init_macaddr(ah);
584 if (r) {
585 ath_print(common, ATH_DBG_FATAL,
586 "Failed to initialize MAC address\n");
587 return r;
590 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
591 ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
592 else
593 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
595 if (AR_SREV_9300_20_OR_LATER(ah))
596 ar9003_hw_set_nf_limits(ah);
598 ath9k_init_nfcal_hist_buffer(ah);
600 common->state = ATH_HW_INITIALIZED;
602 return 0;
605 int ath9k_hw_init(struct ath_hw *ah)
607 int ret;
608 struct ath_common *common = ath9k_hw_common(ah);
610 /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
611 switch (ah->hw_version.devid) {
612 case AR5416_DEVID_PCI:
613 case AR5416_DEVID_PCIE:
614 case AR5416_AR9100_DEVID:
615 case AR9160_DEVID_PCI:
616 case AR9280_DEVID_PCI:
617 case AR9280_DEVID_PCIE:
618 case AR9285_DEVID_PCIE:
619 case AR9287_DEVID_PCI:
620 case AR9287_DEVID_PCIE:
621 case AR2427_DEVID_PCIE:
622 case AR9300_DEVID_PCIE:
623 break;
624 default:
625 if (common->bus_ops->ath_bus_type == ATH_USB)
626 break;
627 ath_print(common, ATH_DBG_FATAL,
628 "Hardware device ID 0x%04x not supported\n",
629 ah->hw_version.devid);
630 return -EOPNOTSUPP;
633 ret = __ath9k_hw_init(ah);
634 if (ret) {
635 ath_print(common, ATH_DBG_FATAL,
636 "Unable to initialize hardware; "
637 "initialization status: %d\n", ret);
638 return ret;
641 return 0;
643 EXPORT_SYMBOL(ath9k_hw_init);
645 static void ath9k_hw_init_qos(struct ath_hw *ah)
647 ENABLE_REGWRITE_BUFFER(ah);
649 REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
650 REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
652 REG_WRITE(ah, AR_QOS_NO_ACK,
653 SM(2, AR_QOS_NO_ACK_TWO_BIT) |
654 SM(5, AR_QOS_NO_ACK_BIT_OFF) |
655 SM(0, AR_QOS_NO_ACK_BYTE_OFF));
657 REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
658 REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
659 REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
660 REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
661 REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
663 REGWRITE_BUFFER_FLUSH(ah);
664 DISABLE_REGWRITE_BUFFER(ah);
667 static void ath9k_hw_init_pll(struct ath_hw *ah,
668 struct ath9k_channel *chan)
670 u32 pll = ath9k_hw_compute_pll_control(ah, chan);
672 REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
674 /* Switch the core clock for ar9271 to 117Mhz */
675 if (AR_SREV_9271(ah)) {
676 udelay(500);
677 REG_WRITE(ah, 0x50040, 0x304);
680 udelay(RTC_PLL_SETTLE_DELAY);
682 REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
685 static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
686 enum nl80211_iftype opmode)
688 u32 imr_reg = AR_IMR_TXERR |
689 AR_IMR_TXURN |
690 AR_IMR_RXERR |
691 AR_IMR_RXORN |
692 AR_IMR_BCNMISC;
694 if (AR_SREV_9300_20_OR_LATER(ah)) {
695 imr_reg |= AR_IMR_RXOK_HP;
696 if (ah->config.rx_intr_mitigation)
697 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
698 else
699 imr_reg |= AR_IMR_RXOK_LP;
701 } else {
702 if (ah->config.rx_intr_mitigation)
703 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
704 else
705 imr_reg |= AR_IMR_RXOK;
708 if (ah->config.tx_intr_mitigation)
709 imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
710 else
711 imr_reg |= AR_IMR_TXOK;
713 if (opmode == NL80211_IFTYPE_AP)
714 imr_reg |= AR_IMR_MIB;
716 ENABLE_REGWRITE_BUFFER(ah);
718 REG_WRITE(ah, AR_IMR, imr_reg);
719 ah->imrs2_reg |= AR_IMR_S2_GTT;
720 REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
722 if (!AR_SREV_9100(ah)) {
723 REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
724 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_DEFAULT);
725 REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
728 REGWRITE_BUFFER_FLUSH(ah);
729 DISABLE_REGWRITE_BUFFER(ah);
731 if (AR_SREV_9300_20_OR_LATER(ah)) {
732 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
733 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
734 REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
735 REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
739 static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
741 u32 val = ath9k_hw_mac_to_clks(ah, us);
742 val = min(val, (u32) 0xFFFF);
743 REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
746 static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
748 u32 val = ath9k_hw_mac_to_clks(ah, us);
749 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
750 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
753 static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
755 u32 val = ath9k_hw_mac_to_clks(ah, us);
756 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
757 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
760 static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
762 if (tu > 0xFFFF) {
763 ath_print(ath9k_hw_common(ah), ATH_DBG_XMIT,
764 "bad global tx timeout %u\n", tu);
765 ah->globaltxtimeout = (u32) -1;
766 return false;
767 } else {
768 REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
769 ah->globaltxtimeout = tu;
770 return true;
774 void ath9k_hw_init_global_settings(struct ath_hw *ah)
776 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
777 int acktimeout;
778 int slottime;
779 int sifstime;
781 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET, "ah->misc_mode 0x%x\n",
782 ah->misc_mode);
784 if (ah->misc_mode != 0)
785 REG_WRITE(ah, AR_PCU_MISC,
786 REG_READ(ah, AR_PCU_MISC) | ah->misc_mode);
788 if (conf->channel && conf->channel->band == IEEE80211_BAND_5GHZ)
789 sifstime = 16;
790 else
791 sifstime = 10;
793 /* As defined by IEEE 802.11-2007 17.3.8.6 */
794 slottime = ah->slottime + 3 * ah->coverage_class;
795 acktimeout = slottime + sifstime;
798 * Workaround for early ACK timeouts, add an offset to match the
799 * initval's 64us ack timeout value.
800 * This was initially only meant to work around an issue with delayed
801 * BA frames in some implementations, but it has been found to fix ACK
802 * timeout issues in other cases as well.
804 if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ)
805 acktimeout += 64 - sifstime - ah->slottime;
807 ath9k_hw_setslottime(ah, slottime);
808 ath9k_hw_set_ack_timeout(ah, acktimeout);
809 ath9k_hw_set_cts_timeout(ah, acktimeout);
810 if (ah->globaltxtimeout != (u32) -1)
811 ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
813 EXPORT_SYMBOL(ath9k_hw_init_global_settings);
815 void ath9k_hw_deinit(struct ath_hw *ah)
817 struct ath_common *common = ath9k_hw_common(ah);
819 if (common->state < ATH_HW_INITIALIZED)
820 goto free_hw;
822 if (!AR_SREV_9100(ah))
823 ath9k_hw_ani_disable(ah);
825 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
827 free_hw:
828 ath9k_hw_rf_free_ext_banks(ah);
830 EXPORT_SYMBOL(ath9k_hw_deinit);
832 /*******/
833 /* INI */
834 /*******/
836 u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
838 u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
840 if (IS_CHAN_B(chan))
841 ctl |= CTL_11B;
842 else if (IS_CHAN_G(chan))
843 ctl |= CTL_11G;
844 else
845 ctl |= CTL_11A;
847 return ctl;
850 /****************************************/
851 /* Reset and Channel Switching Routines */
852 /****************************************/
854 static inline void ath9k_hw_set_dma(struct ath_hw *ah)
856 struct ath_common *common = ath9k_hw_common(ah);
857 u32 regval;
859 ENABLE_REGWRITE_BUFFER(ah);
862 * set AHB_MODE not to do cacheline prefetches
864 if (!AR_SREV_9300_20_OR_LATER(ah)) {
865 regval = REG_READ(ah, AR_AHB_MODE);
866 REG_WRITE(ah, AR_AHB_MODE, regval | AR_AHB_PREFETCH_RD_EN);
870 * let mac dma reads be in 128 byte chunks
872 regval = REG_READ(ah, AR_TXCFG) & ~AR_TXCFG_DMASZ_MASK;
873 REG_WRITE(ah, AR_TXCFG, regval | AR_TXCFG_DMASZ_128B);
875 REGWRITE_BUFFER_FLUSH(ah);
876 DISABLE_REGWRITE_BUFFER(ah);
879 * Restore TX Trigger Level to its pre-reset value.
880 * The initial value depends on whether aggregation is enabled, and is
881 * adjusted whenever underruns are detected.
883 if (!AR_SREV_9300_20_OR_LATER(ah))
884 REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
886 ENABLE_REGWRITE_BUFFER(ah);
889 * let mac dma writes be in 128 byte chunks
891 regval = REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_DMASZ_MASK;
892 REG_WRITE(ah, AR_RXCFG, regval | AR_RXCFG_DMASZ_128B);
895 * Setup receive FIFO threshold to hold off TX activities
897 REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
899 if (AR_SREV_9300_20_OR_LATER(ah)) {
900 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
901 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
903 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
904 ah->caps.rx_status_len);
908 * reduce the number of usable entries in PCU TXBUF to avoid
909 * wrap around issues.
911 if (AR_SREV_9285(ah)) {
912 /* For AR9285 the number of Fifos are reduced to half.
913 * So set the usable tx buf size also to half to
914 * avoid data/delimiter underruns
916 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
917 AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
918 } else if (!AR_SREV_9271(ah)) {
919 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
920 AR_PCU_TXBUF_CTRL_USABLE_SIZE);
923 REGWRITE_BUFFER_FLUSH(ah);
924 DISABLE_REGWRITE_BUFFER(ah);
926 if (AR_SREV_9300_20_OR_LATER(ah))
927 ath9k_hw_reset_txstatus_ring(ah);
930 static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
932 u32 val;
934 val = REG_READ(ah, AR_STA_ID1);
935 val &= ~(AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC);
936 switch (opmode) {
937 case NL80211_IFTYPE_AP:
938 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_STA_AP
939 | AR_STA_ID1_KSRCH_MODE);
940 REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
941 break;
942 case NL80211_IFTYPE_ADHOC:
943 case NL80211_IFTYPE_MESH_POINT:
944 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_ADHOC
945 | AR_STA_ID1_KSRCH_MODE);
946 REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
947 break;
948 case NL80211_IFTYPE_STATION:
949 case NL80211_IFTYPE_MONITOR:
950 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_KSRCH_MODE);
951 break;
955 void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
956 u32 *coef_mantissa, u32 *coef_exponent)
958 u32 coef_exp, coef_man;
960 for (coef_exp = 31; coef_exp > 0; coef_exp--)
961 if ((coef_scaled >> coef_exp) & 0x1)
962 break;
964 coef_exp = 14 - (coef_exp - COEF_SCALE_S);
966 coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
968 *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
969 *coef_exponent = coef_exp - 16;
972 static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
974 u32 rst_flags;
975 u32 tmpReg;
977 if (AR_SREV_9100(ah)) {
978 u32 val = REG_READ(ah, AR_RTC_DERIVED_CLK);
979 val &= ~AR_RTC_DERIVED_CLK_PERIOD;
980 val |= SM(1, AR_RTC_DERIVED_CLK_PERIOD);
981 REG_WRITE(ah, AR_RTC_DERIVED_CLK, val);
982 (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
985 ENABLE_REGWRITE_BUFFER(ah);
987 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
988 AR_RTC_FORCE_WAKE_ON_INT);
990 if (AR_SREV_9100(ah)) {
991 rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
992 AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
993 } else {
994 tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
995 if (tmpReg &
996 (AR_INTR_SYNC_LOCAL_TIMEOUT |
997 AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
998 u32 val;
999 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
1001 val = AR_RC_HOSTIF;
1002 if (!AR_SREV_9300_20_OR_LATER(ah))
1003 val |= AR_RC_AHB;
1004 REG_WRITE(ah, AR_RC, val);
1006 } else if (!AR_SREV_9300_20_OR_LATER(ah))
1007 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1009 rst_flags = AR_RTC_RC_MAC_WARM;
1010 if (type == ATH9K_RESET_COLD)
1011 rst_flags |= AR_RTC_RC_MAC_COLD;
1014 REG_WRITE(ah, AR_RTC_RC, rst_flags);
1016 REGWRITE_BUFFER_FLUSH(ah);
1017 DISABLE_REGWRITE_BUFFER(ah);
1019 udelay(50);
1021 REG_WRITE(ah, AR_RTC_RC, 0);
1022 if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
1023 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
1024 "RTC stuck in MAC reset\n");
1025 return false;
1028 if (!AR_SREV_9100(ah))
1029 REG_WRITE(ah, AR_RC, 0);
1031 if (AR_SREV_9100(ah))
1032 udelay(50);
1034 return true;
1037 static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
1039 ENABLE_REGWRITE_BUFFER(ah);
1041 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1042 AR_RTC_FORCE_WAKE_ON_INT);
1044 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1045 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1047 REG_WRITE(ah, AR_RTC_RESET, 0);
1049 REGWRITE_BUFFER_FLUSH(ah);
1050 DISABLE_REGWRITE_BUFFER(ah);
1052 if (!AR_SREV_9300_20_OR_LATER(ah))
1053 udelay(2);
1055 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1056 REG_WRITE(ah, AR_RC, 0);
1058 REG_WRITE(ah, AR_RTC_RESET, 1);
1060 if (!ath9k_hw_wait(ah,
1061 AR_RTC_STATUS,
1062 AR_RTC_STATUS_M,
1063 AR_RTC_STATUS_ON,
1064 AH_WAIT_TIMEOUT)) {
1065 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
1066 "RTC not waking up\n");
1067 return false;
1070 ath9k_hw_read_revisions(ah);
1072 return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
1075 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
1077 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1078 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
1080 switch (type) {
1081 case ATH9K_RESET_POWER_ON:
1082 return ath9k_hw_set_reset_power_on(ah);
1083 case ATH9K_RESET_WARM:
1084 case ATH9K_RESET_COLD:
1085 return ath9k_hw_set_reset(ah, type);
1086 default:
1087 return false;
1091 static bool ath9k_hw_chip_reset(struct ath_hw *ah,
1092 struct ath9k_channel *chan)
1094 if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)) {
1095 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON))
1096 return false;
1097 } else if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
1098 return false;
1100 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1101 return false;
1103 ah->chip_fullsleep = false;
1104 ath9k_hw_init_pll(ah, chan);
1105 ath9k_hw_set_rfmode(ah, chan);
1107 return true;
1110 static bool ath9k_hw_channel_change(struct ath_hw *ah,
1111 struct ath9k_channel *chan)
1113 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1114 struct ath_common *common = ath9k_hw_common(ah);
1115 struct ieee80211_channel *channel = chan->chan;
1116 u32 qnum;
1117 int r;
1119 for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
1120 if (ath9k_hw_numtxpending(ah, qnum)) {
1121 ath_print(common, ATH_DBG_QUEUE,
1122 "Transmit frames pending on "
1123 "queue %d\n", qnum);
1124 return false;
1128 if (!ath9k_hw_rfbus_req(ah)) {
1129 ath_print(common, ATH_DBG_FATAL,
1130 "Could not kill baseband RX\n");
1131 return false;
1134 ath9k_hw_set_channel_regs(ah, chan);
1136 r = ath9k_hw_rf_set_freq(ah, chan);
1137 if (r) {
1138 ath_print(common, ATH_DBG_FATAL,
1139 "Failed to set channel\n");
1140 return false;
1143 ah->eep_ops->set_txpower(ah, chan,
1144 ath9k_regd_get_ctl(regulatory, chan),
1145 channel->max_antenna_gain * 2,
1146 channel->max_power * 2,
1147 min((u32) MAX_RATE_POWER,
1148 (u32) regulatory->power_limit));
1150 ath9k_hw_rfbus_done(ah);
1152 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1153 ath9k_hw_set_delta_slope(ah, chan);
1155 ath9k_hw_spur_mitigate_freq(ah, chan);
1157 if (!chan->oneTimeCalsDone)
1158 chan->oneTimeCalsDone = true;
1160 return true;
1163 bool ath9k_hw_check_alive(struct ath_hw *ah)
1165 int count = 50;
1166 u32 reg;
1168 if (AR_SREV_9285_10_OR_LATER(ah))
1169 return true;
1171 do {
1172 reg = REG_READ(ah, AR_OBS_BUS_1);
1174 if ((reg & 0x7E7FFFEF) == 0x00702400)
1175 continue;
1177 switch (reg & 0x7E000B00) {
1178 case 0x1E000000:
1179 case 0x52000B00:
1180 case 0x18000B00:
1181 continue;
1182 default:
1183 return true;
1185 } while (count-- > 0);
1187 return false;
1189 EXPORT_SYMBOL(ath9k_hw_check_alive);
1191 int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
1192 bool bChannelChange)
1194 struct ath_common *common = ath9k_hw_common(ah);
1195 u32 saveLedState;
1196 struct ath9k_channel *curchan = ah->curchan;
1197 u32 saveDefAntenna;
1198 u32 macStaId1;
1199 u64 tsf = 0;
1200 int i, r;
1202 ah->txchainmask = common->tx_chainmask;
1203 ah->rxchainmask = common->rx_chainmask;
1205 if (!ah->chip_fullsleep) {
1206 ath9k_hw_abortpcurecv(ah);
1207 if (!ath9k_hw_stopdmarecv(ah))
1208 ath_print(common, ATH_DBG_XMIT,
1209 "Failed to stop receive dma\n");
1212 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1213 return -EIO;
1215 if (curchan && !ah->chip_fullsleep)
1216 ath9k_hw_getnf(ah, curchan);
1218 if (bChannelChange &&
1219 (ah->chip_fullsleep != true) &&
1220 (ah->curchan != NULL) &&
1221 (chan->channel != ah->curchan->channel) &&
1222 ((chan->channelFlags & CHANNEL_ALL) ==
1223 (ah->curchan->channelFlags & CHANNEL_ALL)) &&
1224 !(AR_SREV_9280(ah) || IS_CHAN_A_5MHZ_SPACED(chan) ||
1225 IS_CHAN_A_5MHZ_SPACED(ah->curchan))) {
1227 if (ath9k_hw_channel_change(ah, chan)) {
1228 ath9k_hw_loadnf(ah, ah->curchan);
1229 ath9k_hw_start_nfcal(ah);
1230 return 0;
1234 saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
1235 if (saveDefAntenna == 0)
1236 saveDefAntenna = 1;
1238 macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
1240 /* For chips on which RTC reset is done, save TSF before it gets cleared */
1241 if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
1242 tsf = ath9k_hw_gettsf64(ah);
1244 saveLedState = REG_READ(ah, AR_CFG_LED) &
1245 (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
1246 AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
1248 ath9k_hw_mark_phy_inactive(ah);
1250 /* Only required on the first reset */
1251 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1252 REG_WRITE(ah,
1253 AR9271_RESET_POWER_DOWN_CONTROL,
1254 AR9271_RADIO_RF_RST);
1255 udelay(50);
1258 if (!ath9k_hw_chip_reset(ah, chan)) {
1259 ath_print(common, ATH_DBG_FATAL, "Chip reset failed\n");
1260 return -EINVAL;
1263 /* Only required on the first reset */
1264 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1265 ah->htc_reset_init = false;
1266 REG_WRITE(ah,
1267 AR9271_RESET_POWER_DOWN_CONTROL,
1268 AR9271_GATE_MAC_CTL);
1269 udelay(50);
1272 /* Restore TSF */
1273 if (tsf && AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
1274 ath9k_hw_settsf64(ah, tsf);
1276 if (AR_SREV_9280_10_OR_LATER(ah))
1277 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
1279 r = ath9k_hw_process_ini(ah, chan);
1280 if (r)
1281 return r;
1283 /* Setup MFP options for CCMP */
1284 if (AR_SREV_9280_20_OR_LATER(ah)) {
1285 /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
1286 * frames when constructing CCMP AAD. */
1287 REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
1288 0xc7ff);
1289 ah->sw_mgmt_crypto = false;
1290 } else if (AR_SREV_9160_10_OR_LATER(ah)) {
1291 /* Disable hardware crypto for management frames */
1292 REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
1293 AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
1294 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1295 AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
1296 ah->sw_mgmt_crypto = true;
1297 } else
1298 ah->sw_mgmt_crypto = true;
1300 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1301 ath9k_hw_set_delta_slope(ah, chan);
1303 ath9k_hw_spur_mitigate_freq(ah, chan);
1304 ah->eep_ops->set_board_values(ah, chan);
1306 ath9k_hw_set_operating_mode(ah, ah->opmode);
1308 ENABLE_REGWRITE_BUFFER(ah);
1310 REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
1311 REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
1312 | macStaId1
1313 | AR_STA_ID1_RTS_USE_DEF
1314 | (ah->config.
1315 ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
1316 | ah->sta_id1_defaults);
1317 ath_hw_setbssidmask(common);
1318 REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
1319 ath9k_hw_write_associd(ah);
1320 REG_WRITE(ah, AR_ISR, ~0);
1321 REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
1323 REGWRITE_BUFFER_FLUSH(ah);
1324 DISABLE_REGWRITE_BUFFER(ah);
1326 r = ath9k_hw_rf_set_freq(ah, chan);
1327 if (r)
1328 return r;
1330 ENABLE_REGWRITE_BUFFER(ah);
1332 for (i = 0; i < AR_NUM_DCU; i++)
1333 REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
1335 REGWRITE_BUFFER_FLUSH(ah);
1336 DISABLE_REGWRITE_BUFFER(ah);
1338 ah->intr_txqs = 0;
1339 for (i = 0; i < ah->caps.total_queues; i++)
1340 ath9k_hw_resettxqueue(ah, i);
1342 ath9k_hw_init_interrupt_masks(ah, ah->opmode);
1343 ath9k_hw_init_qos(ah);
1345 if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1346 ath9k_enable_rfkill(ah);
1348 ath9k_hw_init_global_settings(ah);
1350 if (!AR_SREV_9300_20_OR_LATER(ah)) {
1351 ar9002_hw_enable_async_fifo(ah);
1352 ar9002_hw_enable_wep_aggregation(ah);
1355 REG_WRITE(ah, AR_STA_ID1,
1356 REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PRESERVE_SEQNUM);
1358 ath9k_hw_set_dma(ah);
1360 REG_WRITE(ah, AR_OBS, 8);
1362 if (ah->config.rx_intr_mitigation) {
1363 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
1364 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
1367 if (ah->config.tx_intr_mitigation) {
1368 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
1369 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
1372 ath9k_hw_init_bb(ah, chan);
1374 if (!ath9k_hw_init_cal(ah, chan))
1375 return -EIO;
1377 ENABLE_REGWRITE_BUFFER(ah);
1379 ath9k_hw_restore_chainmask(ah);
1380 REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
1382 REGWRITE_BUFFER_FLUSH(ah);
1383 DISABLE_REGWRITE_BUFFER(ah);
1386 * For big endian systems turn on swapping for descriptors
1388 if (AR_SREV_9100(ah)) {
1389 u32 mask;
1390 mask = REG_READ(ah, AR_CFG);
1391 if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
1392 ath_print(common, ATH_DBG_RESET,
1393 "CFG Byte Swap Set 0x%x\n", mask);
1394 } else {
1395 mask =
1396 INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
1397 REG_WRITE(ah, AR_CFG, mask);
1398 ath_print(common, ATH_DBG_RESET,
1399 "Setting CFG 0x%x\n", REG_READ(ah, AR_CFG));
1401 } else {
1402 /* Configure AR9271 target WLAN */
1403 if (AR_SREV_9271(ah))
1404 REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
1405 #ifdef __BIG_ENDIAN
1406 else
1407 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1408 #endif
1411 if (ah->btcoex_hw.enabled)
1412 ath9k_hw_btcoex_enable(ah);
1414 if (AR_SREV_9300_20_OR_LATER(ah)) {
1415 ath9k_hw_loadnf(ah, curchan);
1416 ath9k_hw_start_nfcal(ah);
1419 return 0;
1421 EXPORT_SYMBOL(ath9k_hw_reset);
1423 /************************/
1424 /* Key Cache Management */
1425 /************************/
1427 bool ath9k_hw_keyreset(struct ath_hw *ah, u16 entry)
1429 u32 keyType;
1431 if (entry >= ah->caps.keycache_size) {
1432 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
1433 "keychache entry %u out of range\n", entry);
1434 return false;
1437 keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
1439 REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
1440 REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
1441 REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
1442 REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
1443 REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
1444 REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
1445 REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
1446 REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
1448 if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
1449 u16 micentry = entry + 64;
1451 REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
1452 REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
1453 REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
1454 REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
1458 return true;
1460 EXPORT_SYMBOL(ath9k_hw_keyreset);
1462 bool ath9k_hw_keysetmac(struct ath_hw *ah, u16 entry, const u8 *mac)
1464 u32 macHi, macLo;
1466 if (entry >= ah->caps.keycache_size) {
1467 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
1468 "keychache entry %u out of range\n", entry);
1469 return false;
1472 if (mac != NULL) {
1473 macHi = (mac[5] << 8) | mac[4];
1474 macLo = (mac[3] << 24) |
1475 (mac[2] << 16) |
1476 (mac[1] << 8) |
1477 mac[0];
1478 macLo >>= 1;
1479 macLo |= (macHi & 1) << 31;
1480 macHi >>= 1;
1481 } else {
1482 macLo = macHi = 0;
1484 REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
1485 REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | AR_KEYTABLE_VALID);
1487 return true;
1489 EXPORT_SYMBOL(ath9k_hw_keysetmac);
1491 bool ath9k_hw_set_keycache_entry(struct ath_hw *ah, u16 entry,
1492 const struct ath9k_keyval *k,
1493 const u8 *mac)
1495 const struct ath9k_hw_capabilities *pCap = &ah->caps;
1496 struct ath_common *common = ath9k_hw_common(ah);
1497 u32 key0, key1, key2, key3, key4;
1498 u32 keyType;
1500 if (entry >= pCap->keycache_size) {
1501 ath_print(common, ATH_DBG_FATAL,
1502 "keycache entry %u out of range\n", entry);
1503 return false;
1506 switch (k->kv_type) {
1507 case ATH9K_CIPHER_AES_OCB:
1508 keyType = AR_KEYTABLE_TYPE_AES;
1509 break;
1510 case ATH9K_CIPHER_AES_CCM:
1511 if (!(pCap->hw_caps & ATH9K_HW_CAP_CIPHER_AESCCM)) {
1512 ath_print(common, ATH_DBG_ANY,
1513 "AES-CCM not supported by mac rev 0x%x\n",
1514 ah->hw_version.macRev);
1515 return false;
1517 keyType = AR_KEYTABLE_TYPE_CCM;
1518 break;
1519 case ATH9K_CIPHER_TKIP:
1520 keyType = AR_KEYTABLE_TYPE_TKIP;
1521 if (ATH9K_IS_MIC_ENABLED(ah)
1522 && entry + 64 >= pCap->keycache_size) {
1523 ath_print(common, ATH_DBG_ANY,
1524 "entry %u inappropriate for TKIP\n", entry);
1525 return false;
1527 break;
1528 case ATH9K_CIPHER_WEP:
1529 if (k->kv_len < WLAN_KEY_LEN_WEP40) {
1530 ath_print(common, ATH_DBG_ANY,
1531 "WEP key length %u too small\n", k->kv_len);
1532 return false;
1534 if (k->kv_len <= WLAN_KEY_LEN_WEP40)
1535 keyType = AR_KEYTABLE_TYPE_40;
1536 else if (k->kv_len <= WLAN_KEY_LEN_WEP104)
1537 keyType = AR_KEYTABLE_TYPE_104;
1538 else
1539 keyType = AR_KEYTABLE_TYPE_128;
1540 break;
1541 case ATH9K_CIPHER_CLR:
1542 keyType = AR_KEYTABLE_TYPE_CLR;
1543 break;
1544 default:
1545 ath_print(common, ATH_DBG_FATAL,
1546 "cipher %u not supported\n", k->kv_type);
1547 return false;
1550 key0 = get_unaligned_le32(k->kv_val + 0);
1551 key1 = get_unaligned_le16(k->kv_val + 4);
1552 key2 = get_unaligned_le32(k->kv_val + 6);
1553 key3 = get_unaligned_le16(k->kv_val + 10);
1554 key4 = get_unaligned_le32(k->kv_val + 12);
1555 if (k->kv_len <= WLAN_KEY_LEN_WEP104)
1556 key4 &= 0xff;
1559 * Note: Key cache registers access special memory area that requires
1560 * two 32-bit writes to actually update the values in the internal
1561 * memory. Consequently, the exact order and pairs used here must be
1562 * maintained.
1565 if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
1566 u16 micentry = entry + 64;
1569 * Write inverted key[47:0] first to avoid Michael MIC errors
1570 * on frames that could be sent or received at the same time.
1571 * The correct key will be written in the end once everything
1572 * else is ready.
1574 REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
1575 REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
1577 /* Write key[95:48] */
1578 REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
1579 REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
1581 /* Write key[127:96] and key type */
1582 REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
1583 REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
1585 /* Write MAC address for the entry */
1586 (void) ath9k_hw_keysetmac(ah, entry, mac);
1588 if (ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA) {
1590 * TKIP uses two key cache entries:
1591 * Michael MIC TX/RX keys in the same key cache entry
1592 * (idx = main index + 64):
1593 * key0 [31:0] = RX key [31:0]
1594 * key1 [15:0] = TX key [31:16]
1595 * key1 [31:16] = reserved
1596 * key2 [31:0] = RX key [63:32]
1597 * key3 [15:0] = TX key [15:0]
1598 * key3 [31:16] = reserved
1599 * key4 [31:0] = TX key [63:32]
1601 u32 mic0, mic1, mic2, mic3, mic4;
1603 mic0 = get_unaligned_le32(k->kv_mic + 0);
1604 mic2 = get_unaligned_le32(k->kv_mic + 4);
1605 mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
1606 mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
1607 mic4 = get_unaligned_le32(k->kv_txmic + 4);
1609 /* Write RX[31:0] and TX[31:16] */
1610 REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
1611 REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
1613 /* Write RX[63:32] and TX[15:0] */
1614 REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
1615 REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
1617 /* Write TX[63:32] and keyType(reserved) */
1618 REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
1619 REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
1620 AR_KEYTABLE_TYPE_CLR);
1622 } else {
1624 * TKIP uses four key cache entries (two for group
1625 * keys):
1626 * Michael MIC TX/RX keys are in different key cache
1627 * entries (idx = main index + 64 for TX and
1628 * main index + 32 + 96 for RX):
1629 * key0 [31:0] = TX/RX MIC key [31:0]
1630 * key1 [31:0] = reserved
1631 * key2 [31:0] = TX/RX MIC key [63:32]
1632 * key3 [31:0] = reserved
1633 * key4 [31:0] = reserved
1635 * Upper layer code will call this function separately
1636 * for TX and RX keys when these registers offsets are
1637 * used.
1639 u32 mic0, mic2;
1641 mic0 = get_unaligned_le32(k->kv_mic + 0);
1642 mic2 = get_unaligned_le32(k->kv_mic + 4);
1644 /* Write MIC key[31:0] */
1645 REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
1646 REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
1648 /* Write MIC key[63:32] */
1649 REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
1650 REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
1652 /* Write TX[63:32] and keyType(reserved) */
1653 REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
1654 REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
1655 AR_KEYTABLE_TYPE_CLR);
1658 /* MAC address registers are reserved for the MIC entry */
1659 REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
1660 REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
1663 * Write the correct (un-inverted) key[47:0] last to enable
1664 * TKIP now that all other registers are set with correct
1665 * values.
1667 REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
1668 REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
1669 } else {
1670 /* Write key[47:0] */
1671 REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
1672 REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
1674 /* Write key[95:48] */
1675 REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
1676 REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
1678 /* Write key[127:96] and key type */
1679 REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
1680 REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
1682 /* Write MAC address for the entry */
1683 (void) ath9k_hw_keysetmac(ah, entry, mac);
1686 return true;
1688 EXPORT_SYMBOL(ath9k_hw_set_keycache_entry);
1690 bool ath9k_hw_keyisvalid(struct ath_hw *ah, u16 entry)
1692 if (entry < ah->caps.keycache_size) {
1693 u32 val = REG_READ(ah, AR_KEYTABLE_MAC1(entry));
1694 if (val & AR_KEYTABLE_VALID)
1695 return true;
1697 return false;
1699 EXPORT_SYMBOL(ath9k_hw_keyisvalid);
1701 /******************************/
1702 /* Power Management (Chipset) */
1703 /******************************/
1706 * Notify Power Mgt is disabled in self-generated frames.
1707 * If requested, force chip to sleep.
1709 static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
1711 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1712 if (setChip) {
1714 * Clear the RTC force wake bit to allow the
1715 * mac to go to sleep.
1717 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1718 AR_RTC_FORCE_WAKE_EN);
1719 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1720 REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
1722 /* Shutdown chip. Active low */
1723 if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah))
1724 REG_CLR_BIT(ah, (AR_RTC_RESET),
1725 AR_RTC_RESET_EN);
1730 * Notify Power Management is enabled in self-generating
1731 * frames. If request, set power mode of chip to
1732 * auto/normal. Duration in units of 128us (1/8 TU).
1734 static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
1736 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1737 if (setChip) {
1738 struct ath9k_hw_capabilities *pCap = &ah->caps;
1740 if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1741 /* Set WakeOnInterrupt bit; clear ForceWake bit */
1742 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1743 AR_RTC_FORCE_WAKE_ON_INT);
1744 } else {
1746 * Clear the RTC force wake bit to allow the
1747 * mac to go to sleep.
1749 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1750 AR_RTC_FORCE_WAKE_EN);
1755 static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
1757 u32 val;
1758 int i;
1760 if (setChip) {
1761 if ((REG_READ(ah, AR_RTC_STATUS) &
1762 AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
1763 if (ath9k_hw_set_reset_reg(ah,
1764 ATH9K_RESET_POWER_ON) != true) {
1765 return false;
1767 if (!AR_SREV_9300_20_OR_LATER(ah))
1768 ath9k_hw_init_pll(ah, NULL);
1770 if (AR_SREV_9100(ah))
1771 REG_SET_BIT(ah, AR_RTC_RESET,
1772 AR_RTC_RESET_EN);
1774 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1775 AR_RTC_FORCE_WAKE_EN);
1776 udelay(50);
1778 for (i = POWER_UP_TIME / 50; i > 0; i--) {
1779 val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
1780 if (val == AR_RTC_STATUS_ON)
1781 break;
1782 udelay(50);
1783 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1784 AR_RTC_FORCE_WAKE_EN);
1786 if (i == 0) {
1787 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
1788 "Failed to wakeup in %uus\n",
1789 POWER_UP_TIME / 20);
1790 return false;
1794 REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1796 return true;
1799 bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
1801 struct ath_common *common = ath9k_hw_common(ah);
1802 int status = true, setChip = true;
1803 static const char *modes[] = {
1804 "AWAKE",
1805 "FULL-SLEEP",
1806 "NETWORK SLEEP",
1807 "UNDEFINED"
1810 if (ah->power_mode == mode)
1811 return status;
1813 ath_print(common, ATH_DBG_RESET, "%s -> %s\n",
1814 modes[ah->power_mode], modes[mode]);
1816 switch (mode) {
1817 case ATH9K_PM_AWAKE:
1818 status = ath9k_hw_set_power_awake(ah, setChip);
1819 break;
1820 case ATH9K_PM_FULL_SLEEP:
1821 ath9k_set_power_sleep(ah, setChip);
1822 ah->chip_fullsleep = true;
1823 break;
1824 case ATH9K_PM_NETWORK_SLEEP:
1825 ath9k_set_power_network_sleep(ah, setChip);
1826 break;
1827 default:
1828 ath_print(common, ATH_DBG_FATAL,
1829 "Unknown power mode %u\n", mode);
1830 return false;
1832 ah->power_mode = mode;
1834 return status;
1836 EXPORT_SYMBOL(ath9k_hw_setpower);
1838 /*******************/
1839 /* Beacon Handling */
1840 /*******************/
1842 void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
1844 int flags = 0;
1846 ah->beacon_interval = beacon_period;
1848 ENABLE_REGWRITE_BUFFER(ah);
1850 switch (ah->opmode) {
1851 case NL80211_IFTYPE_STATION:
1852 case NL80211_IFTYPE_MONITOR:
1853 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
1854 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, 0xffff);
1855 REG_WRITE(ah, AR_NEXT_SWBA, 0x7ffff);
1856 flags |= AR_TBTT_TIMER_EN;
1857 break;
1858 case NL80211_IFTYPE_ADHOC:
1859 case NL80211_IFTYPE_MESH_POINT:
1860 REG_SET_BIT(ah, AR_TXCFG,
1861 AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
1862 REG_WRITE(ah, AR_NEXT_NDP_TIMER,
1863 TU_TO_USEC(next_beacon +
1864 (ah->atim_window ? ah->
1865 atim_window : 1)));
1866 flags |= AR_NDP_TIMER_EN;
1867 case NL80211_IFTYPE_AP:
1868 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
1869 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT,
1870 TU_TO_USEC(next_beacon -
1871 ah->config.
1872 dma_beacon_response_time));
1873 REG_WRITE(ah, AR_NEXT_SWBA,
1874 TU_TO_USEC(next_beacon -
1875 ah->config.
1876 sw_beacon_response_time));
1877 flags |=
1878 AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
1879 break;
1880 default:
1881 ath_print(ath9k_hw_common(ah), ATH_DBG_BEACON,
1882 "%s: unsupported opmode: %d\n",
1883 __func__, ah->opmode);
1884 return;
1885 break;
1888 REG_WRITE(ah, AR_BEACON_PERIOD, TU_TO_USEC(beacon_period));
1889 REG_WRITE(ah, AR_DMA_BEACON_PERIOD, TU_TO_USEC(beacon_period));
1890 REG_WRITE(ah, AR_SWBA_PERIOD, TU_TO_USEC(beacon_period));
1891 REG_WRITE(ah, AR_NDP_PERIOD, TU_TO_USEC(beacon_period));
1893 REGWRITE_BUFFER_FLUSH(ah);
1894 DISABLE_REGWRITE_BUFFER(ah);
1896 beacon_period &= ~ATH9K_BEACON_ENA;
1897 if (beacon_period & ATH9K_BEACON_RESET_TSF) {
1898 ath9k_hw_reset_tsf(ah);
1901 REG_SET_BIT(ah, AR_TIMER_MODE, flags);
1903 EXPORT_SYMBOL(ath9k_hw_beaconinit);
1905 void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
1906 const struct ath9k_beacon_state *bs)
1908 u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
1909 struct ath9k_hw_capabilities *pCap = &ah->caps;
1910 struct ath_common *common = ath9k_hw_common(ah);
1912 ENABLE_REGWRITE_BUFFER(ah);
1914 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
1916 REG_WRITE(ah, AR_BEACON_PERIOD,
1917 TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1918 REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
1919 TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1921 REGWRITE_BUFFER_FLUSH(ah);
1922 DISABLE_REGWRITE_BUFFER(ah);
1924 REG_RMW_FIELD(ah, AR_RSSI_THR,
1925 AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
1927 beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
1929 if (bs->bs_sleepduration > beaconintval)
1930 beaconintval = bs->bs_sleepduration;
1932 dtimperiod = bs->bs_dtimperiod;
1933 if (bs->bs_sleepduration > dtimperiod)
1934 dtimperiod = bs->bs_sleepduration;
1936 if (beaconintval == dtimperiod)
1937 nextTbtt = bs->bs_nextdtim;
1938 else
1939 nextTbtt = bs->bs_nexttbtt;
1941 ath_print(common, ATH_DBG_BEACON, "next DTIM %d\n", bs->bs_nextdtim);
1942 ath_print(common, ATH_DBG_BEACON, "next beacon %d\n", nextTbtt);
1943 ath_print(common, ATH_DBG_BEACON, "beacon period %d\n", beaconintval);
1944 ath_print(common, ATH_DBG_BEACON, "DTIM period %d\n", dtimperiod);
1946 ENABLE_REGWRITE_BUFFER(ah);
1948 REG_WRITE(ah, AR_NEXT_DTIM,
1949 TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
1950 REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
1952 REG_WRITE(ah, AR_SLEEP1,
1953 SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
1954 | AR_SLEEP1_ASSUME_DTIM);
1956 if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
1957 beacontimeout = (BEACON_TIMEOUT_VAL << 3);
1958 else
1959 beacontimeout = MIN_BEACON_TIMEOUT_VAL;
1961 REG_WRITE(ah, AR_SLEEP2,
1962 SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
1964 REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
1965 REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
1967 REGWRITE_BUFFER_FLUSH(ah);
1968 DISABLE_REGWRITE_BUFFER(ah);
1970 REG_SET_BIT(ah, AR_TIMER_MODE,
1971 AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
1972 AR_DTIM_TIMER_EN);
1974 /* TSF Out of Range Threshold */
1975 REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
1977 EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
1979 /*******************/
1980 /* HW Capabilities */
1981 /*******************/
1983 int ath9k_hw_fill_cap_info(struct ath_hw *ah)
1985 struct ath9k_hw_capabilities *pCap = &ah->caps;
1986 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1987 struct ath_common *common = ath9k_hw_common(ah);
1988 struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
1990 u16 capField = 0, eeval;
1992 eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
1993 regulatory->current_rd = eeval;
1995 eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_1);
1996 if (AR_SREV_9285_10_OR_LATER(ah))
1997 eeval |= AR9285_RDEXT_DEFAULT;
1998 regulatory->current_rd_ext = eeval;
2000 capField = ah->eep_ops->get_eeprom(ah, EEP_OP_CAP);
2002 if (ah->opmode != NL80211_IFTYPE_AP &&
2003 ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
2004 if (regulatory->current_rd == 0x64 ||
2005 regulatory->current_rd == 0x65)
2006 regulatory->current_rd += 5;
2007 else if (regulatory->current_rd == 0x41)
2008 regulatory->current_rd = 0x43;
2009 ath_print(common, ATH_DBG_REGULATORY,
2010 "regdomain mapped to 0x%x\n", regulatory->current_rd);
2013 eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
2014 if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
2015 ath_print(common, ATH_DBG_FATAL,
2016 "no band has been marked as supported in EEPROM.\n");
2017 return -EINVAL;
2020 bitmap_zero(pCap->wireless_modes, ATH9K_MODE_MAX);
2022 if (eeval & AR5416_OPFLAGS_11A) {
2023 set_bit(ATH9K_MODE_11A, pCap->wireless_modes);
2024 if (ah->config.ht_enable) {
2025 if (!(eeval & AR5416_OPFLAGS_N_5G_HT20))
2026 set_bit(ATH9K_MODE_11NA_HT20,
2027 pCap->wireless_modes);
2028 if (!(eeval & AR5416_OPFLAGS_N_5G_HT40)) {
2029 set_bit(ATH9K_MODE_11NA_HT40PLUS,
2030 pCap->wireless_modes);
2031 set_bit(ATH9K_MODE_11NA_HT40MINUS,
2032 pCap->wireless_modes);
2037 if (eeval & AR5416_OPFLAGS_11G) {
2038 set_bit(ATH9K_MODE_11G, pCap->wireless_modes);
2039 if (ah->config.ht_enable) {
2040 if (!(eeval & AR5416_OPFLAGS_N_2G_HT20))
2041 set_bit(ATH9K_MODE_11NG_HT20,
2042 pCap->wireless_modes);
2043 if (!(eeval & AR5416_OPFLAGS_N_2G_HT40)) {
2044 set_bit(ATH9K_MODE_11NG_HT40PLUS,
2045 pCap->wireless_modes);
2046 set_bit(ATH9K_MODE_11NG_HT40MINUS,
2047 pCap->wireless_modes);
2052 pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
2054 * For AR9271 we will temporarilly uses the rx chainmax as read from
2055 * the EEPROM.
2057 if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
2058 !(eeval & AR5416_OPFLAGS_11A) &&
2059 !(AR_SREV_9271(ah)))
2060 /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
2061 pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
2062 else
2063 /* Use rx_chainmask from EEPROM. */
2064 pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
2066 if (!(AR_SREV_9280(ah) && (ah->hw_version.macRev == 0)))
2067 ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
2069 pCap->low_2ghz_chan = 2312;
2070 pCap->high_2ghz_chan = 2732;
2072 pCap->low_5ghz_chan = 4920;
2073 pCap->high_5ghz_chan = 6100;
2075 pCap->hw_caps &= ~ATH9K_HW_CAP_CIPHER_CKIP;
2076 pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_TKIP;
2077 pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_AESCCM;
2079 pCap->hw_caps &= ~ATH9K_HW_CAP_MIC_CKIP;
2080 pCap->hw_caps |= ATH9K_HW_CAP_MIC_TKIP;
2081 pCap->hw_caps |= ATH9K_HW_CAP_MIC_AESCCM;
2083 if (ah->config.ht_enable)
2084 pCap->hw_caps |= ATH9K_HW_CAP_HT;
2085 else
2086 pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
2088 pCap->hw_caps |= ATH9K_HW_CAP_GTT;
2089 pCap->hw_caps |= ATH9K_HW_CAP_VEOL;
2090 pCap->hw_caps |= ATH9K_HW_CAP_BSSIDMASK;
2091 pCap->hw_caps &= ~ATH9K_HW_CAP_MCAST_KEYSEARCH;
2093 if (capField & AR_EEPROM_EEPCAP_MAXQCU)
2094 pCap->total_queues =
2095 MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
2096 else
2097 pCap->total_queues = ATH9K_NUM_TX_QUEUES;
2099 if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
2100 pCap->keycache_size =
2101 1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
2102 else
2103 pCap->keycache_size = AR_KEYTABLE_SIZE;
2105 pCap->hw_caps |= ATH9K_HW_CAP_FASTCC;
2107 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
2108 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD >> 1;
2109 else
2110 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
2112 if (AR_SREV_9271(ah))
2113 pCap->num_gpio_pins = AR9271_NUM_GPIO;
2114 else if (AR_SREV_9285_10_OR_LATER(ah))
2115 pCap->num_gpio_pins = AR9285_NUM_GPIO;
2116 else if (AR_SREV_9280_10_OR_LATER(ah))
2117 pCap->num_gpio_pins = AR928X_NUM_GPIO;
2118 else
2119 pCap->num_gpio_pins = AR_NUM_GPIO;
2121 if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
2122 pCap->hw_caps |= ATH9K_HW_CAP_CST;
2123 pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
2124 } else {
2125 pCap->rts_aggr_limit = (8 * 1024);
2128 pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
2130 #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
2131 ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
2132 if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
2133 ah->rfkill_gpio =
2134 MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
2135 ah->rfkill_polarity =
2136 MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
2138 pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
2140 #endif
2141 if (AR_SREV_9271(ah))
2142 pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
2143 else
2144 pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
2146 if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
2147 pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
2148 else
2149 pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
2151 if (regulatory->current_rd_ext & (1 << REG_EXT_JAPAN_MIDBAND)) {
2152 pCap->reg_cap =
2153 AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
2154 AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
2155 AR_EEPROM_EEREGCAP_EN_KK_U2 |
2156 AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
2157 } else {
2158 pCap->reg_cap =
2159 AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
2160 AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
2163 /* Advertise midband for AR5416 with FCC midband set in eeprom */
2164 if (regulatory->current_rd_ext & (1 << REG_EXT_FCC_MIDBAND) &&
2165 AR_SREV_5416(ah))
2166 pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
2168 pCap->num_antcfg_5ghz =
2169 ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_5GHZ);
2170 pCap->num_antcfg_2ghz =
2171 ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_2GHZ);
2173 if (AR_SREV_9280_10_OR_LATER(ah) &&
2174 ath9k_hw_btcoex_supported(ah)) {
2175 btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO;
2176 btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO;
2178 if (AR_SREV_9285(ah)) {
2179 btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
2180 btcoex_hw->btpriority_gpio = ATH_BTPRIORITY_GPIO;
2181 } else {
2182 btcoex_hw->scheme = ATH_BTCOEX_CFG_2WIRE;
2184 } else {
2185 btcoex_hw->scheme = ATH_BTCOEX_CFG_NONE;
2188 if (AR_SREV_9300_20_OR_LATER(ah)) {
2189 pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_LDPC;
2190 pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
2191 pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
2192 pCap->rx_status_len = sizeof(struct ar9003_rxs);
2193 pCap->tx_desc_len = sizeof(struct ar9003_txc);
2194 pCap->txs_len = sizeof(struct ar9003_txs);
2195 } else {
2196 pCap->tx_desc_len = sizeof(struct ath_desc);
2199 if (AR_SREV_9300_20_OR_LATER(ah))
2200 pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
2202 return 0;
2205 bool ath9k_hw_getcapability(struct ath_hw *ah, enum ath9k_capability_type type,
2206 u32 capability, u32 *result)
2208 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
2209 switch (type) {
2210 case ATH9K_CAP_CIPHER:
2211 switch (capability) {
2212 case ATH9K_CIPHER_AES_CCM:
2213 case ATH9K_CIPHER_AES_OCB:
2214 case ATH9K_CIPHER_TKIP:
2215 case ATH9K_CIPHER_WEP:
2216 case ATH9K_CIPHER_MIC:
2217 case ATH9K_CIPHER_CLR:
2218 return true;
2219 default:
2220 return false;
2222 case ATH9K_CAP_TKIP_MIC:
2223 switch (capability) {
2224 case 0:
2225 return true;
2226 case 1:
2227 return (ah->sta_id1_defaults &
2228 AR_STA_ID1_CRPT_MIC_ENABLE) ? true :
2229 false;
2231 case ATH9K_CAP_TKIP_SPLIT:
2232 return (ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA) ?
2233 false : true;
2234 case ATH9K_CAP_MCAST_KEYSRCH:
2235 switch (capability) {
2236 case 0:
2237 return true;
2238 case 1:
2239 if (REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_ADHOC) {
2240 return false;
2241 } else {
2242 return (ah->sta_id1_defaults &
2243 AR_STA_ID1_MCAST_KSRCH) ? true :
2244 false;
2247 return false;
2248 case ATH9K_CAP_TXPOW:
2249 switch (capability) {
2250 case 0:
2251 return 0;
2252 case 1:
2253 *result = regulatory->power_limit;
2254 return 0;
2255 case 2:
2256 *result = regulatory->max_power_level;
2257 return 0;
2258 case 3:
2259 *result = regulatory->tp_scale;
2260 return 0;
2262 return false;
2263 case ATH9K_CAP_DS:
2264 return (AR_SREV_9280_20_OR_LATER(ah) &&
2265 (ah->eep_ops->get_eeprom(ah, EEP_RC_CHAIN_MASK) == 1))
2266 ? false : true;
2267 default:
2268 return false;
2271 EXPORT_SYMBOL(ath9k_hw_getcapability);
2273 bool ath9k_hw_setcapability(struct ath_hw *ah, enum ath9k_capability_type type,
2274 u32 capability, u32 setting, int *status)
2276 switch (type) {
2277 case ATH9K_CAP_TKIP_MIC:
2278 if (setting)
2279 ah->sta_id1_defaults |=
2280 AR_STA_ID1_CRPT_MIC_ENABLE;
2281 else
2282 ah->sta_id1_defaults &=
2283 ~AR_STA_ID1_CRPT_MIC_ENABLE;
2284 return true;
2285 case ATH9K_CAP_MCAST_KEYSRCH:
2286 if (setting)
2287 ah->sta_id1_defaults |= AR_STA_ID1_MCAST_KSRCH;
2288 else
2289 ah->sta_id1_defaults &= ~AR_STA_ID1_MCAST_KSRCH;
2290 return true;
2291 default:
2292 return false;
2295 EXPORT_SYMBOL(ath9k_hw_setcapability);
2297 /****************************/
2298 /* GPIO / RFKILL / Antennae */
2299 /****************************/
2301 static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
2302 u32 gpio, u32 type)
2304 int addr;
2305 u32 gpio_shift, tmp;
2307 if (gpio > 11)
2308 addr = AR_GPIO_OUTPUT_MUX3;
2309 else if (gpio > 5)
2310 addr = AR_GPIO_OUTPUT_MUX2;
2311 else
2312 addr = AR_GPIO_OUTPUT_MUX1;
2314 gpio_shift = (gpio % 6) * 5;
2316 if (AR_SREV_9280_20_OR_LATER(ah)
2317 || (addr != AR_GPIO_OUTPUT_MUX1)) {
2318 REG_RMW(ah, addr, (type << gpio_shift),
2319 (0x1f << gpio_shift));
2320 } else {
2321 tmp = REG_READ(ah, addr);
2322 tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
2323 tmp &= ~(0x1f << gpio_shift);
2324 tmp |= (type << gpio_shift);
2325 REG_WRITE(ah, addr, tmp);
2329 void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
2331 u32 gpio_shift;
2333 BUG_ON(gpio >= ah->caps.num_gpio_pins);
2335 gpio_shift = gpio << 1;
2337 REG_RMW(ah,
2338 AR_GPIO_OE_OUT,
2339 (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
2340 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2342 EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
2344 u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
2346 #define MS_REG_READ(x, y) \
2347 (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
2349 if (gpio >= ah->caps.num_gpio_pins)
2350 return 0xffffffff;
2352 if (AR_SREV_9300_20_OR_LATER(ah))
2353 return MS_REG_READ(AR9300, gpio) != 0;
2354 else if (AR_SREV_9271(ah))
2355 return MS_REG_READ(AR9271, gpio) != 0;
2356 else if (AR_SREV_9287_10_OR_LATER(ah))
2357 return MS_REG_READ(AR9287, gpio) != 0;
2358 else if (AR_SREV_9285_10_OR_LATER(ah))
2359 return MS_REG_READ(AR9285, gpio) != 0;
2360 else if (AR_SREV_9280_10_OR_LATER(ah))
2361 return MS_REG_READ(AR928X, gpio) != 0;
2362 else
2363 return MS_REG_READ(AR, gpio) != 0;
2365 EXPORT_SYMBOL(ath9k_hw_gpio_get);
2367 void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
2368 u32 ah_signal_type)
2370 u32 gpio_shift;
2372 ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
2374 gpio_shift = 2 * gpio;
2376 REG_RMW(ah,
2377 AR_GPIO_OE_OUT,
2378 (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
2379 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2381 EXPORT_SYMBOL(ath9k_hw_cfg_output);
2383 void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
2385 if (AR_SREV_9271(ah))
2386 val = ~val;
2388 REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
2389 AR_GPIO_BIT(gpio));
2391 EXPORT_SYMBOL(ath9k_hw_set_gpio);
2393 u32 ath9k_hw_getdefantenna(struct ath_hw *ah)
2395 return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
2397 EXPORT_SYMBOL(ath9k_hw_getdefantenna);
2399 void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
2401 REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
2403 EXPORT_SYMBOL(ath9k_hw_setantenna);
2405 /*********************/
2406 /* General Operation */
2407 /*********************/
2409 u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
2411 u32 bits = REG_READ(ah, AR_RX_FILTER);
2412 u32 phybits = REG_READ(ah, AR_PHY_ERR);
2414 if (phybits & AR_PHY_ERR_RADAR)
2415 bits |= ATH9K_RX_FILTER_PHYRADAR;
2416 if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
2417 bits |= ATH9K_RX_FILTER_PHYERR;
2419 return bits;
2421 EXPORT_SYMBOL(ath9k_hw_getrxfilter);
2423 void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
2425 u32 phybits;
2427 ENABLE_REGWRITE_BUFFER(ah);
2429 REG_WRITE(ah, AR_RX_FILTER, bits);
2431 phybits = 0;
2432 if (bits & ATH9K_RX_FILTER_PHYRADAR)
2433 phybits |= AR_PHY_ERR_RADAR;
2434 if (bits & ATH9K_RX_FILTER_PHYERR)
2435 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
2436 REG_WRITE(ah, AR_PHY_ERR, phybits);
2438 if (phybits)
2439 REG_WRITE(ah, AR_RXCFG,
2440 REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
2441 else
2442 REG_WRITE(ah, AR_RXCFG,
2443 REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_ZLFDMA);
2445 REGWRITE_BUFFER_FLUSH(ah);
2446 DISABLE_REGWRITE_BUFFER(ah);
2448 EXPORT_SYMBOL(ath9k_hw_setrxfilter);
2450 bool ath9k_hw_phy_disable(struct ath_hw *ah)
2452 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
2453 return false;
2455 ath9k_hw_init_pll(ah, NULL);
2456 return true;
2458 EXPORT_SYMBOL(ath9k_hw_phy_disable);
2460 bool ath9k_hw_disable(struct ath_hw *ah)
2462 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
2463 return false;
2465 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
2466 return false;
2468 ath9k_hw_init_pll(ah, NULL);
2469 return true;
2471 EXPORT_SYMBOL(ath9k_hw_disable);
2473 void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit)
2475 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
2476 struct ath9k_channel *chan = ah->curchan;
2477 struct ieee80211_channel *channel = chan->chan;
2479 regulatory->power_limit = min(limit, (u32) MAX_RATE_POWER);
2481 ah->eep_ops->set_txpower(ah, chan,
2482 ath9k_regd_get_ctl(regulatory, chan),
2483 channel->max_antenna_gain * 2,
2484 channel->max_power * 2,
2485 min((u32) MAX_RATE_POWER,
2486 (u32) regulatory->power_limit));
2488 EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
2490 void ath9k_hw_setmac(struct ath_hw *ah, const u8 *mac)
2492 memcpy(ath9k_hw_common(ah)->macaddr, mac, ETH_ALEN);
2494 EXPORT_SYMBOL(ath9k_hw_setmac);
2496 void ath9k_hw_setopmode(struct ath_hw *ah)
2498 ath9k_hw_set_operating_mode(ah, ah->opmode);
2500 EXPORT_SYMBOL(ath9k_hw_setopmode);
2502 void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
2504 REG_WRITE(ah, AR_MCAST_FIL0, filter0);
2505 REG_WRITE(ah, AR_MCAST_FIL1, filter1);
2507 EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
2509 void ath9k_hw_write_associd(struct ath_hw *ah)
2511 struct ath_common *common = ath9k_hw_common(ah);
2513 REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
2514 REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
2515 ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
2517 EXPORT_SYMBOL(ath9k_hw_write_associd);
2519 #define ATH9K_MAX_TSF_READ 10
2521 u64 ath9k_hw_gettsf64(struct ath_hw *ah)
2523 u32 tsf_lower, tsf_upper1, tsf_upper2;
2524 int i;
2526 tsf_upper1 = REG_READ(ah, AR_TSF_U32);
2527 for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
2528 tsf_lower = REG_READ(ah, AR_TSF_L32);
2529 tsf_upper2 = REG_READ(ah, AR_TSF_U32);
2530 if (tsf_upper2 == tsf_upper1)
2531 break;
2532 tsf_upper1 = tsf_upper2;
2535 WARN_ON( i == ATH9K_MAX_TSF_READ );
2537 return (((u64)tsf_upper1 << 32) | tsf_lower);
2539 EXPORT_SYMBOL(ath9k_hw_gettsf64);
2541 void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
2543 REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
2544 REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
2546 EXPORT_SYMBOL(ath9k_hw_settsf64);
2548 void ath9k_hw_reset_tsf(struct ath_hw *ah)
2550 if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
2551 AH_TSF_WRITE_TIMEOUT))
2552 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
2553 "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
2555 REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
2557 EXPORT_SYMBOL(ath9k_hw_reset_tsf);
2559 void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
2561 if (setting)
2562 ah->misc_mode |= AR_PCU_TX_ADD_TSF;
2563 else
2564 ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
2566 EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
2569 * Extend 15-bit time stamp from rx descriptor to
2570 * a full 64-bit TSF using the current h/w TSF.
2572 u64 ath9k_hw_extend_tsf(struct ath_hw *ah, u32 rstamp)
2574 u64 tsf;
2576 tsf = ath9k_hw_gettsf64(ah);
2577 if ((tsf & 0x7fff) < rstamp)
2578 tsf -= 0x8000;
2579 return (tsf & ~0x7fff) | rstamp;
2581 EXPORT_SYMBOL(ath9k_hw_extend_tsf);
2583 void ath9k_hw_set11nmac2040(struct ath_hw *ah)
2585 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
2586 u32 macmode;
2588 if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
2589 macmode = AR_2040_JOINED_RX_CLEAR;
2590 else
2591 macmode = 0;
2593 REG_WRITE(ah, AR_2040_MODE, macmode);
2596 /* HW Generic timers configuration */
2598 static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
2600 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2601 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2602 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2603 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2604 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2605 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2606 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2607 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2608 {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
2609 {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
2610 AR_NDP2_TIMER_MODE, 0x0002},
2611 {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
2612 AR_NDP2_TIMER_MODE, 0x0004},
2613 {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
2614 AR_NDP2_TIMER_MODE, 0x0008},
2615 {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
2616 AR_NDP2_TIMER_MODE, 0x0010},
2617 {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
2618 AR_NDP2_TIMER_MODE, 0x0020},
2619 {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
2620 AR_NDP2_TIMER_MODE, 0x0040},
2621 {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
2622 AR_NDP2_TIMER_MODE, 0x0080}
2625 /* HW generic timer primitives */
2627 /* compute and clear index of rightmost 1 */
2628 static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
2630 u32 b;
2632 b = *mask;
2633 b &= (0-b);
2634 *mask &= ~b;
2635 b *= debruijn32;
2636 b >>= 27;
2638 return timer_table->gen_timer_index[b];
2641 u32 ath9k_hw_gettsf32(struct ath_hw *ah)
2643 return REG_READ(ah, AR_TSF_L32);
2645 EXPORT_SYMBOL(ath9k_hw_gettsf32);
2647 struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
2648 void (*trigger)(void *),
2649 void (*overflow)(void *),
2650 void *arg,
2651 u8 timer_index)
2653 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2654 struct ath_gen_timer *timer;
2656 timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
2658 if (timer == NULL) {
2659 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
2660 "Failed to allocate memory"
2661 "for hw timer[%d]\n", timer_index);
2662 return NULL;
2665 /* allocate a hardware generic timer slot */
2666 timer_table->timers[timer_index] = timer;
2667 timer->index = timer_index;
2668 timer->trigger = trigger;
2669 timer->overflow = overflow;
2670 timer->arg = arg;
2672 return timer;
2674 EXPORT_SYMBOL(ath_gen_timer_alloc);
2676 void ath9k_hw_gen_timer_start(struct ath_hw *ah,
2677 struct ath_gen_timer *timer,
2678 u32 timer_next,
2679 u32 timer_period)
2681 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2682 u32 tsf;
2684 BUG_ON(!timer_period);
2686 set_bit(timer->index, &timer_table->timer_mask.timer_bits);
2688 tsf = ath9k_hw_gettsf32(ah);
2690 ath_print(ath9k_hw_common(ah), ATH_DBG_HWTIMER,
2691 "curent tsf %x period %x"
2692 "timer_next %x\n", tsf, timer_period, timer_next);
2695 * Pull timer_next forward if the current TSF already passed it
2696 * because of software latency
2698 if (timer_next < tsf)
2699 timer_next = tsf + timer_period;
2702 * Program generic timer registers
2704 REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
2705 timer_next);
2706 REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
2707 timer_period);
2708 REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2709 gen_tmr_configuration[timer->index].mode_mask);
2711 /* Enable both trigger and thresh interrupt masks */
2712 REG_SET_BIT(ah, AR_IMR_S5,
2713 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2714 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2716 EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
2718 void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
2720 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2722 if ((timer->index < AR_FIRST_NDP_TIMER) ||
2723 (timer->index >= ATH_MAX_GEN_TIMER)) {
2724 return;
2727 /* Clear generic timer enable bits. */
2728 REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2729 gen_tmr_configuration[timer->index].mode_mask);
2731 /* Disable both trigger and thresh interrupt masks */
2732 REG_CLR_BIT(ah, AR_IMR_S5,
2733 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2734 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2736 clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
2738 EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
2740 void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
2742 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2744 /* free the hardware generic timer slot */
2745 timer_table->timers[timer->index] = NULL;
2746 kfree(timer);
2748 EXPORT_SYMBOL(ath_gen_timer_free);
2751 * Generic Timer Interrupts handling
2753 void ath_gen_timer_isr(struct ath_hw *ah)
2755 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2756 struct ath_gen_timer *timer;
2757 struct ath_common *common = ath9k_hw_common(ah);
2758 u32 trigger_mask, thresh_mask, index;
2760 /* get hardware generic timer interrupt status */
2761 trigger_mask = ah->intr_gen_timer_trigger;
2762 thresh_mask = ah->intr_gen_timer_thresh;
2763 trigger_mask &= timer_table->timer_mask.val;
2764 thresh_mask &= timer_table->timer_mask.val;
2766 trigger_mask &= ~thresh_mask;
2768 while (thresh_mask) {
2769 index = rightmost_index(timer_table, &thresh_mask);
2770 timer = timer_table->timers[index];
2771 BUG_ON(!timer);
2772 ath_print(common, ATH_DBG_HWTIMER,
2773 "TSF overflow for Gen timer %d\n", index);
2774 timer->overflow(timer->arg);
2777 while (trigger_mask) {
2778 index = rightmost_index(timer_table, &trigger_mask);
2779 timer = timer_table->timers[index];
2780 BUG_ON(!timer);
2781 ath_print(common, ATH_DBG_HWTIMER,
2782 "Gen timer[%d] trigger\n", index);
2783 timer->trigger(timer->arg);
2786 EXPORT_SYMBOL(ath_gen_timer_isr);
2788 /********/
2789 /* HTC */
2790 /********/
2792 void ath9k_hw_htc_resetinit(struct ath_hw *ah)
2794 ah->htc_reset_init = true;
2796 EXPORT_SYMBOL(ath9k_hw_htc_resetinit);
2798 static struct {
2799 u32 version;
2800 const char * name;
2801 } ath_mac_bb_names[] = {
2802 /* Devices with external radios */
2803 { AR_SREV_VERSION_5416_PCI, "5416" },
2804 { AR_SREV_VERSION_5416_PCIE, "5418" },
2805 { AR_SREV_VERSION_9100, "9100" },
2806 { AR_SREV_VERSION_9160, "9160" },
2807 /* Single-chip solutions */
2808 { AR_SREV_VERSION_9280, "9280" },
2809 { AR_SREV_VERSION_9285, "9285" },
2810 { AR_SREV_VERSION_9287, "9287" },
2811 { AR_SREV_VERSION_9271, "9271" },
2812 { AR_SREV_VERSION_9300, "9300" },
2815 /* For devices with external radios */
2816 static struct {
2817 u16 version;
2818 const char * name;
2819 } ath_rf_names[] = {
2820 { 0, "5133" },
2821 { AR_RAD5133_SREV_MAJOR, "5133" },
2822 { AR_RAD5122_SREV_MAJOR, "5122" },
2823 { AR_RAD2133_SREV_MAJOR, "2133" },
2824 { AR_RAD2122_SREV_MAJOR, "2122" }
2828 * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
2830 static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
2832 int i;
2834 for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
2835 if (ath_mac_bb_names[i].version == mac_bb_version) {
2836 return ath_mac_bb_names[i].name;
2840 return "????";
2844 * Return the RF name. "????" is returned if the RF is unknown.
2845 * Used for devices with external radios.
2847 static const char *ath9k_hw_rf_name(u16 rf_version)
2849 int i;
2851 for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
2852 if (ath_rf_names[i].version == rf_version) {
2853 return ath_rf_names[i].name;
2857 return "????";
2860 void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
2862 int used;
2864 /* chipsets >= AR9280 are single-chip */
2865 if (AR_SREV_9280_10_OR_LATER(ah)) {
2866 used = snprintf(hw_name, len,
2867 "Atheros AR%s Rev:%x",
2868 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2869 ah->hw_version.macRev);
2871 else {
2872 used = snprintf(hw_name, len,
2873 "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
2874 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2875 ah->hw_version.macRev,
2876 ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
2877 AR_RADIO_SREV_MAJOR)),
2878 ah->hw_version.phyRev);
2881 hw_name[used] = '\0';
2883 EXPORT_SYMBOL(ath9k_hw_name);