ar5416_power.c revision 185380
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 * $Id: ar5416_power.c,v 1.6 2008/11/11 00:11:30 sam Exp $
18 */
19#include "opt_ah.h"
20
21#ifdef AH_SUPPORT_AR5416
22
23#include "ah.h"
24#include "ah_internal.h"
25
26#include "ar5416/ar5416.h"
27#include "ar5416/ar5416reg.h"
28
29/*
30 * Notify Power Mgt is enabled in self-generated frames.
31 * If requested, force chip awake.
32 *
33 * Returns A_OK if chip is awake or successfully forced awake.
34 *
35 * WARNING WARNING WARNING
36 * There is a problem with the chip where sometimes it will not wake up.
37 */
38static HAL_BOOL
39ar5416SetPowerModeAwake(struct ath_hal *ah, int setChip)
40{
41#define	POWER_UP_TIME	200000
42	uint32_t val;
43	int i = 0;
44
45	if (setChip) {
46		/*
47		 * Do a Power-On-Reset if OWL is shutdown
48		 * the NetBSD driver  power-cycles the Cardbus slot
49		 * as part of the reset procedure.
50		 */
51		if ((OS_REG_READ(ah, AR_RTC_STATUS)
52			& AR_RTC_PM_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
53			if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON))
54				goto bad;
55		}
56
57		OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
58		OS_DELAY(50);   /* Give chip the chance to awake */
59
60		for (i = POWER_UP_TIME / 50; i != 0; i--) {
61			val = OS_REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
62			if (val == AR_RTC_STATUS_ON)
63				break;
64			OS_DELAY(50);
65			OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
66		}
67	bad:
68		if (i == 0) {
69#ifdef AH_DEBUG
70			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
71				__func__, POWER_UP_TIME/1000);
72#endif
73			return AH_FALSE;
74		}
75	}
76
77	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
78	return AH_TRUE;
79#undef POWER_UP_TIME
80}
81
82/*
83 * Notify Power Mgt is disabled in self-generated frames.
84 * If requested, force chip to sleep.
85 */
86static void
87ar5416SetPowerModeSleep(struct ath_hal *ah, int setChip)
88{
89	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
90	if (setChip) {
91		/* Clear the RTC force wake bit to allow the mac to sleep */
92		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
93		OS_REG_WRITE(ah, AR_RC, AR_RC_AHB|AR_RC_HOSTIF);
94		/* Shutdown chip. Active low */
95		OS_REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
96	}
97}
98
99/*
100 * Notify Power Management is enabled in self-generating
101 * fames.  If request, set power mode of chip to
102 * auto/normal.  Duration in units of 128us (1/8 TU).
103 */
104static void
105ar5416SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
106{
107	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
108
109	if (setChip)
110		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
111}
112
113/*
114 * Set power mgt to the requested mode, and conditionally set
115 * the chip as well
116 */
117HAL_BOOL
118ar5416SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
119{
120	struct ath_hal_5212 *ahp = AH5212(ah);
121#ifdef AH_DEBUG
122	static const char* modes[] = {
123		"AWAKE",
124		"FULL-SLEEP",
125		"NETWORK SLEEP",
126		"UNDEFINED"
127	};
128#endif
129	int status = AH_TRUE;
130	if (!setChip)
131		return AH_TRUE;
132
133	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
134	    modes[ahp->ah_powerMode], modes[mode], setChip ? "set chip " : "");
135	switch (mode) {
136	case HAL_PM_AWAKE:
137		status = ar5416SetPowerModeAwake(ah, setChip);
138		break;
139	case HAL_PM_FULL_SLEEP:
140		ar5416SetPowerModeSleep(ah, setChip);
141		break;
142	case HAL_PM_NETWORK_SLEEP:
143		ar5416SetPowerModeNetworkSleep(ah, setChip);
144		break;
145	default:
146		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode 0x%x\n",
147		    __func__, mode);
148		return AH_FALSE;
149	}
150	ahp->ah_powerMode = mode;
151	return status;
152}
153
154/*
155 * Return the current sleep mode of the chip
156 */
157HAL_POWER_MODE
158ar5416GetPowerMode(struct ath_hal *ah)
159{
160	int mode = OS_REG_READ(ah, AR_RTC_STATUS);
161	switch (mode & AR_RTC_PM_STATUS_M) {
162	case AR_RTC_STATUS_ON:
163	case AR_RTC_STATUS_WAKEUP:
164		return HAL_PM_AWAKE;
165	case AR_RTC_STATUS_SLEEP:
166		return HAL_PM_NETWORK_SLEEP;
167	case AR_RTC_STATUS_SHUTDOWN:
168		return HAL_PM_FULL_SLEEP;
169	default:
170		HALDEBUG(ah, HAL_DEBUG_ANY,
171		    "%s: unknown power mode, RTC_STATUS 0x%x\n",
172		    __func__, mode);
173		return HAL_PM_UNDEFINED;
174	}
175}
176#endif /* AH_SUPPORT_AR5416 */
177