NFC: trf7970a: Add support for Felica Type 3 tags
[pandora-kernel.git] / drivers / net / wireless / b43 / phy_common.c
1 /*
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 <m@bues.ch>
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"
32 #include "phy_n.h"
33 #include "phy_lp.h"
34 #include "phy_ht.h"
35 #include "phy_lcn.h"
36 #include "b43.h"
37 #include "main.h"
38
39
40 int b43_phy_allocate(struct b43_wldev *dev)
41 {
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:
55 #ifdef CONFIG_B43_PHY_N
56                 phy->ops = &b43_phyops_n;
57 #endif
58                 break;
59         case B43_PHYTYPE_LP:
60 #ifdef CONFIG_B43_PHY_LP
61                 phy->ops = &b43_phyops_lp;
62 #endif
63                 break;
64         case B43_PHYTYPE_HT:
65 #ifdef CONFIG_B43_PHY_HT
66                 phy->ops = &b43_phyops_ht;
67 #endif
68                 break;
69         case B43_PHYTYPE_LCN:
70 #ifdef CONFIG_B43_PHY_LCN
71                 phy->ops = &b43_phyops_lcn;
72 #endif
73                 break;
74         }
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
85 void b43_phy_free(struct b43_wldev *dev)
86 {
87         dev->phy.ops->free(dev);
88         dev->phy.ops = NULL;
89 }
90
91 int 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
99         phy->ops->switch_analog(dev, true);
100         b43_software_rfkill(dev, false);
101         err = ops->init(dev);
102         if (err) {
103                 b43err(dev->wl, "PHY init failed\n");
104                 goto err_block_rf;
105         }
106         /* Make sure to switch hardware and firmware (SHM) to
107          * the default channel. */
108         err = b43_switch_channel(dev, ops->get_default_chan(dev));
109         if (err) {
110                 b43err(dev->wl, "PHY init: Channel switch to default failed\n");
111                 goto err_phy_exit;
112         }
113
114         return 0;
115
116 err_phy_exit:
117         if (ops->exit)
118                 ops->exit(dev);
119 err_block_rf:
120         b43_software_rfkill(dev, true);
121
122         return err;
123 }
124
125 void b43_phy_exit(struct b43_wldev *dev)
126 {
127         const struct b43_phy_operations *ops = dev->phy.ops;
128
129         b43_software_rfkill(dev, true);
130         if (ops->exit)
131                 ops->exit(dev);
132 }
133
134 bool b43_has_hardware_pctl(struct b43_wldev *dev)
135 {
136         if (!dev->phy.hardware_power_control)
137                 return false;
138         if (!dev->phy.ops->supports_hwpctl)
139                 return false;
140         return dev->phy.ops->supports_hwpctl(dev);
141 }
142
143 void b43_radio_lock(struct b43_wldev *dev)
144 {
145         u32 macctl;
146
147 #if B43_DEBUG
148         B43_WARN_ON(dev->phy.radio_locked);
149         dev->phy.radio_locked = true;
150 #endif
151
152         macctl = b43_read32(dev, B43_MMIO_MACCTL);
153         macctl |= B43_MACCTL_RADIOLOCK;
154         b43_write32(dev, B43_MMIO_MACCTL, macctl);
155         /* Commit the write and wait for the firmware
156          * to finish any radio register access. */
157         b43_read32(dev, B43_MMIO_MACCTL);
158         udelay(10);
159 }
160
161 void b43_radio_unlock(struct b43_wldev *dev)
162 {
163         u32 macctl;
164
165 #if B43_DEBUG
166         B43_WARN_ON(!dev->phy.radio_locked);
167         dev->phy.radio_locked = false;
168 #endif
169
170         /* Commit any write */
171         b43_read16(dev, B43_MMIO_PHY_VER);
172         /* unlock */
173         macctl = b43_read32(dev, B43_MMIO_MACCTL);
174         macctl &= ~B43_MACCTL_RADIOLOCK;
175         b43_write32(dev, B43_MMIO_MACCTL, macctl);
176 }
177
178 void b43_phy_lock(struct b43_wldev *dev)
179 {
180 #if B43_DEBUG
181         B43_WARN_ON(dev->phy.phy_locked);
182         dev->phy.phy_locked = true;
183 #endif
184         B43_WARN_ON(dev->dev->core_rev < 3);
185
186         if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
187                 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
188 }
189
190 void b43_phy_unlock(struct b43_wldev *dev)
191 {
192 #if B43_DEBUG
193         B43_WARN_ON(!dev->phy.phy_locked);
194         dev->phy.phy_locked = false;
195 #endif
196         B43_WARN_ON(dev->dev->core_rev < 3);
197
198         if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
199                 b43_power_saving_ctl_bits(dev, 0);
200 }
201
202 static inline void assert_mac_suspended(struct b43_wldev *dev)
203 {
204         if (!B43_DEBUG)
205                 return;
206         if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
207             (dev->mac_suspended <= 0)) {
208                 b43dbg(dev->wl, "PHY/RADIO register access with "
209                        "enabled MAC.\n");
210                 dump_stack();
211         }
212 }
213
214 u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
215 {
216         assert_mac_suspended(dev);
217         return dev->phy.ops->radio_read(dev, reg);
218 }
219
220 void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
221 {
222         assert_mac_suspended(dev);
223         dev->phy.ops->radio_write(dev, reg, value);
224 }
225
226 void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
227 {
228         b43_radio_write16(dev, offset,
229                           b43_radio_read16(dev, offset) & mask);
230 }
231
232 void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
233 {
234         b43_radio_write16(dev, offset,
235                           b43_radio_read16(dev, offset) | set);
236 }
237
238 void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
239 {
240         b43_radio_write16(dev, offset,
241                           (b43_radio_read16(dev, offset) & mask) | set);
242 }
243
244 bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
245                           u16 value, int delay, int timeout)
246 {
247         u16 val;
248         int i;
249
250         for (i = 0; i < timeout; i += delay) {
251                 val = b43_radio_read(dev, offset);
252                 if ((val & mask) == value)
253                         return true;
254                 udelay(delay);
255         }
256         return false;
257 }
258
259 u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
260 {
261         assert_mac_suspended(dev);
262         dev->phy.writes_counter = 0;
263         return dev->phy.ops->phy_read(dev, reg);
264 }
265
266 void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
267 {
268         assert_mac_suspended(dev);
269         dev->phy.ops->phy_write(dev, reg, value);
270         if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
271                 b43_read16(dev, B43_MMIO_PHY_VER);
272                 dev->phy.writes_counter = 0;
273         }
274 }
275
276 void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
277 {
278         assert_mac_suspended(dev);
279         dev->phy.ops->phy_write(dev, destreg,
280                 dev->phy.ops->phy_read(dev, srcreg));
281 }
282
283 void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
284 {
285         if (dev->phy.ops->phy_maskset) {
286                 assert_mac_suspended(dev);
287                 dev->phy.ops->phy_maskset(dev, offset, mask, 0);
288         } else {
289                 b43_phy_write(dev, offset,
290                               b43_phy_read(dev, offset) & mask);
291         }
292 }
293
294 void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
295 {
296         if (dev->phy.ops->phy_maskset) {
297                 assert_mac_suspended(dev);
298                 dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
299         } else {
300                 b43_phy_write(dev, offset,
301                               b43_phy_read(dev, offset) | set);
302         }
303 }
304
305 void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
306 {
307         if (dev->phy.ops->phy_maskset) {
308                 assert_mac_suspended(dev);
309                 dev->phy.ops->phy_maskset(dev, offset, mask, set);
310         } else {
311                 b43_phy_write(dev, offset,
312                               (b43_phy_read(dev, offset) & mask) | set);
313         }
314 }
315
316 void b43_phy_put_into_reset(struct b43_wldev *dev)
317 {
318         u32 tmp;
319
320         switch (dev->dev->bus_type) {
321 #ifdef CONFIG_B43_BCMA
322         case B43_BUS_BCMA:
323                 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
324                 tmp &= ~B43_BCMA_IOCTL_GMODE;
325                 tmp |= B43_BCMA_IOCTL_PHY_RESET;
326                 tmp |= BCMA_IOCTL_FGC;
327                 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
328                 udelay(1);
329
330                 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
331                 tmp &= ~BCMA_IOCTL_FGC;
332                 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
333                 udelay(1);
334                 break;
335 #endif
336 #ifdef CONFIG_B43_SSB
337         case B43_BUS_SSB:
338                 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
339                 tmp &= ~B43_TMSLOW_GMODE;
340                 tmp |= B43_TMSLOW_PHYRESET;
341                 tmp |= SSB_TMSLOW_FGC;
342                 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
343                 usleep_range(1000, 2000);
344
345                 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
346                 tmp &= ~SSB_TMSLOW_FGC;
347                 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
348                 usleep_range(1000, 2000);
349
350                 break;
351 #endif
352         }
353 }
354
355 void b43_phy_take_out_of_reset(struct b43_wldev *dev)
356 {
357         u32 tmp;
358
359         switch (dev->dev->bus_type) {
360 #ifdef CONFIG_B43_BCMA
361         case B43_BUS_BCMA:
362                 /* Unset reset bit (with forcing clock) */
363                 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
364                 tmp &= ~B43_BCMA_IOCTL_PHY_RESET;
365                 tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
366                 tmp |= BCMA_IOCTL_FGC;
367                 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
368                 udelay(1);
369
370                 /* Do not force clock anymore */
371                 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
372                 tmp &= ~BCMA_IOCTL_FGC;
373                 tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
374                 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
375                 udelay(1);
376                 break;
377 #endif
378 #ifdef CONFIG_B43_SSB
379         case B43_BUS_SSB:
380                 /* Unset reset bit (with forcing clock) */
381                 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
382                 tmp &= ~B43_TMSLOW_PHYRESET;
383                 tmp &= ~B43_TMSLOW_PHYCLKEN;
384                 tmp |= SSB_TMSLOW_FGC;
385                 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
386                 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
387                 usleep_range(1000, 2000);
388
389                 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
390                 tmp &= ~SSB_TMSLOW_FGC;
391                 tmp |= B43_TMSLOW_PHYCLKEN;
392                 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
393                 ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
394                 usleep_range(1000, 2000);
395                 break;
396 #endif
397         }
398 }
399
400 int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
401 {
402         struct b43_phy *phy = &(dev->phy);
403         u16 channelcookie, savedcookie;
404         int err;
405
406         if (new_channel == B43_DEFAULT_CHANNEL)
407                 new_channel = phy->ops->get_default_chan(dev);
408
409         /* First we set the channel radio code to prevent the
410          * firmware from sending ghost packets.
411          */
412         channelcookie = new_channel;
413         if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
414                 channelcookie |= B43_SHM_SH_CHAN_5GHZ;
415         /* FIXME: set 40Mhz flag if required */
416         if (0)
417                 channelcookie |= B43_SHM_SH_CHAN_40MHZ;
418         savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
419         b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
420
421         /* Now try to switch the PHY hardware channel. */
422         err = phy->ops->switch_channel(dev, new_channel);
423         if (err)
424                 goto err_restore_cookie;
425
426         dev->phy.channel = new_channel;
427         /* Wait for the radio to tune to the channel and stabilize. */
428         msleep(8);
429
430         return 0;
431
432 err_restore_cookie:
433         b43_shm_write16(dev, B43_SHM_SHARED,
434                         B43_SHM_SH_CHAN, savedcookie);
435
436         return err;
437 }
438
439 void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
440 {
441         struct b43_phy *phy = &dev->phy;
442
443         b43_mac_suspend(dev);
444         phy->ops->software_rfkill(dev, blocked);
445         phy->radio_on = !blocked;
446         b43_mac_enable(dev);
447 }
448
449 /**
450  * b43_phy_txpower_adjust_work - TX power workqueue.
451  *
452  * Workqueue for updating the TX power parameters in hardware.
453  */
454 void b43_phy_txpower_adjust_work(struct work_struct *work)
455 {
456         struct b43_wl *wl = container_of(work, struct b43_wl,
457                                          txpower_adjust_work);
458         struct b43_wldev *dev;
459
460         mutex_lock(&wl->mutex);
461         dev = wl->current_dev;
462
463         if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
464                 dev->phy.ops->adjust_txpower(dev);
465
466         mutex_unlock(&wl->mutex);
467 }
468
469 void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
470 {
471         struct b43_phy *phy = &dev->phy;
472         unsigned long now = jiffies;
473         enum b43_txpwr_result result;
474
475         if (!(flags & B43_TXPWR_IGNORE_TIME)) {
476                 /* Check if it's time for a TXpower check. */
477                 if (time_before(now, phy->next_txpwr_check_time))
478                         return; /* Not yet */
479         }
480         /* The next check will be needed in two seconds, or later. */
481         phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
482
483         if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
484             (dev->dev->board_type == SSB_BOARD_BU4306))
485                 return; /* No software txpower adjustment needed */
486
487         result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
488         if (result == B43_TXPWR_RES_DONE)
489                 return; /* We are done. */
490         B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
491         B43_WARN_ON(phy->ops->adjust_txpower == NULL);
492
493         /* We must adjust the transmission power in hardware.
494          * Schedule b43_phy_txpower_adjust_work(). */
495         ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
496 }
497
498 int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
499 {
500         const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
501         unsigned int a, b, c, d;
502         unsigned int average;
503         u32 tmp;
504
505         tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
506         a = tmp & 0xFF;
507         b = (tmp >> 8) & 0xFF;
508         c = (tmp >> 16) & 0xFF;
509         d = (tmp >> 24) & 0xFF;
510         if (a == 0 || a == B43_TSSI_MAX ||
511             b == 0 || b == B43_TSSI_MAX ||
512             c == 0 || c == B43_TSSI_MAX ||
513             d == 0 || d == B43_TSSI_MAX)
514                 return -ENOENT;
515         /* The values are OK. Clear them. */
516         tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
517               (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
518         b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
519
520         if (is_ofdm) {
521                 a = (a + 32) & 0x3F;
522                 b = (b + 32) & 0x3F;
523                 c = (c + 32) & 0x3F;
524                 d = (d + 32) & 0x3F;
525         }
526
527         /* Get the average of the values with 0.5 added to each value. */
528         average = (a + b + c + d + 2) / 4;
529         if (is_ofdm) {
530                 /* Adjust for CCK-boost */
531                 if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
532                     & B43_HF_CCKBOOST)
533                         average = (average >= 13) ? (average - 13) : 0;
534         }
535
536         return average;
537 }
538
539 void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
540 {
541         b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
542 }
543
544
545 bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
546 {
547         return (channel_type == NL80211_CHAN_HT40MINUS ||
548                 channel_type == NL80211_CHAN_HT40PLUS);
549 }
550
551 /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
552 void b43_phy_force_clock(struct b43_wldev *dev, bool force)
553 {
554         u32 tmp;
555
556         WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
557                 dev->phy.type != B43_PHYTYPE_HT);
558
559         switch (dev->dev->bus_type) {
560 #ifdef CONFIG_B43_BCMA
561         case B43_BUS_BCMA:
562                 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
563                 if (force)
564                         tmp |= BCMA_IOCTL_FGC;
565                 else
566                         tmp &= ~BCMA_IOCTL_FGC;
567                 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
568                 break;
569 #endif
570 #ifdef CONFIG_B43_SSB
571         case B43_BUS_SSB:
572                 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
573                 if (force)
574                         tmp |= SSB_TMSLOW_FGC;
575                 else
576                         tmp &= ~SSB_TMSLOW_FGC;
577                 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
578                 break;
579 #endif
580         }
581 }
582
583 /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
584 struct b43_c32 b43_cordic(int theta)
585 {
586         static const u32 arctg[] = {
587                 2949120, 1740967, 919879, 466945, 234379, 117304,
588                   58666,   29335,  14668,   7334,   3667,   1833,
589                     917,     458,    229,    115,     57,     29,
590         };
591         u8 i;
592         s32 tmp;
593         s8 signx = 1;
594         u32 angle = 0;
595         struct b43_c32 ret = { .i = 39797, .q = 0, };
596
597         while (theta > (180 << 16))
598                 theta -= (360 << 16);
599         while (theta < -(180 << 16))
600                 theta += (360 << 16);
601
602         if (theta > (90 << 16)) {
603                 theta -= (180 << 16);
604                 signx = -1;
605         } else if (theta < -(90 << 16)) {
606                 theta += (180 << 16);
607                 signx = -1;
608         }
609
610         for (i = 0; i <= 17; i++) {
611                 if (theta > angle) {
612                         tmp = ret.i - (ret.q >> i);
613                         ret.q += ret.i >> i;
614                         ret.i = tmp;
615                         angle += arctg[i];
616                 } else {
617                         tmp = ret.i + (ret.q >> i);
618                         ret.q -= ret.i >> i;
619                         ret.i = tmp;
620                         angle -= arctg[i];
621                 }
622         }
623
624         ret.i *= signx;
625         ret.q *= signx;
626
627         return ret;
628 }