blob: 05816778f3dbed1b3e87589ebdcc38e0984fac8d [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
6 * Contact: Kalle Valo <kalle.valo@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include "reg.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030025#include "wl1251_ps.h"
Bob Copeland0764de62009-08-07 13:32:56 +030026#include "wl1251_cmd.h"
27#include "wl1251_io.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030028
Kalle Valo80301cd2009-06-12 14:17:39 +030029#define WL1251_WAKEUP_TIMEOUT 2000
Kalle Valo2f01a1f2009-04-29 23:33:31 +030030
31/* Routines to toggle sleep mode while in ELP */
Kalle Valo80301cd2009-06-12 14:17:39 +030032void wl1251_ps_elp_sleep(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033{
34 if (wl->elp || !wl->psm)
35 return;
36
Kalle Valo80301cd2009-06-12 14:17:39 +030037 wl1251_debug(DEBUG_PSM, "chip to elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030038
Kalle Valo80301cd2009-06-12 14:17:39 +030039 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030040
41 wl->elp = true;
42}
43
Kalle Valo80301cd2009-06-12 14:17:39 +030044int wl1251_ps_elp_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030045{
46 unsigned long timeout;
47 u32 elp_reg;
48
49 if (!wl->elp)
50 return 0;
51
Kalle Valo80301cd2009-06-12 14:17:39 +030052 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053
Kalle Valo80301cd2009-06-12 14:17:39 +030054 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030055
Kalle Valo80301cd2009-06-12 14:17:39 +030056 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030057
Kalle Valo80301cd2009-06-12 14:17:39 +030058 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030059
60 /*
61 * FIXME: we should wait for irq from chip but, as a temporary
62 * solution to simplify locking, let's poll instead
63 */
64 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
65 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030066 wl1251_error("elp wakeup timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030067 return -ETIMEDOUT;
68 }
69 msleep(1);
Kalle Valo80301cd2009-06-12 14:17:39 +030070 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030071 }
72
Kalle Valo80301cd2009-06-12 14:17:39 +030073 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030074 jiffies_to_msecs(jiffies) -
Kalle Valo80301cd2009-06-12 14:17:39 +030075 (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
Kalle Valo2f01a1f2009-04-29 23:33:31 +030076
77 wl->elp = false;
78
79 return 0;
80}
81
Kalle Valo80301cd2009-06-12 14:17:39 +030082static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030083{
84 int ret;
85
86 if (enable) {
Kalle Valo80301cd2009-06-12 14:17:39 +030087 wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030088
Kalle Valo80301cd2009-06-12 14:17:39 +030089 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 if (ret < 0)
91 return ret;
92
Kalle Valo80301cd2009-06-12 14:17:39 +030093 wl1251_ps_elp_sleep(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030094 } else {
Kalle Valo80301cd2009-06-12 14:17:39 +030095 wl1251_debug(DEBUG_PSM, "sleep auth cam");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030096
97 /*
98 * When the target is in ELP, we can only
99 * access the ELP control register. Thus,
100 * we have to wake the target up before
101 * changing the power authorization.
102 */
103
Kalle Valo80301cd2009-06-12 14:17:39 +0300104 wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300105
Kalle Valo80301cd2009-06-12 14:17:39 +0300106 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300107 if (ret < 0)
108 return ret;
109 }
110
111 return 0;
112}
113
Kalle Valo80301cd2009-06-12 14:17:39 +0300114int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300115{
116 int ret;
117
118 switch (mode) {
119 case STATION_POWER_SAVE_MODE:
Kalle Valo80301cd2009-06-12 14:17:39 +0300120 wl1251_debug(DEBUG_PSM, "entering psm");
121 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300122 if (ret < 0)
123 return ret;
124
Kalle Valo80301cd2009-06-12 14:17:39 +0300125 ret = wl1251_ps_set_elp(wl, true);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126 if (ret < 0)
127 return ret;
128
129 wl->psm = 1;
130 break;
131 case STATION_ACTIVE_MODE:
132 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300133 wl1251_debug(DEBUG_PSM, "leaving psm");
134 ret = wl1251_ps_set_elp(wl, false);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135 if (ret < 0)
136 return ret;
137
Kalle Valo80301cd2009-06-12 14:17:39 +0300138 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300139 if (ret < 0)
140 return ret;
141
142 wl->psm = 0;
143 break;
144 }
145
146 return ret;
147}
148