3 Broadcom B43 wireless driver
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6 Copyright (c) 2005-2007 Stefano Brivio <stefano.brivio@polimi.it>
7 Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
8 Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
24 Boston, MA 02110-1301, USA.
28 #include <linux/delay.h>
30 #include <linux/types.h>
31 #include <linux/bitrev.h>
42 static const s8 b43_tssi2dbm_b_table[] = {
43 0x4D, 0x4C, 0x4B, 0x4A,
44 0x4A, 0x49, 0x48, 0x47,
45 0x47, 0x46, 0x45, 0x45,
46 0x44, 0x43, 0x42, 0x42,
47 0x41, 0x40, 0x3F, 0x3E,
48 0x3D, 0x3C, 0x3B, 0x3A,
49 0x39, 0x38, 0x37, 0x36,
50 0x35, 0x34, 0x32, 0x31,
51 0x30, 0x2F, 0x2D, 0x2C,
52 0x2B, 0x29, 0x28, 0x26,
53 0x25, 0x23, 0x21, 0x1F,
54 0x1D, 0x1A, 0x17, 0x14,
55 0x10, 0x0C, 0x06, 0x00,
61 static const s8 b43_tssi2dbm_g_table[] = {
80 const u8 b43_radio_channel_codes_bg[] = {
87 #define bitrev4(tmp) (bitrev8(tmp) >> 4)
88 static void b43_phy_initg(struct b43_wldev *dev);
90 static void generate_rfatt_list(struct b43_wldev *dev,
91 struct b43_rfatt_list *list)
93 struct b43_phy *phy = &dev->phy;
95 /* APHY.rev < 5 || GPHY.rev < 6 */
96 static const struct b43_rfatt rfatt_0[] = {
97 {.att = 3,.with_padmix = 0,},
98 {.att = 1,.with_padmix = 0,},
99 {.att = 5,.with_padmix = 0,},
100 {.att = 7,.with_padmix = 0,},
101 {.att = 9,.with_padmix = 0,},
102 {.att = 2,.with_padmix = 0,},
103 {.att = 0,.with_padmix = 0,},
104 {.att = 4,.with_padmix = 0,},
105 {.att = 6,.with_padmix = 0,},
106 {.att = 8,.with_padmix = 0,},
107 {.att = 1,.with_padmix = 1,},
108 {.att = 2,.with_padmix = 1,},
109 {.att = 3,.with_padmix = 1,},
110 {.att = 4,.with_padmix = 1,},
112 /* Radio.rev == 8 && Radio.version == 0x2050 */
113 static const struct b43_rfatt rfatt_1[] = {
114 {.att = 2,.with_padmix = 1,},
115 {.att = 4,.with_padmix = 1,},
116 {.att = 6,.with_padmix = 1,},
117 {.att = 8,.with_padmix = 1,},
118 {.att = 10,.with_padmix = 1,},
119 {.att = 12,.with_padmix = 1,},
120 {.att = 14,.with_padmix = 1,},
123 static const struct b43_rfatt rfatt_2[] = {
124 {.att = 0,.with_padmix = 1,},
125 {.att = 2,.with_padmix = 1,},
126 {.att = 4,.with_padmix = 1,},
127 {.att = 6,.with_padmix = 1,},
128 {.att = 8,.with_padmix = 1,},
129 {.att = 9,.with_padmix = 1,},
130 {.att = 9,.with_padmix = 1,},
133 if (!b43_has_hardware_pctl(phy)) {
135 list->list = rfatt_0;
136 list->len = ARRAY_SIZE(rfatt_0);
141 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
143 list->list = rfatt_1;
144 list->len = ARRAY_SIZE(rfatt_1);
150 list->list = rfatt_2;
151 list->len = ARRAY_SIZE(rfatt_2);
156 static void generate_bbatt_list(struct b43_wldev *dev,
157 struct b43_bbatt_list *list)
159 static const struct b43_bbatt bbatt_0[] = {
171 list->list = bbatt_0;
172 list->len = ARRAY_SIZE(bbatt_0);
177 bool b43_has_hardware_pctl(struct b43_phy *phy)
179 if (!phy->hardware_power_control)
196 static void b43_shm_clear_tssi(struct b43_wldev *dev)
198 struct b43_phy *phy = &dev->phy;
202 b43_shm_write16(dev, B43_SHM_SHARED, 0x0068, 0x7F7F);
203 b43_shm_write16(dev, B43_SHM_SHARED, 0x006a, 0x7F7F);
207 b43_shm_write16(dev, B43_SHM_SHARED, 0x0058, 0x7F7F);
208 b43_shm_write16(dev, B43_SHM_SHARED, 0x005a, 0x7F7F);
209 b43_shm_write16(dev, B43_SHM_SHARED, 0x0070, 0x7F7F);
210 b43_shm_write16(dev, B43_SHM_SHARED, 0x0072, 0x7F7F);
215 /* Lock the PHY registers against concurrent access from the microcode.
216 * This lock is nonrecursive. */
217 void b43_phy_lock(struct b43_wldev *dev)
220 B43_WARN_ON(dev->phy.phy_locked);
221 dev->phy.phy_locked = 1;
223 B43_WARN_ON(dev->dev->id.revision < 3);
225 if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
226 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
229 void b43_phy_unlock(struct b43_wldev *dev)
232 B43_WARN_ON(!dev->phy.phy_locked);
233 dev->phy.phy_locked = 0;
235 B43_WARN_ON(dev->dev->id.revision < 3);
237 if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
238 b43_power_saving_ctl_bits(dev, 0);
241 /* Different PHYs require different register routing flags.
242 * This adjusts (and does sanity checks on) the routing flags.
244 static inline u16 adjust_phyreg_for_phytype(struct b43_phy *phy,
245 u16 offset, struct b43_wldev *dev)
247 if (phy->type == B43_PHYTYPE_A) {
248 /* OFDM registers are base-registers for the A-PHY. */
249 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_OFDM_GPHY) {
250 offset &= ~B43_PHYROUTE;
251 offset |= B43_PHYROUTE_BASE;
256 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_EXT_GPHY) {
257 /* Ext-G registers are only available on G-PHYs */
258 if (phy->type != B43_PHYTYPE_G) {
259 b43err(dev->wl, "Invalid EXT-G PHY access at "
260 "0x%04X on PHY type %u\n", offset, phy->type);
264 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_N_BMODE) {
265 /* N-BMODE registers are only available on N-PHYs */
266 if (phy->type != B43_PHYTYPE_N) {
267 b43err(dev->wl, "Invalid N-BMODE PHY access at "
268 "0x%04X on PHY type %u\n", offset, phy->type);
272 #endif /* B43_DEBUG */
277 u16 b43_phy_read(struct b43_wldev * dev, u16 offset)
279 struct b43_phy *phy = &dev->phy;
281 offset = adjust_phyreg_for_phytype(phy, offset, dev);
282 b43_write16(dev, B43_MMIO_PHY_CONTROL, offset);
283 return b43_read16(dev, B43_MMIO_PHY_DATA);
286 void b43_phy_write(struct b43_wldev *dev, u16 offset, u16 val)
288 struct b43_phy *phy = &dev->phy;
290 offset = adjust_phyreg_for_phytype(phy, offset, dev);
291 b43_write16(dev, B43_MMIO_PHY_CONTROL, offset);
292 b43_write16(dev, B43_MMIO_PHY_DATA, val);
295 void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
297 b43_phy_write(dev, offset,
298 b43_phy_read(dev, offset) & mask);
301 void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
303 b43_phy_write(dev, offset,
304 b43_phy_read(dev, offset) | set);
307 void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
309 b43_phy_write(dev, offset,
310 (b43_phy_read(dev, offset) & mask) | set);
313 /* Adjust the transmission power output (G-PHY) */
314 void b43_set_txpower_g(struct b43_wldev *dev,
315 const struct b43_bbatt *bbatt,
316 const struct b43_rfatt *rfatt, u8 tx_control)
318 struct b43_phy *phy = &dev->phy;
319 struct b43_txpower_lo_control *lo = phy->lo_control;
321 u16 tx_bias, tx_magn;
325 tx_bias = lo->tx_bias;
326 tx_magn = lo->tx_magn;
327 if (unlikely(tx_bias == 0xFF))
330 /* Save the values for later */
331 phy->tx_control = tx_control;
332 memcpy(&phy->rfatt, rfatt, sizeof(*rfatt));
333 phy->rfatt.with_padmix = !!(tx_control & B43_TXCTL_TXMIX);
334 memcpy(&phy->bbatt, bbatt, sizeof(*bbatt));
336 if (b43_debug(dev, B43_DBG_XMITPOWER)) {
337 b43dbg(dev->wl, "Tuning TX-power to bbatt(%u), "
338 "rfatt(%u), tx_control(0x%02X), "
339 "tx_bias(0x%02X), tx_magn(0x%02X)\n",
340 bb, rf, tx_control, tx_bias, tx_magn);
343 b43_phy_set_baseband_attenuation(dev, bb);
344 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RFATT, rf);
345 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
346 b43_radio_write16(dev, 0x43,
347 (rf & 0x000F) | (tx_control & 0x0070));
349 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
350 & 0xFFF0) | (rf & 0x000F));
351 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
352 & ~0x0070) | (tx_control &
355 if (has_tx_magnification(phy)) {
356 b43_radio_write16(dev, 0x52, tx_magn | tx_bias);
358 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
359 & 0xFFF0) | (tx_bias & 0x000F));
361 if (phy->type == B43_PHYTYPE_G)
362 b43_lo_g_adjust(dev);
365 static void default_baseband_attenuation(struct b43_wldev *dev,
366 struct b43_bbatt *bb)
368 struct b43_phy *phy = &dev->phy;
370 if (phy->radio_ver == 0x2050 && phy->radio_rev < 6)
376 static void default_radio_attenuation(struct b43_wldev *dev,
377 struct b43_rfatt *rf)
379 struct ssb_bus *bus = dev->dev->bus;
380 struct b43_phy *phy = &dev->phy;
384 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM &&
385 bus->boardinfo.type == SSB_BOARD_BCM4309G) {
386 if (bus->boardinfo.rev < 0x43) {
389 } else if (bus->boardinfo.rev < 0x51) {
395 if (phy->type == B43_PHYTYPE_A) {
400 switch (phy->radio_ver) {
402 switch (phy->radio_rev) {
409 switch (phy->radio_rev) {
414 if (phy->type == B43_PHYTYPE_G) {
415 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
416 && bus->boardinfo.type == SSB_BOARD_BCM4309G
417 && bus->boardinfo.rev >= 30)
419 else if (bus->boardinfo.vendor ==
421 && bus->boardinfo.type ==
427 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
428 && bus->boardinfo.type == SSB_BOARD_BCM4309G
429 && bus->boardinfo.rev >= 30)
436 if (phy->type == B43_PHYTYPE_G) {
437 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
438 && bus->boardinfo.type == SSB_BOARD_BCM4309G
439 && bus->boardinfo.rev >= 30)
441 else if (bus->boardinfo.vendor ==
443 && bus->boardinfo.type ==
446 else if (bus->chip_id == 0x4320)
477 static u16 default_tx_control(struct b43_wldev *dev)
479 struct b43_phy *phy = &dev->phy;
481 if (phy->radio_ver != 0x2050)
483 if (phy->radio_rev == 1)
484 return B43_TXCTL_PA2DB | B43_TXCTL_TXMIX;
485 if (phy->radio_rev < 6)
486 return B43_TXCTL_PA2DB;
487 if (phy->radio_rev == 8)
488 return B43_TXCTL_TXMIX;
492 /* This func is called "PHY calibrate" in the specs... */
493 void b43_phy_early_init(struct b43_wldev *dev)
495 struct b43_phy *phy = &dev->phy;
496 struct b43_txpower_lo_control *lo = phy->lo_control;
498 default_baseband_attenuation(dev, &phy->bbatt);
499 default_radio_attenuation(dev, &phy->rfatt);
500 phy->tx_control = (default_tx_control(dev) << 4);
502 /* Commit previous writes */
503 b43_read32(dev, B43_MMIO_MACCTL);
505 if (phy->type == B43_PHYTYPE_B || phy->type == B43_PHYTYPE_G) {
506 generate_rfatt_list(dev, &lo->rfatt_list);
507 generate_bbatt_list(dev, &lo->bbatt_list);
509 if (phy->type == B43_PHYTYPE_G && phy->rev == 1) {
510 /* Workaround: Temporarly disable gmode through the early init
511 * phase, as the gmode stuff is not needed for phy rev 1 */
513 b43_wireless_core_reset(dev, 0);
516 b43_wireless_core_reset(dev, B43_TMSLOW_GMODE);
520 /* GPHY_TSSI_Power_Lookup_Table_Init */
521 static void b43_gphy_tssi_power_lt_init(struct b43_wldev *dev)
523 struct b43_phy *phy = &dev->phy;
527 for (i = 0; i < 32; i++)
528 b43_ofdmtab_write16(dev, 0x3C20, i, phy->tssi2dbm[i]);
529 for (i = 32; i < 64; i++)
530 b43_ofdmtab_write16(dev, 0x3C00, i - 32, phy->tssi2dbm[i]);
531 for (i = 0; i < 64; i += 2) {
532 value = (u16) phy->tssi2dbm[i];
533 value |= ((u16) phy->tssi2dbm[i + 1]) << 8;
534 b43_phy_write(dev, 0x380 + (i / 2), value);
538 /* GPHY_Gain_Lookup_Table_Init */
539 static void b43_gphy_gain_lt_init(struct b43_wldev *dev)
541 struct b43_phy *phy = &dev->phy;
542 struct b43_txpower_lo_control *lo = phy->lo_control;
547 for (rf = 0; rf < lo->rfatt_list.len; rf++) {
548 for (bb = 0; bb < lo->bbatt_list.len; bb++) {
549 if (nr_written >= 0x40)
551 tmp = lo->bbatt_list.list[bb].att;
553 if (phy->radio_rev == 8)
557 tmp |= lo->rfatt_list.list[rf].att;
558 b43_phy_write(dev, 0x3C0 + nr_written, tmp);
564 static void hardware_pctl_init_aphy(struct b43_wldev *dev)
569 static void hardware_pctl_init_gphy(struct b43_wldev *dev)
571 struct b43_phy *phy = &dev->phy;
573 b43_phy_write(dev, 0x0036, (b43_phy_read(dev, 0x0036) & 0xFFC0)
574 | (phy->tgt_idle_tssi - phy->cur_idle_tssi));
575 b43_phy_write(dev, 0x0478, (b43_phy_read(dev, 0x0478) & 0xFF00)
576 | (phy->tgt_idle_tssi - phy->cur_idle_tssi));
577 b43_gphy_tssi_power_lt_init(dev);
578 b43_gphy_gain_lt_init(dev);
579 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060) & 0xFFBF);
580 b43_phy_write(dev, 0x0014, 0x0000);
582 B43_WARN_ON(phy->rev < 6);
583 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478)
585 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478)
587 b43_phy_write(dev, 0x0801, b43_phy_read(dev, 0x0801)
590 b43_gphy_dc_lt_init(dev, 1);
593 /* HardwarePowerControl init for A and G PHY */
594 static void b43_hardware_pctl_init(struct b43_wldev *dev)
596 struct b43_phy *phy = &dev->phy;
598 if (!b43_has_hardware_pctl(phy)) {
599 /* No hardware power control */
600 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_HWPCTL);
603 /* Init the hwpctl related hardware */
606 hardware_pctl_init_aphy(dev);
609 hardware_pctl_init_gphy(dev);
614 /* Enable hardware pctl in firmware. */
615 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_HWPCTL);
618 static void b43_hardware_pctl_early_init(struct b43_wldev *dev)
620 struct b43_phy *phy = &dev->phy;
622 if (!b43_has_hardware_pctl(phy)) {
623 b43_phy_write(dev, 0x047A, 0xC111);
627 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036) & 0xFEFF);
628 b43_phy_write(dev, 0x002F, 0x0202);
629 b43_phy_write(dev, 0x047C, b43_phy_read(dev, 0x047C) | 0x0002);
630 b43_phy_write(dev, 0x047A, b43_phy_read(dev, 0x047A) | 0xF000);
631 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
632 b43_phy_write(dev, 0x047A, (b43_phy_read(dev, 0x047A)
634 b43_phy_write(dev, 0x005D, b43_phy_read(dev, 0x005D)
636 b43_phy_write(dev, 0x004E, (b43_phy_read(dev, 0x004E)
638 b43_phy_write(dev, 0x002E, 0xC07F);
639 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
642 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
644 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
646 b43_phy_write(dev, 0x005D, b43_phy_read(dev, 0x005D)
648 b43_phy_write(dev, 0x004F, b43_phy_read(dev, 0x004F)
650 b43_phy_write(dev, 0x004E, (b43_phy_read(dev, 0x004E)
652 b43_phy_write(dev, 0x002E, 0xC07F);
653 b43_phy_write(dev, 0x047A, (b43_phy_read(dev, 0x047A)
658 /* Intialize B/G PHY power control
659 * as described in http://bcm-specs.sipsolutions.net/InitPowerControl
661 static void b43_phy_init_pctl(struct b43_wldev *dev)
663 struct ssb_bus *bus = dev->dev->bus;
664 struct b43_phy *phy = &dev->phy;
665 struct b43_rfatt old_rfatt;
666 struct b43_bbatt old_bbatt;
667 u8 old_tx_control = 0;
669 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
670 (bus->boardinfo.type == SSB_BOARD_BU4306))
673 b43_phy_write(dev, 0x0028, 0x8018);
675 /* This does something with the Analog... */
676 b43_write16(dev, B43_MMIO_PHY0, b43_read16(dev, B43_MMIO_PHY0)
679 if (phy->type == B43_PHYTYPE_G && !phy->gmode)
681 b43_hardware_pctl_early_init(dev);
682 if (phy->cur_idle_tssi == 0) {
683 if (phy->radio_ver == 0x2050 && phy->analog == 0) {
684 b43_radio_write16(dev, 0x0076,
685 (b43_radio_read16(dev, 0x0076)
688 struct b43_rfatt rfatt;
689 struct b43_bbatt bbatt;
691 memcpy(&old_rfatt, &phy->rfatt, sizeof(old_rfatt));
692 memcpy(&old_bbatt, &phy->bbatt, sizeof(old_bbatt));
693 old_tx_control = phy->tx_control;
696 if (phy->radio_rev == 8) {
698 rfatt.with_padmix = 1;
701 rfatt.with_padmix = 0;
703 b43_set_txpower_g(dev, &bbatt, &rfatt, 0);
705 b43_dummy_transmission(dev);
706 phy->cur_idle_tssi = b43_phy_read(dev, B43_PHY_ITSSI);
708 /* Current-Idle-TSSI sanity check. */
709 if (abs(phy->cur_idle_tssi - phy->tgt_idle_tssi) >= 20) {
711 "!WARNING! Idle-TSSI phy->cur_idle_tssi "
712 "measuring failed. (cur=%d, tgt=%d). Disabling TX power "
713 "adjustment.\n", phy->cur_idle_tssi,
715 phy->cur_idle_tssi = 0;
718 if (phy->radio_ver == 0x2050 && phy->analog == 0) {
719 b43_radio_write16(dev, 0x0076,
720 b43_radio_read16(dev, 0x0076)
723 b43_set_txpower_g(dev, &old_bbatt,
724 &old_rfatt, old_tx_control);
727 b43_hardware_pctl_init(dev);
728 b43_shm_clear_tssi(dev);
731 static void b43_phy_rssiagc(struct b43_wldev *dev, u8 enable)
735 if (dev->phy.rev < 3) {
737 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++) {
738 b43_ofdmtab_write16(dev,
739 B43_OFDMTAB_LNAHPFGAIN1, i, 0xFFF8);
740 b43_ofdmtab_write16(dev,
741 B43_OFDMTAB_WRSSI, i, 0xFFF8);
744 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++) {
745 b43_ofdmtab_write16(dev,
746 B43_OFDMTAB_LNAHPFGAIN1, i, b43_tab_rssiagc1[i]);
747 b43_ofdmtab_write16(dev,
748 B43_OFDMTAB_WRSSI, i, b43_tab_rssiagc1[i]);
752 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++)
753 b43_ofdmtab_write16(dev,
754 B43_OFDMTAB_WRSSI, i, 0x0820);
756 for (i = 0; i < B43_TAB_RSSIAGC2_SIZE; i++)
757 b43_ofdmtab_write16(dev,
758 B43_OFDMTAB_WRSSI, i, b43_tab_rssiagc2[i]);
762 static void b43_phy_ww(struct b43_wldev *dev)
764 u16 b, curr_s, best_s = 0xFFFF;
767 b43_phy_write(dev, B43_PHY_CRS0,
768 b43_phy_read(dev, B43_PHY_CRS0) & ~B43_PHY_CRS0_EN);
769 b43_phy_write(dev, B43_PHY_OFDM(0x1B),
770 b43_phy_read(dev, B43_PHY_OFDM(0x1B)) | 0x1000);
771 b43_phy_write(dev, B43_PHY_OFDM(0x82),
772 (b43_phy_read(dev, B43_PHY_OFDM(0x82)) & 0xF0FF) | 0x0300);
773 b43_radio_write16(dev, 0x0009,
774 b43_radio_read16(dev, 0x0009) | 0x0080);
775 b43_radio_write16(dev, 0x0012,
776 (b43_radio_read16(dev, 0x0012) & 0xFFFC) | 0x0002);
777 b43_wa_initgains(dev);
778 b43_phy_write(dev, B43_PHY_OFDM(0xBA), 0x3ED5);
779 b = b43_phy_read(dev, B43_PHY_PWRDOWN);
780 b43_phy_write(dev, B43_PHY_PWRDOWN, (b & 0xFFF8) | 0x0005);
781 b43_radio_write16(dev, 0x0004,
782 b43_radio_read16(dev, 0x0004) | 0x0004);
783 for (i = 0x10; i <= 0x20; i++) {
784 b43_radio_write16(dev, 0x0013, i);
785 curr_s = b43_phy_read(dev, B43_PHY_OTABLEQ) & 0x00FF;
789 } else if (curr_s >= 0x0080)
790 curr_s = 0x0100 - curr_s;
794 b43_phy_write(dev, B43_PHY_PWRDOWN, b);
795 b43_radio_write16(dev, 0x0004,
796 b43_radio_read16(dev, 0x0004) & 0xFFFB);
797 b43_radio_write16(dev, 0x0013, best_s);
798 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1_R1, 0, 0xFFEC);
799 b43_phy_write(dev, B43_PHY_OFDM(0xB7), 0x1E80);
800 b43_phy_write(dev, B43_PHY_OFDM(0xB6), 0x1C00);
801 b43_phy_write(dev, B43_PHY_OFDM(0xB5), 0x0EC0);
802 b43_phy_write(dev, B43_PHY_OFDM(0xB2), 0x00C0);
803 b43_phy_write(dev, B43_PHY_OFDM(0xB9), 0x1FFF);
804 b43_phy_write(dev, B43_PHY_OFDM(0xBB),
805 (b43_phy_read(dev, B43_PHY_OFDM(0xBB)) & 0xF000) | 0x0053);
806 b43_phy_write(dev, B43_PHY_OFDM61,
807 (b43_phy_read(dev, B43_PHY_OFDM61) & 0xFE1F) | 0x0120);
808 b43_phy_write(dev, B43_PHY_OFDM(0x13),
809 (b43_phy_read(dev, B43_PHY_OFDM(0x13)) & 0x0FFF) | 0x3000);
810 b43_phy_write(dev, B43_PHY_OFDM(0x14),
811 (b43_phy_read(dev, B43_PHY_OFDM(0x14)) & 0x0FFF) | 0x3000);
812 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 6, 0x0017);
813 for (i = 0; i < 6; i++)
814 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, i, 0x000F);
815 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0D, 0x000E);
816 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0E, 0x0011);
817 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0F, 0x0013);
818 b43_phy_write(dev, B43_PHY_OFDM(0x33), 0x5030);
819 b43_phy_write(dev, B43_PHY_CRS0,
820 b43_phy_read(dev, B43_PHY_CRS0) | B43_PHY_CRS0_EN);
823 /* Initialize APHY. This is also called for the GPHY in some cases. */
824 static void b43_phy_inita(struct b43_wldev *dev)
826 struct ssb_bus *bus = dev->dev->bus;
827 struct b43_phy *phy = &dev->phy;
832 if (phy->type == B43_PHYTYPE_A)
833 b43_phy_write(dev, B43_PHY_OFDM(0x1B),
834 b43_phy_read(dev, B43_PHY_OFDM(0x1B)) & ~0x1000);
835 if (b43_phy_read(dev, B43_PHY_ENCORE) & B43_PHY_ENCORE_EN)
836 b43_phy_write(dev, B43_PHY_ENCORE,
837 b43_phy_read(dev, B43_PHY_ENCORE) | 0x0010);
839 b43_phy_write(dev, B43_PHY_ENCORE,
840 b43_phy_read(dev, B43_PHY_ENCORE) & ~0x1010);
845 if (phy->type == B43_PHYTYPE_A) {
846 if (phy->gmode && (phy->rev < 3))
847 b43_phy_write(dev, 0x0034,
848 b43_phy_read(dev, 0x0034) | 0x0001);
849 b43_phy_rssiagc(dev, 0);
851 b43_phy_write(dev, B43_PHY_CRS0,
852 b43_phy_read(dev, B43_PHY_CRS0) | B43_PHY_CRS0_EN);
854 b43_radio_init2060(dev);
856 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
857 ((bus->boardinfo.type == SSB_BOARD_BU4306) ||
858 (bus->boardinfo.type == SSB_BOARD_BU4309))) {
865 hardware_pctl_init_aphy(dev);
867 //TODO: radar detection
870 if ((phy->type == B43_PHYTYPE_G) &&
871 (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL)) {
872 b43_phy_write(dev, B43_PHY_OFDM(0x6E),
873 (b43_phy_read(dev, B43_PHY_OFDM(0x6E))
878 static void b43_phy_initb5(struct b43_wldev *dev)
880 struct ssb_bus *bus = dev->dev->bus;
881 struct b43_phy *phy = &dev->phy;
885 if (phy->analog == 1) {
886 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A)
889 if ((bus->boardinfo.vendor != SSB_BOARDVENDOR_BCM) &&
890 (bus->boardinfo.type != SSB_BOARD_BU4306)) {
892 for (offset = 0x00A8; offset < 0x00C7; offset++) {
893 b43_phy_write(dev, offset, value);
897 b43_phy_write(dev, 0x0035, (b43_phy_read(dev, 0x0035) & 0xF0FF)
899 if (phy->radio_ver == 0x2050)
900 b43_phy_write(dev, 0x0038, 0x0667);
902 if (phy->gmode || phy->rev >= 2) {
903 if (phy->radio_ver == 0x2050) {
904 b43_radio_write16(dev, 0x007A,
905 b43_radio_read16(dev, 0x007A)
907 b43_radio_write16(dev, 0x0051,
908 b43_radio_read16(dev, 0x0051)
911 b43_write16(dev, B43_MMIO_PHY_RADIO, 0x0000);
913 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x0100);
914 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x2000);
916 b43_phy_write(dev, 0x001C, 0x186A);
918 b43_phy_write(dev, 0x0013,
919 (b43_phy_read(dev, 0x0013) & 0x00FF) | 0x1900);
920 b43_phy_write(dev, 0x0035,
921 (b43_phy_read(dev, 0x0035) & 0xFFC0) | 0x0064);
922 b43_phy_write(dev, 0x005D,
923 (b43_phy_read(dev, 0x005D) & 0xFF80) | 0x000A);
926 if (dev->bad_frames_preempt) {
927 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
929 B43_PHY_RADIO_BITFIELD) | (1 << 11));
932 if (phy->analog == 1) {
933 b43_phy_write(dev, 0x0026, 0xCE00);
934 b43_phy_write(dev, 0x0021, 0x3763);
935 b43_phy_write(dev, 0x0022, 0x1BC3);
936 b43_phy_write(dev, 0x0023, 0x06F9);
937 b43_phy_write(dev, 0x0024, 0x037E);
939 b43_phy_write(dev, 0x0026, 0xCC00);
940 b43_phy_write(dev, 0x0030, 0x00C6);
941 b43_write16(dev, 0x03EC, 0x3F22);
943 if (phy->analog == 1)
944 b43_phy_write(dev, 0x0020, 0x3E1C);
946 b43_phy_write(dev, 0x0020, 0x301C);
948 if (phy->analog == 0)
949 b43_write16(dev, 0x03E4, 0x3000);
951 old_channel = phy->channel;
952 /* Force to channel 7, even if not supported. */
953 b43_radio_selectchannel(dev, 7, 0);
955 if (phy->radio_ver != 0x2050) {
956 b43_radio_write16(dev, 0x0075, 0x0080);
957 b43_radio_write16(dev, 0x0079, 0x0081);
960 b43_radio_write16(dev, 0x0050, 0x0020);
961 b43_radio_write16(dev, 0x0050, 0x0023);
963 if (phy->radio_ver == 0x2050) {
964 b43_radio_write16(dev, 0x0050, 0x0020);
965 b43_radio_write16(dev, 0x005A, 0x0070);
968 b43_radio_write16(dev, 0x005B, 0x007B);
969 b43_radio_write16(dev, 0x005C, 0x00B0);
971 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0007);
973 b43_radio_selectchannel(dev, old_channel, 0);
975 b43_phy_write(dev, 0x0014, 0x0080);
976 b43_phy_write(dev, 0x0032, 0x00CA);
977 b43_phy_write(dev, 0x002A, 0x88A3);
979 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt, phy->tx_control);
981 if (phy->radio_ver == 0x2050)
982 b43_radio_write16(dev, 0x005D, 0x000D);
984 b43_write16(dev, 0x03E4, (b43_read16(dev, 0x03E4) & 0xFFC0) | 0x0004);
987 static void b43_phy_initb6(struct b43_wldev *dev)
989 struct b43_phy *phy = &dev->phy;
993 b43_phy_write(dev, 0x003E, 0x817A);
994 b43_radio_write16(dev, 0x007A,
995 (b43_radio_read16(dev, 0x007A) | 0x0058));
996 if (phy->radio_rev == 4 || phy->radio_rev == 5) {
997 b43_radio_write16(dev, 0x51, 0x37);
998 b43_radio_write16(dev, 0x52, 0x70);
999 b43_radio_write16(dev, 0x53, 0xB3);
1000 b43_radio_write16(dev, 0x54, 0x9B);
1001 b43_radio_write16(dev, 0x5A, 0x88);
1002 b43_radio_write16(dev, 0x5B, 0x88);
1003 b43_radio_write16(dev, 0x5D, 0x88);
1004 b43_radio_write16(dev, 0x5E, 0x88);
1005 b43_radio_write16(dev, 0x7D, 0x88);
1006 b43_hf_write(dev, b43_hf_read(dev)
1007 | B43_HF_TSSIRPSMW);
1009 B43_WARN_ON(phy->radio_rev == 6 || phy->radio_rev == 7); /* We had code for these revs here... */
1010 if (phy->radio_rev == 8) {
1011 b43_radio_write16(dev, 0x51, 0);
1012 b43_radio_write16(dev, 0x52, 0x40);
1013 b43_radio_write16(dev, 0x53, 0xB7);
1014 b43_radio_write16(dev, 0x54, 0x98);
1015 b43_radio_write16(dev, 0x5A, 0x88);
1016 b43_radio_write16(dev, 0x5B, 0x6B);
1017 b43_radio_write16(dev, 0x5C, 0x0F);
1018 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_ALTIQ) {
1019 b43_radio_write16(dev, 0x5D, 0xFA);
1020 b43_radio_write16(dev, 0x5E, 0xD8);
1022 b43_radio_write16(dev, 0x5D, 0xF5);
1023 b43_radio_write16(dev, 0x5E, 0xB8);
1025 b43_radio_write16(dev, 0x0073, 0x0003);
1026 b43_radio_write16(dev, 0x007D, 0x00A8);
1027 b43_radio_write16(dev, 0x007C, 0x0001);
1028 b43_radio_write16(dev, 0x007E, 0x0008);
1031 for (offset = 0x0088; offset < 0x0098; offset++) {
1032 b43_phy_write(dev, offset, val);
1036 for (offset = 0x0098; offset < 0x00A8; offset++) {
1037 b43_phy_write(dev, offset, val);
1041 for (offset = 0x00A8; offset < 0x00C8; offset++) {
1042 b43_phy_write(dev, offset, (val & 0x3F3F));
1045 if (phy->type == B43_PHYTYPE_G) {
1046 b43_radio_write16(dev, 0x007A,
1047 b43_radio_read16(dev, 0x007A) | 0x0020);
1048 b43_radio_write16(dev, 0x0051,
1049 b43_radio_read16(dev, 0x0051) | 0x0004);
1050 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x0100);
1051 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x2000);
1052 b43_phy_write(dev, 0x5B, 0);
1053 b43_phy_write(dev, 0x5C, 0);
1056 old_channel = phy->channel;
1057 if (old_channel >= 8)
1058 b43_radio_selectchannel(dev, 1, 0);
1060 b43_radio_selectchannel(dev, 13, 0);
1062 b43_radio_write16(dev, 0x0050, 0x0020);
1063 b43_radio_write16(dev, 0x0050, 0x0023);
1065 if (phy->radio_rev < 6 || phy->radio_rev == 8) {
1066 b43_radio_write16(dev, 0x7C, (b43_radio_read16(dev, 0x7C)
1068 b43_radio_write16(dev, 0x50, 0x20);
1070 if (phy->radio_rev <= 2) {
1071 b43_radio_write16(dev, 0x7C, 0x20);
1072 b43_radio_write16(dev, 0x5A, 0x70);
1073 b43_radio_write16(dev, 0x5B, 0x7B);
1074 b43_radio_write16(dev, 0x5C, 0xB0);
1076 b43_radio_write16(dev, 0x007A,
1077 (b43_radio_read16(dev, 0x007A) & 0x00F8) | 0x0007);
1079 b43_radio_selectchannel(dev, old_channel, 0);
1081 b43_phy_write(dev, 0x0014, 0x0200);
1082 if (phy->radio_rev >= 6)
1083 b43_phy_write(dev, 0x2A, 0x88C2);
1085 b43_phy_write(dev, 0x2A, 0x8AC0);
1086 b43_phy_write(dev, 0x0038, 0x0668);
1087 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt, phy->tx_control);
1088 if (phy->radio_rev <= 5) {
1089 b43_phy_write(dev, 0x5D, (b43_phy_read(dev, 0x5D)
1090 & 0xFF80) | 0x0003);
1092 if (phy->radio_rev <= 2)
1093 b43_radio_write16(dev, 0x005D, 0x000D);
1095 if (phy->analog == 4) {
1096 b43_write16(dev, 0x3E4, 9);
1097 b43_phy_write(dev, 0x61, b43_phy_read(dev, 0x61)
1100 b43_phy_write(dev, 0x0002, (b43_phy_read(dev, 0x0002) & 0xFFC0)
1103 if (phy->type == B43_PHYTYPE_B)
1105 else if (phy->type == B43_PHYTYPE_G)
1106 b43_write16(dev, 0x03E6, 0x0);
1109 static void b43_calc_loopback_gain(struct b43_wldev *dev)
1111 struct b43_phy *phy = &dev->phy;
1112 u16 backup_phy[16] = { 0 };
1113 u16 backup_radio[3];
1115 u16 i, j, loop_i_max;
1117 u16 loop1_outer_done, loop1_inner_done;
1119 backup_phy[0] = b43_phy_read(dev, B43_PHY_CRS0);
1120 backup_phy[1] = b43_phy_read(dev, B43_PHY_CCKBBANDCFG);
1121 backup_phy[2] = b43_phy_read(dev, B43_PHY_RFOVER);
1122 backup_phy[3] = b43_phy_read(dev, B43_PHY_RFOVERVAL);
1123 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1124 backup_phy[4] = b43_phy_read(dev, B43_PHY_ANALOGOVER);
1125 backup_phy[5] = b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
1127 backup_phy[6] = b43_phy_read(dev, B43_PHY_CCK(0x5A));
1128 backup_phy[7] = b43_phy_read(dev, B43_PHY_CCK(0x59));
1129 backup_phy[8] = b43_phy_read(dev, B43_PHY_CCK(0x58));
1130 backup_phy[9] = b43_phy_read(dev, B43_PHY_CCK(0x0A));
1131 backup_phy[10] = b43_phy_read(dev, B43_PHY_CCK(0x03));
1132 backup_phy[11] = b43_phy_read(dev, B43_PHY_LO_MASK);
1133 backup_phy[12] = b43_phy_read(dev, B43_PHY_LO_CTL);
1134 backup_phy[13] = b43_phy_read(dev, B43_PHY_CCK(0x2B));
1135 backup_phy[14] = b43_phy_read(dev, B43_PHY_PGACTL);
1136 backup_phy[15] = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
1137 backup_bband = phy->bbatt.att;
1138 backup_radio[0] = b43_radio_read16(dev, 0x52);
1139 backup_radio[1] = b43_radio_read16(dev, 0x43);
1140 backup_radio[2] = b43_radio_read16(dev, 0x7A);
1142 b43_phy_write(dev, B43_PHY_CRS0,
1143 b43_phy_read(dev, B43_PHY_CRS0) & 0x3FFF);
1144 b43_phy_write(dev, B43_PHY_CCKBBANDCFG,
1145 b43_phy_read(dev, B43_PHY_CCKBBANDCFG) | 0x8000);
1146 b43_phy_write(dev, B43_PHY_RFOVER,
1147 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0002);
1148 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1149 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xFFFD);
1150 b43_phy_write(dev, B43_PHY_RFOVER,
1151 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0001);
1152 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1153 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xFFFE);
1154 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1155 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1156 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0001);
1157 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1159 B43_PHY_ANALOGOVERVAL) & 0xFFFE);
1160 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1161 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0002);
1162 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1164 B43_PHY_ANALOGOVERVAL) & 0xFFFD);
1166 b43_phy_write(dev, B43_PHY_RFOVER,
1167 b43_phy_read(dev, B43_PHY_RFOVER) | 0x000C);
1168 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1169 b43_phy_read(dev, B43_PHY_RFOVERVAL) | 0x000C);
1170 b43_phy_write(dev, B43_PHY_RFOVER,
1171 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0030);
1172 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1173 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1176 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0780);
1177 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
1178 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
1180 b43_phy_write(dev, B43_PHY_CCK(0x0A),
1181 b43_phy_read(dev, B43_PHY_CCK(0x0A)) | 0x2000);
1182 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1183 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1184 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0004);
1185 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1187 B43_PHY_ANALOGOVERVAL) & 0xFFFB);
1189 b43_phy_write(dev, B43_PHY_CCK(0x03),
1190 (b43_phy_read(dev, B43_PHY_CCK(0x03))
1193 if (phy->radio_rev == 8) {
1194 b43_radio_write16(dev, 0x43, 0x000F);
1196 b43_radio_write16(dev, 0x52, 0);
1197 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
1200 b43_phy_set_baseband_attenuation(dev, 11);
1203 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC020);
1205 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8020);
1206 b43_phy_write(dev, B43_PHY_LO_CTL, 0);
1208 b43_phy_write(dev, B43_PHY_CCK(0x2B),
1209 (b43_phy_read(dev, B43_PHY_CCK(0x2B))
1211 b43_phy_write(dev, B43_PHY_CCK(0x2B),
1212 (b43_phy_read(dev, B43_PHY_CCK(0x2B))
1215 b43_phy_write(dev, B43_PHY_RFOVER,
1216 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0100);
1217 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1218 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xCFFF);
1220 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA) {
1221 if (phy->rev >= 7) {
1222 b43_phy_write(dev, B43_PHY_RFOVER,
1223 b43_phy_read(dev, B43_PHY_RFOVER)
1225 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1226 b43_phy_read(dev, B43_PHY_RFOVERVAL)
1230 b43_radio_write16(dev, 0x7A, b43_radio_read16(dev, 0x7A)
1234 loop_i_max = (phy->radio_rev == 8) ? 15 : 9;
1235 for (i = 0; i < loop_i_max; i++) {
1236 for (j = 0; j < 16; j++) {
1237 b43_radio_write16(dev, 0x43, i);
1238 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1239 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1240 & 0xF0FF) | (j << 8));
1241 b43_phy_write(dev, B43_PHY_PGACTL,
1242 (b43_phy_read(dev, B43_PHY_PGACTL)
1243 & 0x0FFF) | 0xA000);
1244 b43_phy_write(dev, B43_PHY_PGACTL,
1245 b43_phy_read(dev, B43_PHY_PGACTL)
1248 if (b43_phy_read(dev, B43_PHY_LO_LEAKAGE) >= 0xDFC)
1253 loop1_outer_done = i;
1254 loop1_inner_done = j;
1256 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1257 b43_phy_read(dev, B43_PHY_RFOVERVAL)
1260 for (j = j - 8; j < 16; j++) {
1261 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1262 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1263 & 0xF0FF) | (j << 8));
1264 b43_phy_write(dev, B43_PHY_PGACTL,
1265 (b43_phy_read(dev, B43_PHY_PGACTL)
1266 & 0x0FFF) | 0xA000);
1267 b43_phy_write(dev, B43_PHY_PGACTL,
1268 b43_phy_read(dev, B43_PHY_PGACTL)
1272 if (b43_phy_read(dev, B43_PHY_LO_LEAKAGE) >= 0xDFC)
1279 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1280 b43_phy_write(dev, B43_PHY_ANALOGOVER, backup_phy[4]);
1281 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL, backup_phy[5]);
1283 b43_phy_write(dev, B43_PHY_CCK(0x5A), backup_phy[6]);
1284 b43_phy_write(dev, B43_PHY_CCK(0x59), backup_phy[7]);
1285 b43_phy_write(dev, B43_PHY_CCK(0x58), backup_phy[8]);
1286 b43_phy_write(dev, B43_PHY_CCK(0x0A), backup_phy[9]);
1287 b43_phy_write(dev, B43_PHY_CCK(0x03), backup_phy[10]);
1288 b43_phy_write(dev, B43_PHY_LO_MASK, backup_phy[11]);
1289 b43_phy_write(dev, B43_PHY_LO_CTL, backup_phy[12]);
1290 b43_phy_write(dev, B43_PHY_CCK(0x2B), backup_phy[13]);
1291 b43_phy_write(dev, B43_PHY_PGACTL, backup_phy[14]);
1293 b43_phy_set_baseband_attenuation(dev, backup_bband);
1295 b43_radio_write16(dev, 0x52, backup_radio[0]);
1296 b43_radio_write16(dev, 0x43, backup_radio[1]);
1297 b43_radio_write16(dev, 0x7A, backup_radio[2]);
1299 b43_phy_write(dev, B43_PHY_RFOVER, backup_phy[2] | 0x0003);
1301 b43_phy_write(dev, B43_PHY_RFOVER, backup_phy[2]);
1302 b43_phy_write(dev, B43_PHY_RFOVERVAL, backup_phy[3]);
1303 b43_phy_write(dev, B43_PHY_CRS0, backup_phy[0]);
1304 b43_phy_write(dev, B43_PHY_CCKBBANDCFG, backup_phy[1]);
1307 ((loop1_inner_done * 6) - (loop1_outer_done * 4)) - 11;
1308 phy->trsw_rx_gain = trsw_rx * 2;
1311 static void b43_phy_initg(struct b43_wldev *dev)
1313 struct b43_phy *phy = &dev->phy;
1317 b43_phy_initb5(dev);
1319 b43_phy_initb6(dev);
1321 if (phy->rev >= 2 || phy->gmode)
1324 if (phy->rev >= 2) {
1325 b43_phy_write(dev, B43_PHY_ANALOGOVER, 0);
1326 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL, 0);
1328 if (phy->rev == 2) {
1329 b43_phy_write(dev, B43_PHY_RFOVER, 0);
1330 b43_phy_write(dev, B43_PHY_PGACTL, 0xC0);
1333 b43_phy_write(dev, B43_PHY_RFOVER, 0x400);
1334 b43_phy_write(dev, B43_PHY_PGACTL, 0xC0);
1336 if (phy->gmode || phy->rev >= 2) {
1337 tmp = b43_phy_read(dev, B43_PHY_VERSION_OFDM);
1338 tmp &= B43_PHYVER_VERSION;
1339 if (tmp == 3 || tmp == 5) {
1340 b43_phy_write(dev, B43_PHY_OFDM(0xC2), 0x1816);
1341 b43_phy_write(dev, B43_PHY_OFDM(0xC3), 0x8006);
1344 b43_phy_write(dev, B43_PHY_OFDM(0xCC),
1345 (b43_phy_read(dev, B43_PHY_OFDM(0xCC))
1346 & 0x00FF) | 0x1F00);
1349 if ((phy->rev <= 2 && phy->gmode) || phy->rev >= 2)
1350 b43_phy_write(dev, B43_PHY_OFDM(0x7E), 0x78);
1351 if (phy->radio_rev == 8) {
1352 b43_phy_write(dev, B43_PHY_EXTG(0x01),
1353 b43_phy_read(dev, B43_PHY_EXTG(0x01))
1355 b43_phy_write(dev, B43_PHY_OFDM(0x3E),
1356 b43_phy_read(dev, B43_PHY_OFDM(0x3E))
1359 if (has_loopback_gain(phy))
1360 b43_calc_loopback_gain(dev);
1362 if (phy->radio_rev != 8) {
1363 if (phy->initval == 0xFFFF)
1364 phy->initval = b43_radio_init2050(dev);
1366 b43_radio_write16(dev, 0x0078, phy->initval);
1369 if (has_tx_magnification(phy)) {
1370 b43_radio_write16(dev, 0x52,
1371 (b43_radio_read16(dev, 0x52) & 0xFF00)
1372 | phy->lo_control->tx_bias | phy->
1373 lo_control->tx_magn);
1375 b43_radio_write16(dev, 0x52,
1376 (b43_radio_read16(dev, 0x52) & 0xFFF0)
1377 | phy->lo_control->tx_bias);
1379 if (phy->rev >= 6) {
1380 b43_phy_write(dev, B43_PHY_CCK(0x36),
1381 (b43_phy_read(dev, B43_PHY_CCK(0x36))
1382 & 0x0FFF) | (phy->lo_control->
1385 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL)
1386 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8075);
1388 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x807F);
1390 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x101);
1392 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x202);
1393 if (phy->gmode || phy->rev >= 2) {
1394 b43_lo_g_adjust(dev);
1395 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
1398 if (!(dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI)) {
1399 /* The specs state to update the NRSSI LT with
1400 * the value 0x7FFFFFFF here. I think that is some weird
1401 * compiler optimization in the original driver.
1402 * Essentially, what we do here is resetting all NRSSI LT
1403 * entries to -32 (see the clamp_val() in nrssi_hw_update())
1405 b43_nrssi_hw_update(dev, 0xFFFF); //FIXME?
1406 b43_calc_nrssi_threshold(dev);
1407 } else if (phy->gmode || phy->rev >= 2) {
1408 if (phy->nrssi[0] == -1000) {
1409 B43_WARN_ON(phy->nrssi[1] != -1000);
1410 b43_calc_nrssi_slope(dev);
1412 b43_calc_nrssi_threshold(dev);
1414 if (phy->radio_rev == 8)
1415 b43_phy_write(dev, B43_PHY_EXTG(0x05), 0x3230);
1416 b43_phy_init_pctl(dev);
1417 /* FIXME: The spec says in the following if, the 0 should be replaced
1418 'if OFDM may not be used in the current locale'
1419 but OFDM is legal everywhere */
1420 if ((dev->dev->bus->chip_id == 0x4306
1421 && dev->dev->bus->chip_package == 2) || 0) {
1422 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
1424 b43_phy_write(dev, B43_PHY_OFDM(0xC3),
1425 b43_phy_read(dev, B43_PHY_OFDM(0xC3))
1430 /* Set the baseband attenuation value on chip. */
1431 void b43_phy_set_baseband_attenuation(struct b43_wldev *dev,
1432 u16 baseband_attenuation)
1434 struct b43_phy *phy = &dev->phy;
1436 if (phy->analog == 0) {
1437 b43_write16(dev, B43_MMIO_PHY0, (b43_read16(dev, B43_MMIO_PHY0)
1439 baseband_attenuation);
1440 } else if (phy->analog > 1) {
1441 b43_phy_write(dev, B43_PHY_DACCTL,
1442 (b43_phy_read(dev, B43_PHY_DACCTL)
1443 & 0xFFC3) | (baseband_attenuation << 2));
1445 b43_phy_write(dev, B43_PHY_DACCTL,
1446 (b43_phy_read(dev, B43_PHY_DACCTL)
1447 & 0xFF87) | (baseband_attenuation << 3));
1451 /* http://bcm-specs.sipsolutions.net/EstimatePowerOut
1452 * This function converts a TSSI value to dBm in Q5.2
1454 static s8 b43_phy_estimate_power_out(struct b43_wldev *dev, s8 tssi)
1456 struct b43_phy *phy = &dev->phy;
1460 tmp = (phy->tgt_idle_tssi - phy->cur_idle_tssi + tssi);
1462 switch (phy->type) {
1465 tmp = clamp_val(tmp, 0x00, 0xFF);
1466 dbm = phy->tssi2dbm[tmp];
1467 //TODO: There's a FIXME on the specs
1471 tmp = clamp_val(tmp, 0x00, 0x3F);
1472 dbm = phy->tssi2dbm[tmp];
1481 void b43_put_attenuation_into_ranges(struct b43_wldev *dev,
1482 int *_bbatt, int *_rfatt)
1484 int rfatt = *_rfatt;
1485 int bbatt = *_bbatt;
1486 struct b43_txpower_lo_control *lo = dev->phy.lo_control;
1488 /* Get baseband and radio attenuation values into their permitted ranges.
1489 * Radio attenuation affects power level 4 times as much as baseband. */
1491 /* Range constants */
1492 const int rf_min = lo->rfatt_list.min_val;
1493 const int rf_max = lo->rfatt_list.max_val;
1494 const int bb_min = lo->bbatt_list.min_val;
1495 const int bb_max = lo->bbatt_list.max_val;
1498 if (rfatt > rf_max && bbatt > bb_max - 4)
1499 break; /* Can not get it into ranges */
1500 if (rfatt < rf_min && bbatt < bb_min + 4)
1501 break; /* Can not get it into ranges */
1502 if (bbatt > bb_max && rfatt > rf_max - 1)
1503 break; /* Can not get it into ranges */
1504 if (bbatt < bb_min && rfatt < rf_min + 1)
1505 break; /* Can not get it into ranges */
1507 if (bbatt > bb_max) {
1512 if (bbatt < bb_min) {
1517 if (rfatt > rf_max) {
1522 if (rfatt < rf_min) {
1530 *_rfatt = clamp_val(rfatt, rf_min, rf_max);
1531 *_bbatt = clamp_val(bbatt, bb_min, bb_max);
1534 /* http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower */
1535 void b43_phy_xmitpower(struct b43_wldev *dev)
1537 struct ssb_bus *bus = dev->dev->bus;
1538 struct b43_phy *phy = &dev->phy;
1540 if (phy->cur_idle_tssi == 0)
1542 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
1543 (bus->boardinfo.type == SSB_BOARD_BU4306))
1545 #ifdef CONFIG_B43_DEBUG
1546 if (phy->manual_txpower_control)
1550 switch (phy->type) {
1551 case B43_PHYTYPE_A:{
1553 //TODO: Nothing for A PHYs yet :-/
1558 case B43_PHYTYPE_G:{
1563 int desired_pwr, estimated_pwr, pwr_adjust;
1564 int rfatt_delta, bbatt_delta;
1568 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x0058);
1569 v0 = (s8) (tmp & 0x00FF);
1570 v1 = (s8) ((tmp & 0xFF00) >> 8);
1571 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x005A);
1572 v2 = (s8) (tmp & 0x00FF);
1573 v3 = (s8) ((tmp & 0xFF00) >> 8);
1576 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F
1579 b43_shm_read16(dev, B43_SHM_SHARED, 0x0070);
1580 v0 = (s8) (tmp & 0x00FF);
1581 v1 = (s8) ((tmp & 0xFF00) >> 8);
1583 b43_shm_read16(dev, B43_SHM_SHARED, 0x0072);
1584 v2 = (s8) (tmp & 0x00FF);
1585 v3 = (s8) ((tmp & 0xFF00) >> 8);
1586 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F
1589 v0 = (v0 + 0x20) & 0x3F;
1590 v1 = (v1 + 0x20) & 0x3F;
1591 v2 = (v2 + 0x20) & 0x3F;
1592 v3 = (v3 + 0x20) & 0x3F;
1595 b43_shm_clear_tssi(dev);
1597 average = (v0 + v1 + v2 + v3 + 2) / 4;
1600 && (b43_shm_read16(dev, B43_SHM_SHARED, 0x005E) &
1605 b43_phy_estimate_power_out(dev, average);
1607 max_pwr = dev->dev->bus->sprom.maxpwr_bg;
1608 if ((dev->dev->bus->sprom.boardflags_lo
1609 & B43_BFL_PACTRL) && (phy->type == B43_PHYTYPE_G))
1611 if (unlikely(max_pwr <= 0)) {
1613 "Invalid max-TX-power value in SPROM.\n");
1614 max_pwr = 60; /* fake it */
1615 dev->dev->bus->sprom.maxpwr_bg = max_pwr;
1619 max_pwr = min(REG - dev->dev->bus->sprom.antennagain_bgphy - 0x6, max_pwr)
1620 where REG is the max power as per the regulatory domain
1623 /* Get desired power (in Q5.2) */
1624 desired_pwr = INT_TO_Q52(phy->power_level);
1625 /* And limit it. max_pwr already is Q5.2 */
1626 desired_pwr = clamp_val(desired_pwr, 0, max_pwr);
1627 if (b43_debug(dev, B43_DBG_XMITPOWER)) {
1629 "Current TX power output: " Q52_FMT
1630 " dBm, " "Desired TX power output: "
1631 Q52_FMT " dBm\n", Q52_ARG(estimated_pwr),
1632 Q52_ARG(desired_pwr));
1635 /* Calculate the adjustment delta. */
1636 pwr_adjust = desired_pwr - estimated_pwr;
1638 /* RF attenuation delta. */
1639 rfatt_delta = ((pwr_adjust + 7) / 8);
1640 /* Lower attenuation => Bigger power output. Negate it. */
1641 rfatt_delta = -rfatt_delta;
1643 /* Baseband attenuation delta. */
1644 bbatt_delta = pwr_adjust / 2;
1645 /* Lower attenuation => Bigger power output. Negate it. */
1646 bbatt_delta = -bbatt_delta;
1647 /* RF att affects power level 4 times as much as
1648 * Baseband attennuation. Subtract it. */
1649 bbatt_delta -= 4 * rfatt_delta;
1651 /* So do we finally need to adjust something? */
1652 if ((rfatt_delta == 0) && (bbatt_delta == 0))
1655 /* Calculate the new attenuation values. */
1656 bbatt = phy->bbatt.att;
1657 bbatt += bbatt_delta;
1658 rfatt = phy->rfatt.att;
1659 rfatt += rfatt_delta;
1661 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
1662 tx_control = phy->tx_control;
1663 if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 2)) {
1665 if (tx_control == 0) {
1671 } else if (dev->dev->bus->sprom.
1674 bbatt += 4 * (rfatt - 2);
1677 } else if (rfatt > 4 && tx_control) {
1688 /* Save the control values */
1689 phy->tx_control = tx_control;
1690 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
1691 phy->rfatt.att = rfatt;
1692 phy->bbatt.att = bbatt;
1694 /* Adjust the hardware */
1696 b43_radio_lock(dev);
1697 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt,
1699 b43_radio_unlock(dev);
1700 b43_phy_unlock(dev);
1704 b43_nphy_xmitpower(dev);
1711 static inline s32 b43_tssi2dbm_ad(s32 num, s32 den)
1716 return (num + den / 2) / den;
1720 s8 b43_tssi2dbm_entry(s8 entry[], u8 index, s16 pab0, s16 pab1, s16 pab2)
1722 s32 m1, m2, f = 256, q, delta;
1725 m1 = b43_tssi2dbm_ad(16 * pab0 + index * pab1, 32);
1726 m2 = max(b43_tssi2dbm_ad(32768 + index * pab2, 256), 1);
1730 q = b43_tssi2dbm_ad(f * 4096 -
1731 b43_tssi2dbm_ad(m2 * f, 16) * f, 2048);
1735 } while (delta >= 2);
1736 entry[index] = clamp_val(b43_tssi2dbm_ad(m1 * f, 8192), -127, 128);
1740 /* http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table */
1741 int b43_phy_init_tssi2dbm_table(struct b43_wldev *dev)
1743 struct b43_phy *phy = &dev->phy;
1744 s16 pab0, pab1, pab2;
1748 if (phy->type == B43_PHYTYPE_A) {
1749 pab0 = (s16) (dev->dev->bus->sprom.pa1b0);
1750 pab1 = (s16) (dev->dev->bus->sprom.pa1b1);
1751 pab2 = (s16) (dev->dev->bus->sprom.pa1b2);
1753 pab0 = (s16) (dev->dev->bus->sprom.pa0b0);
1754 pab1 = (s16) (dev->dev->bus->sprom.pa0b1);
1755 pab2 = (s16) (dev->dev->bus->sprom.pa0b2);
1758 if ((dev->dev->bus->chip_id == 0x4301) && (phy->radio_ver != 0x2050)) {
1759 phy->tgt_idle_tssi = 0x34;
1760 phy->tssi2dbm = b43_tssi2dbm_b_table;
1764 if (pab0 != 0 && pab1 != 0 && pab2 != 0 &&
1765 pab0 != -1 && pab1 != -1 && pab2 != -1) {
1766 /* The pabX values are set in SPROM. Use them. */
1767 if (phy->type == B43_PHYTYPE_A) {
1768 if ((s8) dev->dev->bus->sprom.itssi_a != 0 &&
1769 (s8) dev->dev->bus->sprom.itssi_a != -1)
1770 phy->tgt_idle_tssi =
1771 (s8) (dev->dev->bus->sprom.itssi_a);
1773 phy->tgt_idle_tssi = 62;
1775 if ((s8) dev->dev->bus->sprom.itssi_bg != 0 &&
1776 (s8) dev->dev->bus->sprom.itssi_bg != -1)
1777 phy->tgt_idle_tssi =
1778 (s8) (dev->dev->bus->sprom.itssi_bg);
1780 phy->tgt_idle_tssi = 62;
1782 dyn_tssi2dbm = kmalloc(64, GFP_KERNEL);
1783 if (dyn_tssi2dbm == NULL) {
1784 b43err(dev->wl, "Could not allocate memory "
1785 "for tssi2dbm table\n");
1788 for (idx = 0; idx < 64; idx++)
1789 if (b43_tssi2dbm_entry
1790 (dyn_tssi2dbm, idx, pab0, pab1, pab2)) {
1791 phy->tssi2dbm = NULL;
1792 b43err(dev->wl, "Could not generate "
1793 "tssi2dBm table\n");
1794 kfree(dyn_tssi2dbm);
1797 phy->tssi2dbm = dyn_tssi2dbm;
1798 phy->dyn_tssi_tbl = 1;
1800 /* pabX values not set in SPROM. */
1801 switch (phy->type) {
1803 /* APHY needs a generated table. */
1804 phy->tssi2dbm = NULL;
1805 b43err(dev->wl, "Could not generate tssi2dBm "
1806 "table (wrong SPROM info)!\n");
1809 phy->tgt_idle_tssi = 0x34;
1810 phy->tssi2dbm = b43_tssi2dbm_b_table;
1813 phy->tgt_idle_tssi = 0x34;
1814 phy->tssi2dbm = b43_tssi2dbm_g_table;
1822 int b43_phy_init(struct b43_wldev *dev)
1824 struct b43_phy *phy = &dev->phy;
1825 bool unsupported = 0;
1828 switch (phy->type) {
1830 if (phy->rev == 2 || phy->rev == 3)
1839 err = b43_phy_initn(dev);
1845 b43err(dev->wl, "Unknown PHYTYPE found\n");
1850 void b43_set_rx_antenna(struct b43_wldev *dev, int antenna)
1852 struct b43_phy *phy = &dev->phy;
1857 if (antenna == B43_ANTENNA_AUTO0 || antenna == B43_ANTENNA_AUTO1)
1860 hf = b43_hf_read(dev);
1861 hf &= ~B43_HF_ANTDIVHELP;
1862 b43_hf_write(dev, hf);
1864 switch (phy->type) {
1867 tmp = b43_phy_read(dev, B43_PHY_BBANDCFG);
1868 tmp &= ~B43_PHY_BBANDCFG_RXANT;
1869 tmp |= (autodiv ? B43_ANTENNA_AUTO0 : antenna)
1870 << B43_PHY_BBANDCFG_RXANT_SHIFT;
1871 b43_phy_write(dev, B43_PHY_BBANDCFG, tmp);
1874 tmp = b43_phy_read(dev, B43_PHY_ANTDWELL);
1875 if (antenna == B43_ANTENNA_AUTO0)
1876 tmp &= ~B43_PHY_ANTDWELL_AUTODIV1;
1878 tmp |= B43_PHY_ANTDWELL_AUTODIV1;
1879 b43_phy_write(dev, B43_PHY_ANTDWELL, tmp);
1881 if (phy->type == B43_PHYTYPE_G) {
1882 tmp = b43_phy_read(dev, B43_PHY_ANTWRSETT);
1884 tmp |= B43_PHY_ANTWRSETT_ARXDIV;
1886 tmp &= ~B43_PHY_ANTWRSETT_ARXDIV;
1887 b43_phy_write(dev, B43_PHY_ANTWRSETT, tmp);
1888 if (phy->rev >= 2) {
1889 tmp = b43_phy_read(dev, B43_PHY_OFDM61);
1890 tmp |= B43_PHY_OFDM61_10;
1891 b43_phy_write(dev, B43_PHY_OFDM61, tmp);
1894 b43_phy_read(dev, B43_PHY_DIVSRCHGAINBACK);
1895 tmp = (tmp & 0xFF00) | 0x15;
1896 b43_phy_write(dev, B43_PHY_DIVSRCHGAINBACK,
1899 if (phy->rev == 2) {
1900 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1905 B43_PHY_ADIVRELATED);
1906 tmp = (tmp & 0xFF00) | 8;
1907 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1912 b43_phy_write(dev, B43_PHY_OFDM9B, 0xDC);
1915 tmp = b43_phy_read(dev, B43_PHY_ANTDWELL);
1916 tmp = (tmp & 0xFF00) | 0x24;
1917 b43_phy_write(dev, B43_PHY_ANTDWELL, tmp);
1919 tmp = b43_phy_read(dev, B43_PHY_OFDM61);
1921 b43_phy_write(dev, B43_PHY_OFDM61, tmp);
1922 if (phy->analog == 3) {
1923 b43_phy_write(dev, B43_PHY_CLIPPWRDOWNT,
1925 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1928 b43_phy_write(dev, B43_PHY_CLIPPWRDOWNT,
1932 B43_PHY_ADIVRELATED);
1933 tmp = (tmp & 0xFF00) | 8;
1934 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1941 tmp = b43_phy_read(dev, B43_PHY_CCKBBANDCFG);
1942 tmp &= ~B43_PHY_BBANDCFG_RXANT;
1943 tmp |= (autodiv ? B43_ANTENNA_AUTO0 : antenna)
1944 << B43_PHY_BBANDCFG_RXANT_SHIFT;
1945 b43_phy_write(dev, B43_PHY_CCKBBANDCFG, tmp);
1948 b43_nphy_set_rxantenna(dev, antenna);
1954 hf |= B43_HF_ANTDIVHELP;
1955 b43_hf_write(dev, hf);
1958 /* Get the freq, as it has to be written to the device. */
1959 static inline u16 channel2freq_bg(u8 channel)
1961 B43_WARN_ON(!(channel >= 1 && channel <= 14));
1963 return b43_radio_channel_codes_bg[channel - 1];
1966 /* Get the freq, as it has to be written to the device. */
1967 static inline u16 channel2freq_a(u8 channel)
1969 B43_WARN_ON(channel > 200);
1971 return (5000 + 5 * channel);
1974 void b43_radio_lock(struct b43_wldev *dev)
1978 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1979 B43_WARN_ON(macctl & B43_MACCTL_RADIOLOCK);
1980 macctl |= B43_MACCTL_RADIOLOCK;
1981 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1982 /* Commit the write and wait for the device
1983 * to exit any radio register access. */
1984 b43_read32(dev, B43_MMIO_MACCTL);
1988 void b43_radio_unlock(struct b43_wldev *dev)
1992 /* Commit any write */
1993 b43_read16(dev, B43_MMIO_PHY_VER);
1995 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1996 B43_WARN_ON(!(macctl & B43_MACCTL_RADIOLOCK));
1997 macctl &= ~B43_MACCTL_RADIOLOCK;
1998 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2001 u16 b43_radio_read16(struct b43_wldev *dev, u16 offset)
2003 struct b43_phy *phy = &dev->phy;
2005 /* Offset 1 is a 32-bit register. */
2006 B43_WARN_ON(offset == 1);
2008 switch (phy->type) {
2013 if (phy->radio_ver == 0x2053) {
2016 else if (offset < 0x80)
2018 } else if (phy->radio_ver == 0x2050) {
2029 case B43_PHYTYPE_LP:
2030 /* No adjustment required. */
2036 b43_write16(dev, B43_MMIO_RADIO_CONTROL, offset);
2037 return b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
2040 void b43_radio_write16(struct b43_wldev *dev, u16 offset, u16 val)
2042 /* Offset 1 is a 32-bit register. */
2043 B43_WARN_ON(offset == 1);
2045 b43_write16(dev, B43_MMIO_RADIO_CONTROL, offset);
2046 b43_write16(dev, B43_MMIO_RADIO_DATA_LOW, val);
2049 void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
2051 b43_radio_write16(dev, offset,
2052 b43_radio_read16(dev, offset) & mask);
2055 void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
2057 b43_radio_write16(dev, offset,
2058 b43_radio_read16(dev, offset) | set);
2061 void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
2063 b43_radio_write16(dev, offset,
2064 (b43_radio_read16(dev, offset) & mask) | set);
2067 static void b43_set_all_gains(struct b43_wldev *dev,
2068 s16 first, s16 second, s16 third)
2070 struct b43_phy *phy = &dev->phy;
2072 u16 start = 0x08, end = 0x18;
2076 if (phy->rev <= 1) {
2081 table = B43_OFDMTAB_GAINX;
2083 table = B43_OFDMTAB_GAINX_R1;
2084 for (i = 0; i < 4; i++)
2085 b43_ofdmtab_write16(dev, table, i, first);
2087 for (i = start; i < end; i++)
2088 b43_ofdmtab_write16(dev, table, i, second);
2091 tmp = ((u16) third << 14) | ((u16) third << 6);
2092 b43_phy_write(dev, 0x04A0,
2093 (b43_phy_read(dev, 0x04A0) & 0xBFBF) | tmp);
2094 b43_phy_write(dev, 0x04A1,
2095 (b43_phy_read(dev, 0x04A1) & 0xBFBF) | tmp);
2096 b43_phy_write(dev, 0x04A2,
2097 (b43_phy_read(dev, 0x04A2) & 0xBFBF) | tmp);
2099 b43_dummy_transmission(dev);
2102 static void b43_set_original_gains(struct b43_wldev *dev)
2104 struct b43_phy *phy = &dev->phy;
2107 u16 start = 0x0008, end = 0x0018;
2109 if (phy->rev <= 1) {
2114 table = B43_OFDMTAB_GAINX;
2116 table = B43_OFDMTAB_GAINX_R1;
2117 for (i = 0; i < 4; i++) {
2119 tmp |= (i & 0x0001) << 1;
2120 tmp |= (i & 0x0002) >> 1;
2122 b43_ofdmtab_write16(dev, table, i, tmp);
2125 for (i = start; i < end; i++)
2126 b43_ofdmtab_write16(dev, table, i, i - start);
2128 b43_phy_write(dev, 0x04A0,
2129 (b43_phy_read(dev, 0x04A0) & 0xBFBF) | 0x4040);
2130 b43_phy_write(dev, 0x04A1,
2131 (b43_phy_read(dev, 0x04A1) & 0xBFBF) | 0x4040);
2132 b43_phy_write(dev, 0x04A2,
2133 (b43_phy_read(dev, 0x04A2) & 0xBFBF) | 0x4000);
2134 b43_dummy_transmission(dev);
2137 /* Synthetic PU workaround */
2138 static void b43_synth_pu_workaround(struct b43_wldev *dev, u8 channel)
2140 struct b43_phy *phy = &dev->phy;
2144 if (phy->radio_ver != 0x2050 || phy->radio_rev >= 6) {
2145 /* We do not need the workaround. */
2149 if (channel <= 10) {
2150 b43_write16(dev, B43_MMIO_CHANNEL,
2151 channel2freq_bg(channel + 4));
2153 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(1));
2156 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(channel));
2159 u8 b43_radio_aci_detect(struct b43_wldev *dev, u8 channel)
2161 struct b43_phy *phy = &dev->phy;
2163 u16 saved, rssi, temp;
2166 saved = b43_phy_read(dev, 0x0403);
2167 b43_radio_selectchannel(dev, channel, 0);
2168 b43_phy_write(dev, 0x0403, (saved & 0xFFF8) | 5);
2169 if (phy->aci_hw_rssi)
2170 rssi = b43_phy_read(dev, 0x048A) & 0x3F;
2172 rssi = saved & 0x3F;
2173 /* clamp temp to signed 5bit */
2176 for (i = 0; i < 100; i++) {
2177 temp = (b43_phy_read(dev, 0x047F) >> 8) & 0x3F;
2185 b43_phy_write(dev, 0x0403, saved);
2190 u8 b43_radio_aci_scan(struct b43_wldev * dev)
2192 struct b43_phy *phy = &dev->phy;
2194 unsigned int channel = phy->channel;
2195 unsigned int i, j, start, end;
2197 if (!((phy->type == B43_PHYTYPE_G) && (phy->rev > 0)))
2201 b43_radio_lock(dev);
2202 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & 0xFFFC);
2203 b43_phy_write(dev, B43_PHY_G_CRS,
2204 b43_phy_read(dev, B43_PHY_G_CRS) & 0x7FFF);
2205 b43_set_all_gains(dev, 3, 8, 1);
2207 start = (channel - 5 > 0) ? channel - 5 : 1;
2208 end = (channel + 5 < 14) ? channel + 5 : 13;
2210 for (i = start; i <= end; i++) {
2211 if (abs(channel - i) > 2)
2212 ret[i - 1] = b43_radio_aci_detect(dev, i);
2214 b43_radio_selectchannel(dev, channel, 0);
2215 b43_phy_write(dev, 0x0802,
2216 (b43_phy_read(dev, 0x0802) & 0xFFFC) | 0x0003);
2217 b43_phy_write(dev, 0x0403, b43_phy_read(dev, 0x0403) & 0xFFF8);
2218 b43_phy_write(dev, B43_PHY_G_CRS,
2219 b43_phy_read(dev, B43_PHY_G_CRS) | 0x8000);
2220 b43_set_original_gains(dev);
2221 for (i = 0; i < 13; i++) {
2224 end = (i + 5 < 13) ? i + 5 : 13;
2225 for (j = i; j < end; j++)
2228 b43_radio_unlock(dev);
2229 b43_phy_unlock(dev);
2231 return ret[channel - 1];
2234 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2235 void b43_nrssi_hw_write(struct b43_wldev *dev, u16 offset, s16 val)
2237 b43_phy_write(dev, B43_PHY_NRSSILT_CTRL, offset);
2239 b43_phy_write(dev, B43_PHY_NRSSILT_DATA, (u16) val);
2242 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2243 s16 b43_nrssi_hw_read(struct b43_wldev *dev, u16 offset)
2247 b43_phy_write(dev, B43_PHY_NRSSILT_CTRL, offset);
2248 val = b43_phy_read(dev, B43_PHY_NRSSILT_DATA);
2253 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2254 void b43_nrssi_hw_update(struct b43_wldev *dev, u16 val)
2259 for (i = 0; i < 64; i++) {
2260 tmp = b43_nrssi_hw_read(dev, i);
2262 tmp = clamp_val(tmp, -32, 31);
2263 b43_nrssi_hw_write(dev, i, tmp);
2267 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2268 void b43_nrssi_mem_update(struct b43_wldev *dev)
2270 struct b43_phy *phy = &dev->phy;
2274 delta = 0x1F - phy->nrssi[0];
2275 for (i = 0; i < 64; i++) {
2276 tmp = (i - delta) * phy->nrssislope;
2279 tmp = clamp_val(tmp, 0, 0x3F);
2280 phy->nrssi_lt[i] = tmp;
2284 static void b43_calc_nrssi_offset(struct b43_wldev *dev)
2286 struct b43_phy *phy = &dev->phy;
2287 u16 backup[20] = { 0 };
2292 backup[0] = b43_phy_read(dev, 0x0001);
2293 backup[1] = b43_phy_read(dev, 0x0811);
2294 backup[2] = b43_phy_read(dev, 0x0812);
2295 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2296 backup[3] = b43_phy_read(dev, 0x0814);
2297 backup[4] = b43_phy_read(dev, 0x0815);
2299 backup[5] = b43_phy_read(dev, 0x005A);
2300 backup[6] = b43_phy_read(dev, 0x0059);
2301 backup[7] = b43_phy_read(dev, 0x0058);
2302 backup[8] = b43_phy_read(dev, 0x000A);
2303 backup[9] = b43_phy_read(dev, 0x0003);
2304 backup[10] = b43_radio_read16(dev, 0x007A);
2305 backup[11] = b43_radio_read16(dev, 0x0043);
2307 b43_phy_write(dev, 0x0429, b43_phy_read(dev, 0x0429) & 0x7FFF);
2308 b43_phy_write(dev, 0x0001,
2309 (b43_phy_read(dev, 0x0001) & 0x3FFF) | 0x4000);
2310 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x000C);
2311 b43_phy_write(dev, 0x0812,
2312 (b43_phy_read(dev, 0x0812) & 0xFFF3) | 0x0004);
2313 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & ~(0x1 | 0x2));
2314 if (phy->rev >= 6) {
2315 backup[12] = b43_phy_read(dev, 0x002E);
2316 backup[13] = b43_phy_read(dev, 0x002F);
2317 backup[14] = b43_phy_read(dev, 0x080F);
2318 backup[15] = b43_phy_read(dev, 0x0810);
2319 backup[16] = b43_phy_read(dev, 0x0801);
2320 backup[17] = b43_phy_read(dev, 0x0060);
2321 backup[18] = b43_phy_read(dev, 0x0014);
2322 backup[19] = b43_phy_read(dev, 0x0478);
2324 b43_phy_write(dev, 0x002E, 0);
2325 b43_phy_write(dev, 0x002F, 0);
2326 b43_phy_write(dev, 0x080F, 0);
2327 b43_phy_write(dev, 0x0810, 0);
2328 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478) | 0x0100);
2329 b43_phy_write(dev, 0x0801, b43_phy_read(dev, 0x0801) | 0x0040);
2330 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060) | 0x0040);
2331 b43_phy_write(dev, 0x0014, b43_phy_read(dev, 0x0014) | 0x0200);
2333 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0070);
2334 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0080);
2337 v47F = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2341 for (i = 7; i >= 4; i--) {
2342 b43_radio_write16(dev, 0x007B, i);
2345 (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2348 if (v47F < 31 && saved == 0xFFFF)
2351 if (saved == 0xFFFF)
2354 b43_radio_write16(dev, 0x007A,
2355 b43_radio_read16(dev, 0x007A) & 0x007F);
2356 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2357 b43_phy_write(dev, 0x0814,
2358 b43_phy_read(dev, 0x0814) | 0x0001);
2359 b43_phy_write(dev, 0x0815,
2360 b43_phy_read(dev, 0x0815) & 0xFFFE);
2362 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x000C);
2363 b43_phy_write(dev, 0x0812, b43_phy_read(dev, 0x0812) | 0x000C);
2364 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x0030);
2365 b43_phy_write(dev, 0x0812, b43_phy_read(dev, 0x0812) | 0x0030);
2366 b43_phy_write(dev, 0x005A, 0x0480);
2367 b43_phy_write(dev, 0x0059, 0x0810);
2368 b43_phy_write(dev, 0x0058, 0x000D);
2369 if (phy->rev == 0) {
2370 b43_phy_write(dev, 0x0003, 0x0122);
2372 b43_phy_write(dev, 0x000A, b43_phy_read(dev, 0x000A)
2375 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2376 b43_phy_write(dev, 0x0814,
2377 b43_phy_read(dev, 0x0814) | 0x0004);
2378 b43_phy_write(dev, 0x0815,
2379 b43_phy_read(dev, 0x0815) & 0xFFFB);
2381 b43_phy_write(dev, 0x0003, (b43_phy_read(dev, 0x0003) & 0xFF9F)
2383 b43_radio_write16(dev, 0x007A,
2384 b43_radio_read16(dev, 0x007A) | 0x000F);
2385 b43_set_all_gains(dev, 3, 0, 1);
2386 b43_radio_write16(dev, 0x0043, (b43_radio_read16(dev, 0x0043)
2387 & 0x00F0) | 0x000F);
2389 v47F = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2393 for (i = 0; i < 4; i++) {
2394 b43_radio_write16(dev, 0x007B, i);
2397 (s16) ((b43_phy_read(dev, 0x047F) >> 8) &
2401 if (v47F > -31 && saved == 0xFFFF)
2404 if (saved == 0xFFFF)
2409 b43_radio_write16(dev, 0x007B, saved);
2411 if (phy->rev >= 6) {
2412 b43_phy_write(dev, 0x002E, backup[12]);
2413 b43_phy_write(dev, 0x002F, backup[13]);
2414 b43_phy_write(dev, 0x080F, backup[14]);
2415 b43_phy_write(dev, 0x0810, backup[15]);
2417 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2418 b43_phy_write(dev, 0x0814, backup[3]);
2419 b43_phy_write(dev, 0x0815, backup[4]);
2421 b43_phy_write(dev, 0x005A, backup[5]);
2422 b43_phy_write(dev, 0x0059, backup[6]);
2423 b43_phy_write(dev, 0x0058, backup[7]);
2424 b43_phy_write(dev, 0x000A, backup[8]);
2425 b43_phy_write(dev, 0x0003, backup[9]);
2426 b43_radio_write16(dev, 0x0043, backup[11]);
2427 b43_radio_write16(dev, 0x007A, backup[10]);
2428 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x1 | 0x2);
2429 b43_phy_write(dev, 0x0429, b43_phy_read(dev, 0x0429) | 0x8000);
2430 b43_set_original_gains(dev);
2431 if (phy->rev >= 6) {
2432 b43_phy_write(dev, 0x0801, backup[16]);
2433 b43_phy_write(dev, 0x0060, backup[17]);
2434 b43_phy_write(dev, 0x0014, backup[18]);
2435 b43_phy_write(dev, 0x0478, backup[19]);
2437 b43_phy_write(dev, 0x0001, backup[0]);
2438 b43_phy_write(dev, 0x0812, backup[2]);
2439 b43_phy_write(dev, 0x0811, backup[1]);
2442 void b43_calc_nrssi_slope(struct b43_wldev *dev)
2444 struct b43_phy *phy = &dev->phy;
2445 u16 backup[18] = { 0 };
2449 switch (phy->type) {
2451 backup[0] = b43_radio_read16(dev, 0x007A);
2452 backup[1] = b43_radio_read16(dev, 0x0052);
2453 backup[2] = b43_radio_read16(dev, 0x0043);
2454 backup[3] = b43_phy_read(dev, 0x0030);
2455 backup[4] = b43_phy_read(dev, 0x0026);
2456 backup[5] = b43_phy_read(dev, 0x0015);
2457 backup[6] = b43_phy_read(dev, 0x002A);
2458 backup[7] = b43_phy_read(dev, 0x0020);
2459 backup[8] = b43_phy_read(dev, 0x005A);
2460 backup[9] = b43_phy_read(dev, 0x0059);
2461 backup[10] = b43_phy_read(dev, 0x0058);
2462 backup[11] = b43_read16(dev, 0x03E2);
2463 backup[12] = b43_read16(dev, 0x03E6);
2464 backup[13] = b43_read16(dev, B43_MMIO_CHANNEL_EXT);
2466 tmp = b43_radio_read16(dev, 0x007A);
2467 tmp &= (phy->rev >= 5) ? 0x007F : 0x000F;
2468 b43_radio_write16(dev, 0x007A, tmp);
2469 b43_phy_write(dev, 0x0030, 0x00FF);
2470 b43_write16(dev, 0x03EC, 0x7F7F);
2471 b43_phy_write(dev, 0x0026, 0x0000);
2472 b43_phy_write(dev, 0x0015, b43_phy_read(dev, 0x0015) | 0x0020);
2473 b43_phy_write(dev, 0x002A, 0x08A3);
2474 b43_radio_write16(dev, 0x007A,
2475 b43_radio_read16(dev, 0x007A) | 0x0080);
2477 nrssi0 = (s16) b43_phy_read(dev, 0x0027);
2478 b43_radio_write16(dev, 0x007A,
2479 b43_radio_read16(dev, 0x007A) & 0x007F);
2480 if (phy->rev >= 2) {
2481 b43_write16(dev, 0x03E6, 0x0040);
2482 } else if (phy->rev == 0) {
2483 b43_write16(dev, 0x03E6, 0x0122);
2485 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
2487 B43_MMIO_CHANNEL_EXT) & 0x2000);
2489 b43_phy_write(dev, 0x0020, 0x3F3F);
2490 b43_phy_write(dev, 0x0015, 0xF330);
2491 b43_radio_write16(dev, 0x005A, 0x0060);
2492 b43_radio_write16(dev, 0x0043,
2493 b43_radio_read16(dev, 0x0043) & 0x00F0);
2494 b43_phy_write(dev, 0x005A, 0x0480);
2495 b43_phy_write(dev, 0x0059, 0x0810);
2496 b43_phy_write(dev, 0x0058, 0x000D);
2499 nrssi1 = (s16) b43_phy_read(dev, 0x0027);
2500 b43_phy_write(dev, 0x0030, backup[3]);
2501 b43_radio_write16(dev, 0x007A, backup[0]);
2502 b43_write16(dev, 0x03E2, backup[11]);
2503 b43_phy_write(dev, 0x0026, backup[4]);
2504 b43_phy_write(dev, 0x0015, backup[5]);
2505 b43_phy_write(dev, 0x002A, backup[6]);
2506 b43_synth_pu_workaround(dev, phy->channel);
2508 b43_write16(dev, 0x03F4, backup[13]);
2510 b43_phy_write(dev, 0x0020, backup[7]);
2511 b43_phy_write(dev, 0x005A, backup[8]);
2512 b43_phy_write(dev, 0x0059, backup[9]);
2513 b43_phy_write(dev, 0x0058, backup[10]);
2514 b43_radio_write16(dev, 0x0052, backup[1]);
2515 b43_radio_write16(dev, 0x0043, backup[2]);
2517 if (nrssi0 == nrssi1)
2518 phy->nrssislope = 0x00010000;
2520 phy->nrssislope = 0x00400000 / (nrssi0 - nrssi1);
2523 phy->nrssi[0] = nrssi0;
2524 phy->nrssi[1] = nrssi1;
2528 if (phy->radio_rev >= 9)
2530 if (phy->radio_rev == 8)
2531 b43_calc_nrssi_offset(dev);
2533 b43_phy_write(dev, B43_PHY_G_CRS,
2534 b43_phy_read(dev, B43_PHY_G_CRS) & 0x7FFF);
2535 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & 0xFFFC);
2536 backup[7] = b43_read16(dev, 0x03E2);
2537 b43_write16(dev, 0x03E2, b43_read16(dev, 0x03E2) | 0x8000);
2538 backup[0] = b43_radio_read16(dev, 0x007A);
2539 backup[1] = b43_radio_read16(dev, 0x0052);
2540 backup[2] = b43_radio_read16(dev, 0x0043);
2541 backup[3] = b43_phy_read(dev, 0x0015);
2542 backup[4] = b43_phy_read(dev, 0x005A);
2543 backup[5] = b43_phy_read(dev, 0x0059);
2544 backup[6] = b43_phy_read(dev, 0x0058);
2545 backup[8] = b43_read16(dev, 0x03E6);
2546 backup[9] = b43_read16(dev, B43_MMIO_CHANNEL_EXT);
2547 if (phy->rev >= 3) {
2548 backup[10] = b43_phy_read(dev, 0x002E);
2549 backup[11] = b43_phy_read(dev, 0x002F);
2550 backup[12] = b43_phy_read(dev, 0x080F);
2551 backup[13] = b43_phy_read(dev, B43_PHY_G_LO_CONTROL);
2552 backup[14] = b43_phy_read(dev, 0x0801);
2553 backup[15] = b43_phy_read(dev, 0x0060);
2554 backup[16] = b43_phy_read(dev, 0x0014);
2555 backup[17] = b43_phy_read(dev, 0x0478);
2556 b43_phy_write(dev, 0x002E, 0);
2557 b43_phy_write(dev, B43_PHY_G_LO_CONTROL, 0);
2562 b43_phy_write(dev, 0x0478,
2563 b43_phy_read(dev, 0x0478)
2565 b43_phy_write(dev, 0x0801,
2566 b43_phy_read(dev, 0x0801)
2571 b43_phy_write(dev, 0x0801,
2572 b43_phy_read(dev, 0x0801)
2576 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060)
2578 b43_phy_write(dev, 0x0014, b43_phy_read(dev, 0x0014)
2581 b43_radio_write16(dev, 0x007A,
2582 b43_radio_read16(dev, 0x007A) | 0x0070);
2583 b43_set_all_gains(dev, 0, 8, 0);
2584 b43_radio_write16(dev, 0x007A,
2585 b43_radio_read16(dev, 0x007A) & 0x00F7);
2586 if (phy->rev >= 2) {
2587 b43_phy_write(dev, 0x0811,
2588 (b43_phy_read(dev, 0x0811) & 0xFFCF) |
2590 b43_phy_write(dev, 0x0812,
2591 (b43_phy_read(dev, 0x0812) & 0xFFCF) |
2594 b43_radio_write16(dev, 0x007A,
2595 b43_radio_read16(dev, 0x007A) | 0x0080);
2598 nrssi0 = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2599 if (nrssi0 >= 0x0020)
2602 b43_radio_write16(dev, 0x007A,
2603 b43_radio_read16(dev, 0x007A) & 0x007F);
2604 if (phy->rev >= 2) {
2605 b43_phy_write(dev, 0x0003, (b43_phy_read(dev, 0x0003)
2606 & 0xFF9F) | 0x0040);
2609 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
2610 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
2612 b43_radio_write16(dev, 0x007A,
2613 b43_radio_read16(dev, 0x007A) | 0x000F);
2614 b43_phy_write(dev, 0x0015, 0xF330);
2615 if (phy->rev >= 2) {
2616 b43_phy_write(dev, 0x0812,
2617 (b43_phy_read(dev, 0x0812) & 0xFFCF) |
2619 b43_phy_write(dev, 0x0811,
2620 (b43_phy_read(dev, 0x0811) & 0xFFCF) |
2624 b43_set_all_gains(dev, 3, 0, 1);
2625 if (phy->radio_rev == 8) {
2626 b43_radio_write16(dev, 0x0043, 0x001F);
2628 tmp = b43_radio_read16(dev, 0x0052) & 0xFF0F;
2629 b43_radio_write16(dev, 0x0052, tmp | 0x0060);
2630 tmp = b43_radio_read16(dev, 0x0043) & 0xFFF0;
2631 b43_radio_write16(dev, 0x0043, tmp | 0x0009);
2633 b43_phy_write(dev, 0x005A, 0x0480);
2634 b43_phy_write(dev, 0x0059, 0x0810);
2635 b43_phy_write(dev, 0x0058, 0x000D);
2637 nrssi1 = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2638 if (nrssi1 >= 0x0020)
2640 if (nrssi0 == nrssi1)
2641 phy->nrssislope = 0x00010000;
2643 phy->nrssislope = 0x00400000 / (nrssi0 - nrssi1);
2645 phy->nrssi[0] = nrssi1;
2646 phy->nrssi[1] = nrssi0;
2648 if (phy->rev >= 3) {
2649 b43_phy_write(dev, 0x002E, backup[10]);
2650 b43_phy_write(dev, 0x002F, backup[11]);
2651 b43_phy_write(dev, 0x080F, backup[12]);
2652 b43_phy_write(dev, B43_PHY_G_LO_CONTROL, backup[13]);
2654 if (phy->rev >= 2) {
2655 b43_phy_write(dev, 0x0812,
2656 b43_phy_read(dev, 0x0812) & 0xFFCF);
2657 b43_phy_write(dev, 0x0811,
2658 b43_phy_read(dev, 0x0811) & 0xFFCF);
2661 b43_radio_write16(dev, 0x007A, backup[0]);
2662 b43_radio_write16(dev, 0x0052, backup[1]);
2663 b43_radio_write16(dev, 0x0043, backup[2]);
2664 b43_write16(dev, 0x03E2, backup[7]);
2665 b43_write16(dev, 0x03E6, backup[8]);
2666 b43_write16(dev, B43_MMIO_CHANNEL_EXT, backup[9]);
2667 b43_phy_write(dev, 0x0015, backup[3]);
2668 b43_phy_write(dev, 0x005A, backup[4]);
2669 b43_phy_write(dev, 0x0059, backup[5]);
2670 b43_phy_write(dev, 0x0058, backup[6]);
2671 b43_synth_pu_workaround(dev, phy->channel);
2672 b43_phy_write(dev, 0x0802,
2673 b43_phy_read(dev, 0x0802) | (0x0001 | 0x0002));
2674 b43_set_original_gains(dev);
2675 b43_phy_write(dev, B43_PHY_G_CRS,
2676 b43_phy_read(dev, B43_PHY_G_CRS) | 0x8000);
2677 if (phy->rev >= 3) {
2678 b43_phy_write(dev, 0x0801, backup[14]);
2679 b43_phy_write(dev, 0x0060, backup[15]);
2680 b43_phy_write(dev, 0x0014, backup[16]);
2681 b43_phy_write(dev, 0x0478, backup[17]);
2683 b43_nrssi_mem_update(dev);
2684 b43_calc_nrssi_threshold(dev);
2691 void b43_calc_nrssi_threshold(struct b43_wldev *dev)
2693 struct b43_phy *phy = &dev->phy;
2699 switch (phy->type) {
2700 case B43_PHYTYPE_B:{
2701 if (phy->radio_ver != 0x2050)
2704 (dev->dev->bus->sprom.
2705 boardflags_lo & B43_BFL_RSSI))
2708 if (phy->radio_rev >= 6) {
2710 (phy->nrssi[1] - phy->nrssi[0]) * 32;
2711 threshold += 20 * (phy->nrssi[0] + 1);
2714 threshold = phy->nrssi[1] - 5;
2716 threshold = clamp_val(threshold, 0, 0x3E);
2717 b43_phy_read(dev, 0x0020); /* dummy read */
2718 b43_phy_write(dev, 0x0020,
2719 (((u16) threshold) << 8) | 0x001C);
2721 if (phy->radio_rev >= 6) {
2722 b43_phy_write(dev, 0x0087, 0x0E0D);
2723 b43_phy_write(dev, 0x0086, 0x0C0B);
2724 b43_phy_write(dev, 0x0085, 0x0A09);
2725 b43_phy_write(dev, 0x0084, 0x0808);
2726 b43_phy_write(dev, 0x0083, 0x0808);
2727 b43_phy_write(dev, 0x0082, 0x0604);
2728 b43_phy_write(dev, 0x0081, 0x0302);
2729 b43_phy_write(dev, 0x0080, 0x0100);
2735 !(dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI)) {
2736 tmp16 = b43_nrssi_hw_read(dev, 0x20);
2740 b43_phy_write(dev, 0x048A,
2741 (b43_phy_read(dev, 0x048A)
2742 & 0xF000) | 0x09EB);
2744 b43_phy_write(dev, 0x048A,
2745 (b43_phy_read(dev, 0x048A)
2746 & 0xF000) | 0x0AED);
2749 if (phy->interfmode == B43_INTERFMODE_NONWLAN) {
2752 } else if (!phy->aci_wlan_automatic && phy->aci_enable) {
2760 a = a * (phy->nrssi[1] - phy->nrssi[0]);
2761 a += (phy->nrssi[0] << 6);
2767 a = clamp_val(a, -31, 31);
2769 b = b * (phy->nrssi[1] - phy->nrssi[0]);
2770 b += (phy->nrssi[0] << 6);
2776 b = clamp_val(b, -31, 31);
2778 tmp_u16 = b43_phy_read(dev, 0x048A) & 0xF000;
2779 tmp_u16 |= ((u32) b & 0x0000003F);
2780 tmp_u16 |= (((u32) a & 0x0000003F) << 6);
2781 b43_phy_write(dev, 0x048A, tmp_u16);
2789 /* Stack implementation to save/restore values from the
2790 * interference mitigation code.
2791 * It is save to restore values in random order.
2793 static void _stack_save(u32 * _stackptr, size_t * stackidx,
2794 u8 id, u16 offset, u16 value)
2796 u32 *stackptr = &(_stackptr[*stackidx]);
2798 B43_WARN_ON(offset & 0xF000);
2799 B43_WARN_ON(id & 0xF0);
2801 *stackptr |= ((u32) id) << 12;
2802 *stackptr |= ((u32) value) << 16;
2804 B43_WARN_ON(*stackidx >= B43_INTERFSTACK_SIZE);
2807 static u16 _stack_restore(u32 * stackptr, u8 id, u16 offset)
2811 B43_WARN_ON(offset & 0xF000);
2812 B43_WARN_ON(id & 0xF0);
2813 for (i = 0; i < B43_INTERFSTACK_SIZE; i++, stackptr++) {
2814 if ((*stackptr & 0x00000FFF) != offset)
2816 if (((*stackptr & 0x0000F000) >> 12) != id)
2818 return ((*stackptr & 0xFFFF0000) >> 16);
2825 #define phy_stacksave(offset) \
2827 _stack_save(stack, &stackidx, 0x1, (offset), \
2828 b43_phy_read(dev, (offset))); \
2830 #define phy_stackrestore(offset) \
2832 b43_phy_write(dev, (offset), \
2833 _stack_restore(stack, 0x1, \
2836 #define radio_stacksave(offset) \
2838 _stack_save(stack, &stackidx, 0x2, (offset), \
2839 b43_radio_read16(dev, (offset))); \
2841 #define radio_stackrestore(offset) \
2843 b43_radio_write16(dev, (offset), \
2844 _stack_restore(stack, 0x2, \
2847 #define ofdmtab_stacksave(table, offset) \
2849 _stack_save(stack, &stackidx, 0x3, (offset)|(table), \
2850 b43_ofdmtab_read16(dev, (table), (offset))); \
2852 #define ofdmtab_stackrestore(table, offset) \
2854 b43_ofdmtab_write16(dev, (table), (offset), \
2855 _stack_restore(stack, 0x3, \
2856 (offset)|(table))); \
2860 b43_radio_interference_mitigation_enable(struct b43_wldev *dev, int mode)
2862 struct b43_phy *phy = &dev->phy;
2864 size_t stackidx = 0;
2865 u32 *stack = phy->interfstack;
2868 case B43_INTERFMODE_NONWLAN:
2869 if (phy->rev != 1) {
2870 b43_phy_write(dev, 0x042B,
2871 b43_phy_read(dev, 0x042B) | 0x0800);
2872 b43_phy_write(dev, B43_PHY_G_CRS,
2874 B43_PHY_G_CRS) & ~0x4000);
2877 radio_stacksave(0x0078);
2878 tmp = (b43_radio_read16(dev, 0x0078) & 0x001E);
2879 B43_WARN_ON(tmp > 15);
2880 flipped = bitrev4(tmp);
2881 if (flipped < 10 && flipped >= 8)
2883 else if (flipped >= 10)
2885 flipped = (bitrev4(flipped) << 1) | 0x0020;
2886 b43_radio_write16(dev, 0x0078, flipped);
2888 b43_calc_nrssi_threshold(dev);
2890 phy_stacksave(0x0406);
2891 b43_phy_write(dev, 0x0406, 0x7E28);
2893 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x0800);
2894 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
2896 B43_PHY_RADIO_BITFIELD) | 0x1000);
2898 phy_stacksave(0x04A0);
2899 b43_phy_write(dev, 0x04A0,
2900 (b43_phy_read(dev, 0x04A0) & 0xC0C0) | 0x0008);
2901 phy_stacksave(0x04A1);
2902 b43_phy_write(dev, 0x04A1,
2903 (b43_phy_read(dev, 0x04A1) & 0xC0C0) | 0x0605);
2904 phy_stacksave(0x04A2);
2905 b43_phy_write(dev, 0x04A2,
2906 (b43_phy_read(dev, 0x04A2) & 0xC0C0) | 0x0204);
2907 phy_stacksave(0x04A8);
2908 b43_phy_write(dev, 0x04A8,
2909 (b43_phy_read(dev, 0x04A8) & 0xC0C0) | 0x0803);
2910 phy_stacksave(0x04AB);
2911 b43_phy_write(dev, 0x04AB,
2912 (b43_phy_read(dev, 0x04AB) & 0xC0C0) | 0x0605);
2914 phy_stacksave(0x04A7);
2915 b43_phy_write(dev, 0x04A7, 0x0002);
2916 phy_stacksave(0x04A3);
2917 b43_phy_write(dev, 0x04A3, 0x287A);
2918 phy_stacksave(0x04A9);
2919 b43_phy_write(dev, 0x04A9, 0x2027);
2920 phy_stacksave(0x0493);
2921 b43_phy_write(dev, 0x0493, 0x32F5);
2922 phy_stacksave(0x04AA);
2923 b43_phy_write(dev, 0x04AA, 0x2027);
2924 phy_stacksave(0x04AC);
2925 b43_phy_write(dev, 0x04AC, 0x32F5);
2927 case B43_INTERFMODE_MANUALWLAN:
2928 if (b43_phy_read(dev, 0x0033) & 0x0800)
2931 phy->aci_enable = 1;
2933 phy_stacksave(B43_PHY_RADIO_BITFIELD);
2934 phy_stacksave(B43_PHY_G_CRS);
2936 phy_stacksave(0x0406);
2938 phy_stacksave(0x04C0);
2939 phy_stacksave(0x04C1);
2941 phy_stacksave(0x0033);
2942 phy_stacksave(0x04A7);
2943 phy_stacksave(0x04A3);
2944 phy_stacksave(0x04A9);
2945 phy_stacksave(0x04AA);
2946 phy_stacksave(0x04AC);
2947 phy_stacksave(0x0493);
2948 phy_stacksave(0x04A1);
2949 phy_stacksave(0x04A0);
2950 phy_stacksave(0x04A2);
2951 phy_stacksave(0x048A);
2952 phy_stacksave(0x04A8);
2953 phy_stacksave(0x04AB);
2954 if (phy->rev == 2) {
2955 phy_stacksave(0x04AD);
2956 phy_stacksave(0x04AE);
2957 } else if (phy->rev >= 3) {
2958 phy_stacksave(0x04AD);
2959 phy_stacksave(0x0415);
2960 phy_stacksave(0x0416);
2961 phy_stacksave(0x0417);
2962 ofdmtab_stacksave(0x1A00, 0x2);
2963 ofdmtab_stacksave(0x1A00, 0x3);
2965 phy_stacksave(0x042B);
2966 phy_stacksave(0x048C);
2968 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
2969 b43_phy_read(dev, B43_PHY_RADIO_BITFIELD)
2971 b43_phy_write(dev, B43_PHY_G_CRS,
2972 (b43_phy_read(dev, B43_PHY_G_CRS)
2973 & 0xFFFC) | 0x0002);
2975 b43_phy_write(dev, 0x0033, 0x0800);
2976 b43_phy_write(dev, 0x04A3, 0x2027);
2977 b43_phy_write(dev, 0x04A9, 0x1CA8);
2978 b43_phy_write(dev, 0x0493, 0x287A);
2979 b43_phy_write(dev, 0x04AA, 0x1CA8);
2980 b43_phy_write(dev, 0x04AC, 0x287A);
2982 b43_phy_write(dev, 0x04A0, (b43_phy_read(dev, 0x04A0)
2983 & 0xFFC0) | 0x001A);
2984 b43_phy_write(dev, 0x04A7, 0x000D);
2987 b43_phy_write(dev, 0x0406, 0xFF0D);
2988 } else if (phy->rev == 2) {
2989 b43_phy_write(dev, 0x04C0, 0xFFFF);
2990 b43_phy_write(dev, 0x04C1, 0x00A9);
2992 b43_phy_write(dev, 0x04C0, 0x00C1);
2993 b43_phy_write(dev, 0x04C1, 0x0059);
2996 b43_phy_write(dev, 0x04A1, (b43_phy_read(dev, 0x04A1)
2997 & 0xC0FF) | 0x1800);
2998 b43_phy_write(dev, 0x04A1, (b43_phy_read(dev, 0x04A1)
2999 & 0xFFC0) | 0x0015);
3000 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3001 & 0xCFFF) | 0x1000);
3002 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3003 & 0xF0FF) | 0x0A00);
3004 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3005 & 0xCFFF) | 0x1000);
3006 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3007 & 0xF0FF) | 0x0800);
3008 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3009 & 0xFFCF) | 0x0010);
3010 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3011 & 0xFFF0) | 0x0005);
3012 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3013 & 0xFFCF) | 0x0010);
3014 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3015 & 0xFFF0) | 0x0006);
3016 b43_phy_write(dev, 0x04A2, (b43_phy_read(dev, 0x04A2)
3017 & 0xF0FF) | 0x0800);
3018 b43_phy_write(dev, 0x04A0, (b43_phy_read(dev, 0x04A0)
3019 & 0xF0FF) | 0x0500);
3020 b43_phy_write(dev, 0x04A2, (b43_phy_read(dev, 0x04A2)
3021 & 0xFFF0) | 0x000B);
3023 if (phy->rev >= 3) {
3024 b43_phy_write(dev, 0x048A, b43_phy_read(dev, 0x048A)
3026 b43_phy_write(dev, 0x0415, (b43_phy_read(dev, 0x0415)
3027 & 0x8000) | 0x36D8);
3028 b43_phy_write(dev, 0x0416, (b43_phy_read(dev, 0x0416)
3029 & 0x8000) | 0x36D8);
3030 b43_phy_write(dev, 0x0417, (b43_phy_read(dev, 0x0417)
3031 & 0xFE00) | 0x016D);
3033 b43_phy_write(dev, 0x048A, b43_phy_read(dev, 0x048A)
3035 b43_phy_write(dev, 0x048A, (b43_phy_read(dev, 0x048A)
3036 & 0x9FFF) | 0x2000);
3037 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_ACIW);
3039 if (phy->rev >= 2) {
3040 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B)
3043 b43_phy_write(dev, 0x048C, (b43_phy_read(dev, 0x048C)
3044 & 0xF0FF) | 0x0200);
3045 if (phy->rev == 2) {
3046 b43_phy_write(dev, 0x04AE, (b43_phy_read(dev, 0x04AE)
3047 & 0xFF00) | 0x007F);
3048 b43_phy_write(dev, 0x04AD, (b43_phy_read(dev, 0x04AD)
3049 & 0x00FF) | 0x1300);
3050 } else if (phy->rev >= 6) {
3051 b43_ofdmtab_write16(dev, 0x1A00, 0x3, 0x007F);
3052 b43_ofdmtab_write16(dev, 0x1A00, 0x2, 0x007F);
3053 b43_phy_write(dev, 0x04AD, b43_phy_read(dev, 0x04AD)
3056 b43_calc_nrssi_slope(dev);
3064 b43_radio_interference_mitigation_disable(struct b43_wldev *dev, int mode)
3066 struct b43_phy *phy = &dev->phy;
3067 u32 *stack = phy->interfstack;
3070 case B43_INTERFMODE_NONWLAN:
3071 if (phy->rev != 1) {
3072 b43_phy_write(dev, 0x042B,
3073 b43_phy_read(dev, 0x042B) & ~0x0800);
3074 b43_phy_write(dev, B43_PHY_G_CRS,
3076 B43_PHY_G_CRS) | 0x4000);
3079 radio_stackrestore(0x0078);
3080 b43_calc_nrssi_threshold(dev);
3081 phy_stackrestore(0x0406);
3082 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) & ~0x0800);
3083 if (!dev->bad_frames_preempt) {
3084 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
3085 b43_phy_read(dev, B43_PHY_RADIO_BITFIELD)
3088 b43_phy_write(dev, B43_PHY_G_CRS,
3089 b43_phy_read(dev, B43_PHY_G_CRS) | 0x4000);
3090 phy_stackrestore(0x04A0);
3091 phy_stackrestore(0x04A1);
3092 phy_stackrestore(0x04A2);
3093 phy_stackrestore(0x04A8);
3094 phy_stackrestore(0x04AB);
3095 phy_stackrestore(0x04A7);
3096 phy_stackrestore(0x04A3);
3097 phy_stackrestore(0x04A9);
3098 phy_stackrestore(0x0493);
3099 phy_stackrestore(0x04AA);
3100 phy_stackrestore(0x04AC);
3102 case B43_INTERFMODE_MANUALWLAN:
3103 if (!(b43_phy_read(dev, 0x0033) & 0x0800))
3106 phy->aci_enable = 0;
3108 phy_stackrestore(B43_PHY_RADIO_BITFIELD);
3109 phy_stackrestore(B43_PHY_G_CRS);
3110 phy_stackrestore(0x0033);
3111 phy_stackrestore(0x04A3);
3112 phy_stackrestore(0x04A9);
3113 phy_stackrestore(0x0493);
3114 phy_stackrestore(0x04AA);
3115 phy_stackrestore(0x04AC);
3116 phy_stackrestore(0x04A0);
3117 phy_stackrestore(0x04A7);
3118 if (phy->rev >= 2) {
3119 phy_stackrestore(0x04C0);
3120 phy_stackrestore(0x04C1);
3122 phy_stackrestore(0x0406);
3123 phy_stackrestore(0x04A1);
3124 phy_stackrestore(0x04AB);
3125 phy_stackrestore(0x04A8);
3126 if (phy->rev == 2) {
3127 phy_stackrestore(0x04AD);
3128 phy_stackrestore(0x04AE);
3129 } else if (phy->rev >= 3) {
3130 phy_stackrestore(0x04AD);
3131 phy_stackrestore(0x0415);
3132 phy_stackrestore(0x0416);
3133 phy_stackrestore(0x0417);
3134 ofdmtab_stackrestore(0x1A00, 0x2);
3135 ofdmtab_stackrestore(0x1A00, 0x3);
3137 phy_stackrestore(0x04A2);
3138 phy_stackrestore(0x048A);
3139 phy_stackrestore(0x042B);
3140 phy_stackrestore(0x048C);
3141 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_ACIW);
3142 b43_calc_nrssi_slope(dev);
3149 #undef phy_stacksave
3150 #undef phy_stackrestore
3151 #undef radio_stacksave
3152 #undef radio_stackrestore
3153 #undef ofdmtab_stacksave
3154 #undef ofdmtab_stackrestore
3156 int b43_radio_set_interference_mitigation(struct b43_wldev *dev, int mode)
3158 struct b43_phy *phy = &dev->phy;
3161 if ((phy->type != B43_PHYTYPE_G) || (phy->rev == 0) || (!phy->gmode))
3164 phy->aci_wlan_automatic = 0;
3166 case B43_INTERFMODE_AUTOWLAN:
3167 phy->aci_wlan_automatic = 1;
3168 if (phy->aci_enable)
3169 mode = B43_INTERFMODE_MANUALWLAN;
3171 mode = B43_INTERFMODE_NONE;
3173 case B43_INTERFMODE_NONE:
3174 case B43_INTERFMODE_NONWLAN:
3175 case B43_INTERFMODE_MANUALWLAN:
3181 currentmode = phy->interfmode;
3182 if (currentmode == mode)
3184 if (currentmode != B43_INTERFMODE_NONE)
3185 b43_radio_interference_mitigation_disable(dev, currentmode);
3187 if (mode == B43_INTERFMODE_NONE) {
3188 phy->aci_enable = 0;
3189 phy->aci_hw_rssi = 0;
3191 b43_radio_interference_mitigation_enable(dev, mode);
3192 phy->interfmode = mode;
3197 static u16 b43_radio_core_calibration_value(struct b43_wldev *dev)
3199 u16 reg, index, ret;
3201 static const u8 rcc_table[] = {
3202 0x02, 0x03, 0x01, 0x0F,
3203 0x06, 0x07, 0x05, 0x0F,
3204 0x0A, 0x0B, 0x09, 0x0F,
3205 0x0E, 0x0F, 0x0D, 0x0F,
3208 reg = b43_radio_read16(dev, 0x60);
3209 index = (reg & 0x001E) >> 1;
3210 ret = rcc_table[index] << 1;
3211 ret |= (reg & 0x0001);
3217 #define LPD(L, P, D) (((L) << 2) | ((P) << 1) | ((D) << 0))
3218 static u16 radio2050_rfover_val(struct b43_wldev *dev,
3219 u16 phy_register, unsigned int lpd)
3221 struct b43_phy *phy = &dev->phy;
3222 struct ssb_sprom *sprom = &(dev->dev->bus->sprom);
3227 if (has_loopback_gain(phy)) {
3228 int max_lb_gain = phy->max_lb_gain;
3232 if (phy->radio_rev == 8)
3233 max_lb_gain += 0x3E;
3235 max_lb_gain += 0x26;
3236 if (max_lb_gain >= 0x46) {
3238 max_lb_gain -= 0x46;
3239 } else if (max_lb_gain >= 0x3A) {
3241 max_lb_gain -= 0x3A;
3242 } else if (max_lb_gain >= 0x2E) {
3244 max_lb_gain -= 0x2E;
3247 max_lb_gain -= 0x10;
3250 for (i = 0; i < 16; i++) {
3251 max_lb_gain -= (i * 6);
3252 if (max_lb_gain < 6)
3256 if ((phy->rev < 7) ||
3257 !(sprom->boardflags_lo & B43_BFL_EXTLNA)) {
3258 if (phy_register == B43_PHY_RFOVER) {
3260 } else if (phy_register == B43_PHY_RFOVERVAL) {
3267 return (0x0092 | extlna);
3269 return (0x0093 | extlna);
3275 if (phy_register == B43_PHY_RFOVER) {
3277 } else if (phy_register == B43_PHY_RFOVERVAL) {
3285 return (0x8092 | extlna);
3287 return (0x2092 | extlna);
3289 return (0x2093 | extlna);
3296 if ((phy->rev < 7) ||
3297 !(sprom->boardflags_lo & B43_BFL_EXTLNA)) {
3298 if (phy_register == B43_PHY_RFOVER) {
3300 } else if (phy_register == B43_PHY_RFOVERVAL) {
3315 if (phy_register == B43_PHY_RFOVER) {
3317 } else if (phy_register == B43_PHY_RFOVERVAL) {
3336 struct init2050_saved_values {
3337 /* Core registers */
3341 /* Radio registers */
3354 u16 phy_analogoverval;
3362 u16 b43_radio_init2050(struct b43_wldev *dev)
3364 struct b43_phy *phy = &dev->phy;
3365 struct init2050_saved_values sav;
3370 u32 tmp1 = 0, tmp2 = 0;
3372 memset(&sav, 0, sizeof(sav)); /* get rid of "may be used uninitialized..." */
3374 sav.radio_43 = b43_radio_read16(dev, 0x43);
3375 sav.radio_51 = b43_radio_read16(dev, 0x51);
3376 sav.radio_52 = b43_radio_read16(dev, 0x52);
3377 sav.phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
3378 sav.phy_cck_5A = b43_phy_read(dev, B43_PHY_CCK(0x5A));
3379 sav.phy_cck_59 = b43_phy_read(dev, B43_PHY_CCK(0x59));
3380 sav.phy_cck_58 = b43_phy_read(dev, B43_PHY_CCK(0x58));
3382 if (phy->type == B43_PHYTYPE_B) {
3383 sav.phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
3384 sav.reg_3EC = b43_read16(dev, 0x3EC);
3386 b43_phy_write(dev, B43_PHY_CCK(0x30), 0xFF);
3387 b43_write16(dev, 0x3EC, 0x3F3F);
3388 } else if (phy->gmode || phy->rev >= 2) {
3389 sav.phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
3390 sav.phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
3391 sav.phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
3392 sav.phy_analogoverval =
3393 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
3394 sav.phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
3395 sav.phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
3397 b43_phy_write(dev, B43_PHY_ANALOGOVER,
3398 b43_phy_read(dev, B43_PHY_ANALOGOVER)
3400 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
3401 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
3403 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
3405 b43_phy_write(dev, B43_PHY_CLASSCTL,
3406 b43_phy_read(dev, B43_PHY_CLASSCTL)
3408 if (has_loopback_gain(phy)) {
3409 sav.phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
3410 sav.phy_lo_ctl = b43_phy_read(dev, B43_PHY_LO_CTL);
3413 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC020);
3415 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8020);
3416 b43_phy_write(dev, B43_PHY_LO_CTL, 0);
3419 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3420 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3422 b43_phy_write(dev, B43_PHY_RFOVER,
3423 radio2050_rfover_val(dev, B43_PHY_RFOVER, 0));
3425 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2) | 0x8000);
3427 sav.phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
3428 b43_phy_write(dev, B43_PHY_SYNCCTL, b43_phy_read(dev, B43_PHY_SYNCCTL)
3430 sav.reg_3E6 = b43_read16(dev, 0x3E6);
3431 sav.reg_3F4 = b43_read16(dev, 0x3F4);
3433 if (phy->analog == 0) {
3434 b43_write16(dev, 0x03E6, 0x0122);
3436 if (phy->analog >= 2) {
3437 b43_phy_write(dev, B43_PHY_CCK(0x03),
3438 (b43_phy_read(dev, B43_PHY_CCK(0x03))
3441 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3442 (b43_read16(dev, B43_MMIO_CHANNEL_EXT) | 0x2000));
3445 rcc = b43_radio_core_calibration_value(dev);
3447 if (phy->type == B43_PHYTYPE_B)
3448 b43_radio_write16(dev, 0x78, 0x26);
3449 if (phy->gmode || phy->rev >= 2) {
3450 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3451 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3454 b43_phy_write(dev, B43_PHY_PGACTL, 0xBFAF);
3455 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1403);
3456 if (phy->gmode || phy->rev >= 2) {
3457 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3458 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3461 b43_phy_write(dev, B43_PHY_PGACTL, 0xBFA0);
3462 b43_radio_write16(dev, 0x51, b43_radio_read16(dev, 0x51)
3464 if (phy->radio_rev == 8) {
3465 b43_radio_write16(dev, 0x43, 0x1F);
3467 b43_radio_write16(dev, 0x52, 0);
3468 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
3469 & 0xFFF0) | 0x0009);
3471 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3473 for (i = 0; i < 16; i++) {
3474 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0480);
3475 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
3476 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
3477 if (phy->gmode || phy->rev >= 2) {
3478 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3479 radio2050_rfover_val(dev,
3483 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3485 if (phy->gmode || phy->rev >= 2) {
3486 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3487 radio2050_rfover_val(dev,
3491 b43_phy_write(dev, B43_PHY_PGACTL, 0xEFB0);
3493 if (phy->gmode || phy->rev >= 2) {
3494 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3495 radio2050_rfover_val(dev,
3499 b43_phy_write(dev, B43_PHY_PGACTL, 0xFFF0);
3501 tmp1 += b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
3502 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3503 if (phy->gmode || phy->rev >= 2) {
3504 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3505 radio2050_rfover_val(dev,
3509 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3513 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3517 for (i = 0; i < 16; i++) {
3518 radio78 = (bitrev4(i) << 1) | 0x0020;
3519 b43_radio_write16(dev, 0x78, radio78);
3521 for (j = 0; j < 16; j++) {
3522 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0D80);
3523 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
3524 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
3525 if (phy->gmode || phy->rev >= 2) {
3526 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3527 radio2050_rfover_val(dev,
3532 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3534 if (phy->gmode || phy->rev >= 2) {
3535 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3536 radio2050_rfover_val(dev,
3541 b43_phy_write(dev, B43_PHY_PGACTL, 0xEFB0);
3543 if (phy->gmode || phy->rev >= 2) {
3544 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3545 radio2050_rfover_val(dev,
3550 b43_phy_write(dev, B43_PHY_PGACTL, 0xFFF0);
3552 tmp2 += b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
3553 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3554 if (phy->gmode || phy->rev >= 2) {
3555 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3556 radio2050_rfover_val(dev,
3561 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3569 /* Restore the registers */
3570 b43_phy_write(dev, B43_PHY_PGACTL, sav.phy_pgactl);
3571 b43_radio_write16(dev, 0x51, sav.radio_51);
3572 b43_radio_write16(dev, 0x52, sav.radio_52);
3573 b43_radio_write16(dev, 0x43, sav.radio_43);
3574 b43_phy_write(dev, B43_PHY_CCK(0x5A), sav.phy_cck_5A);
3575 b43_phy_write(dev, B43_PHY_CCK(0x59), sav.phy_cck_59);
3576 b43_phy_write(dev, B43_PHY_CCK(0x58), sav.phy_cck_58);
3577 b43_write16(dev, 0x3E6, sav.reg_3E6);
3578 if (phy->analog != 0)
3579 b43_write16(dev, 0x3F4, sav.reg_3F4);
3580 b43_phy_write(dev, B43_PHY_SYNCCTL, sav.phy_syncctl);
3581 b43_synth_pu_workaround(dev, phy->channel);
3582 if (phy->type == B43_PHYTYPE_B) {
3583 b43_phy_write(dev, B43_PHY_CCK(0x30), sav.phy_cck_30);
3584 b43_write16(dev, 0x3EC, sav.reg_3EC);
3585 } else if (phy->gmode) {
3586 b43_write16(dev, B43_MMIO_PHY_RADIO,
3587 b43_read16(dev, B43_MMIO_PHY_RADIO)
3589 b43_phy_write(dev, B43_PHY_RFOVER, sav.phy_rfover);
3590 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav.phy_rfoverval);
3591 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav.phy_analogover);
3592 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
3593 sav.phy_analogoverval);
3594 b43_phy_write(dev, B43_PHY_CRS0, sav.phy_crs0);
3595 b43_phy_write(dev, B43_PHY_CLASSCTL, sav.phy_classctl);
3596 if (has_loopback_gain(phy)) {
3597 b43_phy_write(dev, B43_PHY_LO_MASK, sav.phy_lo_mask);
3598 b43_phy_write(dev, B43_PHY_LO_CTL, sav.phy_lo_ctl);
3609 void b43_radio_init2060(struct b43_wldev *dev)
3613 b43_radio_write16(dev, 0x0004, 0x00C0);
3614 b43_radio_write16(dev, 0x0005, 0x0008);
3615 b43_radio_write16(dev, 0x0009, 0x0040);
3616 b43_radio_write16(dev, 0x0005, 0x00AA);
3617 b43_radio_write16(dev, 0x0032, 0x008F);
3618 b43_radio_write16(dev, 0x0006, 0x008F);
3619 b43_radio_write16(dev, 0x0034, 0x008F);
3620 b43_radio_write16(dev, 0x002C, 0x0007);
3621 b43_radio_write16(dev, 0x0082, 0x0080);
3622 b43_radio_write16(dev, 0x0080, 0x0000);
3623 b43_radio_write16(dev, 0x003F, 0x00DA);
3624 b43_radio_write16(dev, 0x0005, b43_radio_read16(dev, 0x0005) & ~0x0008);
3625 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0010);
3626 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0020);
3627 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0020);
3628 msleep(1); /* delay 400usec */
3630 b43_radio_write16(dev, 0x0081,
3631 (b43_radio_read16(dev, 0x0081) & ~0x0020) | 0x0010);
3632 msleep(1); /* delay 400usec */
3634 b43_radio_write16(dev, 0x0005,
3635 (b43_radio_read16(dev, 0x0005) & ~0x0008) | 0x0008);
3636 b43_radio_write16(dev, 0x0085, b43_radio_read16(dev, 0x0085) & ~0x0010);
3637 b43_radio_write16(dev, 0x0005, b43_radio_read16(dev, 0x0005) & ~0x0008);
3638 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0040);
3639 b43_radio_write16(dev, 0x0081,
3640 (b43_radio_read16(dev, 0x0081) & ~0x0040) | 0x0040);
3641 b43_radio_write16(dev, 0x0005,
3642 (b43_radio_read16(dev, 0x0081) & ~0x0008) | 0x0008);
3643 b43_phy_write(dev, 0x0063, 0xDDC6);
3644 b43_phy_write(dev, 0x0069, 0x07BE);
3645 b43_phy_write(dev, 0x006A, 0x0000);
3647 err = b43_radio_selectchannel(dev, B43_DEFAULT_CHANNEL_A, 0);
3653 static inline u16 freq_r3A_value(u16 frequency)
3657 if (frequency < 5091)
3659 else if (frequency < 5321)
3661 else if (frequency < 5806)
3669 void b43_radio_set_tx_iq(struct b43_wldev *dev)
3671 static const u8 data_high[5] = { 0x00, 0x40, 0x80, 0x90, 0xD0 };
3672 static const u8 data_low[5] = { 0x00, 0x01, 0x05, 0x06, 0x0A };
3673 u16 tmp = b43_radio_read16(dev, 0x001E);
3676 for (i = 0; i < 5; i++) {
3677 for (j = 0; j < 5; j++) {
3678 if (tmp == (data_high[i] << 4 | data_low[j])) {
3679 b43_phy_write(dev, 0x0069,
3680 (i - j) << 8 | 0x00C0);
3687 int b43_radio_selectchannel(struct b43_wldev *dev,
3688 u8 channel, int synthetic_pu_workaround)
3690 struct b43_phy *phy = &dev->phy;
3693 u16 channelcookie, savedcookie;
3696 if (channel == 0xFF) {
3697 switch (phy->type) {
3699 channel = B43_DEFAULT_CHANNEL_A;
3703 channel = B43_DEFAULT_CHANNEL_BG;
3706 //FIXME check if we are on 2.4GHz or 5GHz and set a default channel.
3714 /* First we set the channel radio code to prevent the
3715 * firmware from sending ghost packets.
3717 channelcookie = channel;
3718 if (0 /*FIXME on 5Ghz */)
3719 channelcookie |= 0x100;
3720 //FIXME set 40Mhz flag if required
3721 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
3722 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
3724 switch (phy->type) {
3726 if (channel > 200) {
3730 freq = channel2freq_a(channel);
3732 r8 = b43_radio_read16(dev, 0x0008);
3733 b43_write16(dev, 0x03F0, freq);
3734 b43_radio_write16(dev, 0x0008, r8);
3736 //TODO: write max channel TX power? to Radio 0x2D
3737 tmp = b43_radio_read16(dev, 0x002E);
3739 //TODO: OR tmp with the Power out estimation for this channel?
3740 b43_radio_write16(dev, 0x002E, tmp);
3742 if (freq >= 4920 && freq <= 5500) {
3744 * r8 = (((freq * 15 * 0xE1FC780F) >> 32) / 29) & 0x0F;
3745 * = (freq * 0.025862069
3747 r8 = 3 * freq / 116; /* is equal to r8 = freq * 0.025862 */
3749 b43_radio_write16(dev, 0x0007, (r8 << 4) | r8);
3750 b43_radio_write16(dev, 0x0020, (r8 << 4) | r8);
3751 b43_radio_write16(dev, 0x0021, (r8 << 4) | r8);
3752 b43_radio_write16(dev, 0x0022, (b43_radio_read16(dev, 0x0022)
3753 & 0x000F) | (r8 << 4));
3754 b43_radio_write16(dev, 0x002A, (r8 << 4));
3755 b43_radio_write16(dev, 0x002B, (r8 << 4));
3756 b43_radio_write16(dev, 0x0008, (b43_radio_read16(dev, 0x0008)
3757 & 0x00F0) | (r8 << 4));
3758 b43_radio_write16(dev, 0x0029, (b43_radio_read16(dev, 0x0029)
3759 & 0xFF0F) | 0x00B0);
3760 b43_radio_write16(dev, 0x0035, 0x00AA);
3761 b43_radio_write16(dev, 0x0036, 0x0085);
3762 b43_radio_write16(dev, 0x003A, (b43_radio_read16(dev, 0x003A)
3764 freq_r3A_value(freq));
3765 b43_radio_write16(dev, 0x003D,
3766 b43_radio_read16(dev, 0x003D) & 0x00FF);
3767 b43_radio_write16(dev, 0x0081, (b43_radio_read16(dev, 0x0081)
3768 & 0xFF7F) | 0x0080);
3769 b43_radio_write16(dev, 0x0035,
3770 b43_radio_read16(dev, 0x0035) & 0xFFEF);
3771 b43_radio_write16(dev, 0x0035, (b43_radio_read16(dev, 0x0035)
3772 & 0xFFEF) | 0x0010);
3773 b43_radio_set_tx_iq(dev);
3774 //TODO: TSSI2dbm workaround
3775 b43_phy_xmitpower(dev); //FIXME correct?
3778 if ((channel < 1) || (channel > 14)) {
3783 if (synthetic_pu_workaround)
3784 b43_synth_pu_workaround(dev, channel);
3786 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(channel));
3788 if (channel == 14) {
3789 if (dev->dev->bus->sprom.country_code ==
3790 SSB_SPROM1CCODE_JAPAN)
3792 b43_hf_read(dev) & ~B43_HF_ACPR);
3795 b43_hf_read(dev) | B43_HF_ACPR);
3796 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3797 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
3800 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3801 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
3806 err = b43_nphy_selectchannel(dev, channel);
3814 phy->channel = channel;
3815 /* Wait for the radio to tune to the channel and stabilize. */
3819 b43_shm_write16(dev, B43_SHM_SHARED,
3820 B43_SHM_SH_CHAN, savedcookie);
3825 void b43_radio_turn_on(struct b43_wldev *dev)
3827 struct b43_phy *phy = &dev->phy;
3836 switch (phy->type) {
3838 b43_radio_write16(dev, 0x0004, 0x00C0);
3839 b43_radio_write16(dev, 0x0005, 0x0008);
3840 b43_phy_write(dev, 0x0010, b43_phy_read(dev, 0x0010) & 0xFFF7);
3841 b43_phy_write(dev, 0x0011, b43_phy_read(dev, 0x0011) & 0xFFF7);
3842 b43_radio_init2060(dev);
3846 b43_phy_write(dev, 0x0015, 0x8000);
3847 b43_phy_write(dev, 0x0015, 0xCC00);
3848 b43_phy_write(dev, 0x0015, (phy->gmode ? 0x00C0 : 0x0000));
3849 if (phy->radio_off_context.valid) {
3850 /* Restore the RFover values. */
3851 b43_phy_write(dev, B43_PHY_RFOVER,
3852 phy->radio_off_context.rfover);
3853 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3854 phy->radio_off_context.rfoverval);
3855 phy->radio_off_context.valid = 0;
3857 channel = phy->channel;
3858 err = b43_radio_selectchannel(dev, B43_DEFAULT_CHANNEL_BG, 1);
3859 err |= b43_radio_selectchannel(dev, channel, 0);
3863 b43_nphy_radio_turn_on(dev);
3871 void b43_radio_turn_off(struct b43_wldev *dev, bool force)
3873 struct b43_phy *phy = &dev->phy;
3875 if (!phy->radio_on && !force)
3878 switch (phy->type) {
3880 b43_nphy_radio_turn_off(dev);
3883 b43_radio_write16(dev, 0x0004, 0x00FF);
3884 b43_radio_write16(dev, 0x0005, 0x00FB);
3885 b43_phy_write(dev, 0x0010, b43_phy_read(dev, 0x0010) | 0x0008);
3886 b43_phy_write(dev, 0x0011, b43_phy_read(dev, 0x0011) | 0x0008);
3888 case B43_PHYTYPE_G: {
3889 u16 rfover, rfoverval;
3891 rfover = b43_phy_read(dev, B43_PHY_RFOVER);
3892 rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
3894 phy->radio_off_context.rfover = rfover;
3895 phy->radio_off_context.rfoverval = rfoverval;
3896 phy->radio_off_context.valid = 1;
3898 b43_phy_write(dev, B43_PHY_RFOVER, rfover | 0x008C);
3899 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfoverval & 0xFF73);