Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5210 / ar5210_power.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2004 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: ar5210_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 "ar5210/ar5210.h"
26 #include "ar5210/ar5210reg.h"
27
28 /*
29  * Notify Power Mgt is disabled in self-generated frames.
30  * If requested, set Power Mode of chip to auto/normal.
31  */
32 static void
33 ar5210SetPowerModeAuto(struct ath_hal *ah, int setChip)
34 {
35         OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
36         if (setChip)
37                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_ALLOW);
38 }
39
40 /*
41  * Notify Power Mgt is enabled in self-generated frames.
42  * If requested, force chip awake.
43  *
44  * Returns A_OK if chip is awake or successfully forced awake.
45  *
46  * WARNING WARNING WARNING
47  * There is a problem with the chip where sometimes it will not wake up.
48  */
49 static HAL_BOOL
50 ar5210SetPowerModeAwake(struct ath_hal *ah, int setChip)
51 {
52 #define POWER_UP_TIME   2000
53         uint32_t val;
54         int i;
55
56         if (setChip) {
57                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_WAKE);
58                 OS_DELAY(2000); /* Give chip the chance to awake */
59
60                 for (i = POWER_UP_TIME / 200; i != 0; i--) {
61                         val = OS_REG_READ(ah, AR_PCICFG);
62                         if ((val & AR_PCICFG_SPWR_DN) == 0)
63                                 break;
64                         OS_DELAY(200);
65                         OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE,
66                                 AR_SCR_SLE_WAKE);
67                 }
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/20);
72 #endif
73                         return AH_FALSE;
74                 }
75         } 
76
77         OS_REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
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  */
86 static void
87 ar5210SetPowerModeSleep(struct ath_hal *ah, int setChip)
88 {
89         OS_REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SV);
90         if (setChip)
91                 OS_REG_RMW_FIELD(ah, AR_SCR, AR_SCR_SLE, AR_SCR_SLE_SLP);
92 }
93
94 HAL_BOOL
95 ar5210SetPowerMode(struct ath_hal *ah, HAL_POWER_MODE mode, int setChip)
96 {
97         struct ath_hal_5210 *ahp = AH5210(ah);
98 #ifdef AH_DEBUG
99         static const char* modes[] = {
100                 "AWAKE",
101                 "FULL-SLEEP",
102                 "NETWORK SLEEP",
103                 "UNDEFINED"
104         };
105 #endif
106         int status = AH_TRUE;
107
108         HALDEBUG(ah, HAL_DEBUG_POWER, "%s: %s -> %s (%s)\n", __func__,
109                 modes[ahp->ah_powerMode], modes[mode],
110                 setChip ? "set chip " : "");
111         switch (mode) {
112         case HAL_PM_AWAKE:
113                 status = ar5210SetPowerModeAwake(ah, setChip);
114                 break;
115         case HAL_PM_FULL_SLEEP:
116                 ar5210SetPowerModeSleep(ah, setChip);
117                 break;
118         case HAL_PM_NETWORK_SLEEP:
119                 ar5210SetPowerModeAuto(ah, setChip);
120                 break;
121         default:
122                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown power mode %u\n",
123                     __func__, mode);
124                 return AH_FALSE;
125         }
126         ahp->ah_powerMode = mode;
127         return status; 
128 }
129
130 HAL_POWER_MODE
131 ar5210GetPowerMode(struct ath_hal *ah)
132 {
133         /* Just so happens the h/w maps directly to the abstracted value */
134         return MS(OS_REG_READ(ah, AR_SCR), AR_SCR_SLE);
135 }