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