xref: /freebsd/sys/dev/rtwn/rtl8192e/r92e_rf.c (revision 5f9a2eec337c9cfdbfa800aff8875f5424e847e8)
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/rtl8192e/r92e.h>
5560b9567dSKevin Lo #include <dev/rtwn/rtl8192e/r92e_reg.h>
5660b9567dSKevin Lo 
5760b9567dSKevin Lo uint32_t
r92e_rf_read(struct rtwn_softc * sc,int chain,uint8_t addr)5860b9567dSKevin Lo r92e_rf_read(struct rtwn_softc *sc, int chain, uint8_t addr)
5960b9567dSKevin Lo {
6060b9567dSKevin Lo 	uint32_t val;
6160b9567dSKevin Lo 
6260b9567dSKevin Lo 	val = rtwn_bb_read(sc, R92C_HSSI_PARAM2(chain));
6360b9567dSKevin Lo 	rtwn_bb_write(sc, R92C_HSSI_PARAM2(chain),
6460b9567dSKevin Lo 	    RW(val, R92C_HSSI_PARAM2_READ_ADDR, addr) &
6560b9567dSKevin Lo 	    ~R92C_HSSI_PARAM2_READ_EDGE);
6660b9567dSKevin Lo 
67*5f9a2eecSAdrian Chadd 	rtwn_delay(sc, 10);
6860b9567dSKevin Lo 	rtwn_bb_setbits(sc, R92C_HSSI_PARAM2(0), R92C_HSSI_PARAM2_READ_EDGE, 0);
69*5f9a2eecSAdrian Chadd 	rtwn_delay(sc, 100);
7060b9567dSKevin Lo 	rtwn_bb_setbits(sc, R92C_HSSI_PARAM2(0), 0, R92C_HSSI_PARAM2_READ_EDGE);
7160b9567dSKevin Lo 	rtwn_delay(sc, 20);
7260b9567dSKevin Lo 
7360b9567dSKevin Lo 	if (rtwn_bb_read(sc, R92C_HSSI_PARAM1(chain)) & R92C_HSSI_PARAM1_PI)
7460b9567dSKevin Lo 		val = rtwn_bb_read(sc, R92C_HSPI_READBACK(chain));
7560b9567dSKevin Lo 	else
7660b9567dSKevin Lo 		val = rtwn_bb_read(sc, R92C_LSSI_READBACK(chain));
7760b9567dSKevin Lo 	return (MS(val, R92C_LSSI_READBACK_DATA));
7860b9567dSKevin Lo }
7960b9567dSKevin Lo 
8060b9567dSKevin Lo void
r92e_rf_write(struct rtwn_softc * sc,int chain,uint8_t addr,uint32_t val)8160b9567dSKevin Lo r92e_rf_write(struct rtwn_softc *sc, int chain, uint8_t addr, uint32_t val)
8260b9567dSKevin Lo {
8360b9567dSKevin Lo 
8460b9567dSKevin Lo 	rtwn_bb_setbits(sc, 0x818, 0x20000, 0);
8560b9567dSKevin Lo 	rtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
8660b9567dSKevin Lo 	    SM(R88E_LSSI_PARAM_ADDR, addr) | SM(R92C_LSSI_PARAM_DATA, val));
87*5f9a2eecSAdrian Chadd 	rtwn_delay(sc, 1);
8860b9567dSKevin Lo 	rtwn_bb_setbits(sc, 0x818, 0, 0x20000);
8960b9567dSKevin Lo }
90