blob: 4c82d582a524dbf512eb2ab8e2b14bba2540186e [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 Büscheb032b92011-07-04 20:50:05 +02009 Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -040010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040038
39
John Daiker99da1852009-02-24 02:16:42 -080040static struct b43_lo_calib *b43_find_lo_calib(struct b43_txpower_lo_control *lo,
41 const struct b43_bbatt *bbatt,
Michael Bueschf5eda472008-04-20 16:03:32 +020042 const struct b43_rfatt *rfatt)
43{
44 struct b43_lo_calib *c;
Michael Buesche4d6b792007-09-18 15:39:42 -040045
Michael Bueschf5eda472008-04-20 16:03:32 +020046 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;
52 }
53
54 return NULL;
55}
Michael Buesche4d6b792007-09-18 15:39:42 -040056
57/* Write the LocalOscillator Control (adjust) value-pair. */
58static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
59{
60 struct b43_phy *phy = &dev->phy;
61 u16 value;
Michael Buesche4d6b792007-09-18 15:39:42 -040062
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;
69 }
70 }
Michael Bueschf4440e82008-04-20 16:23:26 +020071 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
Michael Buesche4d6b792007-09-18 15:39:42 -040072
73 value = (u8) (control->q);
74 value |= ((u8) (control->i)) << 8;
Michael Bueschf4440e82008-04-20 16:23:26 +020075 b43_phy_write(dev, B43_PHY_LO_CTL, value);
Michael Buesche4d6b792007-09-18 15:39:42 -040076}
77
Michael Buesche4d6b792007-09-18 15:39:42 -040078static u16 lo_measure_feedthrough(struct b43_wldev *dev,
79 u16 lna, u16 pga, u16 trsw_rx)
80{
81 struct b43_phy *phy = &dev->phy;
82 u16 rfover;
83 u16 feedthrough;
84
85 if (phy->gmode) {
86 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
87 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
88
89 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
90 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
91/*FIXME This assertion fails B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
92 B43_PHY_RFOVERVAL_BW));
93*/
94 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
95
96 /* Construct the RF Override Value */
97 rfover = B43_PHY_RFOVERVAL_UNK;
98 rfover |= pga;
99 rfover |= lna;
100 rfover |= trsw_rx;
Rafał Miłecki05814832011-05-18 02:06:39 +0200101 if ((dev->dev->bus_sprom->boardflags_lo & B43_BFL_EXTLNA)
Larry Finger95de2842007-11-09 16:57:18 -0600102 && phy->rev > 6)
Michael Buesche4d6b792007-09-18 15:39:42 -0400103 rfover |= B43_PHY_RFOVERVAL_EXTLNA;
104
105 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
106 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
107 udelay(10);
108 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
109 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
110 udelay(10);
111 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
112 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
113 udelay(10);
114 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
115 } else {
116 pga |= B43_PHY_PGACTL_UNKNOWN;
117 b43_phy_write(dev, B43_PHY_PGACTL, pga);
118 udelay(10);
119 pga |= B43_PHY_PGACTL_LOWBANDW;
120 b43_phy_write(dev, B43_PHY_PGACTL, pga);
121 udelay(10);
122 pga |= B43_PHY_PGACTL_LPF;
123 b43_phy_write(dev, B43_PHY_PGACTL, pga);
124 }
125 udelay(21);
126 feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
127
128 /* This is a good place to check if we need to relax a bit,
129 * as this is the main function called regularly
130 * in the LO calibration. */
131 cond_resched();
132
133 return feedthrough;
134}
135
136/* TXCTL Register and Value Table.
137 * Returns the "TXCTL Register".
138 * "value" is the "TXCTL Value".
139 * "pad_mix_gain" is the PAD Mixer Gain.
140 */
141static u16 lo_txctl_register_table(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800142 u16 *value, u16 *pad_mix_gain)
Michael Buesche4d6b792007-09-18 15:39:42 -0400143{
144 struct b43_phy *phy = &dev->phy;
145 u16 reg, v, padmix;
146
147 if (phy->type == B43_PHYTYPE_B) {
148 v = 0x30;
149 if (phy->radio_rev <= 5) {
150 reg = 0x43;
151 padmix = 0;
152 } else {
153 reg = 0x52;
154 padmix = 5;
155 }
156 } else {
157 if (phy->rev >= 2 && phy->radio_rev == 8) {
158 reg = 0x43;
159 v = 0x10;
160 padmix = 2;
161 } else {
162 reg = 0x52;
163 v = 0x30;
164 padmix = 5;
165 }
166 }
167 if (value)
168 *value = v;
169 if (pad_mix_gain)
170 *pad_mix_gain = padmix;
171
172 return reg;
173}
174
175static void lo_measure_txctl_values(struct b43_wldev *dev)
176{
177 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200178 struct b43_phy_g *gphy = phy->g;
179 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400180 u16 reg, mask;
181 u16 trsw_rx, pga;
182 u16 radio_pctl_reg;
183
184 static const u8 tx_bias_values[] = {
185 0x09, 0x08, 0x0A, 0x01, 0x00,
186 0x02, 0x05, 0x04, 0x06,
187 };
188 static const u8 tx_magn_values[] = {
189 0x70, 0x40,
190 };
191
192 if (!has_loopback_gain(phy)) {
193 radio_pctl_reg = 6;
194 trsw_rx = 2;
195 pga = 0;
196 } else {
197 int lb_gain; /* Loopback gain (in dB) */
198
199 trsw_rx = 0;
Michael Bueschef1a6282008-08-27 18:53:02 +0200200 lb_gain = gphy->max_lb_gain / 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400201 if (lb_gain > 10) {
202 radio_pctl_reg = 0;
203 pga = abs(10 - lb_gain) / 6;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -0700204 pga = clamp_val(pga, 0, 15);
Michael Buesche4d6b792007-09-18 15:39:42 -0400205 } else {
206 int cmp_val;
207 int tmp;
208
209 pga = 0;
210 cmp_val = 0x24;
211 if ((phy->rev >= 2) &&
212 (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
213 cmp_val = 0x3C;
214 tmp = lb_gain;
215 if ((10 - lb_gain) < cmp_val)
216 tmp = (10 - lb_gain);
217 if (tmp < 0)
218 tmp += 6;
219 else
220 tmp += 3;
221 cmp_val /= 4;
222 tmp /= 4;
223 if (tmp >= cmp_val)
224 radio_pctl_reg = cmp_val;
225 else
226 radio_pctl_reg = tmp;
227 }
228 }
Michael Buesch5f9724d2009-02-20 19:31:21 +0100229 b43_radio_maskset(dev, 0x43, 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];
Michael Buesch5f9724d2009-02-20 19:31:21 +0100244 b43_radio_maskset(dev, 0x52, 0xFF0F, tx_magn);
Michael Buesche4d6b792007-09-18 15:39:42 -0400245 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
246 tx_bias = tx_bias_values[j];
Michael Buesch5f9724d2009-02-20 19:31:21 +0100247 b43_radio_maskset(dev, 0x52, 0xFFF0, tx_bias);
Michael Buesche4d6b792007-09-18 15:39:42 -0400248 feedthrough =
249 lo_measure_feedthrough(dev, 0, pga,
250 trsw_rx);
251 if (feedthrough < min_feedth) {
252 lo->tx_bias = tx_bias;
253 lo->tx_magn = tx_magn;
254 min_feedth = feedthrough;
255 }
256 if (lo->tx_bias == 0)
257 break;
258 }
259 b43_radio_write16(dev, 0x52,
260 (b43_radio_read16(dev, 0x52)
261 & 0xFF00) | lo->tx_bias | lo->
262 tx_magn);
263 }
264 } else {
265 lo->tx_magn = 0;
266 lo->tx_bias = 0;
Michael Buesch37185822009-02-20 19:30:10 +0100267 b43_radio_mask(dev, 0x52, 0xFFF0); /* TX bias == 0 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400268 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200269 lo->txctl_measured_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400270}
271
272static void lo_read_power_vector(struct b43_wldev *dev)
273{
274 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200275 struct b43_phy_g *gphy = phy->g;
276 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200277 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -0400278 u64 tmp;
279 u64 power_vector = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400280
281 for (i = 0; i < 8; i += 2) {
282 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
Michael Buesche4d6b792007-09-18 15:39:42 -0400283 power_vector |= (tmp << (i * 8));
284 /* Clear the vector on the device. */
285 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
286 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400287 if (power_vector)
288 lo->power_vector = power_vector;
Michael Bueschf5eda472008-04-20 16:03:32 +0200289 lo->pwr_vec_read_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400290}
291
292/* 802.11/LO/GPHY/MeasuringGains */
293static void lo_measure_gain_values(struct b43_wldev *dev,
294 s16 max_rx_gain, int use_trsw_rx)
295{
296 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200297 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400298 u16 tmp;
299
300 if (max_rx_gain < 0)
301 max_rx_gain = 0;
302
303 if (has_loopback_gain(phy)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400304 int trsw_rx_gain;
305
306 if (use_trsw_rx) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200307 trsw_rx_gain = gphy->trsw_rx_gain / 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400308 if (max_rx_gain >= trsw_rx_gain) {
309 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
Michael Buesche4d6b792007-09-18 15:39:42 -0400310 }
311 } else
312 trsw_rx_gain = max_rx_gain;
313 if (trsw_rx_gain < 9) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200314 gphy->lna_lod_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400315 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200316 gphy->lna_lod_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400317 trsw_rx_gain -= 8;
318 }
Harvey Harrisoncdbf0842008-05-02 13:47:48 -0700319 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
Michael Bueschef1a6282008-08-27 18:53:02 +0200320 gphy->pga_gain = trsw_rx_gain / 3;
321 if (gphy->pga_gain >= 5) {
322 gphy->pga_gain -= 5;
323 gphy->lna_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400324 } else
Michael Bueschef1a6282008-08-27 18:53:02 +0200325 gphy->lna_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400326 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200327 gphy->lna_gain = 0;
328 gphy->trsw_rx_gain = 0x20;
Michael Buesche4d6b792007-09-18 15:39:42 -0400329 if (max_rx_gain >= 0x14) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200330 gphy->lna_lod_gain = 1;
331 gphy->pga_gain = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400332 } else if (max_rx_gain >= 0x12) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200333 gphy->lna_lod_gain = 1;
334 gphy->pga_gain = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400335 } else if (max_rx_gain >= 0xF) {
Michael Bueschef1a6282008-08-27 18:53:02 +0200336 gphy->lna_lod_gain = 1;
337 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400338 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200339 gphy->lna_lod_gain = 0;
340 gphy->pga_gain = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400341 }
342 }
343
344 tmp = b43_radio_read16(dev, 0x7A);
Michael Bueschef1a6282008-08-27 18:53:02 +0200345 if (gphy->lna_lod_gain == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -0400346 tmp &= ~0x0008;
347 else
348 tmp |= 0x0008;
349 b43_radio_write16(dev, 0x7A, tmp);
350}
351
352struct lo_g_saved_values {
353 u8 old_channel;
354
355 /* Core registers */
356 u16 reg_3F4;
357 u16 reg_3E2;
358
359 /* PHY registers */
360 u16 phy_lo_mask;
361 u16 phy_extg_01;
362 u16 phy_dacctl_hwpctl;
363 u16 phy_dacctl;
Michael Buesch52507032008-01-09 18:39:09 +0100364 u16 phy_cck_14;
Michael Buesche4d6b792007-09-18 15:39:42 -0400365 u16 phy_hpwr_tssictl;
366 u16 phy_analogover;
367 u16 phy_analogoverval;
368 u16 phy_rfover;
369 u16 phy_rfoverval;
370 u16 phy_classctl;
Michael Buesch52507032008-01-09 18:39:09 +0100371 u16 phy_cck_3E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400372 u16 phy_crs0;
373 u16 phy_pgactl;
Michael Buesch52507032008-01-09 18:39:09 +0100374 u16 phy_cck_2A;
Michael Buesche4d6b792007-09-18 15:39:42 -0400375 u16 phy_syncctl;
Michael Buesch52507032008-01-09 18:39:09 +0100376 u16 phy_cck_30;
377 u16 phy_cck_06;
Michael Buesche4d6b792007-09-18 15:39:42 -0400378
379 /* Radio registers */
380 u16 radio_43;
381 u16 radio_7A;
382 u16 radio_52;
383};
384
385static void lo_measure_setup(struct b43_wldev *dev,
386 struct lo_g_saved_values *sav)
387{
Rafał Miłecki05814832011-05-18 02:06:39 +0200388 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -0400389 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200390 struct b43_phy_g *gphy = phy->g;
391 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400392 u16 tmp;
393
Michael Bueschef1a6282008-08-27 18:53:02 +0200394 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400395 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
396 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
397 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100398 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
Michael Buesche4d6b792007-09-18 15:39:42 -0400399 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
400
Michael Buesche59be0b2009-02-20 19:22:36 +0100401 b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
402 b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
403 b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
404 b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
Michael Buesche4d6b792007-09-18 15:39:42 -0400405 }
406 if (phy->type == B43_PHYTYPE_B &&
407 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
Michael Buesch52507032008-01-09 18:39:09 +0100408 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
409 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
Michael Buesche4d6b792007-09-18 15:39:42 -0400410 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400411 if (phy->rev >= 2) {
412 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
413 sav->phy_analogoverval =
414 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
415 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
416 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
417 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100418 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
Michael Buesche4d6b792007-09-18 15:39:42 -0400419 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
420
Michael Bueschac1ea392009-02-20 19:25:05 +0100421 b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
422 b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
Michael Buesche59be0b2009-02-20 19:22:36 +0100423 b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
Michael Bueschac1ea392009-02-20 19:25:05 +0100424 b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
Michael Buesche4d6b792007-09-18 15:39:42 -0400425 if (phy->type == B43_PHYTYPE_G) {
426 if ((phy->rev >= 7) &&
Larry Finger95de2842007-11-09 16:57:18 -0600427 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400428 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
429 } else {
430 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
431 }
432 } else {
433 b43_phy_write(dev, B43_PHY_RFOVER, 0);
434 }
Michael Buesch52507032008-01-09 18:39:09 +0100435 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400436 }
437 sav->reg_3F4 = b43_read16(dev, 0x3F4);
438 sav->reg_3E2 = b43_read16(dev, 0x3E2);
439 sav->radio_43 = b43_radio_read16(dev, 0x43);
440 sav->radio_7A = b43_radio_read16(dev, 0x7A);
441 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
Michael Buesch52507032008-01-09 18:39:09 +0100442 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
Michael Buesche4d6b792007-09-18 15:39:42 -0400443 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
444 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
445
446 if (!has_tx_magnification(phy)) {
447 sav->radio_52 = b43_radio_read16(dev, 0x52);
448 sav->radio_52 &= 0x00F0;
449 }
450 if (phy->type == B43_PHYTYPE_B) {
Michael Buesch52507032008-01-09 18:39:09 +0100451 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
452 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
453 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
454 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
Michael Buesche4d6b792007-09-18 15:39:42 -0400455 } else {
456 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
457 | 0x8000);
458 }
459 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
460 & 0xF000);
461
462 tmp =
Michael Buesch52507032008-01-09 18:39:09 +0100463 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400464 b43_phy_write(dev, tmp, 0x007F);
465
466 tmp = sav->phy_syncctl;
467 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
468 tmp = sav->radio_7A;
469 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
470
Michael Buesch52507032008-01-09 18:39:09 +0100471 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400472 if (phy->type == B43_PHYTYPE_G ||
473 (phy->type == B43_PHYTYPE_B &&
474 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
Michael Buesch52507032008-01-09 18:39:09 +0100475 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
Michael Buesche4d6b792007-09-18 15:39:42 -0400476 } else
Michael Buesch52507032008-01-09 18:39:09 +0100477 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
Michael Buesche4d6b792007-09-18 15:39:42 -0400478 if (phy->rev >= 2)
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200479 b43_dummy_transmission(dev, false, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200480 b43_gphy_channel_switch(dev, 6, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400481 b43_radio_read16(dev, 0x51); /* dummy read */
482 if (phy->type == B43_PHYTYPE_G)
Michael Buesch52507032008-01-09 18:39:09 +0100483 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
Michael Bueschf5eda472008-04-20 16:03:32 +0200484
485 /* Re-measure the txctl values, if needed. */
486 if (time_before(lo->txctl_measured_time,
487 jiffies - B43_LO_TXCTL_EXPIRE))
Michael Buesche4d6b792007-09-18 15:39:42 -0400488 lo_measure_txctl_values(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200489
Michael Buesche4d6b792007-09-18 15:39:42 -0400490 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
491 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
492 } else {
493 if (phy->type == B43_PHYTYPE_B)
Michael Buesch52507032008-01-09 18:39:09 +0100494 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400495 else
496 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
497 }
498}
499
500static void lo_measure_restore(struct b43_wldev *dev,
501 struct lo_g_saved_values *sav)
502{
503 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200504 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400505 u16 tmp;
506
507 if (phy->rev >= 2) {
508 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
Michael Bueschef1a6282008-08-27 18:53:02 +0200509 tmp = (gphy->pga_gain << 8);
Michael Buesche4d6b792007-09-18 15:39:42 -0400510 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
511 udelay(5);
512 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
513 udelay(2);
514 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
515 } else {
Michael Bueschef1a6282008-08-27 18:53:02 +0200516 tmp = (gphy->pga_gain | 0xEFA0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400517 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
518 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400519 if (phy->type == B43_PHYTYPE_G) {
520 if (phy->rev >= 3)
Michael Buesch52507032008-01-09 18:39:09 +0100521 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400522 else
Michael Buesch52507032008-01-09 18:39:09 +0100523 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400524 if (phy->rev >= 2)
Michael Buesch52507032008-01-09 18:39:09 +0100525 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
Michael Buesche4d6b792007-09-18 15:39:42 -0400526 else
Michael Buesch52507032008-01-09 18:39:09 +0100527 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
Michael Buesche4d6b792007-09-18 15:39:42 -0400528 }
529 b43_write16(dev, 0x3F4, sav->reg_3F4);
530 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
Michael Buesch52507032008-01-09 18:39:09 +0100531 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400532 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
533 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
534 b43_radio_write16(dev, 0x43, sav->radio_43);
535 b43_radio_write16(dev, 0x7A, sav->radio_7A);
536 if (!has_tx_magnification(phy)) {
537 tmp = sav->radio_52;
Michael Buesch5f9724d2009-02-20 19:31:21 +0100538 b43_radio_maskset(dev, 0x52, 0xFF0F, tmp);
Michael Buesche4d6b792007-09-18 15:39:42 -0400539 }
540 b43_write16(dev, 0x3E2, sav->reg_3E2);
541 if (phy->type == B43_PHYTYPE_B &&
542 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
Michael Buesch52507032008-01-09 18:39:09 +0100543 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
544 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
Michael Buesche4d6b792007-09-18 15:39:42 -0400545 }
546 if (phy->rev >= 2) {
547 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
548 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
549 sav->phy_analogoverval);
550 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
551 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
552 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
Michael Buesch52507032008-01-09 18:39:09 +0100553 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400554 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
555 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200556 if (b43_has_hardware_pctl(dev)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400557 tmp = (sav->phy_lo_mask & 0xBFFF);
558 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
559 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
560 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
Michael Buesch52507032008-01-09 18:39:09 +0100561 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
Michael Buesche4d6b792007-09-18 15:39:42 -0400562 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
563 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200564 b43_gphy_channel_switch(dev, sav->old_channel, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400565}
566
567struct b43_lo_g_statemachine {
568 int current_state;
569 int nr_measured;
570 int state_val_multiplier;
571 u16 lowest_feedth;
572 struct b43_loctl min_loctl;
573};
574
575/* Loop over each possible value in this state. */
576static int lo_probe_possible_loctls(struct b43_wldev *dev,
577 struct b43_loctl *probe_loctl,
578 struct b43_lo_g_statemachine *d)
579{
580 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200581 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400582 struct b43_loctl test_loctl;
583 struct b43_loctl orig_loctl;
584 struct b43_loctl prev_loctl = {
585 .i = -100,
586 .q = -100,
587 };
588 int i;
589 int begin, end;
590 int found_lower = 0;
591 u16 feedth;
592
593 static const struct b43_loctl modifiers[] = {
594 {.i = 1,.q = 1,},
595 {.i = 1,.q = 0,},
596 {.i = 1,.q = -1,},
597 {.i = 0,.q = -1,},
598 {.i = -1,.q = -1,},
599 {.i = -1,.q = 0,},
600 {.i = -1,.q = 1,},
601 {.i = 0,.q = 1,},
602 };
603
604 if (d->current_state == 0) {
605 begin = 1;
606 end = 8;
607 } else if (d->current_state % 2 == 0) {
608 begin = d->current_state - 1;
609 end = d->current_state + 1;
610 } else {
611 begin = d->current_state - 2;
612 end = d->current_state + 2;
613 }
614 if (begin < 1)
615 begin += 8;
616 if (end > 8)
617 end -= 8;
618
619 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
620 i = begin;
621 d->current_state = i;
622 while (1) {
623 B43_WARN_ON(!(i >= 1 && i <= 8));
624 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
625 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
626 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
627 if ((test_loctl.i != prev_loctl.i ||
628 test_loctl.q != prev_loctl.q) &&
629 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
630 b43_lo_write(dev, &test_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200631 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
632 gphy->pga_gain,
633 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400634 if (feedth < d->lowest_feedth) {
635 memcpy(probe_loctl, &test_loctl,
636 sizeof(struct b43_loctl));
637 found_lower = 1;
638 d->lowest_feedth = feedth;
639 if ((d->nr_measured < 2) &&
Michael Bueschf5eda472008-04-20 16:03:32 +0200640 !has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400641 break;
642 }
643 }
644 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
645 if (i == end)
646 break;
647 if (i == 8)
648 i = 1;
649 else
650 i++;
651 d->current_state = i;
652 }
653
654 return found_lower;
655}
656
657static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
658 struct b43_loctl *loctl,
659 int *max_rx_gain)
660{
661 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200662 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400663 struct b43_lo_g_statemachine d;
664 u16 feedth;
665 int found_lower;
666 struct b43_loctl probe_loctl;
667 int max_repeat = 1, repeat_cnt = 0;
668
669 d.nr_measured = 0;
670 d.state_val_multiplier = 1;
Michael Bueschf5eda472008-04-20 16:03:32 +0200671 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400672 d.state_val_multiplier = 3;
673
674 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
Michael Bueschf5eda472008-04-20 16:03:32 +0200675 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400676 max_repeat = 4;
677 do {
678 b43_lo_write(dev, &d.min_loctl);
Michael Bueschef1a6282008-08-27 18:53:02 +0200679 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
680 gphy->pga_gain,
681 gphy->trsw_rx_gain);
Michael Bueschf5eda472008-04-20 16:03:32 +0200682 if (feedth < 0x258) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400683 if (feedth >= 0x12C)
684 *max_rx_gain += 6;
685 else
686 *max_rx_gain += 3;
Michael Bueschef1a6282008-08-27 18:53:02 +0200687 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
688 gphy->pga_gain,
689 gphy->trsw_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400690 }
691 d.lowest_feedth = feedth;
692
693 d.current_state = 0;
694 do {
695 B43_WARN_ON(!
696 (d.current_state >= 0
697 && d.current_state <= 8));
698 memcpy(&probe_loctl, &d.min_loctl,
699 sizeof(struct b43_loctl));
700 found_lower =
701 lo_probe_possible_loctls(dev, &probe_loctl, &d);
702 if (!found_lower)
703 break;
704 if ((probe_loctl.i == d.min_loctl.i) &&
705 (probe_loctl.q == d.min_loctl.q))
706 break;
707 memcpy(&d.min_loctl, &probe_loctl,
708 sizeof(struct b43_loctl));
709 d.nr_measured++;
710 } while (d.nr_measured < 24);
711 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
712
713 if (has_loopback_gain(phy)) {
714 if (d.lowest_feedth > 0x1194)
715 *max_rx_gain -= 6;
716 else if (d.lowest_feedth < 0x5DC)
717 *max_rx_gain += 3;
718 if (repeat_cnt == 0) {
719 if (d.lowest_feedth <= 0x5DC) {
720 d.state_val_multiplier = 1;
721 repeat_cnt++;
722 } else
723 d.state_val_multiplier = 2;
724 } else if (repeat_cnt == 2)
725 d.state_val_multiplier = 1;
726 }
727 lo_measure_gain_values(dev, *max_rx_gain,
728 has_loopback_gain(phy));
729 } while (++repeat_cnt < max_repeat);
730}
731
Michael Bueschf5eda472008-04-20 16:03:32 +0200732static
John Daiker99da1852009-02-24 02:16:42 -0800733struct b43_lo_calib *b43_calibrate_lo_setting(struct b43_wldev *dev,
734 const struct b43_bbatt *bbatt,
735 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400736{
737 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200738 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400739 struct b43_loctl loctl = {
740 .i = 0,
741 .q = 0,
742 };
Michael Buesche4d6b792007-09-18 15:39:42 -0400743 int max_rx_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200744 struct b43_lo_calib *cal;
745 struct lo_g_saved_values uninitialized_var(saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400746 /* Values from the "TXCTL Register and Value Table" */
747 u16 txctl_reg;
748 u16 txctl_value;
749 u16 pad_mix_gain;
750
Michael Bueschf5eda472008-04-20 16:03:32 +0200751 saved_regs.old_channel = phy->channel;
752 b43_mac_suspend(dev);
753 lo_measure_setup(dev, &saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400754
755 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
756
Michael Buesch5f9724d2009-02-20 19:31:21 +0100757 b43_radio_maskset(dev, 0x43, 0xFFF0, rfatt->att);
758 b43_radio_maskset(dev, txctl_reg, ~txctl_value, (rfatt->with_padmix ? txctl_value :0));
Michael Buesche4d6b792007-09-18 15:39:42 -0400759
Michael Bueschf5eda472008-04-20 16:03:32 +0200760 max_rx_gain = rfatt->att * 2;
761 max_rx_gain += bbatt->att / 2;
762 if (rfatt->with_padmix)
763 max_rx_gain -= pad_mix_gain;
764 if (has_loopback_gain(phy))
Michael Bueschef1a6282008-08-27 18:53:02 +0200765 max_rx_gain += gphy->max_lb_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200766 lo_measure_gain_values(dev, max_rx_gain,
767 has_loopback_gain(phy));
Michael Buesche4d6b792007-09-18 15:39:42 -0400768
Michael Bueschef1a6282008-08-27 18:53:02 +0200769 b43_gphy_set_baseband_attenuation(dev, bbatt->att);
Michael Bueschf5eda472008-04-20 16:03:32 +0200770 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400771
Michael Bueschf5eda472008-04-20 16:03:32 +0200772 lo_measure_restore(dev, &saved_regs);
773 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400774
Michael Bueschf5eda472008-04-20 16:03:32 +0200775 if (b43_debug(dev, B43_DBG_LO)) {
776 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
777 "=> I=%d Q=%d\n",
778 bbatt->att, rfatt->att, rfatt->with_padmix,
779 loctl.i, loctl.q);
Michael Buesche4d6b792007-09-18 15:39:42 -0400780 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400781
Michael Bueschf5eda472008-04-20 16:03:32 +0200782 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
783 if (!cal) {
784 b43warn(dev->wl, "LO calib: out of memory\n");
785 return NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400786 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200787 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
788 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
789 memcpy(&cal->ctl, &loctl, sizeof(loctl));
790 cal->calib_time = jiffies;
791 INIT_LIST_HEAD(&cal->list);
Michael Buesche4d6b792007-09-18 15:39:42 -0400792
Michael Bueschf5eda472008-04-20 16:03:32 +0200793 return cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400794}
795
Michael Bueschf5eda472008-04-20 16:03:32 +0200796/* Get a calibrated LO setting for the given attenuation values.
797 * Might return a NULL pointer under OOM! */
798static
John Daiker99da1852009-02-24 02:16:42 -0800799struct b43_lo_calib *b43_get_calib_lo_settings(struct b43_wldev *dev,
800 const struct b43_bbatt *bbatt,
801 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400802{
Michael Bueschef1a6282008-08-27 18:53:02 +0200803 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200804 struct b43_lo_calib *c;
805
806 c = b43_find_lo_calib(lo, bbatt, rfatt);
807 if (c)
808 return c;
809 /* Not in the list of calibrated LO settings.
810 * Calibrate it now. */
811 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
812 if (!c)
813 return NULL;
814 list_add(&c->list, &lo->calib_list);
815
816 return c;
Michael Buesche4d6b792007-09-18 15:39:42 -0400817}
818
Michael Bueschf5eda472008-04-20 16:03:32 +0200819void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
Michael Buesche4d6b792007-09-18 15:39:42 -0400820{
821 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200822 struct b43_phy_g *gphy = phy->g;
823 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200824 int i;
825 int rf_offset, bb_offset;
826 const struct b43_rfatt *rfatt;
827 const struct b43_bbatt *bbatt;
828 u64 power_vector;
829 bool table_changed = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400830
Michael Bueschf5eda472008-04-20 16:03:32 +0200831 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
832 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
Michael Buesche4d6b792007-09-18 15:39:42 -0400833
Michael Bueschf5eda472008-04-20 16:03:32 +0200834 power_vector = lo->power_vector;
835 if (!update_all && !power_vector)
836 return; /* Nothing to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400837
Michael Bueschf5eda472008-04-20 16:03:32 +0200838 /* Suspend the MAC now to avoid continuous suspend/enable
839 * cycles in the loop. */
840 b43_mac_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400841
Michael Bueschf5eda472008-04-20 16:03:32 +0200842 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
843 struct b43_lo_calib *cal;
844 int idx;
845 u16 val;
Michael Buesche4d6b792007-09-18 15:39:42 -0400846
Michael Bueschf5eda472008-04-20 16:03:32 +0200847 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
848 continue;
849 /* Update the table entry for this power_vector bit.
850 * The table rows are RFatt entries and columns are BBatt. */
851 bb_offset = i / lo->rfatt_list.len;
852 rf_offset = i % lo->rfatt_list.len;
853 bbatt = &(lo->bbatt_list.list[bb_offset]);
854 rfatt = &(lo->rfatt_list.list[rf_offset]);
855
856 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
857 if (!cal) {
858 b43warn(dev->wl, "LO: Could not "
859 "calibrate DC table entry\n");
860 continue;
861 }
862 /*FIXME: Is Q really in the low nibble? */
863 val = (u8)(cal->ctl.q);
864 val |= ((u8)(cal->ctl.i)) << 4;
865 kfree(cal);
866
867 /* Get the index into the hardware DC LT. */
868 idx = i / 2;
869 /* Change the table in memory. */
870 if (i % 2) {
871 /* Change the high byte. */
872 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
873 | ((val & 0x00FF) << 8);
874 } else {
875 /* Change the low byte. */
876 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
877 | (val & 0x00FF);
878 }
879 table_changed = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400880 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200881 if (table_changed) {
882 /* The table changed in memory. Update the hardware table. */
883 for (i = 0; i < B43_DC_LT_SIZE; i++)
884 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400885 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200886 b43_mac_enable(dev);
887}
888
889/* Fixup the RF attenuation value for the case where we are
890 * using the PAD mixer. */
891static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
892{
893 if (!rf->with_padmix)
894 return;
895 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
896 rf->att = 4;
Michael Buesche4d6b792007-09-18 15:39:42 -0400897}
898
899void b43_lo_g_adjust(struct b43_wldev *dev)
900{
Michael Bueschef1a6282008-08-27 18:53:02 +0200901 struct b43_phy_g *gphy = dev->phy.g;
Michael Bueschf5eda472008-04-20 16:03:32 +0200902 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400903 struct b43_rfatt rf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400904
Michael Bueschef1a6282008-08-27 18:53:02 +0200905 memcpy(&rf, &gphy->rfatt, sizeof(rf));
Michael Bueschf5eda472008-04-20 16:03:32 +0200906 b43_lo_fixup_rfatt(&rf);
Michael Buesche4d6b792007-09-18 15:39:42 -0400907
Michael Bueschef1a6282008-08-27 18:53:02 +0200908 cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
Michael Bueschf5eda472008-04-20 16:03:32 +0200909 if (!cal)
910 return;
911 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400912}
913
914void b43_lo_g_adjust_to(struct b43_wldev *dev,
915 u16 rfatt, u16 bbatt, u16 tx_control)
916{
917 struct b43_rfatt rf;
918 struct b43_bbatt bb;
Michael Bueschf5eda472008-04-20 16:03:32 +0200919 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400920
921 memset(&rf, 0, sizeof(rf));
922 memset(&bb, 0, sizeof(bb));
923 rf.att = rfatt;
924 bb.att = bbatt;
Michael Bueschf5eda472008-04-20 16:03:32 +0200925 b43_lo_fixup_rfatt(&rf);
926 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
927 if (!cal)
928 return;
929 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400930}
931
Michael Bueschf5eda472008-04-20 16:03:32 +0200932/* Periodic LO maintanance work */
933void b43_lo_g_maintanance_work(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400934{
935 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200936 struct b43_phy_g *gphy = phy->g;
937 struct b43_txpower_lo_control *lo = gphy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200938 unsigned long now;
939 unsigned long expire;
940 struct b43_lo_calib *cal, *tmp;
941 bool current_item_expired = 0;
942 bool hwpctl;
Michael Buesche4d6b792007-09-18 15:39:42 -0400943
Michael Bueschf5eda472008-04-20 16:03:32 +0200944 if (!lo)
945 return;
946 now = jiffies;
Michael Bueschef1a6282008-08-27 18:53:02 +0200947 hwpctl = b43_has_hardware_pctl(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200948
949 if (hwpctl) {
950 /* Read the power vector and update it, if needed. */
951 expire = now - B43_LO_PWRVEC_EXPIRE;
952 if (time_before(lo->pwr_vec_read_time, expire)) {
953 lo_read_power_vector(dev);
954 b43_gphy_dc_lt_init(dev, 0);
955 }
956 //FIXME Recalc the whole DC table from time to time?
957 }
958
959 if (hwpctl)
960 return;
961 /* Search for expired LO settings. Remove them.
962 * Recalibrate the current setting, if expired. */
963 expire = now - B43_LO_CALIB_EXPIRE;
964 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
965 if (!time_before(cal->calib_time, expire))
966 continue;
967 /* This item expired. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200968 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
969 b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
Michael Bueschf5eda472008-04-20 16:03:32 +0200970 B43_WARN_ON(current_item_expired);
971 current_item_expired = 1;
972 }
973 if (b43_debug(dev, B43_DBG_LO)) {
974 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
975 "I=%d, Q=%d expired\n",
976 cal->bbatt.att, cal->rfatt.att,
977 cal->rfatt.with_padmix,
978 cal->ctl.i, cal->ctl.q);
979 }
980 list_del(&cal->list);
981 kfree(cal);
982 }
983 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
984 /* Recalibrate currently used LO setting. */
985 if (b43_debug(dev, B43_DBG_LO))
986 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
Michael Bueschef1a6282008-08-27 18:53:02 +0200987 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
Michael Bueschf5eda472008-04-20 16:03:32 +0200988 if (cal) {
989 list_add(&cal->list, &lo->calib_list);
990 b43_lo_write(dev, &cal->ctl);
991 } else
992 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
993 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400994}
995
Michael Bueschf5eda472008-04-20 16:03:32 +0200996void b43_lo_g_cleanup(struct b43_wldev *dev)
997{
Michael Bueschef1a6282008-08-27 18:53:02 +0200998 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200999 struct b43_lo_calib *cal, *tmp;
1000
1001 if (!lo)
1002 return;
1003 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1004 list_del(&cal->list);
1005 kfree(cal);
1006 }
1007}
1008
1009/* LO Initialization */
1010void b43_lo_g_init(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001011{
Michael Bueschef1a6282008-08-27 18:53:02 +02001012 if (b43_has_hardware_pctl(dev)) {
Michael Bueschf5eda472008-04-20 16:03:32 +02001013 lo_read_power_vector(dev);
1014 b43_gphy_dc_lt_init(dev, 1);
1015 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001016}