xref: /freebsd/sys/dev/rtwn/rtl8188e/usb/r88eu_init.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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/rtl8192c/r92c.h>
51 #include <dev/rtwn/rtl8192c/r92c_var.h>
52 
53 #include <dev/rtwn/rtl8188e/usb/r88eu.h>
54 #include <dev/rtwn/rtl8188e/usb/r88eu_reg.h>
55 
56 
57 void
58 r88eu_power_off(struct rtwn_softc *sc)
59 {
60 	uint8_t reg;
61 	int error, ntries;
62 
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 */
67 		return;
68 
69 	/* Stop Rx. */
70 	rtwn_write_1(sc, R92C_CR, 0);
71 
72 	/* Move card to Low Power State. */
73 	/* Block all Tx queues. */
74 	rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
75 
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)
79 			break;
80 
81 		rtwn_delay(sc, 5000);
82 	}
83 	if (ntries == 10) {
84 		device_printf(sc->sc_dev, "%s: failed to block Tx queues\n",
85 		    __func__);
86 		return;
87 	}
88 
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);
91 
92 	rtwn_delay(sc, 1);
93 
94 	/* Reset MAC TRX */
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);
99 
100 	/* check if removed later */
101 	rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSEC, 0, 1);
102 
103 	/* Respond TxOK to scheduler */
104 	rtwn_setbits_1(sc, R92C_DUAL_TSF_RST, 0, 0x20);
105 
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);
110 #endif
111 
112 	/* Reset MCU ready status. */
113 	rtwn_write_1(sc, R92C_MCUFWDL, 0);
114 
115 	/* Disable 32k. */
116 	rtwn_setbits_1(sc, R88E_32K_CTRL, 0x01, 0);
117 
118 	/* Move card to Disabled state. */
119 	/* Turn off RF. */
120 	rtwn_write_1(sc, R92C_RF_CTRL, 0);
121 
122 	/* LDO Sleep mode. */
123 	rtwn_setbits_1(sc, R92C_LPLDO_CTRL, 0, R92C_LPLDO_CTRL_SLEEP);
124 
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);
128 
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)
133 			break;
134 
135 		rtwn_delay(sc, 5000);
136 	}
137 	if (ntries == 10) {
138 		device_printf(sc->sc_dev, "%s: could not turn off MAC\n",
139 		    __func__);
140 		return;
141 	}
142 
143 	/* schmit trigger */
144 	rtwn_setbits_1(sc, R92C_AFE_XTAL_CTRL + 2, 0, 0x80);
145 
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);
149 
150 	/* Enable bandgap mbias in suspend. */
151 	rtwn_write_1(sc, R92C_APS_FSMCO + 3, 0);
152 
153 	/* Clear SIC_EN register. */
154 	rtwn_setbits_1(sc, R92C_GPIO_MUXCFG + 1, 0x10, 0);
155 
156 	/* Set USB suspend enable local register */
157 	rtwn_setbits_1(sc, R92C_USB_SUSPEND, 0, 0x10);
158 
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);
163 
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);
167 
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);
171 
172 	/* Set LNA, TRSW, EX_PA Pin to output mode. */
173 	rtwn_write_4(sc, R88E_BB_PAD_CTRL, 0x00080808);
174 }
175 
176 void
177 r88eu_init_intr(struct rtwn_softc *sc)
178 {
179 	/* TODO: adjust */
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);
187 }
188 
189 void
190 r88eu_init_rx_agg(struct rtwn_softc *sc)
191 {
192 	/* XXX merge? */
193 	rtwn_setbits_1(sc, R92C_TRXDMA_CTRL, 0,
194 	    R92C_TRXDMA_CTRL_RXDMA_AGG_EN);
195 	/* XXX dehardcode */
196 	rtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, 48);
197 	rtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH + 1, 4);
198 }
199 
200 void
201 r88eu_post_init(struct rtwn_softc *sc)
202 {
203 	/* Turn CCK and OFDM blocks on. */
204 	rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, 0, R92C_RFMOD_CCK_EN);
205 	rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, 0, R92C_RFMOD_OFDM_EN);
206 
207 	/* Enable per-packet TX report. */
208 	rtwn_setbits_1(sc, R88E_TX_RPT_CTRL, 0, R88E_TX_RPT1_ENA);
209 
210 	/* Disable Tx if MACID is not associated. */
211 	rtwn_write_4(sc, R88E_MACID_NO_LINK, 0xffffffff);
212 	rtwn_write_4(sc, R88E_MACID_NO_LINK + 4, 0xffffffff);
213 	r88e_macid_enable_link(sc, RTWN_MACID_BC, 1);
214 
215 	/* Perform LO and IQ calibrations. */
216 	r88e_iq_calib(sc);
217 	/* Perform LC calibration. */
218 	r92c_lc_calib(sc);
219 
220 	rtwn_write_1(sc, R92C_USB_HRPWM, 0);
221 
222 	if (sc->sc_ratectl_sysctl == RTWN_RATECTL_FW) {
223 		/* No support (yet?) for f/w rate adaptation. */
224 		sc->sc_ratectl = RTWN_RATECTL_NET80211;
225 	} else
226 		sc->sc_ratectl = sc->sc_ratectl_sysctl;
227 }
228