1478b2a6aSAugustin Cavalier/*-
2478b2a6aSAugustin Cavalier * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
3478b2a6aSAugustin Cavalier * All rights reserved.
4478b2a6aSAugustin Cavalier *
5478b2a6aSAugustin Cavalier * Redistribution and use in source and binary forms, with or without
6478b2a6aSAugustin Cavalier * modification, are permitted provided that the following conditions
7478b2a6aSAugustin Cavalier * are met:
8478b2a6aSAugustin Cavalier * 1. Redistributions of source code must retain the above copyright
9478b2a6aSAugustin Cavalier *    notice, this list of conditions and the following disclaimer.
10478b2a6aSAugustin Cavalier * 2. Redistributions in binary form must reproduce the above copyright
11478b2a6aSAugustin Cavalier *    notice, this list of conditions and the following disclaimer in the
12478b2a6aSAugustin Cavalier *    documentation and/or other materials provided with the distribution.
13478b2a6aSAugustin Cavalier *
14478b2a6aSAugustin Cavalier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15478b2a6aSAugustin Cavalier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16478b2a6aSAugustin Cavalier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17478b2a6aSAugustin Cavalier * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18478b2a6aSAugustin Cavalier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19478b2a6aSAugustin Cavalier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20478b2a6aSAugustin Cavalier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21478b2a6aSAugustin Cavalier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22478b2a6aSAugustin Cavalier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23478b2a6aSAugustin Cavalier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24478b2a6aSAugustin Cavalier * SUCH DAMAGE.
25478b2a6aSAugustin Cavalier */
26478b2a6aSAugustin Cavalier
27478b2a6aSAugustin Cavalier#include <sys/cdefs.h>
28a8bfcf32SAugustin Cavalier__FBSDID("$FreeBSD$");
29478b2a6aSAugustin Cavalier
30478b2a6aSAugustin Cavalier#include "opt_wlan.h"
31478b2a6aSAugustin Cavalier
32478b2a6aSAugustin Cavalier#include <sys/param.h>
33478b2a6aSAugustin Cavalier#include <sys/lock.h>
34478b2a6aSAugustin Cavalier#include <sys/mutex.h>
35478b2a6aSAugustin Cavalier#include <sys/mbuf.h>
36478b2a6aSAugustin Cavalier#include <sys/kernel.h>
37478b2a6aSAugustin Cavalier#include <sys/socket.h>
38478b2a6aSAugustin Cavalier#include <sys/systm.h>
39478b2a6aSAugustin Cavalier#include <sys/malloc.h>
40478b2a6aSAugustin Cavalier#include <sys/queue.h>
41478b2a6aSAugustin Cavalier#include <sys/taskqueue.h>
42478b2a6aSAugustin Cavalier#include <sys/bus.h>
43478b2a6aSAugustin Cavalier#include <sys/endian.h>
44478b2a6aSAugustin Cavalier#include <sys/linker.h>
45478b2a6aSAugustin Cavalier
46478b2a6aSAugustin Cavalier#include <net/if.h>
47478b2a6aSAugustin Cavalier#include <net/ethernet.h>
48478b2a6aSAugustin Cavalier#include <net/if_media.h>
49478b2a6aSAugustin Cavalier
50478b2a6aSAugustin Cavalier#include <net80211/ieee80211_var.h>
51478b2a6aSAugustin Cavalier#include <net80211/ieee80211_radiotap.h>
52478b2a6aSAugustin Cavalier
53478b2a6aSAugustin Cavalier#include <dev/rtwn/if_rtwnreg.h>
54478b2a6aSAugustin Cavalier#include <dev/rtwn/if_rtwnvar.h>
55478b2a6aSAugustin Cavalier
56478b2a6aSAugustin Cavalier#include <dev/rtwn/if_rtwn_debug.h>
57478b2a6aSAugustin Cavalier
58478b2a6aSAugustin Cavalier#include <dev/rtwn/rtl8188e/r88e.h>
59478b2a6aSAugustin Cavalier
60478b2a6aSAugustin Cavalier#include <dev/rtwn/rtl8812a/r12a.h>
61478b2a6aSAugustin Cavalier#include <dev/rtwn/rtl8812a/r12a_reg.h>
62478b2a6aSAugustin Cavalier#include <dev/rtwn/rtl8812a/r12a_var.h>
63478b2a6aSAugustin Cavalier#include <dev/rtwn/rtl8812a/r12a_fw_cmd.h>
64478b2a6aSAugustin Cavalier
65478b2a6aSAugustin Cavalier
66478b2a6aSAugustin Cavalier#ifndef RTWN_WITHOUT_UCODE
67478b2a6aSAugustin Cavaliervoid
68478b2a6aSAugustin Cavalierr12a_fw_reset(struct rtwn_softc *sc, int reason)
69478b2a6aSAugustin Cavalier{
70478b2a6aSAugustin Cavalier	/* Reset MCU IO wrapper. */
71478b2a6aSAugustin Cavalier	rtwn_setbits_1(sc, R92C_RSV_CTRL, R92C_RSV_CTRL_WLOCK_00, 0);
72478b2a6aSAugustin Cavalier	rtwn_setbits_1(sc, R92C_RSV_CTRL + 1, 0x08, 0);
73478b2a6aSAugustin Cavalier
74478b2a6aSAugustin Cavalier	rtwn_setbits_1_shift(sc, R92C_SYS_FUNC_EN,
75478b2a6aSAugustin Cavalier	    R92C_SYS_FUNC_EN_CPUEN, 0, 1);
76478b2a6aSAugustin Cavalier
77478b2a6aSAugustin Cavalier	/* Enable MCU IO wrapper. */
78478b2a6aSAugustin Cavalier	rtwn_setbits_1(sc, R92C_RSV_CTRL, R92C_RSV_CTRL_WLOCK_00, 0);
79478b2a6aSAugustin Cavalier	rtwn_setbits_1(sc, R92C_RSV_CTRL + 1, 0, 0x08);
80478b2a6aSAugustin Cavalier
81478b2a6aSAugustin Cavalier	rtwn_setbits_1_shift(sc, R92C_SYS_FUNC_EN,
82478b2a6aSAugustin Cavalier	    0, R92C_SYS_FUNC_EN_CPUEN, 1);
83478b2a6aSAugustin Cavalier}
84478b2a6aSAugustin Cavalier
85478b2a6aSAugustin Cavaliervoid
86478b2a6aSAugustin Cavalierr12a_fw_download_enable(struct rtwn_softc *sc, int enable)
87478b2a6aSAugustin Cavalier{
88478b2a6aSAugustin Cavalier	if (enable) {
89478b2a6aSAugustin Cavalier		/* MCU firmware download enable. */
90478b2a6aSAugustin Cavalier		rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_EN);
91478b2a6aSAugustin Cavalier		/* 8051 reset. */
92478b2a6aSAugustin Cavalier		rtwn_setbits_1_shift(sc, R92C_MCUFWDL, R92C_MCUFWDL_ROM_DLEN,
93478b2a6aSAugustin Cavalier		    0, 2);
94478b2a6aSAugustin Cavalier	} else {
95478b2a6aSAugustin Cavalier		/* MCU download disable. */
96478b2a6aSAugustin Cavalier		rtwn_setbits_1(sc, R92C_MCUFWDL, R92C_MCUFWDL_EN, 0);
97478b2a6aSAugustin Cavalier	}
98478b2a6aSAugustin Cavalier}
99478b2a6aSAugustin Cavalier
100478b2a6aSAugustin Cavaliervoid
101478b2a6aSAugustin Cavalierr12a_set_media_status(struct rtwn_softc *sc, int macid)
102478b2a6aSAugustin Cavalier{
103478b2a6aSAugustin Cavalier	struct r12a_fw_cmd_msrrpt status;
104478b2a6aSAugustin Cavalier	int error;
105478b2a6aSAugustin Cavalier
106478b2a6aSAugustin Cavalier	if (macid & RTWN_MACID_VALID)
107478b2a6aSAugustin Cavalier		status.msrb0 = R12A_MSRRPT_B0_ASSOC;
108478b2a6aSAugustin Cavalier	else
109478b2a6aSAugustin Cavalier		status.msrb0 = R12A_MSRRPT_B0_DISASSOC;
110478b2a6aSAugustin Cavalier
111478b2a6aSAugustin Cavalier	status.macid = (macid & ~RTWN_MACID_VALID);
112478b2a6aSAugustin Cavalier	status.macid_end = 0;
113478b2a6aSAugustin Cavalier
114478b2a6aSAugustin Cavalier	error = r88e_fw_cmd(sc, R12A_CMD_MSR_RPT, &status, sizeof(status));
115478b2a6aSAugustin Cavalier	if (error != 0)
116478b2a6aSAugustin Cavalier		device_printf(sc->sc_dev, "cannot change media status!\n");
117478b2a6aSAugustin Cavalier}
118478b2a6aSAugustin Cavalier
119478b2a6aSAugustin Cavalierint
120478b2a6aSAugustin Cavalierr12a_set_pwrmode(struct rtwn_softc *sc, struct ieee80211vap *vap,
121478b2a6aSAugustin Cavalier    int off)
122478b2a6aSAugustin Cavalier{
123478b2a6aSAugustin Cavalier	struct r12a_fw_cmd_pwrmode mode;
124478b2a6aSAugustin Cavalier	int error;
125478b2a6aSAugustin Cavalier
126478b2a6aSAugustin Cavalier	if (off && vap->iv_state == IEEE80211_S_RUN &&
127478b2a6aSAugustin Cavalier	    (vap->iv_flags & IEEE80211_F_PMGTON)) {
128478b2a6aSAugustin Cavalier		mode.mode = R88E_PWRMODE_LEG;
129478b2a6aSAugustin Cavalier		/*
130478b2a6aSAugustin Cavalier		 * TODO: switch to RFOFF state
131478b2a6aSAugustin Cavalier		 * (something is missing here - Rx stops with it).
132478b2a6aSAugustin Cavalier		 */
133478b2a6aSAugustin Cavalier#ifdef RTWN_TODO
134478b2a6aSAugustin Cavalier		mode.pwr_state = R88E_PWRMODE_STATE_RFOFF;
135478b2a6aSAugustin Cavalier#else
136478b2a6aSAugustin Cavalier		mode.pwr_state = R88E_PWRMODE_STATE_RFON;
137478b2a6aSAugustin Cavalier#endif
138478b2a6aSAugustin Cavalier	} else {
139478b2a6aSAugustin Cavalier		mode.mode = R88E_PWRMODE_CAM;
140478b2a6aSAugustin Cavalier		mode.pwr_state = R88E_PWRMODE_STATE_ALLON;
141478b2a6aSAugustin Cavalier	}
142478b2a6aSAugustin Cavalier	mode.pwrb1 =
143478b2a6aSAugustin Cavalier	    SM(R88E_PWRMODE_B1_SMART_PS, R88E_PWRMODE_B1_LEG_NULLDATA) |
144478b2a6aSAugustin Cavalier	    SM(R88E_PWRMODE_B1_RLBM, R88E_PWRMODE_B1_MODE_MIN);
145478b2a6aSAugustin Cavalier	/* XXX ignored */
146478b2a6aSAugustin Cavalier	mode.bcn_pass = 0;
147478b2a6aSAugustin Cavalier	mode.queue_uapsd = 0;
148478b2a6aSAugustin Cavalier	mode.pwrb5 = R12A_PWRMODE_B5_NO_BTCOEX;
149478b2a6aSAugustin Cavalier	error = r88e_fw_cmd(sc, R12A_CMD_SET_PWRMODE, &mode, sizeof(mode));
150478b2a6aSAugustin Cavalier	if (error != 0) {
151478b2a6aSAugustin Cavalier		device_printf(sc->sc_dev,
152478b2a6aSAugustin Cavalier		    "%s: CMD_SET_PWRMODE was not sent, error %d\n",
153478b2a6aSAugustin Cavalier		    __func__, error);
154478b2a6aSAugustin Cavalier	}
155478b2a6aSAugustin Cavalier
156478b2a6aSAugustin Cavalier	return (error);
157478b2a6aSAugustin Cavalier}
158478b2a6aSAugustin Cavalier
159478b2a6aSAugustin Cavaliervoid
160478b2a6aSAugustin Cavalierr12a_iq_calib_fw(struct rtwn_softc *sc)
161478b2a6aSAugustin Cavalier{
162478b2a6aSAugustin Cavalier	struct r12a_softc *rs = sc->sc_priv;
163478b2a6aSAugustin Cavalier	struct ieee80211_channel *c = sc->sc_ic.ic_curchan;
164478b2a6aSAugustin Cavalier	struct r12a_fw_cmd_iq_calib cmd;
165478b2a6aSAugustin Cavalier
166478b2a6aSAugustin Cavalier	if (rs->rs_flags & R12A_IQK_RUNNING)
167478b2a6aSAugustin Cavalier		return;
168478b2a6aSAugustin Cavalier
169478b2a6aSAugustin Cavalier	RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB, "Starting IQ calibration (FW)\n");
170478b2a6aSAugustin Cavalier
171478b2a6aSAugustin Cavalier	cmd.chan = rtwn_chan2centieee(c);
172478b2a6aSAugustin Cavalier	if (IEEE80211_IS_CHAN_5GHZ(c))
173478b2a6aSAugustin Cavalier		cmd.band_bw = RTWN_CMD_IQ_BAND_5GHZ;
174478b2a6aSAugustin Cavalier	else
175478b2a6aSAugustin Cavalier		cmd.band_bw = RTWN_CMD_IQ_BAND_2GHZ;
176478b2a6aSAugustin Cavalier
177478b2a6aSAugustin Cavalier	/* TODO: 80/160 MHz. */
178478b2a6aSAugustin Cavalier	if (IEEE80211_IS_CHAN_HT40(c))
179478b2a6aSAugustin Cavalier		cmd.band_bw |= RTWN_CMD_IQ_CHAN_WIDTH_40;
180478b2a6aSAugustin Cavalier	else
181478b2a6aSAugustin Cavalier		cmd.band_bw |= RTWN_CMD_IQ_CHAN_WIDTH_20;
182478b2a6aSAugustin Cavalier
183478b2a6aSAugustin Cavalier	cmd.ext_5g_pa_lna = RTWN_CMD_IQ_EXT_PA_5G(rs->ext_pa_5g);
184478b2a6aSAugustin Cavalier	cmd.ext_5g_pa_lna |= RTWN_CMD_IQ_EXT_LNA_5G(rs->ext_lna_5g);
185478b2a6aSAugustin Cavalier
186478b2a6aSAugustin Cavalier	if (r88e_fw_cmd(sc, R12A_CMD_IQ_CALIBRATE, &cmd, sizeof(cmd)) != 0) {
187478b2a6aSAugustin Cavalier		RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB,
188478b2a6aSAugustin Cavalier		    "error while sending IQ calibration command to FW!\n");
189478b2a6aSAugustin Cavalier		return;
190478b2a6aSAugustin Cavalier	}
191478b2a6aSAugustin Cavalier
192478b2a6aSAugustin Cavalier	rs->rs_flags |= R12A_IQK_RUNNING;
193478b2a6aSAugustin Cavalier}
194478b2a6aSAugustin Cavalier#endif
195