ar9003_wow.c 12 KB

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