blob: 26e390f9e291c8b07a425ba6401a505f6b9fd38a [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>
Michael Büscheb032b92011-07-04 20:50:05 +02008 Copyright (c) 2005-2008 Michael Buesch <m@bues.ch>
Michael Bueschef1a6282008-08-27 18:53:02 +02009 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"
Rafał Miłecki58eb7ff2011-07-07 18:58:25 +020035#include "phy_lcn.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020036#include "b43.h"
37#include "main.h"
38
39
Michael Bueschfb111372008-09-02 13:00:34 +020040int b43_phy_allocate(struct b43_wldev *dev)
Michael Bueschef1a6282008-08-27 18:53:02 +020041{
42 struct b43_phy *phy = &(dev->phy);
43 int err;
44
45 phy->ops = NULL;
46
47 switch (phy->type) {
48 case B43_PHYTYPE_A:
49 phy->ops = &b43_phyops_a;
50 break;
51 case B43_PHYTYPE_G:
52 phy->ops = &b43_phyops_g;
53 break;
54 case B43_PHYTYPE_N:
Rafał Miłecki692d2c02010-12-07 21:56:00 +010055#ifdef CONFIG_B43_PHY_N
Michael Bueschef1a6282008-08-27 18:53:02 +020056 phy->ops = &b43_phyops_n;
57#endif
58 break;
59 case B43_PHYTYPE_LP:
Michael Buesche63e4362008-08-30 10:55:48 +020060#ifdef CONFIG_B43_PHY_LP
61 phy->ops = &b43_phyops_lp;
62#endif
Michael Bueschef1a6282008-08-27 18:53:02 +020063 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +020064 case B43_PHYTYPE_HT:
65#ifdef CONFIG_B43_PHY_HT
66 phy->ops = &b43_phyops_ht;
67#endif
68 break;
Rafał Miłecki58eb7ff2011-07-07 18:58:25 +020069 case B43_PHYTYPE_LCN:
70#ifdef CONFIG_B43_PHY_LCN
71 phy->ops = &b43_phyops_lcn;
72#endif
73 break;
Michael Bueschef1a6282008-08-27 18:53:02 +020074 }
75 if (B43_WARN_ON(!phy->ops))
76 return -ENODEV;
77
78 err = phy->ops->allocate(dev);
79 if (err)
80 phy->ops = NULL;
81
82 return err;
83}
84
Michael Bueschfb111372008-09-02 13:00:34 +020085void b43_phy_free(struct b43_wldev *dev)
86{
87 dev->phy.ops->free(dev);
88 dev->phy.ops = NULL;
89}
90
Michael Bueschef1a6282008-08-27 18:53:02 +020091int b43_phy_init(struct b43_wldev *dev)
92{
93 struct b43_phy *phy = &dev->phy;
94 const struct b43_phy_operations *ops = phy->ops;
95 int err;
96
97 phy->channel = ops->get_default_chan(dev);
98
Rafał Miłeckia6316e22014-04-22 13:54:36 +020099 b43_software_rfkill(dev, false);
Michael Bueschef1a6282008-08-27 18:53:02 +0200100 err = ops->init(dev);
101 if (err) {
102 b43err(dev->wl, "PHY init failed\n");
103 goto err_block_rf;
104 }
105 /* Make sure to switch hardware and firmware (SHM) to
106 * the default channel. */
107 err = b43_switch_channel(dev, ops->get_default_chan(dev));
108 if (err) {
109 b43err(dev->wl, "PHY init: Channel switch to default failed\n");
110 goto err_phy_exit;
111 }
112
113 return 0;
114
115err_phy_exit:
116 if (ops->exit)
117 ops->exit(dev);
118err_block_rf:
Rafał Miłeckia6316e22014-04-22 13:54:36 +0200119 b43_software_rfkill(dev, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200120
121 return err;
122}
123
124void b43_phy_exit(struct b43_wldev *dev)
125{
126 const struct b43_phy_operations *ops = dev->phy.ops;
127
Rafał Miłeckia6316e22014-04-22 13:54:36 +0200128 b43_software_rfkill(dev, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200129 if (ops->exit)
130 ops->exit(dev);
131}
132
133bool b43_has_hardware_pctl(struct b43_wldev *dev)
134{
135 if (!dev->phy.hardware_power_control)
Zhao, Gang1a2b2502014-02-16 22:31:38 +0800136 return false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200137 if (!dev->phy.ops->supports_hwpctl)
Zhao, Gang1a2b2502014-02-16 22:31:38 +0800138 return false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200139 return dev->phy.ops->supports_hwpctl(dev);
140}
141
142void b43_radio_lock(struct b43_wldev *dev)
143{
144 u32 macctl;
145
Michael Buesch591f3dc2009-03-31 12:27:32 +0200146#if B43_DEBUG
147 B43_WARN_ON(dev->phy.radio_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000148 dev->phy.radio_locked = true;
Michael Buesch591f3dc2009-03-31 12:27:32 +0200149#endif
150
Michael Bueschef1a6282008-08-27 18:53:02 +0200151 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200152 macctl |= B43_MACCTL_RADIOLOCK;
153 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesch591f3dc2009-03-31 12:27:32 +0200154 /* Commit the write and wait for the firmware
155 * to finish any radio register access. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200156 b43_read32(dev, B43_MMIO_MACCTL);
157 udelay(10);
158}
159
160void b43_radio_unlock(struct b43_wldev *dev)
161{
162 u32 macctl;
163
Michael Buesch591f3dc2009-03-31 12:27:32 +0200164#if B43_DEBUG
165 B43_WARN_ON(!dev->phy.radio_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000166 dev->phy.radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +0200167#endif
168
Michael Bueschef1a6282008-08-27 18:53:02 +0200169 /* Commit any write */
170 b43_read16(dev, B43_MMIO_PHY_VER);
171 /* unlock */
172 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200173 macctl &= ~B43_MACCTL_RADIOLOCK;
174 b43_write32(dev, B43_MMIO_MACCTL, macctl);
175}
176
177void b43_phy_lock(struct b43_wldev *dev)
178{
179#if B43_DEBUG
180 B43_WARN_ON(dev->phy.phy_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000181 dev->phy.phy_locked = true;
Michael Bueschef1a6282008-08-27 18:53:02 +0200182#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200183 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200184
Johannes Berg05c914f2008-09-11 00:01:58 +0200185 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200186 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
187}
188
189void b43_phy_unlock(struct b43_wldev *dev)
190{
191#if B43_DEBUG
192 B43_WARN_ON(!dev->phy.phy_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000193 dev->phy.phy_locked = false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200194#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200195 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200196
Johannes Berg05c914f2008-09-11 00:01:58 +0200197 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200198 b43_power_saving_ctl_bits(dev, 0);
199}
200
Michael Bueschd10d0e52008-12-18 22:13:39 +0100201static inline void assert_mac_suspended(struct b43_wldev *dev)
202{
203 if (!B43_DEBUG)
204 return;
205 if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
206 (dev->mac_suspended <= 0)) {
207 b43dbg(dev->wl, "PHY/RADIO register access with "
208 "enabled MAC.\n");
209 dump_stack();
210 }
211}
212
Michael Bueschef1a6282008-08-27 18:53:02 +0200213u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
214{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100215 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200216 return dev->phy.ops->radio_read(dev, reg);
217}
218
219void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
220{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100221 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200222 dev->phy.ops->radio_write(dev, reg, value);
223}
224
225void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
226{
227 b43_radio_write16(dev, offset,
228 b43_radio_read16(dev, offset) & mask);
229}
230
231void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
232{
233 b43_radio_write16(dev, offset,
234 b43_radio_read16(dev, offset) | set);
235}
236
237void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
238{
239 b43_radio_write16(dev, offset,
240 (b43_radio_read16(dev, offset) & mask) | set);
241}
242
Rafał Miłecki0f941772012-07-26 00:07:37 +0200243bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
244 u16 value, int delay, int timeout)
245{
246 u16 val;
247 int i;
248
249 for (i = 0; i < timeout; i += delay) {
250 val = b43_radio_read(dev, offset);
251 if ((val & mask) == value)
252 return true;
253 udelay(delay);
254 }
255 return false;
256}
257
Michael Bueschef1a6282008-08-27 18:53:02 +0200258u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
259{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100260 assert_mac_suspended(dev);
Rafał Miłecki15518082010-12-07 09:42:07 +0100261 dev->phy.writes_counter = 0;
Michael Bueschef1a6282008-08-27 18:53:02 +0200262 return dev->phy.ops->phy_read(dev, reg);
263}
264
265void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
266{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100267 assert_mac_suspended(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200268 dev->phy.ops->phy_write(dev, reg, value);
Rafał Miłecki15518082010-12-07 09:42:07 +0100269 if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
270 b43_read16(dev, B43_MMIO_PHY_VER);
271 dev->phy.writes_counter = 0;
272 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200273}
274
Gábor Stefanik738f0f42009-08-03 01:28:12 +0200275void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
276{
277 assert_mac_suspended(dev);
278 dev->phy.ops->phy_write(dev, destreg,
279 dev->phy.ops->phy_read(dev, srcreg));
280}
281
Michael Bueschef1a6282008-08-27 18:53:02 +0200282void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
283{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200284 if (dev->phy.ops->phy_maskset) {
285 assert_mac_suspended(dev);
286 dev->phy.ops->phy_maskset(dev, offset, mask, 0);
287 } else {
288 b43_phy_write(dev, offset,
289 b43_phy_read(dev, offset) & mask);
290 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200291}
292
293void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
294{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200295 if (dev->phy.ops->phy_maskset) {
296 assert_mac_suspended(dev);
297 dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
298 } else {
299 b43_phy_write(dev, offset,
300 b43_phy_read(dev, offset) | set);
301 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200302}
303
304void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
305{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200306 if (dev->phy.ops->phy_maskset) {
307 assert_mac_suspended(dev);
308 dev->phy.ops->phy_maskset(dev, offset, mask, set);
309 } else {
310 b43_phy_write(dev, offset,
311 (b43_phy_read(dev, offset) & mask) | set);
312 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200313}
314
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200315void b43_phy_put_into_reset(struct b43_wldev *dev)
316{
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200317 u32 tmp;
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200318
319 switch (dev->dev->bus_type) {
320#ifdef CONFIG_B43_BCMA
321 case B43_BUS_BCMA:
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200322 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
323 tmp &= ~B43_BCMA_IOCTL_GMODE;
324 tmp |= B43_BCMA_IOCTL_PHY_RESET;
325 tmp |= BCMA_IOCTL_FGC;
326 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
327 udelay(1);
328
329 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
330 tmp &= ~BCMA_IOCTL_FGC;
331 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
332 udelay(1);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200333 break;
334#endif
335#ifdef CONFIG_B43_SSB
336 case B43_BUS_SSB:
337 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
338 tmp &= ~B43_TMSLOW_GMODE;
339 tmp |= B43_TMSLOW_PHYRESET;
340 tmp |= SSB_TMSLOW_FGC;
341 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200342 usleep_range(1000, 2000);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200343
344 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
345 tmp &= ~SSB_TMSLOW_FGC;
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200346 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200347 usleep_range(1000, 2000);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200348
349 break;
350#endif
351 }
352}
353
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200354void b43_phy_take_out_of_reset(struct b43_wldev *dev)
355{
356 u32 tmp;
357
358 switch (dev->dev->bus_type) {
359#ifdef CONFIG_B43_BCMA
360 case B43_BUS_BCMA:
361 /* Unset reset bit (with forcing clock) */
362 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
363 tmp &= ~B43_BCMA_IOCTL_PHY_RESET;
364 tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
365 tmp |= BCMA_IOCTL_FGC;
366 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
367 udelay(1);
368
369 /* Do not force clock anymore */
370 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
371 tmp &= ~BCMA_IOCTL_FGC;
372 tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
373 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
374 udelay(1);
375 break;
376#endif
377#ifdef CONFIG_B43_SSB
378 case B43_BUS_SSB:
379 /* Unset reset bit (with forcing clock) */
380 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
381 tmp &= ~B43_TMSLOW_PHYRESET;
382 tmp &= ~B43_TMSLOW_PHYCLKEN;
383 tmp |= SSB_TMSLOW_FGC;
384 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
385 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
386 usleep_range(1000, 2000);
387
388 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
389 tmp &= ~SSB_TMSLOW_FGC;
390 tmp |= B43_TMSLOW_PHYCLKEN;
391 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
392 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
393 usleep_range(1000, 2000);
394 break;
395#endif
396 }
397}
398
Michael Bueschef1a6282008-08-27 18:53:02 +0200399int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
400{
401 struct b43_phy *phy = &(dev->phy);
402 u16 channelcookie, savedcookie;
403 int err;
404
405 if (new_channel == B43_DEFAULT_CHANNEL)
406 new_channel = phy->ops->get_default_chan(dev);
407
408 /* First we set the channel radio code to prevent the
409 * firmware from sending ghost packets.
410 */
411 channelcookie = new_channel;
412 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Rafał Miłecki106cb092010-10-06 07:50:07 +0200413 channelcookie |= B43_SHM_SH_CHAN_5GHZ;
414 /* FIXME: set 40Mhz flag if required */
415 if (0)
416 channelcookie |= B43_SHM_SH_CHAN_40MHZ;
Michael Bueschef1a6282008-08-27 18:53:02 +0200417 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
418 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
419
420 /* Now try to switch the PHY hardware channel. */
421 err = phy->ops->switch_channel(dev, new_channel);
422 if (err)
423 goto err_restore_cookie;
424
425 dev->phy.channel = new_channel;
426 /* Wait for the radio to tune to the channel and stabilize. */
427 msleep(8);
428
429 return 0;
430
431err_restore_cookie:
432 b43_shm_write16(dev, B43_SHM_SHARED,
433 B43_SHM_SH_CHAN, savedcookie);
434
435 return err;
436}
437
Johannes Berg19d337d2009-06-02 13:01:37 +0200438void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
Michael Bueschef1a6282008-08-27 18:53:02 +0200439{
440 struct b43_phy *phy = &dev->phy;
441
Michael Bueschb929ecf72008-12-19 18:40:00 +0100442 b43_mac_suspend(dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200443 phy->ops->software_rfkill(dev, blocked);
444 phy->radio_on = !blocked;
Michael Bueschb929ecf72008-12-19 18:40:00 +0100445 b43_mac_enable(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200446}
Michael Buesch18c8ade2008-08-28 19:33:40 +0200447
448/**
449 * b43_phy_txpower_adjust_work - TX power workqueue.
450 *
451 * Workqueue for updating the TX power parameters in hardware.
452 */
453void b43_phy_txpower_adjust_work(struct work_struct *work)
454{
455 struct b43_wl *wl = container_of(work, struct b43_wl,
456 txpower_adjust_work);
457 struct b43_wldev *dev;
458
459 mutex_lock(&wl->mutex);
460 dev = wl->current_dev;
461
462 if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
463 dev->phy.ops->adjust_txpower(dev);
464
465 mutex_unlock(&wl->mutex);
466}
467
Michael Buesch18c8ade2008-08-28 19:33:40 +0200468void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
469{
470 struct b43_phy *phy = &dev->phy;
471 unsigned long now = jiffies;
472 enum b43_txpwr_result result;
473
474 if (!(flags & B43_TXPWR_IGNORE_TIME)) {
475 /* Check if it's time for a TXpower check. */
476 if (time_before(now, phy->next_txpwr_check_time))
477 return; /* Not yet */
478 }
479 /* The next check will be needed in two seconds, or later. */
480 phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
481
Rafał Miłecki79d22322011-05-18 02:06:42 +0200482 if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
483 (dev->dev->board_type == SSB_BOARD_BU4306))
Michael Buesch18c8ade2008-08-28 19:33:40 +0200484 return; /* No software txpower adjustment needed */
485
486 result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
487 if (result == B43_TXPWR_RES_DONE)
488 return; /* We are done. */
489 B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
490 B43_WARN_ON(phy->ops->adjust_txpower == NULL);
491
492 /* We must adjust the transmission power in hardware.
493 * Schedule b43_phy_txpower_adjust_work(). */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400494 ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +0200495}
496
497int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
498{
499 const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
500 unsigned int a, b, c, d;
501 unsigned int average;
502 u32 tmp;
503
504 tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
505 a = tmp & 0xFF;
506 b = (tmp >> 8) & 0xFF;
507 c = (tmp >> 16) & 0xFF;
508 d = (tmp >> 24) & 0xFF;
509 if (a == 0 || a == B43_TSSI_MAX ||
510 b == 0 || b == B43_TSSI_MAX ||
511 c == 0 || c == B43_TSSI_MAX ||
512 d == 0 || d == B43_TSSI_MAX)
513 return -ENOENT;
514 /* The values are OK. Clear them. */
515 tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
516 (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
517 b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
518
519 if (is_ofdm) {
520 a = (a + 32) & 0x3F;
521 b = (b + 32) & 0x3F;
522 c = (c + 32) & 0x3F;
523 d = (d + 32) & 0x3F;
524 }
525
526 /* Get the average of the values with 0.5 added to each value. */
527 average = (a + b + c + d + 2) / 4;
528 if (is_ofdm) {
529 /* Adjust for CCK-boost */
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200530 if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
Michael Buesch18c8ade2008-08-28 19:33:40 +0200531 & B43_HF_CCKBOOST)
532 average = (average >= 13) ? (average - 13) : 0;
533 }
534
535 return average;
536}
Michael Bueschcb24f572008-09-03 12:12:20 +0200537
538void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
539{
540 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
541}
Rafał Miłecki98650452010-01-25 18:59:59 +0100542
Rafał Miłeckiabc1f7c2010-12-07 21:55:58 +0100543
544bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
545{
546 return (channel_type == NL80211_CHAN_HT40MINUS ||
547 channel_type == NL80211_CHAN_HT40PLUS);
548}
549
Rafał Miłeckif6a3e992011-08-12 00:03:26 +0200550/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
551void b43_phy_force_clock(struct b43_wldev *dev, bool force)
552{
553 u32 tmp;
554
555 WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
556 dev->phy.type != B43_PHYTYPE_HT);
557
558 switch (dev->dev->bus_type) {
559#ifdef CONFIG_B43_BCMA
560 case B43_BUS_BCMA:
561 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
562 if (force)
563 tmp |= BCMA_IOCTL_FGC;
564 else
565 tmp &= ~BCMA_IOCTL_FGC;
566 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
567 break;
568#endif
569#ifdef CONFIG_B43_SSB
570 case B43_BUS_SSB:
571 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
572 if (force)
573 tmp |= SSB_TMSLOW_FGC;
574 else
575 tmp &= ~SSB_TMSLOW_FGC;
576 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
577 break;
578#endif
579 }
580}
581
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100582/* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
Rafał Miłecki98650452010-01-25 18:59:59 +0100583struct b43_c32 b43_cordic(int theta)
584{
Joe Perches5b4bc642010-11-20 18:38:56 -0800585 static const u32 arctg[] = {
586 2949120, 1740967, 919879, 466945, 234379, 117304,
587 58666, 29335, 14668, 7334, 3667, 1833,
588 917, 458, 229, 115, 57, 29,
589 };
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100590 u8 i;
591 s32 tmp;
592 s8 signx = 1;
593 u32 angle = 0;
Rafał Miłecki98650452010-01-25 18:59:59 +0100594 struct b43_c32 ret = { .i = 39797, .q = 0, };
595
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100596 while (theta > (180 << 16))
597 theta -= (360 << 16);
598 while (theta < -(180 << 16))
599 theta += (360 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100600
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100601 if (theta > (90 << 16)) {
602 theta -= (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100603 signx = -1;
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100604 } else if (theta < -(90 << 16)) {
605 theta += (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100606 signx = -1;
607 }
608
609 for (i = 0; i <= 17; i++) {
610 if (theta > angle) {
611 tmp = ret.i - (ret.q >> i);
612 ret.q += ret.i >> i;
613 ret.i = tmp;
614 angle += arctg[i];
615 } else {
616 tmp = ret.i + (ret.q >> i);
617 ret.q -= ret.i >> i;
618 ret.i = tmp;
619 angle -= arctg[i];
620 }
621 }
622
623 ret.i *= signx;
624 ret.q *= signx;
625
626 return ret;
627}