1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file is part of wl1251
4  *
5  * Copyright (C) 2008 Nokia Corporation
6  */
7 
8 #include "reg.h"
9 #include "ps.h"
10 #include "cmd.h"
11 #include "io.h"
12 
13 /* in ms */
14 #define WL1251_WAKEUP_TIMEOUT 100
15 
wl1251_elp_work(struct work_struct * work)16 void wl1251_elp_work(struct work_struct *work)
17 {
18 	struct delayed_work *dwork;
19 	struct wl1251 *wl;
20 
21 	dwork = to_delayed_work(work);
22 	wl = container_of(dwork, struct wl1251, elp_work);
23 
24 	wl1251_debug(DEBUG_PSM, "elp work");
25 
26 	mutex_lock(&wl->mutex);
27 
28 	if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
29 		goto out;
30 
31 	wl1251_debug(DEBUG_PSM, "chip to elp");
32 	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
33 	wl->elp = true;
34 
35 out:
36 	mutex_unlock(&wl->mutex);
37 }
38 
39 #define ELP_ENTRY_DELAY  5
40 
41 /* Routines to toggle sleep mode while in ELP */
wl1251_ps_elp_sleep(struct wl1251 * wl)42 void wl1251_ps_elp_sleep(struct wl1251 *wl)
43 {
44 	unsigned long delay;
45 
46 	if (wl->station_mode != STATION_ACTIVE_MODE) {
47 		delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
48 		ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
49 	}
50 }
51 
wl1251_ps_elp_wakeup(struct wl1251 * wl)52 int wl1251_ps_elp_wakeup(struct wl1251 *wl)
53 {
54 	unsigned long timeout, start;
55 	u32 elp_reg;
56 
57 	cancel_delayed_work(&wl->elp_work);
58 
59 	if (!wl->elp)
60 		return 0;
61 
62 	wl1251_debug(DEBUG_PSM, "waking up chip from elp");
63 
64 	start = jiffies;
65 	timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
66 
67 	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
68 
69 	elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
70 
71 	/*
72 	 * FIXME: we should wait for irq from chip but, as a temporary
73 	 * solution to simplify locking, let's poll instead
74 	 */
75 	while (!(elp_reg & ELPCTRL_WLAN_READY)) {
76 		if (time_after(jiffies, timeout)) {
77 			wl1251_error("elp wakeup timeout");
78 			return -ETIMEDOUT;
79 		}
80 		msleep(1);
81 		elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
82 	}
83 
84 	wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
85 		     jiffies_to_msecs(jiffies - start));
86 
87 	wl->elp = false;
88 
89 	return 0;
90 }
91 
wl1251_ps_set_mode(struct wl1251 * wl,enum wl1251_station_mode mode)92 int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
93 {
94 	int ret;
95 
96 	switch (mode) {
97 	case STATION_POWER_SAVE_MODE:
98 		wl1251_debug(DEBUG_PSM, "entering psm");
99 
100 		/* enable beacon filtering */
101 		ret = wl1251_acx_beacon_filter_opt(wl, true);
102 		if (ret < 0)
103 			return ret;
104 
105 		ret = wl1251_acx_wake_up_conditions(wl,
106 						    WAKE_UP_EVENT_DTIM_BITMAP,
107 						    wl->listen_int);
108 		if (ret < 0)
109 			return ret;
110 
111 		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
112 					    WL1251_DEFAULT_BET_CONSECUTIVE);
113 		if (ret < 0)
114 			return ret;
115 
116 		ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
117 		if (ret < 0)
118 			return ret;
119 
120 		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
121 		if (ret < 0)
122 			return ret;
123 		break;
124 	case STATION_IDLE:
125 		wl1251_debug(DEBUG_PSM, "entering idle");
126 
127 		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
128 		if (ret < 0)
129 			return ret;
130 
131 		ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
132 		if (ret < 0)
133 			return ret;
134 		break;
135 	case STATION_ACTIVE_MODE:
136 	default:
137 		wl1251_debug(DEBUG_PSM, "leaving psm");
138 
139 		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
140 		if (ret < 0)
141 			return ret;
142 
143 		/* disable BET */
144 		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
145 					    WL1251_DEFAULT_BET_CONSECUTIVE);
146 		if (ret < 0)
147 			return ret;
148 
149 		/* disable beacon filtering */
150 		ret = wl1251_acx_beacon_filter_opt(wl, false);
151 		if (ret < 0)
152 			return ret;
153 
154 		ret = wl1251_acx_wake_up_conditions(wl,
155 						    WAKE_UP_EVENT_DTIM_BITMAP,
156 						    wl->listen_int);
157 		if (ret < 0)
158 			return ret;
159 
160 		ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
161 		if (ret < 0)
162 			return ret;
163 
164 		break;
165 	}
166 	wl->station_mode = mode;
167 
168 	return ret;
169 }
170 
171