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