xref: /freebsd/sys/dev/ath/ath_hal/ar5416/ar5416_power.c (revision b1d046441de9053152c7cf03d6b60d9882687e1b)
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD$
18  */
19 #include "opt_ah.h"
20 
21 #include "ah.h"
22 #include "ah_internal.h"
23 
24 #include "ar5416/ar5416.h"
25 #include "ar5416/ar5416reg.h"
26 
27 /*
28  * Notify Power Mgt is enabled in self-generated frames.
29  * If requested, force chip awake.
30  *
31  * Returns A_OK if chip is awake or successfully forced awake.
32  *
33  * WARNING WARNING WARNING
34  * There is a problem with the chip where sometimes it will not wake up.
35  */
36 static HAL_BOOL
37 ar5416SetPowerModeAwake(struct ath_hal *ah, int setChip)
38 {
39 #define	POWER_UP_TIME	200000
40 	uint32_t val;
41 	int i = 0;
42 
43 	if (setChip) {
44 		/*
45 		 * Do a Power-On-Reset if OWL is shutdown
46 		 * the NetBSD driver  power-cycles the Cardbus slot
47 		 * as part of the reset procedure.
48 		 */
49 		if ((OS_REG_READ(ah, AR_RTC_STATUS)
50 			& AR_RTC_PM_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
51 			if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON))
52 				goto bad;
53 		}
54 
55 		if (AR_SREV_HOWL(ah))
56 			OS_REG_SET_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
57 
58 		OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
59 		if (AR_SREV_HOWL(ah))
60 			OS_DELAY(10000);
61 		else
62 			OS_DELAY(50);   /* Give chip the chance to awake */
63 
64 		for (i = POWER_UP_TIME / 50; i != 0; i--) {
65 			val = OS_REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
66 			if (val == AR_RTC_STATUS_ON)
67 				break;
68 			OS_DELAY(50);
69 			OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
70 		}
71 	bad:
72 		if (i == 0) {
73 #ifdef AH_DEBUG
74 			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
75 				__func__, POWER_UP_TIME/1000);
76 #endif
77 			return AH_FALSE;
78 		}
79 	}
80 
81 	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
82 	return AH_TRUE;
83 #undef POWER_UP_TIME
84 }
85 
86 /*
87  * Notify Power Mgt is disabled in self-generated frames.
88  * If requested, force chip to sleep.
89  */
90 static void
91 ar5416SetPowerModeSleep(struct ath_hal *ah, int setChip)
92 {
93 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
94 	if (setChip) {
95 		/* Clear the RTC force wake bit to allow the mac to sleep */
96 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
97 		if (! AR_SREV_HOWL(ah))
98 			OS_REG_WRITE(ah, AR_RC, AR_RC_AHB|AR_RC_HOSTIF);
99 		/* Shutdown chip. Active low */
100 		if (! AR_SREV_OWL(ah))
101 			OS_REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
102 	}
103 }
104 
105 /*
106  * Notify Power Management is enabled in self-generating
107  * fames.  If request, set power mode of chip to
108  * auto/normal.  Duration in units of 128us (1/8 TU).
109  */
110 static void
111 ar5416SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
112 {
113 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
114 
115 	if (setChip)
116 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
117 }
118 
119 /*
120  * Set power mgt to the requested mode, and conditionally set
121  * the chip as well
122  */
123 HAL_BOOL
124 ar5416SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
125 {
126 	struct ath_hal_5212 *ahp = AH5212(ah);
127 #ifdef AH_DEBUG
128 	static const char* modes[] = {
129 		"AWAKE",
130 		"FULL-SLEEP",
131 		"NETWORK SLEEP",
132 		"UNDEFINED"
133 	};
134 #endif
135 	int status = AH_TRUE;
136 	if (!setChip)
137 		return AH_TRUE;
138 
139 	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
140 	    modes[ahp->ah_powerMode], modes[mode], setChip ? "set chip " : "");
141 	switch (mode) {
142 	case HAL_PM_AWAKE:
143 		status = ar5416SetPowerModeAwake(ah, setChip);
144 		break;
145 	case HAL_PM_FULL_SLEEP:
146 		ar5416SetPowerModeSleep(ah, setChip);
147 		break;
148 	case HAL_PM_NETWORK_SLEEP:
149 		ar5416SetPowerModeNetworkSleep(ah, setChip);
150 		break;
151 	default:
152 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode 0x%x\n",
153 		    __func__, mode);
154 		return AH_FALSE;
155 	}
156 	ahp->ah_powerMode = mode;
157 	return status;
158 }
159 
160 /*
161  * Return the current sleep mode of the chip
162  */
163 HAL_POWER_MODE
164 ar5416GetPowerMode(struct ath_hal *ah)
165 {
166 	int mode = OS_REG_READ(ah, AR_RTC_STATUS);
167 	switch (mode & AR_RTC_PM_STATUS_M) {
168 	case AR_RTC_STATUS_ON:
169 	case AR_RTC_STATUS_WAKEUP:
170 		return HAL_PM_AWAKE;
171 	case AR_RTC_STATUS_SLEEP:
172 		return HAL_PM_NETWORK_SLEEP;
173 	case AR_RTC_STATUS_SHUTDOWN:
174 		return HAL_PM_FULL_SLEEP;
175 	default:
176 		HALDEBUG(ah, HAL_DEBUG_ANY,
177 		    "%s: unknown power mode, RTC_STATUS 0x%x\n",
178 		    __func__, mode);
179 		return HAL_PM_UNDEFINED;
180 	}
181 }
182