reset.c 37 KB

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