xref: /freebsd/sys/dev/ath/ath_hal/ar5416/ar5416_power.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1*6e778a7eSPedro F. Giffuni /*-
2*6e778a7eSPedro F. Giffuni  * SPDX-License-Identifier: ISC
3*6e778a7eSPedro F. Giffuni  *
414779705SSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
514779705SSam Leffler  * Copyright (c) 2002-2008 Atheros Communications, Inc.
614779705SSam Leffler  *
714779705SSam Leffler  * Permission to use, copy, modify, and/or distribute this software for any
814779705SSam Leffler  * purpose with or without fee is hereby granted, provided that the above
914779705SSam Leffler  * copyright notice and this permission notice appear in all copies.
1014779705SSam Leffler  *
1114779705SSam Leffler  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1214779705SSam Leffler  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1314779705SSam Leffler  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1414779705SSam Leffler  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1514779705SSam Leffler  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1614779705SSam Leffler  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1714779705SSam Leffler  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1814779705SSam Leffler  */
1914779705SSam Leffler #include "opt_ah.h"
2014779705SSam Leffler 
2114779705SSam Leffler #include "ah.h"
2214779705SSam Leffler #include "ah_internal.h"
2314779705SSam Leffler 
2414779705SSam Leffler #include "ar5416/ar5416.h"
2514779705SSam Leffler #include "ar5416/ar5416reg.h"
2614779705SSam Leffler 
2714779705SSam Leffler /*
2814779705SSam Leffler  * Notify Power Mgt is enabled in self-generated frames.
2914779705SSam Leffler  * If requested, force chip awake.
3014779705SSam Leffler  *
3114779705SSam Leffler  * Returns A_OK if chip is awake or successfully forced awake.
3214779705SSam Leffler  *
3314779705SSam Leffler  * WARNING WARNING WARNING
3414779705SSam Leffler  * There is a problem with the chip where sometimes it will not wake up.
3514779705SSam Leffler  */
3614779705SSam Leffler static HAL_BOOL
ar5416SetPowerModeAwake(struct ath_hal * ah,int setChip)3714779705SSam Leffler ar5416SetPowerModeAwake(struct ath_hal *ah, int setChip)
3814779705SSam Leffler {
3914779705SSam Leffler #define	POWER_UP_TIME	200000
4014779705SSam Leffler 	uint32_t val;
4114779705SSam Leffler 	int i = 0;
4214779705SSam Leffler 
4314779705SSam Leffler 	if (setChip) {
4414779705SSam Leffler 		/*
4514779705SSam Leffler 		 * Do a Power-On-Reset if OWL is shutdown
4614779705SSam Leffler 		 * the NetBSD driver  power-cycles the Cardbus slot
4714779705SSam Leffler 		 * as part of the reset procedure.
4814779705SSam Leffler 		 */
4914779705SSam Leffler 		if ((OS_REG_READ(ah, AR_RTC_STATUS)
5014779705SSam Leffler 			& AR_RTC_PM_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
5114779705SSam Leffler 			if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON))
5214779705SSam Leffler 				goto bad;
5327ce86b8SAdrian Chadd 			AH5416(ah)->ah_initPLL(ah, AH_NULL);
5414779705SSam Leffler 		}
5514779705SSam Leffler 
569f25ad52SAdrian Chadd 		if (AR_SREV_HOWL(ah))
579f25ad52SAdrian Chadd 			OS_REG_SET_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
589f25ad52SAdrian Chadd 
5914779705SSam Leffler 		OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
608a90965bSAdrian Chadd 		if (AR_SREV_HOWL(ah))
618a90965bSAdrian Chadd 			OS_DELAY(10000);
628a90965bSAdrian Chadd 		else
6314779705SSam Leffler 			OS_DELAY(50);   /* Give chip the chance to awake */
6414779705SSam Leffler 
6514779705SSam Leffler 		for (i = POWER_UP_TIME / 50; i != 0; i--) {
6614779705SSam Leffler 			val = OS_REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
6714779705SSam Leffler 			if (val == AR_RTC_STATUS_ON)
6814779705SSam Leffler 				break;
6914779705SSam Leffler 			OS_DELAY(50);
7014779705SSam Leffler 			OS_REG_SET_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
7114779705SSam Leffler 		}
7214779705SSam Leffler 	bad:
7314779705SSam Leffler 		if (i == 0) {
7414779705SSam Leffler #ifdef AH_DEBUG
7514779705SSam Leffler 			ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
7614779705SSam Leffler 				__func__, POWER_UP_TIME/1000);
7714779705SSam Leffler #endif
7814779705SSam Leffler 			return AH_FALSE;
7914779705SSam Leffler 		}
8014779705SSam Leffler 	}
8114779705SSam Leffler 
8214779705SSam Leffler 	OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
8314779705SSam Leffler 	return AH_TRUE;
8414779705SSam Leffler #undef POWER_UP_TIME
8514779705SSam Leffler }
8614779705SSam Leffler 
8714779705SSam Leffler /*
8814779705SSam Leffler  * Notify Power Mgt is disabled in self-generated frames.
8914779705SSam Leffler  * If requested, force chip to sleep.
9014779705SSam Leffler  */
9114779705SSam Leffler static void
ar5416SetPowerModeSleep(struct ath_hal * ah,int setChip)9214779705SSam Leffler ar5416SetPowerModeSleep(struct ath_hal *ah, int setChip)
9314779705SSam Leffler {
9414779705SSam Leffler 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
9514779705SSam Leffler 	if (setChip) {
9614779705SSam Leffler 		/* Clear the RTC force wake bit to allow the mac to sleep */
9714779705SSam Leffler 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
989f25ad52SAdrian Chadd 		if (! AR_SREV_HOWL(ah))
9914779705SSam Leffler 			OS_REG_WRITE(ah, AR_RC, AR_RC_AHB|AR_RC_HOSTIF);
10014779705SSam Leffler 		/* Shutdown chip. Active low */
1018a90965bSAdrian Chadd 		if (! AR_SREV_OWL(ah))
10214779705SSam Leffler 			OS_REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
10314779705SSam Leffler 	}
10414779705SSam Leffler }
10514779705SSam Leffler 
10614779705SSam Leffler /*
10714779705SSam Leffler  * Notify Power Management is enabled in self-generating
10814779705SSam Leffler  * fames.  If request, set power mode of chip to
10914779705SSam Leffler  * auto/normal.  Duration in units of 128us (1/8 TU).
11014779705SSam Leffler  */
11114779705SSam Leffler static void
ar5416SetPowerModeNetworkSleep(struct ath_hal * ah,int setChip)11214779705SSam Leffler ar5416SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
11314779705SSam Leffler {
11414779705SSam Leffler 	OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
11514779705SSam Leffler 
11614779705SSam Leffler 	if (setChip)
11714779705SSam Leffler 		OS_REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
11814779705SSam Leffler }
11914779705SSam Leffler 
12014779705SSam Leffler /*
12114779705SSam Leffler  * Set power mgt to the requested mode, and conditionally set
12214779705SSam Leffler  * the chip as well
12314779705SSam Leffler  */
12414779705SSam Leffler HAL_BOOL
ar5416SetPowerMode(struct ath_hal * ah,HAL_POWER_MODE mode,int setChip)12514779705SSam Leffler ar5416SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
12614779705SSam Leffler {
12714779705SSam Leffler #ifdef AH_DEBUG
12814779705SSam Leffler 	static const char* modes[] = {
12914779705SSam Leffler 		"AWAKE",
13014779705SSam Leffler 		"FULL-SLEEP",
13114779705SSam Leffler 		"NETWORK SLEEP",
13214779705SSam Leffler 		"UNDEFINED"
13314779705SSam Leffler 	};
13414779705SSam Leffler #endif
13514779705SSam Leffler 	int status = AH_TRUE;
136ce3f9a89SAdrian Chadd 
137ce3f9a89SAdrian Chadd #if 0
13814779705SSam Leffler 	if (!setChip)
13914779705SSam Leffler 		return AH_TRUE;
140ce3f9a89SAdrian Chadd #endif
14114779705SSam Leffler 
14214779705SSam Leffler 	HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
1438a67b42aSAdrian Chadd 	    modes[ah->ah_powerMode], modes[mode], setChip ? "set chip " : "");
14414779705SSam Leffler 	switch (mode) {
14514779705SSam Leffler 	case HAL_PM_AWAKE:
146ce3f9a89SAdrian Chadd 		if (setChip)
147bd369abaSAdrian Chadd 			ah->ah_powerMode = mode;
14814779705SSam Leffler 		status = ar5416SetPowerModeAwake(ah, setChip);
14914779705SSam Leffler 		break;
15014779705SSam Leffler 	case HAL_PM_FULL_SLEEP:
15114779705SSam Leffler 		ar5416SetPowerModeSleep(ah, setChip);
152ce3f9a89SAdrian Chadd 		if (setChip)
153bd369abaSAdrian Chadd 			ah->ah_powerMode = mode;
15414779705SSam Leffler 		break;
15514779705SSam Leffler 	case HAL_PM_NETWORK_SLEEP:
15614779705SSam Leffler 		ar5416SetPowerModeNetworkSleep(ah, setChip);
157ce3f9a89SAdrian Chadd 		if (setChip)
158bd369abaSAdrian Chadd 			ah->ah_powerMode = mode;
15914779705SSam Leffler 		break;
16014779705SSam Leffler 	default:
16114779705SSam Leffler 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode 0x%x\n",
16214779705SSam Leffler 		    __func__, mode);
16314779705SSam Leffler 		return AH_FALSE;
16414779705SSam Leffler 	}
16514779705SSam Leffler 	return status;
16614779705SSam Leffler }
16714779705SSam Leffler 
16814779705SSam Leffler /*
16914779705SSam Leffler  * Return the current sleep mode of the chip
17014779705SSam Leffler  */
17114779705SSam Leffler HAL_POWER_MODE
ar5416GetPowerMode(struct ath_hal * ah)17214779705SSam Leffler ar5416GetPowerMode(struct ath_hal *ah)
17314779705SSam Leffler {
17414779705SSam Leffler 	int mode = OS_REG_READ(ah, AR_RTC_STATUS);
17514779705SSam Leffler 	switch (mode & AR_RTC_PM_STATUS_M) {
17614779705SSam Leffler 	case AR_RTC_STATUS_ON:
17714779705SSam Leffler 	case AR_RTC_STATUS_WAKEUP:
17814779705SSam Leffler 		return HAL_PM_AWAKE;
17914779705SSam Leffler 	case AR_RTC_STATUS_SLEEP:
18014779705SSam Leffler 		return HAL_PM_NETWORK_SLEEP;
18114779705SSam Leffler 	case AR_RTC_STATUS_SHUTDOWN:
18214779705SSam Leffler 		return HAL_PM_FULL_SLEEP;
18314779705SSam Leffler 	default:
18414779705SSam Leffler 		HALDEBUG(ah, HAL_DEBUG_ANY,
18514779705SSam Leffler 		    "%s: unknown power mode, RTC_STATUS 0x%x\n",
18614779705SSam Leffler 		    __func__, mode);
18714779705SSam Leffler 		return HAL_PM_UNDEFINED;
18814779705SSam Leffler 	}
18914779705SSam Leffler }
190