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>
37 #include <linux/slab.h>
40 static struct b43_lo_calib
*b43_find_lo_calib(struct b43_txpower_lo_control
*lo
,
41 const struct b43_bbatt
*bbatt
,
42 const struct b43_rfatt
*rfatt
)
44 struct b43_lo_calib
*c
;
46 list_for_each_entry(c
, &lo
->calib_list
, list
) {
47 if (!b43_compare_bbatt(&c
->bbatt
, bbatt
))
49 if (!b43_compare_rfatt(&c
->rfatt
, rfatt
))
57 /* Write the LocalOscillator Control (adjust) value-pair. */
58 static void b43_lo_write(struct b43_wldev
*dev
, struct b43_loctl
*control
)
60 struct b43_phy
*phy
= &dev
->phy
;
64 if (unlikely(abs(control
->i
) > 16 || abs(control
->q
) > 16)) {
65 b43dbg(dev
->wl
, "Invalid LO control pair "
66 "(I: %d, Q: %d)\n", control
->i
, control
->q
);
71 B43_WARN_ON(phy
->type
!= B43_PHYTYPE_G
);
73 value
= (u8
) (control
->q
);
74 value
|= ((u8
) (control
->i
)) << 8;
75 b43_phy_write(dev
, B43_PHY_LO_CTL
, value
);
78 static u16
lo_measure_feedthrough(struct b43_wldev
*dev
,
79 u16 lna
, u16 pga
, u16 trsw_rx
)
81 struct b43_phy
*phy
= &dev
->phy
;
86 lna
<<= B43_PHY_RFOVERVAL_LNA_SHIFT
;
87 pga
<<= B43_PHY_RFOVERVAL_PGA_SHIFT
;
89 B43_WARN_ON(lna
& ~B43_PHY_RFOVERVAL_LNA
);
90 B43_WARN_ON(pga
& ~B43_PHY_RFOVERVAL_PGA
);
91 trsw_rx
&= (B43_PHY_RFOVERVAL_TRSWRX
| B43_PHY_RFOVERVAL_BW
);
93 /* Construct the RF Override Value */
94 rfover
= B43_PHY_RFOVERVAL_UNK
;
98 if ((dev
->dev
->bus
->sprom
.boardflags_lo
& B43_BFL_EXTLNA
)
100 rfover
|= B43_PHY_RFOVERVAL_EXTLNA
;
102 b43_phy_write(dev
, B43_PHY_PGACTL
, 0xE300);
103 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, rfover
);
105 rfover
|= B43_PHY_RFOVERVAL_BW_LBW
;
106 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, rfover
);
108 rfover
|= B43_PHY_RFOVERVAL_BW_LPF
;
109 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, rfover
);
111 b43_phy_write(dev
, B43_PHY_PGACTL
, 0xF300);
113 pga
|= B43_PHY_PGACTL_UNKNOWN
;
114 b43_phy_write(dev
, B43_PHY_PGACTL
, pga
);
116 pga
|= B43_PHY_PGACTL_LOWBANDW
;
117 b43_phy_write(dev
, B43_PHY_PGACTL
, pga
);
119 pga
|= B43_PHY_PGACTL_LPF
;
120 b43_phy_write(dev
, B43_PHY_PGACTL
, pga
);
123 feedthrough
= b43_phy_read(dev
, B43_PHY_LO_LEAKAGE
);
125 /* This is a good place to check if we need to relax a bit,
126 * as this is the main function called regularly
127 * in the LO calibration. */
133 /* TXCTL Register and Value Table.
134 * Returns the "TXCTL Register".
135 * "value" is the "TXCTL Value".
136 * "pad_mix_gain" is the PAD Mixer Gain.
138 static u16
lo_txctl_register_table(struct b43_wldev
*dev
,
139 u16
*value
, u16
*pad_mix_gain
)
141 struct b43_phy
*phy
= &dev
->phy
;
144 if (phy
->type
== B43_PHYTYPE_B
) {
146 if (phy
->radio_rev
<= 5) {
154 if (phy
->rev
>= 2 && phy
->radio_rev
== 8) {
167 *pad_mix_gain
= padmix
;
172 static void lo_measure_txctl_values(struct b43_wldev
*dev
)
174 struct b43_phy
*phy
= &dev
->phy
;
175 struct b43_phy_g
*gphy
= phy
->g
;
176 struct b43_txpower_lo_control
*lo
= gphy
->lo_control
;
181 static const u8 tx_bias_values
[] = {
182 0x09, 0x08, 0x0A, 0x01, 0x00,
183 0x02, 0x05, 0x04, 0x06,
185 static const u8 tx_magn_values
[] = {
189 if (!has_loopback_gain(phy
)) {
194 int lb_gain
; /* Loopback gain (in dB) */
197 lb_gain
= gphy
->max_lb_gain
/ 2;
200 pga
= abs(10 - lb_gain
) / 6;
201 pga
= clamp_val(pga
, 0, 15);
208 if ((phy
->rev
>= 2) &&
209 (phy
->radio_ver
== 0x2050) && (phy
->radio_rev
== 8))
212 if ((10 - lb_gain
) < cmp_val
)
213 tmp
= (10 - lb_gain
);
221 radio_pctl_reg
= cmp_val
;
223 radio_pctl_reg
= tmp
;
226 b43_radio_maskset(dev
, 0x43, 0xFFF0, radio_pctl_reg
);
227 b43_gphy_set_baseband_attenuation(dev
, 2);
229 reg
= lo_txctl_register_table(dev
, &mask
, NULL
);
231 b43_radio_mask(dev
, reg
, mask
);
233 if (has_tx_magnification(phy
)) {
236 int min_feedth
= 0xFFFF;
239 for (i
= 0; i
< ARRAY_SIZE(tx_magn_values
); i
++) {
240 tx_magn
= tx_magn_values
[i
];
241 b43_radio_maskset(dev
, 0x52, 0xFF0F, tx_magn
);
242 for (j
= 0; j
< ARRAY_SIZE(tx_bias_values
); j
++) {
243 tx_bias
= tx_bias_values
[j
];
244 b43_radio_maskset(dev
, 0x52, 0xFFF0, tx_bias
);
246 lo_measure_feedthrough(dev
, 0, pga
,
248 if (feedthrough
< min_feedth
) {
249 lo
->tx_bias
= tx_bias
;
250 lo
->tx_magn
= tx_magn
;
251 min_feedth
= feedthrough
;
253 if (lo
->tx_bias
== 0)
256 b43_radio_write16(dev
, 0x52,
257 (b43_radio_read16(dev
, 0x52)
258 & 0xFF00) | lo
->tx_bias
| lo
->
264 b43_radio_mask(dev
, 0x52, 0xFFF0); /* TX bias == 0 */
266 lo
->txctl_measured_time
= jiffies
;
269 static void lo_read_power_vector(struct b43_wldev
*dev
)
271 struct b43_phy
*phy
= &dev
->phy
;
272 struct b43_phy_g
*gphy
= phy
->g
;
273 struct b43_txpower_lo_control
*lo
= gphy
->lo_control
;
276 u64 power_vector
= 0;
278 for (i
= 0; i
< 8; i
+= 2) {
279 tmp
= b43_shm_read16(dev
, B43_SHM_SHARED
, 0x310 + i
);
280 power_vector
|= (tmp
<< (i
* 8));
281 /* Clear the vector on the device. */
282 b43_shm_write16(dev
, B43_SHM_SHARED
, 0x310 + i
, 0);
285 lo
->power_vector
= power_vector
;
286 lo
->pwr_vec_read_time
= jiffies
;
289 /* 802.11/LO/GPHY/MeasuringGains */
290 static void lo_measure_gain_values(struct b43_wldev
*dev
,
291 s16 max_rx_gain
, int use_trsw_rx
)
293 struct b43_phy
*phy
= &dev
->phy
;
294 struct b43_phy_g
*gphy
= phy
->g
;
300 if (has_loopback_gain(phy
)) {
305 trsw_rx_gain
= gphy
->trsw_rx_gain
/ 2;
306 if (max_rx_gain
>= trsw_rx_gain
) {
307 trsw_rx_gain
= max_rx_gain
- trsw_rx_gain
;
311 trsw_rx_gain
= max_rx_gain
;
312 if (trsw_rx_gain
< 9) {
313 gphy
->lna_lod_gain
= 0;
315 gphy
->lna_lod_gain
= 1;
318 trsw_rx_gain
= clamp_val(trsw_rx_gain
, 0, 0x2D);
319 gphy
->pga_gain
= trsw_rx_gain
/ 3;
320 if (gphy
->pga_gain
>= 5) {
327 gphy
->trsw_rx_gain
= 0x20;
328 if (max_rx_gain
>= 0x14) {
329 gphy
->lna_lod_gain
= 1;
331 } else if (max_rx_gain
>= 0x12) {
332 gphy
->lna_lod_gain
= 1;
334 } else if (max_rx_gain
>= 0xF) {
335 gphy
->lna_lod_gain
= 1;
338 gphy
->lna_lod_gain
= 0;
343 tmp
= b43_radio_read16(dev
, 0x7A);
344 if (gphy
->lna_lod_gain
== 0)
348 b43_radio_write16(dev
, 0x7A, tmp
);
351 struct lo_g_saved_values
{
361 u16 phy_dacctl_hwpctl
;
364 u16 phy_hpwr_tssictl
;
366 u16 phy_analogoverval
;
378 /* Radio registers */
384 static void lo_measure_setup(struct b43_wldev
*dev
,
385 struct lo_g_saved_values
*sav
)
387 struct ssb_sprom
*sprom
= &dev
->dev
->bus
->sprom
;
388 struct b43_phy
*phy
= &dev
->phy
;
389 struct b43_phy_g
*gphy
= phy
->g
;
390 struct b43_txpower_lo_control
*lo
= gphy
->lo_control
;
393 if (b43_has_hardware_pctl(dev
)) {
394 sav
->phy_lo_mask
= b43_phy_read(dev
, B43_PHY_LO_MASK
);
395 sav
->phy_extg_01
= b43_phy_read(dev
, B43_PHY_EXTG(0x01));
396 sav
->phy_dacctl_hwpctl
= b43_phy_read(dev
, B43_PHY_DACCTL
);
397 sav
->phy_cck_14
= b43_phy_read(dev
, B43_PHY_CCK(0x14));
398 sav
->phy_hpwr_tssictl
= b43_phy_read(dev
, B43_PHY_HPWR_TSSICTL
);
400 b43_phy_set(dev
, B43_PHY_HPWR_TSSICTL
, 0x100);
401 b43_phy_set(dev
, B43_PHY_EXTG(0x01), 0x40);
402 b43_phy_set(dev
, B43_PHY_DACCTL
, 0x40);
403 b43_phy_set(dev
, B43_PHY_CCK(0x14), 0x200);
405 if (phy
->type
== B43_PHYTYPE_B
&&
406 phy
->radio_ver
== 0x2050 && phy
->radio_rev
< 6) {
407 b43_phy_write(dev
, B43_PHY_CCK(0x16), 0x410);
408 b43_phy_write(dev
, B43_PHY_CCK(0x17), 0x820);
411 sav
->phy_analogover
= b43_phy_read(dev
, B43_PHY_ANALOGOVER
);
412 sav
->phy_analogoverval
=
413 b43_phy_read(dev
, B43_PHY_ANALOGOVERVAL
);
414 sav
->phy_rfover
= b43_phy_read(dev
, B43_PHY_RFOVER
);
415 sav
->phy_rfoverval
= b43_phy_read(dev
, B43_PHY_RFOVERVAL
);
416 sav
->phy_classctl
= b43_phy_read(dev
, B43_PHY_CLASSCTL
);
417 sav
->phy_cck_3E
= b43_phy_read(dev
, B43_PHY_CCK(0x3E));
418 sav
->phy_crs0
= b43_phy_read(dev
, B43_PHY_CRS0
);
420 b43_phy_mask(dev
, B43_PHY_CLASSCTL
, 0xFFFC);
421 b43_phy_mask(dev
, B43_PHY_CRS0
, 0x7FFF);
422 b43_phy_set(dev
, B43_PHY_ANALOGOVER
, 0x0003);
423 b43_phy_mask(dev
, B43_PHY_ANALOGOVERVAL
, 0xFFFC);
424 if (phy
->type
== B43_PHYTYPE_G
) {
425 if ((phy
->rev
>= 7) &&
426 (sprom
->boardflags_lo
& B43_BFL_EXTLNA
)) {
427 b43_phy_write(dev
, B43_PHY_RFOVER
, 0x933);
429 b43_phy_write(dev
, B43_PHY_RFOVER
, 0x133);
432 b43_phy_write(dev
, B43_PHY_RFOVER
, 0);
434 b43_phy_write(dev
, B43_PHY_CCK(0x3E), 0);
436 sav
->reg_3F4
= b43_read16(dev
, 0x3F4);
437 sav
->reg_3E2
= b43_read16(dev
, 0x3E2);
438 sav
->radio_43
= b43_radio_read16(dev
, 0x43);
439 sav
->radio_7A
= b43_radio_read16(dev
, 0x7A);
440 sav
->phy_pgactl
= b43_phy_read(dev
, B43_PHY_PGACTL
);
441 sav
->phy_cck_2A
= b43_phy_read(dev
, B43_PHY_CCK(0x2A));
442 sav
->phy_syncctl
= b43_phy_read(dev
, B43_PHY_SYNCCTL
);
443 sav
->phy_dacctl
= b43_phy_read(dev
, B43_PHY_DACCTL
);
445 if (!has_tx_magnification(phy
)) {
446 sav
->radio_52
= b43_radio_read16(dev
, 0x52);
447 sav
->radio_52
&= 0x00F0;
449 if (phy
->type
== B43_PHYTYPE_B
) {
450 sav
->phy_cck_30
= b43_phy_read(dev
, B43_PHY_CCK(0x30));
451 sav
->phy_cck_06
= b43_phy_read(dev
, B43_PHY_CCK(0x06));
452 b43_phy_write(dev
, B43_PHY_CCK(0x30), 0x00FF);
453 b43_phy_write(dev
, B43_PHY_CCK(0x06), 0x3F3F);
455 b43_write16(dev
, 0x3E2, b43_read16(dev
, 0x3E2)
458 b43_write16(dev
, 0x3F4, b43_read16(dev
, 0x3F4)
462 (phy
->type
== B43_PHYTYPE_G
) ? B43_PHY_LO_MASK
: B43_PHY_CCK(0x2E);
463 b43_phy_write(dev
, tmp
, 0x007F);
465 tmp
= sav
->phy_syncctl
;
466 b43_phy_write(dev
, B43_PHY_SYNCCTL
, tmp
& 0xFF7F);
468 b43_radio_write16(dev
, 0x007A, tmp
& 0xFFF0);
470 b43_phy_write(dev
, B43_PHY_CCK(0x2A), 0x8A3);
471 if (phy
->type
== B43_PHYTYPE_G
||
472 (phy
->type
== B43_PHYTYPE_B
&&
473 phy
->radio_ver
== 0x2050 && phy
->radio_rev
>= 6)) {
474 b43_phy_write(dev
, B43_PHY_CCK(0x2B), 0x1003);
476 b43_phy_write(dev
, B43_PHY_CCK(0x2B), 0x0802);
478 b43_dummy_transmission(dev
, false, true);
479 b43_gphy_channel_switch(dev
, 6, 0);
480 b43_radio_read16(dev
, 0x51); /* dummy read */
481 if (phy
->type
== B43_PHYTYPE_G
)
482 b43_phy_write(dev
, B43_PHY_CCK(0x2F), 0);
484 /* Re-measure the txctl values, if needed. */
485 if (time_before(lo
->txctl_measured_time
,
486 jiffies
- B43_LO_TXCTL_EXPIRE
))
487 lo_measure_txctl_values(dev
);
489 if (phy
->type
== B43_PHYTYPE_G
&& phy
->rev
>= 3) {
490 b43_phy_write(dev
, B43_PHY_LO_MASK
, 0xC078);
492 if (phy
->type
== B43_PHYTYPE_B
)
493 b43_phy_write(dev
, B43_PHY_CCK(0x2E), 0x8078);
495 b43_phy_write(dev
, B43_PHY_LO_MASK
, 0x8078);
499 static void lo_measure_restore(struct b43_wldev
*dev
,
500 struct lo_g_saved_values
*sav
)
502 struct b43_phy
*phy
= &dev
->phy
;
503 struct b43_phy_g
*gphy
= phy
->g
;
507 b43_phy_write(dev
, B43_PHY_PGACTL
, 0xE300);
508 tmp
= (gphy
->pga_gain
<< 8);
509 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, tmp
| 0xA0);
511 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, tmp
| 0xA2);
513 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, tmp
| 0xA3);
515 tmp
= (gphy
->pga_gain
| 0xEFA0);
516 b43_phy_write(dev
, B43_PHY_PGACTL
, tmp
);
518 if (phy
->type
== B43_PHYTYPE_G
) {
520 b43_phy_write(dev
, B43_PHY_CCK(0x2E), 0xC078);
522 b43_phy_write(dev
, B43_PHY_CCK(0x2E), 0x8078);
524 b43_phy_write(dev
, B43_PHY_CCK(0x2F), 0x0202);
526 b43_phy_write(dev
, B43_PHY_CCK(0x2F), 0x0101);
528 b43_write16(dev
, 0x3F4, sav
->reg_3F4
);
529 b43_phy_write(dev
, B43_PHY_PGACTL
, sav
->phy_pgactl
);
530 b43_phy_write(dev
, B43_PHY_CCK(0x2A), sav
->phy_cck_2A
);
531 b43_phy_write(dev
, B43_PHY_SYNCCTL
, sav
->phy_syncctl
);
532 b43_phy_write(dev
, B43_PHY_DACCTL
, sav
->phy_dacctl
);
533 b43_radio_write16(dev
, 0x43, sav
->radio_43
);
534 b43_radio_write16(dev
, 0x7A, sav
->radio_7A
);
535 if (!has_tx_magnification(phy
)) {
537 b43_radio_maskset(dev
, 0x52, 0xFF0F, tmp
);
539 b43_write16(dev
, 0x3E2, sav
->reg_3E2
);
540 if (phy
->type
== B43_PHYTYPE_B
&&
541 phy
->radio_ver
== 0x2050 && phy
->radio_rev
<= 5) {
542 b43_phy_write(dev
, B43_PHY_CCK(0x30), sav
->phy_cck_30
);
543 b43_phy_write(dev
, B43_PHY_CCK(0x06), sav
->phy_cck_06
);
546 b43_phy_write(dev
, B43_PHY_ANALOGOVER
, sav
->phy_analogover
);
547 b43_phy_write(dev
, B43_PHY_ANALOGOVERVAL
,
548 sav
->phy_analogoverval
);
549 b43_phy_write(dev
, B43_PHY_CLASSCTL
, sav
->phy_classctl
);
550 b43_phy_write(dev
, B43_PHY_RFOVER
, sav
->phy_rfover
);
551 b43_phy_write(dev
, B43_PHY_RFOVERVAL
, sav
->phy_rfoverval
);
552 b43_phy_write(dev
, B43_PHY_CCK(0x3E), sav
->phy_cck_3E
);
553 b43_phy_write(dev
, B43_PHY_CRS0
, sav
->phy_crs0
);
555 if (b43_has_hardware_pctl(dev
)) {
556 tmp
= (sav
->phy_lo_mask
& 0xBFFF);
557 b43_phy_write(dev
, B43_PHY_LO_MASK
, tmp
);
558 b43_phy_write(dev
, B43_PHY_EXTG(0x01), sav
->phy_extg_01
);
559 b43_phy_write(dev
, B43_PHY_DACCTL
, sav
->phy_dacctl_hwpctl
);
560 b43_phy_write(dev
, B43_PHY_CCK(0x14), sav
->phy_cck_14
);
561 b43_phy_write(dev
, B43_PHY_HPWR_TSSICTL
, sav
->phy_hpwr_tssictl
);
563 b43_gphy_channel_switch(dev
, sav
->old_channel
, 1);
566 struct b43_lo_g_statemachine
{
569 int state_val_multiplier
;
571 struct b43_loctl min_loctl
;
574 /* Loop over each possible value in this state. */
575 static int lo_probe_possible_loctls(struct b43_wldev
*dev
,
576 struct b43_loctl
*probe_loctl
,
577 struct b43_lo_g_statemachine
*d
)
579 struct b43_phy
*phy
= &dev
->phy
;
580 struct b43_phy_g
*gphy
= phy
->g
;
581 struct b43_loctl test_loctl
;
582 struct b43_loctl orig_loctl
;
583 struct b43_loctl prev_loctl
= {
592 static const struct b43_loctl modifiers
[] = {
603 if (d
->current_state
== 0) {
606 } else if (d
->current_state
% 2 == 0) {
607 begin
= d
->current_state
- 1;
608 end
= d
->current_state
+ 1;
610 begin
= d
->current_state
- 2;
611 end
= d
->current_state
+ 2;
618 memcpy(&orig_loctl
, probe_loctl
, sizeof(struct b43_loctl
));
620 d
->current_state
= i
;
622 B43_WARN_ON(!(i
>= 1 && i
<= 8));
623 memcpy(&test_loctl
, &orig_loctl
, sizeof(struct b43_loctl
));
624 test_loctl
.i
+= modifiers
[i
- 1].i
* d
->state_val_multiplier
;
625 test_loctl
.q
+= modifiers
[i
- 1].q
* d
->state_val_multiplier
;
626 if ((test_loctl
.i
!= prev_loctl
.i
||
627 test_loctl
.q
!= prev_loctl
.q
) &&
628 (abs(test_loctl
.i
) <= 16 && abs(test_loctl
.q
) <= 16)) {
629 b43_lo_write(dev
, &test_loctl
);
630 feedth
= lo_measure_feedthrough(dev
, gphy
->lna_gain
,
633 if (feedth
< d
->lowest_feedth
) {
634 memcpy(probe_loctl
, &test_loctl
,
635 sizeof(struct b43_loctl
));
637 d
->lowest_feedth
= feedth
;
638 if ((d
->nr_measured
< 2) &&
639 !has_loopback_gain(phy
))
643 memcpy(&prev_loctl
, &test_loctl
, sizeof(prev_loctl
));
650 d
->current_state
= i
;
656 static void lo_probe_loctls_statemachine(struct b43_wldev
*dev
,
657 struct b43_loctl
*loctl
,
660 struct b43_phy
*phy
= &dev
->phy
;
661 struct b43_phy_g
*gphy
= phy
->g
;
662 struct b43_lo_g_statemachine d
;
665 struct b43_loctl probe_loctl
;
666 int max_repeat
= 1, repeat_cnt
= 0;
669 d
.state_val_multiplier
= 1;
670 if (has_loopback_gain(phy
))
671 d
.state_val_multiplier
= 3;
673 memcpy(&d
.min_loctl
, loctl
, sizeof(struct b43_loctl
));
674 if (has_loopback_gain(phy
))
677 b43_lo_write(dev
, &d
.min_loctl
);
678 feedth
= lo_measure_feedthrough(dev
, gphy
->lna_gain
,
681 if (feedth
< 0x258) {
686 feedth
= lo_measure_feedthrough(dev
, gphy
->lna_gain
,
690 d
.lowest_feedth
= feedth
;
695 (d
.current_state
>= 0
696 && d
.current_state
<= 8));
697 memcpy(&probe_loctl
, &d
.min_loctl
,
698 sizeof(struct b43_loctl
));
700 lo_probe_possible_loctls(dev
, &probe_loctl
, &d
);
703 if ((probe_loctl
.i
== d
.min_loctl
.i
) &&
704 (probe_loctl
.q
== d
.min_loctl
.q
))
706 memcpy(&d
.min_loctl
, &probe_loctl
,
707 sizeof(struct b43_loctl
));
709 } while (d
.nr_measured
< 24);
710 memcpy(loctl
, &d
.min_loctl
, sizeof(struct b43_loctl
));
712 if (has_loopback_gain(phy
)) {
713 if (d
.lowest_feedth
> 0x1194)
715 else if (d
.lowest_feedth
< 0x5DC)
717 if (repeat_cnt
== 0) {
718 if (d
.lowest_feedth
<= 0x5DC) {
719 d
.state_val_multiplier
= 1;
722 d
.state_val_multiplier
= 2;
723 } else if (repeat_cnt
== 2)
724 d
.state_val_multiplier
= 1;
726 lo_measure_gain_values(dev
, *max_rx_gain
,
727 has_loopback_gain(phy
));
728 } while (++repeat_cnt
< max_repeat
);
732 struct b43_lo_calib
*b43_calibrate_lo_setting(struct b43_wldev
*dev
,
733 const struct b43_bbatt
*bbatt
,
734 const struct b43_rfatt
*rfatt
)
736 struct b43_phy
*phy
= &dev
->phy
;
737 struct b43_phy_g
*gphy
= phy
->g
;
738 struct b43_loctl loctl
= {
743 struct b43_lo_calib
*cal
;
744 struct lo_g_saved_values
uninitialized_var(saved_regs
);
745 /* Values from the "TXCTL Register and Value Table" */
750 saved_regs
.old_channel
= phy
->channel
;
751 b43_mac_suspend(dev
);
752 lo_measure_setup(dev
, &saved_regs
);
754 txctl_reg
= lo_txctl_register_table(dev
, &txctl_value
, &pad_mix_gain
);
756 b43_radio_maskset(dev
, 0x43, 0xFFF0, rfatt
->att
);
757 b43_radio_maskset(dev
, txctl_reg
, ~txctl_value
, (rfatt
->with_padmix
? txctl_value
:0));
759 max_rx_gain
= rfatt
->att
* 2;
760 max_rx_gain
+= bbatt
->att
/ 2;
761 if (rfatt
->with_padmix
)
762 max_rx_gain
-= pad_mix_gain
;
763 if (has_loopback_gain(phy
))
764 max_rx_gain
+= gphy
->max_lb_gain
;
765 lo_measure_gain_values(dev
, max_rx_gain
,
766 has_loopback_gain(phy
));
768 b43_gphy_set_baseband_attenuation(dev
, bbatt
->att
);
769 lo_probe_loctls_statemachine(dev
, &loctl
, &max_rx_gain
);
771 lo_measure_restore(dev
, &saved_regs
);
774 if (b43_debug(dev
, B43_DBG_LO
)) {
775 b43dbg(dev
->wl
, "LO: Calibrated for BB(%u), RF(%u,%u) "
777 bbatt
->att
, rfatt
->att
, rfatt
->with_padmix
,
781 cal
= kmalloc(sizeof(*cal
), GFP_KERNEL
);
783 b43warn(dev
->wl
, "LO calib: out of memory\n");
786 memcpy(&cal
->bbatt
, bbatt
, sizeof(*bbatt
));
787 memcpy(&cal
->rfatt
, rfatt
, sizeof(*rfatt
));
788 memcpy(&cal
->ctl
, &loctl
, sizeof(loctl
));
789 cal
->calib_time
= jiffies
;
790 INIT_LIST_HEAD(&cal
->list
);
795 /* Get a calibrated LO setting for the given attenuation values.
796 * Might return a NULL pointer under OOM! */
798 struct b43_lo_calib
*b43_get_calib_lo_settings(struct b43_wldev
*dev
,
799 const struct b43_bbatt
*bbatt
,
800 const struct b43_rfatt
*rfatt
)
802 struct b43_txpower_lo_control
*lo
= dev
->phy
.g
->lo_control
;
803 struct b43_lo_calib
*c
;
805 c
= b43_find_lo_calib(lo
, bbatt
, rfatt
);
808 /* Not in the list of calibrated LO settings.
809 * Calibrate it now. */
810 c
= b43_calibrate_lo_setting(dev
, bbatt
, rfatt
);
813 list_add(&c
->list
, &lo
->calib_list
);
818 void b43_gphy_dc_lt_init(struct b43_wldev
*dev
, bool update_all
)
820 struct b43_phy
*phy
= &dev
->phy
;
821 struct b43_phy_g
*gphy
= phy
->g
;
822 struct b43_txpower_lo_control
*lo
= gphy
->lo_control
;
824 int rf_offset
, bb_offset
;
825 const struct b43_rfatt
*rfatt
;
826 const struct b43_bbatt
*bbatt
;
828 bool table_changed
= 0;
830 BUILD_BUG_ON(B43_DC_LT_SIZE
!= 32);
831 B43_WARN_ON(lo
->rfatt_list
.len
* lo
->bbatt_list
.len
> 64);
833 power_vector
= lo
->power_vector
;
834 if (!update_all
&& !power_vector
)
835 return; /* Nothing to do. */
837 /* Suspend the MAC now to avoid continuous suspend/enable
838 * cycles in the loop. */
839 b43_mac_suspend(dev
);
841 for (i
= 0; i
< B43_DC_LT_SIZE
* 2; i
++) {
842 struct b43_lo_calib
*cal
;
846 if (!update_all
&& !(power_vector
& (((u64
)1ULL) << i
)))
848 /* Update the table entry for this power_vector bit.
849 * The table rows are RFatt entries and columns are BBatt. */
850 bb_offset
= i
/ lo
->rfatt_list
.len
;
851 rf_offset
= i
% lo
->rfatt_list
.len
;
852 bbatt
= &(lo
->bbatt_list
.list
[bb_offset
]);
853 rfatt
= &(lo
->rfatt_list
.list
[rf_offset
]);
855 cal
= b43_calibrate_lo_setting(dev
, bbatt
, rfatt
);
857 b43warn(dev
->wl
, "LO: Could not "
858 "calibrate DC table entry\n");
861 val
= (u8
)(cal
->ctl
.q
);
862 val
|= ((u8
)(cal
->ctl
.i
)) << 4;
865 /* Get the index into the hardware DC LT. */
867 /* Change the table in memory. */
869 /* Change the high byte. */
870 lo
->dc_lt
[idx
] = (lo
->dc_lt
[idx
] & 0x00FF)
871 | ((val
& 0x00FF) << 8);
873 /* Change the low byte. */
874 lo
->dc_lt
[idx
] = (lo
->dc_lt
[idx
] & 0xFF00)
880 /* The table changed in memory. Update the hardware table. */
881 for (i
= 0; i
< B43_DC_LT_SIZE
; i
++)
882 b43_phy_write(dev
, 0x3A0 + i
, lo
->dc_lt
[i
]);
887 /* Fixup the RF attenuation value for the case where we are
888 * using the PAD mixer. */
889 static inline void b43_lo_fixup_rfatt(struct b43_rfatt
*rf
)
891 if (!rf
->with_padmix
)
893 if ((rf
->att
!= 1) && (rf
->att
!= 2) && (rf
->att
!= 3))
897 void b43_lo_g_adjust(struct b43_wldev
*dev
)
899 struct b43_phy_g
*gphy
= dev
->phy
.g
;
900 struct b43_lo_calib
*cal
;
903 memcpy(&rf
, &gphy
->rfatt
, sizeof(rf
));
904 b43_lo_fixup_rfatt(&rf
);
906 cal
= b43_get_calib_lo_settings(dev
, &gphy
->bbatt
, &rf
);
909 b43_lo_write(dev
, &cal
->ctl
);
912 void b43_lo_g_adjust_to(struct b43_wldev
*dev
,
913 u16 rfatt
, u16 bbatt
, u16 tx_control
)
917 struct b43_lo_calib
*cal
;
919 memset(&rf
, 0, sizeof(rf
));
920 memset(&bb
, 0, sizeof(bb
));
923 b43_lo_fixup_rfatt(&rf
);
924 cal
= b43_get_calib_lo_settings(dev
, &bb
, &rf
);
927 b43_lo_write(dev
, &cal
->ctl
);
930 /* Periodic LO maintanance work */
931 void b43_lo_g_maintanance_work(struct b43_wldev
*dev
)
933 struct b43_phy
*phy
= &dev
->phy
;
934 struct b43_phy_g
*gphy
= phy
->g
;
935 struct b43_txpower_lo_control
*lo
= gphy
->lo_control
;
937 unsigned long expire
;
938 struct b43_lo_calib
*cal
, *tmp
;
939 bool current_item_expired
= 0;
945 hwpctl
= b43_has_hardware_pctl(dev
);
948 /* Read the power vector and update it, if needed. */
949 expire
= now
- B43_LO_PWRVEC_EXPIRE
;
950 if (time_before(lo
->pwr_vec_read_time
, expire
)) {
951 lo_read_power_vector(dev
);
952 b43_gphy_dc_lt_init(dev
, 0);
958 /* Search for expired LO settings. Remove them.
959 * Recalibrate the current setting, if expired. */
960 expire
= now
- B43_LO_CALIB_EXPIRE
;
961 list_for_each_entry_safe(cal
, tmp
, &lo
->calib_list
, list
) {
962 if (!time_before(cal
->calib_time
, expire
))
964 /* This item expired. */
965 if (b43_compare_bbatt(&cal
->bbatt
, &gphy
->bbatt
) &&
966 b43_compare_rfatt(&cal
->rfatt
, &gphy
->rfatt
)) {
967 B43_WARN_ON(current_item_expired
);
968 current_item_expired
= 1;
970 if (b43_debug(dev
, B43_DBG_LO
)) {
971 b43dbg(dev
->wl
, "LO: Item BB(%u), RF(%u,%u), "
972 "I=%d, Q=%d expired\n",
973 cal
->bbatt
.att
, cal
->rfatt
.att
,
974 cal
->rfatt
.with_padmix
,
975 cal
->ctl
.i
, cal
->ctl
.q
);
977 list_del(&cal
->list
);
980 if (current_item_expired
|| unlikely(list_empty(&lo
->calib_list
))) {
981 /* Recalibrate currently used LO setting. */
982 if (b43_debug(dev
, B43_DBG_LO
))
983 b43dbg(dev
->wl
, "LO: Recalibrating current LO setting\n");
984 cal
= b43_calibrate_lo_setting(dev
, &gphy
->bbatt
, &gphy
->rfatt
);
986 list_add(&cal
->list
, &lo
->calib_list
);
987 b43_lo_write(dev
, &cal
->ctl
);
989 b43warn(dev
->wl
, "Failed to recalibrate current LO setting\n");
993 void b43_lo_g_cleanup(struct b43_wldev
*dev
)
995 struct b43_txpower_lo_control
*lo
= dev
->phy
.g
->lo_control
;
996 struct b43_lo_calib
*cal
, *tmp
;
1000 list_for_each_entry_safe(cal
, tmp
, &lo
->calib_list
, list
) {
1001 list_del(&cal
->list
);
1006 /* LO Initialization */
1007 void b43_lo_g_init(struct b43_wldev
*dev
)
1009 if (b43_has_hardware_pctl(dev
)) {
1010 lo_read_power_vector(dev
);
1011 b43_gphy_dc_lt_init(dev
, 1);