wow.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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_config_serdes_wow_sleep(struct ath_hw *ah)
  34. {
  35. int i;
  36. for (i = 0; i < ah->iniPcieSerdesWow.ia_rows; i++)
  37. REG_WRITE(ah, INI_RA(&ah->iniPcieSerdesWow, i, 0),
  38. INI_RA(&ah->iniPcieSerdesWow, i, 1));
  39. usleep_range(1000, 1500);
  40. }
  41. static void ath9k_hw_set_powermode_wow_sleep(struct ath_hw *ah)
  42. {
  43. struct ath_common *common = ath9k_hw_common(ah);
  44. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  45. /* set rx disable bit */
  46. REG_WRITE(ah, AR_CR, AR_CR_RXD);
  47. if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0, AH_WAIT_TIMEOUT)) {
  48. ath_err(common, "Failed to stop Rx DMA in 10ms AR_CR=0x%08x AR_DIAG_SW=0x%08x\n",
  49. REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
  50. return;
  51. } else {
  52. if (!AR_SREV_9300_20_OR_LATER(ah))
  53. REG_WRITE(ah, AR_RXDP, 0x0);
  54. }
  55. /* AR9280 WoW has sleep issue, do not set it to sleep */
  56. if (AR_SREV_9280_20(ah))
  57. return;
  58. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_ON_INT);
  59. }
  60. static void ath9k_wow_create_keep_alive_pattern(struct ath_hw *ah)
  61. {
  62. struct ath_common *common = ath9k_hw_common(ah);
  63. u8 sta_mac_addr[ETH_ALEN], ap_mac_addr[ETH_ALEN];
  64. u32 ctl[13] = {0};
  65. u32 data_word[KAL_NUM_DATA_WORDS];
  66. u8 i;
  67. u32 wow_ka_data_word0;
  68. memcpy(sta_mac_addr, common->macaddr, ETH_ALEN);
  69. memcpy(ap_mac_addr, common->curbssid, ETH_ALEN);
  70. /* set the transmit buffer */
  71. ctl[0] = (KAL_FRAME_LEN | (MAX_RATE_POWER << 16));
  72. if (!(AR_SREV_9300_20_OR_LATER(ah)))
  73. ctl[0] += (KAL_ANTENNA_MODE << 25);
  74. ctl[1] = 0;
  75. ctl[3] = 0xb; /* OFDM_6M hardware value for this rate */
  76. ctl[4] = 0;
  77. ctl[7] = (ah->txchainmask) << 2;
  78. if (AR_SREV_9300_20_OR_LATER(ah))
  79. ctl[2] = 0xf << 16; /* tx_tries 0 */
  80. else
  81. ctl[2] = 0x7 << 16; /* tx_tries 0 */
  82. for (i = 0; i < KAL_NUM_DESC_WORDS; i++)
  83. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
  84. /* for AR9300 family 13 descriptor words */
  85. if (AR_SREV_9300_20_OR_LATER(ah))
  86. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + i * 4), ctl[i]);
  87. data_word[0] = (KAL_FRAME_TYPE << 2) | (KAL_FRAME_SUB_TYPE << 4) |
  88. (KAL_TO_DS << 8) | (KAL_DURATION_ID << 16);
  89. data_word[1] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
  90. (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
  91. data_word[2] = (sta_mac_addr[1] << 24) | (sta_mac_addr[0] << 16) |
  92. (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
  93. data_word[3] = (sta_mac_addr[5] << 24) | (sta_mac_addr[4] << 16) |
  94. (sta_mac_addr[3] << 8) | (sta_mac_addr[2]);
  95. data_word[4] = (ap_mac_addr[3] << 24) | (ap_mac_addr[2] << 16) |
  96. (ap_mac_addr[1] << 8) | (ap_mac_addr[0]);
  97. data_word[5] = (ap_mac_addr[5] << 8) | (ap_mac_addr[4]);
  98. if (AR_SREV_9462_20(ah)) {
  99. /* AR9462 2.0 has an extra descriptor word (time based
  100. * discard) compared to other chips */
  101. REG_WRITE(ah, (AR_WOW_KA_DESC_WORD2 + (12 * 4)), 0);
  102. wow_ka_data_word0 = AR_WOW_TXBUF(13);
  103. } else {
  104. wow_ka_data_word0 = AR_WOW_TXBUF(12);
  105. }
  106. for (i = 0; i < KAL_NUM_DATA_WORDS; i++)
  107. REG_WRITE(ah, (wow_ka_data_word0 + i*4), data_word[i]);
  108. }
  109. void ath9k_hw_wow_apply_pattern(struct ath_hw *ah, u8 *user_pattern,
  110. u8 *user_mask, int pattern_count,
  111. int pattern_len)
  112. {
  113. int i;
  114. u32 pattern_val, mask_val;
  115. u32 set, clr;
  116. /* FIXME: should check count by querying the hardware capability */
  117. if (pattern_count >= MAX_NUM_PATTERN)
  118. return;
  119. REG_SET_BIT(ah, AR_WOW_PATTERN, BIT(pattern_count));
  120. /* set the registers for pattern */
  121. for (i = 0; i < MAX_PATTERN_SIZE; i += 4) {
  122. memcpy(&pattern_val, user_pattern, 4);
  123. REG_WRITE(ah, (AR_WOW_TB_PATTERN(pattern_count) + i),
  124. pattern_val);
  125. user_pattern += 4;
  126. }
  127. /* set the registers for mask */
  128. for (i = 0; i < MAX_PATTERN_MASK_SIZE; i += 4) {
  129. memcpy(&mask_val, user_mask, 4);
  130. REG_WRITE(ah, (AR_WOW_TB_MASK(pattern_count) + i), mask_val);
  131. user_mask += 4;
  132. }
  133. /* set the pattern length to be matched
  134. *
  135. * AR_WOW_LENGTH1_REG1
  136. * bit 31:24 pattern 0 length
  137. * bit 23:16 pattern 1 length
  138. * bit 15:8 pattern 2 length
  139. * bit 7:0 pattern 3 length
  140. *
  141. * AR_WOW_LENGTH1_REG2
  142. * bit 31:24 pattern 4 length
  143. * bit 23:16 pattern 5 length
  144. * bit 15:8 pattern 6 length
  145. * bit 7:0 pattern 7 length
  146. *
  147. * the below logic writes out the new
  148. * pattern length for the corresponding
  149. * pattern_count, while masking out the
  150. * other fields
  151. */
  152. ah->wow_event_mask |= BIT(pattern_count + AR_WOW_PAT_FOUND_SHIFT);
  153. if (!AR_SREV_9285_12_OR_LATER(ah))
  154. return;
  155. if (pattern_count < 4) {
  156. /* Pattern 0-3 uses AR_WOW_LENGTH1 register */
  157. set = (pattern_len & AR_WOW_LENGTH_MAX) <<
  158. AR_WOW_LEN1_SHIFT(pattern_count);
  159. clr = AR_WOW_LENGTH1_MASK(pattern_count);
  160. REG_RMW(ah, AR_WOW_LENGTH1, set, clr);
  161. } else {
  162. /* Pattern 4-7 uses AR_WOW_LENGTH2 register */
  163. set = (pattern_len & AR_WOW_LENGTH_MAX) <<
  164. AR_WOW_LEN2_SHIFT(pattern_count);
  165. clr = AR_WOW_LENGTH2_MASK(pattern_count);
  166. REG_RMW(ah, AR_WOW_LENGTH2, set, clr);
  167. }
  168. }
  169. EXPORT_SYMBOL(ath9k_hw_wow_apply_pattern);
  170. u32 ath9k_hw_wow_wakeup(struct ath_hw *ah)
  171. {
  172. u32 wow_status = 0;
  173. u32 val = 0, rval;
  174. /*
  175. * read the WoW status register to know
  176. * the wakeup reason
  177. */
  178. rval = REG_READ(ah, AR_WOW_PATTERN);
  179. val = AR_WOW_STATUS(rval);
  180. /*
  181. * mask only the WoW events that we have enabled. Sometimes
  182. * we have spurious WoW events from the AR_WOW_PATTERN
  183. * register. This mask will clean it up.
  184. */
  185. val &= ah->wow_event_mask;
  186. if (val) {
  187. if (val & AR_WOW_MAGIC_PAT_FOUND)
  188. wow_status |= AH_WOW_MAGIC_PATTERN_EN;
  189. if (AR_WOW_PATTERN_FOUND(val))
  190. wow_status |= AH_WOW_USER_PATTERN_EN;
  191. if (val & AR_WOW_KEEP_ALIVE_FAIL)
  192. wow_status |= AH_WOW_LINK_CHANGE;
  193. if (val & AR_WOW_BEACON_FAIL)
  194. wow_status |= AH_WOW_BEACON_MISS;
  195. }
  196. /*
  197. * set and clear WOW_PME_CLEAR registers for the chip to
  198. * generate next wow signal.
  199. * disable D3 before accessing other registers ?
  200. */
  201. /* do we need to check the bit value 0x01000000 (7-10) ?? */
  202. REG_RMW(ah, AR_PCIE_PM_CTRL, AR_PMCTRL_WOW_PME_CLR,
  203. AR_PMCTRL_PWR_STATE_D1D3);
  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. * tie reset register for AR9002 family of chipsets
  211. * NB: not tieing it back might have some repurcussions.
  212. */
  213. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  214. REG_SET_BIT(ah, AR_WA, AR_WA_UNTIE_RESET_EN |
  215. AR_WA_POR_SHORT | AR_WA_RESET_EN);
  216. }
  217. /*
  218. * restore the beacon threshold to init value
  219. */
  220. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  221. /*
  222. * Restore the way the PCI-E reset, Power-On-Reset, external
  223. * PCIE_POR_SHORT pins are tied to its original value.
  224. * Previously just before WoW sleep, we untie the PCI-E
  225. * reset to our Chip's Power On Reset so that any PCI-E
  226. * reset from the bus will not reset our chip
  227. */
  228. if (AR_SREV_9280_20_OR_LATER(ah) && ah->is_pciexpress)
  229. ath9k_hw_configpcipowersave(ah, false);
  230. ah->wow_event_mask = 0;
  231. return wow_status;
  232. }
  233. EXPORT_SYMBOL(ath9k_hw_wow_wakeup);
  234. void ath9k_hw_wow_enable(struct ath_hw *ah, u32 pattern_enable)
  235. {
  236. u32 wow_event_mask;
  237. u32 set, clr;
  238. /*
  239. * wow_event_mask is a mask to the AR_WOW_PATTERN register to
  240. * indicate which WoW events we have enabled. The WoW events
  241. * are from the 'pattern_enable' in this function and
  242. * 'pattern_count' of ath9k_hw_wow_apply_pattern()
  243. */
  244. wow_event_mask = ah->wow_event_mask;
  245. /*
  246. * Untie Power-on-Reset from the PCI-E-Reset. When we are in
  247. * WOW sleep, we do want the Reset from the PCI-E to disturb
  248. * our hw state
  249. */
  250. if (ah->is_pciexpress) {
  251. /*
  252. * we need to untie the internal POR (power-on-reset)
  253. * to the external PCI-E reset. We also need to tie
  254. * the PCI-E Phy reset to the PCI-E reset.
  255. */
  256. if (AR_SREV_9300_20_OR_LATER(ah)) {
  257. set = AR_WA_RESET_EN | AR_WA_POR_SHORT;
  258. clr = AR_WA_UNTIE_RESET_EN | AR_WA_D3_L1_DISABLE;
  259. REG_RMW(ah, AR_WA, set, clr);
  260. } else {
  261. if (AR_SREV_9285(ah) || AR_SREV_9287(ah))
  262. set = AR9285_WA_DEFAULT;
  263. else
  264. set = AR9280_WA_DEFAULT;
  265. /*
  266. * In AR9280 and AR9285, bit 14 in WA register
  267. * (disable L1) should only be set when device
  268. * enters D3 state and be cleared when device
  269. * comes back to D0
  270. */
  271. if (ah->config.pcie_waen & AR_WA_D3_L1_DISABLE)
  272. set |= AR_WA_D3_L1_DISABLE;
  273. clr = AR_WA_UNTIE_RESET_EN;
  274. set |= AR_WA_RESET_EN | AR_WA_POR_SHORT;
  275. REG_RMW(ah, AR_WA, set, clr);
  276. /*
  277. * for WoW sleep, we reprogram the SerDes so that the
  278. * PLL and CLK REQ are both enabled. This uses more
  279. * power but otherwise WoW sleep is unstable and the
  280. * chip may disappear.
  281. */
  282. if (AR_SREV_9285_12_OR_LATER(ah))
  283. ath9k_hw_config_serdes_wow_sleep(ah);
  284. }
  285. }
  286. /*
  287. * set the power states appropriately and enable PME
  288. */
  289. set = AR_PMCTRL_HOST_PME_EN | AR_PMCTRL_PWR_PM_CTRL_ENA |
  290. AR_PMCTRL_AUX_PWR_DET | AR_PMCTRL_WOW_PME_CLR;
  291. /*
  292. * set and clear WOW_PME_CLEAR registers for the chip
  293. * to generate next wow signal.
  294. */
  295. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
  296. clr = AR_PMCTRL_WOW_PME_CLR;
  297. REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
  298. /*
  299. * Setup for:
  300. * - beacon misses
  301. * - magic pattern
  302. * - keep alive timeout
  303. * - pattern matching
  304. */
  305. /*
  306. * Program default values for pattern backoff, aifs/slot/KAL count,
  307. * beacon miss timeout, KAL timeout, etc.
  308. */
  309. set = AR_WOW_BACK_OFF_SHIFT(AR_WOW_PAT_BACKOFF);
  310. REG_SET_BIT(ah, AR_WOW_PATTERN, set);
  311. set = AR_WOW_AIFS_CNT(AR_WOW_CNT_AIFS_CNT) |
  312. AR_WOW_SLOT_CNT(AR_WOW_CNT_SLOT_CNT) |
  313. AR_WOW_KEEP_ALIVE_CNT(AR_WOW_CNT_KA_CNT);
  314. REG_SET_BIT(ah, AR_WOW_COUNT, set);
  315. if (pattern_enable & AH_WOW_BEACON_MISS)
  316. set = AR_WOW_BEACON_TIMO;
  317. /* We are not using beacon miss, program a large value */
  318. else
  319. set = AR_WOW_BEACON_TIMO_MAX;
  320. REG_WRITE(ah, AR_WOW_BCN_TIMO, set);
  321. /*
  322. * Keep alive timo in ms except AR9280
  323. */
  324. if (!pattern_enable || AR_SREV_9280(ah))
  325. set = AR_WOW_KEEP_ALIVE_NEVER;
  326. else
  327. set = KAL_TIMEOUT * 32;
  328. REG_WRITE(ah, AR_WOW_KEEP_ALIVE_TIMO, set);
  329. /*
  330. * Keep alive delay in us. based on 'power on clock',
  331. * therefore in usec
  332. */
  333. set = KAL_DELAY * 1000;
  334. REG_WRITE(ah, AR_WOW_KEEP_ALIVE_DELAY, set);
  335. /*
  336. * Create keep alive pattern to respond to beacons
  337. */
  338. ath9k_wow_create_keep_alive_pattern(ah);
  339. /*
  340. * Configure MAC WoW Registers
  341. */
  342. set = 0;
  343. /* Send keep alive timeouts anyway */
  344. clr = AR_WOW_KEEP_ALIVE_AUTO_DIS;
  345. if (pattern_enable & AH_WOW_LINK_CHANGE)
  346. wow_event_mask |= AR_WOW_KEEP_ALIVE_FAIL;
  347. else
  348. set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
  349. /*
  350. * FIXME: For now disable keep alive frame
  351. * failure. This seems to sometimes trigger
  352. * unnecessary wake up with AR9485 chipsets.
  353. */
  354. set = AR_WOW_KEEP_ALIVE_FAIL_DIS;
  355. REG_RMW(ah, AR_WOW_KEEP_ALIVE, set, clr);
  356. /*
  357. * we are relying on a bmiss failure. ensure we have
  358. * enough threshold to prevent false positives
  359. */
  360. REG_RMW_FIELD(ah, AR_RSSI_THR, AR_RSSI_THR_BM_THR,
  361. AR_WOW_BMISSTHRESHOLD);
  362. set = 0;
  363. clr = 0;
  364. if (pattern_enable & AH_WOW_BEACON_MISS) {
  365. set = AR_WOW_BEACON_FAIL_EN;
  366. wow_event_mask |= AR_WOW_BEACON_FAIL;
  367. } else {
  368. clr = AR_WOW_BEACON_FAIL_EN;
  369. }
  370. REG_RMW(ah, AR_WOW_BCN_EN, set, clr);
  371. set = 0;
  372. clr = 0;
  373. /*
  374. * Enable the magic packet registers
  375. */
  376. if (pattern_enable & AH_WOW_MAGIC_PATTERN_EN) {
  377. set = AR_WOW_MAGIC_EN;
  378. wow_event_mask |= AR_WOW_MAGIC_PAT_FOUND;
  379. } else {
  380. clr = AR_WOW_MAGIC_EN;
  381. }
  382. set |= AR_WOW_MAC_INTR_EN;
  383. REG_RMW(ah, AR_WOW_PATTERN, set, clr);
  384. /*
  385. * For AR9285 and later version of chipsets
  386. * enable WoW pattern match for packets less
  387. * than 256 bytes for all patterns
  388. */
  389. if (AR_SREV_9285_12_OR_LATER(ah))
  390. REG_WRITE(ah, AR_WOW_PATTERN_MATCH_LT_256B,
  391. AR_WOW_PATTERN_SUPPORTED);
  392. /*
  393. * Set the power states appropriately and enable PME
  394. */
  395. clr = 0;
  396. set = AR_PMCTRL_PWR_STATE_D1D3 | AR_PMCTRL_HOST_PME_EN |
  397. AR_PMCTRL_PWR_PM_CTRL_ENA;
  398. /*
  399. * This is needed for AR9300 chipsets to wake-up
  400. * the host.
  401. */
  402. if (AR_SREV_9300_20_OR_LATER(ah))
  403. clr = AR_PCIE_PM_CTRL_ENA;
  404. REG_RMW(ah, AR_PCIE_PM_CTRL, set, clr);
  405. if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
  406. /*
  407. * this is needed to prevent the chip waking up
  408. * the host within 3-4 seconds with certain
  409. * platform/BIOS. The fix is to enable
  410. * D1 & D3 to match original definition and
  411. * also match the OTP value. Anyway this
  412. * is more related to SW WOW.
  413. */
  414. clr = AR_PMCTRL_PWR_STATE_D1D3;
  415. REG_CLR_BIT(ah, AR_PCIE_PM_CTRL, clr);
  416. set = AR_PMCTRL_PWR_STATE_D1D3_REAL;
  417. REG_SET_BIT(ah, AR_PCIE_PM_CTRL, set);
  418. }
  419. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
  420. if (AR_SREV_9300_20_OR_LATER(ah)) {
  421. /* to bring down WOW power low margin */
  422. set = BIT(13);
  423. REG_SET_BIT(ah, AR_PCIE_PHY_REG3, set);
  424. /* HW WoW */
  425. clr = BIT(5);
  426. REG_CLR_BIT(ah, AR_PCU_MISC_MODE3, clr);
  427. }
  428. ath9k_hw_set_powermode_wow_sleep(ah);
  429. ah->wow_event_mask = wow_event_mask;
  430. }
  431. EXPORT_SYMBOL(ath9k_hw_wow_enable);