xref: /freebsd/sys/dev/rtwn/rtl8188e/r88e_fw.c (revision 1f474190)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
39 
40 #include <net/if.h>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
43 
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 
47 #include <dev/rtwn/if_rtwnreg.h>
48 #include <dev/rtwn/if_rtwnvar.h>
49 
50 #include <dev/rtwn/if_rtwn_debug.h>
51 
52 #include <dev/rtwn/rtl8188e/r88e.h>
53 #include <dev/rtwn/rtl8188e/r88e_reg.h>
54 #include <dev/rtwn/rtl8188e/r88e_fw_cmd.h>
55 
56 #ifndef RTWN_WITHOUT_UCODE
57 int
58 r88e_fw_cmd(struct rtwn_softc *sc, uint8_t id, const void *buf, int len)
59 {
60 	struct r88e_fw_cmd cmd;
61 	int ntries, error;
62 
63 	if (!(sc->sc_flags & RTWN_FW_LOADED)) {
64 		RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE, "%s: firmware "
65 		    "was not loaded; command (id %d) will be discarded\n",
66 		    __func__, id);
67 		return (0);
68 	}
69 
70 	/* Wait for current FW box to be empty. */
71 	for (ntries = 0; ntries < 100; ntries++) {
72 		if (!(rtwn_read_1(sc, R92C_HMETFR) & (1 << sc->fwcur)))
73 			break;
74 		rtwn_delay(sc, 2000);
75 	}
76 	if (ntries == 100) {
77 		device_printf(sc->sc_dev,
78 		    "could not send firmware command\n");
79 		return (ETIMEDOUT);
80 	}
81 	memset(&cmd, 0, sizeof(cmd));
82 	cmd.id = id;
83 	KASSERT(len <= sizeof(cmd.msg),
84 	    ("%s: firmware command too long (%d > %zu)\n",
85 	    __func__, len, sizeof(cmd.msg)));
86 	memcpy(cmd.msg, buf, len);
87 
88 	/* Write the first word last since that will trigger the FW. */
89 	if (len > 3) {
90 		error = rtwn_write_4(sc, R88E_HMEBOX_EXT(sc->fwcur),
91 		    *(uint32_t *)((uint8_t *)&cmd + 4));
92 		if (error != 0)
93 			return (error);
94 	}
95 	error = rtwn_write_4(sc, R92C_HMEBOX(sc->fwcur), *(uint32_t *)&cmd);
96 	if (error != 0)
97 		return (error);
98 
99 	sc->fwcur = (sc->fwcur + 1) % R92C_H2C_NBOX;
100 
101 	return (0);
102 }
103 
104 void
105 r88e_fw_reset(struct rtwn_softc *sc, int reason)
106 {
107 	uint16_t reg;
108 
109 	reg = rtwn_read_2(sc, R92C_SYS_FUNC_EN);
110 	rtwn_write_2(sc, R92C_SYS_FUNC_EN, reg & ~R92C_SYS_FUNC_EN_CPUEN);
111 
112 	if (reason != RTWN_FW_RESET_SHUTDOWN) {
113 		rtwn_write_2(sc, R92C_SYS_FUNC_EN,
114 		    reg | R92C_SYS_FUNC_EN_CPUEN);
115 	}
116 }
117 
118 void
119 r88e_fw_download_enable(struct rtwn_softc *sc, int enable)
120 {
121 	if (enable) {
122 		/* MCU firmware download enable. */
123 		rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_EN);
124 		/* 8051 reset. */
125 		rtwn_setbits_1_shift(sc, R92C_MCUFWDL, R92C_MCUFWDL_ROM_DLEN,
126 		    0, 2);
127 	} else {
128 		/* MCU download disable. */
129 		rtwn_setbits_1(sc, R92C_MCUFWDL, R92C_MCUFWDL_EN, 0);
130 		/* Reserved for f/w extension. */
131 		rtwn_write_1(sc, R92C_MCUFWDL + 1, 0);
132 	}
133 }
134 #endif
135 
136 void
137 r88e_macid_enable_link(struct rtwn_softc *sc, int id, int enable)
138 {
139 	uint32_t reg;
140 
141 	reg = R88E_MACID_NO_LINK;
142 	if (id > 32)
143 		reg += 4;
144 
145 	if (enable)
146 		rtwn_setbits_4(sc, reg, 1 << (id % 32), 0);
147 	else
148 		rtwn_setbits_4(sc, reg, 0, 1 << (id % 32));
149 
150 	/* XXX max macid for tx reports */
151 }
152 
153 void
154 r88e_set_media_status(struct rtwn_softc *sc, int macid)
155 {
156 	struct r88e_fw_cmd_msrrpt status;
157 
158 	if (macid & RTWN_MACID_VALID)
159 		status.msrb0 = R88E_MSRRPT_B0_ASSOC;
160 	else
161 		status.msrb0 = R88E_MSRRPT_B0_DISASSOC;
162 	status.macid = (macid & ~RTWN_MACID_VALID);
163 
164 	r88e_macid_enable_link(sc, status.macid,
165 	    (macid & RTWN_MACID_VALID) != 0);
166 
167 #ifndef RTWN_WITHOUT_UCODE
168 	if (r88e_fw_cmd(sc, R88E_CMD_MSR_RPT, &status, sizeof(status)) != 0) {
169 		device_printf(sc->sc_dev, "%s: cannot change media status!\n",
170 		    __func__);
171 	}
172 #endif
173 }
174 
175 #ifndef RTWN_WITHOUT_UCODE
176 int
177 r88e_set_rsvd_page(struct rtwn_softc *sc, int probe_resp, int null,
178     int qos_null)
179 {
180 	struct r88e_fw_cmd_rsvdpage rsvd;
181 
182 	rsvd.probe_resp = probe_resp;
183 	rsvd.ps_poll = 0;
184 	rsvd.null_data = null;
185 	rsvd.null_data_qos = qos_null;
186 	rsvd.null_data_qos_bt = 0;
187 	return (r88e_fw_cmd(sc, R88E_CMD_RSVD_PAGE, &rsvd, sizeof(rsvd)));
188 }
189 
190 int
191 r88e_set_pwrmode(struct rtwn_softc *sc, struct ieee80211vap *vap,
192     int off)
193 {
194 	struct r88e_fw_cmd_pwrmode mode;
195 	int error;
196 
197 	if (off && vap->iv_state == IEEE80211_S_RUN &&
198 	    (vap->iv_flags & IEEE80211_F_PMGTON)) {
199 		mode.mode = R88E_PWRMODE_LEG;
200 		/*
201 		 * TODO: switch to RFOFF state
202 		 * (something is missing here - Rx stops with it).
203 		 */
204 #ifdef RTWN_TODO
205 		mode.pwr_state = R88E_PWRMODE_STATE_RFOFF;
206 #else
207 		mode.pwr_state = R88E_PWRMODE_STATE_RFON;
208 #endif
209 	} else {
210 		mode.mode = R88E_PWRMODE_CAM;
211 		mode.pwr_state = R88E_PWRMODE_STATE_ALLON;
212 	}
213 	mode.pwrb1 =
214 	    SM(R88E_PWRMODE_B1_SMART_PS, R88E_PWRMODE_B1_LEG_NULLDATA) |
215 	    SM(R88E_PWRMODE_B1_RLBM, R88E_PWRMODE_B1_MODE_MIN);
216 	/* XXX ignored */
217 	mode.bcn_pass = 0;
218 	mode.queue_uapsd = 0;
219 	error = r88e_fw_cmd(sc, R88E_CMD_SET_PWRMODE, &mode, sizeof(mode));
220 	if (error != 0) {
221 		device_printf(sc->sc_dev,
222 		    "%s: CMD_SET_PWRMODE was not sent, error %d\n",
223 		    __func__, error);
224 	}
225 
226 	return (error);
227 }
228 #endif
229