3 Broadcom B43 wireless driver
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; see the file COPYING. If not, write to
24 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
25 Boston, MA 02110-1301, USA.
29 #include "phy_common.h"
40 int b43_phy_allocate(struct b43_wldev *dev)
42 struct b43_phy *phy = &(dev->phy);
49 phy->ops = &b43_phyops_a;
52 phy->ops = &b43_phyops_g;
55 #ifdef CONFIG_B43_PHY_N
56 phy->ops = &b43_phyops_n;
60 #ifdef CONFIG_B43_PHY_LP
61 phy->ops = &b43_phyops_lp;
65 #ifdef CONFIG_B43_PHY_HT
66 phy->ops = &b43_phyops_ht;
70 #ifdef CONFIG_B43_PHY_LCN
71 phy->ops = &b43_phyops_lcn;
75 if (B43_WARN_ON(!phy->ops))
78 err = phy->ops->allocate(dev);
85 void b43_phy_free(struct b43_wldev *dev)
87 dev->phy.ops->free(dev);
91 int b43_phy_init(struct b43_wldev *dev)
93 struct b43_phy *phy = &dev->phy;
94 const struct b43_phy_operations *ops = phy->ops;
97 phy->channel = ops->get_default_chan(dev);
99 phy->ops->switch_analog(dev, true);
100 b43_software_rfkill(dev, false);
102 err = ops->init(dev);
104 b43err(dev->wl, "PHY init failed\n");
107 phy->do_full_init = false;
109 /* Make sure to switch hardware and firmware (SHM) to
110 * the default channel. */
111 err = b43_switch_channel(dev, ops->get_default_chan(dev));
113 b43err(dev->wl, "PHY init: Channel switch to default failed\n");
120 phy->do_full_init = true;
124 b43_software_rfkill(dev, true);
129 void b43_phy_exit(struct b43_wldev *dev)
131 const struct b43_phy_operations *ops = dev->phy.ops;
133 b43_software_rfkill(dev, true);
134 dev->phy.do_full_init = true;
139 bool b43_has_hardware_pctl(struct b43_wldev *dev)
141 if (!dev->phy.hardware_power_control)
143 if (!dev->phy.ops->supports_hwpctl)
145 return dev->phy.ops->supports_hwpctl(dev);
148 void b43_radio_lock(struct b43_wldev *dev)
153 B43_WARN_ON(dev->phy.radio_locked);
154 dev->phy.radio_locked = true;
157 macctl = b43_read32(dev, B43_MMIO_MACCTL);
158 macctl |= B43_MACCTL_RADIOLOCK;
159 b43_write32(dev, B43_MMIO_MACCTL, macctl);
160 /* Commit the write and wait for the firmware
161 * to finish any radio register access. */
162 b43_read32(dev, B43_MMIO_MACCTL);
166 void b43_radio_unlock(struct b43_wldev *dev)
171 B43_WARN_ON(!dev->phy.radio_locked);
172 dev->phy.radio_locked = false;
175 /* Commit any write */
176 b43_read16(dev, B43_MMIO_PHY_VER);
178 macctl = b43_read32(dev, B43_MMIO_MACCTL);
179 macctl &= ~B43_MACCTL_RADIOLOCK;
180 b43_write32(dev, B43_MMIO_MACCTL, macctl);
183 void b43_phy_lock(struct b43_wldev *dev)
186 B43_WARN_ON(dev->phy.phy_locked);
187 dev->phy.phy_locked = true;
189 B43_WARN_ON(dev->dev->core_rev < 3);
191 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
192 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
195 void b43_phy_unlock(struct b43_wldev *dev)
198 B43_WARN_ON(!dev->phy.phy_locked);
199 dev->phy.phy_locked = false;
201 B43_WARN_ON(dev->dev->core_rev < 3);
203 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
204 b43_power_saving_ctl_bits(dev, 0);
207 static inline void assert_mac_suspended(struct b43_wldev *dev)
211 if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
212 (dev->mac_suspended <= 0)) {
213 b43dbg(dev->wl, "PHY/RADIO register access with "
219 u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
221 assert_mac_suspended(dev);
222 return dev->phy.ops->radio_read(dev, reg);
225 void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
227 assert_mac_suspended(dev);
228 dev->phy.ops->radio_write(dev, reg, value);
231 void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
233 b43_radio_write16(dev, offset,
234 b43_radio_read16(dev, offset) & mask);
237 void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
239 b43_radio_write16(dev, offset,
240 b43_radio_read16(dev, offset) | set);
243 void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
245 b43_radio_write16(dev, offset,
246 (b43_radio_read16(dev, offset) & mask) | set);
249 bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
250 u16 value, int delay, int timeout)
255 for (i = 0; i < timeout; i += delay) {
256 val = b43_radio_read(dev, offset);
257 if ((val & mask) == value)
264 u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
266 assert_mac_suspended(dev);
267 dev->phy.writes_counter = 0;
268 return dev->phy.ops->phy_read(dev, reg);
271 void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
273 assert_mac_suspended(dev);
274 dev->phy.ops->phy_write(dev, reg, value);
275 if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
276 b43_read16(dev, B43_MMIO_PHY_VER);
277 dev->phy.writes_counter = 0;
281 void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
283 assert_mac_suspended(dev);
284 dev->phy.ops->phy_write(dev, destreg,
285 dev->phy.ops->phy_read(dev, srcreg));
288 void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
290 if (dev->phy.ops->phy_maskset) {
291 assert_mac_suspended(dev);
292 dev->phy.ops->phy_maskset(dev, offset, mask, 0);
294 b43_phy_write(dev, offset,
295 b43_phy_read(dev, offset) & mask);
299 void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
301 if (dev->phy.ops->phy_maskset) {
302 assert_mac_suspended(dev);
303 dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
305 b43_phy_write(dev, offset,
306 b43_phy_read(dev, offset) | set);
310 void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
312 if (dev->phy.ops->phy_maskset) {
313 assert_mac_suspended(dev);
314 dev->phy.ops->phy_maskset(dev, offset, mask, set);
316 b43_phy_write(dev, offset,
317 (b43_phy_read(dev, offset) & mask) | set);
321 void b43_phy_put_into_reset(struct b43_wldev *dev)
325 switch (dev->dev->bus_type) {
326 #ifdef CONFIG_B43_BCMA
328 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
329 tmp &= ~B43_BCMA_IOCTL_GMODE;
330 tmp |= B43_BCMA_IOCTL_PHY_RESET;
331 tmp |= BCMA_IOCTL_FGC;
332 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
335 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
336 tmp &= ~BCMA_IOCTL_FGC;
337 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
341 #ifdef CONFIG_B43_SSB
343 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
344 tmp &= ~B43_TMSLOW_GMODE;
345 tmp |= B43_TMSLOW_PHYRESET;
346 tmp |= SSB_TMSLOW_FGC;
347 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
348 usleep_range(1000, 2000);
350 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
351 tmp &= ~SSB_TMSLOW_FGC;
352 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
353 usleep_range(1000, 2000);
360 void b43_phy_take_out_of_reset(struct b43_wldev *dev)
364 switch (dev->dev->bus_type) {
365 #ifdef CONFIG_B43_BCMA
367 /* Unset reset bit (with forcing clock) */
368 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
369 tmp &= ~B43_BCMA_IOCTL_PHY_RESET;
370 tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
371 tmp |= BCMA_IOCTL_FGC;
372 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
375 /* Do not force clock anymore */
376 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
377 tmp &= ~BCMA_IOCTL_FGC;
378 tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
379 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
383 #ifdef CONFIG_B43_SSB
385 /* Unset reset bit (with forcing clock) */
386 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
387 tmp &= ~B43_TMSLOW_PHYRESET;
388 tmp &= ~B43_TMSLOW_PHYCLKEN;
389 tmp |= SSB_TMSLOW_FGC;
390 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
391 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
392 usleep_range(1000, 2000);
394 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
395 tmp &= ~SSB_TMSLOW_FGC;
396 tmp |= B43_TMSLOW_PHYCLKEN;
397 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
398 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
399 usleep_range(1000, 2000);
405 int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
407 struct b43_phy *phy = &(dev->phy);
408 u16 channelcookie, savedcookie;
411 if (new_channel == B43_DEFAULT_CHANNEL)
412 new_channel = phy->ops->get_default_chan(dev);
414 /* First we set the channel radio code to prevent the
415 * firmware from sending ghost packets.
417 channelcookie = new_channel;
418 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
419 channelcookie |= B43_SHM_SH_CHAN_5GHZ;
420 /* FIXME: set 40Mhz flag if required */
422 channelcookie |= B43_SHM_SH_CHAN_40MHZ;
423 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
424 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
426 /* Now try to switch the PHY hardware channel. */
427 err = phy->ops->switch_channel(dev, new_channel);
429 goto err_restore_cookie;
431 dev->phy.channel = new_channel;
432 /* Wait for the radio to tune to the channel and stabilize. */
438 b43_shm_write16(dev, B43_SHM_SHARED,
439 B43_SHM_SH_CHAN, savedcookie);
444 void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
446 struct b43_phy *phy = &dev->phy;
448 b43_mac_suspend(dev);
449 phy->ops->software_rfkill(dev, blocked);
450 phy->radio_on = !blocked;
455 * b43_phy_txpower_adjust_work - TX power workqueue.
457 * Workqueue for updating the TX power parameters in hardware.
459 void b43_phy_txpower_adjust_work(struct work_struct *work)
461 struct b43_wl *wl = container_of(work, struct b43_wl,
462 txpower_adjust_work);
463 struct b43_wldev *dev;
465 mutex_lock(&wl->mutex);
466 dev = wl->current_dev;
468 if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
469 dev->phy.ops->adjust_txpower(dev);
471 mutex_unlock(&wl->mutex);
474 void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
476 struct b43_phy *phy = &dev->phy;
477 unsigned long now = jiffies;
478 enum b43_txpwr_result result;
480 if (!(flags & B43_TXPWR_IGNORE_TIME)) {
481 /* Check if it's time for a TXpower check. */
482 if (time_before(now, phy->next_txpwr_check_time))
483 return; /* Not yet */
485 /* The next check will be needed in two seconds, or later. */
486 phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
488 if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
489 (dev->dev->board_type == SSB_BOARD_BU4306))
490 return; /* No software txpower adjustment needed */
492 result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
493 if (result == B43_TXPWR_RES_DONE)
494 return; /* We are done. */
495 B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
496 B43_WARN_ON(phy->ops->adjust_txpower == NULL);
498 /* We must adjust the transmission power in hardware.
499 * Schedule b43_phy_txpower_adjust_work(). */
500 ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
503 int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
505 const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
506 unsigned int a, b, c, d;
507 unsigned int average;
510 tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
512 b = (tmp >> 8) & 0xFF;
513 c = (tmp >> 16) & 0xFF;
514 d = (tmp >> 24) & 0xFF;
515 if (a == 0 || a == B43_TSSI_MAX ||
516 b == 0 || b == B43_TSSI_MAX ||
517 c == 0 || c == B43_TSSI_MAX ||
518 d == 0 || d == B43_TSSI_MAX)
520 /* The values are OK. Clear them. */
521 tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
522 (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
523 b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
532 /* Get the average of the values with 0.5 added to each value. */
533 average = (a + b + c + d + 2) / 4;
535 /* Adjust for CCK-boost */
536 if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
538 average = (average >= 13) ? (average - 13) : 0;
544 void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
546 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
550 bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
552 return (channel_type == NL80211_CHAN_HT40MINUS ||
553 channel_type == NL80211_CHAN_HT40PLUS);
556 /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
557 void b43_phy_force_clock(struct b43_wldev *dev, bool force)
561 WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
562 dev->phy.type != B43_PHYTYPE_HT);
564 switch (dev->dev->bus_type) {
565 #ifdef CONFIG_B43_BCMA
567 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
569 tmp |= BCMA_IOCTL_FGC;
571 tmp &= ~BCMA_IOCTL_FGC;
572 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
575 #ifdef CONFIG_B43_SSB
577 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
579 tmp |= SSB_TMSLOW_FGC;
581 tmp &= ~SSB_TMSLOW_FGC;
582 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
588 /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
589 struct b43_c32 b43_cordic(int theta)
591 static const u32 arctg[] = {
592 2949120, 1740967, 919879, 466945, 234379, 117304,
593 58666, 29335, 14668, 7334, 3667, 1833,
594 917, 458, 229, 115, 57, 29,
600 struct b43_c32 ret = { .i = 39797, .q = 0, };
602 while (theta > (180 << 16))
603 theta -= (360 << 16);
604 while (theta < -(180 << 16))
605 theta += (360 << 16);
607 if (theta > (90 << 16)) {
608 theta -= (180 << 16);
610 } else if (theta < -(90 << 16)) {
611 theta += (180 << 16);
615 for (i = 0; i <= 17; i++) {
617 tmp = ret.i - (ret.q >> i);
622 tmp = ret.i + (ret.q >> i);