1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
47 #include <dev/rtwn/if_rtwnreg.h>
48 #include <dev/rtwn/if_rtwnvar.h>
50 #include <dev/rtwn/rtl8192c/r92c.h>
51 #include <dev/rtwn/rtl8192c/r92c_var.h>
53 #include <dev/rtwn/rtl8188e/usb/r88eu.h>
54 #include <dev/rtwn/rtl8188e/usb/r88eu_reg.h>
58 r88eu_power_off(struct rtwn_softc *sc)
63 /* Disable any kind of TX reports. */
64 error = rtwn_setbits_1(sc, R88E_TX_RPT_CTRL,
65 R88E_TX_RPT1_ENA | R88E_TX_RPT2_ENA, 0);
66 if (error == ENXIO) /* hardware gone */
70 rtwn_write_1(sc, R92C_CR, 0);
72 /* Move card to Low Power State. */
73 /* Block all Tx queues. */
74 rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
76 for (ntries = 0; ntries < 10; ntries++) {
77 /* Should be zero if no packet is transmitting. */
78 if (rtwn_read_4(sc, R88E_SCH_TXCMD) == 0)
84 device_printf(sc->sc_dev, "%s: failed to block Tx queues\n",
89 /* CCK and OFDM are disabled, and clock are gated. */
90 rtwn_setbits_1(sc, R92C_SYS_FUNC_EN, R92C_SYS_FUNC_EN_BBRSTB, 0);
95 rtwn_write_1(sc, R92C_CR,
96 R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
97 R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN |
98 R92C_CR_PROTOCOL_EN | R92C_CR_SCHEDULE_EN);
100 /* check if removed later */
101 rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSEC, 0, 1);
103 /* Respond TxOK to scheduler */
104 rtwn_setbits_1(sc, R92C_DUAL_TSF_RST, 0, 0x20);
106 /* If firmware in ram code, do reset. */
107 #ifndef RTWN_WITHOUT_UCODE
108 if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RDY)
109 r88e_fw_reset(sc, RTWN_FW_RESET_SHUTDOWN);
112 /* Reset MCU ready status. */
113 rtwn_write_1(sc, R92C_MCUFWDL, 0);
116 rtwn_setbits_1(sc, R88E_32K_CTRL, 0x01, 0);
118 /* Move card to Disabled state. */
120 rtwn_write_1(sc, R92C_RF_CTRL, 0);
122 /* LDO Sleep mode. */
123 rtwn_setbits_1(sc, R92C_LPLDO_CTRL, 0, R92C_LPLDO_CTRL_SLEEP);
125 /* Turn off MAC by HW state machine */
126 rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0,
127 R92C_APS_FSMCO_APFM_OFF, 1);
129 for (ntries = 0; ntries < 10; ntries++) {
130 /* Wait until it will be disabled. */
131 if ((rtwn_read_2(sc, R92C_APS_FSMCO) &
132 R92C_APS_FSMCO_APFM_OFF) == 0)
135 rtwn_delay(sc, 5000);
138 device_printf(sc->sc_dev, "%s: could not turn off MAC\n",
144 rtwn_setbits_1(sc, R92C_AFE_XTAL_CTRL + 2, 0, 0x80);
146 /* Enable WL suspend. */
147 rtwn_setbits_1_shift(sc, R92C_APS_FSMCO,
148 R92C_APS_FSMCO_AFSM_PCIE, R92C_APS_FSMCO_AFSM_HSUS, 1);
150 /* Enable bandgap mbias in suspend. */
151 rtwn_write_1(sc, R92C_APS_FSMCO + 3, 0);
153 /* Clear SIC_EN register. */
154 rtwn_setbits_1(sc, R92C_GPIO_MUXCFG + 1, 0x10, 0);
156 /* Set USB suspend enable local register */
157 rtwn_setbits_1(sc, R92C_USB_SUSPEND, 0, 0x10);
159 /* Reset MCU IO Wrapper. */
160 reg = rtwn_read_1(sc, R92C_RSV_CTRL + 1);
161 rtwn_write_1(sc, R92C_RSV_CTRL + 1, reg & ~0x08);
162 rtwn_write_1(sc, R92C_RSV_CTRL + 1, reg | 0x08);
164 /* marked as 'For Power Consumption' code. */
165 rtwn_write_1(sc, R92C_GPIO_OUT, rtwn_read_1(sc, R92C_GPIO_IN));
166 rtwn_write_1(sc, R92C_GPIO_IOSEL, 0xff);
168 rtwn_write_1(sc, R92C_GPIO_IO_SEL,
169 rtwn_read_1(sc, R92C_GPIO_IO_SEL) << 4);
170 rtwn_setbits_1(sc, R92C_GPIO_MOD, 0, 0x0f);
172 /* Set LNA, TRSW, EX_PA Pin to output mode. */
173 rtwn_write_4(sc, R88E_BB_PAD_CTRL, 0x00080808);
177 r88eu_init_intr(struct rtwn_softc *sc)
180 rtwn_write_4(sc, R88E_HISR, 0xffffffff);
181 rtwn_write_4(sc, R88E_HIMR, R88E_HIMR_CPWM | R88E_HIMR_CPWM2 |
182 R88E_HIMR_TBDER | R88E_HIMR_PSTIMEOUT);
183 rtwn_write_4(sc, R88E_HIMRE, R88E_HIMRE_RXFOVW |
184 R88E_HIMRE_TXFOVW | R88E_HIMRE_RXERR | R88E_HIMRE_TXERR);
185 rtwn_setbits_1(sc, R92C_USB_SPECIAL_OPTION, 0,
186 R92C_USB_SPECIAL_OPTION_INT_BULK_SEL);
190 r88eu_init_rx_agg(struct rtwn_softc *sc)
193 rtwn_setbits_1(sc, R92C_TRXDMA_CTRL, 0,
194 R92C_TRXDMA_CTRL_RXDMA_AGG_EN);
196 rtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, 48);
197 rtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH + 1, 4);
201 r88eu_post_init(struct rtwn_softc *sc)
204 /* Enable per-packet TX report. */
205 rtwn_setbits_1(sc, R88E_TX_RPT_CTRL, 0, R88E_TX_RPT1_ENA);
207 /* Disable Tx if MACID is not associated. */
208 rtwn_write_4(sc, R88E_MACID_NO_LINK, 0xffffffff);
209 rtwn_write_4(sc, R88E_MACID_NO_LINK + 4, 0xffffffff);
210 r88e_macid_enable_link(sc, RTWN_MACID_BC, 1);
212 /* Perform LO and IQ calibrations. */
214 /* Perform LC calibration. */
217 rtwn_write_1(sc, R92C_USB_HRPWM, 0);
219 if (sc->sc_ratectl_sysctl == RTWN_RATECTL_FW) {
220 /* No support (yet?) for f/w rate adaptation. */
221 sc->sc_ratectl = RTWN_RATECTL_NET80211;
223 sc->sc_ratectl = sc->sc_ratectl_sysctl;