2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/dev/netif/bwi/bwimac.c,v 1.13 2008/02/15 11:15:38 sephe Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/endian.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
56 #include <sys/linker.h>
57 #include <sys/firmware.h>
60 #include <net/if_var.h>
61 #include <net/if_dl.h>
62 #include <net/if_media.h>
63 #include <net/if_types.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/if_llc.h>
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_radiotap.h>
70 #include <net80211/ieee80211_amrr.h>
71 #include <net80211/ieee80211_phy.h>
73 #include <machine/bus.h>
75 #include <dev/bwi/bitops.h>
76 #include <dev/bwi/if_bwireg.h>
77 #include <dev/bwi/if_bwivar.h>
78 #include <dev/bwi/bwimac.h>
79 #include <dev/bwi/bwirf.h>
80 #include <dev/bwi/bwiphy.h>
82 struct bwi_retry_lim {
89 static int bwi_mac_test(struct bwi_mac *);
90 static int bwi_mac_get_property(struct bwi_mac *);
92 static void bwi_mac_set_retry_lim(struct bwi_mac *,
93 const struct bwi_retry_lim *);
94 static void bwi_mac_set_ackrates(struct bwi_mac *,
95 const struct ieee80211_rate_table *rt,
96 const struct ieee80211_rateset *);
98 static int bwi_mac_gpio_init(struct bwi_mac *);
99 static int bwi_mac_gpio_fini(struct bwi_mac *);
100 static void bwi_mac_opmode_init(struct bwi_mac *);
101 static void bwi_mac_hostflags_init(struct bwi_mac *);
102 static void bwi_mac_bss_param_init(struct bwi_mac *);
104 static void bwi_mac_fw_free(struct bwi_mac *);
105 static int bwi_mac_fw_load(struct bwi_mac *);
106 static int bwi_mac_fw_init(struct bwi_mac *);
107 static int bwi_mac_fw_load_iv(struct bwi_mac *, const struct firmware *);
109 static void bwi_mac_setup_tpctl(struct bwi_mac *);
110 static void bwi_mac_adjust_tpctl(struct bwi_mac *, int, int);
112 static void bwi_mac_lock(struct bwi_mac *);
113 static void bwi_mac_unlock(struct bwi_mac *);
115 static const uint8_t bwi_sup_macrev[] = { 2, 4, 5, 6, 7, 9, 10 };
118 bwi_tmplt_write_4(struct bwi_mac *mac, uint32_t ofs, uint32_t val)
120 struct bwi_softc *sc = mac->mac_sc;
122 if (mac->mac_flags & BWI_MAC_F_BSWAP)
125 CSR_WRITE_4(sc, BWI_MAC_TMPLT_CTRL, ofs);
126 CSR_WRITE_4(sc, BWI_MAC_TMPLT_DATA, val);
130 bwi_hostflags_write(struct bwi_mac *mac, uint64_t flags)
134 val = flags & 0xffff;
135 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_LO, val);
137 val = (flags >> 16) & 0xffff;
138 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_MI, val);
140 /* HI has unclear meaning, so leave it as it is */
144 bwi_hostflags_read(struct bwi_mac *mac)
148 /* HI has unclear meaning, so don't touch it */
151 val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_MI);
154 val = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_HFLAGS_LO);
161 bwi_memobj_read_2(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0)
163 struct bwi_softc *sc = mac->mac_sc;
167 data_reg = BWI_MOBJ_DATA;
171 data_reg = BWI_MOBJ_DATA_UNALIGN;
173 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
174 return CSR_READ_2(sc, data_reg);
178 bwi_memobj_read_4(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0)
180 struct bwi_softc *sc = mac->mac_sc;
187 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
188 ret = CSR_READ_2(sc, BWI_MOBJ_DATA_UNALIGN);
191 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
192 BWI_MOBJ_CTRL_VAL(obj_id, ofs + 1));
193 ret |= CSR_READ_2(sc, BWI_MOBJ_DATA);
197 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
198 return CSR_READ_4(sc, BWI_MOBJ_DATA);
203 bwi_memobj_write_2(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0,
206 struct bwi_softc *sc = mac->mac_sc;
210 data_reg = BWI_MOBJ_DATA;
214 data_reg = BWI_MOBJ_DATA_UNALIGN;
216 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
217 CSR_WRITE_2(sc, data_reg, v);
221 bwi_memobj_write_4(struct bwi_mac *mac, uint16_t obj_id, uint16_t ofs0,
224 struct bwi_softc *sc = mac->mac_sc;
229 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
230 CSR_WRITE_2(sc, BWI_MOBJ_DATA_UNALIGN, v >> 16);
232 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
233 BWI_MOBJ_CTRL_VAL(obj_id, ofs + 1));
234 CSR_WRITE_2(sc, BWI_MOBJ_DATA, v & 0xffff);
236 CSR_WRITE_4(sc, BWI_MOBJ_CTRL, BWI_MOBJ_CTRL_VAL(obj_id, ofs));
237 CSR_WRITE_4(sc, BWI_MOBJ_DATA, v);
242 bwi_mac_lateattach(struct bwi_mac *mac)
246 if (mac->mac_rev >= 5)
247 CSR_READ_4(mac->mac_sc, BWI_STATE_HI); /* dummy read */
249 bwi_mac_reset(mac, 1);
251 error = bwi_phy_attach(mac);
255 error = bwi_rf_attach(mac);
259 /* Link 11B/G PHY, unlink 11A PHY */
260 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11A)
261 bwi_mac_reset(mac, 0);
263 bwi_mac_reset(mac, 1);
265 error = bwi_mac_test(mac);
269 error = bwi_mac_get_property(mac);
273 error = bwi_rf_map_txpower(mac);
278 CSR_WRITE_2(mac->mac_sc, BWI_BBP_ATTEN, BWI_BBP_ATTEN_MAGIC);
279 bwi_regwin_disable(mac->mac_sc, &mac->mac_regwin, 0);
285 bwi_mac_init(struct bwi_mac *mac)
287 struct bwi_softc *sc = mac->mac_sc;
290 /* Clear MAC/PHY/RF states */
291 bwi_mac_setup_tpctl(mac);
292 bwi_rf_clear_state(&mac->mac_rf);
293 bwi_phy_clear_state(&mac->mac_phy);
295 /* Enable MAC and linked it to PHY */
296 if (!bwi_regwin_is_enabled(sc, &mac->mac_regwin))
297 bwi_mac_reset(mac, 1);
299 /* Initialize backplane */
300 error = bwi_bus_init(sc, mac);
304 /* do timeout fixup */
305 if (sc->sc_bus_regwin.rw_rev <= 5 &&
306 sc->sc_bus_regwin.rw_type != BWI_REGWIN_T_BUSPCIE) {
307 CSR_SETBITS_4(sc, BWI_CONF_LO,
308 __SHIFTIN(BWI_CONF_LO_SERVTO, BWI_CONF_LO_SERVTO_MASK) |
309 __SHIFTIN(BWI_CONF_LO_REQTO, BWI_CONF_LO_REQTO_MASK));
313 error = bwi_phy_calibrate(mac);
315 device_printf(sc->sc_dev, "PHY calibrate failed\n");
319 /* Prepare to initialize firmware */
320 CSR_WRITE_4(sc, BWI_MAC_STATUS,
321 BWI_MAC_STATUS_UCODE_JUMP0 |
322 BWI_MAC_STATUS_IHREN);
325 * Load and initialize firmwares
327 error = bwi_mac_fw_load(mac);
331 error = bwi_mac_gpio_init(mac);
335 error = bwi_mac_fw_init(mac);
344 /* TODO: LED, hardware rf enabled is only related to LED setting */
349 CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0);
352 /* TODO: interference mitigation */
357 bwi_rf_set_ant_mode(mac, mac->mac_rf.rf_ant_mode);
360 * Initialize operation mode (RX configuration)
362 bwi_mac_opmode_init(mac);
364 /* set up Beacon interval */
365 if (mac->mac_rev < 3) {
366 CSR_WRITE_2(sc, 0x60e, 0);
367 CSR_WRITE_2(sc, 0x610, 0x8000);
368 CSR_WRITE_2(sc, 0x604, 0);
369 CSR_WRITE_2(sc, 0x606, 0x200);
371 CSR_WRITE_4(sc, 0x188, 0x80000000);
372 CSR_WRITE_4(sc, 0x18c, 0x2000000);
376 * Initialize TX/RX interrupts' mask
378 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_INTR_TIMER1);
379 for (i = 0; i < BWI_TXRX_NRING; ++i) {
382 if (BWI_TXRX_IS_RX(i))
383 intrs = BWI_TXRX_RX_INTRS;
385 intrs = BWI_TXRX_TX_INTRS;
386 CSR_WRITE_4(sc, BWI_TXRX_INTR_MASK(i), intrs);
389 /* allow the MAC to control the PHY clock (dynamic on/off) */
390 CSR_SETBITS_4(sc, BWI_STATE_LO, 0x100000);
392 /* Setup MAC power up delay */
393 CSR_WRITE_2(sc, BWI_MAC_POWERUP_DELAY, sc->sc_pwron_delay);
395 /* Set MAC regwin revision */
396 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_MACREV, mac->mac_rev);
399 * Initialize host flags
401 bwi_mac_hostflags_init(mac);
404 * Initialize BSS parameters
406 bwi_mac_bss_param_init(mac);
409 * Initialize TX rings
411 for (i = 0; i < BWI_TX_NRING; ++i) {
412 error = sc->sc_init_tx_ring(sc, i);
414 device_printf(sc->sc_dev,
415 "can't initialize %dth TX ring\n", i);
423 error = sc->sc_init_rx_ring(sc);
425 device_printf(sc->sc_dev, "can't initialize RX ring\n");
430 * Initialize TX stats if the current MAC uses that
432 if (mac->mac_flags & BWI_MAC_F_HAS_TXSTATS) {
433 error = sc->sc_init_txstats(sc);
435 device_printf(sc->sc_dev,
436 "can't initialize TX stats ring\n");
442 CSR_WRITE_2(sc, 0x612, 0x50); /* Force Pre-TBTT to 80? */
443 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, 0x416, 0x50);
444 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, 0x414, 0x1f4);
446 mac->mac_flags |= BWI_MAC_F_INITED;
451 bwi_mac_reset(struct bwi_mac *mac, int link_phy)
453 struct bwi_softc *sc = mac->mac_sc;
454 uint32_t flags, state_lo, status;
456 flags = BWI_STATE_LO_FLAG_PHYRST | BWI_STATE_LO_FLAG_PHYCLKEN;
458 flags |= BWI_STATE_LO_FLAG_PHYLNK;
459 bwi_regwin_enable(sc, &mac->mac_regwin, flags);
462 state_lo = CSR_READ_4(sc, BWI_STATE_LO);
463 state_lo |= BWI_STATE_LO_GATED_CLOCK;
464 state_lo &= ~__SHIFTIN(BWI_STATE_LO_FLAG_PHYRST,
465 BWI_STATE_LO_FLAGS_MASK);
466 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
467 /* Flush pending bus write */
468 CSR_READ_4(sc, BWI_STATE_LO);
471 state_lo &= ~BWI_STATE_LO_GATED_CLOCK;
472 CSR_WRITE_4(sc, BWI_STATE_LO, state_lo);
473 /* Flush pending bus write */
474 CSR_READ_4(sc, BWI_STATE_LO);
477 CSR_WRITE_2(sc, BWI_BBP_ATTEN, 0);
479 status = CSR_READ_4(sc, BWI_MAC_STATUS);
480 status |= BWI_MAC_STATUS_IHREN;
482 status |= BWI_MAC_STATUS_PHYLNK;
484 status &= ~BWI_MAC_STATUS_PHYLNK;
485 CSR_WRITE_4(sc, BWI_MAC_STATUS, status);
488 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH | BWI_DBG_INIT,
489 "%s\n", "PHY is linked");
490 mac->mac_phy.phy_flags |= BWI_PHY_F_LINKED;
492 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH | BWI_DBG_INIT,
493 "%s\n", "PHY is unlinked");
494 mac->mac_phy.phy_flags &= ~BWI_PHY_F_LINKED;
499 bwi_mac_set_tpctl_11bg(struct bwi_mac *mac, const struct bwi_tpctl *new_tpctl)
501 struct bwi_rf *rf = &mac->mac_rf;
502 struct bwi_tpctl *tpctl = &mac->mac_tpctl;
504 if (new_tpctl != NULL) {
505 KASSERT(new_tpctl->bbp_atten <= BWI_BBP_ATTEN_MAX,
506 ("bbp_atten %d", new_tpctl->bbp_atten));
507 KASSERT(new_tpctl->rf_atten <=
508 (rf->rf_rev < 6 ? BWI_RF_ATTEN_MAX0
509 : BWI_RF_ATTEN_MAX1),
510 ("rf_atten %d", new_tpctl->rf_atten));
511 KASSERT(new_tpctl->tp_ctrl1 <= BWI_TPCTL1_MAX,
512 ("tp_ctrl1 %d", new_tpctl->tp_ctrl1));
514 tpctl->bbp_atten = new_tpctl->bbp_atten;
515 tpctl->rf_atten = new_tpctl->rf_atten;
516 tpctl->tp_ctrl1 = new_tpctl->tp_ctrl1;
519 /* Set BBP attenuation */
520 bwi_phy_set_bbp_atten(mac, tpctl->bbp_atten);
522 /* Set RF attenuation */
523 RF_WRITE(mac, BWI_RFR_ATTEN, tpctl->rf_atten);
524 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_RF_ATTEN,
528 if (rf->rf_type == BWI_RF_T_BCM2050) {
529 RF_FILT_SETBITS(mac, BWI_RFR_TXPWR, ~BWI_RFR_TXPWR1_MASK,
530 __SHIFTIN(tpctl->tp_ctrl1, BWI_RFR_TXPWR1_MASK));
533 /* Adjust RF Local Oscillator */
534 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11G)
535 bwi_rf_lo_adjust(mac, tpctl);
539 bwi_mac_test(struct bwi_mac *mac)
541 struct bwi_softc *sc = mac->mac_sc;
542 uint32_t orig_val, val;
544 #define TEST_VAL1 0xaa5555aa
545 #define TEST_VAL2 0x55aaaa55
547 /* Save it for later restoring */
548 orig_val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
551 MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, TEST_VAL1);
552 val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
553 if (val != TEST_VAL1) {
554 device_printf(sc->sc_dev, "TEST1 failed\n");
559 MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, TEST_VAL2);
560 val = MOBJ_READ_4(mac, BWI_COMM_MOBJ, 0);
561 if (val != TEST_VAL2) {
562 device_printf(sc->sc_dev, "TEST2 failed\n");
566 /* Restore to the original value */
567 MOBJ_WRITE_4(mac, BWI_COMM_MOBJ, 0, orig_val);
569 val = CSR_READ_4(sc, BWI_MAC_STATUS);
570 if ((val & ~BWI_MAC_STATUS_PHYLNK) != BWI_MAC_STATUS_IHREN) {
571 device_printf(sc->sc_dev, "%s failed, MAC status 0x%08x\n",
576 val = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
578 device_printf(sc->sc_dev, "%s failed, intr status %08x\n",
590 bwi_mac_setup_tpctl(struct bwi_mac *mac)
592 struct bwi_softc *sc = mac->mac_sc;
593 struct bwi_rf *rf = &mac->mac_rf;
594 struct bwi_phy *phy = &mac->mac_phy;
595 struct bwi_tpctl *tpctl = &mac->mac_tpctl;
597 /* Calc BBP attenuation */
598 if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev < 6)
599 tpctl->bbp_atten = 0;
601 tpctl->bbp_atten = 2;
603 /* Calc TX power CTRL1?? */
605 if (rf->rf_type == BWI_RF_T_BCM2050) {
608 else if (rf->rf_rev < 6)
610 else if (rf->rf_rev == 8)
614 /* Empty TX power CTRL2?? */
615 tpctl->tp_ctrl2 = 0xffff;
618 * Calc RF attenuation
620 if (phy->phy_mode == IEEE80211_MODE_11A) {
621 tpctl->rf_atten = 0x60;
625 if (BWI_IS_BRCM_BCM4309G(sc) && sc->sc_pci_revid < 0x51) {
626 tpctl->rf_atten = sc->sc_pci_revid < 0x43 ? 2 : 3;
632 if (rf->rf_type != BWI_RF_T_BCM2050) {
633 if (rf->rf_type == BWI_RF_T_BCM2053 && rf->rf_rev == 1)
639 * NB: If we reaches here and the card is BRCM_BCM4309G,
640 * then the card's PCI revision must >= 0x51
644 switch (rf->rf_rev) {
646 if (phy->phy_mode == IEEE80211_MODE_11G) {
647 if (BWI_IS_BRCM_BCM4309G(sc) || BWI_IS_BRCM_BU4306(sc))
652 if (BWI_IS_BRCM_BCM4309G(sc))
659 if (phy->phy_mode == IEEE80211_MODE_11G) {
661 * NOTE: Order of following conditions is critical
663 if (BWI_IS_BRCM_BCM4309G(sc))
665 else if (BWI_IS_BRCM_BU4306(sc))
667 else if (sc->sc_bbp_id == BWI_BBPID_BCM4320)
680 tpctl->rf_atten = 0x1a;
684 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_TXPOWER,
685 "bbp atten: %u, rf atten: %u, ctrl1: %u, ctrl2: %u\n",
686 tpctl->bbp_atten, tpctl->rf_atten,
687 tpctl->tp_ctrl1, tpctl->tp_ctrl2);
691 bwi_mac_dummy_xmit(struct bwi_mac *mac)
694 static const uint32_t packet_11a[PACKET_LEN] =
695 { 0x000201cc, 0x00d40000, 0x00000000, 0x01000000, 0x00000000 };
696 static const uint32_t packet_11bg[PACKET_LEN] =
697 { 0x000b846e, 0x00d40000, 0x00000000, 0x01000000, 0x00000000 };
699 struct bwi_softc *sc = mac->mac_sc;
700 struct bwi_rf *rf = &mac->mac_rf;
701 const uint32_t *packet;
705 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11A) {
711 packet = packet_11bg;
715 for (i = 0; i < PACKET_LEN; ++i)
716 TMPLT_WRITE_4(mac, i * 4, packet[i]);
718 CSR_READ_4(sc, BWI_MAC_STATUS); /* dummy read */
720 CSR_WRITE_2(sc, 0x568, 0);
721 CSR_WRITE_2(sc, 0x7c0, 0);
722 CSR_WRITE_2(sc, 0x50c, val_50c);
723 CSR_WRITE_2(sc, 0x508, 0);
724 CSR_WRITE_2(sc, 0x50a, 0);
725 CSR_WRITE_2(sc, 0x54c, 0);
726 CSR_WRITE_2(sc, 0x56a, 0x14);
727 CSR_WRITE_2(sc, 0x568, 0x826);
728 CSR_WRITE_2(sc, 0x500, 0);
729 CSR_WRITE_2(sc, 0x502, 0x30);
731 if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev <= 5)
732 RF_WRITE(mac, 0x51, 0x17);
734 for (i = 0; i < wait_max; ++i) {
735 if (CSR_READ_2(sc, 0x50e) & 0x80)
739 for (i = 0; i < 10; ++i) {
740 if (CSR_READ_2(sc, 0x50e) & 0x400)
744 for (i = 0; i < 10; ++i) {
745 if ((CSR_READ_2(sc, 0x690) & 0x100) == 0)
750 if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev <= 5)
751 RF_WRITE(mac, 0x51, 0x37);
756 bwi_mac_init_tpctl_11bg(struct bwi_mac *mac)
758 struct bwi_softc *sc = mac->mac_sc;
759 struct bwi_phy *phy = &mac->mac_phy;
760 struct bwi_rf *rf = &mac->mac_rf;
761 struct bwi_tpctl tpctl_orig;
762 int restore_tpctl = 0;
764 KASSERT(phy->phy_mode != IEEE80211_MODE_11A,
765 ("phy_mode %d", phy->phy_mode));
767 if (BWI_IS_BRCM_BU4306(sc))
770 PHY_WRITE(mac, 0x28, 0x8018);
771 CSR_CLRBITS_2(sc, BWI_BBP_ATTEN, 0x20);
773 if (phy->phy_mode == IEEE80211_MODE_11G) {
774 if ((phy->phy_flags & BWI_PHY_F_LINKED) == 0)
776 PHY_WRITE(mac, 0x47a, 0xc111);
778 if (mac->mac_flags & BWI_MAC_F_TPCTL_INITED)
781 if (phy->phy_mode == IEEE80211_MODE_11B && phy->phy_rev >= 2 &&
782 rf->rf_type == BWI_RF_T_BCM2050) {
783 RF_SETBITS(mac, 0x76, 0x84);
785 struct bwi_tpctl tpctl;
787 /* Backup original TX power control variables */
788 bcopy(&mac->mac_tpctl, &tpctl_orig, sizeof(tpctl_orig));
791 bcopy(&mac->mac_tpctl, &tpctl, sizeof(tpctl));
792 tpctl.bbp_atten = 11;
795 if (rf->rf_rev >= 6 && rf->rf_rev <= 8)
801 bwi_mac_set_tpctl_11bg(mac, &tpctl);
804 bwi_mac_dummy_xmit(mac);
806 mac->mac_flags |= BWI_MAC_F_TPCTL_INITED;
807 rf->rf_base_tssi = PHY_READ(mac, 0x29);
808 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_TXPOWER,
809 "base tssi %d\n", rf->rf_base_tssi);
811 if (abs(rf->rf_base_tssi - rf->rf_idle_tssi) >= 20) {
812 device_printf(sc->sc_dev, "base tssi measure failed\n");
813 mac->mac_flags |= BWI_MAC_F_TPCTL_ERROR;
817 bwi_mac_set_tpctl_11bg(mac, &tpctl_orig);
819 RF_CLRBITS(mac, 0x76, 0x84);
821 bwi_rf_clear_tssi(mac);
825 bwi_mac_detach(struct bwi_mac *mac)
827 bwi_mac_fw_free(mac);
831 bwi_fwimage_is_valid(struct bwi_softc *sc, const struct firmware *fw,
834 const struct bwi_fwhdr *hdr;
836 if (fw->datasize < sizeof(*hdr)) {
837 device_printf(sc->sc_dev,
838 "invalid firmware (%s): invalid size %zu\n",
839 fw->name, fw->datasize);
843 hdr = (const struct bwi_fwhdr *)fw->data;
845 if (fw_type != BWI_FW_T_IV) {
847 * Don't verify IV's size, it has different meaning
849 if (be32toh(hdr->fw_size) != fw->datasize - sizeof(*hdr)) {
850 device_printf(sc->sc_dev,
851 "invalid firmware (%s): size mismatch, "
852 "fw %u, real %zu\n", fw->name,
853 be32toh(hdr->fw_size), fw->datasize - sizeof(*hdr));
858 if (hdr->fw_type != fw_type) {
859 device_printf(sc->sc_dev,
860 "invalid firmware (%s): type mismatch, "
861 "fw \'%c\', target \'%c\'\n", fw->name,
862 hdr->fw_type, fw_type);
866 if (hdr->fw_gen != BWI_FW_GEN_1) {
867 device_printf(sc->sc_dev,
868 "invalid firmware (%s): wrong generation, "
869 "fw %d, target %d\n", fw->name, hdr->fw_gen, BWI_FW_GEN_1);
879 bwi_mac_fw_alloc(struct bwi_mac *mac)
881 struct bwi_softc *sc = mac->mac_sc;
886 * Try getting the firmware stub so firmware
887 * module would be loaded automatically
889 if (mac->mac_stub == NULL) {
890 snprintf(fwname, sizeof(fwname), BWI_FW_STUB_PATH,
892 mac->mac_stub = firmware_get(fwname);
893 if (mac->mac_stub == NULL)
897 if (mac->mac_ucode == NULL) {
898 snprintf(fwname, sizeof(fwname), BWI_FW_UCODE_PATH,
900 mac->mac_rev >= 5 ? 5 : mac->mac_rev);
902 mac->mac_ucode = firmware_get(fwname);
903 if (mac->mac_ucode == NULL)
905 if (!bwi_fwimage_is_valid(sc, mac->mac_ucode, BWI_FW_T_UCODE))
909 if (mac->mac_pcm == NULL) {
910 snprintf(fwname, sizeof(fwname), BWI_FW_PCM_PATH,
912 mac->mac_rev < 5 ? 4 : 5);
914 mac->mac_pcm = firmware_get(fwname);
915 if (mac->mac_pcm == NULL)
917 if (!bwi_fwimage_is_valid(sc, mac->mac_pcm, BWI_FW_T_PCM))
921 if (mac->mac_iv == NULL) {
923 if (mac->mac_rev == 2 || mac->mac_rev == 4) {
925 } else if (mac->mac_rev >= 5 && mac->mac_rev <= 10) {
928 device_printf(sc->sc_dev,
929 "no suitible IV for MAC rev %d\n", mac->mac_rev);
933 snprintf(fwname, sizeof(fwname), BWI_FW_IV_PATH,
934 sc->sc_fw_version, idx);
936 mac->mac_iv = firmware_get(fwname);
937 if (mac->mac_iv == NULL)
939 if (!bwi_fwimage_is_valid(sc, mac->mac_iv, BWI_FW_T_IV))
943 if (mac->mac_iv_ext == NULL) {
945 if (mac->mac_rev == 2 || mac->mac_rev == 4 ||
946 mac->mac_rev >= 11) {
949 } else if (mac->mac_rev >= 5 && mac->mac_rev <= 10) {
952 device_printf(sc->sc_dev,
953 "no suitible ExtIV for MAC rev %d\n", mac->mac_rev);
957 snprintf(fwname, sizeof(fwname), BWI_FW_IV_EXT_PATH,
958 sc->sc_fw_version, idx);
960 mac->mac_iv_ext = firmware_get(fwname);
961 if (mac->mac_iv_ext == NULL)
963 if (!bwi_fwimage_is_valid(sc, mac->mac_iv_ext, BWI_FW_T_IV))
969 device_printf(sc->sc_dev, "request firmware %s failed\n", fwname);
974 bwi_mac_fw_free(struct bwi_mac *mac)
976 if (mac->mac_ucode != NULL) {
977 firmware_put(mac->mac_ucode, FIRMWARE_UNLOAD);
978 mac->mac_ucode = NULL;
981 if (mac->mac_pcm != NULL) {
982 firmware_put(mac->mac_pcm, FIRMWARE_UNLOAD);
986 if (mac->mac_iv != NULL) {
987 firmware_put(mac->mac_iv, FIRMWARE_UNLOAD);
991 if (mac->mac_iv_ext != NULL) {
992 firmware_put(mac->mac_iv_ext, FIRMWARE_UNLOAD);
993 mac->mac_iv_ext = NULL;
996 if (mac->mac_stub != NULL) {
997 firmware_put(mac->mac_stub, FIRMWARE_UNLOAD);
998 mac->mac_stub = NULL;
1003 bwi_mac_fw_load(struct bwi_mac *mac)
1005 struct bwi_softc *sc = mac->mac_sc;
1013 fw = (const uint32_t *)
1014 ((const uint8_t *)mac->mac_ucode->data + BWI_FWHDR_SZ);
1015 fw_len = (mac->mac_ucode->datasize - BWI_FWHDR_SZ) / sizeof(uint32_t);
1017 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1019 BWI_FW_UCODE_MOBJ | BWI_WR_MOBJ_AUTOINC, 0));
1020 for (i = 0; i < fw_len; ++i) {
1021 CSR_WRITE_4(sc, BWI_MOBJ_DATA, be32toh(fw[i]));
1028 fw = (const uint32_t *)
1029 ((const uint8_t *)mac->mac_pcm->data + BWI_FWHDR_SZ);
1030 fw_len = (mac->mac_pcm->datasize - BWI_FWHDR_SZ) / sizeof(uint32_t);
1032 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1033 BWI_MOBJ_CTRL_VAL(BWI_FW_PCM_MOBJ, 0x01ea));
1034 CSR_WRITE_4(sc, BWI_MOBJ_DATA, 0x4000);
1036 CSR_WRITE_4(sc, BWI_MOBJ_CTRL,
1037 BWI_MOBJ_CTRL_VAL(BWI_FW_PCM_MOBJ, 0x01eb));
1038 for (i = 0; i < fw_len; ++i) {
1039 CSR_WRITE_4(sc, BWI_MOBJ_DATA, be32toh(fw[i]));
1043 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_ALL_INTRS);
1044 CSR_WRITE_4(sc, BWI_MAC_STATUS,
1045 BWI_MAC_STATUS_UCODE_START |
1046 BWI_MAC_STATUS_IHREN |
1047 BWI_MAC_STATUS_INFRA);
1051 for (i = 0; i < NRETRY; ++i) {
1052 uint32_t intr_status;
1054 intr_status = CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1055 if (intr_status == BWI_INTR_READY)
1060 device_printf(sc->sc_dev,
1061 "firmware (ucode&pcm) loading timed out\n");
1067 CSR_READ_4(sc, BWI_MAC_INTR_STATUS); /* dummy read */
1069 fw_rev = MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_FWREV);
1070 if (fw_rev > BWI_FW_VERSION3_REVMAX) {
1071 device_printf(sc->sc_dev,
1072 "firmware version 4 is not supported yet\n");
1076 device_printf(sc->sc_dev,
1077 "firmware rev 0x%04x, patch level 0x%04x\n", fw_rev,
1078 MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_FWPATCHLV));
1083 bwi_mac_gpio_init(struct bwi_mac *mac)
1085 struct bwi_softc *sc = mac->mac_sc;
1086 struct bwi_regwin *old, *gpio_rw;
1087 uint32_t filt, bits;
1090 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_GPOSEL_MASK);
1093 CSR_SETBITS_2(sc, BWI_MAC_GPIO_MASK, 0xf);
1097 if (sc->sc_bbp_id == BWI_BBPID_BCM4301) {
1101 if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9) {
1102 CSR_SETBITS_2(sc, BWI_MAC_GPIO_MASK, 0x200);
1107 gpio_rw = BWI_GPIO_REGWIN(sc);
1108 error = bwi_regwin_switch(sc, gpio_rw, &old);
1112 CSR_FILT_SETBITS_4(sc, BWI_GPIO_CTRL, filt, bits);
1114 return bwi_regwin_switch(sc, old, NULL);
1118 bwi_mac_gpio_fini(struct bwi_mac *mac)
1120 struct bwi_softc *sc = mac->mac_sc;
1121 struct bwi_regwin *old, *gpio_rw;
1124 gpio_rw = BWI_GPIO_REGWIN(sc);
1125 error = bwi_regwin_switch(sc, gpio_rw, &old);
1129 CSR_WRITE_4(sc, BWI_GPIO_CTRL, 0);
1131 return bwi_regwin_switch(sc, old, NULL);
1135 bwi_mac_fw_load_iv(struct bwi_mac *mac, const struct firmware *fw)
1137 struct bwi_softc *sc = mac->mac_sc;
1138 const struct bwi_fwhdr *hdr;
1139 const struct bwi_fw_iv *iv;
1140 int n, i, iv_img_size;
1142 /* Get the number of IVs in the IV image */
1143 hdr = (const struct bwi_fwhdr *)fw->data;
1144 n = be32toh(hdr->fw_iv_cnt);
1145 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_INIT | BWI_DBG_FIRMWARE,
1146 "IV count %d\n", n);
1148 /* Calculate the IV image size, for later sanity check */
1149 iv_img_size = fw->datasize - sizeof(*hdr);
1151 /* Locate the first IV */
1152 iv = (const struct bwi_fw_iv *)
1153 ((const uint8_t *)fw->data + sizeof(*hdr));
1155 for (i = 0; i < n; ++i) {
1156 uint16_t iv_ofs, ofs;
1159 if (iv_img_size < sizeof(iv->iv_ofs)) {
1160 device_printf(sc->sc_dev, "invalid IV image, ofs\n");
1163 iv_img_size -= sizeof(iv->iv_ofs);
1164 sz += sizeof(iv->iv_ofs);
1166 iv_ofs = be16toh(iv->iv_ofs);
1168 ofs = __SHIFTOUT(iv_ofs, BWI_FW_IV_OFS_MASK);
1169 if (ofs >= 0x1000) {
1170 device_printf(sc->sc_dev, "invalid ofs (0x%04x) "
1171 "for %dth iv\n", ofs, i);
1175 if (iv_ofs & BWI_FW_IV_IS_32BIT) {
1178 if (iv_img_size < sizeof(iv->iv_val.val32)) {
1179 device_printf(sc->sc_dev,
1180 "invalid IV image, val32\n");
1183 iv_img_size -= sizeof(iv->iv_val.val32);
1184 sz += sizeof(iv->iv_val.val32);
1186 val32 = be32toh(iv->iv_val.val32);
1187 CSR_WRITE_4(sc, ofs, val32);
1191 if (iv_img_size < sizeof(iv->iv_val.val16)) {
1192 device_printf(sc->sc_dev,
1193 "invalid IV image, val16\n");
1196 iv_img_size -= sizeof(iv->iv_val.val16);
1197 sz += sizeof(iv->iv_val.val16);
1199 val16 = be16toh(iv->iv_val.val16);
1200 CSR_WRITE_2(sc, ofs, val16);
1203 iv = (const struct bwi_fw_iv *)((const uint8_t *)iv + sz);
1206 if (iv_img_size != 0) {
1207 device_printf(sc->sc_dev, "invalid IV image, size left %d\n",
1215 bwi_mac_fw_init(struct bwi_mac *mac)
1217 device_t dev = mac->mac_sc->sc_dev;
1220 error = bwi_mac_fw_load_iv(mac, mac->mac_iv);
1222 device_printf(dev, "load IV failed\n");
1226 if (mac->mac_iv_ext != NULL) {
1227 error = bwi_mac_fw_load_iv(mac, mac->mac_iv_ext);
1229 device_printf(dev, "load ExtIV failed\n");
1235 bwi_mac_opmode_init(struct bwi_mac *mac)
1237 struct bwi_softc *sc = mac->mac_sc;
1238 struct ieee80211com *ic = &sc->sc_ic;
1239 uint32_t mac_status;
1242 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_INFRA);
1243 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_INFRA);
1245 /* Set probe resp timeout to infinite */
1246 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_PROBE_RESP_TO, 0);
1249 * TODO: factor out following part
1252 mac_status = CSR_READ_4(sc, BWI_MAC_STATUS);
1253 mac_status &= ~(BWI_MAC_STATUS_OPMODE_HOSTAP |
1254 BWI_MAC_STATUS_PASS_CTL |
1255 BWI_MAC_STATUS_PASS_BCN |
1256 BWI_MAC_STATUS_PASS_BADPLCP |
1257 BWI_MAC_STATUS_PASS_BADFCS |
1258 BWI_MAC_STATUS_PROMISC);
1259 mac_status |= BWI_MAC_STATUS_INFRA;
1261 /* Always turn on PROMISC on old hardware */
1262 if (mac->mac_rev < 5)
1263 mac_status |= BWI_MAC_STATUS_PROMISC;
1265 switch (ic->ic_opmode) {
1266 case IEEE80211_M_IBSS:
1267 mac_status &= ~BWI_MAC_STATUS_INFRA;
1269 case IEEE80211_M_HOSTAP:
1270 mac_status |= BWI_MAC_STATUS_OPMODE_HOSTAP;
1272 case IEEE80211_M_MONITOR:
1274 /* Do you want data from your microwave oven? */
1275 mac_status |= BWI_MAC_STATUS_PASS_CTL |
1276 BWI_MAC_STATUS_PASS_BADPLCP |
1277 BWI_MAC_STATUS_PASS_BADFCS;
1279 mac_status |= BWI_MAC_STATUS_PASS_CTL;
1287 if (ic->ic_promisc > 0)
1288 mac_status |= BWI_MAC_STATUS_PROMISC;
1290 CSR_WRITE_4(sc, BWI_MAC_STATUS, mac_status);
1292 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1293 ic->ic_opmode != IEEE80211_M_HOSTAP) {
1294 if (sc->sc_bbp_id == BWI_BBPID_BCM4306 && sc->sc_bbp_rev == 3)
1301 CSR_WRITE_2(sc, BWI_MAC_PRE_TBTT, pre_tbtt);
1305 bwi_mac_hostflags_init(struct bwi_mac *mac)
1307 struct bwi_softc *sc = mac->mac_sc;
1308 struct bwi_phy *phy = &mac->mac_phy;
1309 struct bwi_rf *rf = &mac->mac_rf;
1310 uint64_t host_flags;
1312 if (phy->phy_mode == IEEE80211_MODE_11A)
1315 host_flags = HFLAGS_READ(mac);
1316 host_flags |= BWI_HFLAG_SYM_WA;
1318 if (phy->phy_mode == IEEE80211_MODE_11G) {
1319 if (phy->phy_rev == 1)
1320 host_flags |= BWI_HFLAG_GDC_WA;
1321 if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9)
1322 host_flags |= BWI_HFLAG_OFDM_PA;
1323 } else if (phy->phy_mode == IEEE80211_MODE_11B) {
1324 if (phy->phy_rev >= 2 && rf->rf_type == BWI_RF_T_BCM2050)
1325 host_flags &= ~BWI_HFLAG_GDC_WA;
1327 panic("unknown PHY mode %u\n", phy->phy_mode);
1330 HFLAGS_WRITE(mac, host_flags);
1334 bwi_mac_bss_param_init(struct bwi_mac *mac)
1336 struct bwi_softc *sc = mac->mac_sc;
1337 struct bwi_phy *phy = &mac->mac_phy;
1338 struct ieee80211com *ic = &sc->sc_ic;
1339 const struct ieee80211_rate_table *rt;
1340 struct bwi_retry_lim lim;
1344 * Set short/long retry limits
1346 bzero(&lim, sizeof(lim));
1347 lim.shretry = BWI_SHRETRY;
1348 lim.shretry_fb = BWI_SHRETRY_FB;
1349 lim.lgretry = BWI_LGRETRY;
1350 lim.lgretry_fb = BWI_LGRETRY_FB;
1351 bwi_mac_set_retry_lim(mac, &lim);
1354 * Implicitly prevent firmware from sending probe response
1355 * by setting its "probe response timeout" to 1us.
1357 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_PROBE_RESP_TO, 1);
1360 * XXX MAC level acknowledge and CW min/max should depend
1361 * on the char rateset of the IBSS/BSS to join.
1362 * XXX this is all wrong; should be done on channel change
1364 if (phy->phy_mode == IEEE80211_MODE_11B) {
1365 rt = ieee80211_get_ratetable(
1366 ieee80211_find_channel(ic, 2412, IEEE80211_CHAN_B));
1367 bwi_mac_set_ackrates(mac, rt,
1368 &ic->ic_sup_rates[IEEE80211_MODE_11B]);
1370 rt = ieee80211_get_ratetable(
1371 ieee80211_find_channel(ic, 2412, IEEE80211_CHAN_G));
1372 bwi_mac_set_ackrates(mac, rt,
1373 &ic->ic_sup_rates[IEEE80211_MODE_11G]);
1379 if (phy->phy_mode == IEEE80211_MODE_11B)
1380 cw_min = IEEE80211_CW_MIN_0;
1382 cw_min = IEEE80211_CW_MIN_1;
1383 MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_CWMIN, cw_min);
1388 MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_CWMAX,
1393 bwi_mac_set_retry_lim(struct bwi_mac *mac, const struct bwi_retry_lim *lim)
1395 /* Short/Long retry limit */
1396 MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_SHRETRY,
1398 MOBJ_WRITE_2(mac, BWI_80211_MOBJ, BWI_80211_MOBJ_LGRETRY,
1401 /* Short/Long retry fallback limit */
1402 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_SHRETRY_FB,
1404 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_LGRETEY_FB,
1409 bwi_mac_set_ackrates(struct bwi_mac *mac, const struct ieee80211_rate_table *rt,
1410 const struct ieee80211_rateset *rs)
1414 /* XXX not standard conforming */
1415 for (i = 0; i < rs->rs_nrates; ++i) {
1416 enum ieee80211_phytype modtype;
1419 modtype = ieee80211_rate2phytype(rt,
1420 rs->rs_rates[i] & IEEE80211_RATE_VAL);
1422 case IEEE80211_T_DS:
1425 case IEEE80211_T_OFDM:
1429 panic("unsupported modtype %u\n", modtype);
1431 ofs += 2*(ieee80211_rate2plcp(
1432 rs->rs_rates[i] & IEEE80211_RATE_VAL,
1435 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, ofs + 0x20,
1436 MOBJ_READ_2(mac, BWI_COMM_MOBJ, ofs));
1441 bwi_mac_start(struct bwi_mac *mac)
1443 struct bwi_softc *sc = mac->mac_sc;
1445 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_ENABLE);
1446 CSR_WRITE_4(sc, BWI_MAC_INTR_STATUS, BWI_INTR_READY);
1448 /* Flush pending bus writes */
1449 CSR_READ_4(sc, BWI_MAC_STATUS);
1450 CSR_READ_4(sc, BWI_MAC_INTR_STATUS);
1452 return bwi_mac_config_ps(mac);
1456 bwi_mac_stop(struct bwi_mac *mac)
1458 struct bwi_softc *sc = mac->mac_sc;
1461 error = bwi_mac_config_ps(mac);
1465 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_ENABLE);
1467 /* Flush pending bus write */
1468 CSR_READ_4(sc, BWI_MAC_STATUS);
1470 #define NRETRY 10000
1471 for (i = 0; i < NRETRY; ++i) {
1472 if (CSR_READ_4(sc, BWI_MAC_INTR_STATUS) & BWI_INTR_READY)
1477 device_printf(sc->sc_dev, "can't stop MAC\n");
1486 bwi_mac_config_ps(struct bwi_mac *mac)
1488 struct bwi_softc *sc = mac->mac_sc;
1491 status = CSR_READ_4(sc, BWI_MAC_STATUS);
1493 status &= ~BWI_MAC_STATUS_HW_PS;
1494 status |= BWI_MAC_STATUS_WAKEUP;
1495 CSR_WRITE_4(sc, BWI_MAC_STATUS, status);
1497 /* Flush pending bus write */
1498 CSR_READ_4(sc, BWI_MAC_STATUS);
1500 if (mac->mac_rev >= 5) {
1504 for (i = 0; i < NRETRY; ++i) {
1505 if (MOBJ_READ_2(mac, BWI_COMM_MOBJ,
1506 BWI_COMM_MOBJ_UCODE_STATE) != BWI_UCODE_STATE_PS)
1511 device_printf(sc->sc_dev, "config PS failed\n");
1520 bwi_mac_reset_hwkeys(struct bwi_mac *mac)
1522 /* TODO: firmware crypto */
1523 MOBJ_READ_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_KEYTABLE_OFS);
1527 bwi_mac_shutdown(struct bwi_mac *mac)
1529 struct bwi_softc *sc = mac->mac_sc;
1532 if (mac->mac_flags & BWI_MAC_F_HAS_TXSTATS)
1533 sc->sc_free_txstats(sc);
1535 sc->sc_free_rx_ring(sc);
1537 for (i = 0; i < BWI_TX_NRING; ++i)
1538 sc->sc_free_tx_ring(sc, i);
1544 bwi_mac_gpio_fini(mac);
1546 bwi_rf_off(mac); /* XXX again */
1547 CSR_WRITE_2(sc, BWI_BBP_ATTEN, BWI_BBP_ATTEN_MAGIC);
1548 bwi_regwin_disable(sc, &mac->mac_regwin, 0);
1550 mac->mac_flags &= ~BWI_MAC_F_INITED;
1554 bwi_mac_get_property(struct bwi_mac *mac)
1556 struct bwi_softc *sc = mac->mac_sc;
1557 enum bwi_bus_space old_bus_space;
1563 val = CSR_READ_4(sc, BWI_MAC_STATUS);
1564 if (val & BWI_MAC_STATUS_BSWAP) {
1565 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1567 mac->mac_flags |= BWI_MAC_F_BSWAP;
1573 old_bus_space = sc->sc_bus_space;
1575 val = CSR_READ_4(sc, BWI_STATE_HI);
1576 if (__SHIFTOUT(val, BWI_STATE_HI_FLAGS_MASK) &
1577 BWI_STATE_HI_FLAG_64BIT) {
1579 sc->sc_bus_space = BWI_BUS_SPACE_64BIT;
1580 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1583 uint32_t txrx_reg = BWI_TXRX_CTRL_BASE + BWI_TX32_CTRL;
1585 CSR_WRITE_4(sc, txrx_reg, BWI_TXRX32_CTRL_ADDRHI_MASK);
1586 if (CSR_READ_4(sc, txrx_reg) & BWI_TXRX32_CTRL_ADDRHI_MASK) {
1588 sc->sc_bus_space = BWI_BUS_SPACE_32BIT;
1589 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1593 sc->sc_bus_space = BWI_BUS_SPACE_30BIT;
1594 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1599 if (old_bus_space != 0 && old_bus_space != sc->sc_bus_space) {
1600 device_printf(sc->sc_dev, "MACs bus space mismatch!\n");
1607 bwi_mac_updateslot(struct bwi_mac *mac, int shslot)
1611 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11B)
1615 slot_time = IEEE80211_DUR_SHSLOT;
1617 slot_time = IEEE80211_DUR_SLOT;
1619 CSR_WRITE_2(mac->mac_sc, BWI_MAC_SLOTTIME,
1620 slot_time + BWI_MAC_SLOTTIME_ADJUST);
1621 MOBJ_WRITE_2(mac, BWI_COMM_MOBJ, BWI_COMM_MOBJ_SLOTTIME, slot_time);
1625 bwi_mac_attach(struct bwi_softc *sc, int id, uint8_t rev)
1627 struct bwi_mac *mac;
1630 KASSERT(sc->sc_nmac <= BWI_MAC_MAX && sc->sc_nmac >= 0,
1631 ("sc_nmac %d", sc->sc_nmac));
1633 if (sc->sc_nmac == BWI_MAC_MAX) {
1634 device_printf(sc->sc_dev, "too many MACs\n");
1639 * More than one MAC is only supported by BCM4309
1641 if (sc->sc_nmac != 0 &&
1642 sc->sc_pci_did != PCI_PRODUCT_BROADCOM_BCM4309) {
1643 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1644 "ignore second MAC");
1648 mac = &sc->sc_mac[sc->sc_nmac];
1650 /* XXX will this happen? */
1651 if (BWI_REGWIN_EXIST(&mac->mac_regwin)) {
1652 device_printf(sc->sc_dev, "%dth MAC already attached\n",
1658 * Test whether the revision of this MAC is supported
1660 for (i = 0; i < nitems(bwi_sup_macrev); ++i) {
1661 if (bwi_sup_macrev[i] == rev)
1664 if (i == nitems(bwi_sup_macrev)) {
1665 device_printf(sc->sc_dev, "MAC rev %u is "
1666 "not supported\n", rev);
1670 BWI_CREATE_MAC(mac, sc, id, rev);
1673 if (mac->mac_rev < 5) {
1674 mac->mac_flags |= BWI_MAC_F_HAS_TXSTATS;
1675 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_ATTACH, "%s\n",
1678 mac->mac_flags |= BWI_MAC_F_PHYE_RESET;
1681 device_printf(sc->sc_dev, "MAC: rev %u\n", rev);
1685 static __inline void
1686 bwi_mac_balance_atten(int *bbp_atten0, int *rf_atten0)
1688 int bbp_atten, rf_atten, rf_atten_lim = -1;
1690 bbp_atten = *bbp_atten0;
1691 rf_atten = *rf_atten0;
1694 * RF attenuation affects TX power BWI_RF_ATTEN_FACTOR times
1695 * as much as BBP attenuation, so we try our best to keep RF
1696 * attenuation within range. BBP attenuation will be clamped
1697 * later if it is out of range during balancing.
1699 * BWI_RF_ATTEN_MAX0 is used as RF attenuation upper limit.
1703 * Use BBP attenuation to balance RF attenuation
1707 else if (rf_atten > BWI_RF_ATTEN_MAX0)
1708 rf_atten_lim = BWI_RF_ATTEN_MAX0;
1710 if (rf_atten_lim >= 0) {
1711 bbp_atten += (BWI_RF_ATTEN_FACTOR * (rf_atten - rf_atten_lim));
1712 rf_atten = rf_atten_lim;
1716 * If possible, use RF attenuation to balance BBP attenuation
1717 * NOTE: RF attenuation is still kept within range.
1719 while (rf_atten < BWI_RF_ATTEN_MAX0 && bbp_atten > BWI_BBP_ATTEN_MAX) {
1720 bbp_atten -= BWI_RF_ATTEN_FACTOR;
1723 while (rf_atten > 0 && bbp_atten < 0) {
1724 bbp_atten += BWI_RF_ATTEN_FACTOR;
1728 /* RF attenuation MUST be within range */
1729 KASSERT(rf_atten >= 0 && rf_atten <= BWI_RF_ATTEN_MAX0,
1730 ("rf_atten %d", rf_atten));
1733 * Clamp BBP attenuation
1737 else if (bbp_atten > BWI_BBP_ATTEN_MAX)
1738 bbp_atten = BWI_BBP_ATTEN_MAX;
1740 *rf_atten0 = rf_atten;
1741 *bbp_atten0 = bbp_atten;
1745 bwi_mac_adjust_tpctl(struct bwi_mac *mac, int rf_atten_adj, int bbp_atten_adj)
1747 struct bwi_softc *sc = mac->mac_sc;
1748 struct bwi_rf *rf = &mac->mac_rf;
1749 struct bwi_tpctl tpctl;
1750 int bbp_atten, rf_atten, tp_ctrl1;
1752 bcopy(&mac->mac_tpctl, &tpctl, sizeof(tpctl));
1754 /* NOTE: Use signed value to do calulation */
1755 bbp_atten = tpctl.bbp_atten;
1756 rf_atten = tpctl.rf_atten;
1757 tp_ctrl1 = tpctl.tp_ctrl1;
1759 bbp_atten += bbp_atten_adj;
1760 rf_atten += rf_atten_adj;
1762 bwi_mac_balance_atten(&bbp_atten, &rf_atten);
1764 if (rf->rf_type == BWI_RF_T_BCM2050 && rf->rf_rev == 2) {
1765 if (rf_atten <= 1) {
1766 if (tp_ctrl1 == 0) {
1770 } else if (sc->sc_card_flags & BWI_CARD_F_PA_GPIO9) {
1772 (BWI_RF_ATTEN_FACTOR * (rf_atten - 2));
1775 } else if (rf_atten > 4 && tp_ctrl1 != 0) {
1777 if (bbp_atten < 3) {
1785 bwi_mac_balance_atten(&bbp_atten, &rf_atten);
1788 tpctl.bbp_atten = bbp_atten;
1789 tpctl.rf_atten = rf_atten;
1790 tpctl.tp_ctrl1 = tp_ctrl1;
1793 bwi_mac_set_tpctl_11bg(mac, &tpctl);
1794 bwi_mac_unlock(mac);
1798 * http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower
1801 bwi_mac_calibrate_txpower(struct bwi_mac *mac, enum bwi_txpwrcb_type type)
1803 struct bwi_softc *sc = mac->mac_sc;
1804 struct bwi_rf *rf = &mac->mac_rf;
1805 int8_t tssi[4], tssi_avg, cur_txpwr;
1806 int error, i, ofdm_tssi;
1807 int txpwr_diff, rf_atten_adj, bbp_atten_adj;
1809 if (!sc->sc_txpwr_calib)
1812 if (mac->mac_flags & BWI_MAC_F_TPCTL_ERROR) {
1813 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1814 "tpctl error happened, can't set txpower");
1818 if (BWI_IS_BRCM_BU4306(sc)) {
1819 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1820 "BU4306, can't set txpower");
1825 * Save latest TSSI and reset the related memory objects
1828 error = bwi_rf_get_latest_tssi(mac, tssi, BWI_COMM_MOBJ_TSSI_DS);
1830 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1833 if (mac->mac_phy.phy_mode == IEEE80211_MODE_11B) {
1834 if (type == BWI_TXPWR_FORCE) {
1843 error = bwi_rf_get_latest_tssi(mac, tssi,
1844 BWI_COMM_MOBJ_TSSI_OFDM);
1846 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1848 if (type == BWI_TXPWR_FORCE) {
1857 for (i = 0; i < 4; ++i) {
1863 bwi_rf_clear_tssi(mac);
1865 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER,
1866 "tssi0 %d, tssi1 %d, tssi2 %d, tssi3 %d\n",
1867 tssi[0], tssi[1], tssi[2], tssi[3]);
1870 * Calculate RF/BBP attenuation adjustment based on
1871 * the difference between desired TX power and sampled
1874 /* +8 == "each incremented by 1/2" */
1875 tssi_avg = (tssi[0] + tssi[1] + tssi[2] + tssi[3] + 8) / 4;
1876 if (ofdm_tssi && (HFLAGS_READ(mac) & BWI_HFLAG_PWR_BOOST_DS))
1879 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "tssi avg %d\n", tssi_avg);
1881 error = bwi_rf_tssi2dbm(mac, tssi_avg, &cur_txpwr);
1884 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "current txpower %d\n",
1887 txpwr_diff = rf->rf_txpower_max - cur_txpwr; /* XXX ni_txpower */
1889 rf_atten_adj = -howmany(txpwr_diff, 8);
1890 if (type == BWI_TXPWR_INIT) {
1892 * Move toward EEPROM max TX power as fast as we can
1894 bbp_atten_adj = -txpwr_diff;
1896 bbp_atten_adj = -(txpwr_diff / 2);
1898 bbp_atten_adj -= (BWI_RF_ATTEN_FACTOR * rf_atten_adj);
1900 if (rf_atten_adj == 0 && bbp_atten_adj == 0) {
1901 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER, "%s\n",
1902 "no need to adjust RF/BBP attenuation");
1908 DPRINTF(sc, BWI_DBG_MAC | BWI_DBG_TXPOWER,
1909 "rf atten adjust %d, bbp atten adjust %d\n",
1910 rf_atten_adj, bbp_atten_adj);
1911 bwi_mac_adjust_tpctl(mac, rf_atten_adj, bbp_atten_adj);
1916 bwi_mac_lock(struct bwi_mac *mac)
1918 struct bwi_softc *sc = mac->mac_sc;
1919 struct ieee80211com *ic = &sc->sc_ic;
1921 KASSERT((mac->mac_flags & BWI_MAC_F_LOCKED) == 0,
1922 ("mac_flags 0x%x", mac->mac_flags));
1924 if (mac->mac_rev < 3)
1926 else if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1927 bwi_mac_config_ps(mac);
1929 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_RFLOCK);
1931 /* Flush pending bus write */
1932 CSR_READ_4(sc, BWI_MAC_STATUS);
1935 mac->mac_flags |= BWI_MAC_F_LOCKED;
1939 bwi_mac_unlock(struct bwi_mac *mac)
1941 struct bwi_softc *sc = mac->mac_sc;
1942 struct ieee80211com *ic = &sc->sc_ic;
1944 KASSERT(mac->mac_flags & BWI_MAC_F_LOCKED,
1945 ("mac_flags 0x%x", mac->mac_flags));
1947 CSR_READ_2(sc, BWI_PHYINFO); /* dummy read */
1949 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_RFLOCK);
1951 if (mac->mac_rev < 3)
1953 else if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1954 bwi_mac_config_ps(mac);
1956 mac->mac_flags &= ~BWI_MAC_F_LOCKED;
1960 bwi_mac_set_promisc(struct bwi_mac *mac, int promisc)
1962 struct bwi_softc *sc = mac->mac_sc;
1964 if (mac->mac_rev < 5) /* Promisc is always on */
1968 CSR_SETBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PROMISC);
1970 CSR_CLRBITS_4(sc, BWI_MAC_STATUS, BWI_MAC_STATUS_PROMISC);