Merge branches 'release', 'cpuidle-2.6.25' and 'idle' into release
[linux-2.6] / drivers / net / wireless / b43 / lo.c
1 /*
2
3   Broadcom B43 wireless driver
4
5   G PHY LO (LocalOscillator) Measuring and Control routines
6
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>
12
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.
17
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.
22
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.
27
28 */
29
30 #include "b43.h"
31 #include "lo.h"
32 #include "phy.h"
33 #include "main.h"
34
35 #include <linux/delay.h>
36 #include <linux/sched.h>
37
38
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
42
43
44 /* Write the LocalOscillator Control (adjust) value-pair. */
45 static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
46 {
47         struct b43_phy *phy = &dev->phy;
48         u16 value;
49         u16 reg;
50
51         if (B43_DEBUG) {
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);
55                         dump_stack();
56                         return;
57                 }
58         }
59
60         value = (u8) (control->q);
61         value |= ((u8) (control->i)) << 8;
62
63         reg = (phy->type == B43_PHYTYPE_B) ? 0x002F : B43_PHY_LO_CTL;
64         b43_phy_write(dev, reg, value);
65 }
66
67 static int assert_rfatt_and_bbatt(const struct b43_rfatt *rfatt,
68                                   const struct b43_bbatt *bbatt,
69                                   struct b43_wldev *dev)
70 {
71         int err = 0;
72
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);
76                 err = -EINVAL;
77         }
78         if (unlikely(bbatt->att >= B43_NR_BB)) {
79                 b43err(dev->wl, "bbatt(%u) >= size of LO array\n", bbatt->att);
80                 err = -EINVAL;
81         }
82
83         return err;
84 }
85
86 #if !B43_CALIB_ALL_LOCTLS
87 static
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)
91 {
92         struct b43_phy *phy = &dev->phy;
93         struct b43_txpower_lo_control *lo = phy->lo_control;
94
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]);
98 }
99 #endif /* !B43_CALIB_ALL_LOCTLS */
100
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)
104 {
105         struct b43_phy *phy = &dev->phy;
106         struct b43_txpower_lo_control *lo = phy->lo_control;
107
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]);
113 }
114
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 *,
118                                                   struct b43_loctl *))
119 {
120         struct b43_phy *phy = &dev->phy;
121         struct b43_txpower_lo_control *ctl = phy->lo_control;
122         int i, j;
123
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]));
127         }
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]));
131         }
132 }
133
134 static u16 lo_b_r15_loop(struct b43_wldev *dev)
135 {
136         int i;
137         u16 ret = 0;
138
139         for (i = 0; i < 10; i++) {
140                 b43_phy_write(dev, 0x0015, 0xAFA0);
141                 udelay(1);
142                 b43_phy_write(dev, 0x0015, 0xEFA0);
143                 udelay(10);
144                 b43_phy_write(dev, 0x0015, 0xFFA0);
145                 udelay(40);
146                 ret += b43_phy_read(dev, 0x002C);
147         }
148
149         return ret;
150 }
151
152 void b43_lo_b_measure(struct b43_wldev *dev)
153 {
154         struct b43_phy *phy = &dev->phy;
155         u16 regstack[12] = { 0 };
156         u16 mls;
157         u16 fval;
158         int i, j;
159
160         regstack[0] = b43_phy_read(dev, 0x0015);
161         regstack[1] = b43_radio_read16(dev, 0x0052) & 0xFFF0;
162
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);
170
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;
175
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);
180         }
181         b43_phy_write(dev, 0x0015, 0xB000);
182         b43_phy_write(dev, 0x002B, 0x0004);
183
184         if (phy->radio_ver == 0x2053) {
185                 b43_phy_write(dev, 0x002B, 0x0203);
186                 b43_phy_write(dev, 0x002A, 0x08A3);
187         }
188
189         phy->minlowsig[0] = 0xFFFF;
190
191         for (i = 0; i < 4; i++) {
192                 b43_radio_write16(dev, 0x0052, regstack[1] | i);
193                 lo_b_r15_loop(dev);
194         }
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;
201                 }
202         }
203         b43_radio_write16(dev, 0x0052, regstack[1] | phy->minlowsigpos[0]);
204
205         phy->minlowsig[1] = 0xFFFF;
206
207         for (i = -4; i < 5; i += 2) {
208                 for (j = -4; j < 5; j += 2) {
209                         if (j < 0)
210                                 fval = (0x0100 * i) + j + 0x0100;
211                         else
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;
218                         }
219                 }
220         }
221         phy->minlowsigpos[1] += 0x0101;
222
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]);
231
232                 b43_radio_write16(dev, 0x0043, regstack[8]);
233                 b43_radio_write16(dev, 0x007A, regstack[9]);
234
235                 b43_radio_write16(dev, 0x0052,
236                                   (b43_radio_read16(dev, 0x0052) & 0x000F)
237                                   | regstack[11]);
238
239                 b43_write16(dev, 0x03EC, regstack[10]);
240         }
241         b43_phy_write(dev, 0x0015, regstack[0]);
242 }
243
244 static u16 lo_measure_feedthrough(struct b43_wldev *dev,
245                                   u16 lna, u16 pga, u16 trsw_rx)
246 {
247         struct b43_phy *phy = &dev->phy;
248         u16 rfover;
249         u16 feedthrough;
250
251         if (phy->gmode) {
252                 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
253                 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
254
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));
259 */
260                 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
261
262                 /* Construct the RF Override Value */
263                 rfover = B43_PHY_RFOVERVAL_UNK;
264                 rfover |= pga;
265                 rfover |= lna;
266                 rfover |= trsw_rx;
267                 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
268                     && phy->rev > 6)
269                         rfover |= B43_PHY_RFOVERVAL_EXTLNA;
270
271                 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
272                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
273                 udelay(10);
274                 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
275                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
276                 udelay(10);
277                 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
278                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
279                 udelay(10);
280                 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
281         } else {
282                 pga |= B43_PHY_PGACTL_UNKNOWN;
283                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
284                 udelay(10);
285                 pga |= B43_PHY_PGACTL_LOWBANDW;
286                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
287                 udelay(10);
288                 pga |= B43_PHY_PGACTL_LPF;
289                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
290         }
291         udelay(21);
292         feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
293
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. */
297         cond_resched();
298
299         return feedthrough;
300 }
301
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.
306  */
307 static u16 lo_txctl_register_table(struct b43_wldev *dev,
308                                    u16 * value, u16 * pad_mix_gain)
309 {
310         struct b43_phy *phy = &dev->phy;
311         u16 reg, v, padmix;
312
313         if (phy->type == B43_PHYTYPE_B) {
314                 v = 0x30;
315                 if (phy->radio_rev <= 5) {
316                         reg = 0x43;
317                         padmix = 0;
318                 } else {
319                         reg = 0x52;
320                         padmix = 5;
321                 }
322         } else {
323                 if (phy->rev >= 2 && phy->radio_rev == 8) {
324                         reg = 0x43;
325                         v = 0x10;
326                         padmix = 2;
327                 } else {
328                         reg = 0x52;
329                         v = 0x30;
330                         padmix = 5;
331                 }
332         }
333         if (value)
334                 *value = v;
335         if (pad_mix_gain)
336                 *pad_mix_gain = padmix;
337
338         return reg;
339 }
340
341 static void lo_measure_txctl_values(struct b43_wldev *dev)
342 {
343         struct b43_phy *phy = &dev->phy;
344         struct b43_txpower_lo_control *lo = phy->lo_control;
345         u16 reg, mask;
346         u16 trsw_rx, pga;
347         u16 radio_pctl_reg;
348
349         static const u8 tx_bias_values[] = {
350                 0x09, 0x08, 0x0A, 0x01, 0x00,
351                 0x02, 0x05, 0x04, 0x06,
352         };
353         static const u8 tx_magn_values[] = {
354                 0x70, 0x40,
355         };
356
357         if (!has_loopback_gain(phy)) {
358                 radio_pctl_reg = 6;
359                 trsw_rx = 2;
360                 pga = 0;
361         } else {
362                 int lb_gain;    /* Loopback gain (in dB) */
363
364                 trsw_rx = 0;
365                 lb_gain = phy->max_lb_gain / 2;
366                 if (lb_gain > 10) {
367                         radio_pctl_reg = 0;
368                         pga = abs(10 - lb_gain) / 6;
369                         pga = limit_value(pga, 0, 15);
370                 } else {
371                         int cmp_val;
372                         int tmp;
373
374                         pga = 0;
375                         cmp_val = 0x24;
376                         if ((phy->rev >= 2) &&
377                             (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
378                                 cmp_val = 0x3C;
379                         tmp = lb_gain;
380                         if ((10 - lb_gain) < cmp_val)
381                                 tmp = (10 - lb_gain);
382                         if (tmp < 0)
383                                 tmp += 6;
384                         else
385                                 tmp += 3;
386                         cmp_val /= 4;
387                         tmp /= 4;
388                         if (tmp >= cmp_val)
389                                 radio_pctl_reg = cmp_val;
390                         else
391                                 radio_pctl_reg = tmp;
392                 }
393         }
394         b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
395                                       & 0xFFF0) | radio_pctl_reg);
396         b43_phy_set_baseband_attenuation(dev, 2);
397
398         reg = lo_txctl_register_table(dev, &mask, NULL);
399         mask = ~mask;
400         b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
401                           & mask);
402
403         if (has_tx_magnification(phy)) {
404                 int i, j;
405                 int feedthrough;
406                 int min_feedth = 0xFFFF;
407                 u8 tx_magn, tx_bias;
408
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);
419                                 feedthrough =
420                                     lo_measure_feedthrough(dev, 0, pga,
421                                                            trsw_rx);
422                                 if (feedthrough < min_feedth) {
423                                         lo->tx_bias = tx_bias;
424                                         lo->tx_magn = tx_magn;
425                                         min_feedth = feedthrough;
426                                 }
427                                 if (lo->tx_bias == 0)
428                                         break;
429                         }
430                         b43_radio_write16(dev, 0x52,
431                                           (b43_radio_read16(dev, 0x52)
432                                            & 0xFF00) | lo->tx_bias | lo->
433                                           tx_magn);
434                 }
435         } else {
436                 lo->tx_magn = 0;
437                 lo->tx_bias = 0;
438                 b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
439                                   & 0xFFF0);    /* TX bias == 0 */
440         }
441 }
442
443 static void lo_read_power_vector(struct b43_wldev *dev)
444 {
445         struct b43_phy *phy = &dev->phy;
446         struct b43_txpower_lo_control *lo = phy->lo_control;
447         u16 i;
448         u64 tmp;
449         u64 power_vector = 0;
450         int rf_offset, bb_offset;
451         struct b43_loctl *loctl;
452
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... */
456                 tmp &= 0xFF;
457                 power_vector |= (tmp << (i * 8));
458                 /* Clear the vector on the device. */
459                 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
460         }
461
462         if (power_vector)
463                 lo->power_vector = power_vector;
464         power_vector = lo->power_vector;
465
466         for (i = 0; i < 64; i++) {
467                 if (power_vector & ((u64) 1ULL << i)) {
468                         /* Now figure out which b43_loctl corresponds
469                          * to this bit.
470                          */
471                         rf_offset = i / lo->rfatt_list.len;
472                         bb_offset = i % lo->rfatt_list.len;     //FIXME?
473                         loctl =
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.
479                          */
480                         loctl->used = 1;
481                 }
482         }
483 }
484
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)
488 {
489         struct b43_phy *phy = &dev->phy;
490         u16 tmp;
491
492         if (max_rx_gain < 0)
493                 max_rx_gain = 0;
494
495         if (has_loopback_gain(phy)) {
496                 int trsw_rx = 0;
497                 int trsw_rx_gain;
498
499                 if (use_trsw_rx) {
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;
503                                 trsw_rx = 0x20;
504                         }
505                 } else
506                         trsw_rx_gain = max_rx_gain;
507                 if (trsw_rx_gain < 9) {
508                         phy->lna_lod_gain = 0;
509                 } else {
510                         phy->lna_lod_gain = 1;
511                         trsw_rx_gain -= 8;
512                 }
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) {
516                         phy->pga_gain -= 5;
517                         phy->lna_gain = 2;
518                 } else
519                         phy->lna_gain = 0;
520         } else {
521                 phy->lna_gain = 0;
522                 phy->trsw_rx_gain = 0x20;
523                 if (max_rx_gain >= 0x14) {
524                         phy->lna_lod_gain = 1;
525                         phy->pga_gain = 2;
526                 } else if (max_rx_gain >= 0x12) {
527                         phy->lna_lod_gain = 1;
528                         phy->pga_gain = 1;
529                 } else if (max_rx_gain >= 0xF) {
530                         phy->lna_lod_gain = 1;
531                         phy->pga_gain = 0;
532                 } else {
533                         phy->lna_lod_gain = 0;
534                         phy->pga_gain = 0;
535                 }
536         }
537
538         tmp = b43_radio_read16(dev, 0x7A);
539         if (phy->lna_lod_gain == 0)
540                 tmp &= ~0x0008;
541         else
542                 tmp |= 0x0008;
543         b43_radio_write16(dev, 0x7A, tmp);
544 }
545
546 struct lo_g_saved_values {
547         u8 old_channel;
548
549         /* Core registers */
550         u16 reg_3F4;
551         u16 reg_3E2;
552
553         /* PHY registers */
554         u16 phy_lo_mask;
555         u16 phy_extg_01;
556         u16 phy_dacctl_hwpctl;
557         u16 phy_dacctl;
558         u16 phy_cck_14;
559         u16 phy_hpwr_tssictl;
560         u16 phy_analogover;
561         u16 phy_analogoverval;
562         u16 phy_rfover;
563         u16 phy_rfoverval;
564         u16 phy_classctl;
565         u16 phy_cck_3E;
566         u16 phy_crs0;
567         u16 phy_pgactl;
568         u16 phy_cck_2A;
569         u16 phy_syncctl;
570         u16 phy_cck_30;
571         u16 phy_cck_06;
572
573         /* Radio registers */
574         u16 radio_43;
575         u16 radio_7A;
576         u16 radio_52;
577 };
578
579 static void lo_measure_setup(struct b43_wldev *dev,
580                              struct lo_g_saved_values *sav)
581 {
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;
585         u16 tmp;
586
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);
593
594                 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
595                               b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
596                               | 0x100);
597                 b43_phy_write(dev, B43_PHY_EXTG(0x01),
598                               b43_phy_read(dev, B43_PHY_EXTG(0x01))
599                               | 0x40);
600                 b43_phy_write(dev, B43_PHY_DACCTL,
601                               b43_phy_read(dev, B43_PHY_DACCTL)
602                               | 0x40);
603                 b43_phy_write(dev, B43_PHY_CCK(0x14),
604                               b43_phy_read(dev, B43_PHY_CCK(0x14))
605                               | 0x200);
606         }
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);
611         }
612         if (!lo->rebuild && b43_has_hardware_pctl(phy))
613                 lo_read_power_vector(dev);
614         if (phy->rev >= 2) {
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);
623
624                 b43_phy_write(dev, B43_PHY_CLASSCTL,
625                               b43_phy_read(dev, B43_PHY_CLASSCTL)
626                               & 0xFFFC);
627                 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
628                               & 0x7FFF);
629                 b43_phy_write(dev, B43_PHY_ANALOGOVER,
630                               b43_phy_read(dev, B43_PHY_ANALOGOVER)
631                               | 0x0003);
632                 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
633                               b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
634                               & 0xFFFC);
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);
639                         } else {
640                                 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
641                         }
642                 } else {
643                         b43_phy_write(dev, B43_PHY_RFOVER, 0);
644                 }
645                 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
646         }
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);
655
656         if (!has_tx_magnification(phy)) {
657                 sav->radio_52 = b43_radio_read16(dev, 0x52);
658                 sav->radio_52 &= 0x00F0;
659         }
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);
665         } else {
666                 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
667                             | 0x8000);
668         }
669         b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
670                     & 0xF000);
671
672         tmp =
673             (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
674         b43_phy_write(dev, tmp, 0x007F);
675
676         tmp = sav->phy_syncctl;
677         b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
678         tmp = sav->radio_7A;
679         b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
680
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);
686         } else
687                 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
688         if (phy->rev >= 2)
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);
694         if (lo->rebuild)
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);
698         } else {
699                 if (phy->type == B43_PHYTYPE_B)
700                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
701                 else
702                         b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
703         }
704 }
705
706 static void lo_measure_restore(struct b43_wldev *dev,
707                                struct lo_g_saved_values *sav)
708 {
709         struct b43_phy *phy = &dev->phy;
710         struct b43_txpower_lo_control *lo = phy->lo_control;
711         u16 tmp;
712
713         if (phy->rev >= 2) {
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);
717                 udelay(5);
718                 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
719                 udelay(2);
720                 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
721         } else {
722                 tmp = (phy->pga_gain | 0xEFA0);
723                 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
724         }
725         if (b43_has_hardware_pctl(phy)) {
726                 b43_gphy_dc_lt_init(dev);
727         } else {
728                 if (lo->rebuild)
729                         b43_lo_g_adjust_to(dev, 3, 2, 0);
730                 else
731                         b43_lo_g_adjust(dev);
732         }
733         if (phy->type == B43_PHYTYPE_G) {
734                 if (phy->rev >= 3)
735                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
736                 else
737                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
738                 if (phy->rev >= 2)
739                         b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
740                 else
741                         b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
742         }
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)) {
751                 tmp = sav->radio_52;
752                 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
753                                               & 0xFF0F) | tmp);
754         }
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);
760         }
761         if (phy->rev >= 2) {
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);
770         }
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);
778         }
779         b43_radio_selectchannel(dev, sav->old_channel, 1);
780 }
781
782 struct b43_lo_g_statemachine {
783         int current_state;
784         int nr_measured;
785         int state_val_multiplier;
786         u16 lowest_feedth;
787         struct b43_loctl min_loctl;
788 };
789
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)
794 {
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 = {
800                 .i = -100,
801                 .q = -100,
802         };
803         int i;
804         int begin, end;
805         int found_lower = 0;
806         u16 feedth;
807
808         static const struct b43_loctl modifiers[] = {
809                 {.i = 1,.q = 1,},
810                 {.i = 1,.q = 0,},
811                 {.i = 1,.q = -1,},
812                 {.i = 0,.q = -1,},
813                 {.i = -1,.q = -1,},
814                 {.i = -1,.q = 0,},
815                 {.i = -1,.q = 1,},
816                 {.i = 0,.q = 1,},
817         };
818
819         if (d->current_state == 0) {
820                 begin = 1;
821                 end = 8;
822         } else if (d->current_state % 2 == 0) {
823                 begin = d->current_state - 1;
824                 end = d->current_state + 1;
825         } else {
826                 begin = d->current_state - 2;
827                 end = d->current_state + 2;
828         }
829         if (begin < 1)
830                 begin += 8;
831         if (end > 8)
832                 end -= 8;
833
834         memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
835         i = begin;
836         d->current_state = i;
837         while (1) {
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,
847                                                         phy->pga_gain,
848                                                         phy->trsw_rx_gain);
849                         if (feedth < d->lowest_feedth) {
850                                 memcpy(probe_loctl, &test_loctl,
851                                        sizeof(struct b43_loctl));
852                                 found_lower = 1;
853                                 d->lowest_feedth = feedth;
854                                 if ((d->nr_measured < 2) &&
855                                     (!has_loopback_gain(phy) || lo->rebuild))
856                                         break;
857                         }
858                 }
859                 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
860                 if (i == end)
861                         break;
862                 if (i == 8)
863                         i = 1;
864                 else
865                         i++;
866                 d->current_state = i;
867         }
868
869         return found_lower;
870 }
871
872 static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
873                                          struct b43_loctl *loctl,
874                                          int *max_rx_gain)
875 {
876         struct b43_phy *phy = &dev->phy;
877         struct b43_txpower_lo_control *lo = phy->lo_control;
878         struct b43_lo_g_statemachine d;
879         u16 feedth;
880         int found_lower;
881         struct b43_loctl probe_loctl;
882         int max_repeat = 1, repeat_cnt = 0;
883
884         d.nr_measured = 0;
885         d.state_val_multiplier = 1;
886         if (has_loopback_gain(phy) && !lo->rebuild)
887                 d.state_val_multiplier = 3;
888
889         memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
890         if (has_loopback_gain(phy) && lo->rebuild)
891                 max_repeat = 4;
892         do {
893                 b43_lo_write(dev, &d.min_loctl);
894                 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
895                                                 phy->pga_gain,
896                                                 phy->trsw_rx_gain);
897                 if (!lo->rebuild && feedth < 0x258) {
898                         if (feedth >= 0x12C)
899                                 *max_rx_gain += 6;
900                         else
901                                 *max_rx_gain += 3;
902                         feedth = lo_measure_feedthrough(dev, phy->lna_gain,
903                                                         phy->pga_gain,
904                                                         phy->trsw_rx_gain);
905                 }
906                 d.lowest_feedth = feedth;
907
908                 d.current_state = 0;
909                 do {
910                         B43_WARN_ON(!
911                                     (d.current_state >= 0
912                                      && d.current_state <= 8));
913                         memcpy(&probe_loctl, &d.min_loctl,
914                                sizeof(struct b43_loctl));
915                         found_lower =
916                             lo_probe_possible_loctls(dev, &probe_loctl, &d);
917                         if (!found_lower)
918                                 break;
919                         if ((probe_loctl.i == d.min_loctl.i) &&
920                             (probe_loctl.q == d.min_loctl.q))
921                                 break;
922                         memcpy(&d.min_loctl, &probe_loctl,
923                                sizeof(struct b43_loctl));
924                         d.nr_measured++;
925                 } while (d.nr_measured < 24);
926                 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
927
928                 if (has_loopback_gain(phy)) {
929                         if (d.lowest_feedth > 0x1194)
930                                 *max_rx_gain -= 6;
931                         else if (d.lowest_feedth < 0x5DC)
932                                 *max_rx_gain += 3;
933                         if (repeat_cnt == 0) {
934                                 if (d.lowest_feedth <= 0x5DC) {
935                                         d.state_val_multiplier = 1;
936                                         repeat_cnt++;
937                                 } else
938                                         d.state_val_multiplier = 2;
939                         } else if (repeat_cnt == 2)
940                                 d.state_val_multiplier = 1;
941                 }
942                 lo_measure_gain_values(dev, *max_rx_gain,
943                                        has_loopback_gain(phy));
944         } while (++repeat_cnt < max_repeat);
945 }
946
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, },
981 };
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),
985 };
986
987 static const struct b43_bbatt b43_full_bbatt_list_items[] = {
988         { .att = 0, },
989         { .att = 1, },
990         { .att = 2, },
991         { .att = 3, },
992         { .att = 4, },
993         { .att = 5, },
994         { .att = 6, },
995         { .att = 7, },
996         { .att = 8, },
997         { .att = 9, },
998         { .att = 10, },
999         { .att = 11, },
1000 };
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),
1004 };
1005 #endif /* B43_CALIB_ALL_LOCTLS */
1006
1007 static void lo_measure(struct b43_wldev *dev)
1008 {
1009         struct b43_phy *phy = &dev->phy;
1010         struct b43_txpower_lo_control *lo = phy->lo_control;
1011         struct b43_loctl loctl = {
1012                 .i = 0,
1013                 .q = 0,
1014         };
1015         struct b43_loctl *ploctl;
1016         int max_rx_gain;
1017         int rfidx, bbidx;
1018         const struct b43_bbatt_list *bbatt_list;
1019         const struct b43_rfatt_list *rfatt_list;
1020
1021         /* Values from the "TXCTL Register and Value Table" */
1022         u16 txctl_reg;
1023         u16 txctl_value;
1024         u16 pad_mix_gain;
1025
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;
1031 #endif
1032
1033         txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
1034
1035         for (rfidx = 0; rfidx < rfatt_list->len; rfidx++) {
1036
1037                 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
1038                                               & 0xFFF0) |
1039                                   rfatt_list->list[rfidx].att);
1040                 b43_radio_write16(dev, txctl_reg,
1041                                   (b43_radio_read16(dev, txctl_reg)
1042                                    & ~txctl_value)
1043                                   | (rfatt_list->list[rfidx].with_padmix ?
1044                                      txctl_value : 0));
1045
1046                 for (bbidx = 0; bbidx < bbatt_list->len; bbidx++) {
1047                         if (lo->rebuild) {
1048 #if B43_CALIB_ALL_LOCTLS
1049                                 ploctl = b43_get_lo_g_ctl(dev,
1050                                                           &rfatt_list->list[rfidx],
1051                                                           &bbatt_list->list[bbidx]);
1052 #else
1053                                 ploctl = b43_get_lo_g_ctl_nopadmix(dev,
1054                                                                    &rfatt_list->
1055                                                                    list[rfidx],
1056                                                                    &bbatt_list->
1057                                                                    list[bbidx]);
1058 #endif
1059                         } else {
1060                                 ploctl = b43_get_lo_g_ctl(dev,
1061                                                           &rfatt_list->list[rfidx],
1062                                                           &bbatt_list->list[bbidx]);
1063                                 if (!ploctl->used)
1064                                         continue;
1065                         }
1066                         memcpy(&loctl, ploctl, sizeof(loctl));
1067                         loctl.i = 0;
1068                         loctl.q = 0;
1069
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));
1078
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) {
1083                                 loctl.i++;
1084                                 loctl.q++;
1085                         }
1086                         b43_loctl_set_calibrated(&loctl, 1);
1087                         memcpy(ploctl, &loctl, sizeof(loctl));
1088                 }
1089         }
1090 }
1091
1092 #if B43_DEBUG
1093 static void do_validate_loctl(struct b43_wldev *dev, struct b43_loctl *control)
1094 {
1095         const int is_initializing = (b43_status(dev) == B43_STAT_UNINIT);
1096         int i = control->i;
1097         int q = control->q;
1098
1099         if (b43_loctl_is_calibrated(control)) {
1100                 if ((abs(i) > 16) || (abs(q) > 16))
1101                         goto error;
1102         } else {
1103                 if (control->used)
1104                         goto error;
1105                 if (dev->phy.lo_control->rebuild) {
1106                         control->i = 0;
1107                         control->q = 0;
1108                         if ((i != B43_LOCTL_POISON) ||
1109                             (q != B43_LOCTL_POISON))
1110                                 goto error;
1111                 }
1112         }
1113         if (is_initializing && control->used)
1114                 goto error;
1115
1116         return;
1117 error:
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),
1122                is_initializing);
1123 }
1124
1125 static void validate_all_loctls(struct b43_wldev *dev)
1126 {
1127         b43_call_for_each_loctl(dev, do_validate_loctl);
1128 }
1129
1130 static void do_reset_calib(struct b43_wldev *dev, struct b43_loctl *control)
1131 {
1132         if (dev->phy.lo_control->rebuild ||
1133             control->used) {
1134                 b43_loctl_set_calibrated(control, 0);
1135                 control->i = B43_LOCTL_POISON;
1136                 control->q = B43_LOCTL_POISON;
1137         }
1138 }
1139
1140 static void reset_all_loctl_calibration_states(struct b43_wldev *dev)
1141 {
1142         b43_call_for_each_loctl(dev, do_reset_calib);
1143 }
1144
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 */
1149
1150 void b43_lo_g_measure(struct b43_wldev *dev)
1151 {
1152         struct b43_phy *phy = &dev->phy;
1153         struct lo_g_saved_values uninitialized_var(sav);
1154
1155         B43_WARN_ON((phy->type != B43_PHYTYPE_B) &&
1156                     (phy->type != B43_PHYTYPE_G));
1157
1158         sav.old_channel = phy->channel;
1159         lo_measure_setup(dev, &sav);
1160         reset_all_loctl_calibration_states(dev);
1161         lo_measure(dev);
1162         lo_measure_restore(dev, &sav);
1163
1164         validate_all_loctls(dev);
1165
1166         phy->lo_control->lo_measured = 1;
1167         phy->lo_control->rebuild = 0;
1168 }
1169
1170 #if B43_DEBUG
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)
1175 {
1176         if (b43_loctl_is_calibrated(loctl))
1177                 return;
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. */
1181                 return;
1182         }
1183         b43err(dev->wl, "Adjusting Local Oscillator to an uncalibrated "
1184                "control pair: rfatt=%u,%spadmix bbatt=%u\n",
1185                rfatt->att,
1186                (rfatt->with_padmix) ? "" : "no-",
1187                bbatt->att);
1188 }
1189 #else
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)
1194 {
1195 }
1196 #endif
1197
1198 static inline void fixup_rfatt_for_txcontrol(struct b43_rfatt *rf,
1199                                              u8 tx_control)
1200 {
1201         if (tx_control & B43_TXCTL_TXMIX) {
1202                 if (rf->att < 5)
1203                         rf->att = 4;
1204         }
1205 }
1206
1207 void b43_lo_g_adjust(struct b43_wldev *dev)
1208 {
1209         struct b43_phy *phy = &dev->phy;
1210         struct b43_rfatt rf;
1211         struct b43_loctl *loctl;
1212
1213         memcpy(&rf, &phy->rfatt, sizeof(rf));
1214         fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
1215
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);
1219 }
1220
1221 void b43_lo_g_adjust_to(struct b43_wldev *dev,
1222                         u16 rfatt, u16 bbatt, u16 tx_control)
1223 {
1224         struct b43_rfatt rf;
1225         struct b43_bbatt bb;
1226         struct b43_loctl *loctl;
1227
1228         memset(&rf, 0, sizeof(rf));
1229         memset(&bb, 0, sizeof(bb));
1230         rf.att = rfatt;
1231         bb.att = bbatt;
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);
1236 }
1237
1238 static void do_mark_unused(struct b43_wldev *dev, struct b43_loctl *control)
1239 {
1240         control->used = 0;
1241 }
1242
1243 void b43_lo_g_ctl_mark_all_unused(struct b43_wldev *dev)
1244 {
1245         struct b43_phy *phy = &dev->phy;
1246         struct b43_txpower_lo_control *lo = phy->lo_control;
1247
1248         b43_call_for_each_loctl(dev, do_mark_unused);
1249         lo->rebuild = 1;
1250 }
1251
1252 void b43_lo_g_ctl_mark_cur_used(struct b43_wldev *dev)
1253 {
1254         struct b43_phy *phy = &dev->phy;
1255         struct b43_rfatt rf;
1256
1257         memcpy(&rf, &phy->rfatt, sizeof(rf));
1258         fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
1259
1260         b43_get_lo_g_ctl(dev, &rf, &phy->bbatt)->used = 1;
1261 }