[PATCH] bcm43xx: OFDM fix for rev 1 cards
[linux-2.6] / drivers / net / wireless / bcm43xx / bcm43xx_phy.c
1 /*
2
3   Broadcom BCM43xx wireless driver
4
5   Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6                      Stefano Brivio <st3@riseup.net>
7                      Michael Buesch <mbuesch@freenet.de>
8                      Danny van Dyk <kugelfang@gentoo.org>
9                      Andreas Jaggi <andreas.jaggi@waterwave.ch>
10
11   Some parts of the code in this file are derived from the ipw2200
12   driver  Copyright(c) 2003 - 2004 Intel Corporation.
13
14   This program is free software; you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation; either version 2 of the License, or
17   (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; see the file COPYING.  If not, write to
26   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27   Boston, MA 02110-1301, USA.
28
29 */
30
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/types.h>
34
35 #include "bcm43xx.h"
36 #include "bcm43xx_phy.h"
37 #include "bcm43xx_main.h"
38 #include "bcm43xx_radio.h"
39 #include "bcm43xx_ilt.h"
40 #include "bcm43xx_power.h"
41
42
43 static const s8 bcm43xx_tssi2dbm_b_table[] = {
44         0x4D, 0x4C, 0x4B, 0x4A,
45         0x4A, 0x49, 0x48, 0x47,
46         0x47, 0x46, 0x45, 0x45,
47         0x44, 0x43, 0x42, 0x42,
48         0x41, 0x40, 0x3F, 0x3E,
49         0x3D, 0x3C, 0x3B, 0x3A,
50         0x39, 0x38, 0x37, 0x36,
51         0x35, 0x34, 0x32, 0x31,
52         0x30, 0x2F, 0x2D, 0x2C,
53         0x2B, 0x29, 0x28, 0x26,
54         0x25, 0x23, 0x21, 0x1F,
55         0x1D, 0x1A, 0x17, 0x14,
56         0x10, 0x0C, 0x06, 0x00,
57           -7,   -7,   -7,   -7,
58           -7,   -7,   -7,   -7,
59           -7,   -7,   -7,   -7,
60 };
61
62 static const s8 bcm43xx_tssi2dbm_g_table[] = {
63          77,  77,  77,  76,
64          76,  76,  75,  75,
65          74,  74,  73,  73,
66          73,  72,  72,  71,
67          71,  70,  70,  69,
68          68,  68,  67,  67,
69          66,  65,  65,  64,
70          63,  63,  62,  61,
71          60,  59,  58,  57,
72          56,  55,  54,  53,
73          52,  50,  49,  47,
74          45,  43,  40,  37,
75          33,  28,  22,  14,
76           5,  -7, -20, -20,
77         -20, -20, -20, -20,
78         -20, -20, -20, -20,
79 };
80
81 static void bcm43xx_phy_initg(struct bcm43xx_private *bcm);
82
83
84 static inline
85 void bcm43xx_voluntary_preempt(void)
86 {
87         assert(!in_atomic() && !in_irq() &&
88                !in_interrupt() && !irqs_disabled());
89 #ifndef CONFIG_PREEMPT
90         cond_resched();
91 #endif /* CONFIG_PREEMPT */
92 }
93
94 void bcm43xx_raw_phy_lock(struct bcm43xx_private *bcm)
95 {
96         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
97
98         assert(irqs_disabled());
99         if (bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD) == 0x00000000) {
100                 phy->is_locked = 0;
101                 return;
102         }
103         if (bcm->current_core->rev < 3) {
104                 bcm43xx_mac_suspend(bcm);
105                 spin_lock(&phy->lock);
106         } else {
107                 if (bcm->ieee->iw_mode != IW_MODE_MASTER)
108                         bcm43xx_power_saving_ctl_bits(bcm, -1, 1);
109         }
110         phy->is_locked = 1;
111 }
112
113 void bcm43xx_raw_phy_unlock(struct bcm43xx_private *bcm)
114 {
115         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
116
117         assert(irqs_disabled());
118         if (bcm->current_core->rev < 3) {
119                 if (phy->is_locked) {
120                         spin_unlock(&phy->lock);
121                         bcm43xx_mac_enable(bcm);
122                 }
123         } else {
124                 if (bcm->ieee->iw_mode != IW_MODE_MASTER)
125                         bcm43xx_power_saving_ctl_bits(bcm, -1, -1);
126         }
127         phy->is_locked = 0;
128 }
129
130 u16 bcm43xx_phy_read(struct bcm43xx_private *bcm, u16 offset)
131 {
132         bcm43xx_write16(bcm, BCM43xx_MMIO_PHY_CONTROL, offset);
133         return bcm43xx_read16(bcm, BCM43xx_MMIO_PHY_DATA);
134 }
135
136 void bcm43xx_phy_write(struct bcm43xx_private *bcm, u16 offset, u16 val)
137 {
138         bcm43xx_write16(bcm, BCM43xx_MMIO_PHY_CONTROL, offset);
139         mmiowb();
140         bcm43xx_write16(bcm, BCM43xx_MMIO_PHY_DATA, val);
141 }
142
143 void bcm43xx_phy_calibrate(struct bcm43xx_private *bcm)
144 {
145         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
146
147         bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD); /* Dummy read. */
148         if (phy->calibrated)
149                 return;
150         if (phy->type == BCM43xx_PHYTYPE_G && phy->rev == 1) {
151                 bcm43xx_wireless_core_reset(bcm, 0);
152                 bcm43xx_phy_initg(bcm);
153                 bcm43xx_wireless_core_reset(bcm, 1);
154         }
155         phy->calibrated = 1;
156 }
157
158 /* Connect the PHY 
159  * http://bcm-specs.sipsolutions.net/SetPHY
160  */
161 int bcm43xx_phy_connect(struct bcm43xx_private *bcm, int connect)
162 {
163         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
164         u32 flags;
165
166         if (bcm->current_core->rev < 5)
167                 goto out;
168
169         flags = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATEHIGH);
170         if (connect) {
171                 if (!(flags & 0x00010000))
172                         return -ENODEV;
173                 flags = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
174                 flags |= (0x800 << 18);
175                 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, flags);
176         } else {
177                 if (!(flags & 0x00020000))
178                         return -ENODEV;
179                 flags = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
180                 flags &= ~(0x800 << 18);
181                 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, flags);
182         }
183 out:
184         phy->connected = connect;
185         if (connect)
186                 dprintk(KERN_INFO PFX "PHY connected\n");
187         else
188                 dprintk(KERN_INFO PFX "PHY disconnected\n");
189
190         return 0;
191 }
192
193 /* intialize B PHY power control
194  * as described in http://bcm-specs.sipsolutions.net/InitPowerControl
195  */
196 static void bcm43xx_phy_init_pctl(struct bcm43xx_private *bcm)
197 {
198         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
199         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
200         u16 saved_batt = 0, saved_ratt = 0, saved_txctl1 = 0;
201         int must_reset_txpower = 0;
202
203         assert(phy->type != BCM43xx_PHYTYPE_A);
204         if ((bcm->board_vendor == PCI_VENDOR_ID_BROADCOM) &&
205             (bcm->board_type == 0x0416))
206                 return;
207
208         bcm43xx_phy_write(bcm, 0x0028, 0x8018);
209         bcm43xx_write16(bcm, 0x03E6, bcm43xx_read16(bcm, 0x03E6) & 0xFFDF);
210
211         if (phy->type == BCM43xx_PHYTYPE_G) {
212                 if (!phy->connected)
213                         return;
214                 bcm43xx_phy_write(bcm, 0x047A, 0xC111);
215         }
216         if (phy->savedpctlreg != 0xFFFF)
217                 return;
218
219         if (phy->type == BCM43xx_PHYTYPE_B &&
220             phy->rev >= 2 &&
221             radio->version == 0x2050) {
222                 bcm43xx_radio_write16(bcm, 0x0076,
223                                       bcm43xx_radio_read16(bcm, 0x0076) | 0x0084);
224         } else {
225                 saved_batt = radio->baseband_atten;
226                 saved_ratt = radio->radio_atten;
227                 saved_txctl1 = radio->txctl1;
228                 if ((radio->revision >= 6) && (radio->revision <= 8)
229                     && /*FIXME: incomplete specs for 5 < revision < 9 */ 0)
230                         bcm43xx_radio_set_txpower_bg(bcm, 0xB, 0x1F, 0);
231                 else
232                         bcm43xx_radio_set_txpower_bg(bcm, 0xB, 9, 0);
233                 must_reset_txpower = 1;
234         }
235         bcm43xx_dummy_transmission(bcm);
236
237         phy->savedpctlreg = bcm43xx_phy_read(bcm, BCM43xx_PHY_G_PCTL);
238
239         if (must_reset_txpower)
240                 bcm43xx_radio_set_txpower_bg(bcm, saved_batt, saved_ratt, saved_txctl1);
241         else
242                 bcm43xx_radio_write16(bcm, 0x0076, bcm43xx_radio_read16(bcm, 0x0076) & 0xFF7B);
243         bcm43xx_radio_clear_tssi(bcm);
244 }
245
246 static void bcm43xx_phy_agcsetup(struct bcm43xx_private *bcm)
247 {
248         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
249         u16 offset = 0x0000;
250
251         if (phy->rev == 1)
252                 offset = 0x4C00;
253
254         bcm43xx_ilt_write(bcm, offset, 0x00FE);
255         bcm43xx_ilt_write(bcm, offset + 1, 0x000D);
256         bcm43xx_ilt_write(bcm, offset + 2, 0x0013);
257         bcm43xx_ilt_write(bcm, offset + 3, 0x0019);
258
259         if (phy->rev == 1) {
260                 bcm43xx_ilt_write(bcm, 0x1800, 0x2710);
261                 bcm43xx_ilt_write(bcm, 0x1801, 0x9B83);
262                 bcm43xx_ilt_write(bcm, 0x1802, 0x9B83);
263                 bcm43xx_ilt_write(bcm, 0x1803, 0x0F8D);
264                 bcm43xx_phy_write(bcm, 0x0455, 0x0004);
265         }
266
267         bcm43xx_phy_write(bcm, 0x04A5, (bcm43xx_phy_read(bcm, 0x04A5) & 0x00FF) | 0x5700);
268         bcm43xx_phy_write(bcm, 0x041A, (bcm43xx_phy_read(bcm, 0x041A) & 0xFF80) | 0x000F);
269         bcm43xx_phy_write(bcm, 0x041A, (bcm43xx_phy_read(bcm, 0x041A) & 0xC07F) | 0x2B80);
270         bcm43xx_phy_write(bcm, 0x048C, (bcm43xx_phy_read(bcm, 0x048C) & 0xF0FF) | 0x0300);
271
272         bcm43xx_radio_write16(bcm, 0x007A, bcm43xx_radio_read16(bcm, 0x007A) | 0x0008);
273
274         bcm43xx_phy_write(bcm, 0x04A0, (bcm43xx_phy_read(bcm, 0x04A0) & 0xFFF0) | 0x0008);
275         bcm43xx_phy_write(bcm, 0x04A1, (bcm43xx_phy_read(bcm, 0x04A1) & 0xF0FF) | 0x0600);
276         bcm43xx_phy_write(bcm, 0x04A2, (bcm43xx_phy_read(bcm, 0x04A2) & 0xF0FF) | 0x0700);
277         bcm43xx_phy_write(bcm, 0x04A0, (bcm43xx_phy_read(bcm, 0x04A0) & 0xF0FF) | 0x0100);
278
279         if (phy->rev == 1)
280                 bcm43xx_phy_write(bcm, 0x04A2, (bcm43xx_phy_read(bcm, 0x04A2) & 0xFFF0) | 0x0007);
281
282         bcm43xx_phy_write(bcm, 0x0488, (bcm43xx_phy_read(bcm, 0x0488) & 0xFF00) | 0x001C);
283         bcm43xx_phy_write(bcm, 0x0488, (bcm43xx_phy_read(bcm, 0x0488) & 0xC0FF) | 0x0200);
284         bcm43xx_phy_write(bcm, 0x0496, (bcm43xx_phy_read(bcm, 0x0496) & 0xFF00) | 0x001C);
285         bcm43xx_phy_write(bcm, 0x0489, (bcm43xx_phy_read(bcm, 0x0489) & 0xFF00) | 0x0020);
286         bcm43xx_phy_write(bcm, 0x0489, (bcm43xx_phy_read(bcm, 0x0489) & 0xC0FF) | 0x0200);
287         bcm43xx_phy_write(bcm, 0x0482, (bcm43xx_phy_read(bcm, 0x0482) & 0xFF00) | 0x002E);
288         bcm43xx_phy_write(bcm, 0x0496, (bcm43xx_phy_read(bcm, 0x0496) & 0x00FF) | 0x1A00);
289         bcm43xx_phy_write(bcm, 0x0481, (bcm43xx_phy_read(bcm, 0x0481) & 0xFF00) | 0x0028);
290         bcm43xx_phy_write(bcm, 0x0481, (bcm43xx_phy_read(bcm, 0x0481) & 0x00FF) | 0x2C00);
291
292         if (phy->rev == 1) {
293                 bcm43xx_phy_write(bcm, 0x0430, 0x092B);
294                 bcm43xx_phy_write(bcm, 0x041B, (bcm43xx_phy_read(bcm, 0x041B) & 0xFFE1) | 0x0002);
295         } else {
296                 bcm43xx_phy_write(bcm, 0x041B, bcm43xx_phy_read(bcm, 0x041B) & 0xFFE1);
297                 bcm43xx_phy_write(bcm, 0x041F, 0x287A);
298                 bcm43xx_phy_write(bcm, 0x0420, (bcm43xx_phy_read(bcm, 0x0420) & 0xFFF0) | 0x0004);
299         }
300
301         if (phy->rev > 2) {
302                 bcm43xx_phy_write(bcm, 0x0422, 0x287A);
303                 bcm43xx_phy_write(bcm, 0x0420, (bcm43xx_phy_read(bcm, 0x0420) & 0x0FFF) | 0x3000); 
304         }
305                 
306         bcm43xx_phy_write(bcm, 0x04A8, (bcm43xx_phy_read(bcm, 0x04A8) & 0x8080) | 0x7874);
307         bcm43xx_phy_write(bcm, 0x048E, 0x1C00);
308
309         if (phy->rev == 1) {
310                 bcm43xx_phy_write(bcm, 0x04AB, (bcm43xx_phy_read(bcm, 0x04AB) & 0xF0FF) | 0x0600);
311                 bcm43xx_phy_write(bcm, 0x048B, 0x005E);
312                 bcm43xx_phy_write(bcm, 0x048C, (bcm43xx_phy_read(bcm, 0x048C) & 0xFF00) | 0x001E);
313                 bcm43xx_phy_write(bcm, 0x048D, 0x0002);
314         }
315
316         bcm43xx_ilt_write(bcm, offset + 0x0800, 0);
317         bcm43xx_ilt_write(bcm, offset + 0x0801, 7);
318         bcm43xx_ilt_write(bcm, offset + 0x0802, 16);
319         bcm43xx_ilt_write(bcm, offset + 0x0803, 28);
320
321         if (phy->rev >= 6) {
322                 bcm43xx_phy_write(bcm, 0x0426, (bcm43xx_phy_read(bcm, 0x0426)
323                                   & 0xFFFC));
324                 bcm43xx_phy_write(bcm, 0x0426, (bcm43xx_phy_read(bcm, 0x0426)
325                                   & 0xEFFF));
326         }
327 }
328
329 static void bcm43xx_phy_setupg(struct bcm43xx_private *bcm)
330 {
331         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
332         u16 i;
333
334         assert(phy->type == BCM43xx_PHYTYPE_G);
335         if (phy->rev == 1) {
336                 bcm43xx_phy_write(bcm, 0x0406, 0x4F19);
337                 bcm43xx_phy_write(bcm, BCM43xx_PHY_G_CRS,
338                                   (bcm43xx_phy_read(bcm, BCM43xx_PHY_G_CRS) & 0xFC3F) | 0x0340);
339                 bcm43xx_phy_write(bcm, 0x042C, 0x005A);
340                 bcm43xx_phy_write(bcm, 0x0427, 0x001A);
341
342                 for (i = 0; i < BCM43xx_ILT_FINEFREQG_SIZE; i++)
343                         bcm43xx_ilt_write(bcm, 0x5800 + i, bcm43xx_ilt_finefreqg[i]);
344                 for (i = 0; i < BCM43xx_ILT_NOISEG1_SIZE; i++)
345                         bcm43xx_ilt_write(bcm, 0x1800 + i, bcm43xx_ilt_noiseg1[i]);
346                 for (i = 0; i < BCM43xx_ILT_ROTOR_SIZE; i++)
347                         bcm43xx_ilt_write32(bcm, 0x2000 + i, bcm43xx_ilt_rotor[i]);
348         } else {
349                 /* nrssi values are signed 6-bit values. Not sure why we write 0x7654 here... */
350                 bcm43xx_nrssi_hw_write(bcm, 0xBA98, (s16)0x7654);
351
352                 if (phy->rev == 2) {
353                         bcm43xx_phy_write(bcm, 0x04C0, 0x1861);
354                         bcm43xx_phy_write(bcm, 0x04C1, 0x0271);
355                 } else if (phy->rev > 2) {
356                         bcm43xx_phy_write(bcm, 0x04C0, 0x0098);
357                         bcm43xx_phy_write(bcm, 0x04C1, 0x0070);
358                         bcm43xx_phy_write(bcm, 0x04C9, 0x0080);
359                 }
360                 bcm43xx_phy_write(bcm, 0x042B, bcm43xx_phy_read(bcm, 0x042B) | 0x800);
361
362                 for (i = 0; i < 64; i++)
363                         bcm43xx_ilt_write(bcm, 0x4000 + i, i);
364                 for (i = 0; i < BCM43xx_ILT_NOISEG2_SIZE; i++)
365                         bcm43xx_ilt_write(bcm, 0x1800 + i, bcm43xx_ilt_noiseg2[i]);
366         }
367         
368         if (phy->rev <= 2)
369                 for (i = 0; i < BCM43xx_ILT_NOISESCALEG_SIZE; i++)
370                         bcm43xx_ilt_write(bcm, 0x1400 + i, bcm43xx_ilt_noisescaleg1[i]);
371         else if ((phy->rev >= 7) && (bcm43xx_phy_read(bcm, 0x0449) & 0x0200))
372                 for (i = 0; i < BCM43xx_ILT_NOISESCALEG_SIZE; i++)
373                         bcm43xx_ilt_write(bcm, 0x1400 + i, bcm43xx_ilt_noisescaleg3[i]);
374         else
375                 for (i = 0; i < BCM43xx_ILT_NOISESCALEG_SIZE; i++)
376                         bcm43xx_ilt_write(bcm, 0x1400 + i, bcm43xx_ilt_noisescaleg2[i]);
377         
378         if (phy->rev == 2)
379                 for (i = 0; i < BCM43xx_ILT_SIGMASQR_SIZE; i++)
380                         bcm43xx_ilt_write(bcm, 0x5000 + i, bcm43xx_ilt_sigmasqr1[i]);
381         else if ((phy->rev > 2) && (phy->rev <= 8))
382                 for (i = 0; i < BCM43xx_ILT_SIGMASQR_SIZE; i++)
383                         bcm43xx_ilt_write(bcm, 0x5000 + i, bcm43xx_ilt_sigmasqr2[i]);
384         
385         if (phy->rev == 1) {
386                 for (i = 0; i < BCM43xx_ILT_RETARD_SIZE; i++)
387                         bcm43xx_ilt_write32(bcm, 0x2400 + i, bcm43xx_ilt_retard[i]);
388                 for (i = 0; i < 4; i++) {
389                         bcm43xx_ilt_write(bcm, 0x5404 + i, 0x0020);
390                         bcm43xx_ilt_write(bcm, 0x5408 + i, 0x0020);
391                         bcm43xx_ilt_write(bcm, 0x540C + i, 0x0020);
392                         bcm43xx_ilt_write(bcm, 0x5410 + i, 0x0020);
393                 }
394                 bcm43xx_phy_agcsetup(bcm);
395
396                 if ((bcm->board_vendor == PCI_VENDOR_ID_BROADCOM) &&
397                     (bcm->board_type == 0x0416) &&
398                     (bcm->board_revision == 0x0017))
399                         return;
400
401                 bcm43xx_ilt_write(bcm, 0x5001, 0x0002);
402                 bcm43xx_ilt_write(bcm, 0x5002, 0x0001);
403         } else {
404                 for (i = 0; i <= 0x2F; i++)
405                         bcm43xx_ilt_write(bcm, 0x1000 + i, 0x0820);
406                 bcm43xx_phy_agcsetup(bcm);
407                 bcm43xx_phy_read(bcm, 0x0400); /* dummy read */
408                 bcm43xx_phy_write(bcm, 0x0403, 0x1000);
409                 bcm43xx_ilt_write(bcm, 0x3C02, 0x000F);
410                 bcm43xx_ilt_write(bcm, 0x3C03, 0x0014);
411
412                 if ((bcm->board_vendor == PCI_VENDOR_ID_BROADCOM) &&
413                     (bcm->board_type == 0x0416) &&
414                     (bcm->board_revision == 0x0017))
415                         return;
416
417                 bcm43xx_ilt_write(bcm, 0x0401, 0x0002);
418                 bcm43xx_ilt_write(bcm, 0x0402, 0x0001);
419         }
420 }
421
422 /* Initialize the noisescaletable for APHY */
423 static void bcm43xx_phy_init_noisescaletbl(struct bcm43xx_private *bcm)
424 {
425         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
426         int i;
427
428         bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_CTRL, 0x1400);
429         for (i = 0; i < 12; i++) {
430                 if (phy->rev == 2)
431                         bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x6767);
432                 else
433                         bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x2323);
434         }
435         if (phy->rev == 2)
436                 bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x6700);
437         else
438                 bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x2300);
439         for (i = 0; i < 11; i++) {
440                 if (phy->rev == 2)
441                         bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x6767);
442                 else
443                         bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x2323);
444         }
445         if (phy->rev == 2)
446                 bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x0067);
447         else
448                 bcm43xx_phy_write(bcm, BCM43xx_PHY_ILT_A_DATA1, 0x0023);
449 }
450
451 static void bcm43xx_phy_setupa(struct bcm43xx_private *bcm)
452 {
453         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
454         u16 i;
455
456         assert(phy->type == BCM43xx_PHYTYPE_A);
457         switch (phy->rev) {
458         case 2:
459                 bcm43xx_phy_write(bcm, 0x008E, 0x3800);
460                 bcm43xx_phy_write(bcm, 0x0035, 0x03FF);
461                 bcm43xx_phy_write(bcm, 0x0036, 0x0400);
462
463                 bcm43xx_ilt_write(bcm, 0x3807, 0x0051);
464
465                 bcm43xx_phy_write(bcm, 0x001C, 0x0FF9);
466                 bcm43xx_phy_write(bcm, 0x0020, bcm43xx_phy_read(bcm, 0x0020) & 0xFF0F);
467                 bcm43xx_ilt_write(bcm, 0x3C0C, 0x07BF);
468                 bcm43xx_radio_write16(bcm, 0x0002, 0x07BF);
469
470                 bcm43xx_phy_write(bcm, 0x0024, 0x4680);
471                 bcm43xx_phy_write(bcm, 0x0020, 0x0003);
472                 bcm43xx_phy_write(bcm, 0x001D, 0x0F40);
473                 bcm43xx_phy_write(bcm, 0x001F, 0x1C00);
474
475                 bcm43xx_phy_write(bcm, 0x002A, (bcm43xx_phy_read(bcm, 0x002A) & 0x00FF) | 0x0400);
476                 bcm43xx_phy_write(bcm, 0x002B, bcm43xx_phy_read(bcm, 0x002B) & 0xFBFF);
477                 bcm43xx_phy_write(bcm, 0x008E, 0x58C1);
478
479                 bcm43xx_ilt_write(bcm, 0x0803, 0x000F);
480                 bcm43xx_ilt_write(bcm, 0x0804, 0x001F);
481                 bcm43xx_ilt_write(bcm, 0x0805, 0x002A);
482                 bcm43xx_ilt_write(bcm, 0x0805, 0x0030);
483                 bcm43xx_ilt_write(bcm, 0x0807, 0x003A);
484
485                 bcm43xx_ilt_write(bcm, 0x0000, 0x0013);
486                 bcm43xx_ilt_write(bcm, 0x0001, 0x0013);
487                 bcm43xx_ilt_write(bcm, 0x0002, 0x0013);
488                 bcm43xx_ilt_write(bcm, 0x0003, 0x0013);
489                 bcm43xx_ilt_write(bcm, 0x0004, 0x0015);
490                 bcm43xx_ilt_write(bcm, 0x0005, 0x0015);
491                 bcm43xx_ilt_write(bcm, 0x0006, 0x0019);
492
493                 bcm43xx_ilt_write(bcm, 0x0404, 0x0003);
494                 bcm43xx_ilt_write(bcm, 0x0405, 0x0003);
495                 bcm43xx_ilt_write(bcm, 0x0406, 0x0007);
496
497                 for (i = 0; i < 16; i++)
498                         bcm43xx_ilt_write(bcm, 0x4000 + i, (0x8 + i) & 0x000F);
499
500                 bcm43xx_ilt_write(bcm, 0x3003, 0x1044);
501                 bcm43xx_ilt_write(bcm, 0x3004, 0x7201);
502                 bcm43xx_ilt_write(bcm, 0x3006, 0x0040);
503                 bcm43xx_ilt_write(bcm, 0x3001, (bcm43xx_ilt_read(bcm, 0x3001) & 0x0010) | 0x0008);
504
505                 for (i = 0; i < BCM43xx_ILT_FINEFREQA_SIZE; i++)
506                         bcm43xx_ilt_write(bcm, 0x5800 + i, bcm43xx_ilt_finefreqa[i]);
507                 for (i = 0; i < BCM43xx_ILT_NOISEA2_SIZE; i++)
508                         bcm43xx_ilt_write(bcm, 0x1800 + i, bcm43xx_ilt_noisea2[i]);
509                 for (i = 0; i < BCM43xx_ILT_ROTOR_SIZE; i++)
510                         bcm43xx_ilt_write32(bcm, 0x2000 + i, bcm43xx_ilt_rotor[i]);
511                 bcm43xx_phy_init_noisescaletbl(bcm);
512                 for (i = 0; i < BCM43xx_ILT_RETARD_SIZE; i++)
513                         bcm43xx_ilt_write32(bcm, 0x2400 + i, bcm43xx_ilt_retard[i]);
514                 break;
515         case 3:
516                 for (i = 0; i < 64; i++)
517                         bcm43xx_ilt_write(bcm, 0x4000 + i, i);
518
519                 bcm43xx_ilt_write(bcm, 0x3807, 0x0051);
520
521                 bcm43xx_phy_write(bcm, 0x001C, 0x0FF9);
522                 bcm43xx_phy_write(bcm, 0x0020, bcm43xx_phy_read(bcm, 0x0020) & 0xFF0F);
523                 bcm43xx_radio_write16(bcm, 0x0002, 0x07BF);
524
525                 bcm43xx_phy_write(bcm, 0x0024, 0x4680);
526                 bcm43xx_phy_write(bcm, 0x0020, 0x0003);
527                 bcm43xx_phy_write(bcm, 0x001D, 0x0F40);
528                 bcm43xx_phy_write(bcm, 0x001F, 0x1C00);
529                 bcm43xx_phy_write(bcm, 0x002A, (bcm43xx_phy_read(bcm, 0x002A) & 0x00FF) | 0x0400);
530
531                 bcm43xx_ilt_write(bcm, 0x3001, (bcm43xx_ilt_read(bcm, 0x3001) & 0x0010) | 0x0008);
532                 for (i = 0; i < BCM43xx_ILT_NOISEA3_SIZE; i++)
533                         bcm43xx_ilt_write(bcm, 0x1800 + i, bcm43xx_ilt_noisea3[i]);
534                 bcm43xx_phy_init_noisescaletbl(bcm);
535                 for (i = 0; i < BCM43xx_ILT_SIGMASQR_SIZE; i++)
536                         bcm43xx_ilt_write(bcm, 0x5000 + i, bcm43xx_ilt_sigmasqr1[i]);
537
538                 bcm43xx_phy_write(bcm, 0x0003, 0x1808);
539
540                 bcm43xx_ilt_write(bcm, 0x0803, 0x000F);
541                 bcm43xx_ilt_write(bcm, 0x0804, 0x001F);
542                 bcm43xx_ilt_write(bcm, 0x0805, 0x002A);
543                 bcm43xx_ilt_write(bcm, 0x0805, 0x0030);
544                 bcm43xx_ilt_write(bcm, 0x0807, 0x003A);
545
546                 bcm43xx_ilt_write(bcm, 0x0000, 0x0013);
547                 bcm43xx_ilt_write(bcm, 0x0001, 0x0013);
548                 bcm43xx_ilt_write(bcm, 0x0002, 0x0013);
549                 bcm43xx_ilt_write(bcm, 0x0003, 0x0013);
550                 bcm43xx_ilt_write(bcm, 0x0004, 0x0015);
551                 bcm43xx_ilt_write(bcm, 0x0005, 0x0015);
552                 bcm43xx_ilt_write(bcm, 0x0006, 0x0019);
553
554                 bcm43xx_ilt_write(bcm, 0x0404, 0x0003);
555                 bcm43xx_ilt_write(bcm, 0x0405, 0x0003);
556                 bcm43xx_ilt_write(bcm, 0x0406, 0x0007);
557
558                 bcm43xx_ilt_write(bcm, 0x3C02, 0x000F);
559                 bcm43xx_ilt_write(bcm, 0x3C03, 0x0014);
560                 break;
561         default:
562                 assert(0);
563         }
564 }
565
566 /* Initialize APHY. This is also called for the GPHY in some cases. */
567 static void bcm43xx_phy_inita(struct bcm43xx_private *bcm)
568 {
569         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
570         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
571         u16 tval;
572
573         if (phy->type == BCM43xx_PHYTYPE_A) {
574                 bcm43xx_phy_setupa(bcm);
575         } else {
576                 bcm43xx_phy_setupg(bcm);
577                 if (bcm->sprom.boardflags & BCM43xx_BFL_PACTRL)
578                         bcm43xx_phy_write(bcm, 0x046E, 0x03CF);
579                 return;
580         }
581
582         bcm43xx_phy_write(bcm, BCM43xx_PHY_A_CRS,
583                           (bcm43xx_phy_read(bcm, BCM43xx_PHY_A_CRS) & 0xF83C) | 0x0340);
584         bcm43xx_phy_write(bcm, 0x0034, 0x0001);
585
586         TODO();//TODO: RSSI AGC
587         bcm43xx_phy_write(bcm, BCM43xx_PHY_A_CRS,
588                           bcm43xx_phy_read(bcm, BCM43xx_PHY_A_CRS) | (1 << 14));
589         bcm43xx_radio_init2060(bcm);
590
591         if ((bcm->board_vendor == PCI_VENDOR_ID_BROADCOM)
592             && ((bcm->board_type == 0x0416) || (bcm->board_type == 0x040A))) {
593                 if (radio->lofcal == 0xFFFF) {
594                         TODO();//TODO: LOF Cal
595                         bcm43xx_radio_set_tx_iq(bcm);
596                 } else
597                         bcm43xx_radio_write16(bcm, 0x001E, radio->lofcal);
598         }
599
600         bcm43xx_phy_write(bcm, 0x007A, 0xF111);
601
602         if (phy->savedpctlreg == 0xFFFF) {
603                 bcm43xx_radio_write16(bcm, 0x0019, 0x0000);
604                 bcm43xx_radio_write16(bcm, 0x0017, 0x0020);
605
606                 tval = bcm43xx_ilt_read(bcm, 0x3001);
607                 if (phy->rev == 1) {
608                         bcm43xx_ilt_write(bcm, 0x3001,
609                                           (bcm43xx_ilt_read(bcm, 0x3001) & 0xFF87)
610                                           | 0x0058);
611                 } else {
612                         bcm43xx_ilt_write(bcm, 0x3001,
613                                           (bcm43xx_ilt_read(bcm, 0x3001) & 0xFFC3)
614                                           | 0x002C);
615                 }
616                 bcm43xx_dummy_transmission(bcm);
617                 phy->savedpctlreg = bcm43xx_phy_read(bcm, BCM43xx_PHY_A_PCTL);
618                 bcm43xx_ilt_write(bcm, 0x3001, tval);
619
620                 bcm43xx_radio_set_txpower_a(bcm, 0x0018);
621         }
622         bcm43xx_radio_clear_tssi(bcm);
623 }
624
625 static void bcm43xx_phy_initb2(struct bcm43xx_private *bcm)
626 {
627         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
628         u16 offset, val;
629
630         bcm43xx_write16(bcm, 0x03EC, 0x3F22);
631         bcm43xx_phy_write(bcm, 0x0020, 0x301C);
632         bcm43xx_phy_write(bcm, 0x0026, 0x0000);
633         bcm43xx_phy_write(bcm, 0x0030, 0x00C6);
634         bcm43xx_phy_write(bcm, 0x0088, 0x3E00);
635         val = 0x3C3D;
636         for (offset = 0x0089; offset < 0x00A7; offset++) {
637                 bcm43xx_phy_write(bcm, offset, val);
638                 val -= 0x0202;
639         }
640         bcm43xx_phy_write(bcm, 0x03E4, 0x3000);
641         if (radio->channel == 0xFF)
642                 bcm43xx_radio_selectchannel(bcm, BCM43xx_RADIO_DEFAULT_CHANNEL_BG, 0);
643         else
644                 bcm43xx_radio_selectchannel(bcm, radio->channel, 0);
645         if (radio->version != 0x2050) {
646                 bcm43xx_radio_write16(bcm, 0x0075, 0x0080);
647                 bcm43xx_radio_write16(bcm, 0x0079, 0x0081);
648         }
649         bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
650         bcm43xx_radio_write16(bcm, 0x0050, 0x0023);
651         if (radio->version == 0x2050) {
652                 bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
653                 bcm43xx_radio_write16(bcm, 0x005A, 0x0070);
654                 bcm43xx_radio_write16(bcm, 0x005B, 0x007B);
655                 bcm43xx_radio_write16(bcm, 0x005C, 0x00B0);
656                 bcm43xx_radio_write16(bcm, 0x007A, 0x000F);
657                 bcm43xx_phy_write(bcm, 0x0038, 0x0677);
658                 bcm43xx_radio_init2050(bcm);
659         }
660         bcm43xx_phy_write(bcm, 0x0014, 0x0080);
661         bcm43xx_phy_write(bcm, 0x0032, 0x00CA);
662         bcm43xx_phy_write(bcm, 0x0032, 0x00CC);
663         bcm43xx_phy_write(bcm, 0x0035, 0x07C2);
664         bcm43xx_phy_lo_b_measure(bcm);
665         bcm43xx_phy_write(bcm, 0x0026, 0xCC00);
666         if (radio->version != 0x2050)
667                 bcm43xx_phy_write(bcm, 0x0026, 0xCE00);
668         bcm43xx_write16(bcm, BCM43xx_MMIO_CHANNEL_EXT, 0x1000);
669         bcm43xx_phy_write(bcm, 0x002A, 0x88A3);
670         if (radio->version != 0x2050)
671                 bcm43xx_phy_write(bcm, 0x002A, 0x88C2);
672         bcm43xx_radio_set_txpower_bg(bcm, 0xFFFF, 0xFFFF, 0xFFFF);
673         bcm43xx_phy_init_pctl(bcm);
674 }
675
676 static void bcm43xx_phy_initb4(struct bcm43xx_private *bcm)
677 {
678         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
679         u16 offset, val;
680
681         bcm43xx_write16(bcm, 0x03EC, 0x3F22);
682         bcm43xx_phy_write(bcm, 0x0020, 0x301C);
683         bcm43xx_phy_write(bcm, 0x0026, 0x0000);
684         bcm43xx_phy_write(bcm, 0x0030, 0x00C6);
685         bcm43xx_phy_write(bcm, 0x0088, 0x3E00);
686         val = 0x3C3D;
687         for (offset = 0x0089; offset < 0x00A7; offset++) {
688                 bcm43xx_phy_write(bcm, offset, val);
689                 val -= 0x0202;
690         }
691         bcm43xx_phy_write(bcm, 0x03E4, 0x3000);
692         if (radio->channel == 0xFF)
693                 bcm43xx_radio_selectchannel(bcm, BCM43xx_RADIO_DEFAULT_CHANNEL_BG, 0);
694         else
695                 bcm43xx_radio_selectchannel(bcm, radio->channel, 0);
696         if (radio->version != 0x2050) {
697                 bcm43xx_radio_write16(bcm, 0x0075, 0x0080);
698                 bcm43xx_radio_write16(bcm, 0x0079, 0x0081);
699         }
700         bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
701         bcm43xx_radio_write16(bcm, 0x0050, 0x0023);
702         if (radio->version == 0x2050) {
703                 bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
704                 bcm43xx_radio_write16(bcm, 0x005A, 0x0070);
705                 bcm43xx_radio_write16(bcm, 0x005B, 0x007B);
706                 bcm43xx_radio_write16(bcm, 0x005C, 0x00B0);
707                 bcm43xx_radio_write16(bcm, 0x007A, 0x000F);
708                 bcm43xx_phy_write(bcm, 0x0038, 0x0677);
709                 bcm43xx_radio_init2050(bcm);
710         }
711         bcm43xx_phy_write(bcm, 0x0014, 0x0080);
712         bcm43xx_phy_write(bcm, 0x0032, 0x00CA);
713         if (radio->version == 0x2050)
714                 bcm43xx_phy_write(bcm, 0x0032, 0x00E0);
715         bcm43xx_phy_write(bcm, 0x0035, 0x07C2);
716
717         bcm43xx_phy_lo_b_measure(bcm);
718
719         bcm43xx_phy_write(bcm, 0x0026, 0xCC00);
720         if (radio->version == 0x2050)
721                 bcm43xx_phy_write(bcm, 0x0026, 0xCE00);
722         bcm43xx_write16(bcm, BCM43xx_MMIO_CHANNEL_EXT, 0x1100);
723         bcm43xx_phy_write(bcm, 0x002A, 0x88A3);
724         if (radio->version == 0x2050)
725                 bcm43xx_phy_write(bcm, 0x002A, 0x88C2);
726         bcm43xx_radio_set_txpower_bg(bcm, 0xFFFF, 0xFFFF, 0xFFFF);
727         if (bcm->sprom.boardflags & BCM43xx_BFL_RSSI) {
728                 bcm43xx_calc_nrssi_slope(bcm);
729                 bcm43xx_calc_nrssi_threshold(bcm);
730         }
731         bcm43xx_phy_init_pctl(bcm);
732 }
733
734 static void bcm43xx_phy_initb5(struct bcm43xx_private *bcm)
735 {
736         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
737         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
738         u16 offset;
739         u16 value;
740         u8 old_channel;
741
742         if (phy->analog == 1)
743                 bcm43xx_radio_write16(bcm, 0x007A,
744                                       bcm43xx_radio_read16(bcm, 0x007A)
745                                       | 0x0050);
746         if ((bcm->board_vendor != PCI_VENDOR_ID_BROADCOM) &&
747             (bcm->board_type != 0x0416)) {
748                 value = 0x2120;
749                 for (offset = 0x00A8 ; offset < 0x00C7; offset++) {
750                         bcm43xx_phy_write(bcm, offset, value);
751                         value += 0x0202;
752                 }
753         }
754         bcm43xx_phy_write(bcm, 0x0035,
755                           (bcm43xx_phy_read(bcm, 0x0035) & 0xF0FF)
756                           | 0x0700);
757         if (radio->version == 0x2050)
758                 bcm43xx_phy_write(bcm, 0x0038, 0x0667);
759
760         if (phy->type == BCM43xx_PHYTYPE_G) {
761                 if (radio->version == 0x2050) {
762                         bcm43xx_radio_write16(bcm, 0x007A,
763                                               bcm43xx_radio_read16(bcm, 0x007A)
764                                               | 0x0020);
765                         bcm43xx_radio_write16(bcm, 0x0051,
766                                               bcm43xx_radio_read16(bcm, 0x0051)
767                                               | 0x0004);
768                 }
769                 bcm43xx_write16(bcm, BCM43xx_MMIO_PHY_RADIO, 0x0000);
770
771                 bcm43xx_phy_write(bcm, 0x0802, bcm43xx_phy_read(bcm, 0x0802) | 0x0100);
772                 bcm43xx_phy_write(bcm, 0x042B, bcm43xx_phy_read(bcm, 0x042B) | 0x2000);
773
774                 bcm43xx_phy_write(bcm, 0x001C, 0x186A);
775
776                 bcm43xx_phy_write(bcm, 0x0013, (bcm43xx_phy_read(bcm, 0x0013) & 0x00FF) | 0x1900);
777                 bcm43xx_phy_write(bcm, 0x0035, (bcm43xx_phy_read(bcm, 0x0035) & 0xFFC0) | 0x0064);
778                 bcm43xx_phy_write(bcm, 0x005D, (bcm43xx_phy_read(bcm, 0x005D) & 0xFF80) | 0x000A);
779         }
780
781         if (bcm->bad_frames_preempt) {
782                 bcm43xx_phy_write(bcm, BCM43xx_PHY_RADIO_BITFIELD,
783                                   bcm43xx_phy_read(bcm, BCM43xx_PHY_RADIO_BITFIELD) | (1 << 11));
784         }
785
786         if (phy->analog == 1) {
787                 bcm43xx_phy_write(bcm, 0x0026, 0xCE00);
788                 bcm43xx_phy_write(bcm, 0x0021, 0x3763);
789                 bcm43xx_phy_write(bcm, 0x0022, 0x1BC3);
790                 bcm43xx_phy_write(bcm, 0x0023, 0x06F9);
791                 bcm43xx_phy_write(bcm, 0x0024, 0x037E);
792         } else
793                 bcm43xx_phy_write(bcm, 0x0026, 0xCC00);
794         bcm43xx_phy_write(bcm, 0x0030, 0x00C6);
795         bcm43xx_write16(bcm, 0x03EC, 0x3F22);
796
797         if (phy->analog == 1)
798                 bcm43xx_phy_write(bcm, 0x0020, 0x3E1C);
799         else
800                 bcm43xx_phy_write(bcm, 0x0020, 0x301C);
801
802         if (phy->analog == 0)
803                 bcm43xx_write16(bcm, 0x03E4, 0x3000);
804
805         old_channel = radio->channel;
806         /* Force to channel 7, even if not supported. */
807         bcm43xx_radio_selectchannel(bcm, 7, 0);
808
809         if (radio->version != 0x2050) {
810                 bcm43xx_radio_write16(bcm, 0x0075, 0x0080);
811                 bcm43xx_radio_write16(bcm, 0x0079, 0x0081);
812         }
813
814         bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
815         bcm43xx_radio_write16(bcm, 0x0050, 0x0023);
816
817         if (radio->version == 0x2050) {
818                 bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
819                 bcm43xx_radio_write16(bcm, 0x005A, 0x0070);
820         }
821
822         bcm43xx_radio_write16(bcm, 0x005B, 0x007B);
823         bcm43xx_radio_write16(bcm, 0x005C, 0x00B0);
824
825         bcm43xx_radio_write16(bcm, 0x007A, bcm43xx_radio_read16(bcm, 0x007A) | 0x0007);
826
827         bcm43xx_radio_selectchannel(bcm, old_channel, 0);
828
829         bcm43xx_phy_write(bcm, 0x0014, 0x0080);
830         bcm43xx_phy_write(bcm, 0x0032, 0x00CA);
831         bcm43xx_phy_write(bcm, 0x002A, 0x88A3);
832
833         bcm43xx_radio_set_txpower_bg(bcm, 0xFFFF, 0xFFFF, 0xFFFF);
834
835         if (radio->version == 0x2050)
836                 bcm43xx_radio_write16(bcm, 0x005D, 0x000D);
837
838         bcm43xx_write16(bcm, 0x03E4, (bcm43xx_read16(bcm, 0x03E4) & 0xFFC0) | 0x0004);
839 }
840
841 static void bcm43xx_phy_initb6(struct bcm43xx_private *bcm)
842 {
843         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
844         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
845         u16 offset, val;
846         u8 old_channel;
847
848         bcm43xx_phy_write(bcm, 0x003E, 0x817A);
849         bcm43xx_radio_write16(bcm, 0x007A,
850                               (bcm43xx_radio_read16(bcm, 0x007A) | 0x0058));
851         if (radio->revision == 4 ||
852              radio->revision == 5) {
853                 bcm43xx_radio_write16(bcm, 0x0051, 0x0037);
854                 bcm43xx_radio_write16(bcm, 0x0052, 0x0070);
855                 bcm43xx_radio_write16(bcm, 0x0053, 0x00B3);
856                 bcm43xx_radio_write16(bcm, 0x0054, 0x009B);
857                 bcm43xx_radio_write16(bcm, 0x005A, 0x0088);
858                 bcm43xx_radio_write16(bcm, 0x005B, 0x0088);
859                 bcm43xx_radio_write16(bcm, 0x005D, 0x0088);
860                 bcm43xx_radio_write16(bcm, 0x005E, 0x0088);
861                 bcm43xx_radio_write16(bcm, 0x007D, 0x0088);
862         }
863         if (radio->revision == 8) {
864                 bcm43xx_radio_write16(bcm, 0x0051, 0x0000);
865                 bcm43xx_radio_write16(bcm, 0x0052, 0x0040);
866                 bcm43xx_radio_write16(bcm, 0x0053, 0x00B7);
867                 bcm43xx_radio_write16(bcm, 0x0054, 0x0098);
868                 bcm43xx_radio_write16(bcm, 0x005A, 0x0088);
869                 bcm43xx_radio_write16(bcm, 0x005B, 0x006B);
870                 bcm43xx_radio_write16(bcm, 0x005C, 0x000F);
871                 if (bcm->sprom.boardflags & 0x8000) {
872                         bcm43xx_radio_write16(bcm, 0x005D, 0x00FA);
873                         bcm43xx_radio_write16(bcm, 0x005E, 0x00D8);
874                 } else {
875                         bcm43xx_radio_write16(bcm, 0x005D, 0x00F5);
876                         bcm43xx_radio_write16(bcm, 0x005E, 0x00B8);
877                 }
878                 bcm43xx_radio_write16(bcm, 0x0073, 0x0003);
879                 bcm43xx_radio_write16(bcm, 0x007D, 0x00A8);
880                 bcm43xx_radio_write16(bcm, 0x007C, 0x0001);
881                 bcm43xx_radio_write16(bcm, 0x007E, 0x0008);
882         }
883         val = 0x1E1F;
884         for (offset = 0x0088; offset < 0x0098; offset++) {
885                 bcm43xx_phy_write(bcm, offset, val);
886                 val -= 0x0202;
887         }
888         val = 0x3E3F;
889         for (offset = 0x0098; offset < 0x00A8; offset++) {
890                 bcm43xx_phy_write(bcm, offset, val);
891                 val -= 0x0202;
892         }
893         val = 0x2120;
894         for (offset = 0x00A8; offset < 0x00C8; offset++) {
895                 bcm43xx_phy_write(bcm, offset, (val & 0x3F3F));
896                 val += 0x0202;
897         }
898         if (phy->type == BCM43xx_PHYTYPE_G) {
899                 bcm43xx_radio_write16(bcm, 0x007A,
900                                       bcm43xx_radio_read16(bcm, 0x007A) | 0x0020);
901                 bcm43xx_radio_write16(bcm, 0x0051,
902                                       bcm43xx_radio_read16(bcm, 0x0051) | 0x0004);
903                 bcm43xx_phy_write(bcm, 0x0802,
904                                   bcm43xx_phy_read(bcm, 0x0802) | 0x0100);
905                 bcm43xx_phy_write(bcm, 0x042B,
906                                   bcm43xx_phy_read(bcm, 0x042B) | 0x2000);
907                 bcm43xx_phy_write(bcm, 0x5B, 0x0000);
908                 bcm43xx_phy_write(bcm, 0x5C, 0x0000);
909         }
910
911         old_channel = radio->channel;
912         if (old_channel >= 8)
913                 bcm43xx_radio_selectchannel(bcm, 1, 0);
914         else
915                 bcm43xx_radio_selectchannel(bcm, 13, 0);
916
917         bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
918         bcm43xx_radio_write16(bcm, 0x0050, 0x0023);
919         udelay(40);
920         if (radio->revision < 6 || radio-> revision == 8) {
921                 bcm43xx_radio_write16(bcm, 0x007C, (bcm43xx_radio_read16(bcm, 0x007C)
922                                       | 0x0002));
923                 bcm43xx_radio_write16(bcm, 0x0050, 0x0020);
924         }
925         if (radio->revision <= 2) {
926                 bcm43xx_radio_write16(bcm, 0x007C, 0x0020);
927                 bcm43xx_radio_write16(bcm, 0x005A, 0x0070);
928                 bcm43xx_radio_write16(bcm, 0x005B, 0x007B);
929                 bcm43xx_radio_write16(bcm, 0x005C, 0x00B0);
930         }
931         bcm43xx_radio_write16(bcm, 0x007A,
932                               (bcm43xx_radio_read16(bcm, 0x007A) & 0x00F8) | 0x0007);
933
934         bcm43xx_radio_selectchannel(bcm, old_channel, 0);
935
936         bcm43xx_phy_write(bcm, 0x0014, 0x0200);
937         if (radio->revision >= 6)
938                 bcm43xx_phy_write(bcm, 0x002A, 0x88C2);
939         else
940                 bcm43xx_phy_write(bcm, 0x002A, 0x8AC0);
941         bcm43xx_phy_write(bcm, 0x0038, 0x0668);
942         bcm43xx_radio_set_txpower_bg(bcm, 0xFFFF, 0xFFFF, 0xFFFF);
943         if (radio->revision <= 5)
944                 bcm43xx_phy_write(bcm, 0x005D, bcm43xx_phy_read(bcm, 0x005D) | 0x0003);
945         if (radio->revision <= 2)
946                 bcm43xx_radio_write16(bcm, 0x005D, 0x000D);
947         
948         if (phy->analog == 4){
949                 bcm43xx_write16(bcm, 0x03E4, 0x0009);
950                 bcm43xx_phy_write(bcm, 0x61, bcm43xx_phy_read(bcm, 0x61) & 0xFFF);
951         } else {
952                 bcm43xx_phy_write(bcm, 0x0002, (bcm43xx_phy_read(bcm, 0x0002) & 0xFFC0) | 0x0004);
953         }
954         if (phy->type == BCM43xx_PHYTYPE_G)
955                 bcm43xx_write16(bcm, 0x03E6, 0x0);
956         if (phy->type == BCM43xx_PHYTYPE_B) {
957                 bcm43xx_write16(bcm, 0x03E6, 0x8140);
958                 bcm43xx_phy_write(bcm, 0x0016, 0x0410);
959                 bcm43xx_phy_write(bcm, 0x0017, 0x0820);
960                 bcm43xx_phy_write(bcm, 0x0062, 0x0007);
961                 (void) bcm43xx_radio_calibrationvalue(bcm);
962                 bcm43xx_phy_lo_g_measure(bcm);
963                 if (bcm->sprom.boardflags & BCM43xx_BFL_RSSI) {
964                         bcm43xx_calc_nrssi_slope(bcm);
965                         bcm43xx_calc_nrssi_threshold(bcm);
966                 }
967                 bcm43xx_phy_init_pctl(bcm);
968         }
969 }
970
971 static void bcm43xx_calc_loopback_gain(struct bcm43xx_private *bcm)
972 {
973         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
974         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
975         u16 backup_phy[15];
976         u16 backup_radio[3];
977         u16 backup_bband;
978         u16 i;
979         u16 loop1_cnt, loop1_done, loop1_omitted;
980         u16 loop2_done;
981
982         backup_phy[0] = bcm43xx_phy_read(bcm, 0x0429);
983         backup_phy[1] = bcm43xx_phy_read(bcm, 0x0001);
984         backup_phy[2] = bcm43xx_phy_read(bcm, 0x0811);
985         backup_phy[3] = bcm43xx_phy_read(bcm, 0x0812);
986         backup_phy[4] = bcm43xx_phy_read(bcm, 0x0814);
987         backup_phy[5] = bcm43xx_phy_read(bcm, 0x0815);
988         backup_phy[6] = bcm43xx_phy_read(bcm, 0x005A);
989         backup_phy[7] = bcm43xx_phy_read(bcm, 0x0059);
990         backup_phy[8] = bcm43xx_phy_read(bcm, 0x0058);
991         backup_phy[9] = bcm43xx_phy_read(bcm, 0x000A);
992         backup_phy[10] = bcm43xx_phy_read(bcm, 0x0003);
993         backup_phy[11] = bcm43xx_phy_read(bcm, 0x080F);
994         backup_phy[12] = bcm43xx_phy_read(bcm, 0x0810);
995         backup_phy[13] = bcm43xx_phy_read(bcm, 0x002B);
996         backup_phy[14] = bcm43xx_phy_read(bcm, 0x0015);
997         bcm43xx_phy_read(bcm, 0x002D); /* dummy read */
998         backup_bband = radio->baseband_atten;
999         backup_radio[0] = bcm43xx_radio_read16(bcm, 0x0052);
1000         backup_radio[1] = bcm43xx_radio_read16(bcm, 0x0043);
1001         backup_radio[2] = bcm43xx_radio_read16(bcm, 0x007A);
1002
1003         bcm43xx_phy_write(bcm, 0x0429,
1004                           bcm43xx_phy_read(bcm, 0x0429) & 0x3FFF);
1005         bcm43xx_phy_write(bcm, 0x0001,
1006                           bcm43xx_phy_read(bcm, 0x0001) & 0x8000);
1007         bcm43xx_phy_write(bcm, 0x0811,
1008                           bcm43xx_phy_read(bcm, 0x0811) | 0x0002);
1009         bcm43xx_phy_write(bcm, 0x0812,
1010                           bcm43xx_phy_read(bcm, 0x0812) & 0xFFFD);
1011         bcm43xx_phy_write(bcm, 0x0811,
1012                           bcm43xx_phy_read(bcm, 0x0811) | 0x0001);
1013         bcm43xx_phy_write(bcm, 0x0812,
1014                           bcm43xx_phy_read(bcm, 0x0812) & 0xFFFE);
1015         bcm43xx_phy_write(bcm, 0x0814,
1016                           bcm43xx_phy_read(bcm, 0x0814) | 0x0001);
1017         bcm43xx_phy_write(bcm, 0x0815,
1018                           bcm43xx_phy_read(bcm, 0x0815) & 0xFFFE);
1019         bcm43xx_phy_write(bcm, 0x0814,
1020                           bcm43xx_phy_read(bcm, 0x0814) | 0x0002);
1021         bcm43xx_phy_write(bcm, 0x0815,
1022                           bcm43xx_phy_read(bcm, 0x0815) & 0xFFFD);
1023         bcm43xx_phy_write(bcm, 0x0811,
1024                           bcm43xx_phy_read(bcm, 0x0811) | 0x000C);
1025         bcm43xx_phy_write(bcm, 0x0812,
1026                           bcm43xx_phy_read(bcm, 0x0812) | 0x000C);
1027
1028         bcm43xx_phy_write(bcm, 0x0811,
1029                           (bcm43xx_phy_read(bcm, 0x0811)
1030                            & 0xFFCF) | 0x0030);
1031         bcm43xx_phy_write(bcm, 0x0812,
1032                           (bcm43xx_phy_read(bcm, 0x0812)
1033                            & 0xFFCF) | 0x0010);
1034
1035         bcm43xx_phy_write(bcm, 0x005A, 0x0780);
1036         bcm43xx_phy_write(bcm, 0x0059, 0xC810);
1037         bcm43xx_phy_write(bcm, 0x0058, 0x000D);
1038         if (phy->analog == 0) {
1039                 bcm43xx_phy_write(bcm, 0x0003, 0x0122);
1040         } else {
1041                 bcm43xx_phy_write(bcm, 0x000A,
1042                                   bcm43xx_phy_read(bcm, 0x000A)
1043                                   | 0x2000);
1044         }
1045         bcm43xx_phy_write(bcm, 0x0814,
1046                           bcm43xx_phy_read(bcm, 0x0814) | 0x0004);
1047         bcm43xx_phy_write(bcm, 0x0815,
1048                           bcm43xx_phy_read(bcm, 0x0815) & 0xFFFB);
1049         bcm43xx_phy_write(bcm, 0x0003,
1050                           (bcm43xx_phy_read(bcm, 0x0003)
1051                            & 0xFF9F) | 0x0040);
1052         if (radio->version == 0x2050 && radio->revision == 2) {
1053                 bcm43xx_radio_write16(bcm, 0x0052, 0x0000);
1054                 bcm43xx_radio_write16(bcm, 0x0043,
1055                                       (bcm43xx_radio_read16(bcm, 0x0043)
1056                                        & 0xFFF0) | 0x0009);
1057                 loop1_cnt = 9;
1058         } else if (radio->revision == 8) {
1059                 bcm43xx_radio_write16(bcm, 0x0043, 0x000F);
1060                 loop1_cnt = 15;
1061         } else
1062                 loop1_cnt = 0;
1063
1064         bcm43xx_phy_set_baseband_attenuation(bcm, 11);
1065
1066         if (phy->rev >= 3)
1067                 bcm43xx_phy_write(bcm, 0x080F, 0xC020);
1068         else
1069                 bcm43xx_phy_write(bcm, 0x080F, 0x8020);
1070         bcm43xx_phy_write(bcm, 0x0810, 0x0000);
1071
1072         bcm43xx_phy_write(bcm, 0x002B,
1073                           (bcm43xx_phy_read(bcm, 0x002B)
1074                            & 0xFFC0) | 0x0001);
1075         bcm43xx_phy_write(bcm, 0x002B,
1076                           (bcm43xx_phy_read(bcm, 0x002B)
1077                            & 0xC0FF) | 0x0800);
1078         bcm43xx_phy_write(bcm, 0x0811,
1079                           bcm43xx_phy_read(bcm, 0x0811) | 0x0100);
1080         bcm43xx_phy_write(bcm, 0x0812,
1081                           bcm43xx_phy_read(bcm, 0x0812) & 0xCFFF);
1082         if (bcm->sprom.boardflags & BCM43xx_BFL_EXTLNA) {
1083                 if (phy->rev >= 7) {
1084                         bcm43xx_phy_write(bcm, 0x0811,
1085                                           bcm43xx_phy_read(bcm, 0x0811)
1086                                           | 0x0800);
1087                         bcm43xx_phy_write(bcm, 0x0812,
1088                                           bcm43xx_phy_read(bcm, 0x0812)
1089                                           | 0x8000);
1090                 }
1091         }
1092         bcm43xx_radio_write16(bcm, 0x007A,
1093                               bcm43xx_radio_read16(bcm, 0x007A)
1094                               & 0x00F7);
1095
1096         for (i = 0; i < loop1_cnt; i++) {
1097                 bcm43xx_radio_write16(bcm, 0x0043, loop1_cnt);
1098                 bcm43xx_phy_write(bcm, 0x0812,
1099                                   (bcm43xx_phy_read(bcm, 0x0812)
1100                                    & 0xF0FF) | (i << 8));
1101                 bcm43xx_phy_write(bcm, 0x0015,
1102                                   (bcm43xx_phy_read(bcm, 0x0015)
1103                                    & 0x0FFF) | 0xA000);
1104                 bcm43xx_phy_write(bcm, 0x0015,
1105                                   (bcm43xx_phy_read(bcm, 0x0015)
1106                                    & 0x0FFF) | 0xF000);
1107                 udelay(20);
1108                 if (bcm43xx_phy_read(bcm, 0x002D) >= 0x0DFC)
1109                         break;
1110         }
1111         loop1_done = i;
1112         loop1_omitted = loop1_cnt - loop1_done;
1113
1114         loop2_done = 0;
1115         if (loop1_done >= 8) {
1116                 bcm43xx_phy_write(bcm, 0x0812,
1117                                   bcm43xx_phy_read(bcm, 0x0812)
1118                                   | 0x0030);
1119                 for (i = loop1_done - 8; i < 16; i++) {
1120                         bcm43xx_phy_write(bcm, 0x0812,
1121                                           (bcm43xx_phy_read(bcm, 0x0812)
1122                                            & 0xF0FF) | (i << 8));
1123                         bcm43xx_phy_write(bcm, 0x0015,
1124                                           (bcm43xx_phy_read(bcm, 0x0015)
1125                                            & 0x0FFF) | 0xA000);
1126                         bcm43xx_phy_write(bcm, 0x0015,
1127                                           (bcm43xx_phy_read(bcm, 0x0015)
1128                                            & 0x0FFF) | 0xF000);
1129                         udelay(20);
1130                         if (bcm43xx_phy_read(bcm, 0x002D) >= 0x0DFC)
1131                                 break;
1132                 }
1133         }
1134
1135         bcm43xx_phy_write(bcm, 0x0814, backup_phy[4]);
1136         bcm43xx_phy_write(bcm, 0x0815, backup_phy[5]);
1137         bcm43xx_phy_write(bcm, 0x005A, backup_phy[6]);
1138         bcm43xx_phy_write(bcm, 0x0059, backup_phy[7]);
1139         bcm43xx_phy_write(bcm, 0x0058, backup_phy[8]);
1140         bcm43xx_phy_write(bcm, 0x000A, backup_phy[9]);
1141         bcm43xx_phy_write(bcm, 0x0003, backup_phy[10]);
1142         bcm43xx_phy_write(bcm, 0x080F, backup_phy[11]);
1143         bcm43xx_phy_write(bcm, 0x0810, backup_phy[12]);
1144         bcm43xx_phy_write(bcm, 0x002B, backup_phy[13]);
1145         bcm43xx_phy_write(bcm, 0x0015, backup_phy[14]);
1146
1147         bcm43xx_phy_set_baseband_attenuation(bcm, backup_bband);
1148
1149         bcm43xx_radio_write16(bcm, 0x0052, backup_radio[0]);
1150         bcm43xx_radio_write16(bcm, 0x0043, backup_radio[1]);
1151         bcm43xx_radio_write16(bcm, 0x007A, backup_radio[2]);
1152
1153         bcm43xx_phy_write(bcm, 0x0811, backup_phy[2] | 0x0003);
1154         udelay(10);
1155         bcm43xx_phy_write(bcm, 0x0811, backup_phy[2]);
1156         bcm43xx_phy_write(bcm, 0x0812, backup_phy[3]);
1157         bcm43xx_phy_write(bcm, 0x0429, backup_phy[0]);
1158         bcm43xx_phy_write(bcm, 0x0001, backup_phy[1]);
1159
1160         phy->loopback_gain[0] = ((loop1_done * 6) - (loop1_omitted * 4)) - 11;
1161         phy->loopback_gain[1] = (24 - (3 * loop2_done)) * 2;
1162 }
1163
1164 static void bcm43xx_phy_initg(struct bcm43xx_private *bcm)
1165 {
1166         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1167         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1168         u16 tmp;
1169
1170         if (phy->rev == 1)
1171                 bcm43xx_phy_initb5(bcm);
1172         else
1173                 bcm43xx_phy_initb6(bcm);
1174         if (phy->rev >= 2 || phy->connected)
1175                 bcm43xx_phy_inita(bcm);
1176
1177         if (phy->rev >= 2) {
1178                 bcm43xx_phy_write(bcm, 0x0814, 0x0000);
1179                 bcm43xx_phy_write(bcm, 0x0815, 0x0000);
1180         }
1181         if (phy->rev == 2) {
1182                 bcm43xx_phy_write(bcm, 0x0811, 0x0000);
1183                 bcm43xx_phy_write(bcm, 0x0015, 0x00C0);
1184         }
1185         if (phy->rev >= 3) {
1186                 bcm43xx_phy_write(bcm, 0x0811, 0x0400);
1187                 bcm43xx_phy_write(bcm, 0x0015, 0x00C0);
1188         }
1189         if (phy->connected) {
1190                 tmp = bcm43xx_phy_read(bcm, 0x0400) & 0xFF;
1191                 if (tmp < 6) {
1192                         bcm43xx_phy_write(bcm, 0x04C2, 0x1816);
1193                         bcm43xx_phy_write(bcm, 0x04C3, 0x8006);
1194                         if (tmp != 3) {
1195                                 bcm43xx_phy_write(bcm, 0x04CC,
1196                                                   (bcm43xx_phy_read(bcm, 0x04CC)
1197                                                    & 0x00FF) | 0x1F00);
1198                         }
1199                 }
1200         }
1201         if (phy->rev < 3 && phy->connected)
1202                 bcm43xx_phy_write(bcm, 0x047E, 0x0078);
1203         if (radio->revision == 8) {
1204                 bcm43xx_phy_write(bcm, 0x0801, bcm43xx_phy_read(bcm, 0x0801) | 0x0080);
1205                 bcm43xx_phy_write(bcm, 0x043E, bcm43xx_phy_read(bcm, 0x043E) | 0x0004);
1206         }
1207         if (phy->rev >= 2 && phy->connected)
1208                 bcm43xx_calc_loopback_gain(bcm);
1209         if (radio->revision != 8) {
1210                 if (radio->initval == 0xFFFF)
1211                         radio->initval = bcm43xx_radio_init2050(bcm);
1212                 else
1213                         bcm43xx_radio_write16(bcm, 0x0078, radio->initval);
1214         }
1215         if (radio->txctl2 == 0xFFFF) {
1216                 bcm43xx_phy_lo_g_measure(bcm);
1217         } else {
1218                 if (radio->version == 0x2050 && radio->revision == 8) {
1219                         bcm43xx_radio_write16(bcm, 0x0052,
1220                                               (radio->txctl1 << 4) | radio->txctl2);
1221                 } else {
1222                         bcm43xx_radio_write16(bcm, 0x0052,
1223                                               (bcm43xx_radio_read16(bcm, 0x0052)
1224                                                & 0xFFF0) | radio->txctl1);
1225                 }
1226                 if (phy->rev >= 6) {
1227                         bcm43xx_phy_write(bcm, 0x0036,
1228                                           (bcm43xx_phy_read(bcm, 0x0036)
1229                                            & 0xF000) | (radio->txctl2 << 12));
1230                 }
1231                 if (bcm->sprom.boardflags & BCM43xx_BFL_PACTRL)
1232                         bcm43xx_phy_write(bcm, 0x002E, 0x8075);
1233                 else
1234                         bcm43xx_phy_write(bcm, 0x002E, 0x807F);
1235                 if (phy->rev < 2)
1236                         bcm43xx_phy_write(bcm, 0x002F, 0x0101);
1237                 else
1238                         bcm43xx_phy_write(bcm, 0x002F, 0x0202);
1239         }
1240         if (phy->connected) {
1241                 bcm43xx_phy_lo_adjust(bcm, 0);
1242                 bcm43xx_phy_write(bcm, 0x080F, 0x8078);
1243         }
1244
1245         if (!(bcm->sprom.boardflags & BCM43xx_BFL_RSSI)) {
1246                 /* The specs state to update the NRSSI LT with
1247                  * the value 0x7FFFFFFF here. I think that is some weird
1248                  * compiler optimization in the original driver.
1249                  * Essentially, what we do here is resetting all NRSSI LT
1250                  * entries to -32 (see the limit_value() in nrssi_hw_update())
1251                  */
1252                 bcm43xx_nrssi_hw_update(bcm, 0xFFFF);
1253                 bcm43xx_calc_nrssi_threshold(bcm);
1254         } else if (phy->connected) {
1255                 if (radio->nrssi[0] == -1000) {
1256                         assert(radio->nrssi[1] == -1000);
1257                         bcm43xx_calc_nrssi_slope(bcm);
1258                 } else {
1259                         assert(radio->nrssi[1] != -1000);
1260                         bcm43xx_calc_nrssi_threshold(bcm);
1261                 }
1262         }
1263         if (radio->revision == 8)
1264                 bcm43xx_phy_write(bcm, 0x0805, 0x3230);
1265         bcm43xx_phy_init_pctl(bcm);
1266         if (bcm->chip_id == 0x4306 && bcm->chip_package == 2) {
1267                 bcm43xx_phy_write(bcm, 0x0429,
1268                                   bcm43xx_phy_read(bcm, 0x0429) & 0xBFFF);
1269                 bcm43xx_phy_write(bcm, 0x04C3,
1270                                   bcm43xx_phy_read(bcm, 0x04C3) & 0x7FFF);
1271         }
1272 }
1273
1274 static u16 bcm43xx_phy_lo_b_r15_loop(struct bcm43xx_private *bcm)
1275 {
1276         int i;
1277         u16 ret = 0;
1278         unsigned long flags;
1279
1280         local_irq_save(flags);
1281         for (i = 0; i < 10; i++){
1282                 bcm43xx_phy_write(bcm, 0x0015, 0xAFA0);
1283                 udelay(1);
1284                 bcm43xx_phy_write(bcm, 0x0015, 0xEFA0);
1285                 udelay(10);
1286                 bcm43xx_phy_write(bcm, 0x0015, 0xFFA0);
1287                 udelay(40);
1288                 ret += bcm43xx_phy_read(bcm, 0x002C);
1289         }
1290         local_irq_restore(flags);
1291         bcm43xx_voluntary_preempt();
1292
1293         return ret;
1294 }
1295
1296 void bcm43xx_phy_lo_b_measure(struct bcm43xx_private *bcm)
1297 {
1298         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1299         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1300         u16 regstack[12] = { 0 };
1301         u16 mls;
1302         u16 fval;
1303         int i, j;
1304
1305         regstack[0] = bcm43xx_phy_read(bcm, 0x0015);
1306         regstack[1] = bcm43xx_radio_read16(bcm, 0x0052) & 0xFFF0;
1307
1308         if (radio->version == 0x2053) {
1309                 regstack[2] = bcm43xx_phy_read(bcm, 0x000A);
1310                 regstack[3] = bcm43xx_phy_read(bcm, 0x002A);
1311                 regstack[4] = bcm43xx_phy_read(bcm, 0x0035);
1312                 regstack[5] = bcm43xx_phy_read(bcm, 0x0003);
1313                 regstack[6] = bcm43xx_phy_read(bcm, 0x0001);
1314                 regstack[7] = bcm43xx_phy_read(bcm, 0x0030);
1315
1316                 regstack[8] = bcm43xx_radio_read16(bcm, 0x0043);
1317                 regstack[9] = bcm43xx_radio_read16(bcm, 0x007A);
1318                 regstack[10] = bcm43xx_read16(bcm, 0x03EC);
1319                 regstack[11] = bcm43xx_radio_read16(bcm, 0x0052) & 0x00F0;
1320
1321                 bcm43xx_phy_write(bcm, 0x0030, 0x00FF);
1322                 bcm43xx_write16(bcm, 0x03EC, 0x3F3F);
1323                 bcm43xx_phy_write(bcm, 0x0035, regstack[4] & 0xFF7F);
1324                 bcm43xx_radio_write16(bcm, 0x007A, regstack[9] & 0xFFF0);
1325         }
1326         bcm43xx_phy_write(bcm, 0x0015, 0xB000);
1327         bcm43xx_phy_write(bcm, 0x002B, 0x0004);
1328
1329         if (radio->version == 0x2053) {
1330                 bcm43xx_phy_write(bcm, 0x002B, 0x0203);
1331                 bcm43xx_phy_write(bcm, 0x002A, 0x08A3);
1332         }
1333
1334         phy->minlowsig[0] = 0xFFFF;
1335
1336         for (i = 0; i < 4; i++) {
1337                 bcm43xx_radio_write16(bcm, 0x0052, regstack[1] | i);
1338                 bcm43xx_phy_lo_b_r15_loop(bcm);
1339         }
1340         for (i = 0; i < 10; i++) {
1341                 bcm43xx_radio_write16(bcm, 0x0052, regstack[1] | i);
1342                 mls = bcm43xx_phy_lo_b_r15_loop(bcm) / 10;
1343                 if (mls < phy->minlowsig[0]) {
1344                         phy->minlowsig[0] = mls;
1345                         phy->minlowsigpos[0] = i;
1346                 }
1347         }
1348         bcm43xx_radio_write16(bcm, 0x0052, regstack[1] | phy->minlowsigpos[0]);
1349
1350         phy->minlowsig[1] = 0xFFFF;
1351
1352         for (i = -4; i < 5; i += 2) {
1353                 for (j = -4; j < 5; j += 2) {
1354                         if (j < 0)
1355                                 fval = (0x0100 * i) + j + 0x0100;
1356                         else
1357                                 fval = (0x0100 * i) + j;
1358                         bcm43xx_phy_write(bcm, 0x002F, fval);
1359                         mls = bcm43xx_phy_lo_b_r15_loop(bcm) / 10;
1360                         if (mls < phy->minlowsig[1]) {
1361                                 phy->minlowsig[1] = mls;
1362                                 phy->minlowsigpos[1] = fval;
1363                         }
1364                 }
1365         }
1366         phy->minlowsigpos[1] += 0x0101;
1367
1368         bcm43xx_phy_write(bcm, 0x002F, phy->minlowsigpos[1]);
1369         if (radio->version == 0x2053) {
1370                 bcm43xx_phy_write(bcm, 0x000A, regstack[2]);
1371                 bcm43xx_phy_write(bcm, 0x002A, regstack[3]);
1372                 bcm43xx_phy_write(bcm, 0x0035, regstack[4]);
1373                 bcm43xx_phy_write(bcm, 0x0003, regstack[5]);
1374                 bcm43xx_phy_write(bcm, 0x0001, regstack[6]);
1375                 bcm43xx_phy_write(bcm, 0x0030, regstack[7]);
1376
1377                 bcm43xx_radio_write16(bcm, 0x0043, regstack[8]);
1378                 bcm43xx_radio_write16(bcm, 0x007A, regstack[9]);
1379
1380                 bcm43xx_radio_write16(bcm, 0x0052,
1381                                       (bcm43xx_radio_read16(bcm, 0x0052) & 0x000F)
1382                                       | regstack[11]);
1383
1384                 bcm43xx_write16(bcm, 0x03EC, regstack[10]);
1385         }
1386         bcm43xx_phy_write(bcm, 0x0015, regstack[0]);
1387 }
1388
1389 static inline
1390 u16 bcm43xx_phy_lo_g_deviation_subval(struct bcm43xx_private *bcm, u16 control)
1391 {
1392         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1393         u16 ret;
1394         unsigned long flags;
1395
1396         local_irq_save(flags);
1397         if (phy->connected) {
1398                 bcm43xx_phy_write(bcm, 0x15, 0xE300);
1399                 control <<= 8;
1400                 bcm43xx_phy_write(bcm, 0x0812, control | 0x00B0);
1401                 udelay(5);
1402                 bcm43xx_phy_write(bcm, 0x0812, control | 0x00B2);
1403                 udelay(2);
1404                 bcm43xx_phy_write(bcm, 0x0812, control | 0x00B3);
1405                 udelay(4);
1406                 bcm43xx_phy_write(bcm, 0x0015, 0xF300);
1407                 udelay(8);
1408         } else {
1409                 bcm43xx_phy_write(bcm, 0x0015, control | 0xEFA0);
1410                 udelay(2);
1411                 bcm43xx_phy_write(bcm, 0x0015, control | 0xEFE0);
1412                 udelay(4);
1413                 bcm43xx_phy_write(bcm, 0x0015, control | 0xFFE0);
1414                 udelay(8);
1415         }
1416         ret = bcm43xx_phy_read(bcm, 0x002D);
1417         local_irq_restore(flags);
1418         bcm43xx_voluntary_preempt();
1419
1420         return ret;
1421 }
1422
1423 static u32 bcm43xx_phy_lo_g_singledeviation(struct bcm43xx_private *bcm, u16 control)
1424 {
1425         int i;
1426         u32 ret = 0;
1427
1428         for (i = 0; i < 8; i++)
1429                 ret += bcm43xx_phy_lo_g_deviation_subval(bcm, control);
1430
1431         return ret;
1432 }
1433
1434 /* Write the LocalOscillator CONTROL */
1435 static inline
1436 void bcm43xx_lo_write(struct bcm43xx_private *bcm,
1437                       struct bcm43xx_lopair *pair)
1438 {
1439         u16 value;
1440
1441         value = (u8)(pair->low);
1442         value |= ((u8)(pair->high)) << 8;
1443
1444 #ifdef CONFIG_BCM43XX_DEBUG
1445         /* Sanity check. */
1446         if (pair->low < -8 || pair->low > 8 ||
1447             pair->high < -8 || pair->high > 8) {
1448                 printk(KERN_WARNING PFX
1449                        "WARNING: Writing invalid LOpair "
1450                        "(low: %d, high: %d, index: %lu)\n",
1451                        pair->low, pair->high,
1452                        (unsigned long)(pair - bcm43xx_current_phy(bcm)->_lo_pairs));
1453                 dump_stack();
1454         }
1455 #endif
1456
1457         bcm43xx_phy_write(bcm, BCM43xx_PHY_G_LO_CONTROL, value);
1458 }
1459
1460 static inline
1461 struct bcm43xx_lopair * bcm43xx_find_lopair(struct bcm43xx_private *bcm,
1462                                             u16 baseband_attenuation,
1463                                             u16 radio_attenuation,
1464                                             u16 tx)
1465 {
1466         static const u8 dict[10] = { 11, 10, 11, 12, 13, 12, 13, 12, 13, 12 };
1467         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1468
1469         if (baseband_attenuation > 6)
1470                 baseband_attenuation = 6;
1471         assert(radio_attenuation < 10);
1472
1473         if (tx == 3) {
1474                 return bcm43xx_get_lopair(phy,
1475                                           radio_attenuation,
1476                                           baseband_attenuation);
1477         }
1478         return bcm43xx_get_lopair(phy, dict[radio_attenuation], baseband_attenuation);
1479 }
1480
1481 static inline
1482 struct bcm43xx_lopair * bcm43xx_current_lopair(struct bcm43xx_private *bcm)
1483 {
1484         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1485
1486         return bcm43xx_find_lopair(bcm,
1487                                    radio->baseband_atten,
1488                                    radio->radio_atten,
1489                                    radio->txctl1);
1490 }
1491
1492 /* Adjust B/G LO */
1493 void bcm43xx_phy_lo_adjust(struct bcm43xx_private *bcm, int fixed)
1494 {
1495         struct bcm43xx_lopair *pair;
1496
1497         if (fixed) {
1498                 /* Use fixed values. Only for initialization. */
1499                 pair = bcm43xx_find_lopair(bcm, 2, 3, 0);
1500         } else
1501                 pair = bcm43xx_current_lopair(bcm);
1502         bcm43xx_lo_write(bcm, pair);
1503 }
1504
1505 static void bcm43xx_phy_lo_g_measure_txctl2(struct bcm43xx_private *bcm)
1506 {
1507         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1508         u16 txctl2 = 0, i;
1509         u32 smallest, tmp;
1510
1511         bcm43xx_radio_write16(bcm, 0x0052, 0x0000);
1512         udelay(10);
1513         smallest = bcm43xx_phy_lo_g_singledeviation(bcm, 0);
1514         for (i = 0; i < 16; i++) {
1515                 bcm43xx_radio_write16(bcm, 0x0052, i);
1516                 udelay(10);
1517                 tmp = bcm43xx_phy_lo_g_singledeviation(bcm, 0);
1518                 if (tmp < smallest) {
1519                         smallest = tmp;
1520                         txctl2 = i;
1521                 }
1522         }
1523         radio->txctl2 = txctl2;
1524 }
1525
1526 static
1527 void bcm43xx_phy_lo_g_state(struct bcm43xx_private *bcm,
1528                             const struct bcm43xx_lopair *in_pair,
1529                             struct bcm43xx_lopair *out_pair,
1530                             u16 r27)
1531 {
1532         static const struct bcm43xx_lopair transitions[8] = {
1533                 { .high =  1,  .low =  1, },
1534                 { .high =  1,  .low =  0, },
1535                 { .high =  1,  .low = -1, },
1536                 { .high =  0,  .low = -1, },
1537                 { .high = -1,  .low = -1, },
1538                 { .high = -1,  .low =  0, },
1539                 { .high = -1,  .low =  1, },
1540                 { .high =  0,  .low =  1, },
1541         };
1542         struct bcm43xx_lopair lowest_transition = {
1543                 .high = in_pair->high,
1544                 .low = in_pair->low,
1545         };
1546         struct bcm43xx_lopair tmp_pair;
1547         struct bcm43xx_lopair transition;
1548         int i = 12;
1549         int state = 0;
1550         int found_lower;
1551         int j, begin, end;
1552         u32 lowest_deviation;
1553         u32 tmp;
1554
1555         /* Note that in_pair and out_pair can point to the same pair. Be careful. */
1556
1557         bcm43xx_lo_write(bcm, &lowest_transition);
1558         lowest_deviation = bcm43xx_phy_lo_g_singledeviation(bcm, r27);
1559         do {
1560                 found_lower = 0;
1561                 assert(state >= 0 && state <= 8);
1562                 if (state == 0) {
1563                         begin = 1;
1564                         end = 8;
1565                 } else if (state % 2 == 0) {
1566                         begin = state - 1;
1567                         end = state + 1;
1568                 } else {
1569                         begin = state - 2;
1570                         end = state + 2;
1571                 }
1572                 if (begin < 1)
1573                         begin += 8;
1574                 if (end > 8)
1575                         end -= 8;
1576
1577                 j = begin;
1578                 tmp_pair.high = lowest_transition.high;
1579                 tmp_pair.low = lowest_transition.low;
1580                 while (1) {
1581                         assert(j >= 1 && j <= 8);
1582                         transition.high = tmp_pair.high + transitions[j - 1].high;
1583                         transition.low = tmp_pair.low + transitions[j - 1].low;
1584                         if ((abs(transition.low) < 9) && (abs(transition.high) < 9)) {
1585                                 bcm43xx_lo_write(bcm, &transition);
1586                                 tmp = bcm43xx_phy_lo_g_singledeviation(bcm, r27);
1587                                 if (tmp < lowest_deviation) {
1588                                         lowest_deviation = tmp;
1589                                         state = j;
1590                                         found_lower = 1;
1591
1592                                         lowest_transition.high = transition.high;
1593                                         lowest_transition.low = transition.low;
1594                                 }
1595                         }
1596                         if (j == end)
1597                                 break;
1598                         if (j == 8)
1599                                 j = 1;
1600                         else
1601                                 j++;
1602                 }
1603         } while (i-- && found_lower);
1604
1605         out_pair->high = lowest_transition.high;
1606         out_pair->low = lowest_transition.low;
1607 }
1608
1609 /* Set the baseband attenuation value on chip. */
1610 void bcm43xx_phy_set_baseband_attenuation(struct bcm43xx_private *bcm,
1611                                           u16 baseband_attenuation)
1612 {
1613         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1614         u16 value;
1615
1616         if (phy->analog == 0) {
1617                 value = (bcm43xx_read16(bcm, 0x03E6) & 0xFFF0);
1618                 value |= (baseband_attenuation & 0x000F);
1619                 bcm43xx_write16(bcm, 0x03E6, value);
1620                 return;
1621         }
1622
1623         if (phy->analog > 1) {
1624                 value = bcm43xx_phy_read(bcm, 0x0060) & ~0x003C;
1625                 value |= (baseband_attenuation << 2) & 0x003C;
1626         } else {
1627                 value = bcm43xx_phy_read(bcm, 0x0060) & ~0x0078;
1628                 value |= (baseband_attenuation << 3) & 0x0078;
1629         }
1630         bcm43xx_phy_write(bcm, 0x0060, value);
1631 }
1632
1633 /* http://bcm-specs.sipsolutions.net/LocalOscillator/Measure */
1634 void bcm43xx_phy_lo_g_measure(struct bcm43xx_private *bcm)
1635 {
1636         static const u8 pairorder[10] = { 3, 1, 5, 7, 9, 2, 0, 4, 6, 8 };
1637         const int is_initializing = (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZING);
1638         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1639         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1640         u16 h, i, oldi = 0, j;
1641         struct bcm43xx_lopair control;
1642         struct bcm43xx_lopair *tmp_control;
1643         u16 tmp;
1644         u16 regstack[16] = { 0 };
1645         u8 oldchannel;
1646
1647         //XXX: What are these?
1648         u8 r27 = 0, r31;
1649
1650         oldchannel = radio->channel;
1651         /* Setup */
1652         if (phy->connected) {
1653                 regstack[0] = bcm43xx_phy_read(bcm, BCM43xx_PHY_G_CRS);
1654                 regstack[1] = bcm43xx_phy_read(bcm, 0x0802);
1655                 bcm43xx_phy_write(bcm, BCM43xx_PHY_G_CRS, regstack[0] & 0x7FFF);
1656                 bcm43xx_phy_write(bcm, 0x0802, regstack[1] & 0xFFFC);
1657         }
1658         regstack[3] = bcm43xx_read16(bcm, 0x03E2);
1659         bcm43xx_write16(bcm, 0x03E2, regstack[3] | 0x8000);
1660         regstack[4] = bcm43xx_read16(bcm, BCM43xx_MMIO_CHANNEL_EXT);
1661         regstack[5] = bcm43xx_phy_read(bcm, 0x15);
1662         regstack[6] = bcm43xx_phy_read(bcm, 0x2A);
1663         regstack[7] = bcm43xx_phy_read(bcm, 0x35);
1664         regstack[8] = bcm43xx_phy_read(bcm, 0x60);
1665         regstack[9] = bcm43xx_radio_read16(bcm, 0x43);
1666         regstack[10] = bcm43xx_radio_read16(bcm, 0x7A);
1667         regstack[11] = bcm43xx_radio_read16(bcm, 0x52);
1668         if (phy->connected) {
1669                 regstack[12] = bcm43xx_phy_read(bcm, 0x0811);
1670                 regstack[13] = bcm43xx_phy_read(bcm, 0x0812);
1671                 regstack[14] = bcm43xx_phy_read(bcm, 0x0814);
1672                 regstack[15] = bcm43xx_phy_read(bcm, 0x0815);
1673         }
1674         bcm43xx_radio_selectchannel(bcm, 6, 0);
1675         if (phy->connected) {
1676                 bcm43xx_phy_write(bcm, BCM43xx_PHY_G_CRS, regstack[0] & 0x7FFF);
1677                 bcm43xx_phy_write(bcm, 0x0802, regstack[1] & 0xFFFC);
1678                 bcm43xx_dummy_transmission(bcm);
1679         }
1680         bcm43xx_radio_write16(bcm, 0x0043, 0x0006);
1681
1682         bcm43xx_phy_set_baseband_attenuation(bcm, 2);
1683
1684         bcm43xx_write16(bcm, BCM43xx_MMIO_CHANNEL_EXT, 0x0000);
1685         bcm43xx_phy_write(bcm, 0x002E, 0x007F);
1686         bcm43xx_phy_write(bcm, 0x080F, 0x0078);
1687         bcm43xx_phy_write(bcm, 0x0035, regstack[7] & ~(1 << 7));
1688         bcm43xx_radio_write16(bcm, 0x007A, regstack[10] & 0xFFF0);
1689         bcm43xx_phy_write(bcm, 0x002B, 0x0203);
1690         bcm43xx_phy_write(bcm, 0x002A, 0x08A3);
1691         if (phy->connected) {
1692                 bcm43xx_phy_write(bcm, 0x0814, regstack[14] | 0x0003);
1693                 bcm43xx_phy_write(bcm, 0x0815, regstack[15] & 0xFFFC);
1694                 bcm43xx_phy_write(bcm, 0x0811, 0x01B3);
1695                 bcm43xx_phy_write(bcm, 0x0812, 0x00B2);
1696         }
1697         if (is_initializing)
1698                 bcm43xx_phy_lo_g_measure_txctl2(bcm);
1699         bcm43xx_phy_write(bcm, 0x080F, 0x8078);
1700
1701         /* Measure */
1702         control.low = 0;
1703         control.high = 0;
1704         for (h = 0; h < 10; h++) {
1705                 /* Loop over each possible RadioAttenuation (0-9) */
1706                 i = pairorder[h];
1707                 if (is_initializing) {
1708                         if (i == 3) {
1709                                 control.low = 0;
1710                                 control.high = 0;
1711                         } else if (((i % 2 == 1) && (oldi % 2 == 1)) ||
1712                                   ((i % 2 == 0) && (oldi % 2 == 0))) {
1713                                 tmp_control = bcm43xx_get_lopair(phy, oldi, 0);
1714                                 memcpy(&control, tmp_control, sizeof(control));
1715                         } else {
1716                                 tmp_control = bcm43xx_get_lopair(phy, 3, 0);
1717                                 memcpy(&control, tmp_control, sizeof(control));
1718                         }
1719                 }
1720                 /* Loop over each possible BasebandAttenuation/2 */
1721                 for (j = 0; j < 4; j++) {
1722                         if (is_initializing) {
1723                                 tmp = i * 2 + j;
1724                                 r27 = 0;
1725                                 r31 = 0;
1726                                 if (tmp > 14) {
1727                                         r31 = 1;
1728                                         if (tmp > 17)
1729                                                 r27 = 1;
1730                                         if (tmp > 19)
1731                                                 r27 = 2;
1732                                 }
1733                         } else {
1734                                 tmp_control = bcm43xx_get_lopair(phy, i, j * 2);
1735                                 if (!tmp_control->used)
1736                                         continue;
1737                                 memcpy(&control, tmp_control, sizeof(control));
1738                                 r27 = 3;
1739                                 r31 = 0;
1740                         }
1741                         bcm43xx_radio_write16(bcm, 0x43, i);
1742                         bcm43xx_radio_write16(bcm, 0x52, radio->txctl2);
1743                         udelay(10);
1744                         bcm43xx_voluntary_preempt();
1745
1746                         bcm43xx_phy_set_baseband_attenuation(bcm, j * 2);
1747
1748                         tmp = (regstack[10] & 0xFFF0);
1749                         if (r31)
1750                                 tmp |= 0x0008;
1751                         bcm43xx_radio_write16(bcm, 0x007A, tmp);
1752
1753                         tmp_control = bcm43xx_get_lopair(phy, i, j * 2);
1754                         bcm43xx_phy_lo_g_state(bcm, &control, tmp_control, r27);
1755                 }
1756                 oldi = i;
1757         }
1758         /* Loop over each possible RadioAttenuation (10-13) */
1759         for (i = 10; i < 14; i++) {
1760                 /* Loop over each possible BasebandAttenuation/2 */
1761                 for (j = 0; j < 4; j++) {
1762                         if (is_initializing) {
1763                                 tmp_control = bcm43xx_get_lopair(phy, i - 9, j * 2);
1764                                 memcpy(&control, tmp_control, sizeof(control));
1765                                 tmp = (i - 9) * 2 + j - 5;//FIXME: This is wrong, as the following if statement can never trigger.
1766                                 r27 = 0;
1767                                 r31 = 0;
1768                                 if (tmp > 14) {
1769                                         r31 = 1;
1770                                         if (tmp > 17)
1771                                                 r27 = 1;
1772                                         if (tmp > 19)
1773                                                 r27 = 2;
1774                                 }
1775                         } else {
1776                                 tmp_control = bcm43xx_get_lopair(phy, i - 9, j * 2);
1777                                 if (!tmp_control->used)
1778                                         continue;
1779                                 memcpy(&control, tmp_control, sizeof(control));
1780                                 r27 = 3;
1781                                 r31 = 0;
1782                         }
1783                         bcm43xx_radio_write16(bcm, 0x43, i - 9);
1784                         bcm43xx_radio_write16(bcm, 0x52,
1785                                               radio->txctl2
1786                                               | (3/*txctl1*/ << 4));//FIXME: shouldn't txctl1 be zero here and 3 in the loop above?
1787                         udelay(10);
1788                         bcm43xx_voluntary_preempt();
1789
1790                         bcm43xx_phy_set_baseband_attenuation(bcm, j * 2);
1791
1792                         tmp = (regstack[10] & 0xFFF0);
1793                         if (r31)
1794                                 tmp |= 0x0008;
1795                         bcm43xx_radio_write16(bcm, 0x7A, tmp);
1796
1797                         tmp_control = bcm43xx_get_lopair(phy, i, j * 2);
1798                         bcm43xx_phy_lo_g_state(bcm, &control, tmp_control, r27);
1799                 }
1800         }
1801
1802         /* Restoration */
1803         if (phy->connected) {
1804                 bcm43xx_phy_write(bcm, 0x0015, 0xE300);
1805                 bcm43xx_phy_write(bcm, 0x0812, (r27 << 8) | 0xA0);
1806                 udelay(5);
1807                 bcm43xx_phy_write(bcm, 0x0812, (r27 << 8) | 0xA2);
1808                 udelay(2);
1809                 bcm43xx_phy_write(bcm, 0x0812, (r27 << 8) | 0xA3);
1810                 bcm43xx_voluntary_preempt();
1811         } else
1812                 bcm43xx_phy_write(bcm, 0x0015, r27 | 0xEFA0);
1813         bcm43xx_phy_lo_adjust(bcm, is_initializing);
1814         bcm43xx_phy_write(bcm, 0x002E, 0x807F);
1815         if (phy->connected)
1816                 bcm43xx_phy_write(bcm, 0x002F, 0x0202);
1817         else
1818                 bcm43xx_phy_write(bcm, 0x002F, 0x0101);
1819         bcm43xx_write16(bcm, BCM43xx_MMIO_CHANNEL_EXT, regstack[4]);
1820         bcm43xx_phy_write(bcm, 0x0015, regstack[5]);
1821         bcm43xx_phy_write(bcm, 0x002A, regstack[6]);
1822         bcm43xx_phy_write(bcm, 0x0035, regstack[7]);
1823         bcm43xx_phy_write(bcm, 0x0060, regstack[8]);
1824         bcm43xx_radio_write16(bcm, 0x0043, regstack[9]);
1825         bcm43xx_radio_write16(bcm, 0x007A, regstack[10]);
1826         regstack[11] &= 0x00F0;
1827         regstack[11] |= (bcm43xx_radio_read16(bcm, 0x52) & 0x000F);
1828         bcm43xx_radio_write16(bcm, 0x52, regstack[11]);
1829         bcm43xx_write16(bcm, 0x03E2, regstack[3]);
1830         if (phy->connected) {
1831                 bcm43xx_phy_write(bcm, 0x0811, regstack[12]);
1832                 bcm43xx_phy_write(bcm, 0x0812, regstack[13]);
1833                 bcm43xx_phy_write(bcm, 0x0814, regstack[14]);
1834                 bcm43xx_phy_write(bcm, 0x0815, regstack[15]);
1835                 bcm43xx_phy_write(bcm, BCM43xx_PHY_G_CRS, regstack[0]);
1836                 bcm43xx_phy_write(bcm, 0x0802, regstack[1]);
1837         }
1838         bcm43xx_radio_selectchannel(bcm, oldchannel, 1);
1839
1840 #ifdef CONFIG_BCM43XX_DEBUG
1841         {
1842                 /* Sanity check for all lopairs. */
1843                 for (i = 0; i < BCM43xx_LO_COUNT; i++) {
1844                         tmp_control = phy->_lo_pairs + i;
1845                         if (tmp_control->low < -8 || tmp_control->low > 8 ||
1846                             tmp_control->high < -8 || tmp_control->high > 8) {
1847                                 printk(KERN_WARNING PFX
1848                                        "WARNING: Invalid LOpair (low: %d, high: %d, index: %d)\n",
1849                                        tmp_control->low, tmp_control->high, i);
1850                         }
1851                 }
1852         }
1853 #endif /* CONFIG_BCM43XX_DEBUG */
1854 }
1855
1856 static
1857 void bcm43xx_phy_lo_mark_current_used(struct bcm43xx_private *bcm)
1858 {
1859         struct bcm43xx_lopair *pair;
1860
1861         pair = bcm43xx_current_lopair(bcm);
1862         pair->used = 1;
1863 }
1864
1865 void bcm43xx_phy_lo_mark_all_unused(struct bcm43xx_private *bcm)
1866 {
1867         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1868         struct bcm43xx_lopair *pair;
1869         int i;
1870
1871         for (i = 0; i < BCM43xx_LO_COUNT; i++) {
1872                 pair = phy->_lo_pairs + i;
1873                 pair->used = 0;
1874         }
1875 }
1876
1877 /* http://bcm-specs.sipsolutions.net/EstimatePowerOut
1878  * This function converts a TSSI value to dBm in Q5.2
1879  */
1880 static s8 bcm43xx_phy_estimate_power_out(struct bcm43xx_private *bcm, s8 tssi)
1881 {
1882         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1883         s8 dbm = 0;
1884         s32 tmp;
1885
1886         tmp = phy->idle_tssi;
1887         tmp += tssi;
1888         tmp -= phy->savedpctlreg;
1889
1890         switch (phy->type) {
1891                 case BCM43xx_PHYTYPE_A:
1892                         tmp += 0x80;
1893                         tmp = limit_value(tmp, 0x00, 0xFF);
1894                         dbm = phy->tssi2dbm[tmp];
1895                         TODO(); //TODO: There's a FIXME on the specs
1896                         break;
1897                 case BCM43xx_PHYTYPE_B:
1898                 case BCM43xx_PHYTYPE_G:
1899                         tmp = limit_value(tmp, 0x00, 0x3F);
1900                         dbm = phy->tssi2dbm[tmp];
1901                         break;
1902                 default:
1903                         assert(0);
1904         }
1905
1906         return dbm;
1907 }
1908
1909 /* http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower */
1910 void bcm43xx_phy_xmitpower(struct bcm43xx_private *bcm)
1911 {
1912         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
1913         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1914         
1915         if (phy->savedpctlreg == 0xFFFF)
1916                 return;
1917         if ((bcm->board_type == 0x0416) &&
1918             (bcm->board_vendor == PCI_VENDOR_ID_BROADCOM))
1919                 return;
1920         
1921         switch (phy->type) {
1922         case BCM43xx_PHYTYPE_A: {
1923
1924                 TODO(); //TODO: Nothing for A PHYs yet :-/
1925
1926                 break;
1927         }
1928         case BCM43xx_PHYTYPE_B:
1929         case BCM43xx_PHYTYPE_G: {
1930                 u16 tmp;
1931                 u16 txpower;
1932                 s8 v0, v1, v2, v3;
1933                 s8 average;
1934                 u8 max_pwr;
1935                 s16 desired_pwr, estimated_pwr, pwr_adjust;
1936                 s16 radio_att_delta, baseband_att_delta;
1937                 s16 radio_attenuation, baseband_attenuation;
1938                 unsigned long phylock_flags;
1939
1940                 tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x0058);
1941                 v0 = (s8)(tmp & 0x00FF);
1942                 v1 = (s8)((tmp & 0xFF00) >> 8);
1943                 tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x005A);
1944                 v2 = (s8)(tmp & 0x00FF);
1945                 v3 = (s8)((tmp & 0xFF00) >> 8);
1946                 tmp = 0;
1947
1948                 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F) {
1949                         tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x0070);
1950                         v0 = (s8)(tmp & 0x00FF);
1951                         v1 = (s8)((tmp & 0xFF00) >> 8);
1952                         tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x0072);
1953                         v2 = (s8)(tmp & 0x00FF);
1954                         v3 = (s8)((tmp & 0xFF00) >> 8);
1955                         if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F)
1956                                 return;
1957                         v0 = (v0 + 0x20) & 0x3F;
1958                         v1 = (v1 + 0x20) & 0x3F;
1959                         v2 = (v2 + 0x20) & 0x3F;
1960                         v3 = (v3 + 0x20) & 0x3F;
1961                         tmp = 1;
1962                 }
1963                 bcm43xx_radio_clear_tssi(bcm);
1964
1965                 average = (v0 + v1 + v2 + v3 + 2) / 4;
1966
1967                 if (tmp && (bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x005E) & 0x8))
1968                         average -= 13;
1969
1970                 estimated_pwr = bcm43xx_phy_estimate_power_out(bcm, average);
1971
1972                 max_pwr = bcm->sprom.maxpower_bgphy;
1973
1974                 if ((bcm->sprom.boardflags & BCM43xx_BFL_PACTRL) &&
1975                     (phy->type == BCM43xx_PHYTYPE_G))
1976                         max_pwr -= 0x3;
1977
1978                 /*TODO:
1979                 max_pwr = min(REG - bcm->sprom.antennagain_bgphy - 0x6, max_pwr)
1980                         where REG is the max power as per the regulatory domain
1981                 */
1982
1983                 desired_pwr = limit_value(radio->txpower_desired, 0, max_pwr);
1984                 /* Check if we need to adjust the current power. */
1985                 pwr_adjust = desired_pwr - estimated_pwr;
1986                 radio_att_delta = -(pwr_adjust + 7) >> 3;
1987                 baseband_att_delta = -(pwr_adjust >> 1) - (4 * radio_att_delta);
1988                 if ((radio_att_delta == 0) && (baseband_att_delta == 0)) {
1989                         bcm43xx_phy_lo_mark_current_used(bcm);
1990                         return;
1991                 }
1992
1993                 /* Calculate the new attenuation values. */
1994                 baseband_attenuation = radio->baseband_atten;
1995                 baseband_attenuation += baseband_att_delta;
1996                 radio_attenuation = radio->radio_atten;
1997                 radio_attenuation += radio_att_delta;
1998
1999                 /* Get baseband and radio attenuation values into their permitted ranges.
2000                  * baseband 0-11, radio 0-9.
2001                  * Radio attenuation affects power level 4 times as much as baseband.
2002                  */
2003                 if (radio_attenuation < 0) {
2004                         baseband_attenuation -= (4 * -radio_attenuation);
2005                         radio_attenuation = 0;
2006                 } else if (radio_attenuation > 9) {
2007                         baseband_attenuation += (4 * (radio_attenuation - 9));
2008                         radio_attenuation = 9;
2009                 } else {
2010                         while (baseband_attenuation < 0 && radio_attenuation > 0) {
2011                                 baseband_attenuation += 4;
2012                                 radio_attenuation--;
2013                         }
2014                         while (baseband_attenuation > 11 && radio_attenuation < 9) {
2015                                 baseband_attenuation -= 4;
2016                                 radio_attenuation++;
2017                         }
2018                 }
2019                 baseband_attenuation = limit_value(baseband_attenuation, 0, 11);
2020
2021                 txpower = radio->txctl1;
2022                 if ((radio->version == 0x2050) && (radio->revision == 2)) {
2023                         if (radio_attenuation <= 1) {
2024                                 if (txpower == 0) {
2025                                         txpower = 3;
2026                                         radio_attenuation += 2;
2027                                         baseband_attenuation += 2;
2028                                 } else if (bcm->sprom.boardflags & BCM43xx_BFL_PACTRL) {
2029                                         baseband_attenuation += 4 * (radio_attenuation - 2);
2030                                         radio_attenuation = 2;
2031                                 }
2032                         } else if (radio_attenuation > 4 && txpower != 0) {
2033                                 txpower = 0;
2034                                 if (baseband_attenuation < 3) {
2035                                         radio_attenuation -= 3;
2036                                         baseband_attenuation += 2;
2037                                 } else {
2038                                         radio_attenuation -= 2;
2039                                         baseband_attenuation -= 2;
2040                                 }
2041                         }
2042                 }
2043                 radio->txctl1 = txpower;
2044                 baseband_attenuation = limit_value(baseband_attenuation, 0, 11);
2045                 radio_attenuation = limit_value(radio_attenuation, 0, 9);
2046
2047                 bcm43xx_phy_lock(bcm, phylock_flags);
2048                 bcm43xx_radio_lock(bcm);
2049                 bcm43xx_radio_set_txpower_bg(bcm, baseband_attenuation,
2050                                              radio_attenuation, txpower);
2051                 bcm43xx_phy_lo_mark_current_used(bcm);
2052                 bcm43xx_radio_unlock(bcm);
2053                 bcm43xx_phy_unlock(bcm, phylock_flags);
2054                 break;
2055         }
2056         default:
2057                 assert(0);
2058         }
2059 }
2060
2061 static inline
2062 s32 bcm43xx_tssi2dbm_ad(s32 num, s32 den)
2063 {
2064         if (num < 0)
2065                 return num/den;
2066         else
2067                 return (num+den/2)/den;
2068 }
2069
2070 static inline
2071 s8 bcm43xx_tssi2dbm_entry(s8 entry [], u8 index, s16 pab0, s16 pab1, s16 pab2)
2072 {
2073         s32 m1, m2, f = 256, q, delta;
2074         s8 i = 0;
2075         
2076         m1 = bcm43xx_tssi2dbm_ad(16 * pab0 + index * pab1, 32);
2077         m2 = max(bcm43xx_tssi2dbm_ad(32768 + index * pab2, 256), 1);
2078         do {
2079                 if (i > 15)
2080                         return -EINVAL;
2081                 q = bcm43xx_tssi2dbm_ad(f * 4096 -
2082                                         bcm43xx_tssi2dbm_ad(m2 * f, 16) * f, 2048);
2083                 delta = abs(q - f);
2084                 f = q;
2085                 i++;
2086         } while (delta >= 2);
2087         entry[index] = limit_value(bcm43xx_tssi2dbm_ad(m1 * f, 8192), -127, 128);
2088         return 0;
2089 }
2090
2091 /* http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table */
2092 int bcm43xx_phy_init_tssi2dbm_table(struct bcm43xx_private *bcm)
2093 {
2094         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
2095         struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
2096         s16 pab0, pab1, pab2;
2097         u8 idx;
2098         s8 *dyn_tssi2dbm;
2099         
2100         if (phy->type == BCM43xx_PHYTYPE_A) {
2101                 pab0 = (s16)(bcm->sprom.pa1b0);
2102                 pab1 = (s16)(bcm->sprom.pa1b1);
2103                 pab2 = (s16)(bcm->sprom.pa1b2);
2104         } else {
2105                 pab0 = (s16)(bcm->sprom.pa0b0);
2106                 pab1 = (s16)(bcm->sprom.pa0b1);
2107                 pab2 = (s16)(bcm->sprom.pa0b2);
2108         }
2109
2110         if ((bcm->chip_id == 0x4301) && (radio->version != 0x2050)) {
2111                 phy->idle_tssi = 0x34;
2112                 phy->tssi2dbm = bcm43xx_tssi2dbm_b_table;
2113                 return 0;
2114         }
2115
2116         if (pab0 != 0 && pab1 != 0 && pab2 != 0 &&
2117             pab0 != -1 && pab1 != -1 && pab2 != -1) {
2118                 /* The pabX values are set in SPROM. Use them. */
2119                 if (phy->type == BCM43xx_PHYTYPE_A) {
2120                         if ((s8)bcm->sprom.idle_tssi_tgt_aphy != 0 &&
2121                             (s8)bcm->sprom.idle_tssi_tgt_aphy != -1)
2122                                 phy->idle_tssi = (s8)(bcm->sprom.idle_tssi_tgt_aphy);
2123                         else
2124                                 phy->idle_tssi = 62;
2125                 } else {
2126                         if ((s8)bcm->sprom.idle_tssi_tgt_bgphy != 0 &&
2127                             (s8)bcm->sprom.idle_tssi_tgt_bgphy != -1)
2128                                 phy->idle_tssi = (s8)(bcm->sprom.idle_tssi_tgt_bgphy);
2129                         else
2130                                 phy->idle_tssi = 62;
2131                 }
2132                 dyn_tssi2dbm = kmalloc(64, GFP_KERNEL);
2133                 if (dyn_tssi2dbm == NULL) {
2134                         printk(KERN_ERR PFX "Could not allocate memory"
2135                                             "for tssi2dbm table\n");
2136                         return -ENOMEM;
2137                 }
2138                 for (idx = 0; idx < 64; idx++)
2139                         if (bcm43xx_tssi2dbm_entry(dyn_tssi2dbm, idx, pab0, pab1, pab2)) {
2140                                 phy->tssi2dbm = NULL;
2141                                 printk(KERN_ERR PFX "Could not generate "
2142                                                     "tssi2dBm table\n");
2143                                 kfree(dyn_tssi2dbm);
2144                                 return -ENODEV;
2145                         }
2146                 phy->tssi2dbm = dyn_tssi2dbm;
2147                 phy->dyn_tssi_tbl = 1;
2148         } else {
2149                 /* pabX values not set in SPROM. */
2150                 switch (phy->type) {
2151                 case BCM43xx_PHYTYPE_A:
2152                         /* APHY needs a generated table. */
2153                         phy->tssi2dbm = NULL;
2154                         printk(KERN_ERR PFX "Could not generate tssi2dBm "
2155                                             "table (wrong SPROM info)!\n");
2156                         return -ENODEV;
2157                 case BCM43xx_PHYTYPE_B:
2158                         phy->idle_tssi = 0x34;
2159                         phy->tssi2dbm = bcm43xx_tssi2dbm_b_table;
2160                         break;
2161                 case BCM43xx_PHYTYPE_G:
2162                         phy->idle_tssi = 0x34;
2163                         phy->tssi2dbm = bcm43xx_tssi2dbm_g_table;
2164                         break;
2165                 }
2166         }
2167
2168         return 0;
2169 }
2170
2171 int bcm43xx_phy_init(struct bcm43xx_private *bcm)
2172 {
2173         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
2174         int err = -ENODEV;
2175
2176         switch (phy->type) {
2177         case BCM43xx_PHYTYPE_A:
2178                 if (phy->rev == 2 || phy->rev == 3) {
2179                         bcm43xx_phy_inita(bcm);
2180                         err = 0;
2181                 }
2182                 break;
2183         case BCM43xx_PHYTYPE_B:
2184                 switch (phy->rev) {
2185                 case 2:
2186                         bcm43xx_phy_initb2(bcm);
2187                         err = 0;
2188                         break;
2189                 case 4:
2190                         bcm43xx_phy_initb4(bcm);
2191                         err = 0;
2192                         break;
2193                 case 5:
2194                         bcm43xx_phy_initb5(bcm);
2195                         err = 0;
2196                         break;
2197                 case 6:
2198                         bcm43xx_phy_initb6(bcm);
2199                         err = 0;
2200                         break;
2201                 }
2202                 break;
2203         case BCM43xx_PHYTYPE_G:
2204                 bcm43xx_phy_initg(bcm);
2205                 err = 0;
2206                 break;
2207         }
2208         if (err)
2209                 printk(KERN_WARNING PFX "Unknown PHYTYPE found!\n");
2210
2211         return err;
2212 }
2213
2214 void bcm43xx_phy_set_antenna_diversity(struct bcm43xx_private *bcm)
2215 {
2216         struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
2217         u16 antennadiv;
2218         u16 offset;
2219         u16 value;
2220         u32 ucodeflags;
2221
2222         antennadiv = phy->antenna_diversity;
2223
2224         if (antennadiv == 0xFFFF)
2225                 antennadiv = 3;
2226         assert(antennadiv <= 3);
2227
2228         ucodeflags = bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED,
2229                                         BCM43xx_UCODEFLAGS_OFFSET);
2230         bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED,
2231                             BCM43xx_UCODEFLAGS_OFFSET,
2232                             ucodeflags & ~BCM43xx_UCODEFLAG_AUTODIV);
2233
2234         switch (phy->type) {
2235         case BCM43xx_PHYTYPE_A:
2236         case BCM43xx_PHYTYPE_G:
2237                 if (phy->type == BCM43xx_PHYTYPE_A)
2238                         offset = 0x0000;
2239                 else
2240                         offset = 0x0400;
2241
2242                 if (antennadiv == 2)
2243                         value = (3/*automatic*/ << 7);
2244                 else
2245                         value = (antennadiv << 7);
2246                 bcm43xx_phy_write(bcm, offset + 1,
2247                                   (bcm43xx_phy_read(bcm, offset + 1)
2248                                    & 0x7E7F) | value);
2249
2250                 if (antennadiv >= 2) {
2251                         if (antennadiv == 2)
2252                                 value = (antennadiv << 7);
2253                         else
2254                                 value = (0/*force0*/ << 7);
2255                         bcm43xx_phy_write(bcm, offset + 0x2B,
2256                                           (bcm43xx_phy_read(bcm, offset + 0x2B)
2257                                            & 0xFEFF) | value);
2258                 }
2259
2260                 if (phy->type == BCM43xx_PHYTYPE_G) {
2261                         if (antennadiv >= 2)
2262                                 bcm43xx_phy_write(bcm, 0x048C,
2263                                                   bcm43xx_phy_read(bcm, 0x048C)
2264                                                    | 0x2000);
2265                         else
2266                                 bcm43xx_phy_write(bcm, 0x048C,
2267                                                   bcm43xx_phy_read(bcm, 0x048C)
2268                                                    & ~0x2000);
2269                         if (phy->rev >= 2) {
2270                                 bcm43xx_phy_write(bcm, 0x0461,
2271                                                   bcm43xx_phy_read(bcm, 0x0461)
2272                                                    | 0x0010);
2273                                 bcm43xx_phy_write(bcm, 0x04AD,
2274                                                   (bcm43xx_phy_read(bcm, 0x04AD)
2275                                                    & 0x00FF) | 0x0015);
2276                                 if (phy->rev == 2)
2277                                         bcm43xx_phy_write(bcm, 0x0427, 0x0008);
2278                                 else
2279                                         bcm43xx_phy_write(bcm, 0x0427,
2280                                                 (bcm43xx_phy_read(bcm, 0x0427)
2281                                                  & 0x00FF) | 0x0008);
2282                         }
2283                         else if (phy->rev >= 6)
2284                                 bcm43xx_phy_write(bcm, 0x049B, 0x00DC);
2285                 } else {
2286                         if (phy->rev < 3)
2287                                 bcm43xx_phy_write(bcm, 0x002B,
2288                                                   (bcm43xx_phy_read(bcm, 0x002B)
2289                                                    & 0x00FF) | 0x0024);
2290                         else {
2291                                 bcm43xx_phy_write(bcm, 0x0061,
2292                                                   bcm43xx_phy_read(bcm, 0x0061)
2293                                                    | 0x0010);
2294                                 if (phy->rev == 3) {
2295                                         bcm43xx_phy_write(bcm, 0x0093, 0x001D);
2296                                         bcm43xx_phy_write(bcm, 0x0027, 0x0008);
2297                                 } else {
2298                                         bcm43xx_phy_write(bcm, 0x0093, 0x003A);
2299                                         bcm43xx_phy_write(bcm, 0x0027,
2300                                                 (bcm43xx_phy_read(bcm, 0x0027)
2301                                                  & 0x00FF) | 0x0008);
2302                                 }
2303                         }
2304                 }
2305                 break;
2306         case BCM43xx_PHYTYPE_B:
2307                 if (bcm->current_core->rev == 2)
2308                         value = (3/*automatic*/ << 7);
2309                 else
2310                         value = (antennadiv << 7);
2311                 bcm43xx_phy_write(bcm, 0x03E2,
2312                                   (bcm43xx_phy_read(bcm, 0x03E2)
2313                                    & 0xFE7F) | value);
2314                 break;
2315         default:
2316                 assert(0);
2317         }
2318
2319         if (antennadiv >= 2) {
2320                 ucodeflags = bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED,
2321                                                 BCM43xx_UCODEFLAGS_OFFSET);
2322                 bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED,
2323                                     BCM43xx_UCODEFLAGS_OFFSET,
2324                                     ucodeflags | BCM43xx_UCODEFLAG_AUTODIV);
2325         }
2326
2327         phy->antenna_diversity = antennadiv;
2328 }