1 /* $OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $ */
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org>
6 * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
45 #include <net/ethernet.h>
46 #include <net/if_media.h>
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_radiotap.h>
51 #include <dev/rtwn/if_rtwnvar.h>
53 #include <dev/rtwn/pci/rtwn_pci_var.h>
55 #include <dev/rtwn/rtl8192c/r92c_var.h>
57 #include <dev/rtwn/rtl8192c/pci/r92ce.h>
58 #include <dev/rtwn/rtl8192c/pci/r92ce_reg.h>
62 r92ce_init_intr(struct rtwn_softc *sc)
64 /* Disable interrupts. */
65 rtwn_write_4(sc, R92C_HISR, 0x00000000);
66 rtwn_write_4(sc, R92C_HIMR, 0x00000000);
70 r92ce_init_edca(struct rtwn_softc *sc)
73 rtwn_write_2(sc, R92C_SPEC_SIFS, 0x1010);
74 rtwn_write_2(sc, R92C_MAC_SPEC_SIFS, 0x1010);
75 rtwn_write_2(sc, R92C_SIFS_CCK, 0x1010);
76 rtwn_write_2(sc, R92C_SIFS_OFDM, 0x0e0e);
78 rtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x005ea42b);
79 rtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a44f);
80 rtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4322);
81 rtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3222);
85 r92ce_init_bb(struct rtwn_softc *sc)
88 /* Enable BB and RF. */
89 rtwn_setbits_2(sc, R92C_SYS_FUNC_EN, 0,
90 R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
91 R92C_SYS_FUNC_EN_DIO_RF);
93 rtwn_write_2(sc, R92C_AFE_PLL_CTRL, 0xdb83);
95 rtwn_write_1(sc, R92C_RF_CTRL,
96 R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
98 rtwn_write_1(sc, R92C_SYS_FUNC_EN,
99 R92C_SYS_FUNC_EN_DIO_PCIE | R92C_SYS_FUNC_EN_PCIEA |
100 R92C_SYS_FUNC_EN_PPLL | R92C_SYS_FUNC_EN_BB_GLB_RST |
101 R92C_SYS_FUNC_EN_BBRSTB);
103 rtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80);
105 rtwn_setbits_4(sc, R92C_LEDCFG0, 0, 0x00800000);
107 r92c_init_bb_common(sc);
111 r92ce_power_on(struct rtwn_softc *sc)
113 struct r92c_softc *rs = sc->sc_priv;
117 /* Wait for autoload done bit. */
118 for (ntries = 0; ntries < 1000; ntries++) {
119 if (rtwn_read_1(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_PFM_ALDN)
123 if (ntries == 1000) {
124 device_printf(sc->sc_dev,
125 "timeout waiting for chip autoload\n");
129 /* Unlock ISO/CLK/Power control register. */
130 rtwn_write_1(sc, R92C_RSV_CTRL, 0);
132 if (rs->board_type != R92C_BOARD_TYPE_DONGLE) {
134 rtwn_setbits_4(sc, R92C_APS_FSMCO, 0,
135 R92C_APS_FSMCO_SOP_ABG |
136 R92C_APS_FSMCO_SOP_AMB |
137 R92C_APS_FSMCO_XOP_BTCK);
140 /* Move SPS into PWM mode. */
141 rtwn_write_1(sc, R92C_SPS0_CTRL, 0x2b);
143 /* Set low byte to 0x0f, leave others unchanged. */
144 rtwn_write_1(sc, R92C_AFE_XTAL_CTRL, 0x0f);
146 /* TODO: check if we need this for 8188CE */
147 if (rs->board_type != R92C_BOARD_TYPE_DONGLE) {
149 /* XXX magic from linux */
150 rtwn_setbits_4(sc, R92C_AFE_XTAL_CTRL, 0x024800, 0);
153 rtwn_setbits_2(sc, R92C_SYS_ISO_CTRL, 0xff00,
154 R92C_SYS_ISO_CTRL_PWC_EV12V | R92C_SYS_ISO_CTRL_DIOR);
158 /* TODO: linux does additional btcoex stuff here */
160 /* Auto enable WLAN. */
161 rtwn_setbits_2(sc, R92C_APS_FSMCO, 0, R92C_APS_FSMCO_APFM_ONMAC);
162 for (ntries = 0; ntries < 1000; ntries++) {
163 if (!(rtwn_read_2(sc, R92C_APS_FSMCO) &
164 R92C_APS_FSMCO_APFM_ONMAC))
168 if (ntries == 1000) {
169 device_printf(sc->sc_dev, "timeout waiting for MAC auto ON\n");
173 /* Enable radio, GPIO and LED functions. */
174 rtwn_write_2(sc, R92C_APS_FSMCO,
175 R92C_APS_FSMCO_AFSM_PCIE |
176 R92C_APS_FSMCO_PDN_EN |
177 R92C_APS_FSMCO_PFM_ALDN);
178 /* Release RF digital isolation. */
179 rtwn_setbits_2(sc, R92C_SYS_ISO_CTRL, R92C_SYS_ISO_CTRL_DIOR, 0);
181 if (rs->chip & R92C_CHIP_92C)
182 rtwn_write_1(sc, R92C_PCIE_CTRL_REG + 3, 0x77);
184 rtwn_write_1(sc, R92C_PCIE_CTRL_REG + 3, 0x22);
186 rtwn_write_4(sc, R92C_INT_MIG, 0);
188 if (rs->board_type != R92C_BOARD_TYPE_DONGLE) {
190 /* XXX magic from linux */
191 rtwn_setbits_1(sc, R92C_AFE_XTAL_CTRL + 2, 0x02, 0);
194 rtwn_setbits_1(sc, R92C_GPIO_MUXCFG, R92C_GPIO_MUXCFG_RFKILL, 0);
196 reg = rtwn_read_1(sc, R92C_GPIO_IO_SEL);
197 if (!(reg & R92C_GPIO_IO_SEL_RFKILL)) {
198 device_printf(sc->sc_dev,
199 "radio is disabled by hardware switch\n");
200 /* XXX how driver will know when radio will be enabled? */
204 /* Initialize MAC. */
205 rtwn_setbits_1(sc, R92C_APSD_CTRL, R92C_APSD_CTRL_OFF, 0);
206 for (ntries = 0; ntries < 200; ntries++) {
207 if (!(rtwn_read_1(sc, R92C_APSD_CTRL) &
208 R92C_APSD_CTRL_OFF_STATUS))
213 device_printf(sc->sc_dev,
214 "timeout waiting for MAC initialization\n");
218 /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
219 rtwn_setbits_2(sc, R92C_CR, 0,
220 R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
221 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
222 R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
223 ((sc->sc_hwcrypto != RTWN_CRYPTO_SW) ? R92C_CR_ENSEC : 0));
225 rtwn_write_4(sc, R92C_MCUTST_1, 0x0);
231 r92ce_power_off(struct rtwn_softc *sc)
233 #ifndef RTWN_WITHOUT_UCODE
234 struct r92c_softc *rs = sc->sc_priv;
236 /* Deinit C2H event handler. */
237 callout_stop(&rs->rs_c2h_report);
238 rs->rs_c2h_paused = 0;
239 rs->rs_c2h_pending = 0;
240 rs->rs_c2h_timeout = hz;
244 /* Disable interrupts. */
245 rtwn_write_4(sc, R92C_HISR, 0);
246 rtwn_write_4(sc, R92C_HIMR, 0);
249 rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
252 rtwn_write_1(sc, R92C_RF_CTRL, 0);
254 /* Reset BB state machine */
255 rtwn_setbits_1(sc, R92C_SYS_FUNC_EN, 0, R92C_SYS_FUNC_EN_BB_GLB_RST);
256 rtwn_setbits_1(sc, R92C_SYS_FUNC_EN, R92C_SYS_FUNC_EN_BB_GLB_RST, 0);
258 /* Disable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
259 rtwn_setbits_2(sc, R92C_CR,
260 R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
261 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
262 R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
266 /* If firmware in ram code, do reset. */
267 #ifndef RTWN_WITHOUT_UCODE
268 if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL)
269 r92ce_fw_reset(sc, RTWN_FW_RESET_SHUTDOWN);
272 /* TODO: linux does additional btcoex stuff here */
273 rtwn_write_2(sc, R92C_AFE_PLL_CTRL, 0x80); /* linux magic number */
274 rtwn_write_1(sc, R92C_SPS0_CTRL, 0x23); /* ditto */
275 rtwn_write_1(sc, R92C_AFE_XTAL_CTRL, 0x0e); /* different with btcoex */
276 rtwn_write_1(sc, R92C_RSV_CTRL, 0x0e);
277 rtwn_write_1(sc, R92C_APS_FSMCO, R92C_APS_FSMCO_PDN_EN);
281 r92ce_init_ampdu(struct rtwn_softc *sc)
284 /* Setup AMPDU aggregation. */
285 rtwn_write_4(sc, R92C_AGGLEN_LMT, 0x99997631); /* MCS7~0 */
286 rtwn_write_1(sc, R92C_AGGR_BREAK_TIME, 0x16);
290 r92ce_post_init(struct rtwn_softc *sc)
292 rtwn_write_2(sc, R92C_FWHW_TXQ_CTRL,
293 0x1f00 | R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
295 rtwn_write_1(sc, R92C_BCN_MAX_ERR, 0xff);
297 /* Perform LO and IQ calibrations. */
299 /* Perform LC calibration. */
302 r92c_pa_bias_init(sc);
304 /* Fix for lower temperature. */
305 rtwn_write_1(sc, 0x15, 0xe9);
307 #ifndef RTWN_WITHOUT_UCODE
308 if (sc->sc_flags & RTWN_FW_LOADED) {
309 struct r92c_softc *rs = sc->sc_priv;
311 if (sc->sc_ratectl_sysctl == RTWN_RATECTL_FW) {
312 /* XXX TODO: fix (see comment in r92cu_init.c) */
313 sc->sc_ratectl = RTWN_RATECTL_NET80211;
315 sc->sc_ratectl = sc->sc_ratectl_sysctl;
317 /* Start C2H event handling. */
318 callout_reset(&rs->rs_c2h_report, rs->rs_c2h_timeout,
319 r92c_handle_c2h_report, sc);
322 sc->sc_ratectl = RTWN_RATECTL_NONE;