]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/ath9k/hw.c
ath9k_hw: clean up ANI state handling
[karo-tx-linux.git] / drivers / net / wireless / ath / ath9k / hw.c
1 /*
2  * Copyright (c) 2008-2010 Atheros Communications Inc.
3  *
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.
7  *
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.
15  */
16
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20
21 #include "hw.h"
22 #include "hw-ops.h"
23 #include "rc.h"
24 #include "ar9003_mac.h"
25
26 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
27
28 MODULE_AUTHOR("Atheros Communications");
29 MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
30 MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
31 MODULE_LICENSE("Dual BSD/GPL");
32
33 static int __init ath9k_init(void)
34 {
35         return 0;
36 }
37 module_init(ath9k_init);
38
39 static void __exit ath9k_exit(void)
40 {
41         return;
42 }
43 module_exit(ath9k_exit);
44
45 /* Private hardware callbacks */
46
47 static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
48 {
49         ath9k_hw_private_ops(ah)->init_cal_settings(ah);
50 }
51
52 static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
53 {
54         ath9k_hw_private_ops(ah)->init_mode_regs(ah);
55 }
56
57 static bool ath9k_hw_macversion_supported(struct ath_hw *ah)
58 {
59         struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
60
61         return priv_ops->macversion_supported(ah->hw_version.macVersion);
62 }
63
64 static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
65                                         struct ath9k_channel *chan)
66 {
67         return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
68 }
69
70 static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
71 {
72         if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
73                 return;
74
75         ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
76 }
77
78 static void ath9k_hw_ani_cache_ini_regs(struct ath_hw *ah)
79 {
80         /* You will not have this callback if using the old ANI */
81         if (!ath9k_hw_private_ops(ah)->ani_cache_ini_regs)
82                 return;
83
84         ath9k_hw_private_ops(ah)->ani_cache_ini_regs(ah);
85 }
86
87 /********************/
88 /* Helper Functions */
89 /********************/
90
91 static u32 ath9k_hw_mac_clks(struct ath_hw *ah, u32 usecs)
92 {
93         struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
94
95         if (!ah->curchan) /* should really check for CCK instead */
96                 return usecs *ATH9K_CLOCK_RATE_CCK;
97         if (conf->channel->band == IEEE80211_BAND_2GHZ)
98                 return usecs *ATH9K_CLOCK_RATE_2GHZ_OFDM;
99
100         if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
101                 return usecs * ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
102         else
103                 return usecs * ATH9K_CLOCK_RATE_5GHZ_OFDM;
104 }
105
106 static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
107 {
108         struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
109
110         if (conf_is_ht40(conf))
111                 return ath9k_hw_mac_clks(ah, usecs) * 2;
112         else
113                 return ath9k_hw_mac_clks(ah, usecs);
114 }
115
116 bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
117 {
118         int i;
119
120         BUG_ON(timeout < AH_TIME_QUANTUM);
121
122         for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
123                 if ((REG_READ(ah, reg) & mask) == val)
124                         return true;
125
126                 udelay(AH_TIME_QUANTUM);
127         }
128
129         ath_print(ath9k_hw_common(ah), ATH_DBG_ANY,
130                   "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
131                   timeout, reg, REG_READ(ah, reg), mask, val);
132
133         return false;
134 }
135 EXPORT_SYMBOL(ath9k_hw_wait);
136
137 u32 ath9k_hw_reverse_bits(u32 val, u32 n)
138 {
139         u32 retval;
140         int i;
141
142         for (i = 0, retval = 0; i < n; i++) {
143                 retval = (retval << 1) | (val & 1);
144                 val >>= 1;
145         }
146         return retval;
147 }
148
149 bool ath9k_get_channel_edges(struct ath_hw *ah,
150                              u16 flags, u16 *low,
151                              u16 *high)
152 {
153         struct ath9k_hw_capabilities *pCap = &ah->caps;
154
155         if (flags & CHANNEL_5GHZ) {
156                 *low = pCap->low_5ghz_chan;
157                 *high = pCap->high_5ghz_chan;
158                 return true;
159         }
160         if ((flags & CHANNEL_2GHZ)) {
161                 *low = pCap->low_2ghz_chan;
162                 *high = pCap->high_2ghz_chan;
163                 return true;
164         }
165         return false;
166 }
167
168 u16 ath9k_hw_computetxtime(struct ath_hw *ah,
169                            u8 phy, int kbps,
170                            u32 frameLen, u16 rateix,
171                            bool shortPreamble)
172 {
173         u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
174
175         if (kbps == 0)
176                 return 0;
177
178         switch (phy) {
179         case WLAN_RC_PHY_CCK:
180                 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
181                 if (shortPreamble)
182                         phyTime >>= 1;
183                 numBits = frameLen << 3;
184                 txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
185                 break;
186         case WLAN_RC_PHY_OFDM:
187                 if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
188                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
189                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
190                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
191                         txTime = OFDM_SIFS_TIME_QUARTER
192                                 + OFDM_PREAMBLE_TIME_QUARTER
193                                 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
194                 } else if (ah->curchan &&
195                            IS_CHAN_HALF_RATE(ah->curchan)) {
196                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
197                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
198                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
199                         txTime = OFDM_SIFS_TIME_HALF +
200                                 OFDM_PREAMBLE_TIME_HALF
201                                 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
202                 } else {
203                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
204                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
205                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
206                         txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
207                                 + (numSymbols * OFDM_SYMBOL_TIME);
208                 }
209                 break;
210         default:
211                 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
212                           "Unknown phy %u (rate ix %u)\n", phy, rateix);
213                 txTime = 0;
214                 break;
215         }
216
217         return txTime;
218 }
219 EXPORT_SYMBOL(ath9k_hw_computetxtime);
220
221 void ath9k_hw_get_channel_centers(struct ath_hw *ah,
222                                   struct ath9k_channel *chan,
223                                   struct chan_centers *centers)
224 {
225         int8_t extoff;
226
227         if (!IS_CHAN_HT40(chan)) {
228                 centers->ctl_center = centers->ext_center =
229                         centers->synth_center = chan->channel;
230                 return;
231         }
232
233         if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
234             (chan->chanmode == CHANNEL_G_HT40PLUS)) {
235                 centers->synth_center =
236                         chan->channel + HT40_CHANNEL_CENTER_SHIFT;
237                 extoff = 1;
238         } else {
239                 centers->synth_center =
240                         chan->channel - HT40_CHANNEL_CENTER_SHIFT;
241                 extoff = -1;
242         }
243
244         centers->ctl_center =
245                 centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
246         /* 25 MHz spacing is supported by hw but not on upper layers */
247         centers->ext_center =
248                 centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
249 }
250
251 /******************/
252 /* Chip Revisions */
253 /******************/
254
255 static void ath9k_hw_read_revisions(struct ath_hw *ah)
256 {
257         u32 val;
258
259         val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
260
261         if (val == 0xFF) {
262                 val = REG_READ(ah, AR_SREV);
263                 ah->hw_version.macVersion =
264                         (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
265                 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
266                 ah->is_pciexpress = (val & AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
267         } else {
268                 if (!AR_SREV_9100(ah))
269                         ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
270
271                 ah->hw_version.macRev = val & AR_SREV_REVISION;
272
273                 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
274                         ah->is_pciexpress = true;
275         }
276 }
277
278 /************************************/
279 /* HW Attach, Detach, Init Routines */
280 /************************************/
281
282 static void ath9k_hw_disablepcie(struct ath_hw *ah)
283 {
284         if (AR_SREV_9100(ah))
285                 return;
286
287         ENABLE_REGWRITE_BUFFER(ah);
288
289         REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
290         REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
291         REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
292         REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
293         REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
294         REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
295         REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
296         REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
297         REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
298
299         REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
300
301         REGWRITE_BUFFER_FLUSH(ah);
302 }
303
304 /* This should work for all families including legacy */
305 static bool ath9k_hw_chip_test(struct ath_hw *ah)
306 {
307         struct ath_common *common = ath9k_hw_common(ah);
308         u32 regAddr[2] = { AR_STA_ID0 };
309         u32 regHold[2];
310         u32 patternData[4] = { 0x55555555,
311                                0xaaaaaaaa,
312                                0x66666666,
313                                0x99999999 };
314         int i, j, loop_max;
315
316         if (!AR_SREV_9300_20_OR_LATER(ah)) {
317                 loop_max = 2;
318                 regAddr[1] = AR_PHY_BASE + (8 << 2);
319         } else
320                 loop_max = 1;
321
322         for (i = 0; i < loop_max; i++) {
323                 u32 addr = regAddr[i];
324                 u32 wrData, rdData;
325
326                 regHold[i] = REG_READ(ah, addr);
327                 for (j = 0; j < 0x100; j++) {
328                         wrData = (j << 16) | j;
329                         REG_WRITE(ah, addr, wrData);
330                         rdData = REG_READ(ah, addr);
331                         if (rdData != wrData) {
332                                 ath_print(common, ATH_DBG_FATAL,
333                                           "address test failed "
334                                           "addr: 0x%08x - wr:0x%08x != "
335                                           "rd:0x%08x\n",
336                                           addr, wrData, rdData);
337                                 return false;
338                         }
339                 }
340                 for (j = 0; j < 4; j++) {
341                         wrData = patternData[j];
342                         REG_WRITE(ah, addr, wrData);
343                         rdData = REG_READ(ah, addr);
344                         if (wrData != rdData) {
345                                 ath_print(common, ATH_DBG_FATAL,
346                                           "address test failed "
347                                           "addr: 0x%08x - wr:0x%08x != "
348                                           "rd:0x%08x\n",
349                                           addr, wrData, rdData);
350                                 return false;
351                         }
352                 }
353                 REG_WRITE(ah, regAddr[i], regHold[i]);
354         }
355         udelay(100);
356
357         return true;
358 }
359
360 static void ath9k_hw_init_config(struct ath_hw *ah)
361 {
362         int i;
363
364         ah->config.dma_beacon_response_time = 2;
365         ah->config.sw_beacon_response_time = 10;
366         ah->config.additional_swba_backoff = 0;
367         ah->config.ack_6mb = 0x0;
368         ah->config.cwm_ignore_extcca = 0;
369         ah->config.pcie_powersave_enable = 0;
370         ah->config.pcie_clock_req = 0;
371         ah->config.pcie_waen = 0;
372         ah->config.analog_shiftreg = 1;
373         ah->config.enable_ani = true;
374
375         for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
376                 ah->config.spurchans[i][0] = AR_NO_SPUR;
377                 ah->config.spurchans[i][1] = AR_NO_SPUR;
378         }
379
380         if (ah->hw_version.devid != AR2427_DEVID_PCIE)
381                 ah->config.ht_enable = 1;
382         else
383                 ah->config.ht_enable = 0;
384
385         ah->config.rx_intr_mitigation = true;
386         ah->config.pcieSerDesWrite = true;
387
388         /*
389          * We need this for PCI devices only (Cardbus, PCI, miniPCI)
390          * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
391          * This means we use it for all AR5416 devices, and the few
392          * minor PCI AR9280 devices out there.
393          *
394          * Serialization is required because these devices do not handle
395          * well the case of two concurrent reads/writes due to the latency
396          * involved. During one read/write another read/write can be issued
397          * on another CPU while the previous read/write may still be working
398          * on our hardware, if we hit this case the hardware poops in a loop.
399          * We prevent this by serializing reads and writes.
400          *
401          * This issue is not present on PCI-Express devices or pre-AR5416
402          * devices (legacy, 802.11abg).
403          */
404         if (num_possible_cpus() > 1)
405                 ah->config.serialize_regmode = SER_REG_MODE_AUTO;
406 }
407
408 static void ath9k_hw_init_defaults(struct ath_hw *ah)
409 {
410         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
411
412         regulatory->country_code = CTRY_DEFAULT;
413         regulatory->power_limit = MAX_RATE_POWER;
414         regulatory->tp_scale = ATH9K_TP_SCALE_MAX;
415
416         ah->hw_version.magic = AR5416_MAGIC;
417         ah->hw_version.subvendorid = 0;
418
419         ah->ah_flags = 0;
420         if (!AR_SREV_9100(ah))
421                 ah->ah_flags = AH_USE_EEPROM;
422
423         ah->atim_window = 0;
424         ah->sta_id1_defaults =
425                 AR_STA_ID1_CRPT_MIC_ENABLE |
426                 AR_STA_ID1_MCAST_KSRCH;
427         ah->beacon_interval = 100;
428         ah->enable_32kHz_clock = DONT_USE_32KHZ;
429         ah->slottime = (u32) -1;
430         ah->globaltxtimeout = (u32) -1;
431         ah->power_mode = ATH9K_PM_UNDEFINED;
432 }
433
434 static int ath9k_hw_init_macaddr(struct ath_hw *ah)
435 {
436         struct ath_common *common = ath9k_hw_common(ah);
437         u32 sum;
438         int i;
439         u16 eeval;
440         u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
441
442         sum = 0;
443         for (i = 0; i < 3; i++) {
444                 eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
445                 sum += eeval;
446                 common->macaddr[2 * i] = eeval >> 8;
447                 common->macaddr[2 * i + 1] = eeval & 0xff;
448         }
449         if (sum == 0 || sum == 0xffff * 3)
450                 return -EADDRNOTAVAIL;
451
452         return 0;
453 }
454
455 static int ath9k_hw_post_init(struct ath_hw *ah)
456 {
457         int ecode;
458
459         if (!AR_SREV_9271(ah)) {
460                 if (!ath9k_hw_chip_test(ah))
461                         return -ENODEV;
462         }
463
464         if (!AR_SREV_9300_20_OR_LATER(ah)) {
465                 ecode = ar9002_hw_rf_claim(ah);
466                 if (ecode != 0)
467                         return ecode;
468         }
469
470         ecode = ath9k_hw_eeprom_init(ah);
471         if (ecode != 0)
472                 return ecode;
473
474         ath_print(ath9k_hw_common(ah), ATH_DBG_CONFIG,
475                   "Eeprom VER: %d, REV: %d\n",
476                   ah->eep_ops->get_eeprom_ver(ah),
477                   ah->eep_ops->get_eeprom_rev(ah));
478
479         ecode = ath9k_hw_rf_alloc_ext_banks(ah);
480         if (ecode) {
481                 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
482                           "Failed allocating banks for "
483                           "external radio\n");
484                 return ecode;
485         }
486
487         if (!AR_SREV_9100(ah)) {
488                 ath9k_hw_ani_setup(ah);
489                 ath9k_hw_ani_init(ah);
490         }
491
492         return 0;
493 }
494
495 static void ath9k_hw_attach_ops(struct ath_hw *ah)
496 {
497         if (AR_SREV_9300_20_OR_LATER(ah))
498                 ar9003_hw_attach_ops(ah);
499         else
500                 ar9002_hw_attach_ops(ah);
501 }
502
503 /* Called for all hardware families */
504 static int __ath9k_hw_init(struct ath_hw *ah)
505 {
506         struct ath_common *common = ath9k_hw_common(ah);
507         int r = 0;
508
509         if (ah->hw_version.devid == AR5416_AR9100_DEVID)
510                 ah->hw_version.macVersion = AR_SREV_VERSION_9100;
511
512         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
513                 ath_print(common, ATH_DBG_FATAL,
514                           "Couldn't reset chip\n");
515                 return -EIO;
516         }
517
518         ath9k_hw_init_defaults(ah);
519         ath9k_hw_init_config(ah);
520
521         ath9k_hw_attach_ops(ah);
522
523         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
524                 ath_print(common, ATH_DBG_FATAL, "Couldn't wakeup chip\n");
525                 return -EIO;
526         }
527
528         if (ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
529                 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
530                     ((AR_SREV_9160(ah) || AR_SREV_9280(ah)) &&
531                      !ah->is_pciexpress)) {
532                         ah->config.serialize_regmode =
533                                 SER_REG_MODE_ON;
534                 } else {
535                         ah->config.serialize_regmode =
536                                 SER_REG_MODE_OFF;
537                 }
538         }
539
540         ath_print(common, ATH_DBG_RESET, "serialize_regmode is %d\n",
541                 ah->config.serialize_regmode);
542
543         if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
544                 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
545         else
546                 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
547
548         if (!ath9k_hw_macversion_supported(ah)) {
549                 ath_print(common, ATH_DBG_FATAL,
550                           "Mac Chip Rev 0x%02x.%x is not supported by "
551                           "this driver\n", ah->hw_version.macVersion,
552                           ah->hw_version.macRev);
553                 return -EOPNOTSUPP;
554         }
555
556         if (AR_SREV_9271(ah) || AR_SREV_9100(ah))
557                 ah->is_pciexpress = false;
558
559         ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
560         ath9k_hw_init_cal_settings(ah);
561
562         ah->ani_function = ATH9K_ANI_ALL;
563         if (AR_SREV_9280_20_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
564                 ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
565         if (!AR_SREV_9300_20_OR_LATER(ah))
566                 ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
567
568         ath9k_hw_init_mode_regs(ah);
569
570         /*
571          * Read back AR_WA into a permanent copy and set bits 14 and 17.
572          * We need to do this to avoid RMW of this register. We cannot
573          * read the reg when chip is asleep.
574          */
575         ah->WARegVal = REG_READ(ah, AR_WA);
576         ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
577                          AR_WA_ASPM_TIMER_BASED_DISABLE);
578
579         if (ah->is_pciexpress)
580                 ath9k_hw_configpcipowersave(ah, 0, 0);
581         else
582                 ath9k_hw_disablepcie(ah);
583
584         if (!AR_SREV_9300_20_OR_LATER(ah))
585                 ar9002_hw_cck_chan14_spread(ah);
586
587         r = ath9k_hw_post_init(ah);
588         if (r)
589                 return r;
590
591         ath9k_hw_init_mode_gain_regs(ah);
592         r = ath9k_hw_fill_cap_info(ah);
593         if (r)
594                 return r;
595
596         r = ath9k_hw_init_macaddr(ah);
597         if (r) {
598                 ath_print(common, ATH_DBG_FATAL,
599                           "Failed to initialize MAC address\n");
600                 return r;
601         }
602
603         if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
604                 ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
605         else
606                 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
607
608         ah->bb_watchdog_timeout_ms = 25;
609
610         common->state = ATH_HW_INITIALIZED;
611
612         return 0;
613 }
614
615 int ath9k_hw_init(struct ath_hw *ah)
616 {
617         int ret;
618         struct ath_common *common = ath9k_hw_common(ah);
619
620         /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
621         switch (ah->hw_version.devid) {
622         case AR5416_DEVID_PCI:
623         case AR5416_DEVID_PCIE:
624         case AR5416_AR9100_DEVID:
625         case AR9160_DEVID_PCI:
626         case AR9280_DEVID_PCI:
627         case AR9280_DEVID_PCIE:
628         case AR9285_DEVID_PCIE:
629         case AR9287_DEVID_PCI:
630         case AR9287_DEVID_PCIE:
631         case AR2427_DEVID_PCIE:
632         case AR9300_DEVID_PCIE:
633                 break;
634         default:
635                 if (common->bus_ops->ath_bus_type == ATH_USB)
636                         break;
637                 ath_print(common, ATH_DBG_FATAL,
638                           "Hardware device ID 0x%04x not supported\n",
639                           ah->hw_version.devid);
640                 return -EOPNOTSUPP;
641         }
642
643         ret = __ath9k_hw_init(ah);
644         if (ret) {
645                 ath_print(common, ATH_DBG_FATAL,
646                           "Unable to initialize hardware; "
647                           "initialization status: %d\n", ret);
648                 return ret;
649         }
650
651         return 0;
652 }
653 EXPORT_SYMBOL(ath9k_hw_init);
654
655 static void ath9k_hw_init_qos(struct ath_hw *ah)
656 {
657         ENABLE_REGWRITE_BUFFER(ah);
658
659         REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
660         REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
661
662         REG_WRITE(ah, AR_QOS_NO_ACK,
663                   SM(2, AR_QOS_NO_ACK_TWO_BIT) |
664                   SM(5, AR_QOS_NO_ACK_BIT_OFF) |
665                   SM(0, AR_QOS_NO_ACK_BYTE_OFF));
666
667         REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
668         REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
669         REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
670         REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
671         REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
672
673         REGWRITE_BUFFER_FLUSH(ah);
674 }
675
676 static void ath9k_hw_init_pll(struct ath_hw *ah,
677                               struct ath9k_channel *chan)
678 {
679         u32 pll = ath9k_hw_compute_pll_control(ah, chan);
680
681         REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
682
683         /* Switch the core clock for ar9271 to 117Mhz */
684         if (AR_SREV_9271(ah)) {
685                 udelay(500);
686                 REG_WRITE(ah, 0x50040, 0x304);
687         }
688
689         udelay(RTC_PLL_SETTLE_DELAY);
690
691         REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
692 }
693
694 static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
695                                           enum nl80211_iftype opmode)
696 {
697         u32 imr_reg = AR_IMR_TXERR |
698                 AR_IMR_TXURN |
699                 AR_IMR_RXERR |
700                 AR_IMR_RXORN |
701                 AR_IMR_BCNMISC;
702
703         if (AR_SREV_9300_20_OR_LATER(ah)) {
704                 imr_reg |= AR_IMR_RXOK_HP;
705                 if (ah->config.rx_intr_mitigation)
706                         imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
707                 else
708                         imr_reg |= AR_IMR_RXOK_LP;
709
710         } else {
711                 if (ah->config.rx_intr_mitigation)
712                         imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
713                 else
714                         imr_reg |= AR_IMR_RXOK;
715         }
716
717         if (ah->config.tx_intr_mitigation)
718                 imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
719         else
720                 imr_reg |= AR_IMR_TXOK;
721
722         if (opmode == NL80211_IFTYPE_AP)
723                 imr_reg |= AR_IMR_MIB;
724
725         ENABLE_REGWRITE_BUFFER(ah);
726
727         REG_WRITE(ah, AR_IMR, imr_reg);
728         ah->imrs2_reg |= AR_IMR_S2_GTT;
729         REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
730
731         if (!AR_SREV_9100(ah)) {
732                 REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
733                 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_DEFAULT);
734                 REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
735         }
736
737         REGWRITE_BUFFER_FLUSH(ah);
738
739         if (AR_SREV_9300_20_OR_LATER(ah)) {
740                 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
741                 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
742                 REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
743                 REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
744         }
745 }
746
747 static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
748 {
749         u32 val = ath9k_hw_mac_to_clks(ah, us);
750         val = min(val, (u32) 0xFFFF);
751         REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
752 }
753
754 static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
755 {
756         u32 val = ath9k_hw_mac_to_clks(ah, us);
757         val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
758         REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
759 }
760
761 static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
762 {
763         u32 val = ath9k_hw_mac_to_clks(ah, us);
764         val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
765         REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
766 }
767
768 static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
769 {
770         if (tu > 0xFFFF) {
771                 ath_print(ath9k_hw_common(ah), ATH_DBG_XMIT,
772                           "bad global tx timeout %u\n", tu);
773                 ah->globaltxtimeout = (u32) -1;
774                 return false;
775         } else {
776                 REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
777                 ah->globaltxtimeout = tu;
778                 return true;
779         }
780 }
781
782 void ath9k_hw_init_global_settings(struct ath_hw *ah)
783 {
784         struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
785         int acktimeout;
786         int slottime;
787         int sifstime;
788
789         ath_print(ath9k_hw_common(ah), ATH_DBG_RESET, "ah->misc_mode 0x%x\n",
790                   ah->misc_mode);
791
792         if (ah->misc_mode != 0)
793                 REG_WRITE(ah, AR_PCU_MISC,
794                           REG_READ(ah, AR_PCU_MISC) | ah->misc_mode);
795
796         if (conf->channel && conf->channel->band == IEEE80211_BAND_5GHZ)
797                 sifstime = 16;
798         else
799                 sifstime = 10;
800
801         /* As defined by IEEE 802.11-2007 17.3.8.6 */
802         slottime = ah->slottime + 3 * ah->coverage_class;
803         acktimeout = slottime + sifstime;
804
805         /*
806          * Workaround for early ACK timeouts, add an offset to match the
807          * initval's 64us ack timeout value.
808          * This was initially only meant to work around an issue with delayed
809          * BA frames in some implementations, but it has been found to fix ACK
810          * timeout issues in other cases as well.
811          */
812         if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ)
813                 acktimeout += 64 - sifstime - ah->slottime;
814
815         ath9k_hw_setslottime(ah, slottime);
816         ath9k_hw_set_ack_timeout(ah, acktimeout);
817         ath9k_hw_set_cts_timeout(ah, acktimeout);
818         if (ah->globaltxtimeout != (u32) -1)
819                 ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
820 }
821 EXPORT_SYMBOL(ath9k_hw_init_global_settings);
822
823 void ath9k_hw_deinit(struct ath_hw *ah)
824 {
825         struct ath_common *common = ath9k_hw_common(ah);
826
827         if (common->state < ATH_HW_INITIALIZED)
828                 goto free_hw;
829
830         ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
831
832 free_hw:
833         ath9k_hw_rf_free_ext_banks(ah);
834 }
835 EXPORT_SYMBOL(ath9k_hw_deinit);
836
837 /*******/
838 /* INI */
839 /*******/
840
841 u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
842 {
843         u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
844
845         if (IS_CHAN_B(chan))
846                 ctl |= CTL_11B;
847         else if (IS_CHAN_G(chan))
848                 ctl |= CTL_11G;
849         else
850                 ctl |= CTL_11A;
851
852         return ctl;
853 }
854
855 /****************************************/
856 /* Reset and Channel Switching Routines */
857 /****************************************/
858
859 static inline void ath9k_hw_set_dma(struct ath_hw *ah)
860 {
861         struct ath_common *common = ath9k_hw_common(ah);
862         u32 regval;
863
864         ENABLE_REGWRITE_BUFFER(ah);
865
866         /*
867          * set AHB_MODE not to do cacheline prefetches
868         */
869         if (!AR_SREV_9300_20_OR_LATER(ah)) {
870                 regval = REG_READ(ah, AR_AHB_MODE);
871                 REG_WRITE(ah, AR_AHB_MODE, regval | AR_AHB_PREFETCH_RD_EN);
872         }
873
874         /*
875          * let mac dma reads be in 128 byte chunks
876          */
877         regval = REG_READ(ah, AR_TXCFG) & ~AR_TXCFG_DMASZ_MASK;
878         REG_WRITE(ah, AR_TXCFG, regval | AR_TXCFG_DMASZ_128B);
879
880         REGWRITE_BUFFER_FLUSH(ah);
881
882         /*
883          * Restore TX Trigger Level to its pre-reset value.
884          * The initial value depends on whether aggregation is enabled, and is
885          * adjusted whenever underruns are detected.
886          */
887         if (!AR_SREV_9300_20_OR_LATER(ah))
888                 REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
889
890         ENABLE_REGWRITE_BUFFER(ah);
891
892         /*
893          * let mac dma writes be in 128 byte chunks
894          */
895         regval = REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_DMASZ_MASK;
896         REG_WRITE(ah, AR_RXCFG, regval | AR_RXCFG_DMASZ_128B);
897
898         /*
899          * Setup receive FIFO threshold to hold off TX activities
900          */
901         REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
902
903         if (AR_SREV_9300_20_OR_LATER(ah)) {
904                 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
905                 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
906
907                 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
908                         ah->caps.rx_status_len);
909         }
910
911         /*
912          * reduce the number of usable entries in PCU TXBUF to avoid
913          * wrap around issues.
914          */
915         if (AR_SREV_9285(ah)) {
916                 /* For AR9285 the number of Fifos are reduced to half.
917                  * So set the usable tx buf size also to half to
918                  * avoid data/delimiter underruns
919                  */
920                 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
921                           AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
922         } else if (!AR_SREV_9271(ah)) {
923                 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
924                           AR_PCU_TXBUF_CTRL_USABLE_SIZE);
925         }
926
927         REGWRITE_BUFFER_FLUSH(ah);
928
929         if (AR_SREV_9300_20_OR_LATER(ah))
930                 ath9k_hw_reset_txstatus_ring(ah);
931 }
932
933 static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
934 {
935         u32 val;
936
937         val = REG_READ(ah, AR_STA_ID1);
938         val &= ~(AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC);
939         switch (opmode) {
940         case NL80211_IFTYPE_AP:
941                 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_STA_AP
942                           | AR_STA_ID1_KSRCH_MODE);
943                 REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
944                 break;
945         case NL80211_IFTYPE_ADHOC:
946         case NL80211_IFTYPE_MESH_POINT:
947                 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_ADHOC
948                           | AR_STA_ID1_KSRCH_MODE);
949                 REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
950                 break;
951         case NL80211_IFTYPE_STATION:
952         case NL80211_IFTYPE_MONITOR:
953                 REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_KSRCH_MODE);
954                 break;
955         }
956 }
957
958 void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
959                                    u32 *coef_mantissa, u32 *coef_exponent)
960 {
961         u32 coef_exp, coef_man;
962
963         for (coef_exp = 31; coef_exp > 0; coef_exp--)
964                 if ((coef_scaled >> coef_exp) & 0x1)
965                         break;
966
967         coef_exp = 14 - (coef_exp - COEF_SCALE_S);
968
969         coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
970
971         *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
972         *coef_exponent = coef_exp - 16;
973 }
974
975 static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
976 {
977         u32 rst_flags;
978         u32 tmpReg;
979
980         if (AR_SREV_9100(ah)) {
981                 u32 val = REG_READ(ah, AR_RTC_DERIVED_CLK);
982                 val &= ~AR_RTC_DERIVED_CLK_PERIOD;
983                 val |= SM(1, AR_RTC_DERIVED_CLK_PERIOD);
984                 REG_WRITE(ah, AR_RTC_DERIVED_CLK, val);
985                 (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
986         }
987
988         ENABLE_REGWRITE_BUFFER(ah);
989
990         if (AR_SREV_9300_20_OR_LATER(ah)) {
991                 REG_WRITE(ah, AR_WA, ah->WARegVal);
992                 udelay(10);
993         }
994
995         REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
996                   AR_RTC_FORCE_WAKE_ON_INT);
997
998         if (AR_SREV_9100(ah)) {
999                 rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
1000                         AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
1001         } else {
1002                 tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
1003                 if (tmpReg &
1004                     (AR_INTR_SYNC_LOCAL_TIMEOUT |
1005                      AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
1006                         u32 val;
1007                         REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
1008
1009                         val = AR_RC_HOSTIF;
1010                         if (!AR_SREV_9300_20_OR_LATER(ah))
1011                                 val |= AR_RC_AHB;
1012                         REG_WRITE(ah, AR_RC, val);
1013
1014                 } else if (!AR_SREV_9300_20_OR_LATER(ah))
1015                         REG_WRITE(ah, AR_RC, AR_RC_AHB);
1016
1017                 rst_flags = AR_RTC_RC_MAC_WARM;
1018                 if (type == ATH9K_RESET_COLD)
1019                         rst_flags |= AR_RTC_RC_MAC_COLD;
1020         }
1021
1022         REG_WRITE(ah, AR_RTC_RC, rst_flags);
1023
1024         REGWRITE_BUFFER_FLUSH(ah);
1025
1026         udelay(50);
1027
1028         REG_WRITE(ah, AR_RTC_RC, 0);
1029         if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
1030                 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
1031                           "RTC stuck in MAC reset\n");
1032                 return false;
1033         }
1034
1035         if (!AR_SREV_9100(ah))
1036                 REG_WRITE(ah, AR_RC, 0);
1037
1038         if (AR_SREV_9100(ah))
1039                 udelay(50);
1040
1041         return true;
1042 }
1043
1044 static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
1045 {
1046         ENABLE_REGWRITE_BUFFER(ah);
1047
1048         if (AR_SREV_9300_20_OR_LATER(ah)) {
1049                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1050                 udelay(10);
1051         }
1052
1053         REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1054                   AR_RTC_FORCE_WAKE_ON_INT);
1055
1056         if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1057                 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1058
1059         REG_WRITE(ah, AR_RTC_RESET, 0);
1060         udelay(2);
1061
1062         REGWRITE_BUFFER_FLUSH(ah);
1063
1064         if (!AR_SREV_9300_20_OR_LATER(ah))
1065                 udelay(2);
1066
1067         if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1068                 REG_WRITE(ah, AR_RC, 0);
1069
1070         REG_WRITE(ah, AR_RTC_RESET, 1);
1071
1072         if (!ath9k_hw_wait(ah,
1073                            AR_RTC_STATUS,
1074                            AR_RTC_STATUS_M,
1075                            AR_RTC_STATUS_ON,
1076                            AH_WAIT_TIMEOUT)) {
1077                 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
1078                           "RTC not waking up\n");
1079                 return false;
1080         }
1081
1082         ath9k_hw_read_revisions(ah);
1083
1084         return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
1085 }
1086
1087 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
1088 {
1089         if (AR_SREV_9300_20_OR_LATER(ah)) {
1090                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1091                 udelay(10);
1092         }
1093
1094         REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1095                   AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
1096
1097         switch (type) {
1098         case ATH9K_RESET_POWER_ON:
1099                 return ath9k_hw_set_reset_power_on(ah);
1100         case ATH9K_RESET_WARM:
1101         case ATH9K_RESET_COLD:
1102                 return ath9k_hw_set_reset(ah, type);
1103         default:
1104                 return false;
1105         }
1106 }
1107
1108 static bool ath9k_hw_chip_reset(struct ath_hw *ah,
1109                                 struct ath9k_channel *chan)
1110 {
1111         if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)) {
1112                 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON))
1113                         return false;
1114         } else if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
1115                 return false;
1116
1117         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1118                 return false;
1119
1120         ah->chip_fullsleep = false;
1121         ath9k_hw_init_pll(ah, chan);
1122         ath9k_hw_set_rfmode(ah, chan);
1123
1124         return true;
1125 }
1126
1127 static bool ath9k_hw_channel_change(struct ath_hw *ah,
1128                                     struct ath9k_channel *chan)
1129 {
1130         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1131         struct ath_common *common = ath9k_hw_common(ah);
1132         struct ieee80211_channel *channel = chan->chan;
1133         u32 qnum;
1134         int r;
1135
1136         for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
1137                 if (ath9k_hw_numtxpending(ah, qnum)) {
1138                         ath_print(common, ATH_DBG_QUEUE,
1139                                   "Transmit frames pending on "
1140                                   "queue %d\n", qnum);
1141                         return false;
1142                 }
1143         }
1144
1145         if (!ath9k_hw_rfbus_req(ah)) {
1146                 ath_print(common, ATH_DBG_FATAL,
1147                           "Could not kill baseband RX\n");
1148                 return false;
1149         }
1150
1151         ath9k_hw_set_channel_regs(ah, chan);
1152
1153         r = ath9k_hw_rf_set_freq(ah, chan);
1154         if (r) {
1155                 ath_print(common, ATH_DBG_FATAL,
1156                           "Failed to set channel\n");
1157                 return false;
1158         }
1159
1160         ah->eep_ops->set_txpower(ah, chan,
1161                              ath9k_regd_get_ctl(regulatory, chan),
1162                              channel->max_antenna_gain * 2,
1163                              channel->max_power * 2,
1164                              min((u32) MAX_RATE_POWER,
1165                              (u32) regulatory->power_limit));
1166
1167         ath9k_hw_rfbus_done(ah);
1168
1169         if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1170                 ath9k_hw_set_delta_slope(ah, chan);
1171
1172         ath9k_hw_spur_mitigate_freq(ah, chan);
1173
1174         return true;
1175 }
1176
1177 bool ath9k_hw_check_alive(struct ath_hw *ah)
1178 {
1179         int count = 50;
1180         u32 reg;
1181
1182         if (AR_SREV_9285_12_OR_LATER(ah))
1183                 return true;
1184
1185         do {
1186                 reg = REG_READ(ah, AR_OBS_BUS_1);
1187
1188                 if ((reg & 0x7E7FFFEF) == 0x00702400)
1189                         continue;
1190
1191                 switch (reg & 0x7E000B00) {
1192                 case 0x1E000000:
1193                 case 0x52000B00:
1194                 case 0x18000B00:
1195                         continue;
1196                 default:
1197                         return true;
1198                 }
1199         } while (count-- > 0);
1200
1201         return false;
1202 }
1203 EXPORT_SYMBOL(ath9k_hw_check_alive);
1204
1205 int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
1206                    struct ath9k_hw_cal_data *caldata, bool bChannelChange)
1207 {
1208         struct ath_common *common = ath9k_hw_common(ah);
1209         u32 saveLedState;
1210         struct ath9k_channel *curchan = ah->curchan;
1211         u32 saveDefAntenna;
1212         u32 macStaId1;
1213         u64 tsf = 0;
1214         int i, r;
1215
1216         ah->txchainmask = common->tx_chainmask;
1217         ah->rxchainmask = common->rx_chainmask;
1218
1219         if (!ah->chip_fullsleep) {
1220                 ath9k_hw_abortpcurecv(ah);
1221                 if (!ath9k_hw_stopdmarecv(ah)) {
1222                         ath_print(common, ATH_DBG_XMIT,
1223                                 "Failed to stop receive dma\n");
1224                         bChannelChange = false;
1225                 }
1226         }
1227
1228         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1229                 return -EIO;
1230
1231         if (curchan && !ah->chip_fullsleep)
1232                 ath9k_hw_getnf(ah, curchan);
1233
1234         ah->caldata = caldata;
1235         if (caldata &&
1236             (chan->channel != caldata->channel ||
1237              (chan->channelFlags & ~CHANNEL_CW_INT) !=
1238              (caldata->channelFlags & ~CHANNEL_CW_INT))) {
1239                 /* Operating channel changed, reset channel calibration data */
1240                 memset(caldata, 0, sizeof(*caldata));
1241                 ath9k_init_nfcal_hist_buffer(ah, chan);
1242         }
1243
1244         if (bChannelChange &&
1245             (ah->chip_fullsleep != true) &&
1246             (ah->curchan != NULL) &&
1247             (chan->channel != ah->curchan->channel) &&
1248             ((chan->channelFlags & CHANNEL_ALL) ==
1249              (ah->curchan->channelFlags & CHANNEL_ALL)) &&
1250             (!AR_SREV_9280(ah) || AR_DEVID_7010(ah))) {
1251
1252                 if (ath9k_hw_channel_change(ah, chan)) {
1253                         ath9k_hw_loadnf(ah, ah->curchan);
1254                         ath9k_hw_start_nfcal(ah, true);
1255                         if (AR_SREV_9271(ah))
1256                                 ar9002_hw_load_ani_reg(ah, chan);
1257                         return 0;
1258                 }
1259         }
1260
1261         saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
1262         if (saveDefAntenna == 0)
1263                 saveDefAntenna = 1;
1264
1265         macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
1266
1267         /* For chips on which RTC reset is done, save TSF before it gets cleared */
1268         if (AR_SREV_9100(ah) ||
1269             (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)))
1270                 tsf = ath9k_hw_gettsf64(ah);
1271
1272         saveLedState = REG_READ(ah, AR_CFG_LED) &
1273                 (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
1274                  AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
1275
1276         ath9k_hw_mark_phy_inactive(ah);
1277
1278         /* Only required on the first reset */
1279         if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1280                 REG_WRITE(ah,
1281                           AR9271_RESET_POWER_DOWN_CONTROL,
1282                           AR9271_RADIO_RF_RST);
1283                 udelay(50);
1284         }
1285
1286         if (!ath9k_hw_chip_reset(ah, chan)) {
1287                 ath_print(common, ATH_DBG_FATAL, "Chip reset failed\n");
1288                 return -EINVAL;
1289         }
1290
1291         /* Only required on the first reset */
1292         if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1293                 ah->htc_reset_init = false;
1294                 REG_WRITE(ah,
1295                           AR9271_RESET_POWER_DOWN_CONTROL,
1296                           AR9271_GATE_MAC_CTL);
1297                 udelay(50);
1298         }
1299
1300         /* Restore TSF */
1301         if (tsf)
1302                 ath9k_hw_settsf64(ah, tsf);
1303
1304         if (AR_SREV_9280_20_OR_LATER(ah))
1305                 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
1306
1307         if (!AR_SREV_9300_20_OR_LATER(ah))
1308                 ar9002_hw_enable_async_fifo(ah);
1309
1310         r = ath9k_hw_process_ini(ah, chan);
1311         if (r)
1312                 return r;
1313
1314         /*
1315          * Some AR91xx SoC devices frequently fail to accept TSF writes
1316          * right after the chip reset. When that happens, write a new
1317          * value after the initvals have been applied, with an offset
1318          * based on measured time difference
1319          */
1320         if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
1321                 tsf += 1500;
1322                 ath9k_hw_settsf64(ah, tsf);
1323         }
1324
1325         /* Setup MFP options for CCMP */
1326         if (AR_SREV_9280_20_OR_LATER(ah)) {
1327                 /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
1328                  * frames when constructing CCMP AAD. */
1329                 REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
1330                               0xc7ff);
1331                 ah->sw_mgmt_crypto = false;
1332         } else if (AR_SREV_9160_10_OR_LATER(ah)) {
1333                 /* Disable hardware crypto for management frames */
1334                 REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
1335                             AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
1336                 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1337                             AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
1338                 ah->sw_mgmt_crypto = true;
1339         } else
1340                 ah->sw_mgmt_crypto = true;
1341
1342         if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1343                 ath9k_hw_set_delta_slope(ah, chan);
1344
1345         ath9k_hw_spur_mitigate_freq(ah, chan);
1346         ah->eep_ops->set_board_values(ah, chan);
1347
1348         ath9k_hw_set_operating_mode(ah, ah->opmode);
1349
1350         ENABLE_REGWRITE_BUFFER(ah);
1351
1352         REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
1353         REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
1354                   | macStaId1
1355                   | AR_STA_ID1_RTS_USE_DEF
1356                   | (ah->config.
1357                      ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
1358                   | ah->sta_id1_defaults);
1359         ath_hw_setbssidmask(common);
1360         REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
1361         ath9k_hw_write_associd(ah);
1362         REG_WRITE(ah, AR_ISR, ~0);
1363         REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
1364
1365         REGWRITE_BUFFER_FLUSH(ah);
1366
1367         r = ath9k_hw_rf_set_freq(ah, chan);
1368         if (r)
1369                 return r;
1370
1371         ENABLE_REGWRITE_BUFFER(ah);
1372
1373         for (i = 0; i < AR_NUM_DCU; i++)
1374                 REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
1375
1376         REGWRITE_BUFFER_FLUSH(ah);
1377
1378         ah->intr_txqs = 0;
1379         for (i = 0; i < ah->caps.total_queues; i++)
1380                 ath9k_hw_resettxqueue(ah, i);
1381
1382         ath9k_hw_init_interrupt_masks(ah, ah->opmode);
1383         ath9k_hw_ani_cache_ini_regs(ah);
1384         ath9k_hw_init_qos(ah);
1385
1386         if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1387                 ath9k_enable_rfkill(ah);
1388
1389         ath9k_hw_init_global_settings(ah);
1390
1391         if (!AR_SREV_9300_20_OR_LATER(ah)) {
1392                 ar9002_hw_update_async_fifo(ah);
1393                 ar9002_hw_enable_wep_aggregation(ah);
1394         }
1395
1396         REG_WRITE(ah, AR_STA_ID1,
1397                   REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PRESERVE_SEQNUM);
1398
1399         ath9k_hw_set_dma(ah);
1400
1401         REG_WRITE(ah, AR_OBS, 8);
1402
1403         if (ah->config.rx_intr_mitigation) {
1404                 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
1405                 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
1406         }
1407
1408         if (ah->config.tx_intr_mitigation) {
1409                 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
1410                 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
1411         }
1412
1413         ath9k_hw_init_bb(ah, chan);
1414
1415         if (!ath9k_hw_init_cal(ah, chan))
1416                 return -EIO;
1417
1418         ENABLE_REGWRITE_BUFFER(ah);
1419
1420         ath9k_hw_restore_chainmask(ah);
1421         REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
1422
1423         REGWRITE_BUFFER_FLUSH(ah);
1424
1425         /*
1426          * For big endian systems turn on swapping for descriptors
1427          */
1428         if (AR_SREV_9100(ah)) {
1429                 u32 mask;
1430                 mask = REG_READ(ah, AR_CFG);
1431                 if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
1432                         ath_print(common, ATH_DBG_RESET,
1433                                 "CFG Byte Swap Set 0x%x\n", mask);
1434                 } else {
1435                         mask =
1436                                 INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
1437                         REG_WRITE(ah, AR_CFG, mask);
1438                         ath_print(common, ATH_DBG_RESET,
1439                                 "Setting CFG 0x%x\n", REG_READ(ah, AR_CFG));
1440                 }
1441         } else {
1442                 if (common->bus_ops->ath_bus_type == ATH_USB) {
1443                         /* Configure AR9271 target WLAN */
1444                         if (AR_SREV_9271(ah))
1445                                 REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
1446                         else
1447                                 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1448                 }
1449 #ifdef __BIG_ENDIAN
1450                 else
1451                         REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1452 #endif
1453         }
1454
1455         if (ah->btcoex_hw.enabled)
1456                 ath9k_hw_btcoex_enable(ah);
1457
1458         if (AR_SREV_9300_20_OR_LATER(ah))
1459                 ar9003_hw_bb_watchdog_config(ah);
1460
1461         return 0;
1462 }
1463 EXPORT_SYMBOL(ath9k_hw_reset);
1464
1465 /******************************/
1466 /* Power Management (Chipset) */
1467 /******************************/
1468
1469 /*
1470  * Notify Power Mgt is disabled in self-generated frames.
1471  * If requested, force chip to sleep.
1472  */
1473 static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
1474 {
1475         REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1476         if (setChip) {
1477                 /*
1478                  * Clear the RTC force wake bit to allow the
1479                  * mac to go to sleep.
1480                  */
1481                 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1482                             AR_RTC_FORCE_WAKE_EN);
1483                 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1484                         REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
1485
1486                 /* Shutdown chip. Active low */
1487                 if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah))
1488                         REG_CLR_BIT(ah, (AR_RTC_RESET),
1489                                     AR_RTC_RESET_EN);
1490         }
1491
1492         /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
1493         if (AR_SREV_9300_20_OR_LATER(ah))
1494                 REG_WRITE(ah, AR_WA,
1495                           ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
1496 }
1497
1498 /*
1499  * Notify Power Management is enabled in self-generating
1500  * frames. If request, set power mode of chip to
1501  * auto/normal.  Duration in units of 128us (1/8 TU).
1502  */
1503 static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
1504 {
1505         REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1506         if (setChip) {
1507                 struct ath9k_hw_capabilities *pCap = &ah->caps;
1508
1509                 if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1510                         /* Set WakeOnInterrupt bit; clear ForceWake bit */
1511                         REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1512                                   AR_RTC_FORCE_WAKE_ON_INT);
1513                 } else {
1514                         /*
1515                          * Clear the RTC force wake bit to allow the
1516                          * mac to go to sleep.
1517                          */
1518                         REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1519                                     AR_RTC_FORCE_WAKE_EN);
1520                 }
1521         }
1522
1523         /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
1524         if (AR_SREV_9300_20_OR_LATER(ah))
1525                 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
1526 }
1527
1528 static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
1529 {
1530         u32 val;
1531         int i;
1532
1533         /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
1534         if (AR_SREV_9300_20_OR_LATER(ah)) {
1535                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1536                 udelay(10);
1537         }
1538
1539         if (setChip) {
1540                 if ((REG_READ(ah, AR_RTC_STATUS) &
1541                      AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
1542                         if (ath9k_hw_set_reset_reg(ah,
1543                                            ATH9K_RESET_POWER_ON) != true) {
1544                                 return false;
1545                         }
1546                         if (!AR_SREV_9300_20_OR_LATER(ah))
1547                                 ath9k_hw_init_pll(ah, NULL);
1548                 }
1549                 if (AR_SREV_9100(ah))
1550                         REG_SET_BIT(ah, AR_RTC_RESET,
1551                                     AR_RTC_RESET_EN);
1552
1553                 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1554                             AR_RTC_FORCE_WAKE_EN);
1555                 udelay(50);
1556
1557                 for (i = POWER_UP_TIME / 50; i > 0; i--) {
1558                         val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
1559                         if (val == AR_RTC_STATUS_ON)
1560                                 break;
1561                         udelay(50);
1562                         REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1563                                     AR_RTC_FORCE_WAKE_EN);
1564                 }
1565                 if (i == 0) {
1566                         ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
1567                                   "Failed to wakeup in %uus\n",
1568                                   POWER_UP_TIME / 20);
1569                         return false;
1570                 }
1571         }
1572
1573         REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1574
1575         return true;
1576 }
1577
1578 bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
1579 {
1580         struct ath_common *common = ath9k_hw_common(ah);
1581         int status = true, setChip = true;
1582         static const char *modes[] = {
1583                 "AWAKE",
1584                 "FULL-SLEEP",
1585                 "NETWORK SLEEP",
1586                 "UNDEFINED"
1587         };
1588
1589         if (ah->power_mode == mode)
1590                 return status;
1591
1592         ath_print(common, ATH_DBG_RESET, "%s -> %s\n",
1593                   modes[ah->power_mode], modes[mode]);
1594
1595         switch (mode) {
1596         case ATH9K_PM_AWAKE:
1597                 status = ath9k_hw_set_power_awake(ah, setChip);
1598                 break;
1599         case ATH9K_PM_FULL_SLEEP:
1600                 ath9k_set_power_sleep(ah, setChip);
1601                 ah->chip_fullsleep = true;
1602                 break;
1603         case ATH9K_PM_NETWORK_SLEEP:
1604                 ath9k_set_power_network_sleep(ah, setChip);
1605                 break;
1606         default:
1607                 ath_print(common, ATH_DBG_FATAL,
1608                           "Unknown power mode %u\n", mode);
1609                 return false;
1610         }
1611         ah->power_mode = mode;
1612
1613         return status;
1614 }
1615 EXPORT_SYMBOL(ath9k_hw_setpower);
1616
1617 /*******************/
1618 /* Beacon Handling */
1619 /*******************/
1620
1621 void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
1622 {
1623         int flags = 0;
1624
1625         ah->beacon_interval = beacon_period;
1626
1627         ENABLE_REGWRITE_BUFFER(ah);
1628
1629         switch (ah->opmode) {
1630         case NL80211_IFTYPE_STATION:
1631         case NL80211_IFTYPE_MONITOR:
1632                 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
1633                 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, 0xffff);
1634                 REG_WRITE(ah, AR_NEXT_SWBA, 0x7ffff);
1635                 flags |= AR_TBTT_TIMER_EN;
1636                 break;
1637         case NL80211_IFTYPE_ADHOC:
1638         case NL80211_IFTYPE_MESH_POINT:
1639                 REG_SET_BIT(ah, AR_TXCFG,
1640                             AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
1641                 REG_WRITE(ah, AR_NEXT_NDP_TIMER,
1642                           TU_TO_USEC(next_beacon +
1643                                      (ah->atim_window ? ah->
1644                                       atim_window : 1)));
1645                 flags |= AR_NDP_TIMER_EN;
1646         case NL80211_IFTYPE_AP:
1647                 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
1648                 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT,
1649                           TU_TO_USEC(next_beacon -
1650                                      ah->config.
1651                                      dma_beacon_response_time));
1652                 REG_WRITE(ah, AR_NEXT_SWBA,
1653                           TU_TO_USEC(next_beacon -
1654                                      ah->config.
1655                                      sw_beacon_response_time));
1656                 flags |=
1657                         AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
1658                 break;
1659         default:
1660                 ath_print(ath9k_hw_common(ah), ATH_DBG_BEACON,
1661                           "%s: unsupported opmode: %d\n",
1662                           __func__, ah->opmode);
1663                 return;
1664                 break;
1665         }
1666
1667         REG_WRITE(ah, AR_BEACON_PERIOD, TU_TO_USEC(beacon_period));
1668         REG_WRITE(ah, AR_DMA_BEACON_PERIOD, TU_TO_USEC(beacon_period));
1669         REG_WRITE(ah, AR_SWBA_PERIOD, TU_TO_USEC(beacon_period));
1670         REG_WRITE(ah, AR_NDP_PERIOD, TU_TO_USEC(beacon_period));
1671
1672         REGWRITE_BUFFER_FLUSH(ah);
1673
1674         beacon_period &= ~ATH9K_BEACON_ENA;
1675         if (beacon_period & ATH9K_BEACON_RESET_TSF) {
1676                 ath9k_hw_reset_tsf(ah);
1677         }
1678
1679         REG_SET_BIT(ah, AR_TIMER_MODE, flags);
1680 }
1681 EXPORT_SYMBOL(ath9k_hw_beaconinit);
1682
1683 void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
1684                                     const struct ath9k_beacon_state *bs)
1685 {
1686         u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
1687         struct ath9k_hw_capabilities *pCap = &ah->caps;
1688         struct ath_common *common = ath9k_hw_common(ah);
1689
1690         ENABLE_REGWRITE_BUFFER(ah);
1691
1692         REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
1693
1694         REG_WRITE(ah, AR_BEACON_PERIOD,
1695                   TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1696         REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
1697                   TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1698
1699         REGWRITE_BUFFER_FLUSH(ah);
1700
1701         REG_RMW_FIELD(ah, AR_RSSI_THR,
1702                       AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
1703
1704         beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
1705
1706         if (bs->bs_sleepduration > beaconintval)
1707                 beaconintval = bs->bs_sleepduration;
1708
1709         dtimperiod = bs->bs_dtimperiod;
1710         if (bs->bs_sleepduration > dtimperiod)
1711                 dtimperiod = bs->bs_sleepduration;
1712
1713         if (beaconintval == dtimperiod)
1714                 nextTbtt = bs->bs_nextdtim;
1715         else
1716                 nextTbtt = bs->bs_nexttbtt;
1717
1718         ath_print(common, ATH_DBG_BEACON, "next DTIM %d\n", bs->bs_nextdtim);
1719         ath_print(common, ATH_DBG_BEACON, "next beacon %d\n", nextTbtt);
1720         ath_print(common, ATH_DBG_BEACON, "beacon period %d\n", beaconintval);
1721         ath_print(common, ATH_DBG_BEACON, "DTIM period %d\n", dtimperiod);
1722
1723         ENABLE_REGWRITE_BUFFER(ah);
1724
1725         REG_WRITE(ah, AR_NEXT_DTIM,
1726                   TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
1727         REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
1728
1729         REG_WRITE(ah, AR_SLEEP1,
1730                   SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
1731                   | AR_SLEEP1_ASSUME_DTIM);
1732
1733         if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
1734                 beacontimeout = (BEACON_TIMEOUT_VAL << 3);
1735         else
1736                 beacontimeout = MIN_BEACON_TIMEOUT_VAL;
1737
1738         REG_WRITE(ah, AR_SLEEP2,
1739                   SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
1740
1741         REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
1742         REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
1743
1744         REGWRITE_BUFFER_FLUSH(ah);
1745
1746         REG_SET_BIT(ah, AR_TIMER_MODE,
1747                     AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
1748                     AR_DTIM_TIMER_EN);
1749
1750         /* TSF Out of Range Threshold */
1751         REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
1752 }
1753 EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
1754
1755 /*******************/
1756 /* HW Capabilities */
1757 /*******************/
1758
1759 int ath9k_hw_fill_cap_info(struct ath_hw *ah)
1760 {
1761         struct ath9k_hw_capabilities *pCap = &ah->caps;
1762         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1763         struct ath_common *common = ath9k_hw_common(ah);
1764         struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
1765
1766         u16 capField = 0, eeval;
1767         u8 ant_div_ctl1;
1768
1769         eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
1770         regulatory->current_rd = eeval;
1771
1772         eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_1);
1773         if (AR_SREV_9285_12_OR_LATER(ah))
1774                 eeval |= AR9285_RDEXT_DEFAULT;
1775         regulatory->current_rd_ext = eeval;
1776
1777         capField = ah->eep_ops->get_eeprom(ah, EEP_OP_CAP);
1778
1779         if (ah->opmode != NL80211_IFTYPE_AP &&
1780             ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
1781                 if (regulatory->current_rd == 0x64 ||
1782                     regulatory->current_rd == 0x65)
1783                         regulatory->current_rd += 5;
1784                 else if (regulatory->current_rd == 0x41)
1785                         regulatory->current_rd = 0x43;
1786                 ath_print(common, ATH_DBG_REGULATORY,
1787                           "regdomain mapped to 0x%x\n", regulatory->current_rd);
1788         }
1789
1790         eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
1791         if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
1792                 ath_print(common, ATH_DBG_FATAL,
1793                           "no band has been marked as supported in EEPROM.\n");
1794                 return -EINVAL;
1795         }
1796
1797         bitmap_zero(pCap->wireless_modes, ATH9K_MODE_MAX);
1798
1799         if (eeval & AR5416_OPFLAGS_11A) {
1800                 set_bit(ATH9K_MODE_11A, pCap->wireless_modes);
1801                 if (ah->config.ht_enable) {
1802                         if (!(eeval & AR5416_OPFLAGS_N_5G_HT20))
1803                                 set_bit(ATH9K_MODE_11NA_HT20,
1804                                         pCap->wireless_modes);
1805                         if (!(eeval & AR5416_OPFLAGS_N_5G_HT40)) {
1806                                 set_bit(ATH9K_MODE_11NA_HT40PLUS,
1807                                         pCap->wireless_modes);
1808                                 set_bit(ATH9K_MODE_11NA_HT40MINUS,
1809                                         pCap->wireless_modes);
1810                         }
1811                 }
1812         }
1813
1814         if (eeval & AR5416_OPFLAGS_11G) {
1815                 set_bit(ATH9K_MODE_11G, pCap->wireless_modes);
1816                 if (ah->config.ht_enable) {
1817                         if (!(eeval & AR5416_OPFLAGS_N_2G_HT20))
1818                                 set_bit(ATH9K_MODE_11NG_HT20,
1819                                         pCap->wireless_modes);
1820                         if (!(eeval & AR5416_OPFLAGS_N_2G_HT40)) {
1821                                 set_bit(ATH9K_MODE_11NG_HT40PLUS,
1822                                         pCap->wireless_modes);
1823                                 set_bit(ATH9K_MODE_11NG_HT40MINUS,
1824                                         pCap->wireless_modes);
1825                         }
1826                 }
1827         }
1828
1829         pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
1830         /*
1831          * For AR9271 we will temporarilly uses the rx chainmax as read from
1832          * the EEPROM.
1833          */
1834         if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
1835             !(eeval & AR5416_OPFLAGS_11A) &&
1836             !(AR_SREV_9271(ah)))
1837                 /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
1838                 pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
1839         else
1840                 /* Use rx_chainmask from EEPROM. */
1841                 pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
1842
1843         ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
1844
1845         pCap->low_2ghz_chan = 2312;
1846         pCap->high_2ghz_chan = 2732;
1847
1848         pCap->low_5ghz_chan = 4920;
1849         pCap->high_5ghz_chan = 6100;
1850
1851         common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
1852
1853         if (ah->config.ht_enable)
1854                 pCap->hw_caps |= ATH9K_HW_CAP_HT;
1855         else
1856                 pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
1857
1858         if (capField & AR_EEPROM_EEPCAP_MAXQCU)
1859                 pCap->total_queues =
1860                         MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
1861         else
1862                 pCap->total_queues = ATH9K_NUM_TX_QUEUES;
1863
1864         if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
1865                 pCap->keycache_size =
1866                         1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
1867         else
1868                 pCap->keycache_size = AR_KEYTABLE_SIZE;
1869
1870         if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
1871                 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD >> 1;
1872         else
1873                 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
1874
1875         if (AR_SREV_9271(ah))
1876                 pCap->num_gpio_pins = AR9271_NUM_GPIO;
1877         else if (AR_DEVID_7010(ah))
1878                 pCap->num_gpio_pins = AR7010_NUM_GPIO;
1879         else if (AR_SREV_9285_12_OR_LATER(ah))
1880                 pCap->num_gpio_pins = AR9285_NUM_GPIO;
1881         else if (AR_SREV_9280_20_OR_LATER(ah))
1882                 pCap->num_gpio_pins = AR928X_NUM_GPIO;
1883         else
1884                 pCap->num_gpio_pins = AR_NUM_GPIO;
1885
1886         if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
1887                 pCap->hw_caps |= ATH9K_HW_CAP_CST;
1888                 pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
1889         } else {
1890                 pCap->rts_aggr_limit = (8 * 1024);
1891         }
1892
1893         pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
1894
1895 #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
1896         ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
1897         if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
1898                 ah->rfkill_gpio =
1899                         MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
1900                 ah->rfkill_polarity =
1901                         MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
1902
1903                 pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
1904         }
1905 #endif
1906         if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
1907                 pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
1908         else
1909                 pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
1910
1911         if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
1912                 pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
1913         else
1914                 pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
1915
1916         if (regulatory->current_rd_ext & (1 << REG_EXT_JAPAN_MIDBAND)) {
1917                 pCap->reg_cap =
1918                         AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
1919                         AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
1920                         AR_EEPROM_EEREGCAP_EN_KK_U2 |
1921                         AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
1922         } else {
1923                 pCap->reg_cap =
1924                         AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
1925                         AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
1926         }
1927
1928         /* Advertise midband for AR5416 with FCC midband set in eeprom */
1929         if (regulatory->current_rd_ext & (1 << REG_EXT_FCC_MIDBAND) &&
1930             AR_SREV_5416(ah))
1931                 pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
1932
1933         pCap->num_antcfg_5ghz =
1934                 ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_5GHZ);
1935         pCap->num_antcfg_2ghz =
1936                 ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_2GHZ);
1937
1938         if (AR_SREV_9280_20_OR_LATER(ah) &&
1939             ath9k_hw_btcoex_supported(ah)) {
1940                 btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO;
1941                 btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO;
1942
1943                 if (AR_SREV_9285(ah)) {
1944                         btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
1945                         btcoex_hw->btpriority_gpio = ATH_BTPRIORITY_GPIO;
1946                 } else {
1947                         btcoex_hw->scheme = ATH_BTCOEX_CFG_2WIRE;
1948                 }
1949         } else {
1950                 btcoex_hw->scheme = ATH_BTCOEX_CFG_NONE;
1951         }
1952
1953         if (AR_SREV_9300_20_OR_LATER(ah)) {
1954                 pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_LDPC |
1955                                  ATH9K_HW_CAP_FASTCLOCK;
1956                 pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
1957                 pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
1958                 pCap->rx_status_len = sizeof(struct ar9003_rxs);
1959                 pCap->tx_desc_len = sizeof(struct ar9003_txc);
1960                 pCap->txs_len = sizeof(struct ar9003_txs);
1961                 if (ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
1962                         pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
1963         } else {
1964                 pCap->tx_desc_len = sizeof(struct ath_desc);
1965                 if (AR_SREV_9280_20(ah) &&
1966                     ((ah->eep_ops->get_eeprom(ah, EEP_MINOR_REV) <=
1967                       AR5416_EEP_MINOR_VER_16) ||
1968                      ah->eep_ops->get_eeprom(ah, EEP_FSTCLK_5G)))
1969                         pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
1970         }
1971
1972         if (AR_SREV_9300_20_OR_LATER(ah))
1973                 pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
1974
1975         if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
1976                 pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
1977
1978         if (AR_SREV_9285(ah))
1979                 if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
1980                         ant_div_ctl1 =
1981                                 ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
1982                         if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1))
1983                                 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
1984                 }
1985
1986         return 0;
1987 }
1988
1989 /****************************/
1990 /* GPIO / RFKILL / Antennae */
1991 /****************************/
1992
1993 static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
1994                                          u32 gpio, u32 type)
1995 {
1996         int addr;
1997         u32 gpio_shift, tmp;
1998
1999         if (gpio > 11)
2000                 addr = AR_GPIO_OUTPUT_MUX3;
2001         else if (gpio > 5)
2002                 addr = AR_GPIO_OUTPUT_MUX2;
2003         else
2004                 addr = AR_GPIO_OUTPUT_MUX1;
2005
2006         gpio_shift = (gpio % 6) * 5;
2007
2008         if (AR_SREV_9280_20_OR_LATER(ah)
2009             || (addr != AR_GPIO_OUTPUT_MUX1)) {
2010                 REG_RMW(ah, addr, (type << gpio_shift),
2011                         (0x1f << gpio_shift));
2012         } else {
2013                 tmp = REG_READ(ah, addr);
2014                 tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
2015                 tmp &= ~(0x1f << gpio_shift);
2016                 tmp |= (type << gpio_shift);
2017                 REG_WRITE(ah, addr, tmp);
2018         }
2019 }
2020
2021 void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
2022 {
2023         u32 gpio_shift;
2024
2025         BUG_ON(gpio >= ah->caps.num_gpio_pins);
2026
2027         if (AR_DEVID_7010(ah)) {
2028                 gpio_shift = gpio;
2029                 REG_RMW(ah, AR7010_GPIO_OE,
2030                         (AR7010_GPIO_OE_AS_INPUT << gpio_shift),
2031                         (AR7010_GPIO_OE_MASK << gpio_shift));
2032                 return;
2033         }
2034
2035         gpio_shift = gpio << 1;
2036         REG_RMW(ah,
2037                 AR_GPIO_OE_OUT,
2038                 (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
2039                 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2040 }
2041 EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
2042
2043 u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
2044 {
2045 #define MS_REG_READ(x, y) \
2046         (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
2047
2048         if (gpio >= ah->caps.num_gpio_pins)
2049                 return 0xffffffff;
2050
2051         if (AR_DEVID_7010(ah)) {
2052                 u32 val;
2053                 val = REG_READ(ah, AR7010_GPIO_IN);
2054                 return (MS(val, AR7010_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) == 0;
2055         } else if (AR_SREV_9300_20_OR_LATER(ah))
2056                 return MS_REG_READ(AR9300, gpio) != 0;
2057         else if (AR_SREV_9271(ah))
2058                 return MS_REG_READ(AR9271, gpio) != 0;
2059         else if (AR_SREV_9287_11_OR_LATER(ah))
2060                 return MS_REG_READ(AR9287, gpio) != 0;
2061         else if (AR_SREV_9285_12_OR_LATER(ah))
2062                 return MS_REG_READ(AR9285, gpio) != 0;
2063         else if (AR_SREV_9280_20_OR_LATER(ah))
2064                 return MS_REG_READ(AR928X, gpio) != 0;
2065         else
2066                 return MS_REG_READ(AR, gpio) != 0;
2067 }
2068 EXPORT_SYMBOL(ath9k_hw_gpio_get);
2069
2070 void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
2071                          u32 ah_signal_type)
2072 {
2073         u32 gpio_shift;
2074
2075         if (AR_DEVID_7010(ah)) {
2076                 gpio_shift = gpio;
2077                 REG_RMW(ah, AR7010_GPIO_OE,
2078                         (AR7010_GPIO_OE_AS_OUTPUT << gpio_shift),
2079                         (AR7010_GPIO_OE_MASK << gpio_shift));
2080                 return;
2081         }
2082
2083         ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
2084         gpio_shift = 2 * gpio;
2085         REG_RMW(ah,
2086                 AR_GPIO_OE_OUT,
2087                 (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
2088                 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2089 }
2090 EXPORT_SYMBOL(ath9k_hw_cfg_output);
2091
2092 void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
2093 {
2094         if (AR_DEVID_7010(ah)) {
2095                 val = val ? 0 : 1;
2096                 REG_RMW(ah, AR7010_GPIO_OUT, ((val&1) << gpio),
2097                         AR_GPIO_BIT(gpio));
2098                 return;
2099         }
2100
2101         if (AR_SREV_9271(ah))
2102                 val = ~val;
2103
2104         REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
2105                 AR_GPIO_BIT(gpio));
2106 }
2107 EXPORT_SYMBOL(ath9k_hw_set_gpio);
2108
2109 u32 ath9k_hw_getdefantenna(struct ath_hw *ah)
2110 {
2111         return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
2112 }
2113 EXPORT_SYMBOL(ath9k_hw_getdefantenna);
2114
2115 void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
2116 {
2117         REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
2118 }
2119 EXPORT_SYMBOL(ath9k_hw_setantenna);
2120
2121 /*********************/
2122 /* General Operation */
2123 /*********************/
2124
2125 u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
2126 {
2127         u32 bits = REG_READ(ah, AR_RX_FILTER);
2128         u32 phybits = REG_READ(ah, AR_PHY_ERR);
2129
2130         if (phybits & AR_PHY_ERR_RADAR)
2131                 bits |= ATH9K_RX_FILTER_PHYRADAR;
2132         if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
2133                 bits |= ATH9K_RX_FILTER_PHYERR;
2134
2135         return bits;
2136 }
2137 EXPORT_SYMBOL(ath9k_hw_getrxfilter);
2138
2139 void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
2140 {
2141         u32 phybits;
2142
2143         ENABLE_REGWRITE_BUFFER(ah);
2144
2145         REG_WRITE(ah, AR_RX_FILTER, bits);
2146
2147         phybits = 0;
2148         if (bits & ATH9K_RX_FILTER_PHYRADAR)
2149                 phybits |= AR_PHY_ERR_RADAR;
2150         if (bits & ATH9K_RX_FILTER_PHYERR)
2151                 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
2152         REG_WRITE(ah, AR_PHY_ERR, phybits);
2153
2154         if (phybits)
2155                 REG_WRITE(ah, AR_RXCFG,
2156                           REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
2157         else
2158                 REG_WRITE(ah, AR_RXCFG,
2159                           REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_ZLFDMA);
2160
2161         REGWRITE_BUFFER_FLUSH(ah);
2162 }
2163 EXPORT_SYMBOL(ath9k_hw_setrxfilter);
2164
2165 bool ath9k_hw_phy_disable(struct ath_hw *ah)
2166 {
2167         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
2168                 return false;
2169
2170         ath9k_hw_init_pll(ah, NULL);
2171         return true;
2172 }
2173 EXPORT_SYMBOL(ath9k_hw_phy_disable);
2174
2175 bool ath9k_hw_disable(struct ath_hw *ah)
2176 {
2177         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
2178                 return false;
2179
2180         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
2181                 return false;
2182
2183         ath9k_hw_init_pll(ah, NULL);
2184         return true;
2185 }
2186 EXPORT_SYMBOL(ath9k_hw_disable);
2187
2188 void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit)
2189 {
2190         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
2191         struct ath9k_channel *chan = ah->curchan;
2192         struct ieee80211_channel *channel = chan->chan;
2193
2194         regulatory->power_limit = min(limit, (u32) MAX_RATE_POWER);
2195
2196         ah->eep_ops->set_txpower(ah, chan,
2197                                  ath9k_regd_get_ctl(regulatory, chan),
2198                                  channel->max_antenna_gain * 2,
2199                                  channel->max_power * 2,
2200                                  min((u32) MAX_RATE_POWER,
2201                                  (u32) regulatory->power_limit));
2202 }
2203 EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
2204
2205 void ath9k_hw_setopmode(struct ath_hw *ah)
2206 {
2207         ath9k_hw_set_operating_mode(ah, ah->opmode);
2208 }
2209 EXPORT_SYMBOL(ath9k_hw_setopmode);
2210
2211 void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
2212 {
2213         REG_WRITE(ah, AR_MCAST_FIL0, filter0);
2214         REG_WRITE(ah, AR_MCAST_FIL1, filter1);
2215 }
2216 EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
2217
2218 void ath9k_hw_write_associd(struct ath_hw *ah)
2219 {
2220         struct ath_common *common = ath9k_hw_common(ah);
2221
2222         REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
2223         REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
2224                   ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
2225 }
2226 EXPORT_SYMBOL(ath9k_hw_write_associd);
2227
2228 #define ATH9K_MAX_TSF_READ 10
2229
2230 u64 ath9k_hw_gettsf64(struct ath_hw *ah)
2231 {
2232         u32 tsf_lower, tsf_upper1, tsf_upper2;
2233         int i;
2234
2235         tsf_upper1 = REG_READ(ah, AR_TSF_U32);
2236         for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
2237                 tsf_lower = REG_READ(ah, AR_TSF_L32);
2238                 tsf_upper2 = REG_READ(ah, AR_TSF_U32);
2239                 if (tsf_upper2 == tsf_upper1)
2240                         break;
2241                 tsf_upper1 = tsf_upper2;
2242         }
2243
2244         WARN_ON( i == ATH9K_MAX_TSF_READ );
2245
2246         return (((u64)tsf_upper1 << 32) | tsf_lower);
2247 }
2248 EXPORT_SYMBOL(ath9k_hw_gettsf64);
2249
2250 void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
2251 {
2252         REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
2253         REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
2254 }
2255 EXPORT_SYMBOL(ath9k_hw_settsf64);
2256
2257 void ath9k_hw_reset_tsf(struct ath_hw *ah)
2258 {
2259         if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
2260                            AH_TSF_WRITE_TIMEOUT))
2261                 ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
2262                           "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
2263
2264         REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
2265 }
2266 EXPORT_SYMBOL(ath9k_hw_reset_tsf);
2267
2268 void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
2269 {
2270         if (setting)
2271                 ah->misc_mode |= AR_PCU_TX_ADD_TSF;
2272         else
2273                 ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
2274 }
2275 EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
2276
2277 void ath9k_hw_set11nmac2040(struct ath_hw *ah)
2278 {
2279         struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
2280         u32 macmode;
2281
2282         if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
2283                 macmode = AR_2040_JOINED_RX_CLEAR;
2284         else
2285                 macmode = 0;
2286
2287         REG_WRITE(ah, AR_2040_MODE, macmode);
2288 }
2289
2290 /* HW Generic timers configuration */
2291
2292 static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
2293 {
2294         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2295         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2296         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2297         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2298         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2299         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2300         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2301         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2302         {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
2303         {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
2304                                 AR_NDP2_TIMER_MODE, 0x0002},
2305         {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
2306                                 AR_NDP2_TIMER_MODE, 0x0004},
2307         {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
2308                                 AR_NDP2_TIMER_MODE, 0x0008},
2309         {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
2310                                 AR_NDP2_TIMER_MODE, 0x0010},
2311         {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
2312                                 AR_NDP2_TIMER_MODE, 0x0020},
2313         {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
2314                                 AR_NDP2_TIMER_MODE, 0x0040},
2315         {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
2316                                 AR_NDP2_TIMER_MODE, 0x0080}
2317 };
2318
2319 /* HW generic timer primitives */
2320
2321 /* compute and clear index of rightmost 1 */
2322 static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
2323 {
2324         u32 b;
2325
2326         b = *mask;
2327         b &= (0-b);
2328         *mask &= ~b;
2329         b *= debruijn32;
2330         b >>= 27;
2331
2332         return timer_table->gen_timer_index[b];
2333 }
2334
2335 u32 ath9k_hw_gettsf32(struct ath_hw *ah)
2336 {
2337         return REG_READ(ah, AR_TSF_L32);
2338 }
2339 EXPORT_SYMBOL(ath9k_hw_gettsf32);
2340
2341 struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
2342                                           void (*trigger)(void *),
2343                                           void (*overflow)(void *),
2344                                           void *arg,
2345                                           u8 timer_index)
2346 {
2347         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2348         struct ath_gen_timer *timer;
2349
2350         timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
2351
2352         if (timer == NULL) {
2353                 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
2354                           "Failed to allocate memory"
2355                           "for hw timer[%d]\n", timer_index);
2356                 return NULL;
2357         }
2358
2359         /* allocate a hardware generic timer slot */
2360         timer_table->timers[timer_index] = timer;
2361         timer->index = timer_index;
2362         timer->trigger = trigger;
2363         timer->overflow = overflow;
2364         timer->arg = arg;
2365
2366         return timer;
2367 }
2368 EXPORT_SYMBOL(ath_gen_timer_alloc);
2369
2370 void ath9k_hw_gen_timer_start(struct ath_hw *ah,
2371                               struct ath_gen_timer *timer,
2372                               u32 timer_next,
2373                               u32 timer_period)
2374 {
2375         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2376         u32 tsf;
2377
2378         BUG_ON(!timer_period);
2379
2380         set_bit(timer->index, &timer_table->timer_mask.timer_bits);
2381
2382         tsf = ath9k_hw_gettsf32(ah);
2383
2384         ath_print(ath9k_hw_common(ah), ATH_DBG_HWTIMER,
2385                   "curent tsf %x period %x"
2386                   "timer_next %x\n", tsf, timer_period, timer_next);
2387
2388         /*
2389          * Pull timer_next forward if the current TSF already passed it
2390          * because of software latency
2391          */
2392         if (timer_next < tsf)
2393                 timer_next = tsf + timer_period;
2394
2395         /*
2396          * Program generic timer registers
2397          */
2398         REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
2399                  timer_next);
2400         REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
2401                   timer_period);
2402         REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2403                     gen_tmr_configuration[timer->index].mode_mask);
2404
2405         /* Enable both trigger and thresh interrupt masks */
2406         REG_SET_BIT(ah, AR_IMR_S5,
2407                 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2408                 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2409 }
2410 EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
2411
2412 void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
2413 {
2414         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2415
2416         if ((timer->index < AR_FIRST_NDP_TIMER) ||
2417                 (timer->index >= ATH_MAX_GEN_TIMER)) {
2418                 return;
2419         }
2420
2421         /* Clear generic timer enable bits. */
2422         REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2423                         gen_tmr_configuration[timer->index].mode_mask);
2424
2425         /* Disable both trigger and thresh interrupt masks */
2426         REG_CLR_BIT(ah, AR_IMR_S5,
2427                 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2428                 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2429
2430         clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
2431 }
2432 EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
2433
2434 void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
2435 {
2436         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2437
2438         /* free the hardware generic timer slot */
2439         timer_table->timers[timer->index] = NULL;
2440         kfree(timer);
2441 }
2442 EXPORT_SYMBOL(ath_gen_timer_free);
2443
2444 /*
2445  * Generic Timer Interrupts handling
2446  */
2447 void ath_gen_timer_isr(struct ath_hw *ah)
2448 {
2449         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2450         struct ath_gen_timer *timer;
2451         struct ath_common *common = ath9k_hw_common(ah);
2452         u32 trigger_mask, thresh_mask, index;
2453
2454         /* get hardware generic timer interrupt status */
2455         trigger_mask = ah->intr_gen_timer_trigger;
2456         thresh_mask = ah->intr_gen_timer_thresh;
2457         trigger_mask &= timer_table->timer_mask.val;
2458         thresh_mask &= timer_table->timer_mask.val;
2459
2460         trigger_mask &= ~thresh_mask;
2461
2462         while (thresh_mask) {
2463                 index = rightmost_index(timer_table, &thresh_mask);
2464                 timer = timer_table->timers[index];
2465                 BUG_ON(!timer);
2466                 ath_print(common, ATH_DBG_HWTIMER,
2467                           "TSF overflow for Gen timer %d\n", index);
2468                 timer->overflow(timer->arg);
2469         }
2470
2471         while (trigger_mask) {
2472                 index = rightmost_index(timer_table, &trigger_mask);
2473                 timer = timer_table->timers[index];
2474                 BUG_ON(!timer);
2475                 ath_print(common, ATH_DBG_HWTIMER,
2476                           "Gen timer[%d] trigger\n", index);
2477                 timer->trigger(timer->arg);
2478         }
2479 }
2480 EXPORT_SYMBOL(ath_gen_timer_isr);
2481
2482 /********/
2483 /* HTC  */
2484 /********/
2485
2486 void ath9k_hw_htc_resetinit(struct ath_hw *ah)
2487 {
2488         ah->htc_reset_init = true;
2489 }
2490 EXPORT_SYMBOL(ath9k_hw_htc_resetinit);
2491
2492 static struct {
2493         u32 version;
2494         const char * name;
2495 } ath_mac_bb_names[] = {
2496         /* Devices with external radios */
2497         { AR_SREV_VERSION_5416_PCI,     "5416" },
2498         { AR_SREV_VERSION_5416_PCIE,    "5418" },
2499         { AR_SREV_VERSION_9100,         "9100" },
2500         { AR_SREV_VERSION_9160,         "9160" },
2501         /* Single-chip solutions */
2502         { AR_SREV_VERSION_9280,         "9280" },
2503         { AR_SREV_VERSION_9285,         "9285" },
2504         { AR_SREV_VERSION_9287,         "9287" },
2505         { AR_SREV_VERSION_9271,         "9271" },
2506         { AR_SREV_VERSION_9300,         "9300" },
2507 };
2508
2509 /* For devices with external radios */
2510 static struct {
2511         u16 version;
2512         const char * name;
2513 } ath_rf_names[] = {
2514         { 0,                            "5133" },
2515         { AR_RAD5133_SREV_MAJOR,        "5133" },
2516         { AR_RAD5122_SREV_MAJOR,        "5122" },
2517         { AR_RAD2133_SREV_MAJOR,        "2133" },
2518         { AR_RAD2122_SREV_MAJOR,        "2122" }
2519 };
2520
2521 /*
2522  * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
2523  */
2524 static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
2525 {
2526         int i;
2527
2528         for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
2529                 if (ath_mac_bb_names[i].version == mac_bb_version) {
2530                         return ath_mac_bb_names[i].name;
2531                 }
2532         }
2533
2534         return "????";
2535 }
2536
2537 /*
2538  * Return the RF name. "????" is returned if the RF is unknown.
2539  * Used for devices with external radios.
2540  */
2541 static const char *ath9k_hw_rf_name(u16 rf_version)
2542 {
2543         int i;
2544
2545         for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
2546                 if (ath_rf_names[i].version == rf_version) {
2547                         return ath_rf_names[i].name;
2548                 }
2549         }
2550
2551         return "????";
2552 }
2553
2554 void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
2555 {
2556         int used;
2557
2558         /* chipsets >= AR9280 are single-chip */
2559         if (AR_SREV_9280_20_OR_LATER(ah)) {
2560                 used = snprintf(hw_name, len,
2561                                "Atheros AR%s Rev:%x",
2562                                ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2563                                ah->hw_version.macRev);
2564         }
2565         else {
2566                 used = snprintf(hw_name, len,
2567                                "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
2568                                ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2569                                ah->hw_version.macRev,
2570                                ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
2571                                                 AR_RADIO_SREV_MAJOR)),
2572                                ah->hw_version.phyRev);
2573         }
2574
2575         hw_name[used] = '\0';
2576 }
2577 EXPORT_SYMBOL(ath9k_hw_name);