reset.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 <linux/platform_device.h>
  28. #include "ath5k.h"
  29. #include "reg.h"
  30. #include "base.h"
  31. #include "debug.h"
  32. /******************\
  33. * Helper functions *
  34. \******************/
  35. /*
  36. * Check if a register write has been completed
  37. */
  38. int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
  39. bool is_set)
  40. {
  41. int i;
  42. u32 data;
  43. for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
  44. data = ath5k_hw_reg_read(ah, reg);
  45. if (is_set && (data & flag))
  46. break;
  47. else if ((data & flag) == val)
  48. break;
  49. udelay(15);
  50. }
  51. return (i <= 0) ? -EAGAIN : 0;
  52. }
  53. /*************************\
  54. * Clock related functions *
  55. \*************************/
  56. /**
  57. * ath5k_hw_htoclock - Translate usec to hw clock units
  58. *
  59. * @ah: The &struct ath5k_hw
  60. * @usec: value in microseconds
  61. */
  62. unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
  63. {
  64. struct ath_common *common = ath5k_hw_common(ah);
  65. return usec * common->clockrate;
  66. }
  67. /**
  68. * ath5k_hw_clocktoh - Translate hw clock units to usec
  69. * @clock: value in hw clock units
  70. */
  71. unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
  72. {
  73. struct ath_common *common = ath5k_hw_common(ah);
  74. return clock / common->clockrate;
  75. }
  76. /**
  77. * ath5k_hw_init_core_clock - Initialize core clock
  78. *
  79. * @ah The &struct ath5k_hw
  80. *
  81. * Initialize core clock parameters (usec, usec32, latencies etc).
  82. */
  83. static void ath5k_hw_init_core_clock(struct ath5k_hw *ah)
  84. {
  85. struct ieee80211_channel *channel = ah->ah_current_channel;
  86. struct ath_common *common = ath5k_hw_common(ah);
  87. u32 usec_reg, txlat, rxlat, usec, clock, sclock, txf2txs;
  88. /*
  89. * Set core clock frequency
  90. */
  91. switch (channel->hw_value) {
  92. case AR5K_MODE_11A:
  93. clock = 40;
  94. break;
  95. case AR5K_MODE_11B:
  96. clock = 22;
  97. break;
  98. case AR5K_MODE_11G:
  99. default:
  100. clock = 44;
  101. break;
  102. }
  103. /* Use clock multiplier for non-default
  104. * bwmode */
  105. switch (ah->ah_bwmode) {
  106. case AR5K_BWMODE_40MHZ:
  107. clock *= 2;
  108. break;
  109. case AR5K_BWMODE_10MHZ:
  110. clock /= 2;
  111. break;
  112. case AR5K_BWMODE_5MHZ:
  113. clock /= 4;
  114. break;
  115. default:
  116. break;
  117. }
  118. common->clockrate = clock;
  119. /*
  120. * Set USEC parameters
  121. */
  122. /* Set USEC counter on PCU*/
  123. usec = clock - 1;
  124. usec = AR5K_REG_SM(usec, AR5K_USEC_1);
  125. /* Set usec duration on DCU */
  126. if (ah->ah_version != AR5K_AR5210)
  127. AR5K_REG_WRITE_BITS(ah, AR5K_DCU_GBL_IFS_MISC,
  128. AR5K_DCU_GBL_IFS_MISC_USEC_DUR,
  129. clock);
  130. /* Set 32MHz USEC counter */
  131. if ((ah->ah_radio == AR5K_RF5112) ||
  132. (ah->ah_radio == AR5K_RF2413) ||
  133. (ah->ah_radio == AR5K_RF5413) ||
  134. (ah->ah_radio == AR5K_RF2316) ||
  135. (ah->ah_radio == AR5K_RF2317))
  136. /* Remain on 40MHz clock ? */
  137. sclock = 40 - 1;
  138. else
  139. sclock = 32 - 1;
  140. sclock = AR5K_REG_SM(sclock, AR5K_USEC_32);
  141. /*
  142. * Set tx/rx latencies
  143. */
  144. usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
  145. txlat = AR5K_REG_MS(usec_reg, AR5K_USEC_TX_LATENCY_5211);
  146. rxlat = AR5K_REG_MS(usec_reg, AR5K_USEC_RX_LATENCY_5211);
  147. /*
  148. * Set default Tx frame to Tx data start delay
  149. */
  150. txf2txs = AR5K_INIT_TXF2TXD_START_DEFAULT;
  151. /*
  152. * 5210 initvals don't include usec settings
  153. * so we need to use magic values here for
  154. * tx/rx latencies
  155. */
  156. if (ah->ah_version == AR5K_AR5210) {
  157. /* same for turbo */
  158. txlat = AR5K_INIT_TX_LATENCY_5210;
  159. rxlat = AR5K_INIT_RX_LATENCY_5210;
  160. }
  161. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  162. /* 5311 has different tx/rx latency masks
  163. * from 5211, since we deal 5311 the same
  164. * as 5211 when setting initvals, shift
  165. * values here to their proper locations
  166. *
  167. * Note: Initvals indicate tx/rx/ latencies
  168. * are the same for turbo mode */
  169. txlat = AR5K_REG_SM(txlat, AR5K_USEC_TX_LATENCY_5210);
  170. rxlat = AR5K_REG_SM(rxlat, AR5K_USEC_RX_LATENCY_5210);
  171. } else
  172. switch (ah->ah_bwmode) {
  173. case AR5K_BWMODE_10MHZ:
  174. txlat = AR5K_REG_SM(txlat * 2,
  175. AR5K_USEC_TX_LATENCY_5211);
  176. rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
  177. AR5K_USEC_RX_LATENCY_5211);
  178. txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_10MHZ;
  179. break;
  180. case AR5K_BWMODE_5MHZ:
  181. txlat = AR5K_REG_SM(txlat * 4,
  182. AR5K_USEC_TX_LATENCY_5211);
  183. rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
  184. AR5K_USEC_RX_LATENCY_5211);
  185. txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_5MHZ;
  186. break;
  187. case AR5K_BWMODE_40MHZ:
  188. txlat = AR5K_INIT_TX_LAT_MIN;
  189. rxlat = AR5K_REG_SM(rxlat / 2,
  190. AR5K_USEC_RX_LATENCY_5211);
  191. txf2txs = AR5K_INIT_TXF2TXD_START_DEFAULT;
  192. break;
  193. default:
  194. break;
  195. }
  196. usec_reg = (usec | sclock | txlat | rxlat);
  197. ath5k_hw_reg_write(ah, usec_reg, AR5K_USEC);
  198. /* On 5112 set tx frame to tx data start delay */
  199. if (ah->ah_radio == AR5K_RF5112) {
  200. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL2,
  201. AR5K_PHY_RF_CTL2_TXF2TXD_START,
  202. txf2txs);
  203. }
  204. }
  205. /*
  206. * If there is an external 32KHz crystal available, use it
  207. * as ref. clock instead of 32/40MHz clock and baseband clocks
  208. * to save power during sleep or restore normal 32/40MHz
  209. * operation.
  210. *
  211. * XXX: When operating on 32KHz certain PHY registers (27 - 31,
  212. * 123 - 127) require delay on access.
  213. */
  214. static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
  215. {
  216. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  217. u32 scal, spending, sclock;
  218. /* Only set 32KHz settings if we have an external
  219. * 32KHz crystal present */
  220. if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
  221. AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
  222. enable) {
  223. /* 1 usec/cycle */
  224. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
  225. /* Set up tsf increment on each cycle */
  226. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
  227. /* Set baseband sleep control registers
  228. * and sleep control rate */
  229. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  230. if ((ah->ah_radio == AR5K_RF5112) ||
  231. (ah->ah_radio == AR5K_RF5413) ||
  232. (ah->ah_radio == AR5K_RF2316) ||
  233. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  234. spending = 0x14;
  235. else
  236. spending = 0x18;
  237. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  238. if ((ah->ah_radio == AR5K_RF5112) ||
  239. (ah->ah_radio == AR5K_RF5413) ||
  240. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  241. ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
  242. ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
  243. ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
  244. ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
  245. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  246. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
  247. } else {
  248. ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
  249. ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
  250. ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
  251. ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
  252. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  253. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
  254. }
  255. /* Enable sleep clock operation */
  256. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
  257. AR5K_PCICFG_SLEEP_CLOCK_EN);
  258. } else {
  259. /* Disable sleep clock operation and
  260. * restore default parameters */
  261. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  262. AR5K_PCICFG_SLEEP_CLOCK_EN);
  263. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  264. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
  265. /* Set DAC/ADC delays */
  266. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  267. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  268. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  269. scal = AR5K_PHY_SCAL_32MHZ_2417;
  270. else if (ee->ee_is_hb63)
  271. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  272. else
  273. scal = AR5K_PHY_SCAL_32MHZ;
  274. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  275. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  276. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  277. if ((ah->ah_radio == AR5K_RF5112) ||
  278. (ah->ah_radio == AR5K_RF5413) ||
  279. (ah->ah_radio == AR5K_RF2316) ||
  280. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  281. spending = 0x14;
  282. else
  283. spending = 0x18;
  284. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  285. /* Set up tsf increment on each cycle */
  286. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
  287. if ((ah->ah_radio == AR5K_RF5112) ||
  288. (ah->ah_radio == AR5K_RF5413) ||
  289. (ah->ah_radio == AR5K_RF2316) ||
  290. (ah->ah_radio == AR5K_RF2317))
  291. sclock = 40 - 1;
  292. else
  293. sclock = 32 - 1;
  294. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, sclock);
  295. }
  296. }
  297. /*********************\
  298. * Reset/Sleep control *
  299. \*********************/
  300. /*
  301. * Reset chipset
  302. */
  303. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  304. {
  305. int ret;
  306. u32 mask = val ? val : ~0U;
  307. /* Read-and-clear RX Descriptor Pointer*/
  308. ath5k_hw_reg_read(ah, AR5K_RXDP);
  309. /*
  310. * Reset the device and wait until success
  311. */
  312. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  313. /* Wait at least 128 PCI clocks */
  314. udelay(15);
  315. if (ah->ah_version == AR5K_AR5210) {
  316. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  317. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  318. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  319. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  320. } else {
  321. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  322. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  323. }
  324. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  325. /*
  326. * Reset configuration register (for hw byte-swap). Note that this
  327. * is only set for big endian. We do the necessary magic in
  328. * AR5K_INIT_CFG.
  329. */
  330. if ((val & AR5K_RESET_CTL_PCU) == 0)
  331. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  332. return ret;
  333. }
  334. /*
  335. * Reset AHB chipset
  336. * AR5K_RESET_CTL_PCU flag resets WMAC
  337. * AR5K_RESET_CTL_BASEBAND flag resets WBB
  338. */
  339. static int ath5k_hw_wisoc_reset(struct ath5k_hw *ah, u32 flags)
  340. {
  341. u32 mask = flags ? flags : ~0U;
  342. u32 __iomem *reg;
  343. u32 regval;
  344. u32 val = 0;
  345. /* ah->ah_mac_srev is not available at this point yet */
  346. if (ah->devid >= AR5K_SREV_AR2315_R6) {
  347. reg = (u32 __iomem *) AR5K_AR2315_RESET;
  348. if (mask & AR5K_RESET_CTL_PCU)
  349. val |= AR5K_AR2315_RESET_WMAC;
  350. if (mask & AR5K_RESET_CTL_BASEBAND)
  351. val |= AR5K_AR2315_RESET_BB_WARM;
  352. } else {
  353. reg = (u32 __iomem *) AR5K_AR5312_RESET;
  354. if (to_platform_device(ah->dev)->id == 0) {
  355. if (mask & AR5K_RESET_CTL_PCU)
  356. val |= AR5K_AR5312_RESET_WMAC0;
  357. if (mask & AR5K_RESET_CTL_BASEBAND)
  358. val |= AR5K_AR5312_RESET_BB0_COLD |
  359. AR5K_AR5312_RESET_BB0_WARM;
  360. } else {
  361. if (mask & AR5K_RESET_CTL_PCU)
  362. val |= AR5K_AR5312_RESET_WMAC1;
  363. if (mask & AR5K_RESET_CTL_BASEBAND)
  364. val |= AR5K_AR5312_RESET_BB1_COLD |
  365. AR5K_AR5312_RESET_BB1_WARM;
  366. }
  367. }
  368. /* Put BB/MAC into reset */
  369. regval = __raw_readl(reg);
  370. __raw_writel(regval | val, reg);
  371. regval = __raw_readl(reg);
  372. udelay(100);
  373. /* Bring BB/MAC out of reset */
  374. __raw_writel(regval & ~val, reg);
  375. regval = __raw_readl(reg);
  376. /*
  377. * Reset configuration register (for hw byte-swap). Note that this
  378. * is only set for big endian. We do the necessary magic in
  379. * AR5K_INIT_CFG.
  380. */
  381. if ((flags & AR5K_RESET_CTL_PCU) == 0)
  382. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  383. return 0;
  384. }
  385. /*
  386. * Sleep control
  387. */
  388. static int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  389. bool set_chip, u16 sleep_duration)
  390. {
  391. unsigned int i;
  392. u32 staid, data;
  393. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  394. switch (mode) {
  395. case AR5K_PM_AUTO:
  396. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  397. /* fallthrough */
  398. case AR5K_PM_NETWORK_SLEEP:
  399. if (set_chip)
  400. ath5k_hw_reg_write(ah,
  401. AR5K_SLEEP_CTL_SLE_ALLOW |
  402. sleep_duration,
  403. AR5K_SLEEP_CTL);
  404. staid |= AR5K_STA_ID1_PWR_SV;
  405. break;
  406. case AR5K_PM_FULL_SLEEP:
  407. if (set_chip)
  408. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  409. AR5K_SLEEP_CTL);
  410. staid |= AR5K_STA_ID1_PWR_SV;
  411. break;
  412. case AR5K_PM_AWAKE:
  413. staid &= ~AR5K_STA_ID1_PWR_SV;
  414. if (!set_chip)
  415. goto commit;
  416. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  417. /* If card is down we 'll get 0xffff... so we
  418. * need to clean this up before we write the register
  419. */
  420. if (data & 0xffc00000)
  421. data = 0;
  422. else
  423. /* Preserve sleep duration etc */
  424. data = data & ~AR5K_SLEEP_CTL_SLE;
  425. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  426. AR5K_SLEEP_CTL);
  427. udelay(15);
  428. for (i = 200; i > 0; i--) {
  429. /* Check if the chip did wake up */
  430. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  431. AR5K_PCICFG_SPWR_DN) == 0)
  432. break;
  433. /* Wait a bit and retry */
  434. udelay(50);
  435. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  436. AR5K_SLEEP_CTL);
  437. }
  438. /* Fail if the chip didn't wake up */
  439. if (i == 0)
  440. return -EIO;
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. commit:
  446. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  447. return 0;
  448. }
  449. /*
  450. * Put device on hold
  451. *
  452. * Put MAC and Baseband on warm reset and
  453. * keep that state (don't clean sleep control
  454. * register). After this MAC and Baseband are
  455. * disabled and a full reset is needed to come
  456. * back. This way we save as much power as possible
  457. * without putting the card on full sleep.
  458. */
  459. int ath5k_hw_on_hold(struct ath5k_hw *ah)
  460. {
  461. struct pci_dev *pdev = ah->pdev;
  462. u32 bus_flags;
  463. int ret;
  464. if (ath5k_get_bus_type(ah) == ATH_AHB)
  465. return 0;
  466. /* Make sure device is awake */
  467. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  468. if (ret) {
  469. ATH5K_ERR(ah, "failed to wakeup the MAC Chip\n");
  470. return ret;
  471. }
  472. /*
  473. * Put chipset on warm reset...
  474. *
  475. * Note: putting PCI core on warm reset on PCI-E cards
  476. * results card to hang and always return 0xffff... so
  477. * we ignore that flag for PCI-E cards. On PCI cards
  478. * this flag gets cleared after 64 PCI clocks.
  479. */
  480. bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
  481. if (ah->ah_version == AR5K_AR5210) {
  482. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  483. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  484. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  485. mdelay(2);
  486. } else {
  487. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  488. AR5K_RESET_CTL_BASEBAND | bus_flags);
  489. }
  490. if (ret) {
  491. ATH5K_ERR(ah, "failed to put device on warm reset\n");
  492. return -EIO;
  493. }
  494. /* ...wakeup again!*/
  495. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  496. if (ret) {
  497. ATH5K_ERR(ah, "failed to put device on hold\n");
  498. return ret;
  499. }
  500. return ret;
  501. }
  502. /*
  503. * Bring up MAC + PHY Chips and program PLL
  504. * Channel is NULL for the initial wakeup.
  505. */
  506. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  507. {
  508. struct pci_dev *pdev = ah->pdev;
  509. u32 turbo, mode, clock, bus_flags;
  510. int ret;
  511. turbo = 0;
  512. mode = 0;
  513. clock = 0;
  514. if ((ath5k_get_bus_type(ah) != ATH_AHB) || channel) {
  515. /* Wakeup the device */
  516. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  517. if (ret) {
  518. ATH5K_ERR(ah, "failed to wakeup the MAC Chip\n");
  519. return ret;
  520. }
  521. }
  522. /*
  523. * Put chipset on warm reset...
  524. *
  525. * Note: putting PCI core on warm reset on PCI-E cards
  526. * results card to hang and always return 0xffff... so
  527. * we ignore that flag for PCI-E cards. On PCI cards
  528. * this flag gets cleared after 64 PCI clocks.
  529. */
  530. bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
  531. if (ah->ah_version == AR5K_AR5210) {
  532. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  533. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  534. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  535. mdelay(2);
  536. } else {
  537. if (ath5k_get_bus_type(ah) == ATH_AHB)
  538. ret = ath5k_hw_wisoc_reset(ah, AR5K_RESET_CTL_PCU |
  539. AR5K_RESET_CTL_BASEBAND);
  540. else
  541. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  542. AR5K_RESET_CTL_BASEBAND | bus_flags);
  543. }
  544. if (ret) {
  545. ATH5K_ERR(ah, "failed to reset the MAC Chip\n");
  546. return -EIO;
  547. }
  548. /* ...wakeup again!...*/
  549. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  550. if (ret) {
  551. ATH5K_ERR(ah, "failed to resume the MAC Chip\n");
  552. return ret;
  553. }
  554. /* ...reset configuration register on Wisoc ...
  555. * ...clear reset control register and pull device out of
  556. * warm reset on others */
  557. if (ath5k_get_bus_type(ah) == ATH_AHB)
  558. ret = ath5k_hw_wisoc_reset(ah, 0);
  559. else
  560. ret = ath5k_hw_nic_reset(ah, 0);
  561. if (ret) {
  562. ATH5K_ERR(ah, "failed to warm reset the MAC Chip\n");
  563. return -EIO;
  564. }
  565. /* On initialization skip PLL programming since we don't have
  566. * a channel / mode set yet */
  567. if (!channel)
  568. return 0;
  569. if (ah->ah_version != AR5K_AR5210) {
  570. /*
  571. * Get channel mode flags
  572. */
  573. if (ah->ah_radio >= AR5K_RF5112) {
  574. mode = AR5K_PHY_MODE_RAD_RF5112;
  575. clock = AR5K_PHY_PLL_RF5112;
  576. } else {
  577. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  578. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  579. }
  580. if (channel->band == IEEE80211_BAND_2GHZ) {
  581. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  582. clock |= AR5K_PHY_PLL_44MHZ;
  583. if (channel->hw_value == AR5K_MODE_11B) {
  584. mode |= AR5K_PHY_MODE_MOD_CCK;
  585. } else {
  586. /* XXX Dynamic OFDM/CCK is not supported by the
  587. * AR5211 so we set MOD_OFDM for plain g (no
  588. * CCK headers) operation. We need to test
  589. * this, 5211 might support ofdm-only g after
  590. * all, there are also initial register values
  591. * in the code for g mode (see initvals.c).
  592. */
  593. if (ah->ah_version == AR5K_AR5211)
  594. mode |= AR5K_PHY_MODE_MOD_OFDM;
  595. else
  596. mode |= AR5K_PHY_MODE_MOD_DYN;
  597. }
  598. } else if (channel->band == IEEE80211_BAND_5GHZ) {
  599. mode |= (AR5K_PHY_MODE_FREQ_5GHZ |
  600. AR5K_PHY_MODE_MOD_OFDM);
  601. /* Different PLL setting for 5413 */
  602. if (ah->ah_radio == AR5K_RF5413)
  603. clock = AR5K_PHY_PLL_40MHZ_5413;
  604. else
  605. clock |= AR5K_PHY_PLL_40MHZ;
  606. } else {
  607. ATH5K_ERR(ah, "invalid radio frequency mode\n");
  608. return -EINVAL;
  609. }
  610. /*XXX: Can bwmode be used with dynamic mode ?
  611. * (I don't think it supports 44MHz) */
  612. /* On 2425 initvals TURBO_SHORT is not present */
  613. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
  614. turbo = AR5K_PHY_TURBO_MODE |
  615. (ah->ah_radio == AR5K_RF2425) ? 0 :
  616. AR5K_PHY_TURBO_SHORT;
  617. } else if (ah->ah_bwmode != AR5K_BWMODE_DEFAULT) {
  618. if (ah->ah_radio == AR5K_RF5413) {
  619. mode |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
  620. AR5K_PHY_MODE_HALF_RATE :
  621. AR5K_PHY_MODE_QUARTER_RATE;
  622. } else if (ah->ah_version == AR5K_AR5212) {
  623. clock |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
  624. AR5K_PHY_PLL_HALF_RATE :
  625. AR5K_PHY_PLL_QUARTER_RATE;
  626. }
  627. }
  628. } else { /* Reset the device */
  629. /* ...enable Atheros turbo mode if requested */
  630. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  631. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  632. AR5K_PHY_TURBO);
  633. }
  634. if (ah->ah_version != AR5K_AR5210) {
  635. /* ...update PLL if needed */
  636. if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
  637. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  638. udelay(300);
  639. }
  640. /* ...set the PHY operating mode */
  641. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  642. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  643. }
  644. return 0;
  645. }
  646. /**************************************\
  647. * Post-initvals register modifications *
  648. \**************************************/
  649. /* TODO: Half/Quarter rate */
  650. static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
  651. struct ieee80211_channel *channel)
  652. {
  653. if (ah->ah_version == AR5K_AR5212 &&
  654. ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  655. /* Setup ADC control */
  656. ath5k_hw_reg_write(ah,
  657. (AR5K_REG_SM(2,
  658. AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
  659. AR5K_REG_SM(2,
  660. AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
  661. AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
  662. AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
  663. AR5K_PHY_ADC_CTL);
  664. /* Disable barker RSSI threshold */
  665. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  666. AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
  667. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  668. AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
  669. /* Set the mute mask */
  670. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  671. }
  672. /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
  673. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
  674. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
  675. /* Enable DCU double buffering */
  676. if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
  677. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  678. AR5K_TXCFG_DCU_DBL_BUF_DIS);
  679. /* Set fast ADC */
  680. if ((ah->ah_radio == AR5K_RF5413) ||
  681. (ah->ah_radio == AR5K_RF2317) ||
  682. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  683. u32 fast_adc = true;
  684. if (channel->center_freq == 2462 ||
  685. channel->center_freq == 2467)
  686. fast_adc = 0;
  687. /* Only update if needed */
  688. if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
  689. ath5k_hw_reg_write(ah, fast_adc,
  690. AR5K_PHY_FAST_ADC);
  691. }
  692. /* Fix for first revision of the RF5112 RF chipset */
  693. if (ah->ah_radio == AR5K_RF5112 &&
  694. ah->ah_radio_5ghz_revision <
  695. AR5K_SREV_RAD_5112A) {
  696. u32 data;
  697. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  698. AR5K_PHY_CCKTXCTL);
  699. if (channel->band == IEEE80211_BAND_5GHZ)
  700. data = 0xffb81020;
  701. else
  702. data = 0xffb80d20;
  703. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  704. }
  705. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  706. /* Clear QCU/DCU clock gating register */
  707. ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
  708. /* Set DAC/ADC delays */
  709. ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ_5311,
  710. AR5K_PHY_SCAL);
  711. /* Enable PCU FIFO corruption ECO */
  712. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  713. AR5K_DIAG_SW_ECO_ENABLE);
  714. }
  715. if (ah->ah_bwmode) {
  716. /* Increase PHY switch and AGC settling time
  717. * on turbo mode (ath5k_hw_commit_eeprom_settings
  718. * will override settling time if available) */
  719. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
  720. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  721. AR5K_PHY_SETTLING_AGC,
  722. AR5K_AGC_SETTLING_TURBO);
  723. /* XXX: Initvals indicate we only increase
  724. * switch time on AR5212, 5211 and 5210
  725. * only change agc time (bug?) */
  726. if (ah->ah_version == AR5K_AR5212)
  727. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  728. AR5K_PHY_SETTLING_SWITCH,
  729. AR5K_SWITCH_SETTLING_TURBO);
  730. if (ah->ah_version == AR5K_AR5210) {
  731. /* Set Frame Control Register */
  732. ath5k_hw_reg_write(ah,
  733. (AR5K_PHY_FRAME_CTL_INI |
  734. AR5K_PHY_TURBO_MODE |
  735. AR5K_PHY_TURBO_SHORT | 0x2020),
  736. AR5K_PHY_FRAME_CTL_5210);
  737. }
  738. /* On 5413 PHY force window length for half/quarter rate*/
  739. } else if ((ah->ah_mac_srev >= AR5K_SREV_AR5424) &&
  740. (ah->ah_mac_srev <= AR5K_SREV_AR5414)) {
  741. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL_5211,
  742. AR5K_PHY_FRAME_CTL_WIN_LEN,
  743. 3);
  744. }
  745. } else if (ah->ah_version == AR5K_AR5210) {
  746. /* Set Frame Control Register for normal operation */
  747. ath5k_hw_reg_write(ah, (AR5K_PHY_FRAME_CTL_INI | 0x1020),
  748. AR5K_PHY_FRAME_CTL_5210);
  749. }
  750. }
  751. static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
  752. struct ieee80211_channel *channel)
  753. {
  754. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  755. s16 cck_ofdm_pwr_delta;
  756. u8 ee_mode;
  757. /* TODO: Add support for AR5210 EEPROM */
  758. if (ah->ah_version == AR5K_AR5210)
  759. return;
  760. ee_mode = ath5k_eeprom_mode_from_channel(channel);
  761. /* Adjust power delta for channel 14 */
  762. if (channel->center_freq == 2484)
  763. cck_ofdm_pwr_delta =
  764. ((ee->ee_cck_ofdm_power_delta -
  765. ee->ee_scaled_cck_delta) * 2) / 10;
  766. else
  767. cck_ofdm_pwr_delta =
  768. (ee->ee_cck_ofdm_power_delta * 2) / 10;
  769. /* Set CCK to OFDM power delta on tx power
  770. * adjustment register */
  771. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  772. if (channel->hw_value == AR5K_MODE_11G)
  773. ath5k_hw_reg_write(ah,
  774. AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
  775. AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
  776. AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
  777. AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
  778. AR5K_PHY_TX_PWR_ADJ);
  779. else
  780. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
  781. } else {
  782. /* For older revs we scale power on sw during tx power
  783. * setup */
  784. ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
  785. ah->ah_txpower.txp_cck_ofdm_gainf_delta =
  786. ee->ee_cck_ofdm_gain_delta;
  787. }
  788. /* XXX: necessary here? is called from ath5k_hw_set_antenna_mode()
  789. * too */
  790. ath5k_hw_set_antenna_switch(ah, ee_mode);
  791. /* Noise floor threshold */
  792. ath5k_hw_reg_write(ah,
  793. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  794. AR5K_PHY_NFTHRES);
  795. if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) &&
  796. (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
  797. /* Switch settling time (Turbo) */
  798. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  799. AR5K_PHY_SETTLING_SWITCH,
  800. ee->ee_switch_settling_turbo[ee_mode]);
  801. /* Tx/Rx attenuation (Turbo) */
  802. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  803. AR5K_PHY_GAIN_TXRX_ATTEN,
  804. ee->ee_atn_tx_rx_turbo[ee_mode]);
  805. /* ADC/PGA desired size (Turbo) */
  806. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  807. AR5K_PHY_DESIRED_SIZE_ADC,
  808. ee->ee_adc_desired_size_turbo[ee_mode]);
  809. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  810. AR5K_PHY_DESIRED_SIZE_PGA,
  811. ee->ee_pga_desired_size_turbo[ee_mode]);
  812. /* Tx/Rx margin (Turbo) */
  813. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  814. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  815. ee->ee_margin_tx_rx_turbo[ee_mode]);
  816. } else {
  817. /* Switch settling time */
  818. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  819. AR5K_PHY_SETTLING_SWITCH,
  820. ee->ee_switch_settling[ee_mode]);
  821. /* Tx/Rx attenuation */
  822. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  823. AR5K_PHY_GAIN_TXRX_ATTEN,
  824. ee->ee_atn_tx_rx[ee_mode]);
  825. /* ADC/PGA desired size */
  826. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  827. AR5K_PHY_DESIRED_SIZE_ADC,
  828. ee->ee_adc_desired_size[ee_mode]);
  829. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  830. AR5K_PHY_DESIRED_SIZE_PGA,
  831. ee->ee_pga_desired_size[ee_mode]);
  832. /* Tx/Rx margin */
  833. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  834. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  835. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  836. ee->ee_margin_tx_rx[ee_mode]);
  837. }
  838. /* XPA delays */
  839. ath5k_hw_reg_write(ah,
  840. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  841. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  842. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  843. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  844. /* XLNA delay */
  845. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
  846. AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
  847. ee->ee_tx_end2xlna_enable[ee_mode]);
  848. /* Thresh64 (ANI) */
  849. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
  850. AR5K_PHY_NF_THRESH62,
  851. ee->ee_thr_62[ee_mode]);
  852. /* False detect backoff for channels
  853. * that have spur noise. Write the new
  854. * cyclic power RSSI threshold. */
  855. if (ath5k_hw_chan_has_spur_noise(ah, channel))
  856. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  857. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  858. AR5K_INIT_CYCRSSI_THR1 +
  859. ee->ee_false_detect[ee_mode]);
  860. else
  861. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  862. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  863. AR5K_INIT_CYCRSSI_THR1);
  864. /* I/Q correction (set enable bit last to match HAL sources) */
  865. /* TODO: Per channel i/q infos ? */
  866. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  867. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
  868. ee->ee_i_cal[ee_mode]);
  869. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
  870. ee->ee_q_cal[ee_mode]);
  871. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  872. }
  873. /* Heavy clipping -disable for now */
  874. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  875. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  876. }
  877. /*********************\
  878. * Main reset function *
  879. \*********************/
  880. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  881. struct ieee80211_channel *channel, bool fast, bool skip_pcu)
  882. {
  883. u32 s_seq[10], s_led[3], tsf_up, tsf_lo;
  884. u8 mode;
  885. int i, ret;
  886. tsf_up = 0;
  887. tsf_lo = 0;
  888. mode = 0;
  889. /*
  890. * Sanity check for fast flag
  891. * Fast channel change only available
  892. * on AR2413/AR5413.
  893. */
  894. if (fast && (ah->ah_radio != AR5K_RF2413) &&
  895. (ah->ah_radio != AR5K_RF5413))
  896. fast = 0;
  897. /* Disable sleep clock operation
  898. * to avoid register access delay on certain
  899. * PHY registers */
  900. if (ah->ah_version == AR5K_AR5212)
  901. ath5k_hw_set_sleep_clock(ah, false);
  902. /*
  903. * Stop PCU
  904. */
  905. ath5k_hw_stop_rx_pcu(ah);
  906. /*
  907. * Stop DMA
  908. *
  909. * Note: If DMA didn't stop continue
  910. * since only a reset will fix it.
  911. */
  912. ret = ath5k_hw_dma_stop(ah);
  913. /* RF Bus grant won't work if we have pending
  914. * frames */
  915. if (ret && fast) {
  916. ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
  917. "DMA didn't stop, falling back to normal reset\n");
  918. fast = 0;
  919. /* Non fatal, just continue with
  920. * normal reset */
  921. ret = 0;
  922. }
  923. mode = channel->hw_value;
  924. switch (mode) {
  925. case AR5K_MODE_11A:
  926. break;
  927. case AR5K_MODE_11G:
  928. if (ah->ah_version <= AR5K_AR5211) {
  929. ATH5K_ERR(ah,
  930. "G mode not available on 5210/5211");
  931. return -EINVAL;
  932. }
  933. break;
  934. case AR5K_MODE_11B:
  935. if (ah->ah_version < AR5K_AR5211) {
  936. ATH5K_ERR(ah,
  937. "B mode not available on 5210");
  938. return -EINVAL;
  939. }
  940. break;
  941. default:
  942. ATH5K_ERR(ah,
  943. "invalid channel: %d\n", channel->center_freq);
  944. return -EINVAL;
  945. }
  946. /*
  947. * If driver requested fast channel change and DMA has stopped
  948. * go on. If it fails continue with a normal reset.
  949. */
  950. if (fast) {
  951. ret = ath5k_hw_phy_init(ah, channel, mode, true);
  952. if (ret) {
  953. ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
  954. "fast chan change failed, falling back to normal reset\n");
  955. /* Non fatal, can happen eg.
  956. * on mode change */
  957. ret = 0;
  958. } else {
  959. ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
  960. "fast chan change successful\n");
  961. return 0;
  962. }
  963. }
  964. /*
  965. * Save some registers before a reset
  966. */
  967. if (ah->ah_version != AR5K_AR5210) {
  968. /*
  969. * Save frame sequence count
  970. * For revs. after Oahu, only save
  971. * seq num for DCU 0 (Global seq num)
  972. */
  973. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  974. for (i = 0; i < 10; i++)
  975. s_seq[i] = ath5k_hw_reg_read(ah,
  976. AR5K_QUEUE_DCU_SEQNUM(i));
  977. } else {
  978. s_seq[0] = ath5k_hw_reg_read(ah,
  979. AR5K_QUEUE_DCU_SEQNUM(0));
  980. }
  981. /* TSF accelerates on AR5211 during reset
  982. * As a workaround save it here and restore
  983. * it later so that it's back in time after
  984. * reset. This way it'll get re-synced on the
  985. * next beacon without breaking ad-hoc.
  986. *
  987. * On AR5212 TSF is almost preserved across a
  988. * reset so it stays back in time anyway and
  989. * we don't have to save/restore it.
  990. *
  991. * XXX: Since this breaks power saving we have
  992. * to disable power saving until we receive the
  993. * next beacon, so we can resync beacon timers */
  994. if (ah->ah_version == AR5K_AR5211) {
  995. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  996. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  997. }
  998. }
  999. /*GPIOs*/
  1000. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  1001. AR5K_PCICFG_LEDSTATE;
  1002. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  1003. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  1004. /*
  1005. * Since we are going to write rf buffer
  1006. * check if we have any pending gain_F
  1007. * optimization settings
  1008. */
  1009. if (ah->ah_version == AR5K_AR5212 &&
  1010. (ah->ah_radio <= AR5K_RF5112)) {
  1011. if (!fast && ah->ah_rf_banks != NULL)
  1012. ath5k_hw_gainf_calibrate(ah);
  1013. }
  1014. /* Wakeup the device */
  1015. ret = ath5k_hw_nic_wakeup(ah, channel);
  1016. if (ret)
  1017. return ret;
  1018. /* PHY access enable */
  1019. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  1020. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1021. else
  1022. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  1023. AR5K_PHY(0));
  1024. /* Write initial settings */
  1025. ret = ath5k_hw_write_initvals(ah, mode, skip_pcu);
  1026. if (ret)
  1027. return ret;
  1028. /* Initialize core clock settings */
  1029. ath5k_hw_init_core_clock(ah);
  1030. /*
  1031. * Tweak initval settings for revised
  1032. * chipsets and add some more config
  1033. * bits
  1034. */
  1035. ath5k_hw_tweak_initval_settings(ah, channel);
  1036. /* Commit values from EEPROM */
  1037. ath5k_hw_commit_eeprom_settings(ah, channel);
  1038. /*
  1039. * Restore saved values
  1040. */
  1041. /* Seqnum, TSF */
  1042. if (ah->ah_version != AR5K_AR5210) {
  1043. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  1044. for (i = 0; i < 10; i++)
  1045. ath5k_hw_reg_write(ah, s_seq[i],
  1046. AR5K_QUEUE_DCU_SEQNUM(i));
  1047. } else {
  1048. ath5k_hw_reg_write(ah, s_seq[0],
  1049. AR5K_QUEUE_DCU_SEQNUM(0));
  1050. }
  1051. if (ah->ah_version == AR5K_AR5211) {
  1052. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  1053. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  1054. }
  1055. }
  1056. /* Ledstate */
  1057. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  1058. /* Gpio settings */
  1059. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  1060. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  1061. /*
  1062. * Initialize PCU
  1063. */
  1064. ath5k_hw_pcu_init(ah, op_mode, mode);
  1065. /*
  1066. * Initialize PHY
  1067. */
  1068. ret = ath5k_hw_phy_init(ah, channel, mode, false);
  1069. if (ret) {
  1070. ATH5K_ERR(ah,
  1071. "failed to initialize PHY (%i) !\n", ret);
  1072. return ret;
  1073. }
  1074. /*
  1075. * Configure QCUs/DCUs
  1076. */
  1077. ret = ath5k_hw_init_queues(ah);
  1078. if (ret)
  1079. return ret;
  1080. /*
  1081. * Initialize DMA/Interrupts
  1082. */
  1083. ath5k_hw_dma_init(ah);
  1084. /*
  1085. * Enable 32KHz clock function for AR5212+ chips
  1086. * Set clocks to 32KHz operation and use an
  1087. * external 32KHz crystal when sleeping if one
  1088. * exists.
  1089. * Disabled by default because it is also disabled in
  1090. * other drivers and it is known to cause stability
  1091. * issues on some devices
  1092. */
  1093. if (ah->ah_use_32khz_clock && ah->ah_version == AR5K_AR5212 &&
  1094. op_mode != NL80211_IFTYPE_AP)
  1095. ath5k_hw_set_sleep_clock(ah, true);
  1096. /*
  1097. * Disable beacons and reset the TSF
  1098. */
  1099. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
  1100. ath5k_hw_reset_tsf(ah);
  1101. return 0;
  1102. }