blob: c5e07fc2d06907f27a0947b02acb524987754478 [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;
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 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200275 lo->txctl_measured_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400276}
277
278static void lo_read_power_vector(struct b43_wldev *dev)
279{
280 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200281 struct b43_phy_g *gphy = phy->g;
282 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200283 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -0400284 u64 tmp;
285 u64 power_vector = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400286
287 for (i = 0; i < 8; i += 2) {
288 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
Michael Buesche4d6b792007-09-18 15:39:42 -0400289 power_vector |= (tmp << (i * 8));
290 /* Clear the vector on the device. */
291 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
292 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400293 if (power_vector)
294 lo->power_vector = power_vector;
Michael Bueschf5eda472008-04-20 16:03:32 +0200295 lo->pwr_vec_read_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400296}
297
298/* 802.11/LO/GPHY/MeasuringGains */
299static 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;
Michael Bueschef1a6282008-08-27 18:53:02 +0200303 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400304 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) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200314 trsw_rx_gain = gphy->trsw_rx_gain / 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400315 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) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200322 gphy->lna_lod_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400323 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200324 gphy->lna_lod_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400325 trsw_rx_gain -= 8;
326 }
Harvey Harrisoncdbf0842008-05-02 13:47:48 -0700327 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
Michael Bueschef1a6282008-08-27 18:53:02 +0200328 gphy->pga_gain = trsw_rx_gain / 3;
329 if (gphy->pga_gain >= 5) {
330 gphy->pga_gain -= 5;
331 gphy->lna_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400332 } else
Michael Bueschef1a6282008-08-27 18:53:02 +0200333 gphy->lna_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400334 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200335 gphy->lna_gain = 0;
336 gphy->trsw_rx_gain = 0x20;
Michael Buesche4d6b792007-09-18 15:39:42 -0400337 if (max_rx_gain >= 0x14) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200338 gphy->lna_lod_gain = 1;
339 gphy->pga_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400340 } else if (max_rx_gain >= 0x12) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200341 gphy->lna_lod_gain = 1;
342 gphy->pga_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400343 } else if (max_rx_gain >= 0xF) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200344 gphy->lna_lod_gain = 1;
345 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400346 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200347 gphy->lna_lod_gain = 0;
348 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400349 }
350 }
351
352 tmp = b43_radio_read16(dev, 0x7A);
Michael Bueschef1a6282008-08-27 18:53:02 +0200353 if (gphy->lna_lod_gain == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -0400354 tmp &= ~0x0008;
355 else
356 tmp |= 0x0008;
357 b43_radio_write16(dev, 0x7A, tmp);
358}
359
360struct 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;
Michael Buesch52507032008-01-09 18:39:09 +0100372 u16 phy_cck_14;
Michael Buesche4d6b792007-09-18 15:39:42 -0400373 u16 phy_hpwr_tssictl;
374 u16 phy_analogover;
375 u16 phy_analogoverval;
376 u16 phy_rfover;
377 u16 phy_rfoverval;
378 u16 phy_classctl;
Michael Buesch52507032008-01-09 18:39:09 +0100379 u16 phy_cck_3E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400380 u16 phy_crs0;
381 u16 phy_pgactl;
Michael Buesch52507032008-01-09 18:39:09 +0100382 u16 phy_cck_2A;
Michael Buesche4d6b792007-09-18 15:39:42 -0400383 u16 phy_syncctl;
Michael Buesch52507032008-01-09 18:39:09 +0100384 u16 phy_cck_30;
385 u16 phy_cck_06;
Michael Buesche4d6b792007-09-18 15:39:42 -0400386
387 /* Radio registers */
388 u16 radio_43;
389 u16 radio_7A;
390 u16 radio_52;
391};
392
393static 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;
Michael Bueschef1a6282008-08-27 18:53:02 +0200398 struct b43_phy_g *gphy = phy->g;
399 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400400 u16 tmp;
401
Michael Bueschef1a6282008-08-27 18:53:02 +0200402 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 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);
Michael Buesch52507032008-01-09 18:39:09 +0100406 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
Michael Buesche4d6b792007-09-18 15:39:42 -0400407 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
408
Michael Buesche59be0b2009-02-20 19:22:36 +0100409 b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
410 b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
411 b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
412 b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
Michael Buesche4d6b792007-09-18 15:39:42 -0400413 }
414 if (phy->type == B43_PHYTYPE_B &&
415 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
Michael Buesch52507032008-01-09 18:39:09 +0100416 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
417 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
Michael Buesche4d6b792007-09-18 15:39:42 -0400418 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400419 if (phy->rev >= 2) {
420 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
421 sav->phy_analogoverval =
422 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
423 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
424 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
425 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100426 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
Michael Buesche4d6b792007-09-18 15:39:42 -0400427 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
428
Michael Bueschac1ea392009-02-20 19:25:05 +0100429 b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
430 b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
Michael Buesche59be0b2009-02-20 19:22:36 +0100431 b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
Michael Bueschac1ea392009-02-20 19:25:05 +0100432 b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
Michael Buesche4d6b792007-09-18 15:39:42 -0400433 if (phy->type == B43_PHYTYPE_G) {
434 if ((phy->rev >= 7) &&
Larry Finger95de2842007-11-09 16:57:18 -0600435 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400436 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
437 } else {
438 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
439 }
440 } else {
441 b43_phy_write(dev, B43_PHY_RFOVER, 0);
442 }
Michael Buesch52507032008-01-09 18:39:09 +0100443 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400444 }
445 sav->reg_3F4 = b43_read16(dev, 0x3F4);
446 sav->reg_3E2 = b43_read16(dev, 0x3E2);
447 sav->radio_43 = b43_radio_read16(dev, 0x43);
448 sav->radio_7A = b43_radio_read16(dev, 0x7A);
449 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
Michael Buesch52507032008-01-09 18:39:09 +0100450 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
Michael Buesche4d6b792007-09-18 15:39:42 -0400451 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
452 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
453
454 if (!has_tx_magnification(phy)) {
455 sav->radio_52 = b43_radio_read16(dev, 0x52);
456 sav->radio_52 &= 0x00F0;
457 }
458 if (phy->type == B43_PHYTYPE_B) {
Michael Buesch52507032008-01-09 18:39:09 +0100459 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
460 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
461 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
462 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
Michael Buesche4d6b792007-09-18 15:39:42 -0400463 } else {
464 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
465 | 0x8000);
466 }
467 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
468 & 0xF000);
469
470 tmp =
Michael Buesch52507032008-01-09 18:39:09 +0100471 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400472 b43_phy_write(dev, tmp, 0x007F);
473
474 tmp = sav->phy_syncctl;
475 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
476 tmp = sav->radio_7A;
477 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
478
Michael Buesch52507032008-01-09 18:39:09 +0100479 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400480 if (phy->type == B43_PHYTYPE_G ||
481 (phy->type == B43_PHYTYPE_B &&
482 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
Michael Buesch52507032008-01-09 18:39:09 +0100483 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
Michael Buesche4d6b792007-09-18 15:39:42 -0400484 } else
Michael Buesch52507032008-01-09 18:39:09 +0100485 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
Michael Buesche4d6b792007-09-18 15:39:42 -0400486 if (phy->rev >= 2)
487 b43_dummy_transmission(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200488 b43_gphy_channel_switch(dev, 6, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400489 b43_radio_read16(dev, 0x51); /* dummy read */
490 if (phy->type == B43_PHYTYPE_G)
Michael Buesch52507032008-01-09 18:39:09 +0100491 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
Michael Bueschf5eda472008-04-20 16:03:32 +0200492
493 /* Re-measure the txctl values, if needed. */
494 if (time_before(lo->txctl_measured_time,
495 jiffies - B43_LO_TXCTL_EXPIRE))
Michael Buesche4d6b792007-09-18 15:39:42 -0400496 lo_measure_txctl_values(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200497
Michael Buesche4d6b792007-09-18 15:39:42 -0400498 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
499 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
500 } else {
501 if (phy->type == B43_PHYTYPE_B)
Michael Buesch52507032008-01-09 18:39:09 +0100502 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400503 else
504 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
505 }
506}
507
508static void lo_measure_restore(struct b43_wldev *dev,
509 struct lo_g_saved_values *sav)
510{
511 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200512 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400513 u16 tmp;
514
515 if (phy->rev >= 2) {
516 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
Michael Bueschef1a6282008-08-27 18:53:02 +0200517 tmp = (gphy->pga_gain << 8);
Michael Buesche4d6b792007-09-18 15:39:42 -0400518 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
519 udelay(5);
520 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
521 udelay(2);
522 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
523 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200524 tmp = (gphy->pga_gain | 0xEFA0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400525 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
526 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400527 if (phy->type == B43_PHYTYPE_G) {
528 if (phy->rev >= 3)
Michael Buesch52507032008-01-09 18:39:09 +0100529 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400530 else
Michael Buesch52507032008-01-09 18:39:09 +0100531 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400532 if (phy->rev >= 2)
Michael Buesch52507032008-01-09 18:39:09 +0100533 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
Michael Buesche4d6b792007-09-18 15:39:42 -0400534 else
Michael Buesch52507032008-01-09 18:39:09 +0100535 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
Michael Buesche4d6b792007-09-18 15:39:42 -0400536 }
537 b43_write16(dev, 0x3F4, sav->reg_3F4);
538 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
Michael Buesch52507032008-01-09 18:39:09 +0100539 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400540 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
541 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
542 b43_radio_write16(dev, 0x43, sav->radio_43);
543 b43_radio_write16(dev, 0x7A, sav->radio_7A);
544 if (!has_tx_magnification(phy)) {
545 tmp = sav->radio_52;
546 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
547 & 0xFF0F) | tmp);
548 }
549 b43_write16(dev, 0x3E2, sav->reg_3E2);
550 if (phy->type == B43_PHYTYPE_B &&
551 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
Michael Buesch52507032008-01-09 18:39:09 +0100552 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
553 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
Michael Buesche4d6b792007-09-18 15:39:42 -0400554 }
555 if (phy->rev >= 2) {
556 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
557 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
558 sav->phy_analogoverval);
559 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
560 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
561 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
Michael Buesch52507032008-01-09 18:39:09 +0100562 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400563 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
564 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200565 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400566 tmp = (sav->phy_lo_mask & 0xBFFF);
567 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
568 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
569 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
Michael Buesch52507032008-01-09 18:39:09 +0100570 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
Michael Buesche4d6b792007-09-18 15:39:42 -0400571 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
572 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200573 b43_gphy_channel_switch(dev, sav->old_channel, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400574}
575
576struct b43_lo_g_statemachine {
577 int current_state;
578 int nr_measured;
579 int state_val_multiplier;
580 u16 lowest_feedth;
581 struct b43_loctl min_loctl;
582};
583
584/* Loop over each possible value in this state. */
585static int lo_probe_possible_loctls(struct b43_wldev *dev,
586 struct b43_loctl *probe_loctl,
587 struct b43_lo_g_statemachine *d)
588{
589 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200590 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400591 struct b43_loctl test_loctl;
592 struct b43_loctl orig_loctl;
593 struct b43_loctl prev_loctl = {
594 .i = -100,
595 .q = -100,
596 };
597 int i;
598 int begin, end;
599 int found_lower = 0;
600 u16 feedth;
601
602 static const struct b43_loctl modifiers[] = {
603 {.i = 1,.q = 1,},
604 {.i = 1,.q = 0,},
605 {.i = 1,.q = -1,},
606 {.i = 0,.q = -1,},
607 {.i = -1,.q = -1,},
608 {.i = -1,.q = 0,},
609 {.i = -1,.q = 1,},
610 {.i = 0,.q = 1,},
611 };
612
613 if (d->current_state == 0) {
614 begin = 1;
615 end = 8;
616 } else if (d->current_state % 2 == 0) {
617 begin = d->current_state - 1;
618 end = d->current_state + 1;
619 } else {
620 begin = d->current_state - 2;
621 end = d->current_state + 2;
622 }
623 if (begin < 1)
624 begin += 8;
625 if (end > 8)
626 end -= 8;
627
628 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
629 i = begin;
630 d->current_state = i;
631 while (1) {
632 B43_WARN_ON(!(i >= 1 && i <= 8));
633 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
634 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
635 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
636 if ((test_loctl.i != prev_loctl.i ||
637 test_loctl.q != prev_loctl.q) &&
638 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
639 b43_lo_write(dev, &test_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200640 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
641 gphy->pga_gain,
642 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400643 if (feedth < d->lowest_feedth) {
644 memcpy(probe_loctl, &test_loctl,
645 sizeof(struct b43_loctl));
646 found_lower = 1;
647 d->lowest_feedth = feedth;
648 if ((d->nr_measured < 2) &&
Michael Bueschf5eda472008-04-20 16:03:32 +0200649 !has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400650 break;
651 }
652 }
653 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
654 if (i == end)
655 break;
656 if (i == 8)
657 i = 1;
658 else
659 i++;
660 d->current_state = i;
661 }
662
663 return found_lower;
664}
665
666static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
667 struct b43_loctl *loctl,
668 int *max_rx_gain)
669{
670 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200671 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400672 struct b43_lo_g_statemachine d;
673 u16 feedth;
674 int found_lower;
675 struct b43_loctl probe_loctl;
676 int max_repeat = 1, repeat_cnt = 0;
677
678 d.nr_measured = 0;
679 d.state_val_multiplier = 1;
Michael Bueschf5eda472008-04-20 16:03:32 +0200680 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400681 d.state_val_multiplier = 3;
682
683 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
Michael Bueschf5eda472008-04-20 16:03:32 +0200684 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400685 max_repeat = 4;
686 do {
687 b43_lo_write(dev, &d.min_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200688 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
689 gphy->pga_gain,
690 gphy->trsw_rx_gain);
Michael Bueschf5eda472008-04-20 16:03:32 +0200691 if (feedth < 0x258) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400692 if (feedth >= 0x12C)
693 *max_rx_gain += 6;
694 else
695 *max_rx_gain += 3;
Michael Bueschef1a6282008-08-27 18:53:02 +0200696 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
697 gphy->pga_gain,
698 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400699 }
700 d.lowest_feedth = feedth;
701
702 d.current_state = 0;
703 do {
704 B43_WARN_ON(!
705 (d.current_state >= 0
706 && d.current_state <= 8));
707 memcpy(&probe_loctl, &d.min_loctl,
708 sizeof(struct b43_loctl));
709 found_lower =
710 lo_probe_possible_loctls(dev, &probe_loctl, &d);
711 if (!found_lower)
712 break;
713 if ((probe_loctl.i == d.min_loctl.i) &&
714 (probe_loctl.q == d.min_loctl.q))
715 break;
716 memcpy(&d.min_loctl, &probe_loctl,
717 sizeof(struct b43_loctl));
718 d.nr_measured++;
719 } while (d.nr_measured < 24);
720 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
721
722 if (has_loopback_gain(phy)) {
723 if (d.lowest_feedth > 0x1194)
724 *max_rx_gain -= 6;
725 else if (d.lowest_feedth < 0x5DC)
726 *max_rx_gain += 3;
727 if (repeat_cnt == 0) {
728 if (d.lowest_feedth <= 0x5DC) {
729 d.state_val_multiplier = 1;
730 repeat_cnt++;
731 } else
732 d.state_val_multiplier = 2;
733 } else if (repeat_cnt == 2)
734 d.state_val_multiplier = 1;
735 }
736 lo_measure_gain_values(dev, *max_rx_gain,
737 has_loopback_gain(phy));
738 } while (++repeat_cnt < max_repeat);
739}
740
Michael Bueschf5eda472008-04-20 16:03:32 +0200741static
742struct b43_lo_calib * b43_calibrate_lo_setting(struct b43_wldev *dev,
743 const struct b43_bbatt *bbatt,
744 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400745{
746 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200747 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400748 struct b43_loctl loctl = {
749 .i = 0,
750 .q = 0,
751 };
Michael Buesche4d6b792007-09-18 15:39:42 -0400752 int max_rx_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200753 struct b43_lo_calib *cal;
754 struct lo_g_saved_values uninitialized_var(saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400755 /* Values from the "TXCTL Register and Value Table" */
756 u16 txctl_reg;
757 u16 txctl_value;
758 u16 pad_mix_gain;
759
Michael Bueschf5eda472008-04-20 16:03:32 +0200760 saved_regs.old_channel = phy->channel;
761 b43_mac_suspend(dev);
762 lo_measure_setup(dev, &saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400763
764 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
765
Michael Bueschf5eda472008-04-20 16:03:32 +0200766 b43_radio_write16(dev, 0x43,
767 (b43_radio_read16(dev, 0x43) & 0xFFF0)
768 | rfatt->att);
769 b43_radio_write16(dev, txctl_reg,
770 (b43_radio_read16(dev, txctl_reg) & ~txctl_value)
Roel Kluinc493b012009-02-15 18:02:39 +0100771 | (rfatt->with_padmix ? txctl_value : 0));
Michael Buesche4d6b792007-09-18 15:39:42 -0400772
Michael Bueschf5eda472008-04-20 16:03:32 +0200773 max_rx_gain = rfatt->att * 2;
774 max_rx_gain += bbatt->att / 2;
775 if (rfatt->with_padmix)
776 max_rx_gain -= pad_mix_gain;
777 if (has_loopback_gain(phy))
Michael Bueschef1a6282008-08-27 18:53:02 +0200778 max_rx_gain += gphy->max_lb_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200779 lo_measure_gain_values(dev, max_rx_gain,
780 has_loopback_gain(phy));
Michael Buesche4d6b792007-09-18 15:39:42 -0400781
Michael Bueschef1a6282008-08-27 18:53:02 +0200782 b43_gphy_set_baseband_attenuation(dev, bbatt->att);
Michael Bueschf5eda472008-04-20 16:03:32 +0200783 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400784
Michael Bueschf5eda472008-04-20 16:03:32 +0200785 lo_measure_restore(dev, &saved_regs);
786 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400787
Michael Bueschf5eda472008-04-20 16:03:32 +0200788 if (b43_debug(dev, B43_DBG_LO)) {
789 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
790 "=> I=%d Q=%d\n",
791 bbatt->att, rfatt->att, rfatt->with_padmix,
792 loctl.i, loctl.q);
Michael Buesche4d6b792007-09-18 15:39:42 -0400793 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400794
Michael Bueschf5eda472008-04-20 16:03:32 +0200795 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
796 if (!cal) {
797 b43warn(dev->wl, "LO calib: out of memory\n");
798 return NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400799 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200800 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
801 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
802 memcpy(&cal->ctl, &loctl, sizeof(loctl));
803 cal->calib_time = jiffies;
804 INIT_LIST_HEAD(&cal->list);
Michael Buesche4d6b792007-09-18 15:39:42 -0400805
Michael Bueschf5eda472008-04-20 16:03:32 +0200806 return cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400807}
808
Michael Bueschf5eda472008-04-20 16:03:32 +0200809/* Get a calibrated LO setting for the given attenuation values.
810 * Might return a NULL pointer under OOM! */
811static
812struct b43_lo_calib * b43_get_calib_lo_settings(struct b43_wldev *dev,
813 const struct b43_bbatt *bbatt,
814 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400815{
Michael Bueschef1a6282008-08-27 18:53:02 +0200816 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200817 struct b43_lo_calib *c;
818
819 c = b43_find_lo_calib(lo, bbatt, rfatt);
820 if (c)
821 return c;
822 /* Not in the list of calibrated LO settings.
823 * Calibrate it now. */
824 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
825 if (!c)
826 return NULL;
827 list_add(&c->list, &lo->calib_list);
828
829 return c;
Michael Buesche4d6b792007-09-18 15:39:42 -0400830}
831
Michael Bueschf5eda472008-04-20 16:03:32 +0200832void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
Michael Buesche4d6b792007-09-18 15:39:42 -0400833{
834 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200835 struct b43_phy_g *gphy = phy->g;
836 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200837 int i;
838 int rf_offset, bb_offset;
839 const struct b43_rfatt *rfatt;
840 const struct b43_bbatt *bbatt;
841 u64 power_vector;
842 bool table_changed = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400843
Michael Bueschf5eda472008-04-20 16:03:32 +0200844 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
845 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
Michael Buesche4d6b792007-09-18 15:39:42 -0400846
Michael Bueschf5eda472008-04-20 16:03:32 +0200847 power_vector = lo->power_vector;
848 if (!update_all && !power_vector)
849 return; /* Nothing to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400850
Michael Bueschf5eda472008-04-20 16:03:32 +0200851 /* Suspend the MAC now to avoid continuous suspend/enable
852 * cycles in the loop. */
853 b43_mac_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400854
Michael Bueschf5eda472008-04-20 16:03:32 +0200855 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
856 struct b43_lo_calib *cal;
857 int idx;
858 u16 val;
Michael Buesche4d6b792007-09-18 15:39:42 -0400859
Michael Bueschf5eda472008-04-20 16:03:32 +0200860 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
861 continue;
862 /* Update the table entry for this power_vector bit.
863 * The table rows are RFatt entries and columns are BBatt. */
864 bb_offset = i / lo->rfatt_list.len;
865 rf_offset = i % lo->rfatt_list.len;
866 bbatt = &(lo->bbatt_list.list[bb_offset]);
867 rfatt = &(lo->rfatt_list.list[rf_offset]);
868
869 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
870 if (!cal) {
871 b43warn(dev->wl, "LO: Could not "
872 "calibrate DC table entry\n");
873 continue;
874 }
875 /*FIXME: Is Q really in the low nibble? */
876 val = (u8)(cal->ctl.q);
877 val |= ((u8)(cal->ctl.i)) << 4;
878 kfree(cal);
879
880 /* Get the index into the hardware DC LT. */
881 idx = i / 2;
882 /* Change the table in memory. */
883 if (i % 2) {
884 /* Change the high byte. */
885 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
886 | ((val & 0x00FF) << 8);
887 } else {
888 /* Change the low byte. */
889 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
890 | (val & 0x00FF);
891 }
892 table_changed = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400893 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200894 if (table_changed) {
895 /* The table changed in memory. Update the hardware table. */
896 for (i = 0; i < B43_DC_LT_SIZE; i++)
897 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400898 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200899 b43_mac_enable(dev);
900}
901
902/* Fixup the RF attenuation value for the case where we are
903 * using the PAD mixer. */
904static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
905{
906 if (!rf->with_padmix)
907 return;
908 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
909 rf->att = 4;
Michael Buesche4d6b792007-09-18 15:39:42 -0400910}
911
912void b43_lo_g_adjust(struct b43_wldev *dev)
913{
Michael Bueschef1a6282008-08-27 18:53:02 +0200914 struct b43_phy_g *gphy = dev->phy.g;
Michael Bueschf5eda472008-04-20 16:03:32 +0200915 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400916 struct b43_rfatt rf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400917
Michael Bueschef1a6282008-08-27 18:53:02 +0200918 memcpy(&rf, &gphy->rfatt, sizeof(rf));
Michael Bueschf5eda472008-04-20 16:03:32 +0200919 b43_lo_fixup_rfatt(&rf);
Michael Buesche4d6b792007-09-18 15:39:42 -0400920
Michael Bueschef1a6282008-08-27 18:53:02 +0200921 cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
Michael Bueschf5eda472008-04-20 16:03:32 +0200922 if (!cal)
923 return;
924 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400925}
926
927void b43_lo_g_adjust_to(struct b43_wldev *dev,
928 u16 rfatt, u16 bbatt, u16 tx_control)
929{
930 struct b43_rfatt rf;
931 struct b43_bbatt bb;
Michael Bueschf5eda472008-04-20 16:03:32 +0200932 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400933
934 memset(&rf, 0, sizeof(rf));
935 memset(&bb, 0, sizeof(bb));
936 rf.att = rfatt;
937 bb.att = bbatt;
Michael Bueschf5eda472008-04-20 16:03:32 +0200938 b43_lo_fixup_rfatt(&rf);
939 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
940 if (!cal)
941 return;
942 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400943}
944
Michael Bueschf5eda472008-04-20 16:03:32 +0200945/* Periodic LO maintanance work */
946void b43_lo_g_maintanance_work(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400947{
948 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200949 struct b43_phy_g *gphy = phy->g;
950 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200951 unsigned long now;
952 unsigned long expire;
953 struct b43_lo_calib *cal, *tmp;
954 bool current_item_expired = 0;
955 bool hwpctl;
Michael Buesche4d6b792007-09-18 15:39:42 -0400956
Michael Bueschf5eda472008-04-20 16:03:32 +0200957 if (!lo)
958 return;
959 now = jiffies;
Michael Bueschef1a6282008-08-27 18:53:02 +0200960 hwpctl = b43_has_hardware_pctl(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200961
962 if (hwpctl) {
963 /* Read the power vector and update it, if needed. */
964 expire = now - B43_LO_PWRVEC_EXPIRE;
965 if (time_before(lo->pwr_vec_read_time, expire)) {
966 lo_read_power_vector(dev);
967 b43_gphy_dc_lt_init(dev, 0);
968 }
969 //FIXME Recalc the whole DC table from time to time?
970 }
971
972 if (hwpctl)
973 return;
974 /* Search for expired LO settings. Remove them.
975 * Recalibrate the current setting, if expired. */
976 expire = now - B43_LO_CALIB_EXPIRE;
977 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
978 if (!time_before(cal->calib_time, expire))
979 continue;
980 /* This item expired. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200981 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
982 b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
Michael Bueschf5eda472008-04-20 16:03:32 +0200983 B43_WARN_ON(current_item_expired);
984 current_item_expired = 1;
985 }
986 if (b43_debug(dev, B43_DBG_LO)) {
987 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
988 "I=%d, Q=%d expired\n",
989 cal->bbatt.att, cal->rfatt.att,
990 cal->rfatt.with_padmix,
991 cal->ctl.i, cal->ctl.q);
992 }
993 list_del(&cal->list);
994 kfree(cal);
995 }
996 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
997 /* Recalibrate currently used LO setting. */
998 if (b43_debug(dev, B43_DBG_LO))
999 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
Michael Bueschef1a6282008-08-27 18:53:02 +02001000 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
Michael Bueschf5eda472008-04-20 16:03:32 +02001001 if (cal) {
1002 list_add(&cal->list, &lo->calib_list);
1003 b43_lo_write(dev, &cal->ctl);
1004 } else
1005 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
1006 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001007}
1008
Michael Bueschf5eda472008-04-20 16:03:32 +02001009void b43_lo_g_cleanup(struct b43_wldev *dev)
1010{
Michael Bueschef1a6282008-08-27 18:53:02 +02001011 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +02001012 struct b43_lo_calib *cal, *tmp;
1013
1014 if (!lo)
1015 return;
1016 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1017 list_del(&cal->list);
1018 kfree(cal);
1019 }
1020}
1021
1022/* LO Initialization */
1023void b43_lo_g_init(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001024{
Michael Bueschef1a6282008-08-27 18:53:02 +02001025 if (b43_has_hardware_pctl(dev)) {
Michael Bueschf5eda472008-04-20 16:03:32 +02001026 lo_read_power_vector(dev);
1027 b43_gphy_dc_lt_init(dev, 1);
1028 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001029}