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>
49 #include <dev/rtwn/if_rtwn_debug.h>
51 #include <dev/rtwn/usb/rtwn_usb_var.h>
53 #include <dev/rtwn/rtl8192c/r92c_var.h>
55 #include <dev/rtwn/rtl8192c/usb/r92cu.h>
56 #include <dev/rtwn/rtl8192c/usb/r92cu_reg.h>
59 r92cu_init_bb(struct rtwn_softc *sc)
62 /* Enable BB and RF. */
63 rtwn_setbits_2(sc, R92C_SYS_FUNC_EN, 0,
64 R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
65 R92C_SYS_FUNC_EN_DIO_RF);
67 rtwn_write_2(sc, R92C_AFE_PLL_CTRL, 0xdb83);
69 rtwn_write_1(sc, R92C_RF_CTRL,
70 R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
71 rtwn_write_1(sc, R92C_SYS_FUNC_EN,
72 R92C_SYS_FUNC_EN_USBA | R92C_SYS_FUNC_EN_USBD |
73 R92C_SYS_FUNC_EN_BB_GLB_RST | R92C_SYS_FUNC_EN_BBRSTB);
75 rtwn_write_1(sc, R92C_LDOHCI12_CTRL, 0x0f);
76 rtwn_write_1(sc, 0x15, 0xe9);
77 rtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80);
79 r92c_init_bb_common(sc);
83 r92cu_power_on(struct rtwn_softc *sc)
85 #define RTWN_CHK(res) do { \
92 /* Wait for autoload done bit. */
93 for (ntries = 0; ntries < 5000; ntries++) {
94 if (rtwn_read_1(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_PFM_ALDN)
99 device_printf(sc->sc_dev,
100 "timeout waiting for chip autoload\n");
104 /* Unlock ISO/CLK/Power control register. */
105 RTWN_CHK(rtwn_write_1(sc, R92C_RSV_CTRL, 0));
107 /* Move SPS into PWM mode. */
108 RTWN_CHK(rtwn_write_1(sc, R92C_SPS0_CTRL, 0x2b));
110 /* just in case if power_off() was not properly executed. */
113 reg = rtwn_read_1(sc, R92C_LDOV12D_CTRL);
114 if (!(reg & R92C_LDOV12D_CTRL_LDV12_EN)) {
115 RTWN_CHK(rtwn_write_1(sc, R92C_LDOV12D_CTRL,
116 reg | R92C_LDOV12D_CTRL_LDV12_EN));
120 RTWN_CHK(rtwn_setbits_1(sc, R92C_SYS_ISO_CTRL,
121 R92C_SYS_ISO_CTRL_MD2PP, 0));
124 /* Auto enable WLAN. */
125 RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0,
126 R92C_APS_FSMCO_APFM_ONMAC, 1));
128 for (ntries = 0; ntries < 5000; ntries++) {
129 if (!(rtwn_read_2(sc, R92C_APS_FSMCO) &
130 R92C_APS_FSMCO_APFM_ONMAC))
134 if (ntries == 5000) {
135 device_printf(sc->sc_dev,
136 "timeout waiting for MAC auto ON\n");
140 /* Enable radio, GPIO and LED functions. */
141 RTWN_CHK(rtwn_write_2(sc, R92C_APS_FSMCO,
142 R92C_APS_FSMCO_AFSM_HSUS |
143 R92C_APS_FSMCO_PDN_EN |
144 R92C_APS_FSMCO_PFM_ALDN));
146 /* Release RF digital isolation. */
147 RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_SYS_ISO_CTRL,
148 R92C_SYS_ISO_CTRL_DIOR, 0, 1));
150 /* Initialize MAC. */
151 RTWN_CHK(rtwn_setbits_1(sc, R92C_APSD_CTRL,
152 R92C_APSD_CTRL_OFF, 0));
153 for (ntries = 0; ntries < 1000; ntries++) {
154 if (!(rtwn_read_1(sc, R92C_APSD_CTRL) &
155 R92C_APSD_CTRL_OFF_STATUS))
159 if (ntries == 1000) {
160 device_printf(sc->sc_dev,
161 "timeout waiting for MAC initialization\n");
165 /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
166 RTWN_CHK(rtwn_setbits_2(sc, R92C_CR, 0,
167 R92C_CR_HCI_TXDMA_EN | R92C_CR_TXDMA_EN |
168 R92C_CR_HCI_RXDMA_EN | R92C_CR_RXDMA_EN |
169 R92C_CR_PROTOCOL_EN | R92C_CR_SCHEDULE_EN |
170 ((sc->sc_hwcrypto != RTWN_CRYPTO_SW) ? R92C_CR_ENSEC : 0) |
173 RTWN_CHK(rtwn_write_1(sc, 0xfe10, 0x19));
180 r92cu_power_off(struct rtwn_softc *sc)
182 #ifndef RTWN_WITHOUT_UCODE
183 struct r92c_softc *rs = sc->sc_priv;
188 /* Deinit C2H event handler. */
189 #ifndef RTWN_WITHOUT_UCODE
190 callout_stop(&rs->rs_c2h_report);
191 rs->rs_c2h_paused = 0;
192 rs->rs_c2h_pending = 0;
193 rs->rs_c2h_timeout = hz;
196 /* Block all Tx queues. */
197 error = rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
198 if (error == ENXIO) /* hardware gone */
202 rtwn_rf_write(sc, 0, 0, 0);
204 rtwn_write_1(sc, R92C_APSD_CTRL, R92C_APSD_CTRL_OFF);
206 /* Reset BB state machine */
207 rtwn_write_1(sc, R92C_SYS_FUNC_EN,
208 R92C_SYS_FUNC_EN_USBD | R92C_SYS_FUNC_EN_USBA |
209 R92C_SYS_FUNC_EN_BB_GLB_RST);
210 rtwn_write_1(sc, R92C_SYS_FUNC_EN,
211 R92C_SYS_FUNC_EN_USBD | R92C_SYS_FUNC_EN_USBA);
214 * Reset digital sequence
216 #ifndef RTWN_WITHOUT_UCODE
217 if (rtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RDY) {
218 /* Reset MCU ready status */
219 rtwn_write_1(sc, R92C_MCUFWDL, 0);
221 /* If firmware in ram code, do reset */
222 r92c_fw_reset(sc, RTWN_FW_RESET_SHUTDOWN);
226 /* Reset MAC and Enable 8051 */
227 rtwn_write_1(sc, R92C_SYS_FUNC_EN + 1,
228 (R92C_SYS_FUNC_EN_CPUEN |
229 R92C_SYS_FUNC_EN_ELDR |
230 R92C_SYS_FUNC_EN_HWPDN) >> 8);
232 /* Reset MCU ready status */
233 rtwn_write_1(sc, R92C_MCUFWDL, 0);
235 /* Disable MAC clock */
236 rtwn_write_2(sc, R92C_SYS_CLKR,
237 R92C_SYS_CLKR_ANAD16V_EN |
238 R92C_SYS_CLKR_ANA8M |
239 R92C_SYS_CLKR_LOADER_EN |
240 R92C_SYS_CLKR_80M_SSC_DIS |
241 R92C_SYS_CLKR_SYS_EN |
242 R92C_SYS_CLKR_RING_EN |
245 /* Disable AFE PLL */
246 rtwn_write_1(sc, R92C_AFE_PLL_CTRL, 0x80);
248 /* Gated AFE DIG_CLOCK */
249 rtwn_write_2(sc, R92C_AFE_XTAL_CTRL, 0x880F);
251 /* Isolated digital to PON */
252 rtwn_write_1(sc, R92C_SYS_ISO_CTRL,
253 R92C_SYS_ISO_CTRL_MD2PP |
254 R92C_SYS_ISO_CTRL_PA2PCIE |
255 R92C_SYS_ISO_CTRL_PD2CORE |
256 R92C_SYS_ISO_CTRL_IP2MAC |
257 R92C_SYS_ISO_CTRL_DIOP |
258 R92C_SYS_ISO_CTRL_DIOE);
261 * Pull GPIO PIN to balance level and LED control
263 /* 1. Disable GPIO[7:0] */
264 rtwn_write_2(sc, R92C_GPIO_IOSEL, 0x0000);
266 reg = rtwn_read_4(sc, R92C_GPIO_PIN_CTRL) & ~0x0000ff00;
267 reg |= ((reg << 8) & 0x0000ff00) | 0x00ff0000;
268 rtwn_write_4(sc, R92C_GPIO_PIN_CTRL, reg);
270 /* Disable GPIO[10:8] */
271 rtwn_write_1(sc, R92C_MAC_PINMUX_CFG, 0x00);
273 reg = rtwn_read_2(sc, R92C_GPIO_IO_SEL) & ~0x00f0;
274 reg |= (((reg & 0x000f) << 4) | 0x0780);
275 rtwn_write_2(sc, R92C_GPIO_IO_SEL, reg);
277 /* Disable LED0 & 1 */
278 rtwn_write_2(sc, R92C_LEDCFG0, 0x8080);
281 * Reset digital sequence
283 /* Disable ELDR clock */
284 rtwn_write_2(sc, R92C_SYS_CLKR,
285 R92C_SYS_CLKR_ANAD16V_EN |
286 R92C_SYS_CLKR_ANA8M |
287 R92C_SYS_CLKR_LOADER_EN |
288 R92C_SYS_CLKR_80M_SSC_DIS |
289 R92C_SYS_CLKR_SYS_EN |
290 R92C_SYS_CLKR_RING_EN |
293 /* Isolated ELDR to PON */
294 rtwn_write_1(sc, R92C_SYS_ISO_CTRL + 1,
295 (R92C_SYS_ISO_CTRL_DIOR |
296 R92C_SYS_ISO_CTRL_PWC_EV12V) >> 8);
299 * Disable analog sequence
301 /* Disable A15 power */
302 rtwn_write_1(sc, R92C_LDOA15_CTRL, R92C_LDOA15_CTRL_OBUF);
303 /* Disable digital core power */
304 rtwn_setbits_1(sc, R92C_LDOV12D_CTRL,
305 R92C_LDOV12D_CTRL_LDV12_EN, 0);
308 rtwn_write_1(sc, R92C_SPS0_CTRL, 0x23);
310 /* Set USB suspend */
311 rtwn_write_2(sc, R92C_APS_FSMCO,
312 R92C_APS_FSMCO_APDM_HOST |
313 R92C_APS_FSMCO_AFSM_HSUS |
314 R92C_APS_FSMCO_PFM_ALDN);
316 /* Lock ISO/CLK/Power control register. */
317 rtwn_write_1(sc, R92C_RSV_CTRL, 0x0E);
321 r92cu_init_intr(struct rtwn_softc *sc)
323 rtwn_write_4(sc, R92C_HISR, 0xffffffff);
324 rtwn_write_4(sc, R92C_HIMR, 0xffffffff);
328 r92cu_init_tx_agg(struct rtwn_softc *sc)
330 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
333 reg = rtwn_read_4(sc, R92C_TDECTRL);
334 reg = RW(reg, R92C_TDECTRL_BLK_DESC_NUM, uc->tx_agg_desc_num);
335 rtwn_write_4(sc, R92C_TDECTRL, reg);
339 r92cu_init_rx_agg(struct rtwn_softc *sc)
342 /* Rx aggregation (DMA & USB). */
343 rtwn_setbits_1(sc, R92C_TRXDMA_CTRL, 0,
344 R92C_TRXDMA_CTRL_RXDMA_AGG_EN);
345 rtwn_setbits_1(sc, R92C_USB_SPECIAL_OPTION, 0,
346 R92C_USB_SPECIAL_OPTION_AGG_EN);
349 rtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, 48);
350 rtwn_write_1(sc, R92C_USB_DMA_AGG_TO, 4);
351 rtwn_write_1(sc, R92C_USB_AGG_TH, 8);
352 rtwn_write_1(sc, R92C_USB_AGG_TO, 6);
356 r92cu_post_init(struct rtwn_softc *sc)
359 rtwn_write_4(sc, R92C_POWER_STATUS, 0x5);
361 /* Perform LO and IQ calibrations. */
363 /* Perform LC calibration. */
366 /* Fix USB interference issue. */
367 rtwn_write_1(sc, 0xfe40, 0xe0);
368 rtwn_write_1(sc, 0xfe41, 0x8d);
369 rtwn_write_1(sc, 0xfe42, 0x80);
371 r92c_pa_bias_init(sc);
373 /* Fix for lower temperature. */
374 rtwn_write_1(sc, 0x15, 0xe9);
376 #ifndef RTWN_WITHOUT_UCODE
377 if (sc->sc_flags & RTWN_FW_LOADED) {
378 struct r92c_softc *rs = sc->sc_priv;
380 if (sc->sc_ratectl_sysctl == RTWN_RATECTL_FW) {
381 /* XXX firmware RA does not work yet */
382 sc->sc_ratectl = RTWN_RATECTL_NET80211;
384 sc->sc_ratectl = sc->sc_ratectl_sysctl;
386 /* Start C2H event handling. */
387 callout_reset(&rs->rs_c2h_report, rs->rs_c2h_timeout,
388 r92c_handle_c2h_report, sc);
391 sc->sc_ratectl = RTWN_RATECTL_NONE;