wow.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "hw-ops.h"
  20. const char *ath9k_hw_wow_event_to_string(u32 wow_event)
  21. {
  22. if (wow_event & AH_WOW_MAGIC_PATTERN_EN)
  23. return "Magic pattern";
  24. if (wow_event & AH_WOW_USER_PATTERN_EN)
  25. return "User pattern";
  26. if (wow_event & AH_WOW_LINK_CHANGE)
  27. return "Link change";
  28. if (wow_event & AH_WOW_BEACON_MISS)
  29. return "Beacon miss";
  30. return "unknown reason";
  31. }
  32. EXPORT_SYMBOL(ath9k_hw_wow_event_to_string);
  33. static void ath9k_hw_set_powermode_wow_sleep(struct ath_hw *ah)
  34. {
  35. struct ath_common *common = ath9k_hw_common(ah);
  36. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  37. /* set rx disable bit */
  38. REG_WRITE(ah, AR_CR, AR_CR_RXD);
  39. if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0, AH_WAIT_TIMEOUT)) {
  40. ath_err(common, "Failed to stop Rx DMA in 10ms AR_CR=0x%08x AR_DIAG_SW=0x%08x\n",
  41. REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
  42. return;
  43. }
  44. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_ON_INT);
  45. }
  46. static void ath9k_wow_create_keep_alive_pattern(struct ath_hw *ah)
  47. {
  48. struct ath_common *common = ath9k_hw_common(ah);
  49. u8 sta_mac_addr[ETH_ALEN], ap_mac_addr[ETH_ALEN];
  50. u32 ctl[13] = {0};
  51. u32 data_word[KAL_NUM_DATA_WORDS];
  52. u8 i;
  53. u32 wow_ka_data_word0;
  54. memcpy(sta_mac_addr, common->macaddr, ETH_ALEN);
  55. memcpy(ap_mac_addr, common->curbssid, ETH_ALEN);
  56. /* set the transmit buffer */
  57. ctl[0] = (KAL_FRAME_LEN | (MAX_RATE_POWER << 16));
  58. ctl[1] = 0;
  59. ctl[3] = 0xb; /* OFDM_6M hardware value for this rate */
  60. ctl[4] = 0;
  61. ctl[7] = (ah->txchainmask) << 2;
  62. ctl[2] = 0xf << 16; /* tx_tries 0 */
  63. for (i = 0; i < KAL_NUM_DESC_WORDS; i++)
  64. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
  65. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
  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. if (AR_SREV_9462_20(ah)) {
  78. /* AR9462 2.0 has an extra descriptor word (time based
  79. * discard) compared to other chips */
  80. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + (12 * 4)), 0);
  81. wow_ka_data_word0 = AR_WOW_TXBUF(13);
  82. } else {
  83. wow_ka_data_word0 = AR_WOW_TXBUF(12);
  84. }
  85. for (i = 0; i < KAL_NUM_DATA_WORDS; i++)
  86. REG_WRITE(ah, (wow_ka_data_word0 + i*4), data_word[i]);
  87. }
  88. void ath9k_hw_wow_apply_pattern(struct ath_hw *ah, u8 *user_pattern,
  89. u8 *user_mask, int pattern_count,
  90. int pattern_len)
  91. {
  92. int i;
  93. u32 pattern_val, mask_val;
  94. u32 set, clr;
  95. /* FIXME: should check count by querying the hardware capability */
  96. if (pattern_count >= MAX_NUM_PATTERN)
  97. return;
  98. REG_SET_BIT(ah, AR_WOW_PATTERN, BIT(pattern_count));
  99. /* set the registers for pattern */
  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. /* set the registers for mask */
  107. for (i = 0; i < MAX_PATTERN_MASK_SIZE; i += 4) {
  108. memcpy(&mask_val, user_mask, 4);
  109. REG_WRITE(ah, (AR_WOW_TB_MASK(pattern_count) + i), mask_val);
  110. user_mask += 4;
  111. }
  112. /* set the pattern length to be matched
  113. *
  114. * AR_WOW_LENGTH1_REG1
  115. * bit 31:24 pattern 0 length
  116. * bit 23:16 pattern 1 length
  117. * bit 15:8 pattern 2 length
  118. * bit 7:0 pattern 3 length
  119. *
  120. * AR_WOW_LENGTH1_REG2
  121. * bit 31:24 pattern 4 length
  122. * bit 23:16 pattern 5 length
  123. * bit 15:8 pattern 6 length
  124. * bit 7:0 pattern 7 length
  125. *
  126. * the below logic writes out the new
  127. * pattern length for the corresponding
  128. * pattern_count, while masking out the
  129. * other fields
  130. */
  131. ah->wow_event_mask |= BIT(pattern_count + AR_WOW_PAT_FOUND_SHIFT);
  132. if (pattern_count < 4) {
  133. /* Pattern 0-3 uses AR_WOW_LENGTH1 register */
  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 {
  139. /* Pattern 4-7 uses AR_WOW_LENGTH2 register */
  140. set = (pattern_len & AR_WOW_LENGTH_MAX) <<
  141. AR_WOW_LEN2_SHIFT(pattern_count);
  142. clr = AR_WOW_LENGTH2_MASK(pattern_count);
  143. REG_RMW(ah, AR_WOW_LENGTH2, set, clr);
  144. }
  145. }
  146. EXPORT_SYMBOL(ath9k_hw_wow_apply_pattern);
  147. u32 ath9k_hw_wow_wakeup(struct ath_hw *ah)
  148. {
  149. u32 wow_status = 0;
  150. u32 val = 0, rval;
  151. /*
  152. * read the WoW status register to know
  153. * the wakeup reason
  154. */
  155. rval = REG_READ(ah, AR_WOW_PATTERN);
  156. val = AR_WOW_STATUS(rval);
  157. /*
  158. * mask only the WoW events that we have enabled. Sometimes
  159. * we have spurious WoW events from the AR_WOW_PATTERN
  160. * register. This mask will clean it up.
  161. */
  162. val &= ah->wow_event_mask;
  163. if (val) {
  164. if (val & AR_WOW_MAGIC_PAT_FOUND)
  165. wow_status |= AH_WOW_MAGIC_PATTERN_EN;
  166. if (AR_WOW_PATTERN_FOUND(val))
  167. wow_status |= AH_WOW_USER_PATTERN_EN;
  168. if (val & AR_WOW_KEEP_ALIVE_FAIL)
  169. wow_status |= AH_WOW_LINK_CHANGE;
  170. if (val & AR_WOW_BEACON_FAIL)
  171. wow_status |= AH_WOW_BEACON_MISS;
  172. }
  173. /*
  174. * set and clear WOW_PME_CLEAR registers for the chip to
  175. * generate next wow signal.
  176. * disable D3 before accessing other registers ?
  177. */
  178. /* do we need to check the bit value 0x01000000 (7-10) ?? */
  179. REG_RMW(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_WOW_PME_CLR,
  180. AR_PMCTRL_PWR_STATE_D1D3);
  181. /*
  182. * clear all events
  183. */
  184. REG_WRITE(ah, AR_WOW_PATTERN,
  185. AR_WOW_CLEAR_EVENTS(REG_READ(ah, AR_WOW_PATTERN)));
  186. /*
  187. * restore the beacon threshold to init value
  188. */
  189. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  190. /*
  191. * Restore the way the PCI-E reset, Power-On-Reset, external
  192. * PCIE_POR_SHORT pins are tied to its original value.
  193. * Previously just before WoW sleep, we untie the PCI-E
  194. * reset to our Chip's Power On Reset so that any PCI-E
  195. * reset from the bus will not reset our chip
  196. */
  197. if (ah->is_pciexpress)
  198. ath9k_hw_configpcipowersave(ah, false);
  199. ah->wow_event_mask = 0;
  200. return wow_status;
  201. }
  202. EXPORT_SYMBOL(ath9k_hw_wow_wakeup);
  203. void ath9k_hw_wow_enable(struct ath_hw *ah, u32 pattern_enable)
  204. {
  205. u32 wow_event_mask;
  206. u32 set, clr;
  207. /*
  208. * wow_event_mask is a mask to the AR_WOW_PATTERN register to
  209. * indicate which WoW events we have enabled. The WoW events
  210. * are from the 'pattern_enable' in this function and
  211. * 'pattern_count' of ath9k_hw_wow_apply_pattern()
  212. */
  213. wow_event_mask = ah->wow_event_mask;
  214. /*
  215. * Untie Power-on-Reset from the PCI-E-Reset. When we are in
  216. * WOW sleep, we do want the Reset from the PCI-E to disturb
  217. * our hw state
  218. */
  219. if (ah->is_pciexpress) {
  220. /*
  221. * we need to untie the internal POR (power-on-reset)
  222. * to the external PCI-E reset. We also need to tie
  223. * the PCI-E Phy reset to the PCI-E reset.
  224. */
  225. set = AR_WA_RESET_EN | AR_WA_POR_SHORT;
  226. clr = AR_WA_UNTIE_RESET_EN | AR_WA_D3_L1_DISABLE;
  227. REG_RMW(ah, AR_WA, set, clr);
  228. }
  229. /*
  230. * set the power states appropriately and enable PME
  231. */
  232. set = AR_PMCTRL_HOST_PME_EN | AR_PMCTRL_PWR_PM_CTRL_ENA |
  233. AR_PMCTRL_AUX_PWR_DET | AR_PMCTRL_WOW_PME_CLR;
  234. /*
  235. * set and clear WOW_PME_CLEAR registers for the chip
  236. * to generate next wow signal.
  237. */
  238. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
  239. clr = AR_PMCTRL_WOW_PME_CLR;
  240. REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
  241. /*
  242. * Setup for:
  243. * - beacon misses
  244. * - magic pattern
  245. * - keep alive timeout
  246. * - pattern matching
  247. */
  248. /*
  249. * Program default values for pattern backoff, aifs/slot/KAL count,
  250. * beacon miss timeout, KAL timeout, etc.
  251. */
  252. set = AR_WOW_BACK_OFF_SHIFT(AR_WOW_PAT_BACKOFF);
  253. REG_SET_BIT(ah, AR_WOW_PATTERN, set);
  254. set = AR_WOW_AIFS_CNT(AR_WOW_CNT_AIFS_CNT) |
  255. AR_WOW_SLOT_CNT(AR_WOW_CNT_SLOT_CNT) |
  256. AR_WOW_KEEP_ALIVE_CNT(AR_WOW_CNT_KA_CNT);
  257. REG_SET_BIT(ah, AR_WOW_COUNT, set);
  258. if (pattern_enable & AH_WOW_BEACON_MISS)
  259. set = AR_WOW_BEACON_TIMO;
  260. /* We are not using beacon miss, program a large value */
  261. else
  262. set = AR_WOW_BEACON_TIMO_MAX;
  263. REG_WRITE(ah, AR_WOW_BCN_TIMO, set);
  264. /*
  265. * Keep alive timo in ms except AR9280
  266. */
  267. if (!pattern_enable)
  268. set = AR_WOW_KEEP_ALIVE_NEVER;
  269. else
  270. set = KAL_TIMEOUT * 32;
  271. REG_WRITE(ah, AR_WOW_KEEP_ALIVE_TIMO, set);
  272. /*
  273. * Keep alive delay in us. based on 'power on clock',
  274. * therefore in usec
  275. */
  276. set = KAL_DELAY * 1000;
  277. REG_WRITE(ah, AR_WOW_KEEP_ALIVE_DELAY, set);
  278. /*
  279. * Create keep alive pattern to respond to beacons
  280. */
  281. ath9k_wow_create_keep_alive_pattern(ah);
  282. /*
  283. * Configure MAC WoW Registers
  284. */
  285. set = 0;
  286. /* Send keep alive timeouts anyway */
  287. clr = AR_WOW_KEEP_ALIVE_AUTO_DIS;
  288. if (pattern_enable & AH_WOW_LINK_CHANGE)
  289. wow_event_mask |= AR_WOW_KEEP_ALIVE_FAIL;
  290. else
  291. set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
  292. set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
  293. REG_RMW(ah, AR_WOW_KEEP_ALIVE, set, clr);
  294. /*
  295. * we are relying on a bmiss failure. ensure we have
  296. * enough threshold to prevent false positives
  297. */
  298. REG_RMW_FIELD(ah, AR_RSSI_THR, AR_RSSI_THR_BM_THR,
  299. AR_WOW_BMISSTHRESHOLD);
  300. set = 0;
  301. clr = 0;
  302. if (pattern_enable & AH_WOW_BEACON_MISS) {
  303. set = AR_WOW_BEACON_FAIL_EN;
  304. wow_event_mask |= AR_WOW_BEACON_FAIL;
  305. } else {
  306. clr = AR_WOW_BEACON_FAIL_EN;
  307. }
  308. REG_RMW(ah, AR_WOW_BCN_EN, set, clr);
  309. set = 0;
  310. clr = 0;
  311. /*
  312. * Enable the magic packet registers
  313. */
  314. if (pattern_enable & AH_WOW_MAGIC_PATTERN_EN) {
  315. set = AR_WOW_MAGIC_EN;
  316. wow_event_mask |= AR_WOW_MAGIC_PAT_FOUND;
  317. } else {
  318. clr = AR_WOW_MAGIC_EN;
  319. }
  320. set |= AR_WOW_MAC_INTR_EN;
  321. REG_RMW(ah, AR_WOW_PATTERN, set, clr);
  322. REG_WRITE(ah, AR_WOW_PATTERN_MATCH_LT_256B,
  323. AR_WOW_PATTERN_SUPPORTED);
  324. /*
  325. * Set the power states appropriately and enable PME
  326. */
  327. clr = 0;
  328. set = AR_PMCTRL_PWR_STATE_D1D3 | AR_PMCTRL_HOST_PME_EN |
  329. AR_PMCTRL_PWR_PM_CTRL_ENA;
  330. clr = AR_PCIE_PM_CTRL_ENA;
  331. REG_RMW(ah, AR_PCIE_PM_CTRL, set, clr);
  332. /*
  333. * this is needed to prevent the chip waking up
  334. * the host within 3-4 seconds with certain
  335. * platform/BIOS. The fix is to enable
  336. * D1 & D3 to match original definition and
  337. * also match the OTP value. Anyway this
  338. * is more related to SW WOW.
  339. */
  340. clr = AR_PMCTRL_PWR_STATE_D1D3;
  341. REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
  342. set = AR_PMCTRL_PWR_STATE_D1D3_REAL;
  343. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
  344. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
  345. /* to bring down WOW power low margin */
  346. set = BIT(13);
  347. REG_SET_BIT(ah, AR_PCIE_PHY_REG3, set);
  348. /* HW WoW */
  349. clr = BIT(5);
  350. REG_CLR_BIT(ah, AR_PCU_MISC_MODE3, clr);
  351. ath9k_hw_set_powermode_wow_sleep(ah);
  352. ah->wow_event_mask = wow_event_mask;
  353. }
  354. EXPORT_SYMBOL(ath9k_hw_wow_enable);