]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/ath/ath9k/hw.c
ath9k: use ah->get_mac_revision for all SoC devices if available
[linux.git] / drivers / net / wireless / ath / ath9k / hw.c
1 /*
2  * Copyright (c) 2008-2011 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 <linux/module.h>
20 #include <linux/time.h>
21 #include <linux/bitops.h>
22 #include <asm/unaligned.h>
23
24 #include "hw.h"
25 #include "hw-ops.h"
26 #include "ar9003_mac.h"
27 #include "ar9003_mci.h"
28 #include "ar9003_phy.h"
29 #include "ath9k.h"
30
31 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
32
33 MODULE_AUTHOR("Atheros Communications");
34 MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
35 MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
36 MODULE_LICENSE("Dual BSD/GPL");
37
38 static void ath9k_hw_set_clockrate(struct ath_hw *ah)
39 {
40         struct ath_common *common = ath9k_hw_common(ah);
41         struct ath9k_channel *chan = ah->curchan;
42         unsigned int clockrate;
43
44         /* AR9287 v1.3+ uses async FIFO and runs the MAC at 117 MHz */
45         if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah))
46                 clockrate = 117;
47         else if (!chan) /* should really check for CCK instead */
48                 clockrate = ATH9K_CLOCK_RATE_CCK;
49         else if (IS_CHAN_2GHZ(chan))
50                 clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
51         else if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
52                 clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
53         else
54                 clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
55
56         if (chan) {
57                 if (IS_CHAN_HT40(chan))
58                         clockrate *= 2;
59                 if (IS_CHAN_HALF_RATE(chan))
60                         clockrate /= 2;
61                 if (IS_CHAN_QUARTER_RATE(chan))
62                         clockrate /= 4;
63         }
64
65         common->clockrate = clockrate;
66 }
67
68 static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
69 {
70         struct ath_common *common = ath9k_hw_common(ah);
71
72         return usecs * common->clockrate;
73 }
74
75 bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
76 {
77         int i;
78
79         BUG_ON(timeout < AH_TIME_QUANTUM);
80
81         for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
82                 if ((REG_READ(ah, reg) & mask) == val)
83                         return true;
84
85                 udelay(AH_TIME_QUANTUM);
86         }
87
88         ath_dbg(ath9k_hw_common(ah), ANY,
89                 "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
90                 timeout, reg, REG_READ(ah, reg), mask, val);
91
92         return false;
93 }
94 EXPORT_SYMBOL(ath9k_hw_wait);
95
96 void ath9k_hw_synth_delay(struct ath_hw *ah, struct ath9k_channel *chan,
97                           int hw_delay)
98 {
99         hw_delay /= 10;
100
101         if (IS_CHAN_HALF_RATE(chan))
102                 hw_delay *= 2;
103         else if (IS_CHAN_QUARTER_RATE(chan))
104                 hw_delay *= 4;
105
106         udelay(hw_delay + BASE_ACTIVATE_DELAY);
107 }
108
109 void ath9k_hw_write_array(struct ath_hw *ah, const struct ar5416IniArray *array,
110                           int column, unsigned int *writecnt)
111 {
112         int r;
113
114         ENABLE_REGWRITE_BUFFER(ah);
115         for (r = 0; r < array->ia_rows; r++) {
116                 REG_WRITE(ah, INI_RA(array, r, 0),
117                           INI_RA(array, r, column));
118                 DO_DELAY(*writecnt);
119         }
120         REGWRITE_BUFFER_FLUSH(ah);
121 }
122
123 u32 ath9k_hw_reverse_bits(u32 val, u32 n)
124 {
125         u32 retval;
126         int i;
127
128         for (i = 0, retval = 0; i < n; i++) {
129                 retval = (retval << 1) | (val & 1);
130                 val >>= 1;
131         }
132         return retval;
133 }
134
135 u16 ath9k_hw_computetxtime(struct ath_hw *ah,
136                            u8 phy, int kbps,
137                            u32 frameLen, u16 rateix,
138                            bool shortPreamble)
139 {
140         u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
141
142         if (kbps == 0)
143                 return 0;
144
145         switch (phy) {
146         case WLAN_RC_PHY_CCK:
147                 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
148                 if (shortPreamble)
149                         phyTime >>= 1;
150                 numBits = frameLen << 3;
151                 txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
152                 break;
153         case WLAN_RC_PHY_OFDM:
154                 if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
155                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
156                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
157                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
158                         txTime = OFDM_SIFS_TIME_QUARTER
159                                 + OFDM_PREAMBLE_TIME_QUARTER
160                                 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
161                 } else if (ah->curchan &&
162                            IS_CHAN_HALF_RATE(ah->curchan)) {
163                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
164                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
165                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
166                         txTime = OFDM_SIFS_TIME_HALF +
167                                 OFDM_PREAMBLE_TIME_HALF
168                                 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
169                 } else {
170                         bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
171                         numBits = OFDM_PLCP_BITS + (frameLen << 3);
172                         numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
173                         txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
174                                 + (numSymbols * OFDM_SYMBOL_TIME);
175                 }
176                 break;
177         default:
178                 ath_err(ath9k_hw_common(ah),
179                         "Unknown phy %u (rate ix %u)\n", phy, rateix);
180                 txTime = 0;
181                 break;
182         }
183
184         return txTime;
185 }
186 EXPORT_SYMBOL(ath9k_hw_computetxtime);
187
188 void ath9k_hw_get_channel_centers(struct ath_hw *ah,
189                                   struct ath9k_channel *chan,
190                                   struct chan_centers *centers)
191 {
192         int8_t extoff;
193
194         if (!IS_CHAN_HT40(chan)) {
195                 centers->ctl_center = centers->ext_center =
196                         centers->synth_center = chan->channel;
197                 return;
198         }
199
200         if (IS_CHAN_HT40PLUS(chan)) {
201                 centers->synth_center =
202                         chan->channel + HT40_CHANNEL_CENTER_SHIFT;
203                 extoff = 1;
204         } else {
205                 centers->synth_center =
206                         chan->channel - HT40_CHANNEL_CENTER_SHIFT;
207                 extoff = -1;
208         }
209
210         centers->ctl_center =
211                 centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
212         /* 25 MHz spacing is supported by hw but not on upper layers */
213         centers->ext_center =
214                 centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
215 }
216
217 /******************/
218 /* Chip Revisions */
219 /******************/
220
221 static void ath9k_hw_read_revisions(struct ath_hw *ah)
222 {
223         u32 val;
224
225         if (ah->get_mac_revision)
226                 ah->hw_version.macRev = ah->get_mac_revision();
227
228         switch (ah->hw_version.devid) {
229         case AR5416_AR9100_DEVID:
230                 ah->hw_version.macVersion = AR_SREV_VERSION_9100;
231                 break;
232         case AR9300_DEVID_AR9330:
233                 ah->hw_version.macVersion = AR_SREV_VERSION_9330;
234                 if (!ah->get_mac_revision) {
235                         val = REG_READ(ah, AR_SREV);
236                         ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
237                 }
238                 return;
239         case AR9300_DEVID_AR9340:
240                 ah->hw_version.macVersion = AR_SREV_VERSION_9340;
241                 return;
242         case AR9300_DEVID_QCA955X:
243                 ah->hw_version.macVersion = AR_SREV_VERSION_9550;
244                 return;
245         case AR9300_DEVID_AR953X:
246                 ah->hw_version.macVersion = AR_SREV_VERSION_9531;
247                 return;
248         }
249
250         val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
251
252         if (val == 0xFF) {
253                 val = REG_READ(ah, AR_SREV);
254                 ah->hw_version.macVersion =
255                         (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
256                 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
257
258                 if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
259                         ah->is_pciexpress = true;
260                 else
261                         ah->is_pciexpress = (val &
262                                              AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
263         } else {
264                 if (!AR_SREV_9100(ah))
265                         ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
266
267                 ah->hw_version.macRev = val & AR_SREV_REVISION;
268
269                 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
270                         ah->is_pciexpress = true;
271         }
272 }
273
274 /************************************/
275 /* HW Attach, Detach, Init Routines */
276 /************************************/
277
278 static void ath9k_hw_disablepcie(struct ath_hw *ah)
279 {
280         if (!AR_SREV_5416(ah))
281                 return;
282
283         REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
284         REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
285         REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
286         REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
287         REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
288         REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
289         REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
290         REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
291         REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
292
293         REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
294 }
295
296 /* This should work for all families including legacy */
297 static bool ath9k_hw_chip_test(struct ath_hw *ah)
298 {
299         struct ath_common *common = ath9k_hw_common(ah);
300         u32 regAddr[2] = { AR_STA_ID0 };
301         u32 regHold[2];
302         static const u32 patternData[4] = {
303                 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999
304         };
305         int i, j, loop_max;
306
307         if (!AR_SREV_9300_20_OR_LATER(ah)) {
308                 loop_max = 2;
309                 regAddr[1] = AR_PHY_BASE + (8 << 2);
310         } else
311                 loop_max = 1;
312
313         for (i = 0; i < loop_max; i++) {
314                 u32 addr = regAddr[i];
315                 u32 wrData, rdData;
316
317                 regHold[i] = REG_READ(ah, addr);
318                 for (j = 0; j < 0x100; j++) {
319                         wrData = (j << 16) | j;
320                         REG_WRITE(ah, addr, wrData);
321                         rdData = REG_READ(ah, addr);
322                         if (rdData != wrData) {
323                                 ath_err(common,
324                                         "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
325                                         addr, wrData, rdData);
326                                 return false;
327                         }
328                 }
329                 for (j = 0; j < 4; j++) {
330                         wrData = patternData[j];
331                         REG_WRITE(ah, addr, wrData);
332                         rdData = REG_READ(ah, addr);
333                         if (wrData != rdData) {
334                                 ath_err(common,
335                                         "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
336                                         addr, wrData, rdData);
337                                 return false;
338                         }
339                 }
340                 REG_WRITE(ah, regAddr[i], regHold[i]);
341         }
342         udelay(100);
343
344         return true;
345 }
346
347 static void ath9k_hw_init_config(struct ath_hw *ah)
348 {
349         struct ath_common *common = ath9k_hw_common(ah);
350
351         ah->config.dma_beacon_response_time = 1;
352         ah->config.sw_beacon_response_time = 6;
353         ah->config.cwm_ignore_extcca = 0;
354         ah->config.analog_shiftreg = 1;
355
356         ah->config.rx_intr_mitigation = true;
357
358         if (AR_SREV_9300_20_OR_LATER(ah)) {
359                 ah->config.rimt_last = 500;
360                 ah->config.rimt_first = 2000;
361         } else {
362                 ah->config.rimt_last = 250;
363                 ah->config.rimt_first = 700;
364         }
365
366         /*
367          * We need this for PCI devices only (Cardbus, PCI, miniPCI)
368          * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
369          * This means we use it for all AR5416 devices, and the few
370          * minor PCI AR9280 devices out there.
371          *
372          * Serialization is required because these devices do not handle
373          * well the case of two concurrent reads/writes due to the latency
374          * involved. During one read/write another read/write can be issued
375          * on another CPU while the previous read/write may still be working
376          * on our hardware, if we hit this case the hardware poops in a loop.
377          * We prevent this by serializing reads and writes.
378          *
379          * This issue is not present on PCI-Express devices or pre-AR5416
380          * devices (legacy, 802.11abg).
381          */
382         if (num_possible_cpus() > 1)
383                 ah->config.serialize_regmode = SER_REG_MODE_AUTO;
384
385         if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
386                 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
387                     ((AR_SREV_9160(ah) || AR_SREV_9280(ah) || AR_SREV_9287(ah)) &&
388                      !ah->is_pciexpress)) {
389                         ah->config.serialize_regmode = SER_REG_MODE_ON;
390                 } else {
391                         ah->config.serialize_regmode = SER_REG_MODE_OFF;
392                 }
393         }
394
395         ath_dbg(common, RESET, "serialize_regmode is %d\n",
396                 ah->config.serialize_regmode);
397
398         if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
399                 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
400         else
401                 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
402 }
403
404 static void ath9k_hw_init_defaults(struct ath_hw *ah)
405 {
406         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
407
408         regulatory->country_code = CTRY_DEFAULT;
409         regulatory->power_limit = MAX_RATE_POWER;
410
411         ah->hw_version.magic = AR5416_MAGIC;
412         ah->hw_version.subvendorid = 0;
413
414         ah->sta_id1_defaults = AR_STA_ID1_CRPT_MIC_ENABLE |
415                                AR_STA_ID1_MCAST_KSRCH;
416         if (AR_SREV_9100(ah))
417                 ah->sta_id1_defaults |= AR_STA_ID1_AR9100_BA_FIX;
418
419         ah->slottime = ATH9K_SLOT_TIME_9;
420         ah->globaltxtimeout = (u32) -1;
421         ah->power_mode = ATH9K_PM_UNDEFINED;
422         ah->htc_reset_init = true;
423
424         ah->ani_function = ATH9K_ANI_ALL;
425         if (!AR_SREV_9300_20_OR_LATER(ah))
426                 ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
427
428         if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
429                 ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
430         else
431                 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
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         static const 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         struct ath_common *common = ath9k_hw_common(ah);
458         int ecode;
459
460         if (common->bus_ops->ath_bus_type != ATH_USB) {
461                 if (!ath9k_hw_chip_test(ah))
462                         return -ENODEV;
463         }
464
465         if (!AR_SREV_9300_20_OR_LATER(ah)) {
466                 ecode = ar9002_hw_rf_claim(ah);
467                 if (ecode != 0)
468                         return ecode;
469         }
470
471         ecode = ath9k_hw_eeprom_init(ah);
472         if (ecode != 0)
473                 return ecode;
474
475         ath_dbg(ath9k_hw_common(ah), CONFIG, "Eeprom VER: %d, REV: %d\n",
476                 ah->eep_ops->get_eeprom_ver(ah),
477                 ah->eep_ops->get_eeprom_rev(ah));
478
479         ath9k_hw_ani_init(ah);
480
481         /*
482          * EEPROM needs to be initialized before we do this.
483          * This is required for regulatory compliance.
484          */
485         if (AR_SREV_9300_20_OR_LATER(ah)) {
486                 u16 regdmn = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
487                 if ((regdmn & 0xF0) == CTL_FCC) {
488                         ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_FCC_2GHZ;
489                         ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_FCC_5GHZ;
490                 }
491         }
492
493         return 0;
494 }
495
496 static int ath9k_hw_attach_ops(struct ath_hw *ah)
497 {
498         if (!AR_SREV_9300_20_OR_LATER(ah))
499                 return ar9002_hw_attach_ops(ah);
500
501         ar9003_hw_attach_ops(ah);
502         return 0;
503 }
504
505 /* Called for all hardware families */
506 static int __ath9k_hw_init(struct ath_hw *ah)
507 {
508         struct ath_common *common = ath9k_hw_common(ah);
509         int r = 0;
510
511         ath9k_hw_read_revisions(ah);
512
513         switch (ah->hw_version.macVersion) {
514         case AR_SREV_VERSION_5416_PCI:
515         case AR_SREV_VERSION_5416_PCIE:
516         case AR_SREV_VERSION_9160:
517         case AR_SREV_VERSION_9100:
518         case AR_SREV_VERSION_9280:
519         case AR_SREV_VERSION_9285:
520         case AR_SREV_VERSION_9287:
521         case AR_SREV_VERSION_9271:
522         case AR_SREV_VERSION_9300:
523         case AR_SREV_VERSION_9330:
524         case AR_SREV_VERSION_9485:
525         case AR_SREV_VERSION_9340:
526         case AR_SREV_VERSION_9462:
527         case AR_SREV_VERSION_9550:
528         case AR_SREV_VERSION_9565:
529         case AR_SREV_VERSION_9531:
530                 break;
531         default:
532                 ath_err(common,
533                         "Mac Chip Rev 0x%02x.%x is not supported by this driver\n",
534                         ah->hw_version.macVersion, ah->hw_version.macRev);
535                 return -EOPNOTSUPP;
536         }
537
538         /*
539          * Read back AR_WA into a permanent copy and set bits 14 and 17.
540          * We need to do this to avoid RMW of this register. We cannot
541          * read the reg when chip is asleep.
542          */
543         if (AR_SREV_9300_20_OR_LATER(ah)) {
544                 ah->WARegVal = REG_READ(ah, AR_WA);
545                 ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
546                                  AR_WA_ASPM_TIMER_BASED_DISABLE);
547         }
548
549         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
550                 ath_err(common, "Couldn't reset chip\n");
551                 return -EIO;
552         }
553
554         if (AR_SREV_9565(ah)) {
555                 ah->WARegVal |= AR_WA_BIT22;
556                 REG_WRITE(ah, AR_WA, ah->WARegVal);
557         }
558
559         ath9k_hw_init_defaults(ah);
560         ath9k_hw_init_config(ah);
561
562         r = ath9k_hw_attach_ops(ah);
563         if (r)
564                 return r;
565
566         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
567                 ath_err(common, "Couldn't wakeup chip\n");
568                 return -EIO;
569         }
570
571         if (AR_SREV_9271(ah) || AR_SREV_9100(ah) || AR_SREV_9340(ah) ||
572             AR_SREV_9330(ah) || AR_SREV_9550(ah))
573                 ah->is_pciexpress = false;
574
575         ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
576         ath9k_hw_init_cal_settings(ah);
577
578         if (!ah->is_pciexpress)
579                 ath9k_hw_disablepcie(ah);
580
581         r = ath9k_hw_post_init(ah);
582         if (r)
583                 return r;
584
585         ath9k_hw_init_mode_gain_regs(ah);
586         r = ath9k_hw_fill_cap_info(ah);
587         if (r)
588                 return r;
589
590         r = ath9k_hw_init_macaddr(ah);
591         if (r) {
592                 ath_err(common, "Failed to initialize MAC address\n");
593                 return r;
594         }
595
596         ath9k_hw_init_hang_checks(ah);
597
598         common->state = ATH_HW_INITIALIZED;
599
600         return 0;
601 }
602
603 int ath9k_hw_init(struct ath_hw *ah)
604 {
605         int ret;
606         struct ath_common *common = ath9k_hw_common(ah);
607
608         /* These are all the AR5008/AR9001/AR9002/AR9003 hardware family of chipsets */
609         switch (ah->hw_version.devid) {
610         case AR5416_DEVID_PCI:
611         case AR5416_DEVID_PCIE:
612         case AR5416_AR9100_DEVID:
613         case AR9160_DEVID_PCI:
614         case AR9280_DEVID_PCI:
615         case AR9280_DEVID_PCIE:
616         case AR9285_DEVID_PCIE:
617         case AR9287_DEVID_PCI:
618         case AR9287_DEVID_PCIE:
619         case AR2427_DEVID_PCIE:
620         case AR9300_DEVID_PCIE:
621         case AR9300_DEVID_AR9485_PCIE:
622         case AR9300_DEVID_AR9330:
623         case AR9300_DEVID_AR9340:
624         case AR9300_DEVID_QCA955X:
625         case AR9300_DEVID_AR9580:
626         case AR9300_DEVID_AR9462:
627         case AR9485_DEVID_AR1111:
628         case AR9300_DEVID_AR9565:
629         case AR9300_DEVID_AR953X:
630                 break;
631         default:
632                 if (common->bus_ops->ath_bus_type == ATH_USB)
633                         break;
634                 ath_err(common, "Hardware device ID 0x%04x not supported\n",
635                         ah->hw_version.devid);
636                 return -EOPNOTSUPP;
637         }
638
639         ret = __ath9k_hw_init(ah);
640         if (ret) {
641                 ath_err(common,
642                         "Unable to initialize hardware; initialization status: %d\n",
643                         ret);
644                 return ret;
645         }
646
647         ath_dynack_init(ah);
648
649         return 0;
650 }
651 EXPORT_SYMBOL(ath9k_hw_init);
652
653 static void ath9k_hw_init_qos(struct ath_hw *ah)
654 {
655         ENABLE_REGWRITE_BUFFER(ah);
656
657         REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
658         REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
659
660         REG_WRITE(ah, AR_QOS_NO_ACK,
661                   SM(2, AR_QOS_NO_ACK_TWO_BIT) |
662                   SM(5, AR_QOS_NO_ACK_BIT_OFF) |
663                   SM(0, AR_QOS_NO_ACK_BYTE_OFF));
664
665         REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
666         REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
667         REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
668         REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
669         REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
670
671         REGWRITE_BUFFER_FLUSH(ah);
672 }
673
674 u32 ar9003_get_pll_sqsum_dvc(struct ath_hw *ah)
675 {
676         struct ath_common *common = ath9k_hw_common(ah);
677         int i = 0;
678
679         REG_CLR_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
680         udelay(100);
681         REG_SET_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
682
683         while ((REG_READ(ah, PLL4) & PLL4_MEAS_DONE) == 0) {
684
685                 udelay(100);
686
687                 if (WARN_ON_ONCE(i >= 100)) {
688                         ath_err(common, "PLL4 meaurement not done\n");
689                         break;
690                 }
691
692                 i++;
693         }
694
695         return (REG_READ(ah, PLL3) & SQSUM_DVC_MASK) >> 3;
696 }
697 EXPORT_SYMBOL(ar9003_get_pll_sqsum_dvc);
698
699 static void ath9k_hw_init_pll(struct ath_hw *ah,
700                               struct ath9k_channel *chan)
701 {
702         u32 pll;
703
704         if (AR_SREV_9485(ah) || AR_SREV_9565(ah)) {
705                 /* program BB PLL ki and kd value, ki=0x4, kd=0x40 */
706                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
707                               AR_CH0_BB_DPLL2_PLL_PWD, 0x1);
708                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
709                               AR_CH0_DPLL2_KD, 0x40);
710                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
711                               AR_CH0_DPLL2_KI, 0x4);
712
713                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
714                               AR_CH0_BB_DPLL1_REFDIV, 0x5);
715                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
716                               AR_CH0_BB_DPLL1_NINI, 0x58);
717                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
718                               AR_CH0_BB_DPLL1_NFRAC, 0x0);
719
720                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
721                               AR_CH0_BB_DPLL2_OUTDIV, 0x1);
722                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
723                               AR_CH0_BB_DPLL2_LOCAL_PLL, 0x1);
724                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
725                               AR_CH0_BB_DPLL2_EN_NEGTRIG, 0x1);
726
727                 /* program BB PLL phase_shift to 0x6 */
728                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
729                               AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x6);
730
731                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
732                               AR_CH0_BB_DPLL2_PLL_PWD, 0x0);
733                 udelay(1000);
734         } else if (AR_SREV_9330(ah)) {
735                 u32 ddr_dpll2, pll_control2, kd;
736
737                 if (ah->is_clk_25mhz) {
738                         ddr_dpll2 = 0x18e82f01;
739                         pll_control2 = 0xe04a3d;
740                         kd = 0x1d;
741                 } else {
742                         ddr_dpll2 = 0x19e82f01;
743                         pll_control2 = 0x886666;
744                         kd = 0x3d;
745                 }
746
747                 /* program DDR PLL ki and kd value */
748                 REG_WRITE(ah, AR_CH0_DDR_DPLL2, ddr_dpll2);
749
750                 /* program DDR PLL phase_shift */
751                 REG_RMW_FIELD(ah, AR_CH0_DDR_DPLL3,
752                               AR_CH0_DPLL3_PHASE_SHIFT, 0x1);
753
754                 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
755                 udelay(1000);
756
757                 /* program refdiv, nint, frac to RTC register */
758                 REG_WRITE(ah, AR_RTC_PLL_CONTROL2, pll_control2);
759
760                 /* program BB PLL kd and ki value */
761                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KD, kd);
762                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KI, 0x06);
763
764                 /* program BB PLL phase_shift */
765                 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
766                               AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x1);
767         } else if (AR_SREV_9340(ah) || AR_SREV_9550(ah) || AR_SREV_9531(ah)) {
768                 u32 regval, pll2_divint, pll2_divfrac, refdiv;
769
770                 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
771                 udelay(1000);
772
773                 REG_SET_BIT(ah, AR_PHY_PLL_MODE, 0x1 << 16);
774                 udelay(100);
775
776                 if (ah->is_clk_25mhz) {
777                         if (AR_SREV_9531(ah)) {
778                                 pll2_divint = 0x1c;
779                                 pll2_divfrac = 0xa3d2;
780                                 refdiv = 1;
781                         } else {
782                                 pll2_divint = 0x54;
783                                 pll2_divfrac = 0x1eb85;
784                                 refdiv = 3;
785                         }
786                 } else {
787                         if (AR_SREV_9340(ah)) {
788                                 pll2_divint = 88;
789                                 pll2_divfrac = 0;
790                                 refdiv = 5;
791                         } else {
792                                 pll2_divint = 0x11;
793                                 pll2_divfrac =
794                                         AR_SREV_9531(ah) ? 0x26665 : 0x26666;
795                                 refdiv = 1;
796                         }
797                 }
798
799                 regval = REG_READ(ah, AR_PHY_PLL_MODE);
800                 if (AR_SREV_9531(ah))
801                         regval |= (0x1 << 22);
802                 else
803                         regval |= (0x1 << 16);
804                 REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
805                 udelay(100);
806
807                 REG_WRITE(ah, AR_PHY_PLL_CONTROL, (refdiv << 27) |
808                           (pll2_divint << 18) | pll2_divfrac);
809                 udelay(100);
810
811                 regval = REG_READ(ah, AR_PHY_PLL_MODE);
812                 if (AR_SREV_9340(ah))
813                         regval = (regval & 0x80071fff) |
814                                 (0x1 << 30) |
815                                 (0x1 << 13) |
816                                 (0x4 << 26) |
817                                 (0x18 << 19);
818                 else if (AR_SREV_9531(ah))
819                         regval = (regval & 0x01c00fff) |
820                                 (0x1 << 31) |
821                                 (0x2 << 29) |
822                                 (0xa << 25) |
823                                 (0x1 << 19) |
824                                 (0x6 << 12);
825                 else
826                         regval = (regval & 0x80071fff) |
827                                 (0x3 << 30) |
828                                 (0x1 << 13) |
829                                 (0x4 << 26) |
830                                 (0x60 << 19);
831                 REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
832
833                 if (AR_SREV_9531(ah))
834                         REG_WRITE(ah, AR_PHY_PLL_MODE,
835                                   REG_READ(ah, AR_PHY_PLL_MODE) & 0xffbfffff);
836                 else
837                         REG_WRITE(ah, AR_PHY_PLL_MODE,
838                                   REG_READ(ah, AR_PHY_PLL_MODE) & 0xfffeffff);
839
840                 udelay(1000);
841         }
842
843         pll = ath9k_hw_compute_pll_control(ah, chan);
844         if (AR_SREV_9565(ah))
845                 pll |= 0x40000;
846         REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
847
848         if (AR_SREV_9485(ah) || AR_SREV_9340(ah) || AR_SREV_9330(ah) ||
849             AR_SREV_9550(ah))
850                 udelay(1000);
851
852         /* Switch the core clock for ar9271 to 117Mhz */
853         if (AR_SREV_9271(ah)) {
854                 udelay(500);
855                 REG_WRITE(ah, 0x50040, 0x304);
856         }
857
858         udelay(RTC_PLL_SETTLE_DELAY);
859
860         REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
861
862         if (AR_SREV_9340(ah) || AR_SREV_9550(ah)) {
863                 if (ah->is_clk_25mhz) {
864                         REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x17c << 1);
865                         REG_WRITE(ah, AR_SLP32_MODE, 0x0010f3d7);
866                         REG_WRITE(ah,  AR_SLP32_INC, 0x0001e7ae);
867                 } else {
868                         REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x261 << 1);
869                         REG_WRITE(ah, AR_SLP32_MODE, 0x0010f400);
870                         REG_WRITE(ah,  AR_SLP32_INC, 0x0001e800);
871                 }
872                 udelay(100);
873         }
874 }
875
876 static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
877                                           enum nl80211_iftype opmode)
878 {
879         u32 sync_default = AR_INTR_SYNC_DEFAULT;
880         u32 imr_reg = AR_IMR_TXERR |
881                 AR_IMR_TXURN |
882                 AR_IMR_RXERR |
883                 AR_IMR_RXORN |
884                 AR_IMR_BCNMISC;
885
886         if (AR_SREV_9340(ah) || AR_SREV_9550(ah) || AR_SREV_9531(ah))
887                 sync_default &= ~AR_INTR_SYNC_HOST1_FATAL;
888
889         if (AR_SREV_9300_20_OR_LATER(ah)) {
890                 imr_reg |= AR_IMR_RXOK_HP;
891                 if (ah->config.rx_intr_mitigation)
892                         imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
893                 else
894                         imr_reg |= AR_IMR_RXOK_LP;
895
896         } else {
897                 if (ah->config.rx_intr_mitigation)
898                         imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
899                 else
900                         imr_reg |= AR_IMR_RXOK;
901         }
902
903         if (ah->config.tx_intr_mitigation)
904                 imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
905         else
906                 imr_reg |= AR_IMR_TXOK;
907
908         ENABLE_REGWRITE_BUFFER(ah);
909
910         REG_WRITE(ah, AR_IMR, imr_reg);
911         ah->imrs2_reg |= AR_IMR_S2_GTT;
912         REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
913
914         if (!AR_SREV_9100(ah)) {
915                 REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
916                 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, sync_default);
917                 REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
918         }
919
920         REGWRITE_BUFFER_FLUSH(ah);
921
922         if (AR_SREV_9300_20_OR_LATER(ah)) {
923                 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
924                 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
925                 REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
926                 REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
927         }
928 }
929
930 static void ath9k_hw_set_sifs_time(struct ath_hw *ah, u32 us)
931 {
932         u32 val = ath9k_hw_mac_to_clks(ah, us - 2);
933         val = min(val, (u32) 0xFFFF);
934         REG_WRITE(ah, AR_D_GBL_IFS_SIFS, val);
935 }
936
937 void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
938 {
939         u32 val = ath9k_hw_mac_to_clks(ah, us);
940         val = min(val, (u32) 0xFFFF);
941         REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
942 }
943
944 void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
945 {
946         u32 val = ath9k_hw_mac_to_clks(ah, us);
947         val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
948         REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
949 }
950
951 void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
952 {
953         u32 val = ath9k_hw_mac_to_clks(ah, us);
954         val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
955         REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
956 }
957
958 static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
959 {
960         if (tu > 0xFFFF) {
961                 ath_dbg(ath9k_hw_common(ah), XMIT, "bad global tx timeout %u\n",
962                         tu);
963                 ah->globaltxtimeout = (u32) -1;
964                 return false;
965         } else {
966                 REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
967                 ah->globaltxtimeout = tu;
968                 return true;
969         }
970 }
971
972 void ath9k_hw_init_global_settings(struct ath_hw *ah)
973 {
974         struct ath_common *common = ath9k_hw_common(ah);
975         const struct ath9k_channel *chan = ah->curchan;
976         int acktimeout, ctstimeout, ack_offset = 0;
977         int slottime;
978         int sifstime;
979         int rx_lat = 0, tx_lat = 0, eifs = 0;
980         u32 reg;
981
982         ath_dbg(ath9k_hw_common(ah), RESET, "ah->misc_mode 0x%x\n",
983                 ah->misc_mode);
984
985         if (!chan)
986                 return;
987
988         if (ah->misc_mode != 0)
989                 REG_SET_BIT(ah, AR_PCU_MISC, ah->misc_mode);
990
991         if (IS_CHAN_A_FAST_CLOCK(ah, chan))
992                 rx_lat = 41;
993         else
994                 rx_lat = 37;
995         tx_lat = 54;
996
997         if (IS_CHAN_5GHZ(chan))
998                 sifstime = 16;
999         else
1000                 sifstime = 10;
1001
1002         if (IS_CHAN_HALF_RATE(chan)) {
1003                 eifs = 175;
1004                 rx_lat *= 2;
1005                 tx_lat *= 2;
1006                 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
1007                     tx_lat += 11;
1008
1009                 sifstime = 32;
1010                 ack_offset = 16;
1011                 slottime = 13;
1012         } else if (IS_CHAN_QUARTER_RATE(chan)) {
1013                 eifs = 340;
1014                 rx_lat = (rx_lat * 4) - 1;
1015                 tx_lat *= 4;
1016                 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
1017                     tx_lat += 22;
1018
1019                 sifstime = 64;
1020                 ack_offset = 32;
1021                 slottime = 21;
1022         } else {
1023                 if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
1024                         eifs = AR_D_GBL_IFS_EIFS_ASYNC_FIFO;
1025                         reg = AR_USEC_ASYNC_FIFO;
1026                 } else {
1027                         eifs = REG_READ(ah, AR_D_GBL_IFS_EIFS)/
1028                                 common->clockrate;
1029                         reg = REG_READ(ah, AR_USEC);
1030                 }
1031                 rx_lat = MS(reg, AR_USEC_RX_LAT);
1032                 tx_lat = MS(reg, AR_USEC_TX_LAT);
1033
1034                 slottime = ah->slottime;
1035         }
1036
1037         /* As defined by IEEE 802.11-2007 17.3.8.6 */
1038         slottime += 3 * ah->coverage_class;
1039         acktimeout = slottime + sifstime + ack_offset;
1040         ctstimeout = acktimeout;
1041
1042         /*
1043          * Workaround for early ACK timeouts, add an offset to match the
1044          * initval's 64us ack timeout value. Use 48us for the CTS timeout.
1045          * This was initially only meant to work around an issue with delayed
1046          * BA frames in some implementations, but it has been found to fix ACK
1047          * timeout issues in other cases as well.
1048          */
1049         if (IS_CHAN_2GHZ(chan) &&
1050             !IS_CHAN_HALF_RATE(chan) && !IS_CHAN_QUARTER_RATE(chan)) {
1051                 acktimeout += 64 - sifstime - ah->slottime;
1052                 ctstimeout += 48 - sifstime - ah->slottime;
1053         }
1054
1055         if (ah->dynack.enabled) {
1056                 acktimeout = ah->dynack.ackto;
1057                 ctstimeout = acktimeout;
1058                 slottime = (acktimeout - 3) / 2;
1059         } else {
1060                 ah->dynack.ackto = acktimeout;
1061         }
1062
1063         ath9k_hw_set_sifs_time(ah, sifstime);
1064         ath9k_hw_setslottime(ah, slottime);
1065         ath9k_hw_set_ack_timeout(ah, acktimeout);
1066         ath9k_hw_set_cts_timeout(ah, ctstimeout);
1067         if (ah->globaltxtimeout != (u32) -1)
1068                 ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
1069
1070         REG_WRITE(ah, AR_D_GBL_IFS_EIFS, ath9k_hw_mac_to_clks(ah, eifs));
1071         REG_RMW(ah, AR_USEC,
1072                 (common->clockrate - 1) |
1073                 SM(rx_lat, AR_USEC_RX_LAT) |
1074                 SM(tx_lat, AR_USEC_TX_LAT),
1075                 AR_USEC_TX_LAT | AR_USEC_RX_LAT | AR_USEC_USEC);
1076
1077 }
1078 EXPORT_SYMBOL(ath9k_hw_init_global_settings);
1079
1080 void ath9k_hw_deinit(struct ath_hw *ah)
1081 {
1082         struct ath_common *common = ath9k_hw_common(ah);
1083
1084         if (common->state < ATH_HW_INITIALIZED)
1085                 return;
1086
1087         ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
1088 }
1089 EXPORT_SYMBOL(ath9k_hw_deinit);
1090
1091 /*******/
1092 /* INI */
1093 /*******/
1094
1095 u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
1096 {
1097         u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
1098
1099         if (IS_CHAN_2GHZ(chan))
1100                 ctl |= CTL_11G;
1101         else
1102                 ctl |= CTL_11A;
1103
1104         return ctl;
1105 }
1106
1107 /****************************************/
1108 /* Reset and Channel Switching Routines */
1109 /****************************************/
1110
1111 static inline void ath9k_hw_set_dma(struct ath_hw *ah)
1112 {
1113         struct ath_common *common = ath9k_hw_common(ah);
1114         int txbuf_size;
1115
1116         ENABLE_REGWRITE_BUFFER(ah);
1117
1118         /*
1119          * set AHB_MODE not to do cacheline prefetches
1120         */
1121         if (!AR_SREV_9300_20_OR_LATER(ah))
1122                 REG_SET_BIT(ah, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
1123
1124         /*
1125          * let mac dma reads be in 128 byte chunks
1126          */
1127         REG_RMW(ah, AR_TXCFG, AR_TXCFG_DMASZ_128B, AR_TXCFG_DMASZ_MASK);
1128
1129         REGWRITE_BUFFER_FLUSH(ah);
1130
1131         /*
1132          * Restore TX Trigger Level to its pre-reset value.
1133          * The initial value depends on whether aggregation is enabled, and is
1134          * adjusted whenever underruns are detected.
1135          */
1136         if (!AR_SREV_9300_20_OR_LATER(ah))
1137                 REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
1138
1139         ENABLE_REGWRITE_BUFFER(ah);
1140
1141         /*
1142          * let mac dma writes be in 128 byte chunks
1143          */
1144         REG_RMW(ah, AR_RXCFG, AR_RXCFG_DMASZ_128B, AR_RXCFG_DMASZ_MASK);
1145
1146         /*
1147          * Setup receive FIFO threshold to hold off TX activities
1148          */
1149         REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
1150
1151         if (AR_SREV_9300_20_OR_LATER(ah)) {
1152                 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
1153                 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
1154
1155                 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
1156                         ah->caps.rx_status_len);
1157         }
1158
1159         /*
1160          * reduce the number of usable entries in PCU TXBUF to avoid
1161          * wrap around issues.
1162          */
1163         if (AR_SREV_9285(ah)) {
1164                 /* For AR9285 the number of Fifos are reduced to half.
1165                  * So set the usable tx buf size also to half to
1166                  * avoid data/delimiter underruns
1167                  */
1168                 txbuf_size = AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE;
1169         } else if (AR_SREV_9340_13_OR_LATER(ah)) {
1170                 /* Uses fewer entries for AR934x v1.3+ to prevent rx overruns */
1171                 txbuf_size = AR_9340_PCU_TXBUF_CTRL_USABLE_SIZE;
1172         } else {
1173                 txbuf_size = AR_PCU_TXBUF_CTRL_USABLE_SIZE;
1174         }
1175
1176         if (!AR_SREV_9271(ah))
1177                 REG_WRITE(ah, AR_PCU_TXBUF_CTRL, txbuf_size);
1178
1179         REGWRITE_BUFFER_FLUSH(ah);
1180
1181         if (AR_SREV_9300_20_OR_LATER(ah))
1182                 ath9k_hw_reset_txstatus_ring(ah);
1183 }
1184
1185 static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
1186 {
1187         u32 mask = AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC;
1188         u32 set = AR_STA_ID1_KSRCH_MODE;
1189
1190         switch (opmode) {
1191         case NL80211_IFTYPE_ADHOC:
1192                 set |= AR_STA_ID1_ADHOC;
1193                 REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
1194                 break;
1195         case NL80211_IFTYPE_MESH_POINT:
1196         case NL80211_IFTYPE_AP:
1197                 set |= AR_STA_ID1_STA_AP;
1198                 /* fall through */
1199         case NL80211_IFTYPE_STATION:
1200                 REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
1201                 break;
1202         default:
1203                 if (!ah->is_monitoring)
1204                         set = 0;
1205                 break;
1206         }
1207         REG_RMW(ah, AR_STA_ID1, set, mask);
1208 }
1209
1210 void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
1211                                    u32 *coef_mantissa, u32 *coef_exponent)
1212 {
1213         u32 coef_exp, coef_man;
1214
1215         for (coef_exp = 31; coef_exp > 0; coef_exp--)
1216                 if ((coef_scaled >> coef_exp) & 0x1)
1217                         break;
1218
1219         coef_exp = 14 - (coef_exp - COEF_SCALE_S);
1220
1221         coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
1222
1223         *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
1224         *coef_exponent = coef_exp - 16;
1225 }
1226
1227 /* AR9330 WAR:
1228  * call external reset function to reset WMAC if:
1229  * - doing a cold reset
1230  * - we have pending frames in the TX queues.
1231  */
1232 static bool ath9k_hw_ar9330_reset_war(struct ath_hw *ah, int type)
1233 {
1234         int i, npend = 0;
1235
1236         for (i = 0; i < AR_NUM_QCU; i++) {
1237                 npend = ath9k_hw_numtxpending(ah, i);
1238                 if (npend)
1239                         break;
1240         }
1241
1242         if (ah->external_reset &&
1243             (npend || type == ATH9K_RESET_COLD)) {
1244                 int reset_err = 0;
1245
1246                 ath_dbg(ath9k_hw_common(ah), RESET,
1247                         "reset MAC via external reset\n");
1248
1249                 reset_err = ah->external_reset();
1250                 if (reset_err) {
1251                         ath_err(ath9k_hw_common(ah),
1252                                 "External reset failed, err=%d\n",
1253                                 reset_err);
1254                         return false;
1255                 }
1256
1257                 REG_WRITE(ah, AR_RTC_RESET, 1);
1258         }
1259
1260         return true;
1261 }
1262
1263 static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
1264 {
1265         u32 rst_flags;
1266         u32 tmpReg;
1267
1268         if (AR_SREV_9100(ah)) {
1269                 REG_RMW_FIELD(ah, AR_RTC_DERIVED_CLK,
1270                               AR_RTC_DERIVED_CLK_PERIOD, 1);
1271                 (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
1272         }
1273
1274         ENABLE_REGWRITE_BUFFER(ah);
1275
1276         if (AR_SREV_9300_20_OR_LATER(ah)) {
1277                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1278                 udelay(10);
1279         }
1280
1281         REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1282                   AR_RTC_FORCE_WAKE_ON_INT);
1283
1284         if (AR_SREV_9100(ah)) {
1285                 rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
1286                         AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
1287         } else {
1288                 tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
1289                 if (AR_SREV_9340(ah))
1290                         tmpReg &= AR9340_INTR_SYNC_LOCAL_TIMEOUT;
1291                 else
1292                         tmpReg &= AR_INTR_SYNC_LOCAL_TIMEOUT |
1293                                   AR_INTR_SYNC_RADM_CPL_TIMEOUT;
1294
1295                 if (tmpReg) {
1296                         u32 val;
1297                         REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
1298
1299                         val = AR_RC_HOSTIF;
1300                         if (!AR_SREV_9300_20_OR_LATER(ah))
1301                                 val |= AR_RC_AHB;
1302                         REG_WRITE(ah, AR_RC, val);
1303
1304                 } else if (!AR_SREV_9300_20_OR_LATER(ah))
1305                         REG_WRITE(ah, AR_RC, AR_RC_AHB);
1306
1307                 rst_flags = AR_RTC_RC_MAC_WARM;
1308                 if (type == ATH9K_RESET_COLD)
1309                         rst_flags |= AR_RTC_RC_MAC_COLD;
1310         }
1311
1312         if (AR_SREV_9330(ah)) {
1313                 if (!ath9k_hw_ar9330_reset_war(ah, type))
1314                         return false;
1315         }
1316
1317         if (ath9k_hw_mci_is_enabled(ah))
1318                 ar9003_mci_check_gpm_offset(ah);
1319
1320         REG_WRITE(ah, AR_RTC_RC, rst_flags);
1321
1322         REGWRITE_BUFFER_FLUSH(ah);
1323
1324         if (AR_SREV_9300_20_OR_LATER(ah))
1325                 udelay(50);
1326         else if (AR_SREV_9100(ah))
1327                 mdelay(10);
1328         else
1329                 udelay(100);
1330
1331         REG_WRITE(ah, AR_RTC_RC, 0);
1332         if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
1333                 ath_dbg(ath9k_hw_common(ah), RESET, "RTC stuck in MAC reset\n");
1334                 return false;
1335         }
1336
1337         if (!AR_SREV_9100(ah))
1338                 REG_WRITE(ah, AR_RC, 0);
1339
1340         if (AR_SREV_9100(ah))
1341                 udelay(50);
1342
1343         return true;
1344 }
1345
1346 static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
1347 {
1348         ENABLE_REGWRITE_BUFFER(ah);
1349
1350         if (AR_SREV_9300_20_OR_LATER(ah)) {
1351                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1352                 udelay(10);
1353         }
1354
1355         REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1356                   AR_RTC_FORCE_WAKE_ON_INT);
1357
1358         if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1359                 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1360
1361         REG_WRITE(ah, AR_RTC_RESET, 0);
1362
1363         REGWRITE_BUFFER_FLUSH(ah);
1364
1365         udelay(2);
1366
1367         if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
1368                 REG_WRITE(ah, AR_RC, 0);
1369
1370         REG_WRITE(ah, AR_RTC_RESET, 1);
1371
1372         if (!ath9k_hw_wait(ah,
1373                            AR_RTC_STATUS,
1374                            AR_RTC_STATUS_M,
1375                            AR_RTC_STATUS_ON,
1376                            AH_WAIT_TIMEOUT)) {
1377                 ath_dbg(ath9k_hw_common(ah), RESET, "RTC not waking up\n");
1378                 return false;
1379         }
1380
1381         return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
1382 }
1383
1384 static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
1385 {
1386         bool ret = false;
1387
1388         if (AR_SREV_9300_20_OR_LATER(ah)) {
1389                 REG_WRITE(ah, AR_WA, ah->WARegVal);
1390                 udelay(10);
1391         }
1392
1393         REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1394                   AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
1395
1396         if (!ah->reset_power_on)
1397                 type = ATH9K_RESET_POWER_ON;
1398
1399         switch (type) {
1400         case ATH9K_RESET_POWER_ON:
1401                 ret = ath9k_hw_set_reset_power_on(ah);
1402                 if (ret)
1403                         ah->reset_power_on = true;
1404                 break;
1405         case ATH9K_RESET_WARM:
1406         case ATH9K_RESET_COLD:
1407                 ret = ath9k_hw_set_reset(ah, type);
1408                 break;
1409         default:
1410                 break;
1411         }
1412
1413         return ret;
1414 }
1415
1416 static bool ath9k_hw_chip_reset(struct ath_hw *ah,
1417                                 struct ath9k_channel *chan)
1418 {
1419         int reset_type = ATH9K_RESET_WARM;
1420
1421         if (AR_SREV_9280(ah)) {
1422                 if (ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
1423                         reset_type = ATH9K_RESET_POWER_ON;
1424                 else
1425                         reset_type = ATH9K_RESET_COLD;
1426         } else if (ah->chip_fullsleep || REG_READ(ah, AR_Q_TXE) ||
1427                    (REG_READ(ah, AR_CR) & AR_CR_RXE))
1428                 reset_type = ATH9K_RESET_COLD;
1429
1430         if (!ath9k_hw_set_reset_reg(ah, reset_type))
1431                 return false;
1432
1433         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1434                 return false;
1435
1436         ah->chip_fullsleep = false;
1437
1438         if (AR_SREV_9330(ah))
1439                 ar9003_hw_internal_regulator_apply(ah);
1440         ath9k_hw_init_pll(ah, chan);
1441
1442         return true;
1443 }
1444
1445 static bool ath9k_hw_channel_change(struct ath_hw *ah,
1446                                     struct ath9k_channel *chan)
1447 {
1448         struct ath_common *common = ath9k_hw_common(ah);
1449         struct ath9k_hw_capabilities *pCap = &ah->caps;
1450         bool band_switch = false, mode_diff = false;
1451         u8 ini_reloaded = 0;
1452         u32 qnum;
1453         int r;
1454
1455         if (pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH) {
1456                 u32 flags_diff = chan->channelFlags ^ ah->curchan->channelFlags;
1457                 band_switch = !!(flags_diff & CHANNEL_5GHZ);
1458                 mode_diff = !!(flags_diff & ~CHANNEL_HT);
1459         }
1460
1461         for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
1462                 if (ath9k_hw_numtxpending(ah, qnum)) {
1463                         ath_dbg(common, QUEUE,
1464                                 "Transmit frames pending on queue %d\n", qnum);
1465                         return false;
1466                 }
1467         }
1468
1469         if (!ath9k_hw_rfbus_req(ah)) {
1470                 ath_err(common, "Could not kill baseband RX\n");
1471                 return false;
1472         }
1473
1474         if (band_switch || mode_diff) {
1475                 ath9k_hw_mark_phy_inactive(ah);
1476                 udelay(5);
1477
1478                 if (band_switch)
1479                         ath9k_hw_init_pll(ah, chan);
1480
1481                 if (ath9k_hw_fast_chan_change(ah, chan, &ini_reloaded)) {
1482                         ath_err(common, "Failed to do fast channel change\n");
1483                         return false;
1484                 }
1485         }
1486
1487         ath9k_hw_set_channel_regs(ah, chan);
1488
1489         r = ath9k_hw_rf_set_freq(ah, chan);
1490         if (r) {
1491                 ath_err(common, "Failed to set channel\n");
1492                 return false;
1493         }
1494         ath9k_hw_set_clockrate(ah);
1495         ath9k_hw_apply_txpower(ah, chan, false);
1496
1497         ath9k_hw_set_delta_slope(ah, chan);
1498         ath9k_hw_spur_mitigate_freq(ah, chan);
1499
1500         if (band_switch || ini_reloaded)
1501                 ah->eep_ops->set_board_values(ah, chan);
1502
1503         ath9k_hw_init_bb(ah, chan);
1504         ath9k_hw_rfbus_done(ah);
1505
1506         if (band_switch || ini_reloaded) {
1507                 ah->ah_flags |= AH_FASTCC;
1508                 ath9k_hw_init_cal(ah, chan);
1509                 ah->ah_flags &= ~AH_FASTCC;
1510         }
1511
1512         return true;
1513 }
1514
1515 static void ath9k_hw_apply_gpio_override(struct ath_hw *ah)
1516 {
1517         u32 gpio_mask = ah->gpio_mask;
1518         int i;
1519
1520         for (i = 0; gpio_mask; i++, gpio_mask >>= 1) {
1521                 if (!(gpio_mask & 1))
1522                         continue;
1523
1524                 ath9k_hw_cfg_output(ah, i, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1525                 ath9k_hw_set_gpio(ah, i, !!(ah->gpio_val & BIT(i)));
1526         }
1527 }
1528
1529 void ath9k_hw_check_nav(struct ath_hw *ah)
1530 {
1531         struct ath_common *common = ath9k_hw_common(ah);
1532         u32 val;
1533
1534         val = REG_READ(ah, AR_NAV);
1535         if (val != 0xdeadbeef && val > 0x7fff) {
1536                 ath_dbg(common, BSTUCK, "Abnormal NAV: 0x%x\n", val);
1537                 REG_WRITE(ah, AR_NAV, 0);
1538         }
1539 }
1540 EXPORT_SYMBOL(ath9k_hw_check_nav);
1541
1542 bool ath9k_hw_check_alive(struct ath_hw *ah)
1543 {
1544         int count = 50;
1545         u32 reg, last_val;
1546
1547         if (AR_SREV_9300(ah))
1548                 return !ath9k_hw_detect_mac_hang(ah);
1549
1550         if (AR_SREV_9285_12_OR_LATER(ah))
1551                 return true;
1552
1553         last_val = REG_READ(ah, AR_OBS_BUS_1);
1554         do {
1555                 reg = REG_READ(ah, AR_OBS_BUS_1);
1556                 if (reg != last_val)
1557                         return true;
1558
1559                 udelay(1);
1560                 last_val = reg;
1561                 if ((reg & 0x7E7FFFEF) == 0x00702400)
1562                         continue;
1563
1564                 switch (reg & 0x7E000B00) {
1565                 case 0x1E000000:
1566                 case 0x52000B00:
1567                 case 0x18000B00:
1568                         continue;
1569                 default:
1570                         return true;
1571                 }
1572         } while (count-- > 0);
1573
1574         return false;
1575 }
1576 EXPORT_SYMBOL(ath9k_hw_check_alive);
1577
1578 static void ath9k_hw_init_mfp(struct ath_hw *ah)
1579 {
1580         /* Setup MFP options for CCMP */
1581         if (AR_SREV_9280_20_OR_LATER(ah)) {
1582                 /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
1583                  * frames when constructing CCMP AAD. */
1584                 REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
1585                               0xc7ff);
1586                 ah->sw_mgmt_crypto = false;
1587         } else if (AR_SREV_9160_10_OR_LATER(ah)) {
1588                 /* Disable hardware crypto for management frames */
1589                 REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
1590                             AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
1591                 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1592                             AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
1593                 ah->sw_mgmt_crypto = true;
1594         } else {
1595                 ah->sw_mgmt_crypto = true;
1596         }
1597 }
1598
1599 static void ath9k_hw_reset_opmode(struct ath_hw *ah,
1600                                   u32 macStaId1, u32 saveDefAntenna)
1601 {
1602         struct ath_common *common = ath9k_hw_common(ah);
1603
1604         ENABLE_REGWRITE_BUFFER(ah);
1605
1606         REG_RMW(ah, AR_STA_ID1, macStaId1
1607                   | AR_STA_ID1_RTS_USE_DEF
1608                   | ah->sta_id1_defaults,
1609                   ~AR_STA_ID1_SADH_MASK);
1610         ath_hw_setbssidmask(common);
1611         REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
1612         ath9k_hw_write_associd(ah);
1613         REG_WRITE(ah, AR_ISR, ~0);
1614         REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
1615
1616         REGWRITE_BUFFER_FLUSH(ah);
1617
1618         ath9k_hw_set_operating_mode(ah, ah->opmode);
1619 }
1620
1621 static void ath9k_hw_init_queues(struct ath_hw *ah)
1622 {
1623         int i;
1624
1625         ENABLE_REGWRITE_BUFFER(ah);
1626
1627         for (i = 0; i < AR_NUM_DCU; i++)
1628                 REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
1629
1630         REGWRITE_BUFFER_FLUSH(ah);
1631
1632         ah->intr_txqs = 0;
1633         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1634                 ath9k_hw_resettxqueue(ah, i);
1635 }
1636
1637 /*
1638  * For big endian systems turn on swapping for descriptors
1639  */
1640 static void ath9k_hw_init_desc(struct ath_hw *ah)
1641 {
1642         struct ath_common *common = ath9k_hw_common(ah);
1643
1644         if (AR_SREV_9100(ah)) {
1645                 u32 mask;
1646                 mask = REG_READ(ah, AR_CFG);
1647                 if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
1648                         ath_dbg(common, RESET, "CFG Byte Swap Set 0x%x\n",
1649                                 mask);
1650                 } else {
1651                         mask = INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
1652                         REG_WRITE(ah, AR_CFG, mask);
1653                         ath_dbg(common, RESET, "Setting CFG 0x%x\n",
1654                                 REG_READ(ah, AR_CFG));
1655                 }
1656         } else {
1657                 if (common->bus_ops->ath_bus_type == ATH_USB) {
1658                         /* Configure AR9271 target WLAN */
1659                         if (AR_SREV_9271(ah))
1660                                 REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
1661                         else
1662                                 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1663                 }
1664 #ifdef __BIG_ENDIAN
1665                 else if (AR_SREV_9330(ah) || AR_SREV_9340(ah) ||
1666                          AR_SREV_9550(ah) || AR_SREV_9531(ah))
1667                         REG_RMW(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB, 0);
1668                 else
1669                         REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1670 #endif
1671         }
1672 }
1673
1674 /*
1675  * Fast channel change:
1676  * (Change synthesizer based on channel freq without resetting chip)
1677  */
1678 static int ath9k_hw_do_fastcc(struct ath_hw *ah, struct ath9k_channel *chan)
1679 {
1680         struct ath_common *common = ath9k_hw_common(ah);
1681         struct ath9k_hw_capabilities *pCap = &ah->caps;
1682         int ret;
1683
1684         if (AR_SREV_9280(ah) && common->bus_ops->ath_bus_type == ATH_PCI)
1685                 goto fail;
1686
1687         if (ah->chip_fullsleep)
1688                 goto fail;
1689
1690         if (!ah->curchan)
1691                 goto fail;
1692
1693         if (chan->channel == ah->curchan->channel)
1694                 goto fail;
1695
1696         if ((ah->curchan->channelFlags | chan->channelFlags) &
1697             (CHANNEL_HALF | CHANNEL_QUARTER))
1698                 goto fail;
1699
1700         /*
1701          * If cross-band fcc is not supoprted, bail out if channelFlags differ.
1702          */
1703         if (!(pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH) &&
1704             ((chan->channelFlags ^ ah->curchan->channelFlags) & ~CHANNEL_HT))
1705                 goto fail;
1706
1707         if (!ath9k_hw_check_alive(ah))
1708                 goto fail;
1709
1710         /*
1711          * For AR9462, make sure that calibration data for
1712          * re-using are present.
1713          */
1714         if (AR_SREV_9462(ah) && (ah->caldata &&
1715                                  (!test_bit(TXIQCAL_DONE, &ah->caldata->cal_flags) ||
1716                                   !test_bit(TXCLCAL_DONE, &ah->caldata->cal_flags) ||
1717                                   !test_bit(RTT_DONE, &ah->caldata->cal_flags))))
1718                 goto fail;
1719
1720         ath_dbg(common, RESET, "FastChannelChange for %d -> %d\n",
1721                 ah->curchan->channel, chan->channel);
1722
1723         ret = ath9k_hw_channel_change(ah, chan);
1724         if (!ret)
1725                 goto fail;
1726
1727         if (ath9k_hw_mci_is_enabled(ah))
1728                 ar9003_mci_2g5g_switch(ah, false);
1729
1730         ath9k_hw_loadnf(ah, ah->curchan);
1731         ath9k_hw_start_nfcal(ah, true);
1732
1733         if (AR_SREV_9271(ah))
1734                 ar9002_hw_load_ani_reg(ah, chan);
1735
1736         return 0;
1737 fail:
1738         return -EINVAL;
1739 }
1740
1741 u32 ath9k_hw_get_tsf_offset(struct timespec *last, struct timespec *cur)
1742 {
1743         struct timespec ts;
1744         s64 usec;
1745
1746         if (!cur) {
1747                 getrawmonotonic(&ts);
1748                 cur = &ts;
1749         }
1750
1751         usec = cur->tv_sec * 1000000ULL + cur->tv_nsec / 1000;
1752         usec -= last->tv_sec * 1000000ULL + last->tv_nsec / 1000;
1753
1754         return (u32) usec;
1755 }
1756 EXPORT_SYMBOL(ath9k_hw_get_tsf_offset);
1757
1758 int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
1759                    struct ath9k_hw_cal_data *caldata, bool fastcc)
1760 {
1761         struct ath_common *common = ath9k_hw_common(ah);
1762         u32 saveLedState;
1763         u32 saveDefAntenna;
1764         u32 macStaId1;
1765         u64 tsf = 0;
1766         s64 usec = 0;
1767         int r;
1768         bool start_mci_reset = false;
1769         bool save_fullsleep = ah->chip_fullsleep;
1770
1771         if (ath9k_hw_mci_is_enabled(ah)) {
1772                 start_mci_reset = ar9003_mci_start_reset(ah, chan);
1773                 if (start_mci_reset)
1774                         return 0;
1775         }
1776
1777         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
1778                 return -EIO;
1779
1780         if (ah->curchan && !ah->chip_fullsleep)
1781                 ath9k_hw_getnf(ah, ah->curchan);
1782
1783         ah->caldata = caldata;
1784         if (caldata && (chan->channel != caldata->channel ||
1785                         chan->channelFlags != caldata->channelFlags)) {
1786                 /* Operating channel changed, reset channel calibration data */
1787                 memset(caldata, 0, sizeof(*caldata));
1788                 ath9k_init_nfcal_hist_buffer(ah, chan);
1789         } else if (caldata) {
1790                 clear_bit(PAPRD_PACKET_SENT, &caldata->cal_flags);
1791         }
1792         ah->noise = ath9k_hw_getchan_noise(ah, chan, chan->noisefloor);
1793
1794         if (fastcc) {
1795                 r = ath9k_hw_do_fastcc(ah, chan);
1796                 if (!r)
1797                         return r;
1798         }
1799
1800         if (ath9k_hw_mci_is_enabled(ah))
1801                 ar9003_mci_stop_bt(ah, save_fullsleep);
1802
1803         saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
1804         if (saveDefAntenna == 0)
1805                 saveDefAntenna = 1;
1806
1807         macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
1808
1809         /* Save TSF before chip reset, a cold reset clears it */
1810         tsf = ath9k_hw_gettsf64(ah);
1811         usec = ktime_to_us(ktime_get_raw());
1812
1813         saveLedState = REG_READ(ah, AR_CFG_LED) &
1814                 (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
1815                  AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
1816
1817         ath9k_hw_mark_phy_inactive(ah);
1818
1819         ah->paprd_table_write_done = false;
1820
1821         /* Only required on the first reset */
1822         if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1823                 REG_WRITE(ah,
1824                           AR9271_RESET_POWER_DOWN_CONTROL,
1825                           AR9271_RADIO_RF_RST);
1826                 udelay(50);
1827         }
1828
1829         if (!ath9k_hw_chip_reset(ah, chan)) {
1830                 ath_err(common, "Chip reset failed\n");
1831                 return -EINVAL;
1832         }
1833
1834         /* Only required on the first reset */
1835         if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1836                 ah->htc_reset_init = false;
1837                 REG_WRITE(ah,
1838                           AR9271_RESET_POWER_DOWN_CONTROL,
1839                           AR9271_GATE_MAC_CTL);
1840                 udelay(50);
1841         }
1842
1843         /* Restore TSF */
1844         usec = ktime_to_us(ktime_get_raw()) - usec;
1845         ath9k_hw_settsf64(ah, tsf + usec);
1846
1847         if (AR_SREV_9280_20_OR_LATER(ah))
1848                 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
1849
1850         if (!AR_SREV_9300_20_OR_LATER(ah))
1851                 ar9002_hw_enable_async_fifo(ah);
1852
1853         r = ath9k_hw_process_ini(ah, chan);
1854         if (r)
1855                 return r;
1856
1857         ath9k_hw_set_rfmode(ah, chan);
1858
1859         if (ath9k_hw_mci_is_enabled(ah))
1860                 ar9003_mci_reset(ah, false, IS_CHAN_2GHZ(chan), save_fullsleep);
1861
1862         /*
1863          * Some AR91xx SoC devices frequently fail to accept TSF writes
1864          * right after the chip reset. When that happens, write a new
1865          * value after the initvals have been applied, with an offset
1866          * based on measured time difference
1867          */
1868         if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
1869                 tsf += 1500;
1870                 ath9k_hw_settsf64(ah, tsf);
1871         }
1872
1873         ath9k_hw_init_mfp(ah);
1874
1875         ath9k_hw_set_delta_slope(ah, chan);
1876         ath9k_hw_spur_mitigate_freq(ah, chan);
1877         ah->eep_ops->set_board_values(ah, chan);
1878
1879         ath9k_hw_reset_opmode(ah, macStaId1, saveDefAntenna);
1880
1881         r = ath9k_hw_rf_set_freq(ah, chan);
1882         if (r)
1883                 return r;
1884
1885         ath9k_hw_set_clockrate(ah);
1886
1887         ath9k_hw_init_queues(ah);
1888         ath9k_hw_init_interrupt_masks(ah, ah->opmode);
1889         ath9k_hw_ani_cache_ini_regs(ah);
1890         ath9k_hw_init_qos(ah);
1891
1892         if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1893                 ath9k_hw_cfg_gpio_input(ah, ah->rfkill_gpio);
1894
1895         ath9k_hw_init_global_settings(ah);
1896
1897         if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
1898                 REG_SET_BIT(ah, AR_MAC_PCU_LOGIC_ANALYZER,
1899                             AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
1900                 REG_RMW_FIELD(ah, AR_AHB_MODE, AR_AHB_CUSTOM_BURST_EN,
1901                               AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
1902                 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1903                             AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
1904         }
1905
1906         REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
1907
1908         ath9k_hw_set_dma(ah);
1909
1910         if (!ath9k_hw_mci_is_enabled(ah))
1911                 REG_WRITE(ah, AR_OBS, 8);
1912
1913         if (ah->config.rx_intr_mitigation) {
1914                 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, ah->config.rimt_last);
1915                 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, ah->config.rimt_first);
1916         }
1917
1918         if (ah->config.tx_intr_mitigation) {
1919                 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
1920                 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
1921         }
1922
1923         ath9k_hw_init_bb(ah, chan);
1924
1925         if (caldata) {
1926                 clear_bit(TXIQCAL_DONE, &caldata->cal_flags);
1927                 clear_bit(TXCLCAL_DONE, &caldata->cal_flags);
1928         }
1929         if (!ath9k_hw_init_cal(ah, chan))
1930                 return -EIO;
1931
1932         if (ath9k_hw_mci_is_enabled(ah) && ar9003_mci_end_reset(ah, chan, caldata))
1933                 return -EIO;
1934
1935         ENABLE_REGWRITE_BUFFER(ah);
1936
1937         ath9k_hw_restore_chainmask(ah);
1938         REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
1939
1940         REGWRITE_BUFFER_FLUSH(ah);
1941
1942         ath9k_hw_init_desc(ah);
1943
1944         if (ath9k_hw_btcoex_is_enabled(ah))
1945                 ath9k_hw_btcoex_enable(ah);
1946
1947         if (ath9k_hw_mci_is_enabled(ah))
1948                 ar9003_mci_check_bt(ah);
1949
1950         ath9k_hw_loadnf(ah, chan);
1951         ath9k_hw_start_nfcal(ah, true);
1952
1953         if (AR_SREV_9300_20_OR_LATER(ah))
1954                 ar9003_hw_bb_watchdog_config(ah);
1955
1956         if (ah->config.hw_hang_checks & HW_PHYRESTART_CLC_WAR)
1957                 ar9003_hw_disable_phy_restart(ah);
1958
1959         ath9k_hw_apply_gpio_override(ah);
1960
1961         if (AR_SREV_9565(ah) && common->bt_ant_diversity)
1962                 REG_SET_BIT(ah, AR_BTCOEX_WL_LNADIV, AR_BTCOEX_WL_LNADIV_FORCE_ON);
1963
1964         if (ah->hw->conf.radar_enabled) {
1965                 /* set HW specific DFS configuration */
1966                 ah->radar_conf.ext_channel = IS_CHAN_HT40(chan);
1967                 ath9k_hw_set_radar_params(ah);
1968         }
1969
1970         return 0;
1971 }
1972 EXPORT_SYMBOL(ath9k_hw_reset);
1973
1974 /******************************/
1975 /* Power Management (Chipset) */
1976 /******************************/
1977
1978 /*
1979  * Notify Power Mgt is disabled in self-generated frames.
1980  * If requested, force chip to sleep.
1981  */
1982 static void ath9k_set_power_sleep(struct ath_hw *ah)
1983 {
1984         REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1985
1986         if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
1987                 REG_CLR_BIT(ah, AR_TIMER_MODE, 0xff);
1988                 REG_CLR_BIT(ah, AR_NDP2_TIMER_MODE, 0xff);
1989                 REG_CLR_BIT(ah, AR_SLP32_INC, 0xfffff);
1990                 /* xxx Required for WLAN only case ? */
1991                 REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
1992                 udelay(100);
1993         }
1994
1995         /*
1996          * Clear the RTC force wake bit to allow the
1997          * mac to go to sleep.
1998          */
1999         REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
2000
2001         if (ath9k_hw_mci_is_enabled(ah))
2002                 udelay(100);
2003
2004         if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
2005                 REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
2006
2007         /* Shutdown chip. Active low */
2008         if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah)) {
2009                 REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
2010                 udelay(2);
2011         }
2012
2013         /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
2014         if (AR_SREV_9300_20_OR_LATER(ah))
2015                 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
2016 }
2017
2018 /*
2019  * Notify Power Management is enabled in self-generating
2020  * frames. If request, set power mode of chip to
2021  * auto/normal.  Duration in units of 128us (1/8 TU).
2022  */
2023 static void ath9k_set_power_network_sleep(struct ath_hw *ah)
2024 {
2025         struct ath9k_hw_capabilities *pCap = &ah->caps;
2026
2027         REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
2028
2029         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
2030                 /* Set WakeOnInterrupt bit; clear ForceWake bit */
2031                 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
2032                           AR_RTC_FORCE_WAKE_ON_INT);
2033         } else {
2034
2035                 /* When chip goes into network sleep, it could be waken
2036                  * up by MCI_INT interrupt caused by BT's HW messages
2037                  * (LNA_xxx, CONT_xxx) which chould be in a very fast
2038                  * rate (~100us). This will cause chip to leave and
2039                  * re-enter network sleep mode frequently, which in
2040                  * consequence will have WLAN MCI HW to generate lots of
2041                  * SYS_WAKING and SYS_SLEEPING messages which will make
2042                  * BT CPU to busy to process.
2043                  */
2044                 if (ath9k_hw_mci_is_enabled(ah))
2045                         REG_CLR_BIT(ah, AR_MCI_INTERRUPT_RX_MSG_EN,
2046                                     AR_MCI_INTERRUPT_RX_HW_MSG_MASK);
2047                 /*
2048                  * Clear the RTC force wake bit to allow the
2049                  * mac to go to sleep.
2050                  */
2051                 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
2052
2053                 if (ath9k_hw_mci_is_enabled(ah))
2054                         udelay(30);
2055         }
2056
2057         /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
2058         if (AR_SREV_9300_20_OR_LATER(ah))
2059                 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
2060 }
2061
2062 static bool ath9k_hw_set_power_awake(struct ath_hw *ah)
2063 {
2064         u32 val;
2065         int i;
2066
2067         /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
2068         if (AR_SREV_9300_20_OR_LATER(ah)) {
2069                 REG_WRITE(ah, AR_WA, ah->WARegVal);
2070                 udelay(10);
2071         }
2072
2073         if ((REG_READ(ah, AR_RTC_STATUS) &
2074              AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
2075                 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
2076                         return false;
2077                 }
2078                 if (!AR_SREV_9300_20_OR_LATER(ah))
2079                         ath9k_hw_init_pll(ah, NULL);
2080         }
2081         if (AR_SREV_9100(ah))
2082                 REG_SET_BIT(ah, AR_RTC_RESET,
2083                             AR_RTC_RESET_EN);
2084
2085         REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
2086                     AR_RTC_FORCE_WAKE_EN);
2087         if (AR_SREV_9100(ah))
2088                 mdelay(10);
2089         else
2090                 udelay(50);
2091
2092         for (i = POWER_UP_TIME / 50; i > 0; i--) {
2093                 val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
2094                 if (val == AR_RTC_STATUS_ON)
2095                         break;
2096                 udelay(50);
2097                 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
2098                             AR_RTC_FORCE_WAKE_EN);
2099         }
2100         if (i == 0) {
2101                 ath_err(ath9k_hw_common(ah),
2102                         "Failed to wakeup in %uus\n",
2103                         POWER_UP_TIME / 20);
2104                 return false;
2105         }
2106
2107         if (ath9k_hw_mci_is_enabled(ah))
2108                 ar9003_mci_set_power_awake(ah);
2109
2110         REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
2111
2112         return true;
2113 }
2114
2115 bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
2116 {
2117         struct ath_common *common = ath9k_hw_common(ah);
2118         int status = true;
2119         static const char *modes[] = {
2120                 "AWAKE",
2121                 "FULL-SLEEP",
2122                 "NETWORK SLEEP",
2123                 "UNDEFINED"
2124         };
2125
2126         if (ah->power_mode == mode)
2127                 return status;
2128
2129         ath_dbg(common, RESET, "%s -> %s\n",
2130                 modes[ah->power_mode], modes[mode]);
2131
2132         switch (mode) {
2133         case ATH9K_PM_AWAKE:
2134                 status = ath9k_hw_set_power_awake(ah);
2135                 break;
2136         case ATH9K_PM_FULL_SLEEP:
2137                 if (ath9k_hw_mci_is_enabled(ah))
2138                         ar9003_mci_set_full_sleep(ah);
2139
2140                 ath9k_set_power_sleep(ah);
2141                 ah->chip_fullsleep = true;
2142                 break;
2143         case ATH9K_PM_NETWORK_SLEEP:
2144                 ath9k_set_power_network_sleep(ah);
2145                 break;
2146         default:
2147                 ath_err(common, "Unknown power mode %u\n", mode);
2148                 return false;
2149         }
2150         ah->power_mode = mode;
2151
2152         /*
2153          * XXX: If this warning never comes up after a while then
2154          * simply keep the ATH_DBG_WARN_ON_ONCE() but make
2155          * ath9k_hw_setpower() return type void.
2156          */
2157
2158         if (!(ah->ah_flags & AH_UNPLUGGED))
2159                 ATH_DBG_WARN_ON_ONCE(!status);
2160
2161         return status;
2162 }
2163 EXPORT_SYMBOL(ath9k_hw_setpower);
2164
2165 /*******************/
2166 /* Beacon Handling */
2167 /*******************/
2168
2169 void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
2170 {
2171         int flags = 0;
2172
2173         ENABLE_REGWRITE_BUFFER(ah);
2174
2175         switch (ah->opmode) {
2176         case NL80211_IFTYPE_ADHOC:
2177                 REG_SET_BIT(ah, AR_TXCFG,
2178                             AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
2179         case NL80211_IFTYPE_MESH_POINT:
2180         case NL80211_IFTYPE_AP:
2181                 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, next_beacon);
2182                 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, next_beacon -
2183                           TU_TO_USEC(ah->config.dma_beacon_response_time));
2184                 REG_WRITE(ah, AR_NEXT_SWBA, next_beacon -
2185                           TU_TO_USEC(ah->config.sw_beacon_response_time));
2186                 flags |=
2187                         AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
2188                 break;
2189         default:
2190                 ath_dbg(ath9k_hw_common(ah), BEACON,
2191                         "%s: unsupported opmode: %d\n", __func__, ah->opmode);
2192                 return;
2193                 break;
2194         }
2195
2196         REG_WRITE(ah, AR_BEACON_PERIOD, beacon_period);
2197         REG_WRITE(ah, AR_DMA_BEACON_PERIOD, beacon_period);
2198         REG_WRITE(ah, AR_SWBA_PERIOD, beacon_period);
2199
2200         REGWRITE_BUFFER_FLUSH(ah);
2201
2202         REG_SET_BIT(ah, AR_TIMER_MODE, flags);
2203 }
2204 EXPORT_SYMBOL(ath9k_hw_beaconinit);
2205
2206 void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
2207                                     const struct ath9k_beacon_state *bs)
2208 {
2209         u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
2210         struct ath9k_hw_capabilities *pCap = &ah->caps;
2211         struct ath_common *common = ath9k_hw_common(ah);
2212
2213         ENABLE_REGWRITE_BUFFER(ah);
2214
2215         REG_WRITE(ah, AR_NEXT_TBTT_TIMER, bs->bs_nexttbtt);
2216         REG_WRITE(ah, AR_BEACON_PERIOD, bs->bs_intval);
2217         REG_WRITE(ah, AR_DMA_BEACON_PERIOD, bs->bs_intval);
2218
2219         REGWRITE_BUFFER_FLUSH(ah);
2220
2221         REG_RMW_FIELD(ah, AR_RSSI_THR,
2222                       AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
2223
2224         beaconintval = bs->bs_intval;
2225
2226         if (bs->bs_sleepduration > beaconintval)
2227                 beaconintval = bs->bs_sleepduration;
2228
2229         dtimperiod = bs->bs_dtimperiod;
2230         if (bs->bs_sleepduration > dtimperiod)
2231                 dtimperiod = bs->bs_sleepduration;
2232
2233         if (beaconintval == dtimperiod)
2234                 nextTbtt = bs->bs_nextdtim;
2235         else
2236                 nextTbtt = bs->bs_nexttbtt;
2237
2238         ath_dbg(common, BEACON, "next DTIM %d\n", bs->bs_nextdtim);
2239         ath_dbg(common, BEACON, "next beacon %d\n", nextTbtt);
2240         ath_dbg(common, BEACON, "beacon period %d\n", beaconintval);
2241         ath_dbg(common, BEACON, "DTIM period %d\n", dtimperiod);
2242
2243         ENABLE_REGWRITE_BUFFER(ah);
2244
2245         REG_WRITE(ah, AR_NEXT_DTIM, bs->bs_nextdtim - SLEEP_SLOP);
2246         REG_WRITE(ah, AR_NEXT_TIM, nextTbtt - SLEEP_SLOP);
2247
2248         REG_WRITE(ah, AR_SLEEP1,
2249                   SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
2250                   | AR_SLEEP1_ASSUME_DTIM);
2251
2252         if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
2253                 beacontimeout = (BEACON_TIMEOUT_VAL << 3);
2254         else
2255                 beacontimeout = MIN_BEACON_TIMEOUT_VAL;
2256
2257         REG_WRITE(ah, AR_SLEEP2,
2258                   SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
2259
2260         REG_WRITE(ah, AR_TIM_PERIOD, beaconintval);
2261         REG_WRITE(ah, AR_DTIM_PERIOD, dtimperiod);
2262
2263         REGWRITE_BUFFER_FLUSH(ah);
2264
2265         REG_SET_BIT(ah, AR_TIMER_MODE,
2266                     AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
2267                     AR_DTIM_TIMER_EN);
2268
2269         /* TSF Out of Range Threshold */
2270         REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
2271 }
2272 EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
2273
2274 /*******************/
2275 /* HW Capabilities */
2276 /*******************/
2277
2278 static u8 fixup_chainmask(u8 chip_chainmask, u8 eeprom_chainmask)
2279 {
2280         eeprom_chainmask &= chip_chainmask;
2281         if (eeprom_chainmask)
2282                 return eeprom_chainmask;
2283         else
2284                 return chip_chainmask;
2285 }
2286
2287 /**
2288  * ath9k_hw_dfs_tested - checks if DFS has been tested with used chipset
2289  * @ah: the atheros hardware data structure
2290  *
2291  * We enable DFS support upstream on chipsets which have passed a series
2292  * of tests. The testing requirements are going to be documented. Desired
2293  * test requirements are documented at:
2294  *
2295  * http://wireless.kernel.org/en/users/Drivers/ath9k/dfs
2296  *
2297  * Once a new chipset gets properly tested an individual commit can be used
2298  * to document the testing for DFS for that chipset.
2299  */
2300 static bool ath9k_hw_dfs_tested(struct ath_hw *ah)
2301 {
2302
2303         switch (ah->hw_version.macVersion) {
2304         /* for temporary testing DFS with 9280 */
2305         case AR_SREV_VERSION_9280:
2306         /* AR9580 will likely be our first target to get testing on */
2307         case AR_SREV_VERSION_9580:
2308                 return true;
2309         default:
2310                 return false;
2311         }
2312 }
2313
2314 int ath9k_hw_fill_cap_info(struct ath_hw *ah)
2315 {
2316         struct ath9k_hw_capabilities *pCap = &ah->caps;
2317         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
2318         struct ath_common *common = ath9k_hw_common(ah);
2319         unsigned int chip_chainmask;
2320
2321         u16 eeval;
2322         u8 ant_div_ctl1, tx_chainmask, rx_chainmask;
2323
2324         eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
2325         regulatory->current_rd = eeval;
2326
2327         if (ah->opmode != NL80211_IFTYPE_AP &&
2328             ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
2329                 if (regulatory->current_rd == 0x64 ||
2330                     regulatory->current_rd == 0x65)
2331                         regulatory->current_rd += 5;
2332                 else if (regulatory->current_rd == 0x41)
2333                         regulatory->current_rd = 0x43;
2334                 ath_dbg(common, REGULATORY, "regdomain mapped to 0x%x\n",
2335                         regulatory->current_rd);
2336         }
2337
2338         eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
2339         if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
2340                 ath_err(common,
2341                         "no band has been marked as supported in EEPROM\n");
2342                 return -EINVAL;
2343         }
2344
2345         if (eeval & AR5416_OPFLAGS_11A)
2346                 pCap->hw_caps |= ATH9K_HW_CAP_5GHZ;
2347
2348         if (eeval & AR5416_OPFLAGS_11G)
2349                 pCap->hw_caps |= ATH9K_HW_CAP_2GHZ;
2350
2351         if (AR_SREV_9485(ah) ||
2352             AR_SREV_9285(ah) ||
2353             AR_SREV_9330(ah) ||
2354             AR_SREV_9565(ah))
2355                 chip_chainmask = 1;
2356         else if (AR_SREV_9462(ah))
2357                 chip_chainmask = 3;
2358         else if (!AR_SREV_9280_20_OR_LATER(ah))
2359                 chip_chainmask = 7;
2360         else if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9340(ah))
2361                 chip_chainmask = 3;
2362         else
2363                 chip_chainmask = 7;
2364
2365         pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
2366         /*
2367          * For AR9271 we will temporarilly uses the rx chainmax as read from
2368          * the EEPROM.
2369          */
2370         if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
2371             !(eeval & AR5416_OPFLAGS_11A) &&
2372             !(AR_SREV_9271(ah)))
2373                 /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
2374                 pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
2375         else if (AR_SREV_9100(ah))
2376                 pCap->rx_chainmask = 0x7;
2377         else
2378                 /* Use rx_chainmask from EEPROM. */
2379                 pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
2380
2381         pCap->tx_chainmask = fixup_chainmask(chip_chainmask, pCap->tx_chainmask);
2382         pCap->rx_chainmask = fixup_chainmask(chip_chainmask, pCap->rx_chainmask);
2383         ah->txchainmask = pCap->tx_chainmask;
2384         ah->rxchainmask = pCap->rx_chainmask;
2385
2386         ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
2387
2388         /* enable key search for every frame in an aggregate */
2389         if (AR_SREV_9300_20_OR_LATER(ah))
2390                 ah->misc_mode |= AR_PCU_ALWAYS_PERFORM_KEYSEARCH;
2391
2392         common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
2393
2394         if (ah->hw_version.devid != AR2427_DEVID_PCIE)
2395                 pCap->hw_caps |= ATH9K_HW_CAP_HT;
2396         else
2397                 pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
2398
2399         if (AR_SREV_9271(ah))
2400                 pCap->num_gpio_pins = AR9271_NUM_GPIO;
2401         else if (AR_DEVID_7010(ah))
2402                 pCap->num_gpio_pins = AR7010_NUM_GPIO;
2403         else if (AR_SREV_9300_20_OR_LATER(ah))
2404                 pCap->num_gpio_pins = AR9300_NUM_GPIO;
2405         else if (AR_SREV_9287_11_OR_LATER(ah))
2406                 pCap->num_gpio_pins = AR9287_NUM_GPIO;
2407         else if (AR_SREV_9285_12_OR_LATER(ah))
2408                 pCap->num_gpio_pins = AR9285_NUM_GPIO;
2409         else if (AR_SREV_9280_20_OR_LATER(ah))
2410                 pCap->num_gpio_pins = AR928X_NUM_GPIO;
2411         else
2412                 pCap->num_gpio_pins = AR_NUM_GPIO;
2413
2414         if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah))
2415                 pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
2416         else
2417                 pCap->rts_aggr_limit = (8 * 1024);
2418
2419 #ifdef CONFIG_ATH9K_RFKILL
2420         ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
2421         if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
2422                 ah->rfkill_gpio =
2423                         MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
2424                 ah->rfkill_polarity =
2425                         MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
2426
2427                 pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
2428         }
2429 #endif
2430         if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
2431                 pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
2432         else
2433                 pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
2434
2435         if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
2436                 pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
2437         else
2438                 pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
2439
2440         if (AR_SREV_9300_20_OR_LATER(ah)) {
2441                 pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_FASTCLOCK;
2442                 if (!AR_SREV_9330(ah) && !AR_SREV_9485(ah) && !AR_SREV_9565(ah))
2443                         pCap->hw_caps |= ATH9K_HW_CAP_LDPC;
2444
2445                 pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
2446                 pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
2447                 pCap->rx_status_len = sizeof(struct ar9003_rxs);
2448                 pCap->tx_desc_len = sizeof(struct ar9003_txc);
2449                 pCap->txs_len = sizeof(struct ar9003_txs);
2450         } else {
2451                 pCap->tx_desc_len = sizeof(struct ath_desc);
2452                 if (AR_SREV_9280_20(ah))
2453                         pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
2454         }
2455
2456         if (AR_SREV_9300_20_OR_LATER(ah))
2457                 pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
2458
2459         if (AR_SREV_9300_20_OR_LATER(ah))
2460                 ah->ent_mode = REG_READ(ah, AR_ENT_OTP);
2461
2462         if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
2463                 pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
2464
2465         if (AR_SREV_9285(ah)) {
2466                 if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
2467                         ant_div_ctl1 =
2468                                 ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
2469                         if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1)) {
2470                                 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
2471                                 ath_info(common, "Enable LNA combining\n");
2472                         }
2473                 }
2474         }
2475
2476         if (AR_SREV_9300_20_OR_LATER(ah)) {
2477                 if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
2478                         pCap->hw_caps |= ATH9K_HW_CAP_APM;
2479         }
2480
2481         if (AR_SREV_9330(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah)) {
2482                 ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
2483                 if ((ant_div_ctl1 >> 0x6) == 0x3) {
2484                         pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
2485                         ath_info(common, "Enable LNA combining\n");
2486                 }
2487         }
2488
2489         if (ath9k_hw_dfs_tested(ah))
2490                 pCap->hw_caps |= ATH9K_HW_CAP_DFS;
2491
2492         tx_chainmask = pCap->tx_chainmask;
2493         rx_chainmask = pCap->rx_chainmask;
2494         while (tx_chainmask || rx_chainmask) {
2495                 if (tx_chainmask & BIT(0))
2496                         pCap->max_txchains++;
2497                 if (rx_chainmask & BIT(0))
2498                         pCap->max_rxchains++;
2499
2500                 tx_chainmask >>= 1;
2501                 rx_chainmask >>= 1;
2502         }
2503
2504         if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
2505                 if (!(ah->ent_mode & AR_ENT_OTP_49GHZ_DISABLE))
2506                         pCap->hw_caps |= ATH9K_HW_CAP_MCI;
2507
2508                 if (AR_SREV_9462_20_OR_LATER(ah))
2509                         pCap->hw_caps |= ATH9K_HW_CAP_RTT;
2510         }
2511
2512         if (AR_SREV_9462(ah))
2513                 pCap->hw_caps |= ATH9K_HW_WOW_DEVICE_CAPABLE;
2514
2515         if (AR_SREV_9300_20_OR_LATER(ah) &&
2516             ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
2517                         pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
2518
2519         return 0;
2520 }
2521
2522 /****************************/
2523 /* GPIO / RFKILL / Antennae */
2524 /****************************/
2525
2526 static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
2527                                          u32 gpio, u32 type)
2528 {
2529         int addr;
2530         u32 gpio_shift, tmp;
2531
2532         if (gpio > 11)
2533                 addr = AR_GPIO_OUTPUT_MUX3;
2534         else if (gpio > 5)
2535                 addr = AR_GPIO_OUTPUT_MUX2;
2536         else
2537                 addr = AR_GPIO_OUTPUT_MUX1;
2538
2539         gpio_shift = (gpio % 6) * 5;
2540
2541         if (AR_SREV_9280_20_OR_LATER(ah)
2542             || (addr != AR_GPIO_OUTPUT_MUX1)) {
2543                 REG_RMW(ah, addr, (type << gpio_shift),
2544                         (0x1f << gpio_shift));
2545         } else {
2546                 tmp = REG_READ(ah, addr);
2547                 tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
2548                 tmp &= ~(0x1f << gpio_shift);
2549                 tmp |= (type << gpio_shift);
2550                 REG_WRITE(ah, addr, tmp);
2551         }
2552 }
2553
2554 void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
2555 {
2556         u32 gpio_shift;
2557
2558         BUG_ON(gpio >= ah->caps.num_gpio_pins);
2559
2560         if (AR_DEVID_7010(ah)) {
2561                 gpio_shift = gpio;
2562                 REG_RMW(ah, AR7010_GPIO_OE,
2563                         (AR7010_GPIO_OE_AS_INPUT << gpio_shift),
2564                         (AR7010_GPIO_OE_MASK << gpio_shift));
2565                 return;
2566         }
2567
2568         gpio_shift = gpio << 1;
2569         REG_RMW(ah,
2570                 AR_GPIO_OE_OUT,
2571                 (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
2572                 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2573 }
2574 EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
2575
2576 u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
2577 {
2578 #define MS_REG_READ(x, y) \
2579         (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
2580
2581         if (gpio >= ah->caps.num_gpio_pins)
2582                 return 0xffffffff;
2583
2584         if (AR_DEVID_7010(ah)) {
2585                 u32 val;
2586                 val = REG_READ(ah, AR7010_GPIO_IN);
2587                 return (MS(val, AR7010_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) == 0;
2588         } else if (AR_SREV_9300_20_OR_LATER(ah))
2589                 return (MS(REG_READ(ah, AR_GPIO_IN), AR9300_GPIO_IN_VAL) &
2590                         AR_GPIO_BIT(gpio)) != 0;
2591         else if (AR_SREV_9271(ah))
2592                 return MS_REG_READ(AR9271, gpio) != 0;
2593         else if (AR_SREV_9287_11_OR_LATER(ah))
2594                 return MS_REG_READ(AR9287, gpio) != 0;
2595         else if (AR_SREV_9285_12_OR_LATER(ah))
2596                 return MS_REG_READ(AR9285, gpio) != 0;
2597         else if (AR_SREV_9280_20_OR_LATER(ah))
2598                 return MS_REG_READ(AR928X, gpio) != 0;
2599         else
2600                 return MS_REG_READ(AR, gpio) != 0;
2601 }
2602 EXPORT_SYMBOL(ath9k_hw_gpio_get);
2603
2604 void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
2605                          u32 ah_signal_type)
2606 {
2607         u32 gpio_shift;
2608
2609         if (AR_DEVID_7010(ah)) {
2610                 gpio_shift = gpio;
2611                 REG_RMW(ah, AR7010_GPIO_OE,
2612                         (AR7010_GPIO_OE_AS_OUTPUT << gpio_shift),
2613                         (AR7010_GPIO_OE_MASK << gpio_shift));
2614                 return;
2615         }
2616
2617         ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
2618         gpio_shift = 2 * gpio;
2619         REG_RMW(ah,
2620                 AR_GPIO_OE_OUT,
2621                 (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
2622                 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2623 }
2624 EXPORT_SYMBOL(ath9k_hw_cfg_output);
2625
2626 void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
2627 {
2628         if (AR_DEVID_7010(ah)) {
2629                 val = val ? 0 : 1;
2630                 REG_RMW(ah, AR7010_GPIO_OUT, ((val&1) << gpio),
2631                         AR_GPIO_BIT(gpio));
2632                 return;
2633         }
2634
2635         if (AR_SREV_9271(ah))
2636                 val = ~val;
2637
2638         REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
2639                 AR_GPIO_BIT(gpio));
2640 }
2641 EXPORT_SYMBOL(ath9k_hw_set_gpio);
2642
2643 void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
2644 {
2645         REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
2646 }
2647 EXPORT_SYMBOL(ath9k_hw_setantenna);
2648
2649 /*********************/
2650 /* General Operation */
2651 /*********************/
2652
2653 u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
2654 {
2655         u32 bits = REG_READ(ah, AR_RX_FILTER);
2656         u32 phybits = REG_READ(ah, AR_PHY_ERR);
2657
2658         if (phybits & AR_PHY_ERR_RADAR)
2659                 bits |= ATH9K_RX_FILTER_PHYRADAR;
2660         if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
2661                 bits |= ATH9K_RX_FILTER_PHYERR;
2662
2663         return bits;
2664 }
2665 EXPORT_SYMBOL(ath9k_hw_getrxfilter);
2666
2667 void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
2668 {
2669         u32 phybits;
2670
2671         ENABLE_REGWRITE_BUFFER(ah);
2672
2673         if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
2674                 bits |= ATH9K_RX_FILTER_CONTROL_WRAPPER;
2675
2676         REG_WRITE(ah, AR_RX_FILTER, bits);
2677
2678         phybits = 0;
2679         if (bits & ATH9K_RX_FILTER_PHYRADAR)
2680                 phybits |= AR_PHY_ERR_RADAR;
2681         if (bits & ATH9K_RX_FILTER_PHYERR)
2682                 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
2683         REG_WRITE(ah, AR_PHY_ERR, phybits);
2684
2685         if (phybits)
2686                 REG_SET_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
2687         else
2688                 REG_CLR_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
2689
2690         REGWRITE_BUFFER_FLUSH(ah);
2691 }
2692 EXPORT_SYMBOL(ath9k_hw_setrxfilter);
2693
2694 bool ath9k_hw_phy_disable(struct ath_hw *ah)
2695 {
2696         if (ath9k_hw_mci_is_enabled(ah))
2697                 ar9003_mci_bt_gain_ctrl(ah);
2698
2699         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
2700                 return false;
2701
2702         ath9k_hw_init_pll(ah, NULL);
2703         ah->htc_reset_init = true;
2704         return true;
2705 }
2706 EXPORT_SYMBOL(ath9k_hw_phy_disable);
2707
2708 bool ath9k_hw_disable(struct ath_hw *ah)
2709 {
2710         if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
2711                 return false;
2712
2713         if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
2714                 return false;
2715
2716         ath9k_hw_init_pll(ah, NULL);
2717         return true;
2718 }
2719 EXPORT_SYMBOL(ath9k_hw_disable);
2720
2721 static int get_antenna_gain(struct ath_hw *ah, struct ath9k_channel *chan)
2722 {
2723         enum eeprom_param gain_param;
2724
2725         if (IS_CHAN_2GHZ(chan))
2726                 gain_param = EEP_ANTENNA_GAIN_2G;
2727         else
2728                 gain_param = EEP_ANTENNA_GAIN_5G;
2729
2730         return ah->eep_ops->get_eeprom(ah, gain_param);
2731 }
2732
2733 void ath9k_hw_apply_txpower(struct ath_hw *ah, struct ath9k_channel *chan,
2734                             bool test)
2735 {
2736         struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
2737         struct ieee80211_channel *channel;
2738         int chan_pwr, new_pwr, max_gain;
2739         int ant_gain, ant_reduction = 0;
2740
2741         if (!chan)
2742                 return;
2743
2744         channel = chan->chan;
2745         chan_pwr = min_t(int, channel->max_power * 2, MAX_RATE_POWER);
2746         new_pwr = min_t(int, chan_pwr, reg->power_limit);
2747         max_gain = chan_pwr - new_pwr + channel->max_antenna_gain * 2;
2748
2749         ant_gain = get_antenna_gain(ah, chan);
2750         if (ant_gain > max_gain)
2751                 ant_reduction = ant_gain - max_gain;
2752
2753         ah->eep_ops->set_txpower(ah, chan,
2754                                  ath9k_regd_get_ctl(reg, chan),
2755                                  ant_reduction, new_pwr, test);
2756 }
2757
2758 void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit, bool test)
2759 {
2760         struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
2761         struct ath9k_channel *chan = ah->curchan;
2762         struct ieee80211_channel *channel = chan->chan;
2763
2764         reg->power_limit = min_t(u32, limit, MAX_RATE_POWER);
2765         if (test)
2766                 channel->max_power = MAX_RATE_POWER / 2;
2767
2768         ath9k_hw_apply_txpower(ah, chan, test);
2769
2770         if (test)
2771                 channel->max_power = DIV_ROUND_UP(reg->max_power_level, 2);
2772 }
2773 EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
2774
2775 void ath9k_hw_setopmode(struct ath_hw *ah)
2776 {
2777         ath9k_hw_set_operating_mode(ah, ah->opmode);
2778 }
2779 EXPORT_SYMBOL(ath9k_hw_setopmode);
2780
2781 void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
2782 {
2783         REG_WRITE(ah, AR_MCAST_FIL0, filter0);
2784         REG_WRITE(ah, AR_MCAST_FIL1, filter1);
2785 }
2786 EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
2787
2788 void ath9k_hw_write_associd(struct ath_hw *ah)
2789 {
2790         struct ath_common *common = ath9k_hw_common(ah);
2791
2792         REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
2793         REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
2794                   ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
2795 }
2796 EXPORT_SYMBOL(ath9k_hw_write_associd);
2797
2798 #define ATH9K_MAX_TSF_READ 10
2799
2800 u64 ath9k_hw_gettsf64(struct ath_hw *ah)
2801 {
2802         u32 tsf_lower, tsf_upper1, tsf_upper2;
2803         int i;
2804
2805         tsf_upper1 = REG_READ(ah, AR_TSF_U32);
2806         for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
2807                 tsf_lower = REG_READ(ah, AR_TSF_L32);
2808                 tsf_upper2 = REG_READ(ah, AR_TSF_U32);
2809                 if (tsf_upper2 == tsf_upper1)
2810                         break;
2811                 tsf_upper1 = tsf_upper2;
2812         }
2813
2814         WARN_ON( i == ATH9K_MAX_TSF_READ );
2815
2816         return (((u64)tsf_upper1 << 32) | tsf_lower);
2817 }
2818 EXPORT_SYMBOL(ath9k_hw_gettsf64);
2819
2820 void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
2821 {
2822         REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
2823         REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
2824 }
2825 EXPORT_SYMBOL(ath9k_hw_settsf64);
2826
2827 void ath9k_hw_reset_tsf(struct ath_hw *ah)
2828 {
2829         if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
2830                            AH_TSF_WRITE_TIMEOUT))
2831                 ath_dbg(ath9k_hw_common(ah), RESET,
2832                         "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
2833
2834         REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
2835 }
2836 EXPORT_SYMBOL(ath9k_hw_reset_tsf);
2837
2838 void ath9k_hw_set_tsfadjust(struct ath_hw *ah, bool set)
2839 {
2840         if (set)
2841                 ah->misc_mode |= AR_PCU_TX_ADD_TSF;
2842         else
2843                 ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
2844 }
2845 EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
2846
2847 void ath9k_hw_set11nmac2040(struct ath_hw *ah, struct ath9k_channel *chan)
2848 {
2849         u32 macmode;
2850
2851         if (IS_CHAN_HT40(chan) && !ah->config.cwm_ignore_extcca)
2852                 macmode = AR_2040_JOINED_RX_CLEAR;
2853         else
2854                 macmode = 0;
2855
2856         REG_WRITE(ah, AR_2040_MODE, macmode);
2857 }
2858
2859 /* HW Generic timers configuration */
2860
2861 static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
2862 {
2863         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2864         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2865         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2866         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2867         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2868         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2869         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2870         {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2871         {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
2872         {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
2873                                 AR_NDP2_TIMER_MODE, 0x0002},
2874         {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
2875                                 AR_NDP2_TIMER_MODE, 0x0004},
2876         {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
2877                                 AR_NDP2_TIMER_MODE, 0x0008},
2878         {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
2879                                 AR_NDP2_TIMER_MODE, 0x0010},
2880         {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
2881                                 AR_NDP2_TIMER_MODE, 0x0020},
2882         {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
2883                                 AR_NDP2_TIMER_MODE, 0x0040},
2884         {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
2885                                 AR_NDP2_TIMER_MODE, 0x0080}
2886 };
2887
2888 /* HW generic timer primitives */
2889
2890 u32 ath9k_hw_gettsf32(struct ath_hw *ah)
2891 {
2892         return REG_READ(ah, AR_TSF_L32);
2893 }
2894 EXPORT_SYMBOL(ath9k_hw_gettsf32);
2895
2896 struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
2897                                           void (*trigger)(void *),
2898                                           void (*overflow)(void *),
2899                                           void *arg,
2900                                           u8 timer_index)
2901 {
2902         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2903         struct ath_gen_timer *timer;
2904
2905         if ((timer_index < AR_FIRST_NDP_TIMER) ||
2906                 (timer_index >= ATH_MAX_GEN_TIMER))
2907                 return NULL;
2908
2909         timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
2910         if (timer == NULL)
2911                 return NULL;
2912
2913         /* allocate a hardware generic timer slot */
2914         timer_table->timers[timer_index] = timer;
2915         timer->index = timer_index;
2916         timer->trigger = trigger;
2917         timer->overflow = overflow;
2918         timer->arg = arg;
2919
2920         return timer;
2921 }
2922 EXPORT_SYMBOL(ath_gen_timer_alloc);
2923
2924 void ath9k_hw_gen_timer_start(struct ath_hw *ah,
2925                               struct ath_gen_timer *timer,
2926                               u32 timer_next,
2927                               u32 timer_period)
2928 {
2929         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2930         u32 mask = 0;
2931
2932         timer_table->timer_mask |= BIT(timer->index);
2933
2934         /*
2935          * Program generic timer registers
2936          */
2937         REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
2938                  timer_next);
2939         REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
2940                   timer_period);
2941         REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2942                     gen_tmr_configuration[timer->index].mode_mask);
2943
2944         if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
2945                 /*
2946                  * Starting from AR9462, each generic timer can select which tsf
2947                  * to use. But we still follow the old rule, 0 - 7 use tsf and
2948                  * 8 - 15  use tsf2.
2949                  */
2950                 if ((timer->index < AR_GEN_TIMER_BANK_1_LEN))
2951                         REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
2952                                        (1 << timer->index));
2953                 else
2954                         REG_SET_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
2955                                        (1 << timer->index));
2956         }
2957
2958         if (timer->trigger)
2959                 mask |= SM(AR_GENTMR_BIT(timer->index),
2960                            AR_IMR_S5_GENTIMER_TRIG);
2961         if (timer->overflow)
2962                 mask |= SM(AR_GENTMR_BIT(timer->index),
2963                            AR_IMR_S5_GENTIMER_THRESH);
2964
2965         REG_SET_BIT(ah, AR_IMR_S5, mask);
2966
2967         if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
2968                 ah->imask |= ATH9K_INT_GENTIMER;
2969                 ath9k_hw_set_interrupts(ah);
2970         }
2971 }
2972 EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
2973
2974 void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
2975 {
2976         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2977
2978         /* Clear generic timer enable bits. */
2979         REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2980                         gen_tmr_configuration[timer->index].mode_mask);
2981
2982         if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
2983                 /*
2984                  * Need to switch back to TSF if it was using TSF2.
2985                  */
2986                 if ((timer->index >= AR_GEN_TIMER_BANK_1_LEN)) {
2987                         REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
2988                                     (1 << timer->index));
2989                 }
2990         }
2991
2992         /* Disable both trigger and thresh interrupt masks */
2993         REG_CLR_BIT(ah, AR_IMR_S5,
2994                 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2995                 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2996
2997         timer_table->timer_mask &= ~BIT(timer->index);
2998
2999         if (timer_table->timer_mask == 0) {
3000                 ah->imask &= ~ATH9K_INT_GENTIMER;
3001                 ath9k_hw_set_interrupts(ah);
3002         }
3003 }
3004 EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
3005
3006 void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
3007 {
3008         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
3009
3010         /* free the hardware generic timer slot */
3011         timer_table->timers[timer->index] = NULL;
3012         kfree(timer);
3013 }
3014 EXPORT_SYMBOL(ath_gen_timer_free);
3015
3016 /*
3017  * Generic Timer Interrupts handling
3018  */
3019 void ath_gen_timer_isr(struct ath_hw *ah)
3020 {
3021         struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
3022         struct ath_gen_timer *timer;
3023         unsigned long trigger_mask, thresh_mask;
3024         unsigned int index;
3025
3026         /* get hardware generic timer interrupt status */
3027         trigger_mask = ah->intr_gen_timer_trigger;
3028         thresh_mask = ah->intr_gen_timer_thresh;
3029         trigger_mask &= timer_table->timer_mask;
3030         thresh_mask &= timer_table->timer_mask;
3031
3032         for_each_set_bit(index, &thresh_mask, ARRAY_SIZE(timer_table->timers)) {
3033                 timer = timer_table->timers[index];
3034                 if (!timer)
3035                     continue;
3036                 if (!timer->overflow)
3037                     continue;
3038
3039                 trigger_mask &= ~BIT(index);
3040                 timer->overflow(timer->arg);
3041         }
3042
3043         for_each_set_bit(index, &trigger_mask, ARRAY_SIZE(timer_table->timers)) {
3044                 timer = timer_table->timers[index];
3045                 if (!timer)
3046                     continue;
3047                 if (!timer->trigger)
3048                     continue;
3049                 timer->trigger(timer->arg);
3050         }
3051 }
3052 EXPORT_SYMBOL(ath_gen_timer_isr);
3053
3054 /********/
3055 /* HTC  */
3056 /********/
3057
3058 static struct {
3059         u32 version;
3060         const char * name;
3061 } ath_mac_bb_names[] = {
3062         /* Devices with external radios */
3063         { AR_SREV_VERSION_5416_PCI,     "5416" },
3064         { AR_SREV_VERSION_5416_PCIE,    "5418" },
3065         { AR_SREV_VERSION_9100,         "9100" },
3066         { AR_SREV_VERSION_9160,         "9160" },
3067         /* Single-chip solutions */
3068         { AR_SREV_VERSION_9280,         "9280" },
3069         { AR_SREV_VERSION_9285,         "9285" },
3070         { AR_SREV_VERSION_9287,         "9287" },
3071         { AR_SREV_VERSION_9271,         "9271" },
3072         { AR_SREV_VERSION_9300,         "9300" },
3073         { AR_SREV_VERSION_9330,         "9330" },
3074         { AR_SREV_VERSION_9340,         "9340" },
3075         { AR_SREV_VERSION_9485,         "9485" },
3076         { AR_SREV_VERSION_9462,         "9462" },
3077         { AR_SREV_VERSION_9550,         "9550" },
3078         { AR_SREV_VERSION_9565,         "9565" },
3079         { AR_SREV_VERSION_9531,         "9531" },
3080 };
3081
3082 /* For devices with external radios */
3083 static struct {
3084         u16 version;
3085         const char * name;
3086 } ath_rf_names[] = {
3087         { 0,                            "5133" },
3088         { AR_RAD5133_SREV_MAJOR,        "5133" },
3089         { AR_RAD5122_SREV_MAJOR,        "5122" },
3090         { AR_RAD2133_SREV_MAJOR,        "2133" },
3091         { AR_RAD2122_SREV_MAJOR,        "2122" }
3092 };
3093
3094 /*
3095  * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
3096  */
3097 static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
3098 {
3099         int i;
3100
3101         for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
3102                 if (ath_mac_bb_names[i].version == mac_bb_version) {
3103                         return ath_mac_bb_names[i].name;
3104                 }
3105         }
3106
3107         return "????";
3108 }
3109
3110 /*
3111  * Return the RF name. "????" is returned if the RF is unknown.
3112  * Used for devices with external radios.
3113  */
3114 static const char *ath9k_hw_rf_name(u16 rf_version)
3115 {
3116         int i;
3117
3118         for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
3119                 if (ath_rf_names[i].version == rf_version) {
3120                         return ath_rf_names[i].name;
3121                 }
3122         }
3123
3124         return "????";
3125 }
3126
3127 void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
3128 {
3129         int used;
3130
3131         /* chipsets >= AR9280 are single-chip */
3132         if (AR_SREV_9280_20_OR_LATER(ah)) {
3133                 used = scnprintf(hw_name, len,
3134                                  "Atheros AR%s Rev:%x",
3135                                  ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
3136                                  ah->hw_version.macRev);
3137         }
3138         else {
3139                 used = scnprintf(hw_name, len,
3140                                  "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
3141                                  ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
3142                                  ah->hw_version.macRev,
3143                                  ath9k_hw_rf_name((ah->hw_version.analog5GhzRev
3144                                                   & AR_RADIO_SREV_MAJOR)),
3145                                  ah->hw_version.phyRev);
3146         }
3147
3148         hw_name[used] = '\0';
3149 }
3150 EXPORT_SYMBOL(ath9k_hw_name);