blob: 68ff7f1900ed8c2237399696509168f6acfef6bf [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"
26#include "wl1251_spi.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030027
Kalle Valo80301cd2009-06-12 14:17:39 +030028#define WL1251_WAKEUP_TIMEOUT 2000
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029
30/* Routines to toggle sleep mode while in ELP */
Kalle Valo80301cd2009-06-12 14:17:39 +030031void wl1251_ps_elp_sleep(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030032{
33 if (wl->elp || !wl->psm)
34 return;
35
Kalle Valo80301cd2009-06-12 14:17:39 +030036 wl1251_debug(DEBUG_PSM, "chip to elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030037
Kalle Valo80301cd2009-06-12 14:17:39 +030038 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030039
40 wl->elp = true;
41}
42
Kalle Valo80301cd2009-06-12 14:17:39 +030043int wl1251_ps_elp_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030044{
45 unsigned long timeout;
46 u32 elp_reg;
47
48 if (!wl->elp)
49 return 0;
50
Kalle Valo80301cd2009-06-12 14:17:39 +030051 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030052
Kalle Valo80301cd2009-06-12 14:17:39 +030053 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030054
Kalle Valo80301cd2009-06-12 14:17:39 +030055 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056
Kalle Valo80301cd2009-06-12 14:17:39 +030057 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030058
59 /*
60 * FIXME: we should wait for irq from chip but, as a temporary
61 * solution to simplify locking, let's poll instead
62 */
63 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
64 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030065 wl1251_error("elp wakeup timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030066 return -ETIMEDOUT;
67 }
68 msleep(1);
Kalle Valo80301cd2009-06-12 14:17:39 +030069 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030070 }
71
Kalle Valo80301cd2009-06-12 14:17:39 +030072 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030073 jiffies_to_msecs(jiffies) -
Kalle Valo80301cd2009-06-12 14:17:39 +030074 (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075
76 wl->elp = false;
77
78 return 0;
79}
80
Kalle Valo80301cd2009-06-12 14:17:39 +030081static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082{
83 int ret;
84
85 if (enable) {
Kalle Valo80301cd2009-06-12 14:17:39 +030086 wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030087
Kalle Valo80301cd2009-06-12 14:17:39 +030088 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089 if (ret < 0)
90 return ret;
91
Kalle Valo80301cd2009-06-12 14:17:39 +030092 wl1251_ps_elp_sleep(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030093 } else {
Kalle Valo80301cd2009-06-12 14:17:39 +030094 wl1251_debug(DEBUG_PSM, "sleep auth cam");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030095
96 /*
97 * When the target is in ELP, we can only
98 * access the ELP control register. Thus,
99 * we have to wake the target up before
100 * changing the power authorization.
101 */
102
Kalle Valo80301cd2009-06-12 14:17:39 +0300103 wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300104
Kalle Valo80301cd2009-06-12 14:17:39 +0300105 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300106 if (ret < 0)
107 return ret;
108 }
109
110 return 0;
111}
112
Kalle Valo80301cd2009-06-12 14:17:39 +0300113int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114{
115 int ret;
116
117 switch (mode) {
118 case STATION_POWER_SAVE_MODE:
Kalle Valo80301cd2009-06-12 14:17:39 +0300119 wl1251_debug(DEBUG_PSM, "entering psm");
120 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121 if (ret < 0)
122 return ret;
123
Kalle Valo80301cd2009-06-12 14:17:39 +0300124 ret = wl1251_ps_set_elp(wl, true);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300125 if (ret < 0)
126 return ret;
127
128 wl->psm = 1;
129 break;
130 case STATION_ACTIVE_MODE:
131 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300132 wl1251_debug(DEBUG_PSM, "leaving psm");
133 ret = wl1251_ps_set_elp(wl, false);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300134 if (ret < 0)
135 return ret;
136
Kalle Valo80301cd2009-06-12 14:17:39 +0300137 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300138 if (ret < 0)
139 return ret;
140
141 wl->psm = 0;
142 break;
143 }
144
145 return ret;
146}
147