phy_common.c 12 KB

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