phy_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 <mb@bu3sch.de>
  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 "b43.h"
  29. #include "main.h"
  30. int b43_phy_allocate(struct b43_wldev *dev)
  31. {
  32. struct b43_phy *phy = &(dev->phy);
  33. int err;
  34. phy->ops = NULL;
  35. switch (phy->type) {
  36. case B43_PHYTYPE_A:
  37. phy->ops = &b43_phyops_a;
  38. break;
  39. case B43_PHYTYPE_G:
  40. phy->ops = &b43_phyops_g;
  41. break;
  42. case B43_PHYTYPE_N:
  43. #ifdef CONFIG_B43_PHY_N
  44. phy->ops = &b43_phyops_n;
  45. #endif
  46. break;
  47. case B43_PHYTYPE_LP:
  48. #ifdef CONFIG_B43_PHY_LP
  49. phy->ops = &b43_phyops_lp;
  50. #endif
  51. break;
  52. case B43_PHYTYPE_HT:
  53. #ifdef CONFIG_B43_PHY_HT
  54. phy->ops = &b43_phyops_ht;
  55. #endif
  56. break;
  57. }
  58. if (B43_WARN_ON(!phy->ops))
  59. return -ENODEV;
  60. err = phy->ops->allocate(dev);
  61. if (err)
  62. phy->ops = NULL;
  63. return err;
  64. }
  65. void b43_phy_free(struct b43_wldev *dev)
  66. {
  67. dev->phy.ops->free(dev);
  68. dev->phy.ops = NULL;
  69. }
  70. int b43_phy_init(struct b43_wldev *dev)
  71. {
  72. struct b43_phy *phy = &dev->phy;
  73. const struct b43_phy_operations *ops = phy->ops;
  74. int err;
  75. phy->channel = ops->get_default_chan(dev);
  76. ops->software_rfkill(dev, false);
  77. err = ops->init(dev);
  78. if (err) {
  79. b43err(dev->wl, "PHY init failed\n");
  80. goto err_block_rf;
  81. }
  82. /* Make sure to switch hardware and firmware (SHM) to
  83. * the default channel. */
  84. err = b43_switch_channel(dev, ops->get_default_chan(dev));
  85. if (err) {
  86. b43err(dev->wl, "PHY init: Channel switch to default failed\n");
  87. goto err_phy_exit;
  88. }
  89. return 0;
  90. err_phy_exit:
  91. if (ops->exit)
  92. ops->exit(dev);
  93. err_block_rf:
  94. ops->software_rfkill(dev, true);
  95. return err;
  96. }
  97. void b43_phy_exit(struct b43_wldev *dev)
  98. {
  99. const struct b43_phy_operations *ops = dev->phy.ops;
  100. ops->software_rfkill(dev, true);
  101. if (ops->exit)
  102. ops->exit(dev);
  103. }
  104. bool b43_has_hardware_pctl(struct b43_wldev *dev)
  105. {
  106. if (!dev->phy.hardware_power_control)
  107. return 0;
  108. if (!dev->phy.ops->supports_hwpctl)
  109. return 0;
  110. return dev->phy.ops->supports_hwpctl(dev);
  111. }
  112. void b43_radio_lock(struct b43_wldev *dev)
  113. {
  114. u32 macctl;
  115. #if B43_DEBUG
  116. B43_WARN_ON(dev->phy.radio_locked);
  117. dev->phy.radio_locked = 1;
  118. #endif
  119. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  120. macctl |= B43_MACCTL_RADIOLOCK;
  121. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  122. /* Commit the write and wait for the firmware
  123. * to finish any radio register access. */
  124. b43_read32(dev, B43_MMIO_MACCTL);
  125. udelay(10);
  126. }
  127. void b43_radio_unlock(struct b43_wldev *dev)
  128. {
  129. u32 macctl;
  130. #if B43_DEBUG
  131. B43_WARN_ON(!dev->phy.radio_locked);
  132. dev->phy.radio_locked = 0;
  133. #endif
  134. /* Commit any write */
  135. b43_read16(dev, B43_MMIO_PHY_VER);
  136. /* unlock */
  137. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  138. macctl &= ~B43_MACCTL_RADIOLOCK;
  139. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  140. }
  141. void b43_phy_lock(struct b43_wldev *dev)
  142. {
  143. #if B43_DEBUG
  144. B43_WARN_ON(dev->phy.phy_locked);
  145. dev->phy.phy_locked = 1;
  146. #endif
  147. B43_WARN_ON(dev->dev->core_rev < 3);
  148. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  149. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  150. }
  151. void b43_phy_unlock(struct b43_wldev *dev)
  152. {
  153. #if B43_DEBUG
  154. B43_WARN_ON(!dev->phy.phy_locked);
  155. dev->phy.phy_locked = 0;
  156. #endif
  157. B43_WARN_ON(dev->dev->core_rev < 3);
  158. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  159. b43_power_saving_ctl_bits(dev, 0);
  160. }
  161. static inline void assert_mac_suspended(struct b43_wldev *dev)
  162. {
  163. if (!B43_DEBUG)
  164. return;
  165. if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
  166. (dev->mac_suspended <= 0)) {
  167. b43dbg(dev->wl, "PHY/RADIO register access with "
  168. "enabled MAC.\n");
  169. dump_stack();
  170. }
  171. }
  172. u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
  173. {
  174. assert_mac_suspended(dev);
  175. return dev->phy.ops->radio_read(dev, reg);
  176. }
  177. void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
  178. {
  179. assert_mac_suspended(dev);
  180. dev->phy.ops->radio_write(dev, reg, value);
  181. }
  182. void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  183. {
  184. b43_radio_write16(dev, offset,
  185. b43_radio_read16(dev, offset) & mask);
  186. }
  187. void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
  188. {
  189. b43_radio_write16(dev, offset,
  190. b43_radio_read16(dev, offset) | set);
  191. }
  192. void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  193. {
  194. b43_radio_write16(dev, offset,
  195. (b43_radio_read16(dev, offset) & mask) | set);
  196. }
  197. u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
  198. {
  199. assert_mac_suspended(dev);
  200. dev->phy.writes_counter = 0;
  201. return dev->phy.ops->phy_read(dev, reg);
  202. }
  203. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
  204. {
  205. assert_mac_suspended(dev);
  206. dev->phy.ops->phy_write(dev, reg, value);
  207. if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
  208. b43_read16(dev, B43_MMIO_PHY_VER);
  209. dev->phy.writes_counter = 0;
  210. }
  211. }
  212. void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
  213. {
  214. assert_mac_suspended(dev);
  215. dev->phy.ops->phy_write(dev, destreg,
  216. dev->phy.ops->phy_read(dev, srcreg));
  217. }
  218. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  219. {
  220. if (dev->phy.ops->phy_maskset) {
  221. assert_mac_suspended(dev);
  222. dev->phy.ops->phy_maskset(dev, offset, mask, 0);
  223. } else {
  224. b43_phy_write(dev, offset,
  225. b43_phy_read(dev, offset) & mask);
  226. }
  227. }
  228. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
  229. {
  230. if (dev->phy.ops->phy_maskset) {
  231. assert_mac_suspended(dev);
  232. dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
  233. } else {
  234. b43_phy_write(dev, offset,
  235. b43_phy_read(dev, offset) | set);
  236. }
  237. }
  238. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  239. {
  240. if (dev->phy.ops->phy_maskset) {
  241. assert_mac_suspended(dev);
  242. dev->phy.ops->phy_maskset(dev, offset, mask, set);
  243. } else {
  244. b43_phy_write(dev, offset,
  245. (b43_phy_read(dev, offset) & mask) | set);
  246. }
  247. }
  248. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
  249. {
  250. struct b43_phy *phy = &(dev->phy);
  251. u16 channelcookie, savedcookie;
  252. int err;
  253. if (new_channel == B43_DEFAULT_CHANNEL)
  254. new_channel = phy->ops->get_default_chan(dev);
  255. /* First we set the channel radio code to prevent the
  256. * firmware from sending ghost packets.
  257. */
  258. channelcookie = new_channel;
  259. if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
  260. channelcookie |= B43_SHM_SH_CHAN_5GHZ;
  261. /* FIXME: set 40Mhz flag if required */
  262. if (0)
  263. channelcookie |= B43_SHM_SH_CHAN_40MHZ;
  264. savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
  265. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
  266. /* Now try to switch the PHY hardware channel. */
  267. err = phy->ops->switch_channel(dev, new_channel);
  268. if (err)
  269. goto err_restore_cookie;
  270. dev->phy.channel = new_channel;
  271. /* Wait for the radio to tune to the channel and stabilize. */
  272. msleep(8);
  273. return 0;
  274. err_restore_cookie:
  275. b43_shm_write16(dev, B43_SHM_SHARED,
  276. B43_SHM_SH_CHAN, savedcookie);
  277. return err;
  278. }
  279. void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
  280. {
  281. struct b43_phy *phy = &dev->phy;
  282. b43_mac_suspend(dev);
  283. phy->ops->software_rfkill(dev, blocked);
  284. phy->radio_on = !blocked;
  285. b43_mac_enable(dev);
  286. }
  287. /**
  288. * b43_phy_txpower_adjust_work - TX power workqueue.
  289. *
  290. * Workqueue for updating the TX power parameters in hardware.
  291. */
  292. void b43_phy_txpower_adjust_work(struct work_struct *work)
  293. {
  294. struct b43_wl *wl = container_of(work, struct b43_wl,
  295. txpower_adjust_work);
  296. struct b43_wldev *dev;
  297. mutex_lock(&wl->mutex);
  298. dev = wl->current_dev;
  299. if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
  300. dev->phy.ops->adjust_txpower(dev);
  301. mutex_unlock(&wl->mutex);
  302. }
  303. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
  304. {
  305. struct b43_phy *phy = &dev->phy;
  306. unsigned long now = jiffies;
  307. enum b43_txpwr_result result;
  308. if (!(flags & B43_TXPWR_IGNORE_TIME)) {
  309. /* Check if it's time for a TXpower check. */
  310. if (time_before(now, phy->next_txpwr_check_time))
  311. return; /* Not yet */
  312. }
  313. /* The next check will be needed in two seconds, or later. */
  314. phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
  315. if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
  316. (dev->dev->board_type == SSB_BOARD_BU4306))
  317. return; /* No software txpower adjustment needed */
  318. result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
  319. if (result == B43_TXPWR_RES_DONE)
  320. return; /* We are done. */
  321. B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
  322. B43_WARN_ON(phy->ops->adjust_txpower == NULL);
  323. /* We must adjust the transmission power in hardware.
  324. * Schedule b43_phy_txpower_adjust_work(). */
  325. ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
  326. }
  327. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
  328. {
  329. const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
  330. unsigned int a, b, c, d;
  331. unsigned int average;
  332. u32 tmp;
  333. tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
  334. a = tmp & 0xFF;
  335. b = (tmp >> 8) & 0xFF;
  336. c = (tmp >> 16) & 0xFF;
  337. d = (tmp >> 24) & 0xFF;
  338. if (a == 0 || a == B43_TSSI_MAX ||
  339. b == 0 || b == B43_TSSI_MAX ||
  340. c == 0 || c == B43_TSSI_MAX ||
  341. d == 0 || d == B43_TSSI_MAX)
  342. return -ENOENT;
  343. /* The values are OK. Clear them. */
  344. tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
  345. (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
  346. b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
  347. if (is_ofdm) {
  348. a = (a + 32) & 0x3F;
  349. b = (b + 32) & 0x3F;
  350. c = (c + 32) & 0x3F;
  351. d = (d + 32) & 0x3F;
  352. }
  353. /* Get the average of the values with 0.5 added to each value. */
  354. average = (a + b + c + d + 2) / 4;
  355. if (is_ofdm) {
  356. /* Adjust for CCK-boost */
  357. if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO)
  358. & B43_HF_CCKBOOST)
  359. average = (average >= 13) ? (average - 13) : 0;
  360. }
  361. return average;
  362. }
  363. void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
  364. {
  365. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  366. }
  367. bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
  368. {
  369. return (channel_type == NL80211_CHAN_HT40MINUS ||
  370. channel_type == NL80211_CHAN_HT40PLUS);
  371. }
  372. /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
  373. struct b43_c32 b43_cordic(int theta)
  374. {
  375. static const u32 arctg[] = {
  376. 2949120, 1740967, 919879, 466945, 234379, 117304,
  377. 58666, 29335, 14668, 7334, 3667, 1833,
  378. 917, 458, 229, 115, 57, 29,
  379. };
  380. u8 i;
  381. s32 tmp;
  382. s8 signx = 1;
  383. u32 angle = 0;
  384. struct b43_c32 ret = { .i = 39797, .q = 0, };
  385. while (theta > (180 << 16))
  386. theta -= (360 << 16);
  387. while (theta < -(180 << 16))
  388. theta += (360 << 16);
  389. if (theta > (90 << 16)) {
  390. theta -= (180 << 16);
  391. signx = -1;
  392. } else if (theta < -(90 << 16)) {
  393. theta += (180 << 16);
  394. signx = -1;
  395. }
  396. for (i = 0; i <= 17; i++) {
  397. if (theta > angle) {
  398. tmp = ret.i - (ret.q >> i);
  399. ret.q += ret.i >> i;
  400. ret.i = tmp;
  401. angle += arctg[i];
  402. } else {
  403. tmp = ret.i + (ret.q >> i);
  404. ret.q -= ret.i >> i;
  405. ret.i = tmp;
  406. angle -= arctg[i];
  407. }
  408. }
  409. ret.i *= signx;
  410. ret.q *= signx;
  411. return ret;
  412. }