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