17453645fSAndriy Voskoboinyk /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
27453645fSAndriy Voskoboinyk
37453645fSAndriy Voskoboinyk /*-
47453645fSAndriy Voskoboinyk * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
57453645fSAndriy Voskoboinyk * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
67453645fSAndriy Voskoboinyk * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
77453645fSAndriy Voskoboinyk *
87453645fSAndriy Voskoboinyk * Permission to use, copy, modify, and distribute this software for any
97453645fSAndriy Voskoboinyk * purpose with or without fee is hereby granted, provided that the above
107453645fSAndriy Voskoboinyk * copyright notice and this permission notice appear in all copies.
117453645fSAndriy Voskoboinyk *
127453645fSAndriy Voskoboinyk * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
137453645fSAndriy Voskoboinyk * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
147453645fSAndriy Voskoboinyk * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
157453645fSAndriy Voskoboinyk * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
167453645fSAndriy Voskoboinyk * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
177453645fSAndriy Voskoboinyk * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
187453645fSAndriy Voskoboinyk * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
197453645fSAndriy Voskoboinyk */
207453645fSAndriy Voskoboinyk
217453645fSAndriy Voskoboinyk #include <sys/cdefs.h>
227453645fSAndriy Voskoboinyk #include "opt_wlan.h"
237453645fSAndriy Voskoboinyk
247453645fSAndriy Voskoboinyk #include <sys/param.h>
257453645fSAndriy Voskoboinyk #include <sys/lock.h>
267453645fSAndriy Voskoboinyk #include <sys/mutex.h>
277453645fSAndriy Voskoboinyk #include <sys/mbuf.h>
287453645fSAndriy Voskoboinyk #include <sys/kernel.h>
297453645fSAndriy Voskoboinyk #include <sys/socket.h>
307453645fSAndriy Voskoboinyk #include <sys/systm.h>
317453645fSAndriy Voskoboinyk #include <sys/malloc.h>
327453645fSAndriy Voskoboinyk #include <sys/queue.h>
337453645fSAndriy Voskoboinyk #include <sys/taskqueue.h>
347453645fSAndriy Voskoboinyk #include <sys/bus.h>
357453645fSAndriy Voskoboinyk #include <sys/endian.h>
367453645fSAndriy Voskoboinyk #include <sys/linker.h>
377453645fSAndriy Voskoboinyk
387453645fSAndriy Voskoboinyk #include <net/if.h>
397453645fSAndriy Voskoboinyk #include <net/ethernet.h>
407453645fSAndriy Voskoboinyk #include <net/if_media.h>
417453645fSAndriy Voskoboinyk
427453645fSAndriy Voskoboinyk #include <net80211/ieee80211_var.h>
437453645fSAndriy Voskoboinyk #include <net80211/ieee80211_radiotap.h>
447453645fSAndriy Voskoboinyk
457453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnreg.h>
467453645fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwnvar.h>
47*fdc9504fSAndriy Voskoboinyk #include <dev/rtwn/if_rtwn_debug.h>
487453645fSAndriy Voskoboinyk
497453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8192c/r92c.h>
507453645fSAndriy Voskoboinyk #include <dev/rtwn/rtl8192c/r92c_reg.h>
517453645fSAndriy Voskoboinyk
52*fdc9504fSAndriy Voskoboinyk /* Registers to save and restore during IQ calibration. */
53*fdc9504fSAndriy Voskoboinyk struct r92c_iq_cal_reg_vals {
54*fdc9504fSAndriy Voskoboinyk uint32_t adda[16];
55*fdc9504fSAndriy Voskoboinyk uint8_t txpause;
56*fdc9504fSAndriy Voskoboinyk uint8_t bcn_ctrl[2];
57*fdc9504fSAndriy Voskoboinyk uint32_t gpio_muxcfg;
58*fdc9504fSAndriy Voskoboinyk uint32_t cck0_afesetting;
59*fdc9504fSAndriy Voskoboinyk uint32_t ofdm0_trxpathena;
60*fdc9504fSAndriy Voskoboinyk uint32_t ofdm0_trmuxpar;
61*fdc9504fSAndriy Voskoboinyk uint32_t fpga0_rfifacesw0;
62*fdc9504fSAndriy Voskoboinyk uint32_t fpga0_rfifacesw1;
63*fdc9504fSAndriy Voskoboinyk uint32_t fpga0_rfifaceoe0;
64*fdc9504fSAndriy Voskoboinyk uint32_t fpga0_rfifaceoe1;
65*fdc9504fSAndriy Voskoboinyk uint32_t config_ant0;
66*fdc9504fSAndriy Voskoboinyk uint32_t config_ant1;
67*fdc9504fSAndriy Voskoboinyk };
68*fdc9504fSAndriy Voskoboinyk
69*fdc9504fSAndriy Voskoboinyk /* XXX TODO: merge */
70*fdc9504fSAndriy Voskoboinyk static int
r92c_iq_calib_chain(struct rtwn_softc * sc,int chain,uint16_t tx[2],uint16_t rx[2])71*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_chain(struct rtwn_softc *sc, int chain, uint16_t tx[2],
72*fdc9504fSAndriy Voskoboinyk uint16_t rx[2])
73*fdc9504fSAndriy Voskoboinyk {
74*fdc9504fSAndriy Voskoboinyk uint32_t status;
75*fdc9504fSAndriy Voskoboinyk
76*fdc9504fSAndriy Voskoboinyk if (chain == 0) { /* IQ calibration for chain 0. */
77*fdc9504fSAndriy Voskoboinyk /* IQ calibration settings for chain 0. */
78*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK_TONE(0), 0x10008c1f);
79*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_TONE(0), 0x10008c1f);
80*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK_PI(0), 0x82140102);
81*fdc9504fSAndriy Voskoboinyk
82*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1) {
83*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_PI(0), 0x28160202);
84*fdc9504fSAndriy Voskoboinyk /* IQ calibration settings for chain 1. */
85*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK_TONE(1), 0x10008c22);
86*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_TONE(1), 0x10008c22);
87*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK_PI(1), 0x82140102);
88*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_PI(1), 0x28160202);
89*fdc9504fSAndriy Voskoboinyk } else
90*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_PI(0), 0x28160502);
91*fdc9504fSAndriy Voskoboinyk
92*fdc9504fSAndriy Voskoboinyk /* LO calibration settings. */
93*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_IQK_AGC_RSP, 0x001028d1);
94*fdc9504fSAndriy Voskoboinyk /* We're doing LO and IQ calibration in one shot. */
95*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_IQK_AGC_PTS, 0xf9000000);
96*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_IQK_AGC_PTS, 0xf8000000);
97*fdc9504fSAndriy Voskoboinyk
98*fdc9504fSAndriy Voskoboinyk } else { /* IQ calibration for chain 1. */
99*fdc9504fSAndriy Voskoboinyk /* We're doing LO and IQ calibration in one shot. */
100*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_IQK_AGC_CONT, 2);
101*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_IQK_AGC_CONT, 0);
102*fdc9504fSAndriy Voskoboinyk }
103*fdc9504fSAndriy Voskoboinyk
104*fdc9504fSAndriy Voskoboinyk /* Give LO and IQ calibrations the time to complete. */
105*fdc9504fSAndriy Voskoboinyk rtwn_delay(sc, 10000);
106*fdc9504fSAndriy Voskoboinyk
107*fdc9504fSAndriy Voskoboinyk /* Read IQ calibration status. */
108*fdc9504fSAndriy Voskoboinyk status = rtwn_bb_read(sc, R92C_RX_POWER_IQK_AFTER(0));
109*fdc9504fSAndriy Voskoboinyk
110*fdc9504fSAndriy Voskoboinyk if (status & (1 << (28 + chain * 3)))
111*fdc9504fSAndriy Voskoboinyk return (0); /* Tx failed. */
112*fdc9504fSAndriy Voskoboinyk /* Read Tx IQ calibration results. */
113*fdc9504fSAndriy Voskoboinyk tx[0] = MS(rtwn_bb_read(sc, R92C_TX_POWER_IQK_BEFORE(chain)),
114*fdc9504fSAndriy Voskoboinyk R92C_POWER_IQK_RESULT);
115*fdc9504fSAndriy Voskoboinyk tx[1] = MS(rtwn_bb_read(sc, R92C_TX_POWER_IQK_AFTER(chain)),
116*fdc9504fSAndriy Voskoboinyk R92C_POWER_IQK_RESULT);
117*fdc9504fSAndriy Voskoboinyk if (tx[0] == 0x142 || tx[1] == 0x042)
118*fdc9504fSAndriy Voskoboinyk return (0); /* Tx failed. */
119*fdc9504fSAndriy Voskoboinyk
120*fdc9504fSAndriy Voskoboinyk if (status & (1 << (27 + chain * 3)))
121*fdc9504fSAndriy Voskoboinyk return (1); /* Rx failed. */
122*fdc9504fSAndriy Voskoboinyk /* Read Rx IQ calibration results. */
123*fdc9504fSAndriy Voskoboinyk rx[0] = MS(rtwn_bb_read(sc, R92C_RX_POWER_IQK_BEFORE(chain)),
124*fdc9504fSAndriy Voskoboinyk R92C_POWER_IQK_RESULT);
125*fdc9504fSAndriy Voskoboinyk rx[1] = MS(rtwn_bb_read(sc, R92C_RX_POWER_IQK_AFTER(chain)),
126*fdc9504fSAndriy Voskoboinyk R92C_POWER_IQK_RESULT);
127*fdc9504fSAndriy Voskoboinyk if (rx[0] == 0x132 || rx[1] == 0x036)
128*fdc9504fSAndriy Voskoboinyk return (1); /* Rx failed. */
129*fdc9504fSAndriy Voskoboinyk
130*fdc9504fSAndriy Voskoboinyk return (3); /* Both Tx and Rx succeeded. */
131*fdc9504fSAndriy Voskoboinyk }
132*fdc9504fSAndriy Voskoboinyk
133*fdc9504fSAndriy Voskoboinyk static void
r92c_iq_calib_run(struct rtwn_softc * sc,int n,uint16_t tx[2][2],uint16_t rx[2][2],struct r92c_iq_cal_reg_vals * vals)134*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_run(struct rtwn_softc *sc, int n, uint16_t tx[2][2],
135*fdc9504fSAndriy Voskoboinyk uint16_t rx[2][2], struct r92c_iq_cal_reg_vals *vals)
136*fdc9504fSAndriy Voskoboinyk {
137*fdc9504fSAndriy Voskoboinyk /* Registers to save and restore during IQ calibration. */
138*fdc9504fSAndriy Voskoboinyk static const uint16_t reg_adda[16] = {
139*fdc9504fSAndriy Voskoboinyk 0x85c, 0xe6c, 0xe70, 0xe74,
140*fdc9504fSAndriy Voskoboinyk 0xe78, 0xe7c, 0xe80, 0xe84,
141*fdc9504fSAndriy Voskoboinyk 0xe88, 0xe8c, 0xed0, 0xed4,
142*fdc9504fSAndriy Voskoboinyk 0xed8, 0xedc, 0xee0, 0xeec
143*fdc9504fSAndriy Voskoboinyk };
144*fdc9504fSAndriy Voskoboinyk int i, chain;
145*fdc9504fSAndriy Voskoboinyk uint32_t hssi_param1;
146*fdc9504fSAndriy Voskoboinyk
147*fdc9504fSAndriy Voskoboinyk if (n == 0) {
148*fdc9504fSAndriy Voskoboinyk for (i = 0; i < nitems(reg_adda); i++)
149*fdc9504fSAndriy Voskoboinyk vals->adda[i] = rtwn_bb_read(sc, reg_adda[i]);
150*fdc9504fSAndriy Voskoboinyk
151*fdc9504fSAndriy Voskoboinyk vals->txpause = rtwn_read_1(sc, R92C_TXPAUSE);
152*fdc9504fSAndriy Voskoboinyk vals->bcn_ctrl[0] = rtwn_read_1(sc, R92C_BCN_CTRL(0));
153*fdc9504fSAndriy Voskoboinyk vals->bcn_ctrl[1] = rtwn_read_1(sc, R92C_BCN_CTRL(1));
154*fdc9504fSAndriy Voskoboinyk vals->gpio_muxcfg = rtwn_read_4(sc, R92C_GPIO_MUXCFG);
155*fdc9504fSAndriy Voskoboinyk }
156*fdc9504fSAndriy Voskoboinyk
157*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains == 1) {
158*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, reg_adda[0], 0x0b1b25a0);
159*fdc9504fSAndriy Voskoboinyk for (i = 1; i < nitems(reg_adda); i++)
160*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, reg_adda[i], 0x0bdb25a0);
161*fdc9504fSAndriy Voskoboinyk } else {
162*fdc9504fSAndriy Voskoboinyk for (i = 0; i < nitems(reg_adda); i++)
163*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, reg_adda[i], 0x04db25a4);
164*fdc9504fSAndriy Voskoboinyk }
165*fdc9504fSAndriy Voskoboinyk
166*fdc9504fSAndriy Voskoboinyk hssi_param1 = rtwn_bb_read(sc, R92C_HSSI_PARAM1(0));
167*fdc9504fSAndriy Voskoboinyk if (!(hssi_param1 & R92C_HSSI_PARAM1_PI)) {
168*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_HSSI_PARAM1(0),
169*fdc9504fSAndriy Voskoboinyk hssi_param1 | R92C_HSSI_PARAM1_PI);
170*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_HSSI_PARAM1(1),
171*fdc9504fSAndriy Voskoboinyk hssi_param1 | R92C_HSSI_PARAM1_PI);
172*fdc9504fSAndriy Voskoboinyk }
173*fdc9504fSAndriy Voskoboinyk
174*fdc9504fSAndriy Voskoboinyk if (n == 0) {
175*fdc9504fSAndriy Voskoboinyk vals->cck0_afesetting = rtwn_bb_read(sc, R92C_CCK0_AFESETTING);
176*fdc9504fSAndriy Voskoboinyk vals->ofdm0_trxpathena =
177*fdc9504fSAndriy Voskoboinyk rtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA);
178*fdc9504fSAndriy Voskoboinyk vals->ofdm0_trmuxpar = rtwn_bb_read(sc, R92C_OFDM0_TRMUXPAR);
179*fdc9504fSAndriy Voskoboinyk vals->fpga0_rfifacesw0 =
180*fdc9504fSAndriy Voskoboinyk rtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(0));
181*fdc9504fSAndriy Voskoboinyk vals->fpga0_rfifacesw1 =
182*fdc9504fSAndriy Voskoboinyk rtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(1));
183*fdc9504fSAndriy Voskoboinyk vals->fpga0_rfifaceoe0 =
184*fdc9504fSAndriy Voskoboinyk rtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(0));
185*fdc9504fSAndriy Voskoboinyk vals->fpga0_rfifaceoe1 =
186*fdc9504fSAndriy Voskoboinyk rtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(1));
187*fdc9504fSAndriy Voskoboinyk vals->config_ant0 = rtwn_bb_read(sc, R92C_CONFIG_ANT(0));
188*fdc9504fSAndriy Voskoboinyk vals->config_ant1 = rtwn_bb_read(sc, R92C_CONFIG_ANT(1));
189*fdc9504fSAndriy Voskoboinyk }
190*fdc9504fSAndriy Voskoboinyk
191*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_CCK0_AFESETTING, 0, 0x0f000000);
192*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, 0x03a05600);
193*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_OFDM0_TRMUXPAR, 0x000800e4);
194*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(1), 0x22204000);
195*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_FPGA0_RFIFACESW(0), 0, 0x04000400);
196*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_FPGA0_RFIFACEOE(0), 0x400, 0);
197*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_FPGA0_RFIFACEOE(1), 0x400, 0);
198*fdc9504fSAndriy Voskoboinyk
199*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1) {
200*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_LSSI_PARAM(0), 0x00010000);
201*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_LSSI_PARAM(1), 0x00010000);
202*fdc9504fSAndriy Voskoboinyk }
203*fdc9504fSAndriy Voskoboinyk
204*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_TXPAUSE,
205*fdc9504fSAndriy Voskoboinyk R92C_TX_QUEUE_AC | R92C_TX_QUEUE_MGT | R92C_TX_QUEUE_HIGH);
206*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_BCN_CTRL(0),
207*fdc9504fSAndriy Voskoboinyk vals->bcn_ctrl[0] & ~R92C_BCN_CTRL_EN_BCN);
208*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_BCN_CTRL(1),
209*fdc9504fSAndriy Voskoboinyk vals->bcn_ctrl[1] & ~R92C_BCN_CTRL_EN_BCN);
210*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_GPIO_MUXCFG,
211*fdc9504fSAndriy Voskoboinyk vals->gpio_muxcfg & ~R92C_GPIO_MUXCFG_ENBT);
212*fdc9504fSAndriy Voskoboinyk
213*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_CONFIG_ANT(0), 0x00080000);
214*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1)
215*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_CONFIG_ANT(1), 0x00080000);
216*fdc9504fSAndriy Voskoboinyk
217*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_IQK, 0x80800000);
218*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK, 0x01007c00);
219*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK, 0x01004800);
220*fdc9504fSAndriy Voskoboinyk
221*fdc9504fSAndriy Voskoboinyk for (chain = 0; chain < sc->ntxchains; chain++) {
222*fdc9504fSAndriy Voskoboinyk if (chain > 0) {
223*fdc9504fSAndriy Voskoboinyk /* Put chain 0 on standby. */
224*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_IQK, 0);
225*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_LSSI_PARAM(0), 0x00010000);
226*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_IQK, 0x80800000);
227*fdc9504fSAndriy Voskoboinyk
228*fdc9504fSAndriy Voskoboinyk /* Enable chain 1. */
229*fdc9504fSAndriy Voskoboinyk for (i = 0; i < nitems(reg_adda); i++)
230*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, reg_adda[i], 0x0b1b25a4);
231*fdc9504fSAndriy Voskoboinyk }
232*fdc9504fSAndriy Voskoboinyk
233*fdc9504fSAndriy Voskoboinyk /* Run IQ calibration twice. */
234*fdc9504fSAndriy Voskoboinyk for (i = 0; i < 2; i++) {
235*fdc9504fSAndriy Voskoboinyk int ret;
236*fdc9504fSAndriy Voskoboinyk
237*fdc9504fSAndriy Voskoboinyk ret = r92c_iq_calib_chain(sc, chain,
238*fdc9504fSAndriy Voskoboinyk tx[chain], rx[chain]);
239*fdc9504fSAndriy Voskoboinyk if (ret == 0) {
240*fdc9504fSAndriy Voskoboinyk RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB,
241*fdc9504fSAndriy Voskoboinyk "%s: chain %d: Tx failed.\n",
242*fdc9504fSAndriy Voskoboinyk __func__, chain);
243*fdc9504fSAndriy Voskoboinyk tx[chain][0] = 0xff;
244*fdc9504fSAndriy Voskoboinyk tx[chain][1] = 0xff;
245*fdc9504fSAndriy Voskoboinyk rx[chain][0] = 0xff;
246*fdc9504fSAndriy Voskoboinyk rx[chain][1] = 0xff;
247*fdc9504fSAndriy Voskoboinyk } else if (ret == 1) {
248*fdc9504fSAndriy Voskoboinyk RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB,
249*fdc9504fSAndriy Voskoboinyk "%s: chain %d: Rx failed.\n",
250*fdc9504fSAndriy Voskoboinyk __func__, chain);
251*fdc9504fSAndriy Voskoboinyk rx[chain][0] = 0xff;
252*fdc9504fSAndriy Voskoboinyk rx[chain][1] = 0xff;
253*fdc9504fSAndriy Voskoboinyk } else if (ret == 3) {
254*fdc9504fSAndriy Voskoboinyk RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB,
255*fdc9504fSAndriy Voskoboinyk "%s: chain %d: Both Tx and Rx "
256*fdc9504fSAndriy Voskoboinyk "succeeded.\n", __func__, chain);
257*fdc9504fSAndriy Voskoboinyk }
258*fdc9504fSAndriy Voskoboinyk }
259*fdc9504fSAndriy Voskoboinyk
260*fdc9504fSAndriy Voskoboinyk RTWN_DPRINTF(sc, RTWN_DEBUG_CALIB,
261*fdc9504fSAndriy Voskoboinyk "%s: results for run %d chain %d: tx[0] 0x%x, "
262*fdc9504fSAndriy Voskoboinyk "tx[1] 0x%x, rx[0] 0x%x, rx[1] 0x%x\n", __func__, n, chain,
263*fdc9504fSAndriy Voskoboinyk tx[chain][0], tx[chain][1], rx[chain][0], rx[chain][1]);
264*fdc9504fSAndriy Voskoboinyk }
265*fdc9504fSAndriy Voskoboinyk
266*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_CCK0_AFESETTING, vals->cck0_afesetting);
267*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, vals->ofdm0_trxpathena);
268*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(0), vals->fpga0_rfifacesw0);
269*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(1), vals->fpga0_rfifacesw1);
270*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_OFDM0_TRMUXPAR, vals->ofdm0_trmuxpar);
271*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(0), vals->fpga0_rfifaceoe0);
272*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(1), vals->fpga0_rfifaceoe1);
273*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_CONFIG_ANT(0), vals->config_ant0);
274*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_CONFIG_ANT(1), vals->config_ant1);
275*fdc9504fSAndriy Voskoboinyk
276*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_FPGA0_IQK, 0);
277*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_LSSI_PARAM(0), 0x00032ed3);
278*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1)
279*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_LSSI_PARAM(1), 0x00032ed3);
280*fdc9504fSAndriy Voskoboinyk
281*fdc9504fSAndriy Voskoboinyk if (n != 0) {
282*fdc9504fSAndriy Voskoboinyk if (!(hssi_param1 & R92C_HSSI_PARAM1_PI)) {
283*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_HSSI_PARAM1(0), hssi_param1);
284*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_HSSI_PARAM1(1), hssi_param1);
285*fdc9504fSAndriy Voskoboinyk }
286*fdc9504fSAndriy Voskoboinyk
287*fdc9504fSAndriy Voskoboinyk for (i = 0; i < nitems(reg_adda); i++)
288*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, reg_adda[i], vals->adda[i]);
289*fdc9504fSAndriy Voskoboinyk
290*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_TXPAUSE, vals->txpause);
291*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_BCN_CTRL(0), vals->bcn_ctrl[0]);
292*fdc9504fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_BCN_CTRL(1), vals->bcn_ctrl[1]);
293*fdc9504fSAndriy Voskoboinyk rtwn_write_4(sc, R92C_GPIO_MUXCFG, vals->gpio_muxcfg);
294*fdc9504fSAndriy Voskoboinyk
295*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_TX_IQK_TONE(0), 0x01008c00);
296*fdc9504fSAndriy Voskoboinyk rtwn_bb_write(sc, R92C_RX_IQK_TONE(0), 0x01008c00);
297*fdc9504fSAndriy Voskoboinyk }
298*fdc9504fSAndriy Voskoboinyk }
299*fdc9504fSAndriy Voskoboinyk
300*fdc9504fSAndriy Voskoboinyk #define RTWN_IQ_CAL_MAX_TOLERANCE 5
301*fdc9504fSAndriy Voskoboinyk static int
r92c_iq_calib_compare_results(struct rtwn_softc * sc,uint16_t tx1[2][2],uint16_t rx1[2][2],uint16_t tx2[2][2],uint16_t rx2[2][2])302*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_compare_results(struct rtwn_softc *sc, uint16_t tx1[2][2],
303*fdc9504fSAndriy Voskoboinyk uint16_t rx1[2][2], uint16_t tx2[2][2], uint16_t rx2[2][2])
304*fdc9504fSAndriy Voskoboinyk {
305*fdc9504fSAndriy Voskoboinyk int chain, i, tx_ok[2], rx_ok[2];
306*fdc9504fSAndriy Voskoboinyk
307*fdc9504fSAndriy Voskoboinyk tx_ok[0] = tx_ok[1] = rx_ok[0] = rx_ok[1] = 0;
308*fdc9504fSAndriy Voskoboinyk for (chain = 0; chain < sc->ntxchains; chain++) {
309*fdc9504fSAndriy Voskoboinyk for (i = 0; i < 2; i++) {
310*fdc9504fSAndriy Voskoboinyk if (tx1[chain][i] == 0xff || tx2[chain][i] == 0xff ||
311*fdc9504fSAndriy Voskoboinyk rx1[chain][i] == 0xff || rx2[chain][i] == 0xff)
312*fdc9504fSAndriy Voskoboinyk continue;
313*fdc9504fSAndriy Voskoboinyk
314*fdc9504fSAndriy Voskoboinyk tx_ok[chain] = (abs(tx1[chain][i] - tx2[chain][i]) <=
315*fdc9504fSAndriy Voskoboinyk RTWN_IQ_CAL_MAX_TOLERANCE);
316*fdc9504fSAndriy Voskoboinyk
317*fdc9504fSAndriy Voskoboinyk rx_ok[chain] = (abs(rx1[chain][i] - rx2[chain][i]) <=
318*fdc9504fSAndriy Voskoboinyk RTWN_IQ_CAL_MAX_TOLERANCE);
319*fdc9504fSAndriy Voskoboinyk }
320*fdc9504fSAndriy Voskoboinyk }
321*fdc9504fSAndriy Voskoboinyk
322*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1)
323*fdc9504fSAndriy Voskoboinyk return (tx_ok[0] && tx_ok[1] && rx_ok[0] && rx_ok[1]);
324*fdc9504fSAndriy Voskoboinyk else
325*fdc9504fSAndriy Voskoboinyk return (tx_ok[0] && rx_ok[0]);
326*fdc9504fSAndriy Voskoboinyk }
327*fdc9504fSAndriy Voskoboinyk #undef RTWN_IQ_CAL_MAX_TOLERANCE
328*fdc9504fSAndriy Voskoboinyk
329*fdc9504fSAndriy Voskoboinyk static void
r92c_iq_calib_write_results(struct rtwn_softc * sc,uint16_t tx[2],uint16_t rx[2],int chain)330*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_write_results(struct rtwn_softc *sc, uint16_t tx[2],
331*fdc9504fSAndriy Voskoboinyk uint16_t rx[2], int chain)
332*fdc9504fSAndriy Voskoboinyk {
333*fdc9504fSAndriy Voskoboinyk uint32_t reg, val, x;
334*fdc9504fSAndriy Voskoboinyk long y, tx_c;
335*fdc9504fSAndriy Voskoboinyk
336*fdc9504fSAndriy Voskoboinyk if (tx[0] == 0xff || tx[1] == 0xff)
337*fdc9504fSAndriy Voskoboinyk return;
338*fdc9504fSAndriy Voskoboinyk
339*fdc9504fSAndriy Voskoboinyk reg = rtwn_bb_read(sc, R92C_OFDM0_TXIQIMBALANCE(chain));
340*fdc9504fSAndriy Voskoboinyk val = ((reg >> 22) & 0x3ff);
341*fdc9504fSAndriy Voskoboinyk x = tx[0];
342*fdc9504fSAndriy Voskoboinyk if (x & 0x00000200)
343*fdc9504fSAndriy Voskoboinyk x |= 0xfffffc00;
344*fdc9504fSAndriy Voskoboinyk reg = (((x * val) >> 8) & 0x3ff);
345*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_TXIQIMBALANCE(chain), 0x3ff, reg);
346*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_ECCATHRESHOLD, 0x80000000,
347*fdc9504fSAndriy Voskoboinyk ((x * val) & 0x80) << 24);
348*fdc9504fSAndriy Voskoboinyk
349*fdc9504fSAndriy Voskoboinyk y = tx[1];
350*fdc9504fSAndriy Voskoboinyk if (y & 0x00000200)
351*fdc9504fSAndriy Voskoboinyk y |= 0xfffffc00;
352*fdc9504fSAndriy Voskoboinyk tx_c = (y * val) >> 8;
353*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_TXAFE(chain), 0xf0000000,
354*fdc9504fSAndriy Voskoboinyk (tx_c & 0x3c0) << 22);
355*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_TXIQIMBALANCE(chain), 0x003f0000,
356*fdc9504fSAndriy Voskoboinyk (tx_c & 0x3f) << 16);
357*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_ECCATHRESHOLD, 0x20000000,
358*fdc9504fSAndriy Voskoboinyk ((y * val) & 0x80) << 22);
359*fdc9504fSAndriy Voskoboinyk
360*fdc9504fSAndriy Voskoboinyk if (rx[0] == 0xff || rx[1] == 0xff)
361*fdc9504fSAndriy Voskoboinyk return;
362*fdc9504fSAndriy Voskoboinyk
363*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_RXIQIMBALANCE(chain), 0x3ff,
364*fdc9504fSAndriy Voskoboinyk rx[0] & 0x3ff);
365*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_RXIQIMBALANCE(chain), 0xfc00,
366*fdc9504fSAndriy Voskoboinyk (rx[1] & 0x3f) << 10);
367*fdc9504fSAndriy Voskoboinyk
368*fdc9504fSAndriy Voskoboinyk if (chain == 0) {
369*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_RXIQEXTANTA, 0xf0000000,
370*fdc9504fSAndriy Voskoboinyk (rx[1] & 0x3c0) << 22);
371*fdc9504fSAndriy Voskoboinyk } else {
372*fdc9504fSAndriy Voskoboinyk rtwn_bb_setbits(sc, R92C_OFDM0_AGCRSSITABLE, 0xf000,
373*fdc9504fSAndriy Voskoboinyk (rx[1] & 0x3c0) << 6);
374*fdc9504fSAndriy Voskoboinyk }
375*fdc9504fSAndriy Voskoboinyk }
376*fdc9504fSAndriy Voskoboinyk
377*fdc9504fSAndriy Voskoboinyk #define RTWN_IQ_CAL_NRUN 3
3787453645fSAndriy Voskoboinyk void
r92c_iq_calib(struct rtwn_softc * sc)3797453645fSAndriy Voskoboinyk r92c_iq_calib(struct rtwn_softc *sc)
3807453645fSAndriy Voskoboinyk {
381*fdc9504fSAndriy Voskoboinyk struct r92c_iq_cal_reg_vals vals;
382*fdc9504fSAndriy Voskoboinyk uint16_t tx[RTWN_IQ_CAL_NRUN][2][2], rx[RTWN_IQ_CAL_NRUN][2][2];
383*fdc9504fSAndriy Voskoboinyk int n, valid;
384*fdc9504fSAndriy Voskoboinyk
385*fdc9504fSAndriy Voskoboinyk valid = 0;
386*fdc9504fSAndriy Voskoboinyk for (n = 0; n < RTWN_IQ_CAL_NRUN; n++) {
387*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_run(sc, n, tx[n], rx[n], &vals);
388*fdc9504fSAndriy Voskoboinyk
389*fdc9504fSAndriy Voskoboinyk if (n == 0)
390*fdc9504fSAndriy Voskoboinyk continue;
391*fdc9504fSAndriy Voskoboinyk
392*fdc9504fSAndriy Voskoboinyk /* Valid results remain stable after consecutive runs. */
393*fdc9504fSAndriy Voskoboinyk valid = r92c_iq_calib_compare_results(sc, tx[n - 1],
394*fdc9504fSAndriy Voskoboinyk rx[n - 1], tx[n], rx[n]);
395*fdc9504fSAndriy Voskoboinyk if (valid)
396*fdc9504fSAndriy Voskoboinyk break;
3977453645fSAndriy Voskoboinyk }
3987453645fSAndriy Voskoboinyk
399*fdc9504fSAndriy Voskoboinyk if (valid) {
400*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_write_results(sc, tx[n][0], rx[n][0], 0);
401*fdc9504fSAndriy Voskoboinyk if (sc->ntxchains > 1)
402*fdc9504fSAndriy Voskoboinyk r92c_iq_calib_write_results(sc, tx[n][1], rx[n][1], 1);
403*fdc9504fSAndriy Voskoboinyk }
404*fdc9504fSAndriy Voskoboinyk }
405*fdc9504fSAndriy Voskoboinyk #undef RTWN_IQ_CAL_NRUN
406*fdc9504fSAndriy Voskoboinyk
4077453645fSAndriy Voskoboinyk void
r92c_lc_calib(struct rtwn_softc * sc)4087453645fSAndriy Voskoboinyk r92c_lc_calib(struct rtwn_softc *sc)
4097453645fSAndriy Voskoboinyk {
4107453645fSAndriy Voskoboinyk uint32_t rf_ac[2];
4117453645fSAndriy Voskoboinyk uint8_t txmode;
4127453645fSAndriy Voskoboinyk int i;
4137453645fSAndriy Voskoboinyk
4147453645fSAndriy Voskoboinyk txmode = rtwn_read_1(sc, R92C_OFDM1_LSTF + 3);
4157453645fSAndriy Voskoboinyk if ((txmode & 0x70) != 0) {
4167453645fSAndriy Voskoboinyk /* Disable all continuous Tx. */
4177453645fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode & ~0x70);
4187453645fSAndriy Voskoboinyk
4197453645fSAndriy Voskoboinyk /* Set RF mode to standby mode. */
4207453645fSAndriy Voskoboinyk for (i = 0; i < sc->nrxchains; i++) {
4217453645fSAndriy Voskoboinyk rf_ac[i] = rtwn_rf_read(sc, i, R92C_RF_AC);
4227453645fSAndriy Voskoboinyk rtwn_rf_write(sc, i, R92C_RF_AC,
4237453645fSAndriy Voskoboinyk RW(rf_ac[i], R92C_RF_AC_MODE,
4247453645fSAndriy Voskoboinyk R92C_RF_AC_MODE_STANDBY));
4257453645fSAndriy Voskoboinyk }
4267453645fSAndriy Voskoboinyk } else {
4277453645fSAndriy Voskoboinyk /* Block all Tx queues. */
4287453645fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_ALL);
4297453645fSAndriy Voskoboinyk }
4307453645fSAndriy Voskoboinyk /* Start calibration. */
4317453645fSAndriy Voskoboinyk rtwn_rf_setbits(sc, 0, R92C_RF_CHNLBW, 0, R92C_RF_CHNLBW_LCSTART);
4327453645fSAndriy Voskoboinyk
4337453645fSAndriy Voskoboinyk /* Give calibration the time to complete. */
4347453645fSAndriy Voskoboinyk rtwn_delay(sc, 100000); /* 100ms */
4357453645fSAndriy Voskoboinyk
4367453645fSAndriy Voskoboinyk /* Restore configuration. */
4377453645fSAndriy Voskoboinyk if ((txmode & 0x70) != 0) {
4387453645fSAndriy Voskoboinyk /* Restore Tx mode. */
4397453645fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode);
4407453645fSAndriy Voskoboinyk /* Restore RF mode. */
4417453645fSAndriy Voskoboinyk for (i = 0; i < sc->nrxchains; i++)
4427453645fSAndriy Voskoboinyk rtwn_rf_write(sc, i, R92C_RF_AC, rf_ac[i]);
4437453645fSAndriy Voskoboinyk } else {
4447453645fSAndriy Voskoboinyk /* Unblock all Tx queues. */
4457453645fSAndriy Voskoboinyk rtwn_write_1(sc, R92C_TXPAUSE, 0x00);
4467453645fSAndriy Voskoboinyk }
4477453645fSAndriy Voskoboinyk }
4487453645fSAndriy Voskoboinyk
4497453645fSAndriy Voskoboinyk void
r92c_temp_measure(struct rtwn_softc * sc)4507453645fSAndriy Voskoboinyk r92c_temp_measure(struct rtwn_softc *sc)
4517453645fSAndriy Voskoboinyk {
4527453645fSAndriy Voskoboinyk rtwn_rf_write(sc, 0, R92C_RF_T_METER, R92C_RF_T_METER_START);
4537453645fSAndriy Voskoboinyk }
4547453645fSAndriy Voskoboinyk
4557453645fSAndriy Voskoboinyk uint8_t
r92c_temp_read(struct rtwn_softc * sc)4567453645fSAndriy Voskoboinyk r92c_temp_read(struct rtwn_softc *sc)
4577453645fSAndriy Voskoboinyk {
4587453645fSAndriy Voskoboinyk return (MS(rtwn_rf_read(sc, 0, R92C_RF_T_METER),
4597453645fSAndriy Voskoboinyk R92C_RF_T_METER_VAL));
4607453645fSAndriy Voskoboinyk }
461