GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / b43 / lo.c
blob4ef53759e2eb7ef032c18e95f151219312f8e08d
1 /*
3 Broadcom B43 wireless driver
5 G PHY LO (LocalOscillator) Measuring and Control routines
7 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
8 Copyright (c) 2005, 2006 Stefano Brivio <stefano.brivio@polimi.it>
9 Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
10 Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
26 Boston, MA 02110-1301, USA.
30 #include "b43.h"
31 #include "lo.h"
32 #include "phy_g.h"
33 #include "main.h"
35 #include <linux/delay.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
40 static struct b43_lo_calib *b43_find_lo_calib(struct b43_txpower_lo_control *lo,
41 const struct b43_bbatt *bbatt,
42 const struct b43_rfatt *rfatt)
44 struct b43_lo_calib *c;
46 list_for_each_entry(c, &lo->calib_list, list) {
47 if (!b43_compare_bbatt(&c->bbatt, bbatt))
48 continue;
49 if (!b43_compare_rfatt(&c->rfatt, rfatt))
50 continue;
51 return c;
54 return NULL;
57 /* Write the LocalOscillator Control (adjust) value-pair. */
58 static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
60 struct b43_phy *phy = &dev->phy;
61 u16 value;
63 if (B43_DEBUG) {
64 if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
65 b43dbg(dev->wl, "Invalid LO control pair "
66 "(I: %d, Q: %d)\n", control->i, control->q);
67 dump_stack();
68 return;
71 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
73 value = (u8) (control->q);
74 value |= ((u8) (control->i)) << 8;
75 b43_phy_write(dev, B43_PHY_LO_CTL, value);
78 static u16 lo_measure_feedthrough(struct b43_wldev *dev,
79 u16 lna, u16 pga, u16 trsw_rx)
81 struct b43_phy *phy = &dev->phy;
82 u16 rfover;
83 u16 feedthrough;
85 if (phy->gmode) {
86 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
87 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
89 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
90 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
91 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
93 /* Construct the RF Override Value */
94 rfover = B43_PHY_RFOVERVAL_UNK;
95 rfover |= pga;
96 rfover |= lna;
97 rfover |= trsw_rx;
98 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
99 && phy->rev > 6)
100 rfover |= B43_PHY_RFOVERVAL_EXTLNA;
102 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
103 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
104 udelay(10);
105 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
106 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
107 udelay(10);
108 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
109 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
110 udelay(10);
111 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
112 } else {
113 pga |= B43_PHY_PGACTL_UNKNOWN;
114 b43_phy_write(dev, B43_PHY_PGACTL, pga);
115 udelay(10);
116 pga |= B43_PHY_PGACTL_LOWBANDW;
117 b43_phy_write(dev, B43_PHY_PGACTL, pga);
118 udelay(10);
119 pga |= B43_PHY_PGACTL_LPF;
120 b43_phy_write(dev, B43_PHY_PGACTL, pga);
122 udelay(21);
123 feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
125 /* This is a good place to check if we need to relax a bit,
126 * as this is the main function called regularly
127 * in the LO calibration. */
128 cond_resched();
130 return feedthrough;
133 /* TXCTL Register and Value Table.
134 * Returns the "TXCTL Register".
135 * "value" is the "TXCTL Value".
136 * "pad_mix_gain" is the PAD Mixer Gain.
138 static u16 lo_txctl_register_table(struct b43_wldev *dev,
139 u16 *value, u16 *pad_mix_gain)
141 struct b43_phy *phy = &dev->phy;
142 u16 reg, v, padmix;
144 if (phy->type == B43_PHYTYPE_B) {
145 v = 0x30;
146 if (phy->radio_rev <= 5) {
147 reg = 0x43;
148 padmix = 0;
149 } else {
150 reg = 0x52;
151 padmix = 5;
153 } else {
154 if (phy->rev >= 2 && phy->radio_rev == 8) {
155 reg = 0x43;
156 v = 0x10;
157 padmix = 2;
158 } else {
159 reg = 0x52;
160 v = 0x30;
161 padmix = 5;
164 if (value)
165 *value = v;
166 if (pad_mix_gain)
167 *pad_mix_gain = padmix;
169 return reg;
172 static void lo_measure_txctl_values(struct b43_wldev *dev)
174 struct b43_phy *phy = &dev->phy;
175 struct b43_phy_g *gphy = phy->g;
176 struct b43_txpower_lo_control *lo = gphy->lo_control;
177 u16 reg, mask;
178 u16 trsw_rx, pga;
179 u16 radio_pctl_reg;
181 static const u8 tx_bias_values[] = {
182 0x09, 0x08, 0x0A, 0x01, 0x00,
183 0x02, 0x05, 0x04, 0x06,
185 static const u8 tx_magn_values[] = {
186 0x70, 0x40,
189 if (!has_loopback_gain(phy)) {
190 radio_pctl_reg = 6;
191 trsw_rx = 2;
192 pga = 0;
193 } else {
194 int lb_gain; /* Loopback gain (in dB) */
196 trsw_rx = 0;
197 lb_gain = gphy->max_lb_gain / 2;
198 if (lb_gain > 10) {
199 radio_pctl_reg = 0;
200 pga = abs(10 - lb_gain) / 6;
201 pga = clamp_val(pga, 0, 15);
202 } else {
203 int cmp_val;
204 int tmp;
206 pga = 0;
207 cmp_val = 0x24;
208 if ((phy->rev >= 2) &&
209 (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
210 cmp_val = 0x3C;
211 tmp = lb_gain;
212 if ((10 - lb_gain) < cmp_val)
213 tmp = (10 - lb_gain);
214 if (tmp < 0)
215 tmp += 6;
216 else
217 tmp += 3;
218 cmp_val /= 4;
219 tmp /= 4;
220 if (tmp >= cmp_val)
221 radio_pctl_reg = cmp_val;
222 else
223 radio_pctl_reg = tmp;
226 b43_radio_maskset(dev, 0x43, 0xFFF0, radio_pctl_reg);
227 b43_gphy_set_baseband_attenuation(dev, 2);
229 reg = lo_txctl_register_table(dev, &mask, NULL);
230 mask = ~mask;
231 b43_radio_mask(dev, reg, mask);
233 if (has_tx_magnification(phy)) {
234 int i, j;
235 int feedthrough;
236 int min_feedth = 0xFFFF;
237 u8 tx_magn, tx_bias;
239 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
240 tx_magn = tx_magn_values[i];
241 b43_radio_maskset(dev, 0x52, 0xFF0F, tx_magn);
242 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
243 tx_bias = tx_bias_values[j];
244 b43_radio_maskset(dev, 0x52, 0xFFF0, tx_bias);
245 feedthrough =
246 lo_measure_feedthrough(dev, 0, pga,
247 trsw_rx);
248 if (feedthrough < min_feedth) {
249 lo->tx_bias = tx_bias;
250 lo->tx_magn = tx_magn;
251 min_feedth = feedthrough;
253 if (lo->tx_bias == 0)
254 break;
256 b43_radio_write16(dev, 0x52,
257 (b43_radio_read16(dev, 0x52)
258 & 0xFF00) | lo->tx_bias | lo->
259 tx_magn);
261 } else {
262 lo->tx_magn = 0;
263 lo->tx_bias = 0;
264 b43_radio_mask(dev, 0x52, 0xFFF0); /* TX bias == 0 */
266 lo->txctl_measured_time = jiffies;
269 static void lo_read_power_vector(struct b43_wldev *dev)
271 struct b43_phy *phy = &dev->phy;
272 struct b43_phy_g *gphy = phy->g;
273 struct b43_txpower_lo_control *lo = gphy->lo_control;
274 int i;
275 u64 tmp;
276 u64 power_vector = 0;
278 for (i = 0; i < 8; i += 2) {
279 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
280 power_vector |= (tmp << (i * 8));
281 /* Clear the vector on the device. */
282 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
284 if (power_vector)
285 lo->power_vector = power_vector;
286 lo->pwr_vec_read_time = jiffies;
289 /* 802.11/LO/GPHY/MeasuringGains */
290 static void lo_measure_gain_values(struct b43_wldev *dev,
291 s16 max_rx_gain, int use_trsw_rx)
293 struct b43_phy *phy = &dev->phy;
294 struct b43_phy_g *gphy = phy->g;
295 u16 tmp;
297 if (max_rx_gain < 0)
298 max_rx_gain = 0;
300 if (has_loopback_gain(phy)) {
301 int trsw_rx = 0;
302 int trsw_rx_gain;
304 if (use_trsw_rx) {
305 trsw_rx_gain = gphy->trsw_rx_gain / 2;
306 if (max_rx_gain >= trsw_rx_gain) {
307 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
308 trsw_rx = 0x20;
310 } else
311 trsw_rx_gain = max_rx_gain;
312 if (trsw_rx_gain < 9) {
313 gphy->lna_lod_gain = 0;
314 } else {
315 gphy->lna_lod_gain = 1;
316 trsw_rx_gain -= 8;
318 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
319 gphy->pga_gain = trsw_rx_gain / 3;
320 if (gphy->pga_gain >= 5) {
321 gphy->pga_gain -= 5;
322 gphy->lna_gain = 2;
323 } else
324 gphy->lna_gain = 0;
325 } else {
326 gphy->lna_gain = 0;
327 gphy->trsw_rx_gain = 0x20;
328 if (max_rx_gain >= 0x14) {
329 gphy->lna_lod_gain = 1;
330 gphy->pga_gain = 2;
331 } else if (max_rx_gain >= 0x12) {
332 gphy->lna_lod_gain = 1;
333 gphy->pga_gain = 1;
334 } else if (max_rx_gain >= 0xF) {
335 gphy->lna_lod_gain = 1;
336 gphy->pga_gain = 0;
337 } else {
338 gphy->lna_lod_gain = 0;
339 gphy->pga_gain = 0;
343 tmp = b43_radio_read16(dev, 0x7A);
344 if (gphy->lna_lod_gain == 0)
345 tmp &= ~0x0008;
346 else
347 tmp |= 0x0008;
348 b43_radio_write16(dev, 0x7A, tmp);
351 struct lo_g_saved_values {
352 u8 old_channel;
354 /* Core registers */
355 u16 reg_3F4;
356 u16 reg_3E2;
358 /* PHY registers */
359 u16 phy_lo_mask;
360 u16 phy_extg_01;
361 u16 phy_dacctl_hwpctl;
362 u16 phy_dacctl;
363 u16 phy_cck_14;
364 u16 phy_hpwr_tssictl;
365 u16 phy_analogover;
366 u16 phy_analogoverval;
367 u16 phy_rfover;
368 u16 phy_rfoverval;
369 u16 phy_classctl;
370 u16 phy_cck_3E;
371 u16 phy_crs0;
372 u16 phy_pgactl;
373 u16 phy_cck_2A;
374 u16 phy_syncctl;
375 u16 phy_cck_30;
376 u16 phy_cck_06;
378 /* Radio registers */
379 u16 radio_43;
380 u16 radio_7A;
381 u16 radio_52;
384 static void lo_measure_setup(struct b43_wldev *dev,
385 struct lo_g_saved_values *sav)
387 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
388 struct b43_phy *phy = &dev->phy;
389 struct b43_phy_g *gphy = phy->g;
390 struct b43_txpower_lo_control *lo = gphy->lo_control;
391 u16 tmp;
393 if (b43_has_hardware_pctl(dev)) {
394 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
395 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
396 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
397 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
398 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
400 b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
401 b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
402 b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
403 b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
405 if (phy->type == B43_PHYTYPE_B &&
406 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
407 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
408 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
410 if (phy->rev >= 2) {
411 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
412 sav->phy_analogoverval =
413 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
414 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
415 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
416 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
417 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
418 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
420 b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
421 b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
422 b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
423 b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
424 if (phy->type == B43_PHYTYPE_G) {
425 if ((phy->rev >= 7) &&
426 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
427 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
428 } else {
429 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
431 } else {
432 b43_phy_write(dev, B43_PHY_RFOVER, 0);
434 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
436 sav->reg_3F4 = b43_read16(dev, 0x3F4);
437 sav->reg_3E2 = b43_read16(dev, 0x3E2);
438 sav->radio_43 = b43_radio_read16(dev, 0x43);
439 sav->radio_7A = b43_radio_read16(dev, 0x7A);
440 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
441 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
442 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
443 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
445 if (!has_tx_magnification(phy)) {
446 sav->radio_52 = b43_radio_read16(dev, 0x52);
447 sav->radio_52 &= 0x00F0;
449 if (phy->type == B43_PHYTYPE_B) {
450 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
451 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
452 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
453 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
454 } else {
455 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
456 | 0x8000);
458 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
459 & 0xF000);
461 tmp =
462 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
463 b43_phy_write(dev, tmp, 0x007F);
465 tmp = sav->phy_syncctl;
466 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
467 tmp = sav->radio_7A;
468 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
470 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
471 if (phy->type == B43_PHYTYPE_G ||
472 (phy->type == B43_PHYTYPE_B &&
473 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
474 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
475 } else
476 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
477 if (phy->rev >= 2)
478 b43_dummy_transmission(dev, false, true);
479 b43_gphy_channel_switch(dev, 6, 0);
480 b43_radio_read16(dev, 0x51); /* dummy read */
481 if (phy->type == B43_PHYTYPE_G)
482 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
484 /* Re-measure the txctl values, if needed. */
485 if (time_before(lo->txctl_measured_time,
486 jiffies - B43_LO_TXCTL_EXPIRE))
487 lo_measure_txctl_values(dev);
489 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
490 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
491 } else {
492 if (phy->type == B43_PHYTYPE_B)
493 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
494 else
495 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
499 static void lo_measure_restore(struct b43_wldev *dev,
500 struct lo_g_saved_values *sav)
502 struct b43_phy *phy = &dev->phy;
503 struct b43_phy_g *gphy = phy->g;
504 u16 tmp;
506 if (phy->rev >= 2) {
507 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
508 tmp = (gphy->pga_gain << 8);
509 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
510 udelay(5);
511 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
512 udelay(2);
513 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
514 } else {
515 tmp = (gphy->pga_gain | 0xEFA0);
516 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
518 if (phy->type == B43_PHYTYPE_G) {
519 if (phy->rev >= 3)
520 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
521 else
522 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
523 if (phy->rev >= 2)
524 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
525 else
526 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
528 b43_write16(dev, 0x3F4, sav->reg_3F4);
529 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
530 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
531 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
532 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
533 b43_radio_write16(dev, 0x43, sav->radio_43);
534 b43_radio_write16(dev, 0x7A, sav->radio_7A);
535 if (!has_tx_magnification(phy)) {
536 tmp = sav->radio_52;
537 b43_radio_maskset(dev, 0x52, 0xFF0F, tmp);
539 b43_write16(dev, 0x3E2, sav->reg_3E2);
540 if (phy->type == B43_PHYTYPE_B &&
541 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
542 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
543 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
545 if (phy->rev >= 2) {
546 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
547 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
548 sav->phy_analogoverval);
549 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
550 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
551 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
552 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
553 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
555 if (b43_has_hardware_pctl(dev)) {
556 tmp = (sav->phy_lo_mask & 0xBFFF);
557 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
558 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
559 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
560 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
561 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
563 b43_gphy_channel_switch(dev, sav->old_channel, 1);
566 struct b43_lo_g_statemachine {
567 int current_state;
568 int nr_measured;
569 int state_val_multiplier;
570 u16 lowest_feedth;
571 struct b43_loctl min_loctl;
574 /* Loop over each possible value in this state. */
575 static int lo_probe_possible_loctls(struct b43_wldev *dev,
576 struct b43_loctl *probe_loctl,
577 struct b43_lo_g_statemachine *d)
579 struct b43_phy *phy = &dev->phy;
580 struct b43_phy_g *gphy = phy->g;
581 struct b43_loctl test_loctl;
582 struct b43_loctl orig_loctl;
583 struct b43_loctl prev_loctl = {
584 .i = -100,
585 .q = -100,
587 int i;
588 int begin, end;
589 int found_lower = 0;
590 u16 feedth;
592 static const struct b43_loctl modifiers[] = {
593 {.i = 1,.q = 1,},
594 {.i = 1,.q = 0,},
595 {.i = 1,.q = -1,},
596 {.i = 0,.q = -1,},
597 {.i = -1,.q = -1,},
598 {.i = -1,.q = 0,},
599 {.i = -1,.q = 1,},
600 {.i = 0,.q = 1,},
603 if (d->current_state == 0) {
604 begin = 1;
605 end = 8;
606 } else if (d->current_state % 2 == 0) {
607 begin = d->current_state - 1;
608 end = d->current_state + 1;
609 } else {
610 begin = d->current_state - 2;
611 end = d->current_state + 2;
613 if (begin < 1)
614 begin += 8;
615 if (end > 8)
616 end -= 8;
618 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
619 i = begin;
620 d->current_state = i;
621 while (1) {
622 B43_WARN_ON(!(i >= 1 && i <= 8));
623 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
624 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
625 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
626 if ((test_loctl.i != prev_loctl.i ||
627 test_loctl.q != prev_loctl.q) &&
628 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
629 b43_lo_write(dev, &test_loctl);
630 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
631 gphy->pga_gain,
632 gphy->trsw_rx_gain);
633 if (feedth < d->lowest_feedth) {
634 memcpy(probe_loctl, &test_loctl,
635 sizeof(struct b43_loctl));
636 found_lower = 1;
637 d->lowest_feedth = feedth;
638 if ((d->nr_measured < 2) &&
639 !has_loopback_gain(phy))
640 break;
643 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
644 if (i == end)
645 break;
646 if (i == 8)
647 i = 1;
648 else
649 i++;
650 d->current_state = i;
653 return found_lower;
656 static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
657 struct b43_loctl *loctl,
658 int *max_rx_gain)
660 struct b43_phy *phy = &dev->phy;
661 struct b43_phy_g *gphy = phy->g;
662 struct b43_lo_g_statemachine d;
663 u16 feedth;
664 int found_lower;
665 struct b43_loctl probe_loctl;
666 int max_repeat = 1, repeat_cnt = 0;
668 d.nr_measured = 0;
669 d.state_val_multiplier = 1;
670 if (has_loopback_gain(phy))
671 d.state_val_multiplier = 3;
673 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
674 if (has_loopback_gain(phy))
675 max_repeat = 4;
676 do {
677 b43_lo_write(dev, &d.min_loctl);
678 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
679 gphy->pga_gain,
680 gphy->trsw_rx_gain);
681 if (feedth < 0x258) {
682 if (feedth >= 0x12C)
683 *max_rx_gain += 6;
684 else
685 *max_rx_gain += 3;
686 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
687 gphy->pga_gain,
688 gphy->trsw_rx_gain);
690 d.lowest_feedth = feedth;
692 d.current_state = 0;
693 do {
694 B43_WARN_ON(!
695 (d.current_state >= 0
696 && d.current_state <= 8));
697 memcpy(&probe_loctl, &d.min_loctl,
698 sizeof(struct b43_loctl));
699 found_lower =
700 lo_probe_possible_loctls(dev, &probe_loctl, &d);
701 if (!found_lower)
702 break;
703 if ((probe_loctl.i == d.min_loctl.i) &&
704 (probe_loctl.q == d.min_loctl.q))
705 break;
706 memcpy(&d.min_loctl, &probe_loctl,
707 sizeof(struct b43_loctl));
708 d.nr_measured++;
709 } while (d.nr_measured < 24);
710 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
712 if (has_loopback_gain(phy)) {
713 if (d.lowest_feedth > 0x1194)
714 *max_rx_gain -= 6;
715 else if (d.lowest_feedth < 0x5DC)
716 *max_rx_gain += 3;
717 if (repeat_cnt == 0) {
718 if (d.lowest_feedth <= 0x5DC) {
719 d.state_val_multiplier = 1;
720 repeat_cnt++;
721 } else
722 d.state_val_multiplier = 2;
723 } else if (repeat_cnt == 2)
724 d.state_val_multiplier = 1;
726 lo_measure_gain_values(dev, *max_rx_gain,
727 has_loopback_gain(phy));
728 } while (++repeat_cnt < max_repeat);
731 static
732 struct b43_lo_calib *b43_calibrate_lo_setting(struct b43_wldev *dev,
733 const struct b43_bbatt *bbatt,
734 const struct b43_rfatt *rfatt)
736 struct b43_phy *phy = &dev->phy;
737 struct b43_phy_g *gphy = phy->g;
738 struct b43_loctl loctl = {
739 .i = 0,
740 .q = 0,
742 int max_rx_gain;
743 struct b43_lo_calib *cal;
744 struct lo_g_saved_values uninitialized_var(saved_regs);
745 /* Values from the "TXCTL Register and Value Table" */
746 u16 txctl_reg;
747 u16 txctl_value;
748 u16 pad_mix_gain;
750 saved_regs.old_channel = phy->channel;
751 b43_mac_suspend(dev);
752 lo_measure_setup(dev, &saved_regs);
754 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
756 b43_radio_maskset(dev, 0x43, 0xFFF0, rfatt->att);
757 b43_radio_maskset(dev, txctl_reg, ~txctl_value, (rfatt->with_padmix ? txctl_value :0));
759 max_rx_gain = rfatt->att * 2;
760 max_rx_gain += bbatt->att / 2;
761 if (rfatt->with_padmix)
762 max_rx_gain -= pad_mix_gain;
763 if (has_loopback_gain(phy))
764 max_rx_gain += gphy->max_lb_gain;
765 lo_measure_gain_values(dev, max_rx_gain,
766 has_loopback_gain(phy));
768 b43_gphy_set_baseband_attenuation(dev, bbatt->att);
769 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
771 lo_measure_restore(dev, &saved_regs);
772 b43_mac_enable(dev);
774 if (b43_debug(dev, B43_DBG_LO)) {
775 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
776 "=> I=%d Q=%d\n",
777 bbatt->att, rfatt->att, rfatt->with_padmix,
778 loctl.i, loctl.q);
781 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
782 if (!cal) {
783 b43warn(dev->wl, "LO calib: out of memory\n");
784 return NULL;
786 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
787 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
788 memcpy(&cal->ctl, &loctl, sizeof(loctl));
789 cal->calib_time = jiffies;
790 INIT_LIST_HEAD(&cal->list);
792 return cal;
795 /* Get a calibrated LO setting for the given attenuation values.
796 * Might return a NULL pointer under OOM! */
797 static
798 struct b43_lo_calib *b43_get_calib_lo_settings(struct b43_wldev *dev,
799 const struct b43_bbatt *bbatt,
800 const struct b43_rfatt *rfatt)
802 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
803 struct b43_lo_calib *c;
805 c = b43_find_lo_calib(lo, bbatt, rfatt);
806 if (c)
807 return c;
808 /* Not in the list of calibrated LO settings.
809 * Calibrate it now. */
810 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
811 if (!c)
812 return NULL;
813 list_add(&c->list, &lo->calib_list);
815 return c;
818 void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
820 struct b43_phy *phy = &dev->phy;
821 struct b43_phy_g *gphy = phy->g;
822 struct b43_txpower_lo_control *lo = gphy->lo_control;
823 int i;
824 int rf_offset, bb_offset;
825 const struct b43_rfatt *rfatt;
826 const struct b43_bbatt *bbatt;
827 u64 power_vector;
828 bool table_changed = 0;
830 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
831 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
833 power_vector = lo->power_vector;
834 if (!update_all && !power_vector)
835 return; /* Nothing to do. */
837 /* Suspend the MAC now to avoid continuous suspend/enable
838 * cycles in the loop. */
839 b43_mac_suspend(dev);
841 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
842 struct b43_lo_calib *cal;
843 int idx;
844 u16 val;
846 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
847 continue;
848 /* Update the table entry for this power_vector bit.
849 * The table rows are RFatt entries and columns are BBatt. */
850 bb_offset = i / lo->rfatt_list.len;
851 rf_offset = i % lo->rfatt_list.len;
852 bbatt = &(lo->bbatt_list.list[bb_offset]);
853 rfatt = &(lo->rfatt_list.list[rf_offset]);
855 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
856 if (!cal) {
857 b43warn(dev->wl, "LO: Could not "
858 "calibrate DC table entry\n");
859 continue;
861 val = (u8)(cal->ctl.q);
862 val |= ((u8)(cal->ctl.i)) << 4;
863 kfree(cal);
865 /* Get the index into the hardware DC LT. */
866 idx = i / 2;
867 /* Change the table in memory. */
868 if (i % 2) {
869 /* Change the high byte. */
870 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
871 | ((val & 0x00FF) << 8);
872 } else {
873 /* Change the low byte. */
874 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
875 | (val & 0x00FF);
877 table_changed = 1;
879 if (table_changed) {
880 /* The table changed in memory. Update the hardware table. */
881 for (i = 0; i < B43_DC_LT_SIZE; i++)
882 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
884 b43_mac_enable(dev);
887 /* Fixup the RF attenuation value for the case where we are
888 * using the PAD mixer. */
889 static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
891 if (!rf->with_padmix)
892 return;
893 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
894 rf->att = 4;
897 void b43_lo_g_adjust(struct b43_wldev *dev)
899 struct b43_phy_g *gphy = dev->phy.g;
900 struct b43_lo_calib *cal;
901 struct b43_rfatt rf;
903 memcpy(&rf, &gphy->rfatt, sizeof(rf));
904 b43_lo_fixup_rfatt(&rf);
906 cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
907 if (!cal)
908 return;
909 b43_lo_write(dev, &cal->ctl);
912 void b43_lo_g_adjust_to(struct b43_wldev *dev,
913 u16 rfatt, u16 bbatt, u16 tx_control)
915 struct b43_rfatt rf;
916 struct b43_bbatt bb;
917 struct b43_lo_calib *cal;
919 memset(&rf, 0, sizeof(rf));
920 memset(&bb, 0, sizeof(bb));
921 rf.att = rfatt;
922 bb.att = bbatt;
923 b43_lo_fixup_rfatt(&rf);
924 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
925 if (!cal)
926 return;
927 b43_lo_write(dev, &cal->ctl);
930 /* Periodic LO maintanance work */
931 void b43_lo_g_maintanance_work(struct b43_wldev *dev)
933 struct b43_phy *phy = &dev->phy;
934 struct b43_phy_g *gphy = phy->g;
935 struct b43_txpower_lo_control *lo = gphy->lo_control;
936 unsigned long now;
937 unsigned long expire;
938 struct b43_lo_calib *cal, *tmp;
939 bool current_item_expired = 0;
940 bool hwpctl;
942 if (!lo)
943 return;
944 now = jiffies;
945 hwpctl = b43_has_hardware_pctl(dev);
947 if (hwpctl) {
948 /* Read the power vector and update it, if needed. */
949 expire = now - B43_LO_PWRVEC_EXPIRE;
950 if (time_before(lo->pwr_vec_read_time, expire)) {
951 lo_read_power_vector(dev);
952 b43_gphy_dc_lt_init(dev, 0);
956 if (hwpctl)
957 return;
958 /* Search for expired LO settings. Remove them.
959 * Recalibrate the current setting, if expired. */
960 expire = now - B43_LO_CALIB_EXPIRE;
961 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
962 if (!time_before(cal->calib_time, expire))
963 continue;
964 /* This item expired. */
965 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
966 b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
967 B43_WARN_ON(current_item_expired);
968 current_item_expired = 1;
970 if (b43_debug(dev, B43_DBG_LO)) {
971 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
972 "I=%d, Q=%d expired\n",
973 cal->bbatt.att, cal->rfatt.att,
974 cal->rfatt.with_padmix,
975 cal->ctl.i, cal->ctl.q);
977 list_del(&cal->list);
978 kfree(cal);
980 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
981 /* Recalibrate currently used LO setting. */
982 if (b43_debug(dev, B43_DBG_LO))
983 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
984 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
985 if (cal) {
986 list_add(&cal->list, &lo->calib_list);
987 b43_lo_write(dev, &cal->ctl);
988 } else
989 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
993 void b43_lo_g_cleanup(struct b43_wldev *dev)
995 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
996 struct b43_lo_calib *cal, *tmp;
998 if (!lo)
999 return;
1000 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1001 list_del(&cal->list);
1002 kfree(cal);
1006 /* LO Initialization */
1007 void b43_lo_g_init(struct b43_wldev *dev)
1009 if (b43_has_hardware_pctl(dev)) {
1010 lo_read_power_vector(dev);
1011 b43_gphy_dc_lt_init(dev, 1);