phy_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
  204. u16 value, int delay, int timeout)
  205. {
  206. u16 val;
  207. int i;
  208. for (i = 0; i < timeout; i += delay) {
  209. val = b43_radio_read(dev, offset);
  210. if ((val & mask) == value)
  211. return true;
  212. udelay(delay);
  213. }
  214. return false;
  215. }
  216. u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
  217. {
  218. assert_mac_suspended(dev);
  219. dev->phy.writes_counter = 0;
  220. return dev->phy.ops->phy_read(dev, reg);
  221. }
  222. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
  223. {
  224. assert_mac_suspended(dev);
  225. dev->phy.ops->phy_write(dev, reg, value);
  226. if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
  227. b43_read16(dev, B43_MMIO_PHY_VER);
  228. dev->phy.writes_counter = 0;
  229. }
  230. }
  231. void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
  232. {
  233. assert_mac_suspended(dev);
  234. dev->phy.ops->phy_write(dev, destreg,
  235. dev->phy.ops->phy_read(dev, srcreg));
  236. }
  237. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  238. {
  239. if (dev->phy.ops->phy_maskset) {
  240. assert_mac_suspended(dev);
  241. dev->phy.ops->phy_maskset(dev, offset, mask, 0);
  242. } else {
  243. b43_phy_write(dev, offset,
  244. b43_phy_read(dev, offset) & mask);
  245. }
  246. }
  247. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
  248. {
  249. if (dev->phy.ops->phy_maskset) {
  250. assert_mac_suspended(dev);
  251. dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
  252. } else {
  253. b43_phy_write(dev, offset,
  254. b43_phy_read(dev, offset) | set);
  255. }
  256. }
  257. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  258. {
  259. if (dev->phy.ops->phy_maskset) {
  260. assert_mac_suspended(dev);
  261. dev->phy.ops->phy_maskset(dev, offset, mask, set);
  262. } else {
  263. b43_phy_write(dev, offset,
  264. (b43_phy_read(dev, offset) & mask) | set);
  265. }
  266. }
  267. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
  268. {
  269. struct b43_phy *phy = &(dev->phy);
  270. u16 channelcookie, savedcookie;
  271. int err;
  272. if (new_channel == B43_DEFAULT_CHANNEL)
  273. new_channel = phy->ops->get_default_chan(dev);
  274. /* First we set the channel radio code to prevent the
  275. * firmware from sending ghost packets.
  276. */
  277. channelcookie = new_channel;
  278. if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
  279. channelcookie |= B43_SHM_SH_CHAN_5GHZ;
  280. /* FIXME: set 40Mhz flag if required */
  281. if (0)
  282. channelcookie |= B43_SHM_SH_CHAN_40MHZ;
  283. savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
  284. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
  285. /* Now try to switch the PHY hardware channel. */
  286. err = phy->ops->switch_channel(dev, new_channel);
  287. if (err)
  288. goto err_restore_cookie;
  289. dev->phy.channel = new_channel;
  290. /* Wait for the radio to tune to the channel and stabilize. */
  291. msleep(8);
  292. return 0;
  293. err_restore_cookie:
  294. b43_shm_write16(dev, B43_SHM_SHARED,
  295. B43_SHM_SH_CHAN, savedcookie);
  296. return err;
  297. }
  298. void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
  299. {
  300. struct b43_phy *phy = &dev->phy;
  301. b43_mac_suspend(dev);
  302. phy->ops->software_rfkill(dev, blocked);
  303. phy->radio_on = !blocked;
  304. b43_mac_enable(dev);
  305. }
  306. /**
  307. * b43_phy_txpower_adjust_work - TX power workqueue.
  308. *
  309. * Workqueue for updating the TX power parameters in hardware.
  310. */
  311. void b43_phy_txpower_adjust_work(struct work_struct *work)
  312. {
  313. struct b43_wl *wl = container_of(work, struct b43_wl,
  314. txpower_adjust_work);
  315. struct b43_wldev *dev;
  316. mutex_lock(&wl->mutex);
  317. dev = wl->current_dev;
  318. if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
  319. dev->phy.ops->adjust_txpower(dev);
  320. mutex_unlock(&wl->mutex);
  321. }
  322. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
  323. {
  324. struct b43_phy *phy = &dev->phy;
  325. unsigned long now = jiffies;
  326. enum b43_txpwr_result result;
  327. if (!(flags & B43_TXPWR_IGNORE_TIME)) {
  328. /* Check if it's time for a TXpower check. */
  329. if (time_before(now, phy->next_txpwr_check_time))
  330. return; /* Not yet */
  331. }
  332. /* The next check will be needed in two seconds, or later. */
  333. phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
  334. if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
  335. (dev->dev->board_type == SSB_BOARD_BU4306))
  336. return; /* No software txpower adjustment needed */
  337. result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
  338. if (result == B43_TXPWR_RES_DONE)
  339. return; /* We are done. */
  340. B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
  341. B43_WARN_ON(phy->ops->adjust_txpower == NULL);
  342. /* We must adjust the transmission power in hardware.
  343. * Schedule b43_phy_txpower_adjust_work(). */
  344. ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
  345. }
  346. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
  347. {
  348. const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
  349. unsigned int a, b, c, d;
  350. unsigned int average;
  351. u32 tmp;
  352. tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
  353. a = tmp & 0xFF;
  354. b = (tmp >> 8) & 0xFF;
  355. c = (tmp >> 16) & 0xFF;
  356. d = (tmp >> 24) & 0xFF;
  357. if (a == 0 || a == B43_TSSI_MAX ||
  358. b == 0 || b == B43_TSSI_MAX ||
  359. c == 0 || c == B43_TSSI_MAX ||
  360. d == 0 || d == B43_TSSI_MAX)
  361. return -ENOENT;
  362. /* The values are OK. Clear them. */
  363. tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
  364. (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
  365. b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
  366. if (is_ofdm) {
  367. a = (a + 32) & 0x3F;
  368. b = (b + 32) & 0x3F;
  369. c = (c + 32) & 0x3F;
  370. d = (d + 32) & 0x3F;
  371. }
  372. /* Get the average of the values with 0.5 added to each value. */
  373. average = (a + b + c + d + 2) / 4;
  374. if (is_ofdm) {
  375. /* Adjust for CCK-boost */
  376. if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
  377. & B43_HF_CCKBOOST)
  378. average = (average >= 13) ? (average - 13) : 0;
  379. }
  380. return average;
  381. }
  382. void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
  383. {
  384. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  385. }
  386. bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
  387. {
  388. return (channel_type == NL80211_CHAN_HT40MINUS ||
  389. channel_type == NL80211_CHAN_HT40PLUS);
  390. }
  391. /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
  392. void b43_phy_force_clock(struct b43_wldev *dev, bool force)
  393. {
  394. u32 tmp;
  395. WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
  396. dev->phy.type != B43_PHYTYPE_HT);
  397. switch (dev->dev->bus_type) {
  398. #ifdef CONFIG_B43_BCMA
  399. case B43_BUS_BCMA:
  400. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  401. if (force)
  402. tmp |= BCMA_IOCTL_FGC;
  403. else
  404. tmp &= ~BCMA_IOCTL_FGC;
  405. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  406. break;
  407. #endif
  408. #ifdef CONFIG_B43_SSB
  409. case B43_BUS_SSB:
  410. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  411. if (force)
  412. tmp |= SSB_TMSLOW_FGC;
  413. else
  414. tmp &= ~SSB_TMSLOW_FGC;
  415. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  416. break;
  417. #endif
  418. }
  419. }
  420. /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
  421. struct b43_c32 b43_cordic(int theta)
  422. {
  423. static const u32 arctg[] = {
  424. 2949120, 1740967, 919879, 466945, 234379, 117304,
  425. 58666, 29335, 14668, 7334, 3667, 1833,
  426. 917, 458, 229, 115, 57, 29,
  427. };
  428. u8 i;
  429. s32 tmp;
  430. s8 signx = 1;
  431. u32 angle = 0;
  432. struct b43_c32 ret = { .i = 39797, .q = 0, };
  433. while (theta > (180 << 16))
  434. theta -= (360 << 16);
  435. while (theta < -(180 << 16))
  436. theta += (360 << 16);
  437. if (theta > (90 << 16)) {
  438. theta -= (180 << 16);
  439. signx = -1;
  440. } else if (theta < -(90 << 16)) {
  441. theta += (180 << 16);
  442. signx = -1;
  443. }
  444. for (i = 0; i <= 17; i++) {
  445. if (theta > angle) {
  446. tmp = ret.i - (ret.q >> i);
  447. ret.q += ret.i >> i;
  448. ret.i = tmp;
  449. angle += arctg[i];
  450. } else {
  451. tmp = ret.i + (ret.q >> i);
  452. ret.q -= ret.i >> i;
  453. ret.i = tmp;
  454. angle -= arctg[i];
  455. }
  456. }
  457. ret.i *= signx;
  458. ret.q *= signx;
  459. return ret;
  460. }