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