3 Broadcom B43legacy wireless driver
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6 Stefano Brivio <stefano.brivio@polimi.it>
7 Michael Buesch <mbuesch@freenet.de>
8 Danny van Dyk <kugelfang@gentoo.org>
9 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10 Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
12 Some parts of the code in this file are derived from the ipw2200
13 driver Copyright(c) 2003 - 2004 Intel Corporation.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; see the file COPYING. If not, write to
27 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
28 Boston, MA 02110-1301, USA.
32 #include <linux/delay.h>
33 #include <linux/pci.h>
34 #include <linux/types.h>
36 #include "b43legacy.h"
43 static const s8 b43legacy_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,
62 static const s8 b43legacy_tssi2dbm_g_table[] = {
81 static void b43legacy_phy_initg(struct b43legacy_wldev *dev);
85 void b43legacy_voluntary_preempt(void)
87 B43legacy_BUG_ON(!(!in_atomic() && !in_irq() &&
88 !in_interrupt() && !irqs_disabled()));
89 #ifndef CONFIG_PREEMPT
91 #endif /* CONFIG_PREEMPT */
94 /* Lock the PHY registers against concurrent access from the microcode.
95 * This lock is nonrecursive. */
96 void b43legacy_phy_lock(struct b43legacy_wldev *dev)
99 B43legacy_WARN_ON(dev->phy.phy_locked);
100 dev->phy.phy_locked = 1;
103 if (dev->dev->id.revision < 3) {
104 b43legacy_mac_suspend(dev);
106 if (!b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
107 b43legacy_power_saving_ctl_bits(dev, -1, 1);
111 void b43legacy_phy_unlock(struct b43legacy_wldev *dev)
114 B43legacy_WARN_ON(!dev->phy.phy_locked);
115 dev->phy.phy_locked = 0;
118 if (dev->dev->id.revision < 3) {
119 b43legacy_mac_enable(dev);
121 if (!b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
122 b43legacy_power_saving_ctl_bits(dev, -1, -1);
126 u16 b43legacy_phy_read(struct b43legacy_wldev *dev, u16 offset)
128 b43legacy_write16(dev, B43legacy_MMIO_PHY_CONTROL, offset);
129 return b43legacy_read16(dev, B43legacy_MMIO_PHY_DATA);
132 void b43legacy_phy_write(struct b43legacy_wldev *dev, u16 offset, u16 val)
134 b43legacy_write16(dev, B43legacy_MMIO_PHY_CONTROL, offset);
136 b43legacy_write16(dev, B43legacy_MMIO_PHY_DATA, val);
139 void b43legacy_phy_calibrate(struct b43legacy_wldev *dev)
141 struct b43legacy_phy *phy = &dev->phy;
143 b43legacy_read32(dev, B43legacy_MMIO_MACCTL); /* Dummy read. */
146 if (phy->type == B43legacy_PHYTYPE_G && phy->rev == 1) {
147 b43legacy_wireless_core_reset(dev, 0);
148 b43legacy_phy_initg(dev);
149 b43legacy_wireless_core_reset(dev, B43legacy_TMSLOW_GMODE);
154 /* intialize B PHY power control
155 * as described in http://bcm-specs.sipsolutions.net/InitPowerControl
157 static void b43legacy_phy_init_pctl(struct b43legacy_wldev *dev)
159 struct b43legacy_phy *phy = &dev->phy;
162 u16 saved_txctl1 = 0;
163 int must_reset_txpower = 0;
165 B43legacy_BUG_ON(!(phy->type == B43legacy_PHYTYPE_B ||
166 phy->type == B43legacy_PHYTYPE_G));
167 if (is_bcm_board_vendor(dev) &&
168 (dev->dev->bus->boardinfo.type == 0x0416))
171 b43legacy_phy_write(dev, 0x0028, 0x8018);
172 b43legacy_write16(dev, 0x03E6, b43legacy_read16(dev, 0x03E6) & 0xFFDF);
174 if (phy->type == B43legacy_PHYTYPE_G) {
177 b43legacy_phy_write(dev, 0x047A, 0xC111);
179 if (phy->savedpctlreg != 0xFFFF)
181 #ifdef CONFIG_B43LEGACY_DEBUG
182 if (phy->manual_txpower_control)
186 if (phy->type == B43legacy_PHYTYPE_B &&
188 phy->radio_ver == 0x2050)
189 b43legacy_radio_write16(dev, 0x0076,
190 b43legacy_radio_read16(dev, 0x0076)
193 saved_batt = phy->bbatt;
194 saved_ratt = phy->rfatt;
195 saved_txctl1 = phy->txctl1;
196 if ((phy->radio_rev >= 6) && (phy->radio_rev <= 8)
197 && /*FIXME: incomplete specs for 5 < revision < 9 */ 0)
198 b43legacy_radio_set_txpower_bg(dev, 0xB, 0x1F, 0);
200 b43legacy_radio_set_txpower_bg(dev, 0xB, 9, 0);
201 must_reset_txpower = 1;
203 b43legacy_dummy_transmission(dev);
205 phy->savedpctlreg = b43legacy_phy_read(dev, B43legacy_PHY_G_PCTL);
207 if (must_reset_txpower)
208 b43legacy_radio_set_txpower_bg(dev, saved_batt, saved_ratt,
211 b43legacy_radio_write16(dev, 0x0076, b43legacy_radio_read16(dev,
213 b43legacy_radio_clear_tssi(dev);
216 static void b43legacy_phy_agcsetup(struct b43legacy_wldev *dev)
218 struct b43legacy_phy *phy = &dev->phy;
224 b43legacy_ilt_write(dev, offset, 0x00FE);
225 b43legacy_ilt_write(dev, offset + 1, 0x000D);
226 b43legacy_ilt_write(dev, offset + 2, 0x0013);
227 b43legacy_ilt_write(dev, offset + 3, 0x0019);
230 b43legacy_ilt_write(dev, 0x1800, 0x2710);
231 b43legacy_ilt_write(dev, 0x1801, 0x9B83);
232 b43legacy_ilt_write(dev, 0x1802, 0x9B83);
233 b43legacy_ilt_write(dev, 0x1803, 0x0F8D);
234 b43legacy_phy_write(dev, 0x0455, 0x0004);
237 b43legacy_phy_write(dev, 0x04A5, (b43legacy_phy_read(dev, 0x04A5)
239 b43legacy_phy_write(dev, 0x041A, (b43legacy_phy_read(dev, 0x041A)
241 b43legacy_phy_write(dev, 0x041A, (b43legacy_phy_read(dev, 0x041A)
243 b43legacy_phy_write(dev, 0x048C, (b43legacy_phy_read(dev, 0x048C)
246 b43legacy_radio_write16(dev, 0x007A,
247 b43legacy_radio_read16(dev, 0x007A)
250 b43legacy_phy_write(dev, 0x04A0, (b43legacy_phy_read(dev, 0x04A0)
252 b43legacy_phy_write(dev, 0x04A1, (b43legacy_phy_read(dev, 0x04A1)
254 b43legacy_phy_write(dev, 0x04A2, (b43legacy_phy_read(dev, 0x04A2)
256 b43legacy_phy_write(dev, 0x04A0, (b43legacy_phy_read(dev, 0x04A0)
260 b43legacy_phy_write(dev, 0x04A2,
261 (b43legacy_phy_read(dev, 0x04A2)
264 b43legacy_phy_write(dev, 0x0488, (b43legacy_phy_read(dev, 0x0488)
266 b43legacy_phy_write(dev, 0x0488, (b43legacy_phy_read(dev, 0x0488)
268 b43legacy_phy_write(dev, 0x0496, (b43legacy_phy_read(dev, 0x0496)
270 b43legacy_phy_write(dev, 0x0489, (b43legacy_phy_read(dev, 0x0489)
272 b43legacy_phy_write(dev, 0x0489, (b43legacy_phy_read(dev, 0x0489)
274 b43legacy_phy_write(dev, 0x0482, (b43legacy_phy_read(dev, 0x0482)
276 b43legacy_phy_write(dev, 0x0496, (b43legacy_phy_read(dev, 0x0496)
278 b43legacy_phy_write(dev, 0x0481, (b43legacy_phy_read(dev, 0x0481)
280 b43legacy_phy_write(dev, 0x0481, (b43legacy_phy_read(dev, 0x0481)
284 b43legacy_phy_write(dev, 0x0430, 0x092B);
285 b43legacy_phy_write(dev, 0x041B,
286 (b43legacy_phy_read(dev, 0x041B)
289 b43legacy_phy_write(dev, 0x041B,
290 b43legacy_phy_read(dev, 0x041B) & 0xFFE1);
291 b43legacy_phy_write(dev, 0x041F, 0x287A);
292 b43legacy_phy_write(dev, 0x0420,
293 (b43legacy_phy_read(dev, 0x0420)
298 b43legacy_phy_write(dev, 0x0422, 0x287A);
299 b43legacy_phy_write(dev, 0x0420,
300 (b43legacy_phy_read(dev, 0x0420)
304 b43legacy_phy_write(dev, 0x04A8, (b43legacy_phy_read(dev, 0x04A8)
306 b43legacy_phy_write(dev, 0x048E, 0x1C00);
309 b43legacy_phy_write(dev, 0x04AB,
310 (b43legacy_phy_read(dev, 0x04AB)
312 b43legacy_phy_write(dev, 0x048B, 0x005E);
313 b43legacy_phy_write(dev, 0x048C,
314 (b43legacy_phy_read(dev, 0x048C) & 0xFF00)
316 b43legacy_phy_write(dev, 0x048D, 0x0002);
319 b43legacy_ilt_write(dev, offset + 0x0800, 0);
320 b43legacy_ilt_write(dev, offset + 0x0801, 7);
321 b43legacy_ilt_write(dev, offset + 0x0802, 16);
322 b43legacy_ilt_write(dev, offset + 0x0803, 28);
325 b43legacy_phy_write(dev, 0x0426,
326 (b43legacy_phy_read(dev, 0x0426) & 0xFFFC));
327 b43legacy_phy_write(dev, 0x0426,
328 (b43legacy_phy_read(dev, 0x0426) & 0xEFFF));
332 static void b43legacy_phy_setupg(struct b43legacy_wldev *dev)
334 struct b43legacy_phy *phy = &dev->phy;
337 B43legacy_BUG_ON(phy->type != B43legacy_PHYTYPE_G);
339 b43legacy_phy_write(dev, 0x0406, 0x4F19);
340 b43legacy_phy_write(dev, B43legacy_PHY_G_CRS,
341 (b43legacy_phy_read(dev,
342 B43legacy_PHY_G_CRS) & 0xFC3F) | 0x0340);
343 b43legacy_phy_write(dev, 0x042C, 0x005A);
344 b43legacy_phy_write(dev, 0x0427, 0x001A);
346 for (i = 0; i < B43legacy_ILT_FINEFREQG_SIZE; i++)
347 b43legacy_ilt_write(dev, 0x5800 + i,
348 b43legacy_ilt_finefreqg[i]);
349 for (i = 0; i < B43legacy_ILT_NOISEG1_SIZE; i++)
350 b43legacy_ilt_write(dev, 0x1800 + i,
351 b43legacy_ilt_noiseg1[i]);
352 for (i = 0; i < B43legacy_ILT_ROTOR_SIZE; i++)
353 b43legacy_ilt_write32(dev, 0x2000 + i,
354 b43legacy_ilt_rotor[i]);
356 /* nrssi values are signed 6-bit values. Why 0x7654 here? */
357 b43legacy_nrssi_hw_write(dev, 0xBA98, (s16)0x7654);
360 b43legacy_phy_write(dev, 0x04C0, 0x1861);
361 b43legacy_phy_write(dev, 0x04C1, 0x0271);
362 } else if (phy->rev > 2) {
363 b43legacy_phy_write(dev, 0x04C0, 0x0098);
364 b43legacy_phy_write(dev, 0x04C1, 0x0070);
365 b43legacy_phy_write(dev, 0x04C9, 0x0080);
367 b43legacy_phy_write(dev, 0x042B, b43legacy_phy_read(dev,
370 for (i = 0; i < 64; i++)
371 b43legacy_ilt_write(dev, 0x4000 + i, i);
372 for (i = 0; i < B43legacy_ILT_NOISEG2_SIZE; i++)
373 b43legacy_ilt_write(dev, 0x1800 + i,
374 b43legacy_ilt_noiseg2[i]);
378 for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
379 b43legacy_ilt_write(dev, 0x1400 + i,
380 b43legacy_ilt_noisescaleg1[i]);
381 else if ((phy->rev >= 7) && (b43legacy_phy_read(dev, 0x0449) & 0x0200))
382 for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
383 b43legacy_ilt_write(dev, 0x1400 + i,
384 b43legacy_ilt_noisescaleg3[i]);
386 for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
387 b43legacy_ilt_write(dev, 0x1400 + i,
388 b43legacy_ilt_noisescaleg2[i]);
391 for (i = 0; i < B43legacy_ILT_SIGMASQR_SIZE; i++)
392 b43legacy_ilt_write(dev, 0x5000 + i,
393 b43legacy_ilt_sigmasqr1[i]);
394 else if ((phy->rev > 2) && (phy->rev <= 8))
395 for (i = 0; i < B43legacy_ILT_SIGMASQR_SIZE; i++)
396 b43legacy_ilt_write(dev, 0x5000 + i,
397 b43legacy_ilt_sigmasqr2[i]);
400 for (i = 0; i < B43legacy_ILT_RETARD_SIZE; i++)
401 b43legacy_ilt_write32(dev, 0x2400 + i,
402 b43legacy_ilt_retard[i]);
403 for (i = 4; i < 20; i++)
404 b43legacy_ilt_write(dev, 0x5400 + i, 0x0020);
405 b43legacy_phy_agcsetup(dev);
407 if (is_bcm_board_vendor(dev) &&
408 (dev->dev->bus->boardinfo.type == 0x0416) &&
409 (dev->dev->bus->boardinfo.rev == 0x0017))
412 b43legacy_ilt_write(dev, 0x5001, 0x0002);
413 b43legacy_ilt_write(dev, 0x5002, 0x0001);
415 for (i = 0; i <= 0x20; i++)
416 b43legacy_ilt_write(dev, 0x1000 + i, 0x0820);
417 b43legacy_phy_agcsetup(dev);
418 b43legacy_phy_read(dev, 0x0400); /* dummy read */
419 b43legacy_phy_write(dev, 0x0403, 0x1000);
420 b43legacy_ilt_write(dev, 0x3C02, 0x000F);
421 b43legacy_ilt_write(dev, 0x3C03, 0x0014);
423 if (is_bcm_board_vendor(dev) &&
424 (dev->dev->bus->boardinfo.type == 0x0416) &&
425 (dev->dev->bus->boardinfo.rev == 0x0017))
428 b43legacy_ilt_write(dev, 0x0401, 0x0002);
429 b43legacy_ilt_write(dev, 0x0402, 0x0001);
433 /* Initialize the APHY portion of a GPHY. */
434 static void b43legacy_phy_inita(struct b43legacy_wldev *dev)
439 b43legacy_phy_setupg(dev);
440 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL)
441 b43legacy_phy_write(dev, 0x046E, 0x03CF);
444 static void b43legacy_phy_initb2(struct b43legacy_wldev *dev)
446 struct b43legacy_phy *phy = &dev->phy;
450 b43legacy_write16(dev, 0x03EC, 0x3F22);
451 b43legacy_phy_write(dev, 0x0020, 0x301C);
452 b43legacy_phy_write(dev, 0x0026, 0x0000);
453 b43legacy_phy_write(dev, 0x0030, 0x00C6);
454 b43legacy_phy_write(dev, 0x0088, 0x3E00);
456 for (offset = 0x0089; offset < 0x00A7; offset++) {
457 b43legacy_phy_write(dev, offset, val);
460 b43legacy_phy_write(dev, 0x03E4, 0x3000);
461 b43legacy_radio_selectchannel(dev, phy->channel, 0);
462 if (phy->radio_ver != 0x2050) {
463 b43legacy_radio_write16(dev, 0x0075, 0x0080);
464 b43legacy_radio_write16(dev, 0x0079, 0x0081);
466 b43legacy_radio_write16(dev, 0x0050, 0x0020);
467 b43legacy_radio_write16(dev, 0x0050, 0x0023);
468 if (phy->radio_ver == 0x2050) {
469 b43legacy_radio_write16(dev, 0x0050, 0x0020);
470 b43legacy_radio_write16(dev, 0x005A, 0x0070);
471 b43legacy_radio_write16(dev, 0x005B, 0x007B);
472 b43legacy_radio_write16(dev, 0x005C, 0x00B0);
473 b43legacy_radio_write16(dev, 0x007A, 0x000F);
474 b43legacy_phy_write(dev, 0x0038, 0x0677);
475 b43legacy_radio_init2050(dev);
477 b43legacy_phy_write(dev, 0x0014, 0x0080);
478 b43legacy_phy_write(dev, 0x0032, 0x00CA);
479 b43legacy_phy_write(dev, 0x0032, 0x00CC);
480 b43legacy_phy_write(dev, 0x0035, 0x07C2);
481 b43legacy_phy_lo_b_measure(dev);
482 b43legacy_phy_write(dev, 0x0026, 0xCC00);
483 if (phy->radio_ver != 0x2050)
484 b43legacy_phy_write(dev, 0x0026, 0xCE00);
485 b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x1000);
486 b43legacy_phy_write(dev, 0x002A, 0x88A3);
487 if (phy->radio_ver != 0x2050)
488 b43legacy_phy_write(dev, 0x002A, 0x88C2);
489 b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
490 b43legacy_phy_init_pctl(dev);
493 static void b43legacy_phy_initb4(struct b43legacy_wldev *dev)
495 struct b43legacy_phy *phy = &dev->phy;
499 b43legacy_write16(dev, 0x03EC, 0x3F22);
500 b43legacy_phy_write(dev, 0x0020, 0x301C);
501 b43legacy_phy_write(dev, 0x0026, 0x0000);
502 b43legacy_phy_write(dev, 0x0030, 0x00C6);
503 b43legacy_phy_write(dev, 0x0088, 0x3E00);
505 for (offset = 0x0089; offset < 0x00A7; offset++) {
506 b43legacy_phy_write(dev, offset, val);
509 b43legacy_phy_write(dev, 0x03E4, 0x3000);
510 b43legacy_radio_selectchannel(dev, phy->channel, 0);
511 if (phy->radio_ver != 0x2050) {
512 b43legacy_radio_write16(dev, 0x0075, 0x0080);
513 b43legacy_radio_write16(dev, 0x0079, 0x0081);
515 b43legacy_radio_write16(dev, 0x0050, 0x0020);
516 b43legacy_radio_write16(dev, 0x0050, 0x0023);
517 if (phy->radio_ver == 0x2050) {
518 b43legacy_radio_write16(dev, 0x0050, 0x0020);
519 b43legacy_radio_write16(dev, 0x005A, 0x0070);
520 b43legacy_radio_write16(dev, 0x005B, 0x007B);
521 b43legacy_radio_write16(dev, 0x005C, 0x00B0);
522 b43legacy_radio_write16(dev, 0x007A, 0x000F);
523 b43legacy_phy_write(dev, 0x0038, 0x0677);
524 b43legacy_radio_init2050(dev);
526 b43legacy_phy_write(dev, 0x0014, 0x0080);
527 b43legacy_phy_write(dev, 0x0032, 0x00CA);
528 if (phy->radio_ver == 0x2050)
529 b43legacy_phy_write(dev, 0x0032, 0x00E0);
530 b43legacy_phy_write(dev, 0x0035, 0x07C2);
532 b43legacy_phy_lo_b_measure(dev);
534 b43legacy_phy_write(dev, 0x0026, 0xCC00);
535 if (phy->radio_ver == 0x2050)
536 b43legacy_phy_write(dev, 0x0026, 0xCE00);
537 b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x1100);
538 b43legacy_phy_write(dev, 0x002A, 0x88A3);
539 if (phy->radio_ver == 0x2050)
540 b43legacy_phy_write(dev, 0x002A, 0x88C2);
541 b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
542 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
543 b43legacy_calc_nrssi_slope(dev);
544 b43legacy_calc_nrssi_threshold(dev);
546 b43legacy_phy_init_pctl(dev);
549 static void b43legacy_phy_initb5(struct b43legacy_wldev *dev)
551 struct b43legacy_phy *phy = &dev->phy;
556 if (phy->analog == 1)
557 b43legacy_radio_write16(dev, 0x007A,
558 b43legacy_radio_read16(dev, 0x007A)
560 if (!is_bcm_board_vendor(dev) &&
561 (dev->dev->bus->boardinfo.type != 0x0416)) {
563 for (offset = 0x00A8 ; offset < 0x00C7; offset++) {
564 b43legacy_phy_write(dev, offset, value);
568 b43legacy_phy_write(dev, 0x0035,
569 (b43legacy_phy_read(dev, 0x0035) & 0xF0FF)
571 if (phy->radio_ver == 0x2050)
572 b43legacy_phy_write(dev, 0x0038, 0x0667);
575 if (phy->radio_ver == 0x2050) {
576 b43legacy_radio_write16(dev, 0x007A,
577 b43legacy_radio_read16(dev, 0x007A)
579 b43legacy_radio_write16(dev, 0x0051,
580 b43legacy_radio_read16(dev, 0x0051)
583 b43legacy_write16(dev, B43legacy_MMIO_PHY_RADIO, 0x0000);
585 b43legacy_phy_write(dev, 0x0802, b43legacy_phy_read(dev, 0x0802)
587 b43legacy_phy_write(dev, 0x042B, b43legacy_phy_read(dev, 0x042B)
590 b43legacy_phy_write(dev, 0x001C, 0x186A);
592 b43legacy_phy_write(dev, 0x0013, (b43legacy_phy_read(dev,
593 0x0013) & 0x00FF) | 0x1900);
594 b43legacy_phy_write(dev, 0x0035, (b43legacy_phy_read(dev,
595 0x0035) & 0xFFC0) | 0x0064);
596 b43legacy_phy_write(dev, 0x005D, (b43legacy_phy_read(dev,
597 0x005D) & 0xFF80) | 0x000A);
600 if (dev->bad_frames_preempt)
601 b43legacy_phy_write(dev, B43legacy_PHY_RADIO_BITFIELD,
602 b43legacy_phy_read(dev,
603 B43legacy_PHY_RADIO_BITFIELD) | (1 << 11));
605 if (phy->analog == 1) {
606 b43legacy_phy_write(dev, 0x0026, 0xCE00);
607 b43legacy_phy_write(dev, 0x0021, 0x3763);
608 b43legacy_phy_write(dev, 0x0022, 0x1BC3);
609 b43legacy_phy_write(dev, 0x0023, 0x06F9);
610 b43legacy_phy_write(dev, 0x0024, 0x037E);
612 b43legacy_phy_write(dev, 0x0026, 0xCC00);
613 b43legacy_phy_write(dev, 0x0030, 0x00C6);
614 b43legacy_write16(dev, 0x03EC, 0x3F22);
616 if (phy->analog == 1)
617 b43legacy_phy_write(dev, 0x0020, 0x3E1C);
619 b43legacy_phy_write(dev, 0x0020, 0x301C);
621 if (phy->analog == 0)
622 b43legacy_write16(dev, 0x03E4, 0x3000);
624 old_channel = (phy->channel == 0xFF) ? 1 : phy->channel;
625 /* Force to channel 7, even if not supported. */
626 b43legacy_radio_selectchannel(dev, 7, 0);
628 if (phy->radio_ver != 0x2050) {
629 b43legacy_radio_write16(dev, 0x0075, 0x0080);
630 b43legacy_radio_write16(dev, 0x0079, 0x0081);
633 b43legacy_radio_write16(dev, 0x0050, 0x0020);
634 b43legacy_radio_write16(dev, 0x0050, 0x0023);
636 if (phy->radio_ver == 0x2050) {
637 b43legacy_radio_write16(dev, 0x0050, 0x0020);
638 b43legacy_radio_write16(dev, 0x005A, 0x0070);
641 b43legacy_radio_write16(dev, 0x005B, 0x007B);
642 b43legacy_radio_write16(dev, 0x005C, 0x00B0);
644 b43legacy_radio_write16(dev, 0x007A, b43legacy_radio_read16(dev,
647 b43legacy_radio_selectchannel(dev, old_channel, 0);
649 b43legacy_phy_write(dev, 0x0014, 0x0080);
650 b43legacy_phy_write(dev, 0x0032, 0x00CA);
651 b43legacy_phy_write(dev, 0x002A, 0x88A3);
653 b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
655 if (phy->radio_ver == 0x2050)
656 b43legacy_radio_write16(dev, 0x005D, 0x000D);
658 b43legacy_write16(dev, 0x03E4, (b43legacy_read16(dev, 0x03E4) &
662 static void b43legacy_phy_initb6(struct b43legacy_wldev *dev)
664 struct b43legacy_phy *phy = &dev->phy;
669 b43legacy_phy_write(dev, 0x003E, 0x817A);
670 b43legacy_radio_write16(dev, 0x007A,
671 (b43legacy_radio_read16(dev, 0x007A) | 0x0058));
672 if (phy->radio_rev == 4 ||
673 phy->radio_rev == 5) {
674 b43legacy_radio_write16(dev, 0x0051, 0x0037);
675 b43legacy_radio_write16(dev, 0x0052, 0x0070);
676 b43legacy_radio_write16(dev, 0x0053, 0x00B3);
677 b43legacy_radio_write16(dev, 0x0054, 0x009B);
678 b43legacy_radio_write16(dev, 0x005A, 0x0088);
679 b43legacy_radio_write16(dev, 0x005B, 0x0088);
680 b43legacy_radio_write16(dev, 0x005D, 0x0088);
681 b43legacy_radio_write16(dev, 0x005E, 0x0088);
682 b43legacy_radio_write16(dev, 0x007D, 0x0088);
683 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
684 B43legacy_UCODEFLAGS_OFFSET,
685 (b43legacy_shm_read32(dev,
686 B43legacy_SHM_SHARED,
687 B43legacy_UCODEFLAGS_OFFSET)
690 if (phy->radio_rev == 8) {
691 b43legacy_radio_write16(dev, 0x0051, 0x0000);
692 b43legacy_radio_write16(dev, 0x0052, 0x0040);
693 b43legacy_radio_write16(dev, 0x0053, 0x00B7);
694 b43legacy_radio_write16(dev, 0x0054, 0x0098);
695 b43legacy_radio_write16(dev, 0x005A, 0x0088);
696 b43legacy_radio_write16(dev, 0x005B, 0x006B);
697 b43legacy_radio_write16(dev, 0x005C, 0x000F);
698 if (dev->dev->bus->sprom.boardflags_lo & 0x8000) {
699 b43legacy_radio_write16(dev, 0x005D, 0x00FA);
700 b43legacy_radio_write16(dev, 0x005E, 0x00D8);
702 b43legacy_radio_write16(dev, 0x005D, 0x00F5);
703 b43legacy_radio_write16(dev, 0x005E, 0x00B8);
705 b43legacy_radio_write16(dev, 0x0073, 0x0003);
706 b43legacy_radio_write16(dev, 0x007D, 0x00A8);
707 b43legacy_radio_write16(dev, 0x007C, 0x0001);
708 b43legacy_radio_write16(dev, 0x007E, 0x0008);
711 for (offset = 0x0088; offset < 0x0098; offset++) {
712 b43legacy_phy_write(dev, offset, val);
716 for (offset = 0x0098; offset < 0x00A8; offset++) {
717 b43legacy_phy_write(dev, offset, val);
721 for (offset = 0x00A8; offset < 0x00C8; offset++) {
722 b43legacy_phy_write(dev, offset, (val & 0x3F3F));
725 if (phy->type == B43legacy_PHYTYPE_G) {
726 b43legacy_radio_write16(dev, 0x007A,
727 b43legacy_radio_read16(dev, 0x007A) |
729 b43legacy_radio_write16(dev, 0x0051,
730 b43legacy_radio_read16(dev, 0x0051) |
732 b43legacy_phy_write(dev, 0x0802,
733 b43legacy_phy_read(dev, 0x0802) | 0x0100);
734 b43legacy_phy_write(dev, 0x042B,
735 b43legacy_phy_read(dev, 0x042B) | 0x2000);
736 b43legacy_phy_write(dev, 0x5B, 0x0000);
737 b43legacy_phy_write(dev, 0x5C, 0x0000);
740 old_channel = phy->channel;
741 if (old_channel >= 8)
742 b43legacy_radio_selectchannel(dev, 1, 0);
744 b43legacy_radio_selectchannel(dev, 13, 0);
746 b43legacy_radio_write16(dev, 0x0050, 0x0020);
747 b43legacy_radio_write16(dev, 0x0050, 0x0023);
749 if (phy->radio_rev < 6 || phy->radio_rev == 8) {
750 b43legacy_radio_write16(dev, 0x007C,
751 (b43legacy_radio_read16(dev, 0x007C)
753 b43legacy_radio_write16(dev, 0x0050, 0x0020);
755 if (phy->radio_rev <= 2) {
756 b43legacy_radio_write16(dev, 0x007C, 0x0020);
757 b43legacy_radio_write16(dev, 0x005A, 0x0070);
758 b43legacy_radio_write16(dev, 0x005B, 0x007B);
759 b43legacy_radio_write16(dev, 0x005C, 0x00B0);
761 b43legacy_radio_write16(dev, 0x007A,
762 (b43legacy_radio_read16(dev,
763 0x007A) & 0x00F8) | 0x0007);
765 b43legacy_radio_selectchannel(dev, old_channel, 0);
767 b43legacy_phy_write(dev, 0x0014, 0x0200);
768 if (phy->radio_rev >= 6)
769 b43legacy_phy_write(dev, 0x002A, 0x88C2);
771 b43legacy_phy_write(dev, 0x002A, 0x8AC0);
772 b43legacy_phy_write(dev, 0x0038, 0x0668);
773 b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
774 if (phy->radio_rev <= 5)
775 b43legacy_phy_write(dev, 0x005D, (b43legacy_phy_read(dev,
776 0x005D) & 0xFF80) | 0x0003);
777 if (phy->radio_rev <= 2)
778 b43legacy_radio_write16(dev, 0x005D, 0x000D);
780 if (phy->analog == 4) {
781 b43legacy_write16(dev, 0x03E4, 0x0009);
782 b43legacy_phy_write(dev, 0x61, b43legacy_phy_read(dev, 0x61)
785 b43legacy_phy_write(dev, 0x0002, (b43legacy_phy_read(dev,
786 0x0002) & 0xFFC0) | 0x0004);
787 if (phy->type == B43legacy_PHYTYPE_G)
788 b43legacy_write16(dev, 0x03E6, 0x0);
789 if (phy->type == B43legacy_PHYTYPE_B) {
790 b43legacy_write16(dev, 0x03E6, 0x8140);
791 b43legacy_phy_write(dev, 0x0016, 0x0410);
792 b43legacy_phy_write(dev, 0x0017, 0x0820);
793 b43legacy_phy_write(dev, 0x0062, 0x0007);
794 b43legacy_radio_init2050(dev);
795 b43legacy_phy_lo_g_measure(dev);
796 if (dev->dev->bus->sprom.boardflags_lo &
797 B43legacy_BFL_RSSI) {
798 b43legacy_calc_nrssi_slope(dev);
799 b43legacy_calc_nrssi_threshold(dev);
801 b43legacy_phy_init_pctl(dev);
805 static void b43legacy_calc_loopback_gain(struct b43legacy_wldev *dev)
807 struct b43legacy_phy *phy = &dev->phy;
808 u16 backup_phy[15] = {0};
817 backup_phy[0] = b43legacy_phy_read(dev, 0x0429);
818 backup_phy[1] = b43legacy_phy_read(dev, 0x0001);
819 backup_phy[2] = b43legacy_phy_read(dev, 0x0811);
820 backup_phy[3] = b43legacy_phy_read(dev, 0x0812);
822 backup_phy[4] = b43legacy_phy_read(dev, 0x0814);
823 backup_phy[5] = b43legacy_phy_read(dev, 0x0815);
825 backup_phy[6] = b43legacy_phy_read(dev, 0x005A);
826 backup_phy[7] = b43legacy_phy_read(dev, 0x0059);
827 backup_phy[8] = b43legacy_phy_read(dev, 0x0058);
828 backup_phy[9] = b43legacy_phy_read(dev, 0x000A);
829 backup_phy[10] = b43legacy_phy_read(dev, 0x0003);
830 backup_phy[11] = b43legacy_phy_read(dev, 0x080F);
831 backup_phy[12] = b43legacy_phy_read(dev, 0x0810);
832 backup_phy[13] = b43legacy_phy_read(dev, 0x002B);
833 backup_phy[14] = b43legacy_phy_read(dev, 0x0015);
834 b43legacy_phy_read(dev, 0x002D); /* dummy read */
835 backup_bband = phy->bbatt;
836 backup_radio[0] = b43legacy_radio_read16(dev, 0x0052);
837 backup_radio[1] = b43legacy_radio_read16(dev, 0x0043);
838 backup_radio[2] = b43legacy_radio_read16(dev, 0x007A);
840 b43legacy_phy_write(dev, 0x0429,
841 b43legacy_phy_read(dev, 0x0429) & 0x3FFF);
842 b43legacy_phy_write(dev, 0x0001,
843 b43legacy_phy_read(dev, 0x0001) & 0x8000);
844 b43legacy_phy_write(dev, 0x0811,
845 b43legacy_phy_read(dev, 0x0811) | 0x0002);
846 b43legacy_phy_write(dev, 0x0812,
847 b43legacy_phy_read(dev, 0x0812) & 0xFFFD);
848 b43legacy_phy_write(dev, 0x0811,
849 b43legacy_phy_read(dev, 0x0811) | 0x0001);
850 b43legacy_phy_write(dev, 0x0812,
851 b43legacy_phy_read(dev, 0x0812) & 0xFFFE);
853 b43legacy_phy_write(dev, 0x0814,
854 b43legacy_phy_read(dev, 0x0814) | 0x0001);
855 b43legacy_phy_write(dev, 0x0815,
856 b43legacy_phy_read(dev, 0x0815) & 0xFFFE);
857 b43legacy_phy_write(dev, 0x0814,
858 b43legacy_phy_read(dev, 0x0814) | 0x0002);
859 b43legacy_phy_write(dev, 0x0815,
860 b43legacy_phy_read(dev, 0x0815) & 0xFFFD);
862 b43legacy_phy_write(dev, 0x0811, b43legacy_phy_read(dev, 0x0811) |
864 b43legacy_phy_write(dev, 0x0812, b43legacy_phy_read(dev, 0x0812) |
867 b43legacy_phy_write(dev, 0x0811, (b43legacy_phy_read(dev, 0x0811)
869 b43legacy_phy_write(dev, 0x0812, (b43legacy_phy_read(dev, 0x0812)
872 b43legacy_phy_write(dev, 0x005A, 0x0780);
873 b43legacy_phy_write(dev, 0x0059, 0xC810);
874 b43legacy_phy_write(dev, 0x0058, 0x000D);
875 if (phy->analog == 0)
876 b43legacy_phy_write(dev, 0x0003, 0x0122);
878 b43legacy_phy_write(dev, 0x000A,
879 b43legacy_phy_read(dev, 0x000A)
882 b43legacy_phy_write(dev, 0x0814,
883 b43legacy_phy_read(dev, 0x0814) | 0x0004);
884 b43legacy_phy_write(dev, 0x0815,
885 b43legacy_phy_read(dev, 0x0815) & 0xFFFB);
887 b43legacy_phy_write(dev, 0x0003,
888 (b43legacy_phy_read(dev, 0x0003)
890 if (phy->radio_ver == 0x2050 && phy->radio_rev == 2) {
891 b43legacy_radio_write16(dev, 0x0052, 0x0000);
892 b43legacy_radio_write16(dev, 0x0043,
893 (b43legacy_radio_read16(dev, 0x0043)
896 } else if (phy->radio_rev == 8) {
897 b43legacy_radio_write16(dev, 0x0043, 0x000F);
902 b43legacy_phy_set_baseband_attenuation(dev, 11);
905 b43legacy_phy_write(dev, 0x080F, 0xC020);
907 b43legacy_phy_write(dev, 0x080F, 0x8020);
908 b43legacy_phy_write(dev, 0x0810, 0x0000);
910 b43legacy_phy_write(dev, 0x002B,
911 (b43legacy_phy_read(dev, 0x002B)
913 b43legacy_phy_write(dev, 0x002B,
914 (b43legacy_phy_read(dev, 0x002B)
916 b43legacy_phy_write(dev, 0x0811,
917 b43legacy_phy_read(dev, 0x0811) | 0x0100);
918 b43legacy_phy_write(dev, 0x0812,
919 b43legacy_phy_read(dev, 0x0812) & 0xCFFF);
920 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_EXTLNA) {
922 b43legacy_phy_write(dev, 0x0811,
923 b43legacy_phy_read(dev, 0x0811)
925 b43legacy_phy_write(dev, 0x0812,
926 b43legacy_phy_read(dev, 0x0812)
930 b43legacy_radio_write16(dev, 0x007A,
931 b43legacy_radio_read16(dev, 0x007A)
934 for (i = 0; i < loop1_cnt; i++) {
935 b43legacy_radio_write16(dev, 0x0043, loop1_cnt);
936 b43legacy_phy_write(dev, 0x0812,
937 (b43legacy_phy_read(dev, 0x0812)
938 & 0xF0FF) | (i << 8));
939 b43legacy_phy_write(dev, 0x0015,
940 (b43legacy_phy_read(dev, 0x0015)
942 b43legacy_phy_write(dev, 0x0015,
943 (b43legacy_phy_read(dev, 0x0015)
946 if (b43legacy_phy_read(dev, 0x002D) >= 0x0DFC)
950 loop1_omitted = loop1_cnt - loop1_done;
953 if (loop1_done >= 8) {
954 b43legacy_phy_write(dev, 0x0812,
955 b43legacy_phy_read(dev, 0x0812)
957 for (i = loop1_done - 8; i < 16; i++) {
958 b43legacy_phy_write(dev, 0x0812,
959 (b43legacy_phy_read(dev, 0x0812)
960 & 0xF0FF) | (i << 8));
961 b43legacy_phy_write(dev, 0x0015,
962 (b43legacy_phy_read(dev, 0x0015)
964 b43legacy_phy_write(dev, 0x0015,
965 (b43legacy_phy_read(dev, 0x0015)
968 if (b43legacy_phy_read(dev, 0x002D) >= 0x0DFC)
974 b43legacy_phy_write(dev, 0x0814, backup_phy[4]);
975 b43legacy_phy_write(dev, 0x0815, backup_phy[5]);
977 b43legacy_phy_write(dev, 0x005A, backup_phy[6]);
978 b43legacy_phy_write(dev, 0x0059, backup_phy[7]);
979 b43legacy_phy_write(dev, 0x0058, backup_phy[8]);
980 b43legacy_phy_write(dev, 0x000A, backup_phy[9]);
981 b43legacy_phy_write(dev, 0x0003, backup_phy[10]);
982 b43legacy_phy_write(dev, 0x080F, backup_phy[11]);
983 b43legacy_phy_write(dev, 0x0810, backup_phy[12]);
984 b43legacy_phy_write(dev, 0x002B, backup_phy[13]);
985 b43legacy_phy_write(dev, 0x0015, backup_phy[14]);
987 b43legacy_phy_set_baseband_attenuation(dev, backup_bband);
989 b43legacy_radio_write16(dev, 0x0052, backup_radio[0]);
990 b43legacy_radio_write16(dev, 0x0043, backup_radio[1]);
991 b43legacy_radio_write16(dev, 0x007A, backup_radio[2]);
993 b43legacy_phy_write(dev, 0x0811, backup_phy[2] | 0x0003);
995 b43legacy_phy_write(dev, 0x0811, backup_phy[2]);
996 b43legacy_phy_write(dev, 0x0812, backup_phy[3]);
997 b43legacy_phy_write(dev, 0x0429, backup_phy[0]);
998 b43legacy_phy_write(dev, 0x0001, backup_phy[1]);
1000 phy->loopback_gain[0] = ((loop1_done * 6) - (loop1_omitted * 4)) - 11;
1001 phy->loopback_gain[1] = (24 - (3 * loop2_done)) * 2;
1004 static void b43legacy_phy_initg(struct b43legacy_wldev *dev)
1006 struct b43legacy_phy *phy = &dev->phy;
1010 b43legacy_phy_initb5(dev);
1012 b43legacy_phy_initb6(dev);
1013 if (phy->rev >= 2 || phy->gmode)
1014 b43legacy_phy_inita(dev);
1016 if (phy->rev >= 2) {
1017 b43legacy_phy_write(dev, 0x0814, 0x0000);
1018 b43legacy_phy_write(dev, 0x0815, 0x0000);
1020 if (phy->rev == 2) {
1021 b43legacy_phy_write(dev, 0x0811, 0x0000);
1022 b43legacy_phy_write(dev, 0x0015, 0x00C0);
1025 b43legacy_phy_write(dev, 0x0811, 0x0400);
1026 b43legacy_phy_write(dev, 0x0015, 0x00C0);
1028 if (phy->rev >= 2 || phy->gmode) {
1029 tmp = b43legacy_phy_read(dev, 0x0400) & 0xFF;
1030 if (tmp == 3 || tmp == 5) {
1031 b43legacy_phy_write(dev, 0x04C2, 0x1816);
1032 b43legacy_phy_write(dev, 0x04C3, 0x8006);
1034 b43legacy_phy_write(dev, 0x04CC,
1035 (b43legacy_phy_read(dev,
1039 b43legacy_phy_write(dev, 0x047E, 0x0078);
1041 if (phy->radio_rev == 8) {
1042 b43legacy_phy_write(dev, 0x0801, b43legacy_phy_read(dev, 0x0801)
1044 b43legacy_phy_write(dev, 0x043E, b43legacy_phy_read(dev, 0x043E)
1047 if (phy->rev >= 2 && phy->gmode)
1048 b43legacy_calc_loopback_gain(dev);
1049 if (phy->radio_rev != 8) {
1050 if (phy->initval == 0xFFFF)
1051 phy->initval = b43legacy_radio_init2050(dev);
1053 b43legacy_radio_write16(dev, 0x0078, phy->initval);
1055 if (phy->txctl2 == 0xFFFF)
1056 b43legacy_phy_lo_g_measure(dev);
1058 if (phy->radio_ver == 0x2050 && phy->radio_rev == 8)
1059 b43legacy_radio_write16(dev, 0x0052,
1060 (phy->txctl1 << 4) |
1063 b43legacy_radio_write16(dev, 0x0052,
1064 (b43legacy_radio_read16(dev,
1068 b43legacy_phy_write(dev, 0x0036,
1069 (b43legacy_phy_read(dev, 0x0036)
1070 & 0x0FFF) | (phy->txctl2 << 12));
1071 if (dev->dev->bus->sprom.boardflags_lo &
1072 B43legacy_BFL_PACTRL)
1073 b43legacy_phy_write(dev, 0x002E, 0x8075);
1075 b43legacy_phy_write(dev, 0x002E, 0x807F);
1077 b43legacy_phy_write(dev, 0x002F, 0x0101);
1079 b43legacy_phy_write(dev, 0x002F, 0x0202);
1081 if (phy->gmode || phy->rev >= 2) {
1082 b43legacy_phy_lo_adjust(dev, 0);
1083 b43legacy_phy_write(dev, 0x080F, 0x8078);
1086 if (!(dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI)) {
1087 /* The specs state to update the NRSSI LT with
1088 * the value 0x7FFFFFFF here. I think that is some weird
1089 * compiler optimization in the original driver.
1090 * Essentially, what we do here is resetting all NRSSI LT
1091 * entries to -32 (see the limit_value() in nrssi_hw_update())
1093 b43legacy_nrssi_hw_update(dev, 0xFFFF);
1094 b43legacy_calc_nrssi_threshold(dev);
1095 } else if (phy->gmode || phy->rev >= 2) {
1096 if (phy->nrssi[0] == -1000) {
1097 B43legacy_WARN_ON(phy->nrssi[1] != -1000);
1098 b43legacy_calc_nrssi_slope(dev);
1100 B43legacy_WARN_ON(phy->nrssi[1] == -1000);
1101 b43legacy_calc_nrssi_threshold(dev);
1104 if (phy->radio_rev == 8)
1105 b43legacy_phy_write(dev, 0x0805, 0x3230);
1106 b43legacy_phy_init_pctl(dev);
1107 if (dev->dev->bus->chip_id == 0x4306
1108 && dev->dev->bus->chip_package == 2) {
1109 b43legacy_phy_write(dev, 0x0429,
1110 b43legacy_phy_read(dev, 0x0429) & 0xBFFF);
1111 b43legacy_phy_write(dev, 0x04C3,
1112 b43legacy_phy_read(dev, 0x04C3) & 0x7FFF);
1116 static u16 b43legacy_phy_lo_b_r15_loop(struct b43legacy_wldev *dev)
1120 unsigned long flags;
1122 local_irq_save(flags);
1123 for (i = 0; i < 10; i++) {
1124 b43legacy_phy_write(dev, 0x0015, 0xAFA0);
1126 b43legacy_phy_write(dev, 0x0015, 0xEFA0);
1128 b43legacy_phy_write(dev, 0x0015, 0xFFA0);
1130 ret += b43legacy_phy_read(dev, 0x002C);
1132 local_irq_restore(flags);
1133 b43legacy_voluntary_preempt();
1138 void b43legacy_phy_lo_b_measure(struct b43legacy_wldev *dev)
1140 struct b43legacy_phy *phy = &dev->phy;
1141 u16 regstack[12] = { 0 };
1147 regstack[0] = b43legacy_phy_read(dev, 0x0015);
1148 regstack[1] = b43legacy_radio_read16(dev, 0x0052) & 0xFFF0;
1150 if (phy->radio_ver == 0x2053) {
1151 regstack[2] = b43legacy_phy_read(dev, 0x000A);
1152 regstack[3] = b43legacy_phy_read(dev, 0x002A);
1153 regstack[4] = b43legacy_phy_read(dev, 0x0035);
1154 regstack[5] = b43legacy_phy_read(dev, 0x0003);
1155 regstack[6] = b43legacy_phy_read(dev, 0x0001);
1156 regstack[7] = b43legacy_phy_read(dev, 0x0030);
1158 regstack[8] = b43legacy_radio_read16(dev, 0x0043);
1159 regstack[9] = b43legacy_radio_read16(dev, 0x007A);
1160 regstack[10] = b43legacy_read16(dev, 0x03EC);
1161 regstack[11] = b43legacy_radio_read16(dev, 0x0052) & 0x00F0;
1163 b43legacy_phy_write(dev, 0x0030, 0x00FF);
1164 b43legacy_write16(dev, 0x03EC, 0x3F3F);
1165 b43legacy_phy_write(dev, 0x0035, regstack[4] & 0xFF7F);
1166 b43legacy_radio_write16(dev, 0x007A, regstack[9] & 0xFFF0);
1168 b43legacy_phy_write(dev, 0x0015, 0xB000);
1169 b43legacy_phy_write(dev, 0x002B, 0x0004);
1171 if (phy->radio_ver == 0x2053) {
1172 b43legacy_phy_write(dev, 0x002B, 0x0203);
1173 b43legacy_phy_write(dev, 0x002A, 0x08A3);
1176 phy->minlowsig[0] = 0xFFFF;
1178 for (i = 0; i < 4; i++) {
1179 b43legacy_radio_write16(dev, 0x0052, regstack[1] | i);
1180 b43legacy_phy_lo_b_r15_loop(dev);
1182 for (i = 0; i < 10; i++) {
1183 b43legacy_radio_write16(dev, 0x0052, regstack[1] | i);
1184 mls = b43legacy_phy_lo_b_r15_loop(dev) / 10;
1185 if (mls < phy->minlowsig[0]) {
1186 phy->minlowsig[0] = mls;
1187 phy->minlowsigpos[0] = i;
1190 b43legacy_radio_write16(dev, 0x0052, regstack[1]
1191 | phy->minlowsigpos[0]);
1193 phy->minlowsig[1] = 0xFFFF;
1195 for (i = -4; i < 5; i += 2) {
1196 for (j = -4; j < 5; j += 2) {
1198 fval = (0x0100 * i) + j + 0x0100;
1200 fval = (0x0100 * i) + j;
1201 b43legacy_phy_write(dev, 0x002F, fval);
1202 mls = b43legacy_phy_lo_b_r15_loop(dev) / 10;
1203 if (mls < phy->minlowsig[1]) {
1204 phy->minlowsig[1] = mls;
1205 phy->minlowsigpos[1] = fval;
1209 phy->minlowsigpos[1] += 0x0101;
1211 b43legacy_phy_write(dev, 0x002F, phy->minlowsigpos[1]);
1212 if (phy->radio_ver == 0x2053) {
1213 b43legacy_phy_write(dev, 0x000A, regstack[2]);
1214 b43legacy_phy_write(dev, 0x002A, regstack[3]);
1215 b43legacy_phy_write(dev, 0x0035, regstack[4]);
1216 b43legacy_phy_write(dev, 0x0003, regstack[5]);
1217 b43legacy_phy_write(dev, 0x0001, regstack[6]);
1218 b43legacy_phy_write(dev, 0x0030, regstack[7]);
1220 b43legacy_radio_write16(dev, 0x0043, regstack[8]);
1221 b43legacy_radio_write16(dev, 0x007A, regstack[9]);
1223 b43legacy_radio_write16(dev, 0x0052,
1224 (b43legacy_radio_read16(dev, 0x0052)
1225 & 0x000F) | regstack[11]);
1227 b43legacy_write16(dev, 0x03EC, regstack[10]);
1229 b43legacy_phy_write(dev, 0x0015, regstack[0]);
1233 u16 b43legacy_phy_lo_g_deviation_subval(struct b43legacy_wldev *dev,
1236 struct b43legacy_phy *phy = &dev->phy;
1238 unsigned long flags;
1240 local_irq_save(flags);
1242 b43legacy_phy_write(dev, 0x15, 0xE300);
1244 b43legacy_phy_write(dev, 0x0812, control | 0x00B0);
1246 b43legacy_phy_write(dev, 0x0812, control | 0x00B2);
1248 b43legacy_phy_write(dev, 0x0812, control | 0x00B3);
1250 b43legacy_phy_write(dev, 0x0015, 0xF300);
1253 b43legacy_phy_write(dev, 0x0015, control | 0xEFA0);
1255 b43legacy_phy_write(dev, 0x0015, control | 0xEFE0);
1257 b43legacy_phy_write(dev, 0x0015, control | 0xFFE0);
1260 ret = b43legacy_phy_read(dev, 0x002D);
1261 local_irq_restore(flags);
1262 b43legacy_voluntary_preempt();
1267 static u32 b43legacy_phy_lo_g_singledeviation(struct b43legacy_wldev *dev,
1273 for (i = 0; i < 8; i++)
1274 ret += b43legacy_phy_lo_g_deviation_subval(dev, control);
1279 /* Write the LocalOscillator CONTROL */
1281 void b43legacy_lo_write(struct b43legacy_wldev *dev,
1282 struct b43legacy_lopair *pair)
1286 value = (u8)(pair->low);
1287 value |= ((u8)(pair->high)) << 8;
1289 #ifdef CONFIG_B43LEGACY_DEBUG
1291 if (pair->low < -8 || pair->low > 8 ||
1292 pair->high < -8 || pair->high > 8) {
1293 struct b43legacy_phy *phy = &dev->phy;
1294 b43legacydbg(dev->wl,
1295 "WARNING: Writing invalid LOpair "
1296 "(low: %d, high: %d, index: %lu)\n",
1297 pair->low, pair->high,
1298 (unsigned long)(pair - phy->_lo_pairs));
1303 b43legacy_phy_write(dev, B43legacy_PHY_G_LO_CONTROL, value);
1307 struct b43legacy_lopair *b43legacy_find_lopair(struct b43legacy_wldev *dev,
1312 static const u8 dict[10] = { 11, 10, 11, 12, 13, 12, 13, 12, 13, 12 };
1313 struct b43legacy_phy *phy = &dev->phy;
1317 B43legacy_WARN_ON(rfatt >= 10);
1320 return b43legacy_get_lopair(phy, rfatt, bbatt);
1321 return b43legacy_get_lopair(phy, dict[rfatt], bbatt);
1325 struct b43legacy_lopair *b43legacy_current_lopair(struct b43legacy_wldev *dev)
1327 struct b43legacy_phy *phy = &dev->phy;
1329 return b43legacy_find_lopair(dev, phy->bbatt,
1330 phy->rfatt, phy->txctl1);
1334 void b43legacy_phy_lo_adjust(struct b43legacy_wldev *dev, int fixed)
1336 struct b43legacy_lopair *pair;
1339 /* Use fixed values. Only for initialization. */
1340 pair = b43legacy_find_lopair(dev, 2, 3, 0);
1342 pair = b43legacy_current_lopair(dev);
1343 b43legacy_lo_write(dev, pair);
1346 static void b43legacy_phy_lo_g_measure_txctl2(struct b43legacy_wldev *dev)
1348 struct b43legacy_phy *phy = &dev->phy;
1354 b43legacy_radio_write16(dev, 0x0052, 0x0000);
1356 smallest = b43legacy_phy_lo_g_singledeviation(dev, 0);
1357 for (i = 0; i < 16; i++) {
1358 b43legacy_radio_write16(dev, 0x0052, i);
1360 tmp = b43legacy_phy_lo_g_singledeviation(dev, 0);
1361 if (tmp < smallest) {
1366 phy->txctl2 = txctl2;
1370 void b43legacy_phy_lo_g_state(struct b43legacy_wldev *dev,
1371 const struct b43legacy_lopair *in_pair,
1372 struct b43legacy_lopair *out_pair,
1375 static const struct b43legacy_lopair transitions[8] = {
1376 { .high = 1, .low = 1, },
1377 { .high = 1, .low = 0, },
1378 { .high = 1, .low = -1, },
1379 { .high = 0, .low = -1, },
1380 { .high = -1, .low = -1, },
1381 { .high = -1, .low = 0, },
1382 { .high = -1, .low = 1, },
1383 { .high = 0, .low = 1, },
1385 struct b43legacy_lopair lowest_transition = {
1386 .high = in_pair->high,
1387 .low = in_pair->low,
1389 struct b43legacy_lopair tmp_pair;
1390 struct b43legacy_lopair transition;
1397 u32 lowest_deviation;
1400 /* Note that in_pair and out_pair can point to the same pair.
1403 b43legacy_lo_write(dev, &lowest_transition);
1404 lowest_deviation = b43legacy_phy_lo_g_singledeviation(dev, r27);
1407 B43legacy_WARN_ON(!(state >= 0 && state <= 8));
1411 } else if (state % 2 == 0) {
1424 tmp_pair.high = lowest_transition.high;
1425 tmp_pair.low = lowest_transition.low;
1427 B43legacy_WARN_ON(!(j >= 1 && j <= 8));
1428 transition.high = tmp_pair.high +
1429 transitions[j - 1].high;
1430 transition.low = tmp_pair.low + transitions[j - 1].low;
1431 if ((abs(transition.low) < 9)
1432 && (abs(transition.high) < 9)) {
1433 b43legacy_lo_write(dev, &transition);
1434 tmp = b43legacy_phy_lo_g_singledeviation(dev,
1436 if (tmp < lowest_deviation) {
1437 lowest_deviation = tmp;
1441 lowest_transition.high =
1443 lowest_transition.low = transition.low;
1453 } while (i-- && found_lower);
1455 out_pair->high = lowest_transition.high;
1456 out_pair->low = lowest_transition.low;
1459 /* Set the baseband attenuation value on chip. */
1460 void b43legacy_phy_set_baseband_attenuation(struct b43legacy_wldev *dev,
1463 struct b43legacy_phy *phy = &dev->phy;
1466 if (phy->analog == 0) {
1467 value = (b43legacy_read16(dev, 0x03E6) & 0xFFF0);
1468 value |= (bbatt & 0x000F);
1469 b43legacy_write16(dev, 0x03E6, value);
1473 if (phy->analog > 1) {
1474 value = b43legacy_phy_read(dev, 0x0060) & 0xFFC3;
1475 value |= (bbatt << 2) & 0x003C;
1477 value = b43legacy_phy_read(dev, 0x0060) & 0xFF87;
1478 value |= (bbatt << 3) & 0x0078;
1480 b43legacy_phy_write(dev, 0x0060, value);
1483 /* http://bcm-specs.sipsolutions.net/LocalOscillator/Measure */
1484 void b43legacy_phy_lo_g_measure(struct b43legacy_wldev *dev)
1486 static const u8 pairorder[10] = { 3, 1, 5, 7, 9, 2, 0, 4, 6, 8 };
1487 const int is_initializing = (b43legacy_status(dev)
1488 < B43legacy_STAT_STARTED);
1489 struct b43legacy_phy *phy = &dev->phy;
1494 struct b43legacy_lopair control;
1495 struct b43legacy_lopair *tmp_control;
1497 u16 regstack[16] = { 0 };
1500 /* XXX: What are these? */
1504 oldchannel = phy->channel;
1507 regstack[0] = b43legacy_phy_read(dev, B43legacy_PHY_G_CRS);
1508 regstack[1] = b43legacy_phy_read(dev, 0x0802);
1509 b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]
1511 b43legacy_phy_write(dev, 0x0802, regstack[1] & 0xFFFC);
1513 regstack[3] = b43legacy_read16(dev, 0x03E2);
1514 b43legacy_write16(dev, 0x03E2, regstack[3] | 0x8000);
1515 regstack[4] = b43legacy_read16(dev, B43legacy_MMIO_CHANNEL_EXT);
1516 regstack[5] = b43legacy_phy_read(dev, 0x15);
1517 regstack[6] = b43legacy_phy_read(dev, 0x2A);
1518 regstack[7] = b43legacy_phy_read(dev, 0x35);
1519 regstack[8] = b43legacy_phy_read(dev, 0x60);
1520 regstack[9] = b43legacy_radio_read16(dev, 0x43);
1521 regstack[10] = b43legacy_radio_read16(dev, 0x7A);
1522 regstack[11] = b43legacy_radio_read16(dev, 0x52);
1524 regstack[12] = b43legacy_phy_read(dev, 0x0811);
1525 regstack[13] = b43legacy_phy_read(dev, 0x0812);
1526 regstack[14] = b43legacy_phy_read(dev, 0x0814);
1527 regstack[15] = b43legacy_phy_read(dev, 0x0815);
1529 b43legacy_radio_selectchannel(dev, 6, 0);
1531 b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]
1533 b43legacy_phy_write(dev, 0x0802, regstack[1] & 0xFFFC);
1534 b43legacy_dummy_transmission(dev);
1536 b43legacy_radio_write16(dev, 0x0043, 0x0006);
1538 b43legacy_phy_set_baseband_attenuation(dev, 2);
1540 b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x0000);
1541 b43legacy_phy_write(dev, 0x002E, 0x007F);
1542 b43legacy_phy_write(dev, 0x080F, 0x0078);
1543 b43legacy_phy_write(dev, 0x0035, regstack[7] & ~(1 << 7));
1544 b43legacy_radio_write16(dev, 0x007A, regstack[10] & 0xFFF0);
1545 b43legacy_phy_write(dev, 0x002B, 0x0203);
1546 b43legacy_phy_write(dev, 0x002A, 0x08A3);
1548 b43legacy_phy_write(dev, 0x0814, regstack[14] | 0x0003);
1549 b43legacy_phy_write(dev, 0x0815, regstack[15] & 0xFFFC);
1550 b43legacy_phy_write(dev, 0x0811, 0x01B3);
1551 b43legacy_phy_write(dev, 0x0812, 0x00B2);
1553 if (is_initializing)
1554 b43legacy_phy_lo_g_measure_txctl2(dev);
1555 b43legacy_phy_write(dev, 0x080F, 0x8078);
1560 for (h = 0; h < 10; h++) {
1561 /* Loop over each possible RadioAttenuation (0-9) */
1563 if (is_initializing) {
1567 } else if (((i % 2 == 1) && (oldi % 2 == 1)) ||
1568 ((i % 2 == 0) && (oldi % 2 == 0))) {
1569 tmp_control = b43legacy_get_lopair(phy, oldi,
1571 memcpy(&control, tmp_control, sizeof(control));
1573 tmp_control = b43legacy_get_lopair(phy, 3, 0);
1574 memcpy(&control, tmp_control, sizeof(control));
1577 /* Loop over each possible BasebandAttenuation/2 */
1578 for (j = 0; j < 4; j++) {
1579 if (is_initializing) {
1591 tmp_control = b43legacy_get_lopair(phy, i,
1593 if (!tmp_control->used)
1595 memcpy(&control, tmp_control, sizeof(control));
1599 b43legacy_radio_write16(dev, 0x43, i);
1600 b43legacy_radio_write16(dev, 0x52, phy->txctl2);
1602 b43legacy_voluntary_preempt();
1604 b43legacy_phy_set_baseband_attenuation(dev, j * 2);
1606 tmp = (regstack[10] & 0xFFF0);
1609 b43legacy_radio_write16(dev, 0x007A, tmp);
1611 tmp_control = b43legacy_get_lopair(phy, i, j * 2);
1612 b43legacy_phy_lo_g_state(dev, &control, tmp_control,
1617 /* Loop over each possible RadioAttenuation (10-13) */
1618 for (i = 10; i < 14; i++) {
1619 /* Loop over each possible BasebandAttenuation/2 */
1620 for (j = 0; j < 4; j++) {
1621 if (is_initializing) {
1622 tmp_control = b43legacy_get_lopair(phy, i - 9,
1624 memcpy(&control, tmp_control, sizeof(control));
1625 /* FIXME: The next line is wrong, as the
1626 * following if statement can never trigger. */
1627 tmp = (i - 9) * 2 + j - 5;
1638 tmp_control = b43legacy_get_lopair(phy, i - 9,
1640 if (!tmp_control->used)
1642 memcpy(&control, tmp_control, sizeof(control));
1646 b43legacy_radio_write16(dev, 0x43, i - 9);
1647 /* FIXME: shouldn't txctl1 be zero in the next line
1648 * and 3 in the loop above? */
1649 b43legacy_radio_write16(dev, 0x52,
1651 | (3/*txctl1*/ << 4));
1653 b43legacy_voluntary_preempt();
1655 b43legacy_phy_set_baseband_attenuation(dev, j * 2);
1657 tmp = (regstack[10] & 0xFFF0);
1660 b43legacy_radio_write16(dev, 0x7A, tmp);
1662 tmp_control = b43legacy_get_lopair(phy, i, j * 2);
1663 b43legacy_phy_lo_g_state(dev, &control, tmp_control,
1670 b43legacy_phy_write(dev, 0x0015, 0xE300);
1671 b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA0);
1673 b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA2);
1675 b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA3);
1676 b43legacy_voluntary_preempt();
1678 b43legacy_phy_write(dev, 0x0015, r27 | 0xEFA0);
1679 b43legacy_phy_lo_adjust(dev, is_initializing);
1680 b43legacy_phy_write(dev, 0x002E, 0x807F);
1682 b43legacy_phy_write(dev, 0x002F, 0x0202);
1684 b43legacy_phy_write(dev, 0x002F, 0x0101);
1685 b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, regstack[4]);
1686 b43legacy_phy_write(dev, 0x0015, regstack[5]);
1687 b43legacy_phy_write(dev, 0x002A, regstack[6]);
1688 b43legacy_phy_write(dev, 0x0035, regstack[7]);
1689 b43legacy_phy_write(dev, 0x0060, regstack[8]);
1690 b43legacy_radio_write16(dev, 0x0043, regstack[9]);
1691 b43legacy_radio_write16(dev, 0x007A, regstack[10]);
1692 regstack[11] &= 0x00F0;
1693 regstack[11] |= (b43legacy_radio_read16(dev, 0x52) & 0x000F);
1694 b43legacy_radio_write16(dev, 0x52, regstack[11]);
1695 b43legacy_write16(dev, 0x03E2, regstack[3]);
1697 b43legacy_phy_write(dev, 0x0811, regstack[12]);
1698 b43legacy_phy_write(dev, 0x0812, regstack[13]);
1699 b43legacy_phy_write(dev, 0x0814, regstack[14]);
1700 b43legacy_phy_write(dev, 0x0815, regstack[15]);
1701 b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]);
1702 b43legacy_phy_write(dev, 0x0802, regstack[1]);
1704 b43legacy_radio_selectchannel(dev, oldchannel, 1);
1706 #ifdef CONFIG_B43LEGACY_DEBUG
1708 /* Sanity check for all lopairs. */
1709 for (i = 0; i < B43legacy_LO_COUNT; i++) {
1710 tmp_control = phy->_lo_pairs + i;
1711 if (tmp_control->low < -8 || tmp_control->low > 8 ||
1712 tmp_control->high < -8 || tmp_control->high > 8)
1713 b43legacywarn(dev->wl,
1714 "WARNING: Invalid LOpair (low: %d, high:"
1715 " %d, index: %d)\n",
1716 tmp_control->low, tmp_control->high, i);
1719 #endif /* CONFIG_B43LEGACY_DEBUG */
1723 void b43legacy_phy_lo_mark_current_used(struct b43legacy_wldev *dev)
1725 struct b43legacy_lopair *pair;
1727 pair = b43legacy_current_lopair(dev);
1731 void b43legacy_phy_lo_mark_all_unused(struct b43legacy_wldev *dev)
1733 struct b43legacy_phy *phy = &dev->phy;
1734 struct b43legacy_lopair *pair;
1737 for (i = 0; i < B43legacy_LO_COUNT; i++) {
1738 pair = phy->_lo_pairs + i;
1743 /* http://bcm-specs.sipsolutions.net/EstimatePowerOut
1744 * This function converts a TSSI value to dBm in Q5.2
1746 static s8 b43legacy_phy_estimate_power_out(struct b43legacy_wldev *dev, s8 tssi)
1748 struct b43legacy_phy *phy = &dev->phy;
1752 tmp = phy->idle_tssi;
1754 tmp -= phy->savedpctlreg;
1756 switch (phy->type) {
1757 case B43legacy_PHYTYPE_B:
1758 case B43legacy_PHYTYPE_G:
1759 tmp = limit_value(tmp, 0x00, 0x3F);
1760 dbm = phy->tssi2dbm[tmp];
1763 B43legacy_BUG_ON(1);
1769 /* http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower */
1770 void b43legacy_phy_xmitpower(struct b43legacy_wldev *dev)
1772 struct b43legacy_phy *phy = &dev->phy;
1784 s16 radio_att_delta;
1785 s16 baseband_att_delta;
1786 s16 radio_attenuation;
1787 s16 baseband_attenuation;
1789 if (phy->savedpctlreg == 0xFFFF)
1791 if ((dev->dev->bus->boardinfo.type == 0x0416) &&
1792 is_bcm_board_vendor(dev))
1794 #ifdef CONFIG_B43LEGACY_DEBUG
1795 if (phy->manual_txpower_control)
1799 B43legacy_BUG_ON(!(phy->type == B43legacy_PHYTYPE_B ||
1800 phy->type == B43legacy_PHYTYPE_G));
1801 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x0058);
1802 v0 = (s8)(tmp & 0x00FF);
1803 v1 = (s8)((tmp & 0xFF00) >> 8);
1804 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x005A);
1805 v2 = (s8)(tmp & 0x00FF);
1806 v3 = (s8)((tmp & 0xFF00) >> 8);
1809 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F) {
1810 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1812 v0 = (s8)(tmp & 0x00FF);
1813 v1 = (s8)((tmp & 0xFF00) >> 8);
1814 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1816 v2 = (s8)(tmp & 0x00FF);
1817 v3 = (s8)((tmp & 0xFF00) >> 8);
1818 if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F)
1820 v0 = (v0 + 0x20) & 0x3F;
1821 v1 = (v1 + 0x20) & 0x3F;
1822 v2 = (v2 + 0x20) & 0x3F;
1823 v3 = (v3 + 0x20) & 0x3F;
1826 b43legacy_radio_clear_tssi(dev);
1828 average = (v0 + v1 + v2 + v3 + 2) / 4;
1830 if (tmp && (b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x005E)
1834 estimated_pwr = b43legacy_phy_estimate_power_out(dev, average);
1836 max_pwr = dev->dev->bus->sprom.maxpwr_bg;
1838 if ((dev->dev->bus->sprom.boardflags_lo
1839 & B43legacy_BFL_PACTRL) &&
1840 (phy->type == B43legacy_PHYTYPE_G))
1842 if (unlikely(max_pwr <= 0)) {
1843 b43legacywarn(dev->wl, "Invalid max-TX-power value in SPROM."
1845 max_pwr = 74; /* fake it */
1846 dev->dev->bus->sprom.maxpwr_bg = max_pwr;
1849 /* Use regulatory information to get the maximum power.
1850 * In the absence of such data from mac80211, we will use 20 dBm, which
1851 * is the value for the EU, US, Canada, and most of the world.
1852 * The regulatory maximum is reduced by the antenna gain (from sprom)
1853 * and 1.5 dBm (a safety factor??). The result is in Q5.2 format
1854 * which accounts for the factor of 4 */
1855 #define REG_MAX_PWR 20
1856 max_pwr = min(REG_MAX_PWR * 4
1857 - dev->dev->bus->sprom.antenna_gain.ghz24.a0
1860 /* find the desired power in Q5.2 - power_level is in dBm
1861 * and limit it - max_pwr is already in Q5.2 */
1862 desired_pwr = limit_value(phy->power_level << 2, 0, max_pwr);
1863 if (b43legacy_debug(dev, B43legacy_DBG_XMITPOWER))
1864 b43legacydbg(dev->wl, "Current TX power output: " Q52_FMT
1865 " dBm, Desired TX power output: " Q52_FMT
1866 " dBm\n", Q52_ARG(estimated_pwr),
1867 Q52_ARG(desired_pwr));
1868 /* Check if we need to adjust the current power. The factor of 2 is
1870 pwr_adjust = (desired_pwr - estimated_pwr) / 2;
1871 /* RF attenuation delta
1872 * The minus sign is because lower attenuation => more power */
1873 radio_att_delta = -(pwr_adjust + 7) >> 3;
1874 /* Baseband attenuation delta */
1875 baseband_att_delta = -(pwr_adjust >> 1) - (4 * radio_att_delta);
1876 /* Do we need to adjust anything? */
1877 if ((radio_att_delta == 0) && (baseband_att_delta == 0)) {
1878 b43legacy_phy_lo_mark_current_used(dev);
1882 /* Calculate the new attenuation values. */
1883 baseband_attenuation = phy->bbatt;
1884 baseband_attenuation += baseband_att_delta;
1885 radio_attenuation = phy->rfatt;
1886 radio_attenuation += radio_att_delta;
1888 /* Get baseband and radio attenuation values into permitted ranges.
1889 * baseband 0-11, radio 0-9.
1890 * Radio attenuation affects power level 4 times as much as baseband.
1892 if (radio_attenuation < 0) {
1893 baseband_attenuation -= (4 * -radio_attenuation);
1894 radio_attenuation = 0;
1895 } else if (radio_attenuation > 9) {
1896 baseband_attenuation += (4 * (radio_attenuation - 9));
1897 radio_attenuation = 9;
1899 while (baseband_attenuation < 0 && radio_attenuation > 0) {
1900 baseband_attenuation += 4;
1901 radio_attenuation--;
1903 while (baseband_attenuation > 11 && radio_attenuation < 9) {
1904 baseband_attenuation -= 4;
1905 radio_attenuation++;
1908 baseband_attenuation = limit_value(baseband_attenuation, 0, 11);
1910 txpower = phy->txctl1;
1911 if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 2)) {
1912 if (radio_attenuation <= 1) {
1915 radio_attenuation += 2;
1916 baseband_attenuation += 2;
1917 } else if (dev->dev->bus->sprom.boardflags_lo
1918 & B43legacy_BFL_PACTRL) {
1919 baseband_attenuation += 4 *
1920 (radio_attenuation - 2);
1921 radio_attenuation = 2;
1923 } else if (radio_attenuation > 4 && txpower != 0) {
1925 if (baseband_attenuation < 3) {
1926 radio_attenuation -= 3;
1927 baseband_attenuation += 2;
1929 radio_attenuation -= 2;
1930 baseband_attenuation -= 2;
1934 /* Save the control values */
1935 phy->txctl1 = txpower;
1936 baseband_attenuation = limit_value(baseband_attenuation, 0, 11);
1937 radio_attenuation = limit_value(radio_attenuation, 0, 9);
1938 phy->rfatt = radio_attenuation;
1939 phy->bbatt = baseband_attenuation;
1941 /* Adjust the hardware */
1942 b43legacy_phy_lock(dev);
1943 b43legacy_radio_lock(dev);
1944 b43legacy_radio_set_txpower_bg(dev, baseband_attenuation,
1945 radio_attenuation, txpower);
1946 b43legacy_phy_lo_mark_current_used(dev);
1947 b43legacy_radio_unlock(dev);
1948 b43legacy_phy_unlock(dev);
1952 s32 b43legacy_tssi2dbm_ad(s32 num, s32 den)
1957 return (num+den/2)/den;
1961 s8 b43legacy_tssi2dbm_entry(s8 entry [], u8 index, s16 pab0, s16 pab1, s16 pab2)
1970 m1 = b43legacy_tssi2dbm_ad(16 * pab0 + index * pab1, 32);
1971 m2 = max(b43legacy_tssi2dbm_ad(32768 + index * pab2, 256), 1);
1975 q = b43legacy_tssi2dbm_ad(f * 4096 -
1976 b43legacy_tssi2dbm_ad(m2 * f, 16) *
1981 } while (delta >= 2);
1982 entry[index] = limit_value(b43legacy_tssi2dbm_ad(m1 * f, 8192),
1987 /* http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table */
1988 int b43legacy_phy_init_tssi2dbm_table(struct b43legacy_wldev *dev)
1990 struct b43legacy_phy *phy = &dev->phy;
1997 B43legacy_WARN_ON(!(phy->type == B43legacy_PHYTYPE_B ||
1998 phy->type == B43legacy_PHYTYPE_G));
1999 pab0 = (s16)(dev->dev->bus->sprom.pa0b0);
2000 pab1 = (s16)(dev->dev->bus->sprom.pa0b1);
2001 pab2 = (s16)(dev->dev->bus->sprom.pa0b2);
2003 if ((dev->dev->bus->chip_id == 0x4301) && (phy->radio_ver != 0x2050)) {
2004 phy->idle_tssi = 0x34;
2005 phy->tssi2dbm = b43legacy_tssi2dbm_b_table;
2009 if (pab0 != 0 && pab1 != 0 && pab2 != 0 &&
2010 pab0 != -1 && pab1 != -1 && pab2 != -1) {
2011 /* The pabX values are set in SPROM. Use them. */
2012 if ((s8)dev->dev->bus->sprom.itssi_bg != 0 &&
2013 (s8)dev->dev->bus->sprom.itssi_bg != -1)
2014 phy->idle_tssi = (s8)(dev->dev->bus->sprom.
2017 phy->idle_tssi = 62;
2018 dyn_tssi2dbm = kmalloc(64, GFP_KERNEL);
2019 if (dyn_tssi2dbm == NULL) {
2020 b43legacyerr(dev->wl, "Could not allocate memory "
2021 "for tssi2dbm table\n");
2024 for (idx = 0; idx < 64; idx++)
2025 if (b43legacy_tssi2dbm_entry(dyn_tssi2dbm, idx, pab0,
2027 phy->tssi2dbm = NULL;
2028 b43legacyerr(dev->wl, "Could not generate "
2029 "tssi2dBm table\n");
2030 kfree(dyn_tssi2dbm);
2033 phy->tssi2dbm = dyn_tssi2dbm;
2034 phy->dyn_tssi_tbl = 1;
2036 /* pabX values not set in SPROM. */
2037 switch (phy->type) {
2038 case B43legacy_PHYTYPE_B:
2039 phy->idle_tssi = 0x34;
2040 phy->tssi2dbm = b43legacy_tssi2dbm_b_table;
2042 case B43legacy_PHYTYPE_G:
2043 phy->idle_tssi = 0x34;
2044 phy->tssi2dbm = b43legacy_tssi2dbm_g_table;
2052 int b43legacy_phy_init(struct b43legacy_wldev *dev)
2054 struct b43legacy_phy *phy = &dev->phy;
2057 switch (phy->type) {
2058 case B43legacy_PHYTYPE_B:
2061 b43legacy_phy_initb2(dev);
2065 b43legacy_phy_initb4(dev);
2069 b43legacy_phy_initb5(dev);
2073 b43legacy_phy_initb6(dev);
2078 case B43legacy_PHYTYPE_G:
2079 b43legacy_phy_initg(dev);
2084 b43legacyerr(dev->wl, "Unknown PHYTYPE found\n");
2089 void b43legacy_phy_set_antenna_diversity(struct b43legacy_wldev *dev)
2091 struct b43legacy_phy *phy = &dev->phy;
2097 antennadiv = phy->antenna_diversity;
2099 if (antennadiv == 0xFFFF)
2101 B43legacy_WARN_ON(antennadiv > 3);
2103 ucodeflags = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,
2104 B43legacy_UCODEFLAGS_OFFSET);
2105 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
2106 B43legacy_UCODEFLAGS_OFFSET,
2107 ucodeflags & ~B43legacy_UCODEFLAG_AUTODIV);
2109 switch (phy->type) {
2110 case B43legacy_PHYTYPE_G:
2113 if (antennadiv == 2)
2114 value = (3/*automatic*/ << 7);
2116 value = (antennadiv << 7);
2117 b43legacy_phy_write(dev, offset + 1,
2118 (b43legacy_phy_read(dev, offset + 1)
2121 if (antennadiv >= 2) {
2122 if (antennadiv == 2)
2123 value = (antennadiv << 7);
2125 value = (0/*force0*/ << 7);
2126 b43legacy_phy_write(dev, offset + 0x2B,
2127 (b43legacy_phy_read(dev,
2132 if (phy->type == B43legacy_PHYTYPE_G) {
2133 if (antennadiv >= 2)
2134 b43legacy_phy_write(dev, 0x048C,
2135 b43legacy_phy_read(dev,
2138 b43legacy_phy_write(dev, 0x048C,
2139 b43legacy_phy_read(dev,
2141 if (phy->rev >= 2) {
2142 b43legacy_phy_write(dev, 0x0461,
2143 b43legacy_phy_read(dev,
2145 b43legacy_phy_write(dev, 0x04AD,
2146 (b43legacy_phy_read(dev,
2148 & 0x00FF) | 0x0015);
2150 b43legacy_phy_write(dev, 0x0427,
2153 b43legacy_phy_write(dev, 0x0427,
2154 (b43legacy_phy_read(dev, 0x0427)
2155 & 0x00FF) | 0x0008);
2156 } else if (phy->rev >= 6)
2157 b43legacy_phy_write(dev, 0x049B, 0x00DC);
2160 b43legacy_phy_write(dev, 0x002B,
2161 (b43legacy_phy_read(dev,
2165 b43legacy_phy_write(dev, 0x0061,
2166 b43legacy_phy_read(dev,
2168 if (phy->rev == 3) {
2169 b43legacy_phy_write(dev, 0x0093,
2171 b43legacy_phy_write(dev, 0x0027,
2174 b43legacy_phy_write(dev, 0x0093,
2176 b43legacy_phy_write(dev, 0x0027,
2177 (b43legacy_phy_read(dev, 0x0027)
2178 & 0x00FF) | 0x0008);
2183 case B43legacy_PHYTYPE_B:
2184 if (dev->dev->id.revision == 2)
2185 value = (3/*automatic*/ << 7);
2187 value = (antennadiv << 7);
2188 b43legacy_phy_write(dev, 0x03E2,
2189 (b43legacy_phy_read(dev, 0x03E2)
2193 B43legacy_WARN_ON(1);
2196 if (antennadiv >= 2) {
2197 ucodeflags = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,
2198 B43legacy_UCODEFLAGS_OFFSET);
2199 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
2200 B43legacy_UCODEFLAGS_OFFSET,
2201 ucodeflags | B43legacy_UCODEFLAG_AUTODIV);
2204 phy->antenna_diversity = antennadiv;
2207 /* Set the PowerSavingControlBits.
2209 * 0 => unset the bit
2211 * -1 => calculate the bit
2213 void b43legacy_power_saving_ctl_bits(struct b43legacy_wldev *dev,
2214 int bit25, int bit26)
2219 /* FIXME: Force 25 to off and 26 to on for now: */
2224 /* TODO: If powersave is not off and FIXME is not set and we
2225 * are not in adhoc and thus is not an AP and we arei
2226 * associated, set bit 25 */
2229 /* TODO: If the device is awake or this is an AP, or we are
2230 * scanning, or FIXME, or we are associated, or FIXME,
2231 * or the latest PS-Poll packet sent was successful,
2234 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2236 status |= B43legacy_MACCTL_HWPS;
2238 status &= ~B43legacy_MACCTL_HWPS;
2240 status |= B43legacy_MACCTL_AWAKE;
2242 status &= ~B43legacy_MACCTL_AWAKE;
2243 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
2244 if (bit26 && dev->dev->id.revision >= 5) {
2245 for (i = 0; i < 100; i++) {
2246 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,