18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of wl1251
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Nokia Corporation
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "reg.h"
98c2ecf20Sopenharmony_ci#include "ps.h"
108c2ecf20Sopenharmony_ci#include "cmd.h"
118c2ecf20Sopenharmony_ci#include "io.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/* in ms */
148c2ecf20Sopenharmony_ci#define WL1251_WAKEUP_TIMEOUT 100
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_civoid wl1251_elp_work(struct work_struct *work)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	struct delayed_work *dwork;
198c2ecf20Sopenharmony_ci	struct wl1251 *wl;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	dwork = to_delayed_work(work);
228c2ecf20Sopenharmony_ci	wl = container_of(dwork, struct wl1251, elp_work);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	wl1251_debug(DEBUG_PSM, "elp work");
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
298c2ecf20Sopenharmony_ci		goto out;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	wl1251_debug(DEBUG_PSM, "chip to elp");
328c2ecf20Sopenharmony_ci	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
338c2ecf20Sopenharmony_ci	wl->elp = true;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciout:
368c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define ELP_ENTRY_DELAY  5
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Routines to toggle sleep mode while in ELP */
428c2ecf20Sopenharmony_civoid wl1251_ps_elp_sleep(struct wl1251 *wl)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	unsigned long delay;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (wl->station_mode != STATION_ACTIVE_MODE) {
478c2ecf20Sopenharmony_ci		delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
488c2ecf20Sopenharmony_ci		ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciint wl1251_ps_elp_wakeup(struct wl1251 *wl)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned long timeout, start;
558c2ecf20Sopenharmony_ci	u32 elp_reg;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	cancel_delayed_work(&wl->elp_work);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (!wl->elp)
608c2ecf20Sopenharmony_ci		return 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	wl1251_debug(DEBUG_PSM, "waking up chip from elp");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	start = jiffies;
658c2ecf20Sopenharmony_ci	timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * FIXME: we should wait for irq from chip but, as a temporary
738c2ecf20Sopenharmony_ci	 * solution to simplify locking, let's poll instead
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	while (!(elp_reg & ELPCTRL_WLAN_READY)) {
768c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
778c2ecf20Sopenharmony_ci			wl1251_error("elp wakeup timeout");
788c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
798c2ecf20Sopenharmony_ci		}
808c2ecf20Sopenharmony_ci		msleep(1);
818c2ecf20Sopenharmony_ci		elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
858c2ecf20Sopenharmony_ci		     jiffies_to_msecs(jiffies - start));
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	wl->elp = false;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciint wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	int ret;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	switch (mode) {
978c2ecf20Sopenharmony_ci	case STATION_POWER_SAVE_MODE:
988c2ecf20Sopenharmony_ci		wl1251_debug(DEBUG_PSM, "entering psm");
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		/* enable beacon filtering */
1018c2ecf20Sopenharmony_ci		ret = wl1251_acx_beacon_filter_opt(wl, true);
1028c2ecf20Sopenharmony_ci		if (ret < 0)
1038c2ecf20Sopenharmony_ci			return ret;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		ret = wl1251_acx_wake_up_conditions(wl,
1068c2ecf20Sopenharmony_ci						    WAKE_UP_EVENT_DTIM_BITMAP,
1078c2ecf20Sopenharmony_ci						    wl->listen_int);
1088c2ecf20Sopenharmony_ci		if (ret < 0)
1098c2ecf20Sopenharmony_ci			return ret;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
1128c2ecf20Sopenharmony_ci					    WL1251_DEFAULT_BET_CONSECUTIVE);
1138c2ecf20Sopenharmony_ci		if (ret < 0)
1148c2ecf20Sopenharmony_ci			return ret;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
1178c2ecf20Sopenharmony_ci		if (ret < 0)
1188c2ecf20Sopenharmony_ci			return ret;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
1218c2ecf20Sopenharmony_ci		if (ret < 0)
1228c2ecf20Sopenharmony_ci			return ret;
1238c2ecf20Sopenharmony_ci		break;
1248c2ecf20Sopenharmony_ci	case STATION_IDLE:
1258c2ecf20Sopenharmony_ci		wl1251_debug(DEBUG_PSM, "entering idle");
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
1288c2ecf20Sopenharmony_ci		if (ret < 0)
1298c2ecf20Sopenharmony_ci			return ret;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
1328c2ecf20Sopenharmony_ci		if (ret < 0)
1338c2ecf20Sopenharmony_ci			return ret;
1348c2ecf20Sopenharmony_ci		break;
1358c2ecf20Sopenharmony_ci	case STATION_ACTIVE_MODE:
1368c2ecf20Sopenharmony_ci	default:
1378c2ecf20Sopenharmony_ci		wl1251_debug(DEBUG_PSM, "leaving psm");
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
1408c2ecf20Sopenharmony_ci		if (ret < 0)
1418c2ecf20Sopenharmony_ci			return ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		/* disable BET */
1448c2ecf20Sopenharmony_ci		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
1458c2ecf20Sopenharmony_ci					    WL1251_DEFAULT_BET_CONSECUTIVE);
1468c2ecf20Sopenharmony_ci		if (ret < 0)
1478c2ecf20Sopenharmony_ci			return ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		/* disable beacon filtering */
1508c2ecf20Sopenharmony_ci		ret = wl1251_acx_beacon_filter_opt(wl, false);
1518c2ecf20Sopenharmony_ci		if (ret < 0)
1528c2ecf20Sopenharmony_ci			return ret;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci		ret = wl1251_acx_wake_up_conditions(wl,
1558c2ecf20Sopenharmony_ci						    WAKE_UP_EVENT_DTIM_BITMAP,
1568c2ecf20Sopenharmony_ci						    wl->listen_int);
1578c2ecf20Sopenharmony_ci		if (ret < 0)
1588c2ecf20Sopenharmony_ci			return ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
1618c2ecf20Sopenharmony_ci		if (ret < 0)
1628c2ecf20Sopenharmony_ci			return ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		break;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci	wl->station_mode = mode;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return ret;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
171