ar9003_wow.c 13 KB

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