blob: 2aaa1fd4df2f0c9382d14b03cf63d76ba39b9a4c [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2008-2011 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040019#include <linux/module.h>
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070020#include <asm/unaligned.h>
21
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -070022#include "hw.h"
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -040023#include "hw-ops.h"
Luis R. Rodriguezcfe8cba2009-09-13 23:39:31 -070024#include "rc.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040025#include "ar9003_mac.h"
Sujith Manoharanf4701b52012-02-22 12:41:18 +053026#include "ar9003_mci.h"
Ben Greear462e58f2012-04-12 10:04:00 -070027#include "debug.h"
28#include "ath9k.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070029
Sujithcbe61d82009-02-09 13:27:12 +053030static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070031
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -040032MODULE_AUTHOR("Atheros Communications");
33MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
34MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
35MODULE_LICENSE("Dual BSD/GPL");
36
37static int __init ath9k_init(void)
38{
39 return 0;
40}
41module_init(ath9k_init);
42
43static void __exit ath9k_exit(void)
44{
45 return;
46}
47module_exit(ath9k_exit);
48
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -040049/* Private hardware callbacks */
50
51static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
52{
53 ath9k_hw_private_ops(ah)->init_cal_settings(ah);
54}
55
56static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
57{
58 ath9k_hw_private_ops(ah)->init_mode_regs(ah);
59}
60
Luis R. Rodriguez64773962010-04-15 17:38:17 -040061static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
62 struct ath9k_channel *chan)
63{
64 return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
65}
66
Luis R. Rodriguez991312d2010-04-15 17:39:05 -040067static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
68{
69 if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
70 return;
71
72 ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
73}
74
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040075static void ath9k_hw_ani_cache_ini_regs(struct ath_hw *ah)
76{
77 /* You will not have this callback if using the old ANI */
78 if (!ath9k_hw_private_ops(ah)->ani_cache_ini_regs)
79 return;
80
81 ath9k_hw_private_ops(ah)->ani_cache_ini_regs(ah);
82}
83
Sujithf1dc5602008-10-29 10:16:30 +053084/********************/
85/* Helper Functions */
86/********************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087
Ben Greear462e58f2012-04-12 10:04:00 -070088#ifdef CONFIG_ATH9K_DEBUGFS
89
90void ath9k_debug_sync_cause(struct ath_common *common, u32 sync_cause)
91{
92 struct ath_softc *sc = common->priv;
93 if (sync_cause)
94 sc->debug.stats.istats.sync_cause_all++;
95 if (sync_cause & AR_INTR_SYNC_RTC_IRQ)
96 sc->debug.stats.istats.sync_rtc_irq++;
97 if (sync_cause & AR_INTR_SYNC_MAC_IRQ)
98 sc->debug.stats.istats.sync_mac_irq++;
99 if (sync_cause & AR_INTR_SYNC_EEPROM_ILLEGAL_ACCESS)
100 sc->debug.stats.istats.eeprom_illegal_access++;
101 if (sync_cause & AR_INTR_SYNC_APB_TIMEOUT)
102 sc->debug.stats.istats.apb_timeout++;
103 if (sync_cause & AR_INTR_SYNC_PCI_MODE_CONFLICT)
104 sc->debug.stats.istats.pci_mode_conflict++;
105 if (sync_cause & AR_INTR_SYNC_HOST1_FATAL)
106 sc->debug.stats.istats.host1_fatal++;
107 if (sync_cause & AR_INTR_SYNC_HOST1_PERR)
108 sc->debug.stats.istats.host1_perr++;
109 if (sync_cause & AR_INTR_SYNC_TRCV_FIFO_PERR)
110 sc->debug.stats.istats.trcv_fifo_perr++;
111 if (sync_cause & AR_INTR_SYNC_RADM_CPL_EP)
112 sc->debug.stats.istats.radm_cpl_ep++;
113 if (sync_cause & AR_INTR_SYNC_RADM_CPL_DLLP_ABORT)
114 sc->debug.stats.istats.radm_cpl_dllp_abort++;
115 if (sync_cause & AR_INTR_SYNC_RADM_CPL_TLP_ABORT)
116 sc->debug.stats.istats.radm_cpl_tlp_abort++;
117 if (sync_cause & AR_INTR_SYNC_RADM_CPL_ECRC_ERR)
118 sc->debug.stats.istats.radm_cpl_ecrc_err++;
119 if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT)
120 sc->debug.stats.istats.radm_cpl_timeout++;
121 if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT)
122 sc->debug.stats.istats.local_timeout++;
123 if (sync_cause & AR_INTR_SYNC_PM_ACCESS)
124 sc->debug.stats.istats.pm_access++;
125 if (sync_cause & AR_INTR_SYNC_MAC_AWAKE)
126 sc->debug.stats.istats.mac_awake++;
127 if (sync_cause & AR_INTR_SYNC_MAC_ASLEEP)
128 sc->debug.stats.istats.mac_asleep++;
129 if (sync_cause & AR_INTR_SYNC_MAC_SLEEP_ACCESS)
130 sc->debug.stats.istats.mac_sleep_access++;
131}
132#endif
133
134
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200135static void ath9k_hw_set_clockrate(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530136{
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -0700137 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200138 struct ath_common *common = ath9k_hw_common(ah);
139 unsigned int clockrate;
Sujithcbe61d82009-02-09 13:27:12 +0530140
Felix Fietkau087b6ff2011-07-09 11:12:49 +0700141 /* AR9287 v1.3+ uses async FIFO and runs the MAC at 117 MHz */
142 if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah))
143 clockrate = 117;
144 else if (!ah->curchan) /* should really check for CCK instead */
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200145 clockrate = ATH9K_CLOCK_RATE_CCK;
146 else if (conf->channel->band == IEEE80211_BAND_2GHZ)
147 clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
148 else if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
149 clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
Vasanthakumar Thiagarajane5553722010-04-26 15:04:33 -0400150 else
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200151 clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
152
153 if (conf_is_ht40(conf))
154 clockrate *= 2;
155
Felix Fietkau906c7202011-07-09 11:12:48 +0700156 if (ah->curchan) {
157 if (IS_CHAN_HALF_RATE(ah->curchan))
158 clockrate /= 2;
159 if (IS_CHAN_QUARTER_RATE(ah->curchan))
160 clockrate /= 4;
161 }
162
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200163 common->clockrate = clockrate;
Sujithf1dc5602008-10-29 10:16:30 +0530164}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700165
Sujithcbe61d82009-02-09 13:27:12 +0530166static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
Sujithf1dc5602008-10-29 10:16:30 +0530167{
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200168 struct ath_common *common = ath9k_hw_common(ah);
Sujithcbe61d82009-02-09 13:27:12 +0530169
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200170 return usecs * common->clockrate;
Sujithf1dc5602008-10-29 10:16:30 +0530171}
172
Sujith0caa7b12009-02-16 13:23:20 +0530173bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700174{
175 int i;
176
Sujith0caa7b12009-02-16 13:23:20 +0530177 BUG_ON(timeout < AH_TIME_QUANTUM);
178
179 for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700180 if ((REG_READ(ah, reg) & mask) == val)
181 return true;
182
183 udelay(AH_TIME_QUANTUM);
184 }
Sujith04bd4632008-11-28 22:18:05 +0530185
Joe Perchesd2182b62011-12-15 14:55:53 -0800186 ath_dbg(ath9k_hw_common(ah), ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800187 "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
188 timeout, reg, REG_READ(ah, reg), mask, val);
Sujithf1dc5602008-10-29 10:16:30 +0530189
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700190 return false;
191}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400192EXPORT_SYMBOL(ath9k_hw_wait);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700193
Felix Fietkaua9b6b252011-03-23 20:57:27 +0100194void ath9k_hw_write_array(struct ath_hw *ah, struct ar5416IniArray *array,
195 int column, unsigned int *writecnt)
196{
197 int r;
198
199 ENABLE_REGWRITE_BUFFER(ah);
200 for (r = 0; r < array->ia_rows; r++) {
201 REG_WRITE(ah, INI_RA(array, r, 0),
202 INI_RA(array, r, column));
203 DO_DELAY(*writecnt);
204 }
205 REGWRITE_BUFFER_FLUSH(ah);
206}
207
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700208u32 ath9k_hw_reverse_bits(u32 val, u32 n)
209{
210 u32 retval;
211 int i;
212
213 for (i = 0, retval = 0; i < n; i++) {
214 retval = (retval << 1) | (val & 1);
215 val >>= 1;
216 }
217 return retval;
218}
219
Sujithcbe61d82009-02-09 13:27:12 +0530220u16 ath9k_hw_computetxtime(struct ath_hw *ah,
Felix Fietkau545750d2009-11-23 22:21:01 +0100221 u8 phy, int kbps,
Sujithf1dc5602008-10-29 10:16:30 +0530222 u32 frameLen, u16 rateix,
223 bool shortPreamble)
224{
225 u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
Sujithf1dc5602008-10-29 10:16:30 +0530226
227 if (kbps == 0)
228 return 0;
229
Felix Fietkau545750d2009-11-23 22:21:01 +0100230 switch (phy) {
Sujith46d14a52008-11-18 09:08:13 +0530231 case WLAN_RC_PHY_CCK:
Sujithf1dc5602008-10-29 10:16:30 +0530232 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
Felix Fietkau545750d2009-11-23 22:21:01 +0100233 if (shortPreamble)
Sujithf1dc5602008-10-29 10:16:30 +0530234 phyTime >>= 1;
235 numBits = frameLen << 3;
236 txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
237 break;
Sujith46d14a52008-11-18 09:08:13 +0530238 case WLAN_RC_PHY_OFDM:
Sujith2660b812009-02-09 13:27:26 +0530239 if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
Sujithf1dc5602008-10-29 10:16:30 +0530240 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
241 numBits = OFDM_PLCP_BITS + (frameLen << 3);
242 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
243 txTime = OFDM_SIFS_TIME_QUARTER
244 + OFDM_PREAMBLE_TIME_QUARTER
245 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
Sujith2660b812009-02-09 13:27:26 +0530246 } else if (ah->curchan &&
247 IS_CHAN_HALF_RATE(ah->curchan)) {
Sujithf1dc5602008-10-29 10:16:30 +0530248 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
249 numBits = OFDM_PLCP_BITS + (frameLen << 3);
250 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
251 txTime = OFDM_SIFS_TIME_HALF +
252 OFDM_PREAMBLE_TIME_HALF
253 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
254 } else {
255 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
256 numBits = OFDM_PLCP_BITS + (frameLen << 3);
257 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
258 txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
259 + (numSymbols * OFDM_SYMBOL_TIME);
260 }
261 break;
262 default:
Joe Perches38002762010-12-02 19:12:36 -0800263 ath_err(ath9k_hw_common(ah),
264 "Unknown phy %u (rate ix %u)\n", phy, rateix);
Sujithf1dc5602008-10-29 10:16:30 +0530265 txTime = 0;
266 break;
267 }
268
269 return txTime;
270}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400271EXPORT_SYMBOL(ath9k_hw_computetxtime);
Sujithf1dc5602008-10-29 10:16:30 +0530272
Sujithcbe61d82009-02-09 13:27:12 +0530273void ath9k_hw_get_channel_centers(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530274 struct ath9k_channel *chan,
275 struct chan_centers *centers)
276{
277 int8_t extoff;
Sujithf1dc5602008-10-29 10:16:30 +0530278
279 if (!IS_CHAN_HT40(chan)) {
280 centers->ctl_center = centers->ext_center =
281 centers->synth_center = chan->channel;
282 return;
283 }
284
285 if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
286 (chan->chanmode == CHANNEL_G_HT40PLUS)) {
287 centers->synth_center =
288 chan->channel + HT40_CHANNEL_CENTER_SHIFT;
289 extoff = 1;
290 } else {
291 centers->synth_center =
292 chan->channel - HT40_CHANNEL_CENTER_SHIFT;
293 extoff = -1;
294 }
295
296 centers->ctl_center =
297 centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
Luis R. Rodriguez64200142009-09-13 22:05:04 -0700298 /* 25 MHz spacing is supported by hw but not on upper layers */
Sujithf1dc5602008-10-29 10:16:30 +0530299 centers->ext_center =
Luis R. Rodriguez64200142009-09-13 22:05:04 -0700300 centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
Sujithf1dc5602008-10-29 10:16:30 +0530301}
302
303/******************/
304/* Chip Revisions */
305/******************/
306
Sujithcbe61d82009-02-09 13:27:12 +0530307static void ath9k_hw_read_revisions(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530308{
309 u32 val;
310
Vasanthakumar Thiagarajanecb1d382011-04-19 19:29:18 +0530311 switch (ah->hw_version.devid) {
312 case AR5416_AR9100_DEVID:
313 ah->hw_version.macVersion = AR_SREV_VERSION_9100;
314 break;
Gabor Juhos37625612011-06-21 11:23:23 +0200315 case AR9300_DEVID_AR9330:
316 ah->hw_version.macVersion = AR_SREV_VERSION_9330;
317 if (ah->get_mac_revision) {
318 ah->hw_version.macRev = ah->get_mac_revision();
319 } else {
320 val = REG_READ(ah, AR_SREV);
321 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
322 }
323 return;
Vasanthakumar Thiagarajanecb1d382011-04-19 19:29:18 +0530324 case AR9300_DEVID_AR9340:
325 ah->hw_version.macVersion = AR_SREV_VERSION_9340;
326 val = REG_READ(ah, AR_SREV);
327 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
328 return;
329 }
330
Sujithf1dc5602008-10-29 10:16:30 +0530331 val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
332
333 if (val == 0xFF) {
334 val = REG_READ(ah, AR_SREV);
Sujithd535a422009-02-09 13:27:06 +0530335 ah->hw_version.macVersion =
336 (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
337 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
Mohammed Shafi Shajakhan76ed94b2011-09-30 11:31:28 +0530338
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +0530339 if (AR_SREV_9462(ah))
Mohammed Shafi Shajakhan76ed94b2011-09-30 11:31:28 +0530340 ah->is_pciexpress = true;
341 else
342 ah->is_pciexpress = (val &
343 AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
Sujithf1dc5602008-10-29 10:16:30 +0530344 } else {
345 if (!AR_SREV_9100(ah))
Sujithd535a422009-02-09 13:27:06 +0530346 ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
Sujithf1dc5602008-10-29 10:16:30 +0530347
Sujithd535a422009-02-09 13:27:06 +0530348 ah->hw_version.macRev = val & AR_SREV_REVISION;
Sujithf1dc5602008-10-29 10:16:30 +0530349
Sujithd535a422009-02-09 13:27:06 +0530350 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
Sujith2660b812009-02-09 13:27:26 +0530351 ah->is_pciexpress = true;
Sujithf1dc5602008-10-29 10:16:30 +0530352 }
353}
354
Sujithf1dc5602008-10-29 10:16:30 +0530355/************************************/
356/* HW Attach, Detach, Init Routines */
357/************************************/
358
Sujithcbe61d82009-02-09 13:27:12 +0530359static void ath9k_hw_disablepcie(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530360{
Felix Fietkau040b74f2010-12-12 00:51:07 +0100361 if (!AR_SREV_5416(ah))
Sujithf1dc5602008-10-29 10:16:30 +0530362 return;
363
364 REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
365 REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
366 REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
367 REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
368 REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
369 REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
370 REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
371 REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
372 REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
373
374 REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
375}
376
Stanislaw Gruszkad4930082011-07-29 15:59:08 +0200377static void ath9k_hw_aspm_init(struct ath_hw *ah)
378{
379 struct ath_common *common = ath9k_hw_common(ah);
380
381 if (common->bus_ops->aspm_init)
382 common->bus_ops->aspm_init(common);
383}
384
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400385/* This should work for all families including legacy */
Sujithcbe61d82009-02-09 13:27:12 +0530386static bool ath9k_hw_chip_test(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530387{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700388 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400389 u32 regAddr[2] = { AR_STA_ID0 };
Sujithf1dc5602008-10-29 10:16:30 +0530390 u32 regHold[2];
Joe Perches07b2fa52010-11-20 18:38:53 -0800391 static const u32 patternData[4] = {
392 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999
393 };
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400394 int i, j, loop_max;
Sujithf1dc5602008-10-29 10:16:30 +0530395
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400396 if (!AR_SREV_9300_20_OR_LATER(ah)) {
397 loop_max = 2;
398 regAddr[1] = AR_PHY_BASE + (8 << 2);
399 } else
400 loop_max = 1;
401
402 for (i = 0; i < loop_max; i++) {
Sujithf1dc5602008-10-29 10:16:30 +0530403 u32 addr = regAddr[i];
404 u32 wrData, rdData;
405
406 regHold[i] = REG_READ(ah, addr);
407 for (j = 0; j < 0x100; j++) {
408 wrData = (j << 16) | j;
409 REG_WRITE(ah, addr, wrData);
410 rdData = REG_READ(ah, addr);
411 if (rdData != wrData) {
Joe Perches38002762010-12-02 19:12:36 -0800412 ath_err(common,
413 "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
414 addr, wrData, rdData);
Sujithf1dc5602008-10-29 10:16:30 +0530415 return false;
416 }
417 }
418 for (j = 0; j < 4; j++) {
419 wrData = patternData[j];
420 REG_WRITE(ah, addr, wrData);
421 rdData = REG_READ(ah, addr);
422 if (wrData != rdData) {
Joe Perches38002762010-12-02 19:12:36 -0800423 ath_err(common,
424 "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
425 addr, wrData, rdData);
Sujithf1dc5602008-10-29 10:16:30 +0530426 return false;
427 }
428 }
429 REG_WRITE(ah, regAddr[i], regHold[i]);
430 }
431 udelay(100);
Sujithcbe61d82009-02-09 13:27:12 +0530432
Sujithf1dc5602008-10-29 10:16:30 +0530433 return true;
434}
435
Luis R. Rodriguezb8b0f372009-08-03 12:24:43 -0700436static void ath9k_hw_init_config(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700437{
438 int i;
439
Sujith2660b812009-02-09 13:27:26 +0530440 ah->config.dma_beacon_response_time = 2;
441 ah->config.sw_beacon_response_time = 10;
442 ah->config.additional_swba_backoff = 0;
443 ah->config.ack_6mb = 0x0;
444 ah->config.cwm_ignore_extcca = 0;
Sujith2660b812009-02-09 13:27:26 +0530445 ah->config.pcie_clock_req = 0;
Sujith2660b812009-02-09 13:27:26 +0530446 ah->config.pcie_waen = 0;
447 ah->config.analog_shiftreg = 1;
Luis R. Rodriguez03c72512010-06-12 00:33:46 -0400448 ah->config.enable_ani = true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449
450 for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530451 ah->config.spurchans[i][0] = AR_NO_SPUR;
452 ah->config.spurchans[i][1] = AR_NO_SPUR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 }
454
Luis R. Rodriguez6f481012011-01-20 17:47:39 -0800455 /* PAPRD needs some more work to be enabled */
456 ah->config.paprd_disable = 1;
457
Sujith0ce024c2009-12-14 14:57:00 +0530458 ah->config.rx_intr_mitigation = true;
Luis R. Rodriguez6a0ec302010-06-21 18:38:49 -0400459 ah->config.pcieSerDesWrite = true;
Luis R. Rodriguez61584252009-03-12 18:18:49 -0400460
461 /*
462 * We need this for PCI devices only (Cardbus, PCI, miniPCI)
463 * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
464 * This means we use it for all AR5416 devices, and the few
465 * minor PCI AR9280 devices out there.
466 *
467 * Serialization is required because these devices do not handle
468 * well the case of two concurrent reads/writes due to the latency
469 * involved. During one read/write another read/write can be issued
470 * on another CPU while the previous read/write may still be working
471 * on our hardware, if we hit this case the hardware poops in a loop.
472 * We prevent this by serializing reads and writes.
473 *
474 * This issue is not present on PCI-Express devices or pre-AR5416
475 * devices (legacy, 802.11abg).
476 */
477 if (num_possible_cpus() > 1)
David S. Miller2d6a5e92009-03-17 15:01:30 -0700478 ah->config.serialize_regmode = SER_REG_MODE_AUTO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700479}
480
Luis R. Rodriguez50aca252009-08-03 12:24:42 -0700481static void ath9k_hw_init_defaults(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700482{
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700483 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
484
485 regulatory->country_code = CTRY_DEFAULT;
486 regulatory->power_limit = MAX_RATE_POWER;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700487
Sujithd535a422009-02-09 13:27:06 +0530488 ah->hw_version.magic = AR5416_MAGIC;
Sujithd535a422009-02-09 13:27:06 +0530489 ah->hw_version.subvendorid = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700490
Sujith2660b812009-02-09 13:27:26 +0530491 ah->atim_window = 0;
Felix Fietkau16f24112010-06-12 17:22:32 +0200492 ah->sta_id1_defaults =
493 AR_STA_ID1_CRPT_MIC_ENABLE |
494 AR_STA_ID1_MCAST_KSRCH;
Felix Fietkauf1717602011-03-19 13:55:41 +0100495 if (AR_SREV_9100(ah))
496 ah->sta_id1_defaults |= AR_STA_ID1_AR9100_BA_FIX;
Rajkumar Manoharane3f2acc2011-08-27 11:22:59 +0530497 ah->slottime = ATH9K_SLOT_TIME_9;
Sujith2660b812009-02-09 13:27:26 +0530498 ah->globaltxtimeout = (u32) -1;
Gabor Juhoscbdec972009-07-24 17:27:22 +0200499 ah->power_mode = ATH9K_PM_UNDEFINED;
Felix Fietkau8efa7a82012-03-14 16:40:23 +0100500 ah->htc_reset_init = true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700501}
502
Sujithcbe61d82009-02-09 13:27:12 +0530503static int ath9k_hw_init_macaddr(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700505 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530506 u32 sum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700507 int i;
Sujithf1dc5602008-10-29 10:16:30 +0530508 u16 eeval;
Joe Perches07b2fa52010-11-20 18:38:53 -0800509 static const u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700510
Sujithf1dc5602008-10-29 10:16:30 +0530511 sum = 0;
512 for (i = 0; i < 3; i++) {
Luis R. Rodriguez49101672010-04-15 17:39:13 -0400513 eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530514 sum += eeval;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700515 common->macaddr[2 * i] = eeval >> 8;
516 common->macaddr[2 * i + 1] = eeval & 0xff;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700517 }
Sujithd8baa932009-03-30 15:28:25 +0530518 if (sum == 0 || sum == 0xffff * 3)
Sujithf1dc5602008-10-29 10:16:30 +0530519 return -EADDRNOTAVAIL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700520
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700521 return 0;
522}
523
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700524static int ath9k_hw_post_init(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700525{
Sujith Manoharan6cae913d2011-01-04 13:16:37 +0530526 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700527 int ecode;
528
Sujith Manoharan6cae913d2011-01-04 13:16:37 +0530529 if (common->bus_ops->ath_bus_type != ATH_USB) {
Sujith527d4852010-03-17 14:25:16 +0530530 if (!ath9k_hw_chip_test(ah))
531 return -ENODEV;
532 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700533
Luis R. Rodriguezebd5a142010-04-15 17:39:18 -0400534 if (!AR_SREV_9300_20_OR_LATER(ah)) {
535 ecode = ar9002_hw_rf_claim(ah);
536 if (ecode != 0)
537 return ecode;
538 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700539
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700540 ecode = ath9k_hw_eeprom_init(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700541 if (ecode != 0)
542 return ecode;
Sujith7d01b222009-03-13 08:55:55 +0530543
Joe Perchesd2182b62011-12-15 14:55:53 -0800544 ath_dbg(ath9k_hw_common(ah), CONFIG, "Eeprom VER: %d, REV: %d\n",
Joe Perches226afe62010-12-02 19:12:37 -0800545 ah->eep_ops->get_eeprom_ver(ah),
546 ah->eep_ops->get_eeprom_rev(ah));
Sujith7d01b222009-03-13 08:55:55 +0530547
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400548 ecode = ath9k_hw_rf_alloc_ext_banks(ah);
549 if (ecode) {
Joe Perches38002762010-12-02 19:12:36 -0800550 ath_err(ath9k_hw_common(ah),
551 "Failed allocating banks for external radio\n");
Rajkumar Manoharan48a7c3d2010-11-08 20:40:53 +0530552 ath9k_hw_rf_free_ext_banks(ah);
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400553 return ecode;
Luis R. Rodriguez574d6b12009-10-19 02:33:37 -0400554 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700555
Nikolay Martynov42794252011-12-02 22:39:16 -0500556 if (ah->config.enable_ani) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700557 ath9k_hw_ani_setup(ah);
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700558 ath9k_hw_ani_init(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700559 }
Sujithf1dc5602008-10-29 10:16:30 +0530560
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700561 return 0;
562}
563
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400564static void ath9k_hw_attach_ops(struct ath_hw *ah)
Luis R. Rodriguezee2bb462009-08-03 12:24:39 -0700565{
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400566 if (AR_SREV_9300_20_OR_LATER(ah))
567 ar9003_hw_attach_ops(ah);
568 else
569 ar9002_hw_attach_ops(ah);
Luis R. Rodriguezee2bb462009-08-03 12:24:39 -0700570}
571
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400572/* Called for all hardware families */
573static int __ath9k_hw_init(struct ath_hw *ah)
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700574{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700575 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700576 int r = 0;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700577
Senthil Balasubramanianac45c122010-12-22 21:14:20 +0530578 ath9k_hw_read_revisions(ah);
579
Senthil Balasubramanian0a8d7cb2010-12-22 19:17:18 +0530580 /*
581 * Read back AR_WA into a permanent copy and set bits 14 and 17.
582 * We need to do this to avoid RMW of this register. We cannot
583 * read the reg when chip is asleep.
584 */
585 ah->WARegVal = REG_READ(ah, AR_WA);
586 ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
587 AR_WA_ASPM_TIMER_BASED_DISABLE);
588
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700589 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
Joe Perches38002762010-12-02 19:12:36 -0800590 ath_err(common, "Couldn't reset chip\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700591 return -EIO;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700592 }
593
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +0530594 if (AR_SREV_9462(ah))
Rajkumar Manoharaneec353c2011-10-13 10:49:13 +0530595 ah->WARegVal &= ~AR_WA_D3_L1_DISABLE;
596
Luis R. Rodriguezbab1f622010-04-15 17:38:20 -0400597 ath9k_hw_init_defaults(ah);
598 ath9k_hw_init_config(ah);
599
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400600 ath9k_hw_attach_ops(ah);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400601
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700602 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
Joe Perches38002762010-12-02 19:12:36 -0800603 ath_err(common, "Couldn't wakeup chip\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700604 return -EIO;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700605 }
606
Felix Fietkauf3eef642012-03-14 16:40:25 +0100607 if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700608 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
John W. Linville4c85ab12010-07-28 10:06:35 -0400609 ((AR_SREV_9160(ah) || AR_SREV_9280(ah)) &&
610 !ah->is_pciexpress)) {
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700611 ah->config.serialize_regmode =
612 SER_REG_MODE_ON;
613 } else {
614 ah->config.serialize_regmode =
615 SER_REG_MODE_OFF;
616 }
617 }
618
Joe Perchesd2182b62011-12-15 14:55:53 -0800619 ath_dbg(common, RESET, "serialize_regmode is %d\n",
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700620 ah->config.serialize_regmode);
621
Luis R. Rodriguezf4709fd2009-11-24 21:37:57 -0500622 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
623 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
624 else
625 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
626
Felix Fietkau6da5a722010-12-12 00:51:12 +0100627 switch (ah->hw_version.macVersion) {
628 case AR_SREV_VERSION_5416_PCI:
629 case AR_SREV_VERSION_5416_PCIE:
630 case AR_SREV_VERSION_9160:
631 case AR_SREV_VERSION_9100:
632 case AR_SREV_VERSION_9280:
633 case AR_SREV_VERSION_9285:
634 case AR_SREV_VERSION_9287:
635 case AR_SREV_VERSION_9271:
636 case AR_SREV_VERSION_9300:
Gabor Juhos2c8e5932011-06-21 11:23:21 +0200637 case AR_SREV_VERSION_9330:
Felix Fietkau6da5a722010-12-12 00:51:12 +0100638 case AR_SREV_VERSION_9485:
Vasanthakumar Thiagarajanbca04682011-04-19 19:29:20 +0530639 case AR_SREV_VERSION_9340:
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +0530640 case AR_SREV_VERSION_9462:
Felix Fietkau6da5a722010-12-12 00:51:12 +0100641 break;
642 default:
Joe Perches38002762010-12-02 19:12:36 -0800643 ath_err(common,
644 "Mac Chip Rev 0x%02x.%x is not supported by this driver\n",
645 ah->hw_version.macVersion, ah->hw_version.macRev);
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700646 return -EOPNOTSUPP;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700647 }
648
Gabor Juhos2c8e5932011-06-21 11:23:21 +0200649 if (AR_SREV_9271(ah) || AR_SREV_9100(ah) || AR_SREV_9340(ah) ||
650 AR_SREV_9330(ah))
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400651 ah->is_pciexpress = false;
652
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700653 ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700654 ath9k_hw_init_cal_settings(ah);
655
656 ah->ani_function = ATH9K_ANI_ALL;
Felix Fietkau7a370812010-09-22 12:34:52 +0200657 if (AR_SREV_9280_20_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700658 ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400659 if (!AR_SREV_9300_20_OR_LATER(ah))
660 ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700661
Nikolay Martynov4f17c482011-12-06 21:57:17 -0500662 /* disable ANI for 9340 */
663 if (AR_SREV_9340(ah))
Nikolay Martynov42794252011-12-02 22:39:16 -0500664 ah->config.enable_ani = false;
665
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700666 ath9k_hw_init_mode_regs(ah);
667
Stanislaw Gruszka69ce6742011-08-05 13:10:34 +0200668 if (!ah->is_pciexpress)
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700669 ath9k_hw_disablepcie(ah);
670
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700671 r = ath9k_hw_post_init(ah);
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700672 if (r)
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700673 return r;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700674
675 ath9k_hw_init_mode_gain_regs(ah);
Gabor Juhosa9a29ce2009-11-27 12:01:35 +0100676 r = ath9k_hw_fill_cap_info(ah);
677 if (r)
678 return r;
679
Stanislaw Gruszka69ce6742011-08-05 13:10:34 +0200680 if (ah->is_pciexpress)
681 ath9k_hw_aspm_init(ah);
682
Luis R. Rodriguez4f3acf82009-08-03 12:24:36 -0700683 r = ath9k_hw_init_macaddr(ah);
684 if (r) {
Joe Perches38002762010-12-02 19:12:36 -0800685 ath_err(common, "Failed to initialize MAC address\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700686 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700687 }
688
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400689 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
Sujith2660b812009-02-09 13:27:26 +0530690 ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700691 else
Sujith2660b812009-02-09 13:27:26 +0530692 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700693
Gabor Juhos88e641d2011-06-21 11:23:30 +0200694 if (AR_SREV_9330(ah))
695 ah->bb_watchdog_timeout_ms = 85;
696 else
697 ah->bb_watchdog_timeout_ms = 25;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700698
Luis R. Rodriguez211f5852009-10-06 21:19:07 -0400699 common->state = ATH_HW_INITIALIZED;
700
Luis R. Rodriguez4f3acf82009-08-03 12:24:36 -0700701 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700702}
703
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400704int ath9k_hw_init(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530705{
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400706 int ret;
707 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530708
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400709 /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
710 switch (ah->hw_version.devid) {
711 case AR5416_DEVID_PCI:
712 case AR5416_DEVID_PCIE:
713 case AR5416_AR9100_DEVID:
714 case AR9160_DEVID_PCI:
715 case AR9280_DEVID_PCI:
716 case AR9280_DEVID_PCIE:
717 case AR9285_DEVID_PCIE:
Senthil Balasubramaniandb3cc532010-04-15 17:38:18 -0400718 case AR9287_DEVID_PCI:
719 case AR9287_DEVID_PCIE:
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400720 case AR2427_DEVID_PCIE:
Senthil Balasubramaniandb3cc532010-04-15 17:38:18 -0400721 case AR9300_DEVID_PCIE:
Vasanthakumar Thiagarajan3050c912010-12-06 04:27:36 -0800722 case AR9300_DEVID_AR9485_PCIE:
Gabor Juhos999a7a82011-06-21 11:23:52 +0200723 case AR9300_DEVID_AR9330:
Vasanthakumar Thiagarajanbca04682011-04-19 19:29:20 +0530724 case AR9300_DEVID_AR9340:
Luis R. Rodriguez5a63ef02011-08-24 15:36:08 -0700725 case AR9300_DEVID_AR9580:
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +0530726 case AR9300_DEVID_AR9462:
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400727 break;
728 default:
729 if (common->bus_ops->ath_bus_type == ATH_USB)
730 break;
Joe Perches38002762010-12-02 19:12:36 -0800731 ath_err(common, "Hardware device ID 0x%04x not supported\n",
732 ah->hw_version.devid);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400733 return -EOPNOTSUPP;
734 }
Sujithf1dc5602008-10-29 10:16:30 +0530735
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400736 ret = __ath9k_hw_init(ah);
737 if (ret) {
Joe Perches38002762010-12-02 19:12:36 -0800738 ath_err(common,
739 "Unable to initialize hardware; initialization status: %d\n",
740 ret);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400741 return ret;
742 }
Sujithf1dc5602008-10-29 10:16:30 +0530743
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400744 return 0;
Sujithf1dc5602008-10-29 10:16:30 +0530745}
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400746EXPORT_SYMBOL(ath9k_hw_init);
Sujithf1dc5602008-10-29 10:16:30 +0530747
Sujithcbe61d82009-02-09 13:27:12 +0530748static void ath9k_hw_init_qos(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530749{
Sujith7d0d0df2010-04-16 11:53:57 +0530750 ENABLE_REGWRITE_BUFFER(ah);
751
Sujithf1dc5602008-10-29 10:16:30 +0530752 REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
753 REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
754
755 REG_WRITE(ah, AR_QOS_NO_ACK,
756 SM(2, AR_QOS_NO_ACK_TWO_BIT) |
757 SM(5, AR_QOS_NO_ACK_BIT_OFF) |
758 SM(0, AR_QOS_NO_ACK_BYTE_OFF));
759
760 REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
761 REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
762 REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
763 REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
764 REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
Sujith7d0d0df2010-04-16 11:53:57 +0530765
766 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530767}
768
Senthil Balasubramanianb84628e2011-04-22 11:32:12 +0530769u32 ar9003_get_pll_sqsum_dvc(struct ath_hw *ah)
Vivek Natarajanb1415812011-01-27 14:45:07 +0530770{
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100771 REG_CLR_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
772 udelay(100);
773 REG_SET_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
774
775 while ((REG_READ(ah, PLL4) & PLL4_MEAS_DONE) == 0)
Vivek Natarajanb1415812011-01-27 14:45:07 +0530776 udelay(100);
Vivek Natarajanb1415812011-01-27 14:45:07 +0530777
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100778 return (REG_READ(ah, PLL3) & SQSUM_DVC_MASK) >> 3;
Vivek Natarajanb1415812011-01-27 14:45:07 +0530779}
780EXPORT_SYMBOL(ar9003_get_pll_sqsum_dvc);
781
Sujithcbe61d82009-02-09 13:27:12 +0530782static void ath9k_hw_init_pll(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530783 struct ath9k_channel *chan)
784{
Vasanthakumar Thiagarajand09b17f2010-12-06 04:27:44 -0800785 u32 pll;
786
Vivek Natarajan22983c32011-01-27 14:45:09 +0530787 if (AR_SREV_9485(ah)) {
Vivek Natarajan22983c32011-01-27 14:45:09 +0530788
Vasanthakumar Thiagarajan3dfd7f62011-04-11 16:39:40 +0530789 /* program BB PLL ki and kd value, ki=0x4, kd=0x40 */
790 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
791 AR_CH0_BB_DPLL2_PLL_PWD, 0x1);
792 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
793 AR_CH0_DPLL2_KD, 0x40);
794 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
795 AR_CH0_DPLL2_KI, 0x4);
Vivek Natarajan22983c32011-01-27 14:45:09 +0530796
Vasanthakumar Thiagarajan3dfd7f62011-04-11 16:39:40 +0530797 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
798 AR_CH0_BB_DPLL1_REFDIV, 0x5);
799 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
800 AR_CH0_BB_DPLL1_NINI, 0x58);
801 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
802 AR_CH0_BB_DPLL1_NFRAC, 0x0);
803
804 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
805 AR_CH0_BB_DPLL2_OUTDIV, 0x1);
806 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
807 AR_CH0_BB_DPLL2_LOCAL_PLL, 0x1);
808 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
809 AR_CH0_BB_DPLL2_EN_NEGTRIG, 0x1);
810
811 /* program BB PLL phase_shift to 0x6 */
812 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
813 AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x6);
814
815 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
816 AR_CH0_BB_DPLL2_PLL_PWD, 0x0);
Vivek Natarajan75e03512011-03-10 11:05:42 +0530817 udelay(1000);
Gabor Juhosa5415d62011-06-21 11:23:29 +0200818 } else if (AR_SREV_9330(ah)) {
819 u32 ddr_dpll2, pll_control2, kd;
820
821 if (ah->is_clk_25mhz) {
822 ddr_dpll2 = 0x18e82f01;
823 pll_control2 = 0xe04a3d;
824 kd = 0x1d;
825 } else {
826 ddr_dpll2 = 0x19e82f01;
827 pll_control2 = 0x886666;
828 kd = 0x3d;
829 }
830
831 /* program DDR PLL ki and kd value */
832 REG_WRITE(ah, AR_CH0_DDR_DPLL2, ddr_dpll2);
833
834 /* program DDR PLL phase_shift */
835 REG_RMW_FIELD(ah, AR_CH0_DDR_DPLL3,
836 AR_CH0_DPLL3_PHASE_SHIFT, 0x1);
837
838 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
839 udelay(1000);
840
841 /* program refdiv, nint, frac to RTC register */
842 REG_WRITE(ah, AR_RTC_PLL_CONTROL2, pll_control2);
843
844 /* program BB PLL kd and ki value */
845 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KD, kd);
846 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KI, 0x06);
847
848 /* program BB PLL phase_shift */
849 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
850 AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x1);
Vasanthakumar Thiagarajan0b488ac2011-04-20 10:26:15 +0530851 } else if (AR_SREV_9340(ah)) {
852 u32 regval, pll2_divint, pll2_divfrac, refdiv;
853
854 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
855 udelay(1000);
856
857 REG_SET_BIT(ah, AR_PHY_PLL_MODE, 0x1 << 16);
858 udelay(100);
859
860 if (ah->is_clk_25mhz) {
861 pll2_divint = 0x54;
862 pll2_divfrac = 0x1eb85;
863 refdiv = 3;
864 } else {
865 pll2_divint = 88;
866 pll2_divfrac = 0;
867 refdiv = 5;
868 }
869
870 regval = REG_READ(ah, AR_PHY_PLL_MODE);
871 regval |= (0x1 << 16);
872 REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
873 udelay(100);
874
875 REG_WRITE(ah, AR_PHY_PLL_CONTROL, (refdiv << 27) |
876 (pll2_divint << 18) | pll2_divfrac);
877 udelay(100);
878
879 regval = REG_READ(ah, AR_PHY_PLL_MODE);
880 regval = (regval & 0x80071fff) | (0x1 << 30) | (0x1 << 13) |
881 (0x4 << 26) | (0x18 << 19);
882 REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
883 REG_WRITE(ah, AR_PHY_PLL_MODE,
884 REG_READ(ah, AR_PHY_PLL_MODE) & 0xfffeffff);
885 udelay(1000);
Vivek Natarajan22983c32011-01-27 14:45:09 +0530886 }
Vasanthakumar Thiagarajand09b17f2010-12-06 04:27:44 -0800887
888 pll = ath9k_hw_compute_pll_control(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +0530889
Gabor Juhosd03a66c2009-01-14 20:17:09 +0100890 REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
Sujithf1dc5602008-10-29 10:16:30 +0530891
Gabor Juhosa5415d62011-06-21 11:23:29 +0200892 if (AR_SREV_9485(ah) || AR_SREV_9340(ah) || AR_SREV_9330(ah))
Vasanthakumar Thiagarajan3dfd7f62011-04-11 16:39:40 +0530893 udelay(1000);
894
Luis R. Rodriguezc75724d2009-10-19 02:33:34 -0400895 /* Switch the core clock for ar9271 to 117Mhz */
896 if (AR_SREV_9271(ah)) {
Sujith25e2ab12010-03-17 14:25:22 +0530897 udelay(500);
898 REG_WRITE(ah, 0x50040, 0x304);
Luis R. Rodriguezc75724d2009-10-19 02:33:34 -0400899 }
900
Sujithf1dc5602008-10-29 10:16:30 +0530901 udelay(RTC_PLL_SETTLE_DELAY);
902
903 REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
Vasanthakumar Thiagarajan0b488ac2011-04-20 10:26:15 +0530904
905 if (AR_SREV_9340(ah)) {
906 if (ah->is_clk_25mhz) {
907 REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x17c << 1);
908 REG_WRITE(ah, AR_SLP32_MODE, 0x0010f3d7);
909 REG_WRITE(ah, AR_SLP32_INC, 0x0001e7ae);
910 } else {
911 REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x261 << 1);
912 REG_WRITE(ah, AR_SLP32_MODE, 0x0010f400);
913 REG_WRITE(ah, AR_SLP32_INC, 0x0001e800);
914 }
915 udelay(100);
916 }
Sujithf1dc5602008-10-29 10:16:30 +0530917}
918
Sujithcbe61d82009-02-09 13:27:12 +0530919static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
Colin McCabed97809d2008-12-01 13:38:55 -0800920 enum nl80211_iftype opmode)
Sujithf1dc5602008-10-29 10:16:30 +0530921{
Vasanthakumar Thiagarajan79d1d2b2011-04-19 19:29:19 +0530922 u32 sync_default = AR_INTR_SYNC_DEFAULT;
Pavel Roskin152d5302010-03-31 18:05:37 -0400923 u32 imr_reg = AR_IMR_TXERR |
Sujithf1dc5602008-10-29 10:16:30 +0530924 AR_IMR_TXURN |
925 AR_IMR_RXERR |
926 AR_IMR_RXORN |
927 AR_IMR_BCNMISC;
928
Vasanthakumar Thiagarajan79d1d2b2011-04-19 19:29:19 +0530929 if (AR_SREV_9340(ah))
930 sync_default &= ~AR_INTR_SYNC_HOST1_FATAL;
931
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400932 if (AR_SREV_9300_20_OR_LATER(ah)) {
933 imr_reg |= AR_IMR_RXOK_HP;
934 if (ah->config.rx_intr_mitigation)
935 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
936 else
937 imr_reg |= AR_IMR_RXOK_LP;
Sujithf1dc5602008-10-29 10:16:30 +0530938
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400939 } else {
940 if (ah->config.rx_intr_mitigation)
941 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
942 else
943 imr_reg |= AR_IMR_RXOK;
944 }
945
946 if (ah->config.tx_intr_mitigation)
947 imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
948 else
949 imr_reg |= AR_IMR_TXOK;
Sujithf1dc5602008-10-29 10:16:30 +0530950
Colin McCabed97809d2008-12-01 13:38:55 -0800951 if (opmode == NL80211_IFTYPE_AP)
Pavel Roskin152d5302010-03-31 18:05:37 -0400952 imr_reg |= AR_IMR_MIB;
Sujithf1dc5602008-10-29 10:16:30 +0530953
Sujith7d0d0df2010-04-16 11:53:57 +0530954 ENABLE_REGWRITE_BUFFER(ah);
955
Pavel Roskin152d5302010-03-31 18:05:37 -0400956 REG_WRITE(ah, AR_IMR, imr_reg);
Pavel Roskin74bad5c2010-02-23 18:15:27 -0500957 ah->imrs2_reg |= AR_IMR_S2_GTT;
958 REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
Sujithf1dc5602008-10-29 10:16:30 +0530959
960 if (!AR_SREV_9100(ah)) {
961 REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
Vasanthakumar Thiagarajan79d1d2b2011-04-19 19:29:19 +0530962 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, sync_default);
Sujithf1dc5602008-10-29 10:16:30 +0530963 REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
964 }
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400965
Sujith7d0d0df2010-04-16 11:53:57 +0530966 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +0530967
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400968 if (AR_SREV_9300_20_OR_LATER(ah)) {
969 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
970 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
971 REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
972 REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
973 }
Sujithf1dc5602008-10-29 10:16:30 +0530974}
975
Felix Fietkaub6ba41b2011-07-09 11:12:50 +0700976static void ath9k_hw_set_sifs_time(struct ath_hw *ah, u32 us)
977{
978 u32 val = ath9k_hw_mac_to_clks(ah, us - 2);
979 val = min(val, (u32) 0xFFFF);
980 REG_WRITE(ah, AR_D_GBL_IFS_SIFS, val);
981}
982
Felix Fietkau0005baf2010-01-15 02:33:40 +0100983static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
Sujithf1dc5602008-10-29 10:16:30 +0530984{
Felix Fietkau0005baf2010-01-15 02:33:40 +0100985 u32 val = ath9k_hw_mac_to_clks(ah, us);
986 val = min(val, (u32) 0xFFFF);
987 REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
Sujithf1dc5602008-10-29 10:16:30 +0530988}
989
Felix Fietkau0005baf2010-01-15 02:33:40 +0100990static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
Sujithf1dc5602008-10-29 10:16:30 +0530991{
Felix Fietkau0005baf2010-01-15 02:33:40 +0100992 u32 val = ath9k_hw_mac_to_clks(ah, us);
993 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
994 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
995}
996
997static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
998{
999 u32 val = ath9k_hw_mac_to_clks(ah, us);
1000 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
1001 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
Sujithf1dc5602008-10-29 10:16:30 +05301002}
1003
Sujithcbe61d82009-02-09 13:27:12 +05301004static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
Sujithf1dc5602008-10-29 10:16:30 +05301005{
Sujithf1dc5602008-10-29 10:16:30 +05301006 if (tu > 0xFFFF) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001007 ath_dbg(ath9k_hw_common(ah), XMIT, "bad global tx timeout %u\n",
1008 tu);
Sujith2660b812009-02-09 13:27:26 +05301009 ah->globaltxtimeout = (u32) -1;
Sujithf1dc5602008-10-29 10:16:30 +05301010 return false;
1011 } else {
1012 REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
Sujith2660b812009-02-09 13:27:26 +05301013 ah->globaltxtimeout = tu;
Sujithf1dc5602008-10-29 10:16:30 +05301014 return true;
1015 }
1016}
1017
Felix Fietkau0005baf2010-01-15 02:33:40 +01001018void ath9k_hw_init_global_settings(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301019{
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001020 struct ath_common *common = ath9k_hw_common(ah);
1021 struct ieee80211_conf *conf = &common->hw->conf;
1022 const struct ath9k_channel *chan = ah->curchan;
Felix Fietkauadb50662011-08-28 01:52:10 +02001023 int acktimeout, ctstimeout;
Felix Fietkaue239d852010-01-15 02:34:58 +01001024 int slottime;
Felix Fietkau0005baf2010-01-15 02:33:40 +01001025 int sifstime;
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001026 int rx_lat = 0, tx_lat = 0, eifs = 0;
1027 u32 reg;
Felix Fietkau0005baf2010-01-15 02:33:40 +01001028
Joe Perchesd2182b62011-12-15 14:55:53 -08001029 ath_dbg(ath9k_hw_common(ah), RESET, "ah->misc_mode 0x%x\n",
Joe Perches226afe62010-12-02 19:12:37 -08001030 ah->misc_mode);
Sujithf1dc5602008-10-29 10:16:30 +05301031
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001032 if (!chan)
1033 return;
1034
Sujith2660b812009-02-09 13:27:26 +05301035 if (ah->misc_mode != 0)
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001036 REG_SET_BIT(ah, AR_PCU_MISC, ah->misc_mode);
Felix Fietkau0005baf2010-01-15 02:33:40 +01001037
Rajkumar Manoharan81a91d52011-08-31 10:47:30 +05301038 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
1039 rx_lat = 41;
1040 else
1041 rx_lat = 37;
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001042 tx_lat = 54;
1043
1044 if (IS_CHAN_HALF_RATE(chan)) {
1045 eifs = 175;
1046 rx_lat *= 2;
1047 tx_lat *= 2;
1048 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
1049 tx_lat += 11;
1050
1051 slottime = 13;
1052 sifstime = 32;
1053 } else if (IS_CHAN_QUARTER_RATE(chan)) {
1054 eifs = 340;
Rajkumar Manoharan81a91d52011-08-31 10:47:30 +05301055 rx_lat = (rx_lat * 4) - 1;
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001056 tx_lat *= 4;
1057 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
1058 tx_lat += 22;
1059
1060 slottime = 21;
1061 sifstime = 64;
1062 } else {
Rajkumar Manoharana7be0392011-08-27 12:13:21 +05301063 if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
1064 eifs = AR_D_GBL_IFS_EIFS_ASYNC_FIFO;
1065 reg = AR_USEC_ASYNC_FIFO;
1066 } else {
1067 eifs = REG_READ(ah, AR_D_GBL_IFS_EIFS)/
1068 common->clockrate;
1069 reg = REG_READ(ah, AR_USEC);
1070 }
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001071 rx_lat = MS(reg, AR_USEC_RX_LAT);
1072 tx_lat = MS(reg, AR_USEC_TX_LAT);
1073
1074 slottime = ah->slottime;
1075 if (IS_CHAN_5GHZ(chan))
1076 sifstime = 16;
1077 else
1078 sifstime = 10;
1079 }
Felix Fietkau0005baf2010-01-15 02:33:40 +01001080
Felix Fietkaue239d852010-01-15 02:34:58 +01001081 /* As defined by IEEE 802.11-2007 17.3.8.6 */
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001082 acktimeout = slottime + sifstime + 3 * ah->coverage_class;
Felix Fietkauadb50662011-08-28 01:52:10 +02001083 ctstimeout = acktimeout;
Felix Fietkau42c45682010-02-11 18:07:19 +01001084
1085 /*
1086 * Workaround for early ACK timeouts, add an offset to match the
Felix Fietkau55a2bb42012-02-05 21:15:18 +01001087 * initval's 64us ack timeout value. Use 48us for the CTS timeout.
Felix Fietkau42c45682010-02-11 18:07:19 +01001088 * This was initially only meant to work around an issue with delayed
1089 * BA frames in some implementations, but it has been found to fix ACK
1090 * timeout issues in other cases as well.
1091 */
Felix Fietkau55a2bb42012-02-05 21:15:18 +01001092 if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ) {
Felix Fietkau42c45682010-02-11 18:07:19 +01001093 acktimeout += 64 - sifstime - ah->slottime;
Felix Fietkau55a2bb42012-02-05 21:15:18 +01001094 ctstimeout += 48 - sifstime - ah->slottime;
1095 }
1096
Felix Fietkau42c45682010-02-11 18:07:19 +01001097
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001098 ath9k_hw_set_sifs_time(ah, sifstime);
1099 ath9k_hw_setslottime(ah, slottime);
Felix Fietkau0005baf2010-01-15 02:33:40 +01001100 ath9k_hw_set_ack_timeout(ah, acktimeout);
Felix Fietkauadb50662011-08-28 01:52:10 +02001101 ath9k_hw_set_cts_timeout(ah, ctstimeout);
Sujith2660b812009-02-09 13:27:26 +05301102 if (ah->globaltxtimeout != (u32) -1)
1103 ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
Felix Fietkaub6ba41b2011-07-09 11:12:50 +07001104
1105 REG_WRITE(ah, AR_D_GBL_IFS_EIFS, ath9k_hw_mac_to_clks(ah, eifs));
1106 REG_RMW(ah, AR_USEC,
1107 (common->clockrate - 1) |
1108 SM(rx_lat, AR_USEC_RX_LAT) |
1109 SM(tx_lat, AR_USEC_TX_LAT),
1110 AR_USEC_TX_LAT | AR_USEC_RX_LAT | AR_USEC_USEC);
1111
Sujithf1dc5602008-10-29 10:16:30 +05301112}
Felix Fietkau0005baf2010-01-15 02:33:40 +01001113EXPORT_SYMBOL(ath9k_hw_init_global_settings);
Sujithf1dc5602008-10-29 10:16:30 +05301114
Sujith285f2dd2010-01-08 10:36:07 +05301115void ath9k_hw_deinit(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001116{
Luis R. Rodriguez211f5852009-10-06 21:19:07 -04001117 struct ath_common *common = ath9k_hw_common(ah);
1118
Sujith736b3a22010-03-17 14:25:24 +05301119 if (common->state < ATH_HW_INITIALIZED)
Luis R. Rodriguez211f5852009-10-06 21:19:07 -04001120 goto free_hw;
1121
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001122 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
Luis R. Rodriguez211f5852009-10-06 21:19:07 -04001123
1124free_hw:
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001125 ath9k_hw_rf_free_ext_banks(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001126}
Sujith285f2dd2010-01-08 10:36:07 +05301127EXPORT_SYMBOL(ath9k_hw_deinit);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001128
Sujithf1dc5602008-10-29 10:16:30 +05301129/*******/
1130/* INI */
1131/*******/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001132
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001133u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
Bob Copeland3a702e42009-03-30 22:30:29 -04001134{
1135 u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
1136
1137 if (IS_CHAN_B(chan))
1138 ctl |= CTL_11B;
1139 else if (IS_CHAN_G(chan))
1140 ctl |= CTL_11G;
1141 else
1142 ctl |= CTL_11A;
1143
1144 return ctl;
1145}
1146
Sujithf1dc5602008-10-29 10:16:30 +05301147/****************************************/
1148/* Reset and Channel Switching Routines */
1149/****************************************/
1150
Sujithcbe61d82009-02-09 13:27:12 +05301151static inline void ath9k_hw_set_dma(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301152{
Felix Fietkau57b32222010-04-15 17:39:22 -04001153 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301154
Sujith7d0d0df2010-04-16 11:53:57 +05301155 ENABLE_REGWRITE_BUFFER(ah);
1156
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001157 /*
1158 * set AHB_MODE not to do cacheline prefetches
1159 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001160 if (!AR_SREV_9300_20_OR_LATER(ah))
1161 REG_SET_BIT(ah, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
Sujithf1dc5602008-10-29 10:16:30 +05301162
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001163 /*
1164 * let mac dma reads be in 128 byte chunks
1165 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001166 REG_RMW(ah, AR_TXCFG, AR_TXCFG_DMASZ_128B, AR_TXCFG_DMASZ_MASK);
Sujithf1dc5602008-10-29 10:16:30 +05301167
Sujith7d0d0df2010-04-16 11:53:57 +05301168 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301169
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001170 /*
1171 * Restore TX Trigger Level to its pre-reset value.
1172 * The initial value depends on whether aggregation is enabled, and is
1173 * adjusted whenever underruns are detected.
1174 */
Felix Fietkau57b32222010-04-15 17:39:22 -04001175 if (!AR_SREV_9300_20_OR_LATER(ah))
1176 REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
Sujithf1dc5602008-10-29 10:16:30 +05301177
Sujith7d0d0df2010-04-16 11:53:57 +05301178 ENABLE_REGWRITE_BUFFER(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301179
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001180 /*
1181 * let mac dma writes be in 128 byte chunks
1182 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001183 REG_RMW(ah, AR_RXCFG, AR_RXCFG_DMASZ_128B, AR_RXCFG_DMASZ_MASK);
Sujithf1dc5602008-10-29 10:16:30 +05301184
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001185 /*
1186 * Setup receive FIFO threshold to hold off TX activities
1187 */
Sujithf1dc5602008-10-29 10:16:30 +05301188 REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
1189
Felix Fietkau57b32222010-04-15 17:39:22 -04001190 if (AR_SREV_9300_20_OR_LATER(ah)) {
1191 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
1192 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
1193
1194 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
1195 ah->caps.rx_status_len);
1196 }
1197
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001198 /*
1199 * reduce the number of usable entries in PCU TXBUF to avoid
1200 * wrap around issues.
1201 */
Sujithf1dc5602008-10-29 10:16:30 +05301202 if (AR_SREV_9285(ah)) {
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001203 /* For AR9285 the number of Fifos are reduced to half.
1204 * So set the usable tx buf size also to half to
1205 * avoid data/delimiter underruns
1206 */
Sujithf1dc5602008-10-29 10:16:30 +05301207 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
1208 AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001209 } else if (!AR_SREV_9271(ah)) {
Sujithf1dc5602008-10-29 10:16:30 +05301210 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
1211 AR_PCU_TXBUF_CTRL_USABLE_SIZE);
1212 }
Vasanthakumar Thiagarajan744d4022010-04-15 17:39:27 -04001213
Sujith7d0d0df2010-04-16 11:53:57 +05301214 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301215
Vasanthakumar Thiagarajan744d4022010-04-15 17:39:27 -04001216 if (AR_SREV_9300_20_OR_LATER(ah))
1217 ath9k_hw_reset_txstatus_ring(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301218}
1219
Sujithcbe61d82009-02-09 13:27:12 +05301220static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
Sujithf1dc5602008-10-29 10:16:30 +05301221{
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001222 u32 mask = AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC;
1223 u32 set = AR_STA_ID1_KSRCH_MODE;
Sujithf1dc5602008-10-29 10:16:30 +05301224
Sujithf1dc5602008-10-29 10:16:30 +05301225 switch (opmode) {
Colin McCabed97809d2008-12-01 13:38:55 -08001226 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -04001227 case NL80211_IFTYPE_MESH_POINT:
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001228 set |= AR_STA_ID1_ADHOC;
Sujithf1dc5602008-10-29 10:16:30 +05301229 REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
1230 break;
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001231 case NL80211_IFTYPE_AP:
1232 set |= AR_STA_ID1_STA_AP;
1233 /* fall through */
Colin McCabed97809d2008-12-01 13:38:55 -08001234 case NL80211_IFTYPE_STATION:
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001235 REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
Sujithf1dc5602008-10-29 10:16:30 +05301236 break;
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301237 default:
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001238 if (!ah->is_monitoring)
1239 set = 0;
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301240 break;
Sujithf1dc5602008-10-29 10:16:30 +05301241 }
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001242 REG_RMW(ah, AR_STA_ID1, set, mask);
Sujithf1dc5602008-10-29 10:16:30 +05301243}
1244
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001245void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
1246 u32 *coef_mantissa, u32 *coef_exponent)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001247{
1248 u32 coef_exp, coef_man;
1249
1250 for (coef_exp = 31; coef_exp > 0; coef_exp--)
1251 if ((coef_scaled >> coef_exp) & 0x1)
1252 break;
1253
1254 coef_exp = 14 - (coef_exp - COEF_SCALE_S);
1255
1256 coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
1257
1258 *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
1259 *coef_exponent = coef_exp - 16;
1260}
1261
Sujithcbe61d82009-02-09 13:27:12 +05301262static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
Sujithf1dc5602008-10-29 10:16:30 +05301263{
1264 u32 rst_flags;
1265 u32 tmpReg;
1266
Sujith70768492009-02-16 13:23:12 +05301267 if (AR_SREV_9100(ah)) {
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001268 REG_RMW_FIELD(ah, AR_RTC_DERIVED_CLK,
1269 AR_RTC_DERIVED_CLK_PERIOD, 1);
Sujith70768492009-02-16 13:23:12 +05301270 (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
1271 }
1272
Sujith7d0d0df2010-04-16 11:53:57 +05301273 ENABLE_REGWRITE_BUFFER(ah);
1274
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001275 if (AR_SREV_9300_20_OR_LATER(ah)) {
1276 REG_WRITE(ah, AR_WA, ah->WARegVal);
1277 udelay(10);
1278 }
1279
Sujithf1dc5602008-10-29 10:16:30 +05301280 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1281 AR_RTC_FORCE_WAKE_ON_INT);
1282
1283 if (AR_SREV_9100(ah)) {
1284 rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
1285 AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
1286 } else {
1287 tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
1288 if (tmpReg &
1289 (AR_INTR_SYNC_LOCAL_TIMEOUT |
1290 AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001291 u32 val;
Sujithf1dc5602008-10-29 10:16:30 +05301292 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001293
1294 val = AR_RC_HOSTIF;
1295 if (!AR_SREV_9300_20_OR_LATER(ah))
1296 val |= AR_RC_AHB;
1297 REG_WRITE(ah, AR_RC, val);
1298
1299 } else if (!AR_SREV_9300_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05301300 REG_WRITE(ah, AR_RC, AR_RC_AHB);
Sujithf1dc5602008-10-29 10:16:30 +05301301
1302 rst_flags = AR_RTC_RC_MAC_WARM;
1303 if (type == ATH9K_RESET_COLD)
1304 rst_flags |= AR_RTC_RC_MAC_COLD;
1305 }
1306
Gabor Juhos7d95847c2011-06-21 11:23:51 +02001307 if (AR_SREV_9330(ah)) {
1308 int npend = 0;
1309 int i;
1310
1311 /* AR9330 WAR:
1312 * call external reset function to reset WMAC if:
1313 * - doing a cold reset
1314 * - we have pending frames in the TX queues
1315 */
1316
1317 for (i = 0; i < AR_NUM_QCU; i++) {
1318 npend = ath9k_hw_numtxpending(ah, i);
1319 if (npend)
1320 break;
1321 }
1322
1323 if (ah->external_reset &&
1324 (npend || type == ATH9K_RESET_COLD)) {
1325 int reset_err = 0;
1326
Joe Perchesd2182b62011-12-15 14:55:53 -08001327 ath_dbg(ath9k_hw_common(ah), RESET,
Gabor Juhos7d95847c2011-06-21 11:23:51 +02001328 "reset MAC via external reset\n");
1329
1330 reset_err = ah->external_reset();
1331 if (reset_err) {
1332 ath_err(ath9k_hw_common(ah),
1333 "External reset failed, err=%d\n",
1334 reset_err);
1335 return false;
1336 }
1337
1338 REG_WRITE(ah, AR_RTC_RESET, 1);
1339 }
1340 }
1341
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001342 REG_WRITE(ah, AR_RTC_RC, rst_flags);
Sujith7d0d0df2010-04-16 11:53:57 +05301343
1344 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301345
Sujithf1dc5602008-10-29 10:16:30 +05301346 udelay(50);
1347
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001348 REG_WRITE(ah, AR_RTC_RC, 0);
Sujith0caa7b12009-02-16 13:23:20 +05301349 if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001350 ath_dbg(ath9k_hw_common(ah), RESET, "RTC stuck in MAC reset\n");
Sujithf1dc5602008-10-29 10:16:30 +05301351 return false;
1352 }
1353
1354 if (!AR_SREV_9100(ah))
1355 REG_WRITE(ah, AR_RC, 0);
1356
Sujithf1dc5602008-10-29 10:16:30 +05301357 if (AR_SREV_9100(ah))
1358 udelay(50);
1359
1360 return true;
1361}
1362
Sujithcbe61d82009-02-09 13:27:12 +05301363static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301364{
Sujith7d0d0df2010-04-16 11:53:57 +05301365 ENABLE_REGWRITE_BUFFER(ah);
1366
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001367 if (AR_SREV_9300_20_OR_LATER(ah)) {
1368 REG_WRITE(ah, AR_WA, ah->WARegVal);
1369 udelay(10);
1370 }
1371
Sujithf1dc5602008-10-29 10:16:30 +05301372 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1373 AR_RTC_FORCE_WAKE_ON_INT);
1374
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001375 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301376 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1377
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001378 REG_WRITE(ah, AR_RTC_RESET, 0);
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301379
Sujith7d0d0df2010-04-16 11:53:57 +05301380 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301381
Senthil Balasubramanian84e21692010-04-15 17:38:30 -04001382 if (!AR_SREV_9300_20_OR_LATER(ah))
1383 udelay(2);
1384
1385 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301386 REG_WRITE(ah, AR_RC, 0);
1387
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001388 REG_WRITE(ah, AR_RTC_RESET, 1);
Sujithf1dc5602008-10-29 10:16:30 +05301389
1390 if (!ath9k_hw_wait(ah,
1391 AR_RTC_STATUS,
1392 AR_RTC_STATUS_M,
Sujith0caa7b12009-02-16 13:23:20 +05301393 AR_RTC_STATUS_ON,
1394 AH_WAIT_TIMEOUT)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001395 ath_dbg(ath9k_hw_common(ah), RESET, "RTC not waking up\n");
Sujithf1dc5602008-10-29 10:16:30 +05301396 return false;
1397 }
1398
Sujithf1dc5602008-10-29 10:16:30 +05301399 return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
1400}
1401
Sujithcbe61d82009-02-09 13:27:12 +05301402static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
Sujithf1dc5602008-10-29 10:16:30 +05301403{
Mohammed Shafi Shajakhan7a9233f2011-11-30 10:41:25 +05301404 bool ret = false;
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05301405
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001406 if (AR_SREV_9300_20_OR_LATER(ah)) {
1407 REG_WRITE(ah, AR_WA, ah->WARegVal);
1408 udelay(10);
1409 }
1410
Sujithf1dc5602008-10-29 10:16:30 +05301411 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1412 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
1413
1414 switch (type) {
1415 case ATH9K_RESET_POWER_ON:
Mohammed Shafi Shajakhan7a9233f2011-11-30 10:41:25 +05301416 ret = ath9k_hw_set_reset_power_on(ah);
1417 break;
Sujithf1dc5602008-10-29 10:16:30 +05301418 case ATH9K_RESET_WARM:
1419 case ATH9K_RESET_COLD:
Mohammed Shafi Shajakhan7a9233f2011-11-30 10:41:25 +05301420 ret = ath9k_hw_set_reset(ah, type);
1421 break;
Sujithf1dc5602008-10-29 10:16:30 +05301422 default:
Mohammed Shafi Shajakhan7a9233f2011-11-30 10:41:25 +05301423 break;
Sujithf1dc5602008-10-29 10:16:30 +05301424 }
Mohammed Shafi Shajakhan7a9233f2011-11-30 10:41:25 +05301425
1426 if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
1427 REG_WRITE(ah, AR_RTC_KEEP_AWAKE, 0x2);
1428
1429 return ret;
Sujithf1dc5602008-10-29 10:16:30 +05301430}
1431
Sujithcbe61d82009-02-09 13:27:12 +05301432static bool ath9k_hw_chip_reset(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05301433 struct ath9k_channel *chan)
1434{
Felix Fietkau9c083af2012-03-03 15:17:02 +01001435 int reset_type = ATH9K_RESET_WARM;
1436
1437 if (AR_SREV_9280(ah)) {
1438 if (ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
1439 reset_type = ATH9K_RESET_POWER_ON;
1440 else
1441 reset_type = ATH9K_RESET_COLD;
1442 }
1443
1444 if (!ath9k_hw_set_reset_reg(ah, reset_type))
Sujithf1dc5602008-10-29 10:16:30 +05301445 return false;
1446
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001447 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Sujithf1dc5602008-10-29 10:16:30 +05301448 return false;
1449
Sujith2660b812009-02-09 13:27:26 +05301450 ah->chip_fullsleep = false;
Sujithf1dc5602008-10-29 10:16:30 +05301451 ath9k_hw_init_pll(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301452 ath9k_hw_set_rfmode(ah, chan);
1453
1454 return true;
1455}
1456
Sujithcbe61d82009-02-09 13:27:12 +05301457static bool ath9k_hw_channel_change(struct ath_hw *ah,
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07001458 struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +05301459{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001460 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001461 u32 qnum;
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001462 int r;
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301463 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1464 bool band_switch, mode_diff;
1465 u8 ini_reloaded;
1466
1467 band_switch = (chan->channelFlags & (CHANNEL_2GHZ | CHANNEL_5GHZ)) !=
1468 (ah->curchan->channelFlags & (CHANNEL_2GHZ |
1469 CHANNEL_5GHZ));
1470 mode_diff = (chan->chanmode != ah->curchan->chanmode);
Sujithf1dc5602008-10-29 10:16:30 +05301471
1472 for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
1473 if (ath9k_hw_numtxpending(ah, qnum)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001474 ath_dbg(common, QUEUE,
Joe Perches226afe62010-12-02 19:12:37 -08001475 "Transmit frames pending on queue %d\n", qnum);
Sujithf1dc5602008-10-29 10:16:30 +05301476 return false;
1477 }
1478 }
1479
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001480 if (!ath9k_hw_rfbus_req(ah)) {
Joe Perches38002762010-12-02 19:12:36 -08001481 ath_err(common, "Could not kill baseband RX\n");
Sujithf1dc5602008-10-29 10:16:30 +05301482 return false;
1483 }
1484
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301485 if (edma && (band_switch || mode_diff)) {
1486 ath9k_hw_mark_phy_inactive(ah);
1487 udelay(5);
1488
1489 ath9k_hw_init_pll(ah, NULL);
1490
1491 if (ath9k_hw_fast_chan_change(ah, chan, &ini_reloaded)) {
1492 ath_err(common, "Failed to do fast channel change\n");
1493 return false;
1494 }
1495 }
1496
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001497 ath9k_hw_set_channel_regs(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301498
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001499 r = ath9k_hw_rf_set_freq(ah, chan);
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001500 if (r) {
Joe Perches38002762010-12-02 19:12:36 -08001501 ath_err(common, "Failed to set channel\n");
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001502 return false;
Sujithf1dc5602008-10-29 10:16:30 +05301503 }
Felix Fietkaudfdac8a2010-10-08 22:13:51 +02001504 ath9k_hw_set_clockrate(ah);
Felix Fietkauca2c68c2011-10-08 20:06:20 +02001505 ath9k_hw_apply_txpower(ah, chan);
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001506 ath9k_hw_rfbus_done(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301507
1508 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1509 ath9k_hw_set_delta_slope(ah, chan);
1510
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001511 ath9k_hw_spur_mitigate_freq(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301512
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301513 if (edma && (band_switch || mode_diff)) {
Rajkumar Manoharana126ff52011-10-13 11:00:42 +05301514 ah->ah_flags |= AH_FASTCC;
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301515 if (band_switch || ini_reloaded)
1516 ah->eep_ops->set_board_values(ah, chan);
1517
1518 ath9k_hw_init_bb(ah, chan);
1519
1520 if (band_switch || ini_reloaded)
1521 ath9k_hw_init_cal(ah, chan);
Rajkumar Manoharana126ff52011-10-13 11:00:42 +05301522 ah->ah_flags &= ~AH_FASTCC;
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301523 }
1524
Sujithf1dc5602008-10-29 10:16:30 +05301525 return true;
1526}
1527
Felix Fietkau691680b2011-03-19 13:55:38 +01001528static void ath9k_hw_apply_gpio_override(struct ath_hw *ah)
1529{
1530 u32 gpio_mask = ah->gpio_mask;
1531 int i;
1532
1533 for (i = 0; gpio_mask; i++, gpio_mask >>= 1) {
1534 if (!(gpio_mask & 1))
1535 continue;
1536
1537 ath9k_hw_cfg_output(ah, i, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1538 ath9k_hw_set_gpio(ah, i, !!(ah->gpio_val & BIT(i)));
1539 }
1540}
1541
Rajkumar Manoharan01e18912012-03-15 05:34:27 +05301542static bool ath9k_hw_check_dcs(u32 dma_dbg, u32 num_dcu_states,
1543 int *hang_state, int *hang_pos)
1544{
1545 static u32 dcu_chain_state[] = {5, 6, 9}; /* DCU chain stuck states */
1546 u32 chain_state, dcs_pos, i;
1547
1548 for (dcs_pos = 0; dcs_pos < num_dcu_states; dcs_pos++) {
1549 chain_state = (dma_dbg >> (5 * dcs_pos)) & 0x1f;
1550 for (i = 0; i < 3; i++) {
1551 if (chain_state == dcu_chain_state[i]) {
1552 *hang_state = chain_state;
1553 *hang_pos = dcs_pos;
1554 return true;
1555 }
1556 }
1557 }
1558 return false;
1559}
1560
1561#define DCU_COMPLETE_STATE 1
1562#define DCU_COMPLETE_STATE_MASK 0x3
1563#define NUM_STATUS_READS 50
1564static bool ath9k_hw_detect_mac_hang(struct ath_hw *ah)
1565{
1566 u32 chain_state, comp_state, dcs_reg = AR_DMADBG_4;
1567 u32 i, hang_pos, hang_state, num_state = 6;
1568
1569 comp_state = REG_READ(ah, AR_DMADBG_6);
1570
1571 if ((comp_state & DCU_COMPLETE_STATE_MASK) != DCU_COMPLETE_STATE) {
1572 ath_dbg(ath9k_hw_common(ah), RESET,
1573 "MAC Hang signature not found at DCU complete\n");
1574 return false;
1575 }
1576
1577 chain_state = REG_READ(ah, dcs_reg);
1578 if (ath9k_hw_check_dcs(chain_state, num_state, &hang_state, &hang_pos))
1579 goto hang_check_iter;
1580
1581 dcs_reg = AR_DMADBG_5;
1582 num_state = 4;
1583 chain_state = REG_READ(ah, dcs_reg);
1584 if (ath9k_hw_check_dcs(chain_state, num_state, &hang_state, &hang_pos))
1585 goto hang_check_iter;
1586
1587 ath_dbg(ath9k_hw_common(ah), RESET,
1588 "MAC Hang signature 1 not found\n");
1589 return false;
1590
1591hang_check_iter:
1592 ath_dbg(ath9k_hw_common(ah), RESET,
1593 "DCU registers: chain %08x complete %08x Hang: state %d pos %d\n",
1594 chain_state, comp_state, hang_state, hang_pos);
1595
1596 for (i = 0; i < NUM_STATUS_READS; i++) {
1597 chain_state = REG_READ(ah, dcs_reg);
1598 chain_state = (chain_state >> (5 * hang_pos)) & 0x1f;
1599 comp_state = REG_READ(ah, AR_DMADBG_6);
1600
1601 if (((comp_state & DCU_COMPLETE_STATE_MASK) !=
1602 DCU_COMPLETE_STATE) ||
1603 (chain_state != hang_state))
1604 return false;
1605 }
1606
1607 ath_dbg(ath9k_hw_common(ah), RESET, "MAC Hang signature 1 found\n");
1608
1609 return true;
1610}
1611
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001612bool ath9k_hw_check_alive(struct ath_hw *ah)
Johannes Berg3b319aa2009-06-13 14:50:26 +05301613{
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001614 int count = 50;
1615 u32 reg;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301616
Rajkumar Manoharan01e18912012-03-15 05:34:27 +05301617 if (AR_SREV_9300(ah))
1618 return !ath9k_hw_detect_mac_hang(ah);
1619
Felix Fietkaue17f83e2010-09-22 12:34:53 +02001620 if (AR_SREV_9285_12_OR_LATER(ah))
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001621 return true;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301622
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001623 do {
1624 reg = REG_READ(ah, AR_OBS_BUS_1);
1625
1626 if ((reg & 0x7E7FFFEF) == 0x00702400)
1627 continue;
1628
1629 switch (reg & 0x7E000B00) {
1630 case 0x1E000000:
1631 case 0x52000B00:
1632 case 0x18000B00:
1633 continue;
1634 default:
1635 return true;
1636 }
1637 } while (count-- > 0);
1638
1639 return false;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301640}
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001641EXPORT_SYMBOL(ath9k_hw_check_alive);
Johannes Berg3b319aa2009-06-13 14:50:26 +05301642
Sujith Manoharancaed6572012-03-14 14:40:46 +05301643/*
1644 * Fast channel change:
1645 * (Change synthesizer based on channel freq without resetting chip)
1646 *
1647 * Don't do FCC when
1648 * - Flag is not set
1649 * - Chip is just coming out of full sleep
1650 * - Channel to be set is same as current channel
1651 * - Channel flags are different, (eg.,moving from 2GHz to 5GHz channel)
1652 */
1653static int ath9k_hw_do_fastcc(struct ath_hw *ah, struct ath9k_channel *chan)
1654{
1655 struct ath_common *common = ath9k_hw_common(ah);
1656 int ret;
1657
1658 if (AR_SREV_9280(ah) && common->bus_ops->ath_bus_type == ATH_PCI)
1659 goto fail;
1660
1661 if (ah->chip_fullsleep)
1662 goto fail;
1663
1664 if (!ah->curchan)
1665 goto fail;
1666
1667 if (chan->channel == ah->curchan->channel)
1668 goto fail;
1669
1670 if ((chan->channelFlags & CHANNEL_ALL) !=
1671 (ah->curchan->channelFlags & CHANNEL_ALL))
1672 goto fail;
1673
1674 if (!ath9k_hw_check_alive(ah))
1675 goto fail;
1676
1677 /*
1678 * For AR9462, make sure that calibration data for
1679 * re-using are present.
1680 */
1681 if (AR_SREV_9462(ah) && (!ah->caldata ||
1682 !ah->caldata->done_txiqcal_once ||
1683 !ah->caldata->done_txclcal_once ||
1684 !ah->caldata->rtt_hist.num_readings))
1685 goto fail;
1686
1687 ath_dbg(common, RESET, "FastChannelChange for %d -> %d\n",
1688 ah->curchan->channel, chan->channel);
1689
1690 ret = ath9k_hw_channel_change(ah, chan);
1691 if (!ret)
1692 goto fail;
1693
1694 ath9k_hw_loadnf(ah, ah->curchan);
1695 ath9k_hw_start_nfcal(ah, true);
1696
1697 if ((ah->caps.hw_caps & ATH9K_HW_CAP_MCI) && ar9003_mci_is_ready(ah))
1698 ar9003_mci_2g5g_switch(ah, true);
1699
1700 if (AR_SREV_9271(ah))
1701 ar9002_hw_load_ani_reg(ah, chan);
1702
1703 return 0;
1704fail:
1705 return -EINVAL;
1706}
1707
Sujithcbe61d82009-02-09 13:27:12 +05301708int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
Sujith Manoharancaed6572012-03-14 14:40:46 +05301709 struct ath9k_hw_cal_data *caldata, bool fastcc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001710{
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001711 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001712 u32 saveLedState;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001713 u32 saveDefAntenna;
1714 u32 macStaId1;
Sujith46fe7822009-09-17 09:25:25 +05301715 u64 tsf = 0;
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001716 int i, r;
Sujith Manoharancaed6572012-03-14 14:40:46 +05301717 bool start_mci_reset = false;
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301718 bool mci = !!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI);
1719 bool save_fullsleep = ah->chip_fullsleep;
1720
1721 if (mci) {
Sujith Manoharan528e5d32012-02-22 12:41:12 +05301722 start_mci_reset = ar9003_mci_start_reset(ah, chan);
1723 if (start_mci_reset)
1724 return 0;
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301725 }
1726
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001727 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001728 return -EIO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001729
Sujith Manoharancaed6572012-03-14 14:40:46 +05301730 if (ah->curchan && !ah->chip_fullsleep)
1731 ath9k_hw_getnf(ah, ah->curchan);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001732
Felix Fietkau20bd2a02010-07-31 00:12:00 +02001733 ah->caldata = caldata;
1734 if (caldata &&
1735 (chan->channel != caldata->channel ||
1736 (chan->channelFlags & ~CHANNEL_CW_INT) !=
1737 (caldata->channelFlags & ~CHANNEL_CW_INT))) {
1738 /* Operating channel changed, reset channel calibration data */
1739 memset(caldata, 0, sizeof(*caldata));
1740 ath9k_init_nfcal_hist_buffer(ah, chan);
1741 }
Felix Fietkauf23fba492011-07-28 14:08:56 +02001742 ah->noise = ath9k_hw_getchan_noise(ah, chan);
Felix Fietkau20bd2a02010-07-31 00:12:00 +02001743
Sujith Manoharancaed6572012-03-14 14:40:46 +05301744 if (fastcc) {
1745 r = ath9k_hw_do_fastcc(ah, chan);
1746 if (!r)
1747 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001748 }
1749
Sujith Manoharan528e5d32012-02-22 12:41:12 +05301750 if (mci)
1751 ar9003_mci_stop_bt(ah, save_fullsleep);
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301752
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001753 saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
1754 if (saveDefAntenna == 0)
1755 saveDefAntenna = 1;
1756
1757 macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
1758
Sujith46fe7822009-09-17 09:25:25 +05301759 /* For chips on which RTC reset is done, save TSF before it gets cleared */
Felix Fietkauf860d522010-06-30 02:07:48 +02001760 if (AR_SREV_9100(ah) ||
1761 (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)))
Sujith46fe7822009-09-17 09:25:25 +05301762 tsf = ath9k_hw_gettsf64(ah);
1763
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001764 saveLedState = REG_READ(ah, AR_CFG_LED) &
1765 (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
1766 AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
1767
1768 ath9k_hw_mark_phy_inactive(ah);
1769
Vasanthakumar Thiagarajan45ef6a0b2010-12-15 07:30:53 -08001770 ah->paprd_table_write_done = false;
1771
Sujith05020d22010-03-17 14:25:23 +05301772 /* Only required on the first reset */
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001773 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1774 REG_WRITE(ah,
1775 AR9271_RESET_POWER_DOWN_CONTROL,
1776 AR9271_RADIO_RF_RST);
1777 udelay(50);
1778 }
1779
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001780 if (!ath9k_hw_chip_reset(ah, chan)) {
Joe Perches38002762010-12-02 19:12:36 -08001781 ath_err(common, "Chip reset failed\n");
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001782 return -EINVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001783 }
1784
Sujith05020d22010-03-17 14:25:23 +05301785 /* Only required on the first reset */
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001786 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1787 ah->htc_reset_init = false;
1788 REG_WRITE(ah,
1789 AR9271_RESET_POWER_DOWN_CONTROL,
1790 AR9271_GATE_MAC_CTL);
1791 udelay(50);
1792 }
1793
Sujith46fe7822009-09-17 09:25:25 +05301794 /* Restore TSF */
Felix Fietkauf860d522010-06-30 02:07:48 +02001795 if (tsf)
Sujith46fe7822009-09-17 09:25:25 +05301796 ath9k_hw_settsf64(ah, tsf);
1797
Felix Fietkau7a370812010-09-22 12:34:52 +02001798 if (AR_SREV_9280_20_OR_LATER(ah))
Vasanthakumar Thiagarajan369391d2009-01-21 19:24:13 +05301799 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001800
Sujithe9141f72010-06-01 15:14:10 +05301801 if (!AR_SREV_9300_20_OR_LATER(ah))
1802 ar9002_hw_enable_async_fifo(ah);
1803
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07001804 r = ath9k_hw_process_ini(ah, chan);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001805 if (r)
1806 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001807
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301808 if (mci)
1809 ar9003_mci_reset(ah, false, IS_CHAN_2GHZ(chan), save_fullsleep);
1810
Felix Fietkauf860d522010-06-30 02:07:48 +02001811 /*
1812 * Some AR91xx SoC devices frequently fail to accept TSF writes
1813 * right after the chip reset. When that happens, write a new
1814 * value after the initvals have been applied, with an offset
1815 * based on measured time difference
1816 */
1817 if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
1818 tsf += 1500;
1819 ath9k_hw_settsf64(ah, tsf);
1820 }
1821
Jouni Malinen0ced0e12009-01-08 13:32:13 +02001822 /* Setup MFP options for CCMP */
1823 if (AR_SREV_9280_20_OR_LATER(ah)) {
1824 /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
1825 * frames when constructing CCMP AAD. */
1826 REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
1827 0xc7ff);
1828 ah->sw_mgmt_crypto = false;
1829 } else if (AR_SREV_9160_10_OR_LATER(ah)) {
1830 /* Disable hardware crypto for management frames */
1831 REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
1832 AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
1833 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1834 AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
1835 ah->sw_mgmt_crypto = true;
1836 } else
1837 ah->sw_mgmt_crypto = true;
1838
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001839 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1840 ath9k_hw_set_delta_slope(ah, chan);
1841
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001842 ath9k_hw_spur_mitigate_freq(ah, chan);
Sujithd6509152009-03-13 08:56:05 +05301843 ah->eep_ops->set_board_values(ah, chan);
Luis R. Rodrigueza7765822009-10-19 02:33:45 -04001844
Sujith7d0d0df2010-04-16 11:53:57 +05301845 ENABLE_REGWRITE_BUFFER(ah);
1846
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001847 REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
1848 REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001849 | macStaId1
1850 | AR_STA_ID1_RTS_USE_DEF
Sujith2660b812009-02-09 13:27:26 +05301851 | (ah->config.
Sujith60b67f52008-08-07 10:52:38 +05301852 ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
Sujith2660b812009-02-09 13:27:26 +05301853 | ah->sta_id1_defaults);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -07001854 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001855 REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
Luis R. Rodriguez3453ad82009-09-10 08:57:00 -07001856 ath9k_hw_write_associd(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001857 REG_WRITE(ah, AR_ISR, ~0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001858 REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
1859
Sujith7d0d0df2010-04-16 11:53:57 +05301860 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301861
Sujith Manoharan00e00032011-01-26 21:59:05 +05301862 ath9k_hw_set_operating_mode(ah, ah->opmode);
1863
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001864 r = ath9k_hw_rf_set_freq(ah, chan);
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001865 if (r)
1866 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001867
Felix Fietkaudfdac8a2010-10-08 22:13:51 +02001868 ath9k_hw_set_clockrate(ah);
1869
Sujith7d0d0df2010-04-16 11:53:57 +05301870 ENABLE_REGWRITE_BUFFER(ah);
1871
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001872 for (i = 0; i < AR_NUM_DCU; i++)
1873 REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
1874
Sujith7d0d0df2010-04-16 11:53:57 +05301875 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301876
Sujith2660b812009-02-09 13:27:26 +05301877 ah->intr_txqs = 0;
Felix Fietkauf4c607d2011-03-23 20:57:28 +01001878 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001879 ath9k_hw_resettxqueue(ah, i);
1880
Sujith2660b812009-02-09 13:27:26 +05301881 ath9k_hw_init_interrupt_masks(ah, ah->opmode);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001882 ath9k_hw_ani_cache_ini_regs(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001883 ath9k_hw_init_qos(ah);
1884
Sujith2660b812009-02-09 13:27:26 +05301885 if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
Felix Fietkau55821322010-12-17 00:57:01 +01001886 ath9k_hw_cfg_gpio_input(ah, ah->rfkill_gpio);
Johannes Berg3b319aa2009-06-13 14:50:26 +05301887
Felix Fietkau0005baf2010-01-15 02:33:40 +01001888 ath9k_hw_init_global_settings(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001889
Felix Fietkaufe2b6af2011-07-09 11:12:51 +07001890 if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
1891 REG_SET_BIT(ah, AR_MAC_PCU_LOGIC_ANALYZER,
1892 AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
1893 REG_RMW_FIELD(ah, AR_AHB_MODE, AR_AHB_CUSTOM_BURST_EN,
1894 AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
1895 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1896 AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05301897 }
1898
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001899 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001900
1901 ath9k_hw_set_dma(ah);
1902
1903 REG_WRITE(ah, AR_OBS, 8);
1904
Sujith0ce024c2009-12-14 14:57:00 +05301905 if (ah->config.rx_intr_mitigation) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001906 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
1907 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
1908 }
1909
Vasanthakumar Thiagarajan7f62a132010-04-15 17:39:19 -04001910 if (ah->config.tx_intr_mitigation) {
1911 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
1912 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
1913 }
1914
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001915 ath9k_hw_init_bb(ah, chan);
1916
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301917 if (caldata) {
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301918 caldata->done_txiqcal_once = false;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301919 caldata->done_txclcal_once = false;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301920 caldata->rtt_hist.num_readings = 0;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301921 }
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001922 if (!ath9k_hw_init_cal(ah, chan))
Joe Perches6badaaf2009-06-28 09:26:32 -07001923 return -EIO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001924
Rajkumar Manoharan93348922011-10-25 16:47:36 +05301925 ath9k_hw_loadnf(ah, chan);
1926 ath9k_hw_start_nfcal(ah, true);
1927
Sujith Manoharan528e5d32012-02-22 12:41:12 +05301928 if (mci && ar9003_mci_end_reset(ah, chan, caldata))
1929 return -EIO;
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301930
Sujith7d0d0df2010-04-16 11:53:57 +05301931 ENABLE_REGWRITE_BUFFER(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001932
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001933 ath9k_hw_restore_chainmask(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001934 REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
1935
Sujith7d0d0df2010-04-16 11:53:57 +05301936 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301937
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001938 /*
1939 * For big endian systems turn on swapping for descriptors
1940 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001941 if (AR_SREV_9100(ah)) {
1942 u32 mask;
1943 mask = REG_READ(ah, AR_CFG);
1944 if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001945 ath_dbg(common, RESET, "CFG Byte Swap Set 0x%x\n",
1946 mask);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001947 } else {
1948 mask =
1949 INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
1950 REG_WRITE(ah, AR_CFG, mask);
Joe Perchesd2182b62011-12-15 14:55:53 -08001951 ath_dbg(common, RESET, "Setting CFG 0x%x\n",
1952 REG_READ(ah, AR_CFG));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001953 }
1954 } else {
Sujithcbba8cd2010-06-02 15:53:31 +05301955 if (common->bus_ops->ath_bus_type == ATH_USB) {
1956 /* Configure AR9271 target WLAN */
1957 if (AR_SREV_9271(ah))
1958 REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
1959 else
1960 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1961 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001962#ifdef __BIG_ENDIAN
Gabor Juhos4033bda2011-06-21 11:23:35 +02001963 else if (AR_SREV_9330(ah) || AR_SREV_9340(ah))
Vasanthakumar Thiagarajan2be7bfe2011-04-19 19:29:14 +05301964 REG_RMW(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB, 0);
1965 else
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001966 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001967#endif
1968 }
1969
Sujith Manoharandbccdd12012-02-22 17:55:47 +05301970 if (ath9k_hw_btcoex_is_enabled(ah))
Vasanthakumar Thiagarajan42cc41e2009-08-26 21:08:45 +05301971 ath9k_hw_btcoex_enable(ah);
1972
Sujith Manoharan528e5d32012-02-22 12:41:12 +05301973 if (mci)
1974 ar9003_mci_check_bt(ah);
Mohammed Shafi Shajakhan63d32962011-11-30 10:41:27 +05301975
Rajkumar Manoharan51ac8cb2011-05-20 17:52:13 +05301976 if (AR_SREV_9300_20_OR_LATER(ah)) {
Luis R. Rodriguezaea702b2010-05-13 13:33:43 -04001977 ar9003_hw_bb_watchdog_config(ah);
Vasanthakumar Thiagarajand8903a52010-04-15 17:39:25 -04001978
Rajkumar Manoharan51ac8cb2011-05-20 17:52:13 +05301979 ar9003_hw_disable_phy_restart(ah);
1980 }
1981
Felix Fietkau691680b2011-03-19 13:55:38 +01001982 ath9k_hw_apply_gpio_override(ah);
1983
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001984 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001985}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04001986EXPORT_SYMBOL(ath9k_hw_reset);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001987
Sujithf1dc5602008-10-29 10:16:30 +05301988/******************************/
1989/* Power Management (Chipset) */
1990/******************************/
1991
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001992/*
1993 * Notify Power Mgt is disabled in self-generated frames.
1994 * If requested, force chip to sleep.
1995 */
Sujithcbe61d82009-02-09 13:27:12 +05301996static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
Sujithf1dc5602008-10-29 10:16:30 +05301997{
1998 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1999 if (setChip) {
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05302000 if (AR_SREV_9462(ah)) {
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302001 REG_WRITE(ah, AR_TIMER_MODE,
2002 REG_READ(ah, AR_TIMER_MODE) & 0xFFFFFF00);
2003 REG_WRITE(ah, AR_NDP2_TIMER_MODE, REG_READ(ah,
2004 AR_NDP2_TIMER_MODE) & 0xFFFFFF00);
2005 REG_WRITE(ah, AR_SLP32_INC,
2006 REG_READ(ah, AR_SLP32_INC) & 0xFFF00000);
2007 /* xxx Required for WLAN only case ? */
2008 REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
2009 udelay(100);
2010 }
2011
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04002012 /*
2013 * Clear the RTC force wake bit to allow the
2014 * mac to go to sleep.
2015 */
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302016 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
2017
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05302018 if (AR_SREV_9462(ah))
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302019 udelay(100);
2020
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04002021 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05302022 REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
2023
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04002024 /* Shutdown chip. Active low */
Sujith Manoharanc91ec462012-02-22 12:40:03 +05302025 if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah)) {
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302026 REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
2027 udelay(2);
2028 }
Sujithf1dc5602008-10-29 10:16:30 +05302029 }
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04002030
2031 /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
Rafael J. Wysockia7322812011-11-26 23:37:43 +01002032 if (AR_SREV_9300_20_OR_LATER(ah))
2033 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002034}
2035
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04002036/*
2037 * Notify Power Management is enabled in self-generating
2038 * frames. If request, set power mode of chip to
2039 * auto/normal. Duration in units of 128us (1/8 TU).
2040 */
Sujithcbe61d82009-02-09 13:27:12 +05302041static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002042{
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302043 u32 val;
2044
Sujithf1dc5602008-10-29 10:16:30 +05302045 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
2046 if (setChip) {
Sujith2660b812009-02-09 13:27:26 +05302047 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002048
Sujithf1dc5602008-10-29 10:16:30 +05302049 if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04002050 /* Set WakeOnInterrupt bit; clear ForceWake bit */
Sujithf1dc5602008-10-29 10:16:30 +05302051 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
2052 AR_RTC_FORCE_WAKE_ON_INT);
2053 } else {
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302054
2055 /* When chip goes into network sleep, it could be waken
2056 * up by MCI_INT interrupt caused by BT's HW messages
2057 * (LNA_xxx, CONT_xxx) which chould be in a very fast
2058 * rate (~100us). This will cause chip to leave and
2059 * re-enter network sleep mode frequently, which in
2060 * consequence will have WLAN MCI HW to generate lots of
2061 * SYS_WAKING and SYS_SLEEPING messages which will make
2062 * BT CPU to busy to process.
2063 */
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05302064 if (AR_SREV_9462(ah)) {
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302065 val = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_EN) &
2066 ~AR_MCI_INTERRUPT_RX_HW_MSG_MASK;
2067 REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, val);
2068 }
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04002069 /*
2070 * Clear the RTC force wake bit to allow the
2071 * mac to go to sleep.
2072 */
Sujithf1dc5602008-10-29 10:16:30 +05302073 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
2074 AR_RTC_FORCE_WAKE_EN);
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302075
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05302076 if (AR_SREV_9462(ah))
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302077 udelay(30);
Sujithf1dc5602008-10-29 10:16:30 +05302078 }
2079 }
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04002080
2081 /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
2082 if (AR_SREV_9300_20_OR_LATER(ah))
2083 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
Sujithf1dc5602008-10-29 10:16:30 +05302084}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002085
Sujithcbe61d82009-02-09 13:27:12 +05302086static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
Sujithf1dc5602008-10-29 10:16:30 +05302087{
2088 u32 val;
2089 int i;
2090
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04002091 /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
2092 if (AR_SREV_9300_20_OR_LATER(ah)) {
2093 REG_WRITE(ah, AR_WA, ah->WARegVal);
2094 udelay(10);
2095 }
2096
Sujithf1dc5602008-10-29 10:16:30 +05302097 if (setChip) {
2098 if ((REG_READ(ah, AR_RTC_STATUS) &
2099 AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
Joe Perches23677ce2012-02-09 11:17:23 +00002100 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
Sujithf1dc5602008-10-29 10:16:30 +05302101 return false;
2102 }
Luis R. Rodrigueze0412282010-04-15 17:38:15 -04002103 if (!AR_SREV_9300_20_OR_LATER(ah))
2104 ath9k_hw_init_pll(ah, NULL);
Sujithf1dc5602008-10-29 10:16:30 +05302105 }
2106 if (AR_SREV_9100(ah))
2107 REG_SET_BIT(ah, AR_RTC_RESET,
2108 AR_RTC_RESET_EN);
2109
2110 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
2111 AR_RTC_FORCE_WAKE_EN);
2112 udelay(50);
2113
2114 for (i = POWER_UP_TIME / 50; i > 0; i--) {
2115 val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
2116 if (val == AR_RTC_STATUS_ON)
2117 break;
2118 udelay(50);
2119 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
2120 AR_RTC_FORCE_WAKE_EN);
2121 }
2122 if (i == 0) {
Joe Perches38002762010-12-02 19:12:36 -08002123 ath_err(ath9k_hw_common(ah),
2124 "Failed to wakeup in %uus\n",
2125 POWER_UP_TIME / 20);
Sujithf1dc5602008-10-29 10:16:30 +05302126 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002127 }
2128 }
2129
Sujithf1dc5602008-10-29 10:16:30 +05302130 REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
2131
2132 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002133}
2134
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07002135bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
Sujithf1dc5602008-10-29 10:16:30 +05302136{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002137 struct ath_common *common = ath9k_hw_common(ah);
Sujithcbe61d82009-02-09 13:27:12 +05302138 int status = true, setChip = true;
Sujithf1dc5602008-10-29 10:16:30 +05302139 static const char *modes[] = {
2140 "AWAKE",
2141 "FULL-SLEEP",
2142 "NETWORK SLEEP",
2143 "UNDEFINED"
2144 };
Sujithf1dc5602008-10-29 10:16:30 +05302145
Gabor Juhoscbdec972009-07-24 17:27:22 +02002146 if (ah->power_mode == mode)
2147 return status;
2148
Joe Perchesd2182b62011-12-15 14:55:53 -08002149 ath_dbg(common, RESET, "%s -> %s\n",
Joe Perches226afe62010-12-02 19:12:37 -08002150 modes[ah->power_mode], modes[mode]);
Sujithf1dc5602008-10-29 10:16:30 +05302151
2152 switch (mode) {
2153 case ATH9K_PM_AWAKE:
2154 status = ath9k_hw_set_power_awake(ah, setChip);
Mohammed Shafi Shajakhan10109112011-11-30 10:41:24 +05302155
2156 if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
2157 REG_WRITE(ah, AR_RTC_KEEP_AWAKE, 0x2);
2158
Sujithf1dc5602008-10-29 10:16:30 +05302159 break;
2160 case ATH9K_PM_FULL_SLEEP:
Sujith Manoharand1ca8b82012-02-22 12:41:01 +05302161 if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
2162 ar9003_mci_set_full_sleep(ah);
Mohammed Shafi Shajakhan10109112011-11-30 10:41:24 +05302163
Sujithf1dc5602008-10-29 10:16:30 +05302164 ath9k_set_power_sleep(ah, setChip);
Sujith2660b812009-02-09 13:27:26 +05302165 ah->chip_fullsleep = true;
Sujithf1dc5602008-10-29 10:16:30 +05302166 break;
2167 case ATH9K_PM_NETWORK_SLEEP:
Mohammed Shafi Shajakhan10109112011-11-30 10:41:24 +05302168
2169 if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
2170 REG_WRITE(ah, AR_RTC_KEEP_AWAKE, 0x2);
2171
Sujithf1dc5602008-10-29 10:16:30 +05302172 ath9k_set_power_network_sleep(ah, setChip);
2173 break;
2174 default:
Joe Perches38002762010-12-02 19:12:36 -08002175 ath_err(common, "Unknown power mode %u\n", mode);
Sujithf1dc5602008-10-29 10:16:30 +05302176 return false;
2177 }
Sujith2660b812009-02-09 13:27:26 +05302178 ah->power_mode = mode;
Sujithf1dc5602008-10-29 10:16:30 +05302179
Luis R. Rodriguez69f4aab2010-12-07 15:13:23 -08002180 /*
2181 * XXX: If this warning never comes up after a while then
2182 * simply keep the ATH_DBG_WARN_ON_ONCE() but make
2183 * ath9k_hw_setpower() return type void.
2184 */
Sujith Manoharan97dcec52010-12-20 08:02:42 +05302185
2186 if (!(ah->ah_flags & AH_UNPLUGGED))
2187 ATH_DBG_WARN_ON_ONCE(!status);
Luis R. Rodriguez69f4aab2010-12-07 15:13:23 -08002188
Sujithf1dc5602008-10-29 10:16:30 +05302189 return status;
2190}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002191EXPORT_SYMBOL(ath9k_hw_setpower);
Sujithf1dc5602008-10-29 10:16:30 +05302192
Sujithf1dc5602008-10-29 10:16:30 +05302193/*******************/
2194/* Beacon Handling */
2195/*******************/
2196
Sujithcbe61d82009-02-09 13:27:12 +05302197void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002198{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002199 int flags = 0;
2200
Sujith7d0d0df2010-04-16 11:53:57 +05302201 ENABLE_REGWRITE_BUFFER(ah);
2202
Sujith2660b812009-02-09 13:27:26 +05302203 switch (ah->opmode) {
Colin McCabed97809d2008-12-01 13:38:55 -08002204 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -04002205 case NL80211_IFTYPE_MESH_POINT:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002206 REG_SET_BIT(ah, AR_TXCFG,
2207 AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
Felix Fietkaudd347f22011-03-22 21:54:17 +01002208 REG_WRITE(ah, AR_NEXT_NDP_TIMER, next_beacon +
2209 TU_TO_USEC(ah->atim_window ? ah->atim_window : 1));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002210 flags |= AR_NDP_TIMER_EN;
Colin McCabed97809d2008-12-01 13:38:55 -08002211 case NL80211_IFTYPE_AP:
Felix Fietkaudd347f22011-03-22 21:54:17 +01002212 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, next_beacon);
2213 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, next_beacon -
2214 TU_TO_USEC(ah->config.dma_beacon_response_time));
2215 REG_WRITE(ah, AR_NEXT_SWBA, next_beacon -
2216 TU_TO_USEC(ah->config.sw_beacon_response_time));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002217 flags |=
2218 AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
2219 break;
Colin McCabed97809d2008-12-01 13:38:55 -08002220 default:
Joe Perchesd2182b62011-12-15 14:55:53 -08002221 ath_dbg(ath9k_hw_common(ah), BEACON,
2222 "%s: unsupported opmode: %d\n", __func__, ah->opmode);
Colin McCabed97809d2008-12-01 13:38:55 -08002223 return;
2224 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002225 }
2226
Felix Fietkaudd347f22011-03-22 21:54:17 +01002227 REG_WRITE(ah, AR_BEACON_PERIOD, beacon_period);
2228 REG_WRITE(ah, AR_DMA_BEACON_PERIOD, beacon_period);
2229 REG_WRITE(ah, AR_SWBA_PERIOD, beacon_period);
2230 REG_WRITE(ah, AR_NDP_PERIOD, beacon_period);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002231
Sujith7d0d0df2010-04-16 11:53:57 +05302232 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05302233
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002234 REG_SET_BIT(ah, AR_TIMER_MODE, flags);
2235}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002236EXPORT_SYMBOL(ath9k_hw_beaconinit);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002237
Sujithcbe61d82009-02-09 13:27:12 +05302238void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05302239 const struct ath9k_beacon_state *bs)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002240{
2241 u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
Sujith2660b812009-02-09 13:27:26 +05302242 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002243 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002244
Sujith7d0d0df2010-04-16 11:53:57 +05302245 ENABLE_REGWRITE_BUFFER(ah);
2246
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002247 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
2248
2249 REG_WRITE(ah, AR_BEACON_PERIOD,
Rajkumar Manoharanf29f5c02011-05-20 17:52:11 +05302250 TU_TO_USEC(bs->bs_intval));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002251 REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
Rajkumar Manoharanf29f5c02011-05-20 17:52:11 +05302252 TU_TO_USEC(bs->bs_intval));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002253
Sujith7d0d0df2010-04-16 11:53:57 +05302254 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05302255
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002256 REG_RMW_FIELD(ah, AR_RSSI_THR,
2257 AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
2258
Rajkumar Manoharanf29f5c02011-05-20 17:52:11 +05302259 beaconintval = bs->bs_intval;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002260
2261 if (bs->bs_sleepduration > beaconintval)
2262 beaconintval = bs->bs_sleepduration;
2263
2264 dtimperiod = bs->bs_dtimperiod;
2265 if (bs->bs_sleepduration > dtimperiod)
2266 dtimperiod = bs->bs_sleepduration;
2267
2268 if (beaconintval == dtimperiod)
2269 nextTbtt = bs->bs_nextdtim;
2270 else
2271 nextTbtt = bs->bs_nexttbtt;
2272
Joe Perchesd2182b62011-12-15 14:55:53 -08002273 ath_dbg(common, BEACON, "next DTIM %d\n", bs->bs_nextdtim);
2274 ath_dbg(common, BEACON, "next beacon %d\n", nextTbtt);
2275 ath_dbg(common, BEACON, "beacon period %d\n", beaconintval);
2276 ath_dbg(common, BEACON, "DTIM period %d\n", dtimperiod);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002277
Sujith7d0d0df2010-04-16 11:53:57 +05302278 ENABLE_REGWRITE_BUFFER(ah);
2279
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002280 REG_WRITE(ah, AR_NEXT_DTIM,
2281 TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
2282 REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
2283
2284 REG_WRITE(ah, AR_SLEEP1,
2285 SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
2286 | AR_SLEEP1_ASSUME_DTIM);
2287
Sujith60b67f52008-08-07 10:52:38 +05302288 if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002289 beacontimeout = (BEACON_TIMEOUT_VAL << 3);
2290 else
2291 beacontimeout = MIN_BEACON_TIMEOUT_VAL;
2292
2293 REG_WRITE(ah, AR_SLEEP2,
2294 SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
2295
2296 REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
2297 REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
2298
Sujith7d0d0df2010-04-16 11:53:57 +05302299 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05302300
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002301 REG_SET_BIT(ah, AR_TIMER_MODE,
2302 AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
2303 AR_DTIM_TIMER_EN);
2304
Sujith4af9cf42009-02-12 10:06:47 +05302305 /* TSF Out of Range Threshold */
2306 REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002307}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002308EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002309
Sujithf1dc5602008-10-29 10:16:30 +05302310/*******************/
2311/* HW Capabilities */
2312/*******************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002313
Felix Fietkau60540692011-07-19 08:46:44 +02002314static u8 fixup_chainmask(u8 chip_chainmask, u8 eeprom_chainmask)
2315{
2316 eeprom_chainmask &= chip_chainmask;
2317 if (eeprom_chainmask)
2318 return eeprom_chainmask;
2319 else
2320 return chip_chainmask;
2321}
2322
Zefir Kurtisi9a66af32011-12-14 20:16:33 -08002323/**
2324 * ath9k_hw_dfs_tested - checks if DFS has been tested with used chipset
2325 * @ah: the atheros hardware data structure
2326 *
2327 * We enable DFS support upstream on chipsets which have passed a series
2328 * of tests. The testing requirements are going to be documented. Desired
2329 * test requirements are documented at:
2330 *
2331 * http://wireless.kernel.org/en/users/Drivers/ath9k/dfs
2332 *
2333 * Once a new chipset gets properly tested an individual commit can be used
2334 * to document the testing for DFS for that chipset.
2335 */
2336static bool ath9k_hw_dfs_tested(struct ath_hw *ah)
2337{
2338
2339 switch (ah->hw_version.macVersion) {
2340 /* AR9580 will likely be our first target to get testing on */
2341 case AR_SREV_VERSION_9580:
2342 default:
2343 return false;
2344 }
2345}
2346
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01002347int ath9k_hw_fill_cap_info(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002348{
Sujith2660b812009-02-09 13:27:26 +05302349 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002350 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002351 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau60540692011-07-19 08:46:44 +02002352 unsigned int chip_chainmask;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002353
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05302354 u16 eeval;
Vasanthakumar Thiagarajan47c80de2010-12-06 04:27:43 -08002355 u8 ant_div_ctl1, tx_chainmask, rx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002356
Sujithf74df6f2009-02-09 13:27:24 +05302357 eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002358 regulatory->current_rd = eeval;
Sujithf1dc5602008-10-29 10:16:30 +05302359
Sujith2660b812009-02-09 13:27:26 +05302360 if (ah->opmode != NL80211_IFTYPE_AP &&
Sujithd535a422009-02-09 13:27:06 +05302361 ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002362 if (regulatory->current_rd == 0x64 ||
2363 regulatory->current_rd == 0x65)
2364 regulatory->current_rd += 5;
2365 else if (regulatory->current_rd == 0x41)
2366 regulatory->current_rd = 0x43;
Joe Perchesd2182b62011-12-15 14:55:53 -08002367 ath_dbg(common, REGULATORY, "regdomain mapped to 0x%x\n",
2368 regulatory->current_rd);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002369 }
Sujithdc2222a2008-08-14 13:26:55 +05302370
Sujithf74df6f2009-02-09 13:27:24 +05302371 eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01002372 if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
Joe Perches38002762010-12-02 19:12:36 -08002373 ath_err(common,
2374 "no band has been marked as supported in EEPROM\n");
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01002375 return -EINVAL;
2376 }
2377
Felix Fietkaud4659912010-10-14 16:02:39 +02002378 if (eeval & AR5416_OPFLAGS_11A)
2379 pCap->hw_caps |= ATH9K_HW_CAP_5GHZ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002380
Felix Fietkaud4659912010-10-14 16:02:39 +02002381 if (eeval & AR5416_OPFLAGS_11G)
2382 pCap->hw_caps |= ATH9K_HW_CAP_2GHZ;
Sujithf1dc5602008-10-29 10:16:30 +05302383
Felix Fietkau60540692011-07-19 08:46:44 +02002384 if (AR_SREV_9485(ah) || AR_SREV_9285(ah) || AR_SREV_9330(ah))
2385 chip_chainmask = 1;
Mohammed Shafi Shajakhanba5736a2011-11-30 21:10:52 +05302386 else if (AR_SREV_9462(ah))
2387 chip_chainmask = 3;
Felix Fietkau60540692011-07-19 08:46:44 +02002388 else if (!AR_SREV_9280_20_OR_LATER(ah))
2389 chip_chainmask = 7;
2390 else if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9340(ah))
2391 chip_chainmask = 3;
2392 else
2393 chip_chainmask = 7;
2394
Sujithf74df6f2009-02-09 13:27:24 +05302395 pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04002396 /*
2397 * For AR9271 we will temporarilly uses the rx chainmax as read from
2398 * the EEPROM.
2399 */
Sujith8147f5d2009-02-20 15:13:23 +05302400 if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04002401 !(eeval & AR5416_OPFLAGS_11A) &&
2402 !(AR_SREV_9271(ah)))
2403 /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
Sujith8147f5d2009-02-20 15:13:23 +05302404 pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
Felix Fietkau598cdd52011-03-19 13:55:42 +01002405 else if (AR_SREV_9100(ah))
2406 pCap->rx_chainmask = 0x7;
Sujith8147f5d2009-02-20 15:13:23 +05302407 else
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04002408 /* Use rx_chainmask from EEPROM. */
Sujith8147f5d2009-02-20 15:13:23 +05302409 pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
Sujithf1dc5602008-10-29 10:16:30 +05302410
Felix Fietkau60540692011-07-19 08:46:44 +02002411 pCap->tx_chainmask = fixup_chainmask(chip_chainmask, pCap->tx_chainmask);
2412 pCap->rx_chainmask = fixup_chainmask(chip_chainmask, pCap->rx_chainmask);
Felix Fietkau82b2d332011-09-03 01:40:23 +02002413 ah->txchainmask = pCap->tx_chainmask;
2414 ah->rxchainmask = pCap->rx_chainmask;
Felix Fietkau60540692011-07-19 08:46:44 +02002415
Felix Fietkau7a370812010-09-22 12:34:52 +02002416 ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
Sujithf1dc5602008-10-29 10:16:30 +05302417
Felix Fietkau02d2ebb2010-11-22 15:39:39 +01002418 /* enable key search for every frame in an aggregate */
2419 if (AR_SREV_9300_20_OR_LATER(ah))
2420 ah->misc_mode |= AR_PCU_ALWAYS_PERFORM_KEYSEARCH;
2421
Bruno Randolfce2220d2010-09-17 11:36:25 +09002422 common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
2423
Felix Fietkau0db156e2011-03-23 20:57:29 +01002424 if (ah->hw_version.devid != AR2427_DEVID_PCIE)
Sujithf1dc5602008-10-29 10:16:30 +05302425 pCap->hw_caps |= ATH9K_HW_CAP_HT;
2426 else
2427 pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
2428
Sujith5b5fa352010-03-17 14:25:15 +05302429 if (AR_SREV_9271(ah))
2430 pCap->num_gpio_pins = AR9271_NUM_GPIO;
Sujith88c1f4f2010-06-30 14:46:31 +05302431 else if (AR_DEVID_7010(ah))
2432 pCap->num_gpio_pins = AR7010_NUM_GPIO;
Mohammed Shafi Shajakhan6321eb02011-09-30 11:31:27 +05302433 else if (AR_SREV_9300_20_OR_LATER(ah))
2434 pCap->num_gpio_pins = AR9300_NUM_GPIO;
2435 else if (AR_SREV_9287_11_OR_LATER(ah))
2436 pCap->num_gpio_pins = AR9287_NUM_GPIO;
Felix Fietkaue17f83e2010-09-22 12:34:53 +02002437 else if (AR_SREV_9285_12_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302438 pCap->num_gpio_pins = AR9285_NUM_GPIO;
Felix Fietkau7a370812010-09-22 12:34:52 +02002439 else if (AR_SREV_9280_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05302440 pCap->num_gpio_pins = AR928X_NUM_GPIO;
2441 else
2442 pCap->num_gpio_pins = AR_NUM_GPIO;
2443
Mohammed Shafi Shajakhan1b2538b2011-12-07 16:51:39 +05302444 if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah))
Sujithf1dc5602008-10-29 10:16:30 +05302445 pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
Mohammed Shafi Shajakhan1b2538b2011-12-07 16:51:39 +05302446 else
Sujithf1dc5602008-10-29 10:16:30 +05302447 pCap->rts_aggr_limit = (8 * 1024);
Sujithf1dc5602008-10-29 10:16:30 +05302448
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05302449#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith2660b812009-02-09 13:27:26 +05302450 ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
2451 if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
2452 ah->rfkill_gpio =
2453 MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
2454 ah->rfkill_polarity =
2455 MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
Sujithf1dc5602008-10-29 10:16:30 +05302456
2457 pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
2458 }
2459#endif
Vasanthakumar Thiagarajand5d11542010-05-17 18:57:56 -07002460 if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
Vivek Natarajanbde748a2010-04-05 14:48:05 +05302461 pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
2462 else
2463 pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
Sujithf1dc5602008-10-29 10:16:30 +05302464
Senthil Balasubramaniane7594072008-12-08 19:43:48 +05302465 if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
Sujithf1dc5602008-10-29 10:16:30 +05302466 pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
2467 else
2468 pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
2469
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04002470 if (AR_SREV_9300_20_OR_LATER(ah)) {
Vasanthakumar Thiagarajan784ad502010-12-06 04:27:40 -08002471 pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_FASTCLOCK;
Gabor Juhos0e707a92011-06-21 11:23:31 +02002472 if (!AR_SREV_9330(ah) && !AR_SREV_9485(ah))
Vasanthakumar Thiagarajan784ad502010-12-06 04:27:40 -08002473 pCap->hw_caps |= ATH9K_HW_CAP_LDPC;
2474
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04002475 pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
2476 pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
2477 pCap->rx_status_len = sizeof(struct ar9003_rxs);
Vasanthakumar Thiagarajan162c3be2010-04-15 17:38:41 -04002478 pCap->tx_desc_len = sizeof(struct ar9003_txc);
Vasanthakumar Thiagarajan5088c2f2010-04-15 17:39:34 -04002479 pCap->txs_len = sizeof(struct ar9003_txs);
Luis R. Rodriguez6f481012011-01-20 17:47:39 -08002480 if (!ah->config.paprd_disable &&
2481 ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
Felix Fietkau49352502010-06-12 00:33:59 -04002482 pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
Vasanthakumar Thiagarajan162c3be2010-04-15 17:38:41 -04002483 } else {
2484 pCap->tx_desc_len = sizeof(struct ath_desc);
Felix Fietkaua949b172011-07-09 11:12:47 +07002485 if (AR_SREV_9280_20(ah))
Felix Fietkau6b42e8d2010-04-26 15:04:35 -04002486 pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04002487 }
Vasanthakumar Thiagarajan1adf02f2010-04-15 17:38:24 -04002488
Vasanthakumar Thiagarajan6c84ce02010-04-15 17:39:16 -04002489 if (AR_SREV_9300_20_OR_LATER(ah))
2490 pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
2491
Senthil Balasubramanian6ee63f52010-11-10 05:03:16 -08002492 if (AR_SREV_9300_20_OR_LATER(ah))
2493 ah->ent_mode = REG_READ(ah, AR_ENT_OTP);
2494
Felix Fietkaua42acef2010-09-22 12:34:54 +02002495 if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
Vasanthakumar Thiagarajan6473d242010-05-13 18:42:38 -07002496 pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
2497
Vasanthakumar Thiagarajan754dc532010-09-02 01:34:41 -07002498 if (AR_SREV_9285(ah))
2499 if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
2500 ant_div_ctl1 =
2501 ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
2502 if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1))
2503 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
2504 }
Mohammed Shafi Shajakhanea066d52010-11-23 20:42:27 +05302505 if (AR_SREV_9300_20_OR_LATER(ah)) {
2506 if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
2507 pCap->hw_caps |= ATH9K_HW_CAP_APM;
2508 }
2509
2510
Gabor Juhos431da562011-06-21 11:23:41 +02002511 if (AR_SREV_9330(ah) || AR_SREV_9485(ah)) {
Mohammed Shafi Shajakhan21d2c632011-05-13 20:29:31 +05302512 ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
2513 /*
2514 * enable the diversity-combining algorithm only when
2515 * both enable_lna_div and enable_fast_div are set
2516 * Table for Diversity
2517 * ant_div_alt_lnaconf bit 0-1
2518 * ant_div_main_lnaconf bit 2-3
2519 * ant_div_alt_gaintb bit 4
2520 * ant_div_main_gaintb bit 5
2521 * enable_ant_div_lnadiv bit 6
2522 * enable_ant_fast_div bit 7
2523 */
2524 if ((ant_div_ctl1 >> 0x6) == 0x3)
2525 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
2526 }
Vasanthakumar Thiagarajan754dc532010-09-02 01:34:41 -07002527
Vasanthakumar Thiagarajan8060e162010-12-06 04:27:42 -08002528 if (AR_SREV_9485_10(ah)) {
2529 pCap->pcie_lcr_extsync_en = true;
2530 pCap->pcie_lcr_offset = 0x80;
2531 }
2532
Zefir Kurtisi9a66af32011-12-14 20:16:33 -08002533 if (ath9k_hw_dfs_tested(ah))
2534 pCap->hw_caps |= ATH9K_HW_CAP_DFS;
2535
Vasanthakumar Thiagarajan47c80de2010-12-06 04:27:43 -08002536 tx_chainmask = pCap->tx_chainmask;
2537 rx_chainmask = pCap->rx_chainmask;
2538 while (tx_chainmask || rx_chainmask) {
2539 if (tx_chainmask & BIT(0))
2540 pCap->max_txchains++;
2541 if (rx_chainmask & BIT(0))
2542 pCap->max_rxchains++;
2543
2544 tx_chainmask >>= 1;
2545 rx_chainmask >>= 1;
2546 }
2547
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05302548 if (AR_SREV_9300_20_OR_LATER(ah)) {
2549 ah->enabled_cals |= TX_IQ_CAL;
Mohammed Shafi Shajakhan6fea5932011-11-30 21:01:31 +05302550 if (AR_SREV_9485_OR_LATER(ah))
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05302551 ah->enabled_cals |= TX_IQ_ON_AGC_CAL;
2552 }
Mohammed Shafi Shajakhan3789d592012-03-09 12:01:55 +05302553
2554 if (AR_SREV_9462(ah)) {
2555
2556 if (!(ah->ent_mode & AR_ENT_OTP_49GHZ_DISABLE))
2557 pCap->hw_caps |= ATH9K_HW_CAP_MCI;
2558
2559 if (AR_SREV_9462_20(ah))
2560 pCap->hw_caps |= ATH9K_HW_CAP_RTT;
2561
2562 }
2563
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05302564
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01002565 return 0;
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002566}
2567
Sujithf1dc5602008-10-29 10:16:30 +05302568/****************************/
2569/* GPIO / RFKILL / Antennae */
2570/****************************/
2571
Sujithcbe61d82009-02-09 13:27:12 +05302572static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05302573 u32 gpio, u32 type)
2574{
2575 int addr;
2576 u32 gpio_shift, tmp;
2577
2578 if (gpio > 11)
2579 addr = AR_GPIO_OUTPUT_MUX3;
2580 else if (gpio > 5)
2581 addr = AR_GPIO_OUTPUT_MUX2;
2582 else
2583 addr = AR_GPIO_OUTPUT_MUX1;
2584
2585 gpio_shift = (gpio % 6) * 5;
2586
2587 if (AR_SREV_9280_20_OR_LATER(ah)
2588 || (addr != AR_GPIO_OUTPUT_MUX1)) {
2589 REG_RMW(ah, addr, (type << gpio_shift),
2590 (0x1f << gpio_shift));
2591 } else {
2592 tmp = REG_READ(ah, addr);
2593 tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
2594 tmp &= ~(0x1f << gpio_shift);
2595 tmp |= (type << gpio_shift);
2596 REG_WRITE(ah, addr, tmp);
2597 }
2598}
2599
Sujithcbe61d82009-02-09 13:27:12 +05302600void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
Sujithf1dc5602008-10-29 10:16:30 +05302601{
2602 u32 gpio_shift;
2603
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -07002604 BUG_ON(gpio >= ah->caps.num_gpio_pins);
Sujithf1dc5602008-10-29 10:16:30 +05302605
Sujith88c1f4f2010-06-30 14:46:31 +05302606 if (AR_DEVID_7010(ah)) {
2607 gpio_shift = gpio;
2608 REG_RMW(ah, AR7010_GPIO_OE,
2609 (AR7010_GPIO_OE_AS_INPUT << gpio_shift),
2610 (AR7010_GPIO_OE_MASK << gpio_shift));
2611 return;
2612 }
Sujithf1dc5602008-10-29 10:16:30 +05302613
Sujith88c1f4f2010-06-30 14:46:31 +05302614 gpio_shift = gpio << 1;
Sujithf1dc5602008-10-29 10:16:30 +05302615 REG_RMW(ah,
2616 AR_GPIO_OE_OUT,
2617 (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
2618 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2619}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002620EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
Sujithf1dc5602008-10-29 10:16:30 +05302621
Sujithcbe61d82009-02-09 13:27:12 +05302622u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
Sujithf1dc5602008-10-29 10:16:30 +05302623{
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302624#define MS_REG_READ(x, y) \
2625 (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
2626
Sujith2660b812009-02-09 13:27:26 +05302627 if (gpio >= ah->caps.num_gpio_pins)
Sujithf1dc5602008-10-29 10:16:30 +05302628 return 0xffffffff;
2629
Sujith88c1f4f2010-06-30 14:46:31 +05302630 if (AR_DEVID_7010(ah)) {
2631 u32 val;
2632 val = REG_READ(ah, AR7010_GPIO_IN);
2633 return (MS(val, AR7010_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) == 0;
2634 } else if (AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan93069902010-11-30 23:24:09 -08002635 return (MS(REG_READ(ah, AR_GPIO_IN), AR9300_GPIO_IN_VAL) &
2636 AR_GPIO_BIT(gpio)) != 0;
Felix Fietkau783dfca2010-04-15 17:38:11 -04002637 else if (AR_SREV_9271(ah))
Sujith5b5fa352010-03-17 14:25:15 +05302638 return MS_REG_READ(AR9271, gpio) != 0;
Felix Fietkaua42acef2010-09-22 12:34:54 +02002639 else if (AR_SREV_9287_11_OR_LATER(ah))
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05302640 return MS_REG_READ(AR9287, gpio) != 0;
Felix Fietkaue17f83e2010-09-22 12:34:53 +02002641 else if (AR_SREV_9285_12_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302642 return MS_REG_READ(AR9285, gpio) != 0;
Felix Fietkau7a370812010-09-22 12:34:52 +02002643 else if (AR_SREV_9280_20_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302644 return MS_REG_READ(AR928X, gpio) != 0;
2645 else
2646 return MS_REG_READ(AR, gpio) != 0;
Sujithf1dc5602008-10-29 10:16:30 +05302647}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002648EXPORT_SYMBOL(ath9k_hw_gpio_get);
Sujithf1dc5602008-10-29 10:16:30 +05302649
Sujithcbe61d82009-02-09 13:27:12 +05302650void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
Sujithf1dc5602008-10-29 10:16:30 +05302651 u32 ah_signal_type)
2652{
2653 u32 gpio_shift;
2654
Sujith88c1f4f2010-06-30 14:46:31 +05302655 if (AR_DEVID_7010(ah)) {
2656 gpio_shift = gpio;
2657 REG_RMW(ah, AR7010_GPIO_OE,
2658 (AR7010_GPIO_OE_AS_OUTPUT << gpio_shift),
2659 (AR7010_GPIO_OE_MASK << gpio_shift));
2660 return;
2661 }
2662
Sujithf1dc5602008-10-29 10:16:30 +05302663 ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
Sujithf1dc5602008-10-29 10:16:30 +05302664 gpio_shift = 2 * gpio;
Sujithf1dc5602008-10-29 10:16:30 +05302665 REG_RMW(ah,
2666 AR_GPIO_OE_OUT,
2667 (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
2668 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2669}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002670EXPORT_SYMBOL(ath9k_hw_cfg_output);
Sujithf1dc5602008-10-29 10:16:30 +05302671
Sujithcbe61d82009-02-09 13:27:12 +05302672void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
Sujithf1dc5602008-10-29 10:16:30 +05302673{
Sujith88c1f4f2010-06-30 14:46:31 +05302674 if (AR_DEVID_7010(ah)) {
2675 val = val ? 0 : 1;
2676 REG_RMW(ah, AR7010_GPIO_OUT, ((val&1) << gpio),
2677 AR_GPIO_BIT(gpio));
2678 return;
2679 }
2680
Sujith5b5fa352010-03-17 14:25:15 +05302681 if (AR_SREV_9271(ah))
2682 val = ~val;
2683
Sujithf1dc5602008-10-29 10:16:30 +05302684 REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
2685 AR_GPIO_BIT(gpio));
2686}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002687EXPORT_SYMBOL(ath9k_hw_set_gpio);
Sujithf1dc5602008-10-29 10:16:30 +05302688
Sujithcbe61d82009-02-09 13:27:12 +05302689void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
Sujithf1dc5602008-10-29 10:16:30 +05302690{
2691 REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
2692}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002693EXPORT_SYMBOL(ath9k_hw_setantenna);
Sujithf1dc5602008-10-29 10:16:30 +05302694
Sujithf1dc5602008-10-29 10:16:30 +05302695/*********************/
2696/* General Operation */
2697/*********************/
2698
Sujithcbe61d82009-02-09 13:27:12 +05302699u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302700{
2701 u32 bits = REG_READ(ah, AR_RX_FILTER);
2702 u32 phybits = REG_READ(ah, AR_PHY_ERR);
2703
2704 if (phybits & AR_PHY_ERR_RADAR)
2705 bits |= ATH9K_RX_FILTER_PHYRADAR;
2706 if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
2707 bits |= ATH9K_RX_FILTER_PHYERR;
2708
2709 return bits;
2710}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002711EXPORT_SYMBOL(ath9k_hw_getrxfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302712
Sujithcbe61d82009-02-09 13:27:12 +05302713void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
Sujithf1dc5602008-10-29 10:16:30 +05302714{
2715 u32 phybits;
2716
Sujith7d0d0df2010-04-16 11:53:57 +05302717 ENABLE_REGWRITE_BUFFER(ah);
2718
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05302719 if (AR_SREV_9462(ah))
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05302720 bits |= ATH9K_RX_FILTER_CONTROL_WRAPPER;
2721
Sujith7ea310b2009-09-03 12:08:43 +05302722 REG_WRITE(ah, AR_RX_FILTER, bits);
2723
Sujithf1dc5602008-10-29 10:16:30 +05302724 phybits = 0;
2725 if (bits & ATH9K_RX_FILTER_PHYRADAR)
2726 phybits |= AR_PHY_ERR_RADAR;
2727 if (bits & ATH9K_RX_FILTER_PHYERR)
2728 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
2729 REG_WRITE(ah, AR_PHY_ERR, phybits);
2730
2731 if (phybits)
Felix Fietkauca7a4de2011-03-23 20:57:26 +01002732 REG_SET_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
Sujithf1dc5602008-10-29 10:16:30 +05302733 else
Felix Fietkauca7a4de2011-03-23 20:57:26 +01002734 REG_CLR_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
Sujith7d0d0df2010-04-16 11:53:57 +05302735
2736 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +05302737}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002738EXPORT_SYMBOL(ath9k_hw_setrxfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302739
Sujithcbe61d82009-02-09 13:27:12 +05302740bool ath9k_hw_phy_disable(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302741{
Senthil Balasubramanian63a75b92009-09-18 15:07:03 +05302742 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
2743 return false;
2744
2745 ath9k_hw_init_pll(ah, NULL);
Felix Fietkau8efa7a82012-03-14 16:40:23 +01002746 ah->htc_reset_init = true;
Senthil Balasubramanian63a75b92009-09-18 15:07:03 +05302747 return true;
Sujithf1dc5602008-10-29 10:16:30 +05302748}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002749EXPORT_SYMBOL(ath9k_hw_phy_disable);
Sujithf1dc5602008-10-29 10:16:30 +05302750
Sujithcbe61d82009-02-09 13:27:12 +05302751bool ath9k_hw_disable(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302752{
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07002753 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Sujithf1dc5602008-10-29 10:16:30 +05302754 return false;
2755
Senthil Balasubramanian63a75b92009-09-18 15:07:03 +05302756 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
2757 return false;
2758
2759 ath9k_hw_init_pll(ah, NULL);
2760 return true;
Sujithf1dc5602008-10-29 10:16:30 +05302761}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002762EXPORT_SYMBOL(ath9k_hw_disable);
Sujithf1dc5602008-10-29 10:16:30 +05302763
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002764static int get_antenna_gain(struct ath_hw *ah, struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +05302765{
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002766 enum eeprom_param gain_param;
Felix Fietkau9c204b42011-07-27 15:01:05 +02002767
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002768 if (IS_CHAN_2GHZ(chan))
2769 gain_param = EEP_ANTENNA_GAIN_2G;
2770 else
2771 gain_param = EEP_ANTENNA_GAIN_5G;
Sujithf1dc5602008-10-29 10:16:30 +05302772
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002773 return ah->eep_ops->get_eeprom(ah, gain_param);
2774}
2775
2776void ath9k_hw_apply_txpower(struct ath_hw *ah, struct ath9k_channel *chan)
2777{
2778 struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
2779 struct ieee80211_channel *channel;
2780 int chan_pwr, new_pwr, max_gain;
2781 int ant_gain, ant_reduction = 0;
2782
2783 if (!chan)
2784 return;
2785
2786 channel = chan->chan;
2787 chan_pwr = min_t(int, channel->max_power * 2, MAX_RATE_POWER);
2788 new_pwr = min_t(int, chan_pwr, reg->power_limit);
2789 max_gain = chan_pwr - new_pwr + channel->max_antenna_gain * 2;
2790
2791 ant_gain = get_antenna_gain(ah, chan);
2792 if (ant_gain > max_gain)
2793 ant_reduction = ant_gain - max_gain;
Sujithf1dc5602008-10-29 10:16:30 +05302794
Vasanthakumar Thiagarajan8fbff4b2009-05-08 17:54:51 -07002795 ah->eep_ops->set_txpower(ah, chan,
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002796 ath9k_regd_get_ctl(reg, chan),
2797 ant_reduction, new_pwr, false);
2798}
2799
2800void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit, bool test)
2801{
2802 struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
2803 struct ath9k_channel *chan = ah->curchan;
2804 struct ieee80211_channel *channel = chan->chan;
2805
Dan Carpenter48ef5c42011-10-17 10:28:23 +03002806 reg->power_limit = min_t(u32, limit, MAX_RATE_POWER);
Felix Fietkauca2c68c2011-10-08 20:06:20 +02002807 if (test)
2808 channel->max_power = MAX_RATE_POWER / 2;
2809
2810 ath9k_hw_apply_txpower(ah, chan);
2811
2812 if (test)
2813 channel->max_power = DIV_ROUND_UP(reg->max_power_level, 2);
Sujithf1dc5602008-10-29 10:16:30 +05302814}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002815EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
Sujithf1dc5602008-10-29 10:16:30 +05302816
Sujithcbe61d82009-02-09 13:27:12 +05302817void ath9k_hw_setopmode(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302818{
Sujith2660b812009-02-09 13:27:26 +05302819 ath9k_hw_set_operating_mode(ah, ah->opmode);
Sujithf1dc5602008-10-29 10:16:30 +05302820}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002821EXPORT_SYMBOL(ath9k_hw_setopmode);
Sujithf1dc5602008-10-29 10:16:30 +05302822
Sujithcbe61d82009-02-09 13:27:12 +05302823void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
Sujithf1dc5602008-10-29 10:16:30 +05302824{
2825 REG_WRITE(ah, AR_MCAST_FIL0, filter0);
2826 REG_WRITE(ah, AR_MCAST_FIL1, filter1);
2827}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002828EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302829
Luis R. Rodriguezf2b21432009-09-10 08:50:20 -07002830void ath9k_hw_write_associd(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302831{
Luis R. Rodriguez15107182009-09-10 09:22:37 -07002832 struct ath_common *common = ath9k_hw_common(ah);
2833
2834 REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
2835 REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
2836 ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
Sujithf1dc5602008-10-29 10:16:30 +05302837}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002838EXPORT_SYMBOL(ath9k_hw_write_associd);
Sujithf1dc5602008-10-29 10:16:30 +05302839
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002840#define ATH9K_MAX_TSF_READ 10
2841
Sujithcbe61d82009-02-09 13:27:12 +05302842u64 ath9k_hw_gettsf64(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302843{
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002844 u32 tsf_lower, tsf_upper1, tsf_upper2;
2845 int i;
Sujithf1dc5602008-10-29 10:16:30 +05302846
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002847 tsf_upper1 = REG_READ(ah, AR_TSF_U32);
2848 for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
2849 tsf_lower = REG_READ(ah, AR_TSF_L32);
2850 tsf_upper2 = REG_READ(ah, AR_TSF_U32);
2851 if (tsf_upper2 == tsf_upper1)
2852 break;
2853 tsf_upper1 = tsf_upper2;
2854 }
Sujithf1dc5602008-10-29 10:16:30 +05302855
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002856 WARN_ON( i == ATH9K_MAX_TSF_READ );
2857
2858 return (((u64)tsf_upper1 << 32) | tsf_lower);
Sujithf1dc5602008-10-29 10:16:30 +05302859}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002860EXPORT_SYMBOL(ath9k_hw_gettsf64);
Sujithf1dc5602008-10-29 10:16:30 +05302861
Sujithcbe61d82009-02-09 13:27:12 +05302862void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002863{
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002864 REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
Alina Friedrichsenb9a16192009-03-02 23:28:38 +01002865 REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002866}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002867EXPORT_SYMBOL(ath9k_hw_settsf64);
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002868
Sujithcbe61d82009-02-09 13:27:12 +05302869void ath9k_hw_reset_tsf(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302870{
Gabor Juhosf9b604f2009-06-21 00:02:15 +02002871 if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
2872 AH_TSF_WRITE_TIMEOUT))
Joe Perchesd2182b62011-12-15 14:55:53 -08002873 ath_dbg(ath9k_hw_common(ah), RESET,
Joe Perches226afe62010-12-02 19:12:37 -08002874 "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
Gabor Juhosf9b604f2009-06-21 00:02:15 +02002875
Sujithf1dc5602008-10-29 10:16:30 +05302876 REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002877}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002878EXPORT_SYMBOL(ath9k_hw_reset_tsf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002879
Sujith54e4cec2009-08-07 09:45:09 +05302880void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002881{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002882 if (setting)
Sujith2660b812009-02-09 13:27:26 +05302883 ah->misc_mode |= AR_PCU_TX_ADD_TSF;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002884 else
Sujith2660b812009-02-09 13:27:26 +05302885 ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002886}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002887EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002888
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002889void ath9k_hw_set11nmac2040(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002890{
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002891 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Sujithf1dc5602008-10-29 10:16:30 +05302892 u32 macmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002893
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002894 if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
Sujithf1dc5602008-10-29 10:16:30 +05302895 macmode = AR_2040_JOINED_RX_CLEAR;
2896 else
2897 macmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002898
Sujithf1dc5602008-10-29 10:16:30 +05302899 REG_WRITE(ah, AR_2040_MODE, macmode);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002900}
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302901
2902/* HW Generic timers configuration */
2903
2904static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
2905{
2906 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2907 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2908 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2909 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2910 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2911 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2912 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2913 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2914 {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
2915 {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
2916 AR_NDP2_TIMER_MODE, 0x0002},
2917 {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
2918 AR_NDP2_TIMER_MODE, 0x0004},
2919 {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
2920 AR_NDP2_TIMER_MODE, 0x0008},
2921 {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
2922 AR_NDP2_TIMER_MODE, 0x0010},
2923 {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
2924 AR_NDP2_TIMER_MODE, 0x0020},
2925 {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
2926 AR_NDP2_TIMER_MODE, 0x0040},
2927 {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
2928 AR_NDP2_TIMER_MODE, 0x0080}
2929};
2930
2931/* HW generic timer primitives */
2932
2933/* compute and clear index of rightmost 1 */
2934static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
2935{
2936 u32 b;
2937
2938 b = *mask;
2939 b &= (0-b);
2940 *mask &= ~b;
2941 b *= debruijn32;
2942 b >>= 27;
2943
2944 return timer_table->gen_timer_index[b];
2945}
2946
Felix Fietkaudd347f22011-03-22 21:54:17 +01002947u32 ath9k_hw_gettsf32(struct ath_hw *ah)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302948{
2949 return REG_READ(ah, AR_TSF_L32);
2950}
Felix Fietkaudd347f22011-03-22 21:54:17 +01002951EXPORT_SYMBOL(ath9k_hw_gettsf32);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302952
2953struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
2954 void (*trigger)(void *),
2955 void (*overflow)(void *),
2956 void *arg,
2957 u8 timer_index)
2958{
2959 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2960 struct ath_gen_timer *timer;
2961
2962 timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
2963
2964 if (timer == NULL) {
Joe Perches38002762010-12-02 19:12:36 -08002965 ath_err(ath9k_hw_common(ah),
2966 "Failed to allocate memory for hw timer[%d]\n",
2967 timer_index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302968 return NULL;
2969 }
2970
2971 /* allocate a hardware generic timer slot */
2972 timer_table->timers[timer_index] = timer;
2973 timer->index = timer_index;
2974 timer->trigger = trigger;
2975 timer->overflow = overflow;
2976 timer->arg = arg;
2977
2978 return timer;
2979}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002980EXPORT_SYMBOL(ath_gen_timer_alloc);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302981
Luis R. Rodriguezcd9bf682009-09-13 02:08:34 -07002982void ath9k_hw_gen_timer_start(struct ath_hw *ah,
2983 struct ath_gen_timer *timer,
Vasanthakumar Thiagarajan788f6872011-04-21 18:33:27 +05302984 u32 trig_timeout,
Luis R. Rodriguezcd9bf682009-09-13 02:08:34 -07002985 u32 timer_period)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302986{
2987 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
Vasanthakumar Thiagarajan788f6872011-04-21 18:33:27 +05302988 u32 tsf, timer_next;
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302989
2990 BUG_ON(!timer_period);
2991
2992 set_bit(timer->index, &timer_table->timer_mask.timer_bits);
2993
2994 tsf = ath9k_hw_gettsf32(ah);
2995
Vasanthakumar Thiagarajan788f6872011-04-21 18:33:27 +05302996 timer_next = tsf + trig_timeout;
2997
Joe Perchesd2182b62011-12-15 14:55:53 -08002998 ath_dbg(ath9k_hw_common(ah), HWTIMER,
Joe Perches226afe62010-12-02 19:12:37 -08002999 "current tsf %x period %x timer_next %x\n",
3000 tsf, timer_period, timer_next);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303001
3002 /*
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303003 * Program generic timer registers
3004 */
3005 REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
3006 timer_next);
3007 REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
3008 timer_period);
3009 REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
3010 gen_tmr_configuration[timer->index].mode_mask);
3011
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05303012 if (AR_SREV_9462(ah)) {
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05303013 /*
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05303014 * Starting from AR9462, each generic timer can select which tsf
Senthil Balasubramanian2577c6e2011-09-13 22:38:18 +05303015 * to use. But we still follow the old rule, 0 - 7 use tsf and
3016 * 8 - 15 use tsf2.
3017 */
3018 if ((timer->index < AR_GEN_TIMER_BANK_1_LEN))
3019 REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
3020 (1 << timer->index));
3021 else
3022 REG_SET_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
3023 (1 << timer->index));
3024 }
3025
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303026 /* Enable both trigger and thresh interrupt masks */
3027 REG_SET_BIT(ah, AR_IMR_S5,
3028 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
3029 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303030}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04003031EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303032
Luis R. Rodriguezcd9bf682009-09-13 02:08:34 -07003033void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303034{
3035 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
3036
3037 if ((timer->index < AR_FIRST_NDP_TIMER) ||
3038 (timer->index >= ATH_MAX_GEN_TIMER)) {
3039 return;
3040 }
3041
3042 /* Clear generic timer enable bits. */
3043 REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
3044 gen_tmr_configuration[timer->index].mode_mask);
3045
3046 /* Disable both trigger and thresh interrupt masks */
3047 REG_CLR_BIT(ah, AR_IMR_S5,
3048 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
3049 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
3050
3051 clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303052}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04003053EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303054
3055void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
3056{
3057 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
3058
3059 /* free the hardware generic timer slot */
3060 timer_table->timers[timer->index] = NULL;
3061 kfree(timer);
3062}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04003063EXPORT_SYMBOL(ath_gen_timer_free);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303064
3065/*
3066 * Generic Timer Interrupts handling
3067 */
3068void ath_gen_timer_isr(struct ath_hw *ah)
3069{
3070 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
3071 struct ath_gen_timer *timer;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07003072 struct ath_common *common = ath9k_hw_common(ah);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303073 u32 trigger_mask, thresh_mask, index;
3074
3075 /* get hardware generic timer interrupt status */
3076 trigger_mask = ah->intr_gen_timer_trigger;
3077 thresh_mask = ah->intr_gen_timer_thresh;
3078 trigger_mask &= timer_table->timer_mask.val;
3079 thresh_mask &= timer_table->timer_mask.val;
3080
3081 trigger_mask &= ~thresh_mask;
3082
3083 while (thresh_mask) {
3084 index = rightmost_index(timer_table, &thresh_mask);
3085 timer = timer_table->timers[index];
3086 BUG_ON(!timer);
Joe Perchesd2182b62011-12-15 14:55:53 -08003087 ath_dbg(common, HWTIMER, "TSF overflow for Gen timer %d\n",
3088 index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303089 timer->overflow(timer->arg);
3090 }
3091
3092 while (trigger_mask) {
3093 index = rightmost_index(timer_table, &trigger_mask);
3094 timer = timer_table->timers[index];
3095 BUG_ON(!timer);
Joe Perchesd2182b62011-12-15 14:55:53 -08003096 ath_dbg(common, HWTIMER,
Joe Perches226afe62010-12-02 19:12:37 -08003097 "Gen timer[%d] trigger\n", index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05303098 timer->trigger(timer->arg);
3099 }
3100}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04003101EXPORT_SYMBOL(ath_gen_timer_isr);
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003102
Sujith05020d22010-03-17 14:25:23 +05303103/********/
3104/* HTC */
3105/********/
3106
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003107static struct {
3108 u32 version;
3109 const char * name;
3110} ath_mac_bb_names[] = {
3111 /* Devices with external radios */
3112 { AR_SREV_VERSION_5416_PCI, "5416" },
3113 { AR_SREV_VERSION_5416_PCIE, "5418" },
3114 { AR_SREV_VERSION_9100, "9100" },
3115 { AR_SREV_VERSION_9160, "9160" },
3116 /* Single-chip solutions */
3117 { AR_SREV_VERSION_9280, "9280" },
3118 { AR_SREV_VERSION_9285, "9285" },
Luis R. Rodriguez11158472009-10-27 12:59:35 -04003119 { AR_SREV_VERSION_9287, "9287" },
3120 { AR_SREV_VERSION_9271, "9271" },
Luis R. Rodriguezec839032010-04-15 17:39:20 -04003121 { AR_SREV_VERSION_9300, "9300" },
Gabor Juhos2c8e5932011-06-21 11:23:21 +02003122 { AR_SREV_VERSION_9330, "9330" },
Florian Fainelli397e5d52011-08-25 21:33:48 +02003123 { AR_SREV_VERSION_9340, "9340" },
Senthil Balasubramanian8f06ca22011-04-01 17:16:33 +05303124 { AR_SREV_VERSION_9485, "9485" },
Rajkumar Manoharan423e38e2011-10-13 11:00:44 +05303125 { AR_SREV_VERSION_9462, "9462" },
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003126};
3127
3128/* For devices with external radios */
3129static struct {
3130 u16 version;
3131 const char * name;
3132} ath_rf_names[] = {
3133 { 0, "5133" },
3134 { AR_RAD5133_SREV_MAJOR, "5133" },
3135 { AR_RAD5122_SREV_MAJOR, "5122" },
3136 { AR_RAD2133_SREV_MAJOR, "2133" },
3137 { AR_RAD2122_SREV_MAJOR, "2122" }
3138};
3139
3140/*
3141 * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
3142 */
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04003143static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003144{
3145 int i;
3146
3147 for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
3148 if (ath_mac_bb_names[i].version == mac_bb_version) {
3149 return ath_mac_bb_names[i].name;
3150 }
3151 }
3152
3153 return "????";
3154}
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003155
3156/*
3157 * Return the RF name. "????" is returned if the RF is unknown.
3158 * Used for devices with external radios.
3159 */
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04003160static const char *ath9k_hw_rf_name(u16 rf_version)
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04003161{
3162 int i;
3163
3164 for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
3165 if (ath_rf_names[i].version == rf_version) {
3166 return ath_rf_names[i].name;
3167 }
3168 }
3169
3170 return "????";
3171}
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04003172
3173void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
3174{
3175 int used;
3176
3177 /* chipsets >= AR9280 are single-chip */
Felix Fietkau7a370812010-09-22 12:34:52 +02003178 if (AR_SREV_9280_20_OR_LATER(ah)) {
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04003179 used = snprintf(hw_name, len,
3180 "Atheros AR%s Rev:%x",
3181 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
3182 ah->hw_version.macRev);
3183 }
3184 else {
3185 used = snprintf(hw_name, len,
3186 "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
3187 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
3188 ah->hw_version.macRev,
3189 ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
3190 AR_RADIO_SREV_MAJOR)),
3191 ah->hw_version.phyRev);
3192 }
3193
3194 hw_name[used] = '\0';
3195}
3196EXPORT_SYMBOL(ath9k_hw_name);