xref: /freebsd/sys/dev/rtwn/rtl8192e/r92e_init.c (revision 40fc36ff2c1d14f8ea7237e8f9487ab2f25de86b)
160b9567dSKevin Lo /*-
260b9567dSKevin Lo  * Copyright (c) 2017 Kevin Lo <kevlo@FreeBSD.org>
360b9567dSKevin Lo  * All rights reserved.
460b9567dSKevin Lo  *
560b9567dSKevin Lo  * Redistribution and use in source and binary forms, with or without
660b9567dSKevin Lo  * modification, are permitted provided that the following conditions
760b9567dSKevin Lo  * are met:
860b9567dSKevin Lo  * 1. Redistributions of source code must retain the above copyright
960b9567dSKevin Lo  *    notice, this list of conditions and the following disclaimer.
1060b9567dSKevin Lo  * 2. Redistributions in binary form must reproduce the above copyright
1160b9567dSKevin Lo  *    notice, this list of conditions and the following disclaimer in the
1260b9567dSKevin Lo  *    documentation and/or other materials provided with the distribution.
1360b9567dSKevin Lo  *
1460b9567dSKevin Lo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1560b9567dSKevin Lo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1660b9567dSKevin Lo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1760b9567dSKevin Lo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1860b9567dSKevin Lo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1960b9567dSKevin Lo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2060b9567dSKevin Lo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2160b9567dSKevin Lo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2260b9567dSKevin Lo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2360b9567dSKevin Lo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2460b9567dSKevin Lo  * SUCH DAMAGE.
2560b9567dSKevin Lo  */
2660b9567dSKevin Lo 
2760b9567dSKevin Lo #include <sys/cdefs.h>
2860b9567dSKevin Lo #include "opt_wlan.h"
2960b9567dSKevin Lo 
3060b9567dSKevin Lo #include <sys/param.h>
3160b9567dSKevin Lo #include <sys/lock.h>
3260b9567dSKevin Lo #include <sys/mutex.h>
3360b9567dSKevin Lo #include <sys/mbuf.h>
3460b9567dSKevin Lo #include <sys/kernel.h>
3560b9567dSKevin Lo #include <sys/socket.h>
3660b9567dSKevin Lo #include <sys/systm.h>
3760b9567dSKevin Lo #include <sys/malloc.h>
3860b9567dSKevin Lo #include <sys/queue.h>
3960b9567dSKevin Lo #include <sys/taskqueue.h>
4060b9567dSKevin Lo #include <sys/bus.h>
4160b9567dSKevin Lo #include <sys/endian.h>
4260b9567dSKevin Lo #include <sys/linker.h>
4360b9567dSKevin Lo 
4460b9567dSKevin Lo #include <net/if.h>
4560b9567dSKevin Lo #include <net/ethernet.h>
4660b9567dSKevin Lo #include <net/if_media.h>
4760b9567dSKevin Lo 
4860b9567dSKevin Lo #include <net80211/ieee80211_var.h>
4960b9567dSKevin Lo #include <net80211/ieee80211_radiotap.h>
5060b9567dSKevin Lo 
5160b9567dSKevin Lo #include <dev/rtwn/if_rtwnreg.h>
5260b9567dSKevin Lo #include <dev/rtwn/if_rtwnvar.h>
5360b9567dSKevin Lo 
5460b9567dSKevin Lo #include <dev/rtwn/if_rtwn_debug.h>
5560b9567dSKevin Lo 
5660b9567dSKevin Lo #include <dev/rtwn/rtl8192c/r92c.h>
5760b9567dSKevin Lo 
5860b9567dSKevin Lo #include <dev/rtwn/rtl8192e/r92e.h>
5960b9567dSKevin Lo #include <dev/rtwn/rtl8192e/r92e_reg.h>
6060b9567dSKevin Lo #include <dev/rtwn/rtl8192e/r92e_priv.h>
6160b9567dSKevin Lo #include <dev/rtwn/rtl8192e/r92e_var.h>
6260b9567dSKevin Lo 
6360b9567dSKevin Lo int
r92e_llt_init(struct rtwn_softc * sc)6460b9567dSKevin Lo r92e_llt_init(struct rtwn_softc *sc)
6560b9567dSKevin Lo {
6660b9567dSKevin Lo 	int ntries, error;
6760b9567dSKevin Lo 
6860b9567dSKevin Lo 	error = rtwn_setbits_4(sc, R92C_AUTO_LLT, 0, R92C_AUTO_LLT_INIT);
6960b9567dSKevin Lo 	if (error != 0)
7060b9567dSKevin Lo 		return (error);
7160b9567dSKevin Lo 	for (ntries = 0; ntries < 1000; ntries++) {
7260b9567dSKevin Lo 		if (!(rtwn_read_4(sc, R92C_AUTO_LLT) & R92C_AUTO_LLT_INIT))
7360b9567dSKevin Lo 			return (0);
7460b9567dSKevin Lo 		rtwn_delay(sc, 1);
7560b9567dSKevin Lo 	}
7660b9567dSKevin Lo 	return (ETIMEDOUT);
7760b9567dSKevin Lo }
7860b9567dSKevin Lo 
7960b9567dSKevin Lo static void
r92e_crystalcap_write(struct rtwn_softc * sc)8060b9567dSKevin Lo r92e_crystalcap_write(struct rtwn_softc *sc)
8160b9567dSKevin Lo {
8260b9567dSKevin Lo 	struct r92e_softc *rs = sc->sc_priv;
8360b9567dSKevin Lo 	uint32_t reg;
8460b9567dSKevin Lo 	uint8_t val;
8560b9567dSKevin Lo 
8660b9567dSKevin Lo 	val = rs->crystalcap & 0x3f;
8760b9567dSKevin Lo 	reg = rtwn_bb_read(sc, R92E_AFE_XTAL_CTRL);
8860b9567dSKevin Lo 	rtwn_bb_write(sc, R92E_AFE_XTAL_CTRL,
8960b9567dSKevin Lo 	    RW(reg, R92E_AFE_XTAL_CTRL_ADDR, val | val << 6));
9060b9567dSKevin Lo 	rtwn_bb_write(sc, R92C_AFE_XTAL_CTRL, 0x000f81fb);
9160b9567dSKevin Lo }
9260b9567dSKevin Lo 
9360b9567dSKevin Lo void
r92e_init_bb(struct rtwn_softc * sc)9460b9567dSKevin Lo r92e_init_bb(struct rtwn_softc *sc)
9560b9567dSKevin Lo {
9660b9567dSKevin Lo 	int i, j;
9760b9567dSKevin Lo 
9860b9567dSKevin Lo 	rtwn_setbits_2(sc, R92C_SYS_FUNC_EN, 0,
9960b9567dSKevin Lo 	    R92C_SYS_FUNC_EN_USBA | R92C_SYS_FUNC_EN_USBD);
10060b9567dSKevin Lo 
10160b9567dSKevin Lo 	/* Enable BB and RF. */
10260b9567dSKevin Lo 	rtwn_setbits_2(sc, R92C_SYS_FUNC_EN, 0,
10360b9567dSKevin Lo 	    R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
10460b9567dSKevin Lo 	    R92C_SYS_FUNC_EN_DIO_RF);
10560b9567dSKevin Lo 
10660b9567dSKevin Lo 	/* PathA RF Power On. */
10760b9567dSKevin Lo 	rtwn_write_1(sc, R92C_RF_CTRL,
10860b9567dSKevin Lo 	    R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
10960b9567dSKevin Lo 
11060b9567dSKevin Lo 	/* Write BB initialization values. */
11160b9567dSKevin Lo 	for (i = 0; i < sc->bb_size; i++) {
11260b9567dSKevin Lo 		const struct rtwn_bb_prog *bb_prog = &sc->bb_prog[i];
11360b9567dSKevin Lo 
11460b9567dSKevin Lo 		while (!rtwn_check_condition(sc, bb_prog->cond)) {
11560b9567dSKevin Lo 			KASSERT(bb_prog->next != NULL,
11660b9567dSKevin Lo 			    ("%s: wrong condition value (i %d)\n",
11760b9567dSKevin Lo 			    __func__, i));
11860b9567dSKevin Lo 			bb_prog = bb_prog->next;
11960b9567dSKevin Lo 		}
12060b9567dSKevin Lo 
12160b9567dSKevin Lo 		for (j = 0; j < bb_prog->count; j++) {
12260b9567dSKevin Lo 			RTWN_DPRINTF(sc, RTWN_DEBUG_RESET,
12360b9567dSKevin Lo 			    "BB: reg 0x%03x, val 0x%08x\n",
12460b9567dSKevin Lo 			    bb_prog->reg[j], bb_prog->val[j]);
12560b9567dSKevin Lo 
12660b9567dSKevin Lo 			rtwn_bb_write(sc, bb_prog->reg[j], bb_prog->val[j]);
12760b9567dSKevin Lo 			rtwn_delay(sc, 1);
12860b9567dSKevin Lo 		}
12960b9567dSKevin Lo 	}
13060b9567dSKevin Lo 
13160b9567dSKevin Lo 	/* Write AGC values. */
13260b9567dSKevin Lo 	for (i = 0; i < sc->agc_size; i++) {
13360b9567dSKevin Lo 		const struct rtwn_agc_prog *agc_prog = &sc->agc_prog[i];
13460b9567dSKevin Lo 
13560b9567dSKevin Lo 		while (!rtwn_check_condition(sc, agc_prog->cond)) {
13660b9567dSKevin Lo 			KASSERT(agc_prog->next != NULL,
13760b9567dSKevin Lo 			    ("%s: wrong condition value (2) (i %d)\n",
13860b9567dSKevin Lo 			    __func__, i));
13960b9567dSKevin Lo 			agc_prog = agc_prog->next;
14060b9567dSKevin Lo 		}
14160b9567dSKevin Lo 
14260b9567dSKevin Lo 		for (j = 0; j < agc_prog->count; j++) {
14360b9567dSKevin Lo 			RTWN_DPRINTF(sc, RTWN_DEBUG_RESET,
14460b9567dSKevin Lo 			    "AGC: val 0x%08x\n", agc_prog->val[j]);
14560b9567dSKevin Lo 
14660b9567dSKevin Lo 			rtwn_bb_write(sc, R92C_OFDM0_AGCRSSITABLE,
14760b9567dSKevin Lo 			    agc_prog->val[j]);
14860b9567dSKevin Lo 			rtwn_delay(sc, 1);
14960b9567dSKevin Lo 		}
15060b9567dSKevin Lo 	}
15160b9567dSKevin Lo 
15260b9567dSKevin Lo 	if (rtwn_bb_read(sc, R92C_HSSI_PARAM2(0)) & R92C_HSSI_PARAM2_CCK_HIPWR)
15360b9567dSKevin Lo 		sc->sc_flags |= RTWN_FLAG_CCK_HIPWR;
15460b9567dSKevin Lo 
15560b9567dSKevin Lo 	rtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x00040022);
15660b9567dSKevin Lo 	rtwn_delay(sc, 1);
15760b9567dSKevin Lo 	rtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x00040020);
15860b9567dSKevin Lo 	rtwn_delay(sc, 1);
15960b9567dSKevin Lo 
16060b9567dSKevin Lo 	r92e_crystalcap_write(sc);
16160b9567dSKevin Lo }
16260b9567dSKevin Lo 
16360b9567dSKevin Lo void
r92e_init_rf(struct rtwn_softc * sc)16460b9567dSKevin Lo r92e_init_rf(struct rtwn_softc *sc)
16560b9567dSKevin Lo {
16660b9567dSKevin Lo 	struct r92e_softc *rs = sc->sc_priv;
16760b9567dSKevin Lo 	uint32_t reg, type;
16860b9567dSKevin Lo 	int i, chain, idx, off;
16960b9567dSKevin Lo 
17060b9567dSKevin Lo 	for (chain = 0, i = 0; chain < sc->nrxchains; chain++, i++) {
17160b9567dSKevin Lo 		/* Save RF_ENV control type. */
17260b9567dSKevin Lo 		idx = chain / 2;
17360b9567dSKevin Lo 		off = (chain % 2) * 16;
17460b9567dSKevin Lo 		reg = rtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx));
17560b9567dSKevin Lo 		type = (reg >> off) & 0x10;
17660b9567dSKevin Lo 
17760b9567dSKevin Lo 		/* Set RF_ENV enable. */
17860b9567dSKevin Lo 		rtwn_bb_setbits(sc, R92C_FPGA0_RFIFACEOE(chain),
17960b9567dSKevin Lo 		    0, 0x100000);
18060b9567dSKevin Lo 		rtwn_delay(sc, 1);
18160b9567dSKevin Lo 		/* Set RF_ENV output high. */
18260b9567dSKevin Lo 		rtwn_bb_setbits(sc, R92C_FPGA0_RFIFACEOE(chain),
18360b9567dSKevin Lo 		    0, 0x10);
18460b9567dSKevin Lo 		rtwn_delay(sc, 1);
18560b9567dSKevin Lo 		/* Set address and data lengths of RF registers. */
18660b9567dSKevin Lo 		rtwn_bb_setbits(sc, R92C_HSSI_PARAM2(chain),
18760b9567dSKevin Lo 		    R92C_HSSI_PARAM2_ADDR_LENGTH, 0);
18860b9567dSKevin Lo 		rtwn_delay(sc, 1);
18960b9567dSKevin Lo 		rtwn_bb_setbits(sc, R92C_HSSI_PARAM2(chain),
19060b9567dSKevin Lo 		    R92C_HSSI_PARAM2_DATA_LENGTH, 0);
19160b9567dSKevin Lo 		rtwn_delay(sc, 1);
19260b9567dSKevin Lo 
19360b9567dSKevin Lo 		/* Write RF initialization values for this chain. */
19460b9567dSKevin Lo 		i += r92c_init_rf_chain(sc, &sc->rf_prog[i], chain);
19560b9567dSKevin Lo 
19650e3b6aaSJohn Baldwin 		/* Restore RF_ENV control type. */
19750e3b6aaSJohn Baldwin 		reg = rtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx));
19850e3b6aaSJohn Baldwin 		reg &= ~(0x10 << off) | (type << off);
19950e3b6aaSJohn Baldwin 		rtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(idx), reg);
20050e3b6aaSJohn Baldwin 
20160b9567dSKevin Lo 		/* Cache RF register CHNLBW. */
20260b9567dSKevin Lo 		rs->rf_chnlbw[chain] = rtwn_rf_read(sc, chain, R92C_RF_CHNLBW);
20360b9567dSKevin Lo 	}
20460b9567dSKevin Lo 
20560b9567dSKevin Lo 	/* Turn CCK and OFDM blocks on. */
20660b9567dSKevin Lo 	rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, 0, R92C_RFMOD_CCK_EN);
20760b9567dSKevin Lo 	rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, 0, R92C_RFMOD_OFDM_EN);
20860b9567dSKevin Lo }
20960b9567dSKevin Lo 
21060b9567dSKevin Lo static void
r92e_adj_crystal(struct rtwn_softc * sc)21160b9567dSKevin Lo r92e_adj_crystal(struct rtwn_softc *sc)
21260b9567dSKevin Lo {
21360b9567dSKevin Lo 
21460b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_AFE_PLL_CTRL, R92C_AFE_PLL_CTRL_FREF_SEL, 0);
21560b9567dSKevin Lo 	rtwn_setbits_4(sc, R92E_APE_PLL_CTRL_EXT, 0x00000380, 0);
21660b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_AFE_PLL_CTRL, 0x40, 0);
21760b9567dSKevin Lo 	rtwn_setbits_4(sc, R92E_APE_PLL_CTRL_EXT, 0x00200000, 0);
21860b9567dSKevin Lo }
21960b9567dSKevin Lo 
22060b9567dSKevin Lo int
r92e_power_on(struct rtwn_softc * sc)22160b9567dSKevin Lo r92e_power_on(struct rtwn_softc *sc)
22260b9567dSKevin Lo {
22360b9567dSKevin Lo #define RTWN_CHK(res) do {	\
22460b9567dSKevin Lo 	if (res != 0)		\
22560b9567dSKevin Lo 		return (EIO);	\
22660b9567dSKevin Lo } while(0)
22760b9567dSKevin Lo 	int ntries;
22860b9567dSKevin Lo 
22960b9567dSKevin Lo 	if (rtwn_read_4(sc, R92C_SYS_CFG) & R92C_SYS_CFG_TRP_BT_EN)
23060b9567dSKevin Lo 		RTWN_CHK(rtwn_write_1(sc, R92C_LDO_SWR_CTRL, 0xc3));
23160b9567dSKevin Lo 	else {
23260b9567dSKevin Lo 		RTWN_CHK(rtwn_setbits_4(sc, R92E_LDOV12_CTRL, 0x00100000,
23360b9567dSKevin Lo 		    0x00500000));
23460b9567dSKevin Lo 		RTWN_CHK(rtwn_write_1(sc, R92C_LDO_SWR_CTRL, 0x83));
23560b9567dSKevin Lo 	}
23660b9567dSKevin Lo 
23760b9567dSKevin Lo 	r92e_adj_crystal(sc);
23860b9567dSKevin Lo 
23960b9567dSKevin Lo 	/* Enable WL suspend. */
24060b9567dSKevin Lo 	RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_APS_FSMCO,
24160b9567dSKevin Lo 	    R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE, 0, 1));
24260b9567dSKevin Lo 
24360b9567dSKevin Lo 	/* Disable HWPDN, SW LPS and WL suspend. */
24460b9567dSKevin Lo 	RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_APS_FSMCO,
24560b9567dSKevin Lo 	    R92C_APS_FSMCO_APFM_RSM | R92C_APS_FSMCO_AFSM_HSUS |
24660b9567dSKevin Lo 	    R92C_APS_FSMCO_AFSM_PCIE | R92C_APS_FSMCO_APDM_HPDN, 0, 1));
24760b9567dSKevin Lo 
24860b9567dSKevin Lo 	/* Wait for power ready bit. */
24960b9567dSKevin Lo 	for (ntries = 0; ntries < 5000; ntries++) {
25060b9567dSKevin Lo 		if (rtwn_read_4(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_SUS_HOST)
25160b9567dSKevin Lo 			break;
25260b9567dSKevin Lo 		rtwn_delay(sc, 10);
25360b9567dSKevin Lo 	}
25460b9567dSKevin Lo 	if (ntries == 5000) {
25560b9567dSKevin Lo 		device_printf(sc->sc_dev,
25660b9567dSKevin Lo 		    "timeout waiting for chip power up\n");
25760b9567dSKevin Lo 		return (ETIMEDOUT);
25860b9567dSKevin Lo 	}
25960b9567dSKevin Lo 
26060b9567dSKevin Lo 	/* Release WLON reset. */
26160b9567dSKevin Lo 	RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0,
26260b9567dSKevin Lo 	    R92C_APS_FSMCO_RDY_MACON, 2));
26360b9567dSKevin Lo 
26460b9567dSKevin Lo 	RTWN_CHK(rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0,
26560b9567dSKevin Lo 	    R92C_APS_FSMCO_APFM_ONMAC, 1));
26660b9567dSKevin Lo 	for (ntries = 0; ntries < 5000; ntries++) {
26760b9567dSKevin Lo 		if (!(rtwn_read_2(sc, R92C_APS_FSMCO) &
26860b9567dSKevin Lo 		    R92C_APS_FSMCO_APFM_ONMAC))
26960b9567dSKevin Lo 			break;
27060b9567dSKevin Lo 		rtwn_delay(sc, 10);
27160b9567dSKevin Lo 	}
27260b9567dSKevin Lo 	if (ntries == 5000)
27360b9567dSKevin Lo 		return (ETIMEDOUT);
27460b9567dSKevin Lo 
27560b9567dSKevin Lo 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
27660b9567dSKevin Lo 	RTWN_CHK(rtwn_write_2(sc, R92C_CR, 0));
27760b9567dSKevin Lo 	RTWN_CHK(rtwn_setbits_2(sc, R92C_CR, 0,
27860b9567dSKevin Lo 	    R92C_CR_HCI_TXDMA_EN | R92C_CR_TXDMA_EN |
27960b9567dSKevin Lo 	    R92C_CR_HCI_RXDMA_EN | R92C_CR_RXDMA_EN |
28060b9567dSKevin Lo 	    R92C_CR_PROTOCOL_EN | R92C_CR_SCHEDULE_EN |
28160b9567dSKevin Lo 	    ((sc->sc_hwcrypto != RTWN_CRYPTO_SW) ? R92C_CR_ENSEC : 0) |
28260b9567dSKevin Lo 	    R92C_CR_CALTMR_EN));
28360b9567dSKevin Lo 
28460b9567dSKevin Lo 	return (0);
28560b9567dSKevin Lo }
28660b9567dSKevin Lo 
28760b9567dSKevin Lo void
r92e_power_off(struct rtwn_softc * sc)28860b9567dSKevin Lo r92e_power_off(struct rtwn_softc *sc)
28960b9567dSKevin Lo {
29060b9567dSKevin Lo 	int error, ntries;
29160b9567dSKevin Lo 
29260b9567dSKevin Lo 	/* Stop Rx. */
29360b9567dSKevin Lo 	error = rtwn_write_1(sc, R92C_CR, 0);
29460b9567dSKevin Lo 	if (error == ENXIO)	/* hardware gone */
29560b9567dSKevin Lo 		return;
29660b9567dSKevin Lo 
29760b9567dSKevin Lo 	/* Move card to Low Power state. */
29860b9567dSKevin Lo 	/* Block all Tx queues. */
29960b9567dSKevin Lo 	rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
30060b9567dSKevin Lo 
30160b9567dSKevin Lo 	for (ntries = 0; ntries < 5000; ntries++) {
30260b9567dSKevin Lo 		/* Should be zero if no packet is transmitting. */
30360b9567dSKevin Lo 		if (rtwn_read_4(sc, R88E_SCH_TXCMD) == 0)
30460b9567dSKevin Lo 			break;
30560b9567dSKevin Lo 
30660b9567dSKevin Lo 		rtwn_delay(sc, 10);
30760b9567dSKevin Lo 	}
30860b9567dSKevin Lo 	if (ntries == 5000) {
30960b9567dSKevin Lo 		device_printf(sc->sc_dev, "%s: failed to block Tx queues\n",
31060b9567dSKevin Lo 		    __func__);
31160b9567dSKevin Lo 		return;
31260b9567dSKevin Lo 	}
31360b9567dSKevin Lo 
31460b9567dSKevin Lo 	/* CCK and OFDM are disabled, and clock are gated. */
31560b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_SYS_FUNC_EN, R92C_SYS_FUNC_EN_BBRSTB, 0);
31660b9567dSKevin Lo 
31760b9567dSKevin Lo 	rtwn_delay(sc, 1);
31860b9567dSKevin Lo 
31960b9567dSKevin Lo 	/* Reset whole BB. */
32060b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_SYS_FUNC_EN, R92C_SYS_FUNC_EN_BB_GLB_RST, 0);
32160b9567dSKevin Lo 
32260b9567dSKevin Lo 	/* Reset MAC TRX. */
32360b9567dSKevin Lo 	rtwn_write_1(sc, R92C_CR,
32460b9567dSKevin Lo 	    R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN);
32560b9567dSKevin Lo 
32660b9567dSKevin Lo 	/* Check if removed later. */
32760b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSEC, 0, 1);
32860b9567dSKevin Lo 
32960b9567dSKevin Lo 	/* Respond TxOK to scheduler */
33060b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_DUAL_TSF_RST, 0, R92C_DUAL_TSF_RST_TXOK);
33160b9567dSKevin Lo 
33260b9567dSKevin Lo 	/* Reset MCU. */
33360b9567dSKevin Lo 	rtwn_write_1(sc, R92C_MCUFWDL, 0);
33460b9567dSKevin Lo 
33560b9567dSKevin Lo #ifndef RTWN_WITHOUT_UCODE
33660b9567dSKevin Lo 	/* Reset MCU IO wrapper. */
33760b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_RSV_CTRL + 1, 0x01, 0);
33860b9567dSKevin Lo 
33960b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_SYS_FUNC_EN,
34060b9567dSKevin Lo 	    R92C_SYS_FUNC_EN_CPUEN, 0, 1);
34160b9567dSKevin Lo 
34260b9567dSKevin Lo 	/* Enable MCU IO wrapper. */
34360b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_RSV_CTRL + 1, 0, 0x01);
34460b9567dSKevin Lo #endif
34560b9567dSKevin Lo 
34660b9567dSKevin Lo 	/* Move card to Disabled state. */
34760b9567dSKevin Lo 	/* Turn off RF. */
34860b9567dSKevin Lo 	rtwn_write_1(sc, R92C_RF_CTRL, 0);
34960b9567dSKevin Lo 
35060b9567dSKevin Lo 	/* Switch DPDT_SEL_P output. */
35160b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_LEDCFG2, 0x80, 0);
35260b9567dSKevin Lo 
35360b9567dSKevin Lo 	/* Turn off MAC by HW state machine */
35460b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0, R92C_APS_FSMCO_APFM_OFF,
35560b9567dSKevin Lo 	    1);
35660b9567dSKevin Lo 
35760b9567dSKevin Lo 	for (ntries = 0; ntries < 5000; ntries++) {
35860b9567dSKevin Lo 		/* Wait until it will be disabled. */
35960b9567dSKevin Lo 		if ((rtwn_read_2(sc, R92C_APS_FSMCO) &
36060b9567dSKevin Lo 		    R92C_APS_FSMCO_APFM_OFF) == 0)
36160b9567dSKevin Lo 			break;
36260b9567dSKevin Lo 
36360b9567dSKevin Lo 		rtwn_delay(sc, 10);
36460b9567dSKevin Lo 	}
36560b9567dSKevin Lo 	if (ntries == 5000) {
36660b9567dSKevin Lo 		device_printf(sc->sc_dev, "%s: could not turn off MAC\n",
36760b9567dSKevin Lo 		    __func__);
36860b9567dSKevin Lo 		return;
36960b9567dSKevin Lo 	}
37060b9567dSKevin Lo 
371*40fc36ffSAdrian Chadd #if 0
37260b9567dSKevin Lo 	/* SOP option to disable BG/MB. */
37360b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0xff,
37460b9567dSKevin Lo 	   R92C_APS_FSMCO_SOP_RCK, 3);
37560b9567dSKevin Lo 
37660b9567dSKevin Lo 	/* Unlock small LDO Register. */
37760b9567dSKevin Lo 	rtwn_setbits_1(sc, 0xcc, 0, 0x4);
37860b9567dSKevin Lo 
37960b9567dSKevin Lo 	/* Disable small LDO. */
38060b9567dSKevin Lo 	rtwn_setbits_1(sc, R92C_SPS0_CTRL, 0x1, 0);
381*40fc36ffSAdrian Chadd #endif
38260b9567dSKevin Lo 
38360b9567dSKevin Lo 	/* Enable WL suspend. */
38460b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, R92C_APS_FSMCO_AFSM_PCIE,
38560b9567dSKevin Lo 	    R92C_APS_FSMCO_AFSM_HSUS, 1);
38660b9567dSKevin Lo 
387*40fc36ffSAdrian Chadd #if 0
38860b9567dSKevin Lo 	/* Enable SW LPS. */
38960b9567dSKevin Lo 	rtwn_setbits_1_shift(sc, R92C_APS_FSMCO, 0,
39060b9567dSKevin Lo 	    R92C_APS_FSMCO_APFM_RSM, 1);
391*40fc36ffSAdrian Chadd #endif
39260b9567dSKevin Lo }
393