reset.c 33 KB

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