xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar5416/ar5416_power.c (revision d98a0bcf56cb1796ee67614a3ca0c005424745ef)
1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4572ff6f6SMatthew Dillon  *
5572ff6f6SMatthew Dillon  * Permission to use, copy, modify, and/or distribute this software for any
6572ff6f6SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7572ff6f6SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8572ff6f6SMatthew Dillon  *
9572ff6f6SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10572ff6f6SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11572ff6f6SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12572ff6f6SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13572ff6f6SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14572ff6f6SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15572ff6f6SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16572ff6f6SMatthew Dillon  *
17572ff6f6SMatthew Dillon  * $FreeBSD$
18572ff6f6SMatthew Dillon  */
19572ff6f6SMatthew Dillon #include "opt_ah.h"
20572ff6f6SMatthew Dillon 
21572ff6f6SMatthew Dillon #include "ah.h"
22572ff6f6SMatthew Dillon #include "ah_internal.h"
23572ff6f6SMatthew Dillon 
24572ff6f6SMatthew Dillon #include "ar5416/ar5416.h"
25572ff6f6SMatthew Dillon #include "ar5416/ar5416reg.h"
26572ff6f6SMatthew Dillon 
27572ff6f6SMatthew Dillon /*
28572ff6f6SMatthew Dillon  * Notify Power Mgt is enabled in self-generated frames.
29572ff6f6SMatthew Dillon  * If requested, force chip awake.
30572ff6f6SMatthew Dillon  *
31572ff6f6SMatthew Dillon  * Returns A_OK if chip is awake or successfully forced awake.
32572ff6f6SMatthew Dillon  *
33572ff6f6SMatthew Dillon  * WARNING WARNING WARNING
34572ff6f6SMatthew Dillon  * There is a problem with the chip where sometimes it will not wake up.
35572ff6f6SMatthew Dillon  */
36572ff6f6SMatthew Dillon static HAL_BOOL
ar5416SetPowerModeAwake(struct ath_hal * ah,int setChip)37572ff6f6SMatthew Dillon ar5416SetPowerModeAwake(struct ath_hal *ah, int setChip)
38572ff6f6SMatthew Dillon {
39572ff6f6SMatthew Dillon #define	POWER_UP_TIME	200000
40572ff6f6SMatthew Dillon 	uint32_t val;
41572ff6f6SMatthew Dillon 	int i = 0;
42572ff6f6SMatthew Dillon 
43572ff6f6SMatthew Dillon 	if (setChip) {
44572ff6f6SMatthew Dillon 		/*
45572ff6f6SMatthew Dillon 		 * Do a Power-On-Reset if OWL is shutdown
46572ff6f6SMatthew Dillon 		 * the NetBSD driver  power-cycles the Cardbus slot
47572ff6f6SMatthew Dillon 		 * as part of the reset procedure.
48572ff6f6SMatthew Dillon 		 */
49572ff6f6SMatthew Dillon 		if ((OS_REG_READ(ah, AR_RTC_STATUS)
50572ff6f6SMatthew Dillon 			& AR_RTC_PM_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
51572ff6f6SMatthew Dillon 			if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON))
52572ff6f6SMatthew Dillon 				goto bad;
53572ff6f6SMatthew Dillon 			AH5416(ah)->ah_initPLL(ah, AH_NULL);
54572ff6f6SMatthew Dillon 		}
55572ff6f6SMatthew Dillon 
56572ff6f6SMatthew Dillon 		if (AR_SREV_HOWL(ah))
57572ff6f6SMatthew Dillon 			OS_REG_SET_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
58572ff6f6SMatthew Dillon 
59572ff6f6SMatthew Dillon 		OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
60572ff6f6SMatthew Dillon 		if (AR_SREV_HOWL(ah))
61572ff6f6SMatthew Dillon 			OS_DELAY(10000);
62572ff6f6SMatthew Dillon 		else
63572ff6f6SMatthew Dillon 			OS_DELAY(50);   /* Give chip the chance to awake */
64572ff6f6SMatthew Dillon 
65572ff6f6SMatthew Dillon 		for (i = POWER_UP_TIME / 50; i != 0; i--) {
66572ff6f6SMatthew Dillon 			val = OS_REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
67572ff6f6SMatthew Dillon 			if (val == AR_RTC_STATUS_ON)
68572ff6f6SMatthew Dillon 				break;
69572ff6f6SMatthew Dillon 			OS_DELAY(50);
70572ff6f6SMatthew Dillon 			OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
71572ff6f6SMatthew Dillon 		}
72572ff6f6SMatthew Dillon 	bad:
73572ff6f6SMatthew Dillon 		if (i == 0) {
74572ff6f6SMatthew Dillon #ifdef AH_DEBUG
75572ff6f6SMatthew Dillon 			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
76572ff6f6SMatthew Dillon 				__func__, POWER_UP_TIME/1000);
77572ff6f6SMatthew Dillon #endif
78572ff6f6SMatthew Dillon 			return AH_FALSE;
79572ff6f6SMatthew Dillon 		}
80572ff6f6SMatthew Dillon 	}
81572ff6f6SMatthew Dillon 
82572ff6f6SMatthew Dillon 	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
83572ff6f6SMatthew Dillon 	return AH_TRUE;
84572ff6f6SMatthew Dillon #undef POWER_UP_TIME
85572ff6f6SMatthew Dillon }
86572ff6f6SMatthew Dillon 
87572ff6f6SMatthew Dillon /*
88572ff6f6SMatthew Dillon  * Notify Power Mgt is disabled in self-generated frames.
89572ff6f6SMatthew Dillon  * If requested, force chip to sleep.
90572ff6f6SMatthew Dillon  */
91572ff6f6SMatthew Dillon static void
ar5416SetPowerModeSleep(struct ath_hal * ah,int setChip)92572ff6f6SMatthew Dillon ar5416SetPowerModeSleep(struct ath_hal *ah, int setChip)
93572ff6f6SMatthew Dillon {
94572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
95572ff6f6SMatthew Dillon 	if (setChip) {
96572ff6f6SMatthew Dillon 		/* Clear the RTC force wake bit to allow the mac to sleep */
97572ff6f6SMatthew Dillon 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
98572ff6f6SMatthew Dillon 		if (! AR_SREV_HOWL(ah))
99572ff6f6SMatthew Dillon 			OS_REG_WRITE(ah, AR_RC, AR_RC_AHB|AR_RC_HOSTIF);
100572ff6f6SMatthew Dillon 		/* Shutdown chip. Active low */
101572ff6f6SMatthew Dillon 		if (! AR_SREV_OWL(ah))
102572ff6f6SMatthew Dillon 			OS_REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
103572ff6f6SMatthew Dillon 	}
104572ff6f6SMatthew Dillon }
105572ff6f6SMatthew Dillon 
106572ff6f6SMatthew Dillon /*
107572ff6f6SMatthew Dillon  * Notify Power Management is enabled in self-generating
108572ff6f6SMatthew Dillon  * fames.  If request, set power mode of chip to
109572ff6f6SMatthew Dillon  * auto/normal.  Duration in units of 128us (1/8 TU).
110572ff6f6SMatthew Dillon  */
111572ff6f6SMatthew Dillon static void
ar5416SetPowerModeNetworkSleep(struct ath_hal * ah,int setChip)112572ff6f6SMatthew Dillon ar5416SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
113572ff6f6SMatthew Dillon {
114572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
115572ff6f6SMatthew Dillon 
116572ff6f6SMatthew Dillon 	if (setChip)
117572ff6f6SMatthew Dillon 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
118572ff6f6SMatthew Dillon }
119572ff6f6SMatthew Dillon 
120572ff6f6SMatthew Dillon /*
121572ff6f6SMatthew Dillon  * Set power mgt to the requested mode, and conditionally set
122572ff6f6SMatthew Dillon  * the chip as well
123572ff6f6SMatthew Dillon  */
124572ff6f6SMatthew Dillon HAL_BOOL
ar5416SetPowerMode(struct ath_hal * ah,HAL_POWER_MODE mode,int setChip)125572ff6f6SMatthew Dillon ar5416SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
126572ff6f6SMatthew Dillon {
127572ff6f6SMatthew Dillon #ifdef AH_DEBUG
128572ff6f6SMatthew Dillon 	static const char* modes[] = {
129572ff6f6SMatthew Dillon 		"AWAKE",
130572ff6f6SMatthew Dillon 		"FULL-SLEEP",
131572ff6f6SMatthew Dillon 		"NETWORK SLEEP",
132572ff6f6SMatthew Dillon 		"UNDEFINED"
133572ff6f6SMatthew Dillon 	};
134572ff6f6SMatthew Dillon #endif
135572ff6f6SMatthew Dillon 	int status = AH_TRUE;
136*d98a0bcfSMatthew Dillon 
137*d98a0bcfSMatthew Dillon #if 0
138572ff6f6SMatthew Dillon 	if (!setChip)
139572ff6f6SMatthew Dillon 		return AH_TRUE;
140*d98a0bcfSMatthew Dillon #endif
141572ff6f6SMatthew Dillon 
142572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
143*d98a0bcfSMatthew Dillon 	    modes[ah->ah_powerMode], modes[mode], setChip ? "set chip " : "");
144572ff6f6SMatthew Dillon 	switch (mode) {
145572ff6f6SMatthew Dillon 	case HAL_PM_AWAKE:
146*d98a0bcfSMatthew Dillon 		if (setChip)
147*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
148572ff6f6SMatthew Dillon 		status = ar5416SetPowerModeAwake(ah, setChip);
149572ff6f6SMatthew Dillon 		break;
150572ff6f6SMatthew Dillon 	case HAL_PM_FULL_SLEEP:
151572ff6f6SMatthew Dillon 		ar5416SetPowerModeSleep(ah, setChip);
152*d98a0bcfSMatthew Dillon 		if (setChip)
153*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
154572ff6f6SMatthew Dillon 		break;
155572ff6f6SMatthew Dillon 	case HAL_PM_NETWORK_SLEEP:
156572ff6f6SMatthew Dillon 		ar5416SetPowerModeNetworkSleep(ah, setChip);
157*d98a0bcfSMatthew Dillon 		if (setChip)
158*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
159572ff6f6SMatthew Dillon 		break;
160572ff6f6SMatthew Dillon 	default:
161572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode 0x%x\n",
162572ff6f6SMatthew Dillon 		    __func__, mode);
163572ff6f6SMatthew Dillon 		return AH_FALSE;
164572ff6f6SMatthew Dillon 	}
165572ff6f6SMatthew Dillon 	return status;
166572ff6f6SMatthew Dillon }
167572ff6f6SMatthew Dillon 
168572ff6f6SMatthew Dillon /*
169572ff6f6SMatthew Dillon  * Return the current sleep mode of the chip
170572ff6f6SMatthew Dillon  */
171572ff6f6SMatthew Dillon HAL_POWER_MODE
ar5416GetPowerMode(struct ath_hal * ah)172572ff6f6SMatthew Dillon ar5416GetPowerMode(struct ath_hal *ah)
173572ff6f6SMatthew Dillon {
174572ff6f6SMatthew Dillon 	int mode = OS_REG_READ(ah, AR_RTC_STATUS);
175572ff6f6SMatthew Dillon 	switch (mode & AR_RTC_PM_STATUS_M) {
176572ff6f6SMatthew Dillon 	case AR_RTC_STATUS_ON:
177572ff6f6SMatthew Dillon 	case AR_RTC_STATUS_WAKEUP:
178572ff6f6SMatthew Dillon 		return HAL_PM_AWAKE;
179572ff6f6SMatthew Dillon 	case AR_RTC_STATUS_SLEEP:
180572ff6f6SMatthew Dillon 		return HAL_PM_NETWORK_SLEEP;
181572ff6f6SMatthew Dillon 	case AR_RTC_STATUS_SHUTDOWN:
182572ff6f6SMatthew Dillon 		return HAL_PM_FULL_SLEEP;
183572ff6f6SMatthew Dillon 	default:
184572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY,
185572ff6f6SMatthew Dillon 		    "%s: unknown power mode, RTC_STATUS 0x%x\n",
186572ff6f6SMatthew Dillon 		    __func__, mode);
187572ff6f6SMatthew Dillon 		return HAL_PM_UNDEFINED;
188572ff6f6SMatthew Dillon 	}
189572ff6f6SMatthew Dillon }
190