blob: bb9a3f3c1b712452215b25e0437c395c47f34a92 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Luis R. Rodriguezb3950e62010-04-15 17:39:03 -04002 * Copyright (c) 2008-2010 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>
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070019#include <asm/unaligned.h>
20
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -070021#include "hw.h"
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -040022#include "hw-ops.h"
Luis R. Rodriguezcfe8cba2009-09-13 23:39:31 -070023#include "rc.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040024#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070025
Sujithcbe61d82009-02-09 13:27:12 +053026static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070027
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -040028MODULE_AUTHOR("Atheros Communications");
29MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
30MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
31MODULE_LICENSE("Dual BSD/GPL");
32
33static int __init ath9k_init(void)
34{
35 return 0;
36}
37module_init(ath9k_init);
38
39static void __exit ath9k_exit(void)
40{
41 return;
42}
43module_exit(ath9k_exit);
44
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -040045/* Private hardware callbacks */
46
47static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
48{
49 ath9k_hw_private_ops(ah)->init_cal_settings(ah);
50}
51
52static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
53{
54 ath9k_hw_private_ops(ah)->init_mode_regs(ah);
55}
56
Luis R. Rodriguez64773962010-04-15 17:38:17 -040057static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
58 struct ath9k_channel *chan)
59{
60 return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
61}
62
Luis R. Rodriguez991312d2010-04-15 17:39:05 -040063static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
64{
65 if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
66 return;
67
68 ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
69}
70
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -040071static void ath9k_hw_ani_cache_ini_regs(struct ath_hw *ah)
72{
73 /* You will not have this callback if using the old ANI */
74 if (!ath9k_hw_private_ops(ah)->ani_cache_ini_regs)
75 return;
76
77 ath9k_hw_private_ops(ah)->ani_cache_ini_regs(ah);
78}
79
Sujithf1dc5602008-10-29 10:16:30 +053080/********************/
81/* Helper Functions */
82/********************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070083
Felix Fietkaudfdac8a2010-10-08 22:13:51 +020084static void ath9k_hw_set_clockrate(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +053085{
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -070086 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Felix Fietkaudfdac8a2010-10-08 22:13:51 +020087 struct ath_common *common = ath9k_hw_common(ah);
88 unsigned int clockrate;
Sujithcbe61d82009-02-09 13:27:12 +053089
Sujith2660b812009-02-09 13:27:26 +053090 if (!ah->curchan) /* should really check for CCK instead */
Felix Fietkaudfdac8a2010-10-08 22:13:51 +020091 clockrate = ATH9K_CLOCK_RATE_CCK;
92 else if (conf->channel->band == IEEE80211_BAND_2GHZ)
93 clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
94 else if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
95 clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
Vasanthakumar Thiagarajane5553722010-04-26 15:04:33 -040096 else
Felix Fietkaudfdac8a2010-10-08 22:13:51 +020097 clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
98
99 if (conf_is_ht40(conf))
100 clockrate *= 2;
101
102 common->clockrate = clockrate;
Sujithf1dc5602008-10-29 10:16:30 +0530103}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700104
Sujithcbe61d82009-02-09 13:27:12 +0530105static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
Sujithf1dc5602008-10-29 10:16:30 +0530106{
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200107 struct ath_common *common = ath9k_hw_common(ah);
Sujithcbe61d82009-02-09 13:27:12 +0530108
Felix Fietkaudfdac8a2010-10-08 22:13:51 +0200109 return usecs * common->clockrate;
Sujithf1dc5602008-10-29 10:16:30 +0530110}
111
Sujith0caa7b12009-02-16 13:23:20 +0530112bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700113{
114 int i;
115
Sujith0caa7b12009-02-16 13:23:20 +0530116 BUG_ON(timeout < AH_TIME_QUANTUM);
117
118 for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700119 if ((REG_READ(ah, reg) & mask) == val)
120 return true;
121
122 udelay(AH_TIME_QUANTUM);
123 }
Sujith04bd4632008-11-28 22:18:05 +0530124
Joe Perches226afe62010-12-02 19:12:37 -0800125 ath_dbg(ath9k_hw_common(ah), ATH_DBG_ANY,
126 "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
127 timeout, reg, REG_READ(ah, reg), mask, val);
Sujithf1dc5602008-10-29 10:16:30 +0530128
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700129 return false;
130}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400131EXPORT_SYMBOL(ath9k_hw_wait);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700132
Felix Fietkaua9b6b252011-03-23 20:57:27 +0100133void ath9k_hw_write_array(struct ath_hw *ah, struct ar5416IniArray *array,
134 int column, unsigned int *writecnt)
135{
136 int r;
137
138 ENABLE_REGWRITE_BUFFER(ah);
139 for (r = 0; r < array->ia_rows; r++) {
140 REG_WRITE(ah, INI_RA(array, r, 0),
141 INI_RA(array, r, column));
142 DO_DELAY(*writecnt);
143 }
144 REGWRITE_BUFFER_FLUSH(ah);
145}
146
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700147u32 ath9k_hw_reverse_bits(u32 val, u32 n)
148{
149 u32 retval;
150 int i;
151
152 for (i = 0, retval = 0; i < n; i++) {
153 retval = (retval << 1) | (val & 1);
154 val >>= 1;
155 }
156 return retval;
157}
158
Sujithcbe61d82009-02-09 13:27:12 +0530159bool ath9k_get_channel_edges(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530160 u16 flags, u16 *low,
161 u16 *high)
162{
Sujith2660b812009-02-09 13:27:26 +0530163 struct ath9k_hw_capabilities *pCap = &ah->caps;
Sujithf1dc5602008-10-29 10:16:30 +0530164
165 if (flags & CHANNEL_5GHZ) {
166 *low = pCap->low_5ghz_chan;
167 *high = pCap->high_5ghz_chan;
168 return true;
169 }
170 if ((flags & CHANNEL_2GHZ)) {
171 *low = pCap->low_2ghz_chan;
172 *high = pCap->high_2ghz_chan;
173 return true;
174 }
175 return false;
176}
177
Sujithcbe61d82009-02-09 13:27:12 +0530178u16 ath9k_hw_computetxtime(struct ath_hw *ah,
Felix Fietkau545750d2009-11-23 22:21:01 +0100179 u8 phy, int kbps,
Sujithf1dc5602008-10-29 10:16:30 +0530180 u32 frameLen, u16 rateix,
181 bool shortPreamble)
182{
183 u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
Sujithf1dc5602008-10-29 10:16:30 +0530184
185 if (kbps == 0)
186 return 0;
187
Felix Fietkau545750d2009-11-23 22:21:01 +0100188 switch (phy) {
Sujith46d14a52008-11-18 09:08:13 +0530189 case WLAN_RC_PHY_CCK:
Sujithf1dc5602008-10-29 10:16:30 +0530190 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
Felix Fietkau545750d2009-11-23 22:21:01 +0100191 if (shortPreamble)
Sujithf1dc5602008-10-29 10:16:30 +0530192 phyTime >>= 1;
193 numBits = frameLen << 3;
194 txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
195 break;
Sujith46d14a52008-11-18 09:08:13 +0530196 case WLAN_RC_PHY_OFDM:
Sujith2660b812009-02-09 13:27:26 +0530197 if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
Sujithf1dc5602008-10-29 10:16:30 +0530198 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
199 numBits = OFDM_PLCP_BITS + (frameLen << 3);
200 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
201 txTime = OFDM_SIFS_TIME_QUARTER
202 + OFDM_PREAMBLE_TIME_QUARTER
203 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
Sujith2660b812009-02-09 13:27:26 +0530204 } else if (ah->curchan &&
205 IS_CHAN_HALF_RATE(ah->curchan)) {
Sujithf1dc5602008-10-29 10:16:30 +0530206 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
207 numBits = OFDM_PLCP_BITS + (frameLen << 3);
208 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
209 txTime = OFDM_SIFS_TIME_HALF +
210 OFDM_PREAMBLE_TIME_HALF
211 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
212 } else {
213 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
214 numBits = OFDM_PLCP_BITS + (frameLen << 3);
215 numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
216 txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
217 + (numSymbols * OFDM_SYMBOL_TIME);
218 }
219 break;
220 default:
Joe Perches38002762010-12-02 19:12:36 -0800221 ath_err(ath9k_hw_common(ah),
222 "Unknown phy %u (rate ix %u)\n", phy, rateix);
Sujithf1dc5602008-10-29 10:16:30 +0530223 txTime = 0;
224 break;
225 }
226
227 return txTime;
228}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -0400229EXPORT_SYMBOL(ath9k_hw_computetxtime);
Sujithf1dc5602008-10-29 10:16:30 +0530230
Sujithcbe61d82009-02-09 13:27:12 +0530231void ath9k_hw_get_channel_centers(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530232 struct ath9k_channel *chan,
233 struct chan_centers *centers)
234{
235 int8_t extoff;
Sujithf1dc5602008-10-29 10:16:30 +0530236
237 if (!IS_CHAN_HT40(chan)) {
238 centers->ctl_center = centers->ext_center =
239 centers->synth_center = chan->channel;
240 return;
241 }
242
243 if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
244 (chan->chanmode == CHANNEL_G_HT40PLUS)) {
245 centers->synth_center =
246 chan->channel + HT40_CHANNEL_CENTER_SHIFT;
247 extoff = 1;
248 } else {
249 centers->synth_center =
250 chan->channel - HT40_CHANNEL_CENTER_SHIFT;
251 extoff = -1;
252 }
253
254 centers->ctl_center =
255 centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
Luis R. Rodriguez64200142009-09-13 22:05:04 -0700256 /* 25 MHz spacing is supported by hw but not on upper layers */
Sujithf1dc5602008-10-29 10:16:30 +0530257 centers->ext_center =
Luis R. Rodriguez64200142009-09-13 22:05:04 -0700258 centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
Sujithf1dc5602008-10-29 10:16:30 +0530259}
260
261/******************/
262/* Chip Revisions */
263/******************/
264
Sujithcbe61d82009-02-09 13:27:12 +0530265static void ath9k_hw_read_revisions(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530266{
267 u32 val;
268
269 val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
270
271 if (val == 0xFF) {
272 val = REG_READ(ah, AR_SREV);
Sujithd535a422009-02-09 13:27:06 +0530273 ah->hw_version.macVersion =
274 (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
275 ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
Sujith2660b812009-02-09 13:27:26 +0530276 ah->is_pciexpress = (val & AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
Sujithf1dc5602008-10-29 10:16:30 +0530277 } else {
278 if (!AR_SREV_9100(ah))
Sujithd535a422009-02-09 13:27:06 +0530279 ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
Sujithf1dc5602008-10-29 10:16:30 +0530280
Sujithd535a422009-02-09 13:27:06 +0530281 ah->hw_version.macRev = val & AR_SREV_REVISION;
Sujithf1dc5602008-10-29 10:16:30 +0530282
Sujithd535a422009-02-09 13:27:06 +0530283 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
Sujith2660b812009-02-09 13:27:26 +0530284 ah->is_pciexpress = true;
Sujithf1dc5602008-10-29 10:16:30 +0530285 }
286}
287
Sujithf1dc5602008-10-29 10:16:30 +0530288/************************************/
289/* HW Attach, Detach, Init Routines */
290/************************************/
291
Sujithcbe61d82009-02-09 13:27:12 +0530292static void ath9k_hw_disablepcie(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530293{
Felix Fietkau040b74f2010-12-12 00:51:07 +0100294 if (!AR_SREV_5416(ah))
Sujithf1dc5602008-10-29 10:16:30 +0530295 return;
296
297 REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
298 REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
299 REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
300 REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
301 REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
302 REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
303 REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
304 REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
305 REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
306
307 REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
308}
309
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400310/* This should work for all families including legacy */
Sujithcbe61d82009-02-09 13:27:12 +0530311static bool ath9k_hw_chip_test(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530312{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700313 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400314 u32 regAddr[2] = { AR_STA_ID0 };
Sujithf1dc5602008-10-29 10:16:30 +0530315 u32 regHold[2];
Joe Perches07b2fa52010-11-20 18:38:53 -0800316 static const u32 patternData[4] = {
317 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999
318 };
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400319 int i, j, loop_max;
Sujithf1dc5602008-10-29 10:16:30 +0530320
Senthil Balasubramanian1f3f0612010-04-15 17:38:29 -0400321 if (!AR_SREV_9300_20_OR_LATER(ah)) {
322 loop_max = 2;
323 regAddr[1] = AR_PHY_BASE + (8 << 2);
324 } else
325 loop_max = 1;
326
327 for (i = 0; i < loop_max; i++) {
Sujithf1dc5602008-10-29 10:16:30 +0530328 u32 addr = regAddr[i];
329 u32 wrData, rdData;
330
331 regHold[i] = REG_READ(ah, addr);
332 for (j = 0; j < 0x100; j++) {
333 wrData = (j << 16) | j;
334 REG_WRITE(ah, addr, wrData);
335 rdData = REG_READ(ah, addr);
336 if (rdData != wrData) {
Joe Perches38002762010-12-02 19:12:36 -0800337 ath_err(common,
338 "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
339 addr, wrData, rdData);
Sujithf1dc5602008-10-29 10:16:30 +0530340 return false;
341 }
342 }
343 for (j = 0; j < 4; j++) {
344 wrData = patternData[j];
345 REG_WRITE(ah, addr, wrData);
346 rdData = REG_READ(ah, addr);
347 if (wrData != rdData) {
Joe Perches38002762010-12-02 19:12:36 -0800348 ath_err(common,
349 "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
350 addr, wrData, rdData);
Sujithf1dc5602008-10-29 10:16:30 +0530351 return false;
352 }
353 }
354 REG_WRITE(ah, regAddr[i], regHold[i]);
355 }
356 udelay(100);
Sujithcbe61d82009-02-09 13:27:12 +0530357
Sujithf1dc5602008-10-29 10:16:30 +0530358 return true;
359}
360
Luis R. Rodriguezb8b0f372009-08-03 12:24:43 -0700361static void ath9k_hw_init_config(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700362{
363 int i;
364
Sujith2660b812009-02-09 13:27:26 +0530365 ah->config.dma_beacon_response_time = 2;
366 ah->config.sw_beacon_response_time = 10;
367 ah->config.additional_swba_backoff = 0;
368 ah->config.ack_6mb = 0x0;
369 ah->config.cwm_ignore_extcca = 0;
370 ah->config.pcie_powersave_enable = 0;
Sujith2660b812009-02-09 13:27:26 +0530371 ah->config.pcie_clock_req = 0;
Sujith2660b812009-02-09 13:27:26 +0530372 ah->config.pcie_waen = 0;
373 ah->config.analog_shiftreg = 1;
Luis R. Rodriguez03c72512010-06-12 00:33:46 -0400374 ah->config.enable_ani = true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700375
376 for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
Sujith2660b812009-02-09 13:27:26 +0530377 ah->config.spurchans[i][0] = AR_NO_SPUR;
378 ah->config.spurchans[i][1] = AR_NO_SPUR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700379 }
380
Luis R. Rodriguez5ffaf8a2010-02-02 11:58:33 -0500381 if (ah->hw_version.devid != AR2427_DEVID_PCIE)
382 ah->config.ht_enable = 1;
383 else
384 ah->config.ht_enable = 0;
385
Luis R. Rodriguez6f481012011-01-20 17:47:39 -0800386 /* PAPRD needs some more work to be enabled */
387 ah->config.paprd_disable = 1;
388
Sujith0ce024c2009-12-14 14:57:00 +0530389 ah->config.rx_intr_mitigation = true;
Luis R. Rodriguez6a0ec302010-06-21 18:38:49 -0400390 ah->config.pcieSerDesWrite = true;
Luis R. Rodriguez61584252009-03-12 18:18:49 -0400391
392 /*
393 * We need this for PCI devices only (Cardbus, PCI, miniPCI)
394 * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
395 * This means we use it for all AR5416 devices, and the few
396 * minor PCI AR9280 devices out there.
397 *
398 * Serialization is required because these devices do not handle
399 * well the case of two concurrent reads/writes due to the latency
400 * involved. During one read/write another read/write can be issued
401 * on another CPU while the previous read/write may still be working
402 * on our hardware, if we hit this case the hardware poops in a loop.
403 * We prevent this by serializing reads and writes.
404 *
405 * This issue is not present on PCI-Express devices or pre-AR5416
406 * devices (legacy, 802.11abg).
407 */
408 if (num_possible_cpus() > 1)
David S. Miller2d6a5e92009-03-17 15:01:30 -0700409 ah->config.serialize_regmode = SER_REG_MODE_AUTO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700410}
411
Luis R. Rodriguez50aca252009-08-03 12:24:42 -0700412static void ath9k_hw_init_defaults(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700413{
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700414 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
415
416 regulatory->country_code = CTRY_DEFAULT;
417 regulatory->power_limit = MAX_RATE_POWER;
418 regulatory->tp_scale = ATH9K_TP_SCALE_MAX;
419
Sujithd535a422009-02-09 13:27:06 +0530420 ah->hw_version.magic = AR5416_MAGIC;
Sujithd535a422009-02-09 13:27:06 +0530421 ah->hw_version.subvendorid = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700422
Sujith2660b812009-02-09 13:27:26 +0530423 ah->atim_window = 0;
Felix Fietkau16f24112010-06-12 17:22:32 +0200424 ah->sta_id1_defaults =
425 AR_STA_ID1_CRPT_MIC_ENABLE |
426 AR_STA_ID1_MCAST_KSRCH;
Felix Fietkauf1717602011-03-19 13:55:41 +0100427 if (AR_SREV_9100(ah))
428 ah->sta_id1_defaults |= AR_STA_ID1_AR9100_BA_FIX;
Sujith2660b812009-02-09 13:27:26 +0530429 ah->enable_32kHz_clock = DONT_USE_32KHZ;
Felix Fietkau4357c6b2010-12-13 08:40:50 +0100430 ah->slottime = 20;
Sujith2660b812009-02-09 13:27:26 +0530431 ah->globaltxtimeout = (u32) -1;
Gabor Juhoscbdec972009-07-24 17:27:22 +0200432 ah->power_mode = ATH9K_PM_UNDEFINED;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700433}
434
Sujithcbe61d82009-02-09 13:27:12 +0530435static int ath9k_hw_init_macaddr(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700436{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700437 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530438 u32 sum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439 int i;
Sujithf1dc5602008-10-29 10:16:30 +0530440 u16 eeval;
Joe Perches07b2fa52010-11-20 18:38:53 -0800441 static const u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700442
Sujithf1dc5602008-10-29 10:16:30 +0530443 sum = 0;
444 for (i = 0; i < 3; i++) {
Luis R. Rodriguez49101672010-04-15 17:39:13 -0400445 eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
Sujithf1dc5602008-10-29 10:16:30 +0530446 sum += eeval;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700447 common->macaddr[2 * i] = eeval >> 8;
448 common->macaddr[2 * i + 1] = eeval & 0xff;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449 }
Sujithd8baa932009-03-30 15:28:25 +0530450 if (sum == 0 || sum == 0xffff * 3)
Sujithf1dc5602008-10-29 10:16:30 +0530451 return -EADDRNOTAVAIL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700452
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 return 0;
454}
455
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700456static int ath9k_hw_post_init(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700457{
Sujith Manoharan6cae9132011-01-04 13:16:37 +0530458 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700459 int ecode;
460
Sujith Manoharan6cae9132011-01-04 13:16:37 +0530461 if (common->bus_ops->ath_bus_type != ATH_USB) {
Sujith527d4852010-03-17 14:25:16 +0530462 if (!ath9k_hw_chip_test(ah))
463 return -ENODEV;
464 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700465
Luis R. Rodriguezebd5a142010-04-15 17:39:18 -0400466 if (!AR_SREV_9300_20_OR_LATER(ah)) {
467 ecode = ar9002_hw_rf_claim(ah);
468 if (ecode != 0)
469 return ecode;
470 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700471
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700472 ecode = ath9k_hw_eeprom_init(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700473 if (ecode != 0)
474 return ecode;
Sujith7d01b222009-03-13 08:55:55 +0530475
Joe Perches226afe62010-12-02 19:12:37 -0800476 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CONFIG,
477 "Eeprom VER: %d, REV: %d\n",
478 ah->eep_ops->get_eeprom_ver(ah),
479 ah->eep_ops->get_eeprom_rev(ah));
Sujith7d01b222009-03-13 08:55:55 +0530480
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400481 ecode = ath9k_hw_rf_alloc_ext_banks(ah);
482 if (ecode) {
Joe Perches38002762010-12-02 19:12:36 -0800483 ath_err(ath9k_hw_common(ah),
484 "Failed allocating banks for external radio\n");
Rajkumar Manoharan48a7c3d2010-11-08 20:40:53 +0530485 ath9k_hw_rf_free_ext_banks(ah);
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400486 return ecode;
Luis R. Rodriguez574d6b12009-10-19 02:33:37 -0400487 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700488
489 if (!AR_SREV_9100(ah)) {
490 ath9k_hw_ani_setup(ah);
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700491 ath9k_hw_ani_init(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700492 }
Sujithf1dc5602008-10-29 10:16:30 +0530493
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700494 return 0;
495}
496
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400497static void ath9k_hw_attach_ops(struct ath_hw *ah)
Luis R. Rodriguezee2bb462009-08-03 12:24:39 -0700498{
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400499 if (AR_SREV_9300_20_OR_LATER(ah))
500 ar9003_hw_attach_ops(ah);
501 else
502 ar9002_hw_attach_ops(ah);
Luis R. Rodriguezee2bb462009-08-03 12:24:39 -0700503}
504
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400505/* Called for all hardware families */
506static int __ath9k_hw_init(struct ath_hw *ah)
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700507{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700508 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700509 int r = 0;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700510
Luis R. Rodriguezbab1f622010-04-15 17:38:20 -0400511 if (ah->hw_version.devid == AR5416_AR9100_DEVID)
512 ah->hw_version.macVersion = AR_SREV_VERSION_9100;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700513
Senthil Balasubramanianac45c122010-12-22 21:14:20 +0530514 ath9k_hw_read_revisions(ah);
515
Senthil Balasubramanian0a8d7cb2010-12-22 19:17:18 +0530516 /*
517 * Read back AR_WA into a permanent copy and set bits 14 and 17.
518 * We need to do this to avoid RMW of this register. We cannot
519 * read the reg when chip is asleep.
520 */
521 ah->WARegVal = REG_READ(ah, AR_WA);
522 ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
523 AR_WA_ASPM_TIMER_BASED_DISABLE);
524
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700525 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
Joe Perches38002762010-12-02 19:12:36 -0800526 ath_err(common, "Couldn't reset chip\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700527 return -EIO;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700528 }
529
Luis R. Rodriguezbab1f622010-04-15 17:38:20 -0400530 ath9k_hw_init_defaults(ah);
531 ath9k_hw_init_config(ah);
532
Luis R. Rodriguez8525f282010-04-15 17:38:19 -0400533 ath9k_hw_attach_ops(ah);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400534
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700535 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
Joe Perches38002762010-12-02 19:12:36 -0800536 ath_err(common, "Couldn't wakeup chip\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700537 return -EIO;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700538 }
539
540 if (ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
541 if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
John W. Linville4c85ab12010-07-28 10:06:35 -0400542 ((AR_SREV_9160(ah) || AR_SREV_9280(ah)) &&
543 !ah->is_pciexpress)) {
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700544 ah->config.serialize_regmode =
545 SER_REG_MODE_ON;
546 } else {
547 ah->config.serialize_regmode =
548 SER_REG_MODE_OFF;
549 }
550 }
551
Joe Perches226afe62010-12-02 19:12:37 -0800552 ath_dbg(common, ATH_DBG_RESET, "serialize_regmode is %d\n",
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700553 ah->config.serialize_regmode);
554
Luis R. Rodriguezf4709fd2009-11-24 21:37:57 -0500555 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
556 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
557 else
558 ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
559
Felix Fietkau6da5a722010-12-12 00:51:12 +0100560 switch (ah->hw_version.macVersion) {
561 case AR_SREV_VERSION_5416_PCI:
562 case AR_SREV_VERSION_5416_PCIE:
563 case AR_SREV_VERSION_9160:
564 case AR_SREV_VERSION_9100:
565 case AR_SREV_VERSION_9280:
566 case AR_SREV_VERSION_9285:
567 case AR_SREV_VERSION_9287:
568 case AR_SREV_VERSION_9271:
569 case AR_SREV_VERSION_9300:
570 case AR_SREV_VERSION_9485:
571 break;
572 default:
Joe Perches38002762010-12-02 19:12:36 -0800573 ath_err(common,
574 "Mac Chip Rev 0x%02x.%x is not supported by this driver\n",
575 ah->hw_version.macVersion, ah->hw_version.macRev);
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700576 return -EOPNOTSUPP;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700577 }
578
Luis R. Rodriguez0df13da2010-04-15 17:38:59 -0400579 if (AR_SREV_9271(ah) || AR_SREV_9100(ah))
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400580 ah->is_pciexpress = false;
581
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700582 ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700583 ath9k_hw_init_cal_settings(ah);
584
585 ah->ani_function = ATH9K_ANI_ALL;
Felix Fietkau7a370812010-09-22 12:34:52 +0200586 if (AR_SREV_9280_20_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700587 ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -0400588 if (!AR_SREV_9300_20_OR_LATER(ah))
589 ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700590
591 ath9k_hw_init_mode_regs(ah);
592
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -0400593
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700594 if (ah->is_pciexpress)
Vivek Natarajan93b1b372009-09-17 09:24:58 +0530595 ath9k_hw_configpcipowersave(ah, 0, 0);
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700596 else
597 ath9k_hw_disablepcie(ah);
598
Luis R. Rodriguezd8f492b2010-04-15 17:39:04 -0400599 if (!AR_SREV_9300_20_OR_LATER(ah))
600 ar9002_hw_cck_chan14_spread(ah);
Sujith193cd452009-09-18 15:04:07 +0530601
Luis R. Rodriguezf637cfd2009-08-03 12:24:46 -0700602 r = ath9k_hw_post_init(ah);
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700603 if (r)
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700604 return r;
Luis R. Rodriguezaa4058a2009-08-03 12:24:45 -0700605
606 ath9k_hw_init_mode_gain_regs(ah);
Gabor Juhosa9a29ce2009-11-27 12:01:35 +0100607 r = ath9k_hw_fill_cap_info(ah);
608 if (r)
609 return r;
610
Luis R. Rodriguez4f3acf82009-08-03 12:24:36 -0700611 r = ath9k_hw_init_macaddr(ah);
612 if (r) {
Joe Perches38002762010-12-02 19:12:36 -0800613 ath_err(common, "Failed to initialize MAC address\n");
Luis R. Rodriguez95fafca2009-08-03 12:24:54 -0700614 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700615 }
616
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400617 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
Sujith2660b812009-02-09 13:27:26 +0530618 ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700619 else
Sujith2660b812009-02-09 13:27:26 +0530620 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700621
Luis R. Rodriguezaea702b2010-05-13 13:33:43 -0400622 ah->bb_watchdog_timeout_ms = 25;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700623
Luis R. Rodriguez211f5852009-10-06 21:19:07 -0400624 common->state = ATH_HW_INITIALIZED;
625
Luis R. Rodriguez4f3acf82009-08-03 12:24:36 -0700626 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700627}
628
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400629int ath9k_hw_init(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530630{
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400631 int ret;
632 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530633
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400634 /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
635 switch (ah->hw_version.devid) {
636 case AR5416_DEVID_PCI:
637 case AR5416_DEVID_PCIE:
638 case AR5416_AR9100_DEVID:
639 case AR9160_DEVID_PCI:
640 case AR9280_DEVID_PCI:
641 case AR9280_DEVID_PCIE:
642 case AR9285_DEVID_PCIE:
Senthil Balasubramaniandb3cc532010-04-15 17:38:18 -0400643 case AR9287_DEVID_PCI:
644 case AR9287_DEVID_PCIE:
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400645 case AR2427_DEVID_PCIE:
Senthil Balasubramaniandb3cc532010-04-15 17:38:18 -0400646 case AR9300_DEVID_PCIE:
Vasanthakumar Thiagarajan3050c912010-12-06 04:27:36 -0800647 case AR9300_DEVID_AR9485_PCIE:
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400648 break;
649 default:
650 if (common->bus_ops->ath_bus_type == ATH_USB)
651 break;
Joe Perches38002762010-12-02 19:12:36 -0800652 ath_err(common, "Hardware device ID 0x%04x not supported\n",
653 ah->hw_version.devid);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400654 return -EOPNOTSUPP;
655 }
Sujithf1dc5602008-10-29 10:16:30 +0530656
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400657 ret = __ath9k_hw_init(ah);
658 if (ret) {
Joe Perches38002762010-12-02 19:12:36 -0800659 ath_err(common,
660 "Unable to initialize hardware; initialization status: %d\n",
661 ret);
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400662 return ret;
663 }
Sujithf1dc5602008-10-29 10:16:30 +0530664
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400665 return 0;
Sujithf1dc5602008-10-29 10:16:30 +0530666}
Luis R. Rodriguezd70357d2010-04-15 17:38:06 -0400667EXPORT_SYMBOL(ath9k_hw_init);
Sujithf1dc5602008-10-29 10:16:30 +0530668
Sujithcbe61d82009-02-09 13:27:12 +0530669static void ath9k_hw_init_qos(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530670{
Sujith7d0d0df2010-04-16 11:53:57 +0530671 ENABLE_REGWRITE_BUFFER(ah);
672
Sujithf1dc5602008-10-29 10:16:30 +0530673 REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
674 REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
675
676 REG_WRITE(ah, AR_QOS_NO_ACK,
677 SM(2, AR_QOS_NO_ACK_TWO_BIT) |
678 SM(5, AR_QOS_NO_ACK_BIT_OFF) |
679 SM(0, AR_QOS_NO_ACK_BYTE_OFF));
680
681 REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
682 REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
683 REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
684 REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
685 REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
Sujith7d0d0df2010-04-16 11:53:57 +0530686
687 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530688}
689
Vivek Natarajanb1415812011-01-27 14:45:07 +0530690unsigned long ar9003_get_pll_sqsum_dvc(struct ath_hw *ah)
691{
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100692 REG_CLR_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
693 udelay(100);
694 REG_SET_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
695
696 while ((REG_READ(ah, PLL4) & PLL4_MEAS_DONE) == 0)
Vivek Natarajanb1415812011-01-27 14:45:07 +0530697 udelay(100);
Vivek Natarajanb1415812011-01-27 14:45:07 +0530698
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100699 return (REG_READ(ah, PLL3) & SQSUM_DVC_MASK) >> 3;
Vivek Natarajanb1415812011-01-27 14:45:07 +0530700}
701EXPORT_SYMBOL(ar9003_get_pll_sqsum_dvc);
702
Vivek Natarajan22983c32011-01-27 14:45:09 +0530703#define DPLL2_KD_VAL 0x3D
704#define DPLL2_KI_VAL 0x06
705#define DPLL3_PHASE_SHIFT_VAL 0x1
706
Sujithcbe61d82009-02-09 13:27:12 +0530707static void ath9k_hw_init_pll(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +0530708 struct ath9k_channel *chan)
709{
Vasanthakumar Thiagarajand09b17f2010-12-06 04:27:44 -0800710 u32 pll;
711
Vivek Natarajan22983c32011-01-27 14:45:09 +0530712 if (AR_SREV_9485(ah)) {
Vasanthakumar Thiagarajand09b17f2010-12-06 04:27:44 -0800713 REG_WRITE(ah, AR_RTC_PLL_CONTROL2, 0x886666);
Vivek Natarajan22983c32011-01-27 14:45:09 +0530714 REG_WRITE(ah, AR_CH0_DDR_DPLL2, 0x19e82f01);
715
716 REG_RMW_FIELD(ah, AR_CH0_DDR_DPLL3,
717 AR_CH0_DPLL3_PHASE_SHIFT, DPLL3_PHASE_SHIFT_VAL);
718
719 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
Vivek Natarajan75e03512011-03-10 11:05:42 +0530720 udelay(1000);
Vivek Natarajan22983c32011-01-27 14:45:09 +0530721
722 REG_WRITE(ah, AR_RTC_PLL_CONTROL2, 0x886666);
723
724 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
725 AR_CH0_DPLL2_KD, DPLL2_KD_VAL);
726 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
727 AR_CH0_DPLL2_KI, DPLL2_KI_VAL);
728
729 REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
730 AR_CH0_DPLL3_PHASE_SHIFT, DPLL3_PHASE_SHIFT_VAL);
731 REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x142c);
Vivek Natarajan75e03512011-03-10 11:05:42 +0530732 udelay(1000);
Vivek Natarajan22983c32011-01-27 14:45:09 +0530733 }
Vasanthakumar Thiagarajand09b17f2010-12-06 04:27:44 -0800734
735 pll = ath9k_hw_compute_pll_control(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +0530736
Gabor Juhosd03a66c2009-01-14 20:17:09 +0100737 REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
Sujithf1dc5602008-10-29 10:16:30 +0530738
Luis R. Rodriguezc75724d2009-10-19 02:33:34 -0400739 /* Switch the core clock for ar9271 to 117Mhz */
740 if (AR_SREV_9271(ah)) {
Sujith25e2ab12010-03-17 14:25:22 +0530741 udelay(500);
742 REG_WRITE(ah, 0x50040, 0x304);
Luis R. Rodriguezc75724d2009-10-19 02:33:34 -0400743 }
744
Sujithf1dc5602008-10-29 10:16:30 +0530745 udelay(RTC_PLL_SETTLE_DELAY);
746
747 REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
748}
749
Sujithcbe61d82009-02-09 13:27:12 +0530750static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
Colin McCabed97809d2008-12-01 13:38:55 -0800751 enum nl80211_iftype opmode)
Sujithf1dc5602008-10-29 10:16:30 +0530752{
Pavel Roskin152d5302010-03-31 18:05:37 -0400753 u32 imr_reg = AR_IMR_TXERR |
Sujithf1dc5602008-10-29 10:16:30 +0530754 AR_IMR_TXURN |
755 AR_IMR_RXERR |
756 AR_IMR_RXORN |
757 AR_IMR_BCNMISC;
758
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400759 if (AR_SREV_9300_20_OR_LATER(ah)) {
760 imr_reg |= AR_IMR_RXOK_HP;
761 if (ah->config.rx_intr_mitigation)
762 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
763 else
764 imr_reg |= AR_IMR_RXOK_LP;
Sujithf1dc5602008-10-29 10:16:30 +0530765
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400766 } else {
767 if (ah->config.rx_intr_mitigation)
768 imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
769 else
770 imr_reg |= AR_IMR_RXOK;
771 }
772
773 if (ah->config.tx_intr_mitigation)
774 imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
775 else
776 imr_reg |= AR_IMR_TXOK;
Sujithf1dc5602008-10-29 10:16:30 +0530777
Colin McCabed97809d2008-12-01 13:38:55 -0800778 if (opmode == NL80211_IFTYPE_AP)
Pavel Roskin152d5302010-03-31 18:05:37 -0400779 imr_reg |= AR_IMR_MIB;
Sujithf1dc5602008-10-29 10:16:30 +0530780
Sujith7d0d0df2010-04-16 11:53:57 +0530781 ENABLE_REGWRITE_BUFFER(ah);
782
Pavel Roskin152d5302010-03-31 18:05:37 -0400783 REG_WRITE(ah, AR_IMR, imr_reg);
Pavel Roskin74bad5c2010-02-23 18:15:27 -0500784 ah->imrs2_reg |= AR_IMR_S2_GTT;
785 REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
Sujithf1dc5602008-10-29 10:16:30 +0530786
787 if (!AR_SREV_9100(ah)) {
788 REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
789 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_DEFAULT);
790 REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
791 }
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400792
Sujith7d0d0df2010-04-16 11:53:57 +0530793 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +0530794
Vasanthakumar Thiagarajan66860242010-04-15 17:39:07 -0400795 if (AR_SREV_9300_20_OR_LATER(ah)) {
796 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
797 REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
798 REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
799 REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
800 }
Sujithf1dc5602008-10-29 10:16:30 +0530801}
802
Felix Fietkau0005baf2010-01-15 02:33:40 +0100803static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
Sujithf1dc5602008-10-29 10:16:30 +0530804{
Felix Fietkau0005baf2010-01-15 02:33:40 +0100805 u32 val = ath9k_hw_mac_to_clks(ah, us);
806 val = min(val, (u32) 0xFFFF);
807 REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
Sujithf1dc5602008-10-29 10:16:30 +0530808}
809
Felix Fietkau0005baf2010-01-15 02:33:40 +0100810static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
Sujithf1dc5602008-10-29 10:16:30 +0530811{
Felix Fietkau0005baf2010-01-15 02:33:40 +0100812 u32 val = ath9k_hw_mac_to_clks(ah, us);
813 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
814 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
815}
816
817static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
818{
819 u32 val = ath9k_hw_mac_to_clks(ah, us);
820 val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
821 REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
Sujithf1dc5602008-10-29 10:16:30 +0530822}
823
Sujithcbe61d82009-02-09 13:27:12 +0530824static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
Sujithf1dc5602008-10-29 10:16:30 +0530825{
Sujithf1dc5602008-10-29 10:16:30 +0530826 if (tu > 0xFFFF) {
Joe Perches226afe62010-12-02 19:12:37 -0800827 ath_dbg(ath9k_hw_common(ah), ATH_DBG_XMIT,
828 "bad global tx timeout %u\n", tu);
Sujith2660b812009-02-09 13:27:26 +0530829 ah->globaltxtimeout = (u32) -1;
Sujithf1dc5602008-10-29 10:16:30 +0530830 return false;
831 } else {
832 REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
Sujith2660b812009-02-09 13:27:26 +0530833 ah->globaltxtimeout = tu;
Sujithf1dc5602008-10-29 10:16:30 +0530834 return true;
835 }
836}
837
Felix Fietkau0005baf2010-01-15 02:33:40 +0100838void ath9k_hw_init_global_settings(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530839{
Felix Fietkau0005baf2010-01-15 02:33:40 +0100840 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
841 int acktimeout;
Felix Fietkaue239d852010-01-15 02:34:58 +0100842 int slottime;
Felix Fietkau0005baf2010-01-15 02:33:40 +0100843 int sifstime;
844
Joe Perches226afe62010-12-02 19:12:37 -0800845 ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET, "ah->misc_mode 0x%x\n",
846 ah->misc_mode);
Sujithf1dc5602008-10-29 10:16:30 +0530847
Sujith2660b812009-02-09 13:27:26 +0530848 if (ah->misc_mode != 0)
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100849 REG_SET_BIT(ah, AR_PCU_MISC, ah->misc_mode);
Felix Fietkau0005baf2010-01-15 02:33:40 +0100850
851 if (conf->channel && conf->channel->band == IEEE80211_BAND_5GHZ)
852 sifstime = 16;
853 else
854 sifstime = 10;
855
Felix Fietkaue239d852010-01-15 02:34:58 +0100856 /* As defined by IEEE 802.11-2007 17.3.8.6 */
857 slottime = ah->slottime + 3 * ah->coverage_class;
858 acktimeout = slottime + sifstime;
Felix Fietkau42c45682010-02-11 18:07:19 +0100859
860 /*
861 * Workaround for early ACK timeouts, add an offset to match the
862 * initval's 64us ack timeout value.
863 * This was initially only meant to work around an issue with delayed
864 * BA frames in some implementations, but it has been found to fix ACK
865 * timeout issues in other cases as well.
866 */
867 if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ)
868 acktimeout += 64 - sifstime - ah->slottime;
869
Felix Fietkaucaabf2b2010-12-13 08:40:51 +0100870 ath9k_hw_setslottime(ah, ah->slottime);
Felix Fietkau0005baf2010-01-15 02:33:40 +0100871 ath9k_hw_set_ack_timeout(ah, acktimeout);
872 ath9k_hw_set_cts_timeout(ah, acktimeout);
Sujith2660b812009-02-09 13:27:26 +0530873 if (ah->globaltxtimeout != (u32) -1)
874 ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
Sujithf1dc5602008-10-29 10:16:30 +0530875}
Felix Fietkau0005baf2010-01-15 02:33:40 +0100876EXPORT_SYMBOL(ath9k_hw_init_global_settings);
Sujithf1dc5602008-10-29 10:16:30 +0530877
Sujith285f2dd2010-01-08 10:36:07 +0530878void ath9k_hw_deinit(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700879{
Luis R. Rodriguez211f5852009-10-06 21:19:07 -0400880 struct ath_common *common = ath9k_hw_common(ah);
881
Sujith736b3a22010-03-17 14:25:24 +0530882 if (common->state < ATH_HW_INITIALIZED)
Luis R. Rodriguez211f5852009-10-06 21:19:07 -0400883 goto free_hw;
884
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700885 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
Luis R. Rodriguez211f5852009-10-06 21:19:07 -0400886
887free_hw:
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400888 ath9k_hw_rf_free_ext_banks(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700889}
Sujith285f2dd2010-01-08 10:36:07 +0530890EXPORT_SYMBOL(ath9k_hw_deinit);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700891
Sujithf1dc5602008-10-29 10:16:30 +0530892/*******/
893/* INI */
894/*******/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700895
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -0400896u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
Bob Copeland3a702e42009-03-30 22:30:29 -0400897{
898 u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
899
900 if (IS_CHAN_B(chan))
901 ctl |= CTL_11B;
902 else if (IS_CHAN_G(chan))
903 ctl |= CTL_11G;
904 else
905 ctl |= CTL_11A;
906
907 return ctl;
908}
909
Sujithf1dc5602008-10-29 10:16:30 +0530910/****************************************/
911/* Reset and Channel Switching Routines */
912/****************************************/
913
Sujithcbe61d82009-02-09 13:27:12 +0530914static inline void ath9k_hw_set_dma(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +0530915{
Felix Fietkau57b32222010-04-15 17:39:22 -0400916 struct ath_common *common = ath9k_hw_common(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530917
Sujith7d0d0df2010-04-16 11:53:57 +0530918 ENABLE_REGWRITE_BUFFER(ah);
919
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400920 /*
921 * set AHB_MODE not to do cacheline prefetches
922 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100923 if (!AR_SREV_9300_20_OR_LATER(ah))
924 REG_SET_BIT(ah, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
Sujithf1dc5602008-10-29 10:16:30 +0530925
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400926 /*
927 * let mac dma reads be in 128 byte chunks
928 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100929 REG_RMW(ah, AR_TXCFG, AR_TXCFG_DMASZ_128B, AR_TXCFG_DMASZ_MASK);
Sujithf1dc5602008-10-29 10:16:30 +0530930
Sujith7d0d0df2010-04-16 11:53:57 +0530931 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +0530932
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400933 /*
934 * Restore TX Trigger Level to its pre-reset value.
935 * The initial value depends on whether aggregation is enabled, and is
936 * adjusted whenever underruns are detected.
937 */
Felix Fietkau57b32222010-04-15 17:39:22 -0400938 if (!AR_SREV_9300_20_OR_LATER(ah))
939 REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
Sujithf1dc5602008-10-29 10:16:30 +0530940
Sujith7d0d0df2010-04-16 11:53:57 +0530941 ENABLE_REGWRITE_BUFFER(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530942
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400943 /*
944 * let mac dma writes be in 128 byte chunks
945 */
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100946 REG_RMW(ah, AR_RXCFG, AR_RXCFG_DMASZ_128B, AR_RXCFG_DMASZ_MASK);
Sujithf1dc5602008-10-29 10:16:30 +0530947
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400948 /*
949 * Setup receive FIFO threshold to hold off TX activities
950 */
Sujithf1dc5602008-10-29 10:16:30 +0530951 REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
952
Felix Fietkau57b32222010-04-15 17:39:22 -0400953 if (AR_SREV_9300_20_OR_LATER(ah)) {
954 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
955 REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
956
957 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
958 ah->caps.rx_status_len);
959 }
960
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400961 /*
962 * reduce the number of usable entries in PCU TXBUF to avoid
963 * wrap around issues.
964 */
Sujithf1dc5602008-10-29 10:16:30 +0530965 if (AR_SREV_9285(ah)) {
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400966 /* For AR9285 the number of Fifos are reduced to half.
967 * So set the usable tx buf size also to half to
968 * avoid data/delimiter underruns
969 */
Sujithf1dc5602008-10-29 10:16:30 +0530970 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
971 AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -0400972 } else if (!AR_SREV_9271(ah)) {
Sujithf1dc5602008-10-29 10:16:30 +0530973 REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
974 AR_PCU_TXBUF_CTRL_USABLE_SIZE);
975 }
Vasanthakumar Thiagarajan744d4022010-04-15 17:39:27 -0400976
Sujith7d0d0df2010-04-16 11:53:57 +0530977 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +0530978
Vasanthakumar Thiagarajan744d4022010-04-15 17:39:27 -0400979 if (AR_SREV_9300_20_OR_LATER(ah))
980 ath9k_hw_reset_txstatus_ring(ah);
Sujithf1dc5602008-10-29 10:16:30 +0530981}
982
Sujithcbe61d82009-02-09 13:27:12 +0530983static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
Sujithf1dc5602008-10-29 10:16:30 +0530984{
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100985 u32 mask = AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC;
986 u32 set = AR_STA_ID1_KSRCH_MODE;
Sujithf1dc5602008-10-29 10:16:30 +0530987
Sujithf1dc5602008-10-29 10:16:30 +0530988 switch (opmode) {
Colin McCabed97809d2008-12-01 13:38:55 -0800989 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -0400990 case NL80211_IFTYPE_MESH_POINT:
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100991 set |= AR_STA_ID1_ADHOC;
Sujithf1dc5602008-10-29 10:16:30 +0530992 REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
993 break;
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100994 case NL80211_IFTYPE_AP:
995 set |= AR_STA_ID1_STA_AP;
996 /* fall through */
Colin McCabed97809d2008-12-01 13:38:55 -0800997 case NL80211_IFTYPE_STATION:
Felix Fietkauca7a4de2011-03-23 20:57:26 +0100998 REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
Sujithf1dc5602008-10-29 10:16:30 +0530999 break;
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301000 default:
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001001 if (!ah->is_monitoring)
1002 set = 0;
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301003 break;
Sujithf1dc5602008-10-29 10:16:30 +05301004 }
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001005 REG_RMW(ah, AR_STA_ID1, set, mask);
Sujithf1dc5602008-10-29 10:16:30 +05301006}
1007
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001008void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
1009 u32 *coef_mantissa, u32 *coef_exponent)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001010{
1011 u32 coef_exp, coef_man;
1012
1013 for (coef_exp = 31; coef_exp > 0; coef_exp--)
1014 if ((coef_scaled >> coef_exp) & 0x1)
1015 break;
1016
1017 coef_exp = 14 - (coef_exp - COEF_SCALE_S);
1018
1019 coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
1020
1021 *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
1022 *coef_exponent = coef_exp - 16;
1023}
1024
Sujithcbe61d82009-02-09 13:27:12 +05301025static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
Sujithf1dc5602008-10-29 10:16:30 +05301026{
1027 u32 rst_flags;
1028 u32 tmpReg;
1029
Sujith70768492009-02-16 13:23:12 +05301030 if (AR_SREV_9100(ah)) {
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001031 REG_RMW_FIELD(ah, AR_RTC_DERIVED_CLK,
1032 AR_RTC_DERIVED_CLK_PERIOD, 1);
Sujith70768492009-02-16 13:23:12 +05301033 (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
1034 }
1035
Sujith7d0d0df2010-04-16 11:53:57 +05301036 ENABLE_REGWRITE_BUFFER(ah);
1037
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001038 if (AR_SREV_9300_20_OR_LATER(ah)) {
1039 REG_WRITE(ah, AR_WA, ah->WARegVal);
1040 udelay(10);
1041 }
1042
Sujithf1dc5602008-10-29 10:16:30 +05301043 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1044 AR_RTC_FORCE_WAKE_ON_INT);
1045
1046 if (AR_SREV_9100(ah)) {
1047 rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
1048 AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
1049 } else {
1050 tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
1051 if (tmpReg &
1052 (AR_INTR_SYNC_LOCAL_TIMEOUT |
1053 AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001054 u32 val;
Sujithf1dc5602008-10-29 10:16:30 +05301055 REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001056
1057 val = AR_RC_HOSTIF;
1058 if (!AR_SREV_9300_20_OR_LATER(ah))
1059 val |= AR_RC_AHB;
1060 REG_WRITE(ah, AR_RC, val);
1061
1062 } else if (!AR_SREV_9300_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05301063 REG_WRITE(ah, AR_RC, AR_RC_AHB);
Sujithf1dc5602008-10-29 10:16:30 +05301064
1065 rst_flags = AR_RTC_RC_MAC_WARM;
1066 if (type == ATH9K_RESET_COLD)
1067 rst_flags |= AR_RTC_RC_MAC_COLD;
1068 }
1069
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001070 REG_WRITE(ah, AR_RTC_RC, rst_flags);
Sujith7d0d0df2010-04-16 11:53:57 +05301071
1072 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301073
Sujithf1dc5602008-10-29 10:16:30 +05301074 udelay(50);
1075
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001076 REG_WRITE(ah, AR_RTC_RC, 0);
Sujith0caa7b12009-02-16 13:23:20 +05301077 if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
Joe Perches226afe62010-12-02 19:12:37 -08001078 ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
1079 "RTC stuck in MAC reset\n");
Sujithf1dc5602008-10-29 10:16:30 +05301080 return false;
1081 }
1082
1083 if (!AR_SREV_9100(ah))
1084 REG_WRITE(ah, AR_RC, 0);
1085
Sujithf1dc5602008-10-29 10:16:30 +05301086 if (AR_SREV_9100(ah))
1087 udelay(50);
1088
1089 return true;
1090}
1091
Sujithcbe61d82009-02-09 13:27:12 +05301092static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05301093{
Sujith7d0d0df2010-04-16 11:53:57 +05301094 ENABLE_REGWRITE_BUFFER(ah);
1095
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001096 if (AR_SREV_9300_20_OR_LATER(ah)) {
1097 REG_WRITE(ah, AR_WA, ah->WARegVal);
1098 udelay(10);
1099 }
1100
Sujithf1dc5602008-10-29 10:16:30 +05301101 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
1102 AR_RTC_FORCE_WAKE_ON_INT);
1103
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001104 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301105 REG_WRITE(ah, AR_RC, AR_RC_AHB);
1106
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001107 REG_WRITE(ah, AR_RTC_RESET, 0);
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301108
Sujith7d0d0df2010-04-16 11:53:57 +05301109 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301110
Senthil Balasubramanian84e21692010-04-15 17:38:30 -04001111 if (!AR_SREV_9300_20_OR_LATER(ah))
1112 udelay(2);
1113
1114 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan1c29ce62009-08-31 17:48:36 +05301115 REG_WRITE(ah, AR_RC, 0);
1116
Gabor Juhosd03a66c2009-01-14 20:17:09 +01001117 REG_WRITE(ah, AR_RTC_RESET, 1);
Sujithf1dc5602008-10-29 10:16:30 +05301118
1119 if (!ath9k_hw_wait(ah,
1120 AR_RTC_STATUS,
1121 AR_RTC_STATUS_M,
Sujith0caa7b12009-02-16 13:23:20 +05301122 AR_RTC_STATUS_ON,
1123 AH_WAIT_TIMEOUT)) {
Joe Perches226afe62010-12-02 19:12:37 -08001124 ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
1125 "RTC not waking up\n");
Sujithf1dc5602008-10-29 10:16:30 +05301126 return false;
1127 }
1128
Sujithf1dc5602008-10-29 10:16:30 +05301129 return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
1130}
1131
Sujithcbe61d82009-02-09 13:27:12 +05301132static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
Sujithf1dc5602008-10-29 10:16:30 +05301133{
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001134 if (AR_SREV_9300_20_OR_LATER(ah)) {
1135 REG_WRITE(ah, AR_WA, ah->WARegVal);
1136 udelay(10);
1137 }
1138
Sujithf1dc5602008-10-29 10:16:30 +05301139 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1140 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
1141
1142 switch (type) {
1143 case ATH9K_RESET_POWER_ON:
1144 return ath9k_hw_set_reset_power_on(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301145 case ATH9K_RESET_WARM:
1146 case ATH9K_RESET_COLD:
1147 return ath9k_hw_set_reset(ah, type);
Sujithf1dc5602008-10-29 10:16:30 +05301148 default:
1149 return false;
1150 }
1151}
1152
Sujithcbe61d82009-02-09 13:27:12 +05301153static bool ath9k_hw_chip_reset(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05301154 struct ath9k_channel *chan)
1155{
Vivek Natarajan42abfbe2009-09-17 09:27:59 +05301156 if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)) {
Senthil Balasubramanian8bd1d072009-02-12 13:57:03 +05301157 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON))
1158 return false;
1159 } else if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
Sujithf1dc5602008-10-29 10:16:30 +05301160 return false;
1161
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001162 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Sujithf1dc5602008-10-29 10:16:30 +05301163 return false;
1164
Sujith2660b812009-02-09 13:27:26 +05301165 ah->chip_fullsleep = false;
Sujithf1dc5602008-10-29 10:16:30 +05301166 ath9k_hw_init_pll(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301167 ath9k_hw_set_rfmode(ah, chan);
1168
1169 return true;
1170}
1171
Sujithcbe61d82009-02-09 13:27:12 +05301172static bool ath9k_hw_channel_change(struct ath_hw *ah,
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07001173 struct ath9k_channel *chan)
Sujithf1dc5602008-10-29 10:16:30 +05301174{
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001175 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001176 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -08001177 struct ieee80211_channel *channel = chan->chan;
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001178 u32 qnum;
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001179 int r;
Sujithf1dc5602008-10-29 10:16:30 +05301180
1181 for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
1182 if (ath9k_hw_numtxpending(ah, qnum)) {
Joe Perches226afe62010-12-02 19:12:37 -08001183 ath_dbg(common, ATH_DBG_QUEUE,
1184 "Transmit frames pending on queue %d\n", qnum);
Sujithf1dc5602008-10-29 10:16:30 +05301185 return false;
1186 }
1187 }
1188
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001189 if (!ath9k_hw_rfbus_req(ah)) {
Joe Perches38002762010-12-02 19:12:36 -08001190 ath_err(common, "Could not kill baseband RX\n");
Sujithf1dc5602008-10-29 10:16:30 +05301191 return false;
1192 }
1193
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001194 ath9k_hw_set_channel_regs(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301195
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001196 r = ath9k_hw_rf_set_freq(ah, chan);
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001197 if (r) {
Joe Perches38002762010-12-02 19:12:36 -08001198 ath_err(common, "Failed to set channel\n");
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001199 return false;
Sujithf1dc5602008-10-29 10:16:30 +05301200 }
Felix Fietkaudfdac8a2010-10-08 22:13:51 +02001201 ath9k_hw_set_clockrate(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301202
Vasanthakumar Thiagarajan8fbff4b2009-05-08 17:54:51 -07001203 ah->eep_ops->set_txpower(ah, chan,
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001204 ath9k_regd_get_ctl(regulatory, chan),
Sujithf74df6f2009-02-09 13:27:24 +05301205 channel->max_antenna_gain * 2,
1206 channel->max_power * 2,
1207 min((u32) MAX_RATE_POWER,
Felix Fietkaude40f312010-10-20 03:08:53 +02001208 (u32) regulatory->power_limit), false);
Sujithf1dc5602008-10-29 10:16:30 +05301209
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001210 ath9k_hw_rfbus_done(ah);
Sujithf1dc5602008-10-29 10:16:30 +05301211
1212 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1213 ath9k_hw_set_delta_slope(ah, chan);
1214
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001215 ath9k_hw_spur_mitigate_freq(ah, chan);
Sujithf1dc5602008-10-29 10:16:30 +05301216
Sujithf1dc5602008-10-29 10:16:30 +05301217 return true;
1218}
1219
Felix Fietkau691680b2011-03-19 13:55:38 +01001220static void ath9k_hw_apply_gpio_override(struct ath_hw *ah)
1221{
1222 u32 gpio_mask = ah->gpio_mask;
1223 int i;
1224
1225 for (i = 0; gpio_mask; i++, gpio_mask >>= 1) {
1226 if (!(gpio_mask & 1))
1227 continue;
1228
1229 ath9k_hw_cfg_output(ah, i, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1230 ath9k_hw_set_gpio(ah, i, !!(ah->gpio_val & BIT(i)));
1231 }
1232}
1233
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001234bool ath9k_hw_check_alive(struct ath_hw *ah)
Johannes Berg3b319aa2009-06-13 14:50:26 +05301235{
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001236 int count = 50;
1237 u32 reg;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301238
Felix Fietkaue17f83e2010-09-22 12:34:53 +02001239 if (AR_SREV_9285_12_OR_LATER(ah))
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001240 return true;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301241
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001242 do {
1243 reg = REG_READ(ah, AR_OBS_BUS_1);
1244
1245 if ((reg & 0x7E7FFFEF) == 0x00702400)
1246 continue;
1247
1248 switch (reg & 0x7E000B00) {
1249 case 0x1E000000:
1250 case 0x52000B00:
1251 case 0x18000B00:
1252 continue;
1253 default:
1254 return true;
1255 }
1256 } while (count-- > 0);
1257
1258 return false;
Johannes Berg3b319aa2009-06-13 14:50:26 +05301259}
Felix Fietkauc9c99e52010-04-19 19:57:29 +02001260EXPORT_SYMBOL(ath9k_hw_check_alive);
Johannes Berg3b319aa2009-06-13 14:50:26 +05301261
Sujithcbe61d82009-02-09 13:27:12 +05301262int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
Felix Fietkau20bd2a02010-07-31 00:12:00 +02001263 struct ath9k_hw_cal_data *caldata, bool bChannelChange)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001264{
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001265 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001266 u32 saveLedState;
Sujith2660b812009-02-09 13:27:26 +05301267 struct ath9k_channel *curchan = ah->curchan;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001268 u32 saveDefAntenna;
1269 u32 macStaId1;
Sujith46fe7822009-09-17 09:25:25 +05301270 u64 tsf = 0;
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001271 int i, r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001272
Luis R. Rodriguez43c27612009-09-13 21:07:07 -07001273 ah->txchainmask = common->tx_chainmask;
1274 ah->rxchainmask = common->rx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001275
Sujith Manoharan6d501922011-01-04 13:43:39 +05301276 if ((common->bus_ops->ath_bus_type != ATH_USB) && !ah->chip_fullsleep) {
Vasanthakumar Thiagarajan9b9cc612010-04-15 17:39:41 -04001277 ath9k_hw_abortpcurecv(ah);
Felix Fietkau9cc2f3e2010-07-11 12:48:42 +02001278 if (!ath9k_hw_stopdmarecv(ah)) {
Joe Perches226afe62010-12-02 19:12:37 -08001279 ath_dbg(common, ATH_DBG_XMIT,
Vasanthakumar Thiagarajan9b9cc612010-04-15 17:39:41 -04001280 "Failed to stop receive dma\n");
Felix Fietkau9cc2f3e2010-07-11 12:48:42 +02001281 bChannelChange = false;
1282 }
Vasanthakumar Thiagarajan9b9cc612010-04-15 17:39:41 -04001283 }
1284
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001285 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001286 return -EIO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001287
Felix Fietkaud9891c72010-09-29 17:15:27 +02001288 if (curchan && !ah->chip_fullsleep)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001289 ath9k_hw_getnf(ah, curchan);
1290
Felix Fietkau20bd2a02010-07-31 00:12:00 +02001291 ah->caldata = caldata;
1292 if (caldata &&
1293 (chan->channel != caldata->channel ||
1294 (chan->channelFlags & ~CHANNEL_CW_INT) !=
1295 (caldata->channelFlags & ~CHANNEL_CW_INT))) {
1296 /* Operating channel changed, reset channel calibration data */
1297 memset(caldata, 0, sizeof(*caldata));
1298 ath9k_init_nfcal_hist_buffer(ah, chan);
1299 }
1300
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001301 if (bChannelChange &&
Sujith2660b812009-02-09 13:27:26 +05301302 (ah->chip_fullsleep != true) &&
1303 (ah->curchan != NULL) &&
1304 (chan->channel != ah->curchan->channel) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001305 ((chan->channelFlags & CHANNEL_ALL) ==
Sujith2660b812009-02-09 13:27:26 +05301306 (ah->curchan->channelFlags & CHANNEL_ALL)) &&
Rajkumar Manoharan58d7e0f2010-09-08 15:57:12 +05301307 (!AR_SREV_9280(ah) || AR_DEVID_7010(ah))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001308
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07001309 if (ath9k_hw_channel_change(ah, chan)) {
Sujith2660b812009-02-09 13:27:26 +05301310 ath9k_hw_loadnf(ah, ah->curchan);
Felix Fietkau00c86592010-07-30 21:02:09 +02001311 ath9k_hw_start_nfcal(ah, true);
Rajkumar Manoharanc2ba3342010-09-03 16:00:00 +05301312 if (AR_SREV_9271(ah))
1313 ar9002_hw_load_ani_reg(ah, chan);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001314 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001315 }
1316 }
1317
1318 saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
1319 if (saveDefAntenna == 0)
1320 saveDefAntenna = 1;
1321
1322 macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
1323
Sujith46fe7822009-09-17 09:25:25 +05301324 /* For chips on which RTC reset is done, save TSF before it gets cleared */
Felix Fietkauf860d522010-06-30 02:07:48 +02001325 if (AR_SREV_9100(ah) ||
1326 (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)))
Sujith46fe7822009-09-17 09:25:25 +05301327 tsf = ath9k_hw_gettsf64(ah);
1328
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001329 saveLedState = REG_READ(ah, AR_CFG_LED) &
1330 (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
1331 AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
1332
1333 ath9k_hw_mark_phy_inactive(ah);
1334
Vasanthakumar Thiagarajan45ef6a02010-12-15 07:30:53 -08001335 ah->paprd_table_write_done = false;
1336
Sujith05020d22010-03-17 14:25:23 +05301337 /* Only required on the first reset */
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001338 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1339 REG_WRITE(ah,
1340 AR9271_RESET_POWER_DOWN_CONTROL,
1341 AR9271_RADIO_RF_RST);
1342 udelay(50);
1343 }
1344
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001345 if (!ath9k_hw_chip_reset(ah, chan)) {
Joe Perches38002762010-12-02 19:12:36 -08001346 ath_err(common, "Chip reset failed\n");
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001347 return -EINVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001348 }
1349
Sujith05020d22010-03-17 14:25:23 +05301350 /* Only required on the first reset */
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001351 if (AR_SREV_9271(ah) && ah->htc_reset_init) {
1352 ah->htc_reset_init = false;
1353 REG_WRITE(ah,
1354 AR9271_RESET_POWER_DOWN_CONTROL,
1355 AR9271_GATE_MAC_CTL);
1356 udelay(50);
1357 }
1358
Sujith46fe7822009-09-17 09:25:25 +05301359 /* Restore TSF */
Felix Fietkauf860d522010-06-30 02:07:48 +02001360 if (tsf)
Sujith46fe7822009-09-17 09:25:25 +05301361 ath9k_hw_settsf64(ah, tsf);
1362
Felix Fietkau7a370812010-09-22 12:34:52 +02001363 if (AR_SREV_9280_20_OR_LATER(ah))
Vasanthakumar Thiagarajan369391d2009-01-21 19:24:13 +05301364 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001365
Sujithe9141f72010-06-01 15:14:10 +05301366 if (!AR_SREV_9300_20_OR_LATER(ah))
1367 ar9002_hw_enable_async_fifo(ah);
1368
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07001369 r = ath9k_hw_process_ini(ah, chan);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001370 if (r)
1371 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001372
Felix Fietkauf860d522010-06-30 02:07:48 +02001373 /*
1374 * Some AR91xx SoC devices frequently fail to accept TSF writes
1375 * right after the chip reset. When that happens, write a new
1376 * value after the initvals have been applied, with an offset
1377 * based on measured time difference
1378 */
1379 if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
1380 tsf += 1500;
1381 ath9k_hw_settsf64(ah, tsf);
1382 }
1383
Jouni Malinen0ced0e12009-01-08 13:32:13 +02001384 /* Setup MFP options for CCMP */
1385 if (AR_SREV_9280_20_OR_LATER(ah)) {
1386 /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
1387 * frames when constructing CCMP AAD. */
1388 REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
1389 0xc7ff);
1390 ah->sw_mgmt_crypto = false;
1391 } else if (AR_SREV_9160_10_OR_LATER(ah)) {
1392 /* Disable hardware crypto for management frames */
1393 REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
1394 AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
1395 REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
1396 AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
1397 ah->sw_mgmt_crypto = true;
1398 } else
1399 ah->sw_mgmt_crypto = true;
1400
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001401 if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
1402 ath9k_hw_set_delta_slope(ah, chan);
1403
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001404 ath9k_hw_spur_mitigate_freq(ah, chan);
Sujithd6509152009-03-13 08:56:05 +05301405 ah->eep_ops->set_board_values(ah, chan);
Luis R. Rodrigueza7765822009-10-19 02:33:45 -04001406
Sujith7d0d0df2010-04-16 11:53:57 +05301407 ENABLE_REGWRITE_BUFFER(ah);
1408
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001409 REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
1410 REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001411 | macStaId1
1412 | AR_STA_ID1_RTS_USE_DEF
Sujith2660b812009-02-09 13:27:26 +05301413 | (ah->config.
Sujith60b67f52008-08-07 10:52:38 +05301414 ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
Sujith2660b812009-02-09 13:27:26 +05301415 | ah->sta_id1_defaults);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -07001416 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001417 REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
Luis R. Rodriguez3453ad82009-09-10 08:57:00 -07001418 ath9k_hw_write_associd(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001419 REG_WRITE(ah, AR_ISR, ~0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001420 REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
1421
Sujith7d0d0df2010-04-16 11:53:57 +05301422 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301423
Sujith Manoharan00e00032011-01-26 21:59:05 +05301424 ath9k_hw_set_operating_mode(ah, ah->opmode);
1425
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001426 r = ath9k_hw_rf_set_freq(ah, chan);
Luis R. Rodriguez0a3b7ba2009-10-19 02:33:40 -04001427 if (r)
1428 return r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001429
Felix Fietkaudfdac8a2010-10-08 22:13:51 +02001430 ath9k_hw_set_clockrate(ah);
1431
Sujith7d0d0df2010-04-16 11:53:57 +05301432 ENABLE_REGWRITE_BUFFER(ah);
1433
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001434 for (i = 0; i < AR_NUM_DCU; i++)
1435 REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
1436
Sujith7d0d0df2010-04-16 11:53:57 +05301437 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301438
Sujith2660b812009-02-09 13:27:26 +05301439 ah->intr_txqs = 0;
1440 for (i = 0; i < ah->caps.total_queues; i++)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001441 ath9k_hw_resettxqueue(ah, i);
1442
Sujith2660b812009-02-09 13:27:26 +05301443 ath9k_hw_init_interrupt_masks(ah, ah->opmode);
Luis R. Rodrigueze36b27a2010-06-12 00:33:45 -04001444 ath9k_hw_ani_cache_ini_regs(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001445 ath9k_hw_init_qos(ah);
1446
Sujith2660b812009-02-09 13:27:26 +05301447 if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
Felix Fietkau55821322010-12-17 00:57:01 +01001448 ath9k_hw_cfg_gpio_input(ah, ah->rfkill_gpio);
Johannes Berg3b319aa2009-06-13 14:50:26 +05301449
Felix Fietkau0005baf2010-01-15 02:33:40 +01001450 ath9k_hw_init_global_settings(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001451
Luis R. Rodriguez6c94fdc2010-04-15 17:39:24 -04001452 if (!AR_SREV_9300_20_OR_LATER(ah)) {
Sujithe9141f72010-06-01 15:14:10 +05301453 ar9002_hw_update_async_fifo(ah);
Luis R. Rodriguez6c94fdc2010-04-15 17:39:24 -04001454 ar9002_hw_enable_wep_aggregation(ah);
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05301455 }
1456
Felix Fietkauca7a4de2011-03-23 20:57:26 +01001457 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001458
1459 ath9k_hw_set_dma(ah);
1460
1461 REG_WRITE(ah, AR_OBS, 8);
1462
Sujith0ce024c2009-12-14 14:57:00 +05301463 if (ah->config.rx_intr_mitigation) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001464 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
1465 REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
1466 }
1467
Vasanthakumar Thiagarajan7f62a132010-04-15 17:39:19 -04001468 if (ah->config.tx_intr_mitigation) {
1469 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
1470 REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
1471 }
1472
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001473 ath9k_hw_init_bb(ah, chan);
1474
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001475 if (!ath9k_hw_init_cal(ah, chan))
Joe Perches6badaaf2009-06-28 09:26:32 -07001476 return -EIO;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001477
Sujith7d0d0df2010-04-16 11:53:57 +05301478 ENABLE_REGWRITE_BUFFER(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001479
Luis R. Rodriguez8fe65362010-04-15 17:38:14 -04001480 ath9k_hw_restore_chainmask(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001481 REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
1482
Sujith7d0d0df2010-04-16 11:53:57 +05301483 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301484
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001485 /*
1486 * For big endian systems turn on swapping for descriptors
1487 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001488 if (AR_SREV_9100(ah)) {
1489 u32 mask;
1490 mask = REG_READ(ah, AR_CFG);
1491 if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
Joe Perches226afe62010-12-02 19:12:37 -08001492 ath_dbg(common, ATH_DBG_RESET,
Sujith04bd4632008-11-28 22:18:05 +05301493 "CFG Byte Swap Set 0x%x\n", mask);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001494 } else {
1495 mask =
1496 INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
1497 REG_WRITE(ah, AR_CFG, mask);
Joe Perches226afe62010-12-02 19:12:37 -08001498 ath_dbg(common, ATH_DBG_RESET,
Sujith04bd4632008-11-28 22:18:05 +05301499 "Setting CFG 0x%x\n", REG_READ(ah, AR_CFG));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001500 }
1501 } else {
Sujithcbba8cd2010-06-02 15:53:31 +05301502 if (common->bus_ops->ath_bus_type == ATH_USB) {
1503 /* Configure AR9271 target WLAN */
1504 if (AR_SREV_9271(ah))
1505 REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
1506 else
1507 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
1508 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001509#ifdef __BIG_ENDIAN
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001510 else
1511 REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001512#endif
1513 }
1514
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001515 if (ah->btcoex_hw.enabled)
Vasanthakumar Thiagarajan42cc41e2009-08-26 21:08:45 +05301516 ath9k_hw_btcoex_enable(ah);
1517
Felix Fietkau00c86592010-07-30 21:02:09 +02001518 if (AR_SREV_9300_20_OR_LATER(ah))
Luis R. Rodriguezaea702b2010-05-13 13:33:43 -04001519 ar9003_hw_bb_watchdog_config(ah);
Vasanthakumar Thiagarajand8903a52010-04-15 17:39:25 -04001520
Felix Fietkau691680b2011-03-19 13:55:38 +01001521 ath9k_hw_apply_gpio_override(ah);
1522
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001523 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001524}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04001525EXPORT_SYMBOL(ath9k_hw_reset);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001526
Sujithf1dc5602008-10-29 10:16:30 +05301527/******************************/
1528/* Power Management (Chipset) */
1529/******************************/
1530
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001531/*
1532 * Notify Power Mgt is disabled in self-generated frames.
1533 * If requested, force chip to sleep.
1534 */
Sujithcbe61d82009-02-09 13:27:12 +05301535static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
Sujithf1dc5602008-10-29 10:16:30 +05301536{
1537 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1538 if (setChip) {
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001539 /*
1540 * Clear the RTC force wake bit to allow the
1541 * mac to go to sleep.
1542 */
Sujithf1dc5602008-10-29 10:16:30 +05301543 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1544 AR_RTC_FORCE_WAKE_EN);
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001545 if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05301546 REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
1547
Luis R. Rodriguez42d5bc32010-04-15 17:38:12 -04001548 /* Shutdown chip. Active low */
Sujith14b3af32010-03-17 14:25:18 +05301549 if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah))
Sujith4921be82009-09-18 15:04:27 +05301550 REG_CLR_BIT(ah, (AR_RTC_RESET),
1551 AR_RTC_RESET_EN);
Sujithf1dc5602008-10-29 10:16:30 +05301552 }
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001553
1554 /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
1555 if (AR_SREV_9300_20_OR_LATER(ah))
1556 REG_WRITE(ah, AR_WA,
1557 ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001558}
1559
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04001560/*
1561 * Notify Power Management is enabled in self-generating
1562 * frames. If request, set power mode of chip to
1563 * auto/normal. Duration in units of 128us (1/8 TU).
1564 */
Sujithcbe61d82009-02-09 13:27:12 +05301565static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001566{
Sujithf1dc5602008-10-29 10:16:30 +05301567 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1568 if (setChip) {
Sujith2660b812009-02-09 13:27:26 +05301569 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001570
Sujithf1dc5602008-10-29 10:16:30 +05301571 if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04001572 /* Set WakeOnInterrupt bit; clear ForceWake bit */
Sujithf1dc5602008-10-29 10:16:30 +05301573 REG_WRITE(ah, AR_RTC_FORCE_WAKE,
1574 AR_RTC_FORCE_WAKE_ON_INT);
1575 } else {
Luis R. Rodriguezbbd79af2010-04-15 17:38:16 -04001576 /*
1577 * Clear the RTC force wake bit to allow the
1578 * mac to go to sleep.
1579 */
Sujithf1dc5602008-10-29 10:16:30 +05301580 REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
1581 AR_RTC_FORCE_WAKE_EN);
1582 }
1583 }
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001584
1585 /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
1586 if (AR_SREV_9300_20_OR_LATER(ah))
1587 REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
Sujithf1dc5602008-10-29 10:16:30 +05301588}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001589
Sujithcbe61d82009-02-09 13:27:12 +05301590static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
Sujithf1dc5602008-10-29 10:16:30 +05301591{
1592 u32 val;
1593 int i;
1594
Luis R. Rodriguez9a658d22010-06-21 18:38:47 -04001595 /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
1596 if (AR_SREV_9300_20_OR_LATER(ah)) {
1597 REG_WRITE(ah, AR_WA, ah->WARegVal);
1598 udelay(10);
1599 }
1600
Sujithf1dc5602008-10-29 10:16:30 +05301601 if (setChip) {
1602 if ((REG_READ(ah, AR_RTC_STATUS) &
1603 AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
1604 if (ath9k_hw_set_reset_reg(ah,
1605 ATH9K_RESET_POWER_ON) != true) {
1606 return false;
1607 }
Luis R. Rodrigueze0412282010-04-15 17:38:15 -04001608 if (!AR_SREV_9300_20_OR_LATER(ah))
1609 ath9k_hw_init_pll(ah, NULL);
Sujithf1dc5602008-10-29 10:16:30 +05301610 }
1611 if (AR_SREV_9100(ah))
1612 REG_SET_BIT(ah, AR_RTC_RESET,
1613 AR_RTC_RESET_EN);
1614
1615 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1616 AR_RTC_FORCE_WAKE_EN);
1617 udelay(50);
1618
1619 for (i = POWER_UP_TIME / 50; i > 0; i--) {
1620 val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
1621 if (val == AR_RTC_STATUS_ON)
1622 break;
1623 udelay(50);
1624 REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
1625 AR_RTC_FORCE_WAKE_EN);
1626 }
1627 if (i == 0) {
Joe Perches38002762010-12-02 19:12:36 -08001628 ath_err(ath9k_hw_common(ah),
1629 "Failed to wakeup in %uus\n",
1630 POWER_UP_TIME / 20);
Sujithf1dc5602008-10-29 10:16:30 +05301631 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001632 }
1633 }
1634
Sujithf1dc5602008-10-29 10:16:30 +05301635 REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
1636
1637 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001638}
1639
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07001640bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
Sujithf1dc5602008-10-29 10:16:30 +05301641{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001642 struct ath_common *common = ath9k_hw_common(ah);
Sujithcbe61d82009-02-09 13:27:12 +05301643 int status = true, setChip = true;
Sujithf1dc5602008-10-29 10:16:30 +05301644 static const char *modes[] = {
1645 "AWAKE",
1646 "FULL-SLEEP",
1647 "NETWORK SLEEP",
1648 "UNDEFINED"
1649 };
Sujithf1dc5602008-10-29 10:16:30 +05301650
Gabor Juhoscbdec972009-07-24 17:27:22 +02001651 if (ah->power_mode == mode)
1652 return status;
1653
Joe Perches226afe62010-12-02 19:12:37 -08001654 ath_dbg(common, ATH_DBG_RESET, "%s -> %s\n",
1655 modes[ah->power_mode], modes[mode]);
Sujithf1dc5602008-10-29 10:16:30 +05301656
1657 switch (mode) {
1658 case ATH9K_PM_AWAKE:
1659 status = ath9k_hw_set_power_awake(ah, setChip);
1660 break;
1661 case ATH9K_PM_FULL_SLEEP:
1662 ath9k_set_power_sleep(ah, setChip);
Sujith2660b812009-02-09 13:27:26 +05301663 ah->chip_fullsleep = true;
Sujithf1dc5602008-10-29 10:16:30 +05301664 break;
1665 case ATH9K_PM_NETWORK_SLEEP:
1666 ath9k_set_power_network_sleep(ah, setChip);
1667 break;
1668 default:
Joe Perches38002762010-12-02 19:12:36 -08001669 ath_err(common, "Unknown power mode %u\n", mode);
Sujithf1dc5602008-10-29 10:16:30 +05301670 return false;
1671 }
Sujith2660b812009-02-09 13:27:26 +05301672 ah->power_mode = mode;
Sujithf1dc5602008-10-29 10:16:30 +05301673
Luis R. Rodriguez69f4aab2010-12-07 15:13:23 -08001674 /*
1675 * XXX: If this warning never comes up after a while then
1676 * simply keep the ATH_DBG_WARN_ON_ONCE() but make
1677 * ath9k_hw_setpower() return type void.
1678 */
Sujith Manoharan97dcec52010-12-20 08:02:42 +05301679
1680 if (!(ah->ah_flags & AH_UNPLUGGED))
1681 ATH_DBG_WARN_ON_ONCE(!status);
Luis R. Rodriguez69f4aab2010-12-07 15:13:23 -08001682
Sujithf1dc5602008-10-29 10:16:30 +05301683 return status;
1684}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04001685EXPORT_SYMBOL(ath9k_hw_setpower);
Sujithf1dc5602008-10-29 10:16:30 +05301686
Sujithf1dc5602008-10-29 10:16:30 +05301687/*******************/
1688/* Beacon Handling */
1689/*******************/
1690
Sujithcbe61d82009-02-09 13:27:12 +05301691void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001692{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001693 int flags = 0;
1694
Sujith7d0d0df2010-04-16 11:53:57 +05301695 ENABLE_REGWRITE_BUFFER(ah);
1696
Sujith2660b812009-02-09 13:27:26 +05301697 switch (ah->opmode) {
Colin McCabed97809d2008-12-01 13:38:55 -08001698 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -04001699 case NL80211_IFTYPE_MESH_POINT:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001700 REG_SET_BIT(ah, AR_TXCFG,
1701 AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
Felix Fietkaudd347f22011-03-22 21:54:17 +01001702 REG_WRITE(ah, AR_NEXT_NDP_TIMER, next_beacon +
1703 TU_TO_USEC(ah->atim_window ? ah->atim_window : 1));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001704 flags |= AR_NDP_TIMER_EN;
Colin McCabed97809d2008-12-01 13:38:55 -08001705 case NL80211_IFTYPE_AP:
Felix Fietkaudd347f22011-03-22 21:54:17 +01001706 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, next_beacon);
1707 REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, next_beacon -
1708 TU_TO_USEC(ah->config.dma_beacon_response_time));
1709 REG_WRITE(ah, AR_NEXT_SWBA, next_beacon -
1710 TU_TO_USEC(ah->config.sw_beacon_response_time));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001711 flags |=
1712 AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
1713 break;
Colin McCabed97809d2008-12-01 13:38:55 -08001714 default:
Joe Perches226afe62010-12-02 19:12:37 -08001715 ath_dbg(ath9k_hw_common(ah), ATH_DBG_BEACON,
1716 "%s: unsupported opmode: %d\n",
1717 __func__, ah->opmode);
Colin McCabed97809d2008-12-01 13:38:55 -08001718 return;
1719 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001720 }
1721
Felix Fietkaudd347f22011-03-22 21:54:17 +01001722 REG_WRITE(ah, AR_BEACON_PERIOD, beacon_period);
1723 REG_WRITE(ah, AR_DMA_BEACON_PERIOD, beacon_period);
1724 REG_WRITE(ah, AR_SWBA_PERIOD, beacon_period);
1725 REG_WRITE(ah, AR_NDP_PERIOD, beacon_period);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001726
Sujith7d0d0df2010-04-16 11:53:57 +05301727 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301728
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001729 REG_SET_BIT(ah, AR_TIMER_MODE, flags);
1730}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04001731EXPORT_SYMBOL(ath9k_hw_beaconinit);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001732
Sujithcbe61d82009-02-09 13:27:12 +05301733void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05301734 const struct ath9k_beacon_state *bs)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001735{
1736 u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
Sujith2660b812009-02-09 13:27:26 +05301737 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001738 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001739
Sujith7d0d0df2010-04-16 11:53:57 +05301740 ENABLE_REGWRITE_BUFFER(ah);
1741
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001742 REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
1743
1744 REG_WRITE(ah, AR_BEACON_PERIOD,
1745 TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1746 REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
1747 TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
1748
Sujith7d0d0df2010-04-16 11:53:57 +05301749 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301750
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001751 REG_RMW_FIELD(ah, AR_RSSI_THR,
1752 AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
1753
1754 beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
1755
1756 if (bs->bs_sleepduration > beaconintval)
1757 beaconintval = bs->bs_sleepduration;
1758
1759 dtimperiod = bs->bs_dtimperiod;
1760 if (bs->bs_sleepduration > dtimperiod)
1761 dtimperiod = bs->bs_sleepduration;
1762
1763 if (beaconintval == dtimperiod)
1764 nextTbtt = bs->bs_nextdtim;
1765 else
1766 nextTbtt = bs->bs_nexttbtt;
1767
Joe Perches226afe62010-12-02 19:12:37 -08001768 ath_dbg(common, ATH_DBG_BEACON, "next DTIM %d\n", bs->bs_nextdtim);
1769 ath_dbg(common, ATH_DBG_BEACON, "next beacon %d\n", nextTbtt);
1770 ath_dbg(common, ATH_DBG_BEACON, "beacon period %d\n", beaconintval);
1771 ath_dbg(common, ATH_DBG_BEACON, "DTIM period %d\n", dtimperiod);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001772
Sujith7d0d0df2010-04-16 11:53:57 +05301773 ENABLE_REGWRITE_BUFFER(ah);
1774
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001775 REG_WRITE(ah, AR_NEXT_DTIM,
1776 TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
1777 REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
1778
1779 REG_WRITE(ah, AR_SLEEP1,
1780 SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
1781 | AR_SLEEP1_ASSUME_DTIM);
1782
Sujith60b67f52008-08-07 10:52:38 +05301783 if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001784 beacontimeout = (BEACON_TIMEOUT_VAL << 3);
1785 else
1786 beacontimeout = MIN_BEACON_TIMEOUT_VAL;
1787
1788 REG_WRITE(ah, AR_SLEEP2,
1789 SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
1790
1791 REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
1792 REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
1793
Sujith7d0d0df2010-04-16 11:53:57 +05301794 REGWRITE_BUFFER_FLUSH(ah);
Sujith7d0d0df2010-04-16 11:53:57 +05301795
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001796 REG_SET_BIT(ah, AR_TIMER_MODE,
1797 AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
1798 AR_DTIM_TIMER_EN);
1799
Sujith4af9cf42009-02-12 10:06:47 +05301800 /* TSF Out of Range Threshold */
1801 REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001802}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04001803EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001804
Sujithf1dc5602008-10-29 10:16:30 +05301805/*******************/
1806/* HW Capabilities */
1807/*******************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001808
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01001809int ath9k_hw_fill_cap_info(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001810{
Sujith2660b812009-02-09 13:27:26 +05301811 struct ath9k_hw_capabilities *pCap = &ah->caps;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001812 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001813 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001814 struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001815
Sujithf1dc5602008-10-29 10:16:30 +05301816 u16 capField = 0, eeval;
Vasanthakumar Thiagarajan47c80de2010-12-06 04:27:43 -08001817 u8 ant_div_ctl1, tx_chainmask, rx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001818
Sujithf74df6f2009-02-09 13:27:24 +05301819 eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001820 regulatory->current_rd = eeval;
Sujithf1dc5602008-10-29 10:16:30 +05301821
Sujithf74df6f2009-02-09 13:27:24 +05301822 eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_1);
Felix Fietkaue17f83e2010-09-22 12:34:53 +02001823 if (AR_SREV_9285_12_OR_LATER(ah))
Sujithfec0de12009-02-12 10:06:43 +05301824 eeval |= AR9285_RDEXT_DEFAULT;
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001825 regulatory->current_rd_ext = eeval;
Sujithf1dc5602008-10-29 10:16:30 +05301826
Sujithf74df6f2009-02-09 13:27:24 +05301827 capField = ah->eep_ops->get_eeprom(ah, EEP_OP_CAP);
Sujithf1dc5602008-10-29 10:16:30 +05301828
Sujith2660b812009-02-09 13:27:26 +05301829 if (ah->opmode != NL80211_IFTYPE_AP &&
Sujithd535a422009-02-09 13:27:06 +05301830 ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001831 if (regulatory->current_rd == 0x64 ||
1832 regulatory->current_rd == 0x65)
1833 regulatory->current_rd += 5;
1834 else if (regulatory->current_rd == 0x41)
1835 regulatory->current_rd = 0x43;
Joe Perches226afe62010-12-02 19:12:37 -08001836 ath_dbg(common, ATH_DBG_REGULATORY,
1837 "regdomain mapped to 0x%x\n", regulatory->current_rd);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001838 }
Sujithdc2222a2008-08-14 13:26:55 +05301839
Sujithf74df6f2009-02-09 13:27:24 +05301840 eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01001841 if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
Joe Perches38002762010-12-02 19:12:36 -08001842 ath_err(common,
1843 "no band has been marked as supported in EEPROM\n");
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01001844 return -EINVAL;
1845 }
1846
Felix Fietkaud4659912010-10-14 16:02:39 +02001847 if (eeval & AR5416_OPFLAGS_11A)
1848 pCap->hw_caps |= ATH9K_HW_CAP_5GHZ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001849
Felix Fietkaud4659912010-10-14 16:02:39 +02001850 if (eeval & AR5416_OPFLAGS_11G)
1851 pCap->hw_caps |= ATH9K_HW_CAP_2GHZ;
Sujithf1dc5602008-10-29 10:16:30 +05301852
Sujithf74df6f2009-02-09 13:27:24 +05301853 pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001854 /*
1855 * For AR9271 we will temporarilly uses the rx chainmax as read from
1856 * the EEPROM.
1857 */
Sujith8147f5d2009-02-20 15:13:23 +05301858 if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001859 !(eeval & AR5416_OPFLAGS_11A) &&
1860 !(AR_SREV_9271(ah)))
1861 /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
Sujith8147f5d2009-02-20 15:13:23 +05301862 pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
Felix Fietkau598cdd52011-03-19 13:55:42 +01001863 else if (AR_SREV_9100(ah))
1864 pCap->rx_chainmask = 0x7;
Sujith8147f5d2009-02-20 15:13:23 +05301865 else
Luis R. Rodriguezd7e7d222009-08-03 23:14:12 -04001866 /* Use rx_chainmask from EEPROM. */
Sujith8147f5d2009-02-20 15:13:23 +05301867 pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
Sujithf1dc5602008-10-29 10:16:30 +05301868
Felix Fietkau7a370812010-09-22 12:34:52 +02001869 ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
Sujithf1dc5602008-10-29 10:16:30 +05301870
Felix Fietkau02d2ebb2010-11-22 15:39:39 +01001871 /* enable key search for every frame in an aggregate */
1872 if (AR_SREV_9300_20_OR_LATER(ah))
1873 ah->misc_mode |= AR_PCU_ALWAYS_PERFORM_KEYSEARCH;
1874
Sujithf1dc5602008-10-29 10:16:30 +05301875 pCap->low_2ghz_chan = 2312;
1876 pCap->high_2ghz_chan = 2732;
1877
1878 pCap->low_5ghz_chan = 4920;
1879 pCap->high_5ghz_chan = 6100;
1880
Bruno Randolfce2220d2010-09-17 11:36:25 +09001881 common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
1882
Sujith2660b812009-02-09 13:27:26 +05301883 if (ah->config.ht_enable)
Sujithf1dc5602008-10-29 10:16:30 +05301884 pCap->hw_caps |= ATH9K_HW_CAP_HT;
1885 else
1886 pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
1887
Sujithf1dc5602008-10-29 10:16:30 +05301888 if (capField & AR_EEPROM_EEPCAP_MAXQCU)
1889 pCap->total_queues =
1890 MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
1891 else
1892 pCap->total_queues = ATH9K_NUM_TX_QUEUES;
1893
1894 if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
1895 pCap->keycache_size =
1896 1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
1897 else
1898 pCap->keycache_size = AR_KEYTABLE_SIZE;
1899
Luis R. Rodriguezf4709fd2009-11-24 21:37:57 -05001900 if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
1901 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD >> 1;
1902 else
1903 pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
Sujithf1dc5602008-10-29 10:16:30 +05301904
Sujith5b5fa352010-03-17 14:25:15 +05301905 if (AR_SREV_9271(ah))
1906 pCap->num_gpio_pins = AR9271_NUM_GPIO;
Sujith88c1f4f2010-06-30 14:46:31 +05301907 else if (AR_DEVID_7010(ah))
1908 pCap->num_gpio_pins = AR7010_NUM_GPIO;
Felix Fietkaue17f83e2010-09-22 12:34:53 +02001909 else if (AR_SREV_9285_12_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05301910 pCap->num_gpio_pins = AR9285_NUM_GPIO;
Felix Fietkau7a370812010-09-22 12:34:52 +02001911 else if (AR_SREV_9280_20_OR_LATER(ah))
Sujithf1dc5602008-10-29 10:16:30 +05301912 pCap->num_gpio_pins = AR928X_NUM_GPIO;
1913 else
1914 pCap->num_gpio_pins = AR_NUM_GPIO;
1915
Sujithf1dc5602008-10-29 10:16:30 +05301916 if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
1917 pCap->hw_caps |= ATH9K_HW_CAP_CST;
1918 pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
1919 } else {
1920 pCap->rts_aggr_limit = (8 * 1024);
1921 }
1922
1923 pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
1924
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301925#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith2660b812009-02-09 13:27:26 +05301926 ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
1927 if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
1928 ah->rfkill_gpio =
1929 MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
1930 ah->rfkill_polarity =
1931 MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
Sujithf1dc5602008-10-29 10:16:30 +05301932
1933 pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
1934 }
1935#endif
Vasanthakumar Thiagarajand5d11542010-05-17 18:57:56 -07001936 if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
Vivek Natarajanbde748a2010-04-05 14:48:05 +05301937 pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
1938 else
1939 pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
Sujithf1dc5602008-10-29 10:16:30 +05301940
Senthil Balasubramaniane7594072008-12-08 19:43:48 +05301941 if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
Sujithf1dc5602008-10-29 10:16:30 +05301942 pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
1943 else
1944 pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
1945
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07001946 if (regulatory->current_rd_ext & (1 << REG_EXT_JAPAN_MIDBAND)) {
Sujithf1dc5602008-10-29 10:16:30 +05301947 pCap->reg_cap =
1948 AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
1949 AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
1950 AR_EEPROM_EEREGCAP_EN_KK_U2 |
1951 AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
1952 } else {
1953 pCap->reg_cap =
1954 AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
1955 AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
1956 }
1957
Senthil Balasubramanianebb90cf2009-09-18 15:07:33 +05301958 /* Advertise midband for AR5416 with FCC midband set in eeprom */
1959 if (regulatory->current_rd_ext & (1 << REG_EXT_FCC_MIDBAND) &&
1960 AR_SREV_5416(ah))
1961 pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
Sujithf1dc5602008-10-29 10:16:30 +05301962
Vasanthakumar Thiagarajan8f5dcb12010-11-26 06:10:06 -08001963 if (AR_SREV_9280_20_OR_LATER(ah) && common->btcoex_enabled) {
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001964 btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO;
1965 btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO;
Vasanthakumar Thiagarajan22f25d02009-08-26 21:08:47 +05301966
Vasanthakumar Thiagarajan8c8f9ba2009-09-09 15:25:52 +05301967 if (AR_SREV_9285(ah)) {
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001968 btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
1969 btcoex_hw->btpriority_gpio = ATH_BTPRIORITY_GPIO;
Vasanthakumar Thiagarajan8c8f9ba2009-09-09 15:25:52 +05301970 } else {
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001971 btcoex_hw->scheme = ATH_BTCOEX_CFG_2WIRE;
Vasanthakumar Thiagarajan8c8f9ba2009-09-09 15:25:52 +05301972 }
Vasanthakumar Thiagarajan22f25d02009-08-26 21:08:47 +05301973 } else {
Luis R. Rodriguez766ec4a2009-09-09 14:52:02 -07001974 btcoex_hw->scheme = ATH_BTCOEX_CFG_NONE;
Vasanthakumar Thiagarajanc97c92d2009-01-02 15:35:46 +05301975 }
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01001976
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04001977 if (AR_SREV_9300_20_OR_LATER(ah)) {
Vasanthakumar Thiagarajan784ad502010-12-06 04:27:40 -08001978 pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_FASTCLOCK;
1979 if (!AR_SREV_9485(ah))
1980 pCap->hw_caps |= ATH9K_HW_CAP_LDPC;
1981
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04001982 pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
1983 pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
1984 pCap->rx_status_len = sizeof(struct ar9003_rxs);
Vasanthakumar Thiagarajan162c3be2010-04-15 17:38:41 -04001985 pCap->tx_desc_len = sizeof(struct ar9003_txc);
Vasanthakumar Thiagarajan5088c2f2010-04-15 17:39:34 -04001986 pCap->txs_len = sizeof(struct ar9003_txs);
Luis R. Rodriguez6f481012011-01-20 17:47:39 -08001987 if (!ah->config.paprd_disable &&
1988 ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
Felix Fietkau49352502010-06-12 00:33:59 -04001989 pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
Vasanthakumar Thiagarajan162c3be2010-04-15 17:38:41 -04001990 } else {
1991 pCap->tx_desc_len = sizeof(struct ath_desc);
Felix Fietkau6b42e8d2010-04-26 15:04:35 -04001992 if (AR_SREV_9280_20(ah) &&
1993 ((ah->eep_ops->get_eeprom(ah, EEP_MINOR_REV) <=
1994 AR5416_EEP_MINOR_VER_16) ||
1995 ah->eep_ops->get_eeprom(ah, EEP_FSTCLK_5G)))
1996 pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
Vasanthakumar Thiagarajanceb26442010-04-15 17:38:25 -04001997 }
Vasanthakumar Thiagarajan1adf02f2010-04-15 17:38:24 -04001998
Vasanthakumar Thiagarajan6c84ce02010-04-15 17:39:16 -04001999 if (AR_SREV_9300_20_OR_LATER(ah))
2000 pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
2001
Senthil Balasubramanian6ee63f52010-11-10 05:03:16 -08002002 if (AR_SREV_9300_20_OR_LATER(ah))
2003 ah->ent_mode = REG_READ(ah, AR_ENT_OTP);
2004
Felix Fietkaua42acef2010-09-22 12:34:54 +02002005 if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
Vasanthakumar Thiagarajan6473d242010-05-13 18:42:38 -07002006 pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
2007
Vasanthakumar Thiagarajan754dc532010-09-02 01:34:41 -07002008 if (AR_SREV_9285(ah))
2009 if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
2010 ant_div_ctl1 =
2011 ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
2012 if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1))
2013 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
2014 }
Mohammed Shafi Shajakhanea066d52010-11-23 20:42:27 +05302015 if (AR_SREV_9300_20_OR_LATER(ah)) {
2016 if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
2017 pCap->hw_caps |= ATH9K_HW_CAP_APM;
2018 }
2019
2020
Vasanthakumar Thiagarajan754dc532010-09-02 01:34:41 -07002021
Vasanthakumar Thiagarajan8060e162010-12-06 04:27:42 -08002022 if (AR_SREV_9485_10(ah)) {
2023 pCap->pcie_lcr_extsync_en = true;
2024 pCap->pcie_lcr_offset = 0x80;
2025 }
2026
Vasanthakumar Thiagarajan47c80de2010-12-06 04:27:43 -08002027 tx_chainmask = pCap->tx_chainmask;
2028 rx_chainmask = pCap->rx_chainmask;
2029 while (tx_chainmask || rx_chainmask) {
2030 if (tx_chainmask & BIT(0))
2031 pCap->max_txchains++;
2032 if (rx_chainmask & BIT(0))
2033 pCap->max_rxchains++;
2034
2035 tx_chainmask >>= 1;
2036 rx_chainmask >>= 1;
2037 }
2038
Gabor Juhosa9a29ce2009-11-27 12:01:35 +01002039 return 0;
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002040}
2041
Sujithf1dc5602008-10-29 10:16:30 +05302042/****************************/
2043/* GPIO / RFKILL / Antennae */
2044/****************************/
2045
Sujithcbe61d82009-02-09 13:27:12 +05302046static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
Sujithf1dc5602008-10-29 10:16:30 +05302047 u32 gpio, u32 type)
2048{
2049 int addr;
2050 u32 gpio_shift, tmp;
2051
2052 if (gpio > 11)
2053 addr = AR_GPIO_OUTPUT_MUX3;
2054 else if (gpio > 5)
2055 addr = AR_GPIO_OUTPUT_MUX2;
2056 else
2057 addr = AR_GPIO_OUTPUT_MUX1;
2058
2059 gpio_shift = (gpio % 6) * 5;
2060
2061 if (AR_SREV_9280_20_OR_LATER(ah)
2062 || (addr != AR_GPIO_OUTPUT_MUX1)) {
2063 REG_RMW(ah, addr, (type << gpio_shift),
2064 (0x1f << gpio_shift));
2065 } else {
2066 tmp = REG_READ(ah, addr);
2067 tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
2068 tmp &= ~(0x1f << gpio_shift);
2069 tmp |= (type << gpio_shift);
2070 REG_WRITE(ah, addr, tmp);
2071 }
2072}
2073
Sujithcbe61d82009-02-09 13:27:12 +05302074void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
Sujithf1dc5602008-10-29 10:16:30 +05302075{
2076 u32 gpio_shift;
2077
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -07002078 BUG_ON(gpio >= ah->caps.num_gpio_pins);
Sujithf1dc5602008-10-29 10:16:30 +05302079
Sujith88c1f4f2010-06-30 14:46:31 +05302080 if (AR_DEVID_7010(ah)) {
2081 gpio_shift = gpio;
2082 REG_RMW(ah, AR7010_GPIO_OE,
2083 (AR7010_GPIO_OE_AS_INPUT << gpio_shift),
2084 (AR7010_GPIO_OE_MASK << gpio_shift));
2085 return;
2086 }
Sujithf1dc5602008-10-29 10:16:30 +05302087
Sujith88c1f4f2010-06-30 14:46:31 +05302088 gpio_shift = gpio << 1;
Sujithf1dc5602008-10-29 10:16:30 +05302089 REG_RMW(ah,
2090 AR_GPIO_OE_OUT,
2091 (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
2092 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2093}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002094EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
Sujithf1dc5602008-10-29 10:16:30 +05302095
Sujithcbe61d82009-02-09 13:27:12 +05302096u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
Sujithf1dc5602008-10-29 10:16:30 +05302097{
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302098#define MS_REG_READ(x, y) \
2099 (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
2100
Sujith2660b812009-02-09 13:27:26 +05302101 if (gpio >= ah->caps.num_gpio_pins)
Sujithf1dc5602008-10-29 10:16:30 +05302102 return 0xffffffff;
2103
Sujith88c1f4f2010-06-30 14:46:31 +05302104 if (AR_DEVID_7010(ah)) {
2105 u32 val;
2106 val = REG_READ(ah, AR7010_GPIO_IN);
2107 return (MS(val, AR7010_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) == 0;
2108 } else if (AR_SREV_9300_20_OR_LATER(ah))
Vasanthakumar Thiagarajan93069902010-11-30 23:24:09 -08002109 return (MS(REG_READ(ah, AR_GPIO_IN), AR9300_GPIO_IN_VAL) &
2110 AR_GPIO_BIT(gpio)) != 0;
Felix Fietkau783dfca2010-04-15 17:38:11 -04002111 else if (AR_SREV_9271(ah))
Sujith5b5fa352010-03-17 14:25:15 +05302112 return MS_REG_READ(AR9271, gpio) != 0;
Felix Fietkaua42acef2010-09-22 12:34:54 +02002113 else if (AR_SREV_9287_11_OR_LATER(ah))
Vivek Natarajanac88b6e2009-07-23 10:59:57 +05302114 return MS_REG_READ(AR9287, gpio) != 0;
Felix Fietkaue17f83e2010-09-22 12:34:53 +02002115 else if (AR_SREV_9285_12_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302116 return MS_REG_READ(AR9285, gpio) != 0;
Felix Fietkau7a370812010-09-22 12:34:52 +02002117 else if (AR_SREV_9280_20_OR_LATER(ah))
Senthil Balasubramaniancb33c412008-12-24 18:03:58 +05302118 return MS_REG_READ(AR928X, gpio) != 0;
2119 else
2120 return MS_REG_READ(AR, gpio) != 0;
Sujithf1dc5602008-10-29 10:16:30 +05302121}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002122EXPORT_SYMBOL(ath9k_hw_gpio_get);
Sujithf1dc5602008-10-29 10:16:30 +05302123
Sujithcbe61d82009-02-09 13:27:12 +05302124void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
Sujithf1dc5602008-10-29 10:16:30 +05302125 u32 ah_signal_type)
2126{
2127 u32 gpio_shift;
2128
Sujith88c1f4f2010-06-30 14:46:31 +05302129 if (AR_DEVID_7010(ah)) {
2130 gpio_shift = gpio;
2131 REG_RMW(ah, AR7010_GPIO_OE,
2132 (AR7010_GPIO_OE_AS_OUTPUT << gpio_shift),
2133 (AR7010_GPIO_OE_MASK << gpio_shift));
2134 return;
2135 }
2136
Sujithf1dc5602008-10-29 10:16:30 +05302137 ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
Sujithf1dc5602008-10-29 10:16:30 +05302138 gpio_shift = 2 * gpio;
Sujithf1dc5602008-10-29 10:16:30 +05302139 REG_RMW(ah,
2140 AR_GPIO_OE_OUT,
2141 (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
2142 (AR_GPIO_OE_OUT_DRV << gpio_shift));
2143}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002144EXPORT_SYMBOL(ath9k_hw_cfg_output);
Sujithf1dc5602008-10-29 10:16:30 +05302145
Sujithcbe61d82009-02-09 13:27:12 +05302146void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
Sujithf1dc5602008-10-29 10:16:30 +05302147{
Sujith88c1f4f2010-06-30 14:46:31 +05302148 if (AR_DEVID_7010(ah)) {
2149 val = val ? 0 : 1;
2150 REG_RMW(ah, AR7010_GPIO_OUT, ((val&1) << gpio),
2151 AR_GPIO_BIT(gpio));
2152 return;
2153 }
2154
Sujith5b5fa352010-03-17 14:25:15 +05302155 if (AR_SREV_9271(ah))
2156 val = ~val;
2157
Sujithf1dc5602008-10-29 10:16:30 +05302158 REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
2159 AR_GPIO_BIT(gpio));
2160}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002161EXPORT_SYMBOL(ath9k_hw_set_gpio);
Sujithf1dc5602008-10-29 10:16:30 +05302162
Sujithcbe61d82009-02-09 13:27:12 +05302163u32 ath9k_hw_getdefantenna(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302164{
2165 return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
2166}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002167EXPORT_SYMBOL(ath9k_hw_getdefantenna);
Sujithf1dc5602008-10-29 10:16:30 +05302168
Sujithcbe61d82009-02-09 13:27:12 +05302169void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
Sujithf1dc5602008-10-29 10:16:30 +05302170{
2171 REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
2172}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002173EXPORT_SYMBOL(ath9k_hw_setantenna);
Sujithf1dc5602008-10-29 10:16:30 +05302174
Sujithf1dc5602008-10-29 10:16:30 +05302175/*********************/
2176/* General Operation */
2177/*********************/
2178
Sujithcbe61d82009-02-09 13:27:12 +05302179u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302180{
2181 u32 bits = REG_READ(ah, AR_RX_FILTER);
2182 u32 phybits = REG_READ(ah, AR_PHY_ERR);
2183
2184 if (phybits & AR_PHY_ERR_RADAR)
2185 bits |= ATH9K_RX_FILTER_PHYRADAR;
2186 if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
2187 bits |= ATH9K_RX_FILTER_PHYERR;
2188
2189 return bits;
2190}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002191EXPORT_SYMBOL(ath9k_hw_getrxfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302192
Sujithcbe61d82009-02-09 13:27:12 +05302193void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
Sujithf1dc5602008-10-29 10:16:30 +05302194{
2195 u32 phybits;
2196
Sujith7d0d0df2010-04-16 11:53:57 +05302197 ENABLE_REGWRITE_BUFFER(ah);
2198
Sujith7ea310b2009-09-03 12:08:43 +05302199 REG_WRITE(ah, AR_RX_FILTER, bits);
2200
Sujithf1dc5602008-10-29 10:16:30 +05302201 phybits = 0;
2202 if (bits & ATH9K_RX_FILTER_PHYRADAR)
2203 phybits |= AR_PHY_ERR_RADAR;
2204 if (bits & ATH9K_RX_FILTER_PHYERR)
2205 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
2206 REG_WRITE(ah, AR_PHY_ERR, phybits);
2207
2208 if (phybits)
Felix Fietkauca7a4de2011-03-23 20:57:26 +01002209 REG_SET_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
Sujithf1dc5602008-10-29 10:16:30 +05302210 else
Felix Fietkauca7a4de2011-03-23 20:57:26 +01002211 REG_CLR_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
Sujith7d0d0df2010-04-16 11:53:57 +05302212
2213 REGWRITE_BUFFER_FLUSH(ah);
Sujithf1dc5602008-10-29 10:16:30 +05302214}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002215EXPORT_SYMBOL(ath9k_hw_setrxfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302216
Sujithcbe61d82009-02-09 13:27:12 +05302217bool ath9k_hw_phy_disable(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302218{
Senthil Balasubramanian63a75b92009-09-18 15:07:03 +05302219 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
2220 return false;
2221
2222 ath9k_hw_init_pll(ah, NULL);
2223 return true;
Sujithf1dc5602008-10-29 10:16:30 +05302224}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002225EXPORT_SYMBOL(ath9k_hw_phy_disable);
Sujithf1dc5602008-10-29 10:16:30 +05302226
Sujithcbe61d82009-02-09 13:27:12 +05302227bool ath9k_hw_disable(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302228{
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -07002229 if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
Sujithf1dc5602008-10-29 10:16:30 +05302230 return false;
2231
Senthil Balasubramanian63a75b92009-09-18 15:07:03 +05302232 if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
2233 return false;
2234
2235 ath9k_hw_init_pll(ah, NULL);
2236 return true;
Sujithf1dc5602008-10-29 10:16:30 +05302237}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002238EXPORT_SYMBOL(ath9k_hw_disable);
Sujithf1dc5602008-10-29 10:16:30 +05302239
Felix Fietkaude40f312010-10-20 03:08:53 +02002240void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit, bool test)
Sujithf1dc5602008-10-29 10:16:30 +05302241{
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002242 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
Sujith2660b812009-02-09 13:27:26 +05302243 struct ath9k_channel *chan = ah->curchan;
Luis R. Rodriguez5f8e0772009-01-22 15:16:48 -08002244 struct ieee80211_channel *channel = chan->chan;
Sujithf1dc5602008-10-29 10:16:30 +05302245
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002246 regulatory->power_limit = min(limit, (u32) MAX_RATE_POWER);
Sujithf1dc5602008-10-29 10:16:30 +05302247
Vasanthakumar Thiagarajan8fbff4b2009-05-08 17:54:51 -07002248 ah->eep_ops->set_txpower(ah, chan,
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -07002249 ath9k_regd_get_ctl(regulatory, chan),
Vasanthakumar Thiagarajan8fbff4b2009-05-08 17:54:51 -07002250 channel->max_antenna_gain * 2,
2251 channel->max_power * 2,
2252 min((u32) MAX_RATE_POWER,
Felix Fietkaude40f312010-10-20 03:08:53 +02002253 (u32) regulatory->power_limit), test);
Sujithf1dc5602008-10-29 10:16:30 +05302254}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002255EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
Sujithf1dc5602008-10-29 10:16:30 +05302256
Sujithcbe61d82009-02-09 13:27:12 +05302257void ath9k_hw_setopmode(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302258{
Sujith2660b812009-02-09 13:27:26 +05302259 ath9k_hw_set_operating_mode(ah, ah->opmode);
Sujithf1dc5602008-10-29 10:16:30 +05302260}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002261EXPORT_SYMBOL(ath9k_hw_setopmode);
Sujithf1dc5602008-10-29 10:16:30 +05302262
Sujithcbe61d82009-02-09 13:27:12 +05302263void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
Sujithf1dc5602008-10-29 10:16:30 +05302264{
2265 REG_WRITE(ah, AR_MCAST_FIL0, filter0);
2266 REG_WRITE(ah, AR_MCAST_FIL1, filter1);
2267}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002268EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
Sujithf1dc5602008-10-29 10:16:30 +05302269
Luis R. Rodriguezf2b21432009-09-10 08:50:20 -07002270void ath9k_hw_write_associd(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302271{
Luis R. Rodriguez15107182009-09-10 09:22:37 -07002272 struct ath_common *common = ath9k_hw_common(ah);
2273
2274 REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
2275 REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
2276 ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
Sujithf1dc5602008-10-29 10:16:30 +05302277}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002278EXPORT_SYMBOL(ath9k_hw_write_associd);
Sujithf1dc5602008-10-29 10:16:30 +05302279
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002280#define ATH9K_MAX_TSF_READ 10
2281
Sujithcbe61d82009-02-09 13:27:12 +05302282u64 ath9k_hw_gettsf64(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302283{
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002284 u32 tsf_lower, tsf_upper1, tsf_upper2;
2285 int i;
Sujithf1dc5602008-10-29 10:16:30 +05302286
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002287 tsf_upper1 = REG_READ(ah, AR_TSF_U32);
2288 for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
2289 tsf_lower = REG_READ(ah, AR_TSF_L32);
2290 tsf_upper2 = REG_READ(ah, AR_TSF_U32);
2291 if (tsf_upper2 == tsf_upper1)
2292 break;
2293 tsf_upper1 = tsf_upper2;
2294 }
Sujithf1dc5602008-10-29 10:16:30 +05302295
Benoit Papillault1c0fc652010-04-16 00:07:26 +02002296 WARN_ON( i == ATH9K_MAX_TSF_READ );
2297
2298 return (((u64)tsf_upper1 << 32) | tsf_lower);
Sujithf1dc5602008-10-29 10:16:30 +05302299}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002300EXPORT_SYMBOL(ath9k_hw_gettsf64);
Sujithf1dc5602008-10-29 10:16:30 +05302301
Sujithcbe61d82009-02-09 13:27:12 +05302302void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002303{
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002304 REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
Alina Friedrichsenb9a16192009-03-02 23:28:38 +01002305 REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002306}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002307EXPORT_SYMBOL(ath9k_hw_settsf64);
Alina Friedrichsen27abe062009-01-23 05:44:21 +01002308
Sujithcbe61d82009-02-09 13:27:12 +05302309void ath9k_hw_reset_tsf(struct ath_hw *ah)
Sujithf1dc5602008-10-29 10:16:30 +05302310{
Gabor Juhosf9b604f2009-06-21 00:02:15 +02002311 if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
2312 AH_TSF_WRITE_TIMEOUT))
Joe Perches226afe62010-12-02 19:12:37 -08002313 ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
2314 "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
Gabor Juhosf9b604f2009-06-21 00:02:15 +02002315
Sujithf1dc5602008-10-29 10:16:30 +05302316 REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002317}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002318EXPORT_SYMBOL(ath9k_hw_reset_tsf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002319
Sujith54e4cec2009-08-07 09:45:09 +05302320void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002321{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002322 if (setting)
Sujith2660b812009-02-09 13:27:26 +05302323 ah->misc_mode |= AR_PCU_TX_ADD_TSF;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002324 else
Sujith2660b812009-02-09 13:27:26 +05302325 ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002326}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002327EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002328
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002329void ath9k_hw_set11nmac2040(struct ath_hw *ah)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002330{
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002331 struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
Sujithf1dc5602008-10-29 10:16:30 +05302332 u32 macmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002333
Luis R. Rodriguez25c56ee2009-09-13 23:04:44 -07002334 if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
Sujithf1dc5602008-10-29 10:16:30 +05302335 macmode = AR_2040_JOINED_RX_CLEAR;
2336 else
2337 macmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002338
Sujithf1dc5602008-10-29 10:16:30 +05302339 REG_WRITE(ah, AR_2040_MODE, macmode);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002340}
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302341
2342/* HW Generic timers configuration */
2343
2344static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
2345{
2346 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2347 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2348 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2349 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2350 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2351 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2352 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2353 {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
2354 {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
2355 {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
2356 AR_NDP2_TIMER_MODE, 0x0002},
2357 {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
2358 AR_NDP2_TIMER_MODE, 0x0004},
2359 {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
2360 AR_NDP2_TIMER_MODE, 0x0008},
2361 {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
2362 AR_NDP2_TIMER_MODE, 0x0010},
2363 {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
2364 AR_NDP2_TIMER_MODE, 0x0020},
2365 {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
2366 AR_NDP2_TIMER_MODE, 0x0040},
2367 {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
2368 AR_NDP2_TIMER_MODE, 0x0080}
2369};
2370
2371/* HW generic timer primitives */
2372
2373/* compute and clear index of rightmost 1 */
2374static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
2375{
2376 u32 b;
2377
2378 b = *mask;
2379 b &= (0-b);
2380 *mask &= ~b;
2381 b *= debruijn32;
2382 b >>= 27;
2383
2384 return timer_table->gen_timer_index[b];
2385}
2386
Felix Fietkaudd347f22011-03-22 21:54:17 +01002387u32 ath9k_hw_gettsf32(struct ath_hw *ah)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302388{
2389 return REG_READ(ah, AR_TSF_L32);
2390}
Felix Fietkaudd347f22011-03-22 21:54:17 +01002391EXPORT_SYMBOL(ath9k_hw_gettsf32);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302392
2393struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
2394 void (*trigger)(void *),
2395 void (*overflow)(void *),
2396 void *arg,
2397 u8 timer_index)
2398{
2399 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2400 struct ath_gen_timer *timer;
2401
2402 timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
2403
2404 if (timer == NULL) {
Joe Perches38002762010-12-02 19:12:36 -08002405 ath_err(ath9k_hw_common(ah),
2406 "Failed to allocate memory for hw timer[%d]\n",
2407 timer_index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302408 return NULL;
2409 }
2410
2411 /* allocate a hardware generic timer slot */
2412 timer_table->timers[timer_index] = timer;
2413 timer->index = timer_index;
2414 timer->trigger = trigger;
2415 timer->overflow = overflow;
2416 timer->arg = arg;
2417
2418 return timer;
2419}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002420EXPORT_SYMBOL(ath_gen_timer_alloc);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302421
Luis R. Rodriguezcd9bf682009-09-13 02:08:34 -07002422void ath9k_hw_gen_timer_start(struct ath_hw *ah,
2423 struct ath_gen_timer *timer,
2424 u32 timer_next,
2425 u32 timer_period)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302426{
2427 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2428 u32 tsf;
2429
2430 BUG_ON(!timer_period);
2431
2432 set_bit(timer->index, &timer_table->timer_mask.timer_bits);
2433
2434 tsf = ath9k_hw_gettsf32(ah);
2435
Joe Perches226afe62010-12-02 19:12:37 -08002436 ath_dbg(ath9k_hw_common(ah), ATH_DBG_HWTIMER,
2437 "current tsf %x period %x timer_next %x\n",
2438 tsf, timer_period, timer_next);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302439
2440 /*
2441 * Pull timer_next forward if the current TSF already passed it
2442 * because of software latency
2443 */
2444 if (timer_next < tsf)
2445 timer_next = tsf + timer_period;
2446
2447 /*
2448 * Program generic timer registers
2449 */
2450 REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
2451 timer_next);
2452 REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
2453 timer_period);
2454 REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2455 gen_tmr_configuration[timer->index].mode_mask);
2456
2457 /* Enable both trigger and thresh interrupt masks */
2458 REG_SET_BIT(ah, AR_IMR_S5,
2459 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2460 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302461}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002462EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302463
Luis R. Rodriguezcd9bf682009-09-13 02:08:34 -07002464void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302465{
2466 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2467
2468 if ((timer->index < AR_FIRST_NDP_TIMER) ||
2469 (timer->index >= ATH_MAX_GEN_TIMER)) {
2470 return;
2471 }
2472
2473 /* Clear generic timer enable bits. */
2474 REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
2475 gen_tmr_configuration[timer->index].mode_mask);
2476
2477 /* Disable both trigger and thresh interrupt masks */
2478 REG_CLR_BIT(ah, AR_IMR_S5,
2479 (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
2480 SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
2481
2482 clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302483}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002484EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302485
2486void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
2487{
2488 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2489
2490 /* free the hardware generic timer slot */
2491 timer_table->timers[timer->index] = NULL;
2492 kfree(timer);
2493}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002494EXPORT_SYMBOL(ath_gen_timer_free);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302495
2496/*
2497 * Generic Timer Interrupts handling
2498 */
2499void ath_gen_timer_isr(struct ath_hw *ah)
2500{
2501 struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
2502 struct ath_gen_timer *timer;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002503 struct ath_common *common = ath9k_hw_common(ah);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302504 u32 trigger_mask, thresh_mask, index;
2505
2506 /* get hardware generic timer interrupt status */
2507 trigger_mask = ah->intr_gen_timer_trigger;
2508 thresh_mask = ah->intr_gen_timer_thresh;
2509 trigger_mask &= timer_table->timer_mask.val;
2510 thresh_mask &= timer_table->timer_mask.val;
2511
2512 trigger_mask &= ~thresh_mask;
2513
2514 while (thresh_mask) {
2515 index = rightmost_index(timer_table, &thresh_mask);
2516 timer = timer_table->timers[index];
2517 BUG_ON(!timer);
Joe Perches226afe62010-12-02 19:12:37 -08002518 ath_dbg(common, ATH_DBG_HWTIMER,
2519 "TSF overflow for Gen timer %d\n", index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302520 timer->overflow(timer->arg);
2521 }
2522
2523 while (trigger_mask) {
2524 index = rightmost_index(timer_table, &trigger_mask);
2525 timer = timer_table->timers[index];
2526 BUG_ON(!timer);
Joe Perches226afe62010-12-02 19:12:37 -08002527 ath_dbg(common, ATH_DBG_HWTIMER,
2528 "Gen timer[%d] trigger\n", index);
Vasanthakumar Thiagarajanff155a42009-08-26 21:08:49 +05302529 timer->trigger(timer->arg);
2530 }
2531}
Luis R. Rodriguez7322fd12009-09-23 23:07:00 -04002532EXPORT_SYMBOL(ath_gen_timer_isr);
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002533
Sujith05020d22010-03-17 14:25:23 +05302534/********/
2535/* HTC */
2536/********/
2537
2538void ath9k_hw_htc_resetinit(struct ath_hw *ah)
2539{
2540 ah->htc_reset_init = true;
2541}
2542EXPORT_SYMBOL(ath9k_hw_htc_resetinit);
2543
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002544static struct {
2545 u32 version;
2546 const char * name;
2547} ath_mac_bb_names[] = {
2548 /* Devices with external radios */
2549 { AR_SREV_VERSION_5416_PCI, "5416" },
2550 { AR_SREV_VERSION_5416_PCIE, "5418" },
2551 { AR_SREV_VERSION_9100, "9100" },
2552 { AR_SREV_VERSION_9160, "9160" },
2553 /* Single-chip solutions */
2554 { AR_SREV_VERSION_9280, "9280" },
2555 { AR_SREV_VERSION_9285, "9285" },
Luis R. Rodriguez11158472009-10-27 12:59:35 -04002556 { AR_SREV_VERSION_9287, "9287" },
2557 { AR_SREV_VERSION_9271, "9271" },
Luis R. Rodriguezec839032010-04-15 17:39:20 -04002558 { AR_SREV_VERSION_9300, "9300" },
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002559};
2560
2561/* For devices with external radios */
2562static struct {
2563 u16 version;
2564 const char * name;
2565} ath_rf_names[] = {
2566 { 0, "5133" },
2567 { AR_RAD5133_SREV_MAJOR, "5133" },
2568 { AR_RAD5122_SREV_MAJOR, "5122" },
2569 { AR_RAD2133_SREV_MAJOR, "2133" },
2570 { AR_RAD2122_SREV_MAJOR, "2122" }
2571};
2572
2573/*
2574 * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
2575 */
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04002576static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002577{
2578 int i;
2579
2580 for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
2581 if (ath_mac_bb_names[i].version == mac_bb_version) {
2582 return ath_mac_bb_names[i].name;
2583 }
2584 }
2585
2586 return "????";
2587}
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002588
2589/*
2590 * Return the RF name. "????" is returned if the RF is unknown.
2591 * Used for devices with external radios.
2592 */
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04002593static const char *ath9k_hw_rf_name(u16 rf_version)
Luis R. Rodriguez2da4f012009-10-27 12:59:33 -04002594{
2595 int i;
2596
2597 for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
2598 if (ath_rf_names[i].version == rf_version) {
2599 return ath_rf_names[i].name;
2600 }
2601 }
2602
2603 return "????";
2604}
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04002605
2606void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
2607{
2608 int used;
2609
2610 /* chipsets >= AR9280 are single-chip */
Felix Fietkau7a370812010-09-22 12:34:52 +02002611 if (AR_SREV_9280_20_OR_LATER(ah)) {
Luis R. Rodriguezf934c4d2009-10-27 12:59:34 -04002612 used = snprintf(hw_name, len,
2613 "Atheros AR%s Rev:%x",
2614 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2615 ah->hw_version.macRev);
2616 }
2617 else {
2618 used = snprintf(hw_name, len,
2619 "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
2620 ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
2621 ah->hw_version.macRev,
2622 ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
2623 AR_RADIO_SREV_MAJOR)),
2624 ah->hw_version.phyRev);
2625 }
2626
2627 hw_name[used] = '\0';
2628}
2629EXPORT_SYMBOL(ath9k_hw_name);