phy_common.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_operations_setup(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_NPHY
  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. int b43_phy_init(struct b43_wldev *dev)
  60. {
  61. struct b43_phy *phy = &dev->phy;
  62. const struct b43_phy_operations *ops = phy->ops;
  63. int err;
  64. phy->channel = ops->get_default_chan(dev);
  65. ops->software_rfkill(dev, RFKILL_STATE_UNBLOCKED);
  66. err = ops->init(dev);
  67. if (err) {
  68. b43err(dev->wl, "PHY init failed\n");
  69. goto err_block_rf;
  70. }
  71. /* Make sure to switch hardware and firmware (SHM) to
  72. * the default channel. */
  73. err = b43_switch_channel(dev, ops->get_default_chan(dev));
  74. if (err) {
  75. b43err(dev->wl, "PHY init: Channel switch to default failed\n");
  76. goto err_phy_exit;
  77. }
  78. return 0;
  79. err_phy_exit:
  80. if (ops->exit)
  81. ops->exit(dev);
  82. err_block_rf:
  83. ops->software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
  84. return err;
  85. }
  86. void b43_phy_exit(struct b43_wldev *dev)
  87. {
  88. const struct b43_phy_operations *ops = dev->phy.ops;
  89. ops->software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
  90. if (ops->exit)
  91. ops->exit(dev);
  92. }
  93. bool b43_has_hardware_pctl(struct b43_wldev *dev)
  94. {
  95. if (!dev->phy.hardware_power_control)
  96. return 0;
  97. if (!dev->phy.ops->supports_hwpctl)
  98. return 0;
  99. return dev->phy.ops->supports_hwpctl(dev);
  100. }
  101. void b43_radio_lock(struct b43_wldev *dev)
  102. {
  103. u32 macctl;
  104. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  105. B43_WARN_ON(macctl & B43_MACCTL_RADIOLOCK);
  106. macctl |= B43_MACCTL_RADIOLOCK;
  107. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  108. /* Commit the write and wait for the device
  109. * to exit any radio register access. */
  110. b43_read32(dev, B43_MMIO_MACCTL);
  111. udelay(10);
  112. }
  113. void b43_radio_unlock(struct b43_wldev *dev)
  114. {
  115. u32 macctl;
  116. /* Commit any write */
  117. b43_read16(dev, B43_MMIO_PHY_VER);
  118. /* unlock */
  119. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  120. B43_WARN_ON(!(macctl & B43_MACCTL_RADIOLOCK));
  121. macctl &= ~B43_MACCTL_RADIOLOCK;
  122. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  123. }
  124. void b43_phy_lock(struct b43_wldev *dev)
  125. {
  126. #if B43_DEBUG
  127. B43_WARN_ON(dev->phy.phy_locked);
  128. dev->phy.phy_locked = 1;
  129. #endif
  130. B43_WARN_ON(dev->dev->id.revision < 3);
  131. if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
  132. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  133. }
  134. void b43_phy_unlock(struct b43_wldev *dev)
  135. {
  136. #if B43_DEBUG
  137. B43_WARN_ON(!dev->phy.phy_locked);
  138. dev->phy.phy_locked = 0;
  139. #endif
  140. B43_WARN_ON(dev->dev->id.revision < 3);
  141. if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
  142. b43_power_saving_ctl_bits(dev, 0);
  143. }
  144. u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
  145. {
  146. return dev->phy.ops->radio_read(dev, reg);
  147. }
  148. void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
  149. {
  150. dev->phy.ops->radio_write(dev, reg, value);
  151. }
  152. void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  153. {
  154. b43_radio_write16(dev, offset,
  155. b43_radio_read16(dev, offset) & mask);
  156. }
  157. void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
  158. {
  159. b43_radio_write16(dev, offset,
  160. b43_radio_read16(dev, offset) | set);
  161. }
  162. void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  163. {
  164. b43_radio_write16(dev, offset,
  165. (b43_radio_read16(dev, offset) & mask) | set);
  166. }
  167. u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
  168. {
  169. return dev->phy.ops->phy_read(dev, reg);
  170. }
  171. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
  172. {
  173. dev->phy.ops->phy_write(dev, reg, value);
  174. }
  175. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  176. {
  177. b43_phy_write(dev, offset,
  178. b43_phy_read(dev, offset) & mask);
  179. }
  180. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
  181. {
  182. b43_phy_write(dev, offset,
  183. b43_phy_read(dev, offset) | set);
  184. }
  185. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  186. {
  187. b43_phy_write(dev, offset,
  188. (b43_phy_read(dev, offset) & mask) | set);
  189. }
  190. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
  191. {
  192. struct b43_phy *phy = &(dev->phy);
  193. u16 channelcookie, savedcookie;
  194. int err;
  195. if (new_channel == B43_DEFAULT_CHANNEL)
  196. new_channel = phy->ops->get_default_chan(dev);
  197. /* First we set the channel radio code to prevent the
  198. * firmware from sending ghost packets.
  199. */
  200. channelcookie = new_channel;
  201. if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
  202. channelcookie |= 0x100;
  203. //FIXME set 40Mhz flag if required
  204. savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
  205. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
  206. /* Now try to switch the PHY hardware channel. */
  207. err = phy->ops->switch_channel(dev, new_channel);
  208. if (err)
  209. goto err_restore_cookie;
  210. dev->phy.channel = new_channel;
  211. /* Wait for the radio to tune to the channel and stabilize. */
  212. msleep(8);
  213. return 0;
  214. err_restore_cookie:
  215. b43_shm_write16(dev, B43_SHM_SHARED,
  216. B43_SHM_SH_CHAN, savedcookie);
  217. return err;
  218. }
  219. void b43_software_rfkill(struct b43_wldev *dev, enum rfkill_state state)
  220. {
  221. struct b43_phy *phy = &dev->phy;
  222. if (state == RFKILL_STATE_HARD_BLOCKED) {
  223. /* We cannot hardware-block the device */
  224. state = RFKILL_STATE_SOFT_BLOCKED;
  225. }
  226. phy->ops->software_rfkill(dev, state);
  227. phy->radio_on = (state == RFKILL_STATE_UNBLOCKED);
  228. }
  229. /**
  230. * b43_phy_txpower_adjust_work - TX power workqueue.
  231. *
  232. * Workqueue for updating the TX power parameters in hardware.
  233. */
  234. void b43_phy_txpower_adjust_work(struct work_struct *work)
  235. {
  236. struct b43_wl *wl = container_of(work, struct b43_wl,
  237. txpower_adjust_work);
  238. struct b43_wldev *dev;
  239. mutex_lock(&wl->mutex);
  240. dev = wl->current_dev;
  241. if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
  242. dev->phy.ops->adjust_txpower(dev);
  243. mutex_unlock(&wl->mutex);
  244. }
  245. /* Called with wl->irq_lock locked */
  246. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
  247. {
  248. struct b43_phy *phy = &dev->phy;
  249. unsigned long now = jiffies;
  250. enum b43_txpwr_result result;
  251. if (!(flags & B43_TXPWR_IGNORE_TIME)) {
  252. /* Check if it's time for a TXpower check. */
  253. if (time_before(now, phy->next_txpwr_check_time))
  254. return; /* Not yet */
  255. }
  256. /* The next check will be needed in two seconds, or later. */
  257. phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
  258. if ((dev->dev->bus->boardinfo.vendor == SSB_BOARDVENDOR_BCM) &&
  259. (dev->dev->bus->boardinfo.type == SSB_BOARD_BU4306))
  260. return; /* No software txpower adjustment needed */
  261. result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
  262. if (result == B43_TXPWR_RES_DONE)
  263. return; /* We are done. */
  264. B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
  265. B43_WARN_ON(phy->ops->adjust_txpower == NULL);
  266. /* We must adjust the transmission power in hardware.
  267. * Schedule b43_phy_txpower_adjust_work(). */
  268. queue_work(dev->wl->hw->workqueue, &dev->wl->txpower_adjust_work);
  269. }
  270. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
  271. {
  272. const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
  273. unsigned int a, b, c, d;
  274. unsigned int average;
  275. u32 tmp;
  276. tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
  277. a = tmp & 0xFF;
  278. b = (tmp >> 8) & 0xFF;
  279. c = (tmp >> 16) & 0xFF;
  280. d = (tmp >> 24) & 0xFF;
  281. if (a == 0 || a == B43_TSSI_MAX ||
  282. b == 0 || b == B43_TSSI_MAX ||
  283. c == 0 || c == B43_TSSI_MAX ||
  284. d == 0 || d == B43_TSSI_MAX)
  285. return -ENOENT;
  286. /* The values are OK. Clear them. */
  287. tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
  288. (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
  289. b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
  290. if (is_ofdm) {
  291. a = (a + 32) & 0x3F;
  292. b = (b + 32) & 0x3F;
  293. c = (c + 32) & 0x3F;
  294. d = (d + 32) & 0x3F;
  295. }
  296. /* Get the average of the values with 0.5 added to each value. */
  297. average = (a + b + c + d + 2) / 4;
  298. if (is_ofdm) {
  299. /* Adjust for CCK-boost */
  300. if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO)
  301. & B43_HF_CCKBOOST)
  302. average = (average >= 13) ? (average - 13) : 0;
  303. }
  304. return average;
  305. }