2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Sepherosa Ziehau <sepherosa@gmail.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $DragonFly: src/sys/dev/netif/bwi/bwirf.c,v 1.9 2008/08/21 12:19:33 swildner Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/endian.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
59 #include <net/if_var.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_types.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/if_llc.h>
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_radiotap.h>
69 #include <net80211/ieee80211_amrr.h>
71 #include <machine/bus.h>
73 #include <dev/bwi/bitops.h>
74 #include <dev/bwi/if_bwireg.h>
75 #include <dev/bwi/if_bwivar.h>
76 #include <dev/bwi/bwimac.h>
77 #include <dev/bwi/bwirf.h>
78 #include <dev/bwi/bwiphy.h>
80 #define RF_LO_WRITE(mac, lo) bwi_rf_lo_write((mac), (lo))
82 #define BWI_RF_2GHZ_CHAN(chan) \
83 (ieee80211_ieee2mhz((chan), IEEE80211_CHAN_2GHZ) - 2400)
85 #define BWI_DEFAULT_IDLE_TSSI 52
108 #define SAVE_RF_REG(mac, regs, n) (regs)->rf_##n = RF_READ((mac), 0x##n)
109 #define RESTORE_RF_REG(mac, regs, n) RF_WRITE((mac), 0x##n, (regs)->rf_##n)
111 #define SAVE_PHY_REG(mac, regs, n) (regs)->phy_##n = PHY_READ((mac), 0x##n)
112 #define RESTORE_PHY_REG(mac, regs, n) PHY_WRITE((mac), 0x##n, (regs)->phy_##n)
114 static int bwi_rf_calc_txpower(int8_t *, uint8_t, const int16_t[]);
115 static void bwi_rf_work_around(struct bwi_mac *, u_int);
116 static int bwi_rf_gain_max_reached(struct bwi_mac *, int);
117 static uint16_t bwi_rf_calibval(struct bwi_mac *);
118 static uint16_t bwi_rf_get_tp_ctrl2(struct bwi_mac *);
120 static void bwi_rf_lo_update_11b(struct bwi_mac *);
121 static uint16_t bwi_rf_lo_measure_11b(struct bwi_mac *);
123 static void bwi_rf_lo_update_11g(struct bwi_mac *);
124 static uint32_t bwi_rf_lo_devi_measure(struct bwi_mac *, uint16_t);
125 static void bwi_rf_lo_measure_11g(struct bwi_mac *,
126 const struct bwi_rf_lo *, struct bwi_rf_lo *, uint8_t);
127 static uint8_t _bwi_rf_lo_update_11g(struct bwi_mac *, uint16_t);
128 static void bwi_rf_lo_write(struct bwi_mac *, const struct bwi_rf_lo *);
130 static void bwi_rf_set_nrssi_ofs_11g(struct bwi_mac *);
131 static void bwi_rf_calc_nrssi_slope_11b(struct bwi_mac *);
132 static void bwi_rf_calc_nrssi_slope_11g(struct bwi_mac *);
133 static void bwi_rf_set_nrssi_thr_11b(struct bwi_mac *);
134 static void bwi_rf_set_nrssi_thr_11g(struct bwi_mac *);
136 static void bwi_rf_init_sw_nrssi_table(struct bwi_mac *);
138 static int bwi_rf_calc_rssi_bcm2050(struct bwi_mac *,
139 const struct bwi_rxbuf_hdr *);
140 static int bwi_rf_calc_rssi_bcm2053(struct bwi_mac *,
141 const struct bwi_rxbuf_hdr *);
142 static int bwi_rf_calc_rssi_bcm2060(struct bwi_mac *,
143 const struct bwi_rxbuf_hdr *);
144 static int bwi_rf_calc_noise_bcm2050(struct bwi_mac *);
145 static int bwi_rf_calc_noise_bcm2053(struct bwi_mac *);
146 static int bwi_rf_calc_noise_bcm2060(struct bwi_mac *);
148 static void bwi_rf_on_11a(struct bwi_mac *);
149 static void bwi_rf_on_11bg(struct bwi_mac *);
151 static void bwi_rf_off_11a(struct bwi_mac *);
152 static void bwi_rf_off_11bg(struct bwi_mac *);
153 static void bwi_rf_off_11g_rev5(struct bwi_mac *);
155 static const int8_t bwi_txpower_map_11b[BWI_TSSI_MAX] =
156 { BWI_TXPOWER_MAP_11B };
157 static const int8_t bwi_txpower_map_11g[BWI_TSSI_MAX] =
158 { BWI_TXPOWER_MAP_11G };
160 static __inline int16_t
161 bwi_nrssi_11g(struct bwi_mac *mac)
165 #define NRSSI_11G_MASK __BITS(13, 8)
167 val = (int16_t)__SHIFTOUT(PHY_READ(mac, 0x47f), NRSSI_11G_MASK);
172 #undef NRSSI_11G_MASK
175 static __inline struct bwi_rf_lo *
176 bwi_get_rf_lo(struct bwi_mac *mac, uint16_t rf_atten, uint16_t bbp_atten)
180 n = rf_atten + (14 * (bbp_atten / 2));
181 KASSERT(n < BWI_RFLO_MAX, ("n %d", n));
183 return &mac->mac_rf.rf_lo[n];
187 bwi_rf_lo_isused(struct bwi_mac *mac, const struct bwi_rf_lo *lo)
189 struct bwi_rf *rf = &mac->mac_rf;
192 idx = lo - rf->rf_lo;
193 KASSERT(idx >= 0 && idx < BWI_RFLO_MAX, ("idx %d", idx));
195 return isset(rf->rf_lo_used, idx);
199 bwi_rf_write(struct bwi_mac *mac, uint16_t ctrl, uint16_t data)
201 struct bwi_softc *sc = mac->mac_sc;
203 CSR_WRITE_2(sc, BWI_RF_CTRL, ctrl);
204 CSR_WRITE_2(sc, BWI_RF_DATA_LO, data);
208 bwi_rf_read(struct bwi_mac *mac, uint16_t ctrl)
210 struct bwi_rf *rf = &mac->mac_rf;
211 struct bwi_softc *sc = mac->mac_sc;
213 ctrl |= rf->rf_ctrl_rd;
214 if (rf->rf_ctrl_adj) {
218 else if (ctrl < 0x80)
222 CSR_WRITE_2(sc, BWI_RF_CTRL, ctrl);
223 return CSR_READ_2(sc, BWI_RF_DATA_LO);
227 bwi_rf_attach(struct bwi_mac *mac)
229 struct bwi_softc *sc = mac->mac_sc;
230 struct bwi_phy *phy = &mac->mac_phy;
231 struct bwi_rf *rf = &mac->mac_rf;
236 * Get RF manufacture/type/revision
238 if (sc->sc_bbp_id == BWI_BBPID_BCM4317) {
242 manu = BWI_RF_MANUFACT_BCM;
243 type = BWI_RF_T_BCM2050;
244 if (sc->sc_bbp_rev == 0)
246 else if (sc->sc_bbp_rev == 1)
253 CSR_WRITE_2(sc, BWI_RF_CTRL, BWI_RF_CTRL_RFINFO);
254 val = CSR_READ_2(sc, BWI_RF_DATA_HI);
257 CSR_WRITE_2(sc, BWI_RF_CTRL, BWI_RF_CTRL_RFINFO);
258 val |= CSR_READ_2(sc, BWI_RF_DATA_LO);
260 manu = __SHIFTOUT(val, BWI_RFINFO_MANUFACT_MASK);
261 type = __SHIFTOUT(val, BWI_RFINFO_TYPE_MASK);
262 rev = __SHIFTOUT(val, BWI_RFINFO_REV_MASK);
264 device_printf(sc->sc_dev, "RF: manu 0x%03x, type 0x%04x, rev %u\n",
268 * Verify whether the RF is supported
272 switch (phy->phy_mode) {
273 case IEEE80211_MODE_11A:
274 if (manu != BWI_RF_MANUFACT_BCM ||
275 type != BWI_RF_T_BCM2060 ||
277 device_printf(sc->sc_dev, "only BCM2060 rev 1 RF "
278 "is supported for 11A PHY\n");
281 rf->rf_ctrl_rd = BWI_RF_CTRL_RD_11A;
282 rf->rf_on = bwi_rf_on_11a;
283 rf->rf_off = bwi_rf_off_11a;
284 rf->rf_calc_rssi = bwi_rf_calc_rssi_bcm2060;
285 rf->rf_calc_noise = bwi_rf_calc_noise_bcm2060;
287 case IEEE80211_MODE_11B:
288 if (type == BWI_RF_T_BCM2050) {
289 rf->rf_ctrl_rd = BWI_RF_CTRL_RD_11BG;
290 rf->rf_calc_rssi = bwi_rf_calc_rssi_bcm2050;
291 rf->rf_calc_noise = bwi_rf_calc_noise_bcm2050;
292 } else if (type == BWI_RF_T_BCM2053) {
294 rf->rf_calc_rssi = bwi_rf_calc_rssi_bcm2053;
295 rf->rf_calc_noise = bwi_rf_calc_noise_bcm2053;
297 device_printf(sc->sc_dev, "only BCM2050/BCM2053 RF "
298 "is supported for 11B PHY\n");
301 rf->rf_on = bwi_rf_on_11bg;
302 rf->rf_off = bwi_rf_off_11bg;
303 rf->rf_calc_nrssi_slope = bwi_rf_calc_nrssi_slope_11b;
304 rf->rf_set_nrssi_thr = bwi_rf_set_nrssi_thr_11b;
305 if (phy->phy_rev == 6)
306 rf->rf_lo_update = bwi_rf_lo_update_11g;
308 rf->rf_lo_update = bwi_rf_lo_update_11b;
310 case IEEE80211_MODE_11G:
311 if (type != BWI_RF_T_BCM2050) {
312 device_printf(sc->sc_dev, "only BCM2050 RF "
313 "is supported for 11G PHY\n");
316 rf->rf_ctrl_rd = BWI_RF_CTRL_RD_11BG;
317 rf->rf_on = bwi_rf_on_11bg;
318 if (mac->mac_rev >= 5)
319 rf->rf_off = bwi_rf_off_11g_rev5;
321 rf->rf_off = bwi_rf_off_11bg;
322 rf->rf_calc_nrssi_slope = bwi_rf_calc_nrssi_slope_11g;
323 rf->rf_set_nrssi_thr = bwi_rf_set_nrssi_thr_11g;
324 rf->rf_calc_rssi = bwi_rf_calc_rssi_bcm2050;
325 rf->rf_calc_noise = bwi_rf_calc_noise_bcm2050;
326 rf->rf_lo_update = bwi_rf_lo_update_11g;
329 device_printf(sc->sc_dev, "unsupported PHY mode\n");
336 rf->rf_curchan = IEEE80211_CHAN_ANY;
337 rf->rf_ant_mode = BWI_ANT_MODE_AUTO;
342 bwi_rf_set_chan(struct bwi_mac *mac, u_int chan, int work_around)
344 struct bwi_softc *sc = mac->mac_sc;
346 if (chan == IEEE80211_CHAN_ANY)
349 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_CHAN, chan);
354 bwi_rf_work_around(mac, chan);
356 CSR_WRITE_2(sc, BWI_RF_CHAN, BWI_RF_2GHZ_CHAN(chan));
359 if (sc->sc_locale == BWI_SPROM_LOCALE_JAPAN)
360 HFLAGS_CLRBITS(mac, BWI_HFLAG_NOT_JAPAN);
362 HFLAGS_SETBITS(mac, BWI_HFLAG_NOT_JAPAN);
363 CSR_SETBITS_2(sc, BWI_RF_CHAN_EX, (1 << 11)); /* XXX */
365 CSR_CLRBITS_2(sc, BWI_RF_CHAN_EX, 0x840); /* XXX */
367 DELAY(8000); /* DELAY(2000); */
369 mac->mac_rf.rf_curchan = chan;
373 bwi_rf_get_gains(struct bwi_mac *mac)
375 #define SAVE_PHY_MAX 15
376 #define SAVE_RF_MAX 3
378 static const uint16_t save_rf_regs[SAVE_RF_MAX] =
379 { 0x52, 0x43, 0x7a };
380 static const uint16_t save_phy_regs[SAVE_PHY_MAX] = {
381 0x0429, 0x0001, 0x0811, 0x0812,
382 0x0814, 0x0815, 0x005a, 0x0059,
383 0x0058, 0x000a, 0x0003, 0x080f,
384 0x0810, 0x002b, 0x0015
387 struct bwi_softc *sc = mac->mac_sc;
388 struct bwi_phy *phy = &mac->mac_phy;
389 struct bwi_rf *rf = &mac->mac_rf;
390 uint16_t save_phy[SAVE_PHY_MAX];
391 uint16_t save_rf[SAVE_RF_MAX];
393 int i, j, loop1_max, loop1, loop2;
396 * Save PHY/RF registers for later restoration
398 for (i = 0; i < SAVE_PHY_MAX; ++i)
399 save_phy[i] = PHY_READ(mac, save_phy_regs[i]);
400 PHY_READ(mac, 0x2d); /* dummy read */
402 for (i = 0; i < SAVE_RF_MAX; ++i)
403 save_rf[i] = RF_READ(mac, save_rf_regs[i]);
405 PHY_CLRBITS(mac, 0x429, 0xc000);
406 PHY_SETBITS(mac, 0x1, 0x8000);
408 PHY_SETBITS(mac, 0x811, 0x2);
409 PHY_CLRBITS(mac, 0x812, 0x2);
410 PHY_SETBITS(mac, 0x811, 0x1);
411 PHY_CLRBITS(mac, 0x812, 0x1);
413 PHY_SETBITS(mac, 0x814, 0x1);
414 PHY_CLRBITS(mac, 0x815, 0x1);
415 PHY_SETBITS(mac, 0x814, 0x2);
416 PHY_CLRBITS(mac, 0x815, 0x2);
418 PHY_SETBITS(mac, 0x811, 0xc);
419 PHY_SETBITS(mac, 0x812, 0xc);
420 PHY_SETBITS(mac, 0x811, 0x30);
421 PHY_FILT_SETBITS(mac, 0x812, 0xffcf, 0x10);
423 PHY_WRITE(mac, 0x5a, 0x780);
424 PHY_WRITE(mac, 0x59, 0xc810);
425 PHY_WRITE(mac, 0x58, 0xd);
426 PHY_SETBITS(mac, 0xa, 0x2000);
428 PHY_SETBITS(mac, 0x814, 0x4);
429 PHY_CLRBITS(mac, 0x815, 0x4);
431 PHY_FILT_SETBITS(mac, 0x3, 0xff9f, 0x40);
433 if (rf->rf_rev == 8) {
435 RF_WRITE(mac, 0x43, loop1_max);
438 RF_WRITE(mac, 0x52, 0x0);
439 RF_FILT_SETBITS(mac, 0x43, 0xfff0, loop1_max);
442 bwi_phy_set_bbp_atten(mac, 11);
444 if (phy->phy_rev >= 3)
445 PHY_WRITE(mac, 0x80f, 0xc020);
447 PHY_WRITE(mac, 0x80f, 0x8020);
448 PHY_WRITE(mac, 0x810, 0);
450 PHY_FILT_SETBITS(mac, 0x2b, 0xffc0, 0x1);
451 PHY_FILT_SETBITS(mac, 0x2b, 0xc0ff, 0x800);
452 PHY_SETBITS(mac, 0x811, 0x100);
453 PHY_CLRBITS(mac, 0x812, 0x3000);
455 if ((sc->sc_card_flags & BWI_CARD_F_EXT_LNA) &&
457 PHY_SETBITS(mac, 0x811, 0x800);
458 PHY_SETBITS(mac, 0x812, 0x8000);
460 RF_CLRBITS(mac, 0x7a, 0xff08);
463 * Find out 'loop1/loop2', which will be used to calculate
464 * max loopback gain later
467 for (i = 0; i < loop1_max; ++i) {
468 for (j = 0; j < 16; ++j) {
469 RF_WRITE(mac, 0x43, i);
471 if (bwi_rf_gain_max_reached(mac, j))
480 * Find out 'trsw', which will be used to calculate
481 * TRSW(TX/RX switch) RX gain later
484 PHY_SETBITS(mac, 0x812, 0x30);
486 for (i = loop2 - 8; i < 16; ++i) {
488 if (bwi_rf_gain_max_reached(mac, i))
496 * Restore saved PHY/RF registers
498 /* First 4 saved PHY registers need special processing */
499 for (i = 4; i < SAVE_PHY_MAX; ++i)
500 PHY_WRITE(mac, save_phy_regs[i], save_phy[i]);
502 bwi_phy_set_bbp_atten(mac, mac->mac_tpctl.bbp_atten);
504 for (i = 0; i < SAVE_RF_MAX; ++i)
505 RF_WRITE(mac, save_rf_regs[i], save_rf[i]);
507 PHY_WRITE(mac, save_phy_regs[2], save_phy[2] | 0x3);
509 PHY_WRITE(mac, save_phy_regs[2], save_phy[2]);
510 PHY_WRITE(mac, save_phy_regs[3], save_phy[3]);
511 PHY_WRITE(mac, save_phy_regs[0], save_phy[0]);
512 PHY_WRITE(mac, save_phy_regs[1], save_phy[1]);
517 rf->rf_lo_gain = (loop2 * 6) - (loop1 * 4) - 11;
518 rf->rf_rx_gain = trsw * 2;
519 DPRINTF(mac->mac_sc, BWI_DBG_RF | BWI_DBG_INIT,
520 "lo gain: %u, rx gain: %u\n",
521 rf->rf_lo_gain, rf->rf_rx_gain);
528 bwi_rf_init(struct bwi_mac *mac)
530 struct bwi_rf *rf = &mac->mac_rf;
532 if (rf->rf_type == BWI_RF_T_BCM2060) {
535 if (rf->rf_flags & BWI_RF_F_INITED)
536 RF_WRITE(mac, 0x78, rf->rf_calib);
538 bwi_rf_init_bcm2050(mac);
543 bwi_rf_off_11a(struct bwi_mac *mac)
545 RF_WRITE(mac, 0x4, 0xff);
546 RF_WRITE(mac, 0x5, 0xfb);
548 PHY_SETBITS(mac, 0x10, 0x8);
549 PHY_SETBITS(mac, 0x11, 0x8);
551 PHY_WRITE(mac, 0x15, 0xaa00);
555 bwi_rf_off_11bg(struct bwi_mac *mac)
557 PHY_WRITE(mac, 0x15, 0xaa00);
561 bwi_rf_off_11g_rev5(struct bwi_mac *mac)
563 PHY_SETBITS(mac, 0x811, 0x8c);
564 PHY_CLRBITS(mac, 0x812, 0x8c);
568 bwi_rf_work_around(struct bwi_mac *mac, u_int chan)
570 struct bwi_softc *sc = mac->mac_sc;
571 struct bwi_rf *rf = &mac->mac_rf;
573 if (chan == IEEE80211_CHAN_ANY) {
574 device_printf(sc->sc_dev, "%s invalid channel!!\n", __func__);
578 if (rf->rf_type != BWI_RF_T_BCM2050 || rf->rf_rev >= 6)
582 CSR_WRITE_2(sc, BWI_RF_CHAN, BWI_RF_2GHZ_CHAN(chan + 4));
584 CSR_WRITE_2(sc, BWI_RF_CHAN, BWI_RF_2GHZ_CHAN(1));
586 CSR_WRITE_2(sc, BWI_RF_CHAN, BWI_RF_2GHZ_CHAN(chan));
589 static __inline struct bwi_rf_lo *
590 bwi_rf_lo_find(struct bwi_mac *mac, const struct bwi_tpctl *tpctl)
592 uint16_t rf_atten, bbp_atten;
600 if (tpctl->tp_ctrl1 == 3)
603 bbp_atten = tpctl->bbp_atten;
604 rf_atten = tpctl->rf_atten;
610 if (remap_rf_atten) {
612 static const uint16_t map[MAP_MAX] =
613 { 11, 10, 11, 12, 13, 12, 13, 12, 13, 12 };
616 KASSERT(rf_atten < MAP_MAX, ("rf_atten %d", rf_atten));
617 rf_atten = map[rf_atten];
619 if (rf_atten >= MAP_MAX) {
620 rf_atten = 0; /* XXX */
622 rf_atten = map[rf_atten];
628 return bwi_get_rf_lo(mac, rf_atten, bbp_atten);
632 bwi_rf_lo_adjust(struct bwi_mac *mac, const struct bwi_tpctl *tpctl)
634 const struct bwi_rf_lo *lo;
636 lo = bwi_rf_lo_find(mac, tpctl);
637 RF_LO_WRITE(mac, lo);
641 bwi_rf_lo_write(struct bwi_mac *mac, const struct bwi_rf_lo *lo)
645 val = (uint8_t)lo->ctrl_lo;
646 val |= ((uint8_t)lo->ctrl_hi) << 8;
648 PHY_WRITE(mac, BWI_PHYR_RF_LO, val);
652 bwi_rf_gain_max_reached(struct bwi_mac *mac, int idx)
654 PHY_FILT_SETBITS(mac, 0x812, 0xf0ff, idx << 8);
655 PHY_FILT_SETBITS(mac, 0x15, 0xfff, 0xa000);
656 PHY_SETBITS(mac, 0x15, 0xf000);
660 return (PHY_READ(mac, 0x2d) >= 0xdfc);
663 /* XXX use bitmap array */
664 static __inline uint16_t
665 bitswap4(uint16_t val)
669 ret = (val & 0x8) >> 3;
670 ret |= (val & 0x4) >> 1;
671 ret |= (val & 0x2) << 1;
672 ret |= (val & 0x1) << 3;
676 static __inline uint16_t
677 bwi_phy812_value(struct bwi_mac *mac, uint16_t lpd)
679 struct bwi_softc *sc = mac->mac_sc;
680 struct bwi_phy *phy = &mac->mac_phy;
681 struct bwi_rf *rf = &mac->mac_rf;
682 uint16_t lo_gain, ext_lna, loop;
684 if ((phy->phy_flags & BWI_PHY_F_LINKED) == 0)
687 lo_gain = rf->rf_lo_gain;
693 if (lo_gain >= 0x46) {
696 } else if (lo_gain >= 0x3a) {
699 } else if (lo_gain >= 0x2e) {
707 for (loop = 0; loop < 16; ++loop) {
708 lo_gain -= (6 * loop);
713 if (phy->phy_rev >= 7 && (sc->sc_card_flags & BWI_CARD_F_EXT_LNA)) {
716 ext_lna |= (loop << 8);
721 return (0x8092 | ext_lna);
723 return (0x2092 | ext_lna);
725 return (0x2093 | ext_lna);
727 panic("unsupported lpd\n");
730 ext_lna |= (loop << 8);
736 return (0x92 | ext_lna);
738 return (0x93 | ext_lna);
740 panic("unsupported lpd\n");
744 panic("never reached\n");
749 bwi_rf_init_bcm2050(struct bwi_mac *mac)
751 #define SAVE_RF_MAX 3
752 #define SAVE_PHY_COMM_MAX 4
753 #define SAVE_PHY_11G_MAX 6
755 static const uint16_t save_rf_regs[SAVE_RF_MAX] =
756 { 0x0043, 0x0051, 0x0052 };
757 static const uint16_t save_phy_regs_comm[SAVE_PHY_COMM_MAX] =
758 { 0x0015, 0x005a, 0x0059, 0x0058 };
759 static const uint16_t save_phy_regs_11g[SAVE_PHY_11G_MAX] =
760 { 0x0811, 0x0812, 0x0814, 0x0815, 0x0429, 0x0802 };
762 uint16_t save_rf[SAVE_RF_MAX];
763 uint16_t save_phy_comm[SAVE_PHY_COMM_MAX];
764 uint16_t save_phy_11g[SAVE_PHY_11G_MAX];
765 uint16_t phyr_35, phyr_30 = 0, rfr_78, phyr_80f = 0, phyr_810 = 0;
766 uint16_t bphy_ctrl = 0, bbp_atten, rf_chan_ex;
769 uint32_t test_lim, test;
770 struct bwi_softc *sc = mac->mac_sc;
771 struct bwi_phy *phy = &mac->mac_phy;
772 struct bwi_rf *rf = &mac->mac_rf;
776 * Save registers for later restoring
778 for (i = 0; i < SAVE_RF_MAX; ++i)
779 save_rf[i] = RF_READ(mac, save_rf_regs[i]);
780 for (i = 0; i < SAVE_PHY_COMM_MAX; ++i)
781 save_phy_comm[i] = PHY_READ(mac, save_phy_regs_comm[i]);
783 if (phy->phy_mode == IEEE80211_MODE_11B) {
784 phyr_30 = PHY_READ(mac, 0x30);
785 bphy_ctrl = CSR_READ_2(sc, BWI_BPHY_CTRL);
787 PHY_WRITE(mac, 0x30, 0xff);
788 CSR_WRITE_2(sc, BWI_BPHY_CTRL, 0x3f3f);
789 } else if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
790 for (i = 0; i < SAVE_PHY_11G_MAX; ++i) {
792 PHY_READ(mac, save_phy_regs_11g[i]);
795 PHY_SETBITS(mac, 0x814, 0x3);
796 PHY_CLRBITS(mac, 0x815, 0x3);
797 PHY_CLRBITS(mac, 0x429, 0x8000);
798 PHY_CLRBITS(mac, 0x802, 0x3);
800 phyr_80f = PHY_READ(mac, 0x80f);
801 phyr_810 = PHY_READ(mac, 0x810);
803 if (phy->phy_rev >= 3)
804 PHY_WRITE(mac, 0x80f, 0xc020);
806 PHY_WRITE(mac, 0x80f, 0x8020);
807 PHY_WRITE(mac, 0x810, 0);
809 phy812_val = bwi_phy812_value(mac, 0x011);
810 PHY_WRITE(mac, 0x812, phy812_val);
811 if (phy->phy_rev < 7 ||
812 (sc->sc_card_flags & BWI_CARD_F_EXT_LNA) == 0)
813 PHY_WRITE(mac, 0x811, 0x1b3);
815 PHY_WRITE(mac, 0x811, 0x9b3);
817 CSR_SETBITS_2(sc, BWI_RF_ANTDIV, 0x8000);
819 phyr_35 = PHY_READ(mac, 0x35);
820 PHY_CLRBITS(mac, 0x35, 0x80);
822 bbp_atten = CSR_READ_2(sc, BWI_BBP_ATTEN);
823 rf_chan_ex = CSR_READ_2(sc, BWI_RF_CHAN_EX);
825 if (phy->phy_version == 0) {
826 CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0x122);
828 if (phy->phy_version >= 2)
829 PHY_FILT_SETBITS(mac, 0x3, 0xffbf, 0x40);
830 CSR_SETBITS_2(sc, BWI_RF_CHAN_EX, 0x2000);
833 calib = bwi_rf_calibval(mac);
835 if (phy->phy_mode == IEEE80211_MODE_11B)
836 RF_WRITE(mac, 0x78, 0x26);
838 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
839 phy812_val = bwi_phy812_value(mac, 0x011);
840 PHY_WRITE(mac, 0x812, phy812_val);
843 PHY_WRITE(mac, 0x15, 0xbfaf);
844 PHY_WRITE(mac, 0x2b, 0x1403);
846 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
847 phy812_val = bwi_phy812_value(mac, 0x001);
848 PHY_WRITE(mac, 0x812, phy812_val);
851 PHY_WRITE(mac, 0x15, 0xbfa0);
853 RF_SETBITS(mac, 0x51, 0x4);
854 if (rf->rf_rev == 8) {
855 RF_WRITE(mac, 0x43, 0x1f);
857 RF_WRITE(mac, 0x52, 0);
858 RF_FILT_SETBITS(mac, 0x43, 0xfff0, 0x9);
862 PHY_WRITE(mac, 0x58, 0);
863 for (i = 0; i < 16; ++i) {
864 PHY_WRITE(mac, 0x5a, 0x480);
865 PHY_WRITE(mac, 0x59, 0xc810);
867 PHY_WRITE(mac, 0x58, 0xd);
868 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
869 phy812_val = bwi_phy812_value(mac, 0x101);
870 PHY_WRITE(mac, 0x812, phy812_val);
872 PHY_WRITE(mac, 0x15, 0xafb0);
875 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
876 phy812_val = bwi_phy812_value(mac, 0x101);
877 PHY_WRITE(mac, 0x812, phy812_val);
879 PHY_WRITE(mac, 0x15, 0xefb0);
882 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
883 phy812_val = bwi_phy812_value(mac, 0x100);
884 PHY_WRITE(mac, 0x812, phy812_val);
886 PHY_WRITE(mac, 0x15, 0xfff0);
889 test_lim += PHY_READ(mac, 0x2d);
891 PHY_WRITE(mac, 0x58, 0);
892 if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
893 phy812_val = bwi_phy812_value(mac, 0x101);
894 PHY_WRITE(mac, 0x812, phy812_val);
896 PHY_WRITE(mac, 0x15, 0xafb0);
904 PHY_WRITE(mac, 0x58, 0);
905 for (i = 0; i < 16; ++i) {
908 rfr_78 = (bitswap4(i) << 1) | 0x20;
909 RF_WRITE(mac, 0x78, rfr_78);
912 /* NB: This block is slight different than the above one */
913 for (j = 0; j < 16; ++j) {
914 PHY_WRITE(mac, 0x5a, 0xd80);
915 PHY_WRITE(mac, 0x59, 0xc810);
917 PHY_WRITE(mac, 0x58, 0xd);
918 if ((phy->phy_flags & BWI_PHY_F_LINKED) ||
920 phy812_val = bwi_phy812_value(mac, 0x101);
921 PHY_WRITE(mac, 0x812, phy812_val);
923 PHY_WRITE(mac, 0x15, 0xafb0);
926 if ((phy->phy_flags & BWI_PHY_F_LINKED) ||
928 phy812_val = bwi_phy812_value(mac, 0x101);
929 PHY_WRITE(mac, 0x812, phy812_val);
931 PHY_WRITE(mac, 0x15, 0xefb0);
934 if ((phy->phy_flags & BWI_PHY_F_LINKED) ||
936 phy812_val = bwi_phy812_value(mac, 0x100);
937 PHY_WRITE(mac, 0x812, phy812_val);
939 PHY_WRITE(mac, 0x15, 0xfff0);
942 test += PHY_READ(mac, 0x2d);
944 PHY_WRITE(mac, 0x58, 0);
945 if ((phy->phy_flags & BWI_PHY_F_LINKED) ||
947 phy812_val = bwi_phy812_value(mac, 0x101);
948 PHY_WRITE(mac, 0x812, phy812_val);
950 PHY_WRITE(mac, 0x15, 0xafb0);
960 rf->rf_calib = rfr_78;
962 rf->rf_calib = calib;
963 if (rf->rf_calib != 0xffff) {
964 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_INIT,
965 "RF calibration value: 0x%04x\n", rf->rf_calib);
966 rf->rf_flags |= BWI_RF_F_INITED;
970 * Restore trashes registers
972 PHY_WRITE(mac, save_phy_regs_comm[0], save_phy_comm[0]);
974 for (i = 0; i < SAVE_RF_MAX; ++i) {
975 int pos = (i + 1) % SAVE_RF_MAX;
977 RF_WRITE(mac, save_rf_regs[pos], save_rf[pos]);
979 for (i = 1; i < SAVE_PHY_COMM_MAX; ++i)
980 PHY_WRITE(mac, save_phy_regs_comm[i], save_phy_comm[i]);
982 CSR_WRITE_2(sc, BWI_BBP_ATTEN, bbp_atten);
983 if (phy->phy_version != 0)
984 CSR_WRITE_2(sc, BWI_RF_CHAN_EX, rf_chan_ex);
986 PHY_WRITE(mac, 0x35, phyr_35);
987 bwi_rf_work_around(mac, rf->rf_curchan);
989 if (phy->phy_mode == IEEE80211_MODE_11B) {
990 PHY_WRITE(mac, 0x30, phyr_30);
991 CSR_WRITE_2(sc, BWI_BPHY_CTRL, bphy_ctrl);
992 } else if ((phy->phy_flags & BWI_PHY_F_LINKED) || phy->phy_rev >= 2) {
993 /* XXX Spec only says when PHY is linked (gmode) */
994 CSR_CLRBITS_2(sc, BWI_RF_ANTDIV, 0x8000);
996 for (i = 0; i < SAVE_PHY_11G_MAX; ++i) {
997 PHY_WRITE(mac, save_phy_regs_11g[i],
1001 PHY_WRITE(mac, 0x80f, phyr_80f);
1002 PHY_WRITE(mac, 0x810, phyr_810);
1005 #undef SAVE_PHY_11G_MAX
1006 #undef SAVE_PHY_COMM_MAX
1011 bwi_rf_calibval(struct bwi_mac *mac)
1013 /* http://bcm-specs.sipsolutions.net/RCCTable */
1014 static const uint16_t rf_calibvals[] = {
1015 0x2, 0x3, 0x1, 0xf, 0x6, 0x7, 0x5, 0xf,
1016 0xa, 0xb, 0x9, 0xf, 0xe, 0xf, 0xd, 0xf
1018 uint16_t val, calib;
1021 val = RF_READ(mac, BWI_RFR_BBP_ATTEN);
1022 idx = __SHIFTOUT(val, BWI_RFR_BBP_ATTEN_CALIB_IDX);
1023 KASSERT(idx < (int)nitems(rf_calibvals), ("idx %d", idx));
1025 calib = rf_calibvals[idx] << 1;
1026 if (val & BWI_RFR_BBP_ATTEN_CALIB_BIT)
1033 static __inline int32_t
1034 _bwi_adjust_devide(int32_t num, int32_t den)
1039 return (num + den / 2) / den;
1043 * http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table
1044 * "calculating table entries"
1047 bwi_rf_calc_txpower(int8_t *txpwr, uint8_t idx, const int16_t pa_params[])
1049 int32_t m1, m2, f, dbm;
1052 m1 = _bwi_adjust_devide(16 * pa_params[0] + idx * pa_params[1], 32);
1053 m2 = imax(_bwi_adjust_devide(32768 + idx * pa_params[2], 256), 1);
1058 for (i = 0; i < ITER_MAX; ++i) {
1061 q = _bwi_adjust_devide(
1062 f * 4096 - _bwi_adjust_devide(m2 * f, 16) * f, 2048);
1074 dbm = _bwi_adjust_devide(m1 * f, 8192);
1085 bwi_rf_map_txpower(struct bwi_mac *mac)
1087 struct bwi_softc *sc = mac->mac_sc;
1088 struct bwi_rf *rf = &mac->mac_rf;
1089 struct bwi_phy *phy = &mac->mac_phy;
1090 uint16_t sprom_ofs, val, mask;
1091 int16_t pa_params[3];
1092 int error = 0, i, ant_gain, reg_txpower_max;
1095 * Find out max TX power
1097 val = bwi_read_sprom(sc, BWI_SPROM_MAX_TXPWR);
1098 if (phy->phy_mode == IEEE80211_MODE_11A) {
1099 rf->rf_txpower_max = __SHIFTOUT(val,
1100 BWI_SPROM_MAX_TXPWR_MASK_11A);
1102 rf->rf_txpower_max = __SHIFTOUT(val,
1103 BWI_SPROM_MAX_TXPWR_MASK_11BG);
1105 if ((sc->sc_card_flags & BWI_CARD_F_PA_GPIO9) &&
1106 phy->phy_mode == IEEE80211_MODE_11G)
1107 rf->rf_txpower_max -= 3;
1109 if (rf->rf_txpower_max <= 0) {
1110 device_printf(sc->sc_dev, "invalid max txpower in sprom\n");
1111 rf->rf_txpower_max = 74;
1113 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1114 "max txpower from sprom: %d dBm\n", rf->rf_txpower_max);
1117 * Find out region/domain max TX power, which is adjusted
1118 * by antenna gain and 1.5 dBm fluctuation as mentioned
1121 val = bwi_read_sprom(sc, BWI_SPROM_ANT_GAIN);
1122 if (phy->phy_mode == IEEE80211_MODE_11A)
1123 ant_gain = __SHIFTOUT(val, BWI_SPROM_ANT_GAIN_MASK_11A);
1125 ant_gain = __SHIFTOUT(val, BWI_SPROM_ANT_GAIN_MASK_11BG);
1126 if (ant_gain == 0xff) {
1127 device_printf(sc->sc_dev, "invalid antenna gain in sprom\n");
1131 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1132 "ant gain %d dBm\n", ant_gain);
1134 reg_txpower_max = 90 - ant_gain - 6; /* XXX magic number */
1135 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1136 "region/domain max txpower %d dBm\n", reg_txpower_max);
1139 * Force max TX power within region/domain TX power limit
1141 if (rf->rf_txpower_max > reg_txpower_max)
1142 rf->rf_txpower_max = reg_txpower_max;
1143 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1144 "max txpower %d dBm\n", rf->rf_txpower_max);
1147 * Create TSSI to TX power mapping
1150 if (sc->sc_bbp_id == BWI_BBPID_BCM4301 &&
1151 rf->rf_type != BWI_RF_T_BCM2050) {
1152 rf->rf_idle_tssi0 = BWI_DEFAULT_IDLE_TSSI;
1153 bcopy(bwi_txpower_map_11b, rf->rf_txpower_map0,
1154 sizeof(rf->rf_txpower_map0));
1158 #define IS_VALID_PA_PARAM(p) ((p) != 0 && (p) != -1)
1161 * Extract PA parameters
1163 if (phy->phy_mode == IEEE80211_MODE_11A)
1164 sprom_ofs = BWI_SPROM_PA_PARAM_11A;
1166 sprom_ofs = BWI_SPROM_PA_PARAM_11BG;
1167 for (i = 0; i < nitems(pa_params); ++i)
1168 pa_params[i] = (int16_t)bwi_read_sprom(sc, sprom_ofs + (i * 2));
1170 for (i = 0; i < nitems(pa_params); ++i) {
1172 * If one of the PA parameters from SPROM is not valid,
1173 * fall back to the default values, if there are any.
1175 if (!IS_VALID_PA_PARAM(pa_params[i])) {
1176 const int8_t *txpower_map;
1178 if (phy->phy_mode == IEEE80211_MODE_11A) {
1179 device_printf(sc->sc_dev,
1180 "no tssi2dbm table for 11a PHY\n");
1184 if (phy->phy_mode == IEEE80211_MODE_11G) {
1186 BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1187 "%s\n", "use default 11g TSSI map");
1188 txpower_map = bwi_txpower_map_11g;
1191 BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1192 "%s\n", "use default 11b TSSI map");
1193 txpower_map = bwi_txpower_map_11b;
1196 rf->rf_idle_tssi0 = BWI_DEFAULT_IDLE_TSSI;
1197 bcopy(txpower_map, rf->rf_txpower_map0,
1198 sizeof(rf->rf_txpower_map0));
1204 * All of the PA parameters from SPROM are valid.
1208 * Extract idle TSSI from SPROM.
1210 val = bwi_read_sprom(sc, BWI_SPROM_IDLE_TSSI);
1211 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1212 "sprom idle tssi: 0x%04x\n", val);
1214 if (phy->phy_mode == IEEE80211_MODE_11A)
1215 mask = BWI_SPROM_IDLE_TSSI_MASK_11A;
1217 mask = BWI_SPROM_IDLE_TSSI_MASK_11BG;
1219 rf->rf_idle_tssi0 = (int)__SHIFTOUT(val, mask);
1220 if (!IS_VALID_PA_PARAM(rf->rf_idle_tssi0))
1221 rf->rf_idle_tssi0 = 62;
1223 #undef IS_VALID_PA_PARAM
1226 * Calculate TX power map, which is indexed by TSSI
1228 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_ATTACH | BWI_DBG_TXPOWER,
1229 "%s\n", "TSSI-TX power map:");
1230 for (i = 0; i < BWI_TSSI_MAX; ++i) {
1231 error = bwi_rf_calc_txpower(&rf->rf_txpower_map0[i], i,
1234 device_printf(sc->sc_dev,
1235 "bwi_rf_calc_txpower failed\n");
1240 if (i != 0 && i % 8 == 0) {
1242 BWI_DBG_RF | BWI_DBG_ATTACH | BWI_DBG_TXPOWER,
1246 _DPRINTF(sc, BWI_DBG_RF | BWI_DBG_ATTACH | BWI_DBG_TXPOWER,
1247 "%d ", rf->rf_txpower_map0[i]);
1249 _DPRINTF(sc, BWI_DBG_RF | BWI_DBG_ATTACH | BWI_DBG_TXPOWER,
1252 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_TXPOWER | BWI_DBG_ATTACH,
1253 "idle tssi0: %d\n", rf->rf_idle_tssi0);
1258 bwi_rf_lo_update_11g(struct bwi_mac *mac)
1260 struct bwi_softc *sc = mac->mac_sc;
1261 struct bwi_rf *rf = &mac->mac_rf;
1262 struct bwi_phy *phy = &mac->mac_phy;
1263 struct bwi_tpctl *tpctl = &mac->mac_tpctl;
1264 struct rf_saveregs regs;
1265 uint16_t ant_div, chan_ex;
1270 * Save RF/PHY registers for later restoration
1272 orig_chan = rf->rf_curchan;
1273 bzero(®s, sizeof(regs));
1275 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1276 SAVE_PHY_REG(mac, ®s, 429);
1277 SAVE_PHY_REG(mac, ®s, 802);
1279 PHY_WRITE(mac, 0x429, regs.phy_429 & 0x7fff);
1280 PHY_WRITE(mac, 0x802, regs.phy_802 & 0xfffc);
1283 ant_div = CSR_READ_2(sc, BWI_RF_ANTDIV);
1284 CSR_WRITE_2(sc, BWI_RF_ANTDIV, ant_div | 0x8000);
1285 chan_ex = CSR_READ_2(sc, BWI_RF_CHAN_EX);
1287 SAVE_PHY_REG(mac, ®s, 15);
1288 SAVE_PHY_REG(mac, ®s, 2a);
1289 SAVE_PHY_REG(mac, ®s, 35);
1290 SAVE_PHY_REG(mac, ®s, 60);
1291 SAVE_RF_REG(mac, ®s, 43);
1292 SAVE_RF_REG(mac, ®s, 7a);
1293 SAVE_RF_REG(mac, ®s, 52);
1294 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1295 SAVE_PHY_REG(mac, ®s, 811);
1296 SAVE_PHY_REG(mac, ®s, 812);
1297 SAVE_PHY_REG(mac, ®s, 814);
1298 SAVE_PHY_REG(mac, ®s, 815);
1301 /* Force to channel 6 */
1302 bwi_rf_set_chan(mac, 6, 0);
1304 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1305 PHY_WRITE(mac, 0x429, regs.phy_429 & 0x7fff);
1306 PHY_WRITE(mac, 0x802, regs.phy_802 & 0xfffc);
1307 bwi_mac_dummy_xmit(mac);
1309 RF_WRITE(mac, 0x43, 0x6);
1311 bwi_phy_set_bbp_atten(mac, 2);
1313 CSR_WRITE_2(sc, BWI_RF_CHAN_EX, 0);
1315 PHY_WRITE(mac, 0x2e, 0x7f);
1316 PHY_WRITE(mac, 0x80f, 0x78);
1317 PHY_WRITE(mac, 0x35, regs.phy_35 & 0xff7f);
1318 RF_WRITE(mac, 0x7a, regs.rf_7a & 0xfff0);
1319 PHY_WRITE(mac, 0x2b, 0x203);
1320 PHY_WRITE(mac, 0x2a, 0x8a3);
1322 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1323 PHY_WRITE(mac, 0x814, regs.phy_814 | 0x3);
1324 PHY_WRITE(mac, 0x815, regs.phy_815 & 0xfffc);
1325 PHY_WRITE(mac, 0x811, 0x1b3);
1326 PHY_WRITE(mac, 0x812, 0xb2);
1329 if ((sc->sc_flags & BWI_F_RUNNING) == 0)
1330 tpctl->tp_ctrl2 = bwi_rf_get_tp_ctrl2(mac);
1331 PHY_WRITE(mac, 0x80f, 0x8078);
1336 devi_ctrl = _bwi_rf_lo_update_11g(mac, regs.rf_7a);
1339 * Restore saved RF/PHY registers
1341 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1342 PHY_WRITE(mac, 0x15, 0xe300);
1343 PHY_WRITE(mac, 0x812, (devi_ctrl << 8) | 0xa0);
1345 PHY_WRITE(mac, 0x812, (devi_ctrl << 8) | 0xa2);
1347 PHY_WRITE(mac, 0x812, (devi_ctrl << 8) | 0xa3);
1349 PHY_WRITE(mac, 0x15, devi_ctrl | 0xefa0);
1352 if ((sc->sc_flags & BWI_F_RUNNING) == 0)
1354 bwi_rf_lo_adjust(mac, tpctl);
1356 PHY_WRITE(mac, 0x2e, 0x807f);
1357 if (phy->phy_flags & BWI_PHY_F_LINKED)
1358 PHY_WRITE(mac, 0x2f, 0x202);
1360 PHY_WRITE(mac, 0x2f, 0x101);
1362 CSR_WRITE_2(sc, BWI_RF_CHAN_EX, chan_ex);
1364 RESTORE_PHY_REG(mac, ®s, 15);
1365 RESTORE_PHY_REG(mac, ®s, 2a);
1366 RESTORE_PHY_REG(mac, ®s, 35);
1367 RESTORE_PHY_REG(mac, ®s, 60);
1369 RESTORE_RF_REG(mac, ®s, 43);
1370 RESTORE_RF_REG(mac, ®s, 7a);
1373 regs.rf_52 |= (RF_READ(mac, 0x52) & 0xf);
1374 RF_WRITE(mac, 0x52, regs.rf_52);
1376 CSR_WRITE_2(sc, BWI_RF_ANTDIV, ant_div);
1378 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1379 RESTORE_PHY_REG(mac, ®s, 811);
1380 RESTORE_PHY_REG(mac, ®s, 812);
1381 RESTORE_PHY_REG(mac, ®s, 814);
1382 RESTORE_PHY_REG(mac, ®s, 815);
1383 RESTORE_PHY_REG(mac, ®s, 429);
1384 RESTORE_PHY_REG(mac, ®s, 802);
1387 bwi_rf_set_chan(mac, orig_chan, 1);
1391 bwi_rf_lo_devi_measure(struct bwi_mac *mac, uint16_t ctrl)
1393 struct bwi_phy *phy = &mac->mac_phy;
1397 if (phy->phy_flags & BWI_PHY_F_LINKED)
1400 for (i = 0; i < 8; ++i) {
1401 if (phy->phy_flags & BWI_PHY_F_LINKED) {
1402 PHY_WRITE(mac, 0x15, 0xe300);
1403 PHY_WRITE(mac, 0x812, ctrl | 0xb0);
1405 PHY_WRITE(mac, 0x812, ctrl | 0xb2);
1407 PHY_WRITE(mac, 0x812, ctrl | 0xb3);
1409 PHY_WRITE(mac, 0x15, 0xf300);
1411 PHY_WRITE(mac, 0x15, ctrl | 0xefa0);
1413 PHY_WRITE(mac, 0x15, ctrl | 0xefe0);
1415 PHY_WRITE(mac, 0x15, ctrl | 0xffe0);
1418 devi += PHY_READ(mac, 0x2d);
1424 bwi_rf_get_tp_ctrl2(struct bwi_mac *mac)
1427 uint16_t tp_ctrl2 = 0;
1430 RF_WRITE(mac, 0x52, 0);
1432 devi_min = bwi_rf_lo_devi_measure(mac, 0);
1434 for (i = 0; i < 16; ++i) {
1437 RF_WRITE(mac, 0x52, i);
1439 devi = bwi_rf_lo_devi_measure(mac, 0);
1441 if (devi < devi_min) {
1450 _bwi_rf_lo_update_11g(struct bwi_mac *mac, uint16_t orig_rf7a)
1452 #define RF_ATTEN_LISTSZ 14
1453 #define BBP_ATTEN_MAX 4 /* half */
1455 static const int rf_atten_list[RF_ATTEN_LISTSZ] =
1456 { 3, 1, 5, 7, 9, 2, 0, 4, 6, 8, 1, 2, 3, 4 };
1457 static const int rf_atten_init_list[RF_ATTEN_LISTSZ] =
1458 { 0, 3, 1, 5, 7, 3, 2, 0, 4, 6, -1, -1, -1, -1 };
1459 static const int rf_lo_measure_order[RF_ATTEN_LISTSZ] =
1460 { 3, 1, 5, 7, 9, 2, 0, 4, 6, 8, 10, 11, 12, 13 };
1462 struct bwi_softc *sc = mac->mac_sc;
1463 struct bwi_rf_lo lo_save, *lo;
1464 uint8_t devi_ctrl = 0;
1465 int idx, adj_rf7a = 0;
1467 bzero(&lo_save, sizeof(lo_save));
1468 for (idx = 0; idx < RF_ATTEN_LISTSZ; ++idx) {
1469 int init_rf_atten = rf_atten_init_list[idx];
1470 int rf_atten = rf_atten_list[idx];
1473 for (bbp_atten = 0; bbp_atten < BBP_ATTEN_MAX; ++bbp_atten) {
1474 uint16_t tp_ctrl2, rf7a;
1476 if ((sc->sc_flags & BWI_F_RUNNING) == 0) {
1478 bzero(&lo_save, sizeof(lo_save));
1479 } else if (init_rf_atten < 0) {
1480 lo = bwi_get_rf_lo(mac,
1481 rf_atten, 2 * bbp_atten);
1482 bcopy(lo, &lo_save, sizeof(lo_save));
1484 lo = bwi_get_rf_lo(mac,
1486 bcopy(lo, &lo_save, sizeof(lo_save));
1494 * Linux driver overflows 'val'
1496 if (init_rf_atten >= 0) {
1499 val = rf_atten * 2 + bbp_atten;
1509 lo = bwi_get_rf_lo(mac,
1510 rf_atten, 2 * bbp_atten);
1511 if (!bwi_rf_lo_isused(mac, lo))
1513 bcopy(lo, &lo_save, sizeof(lo_save));
1519 RF_WRITE(mac, BWI_RFR_ATTEN, rf_atten);
1521 tp_ctrl2 = mac->mac_tpctl.tp_ctrl2;
1522 if (init_rf_atten < 0)
1523 tp_ctrl2 |= (3 << 4);
1524 RF_WRITE(mac, BWI_RFR_TXPWR, tp_ctrl2);
1528 bwi_phy_set_bbp_atten(mac, bbp_atten * 2);
1530 rf7a = orig_rf7a & 0xfff0;
1533 RF_WRITE(mac, 0x7a, rf7a);
1535 lo = bwi_get_rf_lo(mac,
1536 rf_lo_measure_order[idx], bbp_atten * 2);
1537 bwi_rf_lo_measure_11g(mac, &lo_save, lo, devi_ctrl);
1542 #undef RF_ATTEN_LISTSZ
1543 #undef BBP_ATTEN_MAX
1547 bwi_rf_lo_measure_11g(struct bwi_mac *mac, const struct bwi_rf_lo *src_lo,
1548 struct bwi_rf_lo *dst_lo, uint8_t devi_ctrl)
1550 #define LO_ADJUST_MIN 1
1551 #define LO_ADJUST_MAX 8
1552 #define LO_ADJUST(hi, lo) { .ctrl_hi = hi, .ctrl_lo = lo }
1553 static const struct bwi_rf_lo rf_lo_adjust[LO_ADJUST_MAX] = {
1565 struct bwi_rf_lo lo_min;
1567 int found, loop_count, adjust_state;
1569 bcopy(src_lo, &lo_min, sizeof(lo_min));
1570 RF_LO_WRITE(mac, &lo_min);
1571 devi_min = bwi_rf_lo_devi_measure(mac, devi_ctrl);
1573 loop_count = 12; /* XXX */
1576 struct bwi_rf_lo lo_base;
1580 if (adjust_state == 0) {
1582 fin = LO_ADJUST_MAX;
1583 } else if (adjust_state % 2 == 0) {
1584 i = adjust_state - 1;
1585 fin = adjust_state + 1;
1587 i = adjust_state - 2;
1588 fin = adjust_state + 2;
1591 if (i < LO_ADJUST_MIN)
1593 KASSERT(i <= LO_ADJUST_MAX && i >= LO_ADJUST_MIN, ("i %d", i));
1595 if (fin > LO_ADJUST_MAX)
1596 fin -= LO_ADJUST_MAX;
1597 KASSERT(fin <= LO_ADJUST_MAX && fin >= LO_ADJUST_MIN,
1600 bcopy(&lo_min, &lo_base, sizeof(lo_base));
1602 struct bwi_rf_lo lo;
1604 lo.ctrl_hi = lo_base.ctrl_hi +
1605 rf_lo_adjust[i - 1].ctrl_hi;
1606 lo.ctrl_lo = lo_base.ctrl_lo +
1607 rf_lo_adjust[i - 1].ctrl_lo;
1609 if (abs(lo.ctrl_lo) < 9 && abs(lo.ctrl_hi) < 9) {
1612 RF_LO_WRITE(mac, &lo);
1613 devi = bwi_rf_lo_devi_measure(mac, devi_ctrl);
1614 if (devi < devi_min) {
1618 bcopy(&lo, &lo_min, sizeof(lo_min));
1623 if (i == LO_ADJUST_MAX)
1628 } while (loop_count-- && found);
1630 bcopy(&lo_min, dst_lo, sizeof(*dst_lo));
1632 #undef LO_ADJUST_MIN
1633 #undef LO_ADJUST_MAX
1637 bwi_rf_calc_nrssi_slope_11b(struct bwi_mac *mac)
1639 #define SAVE_RF_MAX 3
1640 #define SAVE_PHY_MAX 8
1642 static const uint16_t save_rf_regs[SAVE_RF_MAX] =
1643 { 0x7a, 0x52, 0x43 };
1644 static const uint16_t save_phy_regs[SAVE_PHY_MAX] =
1645 { 0x30, 0x26, 0x15, 0x2a, 0x20, 0x5a, 0x59, 0x58 };
1647 struct bwi_softc *sc = mac->mac_sc;
1648 struct bwi_rf *rf = &mac->mac_rf;
1649 struct bwi_phy *phy = &mac->mac_phy;
1650 uint16_t save_rf[SAVE_RF_MAX];
1651 uint16_t save_phy[SAVE_PHY_MAX];
1652 uint16_t ant_div, bbp_atten, chan_ex;
1657 * Save RF/PHY registers for later restoration
1659 for (i = 0; i < SAVE_RF_MAX; ++i)
1660 save_rf[i] = RF_READ(mac, save_rf_regs[i]);
1661 for (i = 0; i < SAVE_PHY_MAX; ++i)
1662 save_phy[i] = PHY_READ(mac, save_phy_regs[i]);
1664 ant_div = CSR_READ_2(sc, BWI_RF_ANTDIV);
1665 bbp_atten = CSR_READ_2(sc, BWI_BBP_ATTEN);
1666 chan_ex = CSR_READ_2(sc, BWI_RF_CHAN_EX);
1671 if (phy->phy_rev >= 5)
1672 RF_CLRBITS(mac, 0x7a, 0xff80);
1674 RF_CLRBITS(mac, 0x7a, 0xfff0);
1675 PHY_WRITE(mac, 0x30, 0xff);
1677 CSR_WRITE_2(sc, BWI_BPHY_CTRL, 0x7f7f);
1679 PHY_WRITE(mac, 0x26, 0);
1680 PHY_SETBITS(mac, 0x15, 0x20);
1681 PHY_WRITE(mac, 0x2a, 0x8a3);
1682 RF_SETBITS(mac, 0x7a, 0x80);
1684 nrssi[0] = (int16_t)PHY_READ(mac, 0x27);
1689 RF_CLRBITS(mac, 0x7a, 0xff80);
1690 if (phy->phy_version >= 2)
1691 CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0x40);
1692 else if (phy->phy_version == 0)
1693 CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0x122);
1695 CSR_CLRBITS_2(sc, BWI_RF_CHAN_EX, 0xdfff);
1697 PHY_WRITE(mac, 0x20, 0x3f3f);
1698 PHY_WRITE(mac, 0x15, 0xf330);
1700 RF_WRITE(mac, 0x5a, 0x60);
1701 RF_CLRBITS(mac, 0x43, 0xff0f);
1703 PHY_WRITE(mac, 0x5a, 0x480);
1704 PHY_WRITE(mac, 0x59, 0x810);
1705 PHY_WRITE(mac, 0x58, 0xd);
1709 nrssi[1] = (int16_t)PHY_READ(mac, 0x27);
1712 * Restore saved RF/PHY registers
1714 PHY_WRITE(mac, save_phy_regs[0], save_phy[0]);
1715 RF_WRITE(mac, save_rf_regs[0], save_rf[0]);
1717 CSR_WRITE_2(sc, BWI_RF_ANTDIV, ant_div);
1719 for (i = 1; i < 4; ++i)
1720 PHY_WRITE(mac, save_phy_regs[i], save_phy[i]);
1722 bwi_rf_work_around(mac, rf->rf_curchan);
1724 if (phy->phy_version != 0)
1725 CSR_WRITE_2(sc, BWI_RF_CHAN_EX, chan_ex);
1727 for (; i < SAVE_PHY_MAX; ++i)
1728 PHY_WRITE(mac, save_phy_regs[i], save_phy[i]);
1730 for (i = 1; i < SAVE_RF_MAX; ++i)
1731 RF_WRITE(mac, save_rf_regs[i], save_rf[i]);
1734 * Install calculated narrow RSSI values
1736 if (nrssi[0] == nrssi[1])
1737 rf->rf_nrssi_slope = 0x10000;
1739 rf->rf_nrssi_slope = 0x400000 / (nrssi[0] - nrssi[1]);
1740 if (nrssi[0] <= -4) {
1741 rf->rf_nrssi[0] = nrssi[0];
1742 rf->rf_nrssi[1] = nrssi[1];
1750 bwi_rf_set_nrssi_ofs_11g(struct bwi_mac *mac)
1752 #define SAVE_RF_MAX 2
1753 #define SAVE_PHY_COMM_MAX 10
1754 #define SAVE_PHY6_MAX 8
1756 static const uint16_t save_rf_regs[SAVE_RF_MAX] =
1758 static const uint16_t save_phy_comm_regs[SAVE_PHY_COMM_MAX] = {
1759 0x0001, 0x0811, 0x0812, 0x0814,
1760 0x0815, 0x005a, 0x0059, 0x0058,
1763 static const uint16_t save_phy6_regs[SAVE_PHY6_MAX] = {
1764 0x002e, 0x002f, 0x080f, 0x0810,
1765 0x0801, 0x0060, 0x0014, 0x0478
1768 struct bwi_phy *phy = &mac->mac_phy;
1769 uint16_t save_rf[SAVE_RF_MAX];
1770 uint16_t save_phy_comm[SAVE_PHY_COMM_MAX];
1771 uint16_t save_phy6[SAVE_PHY6_MAX];
1772 uint16_t rf7b = 0xffff;
1774 int i, phy6_idx = 0;
1776 for (i = 0; i < SAVE_PHY_COMM_MAX; ++i)
1777 save_phy_comm[i] = PHY_READ(mac, save_phy_comm_regs[i]);
1778 for (i = 0; i < SAVE_RF_MAX; ++i)
1779 save_rf[i] = RF_READ(mac, save_rf_regs[i]);
1781 PHY_CLRBITS(mac, 0x429, 0x8000);
1782 PHY_FILT_SETBITS(mac, 0x1, 0x3fff, 0x4000);
1783 PHY_SETBITS(mac, 0x811, 0xc);
1784 PHY_FILT_SETBITS(mac, 0x812, 0xfff3, 0x4);
1785 PHY_CLRBITS(mac, 0x802, 0x3);
1787 if (phy->phy_rev >= 6) {
1788 for (i = 0; i < SAVE_PHY6_MAX; ++i)
1789 save_phy6[i] = PHY_READ(mac, save_phy6_regs[i]);
1791 PHY_WRITE(mac, 0x2e, 0);
1792 PHY_WRITE(mac, 0x2f, 0);
1793 PHY_WRITE(mac, 0x80f, 0);
1794 PHY_WRITE(mac, 0x810, 0);
1795 PHY_SETBITS(mac, 0x478, 0x100);
1796 PHY_SETBITS(mac, 0x801, 0x40);
1797 PHY_SETBITS(mac, 0x60, 0x40);
1798 PHY_SETBITS(mac, 0x14, 0x200);
1801 RF_SETBITS(mac, 0x7a, 0x70);
1802 RF_SETBITS(mac, 0x7a, 0x80);
1806 nrssi = bwi_nrssi_11g(mac);
1808 for (i = 7; i >= 4; --i) {
1809 RF_WRITE(mac, 0x7b, i);
1811 nrssi = bwi_nrssi_11g(mac);
1812 if (nrssi < 31 && rf7b == 0xffff)
1818 struct bwi_gains gains;
1820 RF_CLRBITS(mac, 0x7a, 0xff80);
1822 PHY_SETBITS(mac, 0x814, 0x1);
1823 PHY_CLRBITS(mac, 0x815, 0x1);
1824 PHY_SETBITS(mac, 0x811, 0xc);
1825 PHY_SETBITS(mac, 0x812, 0xc);
1826 PHY_SETBITS(mac, 0x811, 0x30);
1827 PHY_SETBITS(mac, 0x812, 0x30);
1828 PHY_WRITE(mac, 0x5a, 0x480);
1829 PHY_WRITE(mac, 0x59, 0x810);
1830 PHY_WRITE(mac, 0x58, 0xd);
1831 if (phy->phy_version == 0)
1832 PHY_WRITE(mac, 0x3, 0x122);
1834 PHY_SETBITS(mac, 0xa, 0x2000);
1835 PHY_SETBITS(mac, 0x814, 0x4);
1836 PHY_CLRBITS(mac, 0x815, 0x4);
1837 PHY_FILT_SETBITS(mac, 0x3, 0xff9f, 0x40);
1838 RF_SETBITS(mac, 0x7a, 0xf);
1840 bzero(&gains, sizeof(gains));
1841 gains.tbl_gain1 = 3;
1842 gains.tbl_gain2 = 0;
1844 bwi_set_gains(mac, &gains);
1846 RF_FILT_SETBITS(mac, 0x43, 0xf0, 0xf);
1849 nrssi = bwi_nrssi_11g(mac);
1851 for (i = 0; i < 4; ++i) {
1852 RF_WRITE(mac, 0x7b, i);
1854 nrssi = bwi_nrssi_11g(mac);
1855 if (nrssi > -31 && rf7b == 0xffff)
1864 RF_WRITE(mac, 0x7b, rf7b);
1867 * Restore saved RF/PHY registers
1869 if (phy->phy_rev >= 6) {
1870 for (phy6_idx = 0; phy6_idx < 4; ++phy6_idx) {
1871 PHY_WRITE(mac, save_phy6_regs[phy6_idx],
1872 save_phy6[phy6_idx]);
1876 /* Saved PHY registers 0, 1, 2 are handled later */
1877 for (i = 3; i < SAVE_PHY_COMM_MAX; ++i)
1878 PHY_WRITE(mac, save_phy_comm_regs[i], save_phy_comm[i]);
1880 for (i = SAVE_RF_MAX - 1; i >= 0; --i)
1881 RF_WRITE(mac, save_rf_regs[i], save_rf[i]);
1883 PHY_SETBITS(mac, 0x802, 0x3);
1884 PHY_SETBITS(mac, 0x429, 0x8000);
1886 bwi_set_gains(mac, NULL);
1888 if (phy->phy_rev >= 6) {
1889 for (; phy6_idx < SAVE_PHY6_MAX; ++phy6_idx) {
1890 PHY_WRITE(mac, save_phy6_regs[phy6_idx],
1891 save_phy6[phy6_idx]);
1895 PHY_WRITE(mac, save_phy_comm_regs[0], save_phy_comm[0]);
1896 PHY_WRITE(mac, save_phy_comm_regs[2], save_phy_comm[2]);
1897 PHY_WRITE(mac, save_phy_comm_regs[1], save_phy_comm[1]);
1900 #undef SAVE_PHY_COMM_MAX
1901 #undef SAVE_PHY6_MAX
1905 bwi_rf_calc_nrssi_slope_11g(struct bwi_mac *mac)
1907 #define SAVE_RF_MAX 3
1908 #define SAVE_PHY_COMM_MAX 4
1909 #define SAVE_PHY3_MAX 8
1911 static const uint16_t save_rf_regs[SAVE_RF_MAX] =
1912 { 0x7a, 0x52, 0x43 };
1913 static const uint16_t save_phy_comm_regs[SAVE_PHY_COMM_MAX] =
1914 { 0x15, 0x5a, 0x59, 0x58 };
1915 static const uint16_t save_phy3_regs[SAVE_PHY3_MAX] = {
1916 0x002e, 0x002f, 0x080f, 0x0810,
1917 0x0801, 0x0060, 0x0014, 0x0478
1920 struct bwi_softc *sc = mac->mac_sc;
1921 struct bwi_phy *phy = &mac->mac_phy;
1922 struct bwi_rf *rf = &mac->mac_rf;
1923 uint16_t save_rf[SAVE_RF_MAX];
1924 uint16_t save_phy_comm[SAVE_PHY_COMM_MAX];
1925 uint16_t save_phy3[SAVE_PHY3_MAX];
1926 uint16_t ant_div, bbp_atten, chan_ex;
1927 struct bwi_gains gains;
1929 int i, phy3_idx = 0;
1931 if (rf->rf_rev >= 9)
1933 else if (rf->rf_rev == 8)
1934 bwi_rf_set_nrssi_ofs_11g(mac);
1936 PHY_CLRBITS(mac, 0x429, 0x8000);
1937 PHY_CLRBITS(mac, 0x802, 0x3);
1940 * Save RF/PHY registers for later restoration
1942 ant_div = CSR_READ_2(sc, BWI_RF_ANTDIV);
1943 CSR_SETBITS_2(sc, BWI_RF_ANTDIV, 0x8000);
1945 for (i = 0; i < SAVE_RF_MAX; ++i)
1946 save_rf[i] = RF_READ(mac, save_rf_regs[i]);
1947 for (i = 0; i < SAVE_PHY_COMM_MAX; ++i)
1948 save_phy_comm[i] = PHY_READ(mac, save_phy_comm_regs[i]);
1950 bbp_atten = CSR_READ_2(sc, BWI_BBP_ATTEN);
1951 chan_ex = CSR_READ_2(sc, BWI_RF_CHAN_EX);
1953 if (phy->phy_rev >= 3) {
1954 for (i = 0; i < SAVE_PHY3_MAX; ++i)
1955 save_phy3[i] = PHY_READ(mac, save_phy3_regs[i]);
1957 PHY_WRITE(mac, 0x2e, 0);
1958 PHY_WRITE(mac, 0x810, 0);
1960 if (phy->phy_rev == 4 || phy->phy_rev == 6 ||
1961 phy->phy_rev == 7) {
1962 PHY_SETBITS(mac, 0x478, 0x100);
1963 PHY_SETBITS(mac, 0x810, 0x40);
1964 } else if (phy->phy_rev == 3 || phy->phy_rev == 5) {
1965 PHY_CLRBITS(mac, 0x810, 0x40);
1968 PHY_SETBITS(mac, 0x60, 0x40);
1969 PHY_SETBITS(mac, 0x14, 0x200);
1975 RF_SETBITS(mac, 0x7a, 0x70);
1977 bzero(&gains, sizeof(gains));
1978 gains.tbl_gain1 = 0;
1979 gains.tbl_gain2 = 8;
1981 bwi_set_gains(mac, &gains);
1983 RF_CLRBITS(mac, 0x7a, 0xff08);
1984 if (phy->phy_rev >= 2) {
1985 PHY_FILT_SETBITS(mac, 0x811, 0xffcf, 0x30);
1986 PHY_FILT_SETBITS(mac, 0x812, 0xffcf, 0x10);
1989 RF_SETBITS(mac, 0x7a, 0x80);
1991 nrssi[0] = bwi_nrssi_11g(mac);
1996 RF_CLRBITS(mac, 0x7a, 0xff80);
1997 if (phy->phy_version >= 2)
1998 PHY_FILT_SETBITS(mac, 0x3, 0xff9f, 0x40);
1999 CSR_SETBITS_2(sc, BWI_RF_CHAN_EX, 0x2000);
2001 RF_SETBITS(mac, 0x7a, 0xf);
2002 PHY_WRITE(mac, 0x15, 0xf330);
2003 if (phy->phy_rev >= 2) {
2004 PHY_FILT_SETBITS(mac, 0x812, 0xffcf, 0x20);
2005 PHY_FILT_SETBITS(mac, 0x811, 0xffcf, 0x20);
2008 bzero(&gains, sizeof(gains));
2009 gains.tbl_gain1 = 3;
2010 gains.tbl_gain2 = 0;
2012 bwi_set_gains(mac, &gains);
2014 if (rf->rf_rev == 8) {
2015 RF_WRITE(mac, 0x43, 0x1f);
2017 RF_FILT_SETBITS(mac, 0x52, 0xff0f, 0x60);
2018 RF_FILT_SETBITS(mac, 0x43, 0xfff0, 0x9);
2020 PHY_WRITE(mac, 0x5a, 0x480);
2021 PHY_WRITE(mac, 0x59, 0x810);
2022 PHY_WRITE(mac, 0x58, 0xd);
2025 nrssi[1] = bwi_nrssi_11g(mac);
2028 * Install calculated narrow RSSI values
2030 if (nrssi[1] == nrssi[0])
2031 rf->rf_nrssi_slope = 0x10000;
2033 rf->rf_nrssi_slope = 0x400000 / (nrssi[0] - nrssi[1]);
2034 if (nrssi[0] >= -4) {
2035 rf->rf_nrssi[0] = nrssi[1];
2036 rf->rf_nrssi[1] = nrssi[0];
2040 * Restore saved RF/PHY registers
2042 if (phy->phy_rev >= 3) {
2043 for (phy3_idx = 0; phy3_idx < 4; ++phy3_idx) {
2044 PHY_WRITE(mac, save_phy3_regs[phy3_idx],
2045 save_phy3[phy3_idx]);
2048 if (phy->phy_rev >= 2) {
2049 PHY_CLRBITS(mac, 0x812, 0x30);
2050 PHY_CLRBITS(mac, 0x811, 0x30);
2053 for (i = 0; i < SAVE_RF_MAX; ++i)
2054 RF_WRITE(mac, save_rf_regs[i], save_rf[i]);
2056 CSR_WRITE_2(sc, BWI_RF_ANTDIV, ant_div);
2057 CSR_WRITE_2(sc, BWI_BBP_ATTEN, bbp_atten);
2058 CSR_WRITE_2(sc, BWI_RF_CHAN_EX, chan_ex);
2060 for (i = 0; i < SAVE_PHY_COMM_MAX; ++i)
2061 PHY_WRITE(mac, save_phy_comm_regs[i], save_phy_comm[i]);
2063 bwi_rf_work_around(mac, rf->rf_curchan);
2064 PHY_SETBITS(mac, 0x802, 0x3);
2065 bwi_set_gains(mac, NULL);
2066 PHY_SETBITS(mac, 0x429, 0x8000);
2068 if (phy->phy_rev >= 3) {
2069 for (; phy3_idx < SAVE_PHY3_MAX; ++phy3_idx) {
2070 PHY_WRITE(mac, save_phy3_regs[phy3_idx],
2071 save_phy3[phy3_idx]);
2075 bwi_rf_init_sw_nrssi_table(mac);
2076 bwi_rf_set_nrssi_thr_11g(mac);
2079 #undef SAVE_PHY_COMM_MAX
2080 #undef SAVE_PHY3_MAX
2084 bwi_rf_init_sw_nrssi_table(struct bwi_mac *mac)
2086 struct bwi_rf *rf = &mac->mac_rf;
2089 d = 0x1f - rf->rf_nrssi[0];
2090 for (i = 0; i < BWI_NRSSI_TBLSZ; ++i) {
2093 val = (((i - d) * rf->rf_nrssi_slope) / 0x10000) + 0x3a;
2096 else if (val > 0x3f)
2099 rf->rf_nrssi_table[i] = val;
2104 bwi_rf_init_hw_nrssi_table(struct bwi_mac *mac, uint16_t adjust)
2108 for (i = 0; i < BWI_NRSSI_TBLSZ; ++i) {
2111 val = bwi_nrssi_read(mac, i);
2119 bwi_nrssi_write(mac, i, val);
2124 bwi_rf_set_nrssi_thr_11b(struct bwi_mac *mac)
2126 struct bwi_rf *rf = &mac->mac_rf;
2129 if (rf->rf_type != BWI_RF_T_BCM2050 ||
2130 (mac->mac_sc->sc_card_flags & BWI_CARD_F_SW_NRSSI) == 0)
2134 * Calculate nrssi threshold
2136 if (rf->rf_rev >= 6) {
2137 thr = (rf->rf_nrssi[1] - rf->rf_nrssi[0]) * 32;
2138 thr += 20 * (rf->rf_nrssi[0] + 1);
2141 thr = rf->rf_nrssi[1] - 5;
2145 else if (thr > 0x3e)
2148 PHY_READ(mac, BWI_PHYR_NRSSI_THR_11B); /* dummy read */
2149 PHY_WRITE(mac, BWI_PHYR_NRSSI_THR_11B, (((uint16_t)thr) << 8) | 0x1c);
2151 if (rf->rf_rev >= 6) {
2152 PHY_WRITE(mac, 0x87, 0xe0d);
2153 PHY_WRITE(mac, 0x86, 0xc0b);
2154 PHY_WRITE(mac, 0x85, 0xa09);
2155 PHY_WRITE(mac, 0x84, 0x808);
2156 PHY_WRITE(mac, 0x83, 0x808);
2157 PHY_WRITE(mac, 0x82, 0x604);
2158 PHY_WRITE(mac, 0x81, 0x302);
2159 PHY_WRITE(mac, 0x80, 0x100);
2163 static __inline int32_t
2164 _nrssi_threshold(const struct bwi_rf *rf, int32_t val)
2166 val *= (rf->rf_nrssi[1] - rf->rf_nrssi[0]);
2167 val += (rf->rf_nrssi[0] << 6);
2181 bwi_rf_set_nrssi_thr_11g(struct bwi_mac *mac)
2187 * Find the two nrssi thresholds
2189 if ((mac->mac_phy.phy_flags & BWI_PHY_F_LINKED) == 0 ||
2190 (mac->mac_sc->sc_card_flags & BWI_CARD_F_SW_NRSSI) == 0) {
2193 nrssi = bwi_nrssi_read(mac, 0x20);
2205 /* TODO Interfere mode */
2206 thr1 = _nrssi_threshold(&mac->mac_rf, 0x11);
2207 thr2 = _nrssi_threshold(&mac->mac_rf, 0xe);
2210 #define NRSSI_THR1_MASK __BITS(5, 0)
2211 #define NRSSI_THR2_MASK __BITS(11, 6)
2213 thr = __SHIFTIN((uint32_t)thr1, NRSSI_THR1_MASK) |
2214 __SHIFTIN((uint32_t)thr2, NRSSI_THR2_MASK);
2215 PHY_FILT_SETBITS(mac, BWI_PHYR_NRSSI_THR_11G, 0xf000, thr);
2217 #undef NRSSI_THR1_MASK
2218 #undef NRSSI_THR2_MASK
2222 bwi_rf_clear_tssi(struct bwi_mac *mac)
2224 /* XXX use function pointer */
2225 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11A) {
2231 val = __SHIFTIN(BWI_INVALID_TSSI, BWI_LO_TSSI_MASK) |
2232 __SHIFTIN(BWI_INVALID_TSSI, BWI_HI_TSSI_MASK);
2234 for (i = 0; i < 2; ++i) {
2235 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ,
2236 BWI_COMM_MOBJ_TSSI_DS + (i * 2), val);
2239 for (i = 0; i < 2; ++i) {
2240 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ,
2241 BWI_COMM_MOBJ_TSSI_OFDM + (i * 2), val);
2247 bwi_rf_clear_state(struct bwi_rf *rf)
2251 rf->rf_flags &= ~BWI_RF_CLEAR_FLAGS;
2252 bzero(rf->rf_lo, sizeof(rf->rf_lo));
2253 bzero(rf->rf_lo_used, sizeof(rf->rf_lo_used));
2255 rf->rf_nrssi_slope = 0;
2256 rf->rf_nrssi[0] = BWI_INVALID_NRSSI;
2257 rf->rf_nrssi[1] = BWI_INVALID_NRSSI;
2259 for (i = 0; i < BWI_NRSSI_TBLSZ; ++i)
2260 rf->rf_nrssi_table[i] = i;
2265 bcopy(rf->rf_txpower_map0, rf->rf_txpower_map,
2266 sizeof(rf->rf_txpower_map));
2267 rf->rf_idle_tssi = rf->rf_idle_tssi0;
2271 bwi_rf_on_11a(struct bwi_mac *mac)
2277 bwi_rf_on_11bg(struct bwi_mac *mac)
2279 struct bwi_phy *phy = &mac->mac_phy;
2281 PHY_WRITE(mac, 0x15, 0x8000);
2282 PHY_WRITE(mac, 0x15, 0xcc00);
2283 if (phy->phy_flags & BWI_PHY_F_LINKED)
2284 PHY_WRITE(mac, 0x15, 0xc0);
2286 PHY_WRITE(mac, 0x15, 0);
2288 bwi_rf_set_chan(mac, 6 /* XXX */, 1);
2292 bwi_rf_set_ant_mode(struct bwi_mac *mac, int ant_mode)
2294 struct bwi_softc *sc = mac->mac_sc;
2295 struct bwi_phy *phy = &mac->mac_phy;
2298 KASSERT(ant_mode == BWI_ANT_MODE_0 ||
2299 ant_mode == BWI_ANT_MODE_1 ||
2300 ant_mode == BWI_ANT_MODE_AUTO, ("ant_mode %d", ant_mode));
2302 HFLAGS_CLRBITS(mac, BWI_HFLAG_AUTO_ANTDIV);
2304 if (phy->phy_mode == IEEE80211_MODE_11B) {
2305 /* NOTE: v4/v3 conflicts, take v3 */
2306 if (mac->mac_rev == 2)
2307 val = BWI_ANT_MODE_AUTO;
2311 PHY_FILT_SETBITS(mac, 0x3e2, 0xfe7f, val);
2312 } else { /* 11a/g */
2313 /* XXX reg/value naming */
2314 val = ant_mode << 7;
2315 PHY_FILT_SETBITS(mac, 0x401, 0x7e7f, val);
2317 if (ant_mode == BWI_ANT_MODE_AUTO)
2318 PHY_CLRBITS(mac, 0x42b, 0x100);
2320 if (phy->phy_mode == IEEE80211_MODE_11A) {
2323 if (ant_mode == BWI_ANT_MODE_AUTO)
2324 PHY_SETBITS(mac, 0x48c, 0x2000);
2326 PHY_CLRBITS(mac, 0x48c, 0x2000);
2328 if (phy->phy_rev >= 2) {
2329 PHY_SETBITS(mac, 0x461, 0x10);
2330 PHY_FILT_SETBITS(mac, 0x4ad, 0xff00, 0x15);
2331 if (phy->phy_rev == 2) {
2332 PHY_WRITE(mac, 0x427, 0x8);
2334 PHY_FILT_SETBITS(mac, 0x427,
2338 if (phy->phy_rev >= 6)
2339 PHY_WRITE(mac, 0x49b, 0xdc);
2344 /* XXX v4 set AUTO_ANTDIV unconditionally */
2345 if (ant_mode == BWI_ANT_MODE_AUTO)
2346 HFLAGS_SETBITS(mac, BWI_HFLAG_AUTO_ANTDIV);
2348 val = ant_mode << 8;
2349 MOBJ_FILT_SETBITS_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_TX_BEACON,
2351 MOBJ_FILT_SETBITS_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_TX_ACK,
2353 MOBJ_FILT_SETBITS_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_TX_PROBE_RESP,
2356 /* XXX what's these */
2357 if (phy->phy_mode == IEEE80211_MODE_11B)
2358 CSR_SETBITS_2(sc, 0x5e, 0x4);
2360 CSR_WRITE_4(sc, 0x100, 0x1000000);
2361 if (mac->mac_rev < 5)
2362 CSR_WRITE_4(sc, 0x10c, 0x1000000);
2364 mac->mac_rf.rf_ant_mode = ant_mode;
2368 bwi_rf_get_latest_tssi(struct bwi_mac *mac, int8_t tssi[], uint16_t ofs)
2372 for (i = 0; i < 4; ) {
2375 val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, ofs + i);
2376 tssi[i++] = (int8_t)__SHIFTOUT(val, BWI_LO_TSSI_MASK);
2377 tssi[i++] = (int8_t)__SHIFTOUT(val, BWI_HI_TSSI_MASK);
2380 for (i = 0; i < 4; ++i) {
2381 if (tssi[i] == BWI_INVALID_TSSI)
2388 bwi_rf_tssi2dbm(struct bwi_mac *mac, int8_t tssi, int8_t *txpwr)
2390 struct bwi_rf *rf = &mac->mac_rf;
2393 pwr_idx = rf->rf_idle_tssi + (int)tssi - rf->rf_base_tssi;
2395 if (pwr_idx < 0 || pwr_idx >= BWI_TSSI_MAX)
2400 else if (pwr_idx >= BWI_TSSI_MAX)
2401 pwr_idx = BWI_TSSI_MAX - 1;
2404 *txpwr = rf->rf_txpower_map[pwr_idx];
2409 bwi_rf_calc_rssi_bcm2050(struct bwi_mac *mac, const struct bwi_rxbuf_hdr *hdr)
2411 uint16_t flags1, flags3;
2414 rssi = hdr->rxh_rssi;
2415 flags1 = le16toh(hdr->rxh_flags1);
2416 flags3 = le16toh(hdr->rxh_flags3);
2418 if (flags1 & BWI_RXH_F1_OFDM) {
2421 if (flags3 & BWI_RXH_F3_BCM2050_RSSI)
2428 if (mac->mac_sc->sc_card_flags & BWI_CARD_F_SW_NRSSI) {
2429 struct bwi_rf *rf = &mac->mac_rf;
2431 if (rssi >= BWI_NRSSI_TBLSZ)
2432 rssi = BWI_NRSSI_TBLSZ - 1;
2434 rssi = ((31 - (int)rf->rf_nrssi_table[rssi]) * -131) / 128;
2437 rssi = ((31 - rssi) * -149) / 128;
2441 if (mac->mac_phy.phy_mode != IEEE80211_MODE_11G)
2444 if (flags3 & BWI_RXH_F3_BCM2050_RSSI)
2447 lna_gain = __SHIFTOUT(le16toh(hdr->rxh_phyinfo),
2448 BWI_RXH_PHYINFO_LNAGAIN);
2449 DPRINTF(mac->mac_sc, BWI_DBG_RF | BWI_DBG_RX,
2450 "lna_gain %d, phyinfo 0x%04x\n",
2451 lna_gain, le16toh(hdr->rxh_phyinfo));
2465 * According to v3 spec, we should do _nothing_ here,
2466 * but it seems that the result RSSI will be too low
2467 * (relative to what ath(4) says). Raise it a little
2473 panic("impossible lna gain %d", lna_gain);
2479 bwi_rf_calc_rssi_bcm2053(struct bwi_mac *mac, const struct bwi_rxbuf_hdr *hdr)
2484 rssi = (((int)hdr->rxh_rssi - 11) * 103) / 64;
2486 flags1 = le16toh(hdr->rxh_flags1);
2487 if (flags1 & BWI_RXH_F1_BCM2053_RSSI)
2495 bwi_rf_calc_rssi_bcm2060(struct bwi_mac *mac, const struct bwi_rxbuf_hdr *hdr)
2499 rssi = hdr->rxh_rssi;
2506 bwi_rf_calc_noise_bcm2050(struct bwi_mac *mac)
2511 val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_RF_NOISE);
2512 noise = (int)val; /* XXX check bounds? */
2514 if (mac->mac_sc->sc_card_flags & BWI_CARD_F_SW_NRSSI) {
2515 struct bwi_rf *rf = &mac->mac_rf;
2517 if (noise >= BWI_NRSSI_TBLSZ)
2518 noise = BWI_NRSSI_TBLSZ - 1;
2520 noise = ((31 - (int)rf->rf_nrssi_table[noise]) * -131) / 128;
2523 noise = ((31 - noise) * -149) / 128;
2530 bwi_rf_calc_noise_bcm2053(struct bwi_mac *mac)
2535 val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_RF_NOISE);
2536 noise = (int)val; /* XXX check bounds? */
2538 noise = ((noise - 11) * 103) / 64;
2544 bwi_rf_calc_noise_bcm2060(struct bwi_mac *mac)
2546 /* XXX Dont know how to calc */
2547 return (BWI_NOISE_FLOOR);
2551 bwi_rf_lo_measure_11b(struct bwi_mac *mac)
2557 for (i = 0; i < 10; ++i) {
2558 PHY_WRITE(mac, 0x15, 0xafa0);
2560 PHY_WRITE(mac, 0x15, 0xefa0);
2562 PHY_WRITE(mac, 0x15, 0xffa0);
2565 val += PHY_READ(mac, 0x2c);
2571 bwi_rf_lo_update_11b(struct bwi_mac *mac)
2573 struct bwi_softc *sc = mac->mac_sc;
2574 struct bwi_rf *rf = &mac->mac_rf;
2575 struct rf_saveregs regs;
2576 uint16_t rf_val, phy_val, min_val, val;
2577 uint16_t rf52, bphy_ctrl;
2580 DPRINTF(sc, BWI_DBG_RF | BWI_DBG_INIT, "%s enter\n", __func__);
2582 bzero(®s, sizeof(regs));
2586 * Save RF/PHY registers for later restoration
2588 SAVE_PHY_REG(mac, ®s, 15);
2589 rf52 = RF_READ(mac, 0x52) & 0xfff0;
2590 if (rf->rf_type == BWI_RF_T_BCM2050) {
2591 SAVE_PHY_REG(mac, ®s, 0a);
2592 SAVE_PHY_REG(mac, ®s, 2a);
2593 SAVE_PHY_REG(mac, ®s, 35);
2594 SAVE_PHY_REG(mac, ®s, 03);
2595 SAVE_PHY_REG(mac, ®s, 01);
2596 SAVE_PHY_REG(mac, ®s, 30);
2598 SAVE_RF_REG(mac, ®s, 43);
2599 SAVE_RF_REG(mac, ®s, 7a);
2601 bphy_ctrl = CSR_READ_2(sc, BWI_BPHY_CTRL);
2603 SAVE_RF_REG(mac, ®s, 52);
2606 PHY_WRITE(mac, 0x30, 0xff);
2607 CSR_WRITE_2(sc, BWI_PHY_CTRL, 0x3f3f);
2608 PHY_WRITE(mac, 0x35, regs.phy_35 & 0xff7f);
2609 RF_WRITE(mac, 0x7a, regs.rf_7a & 0xfff0);
2612 PHY_WRITE(mac, 0x15, 0xb000);
2614 if (rf->rf_type == BWI_RF_T_BCM2050) {
2615 PHY_WRITE(mac, 0x2b, 0x203);
2616 PHY_WRITE(mac, 0x2a, 0x8a3);
2618 PHY_WRITE(mac, 0x2b, 0x1402);
2625 min_val = UINT16_MAX;
2627 for (i = 0; i < 4; ++i) {
2628 RF_WRITE(mac, 0x52, rf52 | i);
2629 bwi_rf_lo_measure_11b(mac); /* Ignore return value */
2631 for (i = 0; i < 10; ++i) {
2632 RF_WRITE(mac, 0x52, rf52 | i);
2634 val = bwi_rf_lo_measure_11b(mac) / 10;
2635 if (val < min_val) {
2640 RF_WRITE(mac, 0x52, rf52 | rf_val);
2646 min_val = UINT16_MAX;
2648 for (i = -4; i < 5; i += 2) {
2651 for (j = -4; j < 5; j += 2) {
2654 phy2f = (0x100 * i) + j;
2657 PHY_WRITE(mac, 0x2f, phy2f);
2659 val = bwi_rf_lo_measure_11b(mac) / 10;
2660 if (val < min_val) {
2666 PHY_WRITE(mac, 0x2f, phy_val + 0x101);
2669 * Restore saved RF/PHY registers
2671 if (rf->rf_type == BWI_RF_T_BCM2050) {
2672 RESTORE_PHY_REG(mac, ®s, 0a);
2673 RESTORE_PHY_REG(mac, ®s, 2a);
2674 RESTORE_PHY_REG(mac, ®s, 35);
2675 RESTORE_PHY_REG(mac, ®s, 03);
2676 RESTORE_PHY_REG(mac, ®s, 01);
2677 RESTORE_PHY_REG(mac, ®s, 30);
2679 RESTORE_RF_REG(mac, ®s, 43);
2680 RESTORE_RF_REG(mac, ®s, 7a);
2682 RF_FILT_SETBITS(mac, 0x52, 0xf, regs.rf_52);
2684 CSR_WRITE_2(sc, BWI_BPHY_CTRL, bphy_ctrl);
2686 RESTORE_PHY_REG(mac, ®s, 15);
2688 bwi_rf_work_around(mac, rf->rf_curchan);