blob: 08ca524664e6acfe49f3a4fd2a14abc691af118f [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) {
Michael Bueschef1a6282008-08-27 18:53:02 +020048 case B43_PHYTYPE_G:
Rafał Miłecki418378f2014-06-20 17:22:01 +020049#ifdef CONFIG_B43_PHY_G
Michael Bueschef1a6282008-08-27 18:53:02 +020050 phy->ops = &b43_phyops_g;
Rafał Miłecki418378f2014-06-20 17:22:01 +020051#endif
Michael Bueschef1a6282008-08-27 18:53:02 +020052 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;
Rafał Miłecki58eb7ff2011-07-07 18:58:25 +020068 case B43_PHYTYPE_LCN:
69#ifdef CONFIG_B43_PHY_LCN
70 phy->ops = &b43_phyops_lcn;
71#endif
72 break;
Michael Bueschef1a6282008-08-27 18:53:02 +020073 }
74 if (B43_WARN_ON(!phy->ops))
75 return -ENODEV;
76
77 err = phy->ops->allocate(dev);
78 if (err)
79 phy->ops = NULL;
80
81 return err;
82}
83
Michael Bueschfb111372008-09-02 13:00:34 +020084void b43_phy_free(struct b43_wldev *dev)
85{
86 dev->phy.ops->free(dev);
87 dev->phy.ops = NULL;
88}
89
Michael Bueschef1a6282008-08-27 18:53:02 +020090int b43_phy_init(struct b43_wldev *dev)
91{
92 struct b43_phy *phy = &dev->phy;
93 const struct b43_phy_operations *ops = phy->ops;
94 int err;
95
Rafał Miłeckiea42e712014-05-31 20:49:38 +020096 /* During PHY init we need to use some channel. On the first init this
97 * function is called *before* b43_op_config, so our pointer is NULL.
98 */
99 if (!phy->chandef) {
100 phy->chandef = &dev->wl->hw->conf.chandef;
101 phy->channel = phy->chandef->chan->hw_value;
102 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200103
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +0200104 phy->ops->switch_analog(dev, true);
Rafał Miłeckia6316e22014-04-22 13:54:36 +0200105 b43_software_rfkill(dev, false);
Rafał Miłecki09951ad2014-05-27 22:07:31 +0200106
Michael Bueschef1a6282008-08-27 18:53:02 +0200107 err = ops->init(dev);
108 if (err) {
109 b43err(dev->wl, "PHY init failed\n");
110 goto err_block_rf;
111 }
Rafał Miłecki09951ad2014-05-27 22:07:31 +0200112 phy->do_full_init = false;
113
Rafał Miłeckieb530b02014-05-31 20:49:36 +0200114 err = b43_switch_channel(dev, phy->channel);
Michael Bueschef1a6282008-08-27 18:53:02 +0200115 if (err) {
116 b43err(dev->wl, "PHY init: Channel switch to default failed\n");
117 goto err_phy_exit;
118 }
119
120 return 0;
121
122err_phy_exit:
Rafał Miłecki09951ad2014-05-27 22:07:31 +0200123 phy->do_full_init = true;
Michael Bueschef1a6282008-08-27 18:53:02 +0200124 if (ops->exit)
125 ops->exit(dev);
126err_block_rf:
Rafał Miłeckia6316e22014-04-22 13:54:36 +0200127 b43_software_rfkill(dev, true);
Michael Bueschef1a6282008-08-27 18:53:02 +0200128
129 return err;
130}
131
132void b43_phy_exit(struct b43_wldev *dev)
133{
134 const struct b43_phy_operations *ops = dev->phy.ops;
135
Rafał Miłeckia6316e22014-04-22 13:54:36 +0200136 b43_software_rfkill(dev, true);
Rafał Miłecki09951ad2014-05-27 22:07:31 +0200137 dev->phy.do_full_init = true;
Michael Bueschef1a6282008-08-27 18:53:02 +0200138 if (ops->exit)
139 ops->exit(dev);
140}
141
142bool b43_has_hardware_pctl(struct b43_wldev *dev)
143{
144 if (!dev->phy.hardware_power_control)
Zhao, Gang1a2b2502014-02-16 22:31:38 +0800145 return false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200146 if (!dev->phy.ops->supports_hwpctl)
Zhao, Gang1a2b2502014-02-16 22:31:38 +0800147 return false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200148 return dev->phy.ops->supports_hwpctl(dev);
149}
150
151void b43_radio_lock(struct b43_wldev *dev)
152{
153 u32 macctl;
154
Michael Buesch591f3dc2009-03-31 12:27:32 +0200155#if B43_DEBUG
156 B43_WARN_ON(dev->phy.radio_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000157 dev->phy.radio_locked = true;
Michael Buesch591f3dc2009-03-31 12:27:32 +0200158#endif
159
Michael Bueschef1a6282008-08-27 18:53:02 +0200160 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200161 macctl |= B43_MACCTL_RADIOLOCK;
162 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesch591f3dc2009-03-31 12:27:32 +0200163 /* Commit the write and wait for the firmware
164 * to finish any radio register access. */
Michael Bueschef1a6282008-08-27 18:53:02 +0200165 b43_read32(dev, B43_MMIO_MACCTL);
166 udelay(10);
167}
168
169void b43_radio_unlock(struct b43_wldev *dev)
170{
171 u32 macctl;
172
Michael Buesch591f3dc2009-03-31 12:27:32 +0200173#if B43_DEBUG
174 B43_WARN_ON(!dev->phy.radio_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000175 dev->phy.radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +0200176#endif
177
Michael Bueschef1a6282008-08-27 18:53:02 +0200178 /* Commit any write */
179 b43_read16(dev, B43_MMIO_PHY_VER);
180 /* unlock */
181 macctl = b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschef1a6282008-08-27 18:53:02 +0200182 macctl &= ~B43_MACCTL_RADIOLOCK;
183 b43_write32(dev, B43_MMIO_MACCTL, macctl);
184}
185
186void b43_phy_lock(struct b43_wldev *dev)
187{
188#if B43_DEBUG
189 B43_WARN_ON(dev->phy.phy_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000190 dev->phy.phy_locked = true;
Michael Bueschef1a6282008-08-27 18:53:02 +0200191#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200192 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200193
Johannes Berg05c914f2008-09-11 00:01:58 +0200194 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200195 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
196}
197
198void b43_phy_unlock(struct b43_wldev *dev)
199{
200#if B43_DEBUG
201 B43_WARN_ON(!dev->phy.phy_locked);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000202 dev->phy.phy_locked = false;
Michael Bueschef1a6282008-08-27 18:53:02 +0200203#endif
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200204 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Bueschef1a6282008-08-27 18:53:02 +0200205
Johannes Berg05c914f2008-09-11 00:01:58 +0200206 if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
Michael Bueschef1a6282008-08-27 18:53:02 +0200207 b43_power_saving_ctl_bits(dev, 0);
208}
209
Michael Bueschd10d0e52008-12-18 22:13:39 +0100210static inline void assert_mac_suspended(struct b43_wldev *dev)
211{
212 if (!B43_DEBUG)
213 return;
214 if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
215 (dev->mac_suspended <= 0)) {
216 b43dbg(dev->wl, "PHY/RADIO register access with "
217 "enabled MAC.\n");
218 dump_stack();
219 }
220}
221
Michael Bueschef1a6282008-08-27 18:53:02 +0200222u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
223{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100224 assert_mac_suspended(dev);
Rafał Miłecki6247d2a2014-07-31 21:59:42 +0200225 dev->phy.writes_counter = 0;
Michael Bueschef1a6282008-08-27 18:53:02 +0200226 return dev->phy.ops->radio_read(dev, reg);
227}
228
229void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
230{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100231 assert_mac_suspended(dev);
Rafał Miłecki6247d2a2014-07-31 21:59:42 +0200232 if (b43_bus_host_is_pci(dev->dev) &&
233 ++dev->phy.writes_counter > B43_MAX_WRITES_IN_ROW) {
234 b43_read32(dev, B43_MMIO_MACCTL);
235 dev->phy.writes_counter = 1;
236 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200237 dev->phy.ops->radio_write(dev, reg, value);
238}
239
240void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
241{
242 b43_radio_write16(dev, offset,
243 b43_radio_read16(dev, offset) & mask);
244}
245
246void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
247{
248 b43_radio_write16(dev, offset,
249 b43_radio_read16(dev, offset) | set);
250}
251
252void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
253{
254 b43_radio_write16(dev, offset,
255 (b43_radio_read16(dev, offset) & mask) | set);
256}
257
Rafał Miłecki0f941772012-07-26 00:07:37 +0200258bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
259 u16 value, int delay, int timeout)
260{
261 u16 val;
262 int i;
263
264 for (i = 0; i < timeout; i += delay) {
265 val = b43_radio_read(dev, offset);
266 if ((val & mask) == value)
267 return true;
268 udelay(delay);
269 }
270 return false;
271}
272
Michael Bueschef1a6282008-08-27 18:53:02 +0200273u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
274{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100275 assert_mac_suspended(dev);
Rafał Miłecki15518082010-12-07 09:42:07 +0100276 dev->phy.writes_counter = 0;
Rafał Miłeckid342b952014-07-31 21:59:43 +0200277
278 if (dev->phy.ops->phy_read)
279 return dev->phy.ops->phy_read(dev, reg);
280
281 b43_write16(dev, B43_MMIO_PHY_CONTROL, reg);
282 return b43_read16(dev, B43_MMIO_PHY_DATA);
Michael Bueschef1a6282008-08-27 18:53:02 +0200283}
284
285void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
286{
Michael Bueschd10d0e52008-12-18 22:13:39 +0100287 assert_mac_suspended(dev);
Rafał Miłecki6247d2a2014-07-31 21:59:42 +0200288 if (b43_bus_host_is_pci(dev->dev) &&
289 ++dev->phy.writes_counter > B43_MAX_WRITES_IN_ROW) {
Rafał Miłecki15518082010-12-07 09:42:07 +0100290 b43_read16(dev, B43_MMIO_PHY_VER);
Rafał Miłecki6247d2a2014-07-31 21:59:42 +0200291 dev->phy.writes_counter = 1;
Rafał Miłecki15518082010-12-07 09:42:07 +0100292 }
Rafał Miłeckid342b952014-07-31 21:59:43 +0200293
294 if (dev->phy.ops->phy_write)
295 return dev->phy.ops->phy_write(dev, reg, value);
296
297 b43_write16(dev, B43_MMIO_PHY_CONTROL, reg);
298 b43_write16(dev, B43_MMIO_PHY_DATA, value);
Michael Bueschef1a6282008-08-27 18:53:02 +0200299}
300
Gábor Stefanik738f0f42009-08-03 01:28:12 +0200301void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
302{
303 assert_mac_suspended(dev);
304 dev->phy.ops->phy_write(dev, destreg,
305 dev->phy.ops->phy_read(dev, srcreg));
306}
307
Michael Bueschef1a6282008-08-27 18:53:02 +0200308void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
309{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200310 if (dev->phy.ops->phy_maskset) {
311 assert_mac_suspended(dev);
312 dev->phy.ops->phy_maskset(dev, offset, mask, 0);
313 } else {
314 b43_phy_write(dev, offset,
315 b43_phy_read(dev, offset) & mask);
316 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200317}
318
319void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
320{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200321 if (dev->phy.ops->phy_maskset) {
322 assert_mac_suspended(dev);
323 dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
324 } else {
325 b43_phy_write(dev, offset,
326 b43_phy_read(dev, offset) | set);
327 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200328}
329
330void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
331{
Gábor Stefanik68ec5322009-08-26 20:51:25 +0200332 if (dev->phy.ops->phy_maskset) {
333 assert_mac_suspended(dev);
334 dev->phy.ops->phy_maskset(dev, offset, mask, set);
335 } else {
336 b43_phy_write(dev, offset,
337 (b43_phy_read(dev, offset) & mask) | set);
338 }
Michael Bueschef1a6282008-08-27 18:53:02 +0200339}
340
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200341void b43_phy_put_into_reset(struct b43_wldev *dev)
342{
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200343 u32 tmp;
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200344
345 switch (dev->dev->bus_type) {
346#ifdef CONFIG_B43_BCMA
347 case B43_BUS_BCMA:
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200348 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
349 tmp &= ~B43_BCMA_IOCTL_GMODE;
350 tmp |= B43_BCMA_IOCTL_PHY_RESET;
351 tmp |= BCMA_IOCTL_FGC;
352 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
353 udelay(1);
354
355 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
356 tmp &= ~BCMA_IOCTL_FGC;
357 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
358 udelay(1);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200359 break;
360#endif
361#ifdef CONFIG_B43_SSB
362 case B43_BUS_SSB:
363 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
364 tmp &= ~B43_TMSLOW_GMODE;
365 tmp |= B43_TMSLOW_PHYRESET;
366 tmp |= SSB_TMSLOW_FGC;
367 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200368 usleep_range(1000, 2000);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200369
370 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
371 tmp &= ~SSB_TMSLOW_FGC;
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200372 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200373 usleep_range(1000, 2000);
Rafał Miłeckib60c3c22014-05-17 23:24:54 +0200374
375 break;
376#endif
377 }
378}
379
Rafał Miłecki50c1b592014-05-17 23:24:55 +0200380void b43_phy_take_out_of_reset(struct b43_wldev *dev)
381{
382 u32 tmp;
383
384 switch (dev->dev->bus_type) {
385#ifdef CONFIG_B43_BCMA
386 case B43_BUS_BCMA:
387 /* Unset reset bit (with forcing clock) */
388 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
389 tmp &= ~B43_BCMA_IOCTL_PHY_RESET;
390 tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
391 tmp |= BCMA_IOCTL_FGC;
392 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
393 udelay(1);
394
395 /* Do not force clock anymore */
396 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
397 tmp &= ~BCMA_IOCTL_FGC;
398 tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
399 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
400 udelay(1);
401 break;
402#endif
403#ifdef CONFIG_B43_SSB
404 case B43_BUS_SSB:
405 /* Unset reset bit (with forcing clock) */
406 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
407 tmp &= ~B43_TMSLOW_PHYRESET;
408 tmp &= ~B43_TMSLOW_PHYCLKEN;
409 tmp |= SSB_TMSLOW_FGC;
410 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
411 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
412 usleep_range(1000, 2000);
413
414 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
415 tmp &= ~SSB_TMSLOW_FGC;
416 tmp |= B43_TMSLOW_PHYCLKEN;
417 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
418 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
419 usleep_range(1000, 2000);
420 break;
421#endif
422 }
423}
424
Michael Bueschef1a6282008-08-27 18:53:02 +0200425int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
426{
427 struct b43_phy *phy = &(dev->phy);
428 u16 channelcookie, savedcookie;
429 int err;
430
Michael Bueschef1a6282008-08-27 18:53:02 +0200431 /* First we set the channel radio code to prevent the
432 * firmware from sending ghost packets.
433 */
434 channelcookie = new_channel;
435 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Rafał Miłecki106cb092010-10-06 07:50:07 +0200436 channelcookie |= B43_SHM_SH_CHAN_5GHZ;
437 /* FIXME: set 40Mhz flag if required */
438 if (0)
439 channelcookie |= B43_SHM_SH_CHAN_40MHZ;
Michael Bueschef1a6282008-08-27 18:53:02 +0200440 savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
441 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
442
443 /* Now try to switch the PHY hardware channel. */
444 err = phy->ops->switch_channel(dev, new_channel);
445 if (err)
446 goto err_restore_cookie;
447
Michael Bueschef1a6282008-08-27 18:53:02 +0200448 /* Wait for the radio to tune to the channel and stabilize. */
449 msleep(8);
450
451 return 0;
452
453err_restore_cookie:
454 b43_shm_write16(dev, B43_SHM_SHARED,
455 B43_SHM_SH_CHAN, savedcookie);
456
457 return err;
458}
459
Johannes Berg19d337d2009-06-02 13:01:37 +0200460void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
Michael Bueschef1a6282008-08-27 18:53:02 +0200461{
462 struct b43_phy *phy = &dev->phy;
463
Michael Bueschb929ecf72008-12-19 18:40:00 +0100464 b43_mac_suspend(dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200465 phy->ops->software_rfkill(dev, blocked);
466 phy->radio_on = !blocked;
Michael Bueschb929ecf72008-12-19 18:40:00 +0100467 b43_mac_enable(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +0200468}
Michael Buesch18c8ade2008-08-28 19:33:40 +0200469
470/**
471 * b43_phy_txpower_adjust_work - TX power workqueue.
472 *
473 * Workqueue for updating the TX power parameters in hardware.
474 */
475void b43_phy_txpower_adjust_work(struct work_struct *work)
476{
477 struct b43_wl *wl = container_of(work, struct b43_wl,
478 txpower_adjust_work);
479 struct b43_wldev *dev;
480
481 mutex_lock(&wl->mutex);
482 dev = wl->current_dev;
483
484 if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
485 dev->phy.ops->adjust_txpower(dev);
486
487 mutex_unlock(&wl->mutex);
488}
489
Michael Buesch18c8ade2008-08-28 19:33:40 +0200490void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
491{
492 struct b43_phy *phy = &dev->phy;
493 unsigned long now = jiffies;
494 enum b43_txpwr_result result;
495
496 if (!(flags & B43_TXPWR_IGNORE_TIME)) {
497 /* Check if it's time for a TXpower check. */
498 if (time_before(now, phy->next_txpwr_check_time))
499 return; /* Not yet */
500 }
501 /* The next check will be needed in two seconds, or later. */
502 phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
503
Rafał Miłecki79d22322011-05-18 02:06:42 +0200504 if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
505 (dev->dev->board_type == SSB_BOARD_BU4306))
Michael Buesch18c8ade2008-08-28 19:33:40 +0200506 return; /* No software txpower adjustment needed */
507
508 result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
509 if (result == B43_TXPWR_RES_DONE)
510 return; /* We are done. */
511 B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
512 B43_WARN_ON(phy->ops->adjust_txpower == NULL);
513
514 /* We must adjust the transmission power in hardware.
515 * Schedule b43_phy_txpower_adjust_work(). */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400516 ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +0200517}
518
519int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
520{
521 const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
522 unsigned int a, b, c, d;
523 unsigned int average;
524 u32 tmp;
525
526 tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
527 a = tmp & 0xFF;
528 b = (tmp >> 8) & 0xFF;
529 c = (tmp >> 16) & 0xFF;
530 d = (tmp >> 24) & 0xFF;
531 if (a == 0 || a == B43_TSSI_MAX ||
532 b == 0 || b == B43_TSSI_MAX ||
533 c == 0 || c == B43_TSSI_MAX ||
534 d == 0 || d == B43_TSSI_MAX)
535 return -ENOENT;
536 /* The values are OK. Clear them. */
537 tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
538 (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
539 b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
540
541 if (is_ofdm) {
542 a = (a + 32) & 0x3F;
543 b = (b + 32) & 0x3F;
544 c = (c + 32) & 0x3F;
545 d = (d + 32) & 0x3F;
546 }
547
548 /* Get the average of the values with 0.5 added to each value. */
549 average = (a + b + c + d + 2) / 4;
550 if (is_ofdm) {
551 /* Adjust for CCK-boost */
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200552 if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
Michael Buesch18c8ade2008-08-28 19:33:40 +0200553 & B43_HF_CCKBOOST)
554 average = (average >= 13) ? (average - 13) : 0;
555 }
556
557 return average;
558}
Michael Bueschcb24f572008-09-03 12:12:20 +0200559
560void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
561{
562 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
563}
Rafał Miłecki98650452010-01-25 18:59:59 +0100564
Rafał Miłeckiabc1f7c2010-12-07 21:55:58 +0100565
Rafał Miłeckibee6d4b2014-05-31 20:49:40 +0200566bool b43_is_40mhz(struct b43_wldev *dev)
567{
568 return dev->phy.chandef->width == NL80211_CHAN_WIDTH_40;
569}
570
Rafał Miłeckif6a3e992011-08-12 00:03:26 +0200571/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
572void b43_phy_force_clock(struct b43_wldev *dev, bool force)
573{
574 u32 tmp;
575
576 WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
577 dev->phy.type != B43_PHYTYPE_HT);
578
579 switch (dev->dev->bus_type) {
580#ifdef CONFIG_B43_BCMA
581 case B43_BUS_BCMA:
582 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
583 if (force)
584 tmp |= BCMA_IOCTL_FGC;
585 else
586 tmp &= ~BCMA_IOCTL_FGC;
587 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
588 break;
589#endif
590#ifdef CONFIG_B43_SSB
591 case B43_BUS_SSB:
592 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
593 if (force)
594 tmp |= SSB_TMSLOW_FGC;
595 else
596 tmp &= ~SSB_TMSLOW_FGC;
597 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
598 break;
599#endif
600 }
601}
602
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100603/* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
Rafał Miłecki98650452010-01-25 18:59:59 +0100604struct b43_c32 b43_cordic(int theta)
605{
Joe Perches5b4bc642010-11-20 18:38:56 -0800606 static const u32 arctg[] = {
607 2949120, 1740967, 919879, 466945, 234379, 117304,
608 58666, 29335, 14668, 7334, 3667, 1833,
609 917, 458, 229, 115, 57, 29,
610 };
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100611 u8 i;
612 s32 tmp;
613 s8 signx = 1;
614 u32 angle = 0;
Rafał Miłecki98650452010-01-25 18:59:59 +0100615 struct b43_c32 ret = { .i = 39797, .q = 0, };
616
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100617 while (theta > (180 << 16))
618 theta -= (360 << 16);
619 while (theta < -(180 << 16))
620 theta += (360 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100621
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100622 if (theta > (90 << 16)) {
623 theta -= (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100624 signx = -1;
Rafał Miłecki6f98e622010-01-25 19:00:00 +0100625 } else if (theta < -(90 << 16)) {
626 theta += (180 << 16);
Rafał Miłecki98650452010-01-25 18:59:59 +0100627 signx = -1;
628 }
629
630 for (i = 0; i <= 17; i++) {
631 if (theta > angle) {
632 tmp = ret.i - (ret.q >> i);
633 ret.q += ret.i >> i;
634 ret.i = tmp;
635 angle += arctg[i];
636 } else {
637 tmp = ret.i + (ret.q >> i);
638 ret.q -= ret.i >> i;
639 ret.i = tmp;
640 angle -= arctg[i];
641 }
642 }
643
644 ret.i *= signx;
645 ret.q *= signx;
646
647 return ret;
648}