phy_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. Broadcom B43 wireless driver
  3. Common PHY routines
  4. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  5. Copyright (c) 2005-2007 Stefano Brivio <stefano.brivio@polimi.it>
  6. Copyright (c) 2005-2008 Michael Buesch <m@bues.ch>
  7. Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
  8. Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  20. Boston, MA 02110-1301, USA.
  21. */
  22. #include "phy_common.h"
  23. #include "phy_g.h"
  24. #include "phy_a.h"
  25. #include "phy_n.h"
  26. #include "phy_lp.h"
  27. #include "phy_ht.h"
  28. #include "phy_lcn.h"
  29. #include "b43.h"
  30. #include "main.h"
  31. int b43_phy_allocate(struct b43_wldev *dev)
  32. {
  33. struct b43_phy *phy = &(dev->phy);
  34. int err;
  35. phy->ops = NULL;
  36. switch (phy->type) {
  37. case B43_PHYTYPE_A:
  38. phy->ops = &b43_phyops_a;
  39. break;
  40. case B43_PHYTYPE_G:
  41. phy->ops = &b43_phyops_g;
  42. break;
  43. case B43_PHYTYPE_N:
  44. #ifdef CONFIG_B43_PHY_N
  45. phy->ops = &b43_phyops_n;
  46. #endif
  47. break;
  48. case B43_PHYTYPE_LP:
  49. #ifdef CONFIG_B43_PHY_LP
  50. phy->ops = &b43_phyops_lp;
  51. #endif
  52. break;
  53. case B43_PHYTYPE_HT:
  54. #ifdef CONFIG_B43_PHY_HT
  55. phy->ops = &b43_phyops_ht;
  56. #endif
  57. break;
  58. case B43_PHYTYPE_LCN:
  59. #ifdef CONFIG_B43_PHY_LCN
  60. phy->ops = &b43_phyops_lcn;
  61. #endif
  62. break;
  63. }
  64. if (B43_WARN_ON(!phy->ops))
  65. return -ENODEV;
  66. err = phy->ops->allocate(dev);
  67. if (err)
  68. phy->ops = NULL;
  69. return err;
  70. }
  71. void b43_phy_free(struct b43_wldev *dev)
  72. {
  73. dev->phy.ops->free(dev);
  74. dev->phy.ops = NULL;
  75. }
  76. int b43_phy_init(struct b43_wldev *dev)
  77. {
  78. struct b43_phy *phy = &dev->phy;
  79. const struct b43_phy_operations *ops = phy->ops;
  80. int err;
  81. phy->channel = ops->get_default_chan(dev);
  82. ops->software_rfkill(dev, false);
  83. err = ops->init(dev);
  84. if (err) {
  85. b43err(dev->wl, "PHY init failed\n");
  86. goto err_block_rf;
  87. }
  88. /* Make sure to switch hardware and firmware (SHM) to
  89. * the default channel. */
  90. err = b43_switch_channel(dev, ops->get_default_chan(dev));
  91. if (err) {
  92. b43err(dev->wl, "PHY init: Channel switch to default failed\n");
  93. goto err_phy_exit;
  94. }
  95. return 0;
  96. err_phy_exit:
  97. if (ops->exit)
  98. ops->exit(dev);
  99. err_block_rf:
  100. ops->software_rfkill(dev, true);
  101. return err;
  102. }
  103. void b43_phy_exit(struct b43_wldev *dev)
  104. {
  105. const struct b43_phy_operations *ops = dev->phy.ops;
  106. ops->software_rfkill(dev, true);
  107. if (ops->exit)
  108. ops->exit(dev);
  109. }
  110. bool b43_has_hardware_pctl(struct b43_wldev *dev)
  111. {
  112. if (!dev->phy.hardware_power_control)
  113. return 0;
  114. if (!dev->phy.ops->supports_hwpctl)
  115. return 0;
  116. return dev->phy.ops->supports_hwpctl(dev);
  117. }
  118. void b43_radio_lock(struct b43_wldev *dev)
  119. {
  120. u32 macctl;
  121. #if B43_DEBUG
  122. B43_WARN_ON(dev->phy.radio_locked);
  123. dev->phy.radio_locked = true;
  124. #endif
  125. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  126. macctl |= B43_MACCTL_RADIOLOCK;
  127. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  128. /* Commit the write and wait for the firmware
  129. * to finish any radio register access. */
  130. b43_read32(dev, B43_MMIO_MACCTL);
  131. udelay(10);
  132. }
  133. void b43_radio_unlock(struct b43_wldev *dev)
  134. {
  135. u32 macctl;
  136. #if B43_DEBUG
  137. B43_WARN_ON(!dev->phy.radio_locked);
  138. dev->phy.radio_locked = false;
  139. #endif
  140. /* Commit any write */
  141. b43_read16(dev, B43_MMIO_PHY_VER);
  142. /* unlock */
  143. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  144. macctl &= ~B43_MACCTL_RADIOLOCK;
  145. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  146. }
  147. void b43_phy_lock(struct b43_wldev *dev)
  148. {
  149. #if B43_DEBUG
  150. B43_WARN_ON(dev->phy.phy_locked);
  151. dev->phy.phy_locked = true;
  152. #endif
  153. B43_WARN_ON(dev->dev->core_rev < 3);
  154. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  155. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  156. }
  157. void b43_phy_unlock(struct b43_wldev *dev)
  158. {
  159. #if B43_DEBUG
  160. B43_WARN_ON(!dev->phy.phy_locked);
  161. dev->phy.phy_locked = false;
  162. #endif
  163. B43_WARN_ON(dev->dev->core_rev < 3);
  164. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  165. b43_power_saving_ctl_bits(dev, 0);
  166. }
  167. static inline void assert_mac_suspended(struct b43_wldev *dev)
  168. {
  169. if (!B43_DEBUG)
  170. return;
  171. if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
  172. (dev->mac_suspended <= 0)) {
  173. b43dbg(dev->wl, "PHY/RADIO register access with "
  174. "enabled MAC.\n");
  175. dump_stack();
  176. }
  177. }
  178. u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
  179. {
  180. assert_mac_suspended(dev);
  181. return dev->phy.ops->radio_read(dev, reg);
  182. }
  183. void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
  184. {
  185. assert_mac_suspended(dev);
  186. dev->phy.ops->radio_write(dev, reg, value);
  187. }
  188. void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  189. {
  190. b43_radio_write16(dev, offset,
  191. b43_radio_read16(dev, offset) & mask);
  192. }
  193. void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
  194. {
  195. b43_radio_write16(dev, offset,
  196. b43_radio_read16(dev, offset) | set);
  197. }
  198. void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  199. {
  200. b43_radio_write16(dev, offset,
  201. (b43_radio_read16(dev, offset) & mask) | set);
  202. }
  203. u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
  204. {
  205. assert_mac_suspended(dev);
  206. dev->phy.writes_counter = 0;
  207. return dev->phy.ops->phy_read(dev, reg);
  208. }
  209. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
  210. {
  211. assert_mac_suspended(dev);
  212. dev->phy.ops->phy_write(dev, reg, value);
  213. if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
  214. b43_read16(dev, B43_MMIO_PHY_VER);
  215. dev->phy.writes_counter = 0;
  216. }
  217. }
  218. void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
  219. {
  220. assert_mac_suspended(dev);
  221. dev->phy.ops->phy_write(dev, destreg,
  222. dev->phy.ops->phy_read(dev, srcreg));
  223. }
  224. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  225. {
  226. if (dev->phy.ops->phy_maskset) {
  227. assert_mac_suspended(dev);
  228. dev->phy.ops->phy_maskset(dev, offset, mask, 0);
  229. } else {
  230. b43_phy_write(dev, offset,
  231. b43_phy_read(dev, offset) & mask);
  232. }
  233. }
  234. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
  235. {
  236. if (dev->phy.ops->phy_maskset) {
  237. assert_mac_suspended(dev);
  238. dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
  239. } else {
  240. b43_phy_write(dev, offset,
  241. b43_phy_read(dev, offset) | set);
  242. }
  243. }
  244. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  245. {
  246. if (dev->phy.ops->phy_maskset) {
  247. assert_mac_suspended(dev);
  248. dev->phy.ops->phy_maskset(dev, offset, mask, set);
  249. } else {
  250. b43_phy_write(dev, offset,
  251. (b43_phy_read(dev, offset) & mask) | set);
  252. }
  253. }
  254. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
  255. {
  256. struct b43_phy *phy = &(dev->phy);
  257. u16 channelcookie, savedcookie;
  258. int err;
  259. if (new_channel == B43_DEFAULT_CHANNEL)
  260. new_channel = phy->ops->get_default_chan(dev);
  261. /* First we set the channel radio code to prevent the
  262. * firmware from sending ghost packets.
  263. */
  264. channelcookie = new_channel;
  265. if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
  266. channelcookie |= B43_SHM_SH_CHAN_5GHZ;
  267. /* FIXME: set 40Mhz flag if required */
  268. if (0)
  269. channelcookie |= B43_SHM_SH_CHAN_40MHZ;
  270. savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
  271. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
  272. /* Now try to switch the PHY hardware channel. */
  273. err = phy->ops->switch_channel(dev, new_channel);
  274. if (err)
  275. goto err_restore_cookie;
  276. dev->phy.channel = new_channel;
  277. /* Wait for the radio to tune to the channel and stabilize. */
  278. msleep(8);
  279. return 0;
  280. err_restore_cookie:
  281. b43_shm_write16(dev, B43_SHM_SHARED,
  282. B43_SHM_SH_CHAN, savedcookie);
  283. return err;
  284. }
  285. void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
  286. {
  287. struct b43_phy *phy = &dev->phy;
  288. b43_mac_suspend(dev);
  289. phy->ops->software_rfkill(dev, blocked);
  290. phy->radio_on = !blocked;
  291. b43_mac_enable(dev);
  292. }
  293. /**
  294. * b43_phy_txpower_adjust_work - TX power workqueue.
  295. *
  296. * Workqueue for updating the TX power parameters in hardware.
  297. */
  298. void b43_phy_txpower_adjust_work(struct work_struct *work)
  299. {
  300. struct b43_wl *wl = container_of(work, struct b43_wl,
  301. txpower_adjust_work);
  302. struct b43_wldev *dev;
  303. mutex_lock(&wl->mutex);
  304. dev = wl->current_dev;
  305. if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
  306. dev->phy.ops->adjust_txpower(dev);
  307. mutex_unlock(&wl->mutex);
  308. }
  309. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
  310. {
  311. struct b43_phy *phy = &dev->phy;
  312. unsigned long now = jiffies;
  313. enum b43_txpwr_result result;
  314. if (!(flags & B43_TXPWR_IGNORE_TIME)) {
  315. /* Check if it's time for a TXpower check. */
  316. if (time_before(now, phy->next_txpwr_check_time))
  317. return; /* Not yet */
  318. }
  319. /* The next check will be needed in two seconds, or later. */
  320. phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
  321. if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
  322. (dev->dev->board_type == SSB_BOARD_BU4306))
  323. return; /* No software txpower adjustment needed */
  324. result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
  325. if (result == B43_TXPWR_RES_DONE)
  326. return; /* We are done. */
  327. B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
  328. B43_WARN_ON(phy->ops->adjust_txpower == NULL);
  329. /* We must adjust the transmission power in hardware.
  330. * Schedule b43_phy_txpower_adjust_work(). */
  331. ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
  332. }
  333. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
  334. {
  335. const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
  336. unsigned int a, b, c, d;
  337. unsigned int average;
  338. u32 tmp;
  339. tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
  340. a = tmp & 0xFF;
  341. b = (tmp >> 8) & 0xFF;
  342. c = (tmp >> 16) & 0xFF;
  343. d = (tmp >> 24) & 0xFF;
  344. if (a == 0 || a == B43_TSSI_MAX ||
  345. b == 0 || b == B43_TSSI_MAX ||
  346. c == 0 || c == B43_TSSI_MAX ||
  347. d == 0 || d == B43_TSSI_MAX)
  348. return -ENOENT;
  349. /* The values are OK. Clear them. */
  350. tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
  351. (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
  352. b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
  353. if (is_ofdm) {
  354. a = (a + 32) & 0x3F;
  355. b = (b + 32) & 0x3F;
  356. c = (c + 32) & 0x3F;
  357. d = (d + 32) & 0x3F;
  358. }
  359. /* Get the average of the values with 0.5 added to each value. */
  360. average = (a + b + c + d + 2) / 4;
  361. if (is_ofdm) {
  362. /* Adjust for CCK-boost */
  363. if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO)
  364. & B43_HF_CCKBOOST)
  365. average = (average >= 13) ? (average - 13) : 0;
  366. }
  367. return average;
  368. }
  369. void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
  370. {
  371. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  372. }
  373. bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
  374. {
  375. return (channel_type == NL80211_CHAN_HT40MINUS ||
  376. channel_type == NL80211_CHAN_HT40PLUS);
  377. }
  378. /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
  379. void b43_phy_force_clock(struct b43_wldev *dev, bool force)
  380. {
  381. u32 tmp;
  382. WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
  383. dev->phy.type != B43_PHYTYPE_HT);
  384. switch (dev->dev->bus_type) {
  385. #ifdef CONFIG_B43_BCMA
  386. case B43_BUS_BCMA:
  387. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  388. if (force)
  389. tmp |= BCMA_IOCTL_FGC;
  390. else
  391. tmp &= ~BCMA_IOCTL_FGC;
  392. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  393. break;
  394. #endif
  395. #ifdef CONFIG_B43_SSB
  396. case B43_BUS_SSB:
  397. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  398. if (force)
  399. tmp |= SSB_TMSLOW_FGC;
  400. else
  401. tmp &= ~SSB_TMSLOW_FGC;
  402. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  403. break;
  404. #endif
  405. }
  406. }
  407. /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
  408. struct b43_c32 b43_cordic(int theta)
  409. {
  410. static const u32 arctg[] = {
  411. 2949120, 1740967, 919879, 466945, 234379, 117304,
  412. 58666, 29335, 14668, 7334, 3667, 1833,
  413. 917, 458, 229, 115, 57, 29,
  414. };
  415. u8 i;
  416. s32 tmp;
  417. s8 signx = 1;
  418. u32 angle = 0;
  419. struct b43_c32 ret = { .i = 39797, .q = 0, };
  420. while (theta > (180 << 16))
  421. theta -= (360 << 16);
  422. while (theta < -(180 << 16))
  423. theta += (360 << 16);
  424. if (theta > (90 << 16)) {
  425. theta -= (180 << 16);
  426. signx = -1;
  427. } else if (theta < -(90 << 16)) {
  428. theta += (180 << 16);
  429. signx = -1;
  430. }
  431. for (i = 0; i <= 17; i++) {
  432. if (theta > angle) {
  433. tmp = ret.i - (ret.q >> i);
  434. ret.q += ret.i >> i;
  435. ret.i = tmp;
  436. angle += arctg[i];
  437. } else {
  438. tmp = ret.i + (ret.q >> i);
  439. ret.q -= ret.i >> i;
  440. ret.i = tmp;
  441. angle -= arctg[i];
  442. }
  443. }
  444. ret.i *= signx;
  445. ret.q *= signx;
  446. return ret;
  447. }