blob: 70c39bbbcf1fc00a677733a4c14c9d893d54052c [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
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>,
Stefano Brivio1f21ad22007-11-06 22:49:20 +01008 Copyright (c) 2005, 2006 Stefano Brivio <stefano.brivio@polimi.it>
Michael Buesche4d6b792007-09-18 15:39:42 -04009 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"
Michael Bueschef1a6282008-08-27 18:53:02 +020032#include "phy_g.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040033#include "main.h"
34
35#include <linux/delay.h>
36#include <linux/sched.h>
37
38
Michael Bueschf5eda472008-04-20 16:03:32 +020039static 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;
Michael Buesche4d6b792007-09-18 15:39:42 -040044
Michael Bueschf5eda472008-04-20 16:03:32 +020045 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}
Michael Buesche4d6b792007-09-18 15:39:42 -040055
56/* Write the LocalOscillator Control (adjust) value-pair. */
57static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
58{
59 struct b43_phy *phy = &dev->phy;
60 u16 value;
Michael Buesche4d6b792007-09-18 15:39:42 -040061
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 }
Michael Bueschf4440e82008-04-20 16:23:26 +020070 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
Michael Buesche4d6b792007-09-18 15:39:42 -040071
72 value = (u8) (control->q);
73 value |= ((u8) (control->i)) << 8;
Michael Bueschf4440e82008-04-20 16:23:26 +020074 b43_phy_write(dev, B43_PHY_LO_CTL, value);
Michael Buesche4d6b792007-09-18 15:39:42 -040075}
76
Michael Buesche4d6b792007-09-18 15:39:42 -040077static 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;
Larry Finger95de2842007-11-09 16:57:18 -0600100 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
101 && phy->rev > 6)
Michael Buesche4d6b792007-09-18 15:39:42 -0400102 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 */
140static 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
174static void lo_measure_txctl_values(struct b43_wldev *dev)
175{
176 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200177 struct b43_phy_g *gphy = phy->g;
178 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400179 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;
Michael Bueschef1a6282008-08-27 18:53:02 +0200199 lb_gain = gphy->max_lb_gain / 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400200 if (lb_gain > 10) {
201 radio_pctl_reg = 0;
202 pga = abs(10 - lb_gain) / 6;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -0700203 pga = clamp_val(pga, 0, 15);
Michael Buesche4d6b792007-09-18 15:39:42 -0400204 } 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);
Michael Bueschef1a6282008-08-27 18:53:02 +0200230 b43_gphy_set_baseband_attenuation(dev, 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400231
232 reg = lo_txctl_register_table(dev, &mask, NULL);
233 mask = ~mask;
Michael Buesch37185822009-02-20 19:30:10 +0100234 b43_radio_mask(dev, reg, mask);
Michael Buesche4d6b792007-09-18 15:39:42 -0400235
236 if (has_tx_magnification(phy)) {
237 int i, j;
238 int feedthrough;
239 int min_feedth = 0xFFFF;
240 u8 tx_magn, tx_bias;
241
242 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
243 tx_magn = tx_magn_values[i];
244 b43_radio_write16(dev, 0x52,
245 (b43_radio_read16(dev, 0x52)
246 & 0xFF0F) | tx_magn);
247 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
248 tx_bias = tx_bias_values[j];
249 b43_radio_write16(dev, 0x52,
250 (b43_radio_read16(dev, 0x52)
251 & 0xFFF0) | tx_bias);
252 feedthrough =
253 lo_measure_feedthrough(dev, 0, pga,
254 trsw_rx);
255 if (feedthrough < min_feedth) {
256 lo->tx_bias = tx_bias;
257 lo->tx_magn = tx_magn;
258 min_feedth = feedthrough;
259 }
260 if (lo->tx_bias == 0)
261 break;
262 }
263 b43_radio_write16(dev, 0x52,
264 (b43_radio_read16(dev, 0x52)
265 & 0xFF00) | lo->tx_bias | lo->
266 tx_magn);
267 }
268 } else {
269 lo->tx_magn = 0;
270 lo->tx_bias = 0;
Michael Buesch37185822009-02-20 19:30:10 +0100271 b43_radio_mask(dev, 0x52, 0xFFF0); /* TX bias == 0 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400272 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200273 lo->txctl_measured_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400274}
275
276static void lo_read_power_vector(struct b43_wldev *dev)
277{
278 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200279 struct b43_phy_g *gphy = phy->g;
280 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200281 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -0400282 u64 tmp;
283 u64 power_vector = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400284
285 for (i = 0; i < 8; i += 2) {
286 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
Michael Buesche4d6b792007-09-18 15:39:42 -0400287 power_vector |= (tmp << (i * 8));
288 /* Clear the vector on the device. */
289 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
290 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400291 if (power_vector)
292 lo->power_vector = power_vector;
Michael Bueschf5eda472008-04-20 16:03:32 +0200293 lo->pwr_vec_read_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400294}
295
296/* 802.11/LO/GPHY/MeasuringGains */
297static void lo_measure_gain_values(struct b43_wldev *dev,
298 s16 max_rx_gain, int use_trsw_rx)
299{
300 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200301 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400302 u16 tmp;
303
304 if (max_rx_gain < 0)
305 max_rx_gain = 0;
306
307 if (has_loopback_gain(phy)) {
308 int trsw_rx = 0;
309 int trsw_rx_gain;
310
311 if (use_trsw_rx) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200312 trsw_rx_gain = gphy->trsw_rx_gain / 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400313 if (max_rx_gain >= trsw_rx_gain) {
314 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
315 trsw_rx = 0x20;
316 }
317 } else
318 trsw_rx_gain = max_rx_gain;
319 if (trsw_rx_gain < 9) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200320 gphy->lna_lod_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400321 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200322 gphy->lna_lod_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400323 trsw_rx_gain -= 8;
324 }
Harvey Harrisoncdbf0842008-05-02 13:47:48 -0700325 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
Michael Bueschef1a6282008-08-27 18:53:02 +0200326 gphy->pga_gain = trsw_rx_gain / 3;
327 if (gphy->pga_gain >= 5) {
328 gphy->pga_gain -= 5;
329 gphy->lna_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400330 } else
Michael Bueschef1a6282008-08-27 18:53:02 +0200331 gphy->lna_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400332 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200333 gphy->lna_gain = 0;
334 gphy->trsw_rx_gain = 0x20;
Michael Buesche4d6b792007-09-18 15:39:42 -0400335 if (max_rx_gain >= 0x14) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200336 gphy->lna_lod_gain = 1;
337 gphy->pga_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400338 } else if (max_rx_gain >= 0x12) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200339 gphy->lna_lod_gain = 1;
340 gphy->pga_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400341 } else if (max_rx_gain >= 0xF) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200342 gphy->lna_lod_gain = 1;
343 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400344 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200345 gphy->lna_lod_gain = 0;
346 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400347 }
348 }
349
350 tmp = b43_radio_read16(dev, 0x7A);
Michael Bueschef1a6282008-08-27 18:53:02 +0200351 if (gphy->lna_lod_gain == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -0400352 tmp &= ~0x0008;
353 else
354 tmp |= 0x0008;
355 b43_radio_write16(dev, 0x7A, tmp);
356}
357
358struct lo_g_saved_values {
359 u8 old_channel;
360
361 /* Core registers */
362 u16 reg_3F4;
363 u16 reg_3E2;
364
365 /* PHY registers */
366 u16 phy_lo_mask;
367 u16 phy_extg_01;
368 u16 phy_dacctl_hwpctl;
369 u16 phy_dacctl;
Michael Buesch52507032008-01-09 18:39:09 +0100370 u16 phy_cck_14;
Michael Buesche4d6b792007-09-18 15:39:42 -0400371 u16 phy_hpwr_tssictl;
372 u16 phy_analogover;
373 u16 phy_analogoverval;
374 u16 phy_rfover;
375 u16 phy_rfoverval;
376 u16 phy_classctl;
Michael Buesch52507032008-01-09 18:39:09 +0100377 u16 phy_cck_3E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400378 u16 phy_crs0;
379 u16 phy_pgactl;
Michael Buesch52507032008-01-09 18:39:09 +0100380 u16 phy_cck_2A;
Michael Buesche4d6b792007-09-18 15:39:42 -0400381 u16 phy_syncctl;
Michael Buesch52507032008-01-09 18:39:09 +0100382 u16 phy_cck_30;
383 u16 phy_cck_06;
Michael Buesche4d6b792007-09-18 15:39:42 -0400384
385 /* Radio registers */
386 u16 radio_43;
387 u16 radio_7A;
388 u16 radio_52;
389};
390
391static void lo_measure_setup(struct b43_wldev *dev,
392 struct lo_g_saved_values *sav)
393{
394 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
395 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200396 struct b43_phy_g *gphy = phy->g;
397 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400398 u16 tmp;
399
Michael Bueschef1a6282008-08-27 18:53:02 +0200400 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400401 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
402 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
403 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100404 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
Michael Buesche4d6b792007-09-18 15:39:42 -0400405 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
406
Michael Buesche59be0b2009-02-20 19:22:36 +0100407 b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
408 b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
409 b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
410 b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
Michael Buesche4d6b792007-09-18 15:39:42 -0400411 }
412 if (phy->type == B43_PHYTYPE_B &&
413 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
Michael Buesch52507032008-01-09 18:39:09 +0100414 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
415 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
Michael Buesche4d6b792007-09-18 15:39:42 -0400416 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400417 if (phy->rev >= 2) {
418 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
419 sav->phy_analogoverval =
420 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
421 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
422 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
423 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100424 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
Michael Buesche4d6b792007-09-18 15:39:42 -0400425 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
426
Michael Bueschac1ea392009-02-20 19:25:05 +0100427 b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
428 b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
Michael Buesche59be0b2009-02-20 19:22:36 +0100429 b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
Michael Bueschac1ea392009-02-20 19:25:05 +0100430 b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
Michael Buesche4d6b792007-09-18 15:39:42 -0400431 if (phy->type == B43_PHYTYPE_G) {
432 if ((phy->rev >= 7) &&
Larry Finger95de2842007-11-09 16:57:18 -0600433 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400434 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
435 } else {
436 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
437 }
438 } else {
439 b43_phy_write(dev, B43_PHY_RFOVER, 0);
440 }
Michael Buesch52507032008-01-09 18:39:09 +0100441 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400442 }
443 sav->reg_3F4 = b43_read16(dev, 0x3F4);
444 sav->reg_3E2 = b43_read16(dev, 0x3E2);
445 sav->radio_43 = b43_radio_read16(dev, 0x43);
446 sav->radio_7A = b43_radio_read16(dev, 0x7A);
447 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
Michael Buesch52507032008-01-09 18:39:09 +0100448 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
Michael Buesche4d6b792007-09-18 15:39:42 -0400449 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
450 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
451
452 if (!has_tx_magnification(phy)) {
453 sav->radio_52 = b43_radio_read16(dev, 0x52);
454 sav->radio_52 &= 0x00F0;
455 }
456 if (phy->type == B43_PHYTYPE_B) {
Michael Buesch52507032008-01-09 18:39:09 +0100457 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
458 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
459 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
460 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
Michael Buesche4d6b792007-09-18 15:39:42 -0400461 } else {
462 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
463 | 0x8000);
464 }
465 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
466 & 0xF000);
467
468 tmp =
Michael Buesch52507032008-01-09 18:39:09 +0100469 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400470 b43_phy_write(dev, tmp, 0x007F);
471
472 tmp = sav->phy_syncctl;
473 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
474 tmp = sav->radio_7A;
475 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
476
Michael Buesch52507032008-01-09 18:39:09 +0100477 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400478 if (phy->type == B43_PHYTYPE_G ||
479 (phy->type == B43_PHYTYPE_B &&
480 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
Michael Buesch52507032008-01-09 18:39:09 +0100481 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
Michael Buesche4d6b792007-09-18 15:39:42 -0400482 } else
Michael Buesch52507032008-01-09 18:39:09 +0100483 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
Michael Buesche4d6b792007-09-18 15:39:42 -0400484 if (phy->rev >= 2)
485 b43_dummy_transmission(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200486 b43_gphy_channel_switch(dev, 6, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400487 b43_radio_read16(dev, 0x51); /* dummy read */
488 if (phy->type == B43_PHYTYPE_G)
Michael Buesch52507032008-01-09 18:39:09 +0100489 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
Michael Bueschf5eda472008-04-20 16:03:32 +0200490
491 /* Re-measure the txctl values, if needed. */
492 if (time_before(lo->txctl_measured_time,
493 jiffies - B43_LO_TXCTL_EXPIRE))
Michael Buesche4d6b792007-09-18 15:39:42 -0400494 lo_measure_txctl_values(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200495
Michael Buesche4d6b792007-09-18 15:39:42 -0400496 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
497 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
498 } else {
499 if (phy->type == B43_PHYTYPE_B)
Michael Buesch52507032008-01-09 18:39:09 +0100500 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400501 else
502 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
503 }
504}
505
506static void lo_measure_restore(struct b43_wldev *dev,
507 struct lo_g_saved_values *sav)
508{
509 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200510 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400511 u16 tmp;
512
513 if (phy->rev >= 2) {
514 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
Michael Bueschef1a6282008-08-27 18:53:02 +0200515 tmp = (gphy->pga_gain << 8);
Michael Buesche4d6b792007-09-18 15:39:42 -0400516 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
517 udelay(5);
518 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
519 udelay(2);
520 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
521 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200522 tmp = (gphy->pga_gain | 0xEFA0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400523 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
524 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400525 if (phy->type == B43_PHYTYPE_G) {
526 if (phy->rev >= 3)
Michael Buesch52507032008-01-09 18:39:09 +0100527 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400528 else
Michael Buesch52507032008-01-09 18:39:09 +0100529 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400530 if (phy->rev >= 2)
Michael Buesch52507032008-01-09 18:39:09 +0100531 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
Michael Buesche4d6b792007-09-18 15:39:42 -0400532 else
Michael Buesch52507032008-01-09 18:39:09 +0100533 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
Michael Buesche4d6b792007-09-18 15:39:42 -0400534 }
535 b43_write16(dev, 0x3F4, sav->reg_3F4);
536 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
Michael Buesch52507032008-01-09 18:39:09 +0100537 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400538 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
539 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
540 b43_radio_write16(dev, 0x43, sav->radio_43);
541 b43_radio_write16(dev, 0x7A, sav->radio_7A);
542 if (!has_tx_magnification(phy)) {
543 tmp = sav->radio_52;
544 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
545 & 0xFF0F) | tmp);
546 }
547 b43_write16(dev, 0x3E2, sav->reg_3E2);
548 if (phy->type == B43_PHYTYPE_B &&
549 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
Michael Buesch52507032008-01-09 18:39:09 +0100550 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
551 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
Michael Buesche4d6b792007-09-18 15:39:42 -0400552 }
553 if (phy->rev >= 2) {
554 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
555 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
556 sav->phy_analogoverval);
557 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
558 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
559 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
Michael Buesch52507032008-01-09 18:39:09 +0100560 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400561 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
562 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200563 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400564 tmp = (sav->phy_lo_mask & 0xBFFF);
565 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
566 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
567 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
Michael Buesch52507032008-01-09 18:39:09 +0100568 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
Michael Buesche4d6b792007-09-18 15:39:42 -0400569 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
570 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200571 b43_gphy_channel_switch(dev, sav->old_channel, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400572}
573
574struct b43_lo_g_statemachine {
575 int current_state;
576 int nr_measured;
577 int state_val_multiplier;
578 u16 lowest_feedth;
579 struct b43_loctl min_loctl;
580};
581
582/* Loop over each possible value in this state. */
583static int lo_probe_possible_loctls(struct b43_wldev *dev,
584 struct b43_loctl *probe_loctl,
585 struct b43_lo_g_statemachine *d)
586{
587 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200588 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400589 struct b43_loctl test_loctl;
590 struct b43_loctl orig_loctl;
591 struct b43_loctl prev_loctl = {
592 .i = -100,
593 .q = -100,
594 };
595 int i;
596 int begin, end;
597 int found_lower = 0;
598 u16 feedth;
599
600 static const struct b43_loctl modifiers[] = {
601 {.i = 1,.q = 1,},
602 {.i = 1,.q = 0,},
603 {.i = 1,.q = -1,},
604 {.i = 0,.q = -1,},
605 {.i = -1,.q = -1,},
606 {.i = -1,.q = 0,},
607 {.i = -1,.q = 1,},
608 {.i = 0,.q = 1,},
609 };
610
611 if (d->current_state == 0) {
612 begin = 1;
613 end = 8;
614 } else if (d->current_state % 2 == 0) {
615 begin = d->current_state - 1;
616 end = d->current_state + 1;
617 } else {
618 begin = d->current_state - 2;
619 end = d->current_state + 2;
620 }
621 if (begin < 1)
622 begin += 8;
623 if (end > 8)
624 end -= 8;
625
626 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
627 i = begin;
628 d->current_state = i;
629 while (1) {
630 B43_WARN_ON(!(i >= 1 && i <= 8));
631 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
632 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
633 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
634 if ((test_loctl.i != prev_loctl.i ||
635 test_loctl.q != prev_loctl.q) &&
636 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
637 b43_lo_write(dev, &test_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200638 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
639 gphy->pga_gain,
640 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400641 if (feedth < d->lowest_feedth) {
642 memcpy(probe_loctl, &test_loctl,
643 sizeof(struct b43_loctl));
644 found_lower = 1;
645 d->lowest_feedth = feedth;
646 if ((d->nr_measured < 2) &&
Michael Bueschf5eda472008-04-20 16:03:32 +0200647 !has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400648 break;
649 }
650 }
651 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
652 if (i == end)
653 break;
654 if (i == 8)
655 i = 1;
656 else
657 i++;
658 d->current_state = i;
659 }
660
661 return found_lower;
662}
663
664static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
665 struct b43_loctl *loctl,
666 int *max_rx_gain)
667{
668 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200669 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400670 struct b43_lo_g_statemachine d;
671 u16 feedth;
672 int found_lower;
673 struct b43_loctl probe_loctl;
674 int max_repeat = 1, repeat_cnt = 0;
675
676 d.nr_measured = 0;
677 d.state_val_multiplier = 1;
Michael Bueschf5eda472008-04-20 16:03:32 +0200678 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400679 d.state_val_multiplier = 3;
680
681 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
Michael Bueschf5eda472008-04-20 16:03:32 +0200682 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400683 max_repeat = 4;
684 do {
685 b43_lo_write(dev, &d.min_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200686 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
687 gphy->pga_gain,
688 gphy->trsw_rx_gain);
Michael Bueschf5eda472008-04-20 16:03:32 +0200689 if (feedth < 0x258) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400690 if (feedth >= 0x12C)
691 *max_rx_gain += 6;
692 else
693 *max_rx_gain += 3;
Michael Bueschef1a6282008-08-27 18:53:02 +0200694 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
695 gphy->pga_gain,
696 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400697 }
698 d.lowest_feedth = feedth;
699
700 d.current_state = 0;
701 do {
702 B43_WARN_ON(!
703 (d.current_state >= 0
704 && d.current_state <= 8));
705 memcpy(&probe_loctl, &d.min_loctl,
706 sizeof(struct b43_loctl));
707 found_lower =
708 lo_probe_possible_loctls(dev, &probe_loctl, &d);
709 if (!found_lower)
710 break;
711 if ((probe_loctl.i == d.min_loctl.i) &&
712 (probe_loctl.q == d.min_loctl.q))
713 break;
714 memcpy(&d.min_loctl, &probe_loctl,
715 sizeof(struct b43_loctl));
716 d.nr_measured++;
717 } while (d.nr_measured < 24);
718 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
719
720 if (has_loopback_gain(phy)) {
721 if (d.lowest_feedth > 0x1194)
722 *max_rx_gain -= 6;
723 else if (d.lowest_feedth < 0x5DC)
724 *max_rx_gain += 3;
725 if (repeat_cnt == 0) {
726 if (d.lowest_feedth <= 0x5DC) {
727 d.state_val_multiplier = 1;
728 repeat_cnt++;
729 } else
730 d.state_val_multiplier = 2;
731 } else if (repeat_cnt == 2)
732 d.state_val_multiplier = 1;
733 }
734 lo_measure_gain_values(dev, *max_rx_gain,
735 has_loopback_gain(phy));
736 } while (++repeat_cnt < max_repeat);
737}
738
Michael Bueschf5eda472008-04-20 16:03:32 +0200739static
740struct b43_lo_calib * b43_calibrate_lo_setting(struct b43_wldev *dev,
741 const struct b43_bbatt *bbatt,
742 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400743{
744 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200745 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400746 struct b43_loctl loctl = {
747 .i = 0,
748 .q = 0,
749 };
Michael Buesche4d6b792007-09-18 15:39:42 -0400750 int max_rx_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200751 struct b43_lo_calib *cal;
752 struct lo_g_saved_values uninitialized_var(saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400753 /* Values from the "TXCTL Register and Value Table" */
754 u16 txctl_reg;
755 u16 txctl_value;
756 u16 pad_mix_gain;
757
Michael Bueschf5eda472008-04-20 16:03:32 +0200758 saved_regs.old_channel = phy->channel;
759 b43_mac_suspend(dev);
760 lo_measure_setup(dev, &saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400761
762 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
763
Michael Bueschf5eda472008-04-20 16:03:32 +0200764 b43_radio_write16(dev, 0x43,
765 (b43_radio_read16(dev, 0x43) & 0xFFF0)
766 | rfatt->att);
767 b43_radio_write16(dev, txctl_reg,
768 (b43_radio_read16(dev, txctl_reg) & ~txctl_value)
Roel Kluinc493b012009-02-15 18:02:39 +0100769 | (rfatt->with_padmix ? txctl_value : 0));
Michael Buesche4d6b792007-09-18 15:39:42 -0400770
Michael Bueschf5eda472008-04-20 16:03:32 +0200771 max_rx_gain = rfatt->att * 2;
772 max_rx_gain += bbatt->att / 2;
773 if (rfatt->with_padmix)
774 max_rx_gain -= pad_mix_gain;
775 if (has_loopback_gain(phy))
Michael Bueschef1a6282008-08-27 18:53:02 +0200776 max_rx_gain += gphy->max_lb_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200777 lo_measure_gain_values(dev, max_rx_gain,
778 has_loopback_gain(phy));
Michael Buesche4d6b792007-09-18 15:39:42 -0400779
Michael Bueschef1a6282008-08-27 18:53:02 +0200780 b43_gphy_set_baseband_attenuation(dev, bbatt->att);
Michael Bueschf5eda472008-04-20 16:03:32 +0200781 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400782
Michael Bueschf5eda472008-04-20 16:03:32 +0200783 lo_measure_restore(dev, &saved_regs);
784 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400785
Michael Bueschf5eda472008-04-20 16:03:32 +0200786 if (b43_debug(dev, B43_DBG_LO)) {
787 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
788 "=> I=%d Q=%d\n",
789 bbatt->att, rfatt->att, rfatt->with_padmix,
790 loctl.i, loctl.q);
Michael Buesche4d6b792007-09-18 15:39:42 -0400791 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400792
Michael Bueschf5eda472008-04-20 16:03:32 +0200793 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
794 if (!cal) {
795 b43warn(dev->wl, "LO calib: out of memory\n");
796 return NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400797 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200798 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
799 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
800 memcpy(&cal->ctl, &loctl, sizeof(loctl));
801 cal->calib_time = jiffies;
802 INIT_LIST_HEAD(&cal->list);
Michael Buesche4d6b792007-09-18 15:39:42 -0400803
Michael Bueschf5eda472008-04-20 16:03:32 +0200804 return cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400805}
806
Michael Bueschf5eda472008-04-20 16:03:32 +0200807/* Get a calibrated LO setting for the given attenuation values.
808 * Might return a NULL pointer under OOM! */
809static
810struct b43_lo_calib * b43_get_calib_lo_settings(struct b43_wldev *dev,
811 const struct b43_bbatt *bbatt,
812 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400813{
Michael Bueschef1a6282008-08-27 18:53:02 +0200814 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200815 struct b43_lo_calib *c;
816
817 c = b43_find_lo_calib(lo, bbatt, rfatt);
818 if (c)
819 return c;
820 /* Not in the list of calibrated LO settings.
821 * Calibrate it now. */
822 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
823 if (!c)
824 return NULL;
825 list_add(&c->list, &lo->calib_list);
826
827 return c;
Michael Buesche4d6b792007-09-18 15:39:42 -0400828}
829
Michael Bueschf5eda472008-04-20 16:03:32 +0200830void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
Michael Buesche4d6b792007-09-18 15:39:42 -0400831{
832 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200833 struct b43_phy_g *gphy = phy->g;
834 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200835 int i;
836 int rf_offset, bb_offset;
837 const struct b43_rfatt *rfatt;
838 const struct b43_bbatt *bbatt;
839 u64 power_vector;
840 bool table_changed = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400841
Michael Bueschf5eda472008-04-20 16:03:32 +0200842 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
843 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
Michael Buesche4d6b792007-09-18 15:39:42 -0400844
Michael Bueschf5eda472008-04-20 16:03:32 +0200845 power_vector = lo->power_vector;
846 if (!update_all && !power_vector)
847 return; /* Nothing to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400848
Michael Bueschf5eda472008-04-20 16:03:32 +0200849 /* Suspend the MAC now to avoid continuous suspend/enable
850 * cycles in the loop. */
851 b43_mac_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400852
Michael Bueschf5eda472008-04-20 16:03:32 +0200853 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
854 struct b43_lo_calib *cal;
855 int idx;
856 u16 val;
Michael Buesche4d6b792007-09-18 15:39:42 -0400857
Michael Bueschf5eda472008-04-20 16:03:32 +0200858 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
859 continue;
860 /* Update the table entry for this power_vector bit.
861 * The table rows are RFatt entries and columns are BBatt. */
862 bb_offset = i / lo->rfatt_list.len;
863 rf_offset = i % lo->rfatt_list.len;
864 bbatt = &(lo->bbatt_list.list[bb_offset]);
865 rfatt = &(lo->rfatt_list.list[rf_offset]);
866
867 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
868 if (!cal) {
869 b43warn(dev->wl, "LO: Could not "
870 "calibrate DC table entry\n");
871 continue;
872 }
873 /*FIXME: Is Q really in the low nibble? */
874 val = (u8)(cal->ctl.q);
875 val |= ((u8)(cal->ctl.i)) << 4;
876 kfree(cal);
877
878 /* Get the index into the hardware DC LT. */
879 idx = i / 2;
880 /* Change the table in memory. */
881 if (i % 2) {
882 /* Change the high byte. */
883 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
884 | ((val & 0x00FF) << 8);
885 } else {
886 /* Change the low byte. */
887 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
888 | (val & 0x00FF);
889 }
890 table_changed = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400891 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200892 if (table_changed) {
893 /* The table changed in memory. Update the hardware table. */
894 for (i = 0; i < B43_DC_LT_SIZE; i++)
895 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400896 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200897 b43_mac_enable(dev);
898}
899
900/* Fixup the RF attenuation value for the case where we are
901 * using the PAD mixer. */
902static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
903{
904 if (!rf->with_padmix)
905 return;
906 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
907 rf->att = 4;
Michael Buesche4d6b792007-09-18 15:39:42 -0400908}
909
910void b43_lo_g_adjust(struct b43_wldev *dev)
911{
Michael Bueschef1a6282008-08-27 18:53:02 +0200912 struct b43_phy_g *gphy = dev->phy.g;
Michael Bueschf5eda472008-04-20 16:03:32 +0200913 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400914 struct b43_rfatt rf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400915
Michael Bueschef1a6282008-08-27 18:53:02 +0200916 memcpy(&rf, &gphy->rfatt, sizeof(rf));
Michael Bueschf5eda472008-04-20 16:03:32 +0200917 b43_lo_fixup_rfatt(&rf);
Michael Buesche4d6b792007-09-18 15:39:42 -0400918
Michael Bueschef1a6282008-08-27 18:53:02 +0200919 cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
Michael Bueschf5eda472008-04-20 16:03:32 +0200920 if (!cal)
921 return;
922 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400923}
924
925void b43_lo_g_adjust_to(struct b43_wldev *dev,
926 u16 rfatt, u16 bbatt, u16 tx_control)
927{
928 struct b43_rfatt rf;
929 struct b43_bbatt bb;
Michael Bueschf5eda472008-04-20 16:03:32 +0200930 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400931
932 memset(&rf, 0, sizeof(rf));
933 memset(&bb, 0, sizeof(bb));
934 rf.att = rfatt;
935 bb.att = bbatt;
Michael Bueschf5eda472008-04-20 16:03:32 +0200936 b43_lo_fixup_rfatt(&rf);
937 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
938 if (!cal)
939 return;
940 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400941}
942
Michael Bueschf5eda472008-04-20 16:03:32 +0200943/* Periodic LO maintanance work */
944void b43_lo_g_maintanance_work(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400945{
946 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200947 struct b43_phy_g *gphy = phy->g;
948 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200949 unsigned long now;
950 unsigned long expire;
951 struct b43_lo_calib *cal, *tmp;
952 bool current_item_expired = 0;
953 bool hwpctl;
Michael Buesche4d6b792007-09-18 15:39:42 -0400954
Michael Bueschf5eda472008-04-20 16:03:32 +0200955 if (!lo)
956 return;
957 now = jiffies;
Michael Bueschef1a6282008-08-27 18:53:02 +0200958 hwpctl = b43_has_hardware_pctl(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200959
960 if (hwpctl) {
961 /* Read the power vector and update it, if needed. */
962 expire = now - B43_LO_PWRVEC_EXPIRE;
963 if (time_before(lo->pwr_vec_read_time, expire)) {
964 lo_read_power_vector(dev);
965 b43_gphy_dc_lt_init(dev, 0);
966 }
967 //FIXME Recalc the whole DC table from time to time?
968 }
969
970 if (hwpctl)
971 return;
972 /* Search for expired LO settings. Remove them.
973 * Recalibrate the current setting, if expired. */
974 expire = now - B43_LO_CALIB_EXPIRE;
975 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
976 if (!time_before(cal->calib_time, expire))
977 continue;
978 /* This item expired. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200979 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
980 b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
Michael Bueschf5eda472008-04-20 16:03:32 +0200981 B43_WARN_ON(current_item_expired);
982 current_item_expired = 1;
983 }
984 if (b43_debug(dev, B43_DBG_LO)) {
985 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
986 "I=%d, Q=%d expired\n",
987 cal->bbatt.att, cal->rfatt.att,
988 cal->rfatt.with_padmix,
989 cal->ctl.i, cal->ctl.q);
990 }
991 list_del(&cal->list);
992 kfree(cal);
993 }
994 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
995 /* Recalibrate currently used LO setting. */
996 if (b43_debug(dev, B43_DBG_LO))
997 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
Michael Bueschef1a6282008-08-27 18:53:02 +0200998 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
Michael Bueschf5eda472008-04-20 16:03:32 +0200999 if (cal) {
1000 list_add(&cal->list, &lo->calib_list);
1001 b43_lo_write(dev, &cal->ctl);
1002 } else
1003 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
1004 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001005}
1006
Michael Bueschf5eda472008-04-20 16:03:32 +02001007void b43_lo_g_cleanup(struct b43_wldev *dev)
1008{
Michael Bueschef1a6282008-08-27 18:53:02 +02001009 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +02001010 struct b43_lo_calib *cal, *tmp;
1011
1012 if (!lo)
1013 return;
1014 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1015 list_del(&cal->list);
1016 kfree(cal);
1017 }
1018}
1019
1020/* LO Initialization */
1021void b43_lo_g_init(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001022{
Michael Bueschef1a6282008-08-27 18:53:02 +02001023 if (b43_has_hardware_pctl(dev)) {
Michael Bueschf5eda472008-04-20 16:03:32 +02001024 lo_read_power_vector(dev);
1025 b43_gphy_dc_lt_init(dev, 1);
1026 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001027}