blob: 4b53d0b4d113bfa5f060456444a23d919adb2c4f [file] [log] [blame]
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +05301/*
2 * Copyright (c) 2012 Qualcomm Atheros, Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <linux/export.h>
18#include "ath9k.h"
19#include "reg.h"
Sujith Manoharance6e9822015-01-30 19:05:34 +053020#include "reg_wow.h"
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053021#include "hw-ops.h"
22
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053023static void ath9k_hw_set_powermode_wow_sleep(struct ath_hw *ah)
24{
25 struct ath_common *common = ath9k_hw_common(ah);
26
27 REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
28
29 /* set rx disable bit */
30 REG_WRITE(ah, AR_CR, AR_CR_RXD);
31
32 if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0, AH_WAIT_TIMEOUT)) {
33 ath_err(common, "Failed to stop Rx DMA in 10ms AR_CR=0x%08x AR_DIAG_SW=0x%08x\n",
34 REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
35 return;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053036 }
37
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053038 REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_ON_INT);
39}
40
41static void ath9k_wow_create_keep_alive_pattern(struct ath_hw *ah)
42{
43 struct ath_common *common = ath9k_hw_common(ah);
44 u8 sta_mac_addr[ETH_ALEN], ap_mac_addr[ETH_ALEN];
45 u32 ctl[13] = {0};
46 u32 data_word[KAL_NUM_DATA_WORDS];
47 u8 i;
48 u32 wow_ka_data_word0;
49
50 memcpy(sta_mac_addr, common->macaddr, ETH_ALEN);
51 memcpy(ap_mac_addr, common->curbssid, ETH_ALEN);
52
53 /* set the transmit buffer */
54 ctl[0] = (KAL_FRAME_LEN | (MAX_RATE_POWER << 16));
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053055 ctl[1] = 0;
56 ctl[3] = 0xb; /* OFDM_6M hardware value for this rate */
57 ctl[4] = 0;
58 ctl[7] = (ah->txchainmask) << 2;
Sujith Manoharan846e4382013-06-03 09:19:24 +053059 ctl[2] = 0xf << 16; /* tx_tries 0 */
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053060
61 for (i = 0; i < KAL_NUM_DESC_WORDS; i++)
62 REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
63
Sujith Manoharan846e4382013-06-03 09:19:24 +053064 REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053065
66 data_word[0] = (KAL_FRAME_TYPE << 2) | (KAL_FRAME_SUB_TYPE << 4) |
67 (KAL_TO_DS << 8) | (KAL_DURATION_ID << 16);
68 data_word[1] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
69 (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
70 data_word[2] = (sta_mac_addr[1] << 24) | (sta_mac_addr[0] << 16) |
71 (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
72 data_word[3] = (sta_mac_addr[5] << 24) | (sta_mac_addr[4] << 16) |
73 (sta_mac_addr[3] << 8) | (sta_mac_addr[2]);
74 data_word[4] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
75 (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
76 data_word[5] = (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
77
Rajkumar Manoharan506ed952012-10-15 15:29:52 +053078 if (AR_SREV_9462_20(ah)) {
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053079 /* AR9462 2.0 has an extra descriptor word (time based
80 * discard) compared to other chips */
81 REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + (12 * 4)), 0);
82 wow_ka_data_word0 = AR_WOW_TXBUF(13);
83 } else {
84 wow_ka_data_word0 = AR_WOW_TXBUF(12);
85 }
86
87 for (i = 0; i < KAL_NUM_DATA_WORDS; i++)
88 REG_WRITE(ah, (wow_ka_data_word0 + i*4), data_word[i]);
89
90}
91
Sujith Manoharan6af75e42015-01-30 19:05:37 +053092int ath9k_hw_wow_apply_pattern(struct ath_hw *ah, u8 *user_pattern,
93 u8 *user_mask, int pattern_count,
94 int pattern_len)
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +053095{
96 int i;
97 u32 pattern_val, mask_val;
98 u32 set, clr;
99
Sujith Manoharan6af75e42015-01-30 19:05:37 +0530100 if (pattern_count >= ah->wow.max_patterns)
101 return -ENOSPC;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530102
Sujith Manoharana28815d2015-02-02 18:21:08 +0530103 if (pattern_count < MAX_NUM_PATTERN_LEGACY)
104 REG_SET_BIT(ah, AR_WOW_PATTERN, BIT(pattern_count));
105 else
106 REG_SET_BIT(ah, AR_MAC_PCU_WOW4, BIT(pattern_count - 8));
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530107
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530108 for (i = 0; i < MAX_PATTERN_SIZE; i += 4) {
109 memcpy(&pattern_val, user_pattern, 4);
110 REG_WRITE(ah, (AR_WOW_TB_PATTERN(pattern_count) + i),
111 pattern_val);
112 user_pattern += 4;
113 }
114
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530115 for (i = 0; i < MAX_PATTERN_MASK_SIZE; i += 4) {
116 memcpy(&mask_val, user_mask, 4);
117 REG_WRITE(ah, (AR_WOW_TB_MASK(pattern_count) + i), mask_val);
118 user_mask += 4;
119 }
120
Sujith Manoharana28815d2015-02-02 18:21:08 +0530121 if (pattern_count < MAX_NUM_PATTERN_LEGACY)
122 ah->wow.wow_event_mask |=
123 BIT(pattern_count + AR_WOW_PAT_FOUND_SHIFT);
124 else
125 ah->wow.wow_event_mask2 |=
126 BIT((pattern_count - 8) + AR_WOW_PAT_FOUND_SHIFT);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530127
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530128 if (pattern_count < 4) {
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530129 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
130 AR_WOW_LEN1_SHIFT(pattern_count);
131 clr = AR_WOW_LENGTH1_MASK(pattern_count);
132 REG_RMW(ah, AR_WOW_LENGTH1, set, clr);
Sujith Manoharana28815d2015-02-02 18:21:08 +0530133 } else if (pattern_count < 8) {
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530134 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
135 AR_WOW_LEN2_SHIFT(pattern_count);
136 clr = AR_WOW_LENGTH2_MASK(pattern_count);
137 REG_RMW(ah, AR_WOW_LENGTH2, set, clr);
Sujith Manoharana28815d2015-02-02 18:21:08 +0530138 } else if (pattern_count < 12) {
139 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
140 AR_WOW_LEN3_SHIFT(pattern_count);
141 clr = AR_WOW_LENGTH3_MASK(pattern_count);
142 REG_RMW(ah, AR_WOW_LENGTH3, set, clr);
143 } else if (pattern_count < MAX_NUM_PATTERN) {
144 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
145 AR_WOW_LEN4_SHIFT(pattern_count);
146 clr = AR_WOW_LENGTH4_MASK(pattern_count);
147 REG_RMW(ah, AR_WOW_LENGTH4, set, clr);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530148 }
149
Sujith Manoharan6af75e42015-01-30 19:05:37 +0530150 return 0;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530151}
152EXPORT_SYMBOL(ath9k_hw_wow_apply_pattern);
153
154u32 ath9k_hw_wow_wakeup(struct ath_hw *ah)
155{
156 u32 wow_status = 0;
157 u32 val = 0, rval;
Sujith Manoharan846e4382013-06-03 09:19:24 +0530158
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530159 /*
160 * read the WoW status register to know
161 * the wakeup reason
162 */
163 rval = REG_READ(ah, AR_WOW_PATTERN);
164 val = AR_WOW_STATUS(rval);
165
166 /*
167 * mask only the WoW events that we have enabled. Sometimes
168 * we have spurious WoW events from the AR_WOW_PATTERN
169 * register. This mask will clean it up.
170 */
171
Sujith Manoharan41fe8832015-01-30 19:05:32 +0530172 val &= ah->wow.wow_event_mask;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530173
174 if (val) {
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530175 if (val & AR_WOW_MAGIC_PAT_FOUND)
176 wow_status |= AH_WOW_MAGIC_PATTERN_EN;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530177 if (AR_WOW_PATTERN_FOUND(val))
178 wow_status |= AH_WOW_USER_PATTERN_EN;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530179 if (val & AR_WOW_KEEP_ALIVE_FAIL)
180 wow_status |= AH_WOW_LINK_CHANGE;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530181 if (val & AR_WOW_BEACON_FAIL)
182 wow_status |= AH_WOW_BEACON_MISS;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530183 }
184
185 /*
186 * set and clear WOW_PME_CLEAR registers for the chip to
187 * generate next wow signal.
188 * disable D3 before accessing other registers ?
189 */
190
191 /* do we need to check the bit value 0x01000000 (7-10) ?? */
192 REG_RMW(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_WOW_PME_CLR,
193 AR_PMCTRL_PWR_STATE_D1D3);
194
195 /*
196 * clear all events
197 */
198 REG_WRITE(ah, AR_WOW_PATTERN,
199 AR_WOW_CLEAR_EVENTS(REG_READ(ah, AR_WOW_PATTERN)));
200
201 /*
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530202 * restore the beacon threshold to init value
203 */
204 REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
205
206 /*
207 * Restore the way the PCI-E reset, Power-On-Reset, external
208 * PCIE_POR_SHORT pins are tied to its original value.
209 * Previously just before WoW sleep, we untie the PCI-E
210 * reset to our Chip's Power On Reset so that any PCI-E
211 * reset from the bus will not reset our chip
212 */
Sujith Manoharan846e4382013-06-03 09:19:24 +0530213 if (ah->is_pciexpress)
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530214 ath9k_hw_configpcipowersave(ah, false);
215
Sujith Manoharan41fe8832015-01-30 19:05:32 +0530216 ah->wow.wow_event_mask = 0;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530217
218 return wow_status;
219}
220EXPORT_SYMBOL(ath9k_hw_wow_wakeup);
221
222void ath9k_hw_wow_enable(struct ath_hw *ah, u32 pattern_enable)
223{
224 u32 wow_event_mask;
225 u32 set, clr;
226
227 /*
228 * wow_event_mask is a mask to the AR_WOW_PATTERN register to
229 * indicate which WoW events we have enabled. The WoW events
230 * are from the 'pattern_enable' in this function and
231 * 'pattern_count' of ath9k_hw_wow_apply_pattern()
232 */
Sujith Manoharan41fe8832015-01-30 19:05:32 +0530233 wow_event_mask = ah->wow.wow_event_mask;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530234
235 /*
236 * Untie Power-on-Reset from the PCI-E-Reset. When we are in
237 * WOW sleep, we do want the Reset from the PCI-E to disturb
238 * our hw state
239 */
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530240 if (ah->is_pciexpress) {
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530241 /*
242 * we need to untie the internal POR (power-on-reset)
243 * to the external PCI-E reset. We also need to tie
244 * the PCI-E Phy reset to the PCI-E reset.
245 */
Sujith Manoharan846e4382013-06-03 09:19:24 +0530246 set = AR_WA_RESET_EN | AR_WA_POR_SHORT;
247 clr = AR_WA_UNTIE_RESET_EN | AR_WA_D3_L1_DISABLE;
248 REG_RMW(ah, AR_WA, set, clr);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530249 }
250
251 /*
252 * set the power states appropriately and enable PME
253 */
254 set = AR_PMCTRL_HOST_PME_EN | AR_PMCTRL_PWR_PM_CTRL_ENA |
255 AR_PMCTRL_AUX_PWR_DET | AR_PMCTRL_WOW_PME_CLR;
256
257 /*
258 * set and clear WOW_PME_CLEAR registers for the chip
259 * to generate next wow signal.
260 */
261 REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
262 clr = AR_PMCTRL_WOW_PME_CLR;
263 REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
264
265 /*
266 * Setup for:
267 * - beacon misses
268 * - magic pattern
269 * - keep alive timeout
270 * - pattern matching
271 */
272
273 /*
274 * Program default values for pattern backoff, aifs/slot/KAL count,
275 * beacon miss timeout, KAL timeout, etc.
276 */
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530277 set = AR_WOW_BACK_OFF_SHIFT(AR_WOW_PAT_BACKOFF);
278 REG_SET_BIT(ah, AR_WOW_PATTERN, set);
279
280 set = AR_WOW_AIFS_CNT(AR_WOW_CNT_AIFS_CNT) |
281 AR_WOW_SLOT_CNT(AR_WOW_CNT_SLOT_CNT) |
282 AR_WOW_KEEP_ALIVE_CNT(AR_WOW_CNT_KA_CNT);
283 REG_SET_BIT(ah, AR_WOW_COUNT, set);
284
285 if (pattern_enable & AH_WOW_BEACON_MISS)
286 set = AR_WOW_BEACON_TIMO;
287 /* We are not using beacon miss, program a large value */
288 else
289 set = AR_WOW_BEACON_TIMO_MAX;
290
291 REG_WRITE(ah, AR_WOW_BCN_TIMO, set);
292
293 /*
294 * Keep alive timo in ms except AR9280
295 */
Sujith Manoharan846e4382013-06-03 09:19:24 +0530296 if (!pattern_enable)
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530297 set = AR_WOW_KEEP_ALIVE_NEVER;
298 else
299 set = KAL_TIMEOUT * 32;
300
301 REG_WRITE(ah, AR_WOW_KEEP_ALIVE_TIMO, set);
302
303 /*
304 * Keep alive delay in us. based on 'power on clock',
305 * therefore in usec
306 */
307 set = KAL_DELAY * 1000;
308 REG_WRITE(ah, AR_WOW_KEEP_ALIVE_DELAY, set);
309
310 /*
311 * Create keep alive pattern to respond to beacons
312 */
313 ath9k_wow_create_keep_alive_pattern(ah);
314
315 /*
316 * Configure MAC WoW Registers
317 */
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530318 set = 0;
319 /* Send keep alive timeouts anyway */
320 clr = AR_WOW_KEEP_ALIVE_AUTO_DIS;
321
322 if (pattern_enable & AH_WOW_LINK_CHANGE)
323 wow_event_mask |= AR_WOW_KEEP_ALIVE_FAIL;
324 else
325 set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
326
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530327 set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530328 REG_RMW(ah, AR_WOW_KEEP_ALIVE, set, clr);
329
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530330 /*
331 * we are relying on a bmiss failure. ensure we have
332 * enough threshold to prevent false positives
333 */
334 REG_RMW_FIELD(ah, AR_RSSI_THR, AR_RSSI_THR_BM_THR,
335 AR_WOW_BMISSTHRESHOLD);
336
337 set = 0;
338 clr = 0;
339
340 if (pattern_enable & AH_WOW_BEACON_MISS) {
341 set = AR_WOW_BEACON_FAIL_EN;
342 wow_event_mask |= AR_WOW_BEACON_FAIL;
343 } else {
344 clr = AR_WOW_BEACON_FAIL_EN;
345 }
346
347 REG_RMW(ah, AR_WOW_BCN_EN, set, clr);
348
349 set = 0;
350 clr = 0;
351 /*
352 * Enable the magic packet registers
353 */
354 if (pattern_enable & AH_WOW_MAGIC_PATTERN_EN) {
355 set = AR_WOW_MAGIC_EN;
356 wow_event_mask |= AR_WOW_MAGIC_PAT_FOUND;
357 } else {
358 clr = AR_WOW_MAGIC_EN;
359 }
360 set |= AR_WOW_MAC_INTR_EN;
361 REG_RMW(ah, AR_WOW_PATTERN, set, clr);
362
Sujith Manoharan846e4382013-06-03 09:19:24 +0530363 REG_WRITE(ah, AR_WOW_PATTERN_MATCH_LT_256B,
364 AR_WOW_PATTERN_SUPPORTED);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530365
366 /*
367 * Set the power states appropriately and enable PME
368 */
369 clr = 0;
370 set = AR_PMCTRL_PWR_STATE_D1D3 | AR_PMCTRL_HOST_PME_EN |
371 AR_PMCTRL_PWR_PM_CTRL_ENA;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530372
Sujith Manoharan846e4382013-06-03 09:19:24 +0530373 clr = AR_PCIE_PM_CTRL_ENA;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530374 REG_RMW(ah, AR_PCIE_PM_CTRL, set, clr);
375
Sujith Manoharan846e4382013-06-03 09:19:24 +0530376 /*
377 * this is needed to prevent the chip waking up
378 * the host within 3-4 seconds with certain
379 * platform/BIOS. The fix is to enable
380 * D1 & D3 to match original definition and
381 * also match the OTP value. Anyway this
382 * is more related to SW WOW.
383 */
384 clr = AR_PMCTRL_PWR_STATE_D1D3;
385 REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530386
Sujith Manoharan846e4382013-06-03 09:19:24 +0530387 set = AR_PMCTRL_PWR_STATE_D1D3_REAL;
388 REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530389
390 REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
391
Sujith Manoharan846e4382013-06-03 09:19:24 +0530392 /* to bring down WOW power low margin */
393 set = BIT(13);
394 REG_SET_BIT(ah, AR_PCIE_PHY_REG3, set);
395 /* HW WoW */
396 clr = BIT(5);
397 REG_CLR_BIT(ah, AR_PCU_MISC_MODE3, clr);
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530398
399 ath9k_hw_set_powermode_wow_sleep(ah);
Sujith Manoharan41fe8832015-01-30 19:05:32 +0530400 ah->wow.wow_event_mask = wow_event_mask;
Mohammed Shafi Shajakhan64875c62012-07-10 14:56:15 +0530401}
402EXPORT_SYMBOL(ath9k_hw_wow_enable);