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