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>
41 static const s8 b43_tssi2dbm_b_table[] = {
42 0x4D, 0x4C, 0x4B, 0x4A,
43 0x4A, 0x49, 0x48, 0x47,
44 0x47, 0x46, 0x45, 0x45,
45 0x44, 0x43, 0x42, 0x42,
46 0x41, 0x40, 0x3F, 0x3E,
47 0x3D, 0x3C, 0x3B, 0x3A,
48 0x39, 0x38, 0x37, 0x36,
49 0x35, 0x34, 0x32, 0x31,
50 0x30, 0x2F, 0x2D, 0x2C,
51 0x2B, 0x29, 0x28, 0x26,
52 0x25, 0x23, 0x21, 0x1F,
53 0x1D, 0x1A, 0x17, 0x14,
54 0x10, 0x0C, 0x06, 0x00,
60 static const s8 b43_tssi2dbm_g_table[] = {
79 const u8 b43_radio_channel_codes_bg[] = {
86 static void b43_phy_initg(struct b43_wldev *dev);
88 /* Reverse the bits of a 4bit value.
89 * Example: 1101 is flipped 1011
91 static u16 flip_4bit(u16 value)
95 B43_WARN_ON(value & ~0x000F);
97 flipped |= (value & 0x0001) << 3;
98 flipped |= (value & 0x0002) << 1;
99 flipped |= (value & 0x0004) >> 1;
100 flipped |= (value & 0x0008) >> 3;
105 static void generate_rfatt_list(struct b43_wldev *dev,
106 struct b43_rfatt_list *list)
108 struct b43_phy *phy = &dev->phy;
110 /* APHY.rev < 5 || GPHY.rev < 6 */
111 static const struct b43_rfatt rfatt_0[] = {
112 {.att = 3,.with_padmix = 0,},
113 {.att = 1,.with_padmix = 0,},
114 {.att = 5,.with_padmix = 0,},
115 {.att = 7,.with_padmix = 0,},
116 {.att = 9,.with_padmix = 0,},
117 {.att = 2,.with_padmix = 0,},
118 {.att = 0,.with_padmix = 0,},
119 {.att = 4,.with_padmix = 0,},
120 {.att = 6,.with_padmix = 0,},
121 {.att = 8,.with_padmix = 0,},
122 {.att = 1,.with_padmix = 1,},
123 {.att = 2,.with_padmix = 1,},
124 {.att = 3,.with_padmix = 1,},
125 {.att = 4,.with_padmix = 1,},
127 /* Radio.rev == 8 && Radio.version == 0x2050 */
128 static const struct b43_rfatt rfatt_1[] = {
129 {.att = 2,.with_padmix = 1,},
130 {.att = 4,.with_padmix = 1,},
131 {.att = 6,.with_padmix = 1,},
132 {.att = 8,.with_padmix = 1,},
133 {.att = 10,.with_padmix = 1,},
134 {.att = 12,.with_padmix = 1,},
135 {.att = 14,.with_padmix = 1,},
138 static const struct b43_rfatt rfatt_2[] = {
139 {.att = 0,.with_padmix = 1,},
140 {.att = 2,.with_padmix = 1,},
141 {.att = 4,.with_padmix = 1,},
142 {.att = 6,.with_padmix = 1,},
143 {.att = 8,.with_padmix = 1,},
144 {.att = 9,.with_padmix = 1,},
145 {.att = 9,.with_padmix = 1,},
148 if (!b43_has_hardware_pctl(phy)) {
150 list->list = rfatt_0;
151 list->len = ARRAY_SIZE(rfatt_0);
156 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
158 list->list = rfatt_1;
159 list->len = ARRAY_SIZE(rfatt_1);
165 list->list = rfatt_2;
166 list->len = ARRAY_SIZE(rfatt_2);
171 static void generate_bbatt_list(struct b43_wldev *dev,
172 struct b43_bbatt_list *list)
174 static const struct b43_bbatt bbatt_0[] = {
186 list->list = bbatt_0;
187 list->len = ARRAY_SIZE(bbatt_0);
192 bool b43_has_hardware_pctl(struct b43_phy *phy)
194 if (!phy->hardware_power_control)
211 static void b43_shm_clear_tssi(struct b43_wldev *dev)
213 struct b43_phy *phy = &dev->phy;
217 b43_shm_write16(dev, B43_SHM_SHARED, 0x0068, 0x7F7F);
218 b43_shm_write16(dev, B43_SHM_SHARED, 0x006a, 0x7F7F);
222 b43_shm_write16(dev, B43_SHM_SHARED, 0x0058, 0x7F7F);
223 b43_shm_write16(dev, B43_SHM_SHARED, 0x005a, 0x7F7F);
224 b43_shm_write16(dev, B43_SHM_SHARED, 0x0070, 0x7F7F);
225 b43_shm_write16(dev, B43_SHM_SHARED, 0x0072, 0x7F7F);
230 /* Lock the PHY registers against concurrent access from the microcode.
231 * This lock is nonrecursive. */
232 void b43_phy_lock(struct b43_wldev *dev)
235 B43_WARN_ON(dev->phy.phy_locked);
236 dev->phy.phy_locked = 1;
238 B43_WARN_ON(dev->dev->id.revision < 3);
240 if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
241 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
244 void b43_phy_unlock(struct b43_wldev *dev)
247 B43_WARN_ON(!dev->phy.phy_locked);
248 dev->phy.phy_locked = 0;
250 B43_WARN_ON(dev->dev->id.revision < 3);
252 if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
253 b43_power_saving_ctl_bits(dev, 0);
256 /* Different PHYs require different register routing flags.
257 * This adjusts (and does sanity checks on) the routing flags.
259 static inline u16 adjust_phyreg_for_phytype(struct b43_phy *phy,
260 u16 offset, struct b43_wldev *dev)
262 if (phy->type == B43_PHYTYPE_A) {
263 /* OFDM registers are base-registers for the A-PHY. */
264 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_OFDM_GPHY) {
265 offset &= ~B43_PHYROUTE;
266 offset |= B43_PHYROUTE_BASE;
271 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_EXT_GPHY) {
272 /* Ext-G registers are only available on G-PHYs */
273 if (phy->type != B43_PHYTYPE_G) {
274 b43err(dev->wl, "Invalid EXT-G PHY access at "
275 "0x%04X on PHY type %u\n", offset, phy->type);
279 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_N_BMODE) {
280 /* N-BMODE registers are only available on N-PHYs */
281 if (phy->type != B43_PHYTYPE_N) {
282 b43err(dev->wl, "Invalid N-BMODE PHY access at "
283 "0x%04X on PHY type %u\n", offset, phy->type);
287 #endif /* B43_DEBUG */
292 u16 b43_phy_read(struct b43_wldev * dev, u16 offset)
294 struct b43_phy *phy = &dev->phy;
296 offset = adjust_phyreg_for_phytype(phy, offset, dev);
297 b43_write16(dev, B43_MMIO_PHY_CONTROL, offset);
298 return b43_read16(dev, B43_MMIO_PHY_DATA);
301 void b43_phy_write(struct b43_wldev *dev, u16 offset, u16 val)
303 struct b43_phy *phy = &dev->phy;
305 offset = adjust_phyreg_for_phytype(phy, offset, dev);
306 b43_write16(dev, B43_MMIO_PHY_CONTROL, offset);
307 b43_write16(dev, B43_MMIO_PHY_DATA, val);
310 void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
312 b43_phy_write(dev, offset,
313 b43_phy_read(dev, offset) & mask);
316 void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
318 b43_phy_write(dev, offset,
319 b43_phy_read(dev, offset) | set);
322 void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
324 b43_phy_write(dev, offset,
325 (b43_phy_read(dev, offset) & mask) | set);
328 /* Adjust the transmission power output (G-PHY) */
329 void b43_set_txpower_g(struct b43_wldev *dev,
330 const struct b43_bbatt *bbatt,
331 const struct b43_rfatt *rfatt, u8 tx_control)
333 struct b43_phy *phy = &dev->phy;
334 struct b43_txpower_lo_control *lo = phy->lo_control;
336 u16 tx_bias, tx_magn;
340 tx_bias = lo->tx_bias;
341 tx_magn = lo->tx_magn;
342 if (unlikely(tx_bias == 0xFF))
345 /* Save the values for later */
346 phy->tx_control = tx_control;
347 memcpy(&phy->rfatt, rfatt, sizeof(*rfatt));
348 phy->rfatt.with_padmix = !!(tx_control & B43_TXCTL_TXMIX);
349 memcpy(&phy->bbatt, bbatt, sizeof(*bbatt));
351 if (b43_debug(dev, B43_DBG_XMITPOWER)) {
352 b43dbg(dev->wl, "Tuning TX-power to bbatt(%u), "
353 "rfatt(%u), tx_control(0x%02X), "
354 "tx_bias(0x%02X), tx_magn(0x%02X)\n",
355 bb, rf, tx_control, tx_bias, tx_magn);
358 b43_phy_set_baseband_attenuation(dev, bb);
359 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RFATT, rf);
360 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
361 b43_radio_write16(dev, 0x43,
362 (rf & 0x000F) | (tx_control & 0x0070));
364 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
365 & 0xFFF0) | (rf & 0x000F));
366 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
367 & ~0x0070) | (tx_control &
370 if (has_tx_magnification(phy)) {
371 b43_radio_write16(dev, 0x52, tx_magn | tx_bias);
373 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
374 & 0xFFF0) | (tx_bias & 0x000F));
376 if (phy->type == B43_PHYTYPE_G)
377 b43_lo_g_adjust(dev);
380 static void default_baseband_attenuation(struct b43_wldev *dev,
381 struct b43_bbatt *bb)
383 struct b43_phy *phy = &dev->phy;
385 if (phy->radio_ver == 0x2050 && phy->radio_rev < 6)
391 static void default_radio_attenuation(struct b43_wldev *dev,
392 struct b43_rfatt *rf)
394 struct ssb_bus *bus = dev->dev->bus;
395 struct b43_phy *phy = &dev->phy;
399 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM &&
400 bus->boardinfo.type == SSB_BOARD_BCM4309G) {
401 if (bus->boardinfo.rev < 0x43) {
404 } else if (bus->boardinfo.rev < 0x51) {
410 if (phy->type == B43_PHYTYPE_A) {
415 switch (phy->radio_ver) {
417 switch (phy->radio_rev) {
424 switch (phy->radio_rev) {
429 if (phy->type == B43_PHYTYPE_G) {
430 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
431 && bus->boardinfo.type == SSB_BOARD_BCM4309G
432 && bus->boardinfo.rev >= 30)
434 else if (bus->boardinfo.vendor ==
436 && bus->boardinfo.type ==
442 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
443 && bus->boardinfo.type == SSB_BOARD_BCM4309G
444 && bus->boardinfo.rev >= 30)
451 if (phy->type == B43_PHYTYPE_G) {
452 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM
453 && bus->boardinfo.type == SSB_BOARD_BCM4309G
454 && bus->boardinfo.rev >= 30)
456 else if (bus->boardinfo.vendor ==
458 && bus->boardinfo.type ==
461 else if (bus->chip_id == 0x4320)
492 static u16 default_tx_control(struct b43_wldev *dev)
494 struct b43_phy *phy = &dev->phy;
496 if (phy->radio_ver != 0x2050)
498 if (phy->radio_rev == 1)
499 return B43_TXCTL_PA2DB | B43_TXCTL_TXMIX;
500 if (phy->radio_rev < 6)
501 return B43_TXCTL_PA2DB;
502 if (phy->radio_rev == 8)
503 return B43_TXCTL_TXMIX;
507 /* This func is called "PHY calibrate" in the specs... */
508 void b43_phy_early_init(struct b43_wldev *dev)
510 struct b43_phy *phy = &dev->phy;
511 struct b43_txpower_lo_control *lo = phy->lo_control;
513 default_baseband_attenuation(dev, &phy->bbatt);
514 default_radio_attenuation(dev, &phy->rfatt);
515 phy->tx_control = (default_tx_control(dev) << 4);
517 /* Commit previous writes */
518 b43_read32(dev, B43_MMIO_MACCTL);
520 if (phy->type == B43_PHYTYPE_B || phy->type == B43_PHYTYPE_G) {
521 generate_rfatt_list(dev, &lo->rfatt_list);
522 generate_bbatt_list(dev, &lo->bbatt_list);
524 if (phy->type == B43_PHYTYPE_G && phy->rev == 1) {
525 /* Workaround: Temporarly disable gmode through the early init
526 * phase, as the gmode stuff is not needed for phy rev 1 */
528 b43_wireless_core_reset(dev, 0);
531 b43_wireless_core_reset(dev, B43_TMSLOW_GMODE);
535 /* GPHY_TSSI_Power_Lookup_Table_Init */
536 static void b43_gphy_tssi_power_lt_init(struct b43_wldev *dev)
538 struct b43_phy *phy = &dev->phy;
542 for (i = 0; i < 32; i++)
543 b43_ofdmtab_write16(dev, 0x3C20, i, phy->tssi2dbm[i]);
544 for (i = 32; i < 64; i++)
545 b43_ofdmtab_write16(dev, 0x3C00, i - 32, phy->tssi2dbm[i]);
546 for (i = 0; i < 64; i += 2) {
547 value = (u16) phy->tssi2dbm[i];
548 value |= ((u16) phy->tssi2dbm[i + 1]) << 8;
549 b43_phy_write(dev, 0x380 + (i / 2), value);
553 /* GPHY_Gain_Lookup_Table_Init */
554 static void b43_gphy_gain_lt_init(struct b43_wldev *dev)
556 struct b43_phy *phy = &dev->phy;
557 struct b43_txpower_lo_control *lo = phy->lo_control;
562 for (rf = 0; rf < lo->rfatt_list.len; rf++) {
563 for (bb = 0; bb < lo->bbatt_list.len; bb++) {
564 if (nr_written >= 0x40)
566 tmp = lo->bbatt_list.list[bb].att;
568 if (phy->radio_rev == 8)
572 tmp |= lo->rfatt_list.list[rf].att;
573 b43_phy_write(dev, 0x3C0 + nr_written, tmp);
579 static void hardware_pctl_init_aphy(struct b43_wldev *dev)
584 static void hardware_pctl_init_gphy(struct b43_wldev *dev)
586 struct b43_phy *phy = &dev->phy;
588 b43_phy_write(dev, 0x0036, (b43_phy_read(dev, 0x0036) & 0xFFC0)
589 | (phy->tgt_idle_tssi - phy->cur_idle_tssi));
590 b43_phy_write(dev, 0x0478, (b43_phy_read(dev, 0x0478) & 0xFF00)
591 | (phy->tgt_idle_tssi - phy->cur_idle_tssi));
592 b43_gphy_tssi_power_lt_init(dev);
593 b43_gphy_gain_lt_init(dev);
594 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060) & 0xFFBF);
595 b43_phy_write(dev, 0x0014, 0x0000);
597 B43_WARN_ON(phy->rev < 6);
598 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478)
600 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478)
602 b43_phy_write(dev, 0x0801, b43_phy_read(dev, 0x0801)
605 b43_gphy_dc_lt_init(dev, 1);
608 /* HardwarePowerControl init for A and G PHY */
609 static void b43_hardware_pctl_init(struct b43_wldev *dev)
611 struct b43_phy *phy = &dev->phy;
613 if (!b43_has_hardware_pctl(phy)) {
614 /* No hardware power control */
615 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_HWPCTL);
618 /* Init the hwpctl related hardware */
621 hardware_pctl_init_aphy(dev);
624 hardware_pctl_init_gphy(dev);
629 /* Enable hardware pctl in firmware. */
630 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_HWPCTL);
633 static void b43_hardware_pctl_early_init(struct b43_wldev *dev)
635 struct b43_phy *phy = &dev->phy;
637 if (!b43_has_hardware_pctl(phy)) {
638 b43_phy_write(dev, 0x047A, 0xC111);
642 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036) & 0xFEFF);
643 b43_phy_write(dev, 0x002F, 0x0202);
644 b43_phy_write(dev, 0x047C, b43_phy_read(dev, 0x047C) | 0x0002);
645 b43_phy_write(dev, 0x047A, b43_phy_read(dev, 0x047A) | 0xF000);
646 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8) {
647 b43_phy_write(dev, 0x047A, (b43_phy_read(dev, 0x047A)
649 b43_phy_write(dev, 0x005D, b43_phy_read(dev, 0x005D)
651 b43_phy_write(dev, 0x004E, (b43_phy_read(dev, 0x004E)
653 b43_phy_write(dev, 0x002E, 0xC07F);
654 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
657 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
659 b43_phy_write(dev, 0x0036, b43_phy_read(dev, 0x0036)
661 b43_phy_write(dev, 0x005D, b43_phy_read(dev, 0x005D)
663 b43_phy_write(dev, 0x004F, b43_phy_read(dev, 0x004F)
665 b43_phy_write(dev, 0x004E, (b43_phy_read(dev, 0x004E)
667 b43_phy_write(dev, 0x002E, 0xC07F);
668 b43_phy_write(dev, 0x047A, (b43_phy_read(dev, 0x047A)
673 /* Intialize B/G PHY power control
674 * as described in http://bcm-specs.sipsolutions.net/InitPowerControl
676 static void b43_phy_init_pctl(struct b43_wldev *dev)
678 struct ssb_bus *bus = dev->dev->bus;
679 struct b43_phy *phy = &dev->phy;
680 struct b43_rfatt old_rfatt;
681 struct b43_bbatt old_bbatt;
682 u8 old_tx_control = 0;
684 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
685 (bus->boardinfo.type == SSB_BOARD_BU4306))
688 b43_phy_write(dev, 0x0028, 0x8018);
690 /* This does something with the Analog... */
691 b43_write16(dev, B43_MMIO_PHY0, b43_read16(dev, B43_MMIO_PHY0)
694 if (phy->type == B43_PHYTYPE_G && !phy->gmode)
696 b43_hardware_pctl_early_init(dev);
697 if (phy->cur_idle_tssi == 0) {
698 if (phy->radio_ver == 0x2050 && phy->analog == 0) {
699 b43_radio_write16(dev, 0x0076,
700 (b43_radio_read16(dev, 0x0076)
703 struct b43_rfatt rfatt;
704 struct b43_bbatt bbatt;
706 memcpy(&old_rfatt, &phy->rfatt, sizeof(old_rfatt));
707 memcpy(&old_bbatt, &phy->bbatt, sizeof(old_bbatt));
708 old_tx_control = phy->tx_control;
711 if (phy->radio_rev == 8) {
713 rfatt.with_padmix = 1;
716 rfatt.with_padmix = 0;
718 b43_set_txpower_g(dev, &bbatt, &rfatt, 0);
720 b43_dummy_transmission(dev);
721 phy->cur_idle_tssi = b43_phy_read(dev, B43_PHY_ITSSI);
723 /* Current-Idle-TSSI sanity check. */
724 if (abs(phy->cur_idle_tssi - phy->tgt_idle_tssi) >= 20) {
726 "!WARNING! Idle-TSSI phy->cur_idle_tssi "
727 "measuring failed. (cur=%d, tgt=%d). Disabling TX power "
728 "adjustment.\n", phy->cur_idle_tssi,
730 phy->cur_idle_tssi = 0;
733 if (phy->radio_ver == 0x2050 && phy->analog == 0) {
734 b43_radio_write16(dev, 0x0076,
735 b43_radio_read16(dev, 0x0076)
738 b43_set_txpower_g(dev, &old_bbatt,
739 &old_rfatt, old_tx_control);
742 b43_hardware_pctl_init(dev);
743 b43_shm_clear_tssi(dev);
746 static void b43_phy_rssiagc(struct b43_wldev *dev, u8 enable)
750 if (dev->phy.rev < 3) {
752 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++) {
753 b43_ofdmtab_write16(dev,
754 B43_OFDMTAB_LNAHPFGAIN1, i, 0xFFF8);
755 b43_ofdmtab_write16(dev,
756 B43_OFDMTAB_WRSSI, i, 0xFFF8);
759 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++) {
760 b43_ofdmtab_write16(dev,
761 B43_OFDMTAB_LNAHPFGAIN1, i, b43_tab_rssiagc1[i]);
762 b43_ofdmtab_write16(dev,
763 B43_OFDMTAB_WRSSI, i, b43_tab_rssiagc1[i]);
767 for (i = 0; i < B43_TAB_RSSIAGC1_SIZE; i++)
768 b43_ofdmtab_write16(dev,
769 B43_OFDMTAB_WRSSI, i, 0x0820);
771 for (i = 0; i < B43_TAB_RSSIAGC2_SIZE; i++)
772 b43_ofdmtab_write16(dev,
773 B43_OFDMTAB_WRSSI, i, b43_tab_rssiagc2[i]);
777 static void b43_phy_ww(struct b43_wldev *dev)
779 u16 b, curr_s, best_s = 0xFFFF;
782 b43_phy_write(dev, B43_PHY_CRS0,
783 b43_phy_read(dev, B43_PHY_CRS0) & ~B43_PHY_CRS0_EN);
784 b43_phy_write(dev, B43_PHY_OFDM(0x1B),
785 b43_phy_read(dev, B43_PHY_OFDM(0x1B)) | 0x1000);
786 b43_phy_write(dev, B43_PHY_OFDM(0x82),
787 (b43_phy_read(dev, B43_PHY_OFDM(0x82)) & 0xF0FF) | 0x0300);
788 b43_radio_write16(dev, 0x0009,
789 b43_radio_read16(dev, 0x0009) | 0x0080);
790 b43_radio_write16(dev, 0x0012,
791 (b43_radio_read16(dev, 0x0012) & 0xFFFC) | 0x0002);
792 b43_wa_initgains(dev);
793 b43_phy_write(dev, B43_PHY_OFDM(0xBA), 0x3ED5);
794 b = b43_phy_read(dev, B43_PHY_PWRDOWN);
795 b43_phy_write(dev, B43_PHY_PWRDOWN, (b & 0xFFF8) | 0x0005);
796 b43_radio_write16(dev, 0x0004,
797 b43_radio_read16(dev, 0x0004) | 0x0004);
798 for (i = 0x10; i <= 0x20; i++) {
799 b43_radio_write16(dev, 0x0013, i);
800 curr_s = b43_phy_read(dev, B43_PHY_OTABLEQ) & 0x00FF;
804 } else if (curr_s >= 0x0080)
805 curr_s = 0x0100 - curr_s;
809 b43_phy_write(dev, B43_PHY_PWRDOWN, b);
810 b43_radio_write16(dev, 0x0004,
811 b43_radio_read16(dev, 0x0004) & 0xFFFB);
812 b43_radio_write16(dev, 0x0013, best_s);
813 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1_R1, 0, 0xFFEC);
814 b43_phy_write(dev, B43_PHY_OFDM(0xB7), 0x1E80);
815 b43_phy_write(dev, B43_PHY_OFDM(0xB6), 0x1C00);
816 b43_phy_write(dev, B43_PHY_OFDM(0xB5), 0x0EC0);
817 b43_phy_write(dev, B43_PHY_OFDM(0xB2), 0x00C0);
818 b43_phy_write(dev, B43_PHY_OFDM(0xB9), 0x1FFF);
819 b43_phy_write(dev, B43_PHY_OFDM(0xBB),
820 (b43_phy_read(dev, B43_PHY_OFDM(0xBB)) & 0xF000) | 0x0053);
821 b43_phy_write(dev, B43_PHY_OFDM61,
822 (b43_phy_read(dev, B43_PHY_OFDM61) & 0xFE1F) | 0x0120);
823 b43_phy_write(dev, B43_PHY_OFDM(0x13),
824 (b43_phy_read(dev, B43_PHY_OFDM(0x13)) & 0x0FFF) | 0x3000);
825 b43_phy_write(dev, B43_PHY_OFDM(0x14),
826 (b43_phy_read(dev, B43_PHY_OFDM(0x14)) & 0x0FFF) | 0x3000);
827 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 6, 0x0017);
828 for (i = 0; i < 6; i++)
829 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, i, 0x000F);
830 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0D, 0x000E);
831 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0E, 0x0011);
832 b43_ofdmtab_write16(dev, B43_OFDMTAB_AGC1, 0x0F, 0x0013);
833 b43_phy_write(dev, B43_PHY_OFDM(0x33), 0x5030);
834 b43_phy_write(dev, B43_PHY_CRS0,
835 b43_phy_read(dev, B43_PHY_CRS0) | B43_PHY_CRS0_EN);
838 /* Initialize APHY. This is also called for the GPHY in some cases. */
839 static void b43_phy_inita(struct b43_wldev *dev)
841 struct ssb_bus *bus = dev->dev->bus;
842 struct b43_phy *phy = &dev->phy;
847 if (phy->type == B43_PHYTYPE_A)
848 b43_phy_write(dev, B43_PHY_OFDM(0x1B),
849 b43_phy_read(dev, B43_PHY_OFDM(0x1B)) & ~0x1000);
850 if (b43_phy_read(dev, B43_PHY_ENCORE) & B43_PHY_ENCORE_EN)
851 b43_phy_write(dev, B43_PHY_ENCORE,
852 b43_phy_read(dev, B43_PHY_ENCORE) | 0x0010);
854 b43_phy_write(dev, B43_PHY_ENCORE,
855 b43_phy_read(dev, B43_PHY_ENCORE) & ~0x1010);
860 if (phy->type == B43_PHYTYPE_A) {
861 if (phy->gmode && (phy->rev < 3))
862 b43_phy_write(dev, 0x0034,
863 b43_phy_read(dev, 0x0034) | 0x0001);
864 b43_phy_rssiagc(dev, 0);
866 b43_phy_write(dev, B43_PHY_CRS0,
867 b43_phy_read(dev, B43_PHY_CRS0) | B43_PHY_CRS0_EN);
869 b43_radio_init2060(dev);
871 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
872 ((bus->boardinfo.type == SSB_BOARD_BU4306) ||
873 (bus->boardinfo.type == SSB_BOARD_BU4309))) {
880 hardware_pctl_init_aphy(dev);
882 //TODO: radar detection
885 if ((phy->type == B43_PHYTYPE_G) &&
886 (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL)) {
887 b43_phy_write(dev, B43_PHY_OFDM(0x6E),
888 (b43_phy_read(dev, B43_PHY_OFDM(0x6E))
893 static void b43_phy_initb5(struct b43_wldev *dev)
895 struct ssb_bus *bus = dev->dev->bus;
896 struct b43_phy *phy = &dev->phy;
900 if (phy->analog == 1) {
901 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A)
904 if ((bus->boardinfo.vendor != SSB_BOARDVENDOR_BCM) &&
905 (bus->boardinfo.type != SSB_BOARD_BU4306)) {
907 for (offset = 0x00A8; offset < 0x00C7; offset++) {
908 b43_phy_write(dev, offset, value);
912 b43_phy_write(dev, 0x0035, (b43_phy_read(dev, 0x0035) & 0xF0FF)
914 if (phy->radio_ver == 0x2050)
915 b43_phy_write(dev, 0x0038, 0x0667);
917 if (phy->gmode || phy->rev >= 2) {
918 if (phy->radio_ver == 0x2050) {
919 b43_radio_write16(dev, 0x007A,
920 b43_radio_read16(dev, 0x007A)
922 b43_radio_write16(dev, 0x0051,
923 b43_radio_read16(dev, 0x0051)
926 b43_write16(dev, B43_MMIO_PHY_RADIO, 0x0000);
928 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x0100);
929 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x2000);
931 b43_phy_write(dev, 0x001C, 0x186A);
933 b43_phy_write(dev, 0x0013,
934 (b43_phy_read(dev, 0x0013) & 0x00FF) | 0x1900);
935 b43_phy_write(dev, 0x0035,
936 (b43_phy_read(dev, 0x0035) & 0xFFC0) | 0x0064);
937 b43_phy_write(dev, 0x005D,
938 (b43_phy_read(dev, 0x005D) & 0xFF80) | 0x000A);
941 if (dev->bad_frames_preempt) {
942 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
944 B43_PHY_RADIO_BITFIELD) | (1 << 11));
947 if (phy->analog == 1) {
948 b43_phy_write(dev, 0x0026, 0xCE00);
949 b43_phy_write(dev, 0x0021, 0x3763);
950 b43_phy_write(dev, 0x0022, 0x1BC3);
951 b43_phy_write(dev, 0x0023, 0x06F9);
952 b43_phy_write(dev, 0x0024, 0x037E);
954 b43_phy_write(dev, 0x0026, 0xCC00);
955 b43_phy_write(dev, 0x0030, 0x00C6);
956 b43_write16(dev, 0x03EC, 0x3F22);
958 if (phy->analog == 1)
959 b43_phy_write(dev, 0x0020, 0x3E1C);
961 b43_phy_write(dev, 0x0020, 0x301C);
963 if (phy->analog == 0)
964 b43_write16(dev, 0x03E4, 0x3000);
966 old_channel = phy->channel;
967 /* Force to channel 7, even if not supported. */
968 b43_radio_selectchannel(dev, 7, 0);
970 if (phy->radio_ver != 0x2050) {
971 b43_radio_write16(dev, 0x0075, 0x0080);
972 b43_radio_write16(dev, 0x0079, 0x0081);
975 b43_radio_write16(dev, 0x0050, 0x0020);
976 b43_radio_write16(dev, 0x0050, 0x0023);
978 if (phy->radio_ver == 0x2050) {
979 b43_radio_write16(dev, 0x0050, 0x0020);
980 b43_radio_write16(dev, 0x005A, 0x0070);
983 b43_radio_write16(dev, 0x005B, 0x007B);
984 b43_radio_write16(dev, 0x005C, 0x00B0);
986 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0007);
988 b43_radio_selectchannel(dev, old_channel, 0);
990 b43_phy_write(dev, 0x0014, 0x0080);
991 b43_phy_write(dev, 0x0032, 0x00CA);
992 b43_phy_write(dev, 0x002A, 0x88A3);
994 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt, phy->tx_control);
996 if (phy->radio_ver == 0x2050)
997 b43_radio_write16(dev, 0x005D, 0x000D);
999 b43_write16(dev, 0x03E4, (b43_read16(dev, 0x03E4) & 0xFFC0) | 0x0004);
1002 static void b43_phy_initb6(struct b43_wldev *dev)
1004 struct b43_phy *phy = &dev->phy;
1008 b43_phy_write(dev, 0x003E, 0x817A);
1009 b43_radio_write16(dev, 0x007A,
1010 (b43_radio_read16(dev, 0x007A) | 0x0058));
1011 if (phy->radio_rev == 4 || phy->radio_rev == 5) {
1012 b43_radio_write16(dev, 0x51, 0x37);
1013 b43_radio_write16(dev, 0x52, 0x70);
1014 b43_radio_write16(dev, 0x53, 0xB3);
1015 b43_radio_write16(dev, 0x54, 0x9B);
1016 b43_radio_write16(dev, 0x5A, 0x88);
1017 b43_radio_write16(dev, 0x5B, 0x88);
1018 b43_radio_write16(dev, 0x5D, 0x88);
1019 b43_radio_write16(dev, 0x5E, 0x88);
1020 b43_radio_write16(dev, 0x7D, 0x88);
1021 b43_hf_write(dev, b43_hf_read(dev)
1022 | B43_HF_TSSIRPSMW);
1024 B43_WARN_ON(phy->radio_rev == 6 || phy->radio_rev == 7); /* We had code for these revs here... */
1025 if (phy->radio_rev == 8) {
1026 b43_radio_write16(dev, 0x51, 0);
1027 b43_radio_write16(dev, 0x52, 0x40);
1028 b43_radio_write16(dev, 0x53, 0xB7);
1029 b43_radio_write16(dev, 0x54, 0x98);
1030 b43_radio_write16(dev, 0x5A, 0x88);
1031 b43_radio_write16(dev, 0x5B, 0x6B);
1032 b43_radio_write16(dev, 0x5C, 0x0F);
1033 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_ALTIQ) {
1034 b43_radio_write16(dev, 0x5D, 0xFA);
1035 b43_radio_write16(dev, 0x5E, 0xD8);
1037 b43_radio_write16(dev, 0x5D, 0xF5);
1038 b43_radio_write16(dev, 0x5E, 0xB8);
1040 b43_radio_write16(dev, 0x0073, 0x0003);
1041 b43_radio_write16(dev, 0x007D, 0x00A8);
1042 b43_radio_write16(dev, 0x007C, 0x0001);
1043 b43_radio_write16(dev, 0x007E, 0x0008);
1046 for (offset = 0x0088; offset < 0x0098; offset++) {
1047 b43_phy_write(dev, offset, val);
1051 for (offset = 0x0098; offset < 0x00A8; offset++) {
1052 b43_phy_write(dev, offset, val);
1056 for (offset = 0x00A8; offset < 0x00C8; offset++) {
1057 b43_phy_write(dev, offset, (val & 0x3F3F));
1060 if (phy->type == B43_PHYTYPE_G) {
1061 b43_radio_write16(dev, 0x007A,
1062 b43_radio_read16(dev, 0x007A) | 0x0020);
1063 b43_radio_write16(dev, 0x0051,
1064 b43_radio_read16(dev, 0x0051) | 0x0004);
1065 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x0100);
1066 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x2000);
1067 b43_phy_write(dev, 0x5B, 0);
1068 b43_phy_write(dev, 0x5C, 0);
1071 old_channel = phy->channel;
1072 if (old_channel >= 8)
1073 b43_radio_selectchannel(dev, 1, 0);
1075 b43_radio_selectchannel(dev, 13, 0);
1077 b43_radio_write16(dev, 0x0050, 0x0020);
1078 b43_radio_write16(dev, 0x0050, 0x0023);
1080 if (phy->radio_rev < 6 || phy->radio_rev == 8) {
1081 b43_radio_write16(dev, 0x7C, (b43_radio_read16(dev, 0x7C)
1083 b43_radio_write16(dev, 0x50, 0x20);
1085 if (phy->radio_rev <= 2) {
1086 b43_radio_write16(dev, 0x7C, 0x20);
1087 b43_radio_write16(dev, 0x5A, 0x70);
1088 b43_radio_write16(dev, 0x5B, 0x7B);
1089 b43_radio_write16(dev, 0x5C, 0xB0);
1091 b43_radio_write16(dev, 0x007A,
1092 (b43_radio_read16(dev, 0x007A) & 0x00F8) | 0x0007);
1094 b43_radio_selectchannel(dev, old_channel, 0);
1096 b43_phy_write(dev, 0x0014, 0x0200);
1097 if (phy->radio_rev >= 6)
1098 b43_phy_write(dev, 0x2A, 0x88C2);
1100 b43_phy_write(dev, 0x2A, 0x8AC0);
1101 b43_phy_write(dev, 0x0038, 0x0668);
1102 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt, phy->tx_control);
1103 if (phy->radio_rev <= 5) {
1104 b43_phy_write(dev, 0x5D, (b43_phy_read(dev, 0x5D)
1105 & 0xFF80) | 0x0003);
1107 if (phy->radio_rev <= 2)
1108 b43_radio_write16(dev, 0x005D, 0x000D);
1110 if (phy->analog == 4) {
1111 b43_write16(dev, 0x3E4, 9);
1112 b43_phy_write(dev, 0x61, b43_phy_read(dev, 0x61)
1115 b43_phy_write(dev, 0x0002, (b43_phy_read(dev, 0x0002) & 0xFFC0)
1118 if (phy->type == B43_PHYTYPE_B)
1120 else if (phy->type == B43_PHYTYPE_G)
1121 b43_write16(dev, 0x03E6, 0x0);
1124 static void b43_calc_loopback_gain(struct b43_wldev *dev)
1126 struct b43_phy *phy = &dev->phy;
1127 u16 backup_phy[16] = { 0 };
1128 u16 backup_radio[3];
1130 u16 i, j, loop_i_max;
1132 u16 loop1_outer_done, loop1_inner_done;
1134 backup_phy[0] = b43_phy_read(dev, B43_PHY_CRS0);
1135 backup_phy[1] = b43_phy_read(dev, B43_PHY_CCKBBANDCFG);
1136 backup_phy[2] = b43_phy_read(dev, B43_PHY_RFOVER);
1137 backup_phy[3] = b43_phy_read(dev, B43_PHY_RFOVERVAL);
1138 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1139 backup_phy[4] = b43_phy_read(dev, B43_PHY_ANALOGOVER);
1140 backup_phy[5] = b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
1142 backup_phy[6] = b43_phy_read(dev, B43_PHY_CCK(0x5A));
1143 backup_phy[7] = b43_phy_read(dev, B43_PHY_CCK(0x59));
1144 backup_phy[8] = b43_phy_read(dev, B43_PHY_CCK(0x58));
1145 backup_phy[9] = b43_phy_read(dev, B43_PHY_CCK(0x0A));
1146 backup_phy[10] = b43_phy_read(dev, B43_PHY_CCK(0x03));
1147 backup_phy[11] = b43_phy_read(dev, B43_PHY_LO_MASK);
1148 backup_phy[12] = b43_phy_read(dev, B43_PHY_LO_CTL);
1149 backup_phy[13] = b43_phy_read(dev, B43_PHY_CCK(0x2B));
1150 backup_phy[14] = b43_phy_read(dev, B43_PHY_PGACTL);
1151 backup_phy[15] = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
1152 backup_bband = phy->bbatt.att;
1153 backup_radio[0] = b43_radio_read16(dev, 0x52);
1154 backup_radio[1] = b43_radio_read16(dev, 0x43);
1155 backup_radio[2] = b43_radio_read16(dev, 0x7A);
1157 b43_phy_write(dev, B43_PHY_CRS0,
1158 b43_phy_read(dev, B43_PHY_CRS0) & 0x3FFF);
1159 b43_phy_write(dev, B43_PHY_CCKBBANDCFG,
1160 b43_phy_read(dev, B43_PHY_CCKBBANDCFG) | 0x8000);
1161 b43_phy_write(dev, B43_PHY_RFOVER,
1162 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0002);
1163 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1164 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xFFFD);
1165 b43_phy_write(dev, B43_PHY_RFOVER,
1166 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0001);
1167 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1168 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xFFFE);
1169 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1170 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1171 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0001);
1172 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1174 B43_PHY_ANALOGOVERVAL) & 0xFFFE);
1175 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1176 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0002);
1177 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1179 B43_PHY_ANALOGOVERVAL) & 0xFFFD);
1181 b43_phy_write(dev, B43_PHY_RFOVER,
1182 b43_phy_read(dev, B43_PHY_RFOVER) | 0x000C);
1183 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1184 b43_phy_read(dev, B43_PHY_RFOVERVAL) | 0x000C);
1185 b43_phy_write(dev, B43_PHY_RFOVER,
1186 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0030);
1187 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1188 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1191 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0780);
1192 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
1193 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
1195 b43_phy_write(dev, B43_PHY_CCK(0x0A),
1196 b43_phy_read(dev, B43_PHY_CCK(0x0A)) | 0x2000);
1197 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1198 b43_phy_write(dev, B43_PHY_ANALOGOVER,
1199 b43_phy_read(dev, B43_PHY_ANALOGOVER) | 0x0004);
1200 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
1202 B43_PHY_ANALOGOVERVAL) & 0xFFFB);
1204 b43_phy_write(dev, B43_PHY_CCK(0x03),
1205 (b43_phy_read(dev, B43_PHY_CCK(0x03))
1208 if (phy->radio_rev == 8) {
1209 b43_radio_write16(dev, 0x43, 0x000F);
1211 b43_radio_write16(dev, 0x52, 0);
1212 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
1215 b43_phy_set_baseband_attenuation(dev, 11);
1218 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC020);
1220 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8020);
1221 b43_phy_write(dev, B43_PHY_LO_CTL, 0);
1223 b43_phy_write(dev, B43_PHY_CCK(0x2B),
1224 (b43_phy_read(dev, B43_PHY_CCK(0x2B))
1226 b43_phy_write(dev, B43_PHY_CCK(0x2B),
1227 (b43_phy_read(dev, B43_PHY_CCK(0x2B))
1230 b43_phy_write(dev, B43_PHY_RFOVER,
1231 b43_phy_read(dev, B43_PHY_RFOVER) | 0x0100);
1232 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1233 b43_phy_read(dev, B43_PHY_RFOVERVAL) & 0xCFFF);
1235 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA) {
1236 if (phy->rev >= 7) {
1237 b43_phy_write(dev, B43_PHY_RFOVER,
1238 b43_phy_read(dev, B43_PHY_RFOVER)
1240 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1241 b43_phy_read(dev, B43_PHY_RFOVERVAL)
1245 b43_radio_write16(dev, 0x7A, b43_radio_read16(dev, 0x7A)
1249 loop_i_max = (phy->radio_rev == 8) ? 15 : 9;
1250 for (i = 0; i < loop_i_max; i++) {
1251 for (j = 0; j < 16; j++) {
1252 b43_radio_write16(dev, 0x43, i);
1253 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1254 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1255 & 0xF0FF) | (j << 8));
1256 b43_phy_write(dev, B43_PHY_PGACTL,
1257 (b43_phy_read(dev, B43_PHY_PGACTL)
1258 & 0x0FFF) | 0xA000);
1259 b43_phy_write(dev, B43_PHY_PGACTL,
1260 b43_phy_read(dev, B43_PHY_PGACTL)
1263 if (b43_phy_read(dev, B43_PHY_LO_LEAKAGE) >= 0xDFC)
1268 loop1_outer_done = i;
1269 loop1_inner_done = j;
1271 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1272 b43_phy_read(dev, B43_PHY_RFOVERVAL)
1275 for (j = j - 8; j < 16; j++) {
1276 b43_phy_write(dev, B43_PHY_RFOVERVAL,
1277 (b43_phy_read(dev, B43_PHY_RFOVERVAL)
1278 & 0xF0FF) | (j << 8));
1279 b43_phy_write(dev, B43_PHY_PGACTL,
1280 (b43_phy_read(dev, B43_PHY_PGACTL)
1281 & 0x0FFF) | 0xA000);
1282 b43_phy_write(dev, B43_PHY_PGACTL,
1283 b43_phy_read(dev, B43_PHY_PGACTL)
1287 if (b43_phy_read(dev, B43_PHY_LO_LEAKAGE) >= 0xDFC)
1294 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
1295 b43_phy_write(dev, B43_PHY_ANALOGOVER, backup_phy[4]);
1296 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL, backup_phy[5]);
1298 b43_phy_write(dev, B43_PHY_CCK(0x5A), backup_phy[6]);
1299 b43_phy_write(dev, B43_PHY_CCK(0x59), backup_phy[7]);
1300 b43_phy_write(dev, B43_PHY_CCK(0x58), backup_phy[8]);
1301 b43_phy_write(dev, B43_PHY_CCK(0x0A), backup_phy[9]);
1302 b43_phy_write(dev, B43_PHY_CCK(0x03), backup_phy[10]);
1303 b43_phy_write(dev, B43_PHY_LO_MASK, backup_phy[11]);
1304 b43_phy_write(dev, B43_PHY_LO_CTL, backup_phy[12]);
1305 b43_phy_write(dev, B43_PHY_CCK(0x2B), backup_phy[13]);
1306 b43_phy_write(dev, B43_PHY_PGACTL, backup_phy[14]);
1308 b43_phy_set_baseband_attenuation(dev, backup_bband);
1310 b43_radio_write16(dev, 0x52, backup_radio[0]);
1311 b43_radio_write16(dev, 0x43, backup_radio[1]);
1312 b43_radio_write16(dev, 0x7A, backup_radio[2]);
1314 b43_phy_write(dev, B43_PHY_RFOVER, backup_phy[2] | 0x0003);
1316 b43_phy_write(dev, B43_PHY_RFOVER, backup_phy[2]);
1317 b43_phy_write(dev, B43_PHY_RFOVERVAL, backup_phy[3]);
1318 b43_phy_write(dev, B43_PHY_CRS0, backup_phy[0]);
1319 b43_phy_write(dev, B43_PHY_CCKBBANDCFG, backup_phy[1]);
1322 ((loop1_inner_done * 6) - (loop1_outer_done * 4)) - 11;
1323 phy->trsw_rx_gain = trsw_rx * 2;
1326 static void b43_phy_initg(struct b43_wldev *dev)
1328 struct b43_phy *phy = &dev->phy;
1332 b43_phy_initb5(dev);
1334 b43_phy_initb6(dev);
1336 if (phy->rev >= 2 || phy->gmode)
1339 if (phy->rev >= 2) {
1340 b43_phy_write(dev, B43_PHY_ANALOGOVER, 0);
1341 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL, 0);
1343 if (phy->rev == 2) {
1344 b43_phy_write(dev, B43_PHY_RFOVER, 0);
1345 b43_phy_write(dev, B43_PHY_PGACTL, 0xC0);
1348 b43_phy_write(dev, B43_PHY_RFOVER, 0x400);
1349 b43_phy_write(dev, B43_PHY_PGACTL, 0xC0);
1351 if (phy->gmode || phy->rev >= 2) {
1352 tmp = b43_phy_read(dev, B43_PHY_VERSION_OFDM);
1353 tmp &= B43_PHYVER_VERSION;
1354 if (tmp == 3 || tmp == 5) {
1355 b43_phy_write(dev, B43_PHY_OFDM(0xC2), 0x1816);
1356 b43_phy_write(dev, B43_PHY_OFDM(0xC3), 0x8006);
1359 b43_phy_write(dev, B43_PHY_OFDM(0xCC),
1360 (b43_phy_read(dev, B43_PHY_OFDM(0xCC))
1361 & 0x00FF) | 0x1F00);
1364 if ((phy->rev <= 2 && phy->gmode) || phy->rev >= 2)
1365 b43_phy_write(dev, B43_PHY_OFDM(0x7E), 0x78);
1366 if (phy->radio_rev == 8) {
1367 b43_phy_write(dev, B43_PHY_EXTG(0x01),
1368 b43_phy_read(dev, B43_PHY_EXTG(0x01))
1370 b43_phy_write(dev, B43_PHY_OFDM(0x3E),
1371 b43_phy_read(dev, B43_PHY_OFDM(0x3E))
1374 if (has_loopback_gain(phy))
1375 b43_calc_loopback_gain(dev);
1377 if (phy->radio_rev != 8) {
1378 if (phy->initval == 0xFFFF)
1379 phy->initval = b43_radio_init2050(dev);
1381 b43_radio_write16(dev, 0x0078, phy->initval);
1384 if (has_tx_magnification(phy)) {
1385 b43_radio_write16(dev, 0x52,
1386 (b43_radio_read16(dev, 0x52) & 0xFF00)
1387 | phy->lo_control->tx_bias | phy->
1388 lo_control->tx_magn);
1390 b43_radio_write16(dev, 0x52,
1391 (b43_radio_read16(dev, 0x52) & 0xFFF0)
1392 | phy->lo_control->tx_bias);
1394 if (phy->rev >= 6) {
1395 b43_phy_write(dev, B43_PHY_CCK(0x36),
1396 (b43_phy_read(dev, B43_PHY_CCK(0x36))
1397 & 0x0FFF) | (phy->lo_control->
1400 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL)
1401 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8075);
1403 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x807F);
1405 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x101);
1407 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x202);
1408 if (phy->gmode || phy->rev >= 2) {
1409 b43_lo_g_adjust(dev);
1410 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
1413 if (!(dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI)) {
1414 /* The specs state to update the NRSSI LT with
1415 * the value 0x7FFFFFFF here. I think that is some weird
1416 * compiler optimization in the original driver.
1417 * Essentially, what we do here is resetting all NRSSI LT
1418 * entries to -32 (see the limit_value() in nrssi_hw_update())
1420 b43_nrssi_hw_update(dev, 0xFFFF); //FIXME?
1421 b43_calc_nrssi_threshold(dev);
1422 } else if (phy->gmode || phy->rev >= 2) {
1423 if (phy->nrssi[0] == -1000) {
1424 B43_WARN_ON(phy->nrssi[1] != -1000);
1425 b43_calc_nrssi_slope(dev);
1427 b43_calc_nrssi_threshold(dev);
1429 if (phy->radio_rev == 8)
1430 b43_phy_write(dev, B43_PHY_EXTG(0x05), 0x3230);
1431 b43_phy_init_pctl(dev);
1432 /* FIXME: The spec says in the following if, the 0 should be replaced
1433 'if OFDM may not be used in the current locale'
1434 but OFDM is legal everywhere */
1435 if ((dev->dev->bus->chip_id == 0x4306
1436 && dev->dev->bus->chip_package == 2) || 0) {
1437 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
1439 b43_phy_write(dev, B43_PHY_OFDM(0xC3),
1440 b43_phy_read(dev, B43_PHY_OFDM(0xC3))
1445 /* Set the baseband attenuation value on chip. */
1446 void b43_phy_set_baseband_attenuation(struct b43_wldev *dev,
1447 u16 baseband_attenuation)
1449 struct b43_phy *phy = &dev->phy;
1451 if (phy->analog == 0) {
1452 b43_write16(dev, B43_MMIO_PHY0, (b43_read16(dev, B43_MMIO_PHY0)
1454 baseband_attenuation);
1455 } else if (phy->analog > 1) {
1456 b43_phy_write(dev, B43_PHY_DACCTL,
1457 (b43_phy_read(dev, B43_PHY_DACCTL)
1458 & 0xFFC3) | (baseband_attenuation << 2));
1460 b43_phy_write(dev, B43_PHY_DACCTL,
1461 (b43_phy_read(dev, B43_PHY_DACCTL)
1462 & 0xFF87) | (baseband_attenuation << 3));
1466 /* http://bcm-specs.sipsolutions.net/EstimatePowerOut
1467 * This function converts a TSSI value to dBm in Q5.2
1469 static s8 b43_phy_estimate_power_out(struct b43_wldev *dev, s8 tssi)
1471 struct b43_phy *phy = &dev->phy;
1475 tmp = (phy->tgt_idle_tssi - phy->cur_idle_tssi + tssi);
1477 switch (phy->type) {
1480 tmp = limit_value(tmp, 0x00, 0xFF);
1481 dbm = phy->tssi2dbm[tmp];
1482 //TODO: There's a FIXME on the specs
1486 tmp = limit_value(tmp, 0x00, 0x3F);
1487 dbm = phy->tssi2dbm[tmp];
1496 void b43_put_attenuation_into_ranges(struct b43_wldev *dev,
1497 int *_bbatt, int *_rfatt)
1499 int rfatt = *_rfatt;
1500 int bbatt = *_bbatt;
1501 struct b43_txpower_lo_control *lo = dev->phy.lo_control;
1503 /* Get baseband and radio attenuation values into their permitted ranges.
1504 * Radio attenuation affects power level 4 times as much as baseband. */
1506 /* Range constants */
1507 const int rf_min = lo->rfatt_list.min_val;
1508 const int rf_max = lo->rfatt_list.max_val;
1509 const int bb_min = lo->bbatt_list.min_val;
1510 const int bb_max = lo->bbatt_list.max_val;
1513 if (rfatt > rf_max && bbatt > bb_max - 4)
1514 break; /* Can not get it into ranges */
1515 if (rfatt < rf_min && bbatt < bb_min + 4)
1516 break; /* Can not get it into ranges */
1517 if (bbatt > bb_max && rfatt > rf_max - 1)
1518 break; /* Can not get it into ranges */
1519 if (bbatt < bb_min && rfatt < rf_min + 1)
1520 break; /* Can not get it into ranges */
1522 if (bbatt > bb_max) {
1527 if (bbatt < bb_min) {
1532 if (rfatt > rf_max) {
1537 if (rfatt < rf_min) {
1545 *_rfatt = limit_value(rfatt, rf_min, rf_max);
1546 *_bbatt = limit_value(bbatt, bb_min, bb_max);
1549 /* http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower */
1550 void b43_phy_xmitpower(struct b43_wldev *dev)
1552 struct ssb_bus *bus = dev->dev->bus;
1553 struct b43_phy *phy = &dev->phy;
1555 if (phy->cur_idle_tssi == 0)
1557 if ((bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
1558 (bus->boardinfo.type == SSB_BOARD_BU4306))
1560 #ifdef CONFIG_B43_DEBUG
1561 if (phy->manual_txpower_control)
1565 switch (phy->type) {
1566 case B43_PHYTYPE_A:{
1568 //TODO: Nothing for A PHYs yet :-/
1573 case B43_PHYTYPE_G:{
1578 int desired_pwr, estimated_pwr, pwr_adjust;
1579 int rfatt_delta, bbatt_delta;
1583 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x0058);
1584 v0 = (s8) (tmp & 0x00FF);
1585 v1 = (s8) ((tmp & 0xFF00) >> 8);
1586 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x005A);
1587 v2 = (s8) (tmp & 0x00FF);
1588 v3 = (s8) ((tmp & 0xFF00) >> 8);
1591 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F
1594 b43_shm_read16(dev, B43_SHM_SHARED, 0x0070);
1595 v0 = (s8) (tmp & 0x00FF);
1596 v1 = (s8) ((tmp & 0xFF00) >> 8);
1598 b43_shm_read16(dev, B43_SHM_SHARED, 0x0072);
1599 v2 = (s8) (tmp & 0x00FF);
1600 v3 = (s8) ((tmp & 0xFF00) >> 8);
1601 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F
1604 v0 = (v0 + 0x20) & 0x3F;
1605 v1 = (v1 + 0x20) & 0x3F;
1606 v2 = (v2 + 0x20) & 0x3F;
1607 v3 = (v3 + 0x20) & 0x3F;
1610 b43_shm_clear_tssi(dev);
1612 average = (v0 + v1 + v2 + v3 + 2) / 4;
1615 && (b43_shm_read16(dev, B43_SHM_SHARED, 0x005E) &
1620 b43_phy_estimate_power_out(dev, average);
1622 max_pwr = dev->dev->bus->sprom.maxpwr_bg;
1623 if ((dev->dev->bus->sprom.boardflags_lo
1624 & B43_BFL_PACTRL) && (phy->type == B43_PHYTYPE_G))
1626 if (unlikely(max_pwr <= 0)) {
1628 "Invalid max-TX-power value in SPROM.\n");
1629 max_pwr = 60; /* fake it */
1630 dev->dev->bus->sprom.maxpwr_bg = max_pwr;
1634 max_pwr = min(REG - dev->dev->bus->sprom.antennagain_bgphy - 0x6, max_pwr)
1635 where REG is the max power as per the regulatory domain
1638 /* Get desired power (in Q5.2) */
1639 desired_pwr = INT_TO_Q52(phy->power_level);
1640 /* And limit it. max_pwr already is Q5.2 */
1641 desired_pwr = limit_value(desired_pwr, 0, max_pwr);
1642 if (b43_debug(dev, B43_DBG_XMITPOWER)) {
1644 "Current TX power output: " Q52_FMT
1645 " dBm, " "Desired TX power output: "
1646 Q52_FMT " dBm\n", Q52_ARG(estimated_pwr),
1647 Q52_ARG(desired_pwr));
1650 /* Calculate the adjustment delta. */
1651 pwr_adjust = desired_pwr - estimated_pwr;
1653 /* RF attenuation delta. */
1654 rfatt_delta = ((pwr_adjust + 7) / 8);
1655 /* Lower attenuation => Bigger power output. Negate it. */
1656 rfatt_delta = -rfatt_delta;
1658 /* Baseband attenuation delta. */
1659 bbatt_delta = pwr_adjust / 2;
1660 /* Lower attenuation => Bigger power output. Negate it. */
1661 bbatt_delta = -bbatt_delta;
1662 /* RF att affects power level 4 times as much as
1663 * Baseband attennuation. Subtract it. */
1664 bbatt_delta -= 4 * rfatt_delta;
1666 /* So do we finally need to adjust something? */
1667 if ((rfatt_delta == 0) && (bbatt_delta == 0))
1670 /* Calculate the new attenuation values. */
1671 bbatt = phy->bbatt.att;
1672 bbatt += bbatt_delta;
1673 rfatt = phy->rfatt.att;
1674 rfatt += rfatt_delta;
1676 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
1677 tx_control = phy->tx_control;
1678 if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 2)) {
1680 if (tx_control == 0) {
1686 } else if (dev->dev->bus->sprom.
1689 bbatt += 4 * (rfatt - 2);
1692 } else if (rfatt > 4 && tx_control) {
1703 /* Save the control values */
1704 phy->tx_control = tx_control;
1705 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
1706 phy->rfatt.att = rfatt;
1707 phy->bbatt.att = bbatt;
1709 /* Adjust the hardware */
1711 b43_radio_lock(dev);
1712 b43_set_txpower_g(dev, &phy->bbatt, &phy->rfatt,
1714 b43_radio_unlock(dev);
1715 b43_phy_unlock(dev);
1719 b43_nphy_xmitpower(dev);
1726 static inline s32 b43_tssi2dbm_ad(s32 num, s32 den)
1731 return (num + den / 2) / den;
1735 s8 b43_tssi2dbm_entry(s8 entry[], u8 index, s16 pab0, s16 pab1, s16 pab2)
1737 s32 m1, m2, f = 256, q, delta;
1740 m1 = b43_tssi2dbm_ad(16 * pab0 + index * pab1, 32);
1741 m2 = max(b43_tssi2dbm_ad(32768 + index * pab2, 256), 1);
1745 q = b43_tssi2dbm_ad(f * 4096 -
1746 b43_tssi2dbm_ad(m2 * f, 16) * f, 2048);
1750 } while (delta >= 2);
1751 entry[index] = limit_value(b43_tssi2dbm_ad(m1 * f, 8192), -127, 128);
1755 /* http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table */
1756 int b43_phy_init_tssi2dbm_table(struct b43_wldev *dev)
1758 struct b43_phy *phy = &dev->phy;
1759 s16 pab0, pab1, pab2;
1763 if (phy->type == B43_PHYTYPE_A) {
1764 pab0 = (s16) (dev->dev->bus->sprom.pa1b0);
1765 pab1 = (s16) (dev->dev->bus->sprom.pa1b1);
1766 pab2 = (s16) (dev->dev->bus->sprom.pa1b2);
1768 pab0 = (s16) (dev->dev->bus->sprom.pa0b0);
1769 pab1 = (s16) (dev->dev->bus->sprom.pa0b1);
1770 pab2 = (s16) (dev->dev->bus->sprom.pa0b2);
1773 if ((dev->dev->bus->chip_id == 0x4301) && (phy->radio_ver != 0x2050)) {
1774 phy->tgt_idle_tssi = 0x34;
1775 phy->tssi2dbm = b43_tssi2dbm_b_table;
1779 if (pab0 != 0 && pab1 != 0 && pab2 != 0 &&
1780 pab0 != -1 && pab1 != -1 && pab2 != -1) {
1781 /* The pabX values are set in SPROM. Use them. */
1782 if (phy->type == B43_PHYTYPE_A) {
1783 if ((s8) dev->dev->bus->sprom.itssi_a != 0 &&
1784 (s8) dev->dev->bus->sprom.itssi_a != -1)
1785 phy->tgt_idle_tssi =
1786 (s8) (dev->dev->bus->sprom.itssi_a);
1788 phy->tgt_idle_tssi = 62;
1790 if ((s8) dev->dev->bus->sprom.itssi_bg != 0 &&
1791 (s8) dev->dev->bus->sprom.itssi_bg != -1)
1792 phy->tgt_idle_tssi =
1793 (s8) (dev->dev->bus->sprom.itssi_bg);
1795 phy->tgt_idle_tssi = 62;
1797 dyn_tssi2dbm = kmalloc(64, GFP_KERNEL);
1798 if (dyn_tssi2dbm == NULL) {
1799 b43err(dev->wl, "Could not allocate memory "
1800 "for tssi2dbm table\n");
1803 for (idx = 0; idx < 64; idx++)
1804 if (b43_tssi2dbm_entry
1805 (dyn_tssi2dbm, idx, pab0, pab1, pab2)) {
1806 phy->tssi2dbm = NULL;
1807 b43err(dev->wl, "Could not generate "
1808 "tssi2dBm table\n");
1809 kfree(dyn_tssi2dbm);
1812 phy->tssi2dbm = dyn_tssi2dbm;
1813 phy->dyn_tssi_tbl = 1;
1815 /* pabX values not set in SPROM. */
1816 switch (phy->type) {
1818 /* APHY needs a generated table. */
1819 phy->tssi2dbm = NULL;
1820 b43err(dev->wl, "Could not generate tssi2dBm "
1821 "table (wrong SPROM info)!\n");
1824 phy->tgt_idle_tssi = 0x34;
1825 phy->tssi2dbm = b43_tssi2dbm_b_table;
1828 phy->tgt_idle_tssi = 0x34;
1829 phy->tssi2dbm = b43_tssi2dbm_g_table;
1837 int b43_phy_init(struct b43_wldev *dev)
1839 struct b43_phy *phy = &dev->phy;
1840 bool unsupported = 0;
1843 switch (phy->type) {
1845 if (phy->rev == 2 || phy->rev == 3)
1854 err = b43_phy_initn(dev);
1860 b43err(dev->wl, "Unknown PHYTYPE found\n");
1865 void b43_set_rx_antenna(struct b43_wldev *dev, int antenna)
1867 struct b43_phy *phy = &dev->phy;
1872 if (antenna == B43_ANTENNA_AUTO0 || antenna == B43_ANTENNA_AUTO1)
1875 hf = b43_hf_read(dev);
1876 hf &= ~B43_HF_ANTDIVHELP;
1877 b43_hf_write(dev, hf);
1879 switch (phy->type) {
1882 tmp = b43_phy_read(dev, B43_PHY_BBANDCFG);
1883 tmp &= ~B43_PHY_BBANDCFG_RXANT;
1884 tmp |= (autodiv ? B43_ANTENNA_AUTO0 : antenna)
1885 << B43_PHY_BBANDCFG_RXANT_SHIFT;
1886 b43_phy_write(dev, B43_PHY_BBANDCFG, tmp);
1889 tmp = b43_phy_read(dev, B43_PHY_ANTDWELL);
1890 if (antenna == B43_ANTENNA_AUTO0)
1891 tmp &= ~B43_PHY_ANTDWELL_AUTODIV1;
1893 tmp |= B43_PHY_ANTDWELL_AUTODIV1;
1894 b43_phy_write(dev, B43_PHY_ANTDWELL, tmp);
1896 if (phy->type == B43_PHYTYPE_G) {
1897 tmp = b43_phy_read(dev, B43_PHY_ANTWRSETT);
1899 tmp |= B43_PHY_ANTWRSETT_ARXDIV;
1901 tmp &= ~B43_PHY_ANTWRSETT_ARXDIV;
1902 b43_phy_write(dev, B43_PHY_ANTWRSETT, tmp);
1903 if (phy->rev >= 2) {
1904 tmp = b43_phy_read(dev, B43_PHY_OFDM61);
1905 tmp |= B43_PHY_OFDM61_10;
1906 b43_phy_write(dev, B43_PHY_OFDM61, tmp);
1909 b43_phy_read(dev, B43_PHY_DIVSRCHGAINBACK);
1910 tmp = (tmp & 0xFF00) | 0x15;
1911 b43_phy_write(dev, B43_PHY_DIVSRCHGAINBACK,
1914 if (phy->rev == 2) {
1915 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1920 B43_PHY_ADIVRELATED);
1921 tmp = (tmp & 0xFF00) | 8;
1922 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1927 b43_phy_write(dev, B43_PHY_OFDM9B, 0xDC);
1930 tmp = b43_phy_read(dev, B43_PHY_ANTDWELL);
1931 tmp = (tmp & 0xFF00) | 0x24;
1932 b43_phy_write(dev, B43_PHY_ANTDWELL, tmp);
1934 tmp = b43_phy_read(dev, B43_PHY_OFDM61);
1936 b43_phy_write(dev, B43_PHY_OFDM61, tmp);
1937 if (phy->analog == 3) {
1938 b43_phy_write(dev, B43_PHY_CLIPPWRDOWNT,
1940 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1943 b43_phy_write(dev, B43_PHY_CLIPPWRDOWNT,
1947 B43_PHY_ADIVRELATED);
1948 tmp = (tmp & 0xFF00) | 8;
1949 b43_phy_write(dev, B43_PHY_ADIVRELATED,
1956 tmp = b43_phy_read(dev, B43_PHY_CCKBBANDCFG);
1957 tmp &= ~B43_PHY_BBANDCFG_RXANT;
1958 tmp |= (autodiv ? B43_ANTENNA_AUTO0 : antenna)
1959 << B43_PHY_BBANDCFG_RXANT_SHIFT;
1960 b43_phy_write(dev, B43_PHY_CCKBBANDCFG, tmp);
1963 b43_nphy_set_rxantenna(dev, antenna);
1969 hf |= B43_HF_ANTDIVHELP;
1970 b43_hf_write(dev, hf);
1973 /* Get the freq, as it has to be written to the device. */
1974 static inline u16 channel2freq_bg(u8 channel)
1976 B43_WARN_ON(!(channel >= 1 && channel <= 14));
1978 return b43_radio_channel_codes_bg[channel - 1];
1981 /* Get the freq, as it has to be written to the device. */
1982 static inline u16 channel2freq_a(u8 channel)
1984 B43_WARN_ON(channel > 200);
1986 return (5000 + 5 * channel);
1989 void b43_radio_lock(struct b43_wldev *dev)
1993 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1994 B43_WARN_ON(macctl & B43_MACCTL_RADIOLOCK);
1995 macctl |= B43_MACCTL_RADIOLOCK;
1996 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1997 /* Commit the write and wait for the device
1998 * to exit any radio register access. */
1999 b43_read32(dev, B43_MMIO_MACCTL);
2003 void b43_radio_unlock(struct b43_wldev *dev)
2007 /* Commit any write */
2008 b43_read16(dev, B43_MMIO_PHY_VER);
2010 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2011 B43_WARN_ON(!(macctl & B43_MACCTL_RADIOLOCK));
2012 macctl &= ~B43_MACCTL_RADIOLOCK;
2013 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2016 u16 b43_radio_read16(struct b43_wldev *dev, u16 offset)
2018 struct b43_phy *phy = &dev->phy;
2020 /* Offset 1 is a 32-bit register. */
2021 B43_WARN_ON(offset == 1);
2023 switch (phy->type) {
2028 if (phy->radio_ver == 0x2053) {
2031 else if (offset < 0x80)
2033 } else if (phy->radio_ver == 0x2050) {
2044 case B43_PHYTYPE_LP:
2045 /* No adjustment required. */
2051 b43_write16(dev, B43_MMIO_RADIO_CONTROL, offset);
2052 return b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
2055 void b43_radio_write16(struct b43_wldev *dev, u16 offset, u16 val)
2057 /* Offset 1 is a 32-bit register. */
2058 B43_WARN_ON(offset == 1);
2060 b43_write16(dev, B43_MMIO_RADIO_CONTROL, offset);
2061 b43_write16(dev, B43_MMIO_RADIO_DATA_LOW, val);
2064 void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
2066 b43_radio_write16(dev, offset,
2067 b43_radio_read16(dev, offset) & mask);
2070 void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
2072 b43_radio_write16(dev, offset,
2073 b43_radio_read16(dev, offset) | set);
2076 void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
2078 b43_radio_write16(dev, offset,
2079 (b43_radio_read16(dev, offset) & mask) | set);
2082 static void b43_set_all_gains(struct b43_wldev *dev,
2083 s16 first, s16 second, s16 third)
2085 struct b43_phy *phy = &dev->phy;
2087 u16 start = 0x08, end = 0x18;
2091 if (phy->rev <= 1) {
2096 table = B43_OFDMTAB_GAINX;
2098 table = B43_OFDMTAB_GAINX_R1;
2099 for (i = 0; i < 4; i++)
2100 b43_ofdmtab_write16(dev, table, i, first);
2102 for (i = start; i < end; i++)
2103 b43_ofdmtab_write16(dev, table, i, second);
2106 tmp = ((u16) third << 14) | ((u16) third << 6);
2107 b43_phy_write(dev, 0x04A0,
2108 (b43_phy_read(dev, 0x04A0) & 0xBFBF) | tmp);
2109 b43_phy_write(dev, 0x04A1,
2110 (b43_phy_read(dev, 0x04A1) & 0xBFBF) | tmp);
2111 b43_phy_write(dev, 0x04A2,
2112 (b43_phy_read(dev, 0x04A2) & 0xBFBF) | tmp);
2114 b43_dummy_transmission(dev);
2117 static void b43_set_original_gains(struct b43_wldev *dev)
2119 struct b43_phy *phy = &dev->phy;
2122 u16 start = 0x0008, end = 0x0018;
2124 if (phy->rev <= 1) {
2129 table = B43_OFDMTAB_GAINX;
2131 table = B43_OFDMTAB_GAINX_R1;
2132 for (i = 0; i < 4; i++) {
2134 tmp |= (i & 0x0001) << 1;
2135 tmp |= (i & 0x0002) >> 1;
2137 b43_ofdmtab_write16(dev, table, i, tmp);
2140 for (i = start; i < end; i++)
2141 b43_ofdmtab_write16(dev, table, i, i - start);
2143 b43_phy_write(dev, 0x04A0,
2144 (b43_phy_read(dev, 0x04A0) & 0xBFBF) | 0x4040);
2145 b43_phy_write(dev, 0x04A1,
2146 (b43_phy_read(dev, 0x04A1) & 0xBFBF) | 0x4040);
2147 b43_phy_write(dev, 0x04A2,
2148 (b43_phy_read(dev, 0x04A2) & 0xBFBF) | 0x4000);
2149 b43_dummy_transmission(dev);
2152 /* Synthetic PU workaround */
2153 static void b43_synth_pu_workaround(struct b43_wldev *dev, u8 channel)
2155 struct b43_phy *phy = &dev->phy;
2159 if (phy->radio_ver != 0x2050 || phy->radio_rev >= 6) {
2160 /* We do not need the workaround. */
2164 if (channel <= 10) {
2165 b43_write16(dev, B43_MMIO_CHANNEL,
2166 channel2freq_bg(channel + 4));
2168 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(1));
2171 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(channel));
2174 u8 b43_radio_aci_detect(struct b43_wldev *dev, u8 channel)
2176 struct b43_phy *phy = &dev->phy;
2178 u16 saved, rssi, temp;
2181 saved = b43_phy_read(dev, 0x0403);
2182 b43_radio_selectchannel(dev, channel, 0);
2183 b43_phy_write(dev, 0x0403, (saved & 0xFFF8) | 5);
2184 if (phy->aci_hw_rssi)
2185 rssi = b43_phy_read(dev, 0x048A) & 0x3F;
2187 rssi = saved & 0x3F;
2188 /* clamp temp to signed 5bit */
2191 for (i = 0; i < 100; i++) {
2192 temp = (b43_phy_read(dev, 0x047F) >> 8) & 0x3F;
2200 b43_phy_write(dev, 0x0403, saved);
2205 u8 b43_radio_aci_scan(struct b43_wldev * dev)
2207 struct b43_phy *phy = &dev->phy;
2209 unsigned int channel = phy->channel;
2210 unsigned int i, j, start, end;
2212 if (!((phy->type == B43_PHYTYPE_G) && (phy->rev > 0)))
2216 b43_radio_lock(dev);
2217 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & 0xFFFC);
2218 b43_phy_write(dev, B43_PHY_G_CRS,
2219 b43_phy_read(dev, B43_PHY_G_CRS) & 0x7FFF);
2220 b43_set_all_gains(dev, 3, 8, 1);
2222 start = (channel - 5 > 0) ? channel - 5 : 1;
2223 end = (channel + 5 < 14) ? channel + 5 : 13;
2225 for (i = start; i <= end; i++) {
2226 if (abs(channel - i) > 2)
2227 ret[i - 1] = b43_radio_aci_detect(dev, i);
2229 b43_radio_selectchannel(dev, channel, 0);
2230 b43_phy_write(dev, 0x0802,
2231 (b43_phy_read(dev, 0x0802) & 0xFFFC) | 0x0003);
2232 b43_phy_write(dev, 0x0403, b43_phy_read(dev, 0x0403) & 0xFFF8);
2233 b43_phy_write(dev, B43_PHY_G_CRS,
2234 b43_phy_read(dev, B43_PHY_G_CRS) | 0x8000);
2235 b43_set_original_gains(dev);
2236 for (i = 0; i < 13; i++) {
2239 end = (i + 5 < 13) ? i + 5 : 13;
2240 for (j = i; j < end; j++)
2243 b43_radio_unlock(dev);
2244 b43_phy_unlock(dev);
2246 return ret[channel - 1];
2249 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2250 void b43_nrssi_hw_write(struct b43_wldev *dev, u16 offset, s16 val)
2252 b43_phy_write(dev, B43_PHY_NRSSILT_CTRL, offset);
2254 b43_phy_write(dev, B43_PHY_NRSSILT_DATA, (u16) val);
2257 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2258 s16 b43_nrssi_hw_read(struct b43_wldev *dev, u16 offset)
2262 b43_phy_write(dev, B43_PHY_NRSSILT_CTRL, offset);
2263 val = b43_phy_read(dev, B43_PHY_NRSSILT_DATA);
2268 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2269 void b43_nrssi_hw_update(struct b43_wldev *dev, u16 val)
2274 for (i = 0; i < 64; i++) {
2275 tmp = b43_nrssi_hw_read(dev, i);
2277 tmp = limit_value(tmp, -32, 31);
2278 b43_nrssi_hw_write(dev, i, tmp);
2282 /* http://bcm-specs.sipsolutions.net/NRSSILookupTable */
2283 void b43_nrssi_mem_update(struct b43_wldev *dev)
2285 struct b43_phy *phy = &dev->phy;
2289 delta = 0x1F - phy->nrssi[0];
2290 for (i = 0; i < 64; i++) {
2291 tmp = (i - delta) * phy->nrssislope;
2294 tmp = limit_value(tmp, 0, 0x3F);
2295 phy->nrssi_lt[i] = tmp;
2299 static void b43_calc_nrssi_offset(struct b43_wldev *dev)
2301 struct b43_phy *phy = &dev->phy;
2302 u16 backup[20] = { 0 };
2307 backup[0] = b43_phy_read(dev, 0x0001);
2308 backup[1] = b43_phy_read(dev, 0x0811);
2309 backup[2] = b43_phy_read(dev, 0x0812);
2310 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2311 backup[3] = b43_phy_read(dev, 0x0814);
2312 backup[4] = b43_phy_read(dev, 0x0815);
2314 backup[5] = b43_phy_read(dev, 0x005A);
2315 backup[6] = b43_phy_read(dev, 0x0059);
2316 backup[7] = b43_phy_read(dev, 0x0058);
2317 backup[8] = b43_phy_read(dev, 0x000A);
2318 backup[9] = b43_phy_read(dev, 0x0003);
2319 backup[10] = b43_radio_read16(dev, 0x007A);
2320 backup[11] = b43_radio_read16(dev, 0x0043);
2322 b43_phy_write(dev, 0x0429, b43_phy_read(dev, 0x0429) & 0x7FFF);
2323 b43_phy_write(dev, 0x0001,
2324 (b43_phy_read(dev, 0x0001) & 0x3FFF) | 0x4000);
2325 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x000C);
2326 b43_phy_write(dev, 0x0812,
2327 (b43_phy_read(dev, 0x0812) & 0xFFF3) | 0x0004);
2328 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & ~(0x1 | 0x2));
2329 if (phy->rev >= 6) {
2330 backup[12] = b43_phy_read(dev, 0x002E);
2331 backup[13] = b43_phy_read(dev, 0x002F);
2332 backup[14] = b43_phy_read(dev, 0x080F);
2333 backup[15] = b43_phy_read(dev, 0x0810);
2334 backup[16] = b43_phy_read(dev, 0x0801);
2335 backup[17] = b43_phy_read(dev, 0x0060);
2336 backup[18] = b43_phy_read(dev, 0x0014);
2337 backup[19] = b43_phy_read(dev, 0x0478);
2339 b43_phy_write(dev, 0x002E, 0);
2340 b43_phy_write(dev, 0x002F, 0);
2341 b43_phy_write(dev, 0x080F, 0);
2342 b43_phy_write(dev, 0x0810, 0);
2343 b43_phy_write(dev, 0x0478, b43_phy_read(dev, 0x0478) | 0x0100);
2344 b43_phy_write(dev, 0x0801, b43_phy_read(dev, 0x0801) | 0x0040);
2345 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060) | 0x0040);
2346 b43_phy_write(dev, 0x0014, b43_phy_read(dev, 0x0014) | 0x0200);
2348 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0070);
2349 b43_radio_write16(dev, 0x007A, b43_radio_read16(dev, 0x007A) | 0x0080);
2352 v47F = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2356 for (i = 7; i >= 4; i--) {
2357 b43_radio_write16(dev, 0x007B, i);
2360 (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2363 if (v47F < 31 && saved == 0xFFFF)
2366 if (saved == 0xFFFF)
2369 b43_radio_write16(dev, 0x007A,
2370 b43_radio_read16(dev, 0x007A) & 0x007F);
2371 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2372 b43_phy_write(dev, 0x0814,
2373 b43_phy_read(dev, 0x0814) | 0x0001);
2374 b43_phy_write(dev, 0x0815,
2375 b43_phy_read(dev, 0x0815) & 0xFFFE);
2377 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x000C);
2378 b43_phy_write(dev, 0x0812, b43_phy_read(dev, 0x0812) | 0x000C);
2379 b43_phy_write(dev, 0x0811, b43_phy_read(dev, 0x0811) | 0x0030);
2380 b43_phy_write(dev, 0x0812, b43_phy_read(dev, 0x0812) | 0x0030);
2381 b43_phy_write(dev, 0x005A, 0x0480);
2382 b43_phy_write(dev, 0x0059, 0x0810);
2383 b43_phy_write(dev, 0x0058, 0x000D);
2384 if (phy->rev == 0) {
2385 b43_phy_write(dev, 0x0003, 0x0122);
2387 b43_phy_write(dev, 0x000A, b43_phy_read(dev, 0x000A)
2390 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2391 b43_phy_write(dev, 0x0814,
2392 b43_phy_read(dev, 0x0814) | 0x0004);
2393 b43_phy_write(dev, 0x0815,
2394 b43_phy_read(dev, 0x0815) & 0xFFFB);
2396 b43_phy_write(dev, 0x0003, (b43_phy_read(dev, 0x0003) & 0xFF9F)
2398 b43_radio_write16(dev, 0x007A,
2399 b43_radio_read16(dev, 0x007A) | 0x000F);
2400 b43_set_all_gains(dev, 3, 0, 1);
2401 b43_radio_write16(dev, 0x0043, (b43_radio_read16(dev, 0x0043)
2402 & 0x00F0) | 0x000F);
2404 v47F = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2408 for (i = 0; i < 4; i++) {
2409 b43_radio_write16(dev, 0x007B, i);
2412 (s16) ((b43_phy_read(dev, 0x047F) >> 8) &
2416 if (v47F > -31 && saved == 0xFFFF)
2419 if (saved == 0xFFFF)
2424 b43_radio_write16(dev, 0x007B, saved);
2426 if (phy->rev >= 6) {
2427 b43_phy_write(dev, 0x002E, backup[12]);
2428 b43_phy_write(dev, 0x002F, backup[13]);
2429 b43_phy_write(dev, 0x080F, backup[14]);
2430 b43_phy_write(dev, 0x0810, backup[15]);
2432 if (phy->rev != 1) { /* Not in specs, but needed to prevent PPC machine check */
2433 b43_phy_write(dev, 0x0814, backup[3]);
2434 b43_phy_write(dev, 0x0815, backup[4]);
2436 b43_phy_write(dev, 0x005A, backup[5]);
2437 b43_phy_write(dev, 0x0059, backup[6]);
2438 b43_phy_write(dev, 0x0058, backup[7]);
2439 b43_phy_write(dev, 0x000A, backup[8]);
2440 b43_phy_write(dev, 0x0003, backup[9]);
2441 b43_radio_write16(dev, 0x0043, backup[11]);
2442 b43_radio_write16(dev, 0x007A, backup[10]);
2443 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) | 0x1 | 0x2);
2444 b43_phy_write(dev, 0x0429, b43_phy_read(dev, 0x0429) | 0x8000);
2445 b43_set_original_gains(dev);
2446 if (phy->rev >= 6) {
2447 b43_phy_write(dev, 0x0801, backup[16]);
2448 b43_phy_write(dev, 0x0060, backup[17]);
2449 b43_phy_write(dev, 0x0014, backup[18]);
2450 b43_phy_write(dev, 0x0478, backup[19]);
2452 b43_phy_write(dev, 0x0001, backup[0]);
2453 b43_phy_write(dev, 0x0812, backup[2]);
2454 b43_phy_write(dev, 0x0811, backup[1]);
2457 void b43_calc_nrssi_slope(struct b43_wldev *dev)
2459 struct b43_phy *phy = &dev->phy;
2460 u16 backup[18] = { 0 };
2464 switch (phy->type) {
2466 backup[0] = b43_radio_read16(dev, 0x007A);
2467 backup[1] = b43_radio_read16(dev, 0x0052);
2468 backup[2] = b43_radio_read16(dev, 0x0043);
2469 backup[3] = b43_phy_read(dev, 0x0030);
2470 backup[4] = b43_phy_read(dev, 0x0026);
2471 backup[5] = b43_phy_read(dev, 0x0015);
2472 backup[6] = b43_phy_read(dev, 0x002A);
2473 backup[7] = b43_phy_read(dev, 0x0020);
2474 backup[8] = b43_phy_read(dev, 0x005A);
2475 backup[9] = b43_phy_read(dev, 0x0059);
2476 backup[10] = b43_phy_read(dev, 0x0058);
2477 backup[11] = b43_read16(dev, 0x03E2);
2478 backup[12] = b43_read16(dev, 0x03E6);
2479 backup[13] = b43_read16(dev, B43_MMIO_CHANNEL_EXT);
2481 tmp = b43_radio_read16(dev, 0x007A);
2482 tmp &= (phy->rev >= 5) ? 0x007F : 0x000F;
2483 b43_radio_write16(dev, 0x007A, tmp);
2484 b43_phy_write(dev, 0x0030, 0x00FF);
2485 b43_write16(dev, 0x03EC, 0x7F7F);
2486 b43_phy_write(dev, 0x0026, 0x0000);
2487 b43_phy_write(dev, 0x0015, b43_phy_read(dev, 0x0015) | 0x0020);
2488 b43_phy_write(dev, 0x002A, 0x08A3);
2489 b43_radio_write16(dev, 0x007A,
2490 b43_radio_read16(dev, 0x007A) | 0x0080);
2492 nrssi0 = (s16) b43_phy_read(dev, 0x0027);
2493 b43_radio_write16(dev, 0x007A,
2494 b43_radio_read16(dev, 0x007A) & 0x007F);
2495 if (phy->rev >= 2) {
2496 b43_write16(dev, 0x03E6, 0x0040);
2497 } else if (phy->rev == 0) {
2498 b43_write16(dev, 0x03E6, 0x0122);
2500 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
2502 B43_MMIO_CHANNEL_EXT) & 0x2000);
2504 b43_phy_write(dev, 0x0020, 0x3F3F);
2505 b43_phy_write(dev, 0x0015, 0xF330);
2506 b43_radio_write16(dev, 0x005A, 0x0060);
2507 b43_radio_write16(dev, 0x0043,
2508 b43_radio_read16(dev, 0x0043) & 0x00F0);
2509 b43_phy_write(dev, 0x005A, 0x0480);
2510 b43_phy_write(dev, 0x0059, 0x0810);
2511 b43_phy_write(dev, 0x0058, 0x000D);
2514 nrssi1 = (s16) b43_phy_read(dev, 0x0027);
2515 b43_phy_write(dev, 0x0030, backup[3]);
2516 b43_radio_write16(dev, 0x007A, backup[0]);
2517 b43_write16(dev, 0x03E2, backup[11]);
2518 b43_phy_write(dev, 0x0026, backup[4]);
2519 b43_phy_write(dev, 0x0015, backup[5]);
2520 b43_phy_write(dev, 0x002A, backup[6]);
2521 b43_synth_pu_workaround(dev, phy->channel);
2523 b43_write16(dev, 0x03F4, backup[13]);
2525 b43_phy_write(dev, 0x0020, backup[7]);
2526 b43_phy_write(dev, 0x005A, backup[8]);
2527 b43_phy_write(dev, 0x0059, backup[9]);
2528 b43_phy_write(dev, 0x0058, backup[10]);
2529 b43_radio_write16(dev, 0x0052, backup[1]);
2530 b43_radio_write16(dev, 0x0043, backup[2]);
2532 if (nrssi0 == nrssi1)
2533 phy->nrssislope = 0x00010000;
2535 phy->nrssislope = 0x00400000 / (nrssi0 - nrssi1);
2538 phy->nrssi[0] = nrssi0;
2539 phy->nrssi[1] = nrssi1;
2543 if (phy->radio_rev >= 9)
2545 if (phy->radio_rev == 8)
2546 b43_calc_nrssi_offset(dev);
2548 b43_phy_write(dev, B43_PHY_G_CRS,
2549 b43_phy_read(dev, B43_PHY_G_CRS) & 0x7FFF);
2550 b43_phy_write(dev, 0x0802, b43_phy_read(dev, 0x0802) & 0xFFFC);
2551 backup[7] = b43_read16(dev, 0x03E2);
2552 b43_write16(dev, 0x03E2, b43_read16(dev, 0x03E2) | 0x8000);
2553 backup[0] = b43_radio_read16(dev, 0x007A);
2554 backup[1] = b43_radio_read16(dev, 0x0052);
2555 backup[2] = b43_radio_read16(dev, 0x0043);
2556 backup[3] = b43_phy_read(dev, 0x0015);
2557 backup[4] = b43_phy_read(dev, 0x005A);
2558 backup[5] = b43_phy_read(dev, 0x0059);
2559 backup[6] = b43_phy_read(dev, 0x0058);
2560 backup[8] = b43_read16(dev, 0x03E6);
2561 backup[9] = b43_read16(dev, B43_MMIO_CHANNEL_EXT);
2562 if (phy->rev >= 3) {
2563 backup[10] = b43_phy_read(dev, 0x002E);
2564 backup[11] = b43_phy_read(dev, 0x002F);
2565 backup[12] = b43_phy_read(dev, 0x080F);
2566 backup[13] = b43_phy_read(dev, B43_PHY_G_LO_CONTROL);
2567 backup[14] = b43_phy_read(dev, 0x0801);
2568 backup[15] = b43_phy_read(dev, 0x0060);
2569 backup[16] = b43_phy_read(dev, 0x0014);
2570 backup[17] = b43_phy_read(dev, 0x0478);
2571 b43_phy_write(dev, 0x002E, 0);
2572 b43_phy_write(dev, B43_PHY_G_LO_CONTROL, 0);
2577 b43_phy_write(dev, 0x0478,
2578 b43_phy_read(dev, 0x0478)
2580 b43_phy_write(dev, 0x0801,
2581 b43_phy_read(dev, 0x0801)
2586 b43_phy_write(dev, 0x0801,
2587 b43_phy_read(dev, 0x0801)
2591 b43_phy_write(dev, 0x0060, b43_phy_read(dev, 0x0060)
2593 b43_phy_write(dev, 0x0014, b43_phy_read(dev, 0x0014)
2596 b43_radio_write16(dev, 0x007A,
2597 b43_radio_read16(dev, 0x007A) | 0x0070);
2598 b43_set_all_gains(dev, 0, 8, 0);
2599 b43_radio_write16(dev, 0x007A,
2600 b43_radio_read16(dev, 0x007A) & 0x00F7);
2601 if (phy->rev >= 2) {
2602 b43_phy_write(dev, 0x0811,
2603 (b43_phy_read(dev, 0x0811) & 0xFFCF) |
2605 b43_phy_write(dev, 0x0812,
2606 (b43_phy_read(dev, 0x0812) & 0xFFCF) |
2609 b43_radio_write16(dev, 0x007A,
2610 b43_radio_read16(dev, 0x007A) | 0x0080);
2613 nrssi0 = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2614 if (nrssi0 >= 0x0020)
2617 b43_radio_write16(dev, 0x007A,
2618 b43_radio_read16(dev, 0x007A) & 0x007F);
2619 if (phy->rev >= 2) {
2620 b43_phy_write(dev, 0x0003, (b43_phy_read(dev, 0x0003)
2621 & 0xFF9F) | 0x0040);
2624 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
2625 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
2627 b43_radio_write16(dev, 0x007A,
2628 b43_radio_read16(dev, 0x007A) | 0x000F);
2629 b43_phy_write(dev, 0x0015, 0xF330);
2630 if (phy->rev >= 2) {
2631 b43_phy_write(dev, 0x0812,
2632 (b43_phy_read(dev, 0x0812) & 0xFFCF) |
2634 b43_phy_write(dev, 0x0811,
2635 (b43_phy_read(dev, 0x0811) & 0xFFCF) |
2639 b43_set_all_gains(dev, 3, 0, 1);
2640 if (phy->radio_rev == 8) {
2641 b43_radio_write16(dev, 0x0043, 0x001F);
2643 tmp = b43_radio_read16(dev, 0x0052) & 0xFF0F;
2644 b43_radio_write16(dev, 0x0052, tmp | 0x0060);
2645 tmp = b43_radio_read16(dev, 0x0043) & 0xFFF0;
2646 b43_radio_write16(dev, 0x0043, tmp | 0x0009);
2648 b43_phy_write(dev, 0x005A, 0x0480);
2649 b43_phy_write(dev, 0x0059, 0x0810);
2650 b43_phy_write(dev, 0x0058, 0x000D);
2652 nrssi1 = (s16) ((b43_phy_read(dev, 0x047F) >> 8) & 0x003F);
2653 if (nrssi1 >= 0x0020)
2655 if (nrssi0 == nrssi1)
2656 phy->nrssislope = 0x00010000;
2658 phy->nrssislope = 0x00400000 / (nrssi0 - nrssi1);
2660 phy->nrssi[0] = nrssi1;
2661 phy->nrssi[1] = nrssi0;
2663 if (phy->rev >= 3) {
2664 b43_phy_write(dev, 0x002E, backup[10]);
2665 b43_phy_write(dev, 0x002F, backup[11]);
2666 b43_phy_write(dev, 0x080F, backup[12]);
2667 b43_phy_write(dev, B43_PHY_G_LO_CONTROL, backup[13]);
2669 if (phy->rev >= 2) {
2670 b43_phy_write(dev, 0x0812,
2671 b43_phy_read(dev, 0x0812) & 0xFFCF);
2672 b43_phy_write(dev, 0x0811,
2673 b43_phy_read(dev, 0x0811) & 0xFFCF);
2676 b43_radio_write16(dev, 0x007A, backup[0]);
2677 b43_radio_write16(dev, 0x0052, backup[1]);
2678 b43_radio_write16(dev, 0x0043, backup[2]);
2679 b43_write16(dev, 0x03E2, backup[7]);
2680 b43_write16(dev, 0x03E6, backup[8]);
2681 b43_write16(dev, B43_MMIO_CHANNEL_EXT, backup[9]);
2682 b43_phy_write(dev, 0x0015, backup[3]);
2683 b43_phy_write(dev, 0x005A, backup[4]);
2684 b43_phy_write(dev, 0x0059, backup[5]);
2685 b43_phy_write(dev, 0x0058, backup[6]);
2686 b43_synth_pu_workaround(dev, phy->channel);
2687 b43_phy_write(dev, 0x0802,
2688 b43_phy_read(dev, 0x0802) | (0x0001 | 0x0002));
2689 b43_set_original_gains(dev);
2690 b43_phy_write(dev, B43_PHY_G_CRS,
2691 b43_phy_read(dev, B43_PHY_G_CRS) | 0x8000);
2692 if (phy->rev >= 3) {
2693 b43_phy_write(dev, 0x0801, backup[14]);
2694 b43_phy_write(dev, 0x0060, backup[15]);
2695 b43_phy_write(dev, 0x0014, backup[16]);
2696 b43_phy_write(dev, 0x0478, backup[17]);
2698 b43_nrssi_mem_update(dev);
2699 b43_calc_nrssi_threshold(dev);
2706 void b43_calc_nrssi_threshold(struct b43_wldev *dev)
2708 struct b43_phy *phy = &dev->phy;
2714 switch (phy->type) {
2715 case B43_PHYTYPE_B:{
2716 if (phy->radio_ver != 0x2050)
2719 (dev->dev->bus->sprom.
2720 boardflags_lo & B43_BFL_RSSI))
2723 if (phy->radio_rev >= 6) {
2725 (phy->nrssi[1] - phy->nrssi[0]) * 32;
2726 threshold += 20 * (phy->nrssi[0] + 1);
2729 threshold = phy->nrssi[1] - 5;
2731 threshold = limit_value(threshold, 0, 0x3E);
2732 b43_phy_read(dev, 0x0020); /* dummy read */
2733 b43_phy_write(dev, 0x0020,
2734 (((u16) threshold) << 8) | 0x001C);
2736 if (phy->radio_rev >= 6) {
2737 b43_phy_write(dev, 0x0087, 0x0E0D);
2738 b43_phy_write(dev, 0x0086, 0x0C0B);
2739 b43_phy_write(dev, 0x0085, 0x0A09);
2740 b43_phy_write(dev, 0x0084, 0x0808);
2741 b43_phy_write(dev, 0x0083, 0x0808);
2742 b43_phy_write(dev, 0x0082, 0x0604);
2743 b43_phy_write(dev, 0x0081, 0x0302);
2744 b43_phy_write(dev, 0x0080, 0x0100);
2750 !(dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI)) {
2751 tmp16 = b43_nrssi_hw_read(dev, 0x20);
2755 b43_phy_write(dev, 0x048A,
2756 (b43_phy_read(dev, 0x048A)
2757 & 0xF000) | 0x09EB);
2759 b43_phy_write(dev, 0x048A,
2760 (b43_phy_read(dev, 0x048A)
2761 & 0xF000) | 0x0AED);
2764 if (phy->interfmode == B43_INTERFMODE_NONWLAN) {
2767 } else if (!phy->aci_wlan_automatic && phy->aci_enable) {
2775 a = a * (phy->nrssi[1] - phy->nrssi[0]);
2776 a += (phy->nrssi[0] << 6);
2782 a = limit_value(a, -31, 31);
2784 b = b * (phy->nrssi[1] - phy->nrssi[0]);
2785 b += (phy->nrssi[0] << 6);
2791 b = limit_value(b, -31, 31);
2793 tmp_u16 = b43_phy_read(dev, 0x048A) & 0xF000;
2794 tmp_u16 |= ((u32) b & 0x0000003F);
2795 tmp_u16 |= (((u32) a & 0x0000003F) << 6);
2796 b43_phy_write(dev, 0x048A, tmp_u16);
2804 /* Stack implementation to save/restore values from the
2805 * interference mitigation code.
2806 * It is save to restore values in random order.
2808 static void _stack_save(u32 * _stackptr, size_t * stackidx,
2809 u8 id, u16 offset, u16 value)
2811 u32 *stackptr = &(_stackptr[*stackidx]);
2813 B43_WARN_ON(offset & 0xF000);
2814 B43_WARN_ON(id & 0xF0);
2816 *stackptr |= ((u32) id) << 12;
2817 *stackptr |= ((u32) value) << 16;
2819 B43_WARN_ON(*stackidx >= B43_INTERFSTACK_SIZE);
2822 static u16 _stack_restore(u32 * stackptr, u8 id, u16 offset)
2826 B43_WARN_ON(offset & 0xF000);
2827 B43_WARN_ON(id & 0xF0);
2828 for (i = 0; i < B43_INTERFSTACK_SIZE; i++, stackptr++) {
2829 if ((*stackptr & 0x00000FFF) != offset)
2831 if (((*stackptr & 0x0000F000) >> 12) != id)
2833 return ((*stackptr & 0xFFFF0000) >> 16);
2840 #define phy_stacksave(offset) \
2842 _stack_save(stack, &stackidx, 0x1, (offset), \
2843 b43_phy_read(dev, (offset))); \
2845 #define phy_stackrestore(offset) \
2847 b43_phy_write(dev, (offset), \
2848 _stack_restore(stack, 0x1, \
2851 #define radio_stacksave(offset) \
2853 _stack_save(stack, &stackidx, 0x2, (offset), \
2854 b43_radio_read16(dev, (offset))); \
2856 #define radio_stackrestore(offset) \
2858 b43_radio_write16(dev, (offset), \
2859 _stack_restore(stack, 0x2, \
2862 #define ofdmtab_stacksave(table, offset) \
2864 _stack_save(stack, &stackidx, 0x3, (offset)|(table), \
2865 b43_ofdmtab_read16(dev, (table), (offset))); \
2867 #define ofdmtab_stackrestore(table, offset) \
2869 b43_ofdmtab_write16(dev, (table), (offset), \
2870 _stack_restore(stack, 0x3, \
2871 (offset)|(table))); \
2875 b43_radio_interference_mitigation_enable(struct b43_wldev *dev, int mode)
2877 struct b43_phy *phy = &dev->phy;
2879 size_t stackidx = 0;
2880 u32 *stack = phy->interfstack;
2883 case B43_INTERFMODE_NONWLAN:
2884 if (phy->rev != 1) {
2885 b43_phy_write(dev, 0x042B,
2886 b43_phy_read(dev, 0x042B) | 0x0800);
2887 b43_phy_write(dev, B43_PHY_G_CRS,
2889 B43_PHY_G_CRS) & ~0x4000);
2892 radio_stacksave(0x0078);
2893 tmp = (b43_radio_read16(dev, 0x0078) & 0x001E);
2894 flipped = flip_4bit(tmp);
2895 if (flipped < 10 && flipped >= 8)
2897 else if (flipped >= 10)
2899 flipped = flip_4bit(flipped);
2900 flipped = (flipped << 1) | 0x0020;
2901 b43_radio_write16(dev, 0x0078, flipped);
2903 b43_calc_nrssi_threshold(dev);
2905 phy_stacksave(0x0406);
2906 b43_phy_write(dev, 0x0406, 0x7E28);
2908 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) | 0x0800);
2909 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
2911 B43_PHY_RADIO_BITFIELD) | 0x1000);
2913 phy_stacksave(0x04A0);
2914 b43_phy_write(dev, 0x04A0,
2915 (b43_phy_read(dev, 0x04A0) & 0xC0C0) | 0x0008);
2916 phy_stacksave(0x04A1);
2917 b43_phy_write(dev, 0x04A1,
2918 (b43_phy_read(dev, 0x04A1) & 0xC0C0) | 0x0605);
2919 phy_stacksave(0x04A2);
2920 b43_phy_write(dev, 0x04A2,
2921 (b43_phy_read(dev, 0x04A2) & 0xC0C0) | 0x0204);
2922 phy_stacksave(0x04A8);
2923 b43_phy_write(dev, 0x04A8,
2924 (b43_phy_read(dev, 0x04A8) & 0xC0C0) | 0x0803);
2925 phy_stacksave(0x04AB);
2926 b43_phy_write(dev, 0x04AB,
2927 (b43_phy_read(dev, 0x04AB) & 0xC0C0) | 0x0605);
2929 phy_stacksave(0x04A7);
2930 b43_phy_write(dev, 0x04A7, 0x0002);
2931 phy_stacksave(0x04A3);
2932 b43_phy_write(dev, 0x04A3, 0x287A);
2933 phy_stacksave(0x04A9);
2934 b43_phy_write(dev, 0x04A9, 0x2027);
2935 phy_stacksave(0x0493);
2936 b43_phy_write(dev, 0x0493, 0x32F5);
2937 phy_stacksave(0x04AA);
2938 b43_phy_write(dev, 0x04AA, 0x2027);
2939 phy_stacksave(0x04AC);
2940 b43_phy_write(dev, 0x04AC, 0x32F5);
2942 case B43_INTERFMODE_MANUALWLAN:
2943 if (b43_phy_read(dev, 0x0033) & 0x0800)
2946 phy->aci_enable = 1;
2948 phy_stacksave(B43_PHY_RADIO_BITFIELD);
2949 phy_stacksave(B43_PHY_G_CRS);
2951 phy_stacksave(0x0406);
2953 phy_stacksave(0x04C0);
2954 phy_stacksave(0x04C1);
2956 phy_stacksave(0x0033);
2957 phy_stacksave(0x04A7);
2958 phy_stacksave(0x04A3);
2959 phy_stacksave(0x04A9);
2960 phy_stacksave(0x04AA);
2961 phy_stacksave(0x04AC);
2962 phy_stacksave(0x0493);
2963 phy_stacksave(0x04A1);
2964 phy_stacksave(0x04A0);
2965 phy_stacksave(0x04A2);
2966 phy_stacksave(0x048A);
2967 phy_stacksave(0x04A8);
2968 phy_stacksave(0x04AB);
2969 if (phy->rev == 2) {
2970 phy_stacksave(0x04AD);
2971 phy_stacksave(0x04AE);
2972 } else if (phy->rev >= 3) {
2973 phy_stacksave(0x04AD);
2974 phy_stacksave(0x0415);
2975 phy_stacksave(0x0416);
2976 phy_stacksave(0x0417);
2977 ofdmtab_stacksave(0x1A00, 0x2);
2978 ofdmtab_stacksave(0x1A00, 0x3);
2980 phy_stacksave(0x042B);
2981 phy_stacksave(0x048C);
2983 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
2984 b43_phy_read(dev, B43_PHY_RADIO_BITFIELD)
2986 b43_phy_write(dev, B43_PHY_G_CRS,
2987 (b43_phy_read(dev, B43_PHY_G_CRS)
2988 & 0xFFFC) | 0x0002);
2990 b43_phy_write(dev, 0x0033, 0x0800);
2991 b43_phy_write(dev, 0x04A3, 0x2027);
2992 b43_phy_write(dev, 0x04A9, 0x1CA8);
2993 b43_phy_write(dev, 0x0493, 0x287A);
2994 b43_phy_write(dev, 0x04AA, 0x1CA8);
2995 b43_phy_write(dev, 0x04AC, 0x287A);
2997 b43_phy_write(dev, 0x04A0, (b43_phy_read(dev, 0x04A0)
2998 & 0xFFC0) | 0x001A);
2999 b43_phy_write(dev, 0x04A7, 0x000D);
3002 b43_phy_write(dev, 0x0406, 0xFF0D);
3003 } else if (phy->rev == 2) {
3004 b43_phy_write(dev, 0x04C0, 0xFFFF);
3005 b43_phy_write(dev, 0x04C1, 0x00A9);
3007 b43_phy_write(dev, 0x04C0, 0x00C1);
3008 b43_phy_write(dev, 0x04C1, 0x0059);
3011 b43_phy_write(dev, 0x04A1, (b43_phy_read(dev, 0x04A1)
3012 & 0xC0FF) | 0x1800);
3013 b43_phy_write(dev, 0x04A1, (b43_phy_read(dev, 0x04A1)
3014 & 0xFFC0) | 0x0015);
3015 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3016 & 0xCFFF) | 0x1000);
3017 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3018 & 0xF0FF) | 0x0A00);
3019 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3020 & 0xCFFF) | 0x1000);
3021 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3022 & 0xF0FF) | 0x0800);
3023 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3024 & 0xFFCF) | 0x0010);
3025 b43_phy_write(dev, 0x04AB, (b43_phy_read(dev, 0x04AB)
3026 & 0xFFF0) | 0x0005);
3027 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3028 & 0xFFCF) | 0x0010);
3029 b43_phy_write(dev, 0x04A8, (b43_phy_read(dev, 0x04A8)
3030 & 0xFFF0) | 0x0006);
3031 b43_phy_write(dev, 0x04A2, (b43_phy_read(dev, 0x04A2)
3032 & 0xF0FF) | 0x0800);
3033 b43_phy_write(dev, 0x04A0, (b43_phy_read(dev, 0x04A0)
3034 & 0xF0FF) | 0x0500);
3035 b43_phy_write(dev, 0x04A2, (b43_phy_read(dev, 0x04A2)
3036 & 0xFFF0) | 0x000B);
3038 if (phy->rev >= 3) {
3039 b43_phy_write(dev, 0x048A, b43_phy_read(dev, 0x048A)
3041 b43_phy_write(dev, 0x0415, (b43_phy_read(dev, 0x0415)
3042 & 0x8000) | 0x36D8);
3043 b43_phy_write(dev, 0x0416, (b43_phy_read(dev, 0x0416)
3044 & 0x8000) | 0x36D8);
3045 b43_phy_write(dev, 0x0417, (b43_phy_read(dev, 0x0417)
3046 & 0xFE00) | 0x016D);
3048 b43_phy_write(dev, 0x048A, b43_phy_read(dev, 0x048A)
3050 b43_phy_write(dev, 0x048A, (b43_phy_read(dev, 0x048A)
3051 & 0x9FFF) | 0x2000);
3052 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_ACIW);
3054 if (phy->rev >= 2) {
3055 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B)
3058 b43_phy_write(dev, 0x048C, (b43_phy_read(dev, 0x048C)
3059 & 0xF0FF) | 0x0200);
3060 if (phy->rev == 2) {
3061 b43_phy_write(dev, 0x04AE, (b43_phy_read(dev, 0x04AE)
3062 & 0xFF00) | 0x007F);
3063 b43_phy_write(dev, 0x04AD, (b43_phy_read(dev, 0x04AD)
3064 & 0x00FF) | 0x1300);
3065 } else if (phy->rev >= 6) {
3066 b43_ofdmtab_write16(dev, 0x1A00, 0x3, 0x007F);
3067 b43_ofdmtab_write16(dev, 0x1A00, 0x2, 0x007F);
3068 b43_phy_write(dev, 0x04AD, b43_phy_read(dev, 0x04AD)
3071 b43_calc_nrssi_slope(dev);
3079 b43_radio_interference_mitigation_disable(struct b43_wldev *dev, int mode)
3081 struct b43_phy *phy = &dev->phy;
3082 u32 *stack = phy->interfstack;
3085 case B43_INTERFMODE_NONWLAN:
3086 if (phy->rev != 1) {
3087 b43_phy_write(dev, 0x042B,
3088 b43_phy_read(dev, 0x042B) & ~0x0800);
3089 b43_phy_write(dev, B43_PHY_G_CRS,
3091 B43_PHY_G_CRS) | 0x4000);
3094 radio_stackrestore(0x0078);
3095 b43_calc_nrssi_threshold(dev);
3096 phy_stackrestore(0x0406);
3097 b43_phy_write(dev, 0x042B, b43_phy_read(dev, 0x042B) & ~0x0800);
3098 if (!dev->bad_frames_preempt) {
3099 b43_phy_write(dev, B43_PHY_RADIO_BITFIELD,
3100 b43_phy_read(dev, B43_PHY_RADIO_BITFIELD)
3103 b43_phy_write(dev, B43_PHY_G_CRS,
3104 b43_phy_read(dev, B43_PHY_G_CRS) | 0x4000);
3105 phy_stackrestore(0x04A0);
3106 phy_stackrestore(0x04A1);
3107 phy_stackrestore(0x04A2);
3108 phy_stackrestore(0x04A8);
3109 phy_stackrestore(0x04AB);
3110 phy_stackrestore(0x04A7);
3111 phy_stackrestore(0x04A3);
3112 phy_stackrestore(0x04A9);
3113 phy_stackrestore(0x0493);
3114 phy_stackrestore(0x04AA);
3115 phy_stackrestore(0x04AC);
3117 case B43_INTERFMODE_MANUALWLAN:
3118 if (!(b43_phy_read(dev, 0x0033) & 0x0800))
3121 phy->aci_enable = 0;
3123 phy_stackrestore(B43_PHY_RADIO_BITFIELD);
3124 phy_stackrestore(B43_PHY_G_CRS);
3125 phy_stackrestore(0x0033);
3126 phy_stackrestore(0x04A3);
3127 phy_stackrestore(0x04A9);
3128 phy_stackrestore(0x0493);
3129 phy_stackrestore(0x04AA);
3130 phy_stackrestore(0x04AC);
3131 phy_stackrestore(0x04A0);
3132 phy_stackrestore(0x04A7);
3133 if (phy->rev >= 2) {
3134 phy_stackrestore(0x04C0);
3135 phy_stackrestore(0x04C1);
3137 phy_stackrestore(0x0406);
3138 phy_stackrestore(0x04A1);
3139 phy_stackrestore(0x04AB);
3140 phy_stackrestore(0x04A8);
3141 if (phy->rev == 2) {
3142 phy_stackrestore(0x04AD);
3143 phy_stackrestore(0x04AE);
3144 } else if (phy->rev >= 3) {
3145 phy_stackrestore(0x04AD);
3146 phy_stackrestore(0x0415);
3147 phy_stackrestore(0x0416);
3148 phy_stackrestore(0x0417);
3149 ofdmtab_stackrestore(0x1A00, 0x2);
3150 ofdmtab_stackrestore(0x1A00, 0x3);
3152 phy_stackrestore(0x04A2);
3153 phy_stackrestore(0x048A);
3154 phy_stackrestore(0x042B);
3155 phy_stackrestore(0x048C);
3156 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_ACIW);
3157 b43_calc_nrssi_slope(dev);
3164 #undef phy_stacksave
3165 #undef phy_stackrestore
3166 #undef radio_stacksave
3167 #undef radio_stackrestore
3168 #undef ofdmtab_stacksave
3169 #undef ofdmtab_stackrestore
3171 int b43_radio_set_interference_mitigation(struct b43_wldev *dev, int mode)
3173 struct b43_phy *phy = &dev->phy;
3176 if ((phy->type != B43_PHYTYPE_G) || (phy->rev == 0) || (!phy->gmode))
3179 phy->aci_wlan_automatic = 0;
3181 case B43_INTERFMODE_AUTOWLAN:
3182 phy->aci_wlan_automatic = 1;
3183 if (phy->aci_enable)
3184 mode = B43_INTERFMODE_MANUALWLAN;
3186 mode = B43_INTERFMODE_NONE;
3188 case B43_INTERFMODE_NONE:
3189 case B43_INTERFMODE_NONWLAN:
3190 case B43_INTERFMODE_MANUALWLAN:
3196 currentmode = phy->interfmode;
3197 if (currentmode == mode)
3199 if (currentmode != B43_INTERFMODE_NONE)
3200 b43_radio_interference_mitigation_disable(dev, currentmode);
3202 if (mode == B43_INTERFMODE_NONE) {
3203 phy->aci_enable = 0;
3204 phy->aci_hw_rssi = 0;
3206 b43_radio_interference_mitigation_enable(dev, mode);
3207 phy->interfmode = mode;
3212 static u16 b43_radio_core_calibration_value(struct b43_wldev *dev)
3214 u16 reg, index, ret;
3216 static const u8 rcc_table[] = {
3217 0x02, 0x03, 0x01, 0x0F,
3218 0x06, 0x07, 0x05, 0x0F,
3219 0x0A, 0x0B, 0x09, 0x0F,
3220 0x0E, 0x0F, 0x0D, 0x0F,
3223 reg = b43_radio_read16(dev, 0x60);
3224 index = (reg & 0x001E) >> 1;
3225 ret = rcc_table[index] << 1;
3226 ret |= (reg & 0x0001);
3232 #define LPD(L, P, D) (((L) << 2) | ((P) << 1) | ((D) << 0))
3233 static u16 radio2050_rfover_val(struct b43_wldev *dev,
3234 u16 phy_register, unsigned int lpd)
3236 struct b43_phy *phy = &dev->phy;
3237 struct ssb_sprom *sprom = &(dev->dev->bus->sprom);
3242 if (has_loopback_gain(phy)) {
3243 int max_lb_gain = phy->max_lb_gain;
3247 if (phy->radio_rev == 8)
3248 max_lb_gain += 0x3E;
3250 max_lb_gain += 0x26;
3251 if (max_lb_gain >= 0x46) {
3253 max_lb_gain -= 0x46;
3254 } else if (max_lb_gain >= 0x3A) {
3256 max_lb_gain -= 0x3A;
3257 } else if (max_lb_gain >= 0x2E) {
3259 max_lb_gain -= 0x2E;
3262 max_lb_gain -= 0x10;
3265 for (i = 0; i < 16; i++) {
3266 max_lb_gain -= (i * 6);
3267 if (max_lb_gain < 6)
3271 if ((phy->rev < 7) ||
3272 !(sprom->boardflags_lo & B43_BFL_EXTLNA)) {
3273 if (phy_register == B43_PHY_RFOVER) {
3275 } else if (phy_register == B43_PHY_RFOVERVAL) {
3282 return (0x0092 | extlna);
3284 return (0x0093 | extlna);
3290 if (phy_register == B43_PHY_RFOVER) {
3292 } else if (phy_register == B43_PHY_RFOVERVAL) {
3300 return (0x8092 | extlna);
3302 return (0x2092 | extlna);
3304 return (0x2093 | extlna);
3311 if ((phy->rev < 7) ||
3312 !(sprom->boardflags_lo & B43_BFL_EXTLNA)) {
3313 if (phy_register == B43_PHY_RFOVER) {
3315 } else if (phy_register == B43_PHY_RFOVERVAL) {
3330 if (phy_register == B43_PHY_RFOVER) {
3332 } else if (phy_register == B43_PHY_RFOVERVAL) {
3351 struct init2050_saved_values {
3352 /* Core registers */
3356 /* Radio registers */
3369 u16 phy_analogoverval;
3377 u16 b43_radio_init2050(struct b43_wldev *dev)
3379 struct b43_phy *phy = &dev->phy;
3380 struct init2050_saved_values sav;
3385 u32 tmp1 = 0, tmp2 = 0;
3387 memset(&sav, 0, sizeof(sav)); /* get rid of "may be used uninitialized..." */
3389 sav.radio_43 = b43_radio_read16(dev, 0x43);
3390 sav.radio_51 = b43_radio_read16(dev, 0x51);
3391 sav.radio_52 = b43_radio_read16(dev, 0x52);
3392 sav.phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
3393 sav.phy_cck_5A = b43_phy_read(dev, B43_PHY_CCK(0x5A));
3394 sav.phy_cck_59 = b43_phy_read(dev, B43_PHY_CCK(0x59));
3395 sav.phy_cck_58 = b43_phy_read(dev, B43_PHY_CCK(0x58));
3397 if (phy->type == B43_PHYTYPE_B) {
3398 sav.phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
3399 sav.reg_3EC = b43_read16(dev, 0x3EC);
3401 b43_phy_write(dev, B43_PHY_CCK(0x30), 0xFF);
3402 b43_write16(dev, 0x3EC, 0x3F3F);
3403 } else if (phy->gmode || phy->rev >= 2) {
3404 sav.phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
3405 sav.phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
3406 sav.phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
3407 sav.phy_analogoverval =
3408 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
3409 sav.phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
3410 sav.phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
3412 b43_phy_write(dev, B43_PHY_ANALOGOVER,
3413 b43_phy_read(dev, B43_PHY_ANALOGOVER)
3415 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
3416 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
3418 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
3420 b43_phy_write(dev, B43_PHY_CLASSCTL,
3421 b43_phy_read(dev, B43_PHY_CLASSCTL)
3423 if (has_loopback_gain(phy)) {
3424 sav.phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
3425 sav.phy_lo_ctl = b43_phy_read(dev, B43_PHY_LO_CTL);
3428 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC020);
3430 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8020);
3431 b43_phy_write(dev, B43_PHY_LO_CTL, 0);
3434 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3435 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3437 b43_phy_write(dev, B43_PHY_RFOVER,
3438 radio2050_rfover_val(dev, B43_PHY_RFOVER, 0));
3440 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2) | 0x8000);
3442 sav.phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
3443 b43_phy_write(dev, B43_PHY_SYNCCTL, b43_phy_read(dev, B43_PHY_SYNCCTL)
3445 sav.reg_3E6 = b43_read16(dev, 0x3E6);
3446 sav.reg_3F4 = b43_read16(dev, 0x3F4);
3448 if (phy->analog == 0) {
3449 b43_write16(dev, 0x03E6, 0x0122);
3451 if (phy->analog >= 2) {
3452 b43_phy_write(dev, B43_PHY_CCK(0x03),
3453 (b43_phy_read(dev, B43_PHY_CCK(0x03))
3456 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3457 (b43_read16(dev, B43_MMIO_CHANNEL_EXT) | 0x2000));
3460 rcc = b43_radio_core_calibration_value(dev);
3462 if (phy->type == B43_PHYTYPE_B)
3463 b43_radio_write16(dev, 0x78, 0x26);
3464 if (phy->gmode || phy->rev >= 2) {
3465 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3466 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3469 b43_phy_write(dev, B43_PHY_PGACTL, 0xBFAF);
3470 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1403);
3471 if (phy->gmode || phy->rev >= 2) {
3472 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3473 radio2050_rfover_val(dev, B43_PHY_RFOVERVAL,
3476 b43_phy_write(dev, B43_PHY_PGACTL, 0xBFA0);
3477 b43_radio_write16(dev, 0x51, b43_radio_read16(dev, 0x51)
3479 if (phy->radio_rev == 8) {
3480 b43_radio_write16(dev, 0x43, 0x1F);
3482 b43_radio_write16(dev, 0x52, 0);
3483 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
3484 & 0xFFF0) | 0x0009);
3486 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3488 for (i = 0; i < 16; i++) {
3489 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0480);
3490 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
3491 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
3492 if (phy->gmode || phy->rev >= 2) {
3493 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3494 radio2050_rfover_val(dev,
3498 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3500 if (phy->gmode || phy->rev >= 2) {
3501 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3502 radio2050_rfover_val(dev,
3506 b43_phy_write(dev, B43_PHY_PGACTL, 0xEFB0);
3508 if (phy->gmode || phy->rev >= 2) {
3509 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3510 radio2050_rfover_val(dev,
3514 b43_phy_write(dev, B43_PHY_PGACTL, 0xFFF0);
3516 tmp1 += b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
3517 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3518 if (phy->gmode || phy->rev >= 2) {
3519 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3520 radio2050_rfover_val(dev,
3524 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3528 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3532 for (i = 0; i < 16; i++) {
3533 radio78 = ((flip_4bit(i) << 1) | 0x20);
3534 b43_radio_write16(dev, 0x78, radio78);
3536 for (j = 0; j < 16; j++) {
3537 b43_phy_write(dev, B43_PHY_CCK(0x5A), 0x0D80);
3538 b43_phy_write(dev, B43_PHY_CCK(0x59), 0xC810);
3539 b43_phy_write(dev, B43_PHY_CCK(0x58), 0x000D);
3540 if (phy->gmode || phy->rev >= 2) {
3541 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3542 radio2050_rfover_val(dev,
3547 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3549 if (phy->gmode || phy->rev >= 2) {
3550 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3551 radio2050_rfover_val(dev,
3556 b43_phy_write(dev, B43_PHY_PGACTL, 0xEFB0);
3558 if (phy->gmode || phy->rev >= 2) {
3559 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3560 radio2050_rfover_val(dev,
3565 b43_phy_write(dev, B43_PHY_PGACTL, 0xFFF0);
3567 tmp2 += b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
3568 b43_phy_write(dev, B43_PHY_CCK(0x58), 0);
3569 if (phy->gmode || phy->rev >= 2) {
3570 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3571 radio2050_rfover_val(dev,
3576 b43_phy_write(dev, B43_PHY_PGACTL, 0xAFB0);
3584 /* Restore the registers */
3585 b43_phy_write(dev, B43_PHY_PGACTL, sav.phy_pgactl);
3586 b43_radio_write16(dev, 0x51, sav.radio_51);
3587 b43_radio_write16(dev, 0x52, sav.radio_52);
3588 b43_radio_write16(dev, 0x43, sav.radio_43);
3589 b43_phy_write(dev, B43_PHY_CCK(0x5A), sav.phy_cck_5A);
3590 b43_phy_write(dev, B43_PHY_CCK(0x59), sav.phy_cck_59);
3591 b43_phy_write(dev, B43_PHY_CCK(0x58), sav.phy_cck_58);
3592 b43_write16(dev, 0x3E6, sav.reg_3E6);
3593 if (phy->analog != 0)
3594 b43_write16(dev, 0x3F4, sav.reg_3F4);
3595 b43_phy_write(dev, B43_PHY_SYNCCTL, sav.phy_syncctl);
3596 b43_synth_pu_workaround(dev, phy->channel);
3597 if (phy->type == B43_PHYTYPE_B) {
3598 b43_phy_write(dev, B43_PHY_CCK(0x30), sav.phy_cck_30);
3599 b43_write16(dev, 0x3EC, sav.reg_3EC);
3600 } else if (phy->gmode) {
3601 b43_write16(dev, B43_MMIO_PHY_RADIO,
3602 b43_read16(dev, B43_MMIO_PHY_RADIO)
3604 b43_phy_write(dev, B43_PHY_RFOVER, sav.phy_rfover);
3605 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav.phy_rfoverval);
3606 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav.phy_analogover);
3607 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
3608 sav.phy_analogoverval);
3609 b43_phy_write(dev, B43_PHY_CRS0, sav.phy_crs0);
3610 b43_phy_write(dev, B43_PHY_CLASSCTL, sav.phy_classctl);
3611 if (has_loopback_gain(phy)) {
3612 b43_phy_write(dev, B43_PHY_LO_MASK, sav.phy_lo_mask);
3613 b43_phy_write(dev, B43_PHY_LO_CTL, sav.phy_lo_ctl);
3624 void b43_radio_init2060(struct b43_wldev *dev)
3628 b43_radio_write16(dev, 0x0004, 0x00C0);
3629 b43_radio_write16(dev, 0x0005, 0x0008);
3630 b43_radio_write16(dev, 0x0009, 0x0040);
3631 b43_radio_write16(dev, 0x0005, 0x00AA);
3632 b43_radio_write16(dev, 0x0032, 0x008F);
3633 b43_radio_write16(dev, 0x0006, 0x008F);
3634 b43_radio_write16(dev, 0x0034, 0x008F);
3635 b43_radio_write16(dev, 0x002C, 0x0007);
3636 b43_radio_write16(dev, 0x0082, 0x0080);
3637 b43_radio_write16(dev, 0x0080, 0x0000);
3638 b43_radio_write16(dev, 0x003F, 0x00DA);
3639 b43_radio_write16(dev, 0x0005, b43_radio_read16(dev, 0x0005) & ~0x0008);
3640 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0010);
3641 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0020);
3642 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0020);
3643 msleep(1); /* delay 400usec */
3645 b43_radio_write16(dev, 0x0081,
3646 (b43_radio_read16(dev, 0x0081) & ~0x0020) | 0x0010);
3647 msleep(1); /* delay 400usec */
3649 b43_radio_write16(dev, 0x0005,
3650 (b43_radio_read16(dev, 0x0005) & ~0x0008) | 0x0008);
3651 b43_radio_write16(dev, 0x0085, b43_radio_read16(dev, 0x0085) & ~0x0010);
3652 b43_radio_write16(dev, 0x0005, b43_radio_read16(dev, 0x0005) & ~0x0008);
3653 b43_radio_write16(dev, 0x0081, b43_radio_read16(dev, 0x0081) & ~0x0040);
3654 b43_radio_write16(dev, 0x0081,
3655 (b43_radio_read16(dev, 0x0081) & ~0x0040) | 0x0040);
3656 b43_radio_write16(dev, 0x0005,
3657 (b43_radio_read16(dev, 0x0081) & ~0x0008) | 0x0008);
3658 b43_phy_write(dev, 0x0063, 0xDDC6);
3659 b43_phy_write(dev, 0x0069, 0x07BE);
3660 b43_phy_write(dev, 0x006A, 0x0000);
3662 err = b43_radio_selectchannel(dev, B43_DEFAULT_CHANNEL_A, 0);
3668 static inline u16 freq_r3A_value(u16 frequency)
3672 if (frequency < 5091)
3674 else if (frequency < 5321)
3676 else if (frequency < 5806)
3684 void b43_radio_set_tx_iq(struct b43_wldev *dev)
3686 static const u8 data_high[5] = { 0x00, 0x40, 0x80, 0x90, 0xD0 };
3687 static const u8 data_low[5] = { 0x00, 0x01, 0x05, 0x06, 0x0A };
3688 u16 tmp = b43_radio_read16(dev, 0x001E);
3691 for (i = 0; i < 5; i++) {
3692 for (j = 0; j < 5; j++) {
3693 if (tmp == (data_high[i] << 4 | data_low[j])) {
3694 b43_phy_write(dev, 0x0069,
3695 (i - j) << 8 | 0x00C0);
3702 int b43_radio_selectchannel(struct b43_wldev *dev,
3703 u8 channel, int synthetic_pu_workaround)
3705 struct b43_phy *phy = &dev->phy;
3708 u16 channelcookie, savedcookie;
3711 if (channel == 0xFF) {
3712 switch (phy->type) {
3714 channel = B43_DEFAULT_CHANNEL_A;
3718 channel = B43_DEFAULT_CHANNEL_BG;
3721 //FIXME check if we are on 2.4GHz or 5GHz and set a default channel.
3729 /* First we set the channel radio code to prevent the
3730 * firmware from sending ghost packets.
3732 channelcookie = channel;
3733 if (0 /*FIXME on 5Ghz */)
3734 channelcookie |= 0x100;
3735 //FIXME set 40Mhz flag if required
3736 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
3737 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
3739 switch (phy->type) {
3741 if (channel > 200) {
3745 freq = channel2freq_a(channel);
3747 r8 = b43_radio_read16(dev, 0x0008);
3748 b43_write16(dev, 0x03F0, freq);
3749 b43_radio_write16(dev, 0x0008, r8);
3751 //TODO: write max channel TX power? to Radio 0x2D
3752 tmp = b43_radio_read16(dev, 0x002E);
3754 //TODO: OR tmp with the Power out estimation for this channel?
3755 b43_radio_write16(dev, 0x002E, tmp);
3757 if (freq >= 4920 && freq <= 5500) {
3759 * r8 = (((freq * 15 * 0xE1FC780F) >> 32) / 29) & 0x0F;
3760 * = (freq * 0.025862069
3762 r8 = 3 * freq / 116; /* is equal to r8 = freq * 0.025862 */
3764 b43_radio_write16(dev, 0x0007, (r8 << 4) | r8);
3765 b43_radio_write16(dev, 0x0020, (r8 << 4) | r8);
3766 b43_radio_write16(dev, 0x0021, (r8 << 4) | r8);
3767 b43_radio_write16(dev, 0x0022, (b43_radio_read16(dev, 0x0022)
3768 & 0x000F) | (r8 << 4));
3769 b43_radio_write16(dev, 0x002A, (r8 << 4));
3770 b43_radio_write16(dev, 0x002B, (r8 << 4));
3771 b43_radio_write16(dev, 0x0008, (b43_radio_read16(dev, 0x0008)
3772 & 0x00F0) | (r8 << 4));
3773 b43_radio_write16(dev, 0x0029, (b43_radio_read16(dev, 0x0029)
3774 & 0xFF0F) | 0x00B0);
3775 b43_radio_write16(dev, 0x0035, 0x00AA);
3776 b43_radio_write16(dev, 0x0036, 0x0085);
3777 b43_radio_write16(dev, 0x003A, (b43_radio_read16(dev, 0x003A)
3779 freq_r3A_value(freq));
3780 b43_radio_write16(dev, 0x003D,
3781 b43_radio_read16(dev, 0x003D) & 0x00FF);
3782 b43_radio_write16(dev, 0x0081, (b43_radio_read16(dev, 0x0081)
3783 & 0xFF7F) | 0x0080);
3784 b43_radio_write16(dev, 0x0035,
3785 b43_radio_read16(dev, 0x0035) & 0xFFEF);
3786 b43_radio_write16(dev, 0x0035, (b43_radio_read16(dev, 0x0035)
3787 & 0xFFEF) | 0x0010);
3788 b43_radio_set_tx_iq(dev);
3789 //TODO: TSSI2dbm workaround
3790 b43_phy_xmitpower(dev); //FIXME correct?
3793 if ((channel < 1) || (channel > 14)) {
3798 if (synthetic_pu_workaround)
3799 b43_synth_pu_workaround(dev, channel);
3801 b43_write16(dev, B43_MMIO_CHANNEL, channel2freq_bg(channel));
3803 if (channel == 14) {
3804 if (dev->dev->bus->sprom.country_code ==
3805 SSB_SPROM1CCODE_JAPAN)
3807 b43_hf_read(dev) & ~B43_HF_ACPR);
3810 b43_hf_read(dev) | B43_HF_ACPR);
3811 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3812 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
3815 b43_write16(dev, B43_MMIO_CHANNEL_EXT,
3816 b43_read16(dev, B43_MMIO_CHANNEL_EXT)
3821 err = b43_nphy_selectchannel(dev, channel);
3829 phy->channel = channel;
3830 /* Wait for the radio to tune to the channel and stabilize. */
3834 b43_shm_write16(dev, B43_SHM_SHARED,
3835 B43_SHM_SH_CHAN, savedcookie);
3840 void b43_radio_turn_on(struct b43_wldev *dev)
3842 struct b43_phy *phy = &dev->phy;
3851 switch (phy->type) {
3853 b43_radio_write16(dev, 0x0004, 0x00C0);
3854 b43_radio_write16(dev, 0x0005, 0x0008);
3855 b43_phy_write(dev, 0x0010, b43_phy_read(dev, 0x0010) & 0xFFF7);
3856 b43_phy_write(dev, 0x0011, b43_phy_read(dev, 0x0011) & 0xFFF7);
3857 b43_radio_init2060(dev);
3861 b43_phy_write(dev, 0x0015, 0x8000);
3862 b43_phy_write(dev, 0x0015, 0xCC00);
3863 b43_phy_write(dev, 0x0015, (phy->gmode ? 0x00C0 : 0x0000));
3864 if (phy->radio_off_context.valid) {
3865 /* Restore the RFover values. */
3866 b43_phy_write(dev, B43_PHY_RFOVER,
3867 phy->radio_off_context.rfover);
3868 b43_phy_write(dev, B43_PHY_RFOVERVAL,
3869 phy->radio_off_context.rfoverval);
3870 phy->radio_off_context.valid = 0;
3872 channel = phy->channel;
3873 err = b43_radio_selectchannel(dev, B43_DEFAULT_CHANNEL_BG, 1);
3874 err |= b43_radio_selectchannel(dev, channel, 0);
3878 b43_nphy_radio_turn_on(dev);
3886 void b43_radio_turn_off(struct b43_wldev *dev, bool force)
3888 struct b43_phy *phy = &dev->phy;
3890 if (!phy->radio_on && !force)
3893 switch (phy->type) {
3895 b43_nphy_radio_turn_off(dev);
3898 b43_radio_write16(dev, 0x0004, 0x00FF);
3899 b43_radio_write16(dev, 0x0005, 0x00FB);
3900 b43_phy_write(dev, 0x0010, b43_phy_read(dev, 0x0010) | 0x0008);
3901 b43_phy_write(dev, 0x0011, b43_phy_read(dev, 0x0011) | 0x0008);
3903 case B43_PHYTYPE_G: {
3904 u16 rfover, rfoverval;
3906 rfover = b43_phy_read(dev, B43_PHY_RFOVER);
3907 rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
3909 phy->radio_off_context.rfover = rfover;
3910 phy->radio_off_context.rfoverval = rfoverval;
3911 phy->radio_off_context.valid = 1;
3913 b43_phy_write(dev, B43_PHY_RFOVER, rfover | 0x008C);
3914 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfoverval & 0xFF73);