xref: /dflybsd-src/sys/dev/netif/ath/ath_hal/ar5210/ar5210_power.c (revision d98a0bcf56cb1796ee67614a3ca0c005424745ef)
1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * Copyright (c) 2002-2004 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 "ar5210/ar5210.h"
25572ff6f6SMatthew Dillon #include "ar5210/ar5210reg.h"
26572ff6f6SMatthew Dillon 
27572ff6f6SMatthew Dillon /*
28572ff6f6SMatthew Dillon  * Notify Power Mgt is disabled in self-generated frames.
29572ff6f6SMatthew Dillon  * If requested, set Power Mode of chip to auto/normal.
30572ff6f6SMatthew Dillon  */
31572ff6f6SMatthew Dillon static void
ar5210SetPowerModeAuto(struct ath_hal * ah,int setChip)32572ff6f6SMatthew Dillon ar5210SetPowerModeAuto(struct ath_hal *ah, int setChip)
33572ff6f6SMatthew Dillon {
34572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
35572ff6f6SMatthew Dillon 	if (setChip)
36572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_ALLOW);
37572ff6f6SMatthew Dillon }
38572ff6f6SMatthew Dillon 
39572ff6f6SMatthew Dillon /*
40572ff6f6SMatthew Dillon  * Notify Power Mgt is enabled in self-generated frames.
41572ff6f6SMatthew Dillon  * If requested, force chip awake.
42572ff6f6SMatthew Dillon  *
43572ff6f6SMatthew Dillon  * Returns A_OK if chip is awake or successfully forced awake.
44572ff6f6SMatthew Dillon  *
45572ff6f6SMatthew Dillon  * WARNING WARNING WARNING
46572ff6f6SMatthew Dillon  * There is a problem with the chip where sometimes it will not wake up.
47572ff6f6SMatthew Dillon  */
48572ff6f6SMatthew Dillon static HAL_BOOL
ar5210SetPowerModeAwake(struct ath_hal * ah,int setChip)49572ff6f6SMatthew Dillon ar5210SetPowerModeAwake(struct ath_hal *ah, int setChip)
50572ff6f6SMatthew Dillon {
51572ff6f6SMatthew Dillon #define	POWER_UP_TIME	2000
52572ff6f6SMatthew Dillon 	uint32_t val;
53572ff6f6SMatthew Dillon 	int i;
54572ff6f6SMatthew Dillon 
55572ff6f6SMatthew Dillon 	if (setChip) {
56572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_WAKE);
57572ff6f6SMatthew Dillon 		OS_DELAY(2000);	/* Give chip the chance to awake */
58572ff6f6SMatthew Dillon 
59572ff6f6SMatthew Dillon 		for (i = POWER_UP_TIME / 200; i != 0; i--) {
60572ff6f6SMatthew Dillon 			val = OS_REG_READ(ah, AR_PCICFG);
61572ff6f6SMatthew Dillon 			if ((val & AR_PCICFG_SPWR_DN) == 0)
62572ff6f6SMatthew Dillon 				break;
63572ff6f6SMatthew Dillon 			OS_DELAY(200);
64572ff6f6SMatthew Dillon 			OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE,
65572ff6f6SMatthew Dillon 				AR_SCR_SLE_WAKE);
66572ff6f6SMatthew Dillon 		}
67572ff6f6SMatthew Dillon 		if (i == 0) {
68572ff6f6SMatthew Dillon #ifdef AH_DEBUG
69572ff6f6SMatthew Dillon 			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
70572ff6f6SMatthew Dillon 				__func__, POWER_UP_TIME/20);
71572ff6f6SMatthew Dillon #endif
72572ff6f6SMatthew Dillon 			return AH_FALSE;
73572ff6f6SMatthew Dillon 		}
74572ff6f6SMatthew Dillon 	}
75572ff6f6SMatthew Dillon 
76572ff6f6SMatthew Dillon 	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
77572ff6f6SMatthew Dillon 	return AH_TRUE;
78572ff6f6SMatthew Dillon #undef POWER_UP_TIME
79572ff6f6SMatthew Dillon }
80572ff6f6SMatthew Dillon 
81572ff6f6SMatthew Dillon /*
82572ff6f6SMatthew Dillon  * Notify Power Mgt is disabled in self-generated frames.
83572ff6f6SMatthew Dillon  * If requested, force chip to sleep.
84572ff6f6SMatthew Dillon  */
85572ff6f6SMatthew Dillon static void
ar5210SetPowerModeSleep(struct ath_hal * ah,int setChip)86572ff6f6SMatthew Dillon ar5210SetPowerModeSleep(struct ath_hal *ah, int setChip)
87572ff6f6SMatthew Dillon {
88572ff6f6SMatthew Dillon 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
89572ff6f6SMatthew Dillon 	if (setChip)
90572ff6f6SMatthew Dillon 		OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_SLP);
91572ff6f6SMatthew Dillon }
92572ff6f6SMatthew Dillon 
93572ff6f6SMatthew Dillon HAL_BOOL
ar5210SetPowerMode(struct ath_hal * ah,HAL_POWER_MODE mode,int setChip)94572ff6f6SMatthew Dillon ar5210SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
95572ff6f6SMatthew Dillon {
96572ff6f6SMatthew Dillon #ifdef AH_DEBUG
97572ff6f6SMatthew Dillon 	static const char* modes[] = {
98572ff6f6SMatthew Dillon 		"AWAKE",
99572ff6f6SMatthew Dillon 		"FULL-SLEEP",
100572ff6f6SMatthew Dillon 		"NETWORK SLEEP",
101572ff6f6SMatthew Dillon 		"UNDEFINED"
102572ff6f6SMatthew Dillon 	};
103572ff6f6SMatthew Dillon #endif
104572ff6f6SMatthew Dillon 	int status = AH_TRUE;
105572ff6f6SMatthew Dillon 
106572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
107*d98a0bcfSMatthew Dillon 		modes[ah->ah_powerMode], modes[mode],
108572ff6f6SMatthew Dillon 		setChip ? "set chip " : "");
109572ff6f6SMatthew Dillon 	switch (mode) {
110572ff6f6SMatthew Dillon 	case HAL_PM_AWAKE:
111*d98a0bcfSMatthew Dillon 		if (setChip)
112*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
113572ff6f6SMatthew Dillon 		status = ar5210SetPowerModeAwake(ah, setChip);
114572ff6f6SMatthew Dillon 		break;
115572ff6f6SMatthew Dillon 	case HAL_PM_FULL_SLEEP:
116572ff6f6SMatthew Dillon 		ar5210SetPowerModeSleep(ah, setChip);
117*d98a0bcfSMatthew Dillon 		if (setChip)
118*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
119572ff6f6SMatthew Dillon 		break;
120572ff6f6SMatthew Dillon 	case HAL_PM_NETWORK_SLEEP:
121572ff6f6SMatthew Dillon 		ar5210SetPowerModeAuto(ah, setChip);
122*d98a0bcfSMatthew Dillon 		if (setChip)
123*d98a0bcfSMatthew Dillon 			ah->ah_powerMode = mode;
124572ff6f6SMatthew Dillon 		break;
125572ff6f6SMatthew Dillon 	default:
126572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode %u\n",
127572ff6f6SMatthew Dillon 		    __func__, mode);
128572ff6f6SMatthew Dillon 		return AH_FALSE;
129572ff6f6SMatthew Dillon 	}
130572ff6f6SMatthew Dillon 	return status;
131572ff6f6SMatthew Dillon }
132572ff6f6SMatthew Dillon 
133572ff6f6SMatthew Dillon HAL_POWER_MODE
ar5210GetPowerMode(struct ath_hal * ah)134572ff6f6SMatthew Dillon ar5210GetPowerMode(struct ath_hal *ah)
135572ff6f6SMatthew Dillon {
136572ff6f6SMatthew Dillon 	/* Just so happens the h/w maps directly to the abstracted value */
137572ff6f6SMatthew Dillon 	return MS(OS_REG_READ(ah, AR_SCR), AR_SCR_SLE);
138572ff6f6SMatthew Dillon }
139