xref: /freebsd/sys/dev/ath/ath_hal/ar5211/ar5211_power.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5  * Copyright (c) 2002-2006 Atheros Communications, Inc.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #include "opt_ah.h"
20 
21 #include "ah.h"
22 #include "ah_internal.h"
23 
24 #include "ar5211/ar5211.h"
25 #include "ar5211/ar5211reg.h"
26 #include "ar5211/ar5211desc.h"
27 
28 /*
29  * Notify Power Mgt is enabled in self-generated frames.
30  * If requested, force chip awake.
31  *
32  * Returns A_OK if chip is awake or successfully forced awake.
33  *
34  * WARNING WARNING WARNING
35  * There is a problem with the chip where sometimes it will not wake up.
36  */
37 static HAL_BOOL
38 ar5211SetPowerModeAwake(struct ath_hal *ah, int setChip)
39 {
40 #define	POWER_UP_TIME	2000
41 	uint32_t val;
42 	int i;
43 
44 	if (setChip) {
45 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_WAKE);
46 		OS_DELAY(10);	/* Give chip the chance to awake */
47 
48 		for (i = POWER_UP_TIME / 200; i != 0; i--) {
49 			val = OS_REG_READ(ah, AR_PCICFG);
50 			if ((val & AR_PCICFG_SPWR_DN) == 0)
51 				break;
52 			OS_DELAY(200);
53 			OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE,
54 				AR_SCR_SLE_WAKE);
55 		}
56 		if (i == 0) {
57 #ifdef AH_DEBUG
58 			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
59 				__func__, POWER_UP_TIME/20);
60 #endif
61 			return AH_FALSE;
62 		}
63 	}
64 
65 	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
66 	return AH_TRUE;
67 #undef POWER_UP_TIME
68 }
69 
70 /*
71  * Notify Power Mgt is disabled in self-generated frames.
72  * If requested, force chip to sleep.
73  */
74 static void
75 ar5211SetPowerModeSleep(struct ath_hal *ah, int setChip)
76 {
77 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
78 	if (setChip)
79 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_SLP);
80 }
81 
82 /*
83  * Notify Power Management is enabled in self-generating
84  * fames.  If request, set power mode of chip to
85  * auto/normal.  Duration in units of 128us (1/8 TU).
86  */
87 static void
88 ar5211SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
89 {
90 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
91 	if (setChip)
92 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_NORM);
93 }
94 
95 HAL_BOOL
96 ar5211SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
97 {
98 #ifdef AH_DEBUG
99 	static const char* modes[] = {
100 		"AWAKE",
101 		"FULL-SLEEP",
102 		"NETWORK SLEEP",
103 		"UNDEFINED"
104 	};
105 #endif
106 	int status = AH_TRUE;
107 
108 	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
109 		modes[ah->ah_powerMode], modes[mode],
110 		setChip ? "set chip " : "");
111 	switch (mode) {
112 	case HAL_PM_AWAKE:
113 		if (setChip)
114 			ah->ah_powerMode = mode;
115 		status = ar5211SetPowerModeAwake(ah, setChip);
116 		break;
117 	case HAL_PM_FULL_SLEEP:
118 		ar5211SetPowerModeSleep(ah, setChip);
119 		if (setChip)
120 			ah->ah_powerMode = mode;
121 		break;
122 	case HAL_PM_NETWORK_SLEEP:
123 		ar5211SetPowerModeNetworkSleep(ah, setChip);
124 		if (setChip)
125 			ah->ah_powerMode = mode;
126 		break;
127 	default:
128 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode %u\n",
129 		    __func__, mode);
130 		return AH_FALSE;
131 	}
132 	return status;
133 }
134 
135 HAL_POWER_MODE
136 ar5211GetPowerMode(struct ath_hal *ah)
137 {
138 	/* Just so happens the h/w maps directly to the abstracted value */
139 	return MS(OS_REG_READ(ah, AR_SCR), AR_SCR_SLE);
140 }
141