reset.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
  5. * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
  6. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. /*****************************\
  22. Reset functions and helpers
  23. \*****************************/
  24. #include <asm/unaligned.h>
  25. #include <linux/pci.h> /* To determine if a card is pci-e */
  26. #include <linux/log2.h>
  27. #include "ath5k.h"
  28. #include "reg.h"
  29. #include "base.h"
  30. #include "debug.h"
  31. /******************\
  32. * Helper functions *
  33. \******************/
  34. /*
  35. * Check if a register write has been completed
  36. */
  37. int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
  38. bool is_set)
  39. {
  40. int i;
  41. u32 data;
  42. for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
  43. data = ath5k_hw_reg_read(ah, reg);
  44. if (is_set && (data & flag))
  45. break;
  46. else if ((data & flag) == val)
  47. break;
  48. udelay(15);
  49. }
  50. return (i <= 0) ? -EAGAIN : 0;
  51. }
  52. /*************************\
  53. * Clock related functions *
  54. \*************************/
  55. /**
  56. * ath5k_hw_htoclock - Translate usec to hw clock units
  57. *
  58. * @ah: The &struct ath5k_hw
  59. * @usec: value in microseconds
  60. */
  61. unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
  62. {
  63. struct ath_common *common = ath5k_hw_common(ah);
  64. return usec * common->clockrate;
  65. }
  66. /**
  67. * ath5k_hw_clocktoh - Translate hw clock units to usec
  68. * @clock: value in hw clock units
  69. */
  70. unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
  71. {
  72. struct ath_common *common = ath5k_hw_common(ah);
  73. return clock / common->clockrate;
  74. }
  75. /**
  76. * ath5k_hw_set_clockrate - Set common->clockrate for the current channel
  77. *
  78. * @ah: The &struct ath5k_hw
  79. */
  80. void ath5k_hw_set_clockrate(struct ath5k_hw *ah)
  81. {
  82. struct ieee80211_channel *channel = ah->ah_current_channel;
  83. struct ath_common *common = ath5k_hw_common(ah);
  84. int clock;
  85. if (channel->hw_value & CHANNEL_5GHZ)
  86. clock = 40; /* 802.11a */
  87. else if (channel->hw_value & CHANNEL_CCK)
  88. clock = 22; /* 802.11b */
  89. else
  90. clock = 44; /* 802.11g */
  91. /* Clock rate in turbo modes is twice the normal rate */
  92. if (channel->hw_value & CHANNEL_TURBO)
  93. clock *= 2;
  94. common->clockrate = clock;
  95. }
  96. /*
  97. * If there is an external 32KHz crystal available, use it
  98. * as ref. clock instead of 32/40MHz clock and baseband clocks
  99. * to save power during sleep or restore normal 32/40MHz
  100. * operation.
  101. *
  102. * XXX: When operating on 32KHz certain PHY registers (27 - 31,
  103. * 123 - 127) require delay on access.
  104. */
  105. static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
  106. {
  107. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  108. u32 scal, spending, usec32;
  109. /* Only set 32KHz settings if we have an external
  110. * 32KHz crystal present */
  111. if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
  112. AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
  113. enable) {
  114. /* 1 usec/cycle */
  115. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
  116. /* Set up tsf increment on each cycle */
  117. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
  118. /* Set baseband sleep control registers
  119. * and sleep control rate */
  120. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  121. if ((ah->ah_radio == AR5K_RF5112) ||
  122. (ah->ah_radio == AR5K_RF5413) ||
  123. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  124. spending = 0x14;
  125. else
  126. spending = 0x18;
  127. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  128. if ((ah->ah_radio == AR5K_RF5112) ||
  129. (ah->ah_radio == AR5K_RF5413) ||
  130. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  131. ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
  132. ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
  133. ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
  134. ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
  135. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  136. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
  137. } else {
  138. ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
  139. ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
  140. ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
  141. ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
  142. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  143. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
  144. }
  145. /* Enable sleep clock operation */
  146. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
  147. AR5K_PCICFG_SLEEP_CLOCK_EN);
  148. } else {
  149. /* Disable sleep clock operation and
  150. * restore default parameters */
  151. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  152. AR5K_PCICFG_SLEEP_CLOCK_EN);
  153. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  154. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
  155. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  156. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  157. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  158. scal = AR5K_PHY_SCAL_32MHZ_2417;
  159. else if (ee->ee_is_hb63)
  160. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  161. else
  162. scal = AR5K_PHY_SCAL_32MHZ;
  163. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  164. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  165. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  166. if ((ah->ah_radio == AR5K_RF5112) ||
  167. (ah->ah_radio == AR5K_RF5413) ||
  168. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  169. spending = 0x14;
  170. else
  171. spending = 0x18;
  172. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  173. if ((ah->ah_radio == AR5K_RF5112) ||
  174. (ah->ah_radio == AR5K_RF5413))
  175. usec32 = 39;
  176. else
  177. usec32 = 31;
  178. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
  179. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
  180. }
  181. }
  182. /*********************\
  183. * Reset/Sleep control *
  184. \*********************/
  185. /*
  186. * Reset chipset
  187. */
  188. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  189. {
  190. int ret;
  191. u32 mask = val ? val : ~0U;
  192. /* Read-and-clear RX Descriptor Pointer*/
  193. ath5k_hw_reg_read(ah, AR5K_RXDP);
  194. /*
  195. * Reset the device and wait until success
  196. */
  197. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  198. /* Wait at least 128 PCI clocks */
  199. udelay(15);
  200. if (ah->ah_version == AR5K_AR5210) {
  201. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  202. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  203. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  204. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  205. } else {
  206. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  207. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  208. }
  209. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  210. /*
  211. * Reset configuration register (for hw byte-swap). Note that this
  212. * is only set for big endian. We do the necessary magic in
  213. * AR5K_INIT_CFG.
  214. */
  215. if ((val & AR5K_RESET_CTL_PCU) == 0)
  216. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  217. return ret;
  218. }
  219. /*
  220. * Sleep control
  221. */
  222. static int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  223. bool set_chip, u16 sleep_duration)
  224. {
  225. unsigned int i;
  226. u32 staid, data;
  227. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  228. switch (mode) {
  229. case AR5K_PM_AUTO:
  230. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  231. /* fallthrough */
  232. case AR5K_PM_NETWORK_SLEEP:
  233. if (set_chip)
  234. ath5k_hw_reg_write(ah,
  235. AR5K_SLEEP_CTL_SLE_ALLOW |
  236. sleep_duration,
  237. AR5K_SLEEP_CTL);
  238. staid |= AR5K_STA_ID1_PWR_SV;
  239. break;
  240. case AR5K_PM_FULL_SLEEP:
  241. if (set_chip)
  242. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  243. AR5K_SLEEP_CTL);
  244. staid |= AR5K_STA_ID1_PWR_SV;
  245. break;
  246. case AR5K_PM_AWAKE:
  247. staid &= ~AR5K_STA_ID1_PWR_SV;
  248. if (!set_chip)
  249. goto commit;
  250. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  251. /* If card is down we 'll get 0xffff... so we
  252. * need to clean this up before we write the register
  253. */
  254. if (data & 0xffc00000)
  255. data = 0;
  256. else
  257. /* Preserve sleep duration etc */
  258. data = data & ~AR5K_SLEEP_CTL_SLE;
  259. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  260. AR5K_SLEEP_CTL);
  261. udelay(15);
  262. for (i = 200; i > 0; i--) {
  263. /* Check if the chip did wake up */
  264. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  265. AR5K_PCICFG_SPWR_DN) == 0)
  266. break;
  267. /* Wait a bit and retry */
  268. udelay(50);
  269. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  270. AR5K_SLEEP_CTL);
  271. }
  272. /* Fail if the chip didn't wake up */
  273. if (i == 0)
  274. return -EIO;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. commit:
  280. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  281. return 0;
  282. }
  283. /*
  284. * Put device on hold
  285. *
  286. * Put MAC and Baseband on warm reset and
  287. * keep that state (don't clean sleep control
  288. * register). After this MAC and Baseband are
  289. * disabled and a full reset is needed to come
  290. * back. This way we save as much power as possible
  291. * without putting the card on full sleep.
  292. */
  293. int ath5k_hw_on_hold(struct ath5k_hw *ah)
  294. {
  295. struct pci_dev *pdev = ah->ah_sc->pdev;
  296. u32 bus_flags;
  297. int ret;
  298. /* Make sure device is awake */
  299. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  300. if (ret) {
  301. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  302. return ret;
  303. }
  304. /*
  305. * Put chipset on warm reset...
  306. *
  307. * Note: putting PCI core on warm reset on PCI-E cards
  308. * results card to hang and always return 0xffff... so
  309. * we ingore that flag for PCI-E cards. On PCI cards
  310. * this flag gets cleared after 64 PCI clocks.
  311. */
  312. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  313. if (ah->ah_version == AR5K_AR5210) {
  314. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  315. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  316. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  317. mdelay(2);
  318. } else {
  319. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  320. AR5K_RESET_CTL_BASEBAND | bus_flags);
  321. }
  322. if (ret) {
  323. ATH5K_ERR(ah->ah_sc, "failed to put device on warm reset\n");
  324. return -EIO;
  325. }
  326. /* ...wakeup again!*/
  327. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  328. if (ret) {
  329. ATH5K_ERR(ah->ah_sc, "failed to put device on hold\n");
  330. return ret;
  331. }
  332. return ret;
  333. }
  334. /*
  335. * Bring up MAC + PHY Chips and program PLL
  336. * TODO: Half/Quarter rate support
  337. */
  338. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  339. {
  340. struct pci_dev *pdev = ah->ah_sc->pdev;
  341. u32 turbo, mode, clock, bus_flags;
  342. int ret;
  343. turbo = 0;
  344. mode = 0;
  345. clock = 0;
  346. /* Wakeup the device */
  347. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  348. if (ret) {
  349. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  350. return ret;
  351. }
  352. /*
  353. * Put chipset on warm reset...
  354. *
  355. * Note: putting PCI core on warm reset on PCI-E cards
  356. * results card to hang and always return 0xffff... so
  357. * we ingore that flag for PCI-E cards. On PCI cards
  358. * this flag gets cleared after 64 PCI clocks.
  359. */
  360. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  361. if (ah->ah_version == AR5K_AR5210) {
  362. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  363. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  364. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  365. mdelay(2);
  366. } else {
  367. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  368. AR5K_RESET_CTL_BASEBAND | bus_flags);
  369. }
  370. if (ret) {
  371. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
  372. return -EIO;
  373. }
  374. /* ...wakeup again!...*/
  375. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  376. if (ret) {
  377. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  378. return ret;
  379. }
  380. /* ...clear reset control register and pull device out of
  381. * warm reset */
  382. if (ath5k_hw_nic_reset(ah, 0)) {
  383. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  384. return -EIO;
  385. }
  386. /* On initialization skip PLL programming since we don't have
  387. * a channel / mode set yet */
  388. if (initial)
  389. return 0;
  390. if (ah->ah_version != AR5K_AR5210) {
  391. /*
  392. * Get channel mode flags
  393. */
  394. if (ah->ah_radio >= AR5K_RF5112) {
  395. mode = AR5K_PHY_MODE_RAD_RF5112;
  396. clock = AR5K_PHY_PLL_RF5112;
  397. } else {
  398. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  399. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  400. }
  401. if (flags & CHANNEL_2GHZ) {
  402. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  403. clock |= AR5K_PHY_PLL_44MHZ;
  404. if (flags & CHANNEL_CCK) {
  405. mode |= AR5K_PHY_MODE_MOD_CCK;
  406. } else if (flags & CHANNEL_OFDM) {
  407. /* XXX Dynamic OFDM/CCK is not supported by the
  408. * AR5211 so we set MOD_OFDM for plain g (no
  409. * CCK headers) operation. We need to test
  410. * this, 5211 might support ofdm-only g after
  411. * all, there are also initial register values
  412. * in the code for g mode (see initvals.c). */
  413. if (ah->ah_version == AR5K_AR5211)
  414. mode |= AR5K_PHY_MODE_MOD_OFDM;
  415. else
  416. mode |= AR5K_PHY_MODE_MOD_DYN;
  417. } else {
  418. ATH5K_ERR(ah->ah_sc,
  419. "invalid radio modulation mode\n");
  420. return -EINVAL;
  421. }
  422. } else if (flags & CHANNEL_5GHZ) {
  423. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  424. if (ah->ah_radio == AR5K_RF5413)
  425. clock = AR5K_PHY_PLL_40MHZ_5413;
  426. else
  427. clock |= AR5K_PHY_PLL_40MHZ;
  428. if (flags & CHANNEL_OFDM)
  429. mode |= AR5K_PHY_MODE_MOD_OFDM;
  430. else {
  431. ATH5K_ERR(ah->ah_sc,
  432. "invalid radio modulation mode\n");
  433. return -EINVAL;
  434. }
  435. } else {
  436. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  437. return -EINVAL;
  438. }
  439. if (flags & CHANNEL_TURBO)
  440. turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
  441. } else { /* Reset the device */
  442. /* ...enable Atheros turbo mode if requested */
  443. if (flags & CHANNEL_TURBO)
  444. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  445. AR5K_PHY_TURBO);
  446. }
  447. if (ah->ah_version != AR5K_AR5210) {
  448. /* ...update PLL if needed */
  449. if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
  450. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  451. udelay(300);
  452. }
  453. /* ...set the PHY operating mode */
  454. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  455. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  456. }
  457. return 0;
  458. }
  459. /**************************************\
  460. * Post-initvals register modifications *
  461. \**************************************/
  462. /* TODO: Half/Quarter rate */
  463. static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
  464. struct ieee80211_channel *channel)
  465. {
  466. if (ah->ah_version == AR5K_AR5212 &&
  467. ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  468. /* Setup ADC control */
  469. ath5k_hw_reg_write(ah,
  470. (AR5K_REG_SM(2,
  471. AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
  472. AR5K_REG_SM(2,
  473. AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
  474. AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
  475. AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
  476. AR5K_PHY_ADC_CTL);
  477. /* Disable barker RSSI threshold */
  478. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  479. AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
  480. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  481. AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
  482. /* Set the mute mask */
  483. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  484. }
  485. /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
  486. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
  487. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
  488. /* Enable DCU double buffering */
  489. if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
  490. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  491. AR5K_TXCFG_DCU_DBL_BUF_DIS);
  492. /* Set DAC/ADC delays */
  493. if (ah->ah_version == AR5K_AR5212) {
  494. u32 scal;
  495. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  496. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  497. scal = AR5K_PHY_SCAL_32MHZ_2417;
  498. else if (ee->ee_is_hb63)
  499. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  500. else
  501. scal = AR5K_PHY_SCAL_32MHZ;
  502. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  503. }
  504. /* Set fast ADC */
  505. if ((ah->ah_radio == AR5K_RF5413) ||
  506. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  507. u32 fast_adc = true;
  508. if (channel->center_freq == 2462 ||
  509. channel->center_freq == 2467)
  510. fast_adc = 0;
  511. /* Only update if needed */
  512. if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
  513. ath5k_hw_reg_write(ah, fast_adc,
  514. AR5K_PHY_FAST_ADC);
  515. }
  516. /* Fix for first revision of the RF5112 RF chipset */
  517. if (ah->ah_radio == AR5K_RF5112 &&
  518. ah->ah_radio_5ghz_revision <
  519. AR5K_SREV_RAD_5112A) {
  520. u32 data;
  521. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  522. AR5K_PHY_CCKTXCTL);
  523. if (channel->hw_value & CHANNEL_5GHZ)
  524. data = 0xffb81020;
  525. else
  526. data = 0xffb80d20;
  527. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  528. }
  529. if ((ah->ah_radio == AR5K_RF5112) &&
  530. (ah->ah_mac_srev < AR5K_SREV_AR5211)) {
  531. u32 usec_reg;
  532. /* 5311 has different tx/rx latency masks
  533. * from 5211, since we deal 5311 the same
  534. * as 5211 when setting initvals, shift
  535. * values here to their proper locations */
  536. usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
  537. ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
  538. AR5K_USEC_32 |
  539. AR5K_USEC_TX_LATENCY_5211 |
  540. AR5K_REG_SM(29,
  541. AR5K_USEC_RX_LATENCY_5210)),
  542. AR5K_USEC_5211);
  543. /* Clear QCU/DCU clock gating register */
  544. ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
  545. /* Set DAC/ADC delays */
  546. ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
  547. /* Enable PCU FIFO corruption ECO */
  548. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  549. AR5K_DIAG_SW_ECO_ENABLE);
  550. }
  551. }
  552. static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
  553. struct ieee80211_channel *channel, u8 ee_mode)
  554. {
  555. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  556. s16 cck_ofdm_pwr_delta;
  557. /* TODO: Add support for AR5210 EEPROM */
  558. if (ah->ah_version == AR5K_AR5210)
  559. return;
  560. /* Adjust power delta for channel 14 */
  561. if (channel->center_freq == 2484)
  562. cck_ofdm_pwr_delta =
  563. ((ee->ee_cck_ofdm_power_delta -
  564. ee->ee_scaled_cck_delta) * 2) / 10;
  565. else
  566. cck_ofdm_pwr_delta =
  567. (ee->ee_cck_ofdm_power_delta * 2) / 10;
  568. /* Set CCK to OFDM power delta on tx power
  569. * adjustment register */
  570. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  571. if (channel->hw_value == CHANNEL_G)
  572. ath5k_hw_reg_write(ah,
  573. AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
  574. AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
  575. AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
  576. AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
  577. AR5K_PHY_TX_PWR_ADJ);
  578. else
  579. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
  580. } else {
  581. /* For older revs we scale power on sw during tx power
  582. * setup */
  583. ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
  584. ah->ah_txpower.txp_cck_ofdm_gainf_delta =
  585. ee->ee_cck_ofdm_gain_delta;
  586. }
  587. /* XXX: necessary here? is called from ath5k_hw_set_antenna_mode()
  588. * too */
  589. ath5k_hw_set_antenna_switch(ah, ee_mode);
  590. /* Noise floor threshold */
  591. ath5k_hw_reg_write(ah,
  592. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  593. AR5K_PHY_NFTHRES);
  594. if ((channel->hw_value & CHANNEL_TURBO) &&
  595. (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
  596. /* Switch settling time (Turbo) */
  597. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  598. AR5K_PHY_SETTLING_SWITCH,
  599. ee->ee_switch_settling_turbo[ee_mode]);
  600. /* Tx/Rx attenuation (Turbo) */
  601. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  602. AR5K_PHY_GAIN_TXRX_ATTEN,
  603. ee->ee_atn_tx_rx_turbo[ee_mode]);
  604. /* ADC/PGA desired size (Turbo) */
  605. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  606. AR5K_PHY_DESIRED_SIZE_ADC,
  607. ee->ee_adc_desired_size_turbo[ee_mode]);
  608. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  609. AR5K_PHY_DESIRED_SIZE_PGA,
  610. ee->ee_pga_desired_size_turbo[ee_mode]);
  611. /* Tx/Rx margin (Turbo) */
  612. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  613. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  614. ee->ee_margin_tx_rx_turbo[ee_mode]);
  615. } else {
  616. /* Switch settling time */
  617. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  618. AR5K_PHY_SETTLING_SWITCH,
  619. ee->ee_switch_settling[ee_mode]);
  620. /* Tx/Rx attenuation */
  621. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  622. AR5K_PHY_GAIN_TXRX_ATTEN,
  623. ee->ee_atn_tx_rx[ee_mode]);
  624. /* ADC/PGA desired size */
  625. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  626. AR5K_PHY_DESIRED_SIZE_ADC,
  627. ee->ee_adc_desired_size[ee_mode]);
  628. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  629. AR5K_PHY_DESIRED_SIZE_PGA,
  630. ee->ee_pga_desired_size[ee_mode]);
  631. /* Tx/Rx margin */
  632. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  633. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  634. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  635. ee->ee_margin_tx_rx[ee_mode]);
  636. }
  637. /* XPA delays */
  638. ath5k_hw_reg_write(ah,
  639. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  640. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  641. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  642. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  643. /* XLNA delay */
  644. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
  645. AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
  646. ee->ee_tx_end2xlna_enable[ee_mode]);
  647. /* Thresh64 (ANI) */
  648. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
  649. AR5K_PHY_NF_THRESH62,
  650. ee->ee_thr_62[ee_mode]);
  651. /* False detect backoff for channels
  652. * that have spur noise. Write the new
  653. * cyclic power RSSI threshold. */
  654. if (ath5k_hw_chan_has_spur_noise(ah, channel))
  655. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  656. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  657. AR5K_INIT_CYCRSSI_THR1 +
  658. ee->ee_false_detect[ee_mode]);
  659. else
  660. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  661. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  662. AR5K_INIT_CYCRSSI_THR1);
  663. /* I/Q correction (set enable bit last to match HAL sources) */
  664. /* TODO: Per channel i/q infos ? */
  665. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  666. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
  667. ee->ee_i_cal[ee_mode]);
  668. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
  669. ee->ee_q_cal[ee_mode]);
  670. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  671. }
  672. /* Heavy clipping -disable for now */
  673. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  674. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  675. }
  676. /*********************\
  677. * Main reset function *
  678. \*********************/
  679. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  680. struct ieee80211_channel *channel, bool change_channel)
  681. {
  682. struct ath_common *common = ath5k_hw_common(ah);
  683. u32 s_seq[10], s_led[3], staid1_flags, tsf_up, tsf_lo;
  684. u8 mode, freq, ee_mode;
  685. int i, ret;
  686. ee_mode = 0;
  687. staid1_flags = 0;
  688. tsf_up = 0;
  689. tsf_lo = 0;
  690. freq = 0;
  691. mode = 0;
  692. /*
  693. * Stop PCU
  694. */
  695. ath5k_hw_stop_rx_pcu(ah);
  696. /*
  697. * Stop DMA
  698. *
  699. * Note: If DMA didn't stop continue
  700. * since only a reset will fix it.
  701. */
  702. ath5k_hw_dma_stop(ah);
  703. /*
  704. * Save some registers before a reset
  705. */
  706. /*DCU/Antenna selection not available on 5210*/
  707. if (ah->ah_version != AR5K_AR5210) {
  708. switch (channel->hw_value & CHANNEL_MODES) {
  709. case CHANNEL_A:
  710. mode = AR5K_MODE_11A;
  711. freq = AR5K_INI_RFGAIN_5GHZ;
  712. ee_mode = AR5K_EEPROM_MODE_11A;
  713. break;
  714. case CHANNEL_G:
  715. mode = AR5K_MODE_11G;
  716. freq = AR5K_INI_RFGAIN_2GHZ;
  717. ee_mode = AR5K_EEPROM_MODE_11G;
  718. break;
  719. case CHANNEL_B:
  720. mode = AR5K_MODE_11B;
  721. freq = AR5K_INI_RFGAIN_2GHZ;
  722. ee_mode = AR5K_EEPROM_MODE_11B;
  723. break;
  724. case CHANNEL_T:
  725. mode = AR5K_MODE_11A_TURBO;
  726. freq = AR5K_INI_RFGAIN_5GHZ;
  727. ee_mode = AR5K_EEPROM_MODE_11A;
  728. break;
  729. case CHANNEL_TG:
  730. if (ah->ah_version == AR5K_AR5211) {
  731. ATH5K_ERR(ah->ah_sc,
  732. "TurboG mode not available on 5211");
  733. return -EINVAL;
  734. }
  735. mode = AR5K_MODE_11G_TURBO;
  736. freq = AR5K_INI_RFGAIN_2GHZ;
  737. ee_mode = AR5K_EEPROM_MODE_11G;
  738. break;
  739. case CHANNEL_XR:
  740. if (ah->ah_version == AR5K_AR5211) {
  741. ATH5K_ERR(ah->ah_sc,
  742. "XR mode not available on 5211");
  743. return -EINVAL;
  744. }
  745. mode = AR5K_MODE_XR;
  746. freq = AR5K_INI_RFGAIN_5GHZ;
  747. ee_mode = AR5K_EEPROM_MODE_11A;
  748. break;
  749. default:
  750. ATH5K_ERR(ah->ah_sc,
  751. "invalid channel: %d\n", channel->center_freq);
  752. return -EINVAL;
  753. }
  754. if (change_channel) {
  755. /*
  756. * Save frame sequence count
  757. * For revs. after Oahu, only save
  758. * seq num for DCU 0 (Global seq num)
  759. */
  760. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  761. for (i = 0; i < 10; i++)
  762. s_seq[i] = ath5k_hw_reg_read(ah,
  763. AR5K_QUEUE_DCU_SEQNUM(i));
  764. } else {
  765. s_seq[0] = ath5k_hw_reg_read(ah,
  766. AR5K_QUEUE_DCU_SEQNUM(0));
  767. }
  768. /* TSF accelerates on AR5211 during reset
  769. * As a workaround save it here and restore
  770. * it later so that it's back in time after
  771. * reset. This way it'll get re-synced on the
  772. * next beacon without breaking ad-hoc.
  773. *
  774. * On AR5212 TSF is almost preserved across a
  775. * reset so it stays back in time anyway and
  776. * we don't have to save/restore it.
  777. *
  778. * XXX: Since this breaks power saving we have
  779. * to disable power saving until we receive the
  780. * next beacon, so we can resync beacon timers */
  781. if (ah->ah_version == AR5K_AR5211) {
  782. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  783. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  784. }
  785. }
  786. if (ah->ah_version == AR5K_AR5212) {
  787. /* Restore normal 32/40MHz clock operation
  788. * to avoid register access delay on certain
  789. * PHY registers */
  790. ath5k_hw_set_sleep_clock(ah, false);
  791. /* Since we are going to write rf buffer
  792. * check if we have any pending gain_F
  793. * optimization settings */
  794. if (change_channel && ah->ah_rf_banks != NULL)
  795. ath5k_hw_gainf_calibrate(ah);
  796. }
  797. }
  798. /*GPIOs*/
  799. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  800. AR5K_PCICFG_LEDSTATE;
  801. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  802. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  803. /* AR5K_STA_ID1 flags, only preserve antenna
  804. * settings and ack/cts rate mode */
  805. staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
  806. (AR5K_STA_ID1_DEFAULT_ANTENNA |
  807. AR5K_STA_ID1_DESC_ANTENNA |
  808. AR5K_STA_ID1_RTS_DEF_ANTENNA |
  809. AR5K_STA_ID1_ACKCTS_6MB |
  810. AR5K_STA_ID1_BASE_RATE_11B |
  811. AR5K_STA_ID1_SELFGEN_DEF_ANT);
  812. /* Wakeup the device */
  813. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  814. if (ret)
  815. return ret;
  816. /* PHY access enable */
  817. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  818. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  819. else
  820. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  821. AR5K_PHY(0));
  822. /* Write initial settings */
  823. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  824. if (ret)
  825. return ret;
  826. /*
  827. * Tweak initval settings for revised
  828. * chipsets and add some more config
  829. * bits
  830. */
  831. ath5k_hw_tweak_initval_settings(ah, channel);
  832. /* Commit values from EEPROM */
  833. ath5k_hw_commit_eeprom_settings(ah, channel, ee_mode);
  834. /*
  835. * Restore saved values
  836. */
  837. /*DCU/Antenna selection not available on 5210*/
  838. if (ah->ah_version != AR5K_AR5210) {
  839. if (change_channel) {
  840. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  841. for (i = 0; i < 10; i++)
  842. ath5k_hw_reg_write(ah, s_seq[i],
  843. AR5K_QUEUE_DCU_SEQNUM(i));
  844. } else {
  845. ath5k_hw_reg_write(ah, s_seq[0],
  846. AR5K_QUEUE_DCU_SEQNUM(0));
  847. }
  848. if (ah->ah_version == AR5K_AR5211) {
  849. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  850. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  851. }
  852. }
  853. }
  854. /* Ledstate */
  855. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  856. /* Gpio settings */
  857. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  858. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  859. /* Restore sta_id flags and preserve our mac address*/
  860. ath5k_hw_reg_write(ah,
  861. get_unaligned_le32(common->macaddr),
  862. AR5K_STA_ID0);
  863. ath5k_hw_reg_write(ah,
  864. staid1_flags | get_unaligned_le16(common->macaddr + 4),
  865. AR5K_STA_ID1);
  866. /*
  867. * Initialize PCU
  868. */
  869. ath5k_hw_pcu_init(ah, op_mode, mode);
  870. /*
  871. * Initialize PHY
  872. */
  873. ret = ath5k_hw_phy_init(ah, channel, mode, ee_mode, freq);
  874. if (ret) {
  875. ATH5K_ERR(ah->ah_sc,
  876. "failed to initialize PHY (%i) !\n", ret);
  877. return ret;
  878. }
  879. /*
  880. * Configure QCUs/DCUs
  881. */
  882. ret = ath5k_hw_init_queues(ah);
  883. if (ret)
  884. return ret;
  885. /*
  886. * Initialize DMA/Interrupts
  887. */
  888. ath5k_hw_dma_init(ah);
  889. /* Enable 32KHz clock function for AR5212+ chips
  890. * Set clocks to 32KHz operation and use an
  891. * external 32KHz crystal when sleeping if one
  892. * exists */
  893. if (ah->ah_version == AR5K_AR5212 &&
  894. op_mode != NL80211_IFTYPE_AP)
  895. ath5k_hw_set_sleep_clock(ah, true);
  896. /*
  897. * Disable beacons and reset the TSF
  898. */
  899. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
  900. ath5k_hw_reset_tsf(ah);
  901. return 0;
  902. }