Merge commit 'v2.6.28-rc8' into x86/doc
[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_g.h"
33 #include "main.h"
34
35 #include <linux/delay.h>
36 #include <linux/sched.h>
37
38
39 static struct b43_lo_calib * b43_find_lo_calib(struct b43_txpower_lo_control *lo,
40                                                const struct b43_bbatt *bbatt,
41                                                const struct b43_rfatt *rfatt)
42 {
43         struct b43_lo_calib *c;
44
45         list_for_each_entry(c, &lo->calib_list, list) {
46                 if (!b43_compare_bbatt(&c->bbatt, bbatt))
47                         continue;
48                 if (!b43_compare_rfatt(&c->rfatt, rfatt))
49                         continue;
50                 return c;
51         }
52
53         return NULL;
54 }
55
56 /* Write the LocalOscillator Control (adjust) value-pair. */
57 static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
58 {
59         struct b43_phy *phy = &dev->phy;
60         u16 value;
61
62         if (B43_DEBUG) {
63                 if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
64                         b43dbg(dev->wl, "Invalid LO control pair "
65                                "(I: %d, Q: %d)\n", control->i, control->q);
66                         dump_stack();
67                         return;
68                 }
69         }
70         B43_WARN_ON(phy->type != B43_PHYTYPE_G);
71
72         value = (u8) (control->q);
73         value |= ((u8) (control->i)) << 8;
74         b43_phy_write(dev, B43_PHY_LO_CTL, value);
75 }
76
77 static u16 lo_measure_feedthrough(struct b43_wldev *dev,
78                                   u16 lna, u16 pga, u16 trsw_rx)
79 {
80         struct b43_phy *phy = &dev->phy;
81         u16 rfover;
82         u16 feedthrough;
83
84         if (phy->gmode) {
85                 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
86                 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
87
88                 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
89                 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
90 /*FIXME This assertion fails            B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
91                                     B43_PHY_RFOVERVAL_BW));
92 */
93                 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
94
95                 /* Construct the RF Override Value */
96                 rfover = B43_PHY_RFOVERVAL_UNK;
97                 rfover |= pga;
98                 rfover |= lna;
99                 rfover |= trsw_rx;
100                 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
101                     && phy->rev > 6)
102                         rfover |= B43_PHY_RFOVERVAL_EXTLNA;
103
104                 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
105                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
106                 udelay(10);
107                 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
108                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
109                 udelay(10);
110                 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
111                 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
112                 udelay(10);
113                 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
114         } else {
115                 pga |= B43_PHY_PGACTL_UNKNOWN;
116                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
117                 udelay(10);
118                 pga |= B43_PHY_PGACTL_LOWBANDW;
119                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
120                 udelay(10);
121                 pga |= B43_PHY_PGACTL_LPF;
122                 b43_phy_write(dev, B43_PHY_PGACTL, pga);
123         }
124         udelay(21);
125         feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
126
127         /* This is a good place to check if we need to relax a bit,
128          * as this is the main function called regularly
129          * in the LO calibration. */
130         cond_resched();
131
132         return feedthrough;
133 }
134
135 /* TXCTL Register and Value Table.
136  * Returns the "TXCTL Register".
137  * "value" is the "TXCTL Value".
138  * "pad_mix_gain" is the PAD Mixer Gain.
139  */
140 static u16 lo_txctl_register_table(struct b43_wldev *dev,
141                                    u16 * value, u16 * pad_mix_gain)
142 {
143         struct b43_phy *phy = &dev->phy;
144         u16 reg, v, padmix;
145
146         if (phy->type == B43_PHYTYPE_B) {
147                 v = 0x30;
148                 if (phy->radio_rev <= 5) {
149                         reg = 0x43;
150                         padmix = 0;
151                 } else {
152                         reg = 0x52;
153                         padmix = 5;
154                 }
155         } else {
156                 if (phy->rev >= 2 && phy->radio_rev == 8) {
157                         reg = 0x43;
158                         v = 0x10;
159                         padmix = 2;
160                 } else {
161                         reg = 0x52;
162                         v = 0x30;
163                         padmix = 5;
164                 }
165         }
166         if (value)
167                 *value = v;
168         if (pad_mix_gain)
169                 *pad_mix_gain = padmix;
170
171         return reg;
172 }
173
174 static void lo_measure_txctl_values(struct b43_wldev *dev)
175 {
176         struct b43_phy *phy = &dev->phy;
177         struct b43_phy_g *gphy = phy->g;
178         struct b43_txpower_lo_control *lo = gphy->lo_control;
179         u16 reg, mask;
180         u16 trsw_rx, pga;
181         u16 radio_pctl_reg;
182
183         static const u8 tx_bias_values[] = {
184                 0x09, 0x08, 0x0A, 0x01, 0x00,
185                 0x02, 0x05, 0x04, 0x06,
186         };
187         static const u8 tx_magn_values[] = {
188                 0x70, 0x40,
189         };
190
191         if (!has_loopback_gain(phy)) {
192                 radio_pctl_reg = 6;
193                 trsw_rx = 2;
194                 pga = 0;
195         } else {
196                 int lb_gain;    /* Loopback gain (in dB) */
197
198                 trsw_rx = 0;
199                 lb_gain = gphy->max_lb_gain / 2;
200                 if (lb_gain > 10) {
201                         radio_pctl_reg = 0;
202                         pga = abs(10 - lb_gain) / 6;
203                         pga = clamp_val(pga, 0, 15);
204                 } else {
205                         int cmp_val;
206                         int tmp;
207
208                         pga = 0;
209                         cmp_val = 0x24;
210                         if ((phy->rev >= 2) &&
211                             (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
212                                 cmp_val = 0x3C;
213                         tmp = lb_gain;
214                         if ((10 - lb_gain) < cmp_val)
215                                 tmp = (10 - lb_gain);
216                         if (tmp < 0)
217                                 tmp += 6;
218                         else
219                                 tmp += 3;
220                         cmp_val /= 4;
221                         tmp /= 4;
222                         if (tmp >= cmp_val)
223                                 radio_pctl_reg = cmp_val;
224                         else
225                                 radio_pctl_reg = tmp;
226                 }
227         }
228         b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
229                                       & 0xFFF0) | radio_pctl_reg);
230         b43_gphy_set_baseband_attenuation(dev, 2);
231
232         reg = lo_txctl_register_table(dev, &mask, NULL);
233         mask = ~mask;
234         b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
235                           & mask);
236
237         if (has_tx_magnification(phy)) {
238                 int i, j;
239                 int feedthrough;
240                 int min_feedth = 0xFFFF;
241                 u8 tx_magn, tx_bias;
242
243                 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
244                         tx_magn = tx_magn_values[i];
245                         b43_radio_write16(dev, 0x52,
246                                           (b43_radio_read16(dev, 0x52)
247                                            & 0xFF0F) | tx_magn);
248                         for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
249                                 tx_bias = tx_bias_values[j];
250                                 b43_radio_write16(dev, 0x52,
251                                                   (b43_radio_read16(dev, 0x52)
252                                                    & 0xFFF0) | tx_bias);
253                                 feedthrough =
254                                     lo_measure_feedthrough(dev, 0, pga,
255                                                            trsw_rx);
256                                 if (feedthrough < min_feedth) {
257                                         lo->tx_bias = tx_bias;
258                                         lo->tx_magn = tx_magn;
259                                         min_feedth = feedthrough;
260                                 }
261                                 if (lo->tx_bias == 0)
262                                         break;
263                         }
264                         b43_radio_write16(dev, 0x52,
265                                           (b43_radio_read16(dev, 0x52)
266                                            & 0xFF00) | lo->tx_bias | lo->
267                                           tx_magn);
268                 }
269         } else {
270                 lo->tx_magn = 0;
271                 lo->tx_bias = 0;
272                 b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
273                                   & 0xFFF0);    /* TX bias == 0 */
274         }
275         lo->txctl_measured_time = jiffies;
276 }
277
278 static void lo_read_power_vector(struct b43_wldev *dev)
279 {
280         struct b43_phy *phy = &dev->phy;
281         struct b43_phy_g *gphy = phy->g;
282         struct b43_txpower_lo_control *lo = gphy->lo_control;
283         int i;
284         u64 tmp;
285         u64 power_vector = 0;
286
287         for (i = 0; i < 8; i += 2) {
288                 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
289                 power_vector |= (tmp << (i * 8));
290                 /* Clear the vector on the device. */
291                 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
292         }
293         if (power_vector)
294                 lo->power_vector = power_vector;
295         lo->pwr_vec_read_time = jiffies;
296 }
297
298 /* 802.11/LO/GPHY/MeasuringGains */
299 static void lo_measure_gain_values(struct b43_wldev *dev,
300                                    s16 max_rx_gain, int use_trsw_rx)
301 {
302         struct b43_phy *phy = &dev->phy;
303         struct b43_phy_g *gphy = phy->g;
304         u16 tmp;
305
306         if (max_rx_gain < 0)
307                 max_rx_gain = 0;
308
309         if (has_loopback_gain(phy)) {
310                 int trsw_rx = 0;
311                 int trsw_rx_gain;
312
313                 if (use_trsw_rx) {
314                         trsw_rx_gain = gphy->trsw_rx_gain / 2;
315                         if (max_rx_gain >= trsw_rx_gain) {
316                                 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
317                                 trsw_rx = 0x20;
318                         }
319                 } else
320                         trsw_rx_gain = max_rx_gain;
321                 if (trsw_rx_gain < 9) {
322                         gphy->lna_lod_gain = 0;
323                 } else {
324                         gphy->lna_lod_gain = 1;
325                         trsw_rx_gain -= 8;
326                 }
327                 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
328                 gphy->pga_gain = trsw_rx_gain / 3;
329                 if (gphy->pga_gain >= 5) {
330                         gphy->pga_gain -= 5;
331                         gphy->lna_gain = 2;
332                 } else
333                         gphy->lna_gain = 0;
334         } else {
335                 gphy->lna_gain = 0;
336                 gphy->trsw_rx_gain = 0x20;
337                 if (max_rx_gain >= 0x14) {
338                         gphy->lna_lod_gain = 1;
339                         gphy->pga_gain = 2;
340                 } else if (max_rx_gain >= 0x12) {
341                         gphy->lna_lod_gain = 1;
342                         gphy->pga_gain = 1;
343                 } else if (max_rx_gain >= 0xF) {
344                         gphy->lna_lod_gain = 1;
345                         gphy->pga_gain = 0;
346                 } else {
347                         gphy->lna_lod_gain = 0;
348                         gphy->pga_gain = 0;
349                 }
350         }
351
352         tmp = b43_radio_read16(dev, 0x7A);
353         if (gphy->lna_lod_gain == 0)
354                 tmp &= ~0x0008;
355         else
356                 tmp |= 0x0008;
357         b43_radio_write16(dev, 0x7A, tmp);
358 }
359
360 struct lo_g_saved_values {
361         u8 old_channel;
362
363         /* Core registers */
364         u16 reg_3F4;
365         u16 reg_3E2;
366
367         /* PHY registers */
368         u16 phy_lo_mask;
369         u16 phy_extg_01;
370         u16 phy_dacctl_hwpctl;
371         u16 phy_dacctl;
372         u16 phy_cck_14;
373         u16 phy_hpwr_tssictl;
374         u16 phy_analogover;
375         u16 phy_analogoverval;
376         u16 phy_rfover;
377         u16 phy_rfoverval;
378         u16 phy_classctl;
379         u16 phy_cck_3E;
380         u16 phy_crs0;
381         u16 phy_pgactl;
382         u16 phy_cck_2A;
383         u16 phy_syncctl;
384         u16 phy_cck_30;
385         u16 phy_cck_06;
386
387         /* Radio registers */
388         u16 radio_43;
389         u16 radio_7A;
390         u16 radio_52;
391 };
392
393 static void lo_measure_setup(struct b43_wldev *dev,
394                              struct lo_g_saved_values *sav)
395 {
396         struct ssb_sprom *sprom = &dev->dev->bus->sprom;
397         struct b43_phy *phy = &dev->phy;
398         struct b43_phy_g *gphy = phy->g;
399         struct b43_txpower_lo_control *lo = gphy->lo_control;
400         u16 tmp;
401
402         if (b43_has_hardware_pctl(dev)) {
403                 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
404                 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
405                 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
406                 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
407                 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
408
409                 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
410                               b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
411                               | 0x100);
412                 b43_phy_write(dev, B43_PHY_EXTG(0x01),
413                               b43_phy_read(dev, B43_PHY_EXTG(0x01))
414                               | 0x40);
415                 b43_phy_write(dev, B43_PHY_DACCTL,
416                               b43_phy_read(dev, B43_PHY_DACCTL)
417                               | 0x40);
418                 b43_phy_write(dev, B43_PHY_CCK(0x14),
419                               b43_phy_read(dev, B43_PHY_CCK(0x14))
420                               | 0x200);
421         }
422         if (phy->type == B43_PHYTYPE_B &&
423             phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
424                 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
425                 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
426         }
427         if (phy->rev >= 2) {
428                 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
429                 sav->phy_analogoverval =
430                     b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
431                 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
432                 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
433                 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
434                 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
435                 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
436
437                 b43_phy_write(dev, B43_PHY_CLASSCTL,
438                               b43_phy_read(dev, B43_PHY_CLASSCTL)
439                               & 0xFFFC);
440                 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
441                               & 0x7FFF);
442                 b43_phy_write(dev, B43_PHY_ANALOGOVER,
443                               b43_phy_read(dev, B43_PHY_ANALOGOVER)
444                               | 0x0003);
445                 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
446                               b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
447                               & 0xFFFC);
448                 if (phy->type == B43_PHYTYPE_G) {
449                         if ((phy->rev >= 7) &&
450                             (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
451                                 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
452                         } else {
453                                 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
454                         }
455                 } else {
456                         b43_phy_write(dev, B43_PHY_RFOVER, 0);
457                 }
458                 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
459         }
460         sav->reg_3F4 = b43_read16(dev, 0x3F4);
461         sav->reg_3E2 = b43_read16(dev, 0x3E2);
462         sav->radio_43 = b43_radio_read16(dev, 0x43);
463         sav->radio_7A = b43_radio_read16(dev, 0x7A);
464         sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
465         sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
466         sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
467         sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
468
469         if (!has_tx_magnification(phy)) {
470                 sav->radio_52 = b43_radio_read16(dev, 0x52);
471                 sav->radio_52 &= 0x00F0;
472         }
473         if (phy->type == B43_PHYTYPE_B) {
474                 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
475                 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
476                 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
477                 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
478         } else {
479                 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
480                             | 0x8000);
481         }
482         b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
483                     & 0xF000);
484
485         tmp =
486             (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
487         b43_phy_write(dev, tmp, 0x007F);
488
489         tmp = sav->phy_syncctl;
490         b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
491         tmp = sav->radio_7A;
492         b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
493
494         b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
495         if (phy->type == B43_PHYTYPE_G ||
496             (phy->type == B43_PHYTYPE_B &&
497              phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
498                 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
499         } else
500                 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
501         if (phy->rev >= 2)
502                 b43_dummy_transmission(dev);
503         b43_gphy_channel_switch(dev, 6, 0);
504         b43_radio_read16(dev, 0x51);    /* dummy read */
505         if (phy->type == B43_PHYTYPE_G)
506                 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
507
508         /* Re-measure the txctl values, if needed. */
509         if (time_before(lo->txctl_measured_time,
510                         jiffies - B43_LO_TXCTL_EXPIRE))
511                 lo_measure_txctl_values(dev);
512
513         if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
514                 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
515         } else {
516                 if (phy->type == B43_PHYTYPE_B)
517                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
518                 else
519                         b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
520         }
521 }
522
523 static void lo_measure_restore(struct b43_wldev *dev,
524                                struct lo_g_saved_values *sav)
525 {
526         struct b43_phy *phy = &dev->phy;
527         struct b43_phy_g *gphy = phy->g;
528         u16 tmp;
529
530         if (phy->rev >= 2) {
531                 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
532                 tmp = (gphy->pga_gain << 8);
533                 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
534                 udelay(5);
535                 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
536                 udelay(2);
537                 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
538         } else {
539                 tmp = (gphy->pga_gain | 0xEFA0);
540                 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
541         }
542         if (phy->type == B43_PHYTYPE_G) {
543                 if (phy->rev >= 3)
544                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
545                 else
546                         b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
547                 if (phy->rev >= 2)
548                         b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
549                 else
550                         b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
551         }
552         b43_write16(dev, 0x3F4, sav->reg_3F4);
553         b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
554         b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
555         b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
556         b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
557         b43_radio_write16(dev, 0x43, sav->radio_43);
558         b43_radio_write16(dev, 0x7A, sav->radio_7A);
559         if (!has_tx_magnification(phy)) {
560                 tmp = sav->radio_52;
561                 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
562                                               & 0xFF0F) | tmp);
563         }
564         b43_write16(dev, 0x3E2, sav->reg_3E2);
565         if (phy->type == B43_PHYTYPE_B &&
566             phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
567                 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
568                 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
569         }
570         if (phy->rev >= 2) {
571                 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
572                 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
573                               sav->phy_analogoverval);
574                 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
575                 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
576                 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
577                 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
578                 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
579         }
580         if (b43_has_hardware_pctl(dev)) {
581                 tmp = (sav->phy_lo_mask & 0xBFFF);
582                 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
583                 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
584                 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
585                 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
586                 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
587         }
588         b43_gphy_channel_switch(dev, sav->old_channel, 1);
589 }
590
591 struct b43_lo_g_statemachine {
592         int current_state;
593         int nr_measured;
594         int state_val_multiplier;
595         u16 lowest_feedth;
596         struct b43_loctl min_loctl;
597 };
598
599 /* Loop over each possible value in this state. */
600 static int lo_probe_possible_loctls(struct b43_wldev *dev,
601                                     struct b43_loctl *probe_loctl,
602                                     struct b43_lo_g_statemachine *d)
603 {
604         struct b43_phy *phy = &dev->phy;
605         struct b43_phy_g *gphy = phy->g;
606         struct b43_loctl test_loctl;
607         struct b43_loctl orig_loctl;
608         struct b43_loctl prev_loctl = {
609                 .i = -100,
610                 .q = -100,
611         };
612         int i;
613         int begin, end;
614         int found_lower = 0;
615         u16 feedth;
616
617         static const struct b43_loctl modifiers[] = {
618                 {.i = 1,.q = 1,},
619                 {.i = 1,.q = 0,},
620                 {.i = 1,.q = -1,},
621                 {.i = 0,.q = -1,},
622                 {.i = -1,.q = -1,},
623                 {.i = -1,.q = 0,},
624                 {.i = -1,.q = 1,},
625                 {.i = 0,.q = 1,},
626         };
627
628         if (d->current_state == 0) {
629                 begin = 1;
630                 end = 8;
631         } else if (d->current_state % 2 == 0) {
632                 begin = d->current_state - 1;
633                 end = d->current_state + 1;
634         } else {
635                 begin = d->current_state - 2;
636                 end = d->current_state + 2;
637         }
638         if (begin < 1)
639                 begin += 8;
640         if (end > 8)
641                 end -= 8;
642
643         memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
644         i = begin;
645         d->current_state = i;
646         while (1) {
647                 B43_WARN_ON(!(i >= 1 && i <= 8));
648                 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
649                 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
650                 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
651                 if ((test_loctl.i != prev_loctl.i ||
652                      test_loctl.q != prev_loctl.q) &&
653                     (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
654                         b43_lo_write(dev, &test_loctl);
655                         feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
656                                                         gphy->pga_gain,
657                                                         gphy->trsw_rx_gain);
658                         if (feedth < d->lowest_feedth) {
659                                 memcpy(probe_loctl, &test_loctl,
660                                        sizeof(struct b43_loctl));
661                                 found_lower = 1;
662                                 d->lowest_feedth = feedth;
663                                 if ((d->nr_measured < 2) &&
664                                     !has_loopback_gain(phy))
665                                         break;
666                         }
667                 }
668                 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
669                 if (i == end)
670                         break;
671                 if (i == 8)
672                         i = 1;
673                 else
674                         i++;
675                 d->current_state = i;
676         }
677
678         return found_lower;
679 }
680
681 static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
682                                          struct b43_loctl *loctl,
683                                          int *max_rx_gain)
684 {
685         struct b43_phy *phy = &dev->phy;
686         struct b43_phy_g *gphy = phy->g;
687         struct b43_lo_g_statemachine d;
688         u16 feedth;
689         int found_lower;
690         struct b43_loctl probe_loctl;
691         int max_repeat = 1, repeat_cnt = 0;
692
693         d.nr_measured = 0;
694         d.state_val_multiplier = 1;
695         if (has_loopback_gain(phy))
696                 d.state_val_multiplier = 3;
697
698         memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
699         if (has_loopback_gain(phy))
700                 max_repeat = 4;
701         do {
702                 b43_lo_write(dev, &d.min_loctl);
703                 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
704                                                 gphy->pga_gain,
705                                                 gphy->trsw_rx_gain);
706                 if (feedth < 0x258) {
707                         if (feedth >= 0x12C)
708                                 *max_rx_gain += 6;
709                         else
710                                 *max_rx_gain += 3;
711                         feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
712                                                         gphy->pga_gain,
713                                                         gphy->trsw_rx_gain);
714                 }
715                 d.lowest_feedth = feedth;
716
717                 d.current_state = 0;
718                 do {
719                         B43_WARN_ON(!
720                                     (d.current_state >= 0
721                                      && d.current_state <= 8));
722                         memcpy(&probe_loctl, &d.min_loctl,
723                                sizeof(struct b43_loctl));
724                         found_lower =
725                             lo_probe_possible_loctls(dev, &probe_loctl, &d);
726                         if (!found_lower)
727                                 break;
728                         if ((probe_loctl.i == d.min_loctl.i) &&
729                             (probe_loctl.q == d.min_loctl.q))
730                                 break;
731                         memcpy(&d.min_loctl, &probe_loctl,
732                                sizeof(struct b43_loctl));
733                         d.nr_measured++;
734                 } while (d.nr_measured < 24);
735                 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
736
737                 if (has_loopback_gain(phy)) {
738                         if (d.lowest_feedth > 0x1194)
739                                 *max_rx_gain -= 6;
740                         else if (d.lowest_feedth < 0x5DC)
741                                 *max_rx_gain += 3;
742                         if (repeat_cnt == 0) {
743                                 if (d.lowest_feedth <= 0x5DC) {
744                                         d.state_val_multiplier = 1;
745                                         repeat_cnt++;
746                                 } else
747                                         d.state_val_multiplier = 2;
748                         } else if (repeat_cnt == 2)
749                                 d.state_val_multiplier = 1;
750                 }
751                 lo_measure_gain_values(dev, *max_rx_gain,
752                                        has_loopback_gain(phy));
753         } while (++repeat_cnt < max_repeat);
754 }
755
756 static
757 struct b43_lo_calib * b43_calibrate_lo_setting(struct b43_wldev *dev,
758                                                const struct b43_bbatt *bbatt,
759                                                const struct b43_rfatt *rfatt)
760 {
761         struct b43_phy *phy = &dev->phy;
762         struct b43_phy_g *gphy = phy->g;
763         struct b43_loctl loctl = {
764                 .i = 0,
765                 .q = 0,
766         };
767         int max_rx_gain;
768         struct b43_lo_calib *cal;
769         struct lo_g_saved_values uninitialized_var(saved_regs);
770         /* Values from the "TXCTL Register and Value Table" */
771         u16 txctl_reg;
772         u16 txctl_value;
773         u16 pad_mix_gain;
774
775         saved_regs.old_channel = phy->channel;
776         b43_mac_suspend(dev);
777         lo_measure_setup(dev, &saved_regs);
778
779         txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
780
781         b43_radio_write16(dev, 0x43,
782                           (b43_radio_read16(dev, 0x43) & 0xFFF0)
783                           | rfatt->att);
784         b43_radio_write16(dev, txctl_reg,
785                           (b43_radio_read16(dev, txctl_reg) & ~txctl_value)
786                           | (rfatt->with_padmix) ? txctl_value : 0);
787
788         max_rx_gain = rfatt->att * 2;
789         max_rx_gain += bbatt->att / 2;
790         if (rfatt->with_padmix)
791                 max_rx_gain -= pad_mix_gain;
792         if (has_loopback_gain(phy))
793                 max_rx_gain += gphy->max_lb_gain;
794         lo_measure_gain_values(dev, max_rx_gain,
795                                has_loopback_gain(phy));
796
797         b43_gphy_set_baseband_attenuation(dev, bbatt->att);
798         lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
799
800         lo_measure_restore(dev, &saved_regs);
801         b43_mac_enable(dev);
802
803         if (b43_debug(dev, B43_DBG_LO)) {
804                 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
805                        "=> I=%d Q=%d\n",
806                        bbatt->att, rfatt->att, rfatt->with_padmix,
807                        loctl.i, loctl.q);
808         }
809
810         cal = kmalloc(sizeof(*cal), GFP_KERNEL);
811         if (!cal) {
812                 b43warn(dev->wl, "LO calib: out of memory\n");
813                 return NULL;
814         }
815         memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
816         memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
817         memcpy(&cal->ctl, &loctl, sizeof(loctl));
818         cal->calib_time = jiffies;
819         INIT_LIST_HEAD(&cal->list);
820
821         return cal;
822 }
823
824 /* Get a calibrated LO setting for the given attenuation values.
825  * Might return a NULL pointer under OOM! */
826 static
827 struct b43_lo_calib * b43_get_calib_lo_settings(struct b43_wldev *dev,
828                                                 const struct b43_bbatt *bbatt,
829                                                 const struct b43_rfatt *rfatt)
830 {
831         struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
832         struct b43_lo_calib *c;
833
834         c = b43_find_lo_calib(lo, bbatt, rfatt);
835         if (c)
836                 return c;
837         /* Not in the list of calibrated LO settings.
838          * Calibrate it now. */
839         c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
840         if (!c)
841                 return NULL;
842         list_add(&c->list, &lo->calib_list);
843
844         return c;
845 }
846
847 void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
848 {
849         struct b43_phy *phy = &dev->phy;
850         struct b43_phy_g *gphy = phy->g;
851         struct b43_txpower_lo_control *lo = gphy->lo_control;
852         int i;
853         int rf_offset, bb_offset;
854         const struct b43_rfatt *rfatt;
855         const struct b43_bbatt *bbatt;
856         u64 power_vector;
857         bool table_changed = 0;
858
859         BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
860         B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
861
862         power_vector = lo->power_vector;
863         if (!update_all && !power_vector)
864                 return; /* Nothing to do. */
865
866         /* Suspend the MAC now to avoid continuous suspend/enable
867          * cycles in the loop. */
868         b43_mac_suspend(dev);
869
870         for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
871                 struct b43_lo_calib *cal;
872                 int idx;
873                 u16 val;
874
875                 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
876                         continue;
877                 /* Update the table entry for this power_vector bit.
878                  * The table rows are RFatt entries and columns are BBatt. */
879                 bb_offset = i / lo->rfatt_list.len;
880                 rf_offset = i % lo->rfatt_list.len;
881                 bbatt = &(lo->bbatt_list.list[bb_offset]);
882                 rfatt = &(lo->rfatt_list.list[rf_offset]);
883
884                 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
885                 if (!cal) {
886                         b43warn(dev->wl, "LO: Could not "
887                                 "calibrate DC table entry\n");
888                         continue;
889                 }
890                 /*FIXME: Is Q really in the low nibble? */
891                 val = (u8)(cal->ctl.q);
892                 val |= ((u8)(cal->ctl.i)) << 4;
893                 kfree(cal);
894
895                 /* Get the index into the hardware DC LT. */
896                 idx = i / 2;
897                 /* Change the table in memory. */
898                 if (i % 2) {
899                         /* Change the high byte. */
900                         lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
901                                          | ((val & 0x00FF) << 8);
902                 } else {
903                         /* Change the low byte. */
904                         lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
905                                          | (val & 0x00FF);
906                 }
907                 table_changed = 1;
908         }
909         if (table_changed) {
910                 /* The table changed in memory. Update the hardware table. */
911                 for (i = 0; i < B43_DC_LT_SIZE; i++)
912                         b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
913         }
914         b43_mac_enable(dev);
915 }
916
917 /* Fixup the RF attenuation value for the case where we are
918  * using the PAD mixer. */
919 static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
920 {
921         if (!rf->with_padmix)
922                 return;
923         if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
924                 rf->att = 4;
925 }
926
927 void b43_lo_g_adjust(struct b43_wldev *dev)
928 {
929         struct b43_phy_g *gphy = dev->phy.g;
930         struct b43_lo_calib *cal;
931         struct b43_rfatt rf;
932
933         memcpy(&rf, &gphy->rfatt, sizeof(rf));
934         b43_lo_fixup_rfatt(&rf);
935
936         cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
937         if (!cal)
938                 return;
939         b43_lo_write(dev, &cal->ctl);
940 }
941
942 void b43_lo_g_adjust_to(struct b43_wldev *dev,
943                         u16 rfatt, u16 bbatt, u16 tx_control)
944 {
945         struct b43_rfatt rf;
946         struct b43_bbatt bb;
947         struct b43_lo_calib *cal;
948
949         memset(&rf, 0, sizeof(rf));
950         memset(&bb, 0, sizeof(bb));
951         rf.att = rfatt;
952         bb.att = bbatt;
953         b43_lo_fixup_rfatt(&rf);
954         cal = b43_get_calib_lo_settings(dev, &bb, &rf);
955         if (!cal)
956                 return;
957         b43_lo_write(dev, &cal->ctl);
958 }
959
960 /* Periodic LO maintanance work */
961 void b43_lo_g_maintanance_work(struct b43_wldev *dev)
962 {
963         struct b43_phy *phy = &dev->phy;
964         struct b43_phy_g *gphy = phy->g;
965         struct b43_txpower_lo_control *lo = gphy->lo_control;
966         unsigned long now;
967         unsigned long expire;
968         struct b43_lo_calib *cal, *tmp;
969         bool current_item_expired = 0;
970         bool hwpctl;
971
972         if (!lo)
973                 return;
974         now = jiffies;
975         hwpctl = b43_has_hardware_pctl(dev);
976
977         if (hwpctl) {
978                 /* Read the power vector and update it, if needed. */
979                 expire = now - B43_LO_PWRVEC_EXPIRE;
980                 if (time_before(lo->pwr_vec_read_time, expire)) {
981                         lo_read_power_vector(dev);
982                         b43_gphy_dc_lt_init(dev, 0);
983                 }
984                 //FIXME Recalc the whole DC table from time to time?
985         }
986
987         if (hwpctl)
988                 return;
989         /* Search for expired LO settings. Remove them.
990          * Recalibrate the current setting, if expired. */
991         expire = now - B43_LO_CALIB_EXPIRE;
992         list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
993                 if (!time_before(cal->calib_time, expire))
994                         continue;
995                 /* This item expired. */
996                 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
997                     b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
998                         B43_WARN_ON(current_item_expired);
999                         current_item_expired = 1;
1000                 }
1001                 if (b43_debug(dev, B43_DBG_LO)) {
1002                         b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
1003                                "I=%d, Q=%d expired\n",
1004                                cal->bbatt.att, cal->rfatt.att,
1005                                cal->rfatt.with_padmix,
1006                                cal->ctl.i, cal->ctl.q);
1007                 }
1008                 list_del(&cal->list);
1009                 kfree(cal);
1010         }
1011         if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
1012                 /* Recalibrate currently used LO setting. */
1013                 if (b43_debug(dev, B43_DBG_LO))
1014                         b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
1015                 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
1016                 if (cal) {
1017                         list_add(&cal->list, &lo->calib_list);
1018                         b43_lo_write(dev, &cal->ctl);
1019                 } else
1020                         b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
1021         }
1022 }
1023
1024 void b43_lo_g_cleanup(struct b43_wldev *dev)
1025 {
1026         struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
1027         struct b43_lo_calib *cal, *tmp;
1028
1029         if (!lo)
1030                 return;
1031         list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1032                 list_del(&cal->list);
1033                 kfree(cal);
1034         }
1035 }
1036
1037 /* LO Initialization */
1038 void b43_lo_g_init(struct b43_wldev *dev)
1039 {
1040         if (b43_has_hardware_pctl(dev)) {
1041                 lo_read_power_vector(dev);
1042                 b43_gphy_dc_lt_init(dev, 1);
1043         }
1044 }