bcm43xx_power.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. Broadcom BCM43xx wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  4. Stefano Brivio <st3@riseup.net>
  5. Michael Buesch <mbuesch@freenet.de>
  6. Danny van Dyk <kugelfang@gentoo.org>
  7. Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Some parts of the code in this file are derived from the ipw2200
  9. driver Copyright(c) 2003 - 2004 Intel Corporation.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  21. Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/delay.h>
  24. #include "bcm43xx.h"
  25. #include "bcm43xx_power.h"
  26. #include "bcm43xx_main.h"
  27. /* Get max/min slowclock frequency
  28. * as described in http://bcm-specs.sipsolutions.net/PowerControl
  29. */
  30. static int bcm43xx_pctl_clockfreqlimit(struct bcm43xx_private *bcm,
  31. int get_max)
  32. {
  33. int limit = 0;
  34. int divisor;
  35. int selection;
  36. int err;
  37. u32 tmp;
  38. struct bcm43xx_coreinfo *old_core;
  39. if (!(bcm->chipcommon_capabilities & BCM43xx_CAPABILITIES_PCTL))
  40. goto out;
  41. old_core = bcm->current_core;
  42. err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
  43. if (err)
  44. goto out;
  45. if (bcm->current_core->rev < 6) {
  46. if ((bcm->bustype == BCM43xx_BUSTYPE_PCMCIA) ||
  47. (bcm->bustype == BCM43xx_BUSTYPE_SB)) {
  48. selection = 1;
  49. divisor = 32;
  50. } else {
  51. err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCTL_OUT, &tmp);
  52. if (err) {
  53. printk(KERN_ERR PFX "clockfreqlimit pcicfg read failure\n");
  54. goto out_switchback;
  55. }
  56. if (tmp & 0x10) {
  57. /* PCI */
  58. selection = 2;
  59. divisor = 64;
  60. } else {
  61. /* XTAL */
  62. selection = 1;
  63. divisor = 32;
  64. }
  65. }
  66. } else if (bcm->current_core->rev < 10) {
  67. selection = (tmp & 0x07);
  68. if (selection) {
  69. tmp = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL);
  70. divisor = 4 * (1 + ((tmp & 0xFFFF0000) >> 16));
  71. } else
  72. divisor = 1;
  73. } else {
  74. tmp = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_SYSCLKCTL);
  75. divisor = 4 * (1 + ((tmp & 0xFFFF0000) >> 16));
  76. selection = 1;
  77. }
  78. switch (selection) {
  79. case 0:
  80. /* LPO */
  81. if (get_max)
  82. limit = 43000;
  83. else
  84. limit = 25000;
  85. break;
  86. case 1:
  87. /* XTAL */
  88. if (get_max)
  89. limit = 20200000;
  90. else
  91. limit = 19800000;
  92. break;
  93. case 2:
  94. /* PCI */
  95. if (get_max)
  96. limit = 34000000;
  97. else
  98. limit = 25000000;
  99. break;
  100. default:
  101. assert(0);
  102. }
  103. limit /= divisor;
  104. out_switchback:
  105. err = bcm43xx_switch_core(bcm, old_core);
  106. assert(err == 0);
  107. out:
  108. return limit;
  109. }
  110. /* init power control
  111. * as described in http://bcm-specs.sipsolutions.net/PowerControl
  112. */
  113. int bcm43xx_pctl_init(struct bcm43xx_private *bcm)
  114. {
  115. int err, maxfreq;
  116. struct bcm43xx_coreinfo *old_core;
  117. if (!(bcm->chipcommon_capabilities & BCM43xx_CAPABILITIES_PCTL))
  118. return 0;
  119. old_core = bcm->current_core;
  120. err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
  121. if (err == -ENODEV)
  122. return 0;
  123. if (err)
  124. goto out;
  125. maxfreq = bcm43xx_pctl_clockfreqlimit(bcm, 1);
  126. bcm43xx_write32(bcm, BCM43xx_CHIPCOMMON_PLLONDELAY,
  127. (maxfreq * 150 + 999999) / 1000000);
  128. bcm43xx_write32(bcm, BCM43xx_CHIPCOMMON_FREFSELDELAY,
  129. (maxfreq * 15 + 999999) / 1000000);
  130. err = bcm43xx_switch_core(bcm, old_core);
  131. assert(err == 0);
  132. out:
  133. return err;
  134. }
  135. u16 bcm43xx_pctl_powerup_delay(struct bcm43xx_private *bcm)
  136. {
  137. u16 delay = 0;
  138. int err;
  139. u32 pll_on_delay;
  140. struct bcm43xx_coreinfo *old_core;
  141. int minfreq;
  142. if (bcm->bustype != BCM43xx_BUSTYPE_PCI)
  143. goto out;
  144. if (!(bcm->chipcommon_capabilities & BCM43xx_CAPABILITIES_PCTL))
  145. goto out;
  146. old_core = bcm->current_core;
  147. err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
  148. if (err == -ENODEV)
  149. goto out;
  150. minfreq = bcm43xx_pctl_clockfreqlimit(bcm, 0);
  151. pll_on_delay = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_PLLONDELAY);
  152. delay = (((pll_on_delay + 2) * 1000000) + (minfreq - 1)) / minfreq;
  153. err = bcm43xx_switch_core(bcm, old_core);
  154. assert(err == 0);
  155. out:
  156. return delay;
  157. }
  158. /* set the powercontrol clock
  159. * as described in http://bcm-specs.sipsolutions.net/PowerControl
  160. */
  161. int bcm43xx_pctl_set_clock(struct bcm43xx_private *bcm, u16 mode)
  162. {
  163. int err;
  164. struct bcm43xx_coreinfo *old_core;
  165. u32 tmp;
  166. old_core = bcm->current_core;
  167. err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
  168. if (err == -ENODEV)
  169. return 0;
  170. if (err)
  171. goto out;
  172. if (bcm->core_chipcommon.rev < 6) {
  173. if (mode == BCM43xx_PCTL_CLK_FAST) {
  174. err = bcm43xx_pctl_set_crystal(bcm, 1);
  175. if (err)
  176. goto out;
  177. }
  178. } else {
  179. if ((bcm->chipcommon_capabilities & BCM43xx_CAPABILITIES_PCTL) &&
  180. (bcm->core_chipcommon.rev < 10)) {
  181. switch (mode) {
  182. case BCM43xx_PCTL_CLK_FAST:
  183. tmp = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL);
  184. tmp = (tmp & ~BCM43xx_PCTL_FORCE_SLOW) | BCM43xx_PCTL_FORCE_PLL;
  185. bcm43xx_write32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL, tmp);
  186. break;
  187. case BCM43xx_PCTL_CLK_SLOW:
  188. tmp = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL);
  189. tmp |= BCM43xx_PCTL_FORCE_SLOW;
  190. bcm43xx_write32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL, tmp);
  191. break;
  192. case BCM43xx_PCTL_CLK_DYNAMIC:
  193. tmp = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL);
  194. tmp &= ~BCM43xx_PCTL_FORCE_SLOW;
  195. tmp |= BCM43xx_PCTL_FORCE_PLL;
  196. tmp &= ~BCM43xx_PCTL_DYN_XTAL;
  197. bcm43xx_write32(bcm, BCM43xx_CHIPCOMMON_SLOWCLKCTL, tmp);
  198. }
  199. }
  200. }
  201. err = bcm43xx_switch_core(bcm, old_core);
  202. assert(err == 0);
  203. out:
  204. return err;
  205. }
  206. int bcm43xx_pctl_set_crystal(struct bcm43xx_private *bcm, int on)
  207. {
  208. int err;
  209. u32 in, out, outenable;
  210. err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCTL_IN, &in);
  211. if (err)
  212. goto err_pci;
  213. err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCTL_OUT, &out);
  214. if (err)
  215. goto err_pci;
  216. err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCTL_OUTENABLE, &outenable);
  217. if (err)
  218. goto err_pci;
  219. outenable |= (BCM43xx_PCTL_XTAL_POWERUP | BCM43xx_PCTL_PLL_POWERDOWN);
  220. if (on) {
  221. if (in & 0x40)
  222. return 0;
  223. out |= (BCM43xx_PCTL_XTAL_POWERUP | BCM43xx_PCTL_PLL_POWERDOWN);
  224. err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCTL_OUT, out);
  225. if (err)
  226. goto err_pci;
  227. err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCTL_OUTENABLE, outenable);
  228. if (err)
  229. goto err_pci;
  230. udelay(1000);
  231. out &= ~BCM43xx_PCTL_PLL_POWERDOWN;
  232. err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCTL_OUT, out);
  233. if (err)
  234. goto err_pci;
  235. udelay(5000);
  236. } else {
  237. if (bcm->current_core->rev < 5)
  238. return 0;
  239. if (bcm->sprom.boardflags & BCM43xx_BFL_XTAL_NOSLOW)
  240. return 0;
  241. /* XXX: Why BCM43xx_MMIO_RADIO_HWENABLED_xx can't be read at this time?
  242. * err = bcm43xx_switch_core(bcm, bcm->active_80211_core);
  243. * if (err)
  244. * return err;
  245. * if (((bcm->current_core->rev >= 3) &&
  246. * (bcm43xx_read32(bcm, BCM43xx_MMIO_RADIO_HWENABLED_HI) & (1 << 16))) ||
  247. * ((bcm->current_core->rev < 3) &&
  248. * !(bcm43xx_read16(bcm, BCM43xx_MMIO_RADIO_HWENABLED_LO) & (1 << 4))))
  249. * return 0;
  250. * err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
  251. * if (err)
  252. * return err;
  253. */
  254. err = bcm43xx_pctl_set_clock(bcm, BCM43xx_PCTL_CLK_SLOW);
  255. if (err)
  256. goto out;
  257. out &= ~BCM43xx_PCTL_XTAL_POWERUP;
  258. out |= BCM43xx_PCTL_PLL_POWERDOWN;
  259. err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCTL_OUT, out);
  260. if (err)
  261. goto err_pci;
  262. err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCTL_OUTENABLE, outenable);
  263. if (err)
  264. goto err_pci;
  265. }
  266. out:
  267. return err;
  268. err_pci:
  269. printk(KERN_ERR PFX "Error: pctl_set_clock() could not access PCI config space!\n");
  270. err = -EBUSY;
  271. goto out;
  272. }
  273. /* Set the PowerSavingControlBits.
  274. * Bitvalues:
  275. * 0 => unset the bit
  276. * 1 => set the bit
  277. * -1 => calculate the bit
  278. */
  279. void bcm43xx_power_saving_ctl_bits(struct bcm43xx_private *bcm,
  280. int bit25, int bit26)
  281. {
  282. int i;
  283. u32 status;
  284. //FIXME: Force 25 to off and 26 to on for now:
  285. bit25 = 0;
  286. bit26 = 1;
  287. if (bit25 == -1) {
  288. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  289. // and thus is not an AP and we are associated, set bit 25
  290. }
  291. if (bit26 == -1) {
  292. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  293. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  294. // successful, set bit26
  295. }
  296. status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
  297. if (bit25)
  298. status |= BCM43xx_SBF_PS1;
  299. else
  300. status &= ~BCM43xx_SBF_PS1;
  301. if (bit26)
  302. status |= BCM43xx_SBF_PS2;
  303. else
  304. status &= ~BCM43xx_SBF_PS2;
  305. bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, status);
  306. if (bit26 && bcm->current_core->rev >= 5) {
  307. for (i = 0; i < 100; i++) {
  308. if (bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED, 0x0040) != 4)
  309. break;
  310. udelay(10);
  311. }
  312. }
  313. }