xref: /freebsd/sys/dev/rtwn/rtl8192c/r92c_rom.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37 
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41 
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 
45 #include <dev/rtwn/if_rtwnreg.h>
46 #include <dev/rtwn/if_rtwnvar.h>
47 
48 #include <dev/rtwn/if_rtwn_debug.h>
49 #include <dev/rtwn/if_rtwn_efuse.h>
50 
51 #include <dev/rtwn/rtl8192c/r92c.h>
52 #include <dev/rtwn/rtl8192c/r92c_priv.h>
53 #include <dev/rtwn/rtl8192c/r92c_var.h>
54 #include <dev/rtwn/rtl8192c/r92c_rom_image.h>
55 
56 static void
57 r92c_set_chains(struct rtwn_softc *sc)
58 {
59 	struct r92c_softc *rs = sc->sc_priv;
60 
61 	if (rs->chip & R92C_CHIP_92C) {
62 		sc->ntxchains = (rs->chip & R92C_CHIP_92C_1T2R) ? 1 : 2;
63 		sc->nrxchains = 2;
64 	} else {
65 		sc->ntxchains = 1;
66 		sc->nrxchains = 1;
67 	}
68 }
69 
70 void
71 r92c_efuse_postread(struct rtwn_softc *sc)
72 {
73 	struct r92c_softc *rs = sc->sc_priv;
74 
75 	/* XXX Weird but this is what the vendor driver does. */
76 	sc->next_rom_addr = 0x1fa;
77 	(void) rtwn_efuse_read_next(sc, &rs->pa_setting);
78 	RTWN_DPRINTF(sc, RTWN_DEBUG_ROM, "%s: PA setting=0x%x\n", __func__,
79 	    rs->pa_setting);
80 }
81 
82 void
83 r92c_parse_rom(struct rtwn_softc *sc, uint8_t *buf)
84 {
85 	struct r92c_softc *rs = sc->sc_priv;
86 	struct rtwn_r92c_txpwr *rt = rs->rs_txpwr;
87 	struct r92c_rom *rom = (struct r92c_rom *)buf;
88 	int i, j;
89 
90 	rs->board_type = MS(rom->rf_opt1, R92C_ROM_RF1_BOARD_TYPE);
91 	rs->regulatory = MS(rom->rf_opt1, R92C_ROM_RF1_REGULATORY);
92 	RTWN_DPRINTF(sc, RTWN_DEBUG_ROM, "%s: regulatory type=%d\n",
93 	    __func__, rs->regulatory);
94 
95 	/* Need to be set before postinit() (but after preinit()). */
96 	rtwn_r92c_set_rom_opts(sc, buf);
97 	r92c_set_chains(sc);
98 
99 	for (j = 0; j < R92C_GROUP_2G; j++) {
100 		for (i = 0; i < sc->ntxchains; i++) {
101 			rt->cck_tx_pwr[i][j] = rom->cck_tx_pwr[i][j];
102 			rt->ht40_1s_tx_pwr[i][j] = rom->ht40_1s_tx_pwr[i][j];
103 		}
104 
105 		rt->ht40_2s_tx_pwr_diff[0][j] =
106 		    MS(rom->ht40_2s_tx_pwr_diff[j], LOW_PART);
107 		rt->ht20_tx_pwr_diff[0][j] =
108 		    RTWN_SIGN4TO8(MS(rom->ht20_tx_pwr_diff[j],
109 			LOW_PART));
110 		rt->ofdm_tx_pwr_diff[0][j] =
111 		    MS(rom->ofdm_tx_pwr_diff[j], LOW_PART);
112 		rt->ht40_max_pwr[0][j] =
113 		    MS(rom->ht40_max_pwr[j], LOW_PART);
114 		rt->ht20_max_pwr[0][j] =
115 		    MS(rom->ht20_max_pwr[j], LOW_PART);
116 
117 		if (sc->ntxchains > 1) {
118 			rt->ht40_2s_tx_pwr_diff[1][j] =
119 			    MS(rom->ht40_2s_tx_pwr_diff[j], HIGH_PART);
120 			rt->ht20_tx_pwr_diff[1][j] =
121 			    RTWN_SIGN4TO8(MS(rom->ht20_tx_pwr_diff[j],
122 				HIGH_PART));
123 			rt->ofdm_tx_pwr_diff[1][j] =
124 			    MS(rom->ofdm_tx_pwr_diff[j], HIGH_PART);
125 			rt->ht40_max_pwr[1][j] =
126 			    MS(rom->ht40_max_pwr[j], HIGH_PART);
127 			rt->ht20_max_pwr[1][j] =
128 			    MS(rom->ht20_max_pwr[j], HIGH_PART);
129 		}
130 	}
131 
132 	sc->thermal_meter = MS(rom->thermal_meter, R92C_ROM_THERMAL_METER);
133 	if (sc->thermal_meter == R92C_ROM_THERMAL_METER_M)
134 		sc->thermal_meter = 0xff;
135 	IEEE80211_ADDR_COPY(sc->sc_ic.ic_macaddr, rom->macaddr);
136 }
137