ath9k: Clear TSF2 properly
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / ar9003_wow.c
1 /*
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"
20 #include "reg_wow.h"
21 #include "hw-ops.h"
22
23 static 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;
36         }
37
38         if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
39                 if (!REG_READ(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL))
40                         REG_CLR_BIT(ah, AR_DIRECT_CONNECT, AR_DC_TSF2_ENABLE);
41         } else if (AR_SREV_9485(ah)){
42                 if (!(REG_READ(ah, AR_NDP2_TIMER_MODE) &
43                       AR_GEN_TIMERS2_MODE_ENABLE_MASK))
44                         REG_CLR_BIT(ah, AR_DIRECT_CONNECT, AR_DC_TSF2_ENABLE);
45         }
46
47         REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_ON_INT);
48 }
49
50 static void ath9k_wow_create_keep_alive_pattern(struct ath_hw *ah)
51 {
52         struct ath_common *common = ath9k_hw_common(ah);
53         u8 sta_mac_addr[ETH_ALEN], ap_mac_addr[ETH_ALEN];
54         u32 ctl[13] = {0};
55         u32 data_word[KAL_NUM_DATA_WORDS];
56         u8 i;
57         u32 wow_ka_data_word0;
58
59         memcpy(sta_mac_addr, common->macaddr, ETH_ALEN);
60         memcpy(ap_mac_addr, common->curbssid, ETH_ALEN);
61
62         /* set the transmit buffer */
63         ctl[0] = (KAL_FRAME_LEN | (MAX_RATE_POWER << 16));
64         ctl[1] = 0;
65         ctl[3] = 0xb;   /* OFDM_6M hardware value for this rate */
66         ctl[4] = 0;
67         ctl[7] = (ah->txchainmask) << 2;
68         ctl[2] = 0xf << 16; /* tx_tries 0 */
69
70         for (i = 0; i < KAL_NUM_DESC_WORDS; i++)
71                 REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
72
73         REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
74
75         data_word[0] = (KAL_FRAME_TYPE << 2) | (KAL_FRAME_SUB_TYPE << 4) |
76                        (KAL_TO_DS << 8) | (KAL_DURATION_ID << 16);
77         data_word[1] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
78                        (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
79         data_word[2] = (sta_mac_addr[1] << 24) | (sta_mac_addr[0] << 16) |
80                        (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
81         data_word[3] = (sta_mac_addr[5] << 24) | (sta_mac_addr[4] << 16) |
82                        (sta_mac_addr[3] << 8) | (sta_mac_addr[2]);
83         data_word[4] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
84                        (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
85         data_word[5] = (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
86
87         if (AR_SREV_9462_20(ah)) {
88                 /* AR9462 2.0 has an extra descriptor word (time based
89                  * discard) compared to other chips */
90                 REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + (12 * 4)), 0);
91                 wow_ka_data_word0 = AR_WOW_TXBUF(13);
92         } else {
93                 wow_ka_data_word0 = AR_WOW_TXBUF(12);
94         }
95
96         for (i = 0; i < KAL_NUM_DATA_WORDS; i++)
97                 REG_WRITE(ah, (wow_ka_data_word0 + i*4), data_word[i]);
98
99 }
100
101 int ath9k_hw_wow_apply_pattern(struct ath_hw *ah, u8 *user_pattern,
102                                u8 *user_mask, int pattern_count,
103                                int pattern_len)
104 {
105         int i;
106         u32 pattern_val, mask_val;
107         u32 set, clr;
108
109         if (pattern_count >= ah->wow.max_patterns)
110                 return -ENOSPC;
111
112         if (pattern_count < MAX_NUM_PATTERN_LEGACY)
113                 REG_SET_BIT(ah, AR_WOW_PATTERN, BIT(pattern_count));
114         else
115                 REG_SET_BIT(ah, AR_MAC_PCU_WOW4, BIT(pattern_count - 8));
116
117         for (i = 0; i < MAX_PATTERN_SIZE; i += 4) {
118                 memcpy(&pattern_val, user_pattern, 4);
119                 REG_WRITE(ah, (AR_WOW_TB_PATTERN(pattern_count) + i),
120                           pattern_val);
121                 user_pattern += 4;
122         }
123
124         for (i = 0; i < MAX_PATTERN_MASK_SIZE; i += 4) {
125                 memcpy(&mask_val, user_mask, 4);
126                 REG_WRITE(ah, (AR_WOW_TB_MASK(pattern_count) + i), mask_val);
127                 user_mask += 4;
128         }
129
130         if (pattern_count < MAX_NUM_PATTERN_LEGACY)
131                 ah->wow.wow_event_mask |=
132                         BIT(pattern_count + AR_WOW_PAT_FOUND_SHIFT);
133         else
134                 ah->wow.wow_event_mask2 |=
135                         BIT((pattern_count - 8) + AR_WOW_PAT_FOUND_SHIFT);
136
137         if (pattern_count < 4) {
138                 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
139                        AR_WOW_LEN1_SHIFT(pattern_count);
140                 clr = AR_WOW_LENGTH1_MASK(pattern_count);
141                 REG_RMW(ah, AR_WOW_LENGTH1, set, clr);
142         } else if (pattern_count < 8) {
143                 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
144                        AR_WOW_LEN2_SHIFT(pattern_count);
145                 clr = AR_WOW_LENGTH2_MASK(pattern_count);
146                 REG_RMW(ah, AR_WOW_LENGTH2, set, clr);
147         } else if (pattern_count < 12) {
148                 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
149                        AR_WOW_LEN3_SHIFT(pattern_count);
150                 clr = AR_WOW_LENGTH3_MASK(pattern_count);
151                 REG_RMW(ah, AR_WOW_LENGTH3, set, clr);
152         } else if (pattern_count < MAX_NUM_PATTERN) {
153                 set = (pattern_len & AR_WOW_LENGTH_MAX) <<
154                        AR_WOW_LEN4_SHIFT(pattern_count);
155                 clr = AR_WOW_LENGTH4_MASK(pattern_count);
156                 REG_RMW(ah, AR_WOW_LENGTH4, set, clr);
157         }
158
159         return 0;
160 }
161 EXPORT_SYMBOL(ath9k_hw_wow_apply_pattern);
162
163 u32 ath9k_hw_wow_wakeup(struct ath_hw *ah)
164 {
165         u32 wow_status = 0;
166         u32 val = 0, rval;
167
168         /*
169          * read the WoW status register to know
170          * the wakeup reason
171          */
172         rval = REG_READ(ah, AR_WOW_PATTERN);
173         val = AR_WOW_STATUS(rval);
174
175         /*
176          * mask only the WoW events that we have enabled. Sometimes
177          * we have spurious WoW events from the AR_WOW_PATTERN
178          * register. This mask will clean it up.
179          */
180
181         val &= ah->wow.wow_event_mask;
182
183         if (val) {
184                 if (val & AR_WOW_MAGIC_PAT_FOUND)
185                         wow_status |= AH_WOW_MAGIC_PATTERN_EN;
186                 if (AR_WOW_PATTERN_FOUND(val))
187                         wow_status |= AH_WOW_USER_PATTERN_EN;
188                 if (val & AR_WOW_KEEP_ALIVE_FAIL)
189                         wow_status |= AH_WOW_LINK_CHANGE;
190                 if (val & AR_WOW_BEACON_FAIL)
191                         wow_status |= AH_WOW_BEACON_MISS;
192         }
193
194         /*
195          * set and clear WOW_PME_CLEAR registers for the chip to
196          * generate next wow signal.
197          * disable D3 before accessing other registers ?
198          */
199
200         /* do we need to check the bit value 0x01000000 (7-10) ?? */
201         REG_RMW(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_WOW_PME_CLR,
202                 AR_PMCTRL_PWR_STATE_D1D3);
203
204         /*
205          * clear all events
206          */
207         REG_WRITE(ah, AR_WOW_PATTERN,
208                   AR_WOW_CLEAR_EVENTS(REG_READ(ah, AR_WOW_PATTERN)));
209
210         /*
211          * restore the beacon threshold to init value
212          */
213         REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
214
215         /*
216          * Restore the way the PCI-E reset, Power-On-Reset, external
217          * PCIE_POR_SHORT pins are tied to its original value.
218          * Previously just before WoW sleep, we untie the PCI-E
219          * reset to our Chip's Power On Reset so that any PCI-E
220          * reset from the bus will not reset our chip
221          */
222         if (ah->is_pciexpress)
223                 ath9k_hw_configpcipowersave(ah, false);
224
225         ah->wow.wow_event_mask = 0;
226
227         return wow_status;
228 }
229 EXPORT_SYMBOL(ath9k_hw_wow_wakeup);
230
231 static void ath9k_hw_wow_set_arwr_reg(struct ath_hw *ah)
232 {
233         u32 wa_reg;
234
235         if (!ah->is_pciexpress)
236                 return;
237
238         /*
239          * We need to untie the internal POR (power-on-reset)
240          * to the external PCI-E reset. We also need to tie
241          * the PCI-E Phy reset to the PCI-E reset.
242          */
243         wa_reg = REG_READ(ah, AR_WA);
244         wa_reg &= ~AR_WA_UNTIE_RESET_EN;
245         wa_reg |= AR_WA_RESET_EN;
246         wa_reg |= AR_WA_POR_SHORT;
247
248         REG_WRITE(ah, AR_WA, wa_reg);
249 }
250
251 void ath9k_hw_wow_enable(struct ath_hw *ah, u32 pattern_enable)
252 {
253         u32 wow_event_mask;
254         u32 keep_alive, magic_pattern, host_pm_ctrl;
255
256         wow_event_mask = ah->wow.wow_event_mask;
257
258         /*
259          * AR_PMCTRL_HOST_PME_EN - Override PME enable in configuration
260          *                         space and allow MAC to generate WoW anyway.
261          *
262          * AR_PMCTRL_PWR_PM_CTRL_ENA - ???
263          *
264          * AR_PMCTRL_AUX_PWR_DET - PCI core SYS_AUX_PWR_DET signal,
265          *                         needs to be set for WoW in PCI mode.
266          *
267          * AR_PMCTRL_WOW_PME_CLR - WoW Clear Signal going to the MAC.
268          *
269          * Set the power states appropriately and enable PME.
270          *
271          * Set and clear WOW_PME_CLEAR for the chip
272          * to generate next wow signal.
273          */
274         REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_HOST_PME_EN |
275                                          AR_PMCTRL_PWR_PM_CTRL_ENA |
276                                          AR_PMCTRL_AUX_PWR_DET |
277                                          AR_PMCTRL_WOW_PME_CLR);
278         REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_WOW_PME_CLR);
279
280         /*
281          * Random Backoff.
282          *
283          * 31:28 in AR_WOW_PATTERN : Indicates the number of bits used in the
284          *                           contention window. For value N,
285          *                           the random backoff will be selected between
286          *                           0 and (2 ^ N) - 1.
287          */
288         REG_SET_BIT(ah, AR_WOW_PATTERN,
289                     AR_WOW_BACK_OFF_SHIFT(AR_WOW_PAT_BACKOFF));
290
291         /*
292          * AIFS time, Slot time, Keep Alive count.
293          */
294         REG_SET_BIT(ah, AR_WOW_COUNT, AR_WOW_AIFS_CNT(AR_WOW_CNT_AIFS_CNT) |
295                                       AR_WOW_SLOT_CNT(AR_WOW_CNT_SLOT_CNT) |
296                                       AR_WOW_KEEP_ALIVE_CNT(AR_WOW_CNT_KA_CNT));
297         /*
298          * Beacon timeout.
299          */
300         if (pattern_enable & AH_WOW_BEACON_MISS)
301                 REG_WRITE(ah, AR_WOW_BCN_TIMO, AR_WOW_BEACON_TIMO);
302         else
303                 REG_WRITE(ah, AR_WOW_BCN_TIMO, AR_WOW_BEACON_TIMO_MAX);
304
305         /*
306          * Keep alive timeout in ms.
307          */
308         if (!pattern_enable)
309                 REG_WRITE(ah, AR_WOW_KEEP_ALIVE_TIMO, AR_WOW_KEEP_ALIVE_NEVER);
310         else
311                 REG_WRITE(ah, AR_WOW_KEEP_ALIVE_TIMO, KAL_TIMEOUT * 32);
312
313         /*
314          * Keep alive delay in us.
315          */
316         REG_WRITE(ah, AR_WOW_KEEP_ALIVE_DELAY, KAL_DELAY * 1000);
317
318         /*
319          * Create keep alive pattern to respond to beacons.
320          */
321         ath9k_wow_create_keep_alive_pattern(ah);
322
323         /*
324          * Configure keep alive register.
325          */
326         keep_alive = REG_READ(ah, AR_WOW_KEEP_ALIVE);
327
328         /* Send keep alive timeouts anyway */
329         keep_alive &= ~AR_WOW_KEEP_ALIVE_AUTO_DIS;
330
331         if (pattern_enable & AH_WOW_LINK_CHANGE) {
332                 keep_alive &= ~AR_WOW_KEEP_ALIVE_FAIL_DIS;
333                 wow_event_mask |= AR_WOW_KEEP_ALIVE_FAIL;
334         } else {
335                 keep_alive |= AR_WOW_KEEP_ALIVE_FAIL_DIS;
336         }
337
338         REG_WRITE(ah, AR_WOW_KEEP_ALIVE, keep_alive);
339
340         /*
341          * We are relying on a bmiss failure, ensure we have
342          * enough threshold to prevent false positives.
343          */
344         REG_RMW_FIELD(ah, AR_RSSI_THR, AR_RSSI_THR_BM_THR,
345                       AR_WOW_BMISSTHRESHOLD);
346
347         if (pattern_enable & AH_WOW_BEACON_MISS) {
348                 wow_event_mask |= AR_WOW_BEACON_FAIL;
349                 REG_SET_BIT(ah, AR_WOW_BCN_EN, AR_WOW_BEACON_FAIL_EN);
350         } else {
351                 REG_CLR_BIT(ah, AR_WOW_BCN_EN, AR_WOW_BEACON_FAIL_EN);
352         }
353
354         /*
355          * Enable the magic packet registers.
356          */
357         magic_pattern = REG_READ(ah, AR_WOW_PATTERN);
358         magic_pattern |= AR_WOW_MAC_INTR_EN;
359
360         if (pattern_enable & AH_WOW_MAGIC_PATTERN_EN) {
361                 magic_pattern |= AR_WOW_MAGIC_EN;
362                 wow_event_mask |= AR_WOW_MAGIC_PAT_FOUND;
363         } else {
364                 magic_pattern &= ~AR_WOW_MAGIC_EN;
365         }
366
367         REG_WRITE(ah, AR_WOW_PATTERN, magic_pattern);
368
369         /*
370          * Enable pattern matching for packets which are less
371          * than 256 bytes.
372          */
373         REG_WRITE(ah, AR_WOW_PATTERN_MATCH_LT_256B,
374                   AR_WOW_PATTERN_SUPPORTED);
375
376         /*
377          * Set the power states appropriately and enable PME.
378          */
379         host_pm_ctrl = REG_READ(ah, AR_PCIE_PM_CTRL);
380         host_pm_ctrl |= AR_PMCTRL_PWR_STATE_D1D3 |
381                         AR_PMCTRL_HOST_PME_EN |
382                         AR_PMCTRL_PWR_PM_CTRL_ENA;
383         host_pm_ctrl &= ~AR_PCIE_PM_CTRL_ENA;
384
385         if (AR_SREV_9462(ah)) {
386                 /*
387                  * This is needed to prevent the chip waking up
388                  * the host within 3-4 seconds with certain
389                  * platform/BIOS.
390                  */
391                 host_pm_ctrl &= ~AR_PMCTRL_PWR_STATE_D1D3;
392                 host_pm_ctrl |= AR_PMCTRL_PWR_STATE_D1D3_REAL;
393         }
394
395         REG_WRITE(ah, AR_PCIE_PM_CTRL, host_pm_ctrl);
396
397         /*
398          * Enable sequence number generation when asleep.
399          */
400         REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
401
402         /* To bring down WOW power low margin */
403         REG_SET_BIT(ah, AR_PCIE_PHY_REG3, BIT(13));
404
405         ath9k_hw_wow_set_arwr_reg(ah);
406
407         /* HW WoW */
408         REG_CLR_BIT(ah, AR_PCU_MISC_MODE3, BIT(5));
409
410         ath9k_hw_set_powermode_wow_sleep(ah);
411         ah->wow.wow_event_mask = wow_event_mask;
412 }
413 EXPORT_SYMBOL(ath9k_hw_wow_enable);