blob: 9705950f059a3d5a08e8d78f87e489af4ce5d0b7 [file] [log] [blame]
Michael Bueschef1a6282008-08-27 18:53:02 +02001/*
2
3 Broadcom B43 wireless driver
4 Common PHY routines
5
6 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
7 Copyright (c) 2005-2007 Stefano Brivio <stefano.brivio@polimi.it>
8 Copyright (c) 2005-2008 Michael Buesch <mb@bu3sch.de>
9 Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
10 Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
11
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.
16
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.
21
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.
26
27*/
28
29#include "phy_common.h"
30#include "phy_g.h"
31#include "phy_a.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020032#include "phy_n.h"
Michael Buesche63e4362008-08-30 10:55:48 +020033#include "phy_lp.h"
Rafał Miłeckid7520b12011-06-13 16:20:06 +020034#include "phy_ht.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020035#include "b43.h"
36#include "main.h"
37
38
Michael Bueschfb111372008-09-02 13:00:34 +020039int b43_phy_allocate(struct b43_wldev *dev)
Michael Bueschef1a6282008-08-27 18:53:02 +020040{
41 struct b43_phy *phy = &(dev->phy);
42 int err;
43
44 phy->ops = NULL;
45
46 switch (phy->type) {
47 case B43_PHYTYPE_A:
48 phy->ops = &b43_phyops_a;
49 break;
50 case B43_PHYTYPE_G:
51 phy->ops = &b43_phyops_g;
52 break;
53 case B43_PHYTYPE_N:
Rafał Miłecki692d2c02010-12-07 21:56:00 +010054#ifdef CONFIG_B43_PHY_N
Michael Bueschef1a6282008-08-27 18:53:02 +020055 phy->ops = &b43_phyops_n;
56#endif
57 break;
58 case B43_PHYTYPE_LP:
Michael Buesche63e4362008-08-30 10:55:48 +020059#ifdef CONFIG_B43_PHY_LP
60 phy->ops = &b43_phyops_lp;
61#endif
Michael Bueschef1a6282008-08-27 18:53:02 +020062 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +020063 case B43_PHYTYPE_HT:
64#ifdef CONFIG_B43_PHY_HT
65 phy->ops = &b43_phyops_ht;
66#endif
67 break;
Michael Bueschef1a6282008-08-27 18:53:02 +020068 }
69 if (B43_WARN_ON(!phy->ops))
70 return -ENODEV;
71
72 err = phy->ops->allocate(dev);
73 if (err)
74 phy->ops = NULL;
75
76 return err;
77}
78
Michael Bueschfb111372008-09-02 13:00:34 +020079void b43_phy_free(struct b43_wldev *dev)
80{
81 dev->phy.ops->free(dev);
82 dev->phy.ops = NULL;
83}
84
Michael Bueschef1a6282008-08-27 18:53:02 +020085int b43_phy_init(struct b43_wldev *dev)
86{
87 struct b43_phy *phy = &dev->phy;
88 const struct b43_phy_operations *ops = phy->ops;
89 int err;
90
91 phy->channel = ops->get_default_chan(dev);
92
Johannes Berg19d337d2009-06-02 13:01:37 +020093 ops->software_rfkill(dev, false);
Michael Bueschef1a6282008-08-27 18:53:02 +020094 err = ops->init(dev);
95 if (err) {
96 b43err(dev->wl, "PHY init failed\n");
97 goto err_block_rf;
98 }
99 /* Make sure to switch hardware and firmware (SHM) to
100 * the default channel. */
101 err = b43_switch_channel(dev, ops->get_default_chan(dev));
102 if (err) {
103 b43err(dev->wl, "PHY init: Channel switch to default failed\n");
104 goto err_phy_exit;
105 }
106
107 return 0;
108
109err_phy_exit:
110 if (ops->exit)
111 ops->exit(dev);
112err_block_rf:
Johannes Berg19d337d2009-06-02 13:01:37 +0200113 ops->software_rfkill(dev, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200114
115 return err;
116}
117
118void b43_phy_exit(struct b43_wldev *dev)
119{
120 const struct b43_phy_operations *ops = dev->phy.ops;
121
Johannes Berg19d337d2009-06-02 13:01:37 +0200122 ops->software_rfkill(dev, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200123 if (ops->exit)
124 ops->exit(dev);
125}
126
127bool b43_has_hardware_pctl(struct b43_wldev *dev)
128{
129 if (!dev->phy.hardware_power_control)
130 return 0;
131 if (!dev->phy.ops->supports_hwpctl)
132 return 0;
133 return dev->phy.ops->supports_hwpctl(dev);
134}
135
136void b43_radio_lock(struct b43_wldev *dev)
137{
138 u32 macctl;
139
Michael Buesch591f3dc2009-03-31 12:27:32 +0200140#if B43_DEBUG
141 B43_WARN_ON(dev->phy.radio_locked);
142 dev->phy.radio_locked = 1;
143#endif
144
Michael Bueschef1a6282008-08-27 18:53:02 +0200145 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200146 macctl |= B43_MACCTL_RADIOLOCK;
147 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesch591f3dc2009-03-31 12:27:32 +0200148 /* Commit the write and wait for the firmware
149 * to finish any radio register access. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200150 b43_read32(dev, B43_MMIO_MACCTL);
151 udelay(10);
152}
153
154void b43_radio_unlock(struct b43_wldev *dev)
155{
156 u32 macctl;
157
Michael Buesch591f3dc2009-03-31 12:27:32 +0200158#if B43_DEBUG
159 B43_WARN_ON(!dev->phy.radio_locked);
160 dev->phy.radio_locked = 0;
161#endif
162
Michael Bueschef1a6282008-08-27 18:53:02 +0200163 /* Commit any write */
164 b43_read16(dev, B43_MMIO_PHY_VER);
165 /* unlock */
166 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200167 macctl &= ~B43_MACCTL_RADIOLOCK;
168 b43_write32(dev, B43_MMIO_MACCTL, macctl);
169}
170
171void b43_phy_lock(struct b43_wldev *dev)
172{
173#if B43_DEBUG
174 B43_WARN_ON(dev->phy.phy_locked);
175 dev->phy.phy_locked = 1;
176#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200177 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200178
Johannes Berg05c914f2008-09-11 00:01:58 +0200179 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200180 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
181}
182
183void b43_phy_unlock(struct b43_wldev *dev)
184{
185#if B43_DEBUG
186 B43_WARN_ON(!dev->phy.phy_locked);
187 dev->phy.phy_locked = 0;
188#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200189 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200190
Johannes Berg05c914f2008-09-11 00:01:58 +0200191 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200192 b43_power_saving_ctl_bits(dev, 0);
193}
194
Michael Bueschd10d0e52008-12-18 22:13:39 +0100195static inline void assert_mac_suspended(struct b43_wldev *dev)
196{
197 if (!B43_DEBUG)
198 return;
199 if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
200 (dev->mac_suspended <= 0)) {
201 b43dbg(dev->wl, "PHY/RADIO register access with "
202 "enabled MAC.\n");
203 dump_stack();
204 }
205}
206
Michael Bueschef1a6282008-08-27 18:53:02 +0200207u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
208{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100209 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200210 return dev->phy.ops->radio_read(dev, reg);
211}
212
213void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
214{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100215 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200216 dev->phy.ops->radio_write(dev, reg, value);
217}
218
219void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
220{
221 b43_radio_write16(dev, offset,
222 b43_radio_read16(dev, offset) & mask);
223}
224
225void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
226{
227 b43_radio_write16(dev, offset,
228 b43_radio_read16(dev, offset) | set);
229}
230
231void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
232{
233 b43_radio_write16(dev, offset,
234 (b43_radio_read16(dev, offset) & mask) | set);
235}
236
237u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
238{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100239 assert_mac_suspended(dev);
Rafał Miłecki15518082010-12-07 09:42:07 +0100240 dev->phy.writes_counter = 0;
Michael Bueschef1a6282008-08-27 18:53:02 +0200241 return dev->phy.ops->phy_read(dev, reg);
242}
243
244void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
245{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100246 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200247 dev->phy.ops->phy_write(dev, reg, value);
Rafał Miłecki15518082010-12-07 09:42:07 +0100248 if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
249 b43_read16(dev, B43_MMIO_PHY_VER);
250 dev->phy.writes_counter = 0;
251 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200252}
253
Gábor Stefanik738f0f42009-08-03 01:28:12 +0200254void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
255{
256 assert_mac_suspended(dev);
257 dev->phy.ops->phy_write(dev, destreg,
258 dev->phy.ops->phy_read(dev, srcreg));
259}
260
Michael Bueschef1a6282008-08-27 18:53:02 +0200261void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
262{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200263 if (dev->phy.ops->phy_maskset) {
264 assert_mac_suspended(dev);
265 dev->phy.ops->phy_maskset(dev, offset, mask, 0);
266 } else {
267 b43_phy_write(dev, offset,
268 b43_phy_read(dev, offset) & mask);
269 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200270}
271
272void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
273{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200274 if (dev->phy.ops->phy_maskset) {
275 assert_mac_suspended(dev);
276 dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
277 } else {
278 b43_phy_write(dev, offset,
279 b43_phy_read(dev, offset) | set);
280 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200281}
282
283void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
284{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200285 if (dev->phy.ops->phy_maskset) {
286 assert_mac_suspended(dev);
287 dev->phy.ops->phy_maskset(dev, offset, mask, set);
288 } else {
289 b43_phy_write(dev, offset,
290 (b43_phy_read(dev, offset) & mask) | set);
291 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200292}
293
294int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
295{
296 struct b43_phy *phy = &(dev->phy);
297 u16 channelcookie, savedcookie;
298 int err;
299
300 if (new_channel == B43_DEFAULT_CHANNEL)
301 new_channel = phy->ops->get_default_chan(dev);
302
303 /* First we set the channel radio code to prevent the
304 * firmware from sending ghost packets.
305 */
306 channelcookie = new_channel;
307 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Rafał Miłecki106cb092010-10-06 07:50:07 +0200308 channelcookie |= B43_SHM_SH_CHAN_5GHZ;
309 /* FIXME: set 40Mhz flag if required */
310 if (0)
311 channelcookie |= B43_SHM_SH_CHAN_40MHZ;
Michael Bueschef1a6282008-08-27 18:53:02 +0200312 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
313 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
314
315 /* Now try to switch the PHY hardware channel. */
316 err = phy->ops->switch_channel(dev, new_channel);
317 if (err)
318 goto err_restore_cookie;
319
320 dev->phy.channel = new_channel;
321 /* Wait for the radio to tune to the channel and stabilize. */
322 msleep(8);
323
324 return 0;
325
326err_restore_cookie:
327 b43_shm_write16(dev, B43_SHM_SHARED,
328 B43_SHM_SH_CHAN, savedcookie);
329
330 return err;
331}
332
Johannes Berg19d337d2009-06-02 13:01:37 +0200333void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
Michael Bueschef1a6282008-08-27 18:53:02 +0200334{
335 struct b43_phy *phy = &dev->phy;
336
Michael Bueschb929ecf72008-12-19 18:40:00 +0100337 b43_mac_suspend(dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200338 phy->ops->software_rfkill(dev, blocked);
339 phy->radio_on = !blocked;
Michael Bueschb929ecf72008-12-19 18:40:00 +0100340 b43_mac_enable(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200341}
Michael Buesch18c8ade2008-08-28 19:33:40 +0200342
343/**
344 * b43_phy_txpower_adjust_work - TX power workqueue.
345 *
346 * Workqueue for updating the TX power parameters in hardware.
347 */
348void b43_phy_txpower_adjust_work(struct work_struct *work)
349{
350 struct b43_wl *wl = container_of(work, struct b43_wl,
351 txpower_adjust_work);
352 struct b43_wldev *dev;
353
354 mutex_lock(&wl->mutex);
355 dev = wl->current_dev;
356
357 if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
358 dev->phy.ops->adjust_txpower(dev);
359
360 mutex_unlock(&wl->mutex);
361}
362
Michael Buesch18c8ade2008-08-28 19:33:40 +0200363void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
364{
365 struct b43_phy *phy = &dev->phy;
366 unsigned long now = jiffies;
367 enum b43_txpwr_result result;
368
369 if (!(flags & B43_TXPWR_IGNORE_TIME)) {
370 /* Check if it's time for a TXpower check. */
371 if (time_before(now, phy->next_txpwr_check_time))
372 return; /* Not yet */
373 }
374 /* The next check will be needed in two seconds, or later. */
375 phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
376
Rafał Miłecki79d22322011-05-18 02:06:42 +0200377 if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
378 (dev->dev->board_type == SSB_BOARD_BU4306))
Michael Buesch18c8ade2008-08-28 19:33:40 +0200379 return; /* No software txpower adjustment needed */
380
381 result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
382 if (result == B43_TXPWR_RES_DONE)
383 return; /* We are done. */
384 B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
385 B43_WARN_ON(phy->ops->adjust_txpower == NULL);
386
387 /* We must adjust the transmission power in hardware.
388 * Schedule b43_phy_txpower_adjust_work(). */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400389 ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +0200390}
391
392int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
393{
394 const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
395 unsigned int a, b, c, d;
396 unsigned int average;
397 u32 tmp;
398
399 tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
400 a = tmp & 0xFF;
401 b = (tmp >> 8) & 0xFF;
402 c = (tmp >> 16) & 0xFF;
403 d = (tmp >> 24) & 0xFF;
404 if (a == 0 || a == B43_TSSI_MAX ||
405 b == 0 || b == B43_TSSI_MAX ||
406 c == 0 || c == B43_TSSI_MAX ||
407 d == 0 || d == B43_TSSI_MAX)
408 return -ENOENT;
409 /* The values are OK. Clear them. */
410 tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
411 (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
412 b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
413
414 if (is_ofdm) {
415 a = (a + 32) & 0x3F;
416 b = (b + 32) & 0x3F;
417 c = (c + 32) & 0x3F;
418 d = (d + 32) & 0x3F;
419 }
420
421 /* Get the average of the values with 0.5 added to each value. */
422 average = (a + b + c + d + 2) / 4;
423 if (is_ofdm) {
424 /* Adjust for CCK-boost */
425 if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO)
426 & B43_HF_CCKBOOST)
427 average = (average >= 13) ? (average - 13) : 0;
428 }
429
430 return average;
431}
Michael Bueschcb24f572008-09-03 12:12:20 +0200432
433void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
434{
435 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
436}
Rafał Miłecki98650452010-01-25 18:59:59 +0100437
Rafał Miłeckiabc1f7c2010-12-07 21:55:58 +0100438
439bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
440{
441 return (channel_type == NL80211_CHAN_HT40MINUS ||
442 channel_type == NL80211_CHAN_HT40PLUS);
443}
444
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100445/* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
Rafał Miłecki98650452010-01-25 18:59:59 +0100446struct b43_c32 b43_cordic(int theta)
447{
Joe Perches5b4bc642010-11-20 18:38:56 -0800448 static const u32 arctg[] = {
449 2949120, 1740967, 919879, 466945, 234379, 117304,
450 58666, 29335, 14668, 7334, 3667, 1833,
451 917, 458, 229, 115, 57, 29,
452 };
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100453 u8 i;
454 s32 tmp;
455 s8 signx = 1;
456 u32 angle = 0;
Rafał Miłecki98650452010-01-25 18:59:59 +0100457 struct b43_c32 ret = { .i = 39797, .q = 0, };
458
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100459 while (theta > (180 << 16))
460 theta -= (360 << 16);
461 while (theta < -(180 << 16))
462 theta += (360 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100463
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100464 if (theta > (90 << 16)) {
465 theta -= (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100466 signx = -1;
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100467 } else if (theta < -(90 << 16)) {
468 theta += (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100469 signx = -1;
470 }
471
472 for (i = 0; i <= 17; i++) {
473 if (theta > angle) {
474 tmp = ret.i - (ret.q >> i);
475 ret.q += ret.i >> i;
476 ret.i = tmp;
477 angle += arctg[i];
478 } else {
479 tmp = ret.i + (ret.q >> i);
480 ret.q -= ret.i >> i;
481 ret.i = tmp;
482 angle -= arctg[i];
483 }
484 }
485
486 ret.i *= signx;
487 ret.q *= signx;
488
489 return ret;
490}