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