reset.c 31 KB

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