xref: /freebsd/sys/dev/rtwn/rtl8812a/r12a_chan.c (revision bf847ea31ae25953d115a57c77ff556e9641cd26)
1 /*-
2  * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_wlan.h"
29 
30 #include <sys/param.h>
31 #include <sys/lock.h>
32 #include <sys/mutex.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
39 #include <sys/taskqueue.h>
40 #include <sys/bus.h>
41 #include <sys/endian.h>
42 #include <sys/linker.h>
43 
44 #include <net/if.h>
45 #include <net/ethernet.h>
46 #include <net/if_media.h>
47 
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_radiotap.h>
50 
51 #include <dev/rtwn/if_rtwnreg.h>
52 #include <dev/rtwn/if_rtwnvar.h>
53 
54 #include <dev/rtwn/if_rtwn_debug.h>
55 #include <dev/rtwn/if_rtwn_ridx.h>
56 #include <dev/rtwn/if_rtwn_rx.h>
57 
58 #include <dev/rtwn/rtl8812a/r12a.h>
59 #include <dev/rtwn/rtl8812a/r12a_reg.h>
60 #include <dev/rtwn/rtl8812a/r12a_var.h>
61 
62 static void
63 r12a_write_txpower_ht(struct rtwn_softc *sc, int chain,
64     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
65 {
66 
67 	/* Write per-MCS Tx power. */
68 	rtwn_bb_write(sc, R12A_TXAGC_MCS3_0(chain),
69 	    SM(R12A_TXAGC_MCS0, power[RTWN_RIDX_HT_MCS(0)]) |
70 	    SM(R12A_TXAGC_MCS1, power[RTWN_RIDX_HT_MCS(1)]) |
71 	    SM(R12A_TXAGC_MCS2, power[RTWN_RIDX_HT_MCS(2)]) |
72 	    SM(R12A_TXAGC_MCS3, power[RTWN_RIDX_HT_MCS(3)]));
73 	rtwn_bb_write(sc, R12A_TXAGC_MCS7_4(chain),
74 	    SM(R12A_TXAGC_MCS4, power[RTWN_RIDX_HT_MCS(4)]) |
75 	    SM(R12A_TXAGC_MCS5, power[RTWN_RIDX_HT_MCS(5)]) |
76 	    SM(R12A_TXAGC_MCS6, power[RTWN_RIDX_HT_MCS(6)]) |
77 	    SM(R12A_TXAGC_MCS7, power[RTWN_RIDX_HT_MCS(7)]));
78 	if (sc->ntxchains >= 2) {
79 		rtwn_bb_write(sc, R12A_TXAGC_MCS11_8(chain),
80 		    SM(R12A_TXAGC_MCS8,  power[RTWN_RIDX_HT_MCS(8)]) |
81 		    SM(R12A_TXAGC_MCS9,  power[RTWN_RIDX_HT_MCS(9)]) |
82 		    SM(R12A_TXAGC_MCS10, power[RTWN_RIDX_HT_MCS(10)]) |
83 		    SM(R12A_TXAGC_MCS11, power[RTWN_RIDX_HT_MCS(11)]));
84 		rtwn_bb_write(sc, R12A_TXAGC_MCS15_12(chain),
85 		    SM(R12A_TXAGC_MCS12, power[RTWN_RIDX_HT_MCS(12)]) |
86 		    SM(R12A_TXAGC_MCS13, power[RTWN_RIDX_HT_MCS(13)]) |
87 		    SM(R12A_TXAGC_MCS14, power[RTWN_RIDX_HT_MCS(14)]) |
88 		    SM(R12A_TXAGC_MCS15, power[RTWN_RIDX_HT_MCS(15)]));
89 	}
90 
91 	/* TODO: HT MCS 16 -> 31 */
92 }
93 
94 static void
95 r12a_write_txpower_vht(struct rtwn_softc *sc, int chain,
96     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
97 {
98 
99 	/* 1SS, MCS 0..3 */
100 	rtwn_bb_write(sc, R12A_TXAGC_NSS1IX3_1IX0(chain),
101 	    SM(R12A_TXAGC_NSS1_MCS0, power[RTWN_RIDX_VHT_MCS(0, 0)]) |
102 	    SM(R12A_TXAGC_NSS1_MCS1, power[RTWN_RIDX_VHT_MCS(0, 1)]) |
103 	    SM(R12A_TXAGC_NSS1_MCS2, power[RTWN_RIDX_VHT_MCS(0, 2)]) |
104 	    SM(R12A_TXAGC_NSS1_MCS3, power[RTWN_RIDX_VHT_MCS(0, 3)]));
105 
106 	/* 1SS, MCS 4..7 */
107 	rtwn_bb_write(sc, R12A_TXAGC_NSS1IX7_1IX4(chain),
108 	    SM(R12A_TXAGC_NSS1_MCS4, power[RTWN_RIDX_VHT_MCS(0, 4)]) |
109 	    SM(R12A_TXAGC_NSS1_MCS5, power[RTWN_RIDX_VHT_MCS(0, 5)]) |
110 	    SM(R12A_TXAGC_NSS1_MCS6, power[RTWN_RIDX_VHT_MCS(0, 6)]) |
111 	    SM(R12A_TXAGC_NSS1_MCS7, power[RTWN_RIDX_VHT_MCS(0, 7)]));
112 
113 	/* 1SS, MCS 8,9 ; 2SS MCS0, 1 */
114 	if (sc->ntxchains == 1) {
115 		rtwn_bb_write(sc, R12A_TXAGC_NSS2IX1_1IX8(chain),
116 		    SM(R12A_TXAGC_NSS1_MCS8, power[RTWN_RIDX_VHT_MCS(0, 8)]) |
117 		    SM(R12A_TXAGC_NSS1_MCS9, power[RTWN_RIDX_VHT_MCS(0, 9)]) |
118 		    SM(R12A_TXAGC_NSS2_MCS0, 0) |
119 		    SM(R12A_TXAGC_NSS2_MCS1, 0));
120 	} else {
121 		rtwn_bb_write(sc, R12A_TXAGC_NSS2IX1_1IX8(chain),
122 		    SM(R12A_TXAGC_NSS1_MCS8, power[RTWN_RIDX_VHT_MCS(0, 8)]) |
123 		    SM(R12A_TXAGC_NSS1_MCS9, power[RTWN_RIDX_VHT_MCS(0, 9)]) |
124 		    SM(R12A_TXAGC_NSS2_MCS0, power[RTWN_RIDX_VHT_MCS(1, 0)]) |
125 		    SM(R12A_TXAGC_NSS2_MCS1, power[RTWN_RIDX_VHT_MCS(1, 1)]));
126 	}
127 
128 	/* 2SS MCS 2..5 */
129 	if (sc->ntxchains > 1) {
130 		rtwn_bb_write(sc, R12A_TXAGC_NSS2IX5_2IX2(chain),
131 		    SM(R12A_TXAGC_NSS2_MCS2, power[RTWN_RIDX_VHT_MCS(1, 2)]) |
132 		    SM(R12A_TXAGC_NSS2_MCS3, power[RTWN_RIDX_VHT_MCS(1, 3)]) |
133 		    SM(R12A_TXAGC_NSS2_MCS4, power[RTWN_RIDX_VHT_MCS(1, 4)]) |
134 		    SM(R12A_TXAGC_NSS2_MCS5, power[RTWN_RIDX_VHT_MCS(1, 5)]));
135 	}
136 
137 	/* 2SS MCS 6..9 */
138 	if (sc->ntxchains > 1) {
139 		rtwn_bb_write(sc, R12A_TXAGC_NSS2IX9_2IX6(chain),
140 		    SM(R12A_TXAGC_NSS2_MCS2, power[RTWN_RIDX_VHT_MCS(1, 6)]) |
141 		    SM(R12A_TXAGC_NSS2_MCS3, power[RTWN_RIDX_VHT_MCS(1, 7)]) |
142 		    SM(R12A_TXAGC_NSS2_MCS4, power[RTWN_RIDX_VHT_MCS(1, 8)]) |
143 		    SM(R12A_TXAGC_NSS2_MCS5, power[RTWN_RIDX_VHT_MCS(1, 9)]));
144 	}
145 
146 	/* TODO: 3SS, 4SS VHT rates */
147 }
148 
149 
150 static void
151 r12a_write_txpower_cck(struct rtwn_softc *sc, int chain,
152     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
153 {
154 
155 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
156 		/* Write per-CCK rate Tx power. */
157 		rtwn_bb_write(sc, R12A_TXAGC_CCK11_1(chain),
158 		    SM(R12A_TXAGC_CCK1,  power[RTWN_RIDX_CCK1]) |
159 		    SM(R12A_TXAGC_CCK2,  power[RTWN_RIDX_CCK2]) |
160 		    SM(R12A_TXAGC_CCK55, power[RTWN_RIDX_CCK55]) |
161 		    SM(R12A_TXAGC_CCK11, power[RTWN_RIDX_CCK11]));
162 	}
163 }
164 
165 static void
166 r12a_write_txpower_ofdm(struct rtwn_softc *sc, int chain,
167     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
168 {
169 
170 	/* Write per-OFDM rate Tx power. */
171 	rtwn_bb_write(sc, R12A_TXAGC_OFDM18_6(chain),
172 	    SM(R12A_TXAGC_OFDM06, power[RTWN_RIDX_OFDM6]) |
173 	    SM(R12A_TXAGC_OFDM09, power[RTWN_RIDX_OFDM9]) |
174 	    SM(R12A_TXAGC_OFDM12, power[RTWN_RIDX_OFDM12]) |
175 	    SM(R12A_TXAGC_OFDM18, power[RTWN_RIDX_OFDM18]));
176 	rtwn_bb_write(sc, R12A_TXAGC_OFDM54_24(chain),
177 	    SM(R12A_TXAGC_OFDM24, power[RTWN_RIDX_OFDM24]) |
178 	    SM(R12A_TXAGC_OFDM36, power[RTWN_RIDX_OFDM36]) |
179 	    SM(R12A_TXAGC_OFDM48, power[RTWN_RIDX_OFDM48]) |
180 	    SM(R12A_TXAGC_OFDM54, power[RTWN_RIDX_OFDM54]));
181 }
182 
183 static void
184 r12a_write_txpower(struct rtwn_softc *sc, int chain,
185     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
186 {
187 
188 	r12a_write_txpower_cck(sc, chain, c, power);
189 	r12a_write_txpower_ofdm(sc, chain, c, power);
190 	r12a_write_txpower_ht(sc, chain, c, power);
191 	r12a_write_txpower_vht(sc, chain, c, power);
192 }
193 
194 static int
195 r12a_get_power_group(struct rtwn_softc *sc, struct ieee80211_channel *c)
196 {
197 	uint8_t chan;
198 	int group;
199 
200 	chan = rtwn_chan2centieee(c);
201 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
202 		if (chan <= 2)			group = 0;
203 		else if (chan <= 5)		group = 1;
204 		else if (chan <= 8)		group = 2;
205 		else if (chan <= 11)		group = 3;
206 		else if (chan <= 14)		group = 4;
207 		else {
208 			KASSERT(0, ("wrong 2GHz channel %d!\n", chan));
209 			return (-1);
210 		}
211 	} else if (IEEE80211_IS_CHAN_5GHZ(c)) {
212 		if (chan < 36)
213 			return (-1);
214 
215 		if (chan <= 42)			group = 0;
216 		else if (chan <= 48)		group = 1;
217 		else if (chan <= 58)		group = 2;
218 		else if (chan <= 64)		group = 3;
219 		else if (chan <= 106)		group = 4;
220 		else if (chan <= 114)		group = 5;
221 		else if (chan <= 122)		group = 6;
222 		else if (chan <= 130)		group = 7;
223 		else if (chan <= 138)		group = 8;
224 		else if (chan <= 144)		group = 9;
225 		else if (chan <= 155)		group = 10;
226 		else if (chan <= 161)		group = 11;
227 		else if (chan <= 171)		group = 12;
228 		else if (chan <= 177)		group = 13;
229 		else {
230 			KASSERT(0, ("wrong 5GHz channel %d!\n", chan));
231 			return (-1);
232 		}
233 	} else {
234 		KASSERT(0, ("wrong channel band (flags %08X)\n", c->ic_flags));
235 		return (-1);
236 	}
237 
238 	return (group);
239 }
240 
241 static void
242 r12a_get_txpower(struct rtwn_softc *sc, int chain,
243     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
244 {
245 	struct r12a_softc *rs = sc->sc_priv;
246 	int i, ridx, group, max_mcs, max_vht_mcs;
247 
248 	/* Determine channel group. */
249 	group = r12a_get_power_group(sc, c);
250 	if (group == -1) {	/* shouldn't happen */
251 		device_printf(sc->sc_dev, "%s: incorrect channel\n", __func__);
252 		return;
253 	}
254 
255 	max_mcs = RTWN_RIDX_HT_MCS(sc->ntxchains * 8 - 1);
256 	max_vht_mcs = RTWN_RIDX_VHT_MCS(sc->ntxchains, 9) - 1;
257 
258 	/* XXX regulatory */
259 	/* XXX net80211 regulatory */
260 
261 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
262 		for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++)
263 			power[ridx] = rs->cck_tx_pwr[chain][group];
264 		for (ridx = RTWN_RIDX_OFDM6; ridx <= max_mcs; ridx++)
265 			power[ridx] = rs->ht40_tx_pwr_2g[chain][group];
266 
267 		for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++)
268 			power[ridx] += rs->ofdm_tx_pwr_diff_2g[chain][0];
269 
270 		for (i = 0; i < sc->ntxchains; i++) {
271 			uint8_t min_mcs;
272 			uint8_t pwr_diff;
273 
274 			if (IEEE80211_IS_CHAN_VHT80(c)) {
275 				/* Vendor driver uses HT40 values here. */
276 				pwr_diff = rs->bw40_tx_pwr_diff_2g[chain][i];
277 			} else
278 			if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_VHT40(c))
279 				pwr_diff = rs->bw40_tx_pwr_diff_2g[chain][i];
280 			else
281 				pwr_diff = rs->bw20_tx_pwr_diff_2g[chain][i];
282 
283 			min_mcs = RTWN_RIDX_HT_MCS(i * 8);
284 			for (ridx = min_mcs; ridx <= max_mcs; ridx++)
285 				power[ridx] += pwr_diff;
286 		}
287 	} else {	/* 5GHz */
288 		/* OFDM + HT */
289 		for (ridx = RTWN_RIDX_OFDM6; ridx <= max_mcs; ridx++)
290 			power[ridx] = rs->ht40_tx_pwr_5g[chain][group];
291 		/* VHT */
292 		for (ridx = RTWN_RIDX_VHT_MCS_SHIFT; ridx <= max_vht_mcs; ridx++)
293 			power[ridx] = rs->ht40_tx_pwr_5g[chain][group];
294 
295 		/* Add power for OFDM rates */
296 		for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++)
297 			power[ridx] += rs->ofdm_tx_pwr_diff_5g[chain][0];
298 
299 		for (i = 0; i < sc->ntxchains; i++) {
300 			uint8_t min_mcs;
301 			uint8_t pwr_diff;
302 
303 			if (IEEE80211_IS_CHAN_VHT80(c)) {
304 				/* TODO: calculate base value. */
305 				pwr_diff = rs->bw80_tx_pwr_diff_5g[chain][i];
306 			} else
307 			if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_VHT40(c))
308 				pwr_diff = rs->bw40_tx_pwr_diff_5g[chain][i];
309 			else
310 				pwr_diff = rs->bw20_tx_pwr_diff_5g[chain][i];
311 
312 			/* Adjust HT rates */
313 			min_mcs = RTWN_RIDX_HT_MCS(i * 8);
314 			for (ridx = min_mcs; ridx <= max_mcs; ridx++)
315 				power[ridx] += pwr_diff;
316 
317 			/* Adjust VHT rates */
318 			for (ridx = RTWN_RIDX_VHT_MCS(i, 0);
319 			    ridx <= RTWN_RIDX_VHT_MCS(i, 9);
320 			    ridx++)
321 				power[ridx] += pwr_diff;
322 
323 		}
324 	}
325 
326 	/* Apply max limit. */
327 	for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++) {
328 		if (power[ridx] > R92C_MAX_TX_PWR)
329 			power[ridx] = R92C_MAX_TX_PWR;
330 	}
331 	for (ridx = RTWN_RIDX_VHT_MCS(0, 0);
332 	    ridx <= RTWN_RIDX_VHT_MCS(3, 9);
333 	    ridx++) {
334 		if (power[ridx] > R92C_MAX_TX_PWR)
335 			power[ridx] = R92C_MAX_TX_PWR;
336 	}
337 
338 #ifdef RTWN_DEBUG
339 	if (sc->sc_debug & RTWN_DEBUG_TXPWR) {
340 		/* Dump per-rate Tx power values. */
341 		printf("Tx power for chain %d:\n", chain);
342 		for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++)
343 			printf("Rate %d = %u\n", ridx, power[ridx]);
344 		/* TODO: dump VHT 0..9 for each spatial stream */
345 	}
346 #endif
347 }
348 
349 static void
350 r12a_set_txpower(struct rtwn_softc *sc, struct ieee80211_channel *c)
351 {
352 	uint8_t power[RTWN_RIDX_COUNT];
353 	int i;
354 
355 	for (i = 0; i < sc->ntxchains; i++) {
356 		memset(power, 0, sizeof(power));
357 		/* Compute per-rate Tx power values. */
358 		r12a_get_txpower(sc, i, c, power);
359 		/* Write per-rate Tx power values to hardware. */
360 		r12a_write_txpower(sc, i, c, power);
361 	}
362 }
363 
364 void
365 r12a_fix_spur(struct rtwn_softc *sc, struct ieee80211_channel *c)
366 {
367 	struct r12a_softc *rs = sc->sc_priv;
368 	uint16_t chan = rtwn_chan2centieee(c);
369 
370 	if (rs->chip & R12A_CHIP_C_CUT) {
371 		if (IEEE80211_IS_CHAN_HT40(c) && chan == 11) {
372 			rtwn_bb_setbits(sc, R12A_RFMOD, 0, 0xc00);
373 			rtwn_bb_setbits(sc, R12A_ADC_BUF_CLK, 0, 0x40000000);
374 		} else {
375 			rtwn_bb_setbits(sc, R12A_RFMOD, 0x400, 0x800);
376 
377 			if (!IEEE80211_IS_CHAN_HT40(c) &&	/* 20 MHz */
378 			    (chan == 13 || chan == 14)) {
379 				rtwn_bb_setbits(sc, R12A_RFMOD, 0, 0x300);
380 				rtwn_bb_setbits(sc, R12A_ADC_BUF_CLK,
381 				    0, 0x40000000);
382 			} else {	/* !80 Mhz */
383 				rtwn_bb_setbits(sc, R12A_RFMOD, 0x100, 0x200);
384 				rtwn_bb_setbits(sc, R12A_ADC_BUF_CLK,
385 				    0x40000000, 0);
386 			}
387 		}
388 	} else {
389 		/* Set ADC clock to 160M to resolve 2480 MHz spur. */
390 		if (!IEEE80211_IS_CHAN_HT40(c) &&	/* 20 MHz */
391 		    (chan == 13 || chan == 14))
392 			rtwn_bb_setbits(sc, R12A_RFMOD, 0, 0x300);
393 		else if (IEEE80211_IS_CHAN_2GHZ(c))
394 			rtwn_bb_setbits(sc, R12A_RFMOD, 0x100, 0x200);
395 	}
396 }
397 
398 static void
399 r12a_set_band(struct rtwn_softc *sc, struct ieee80211_channel *c)
400 {
401 	struct ieee80211com *ic = &sc->sc_ic;
402 	struct r12a_softc *rs = sc->sc_priv;
403 	uint32_t basicrates;
404 	uint8_t swing;
405 	int i;
406 
407 	/* Check if band was changed. */
408 	if ((sc->sc_flags & (RTWN_STARTED | RTWN_RUNNING)) !=
409 	    RTWN_STARTED && IEEE80211_IS_CHAN_5GHZ(c) ^
410 	    !(rtwn_read_1(sc, R12A_CCK_CHECK) & R12A_CCK_CHECK_5GHZ))
411 		return;
412 
413 	rtwn_get_rates(sc, ieee80211_get_suprates(ic, c), NULL, &basicrates,
414 	    NULL, 1);
415 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
416 		rtwn_r12a_set_band_2ghz(sc, basicrates);
417 		swing = rs->tx_bbswing_2g;
418 	} else if (IEEE80211_IS_CHAN_5GHZ(c)) {
419 		rtwn_r12a_set_band_5ghz(sc, basicrates);
420 		swing = rs->tx_bbswing_5g;
421 	} else {
422 		KASSERT(0, ("wrong channel flags %08X\n", c->ic_flags));
423 		return;
424 	}
425 
426 	/* XXX PATH_B is set by vendor driver. */
427 	for (i = 0; i < 2; i++) {
428 		uint16_t val = 0;
429 
430 		switch ((swing >> i * 2) & 0x3) {
431 		case 0:
432 			val = 0x200;	/* 0 dB	*/
433 			break;
434 		case 1:
435 			val = 0x16a;	/* -3 dB */
436 			break;
437 		case 2:
438 			val = 0x101;	/* -6 dB */
439 			break;
440 		case 3:
441 			val = 0xb6;	/* -9 dB */
442 			break;
443 		}
444 
445 		rtwn_bb_setbits(sc, R12A_TX_SCALE(i), R12A_TX_SCALE_SWING_M,
446 		    val << R12A_TX_SCALE_SWING_S);
447 	}
448 }
449 
450 void
451 r12a_set_chan(struct rtwn_softc *sc, struct ieee80211_channel *c)
452 {
453 	uint32_t val;
454 	uint16_t chan;
455 	int i;
456 
457 	r12a_set_band(sc, c);
458 
459 	chan = rtwn_chan2centieee(c);
460 	if (36 <= chan && chan <= 48)
461 		val = 0x09280000;
462 	else if (50 <= chan && chan <= 64)
463 		val = 0x08a60000;
464 	else if (100 <= chan && chan <= 116)
465 		val = 0x08a40000;
466 	else if (118 <= chan)
467 		val = 0x08240000;
468 	else
469 		val = 0x12d40000;
470 
471 	rtwn_bb_setbits(sc, R12A_FC_AREA, 0x1ffe0000, val);
472 
473 	for (i = 0; i < sc->nrxchains; i++) {
474 		if (36 <= chan && chan <= 64)
475 			val = 0x10100;
476 		else if (100 <= chan && chan <= 140)
477 			val = 0x30100;
478 		else if (140 < chan)
479 			val = 0x50100;
480 		else
481 			val = 0x00000;
482 
483 		rtwn_rf_setbits(sc, i, R92C_RF_CHNLBW, 0x70300, val);
484 
485 		/* RTL8812AU-specific */
486 		rtwn_r12a_fix_spur(sc, c);
487 
488 		KASSERT(chan <= 0xff, ("%s: chan %d\n", __func__, chan));
489 		rtwn_rf_setbits(sc, i, R92C_RF_CHNLBW, 0xff, chan);
490 	}
491 
492 #ifdef notyet
493 	if (IEEE80211_IS_CHAN_HT80(c)) {	/* 80 MHz */
494 		rtwn_setbits_2(sc, R92C_WMAC_TRXPTCL_CTL, 0x80, 0x100);
495 
496 		/* TODO */
497 
498 		val = 0x0;
499 	} else
500 #endif
501 	if (IEEE80211_IS_CHAN_HT40(c)) {	/* 40 MHz */
502 		uint8_t ext_chan;
503 
504 		if (IEEE80211_IS_CHAN_HT40U(c))
505 			ext_chan = R12A_DATA_SEC_PRIM_DOWN_20;
506 		else
507 			ext_chan = R12A_DATA_SEC_PRIM_UP_20;
508 
509 		rtwn_setbits_2(sc, R92C_WMAC_TRXPTCL_CTL, 0x100, 0x80);
510 		rtwn_write_1(sc, R12A_DATA_SEC, ext_chan);
511 
512 		rtwn_bb_setbits(sc, R12A_RFMOD, 0x003003c3, 0x00300201);
513 		rtwn_bb_setbits(sc, R12A_ADC_BUF_CLK, 0x40000000, 0);
514 
515 		/* discard high 4 bits */
516 		val = rtwn_bb_read(sc, R12A_RFMOD);
517 		val = RW(val, R12A_RFMOD_EXT_CHAN, ext_chan);
518 		rtwn_bb_write(sc, R12A_RFMOD, val);
519 
520 		val = rtwn_bb_read(sc, R12A_CCA_ON_SEC);
521 		val = RW(val, R12A_CCA_ON_SEC_EXT_CHAN, ext_chan);
522 		rtwn_bb_write(sc, R12A_CCA_ON_SEC, val);
523 
524 		if (rtwn_read_1(sc, 0x837) & 0x04)
525 			val = 0x01800000;
526 		else if (sc->nrxchains == 2 && sc->ntxchains == 2)
527 			val = 0x01c00000;
528 		else
529 			val = 0x02000000;
530 
531 		rtwn_bb_setbits(sc, R12A_L1_PEAK_TH, 0x03c00000, val);
532 
533 		if (IEEE80211_IS_CHAN_HT40U(c))
534 			rtwn_bb_setbits(sc, R92C_CCK0_SYSTEM, 0x10, 0);
535 		else
536 			rtwn_bb_setbits(sc, R92C_CCK0_SYSTEM, 0, 0x10);
537 
538 		val = 0x400;
539 	} else {	/* 20 MHz */
540 		rtwn_setbits_2(sc, R92C_WMAC_TRXPTCL_CTL, 0x180, 0);
541 		rtwn_write_1(sc, R12A_DATA_SEC, R12A_DATA_SEC_NO_EXT);
542 
543 		rtwn_bb_setbits(sc, R12A_RFMOD, 0x003003c3, 0x00300200);
544 		rtwn_bb_setbits(sc, R12A_ADC_BUF_CLK, 0x40000000, 0);
545 
546 		if (sc->nrxchains == 2 && sc->ntxchains == 2)
547 			val = 0x01c00000;
548 		else
549 			val = 0x02000000;
550 
551 		rtwn_bb_setbits(sc, R12A_L1_PEAK_TH, 0x03c00000, val);
552 
553 		val = 0xc00;
554 	}
555 
556 	/* RTL8812AU-specific */
557 	rtwn_r12a_fix_spur(sc, c);
558 
559 	for (i = 0; i < sc->nrxchains; i++)
560 		rtwn_rf_setbits(sc, i, R92C_RF_CHNLBW, 0xc00, val);
561 
562 	/* Set Tx power for this new channel. */
563 	r12a_set_txpower(sc, c);
564 }
565 
566 void
567 r12a_set_band_2ghz(struct rtwn_softc *sc, uint32_t basicrates)
568 {
569 	struct r12a_softc *rs = sc->sc_priv;
570 
571 	/* Enable CCK / OFDM. */
572 	rtwn_bb_setbits(sc, R12A_OFDMCCK_EN,
573 	    0, R12A_OFDMCCK_EN_CCK | R12A_OFDMCCK_EN_OFDM);
574 
575 	rtwn_bb_setbits(sc, R12A_BW_INDICATION, 0x02, 0x01);
576 	rtwn_bb_setbits(sc, R12A_PWED_TH, 0x3e000, 0x2e000);
577 
578 	/* Select AGC table. */
579 	rtwn_bb_setbits(sc, R12A_TXAGC_TABLE_SELECT, 0x03, 0);
580 
581 	switch (rs->rfe_type) {
582 	case 0:
583 	case 1:
584 	case 2:
585 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x77777777);
586 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77777777);
587 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0);
588 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0);
589 		break;
590 	case 3:
591 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x54337770);
592 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x54337770);
593 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0x01000000);
594 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x01000000);
595 		rtwn_bb_setbits(sc, R12A_ANTSEL_SW, 0x0303, 0x01);
596 		break;
597 	case 4:
598 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x77777777);
599 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77777777);
600 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0x00100000);
601 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x00100000);
602 		break;
603 	case 5:
604 		rtwn_write_1(sc, R12A_RFE_PINMUX(0) + 2, 0x77);
605 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77777777);
606 		rtwn_setbits_1(sc, R12A_RFE_INV(0) + 3, 0x01, 0);
607 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0);
608 		break;
609 	default:
610 		break;
611 	}
612 
613 	rtwn_bb_setbits(sc, R12A_TX_PATH, 0xf0, 0x10);
614 	rtwn_bb_setbits(sc, R12A_CCK_RX_PATH, 0x0f000000, 0x01000000);
615 
616 	/* Write basic rates. */
617 	rtwn_set_basicrates(sc, basicrates);
618 
619 	rtwn_write_1(sc, R12A_CCK_CHECK, 0);
620 }
621 
622 void
623 r12a_set_band_5ghz(struct rtwn_softc *sc, uint32_t basicrates)
624 {
625 	struct r12a_softc *rs = sc->sc_priv;
626 	int ntries;
627 
628 	rtwn_write_1(sc, R12A_CCK_CHECK, R12A_CCK_CHECK_5GHZ);
629 
630 	for (ntries = 0; ntries < 100; ntries++) {
631 		if ((rtwn_read_2(sc, R12A_TXPKT_EMPTY) & 0x30) == 0x30)
632 			break;
633 
634 		rtwn_delay(sc, 25);
635 	}
636 	if (ntries == 100) {
637 		device_printf(sc->sc_dev,
638 		    "%s: TXPKT_EMPTY check failed (%04X)\n",
639 		    __func__, rtwn_read_2(sc, R12A_TXPKT_EMPTY));
640 	}
641 
642 	/* Enable OFDM. */
643 	rtwn_bb_setbits(sc, R12A_OFDMCCK_EN, R12A_OFDMCCK_EN_CCK,
644 	    R12A_OFDMCCK_EN_OFDM);
645 
646 	rtwn_bb_setbits(sc, R12A_BW_INDICATION, 0x01, 0x02);
647 	rtwn_bb_setbits(sc, R12A_PWED_TH, 0x3e000, 0x2a000);
648 
649 	/* Select AGC table. */
650 	rtwn_bb_setbits(sc, R12A_TXAGC_TABLE_SELECT, 0x03, 0x01);
651 
652 	switch (rs->rfe_type) {
653 	case 0:
654 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x77337717);
655 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77337717);
656 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0x01000000);
657 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x01000000);
658 		break;
659 	case 1:
660 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x77337717);
661 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77337717);
662 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0);
663 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0);
664 		break;
665 	case 2:
666 	case 4:
667 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x77337777);
668 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77337777);
669 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0x01000000);
670 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x01000000);
671 		break;
672 	case 3:
673 		rtwn_bb_write(sc, R12A_RFE_PINMUX(0), 0x54337717);
674 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x54337717);
675 		rtwn_bb_setbits(sc, R12A_RFE_INV(0), 0x3ff00000, 0x01000000);
676 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x01000000);
677 		rtwn_bb_setbits(sc, R12A_ANTSEL_SW, 0x0303, 0x01);
678 		break;
679 	case 5:
680 		rtwn_write_1(sc, R12A_RFE_PINMUX(0) + 2, 0x33);
681 		rtwn_bb_write(sc, R12A_RFE_PINMUX(1), 0x77337777);
682 		rtwn_setbits_1(sc, R12A_RFE_INV(0) + 3, 0, 0x01);
683 		rtwn_bb_setbits(sc, R12A_RFE_INV(1), 0x3ff00000, 0x01000000);
684 		break;
685 	default:
686 		break;
687 	}
688 
689 	rtwn_bb_setbits(sc, R12A_TX_PATH, 0xf0, 0);
690 	rtwn_bb_setbits(sc, R12A_CCK_RX_PATH, 0, 0x0f000000);
691 
692 	/* Write basic rates. */
693 	rtwn_set_basicrates(sc, basicrates);
694 }
695