3 Broadcom B43 wireless driver
5 G PHY LO (LocalOscillator) Measuring and Control routines
7 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
8 Copyright (c) 2005, 2006 Stefano Brivio <stefano.brivio@polimi.it>
9 Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
10 Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
26 Boston, MA 02110-1301, USA.
35 #include <linux/delay.h>
36 #include <linux/sched.h>
39 /* Define to 1 to always calibrate all possible LO control pairs.
40 * This is a workaround until we fix the partial LO calibration optimization. */
41 #define B43_CALIB_ALL_LOCTLS 1
44 /* Write the LocalOscillator Control (adjust) value-pair. */
45 static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
47 struct b43_phy *phy = &dev->phy;
52 if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
53 b43dbg(dev->wl, "Invalid LO control pair "
54 "(I: %d, Q: %d)\n", control->i, control->q);
60 value = (u8) (control->q);
61 value |= ((u8) (control->i)) << 8;
63 reg = (phy->type == B43_PHYTYPE_B) ? 0x002F : B43_PHY_LO_CTL;
64 b43_phy_write(dev, reg, value);
67 static int assert_rfatt_and_bbatt(const struct b43_rfatt *rfatt,
68 const struct b43_bbatt *bbatt,
69 struct b43_wldev *dev)
73 /* Check the attenuation values against the LO control array sizes. */
74 if (unlikely(rfatt->att >= B43_NR_RF)) {
75 b43err(dev->wl, "rfatt(%u) >= size of LO array\n", rfatt->att);
78 if (unlikely(bbatt->att >= B43_NR_BB)) {
79 b43err(dev->wl, "bbatt(%u) >= size of LO array\n", bbatt->att);
86 #if !B43_CALIB_ALL_LOCTLS
88 struct b43_loctl *b43_get_lo_g_ctl_nopadmix(struct b43_wldev *dev,
89 const struct b43_rfatt *rfatt,
90 const struct b43_bbatt *bbatt)
92 struct b43_phy *phy = &dev->phy;
93 struct b43_txpower_lo_control *lo = phy->lo_control;
95 if (assert_rfatt_and_bbatt(rfatt, bbatt, dev))
96 return &(lo->no_padmix[0][0]); /* Just prevent a crash */
97 return &(lo->no_padmix[bbatt->att][rfatt->att]);
99 #endif /* !B43_CALIB_ALL_LOCTLS */
101 struct b43_loctl *b43_get_lo_g_ctl(struct b43_wldev *dev,
102 const struct b43_rfatt *rfatt,
103 const struct b43_bbatt *bbatt)
105 struct b43_phy *phy = &dev->phy;
106 struct b43_txpower_lo_control *lo = phy->lo_control;
108 if (assert_rfatt_and_bbatt(rfatt, bbatt, dev))
109 return &(lo->no_padmix[0][0]); /* Just prevent a crash */
110 if (rfatt->with_padmix)
111 return &(lo->with_padmix[bbatt->att][rfatt->att]);
112 return &(lo->no_padmix[bbatt->att][rfatt->att]);
115 /* Call a function for every possible LO control value-pair. */
116 static void b43_call_for_each_loctl(struct b43_wldev *dev,
117 void (*func) (struct b43_wldev *,
120 struct b43_phy *phy = &dev->phy;
121 struct b43_txpower_lo_control *ctl = phy->lo_control;
124 for (i = 0; i < B43_NR_BB; i++) {
125 for (j = 0; j < B43_NR_RF; j++)
126 func(dev, &(ctl->with_padmix[i][j]));
128 for (i = 0; i < B43_NR_BB; i++) {
129 for (j = 0; j < B43_NR_RF; j++)
130 func(dev, &(ctl->no_padmix[i][j]));
134 static u16 lo_b_r15_loop(struct b43_wldev *dev)
139 for (i = 0; i < 10; i++) {
140 b43_phy_write(dev, 0x0015, 0xAFA0);
142 b43_phy_write(dev, 0x0015, 0xEFA0);
144 b43_phy_write(dev, 0x0015, 0xFFA0);
146 ret += b43_phy_read(dev, 0x002C);
152 void b43_lo_b_measure(struct b43_wldev *dev)
154 struct b43_phy *phy = &dev->phy;
155 u16 regstack[12] = { 0 };
160 regstack[0] = b43_phy_read(dev, 0x0015);
161 regstack[1] = b43_radio_read16(dev, 0x0052) & 0xFFF0;
163 if (phy->radio_ver == 0x2053) {
164 regstack[2] = b43_phy_read(dev, 0x000A);
165 regstack[3] = b43_phy_read(dev, 0x002A);
166 regstack[4] = b43_phy_read(dev, 0x0035);
167 regstack[5] = b43_phy_read(dev, 0x0003);
168 regstack[6] = b43_phy_read(dev, 0x0001);
169 regstack[7] = b43_phy_read(dev, 0x0030);
171 regstack[8] = b43_radio_read16(dev, 0x0043);
172 regstack[9] = b43_radio_read16(dev, 0x007A);
173 regstack[10] = b43_read16(dev, 0x03EC);
174 regstack[11] = b43_radio_read16(dev, 0x0052) & 0x00F0;
176 b43_phy_write(dev, 0x0030, 0x00FF);
177 b43_write16(dev, 0x03EC, 0x3F3F);
178 b43_phy_write(dev, 0x0035, regstack[4] & 0xFF7F);
179 b43_radio_write16(dev, 0x007A, regstack[9] & 0xFFF0);
181 b43_phy_write(dev, 0x0015, 0xB000);
182 b43_phy_write(dev, 0x002B, 0x0004);
184 if (phy->radio_ver == 0x2053) {
185 b43_phy_write(dev, 0x002B, 0x0203);
186 b43_phy_write(dev, 0x002A, 0x08A3);
189 phy->minlowsig[0] = 0xFFFF;
191 for (i = 0; i < 4; i++) {
192 b43_radio_write16(dev, 0x0052, regstack[1] | i);
195 for (i = 0; i < 10; i++) {
196 b43_radio_write16(dev, 0x0052, regstack[1] | i);
197 mls = lo_b_r15_loop(dev) / 10;
198 if (mls < phy->minlowsig[0]) {
199 phy->minlowsig[0] = mls;
200 phy->minlowsigpos[0] = i;
203 b43_radio_write16(dev, 0x0052, regstack[1] | phy->minlowsigpos[0]);
205 phy->minlowsig[1] = 0xFFFF;
207 for (i = -4; i < 5; i += 2) {
208 for (j = -4; j < 5; j += 2) {
210 fval = (0x0100 * i) + j + 0x0100;
212 fval = (0x0100 * i) + j;
213 b43_phy_write(dev, 0x002F, fval);
214 mls = lo_b_r15_loop(dev) / 10;
215 if (mls < phy->minlowsig[1]) {
216 phy->minlowsig[1] = mls;
217 phy->minlowsigpos[1] = fval;
221 phy->minlowsigpos[1] += 0x0101;
223 b43_phy_write(dev, 0x002F, phy->minlowsigpos[1]);
224 if (phy->radio_ver == 0x2053) {
225 b43_phy_write(dev, 0x000A, regstack[2]);
226 b43_phy_write(dev, 0x002A, regstack[3]);
227 b43_phy_write(dev, 0x0035, regstack[4]);
228 b43_phy_write(dev, 0x0003, regstack[5]);
229 b43_phy_write(dev, 0x0001, regstack[6]);
230 b43_phy_write(dev, 0x0030, regstack[7]);
232 b43_radio_write16(dev, 0x0043, regstack[8]);
233 b43_radio_write16(dev, 0x007A, regstack[9]);
235 b43_radio_write16(dev, 0x0052,
236 (b43_radio_read16(dev, 0x0052) & 0x000F)
239 b43_write16(dev, 0x03EC, regstack[10]);
241 b43_phy_write(dev, 0x0015, regstack[0]);
244 static u16 lo_measure_feedthrough(struct b43_wldev *dev,
245 u16 lna, u16 pga, u16 trsw_rx)
247 struct b43_phy *phy = &dev->phy;
252 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
253 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
255 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
256 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
257 /*FIXME This assertion fails B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
258 B43_PHY_RFOVERVAL_BW));
260 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
262 /* Construct the RF Override Value */
263 rfover = B43_PHY_RFOVERVAL_UNK;
267 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
269 rfover |= B43_PHY_RFOVERVAL_EXTLNA;
271 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
272 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
274 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
275 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
277 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
278 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
280 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
282 pga |= B43_PHY_PGACTL_UNKNOWN;
283 b43_phy_write(dev, B43_PHY_PGACTL, pga);
285 pga |= B43_PHY_PGACTL_LOWBANDW;
286 b43_phy_write(dev, B43_PHY_PGACTL, pga);
288 pga |= B43_PHY_PGACTL_LPF;
289 b43_phy_write(dev, B43_PHY_PGACTL, pga);
292 feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
294 /* This is a good place to check if we need to relax a bit,
295 * as this is the main function called regularly
296 * in the LO calibration. */
302 /* TXCTL Register and Value Table.
303 * Returns the "TXCTL Register".
304 * "value" is the "TXCTL Value".
305 * "pad_mix_gain" is the PAD Mixer Gain.
307 static u16 lo_txctl_register_table(struct b43_wldev *dev,
308 u16 * value, u16 * pad_mix_gain)
310 struct b43_phy *phy = &dev->phy;
313 if (phy->type == B43_PHYTYPE_B) {
315 if (phy->radio_rev <= 5) {
323 if (phy->rev >= 2 && phy->radio_rev == 8) {
336 *pad_mix_gain = padmix;
341 static void lo_measure_txctl_values(struct b43_wldev *dev)
343 struct b43_phy *phy = &dev->phy;
344 struct b43_txpower_lo_control *lo = phy->lo_control;
349 static const u8 tx_bias_values[] = {
350 0x09, 0x08, 0x0A, 0x01, 0x00,
351 0x02, 0x05, 0x04, 0x06,
353 static const u8 tx_magn_values[] = {
357 if (!has_loopback_gain(phy)) {
362 int lb_gain; /* Loopback gain (in dB) */
365 lb_gain = phy->max_lb_gain / 2;
368 pga = abs(10 - lb_gain) / 6;
369 pga = limit_value(pga, 0, 15);
376 if ((phy->rev >= 2) &&
377 (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
380 if ((10 - lb_gain) < cmp_val)
381 tmp = (10 - lb_gain);
389 radio_pctl_reg = cmp_val;
391 radio_pctl_reg = tmp;
394 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
395 & 0xFFF0) | radio_pctl_reg);
396 b43_phy_set_baseband_attenuation(dev, 2);
398 reg = lo_txctl_register_table(dev, &mask, NULL);
400 b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
403 if (has_tx_magnification(phy)) {
406 int min_feedth = 0xFFFF;
409 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
410 tx_magn = tx_magn_values[i];
411 b43_radio_write16(dev, 0x52,
412 (b43_radio_read16(dev, 0x52)
413 & 0xFF0F) | tx_magn);
414 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
415 tx_bias = tx_bias_values[j];
416 b43_radio_write16(dev, 0x52,
417 (b43_radio_read16(dev, 0x52)
418 & 0xFFF0) | tx_bias);
420 lo_measure_feedthrough(dev, 0, pga,
422 if (feedthrough < min_feedth) {
423 lo->tx_bias = tx_bias;
424 lo->tx_magn = tx_magn;
425 min_feedth = feedthrough;
427 if (lo->tx_bias == 0)
430 b43_radio_write16(dev, 0x52,
431 (b43_radio_read16(dev, 0x52)
432 & 0xFF00) | lo->tx_bias | lo->
438 b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
439 & 0xFFF0); /* TX bias == 0 */
443 static void lo_read_power_vector(struct b43_wldev *dev)
445 struct b43_phy *phy = &dev->phy;
446 struct b43_txpower_lo_control *lo = phy->lo_control;
449 u64 power_vector = 0;
450 int rf_offset, bb_offset;
451 struct b43_loctl *loctl;
453 for (i = 0; i < 8; i += 2) {
454 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
455 /* Clear the top byte. We get holes in the bitmap... */
457 power_vector |= (tmp << (i * 8));
458 /* Clear the vector on the device. */
459 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
463 lo->power_vector = power_vector;
464 power_vector = lo->power_vector;
466 for (i = 0; i < 64; i++) {
467 if (power_vector & ((u64) 1ULL << i)) {
468 /* Now figure out which b43_loctl corresponds
471 rf_offset = i / lo->rfatt_list.len;
472 bb_offset = i % lo->rfatt_list.len; //FIXME?
474 b43_get_lo_g_ctl(dev,
475 &lo->rfatt_list.list[rf_offset],
476 &lo->bbatt_list.list[bb_offset]);
477 /* And mark it as "used", as the device told us
478 * through the bitmap it is using it.
485 /* 802.11/LO/GPHY/MeasuringGains */
486 static void lo_measure_gain_values(struct b43_wldev *dev,
487 s16 max_rx_gain, int use_trsw_rx)
489 struct b43_phy *phy = &dev->phy;
495 if (has_loopback_gain(phy)) {
500 trsw_rx_gain = phy->trsw_rx_gain / 2;
501 if (max_rx_gain >= trsw_rx_gain) {
502 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
506 trsw_rx_gain = max_rx_gain;
507 if (trsw_rx_gain < 9) {
508 phy->lna_lod_gain = 0;
510 phy->lna_lod_gain = 1;
513 trsw_rx_gain = limit_value(trsw_rx_gain, 0, 0x2D);
514 phy->pga_gain = trsw_rx_gain / 3;
515 if (phy->pga_gain >= 5) {
522 phy->trsw_rx_gain = 0x20;
523 if (max_rx_gain >= 0x14) {
524 phy->lna_lod_gain = 1;
526 } else if (max_rx_gain >= 0x12) {
527 phy->lna_lod_gain = 1;
529 } else if (max_rx_gain >= 0xF) {
530 phy->lna_lod_gain = 1;
533 phy->lna_lod_gain = 0;
538 tmp = b43_radio_read16(dev, 0x7A);
539 if (phy->lna_lod_gain == 0)
543 b43_radio_write16(dev, 0x7A, tmp);
546 struct lo_g_saved_values {
556 u16 phy_dacctl_hwpctl;
559 u16 phy_hpwr_tssictl;
561 u16 phy_analogoverval;
573 /* Radio registers */
579 static void lo_measure_setup(struct b43_wldev *dev,
580 struct lo_g_saved_values *sav)
582 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
583 struct b43_phy *phy = &dev->phy;
584 struct b43_txpower_lo_control *lo = phy->lo_control;
587 if (b43_has_hardware_pctl(phy)) {
588 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
589 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
590 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
591 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
592 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
594 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
595 b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
597 b43_phy_write(dev, B43_PHY_EXTG(0x01),
598 b43_phy_read(dev, B43_PHY_EXTG(0x01))
600 b43_phy_write(dev, B43_PHY_DACCTL,
601 b43_phy_read(dev, B43_PHY_DACCTL)
603 b43_phy_write(dev, B43_PHY_CCK(0x14),
604 b43_phy_read(dev, B43_PHY_CCK(0x14))
607 if (phy->type == B43_PHYTYPE_B &&
608 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
609 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
610 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
612 if (!lo->rebuild && b43_has_hardware_pctl(phy))
613 lo_read_power_vector(dev);
615 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
616 sav->phy_analogoverval =
617 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
618 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
619 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
620 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
621 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
622 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
624 b43_phy_write(dev, B43_PHY_CLASSCTL,
625 b43_phy_read(dev, B43_PHY_CLASSCTL)
627 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
629 b43_phy_write(dev, B43_PHY_ANALOGOVER,
630 b43_phy_read(dev, B43_PHY_ANALOGOVER)
632 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
633 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
635 if (phy->type == B43_PHYTYPE_G) {
636 if ((phy->rev >= 7) &&
637 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
638 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
640 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
643 b43_phy_write(dev, B43_PHY_RFOVER, 0);
645 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
647 sav->reg_3F4 = b43_read16(dev, 0x3F4);
648 sav->reg_3E2 = b43_read16(dev, 0x3E2);
649 sav->radio_43 = b43_radio_read16(dev, 0x43);
650 sav->radio_7A = b43_radio_read16(dev, 0x7A);
651 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
652 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
653 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
654 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
656 if (!has_tx_magnification(phy)) {
657 sav->radio_52 = b43_radio_read16(dev, 0x52);
658 sav->radio_52 &= 0x00F0;
660 if (phy->type == B43_PHYTYPE_B) {
661 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
662 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
663 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
664 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
666 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
669 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
673 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
674 b43_phy_write(dev, tmp, 0x007F);
676 tmp = sav->phy_syncctl;
677 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
679 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
681 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
682 if (phy->type == B43_PHYTYPE_G ||
683 (phy->type == B43_PHYTYPE_B &&
684 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
685 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
687 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
689 b43_dummy_transmission(dev);
690 b43_radio_selectchannel(dev, 6, 0);
691 b43_radio_read16(dev, 0x51); /* dummy read */
692 if (phy->type == B43_PHYTYPE_G)
693 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
695 lo_measure_txctl_values(dev);
696 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
697 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
699 if (phy->type == B43_PHYTYPE_B)
700 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
702 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
706 static void lo_measure_restore(struct b43_wldev *dev,
707 struct lo_g_saved_values *sav)
709 struct b43_phy *phy = &dev->phy;
710 struct b43_txpower_lo_control *lo = phy->lo_control;
714 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
715 tmp = (phy->pga_gain << 8);
716 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
718 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
720 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
722 tmp = (phy->pga_gain | 0xEFA0);
723 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
725 if (b43_has_hardware_pctl(phy)) {
726 b43_gphy_dc_lt_init(dev);
729 b43_lo_g_adjust_to(dev, 3, 2, 0);
731 b43_lo_g_adjust(dev);
733 if (phy->type == B43_PHYTYPE_G) {
735 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
737 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
739 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
741 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
743 b43_write16(dev, 0x3F4, sav->reg_3F4);
744 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
745 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
746 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
747 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
748 b43_radio_write16(dev, 0x43, sav->radio_43);
749 b43_radio_write16(dev, 0x7A, sav->radio_7A);
750 if (!has_tx_magnification(phy)) {
752 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
755 b43_write16(dev, 0x3E2, sav->reg_3E2);
756 if (phy->type == B43_PHYTYPE_B &&
757 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
758 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
759 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
762 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
763 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
764 sav->phy_analogoverval);
765 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
766 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
767 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
768 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
769 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
771 if (b43_has_hardware_pctl(phy)) {
772 tmp = (sav->phy_lo_mask & 0xBFFF);
773 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
774 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
775 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
776 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
777 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
779 b43_radio_selectchannel(dev, sav->old_channel, 1);
782 struct b43_lo_g_statemachine {
785 int state_val_multiplier;
787 struct b43_loctl min_loctl;
790 /* Loop over each possible value in this state. */
791 static int lo_probe_possible_loctls(struct b43_wldev *dev,
792 struct b43_loctl *probe_loctl,
793 struct b43_lo_g_statemachine *d)
795 struct b43_phy *phy = &dev->phy;
796 struct b43_txpower_lo_control *lo = phy->lo_control;
797 struct b43_loctl test_loctl;
798 struct b43_loctl orig_loctl;
799 struct b43_loctl prev_loctl = {
808 static const struct b43_loctl modifiers[] = {
819 if (d->current_state == 0) {
822 } else if (d->current_state % 2 == 0) {
823 begin = d->current_state - 1;
824 end = d->current_state + 1;
826 begin = d->current_state - 2;
827 end = d->current_state + 2;
834 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
836 d->current_state = i;
838 B43_WARN_ON(!(i >= 1 && i <= 8));
839 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
840 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
841 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
842 if ((test_loctl.i != prev_loctl.i ||
843 test_loctl.q != prev_loctl.q) &&
844 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
845 b43_lo_write(dev, &test_loctl);
846 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
849 if (feedth < d->lowest_feedth) {
850 memcpy(probe_loctl, &test_loctl,
851 sizeof(struct b43_loctl));
853 d->lowest_feedth = feedth;
854 if ((d->nr_measured < 2) &&
855 (!has_loopback_gain(phy) || lo->rebuild))
859 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
866 d->current_state = i;
872 static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
873 struct b43_loctl *loctl,
876 struct b43_phy *phy = &dev->phy;
877 struct b43_txpower_lo_control *lo = phy->lo_control;
878 struct b43_lo_g_statemachine d;
881 struct b43_loctl probe_loctl;
882 int max_repeat = 1, repeat_cnt = 0;
885 d.state_val_multiplier = 1;
886 if (has_loopback_gain(phy) && !lo->rebuild)
887 d.state_val_multiplier = 3;
889 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
890 if (has_loopback_gain(phy) && lo->rebuild)
893 b43_lo_write(dev, &d.min_loctl);
894 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
897 if (!lo->rebuild && feedth < 0x258) {
902 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
906 d.lowest_feedth = feedth;
911 (d.current_state >= 0
912 && d.current_state <= 8));
913 memcpy(&probe_loctl, &d.min_loctl,
914 sizeof(struct b43_loctl));
916 lo_probe_possible_loctls(dev, &probe_loctl, &d);
919 if ((probe_loctl.i == d.min_loctl.i) &&
920 (probe_loctl.q == d.min_loctl.q))
922 memcpy(&d.min_loctl, &probe_loctl,
923 sizeof(struct b43_loctl));
925 } while (d.nr_measured < 24);
926 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
928 if (has_loopback_gain(phy)) {
929 if (d.lowest_feedth > 0x1194)
931 else if (d.lowest_feedth < 0x5DC)
933 if (repeat_cnt == 0) {
934 if (d.lowest_feedth <= 0x5DC) {
935 d.state_val_multiplier = 1;
938 d.state_val_multiplier = 2;
939 } else if (repeat_cnt == 2)
940 d.state_val_multiplier = 1;
942 lo_measure_gain_values(dev, *max_rx_gain,
943 has_loopback_gain(phy));
944 } while (++repeat_cnt < max_repeat);
947 #if B43_CALIB_ALL_LOCTLS
948 static const struct b43_rfatt b43_full_rfatt_list_items[] = {
949 { .att = 0, .with_padmix = 0, },
950 { .att = 1, .with_padmix = 0, },
951 { .att = 2, .with_padmix = 0, },
952 { .att = 3, .with_padmix = 0, },
953 { .att = 4, .with_padmix = 0, },
954 { .att = 5, .with_padmix = 0, },
955 { .att = 6, .with_padmix = 0, },
956 { .att = 7, .with_padmix = 0, },
957 { .att = 8, .with_padmix = 0, },
958 { .att = 9, .with_padmix = 0, },
959 { .att = 10, .with_padmix = 0, },
960 { .att = 11, .with_padmix = 0, },
961 { .att = 12, .with_padmix = 0, },
962 { .att = 13, .with_padmix = 0, },
963 { .att = 14, .with_padmix = 0, },
964 { .att = 15, .with_padmix = 0, },
965 { .att = 0, .with_padmix = 1, },
966 { .att = 1, .with_padmix = 1, },
967 { .att = 2, .with_padmix = 1, },
968 { .att = 3, .with_padmix = 1, },
969 { .att = 4, .with_padmix = 1, },
970 { .att = 5, .with_padmix = 1, },
971 { .att = 6, .with_padmix = 1, },
972 { .att = 7, .with_padmix = 1, },
973 { .att = 8, .with_padmix = 1, },
974 { .att = 9, .with_padmix = 1, },
975 { .att = 10, .with_padmix = 1, },
976 { .att = 11, .with_padmix = 1, },
977 { .att = 12, .with_padmix = 1, },
978 { .att = 13, .with_padmix = 1, },
979 { .att = 14, .with_padmix = 1, },
980 { .att = 15, .with_padmix = 1, },
982 static const struct b43_rfatt_list b43_full_rfatt_list = {
983 .list = b43_full_rfatt_list_items,
984 .len = ARRAY_SIZE(b43_full_rfatt_list_items),
987 static const struct b43_bbatt b43_full_bbatt_list_items[] = {
1001 static const struct b43_bbatt_list b43_full_bbatt_list = {
1002 .list = b43_full_bbatt_list_items,
1003 .len = ARRAY_SIZE(b43_full_bbatt_list_items),
1005 #endif /* B43_CALIB_ALL_LOCTLS */
1007 static void lo_measure(struct b43_wldev *dev)
1009 struct b43_phy *phy = &dev->phy;
1010 struct b43_txpower_lo_control *lo = phy->lo_control;
1011 struct b43_loctl loctl = {
1015 struct b43_loctl *ploctl;
1018 const struct b43_bbatt_list *bbatt_list;
1019 const struct b43_rfatt_list *rfatt_list;
1021 /* Values from the "TXCTL Register and Value Table" */
1026 bbatt_list = &lo->bbatt_list;
1027 rfatt_list = &lo->rfatt_list;
1028 #if B43_CALIB_ALL_LOCTLS
1029 bbatt_list = &b43_full_bbatt_list;
1030 rfatt_list = &b43_full_rfatt_list;
1033 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
1035 for (rfidx = 0; rfidx < rfatt_list->len; rfidx++) {
1037 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
1039 rfatt_list->list[rfidx].att);
1040 b43_radio_write16(dev, txctl_reg,
1041 (b43_radio_read16(dev, txctl_reg)
1043 | (rfatt_list->list[rfidx].with_padmix ?
1046 for (bbidx = 0; bbidx < bbatt_list->len; bbidx++) {
1048 #if B43_CALIB_ALL_LOCTLS
1049 ploctl = b43_get_lo_g_ctl(dev,
1050 &rfatt_list->list[rfidx],
1051 &bbatt_list->list[bbidx]);
1053 ploctl = b43_get_lo_g_ctl_nopadmix(dev,
1060 ploctl = b43_get_lo_g_ctl(dev,
1061 &rfatt_list->list[rfidx],
1062 &bbatt_list->list[bbidx]);
1066 memcpy(&loctl, ploctl, sizeof(loctl));
1070 max_rx_gain = rfatt_list->list[rfidx].att * 2;
1071 max_rx_gain += bbatt_list->list[bbidx].att / 2;
1072 if (rfatt_list->list[rfidx].with_padmix)
1073 max_rx_gain -= pad_mix_gain;
1074 if (has_loopback_gain(phy))
1075 max_rx_gain += phy->max_lb_gain;
1076 lo_measure_gain_values(dev, max_rx_gain,
1077 has_loopback_gain(phy));
1079 b43_phy_set_baseband_attenuation(dev,
1080 bbatt_list->list[bbidx].att);
1081 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
1082 if (phy->type == B43_PHYTYPE_B) {
1086 b43_loctl_set_calibrated(&loctl, 1);
1087 memcpy(ploctl, &loctl, sizeof(loctl));
1093 static void do_validate_loctl(struct b43_wldev *dev, struct b43_loctl *control)
1095 const int is_initializing = (b43_status(dev) == B43_STAT_UNINIT);
1099 if (b43_loctl_is_calibrated(control)) {
1100 if ((abs(i) > 16) || (abs(q) > 16))
1105 if (dev->phy.lo_control->rebuild) {
1108 if ((i != B43_LOCTL_POISON) ||
1109 (q != B43_LOCTL_POISON))
1113 if (is_initializing && control->used)
1118 b43err(dev->wl, "LO control pair validation failed "
1119 "(I: %d, Q: %d, used %u, calib: %u, initing: %d)\n",
1120 i, q, control->used,
1121 b43_loctl_is_calibrated(control),
1125 static void validate_all_loctls(struct b43_wldev *dev)
1127 b43_call_for_each_loctl(dev, do_validate_loctl);
1130 static void do_reset_calib(struct b43_wldev *dev, struct b43_loctl *control)
1132 if (dev->phy.lo_control->rebuild ||
1134 b43_loctl_set_calibrated(control, 0);
1135 control->i = B43_LOCTL_POISON;
1136 control->q = B43_LOCTL_POISON;
1140 static void reset_all_loctl_calibration_states(struct b43_wldev *dev)
1142 b43_call_for_each_loctl(dev, do_reset_calib);
1145 #else /* B43_DEBUG */
1146 static inline void validate_all_loctls(struct b43_wldev *dev) { }
1147 static inline void reset_all_loctl_calibration_states(struct b43_wldev *dev) { }
1148 #endif /* B43_DEBUG */
1150 void b43_lo_g_measure(struct b43_wldev *dev)
1152 struct b43_phy *phy = &dev->phy;
1153 struct lo_g_saved_values uninitialized_var(sav);
1155 B43_WARN_ON((phy->type != B43_PHYTYPE_B) &&
1156 (phy->type != B43_PHYTYPE_G));
1158 sav.old_channel = phy->channel;
1159 lo_measure_setup(dev, &sav);
1160 reset_all_loctl_calibration_states(dev);
1162 lo_measure_restore(dev, &sav);
1164 validate_all_loctls(dev);
1166 phy->lo_control->lo_measured = 1;
1167 phy->lo_control->rebuild = 0;
1171 static void validate_loctl_calibration(struct b43_wldev *dev,
1172 struct b43_loctl *loctl,
1173 struct b43_rfatt *rfatt,
1174 struct b43_bbatt *bbatt)
1176 if (b43_loctl_is_calibrated(loctl))
1178 if (!dev->phy.lo_control->lo_measured) {
1179 /* On init we set the attenuation values before we
1180 * calibrated the LO. I guess that's OK. */
1183 b43err(dev->wl, "Adjusting Local Oscillator to an uncalibrated "
1184 "control pair: rfatt=%u,%spadmix bbatt=%u\n",
1186 (rfatt->with_padmix) ? "" : "no-",
1190 static inline void validate_loctl_calibration(struct b43_wldev *dev,
1191 struct b43_loctl *loctl,
1192 struct b43_rfatt *rfatt,
1193 struct b43_bbatt *bbatt)
1198 static inline void fixup_rfatt_for_txcontrol(struct b43_rfatt *rf,
1201 if (tx_control & B43_TXCTL_TXMIX) {
1207 void b43_lo_g_adjust(struct b43_wldev *dev)
1209 struct b43_phy *phy = &dev->phy;
1210 struct b43_rfatt rf;
1211 struct b43_loctl *loctl;
1213 memcpy(&rf, &phy->rfatt, sizeof(rf));
1214 fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
1216 loctl = b43_get_lo_g_ctl(dev, &rf, &phy->bbatt);
1217 validate_loctl_calibration(dev, loctl, &rf, &phy->bbatt);
1218 b43_lo_write(dev, loctl);
1221 void b43_lo_g_adjust_to(struct b43_wldev *dev,
1222 u16 rfatt, u16 bbatt, u16 tx_control)
1224 struct b43_rfatt rf;
1225 struct b43_bbatt bb;
1226 struct b43_loctl *loctl;
1228 memset(&rf, 0, sizeof(rf));
1229 memset(&bb, 0, sizeof(bb));
1232 fixup_rfatt_for_txcontrol(&rf, tx_control);
1233 loctl = b43_get_lo_g_ctl(dev, &rf, &bb);
1234 validate_loctl_calibration(dev, loctl, &rf, &bb);
1235 b43_lo_write(dev, loctl);
1238 static void do_mark_unused(struct b43_wldev *dev, struct b43_loctl *control)
1243 void b43_lo_g_ctl_mark_all_unused(struct b43_wldev *dev)
1245 struct b43_phy *phy = &dev->phy;
1246 struct b43_txpower_lo_control *lo = phy->lo_control;
1248 b43_call_for_each_loctl(dev, do_mark_unused);
1252 void b43_lo_g_ctl_mark_cur_used(struct b43_wldev *dev)
1254 struct b43_phy *phy = &dev->phy;
1255 struct b43_rfatt rf;
1257 memcpy(&rf, &phy->rfatt, sizeof(rf));
1258 fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
1260 b43_get_lo_g_ctl(dev, &rf, &phy->bbatt)->used = 1;