reset.c 37 KB

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