Update ath_hal from FreeBSD.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5211 / ar5211_power.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2006 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: ar5211_power.c,v 1.4 2008/11/10 04:08:02 sam Exp $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24
25 #include "ar5211/ar5211.h"
26 #include "ar5211/ar5211reg.h"
27 #include "ar5211/ar5211desc.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  */
38 static HAL_BOOL
39 ar5211SetPowerModeAwake(struct ath_hal *ah, int setChip)
40 {
41 #define POWER_UP_TIME   2000
42         uint32_t val;
43         int i;
44
45         if (setChip) {
46                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_WAKE);
47                 OS_DELAY(10);   /* Give chip the chance to awake */
48
49                 for (i = POWER_UP_TIME / 200; i != 0; i--) {
50                         val = OS_REG_READ(ah, AR_PCICFG);
51                         if ((val & AR_PCICFG_SPWR_DN) == 0)
52                                 break;
53                         OS_DELAY(200);
54                         OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE,
55                                 AR_SCR_SLE_WAKE);
56                 }
57                 if (i == 0) {
58 #ifdef AH_DEBUG
59                         ath_hal_printf(ah, "%s: Failed to wakeup in %ums\n",
60                                 __func__, POWER_UP_TIME/20);
61 #endif
62                         return AH_FALSE;
63                 }
64         } 
65
66         OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
67         return AH_TRUE;
68 #undef POWER_UP_TIME
69 }
70
71 /*
72  * Notify Power Mgt is disabled in self-generated frames.
73  * If requested, force chip to sleep.
74  */
75 static void
76 ar5211SetPowerModeSleep(struct ath_hal *ah, int setChip)
77 {
78         OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
79         if (setChip)
80                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_SLP);
81 }
82
83 /*
84  * Notify Power Management is enabled in self-generating
85  * fames.  If request, set power mode of chip to
86  * auto/normal.  Duration in units of 128us (1/8 TU).
87  */
88 static void
89 ar5211SetPowerModeNetworkSleep(struct ath_hal *ah, int setChip)
90 {
91         OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
92         if (setChip)
93                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_NORM);
94 }
95
96 HAL_BOOL
97 ar5211SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
98 {
99         struct ath_hal_5211 *ahp = AH5211(ah);
100 #ifdef AH_DEBUG
101         static const char* modes[] = {
102                 "AWAKE",
103                 "FULL-SLEEP",
104                 "NETWORK SLEEP",
105                 "UNDEFINED"
106         };
107 #endif
108         int status = AH_TRUE;
109
110         HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
111                 modes[ahp->ah_powerMode], modes[mode],
112                 setChip ? "set chip " : "");
113         switch (mode) {
114         case HAL_PM_AWAKE:
115                 status = ar5211SetPowerModeAwake(ah, setChip);
116                 break;
117         case HAL_PM_FULL_SLEEP:
118                 ar5211SetPowerModeSleep(ah, setChip);
119                 break;
120         case HAL_PM_NETWORK_SLEEP:
121                 ar5211SetPowerModeNetworkSleep(ah, setChip);
122                 break;
123         default:
124                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode %u\n",
125                     __func__, mode);
126                 return AH_FALSE;
127         }
128         ahp->ah_powerMode = mode;
129         return status; 
130 }
131
132 HAL_POWER_MODE
133 ar5211GetPowerMode(struct ath_hal *ah)
134 {
135         /* Just so happens the h/w maps directly to the abstracted value */
136         return MS(OS_REG_READ(ah, AR_SCR), AR_SCR_SLE);
137 }