reset.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. #define _ATH5K_RESET
  22. /*****************************\
  23. Reset functions and helpers
  24. \*****************************/
  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 "ath5k.h"
  29. #include "reg.h"
  30. #include "base.h"
  31. #include "debug.h"
  32. /**
  33. * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
  34. *
  35. * @ah: the &struct ath5k_hw
  36. * @channel: the currently set channel upon reset
  37. *
  38. * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
  39. * operation on the AR5212 upon reset. This is a helper for ath5k_hw_reset().
  40. *
  41. * Since delta slope is floating point we split it on its exponent and
  42. * mantissa and provide these values on hw.
  43. *
  44. * For more infos i think this patent is related
  45. * http://www.freepatentsonline.com/7184495.html
  46. */
  47. static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  48. struct ieee80211_channel *channel)
  49. {
  50. /* Get exponent and mantissa and set it */
  51. u32 coef_scaled, coef_exp, coef_man,
  52. ds_coef_exp, ds_coef_man, clock;
  53. BUG_ON(!(ah->ah_version == AR5K_AR5212) ||
  54. !(channel->hw_value & CHANNEL_OFDM));
  55. /* Get coefficient
  56. * ALGO: coef = (5 * clock / carrier_freq) / 2
  57. * we scale coef by shifting clock value by 24 for
  58. * better precision since we use integers */
  59. /* TODO: Half/quarter rate */
  60. clock = (channel->hw_value & CHANNEL_TURBO) ? 80 : 40;
  61. coef_scaled = ((5 * (clock << 24)) / 2) / channel->center_freq;
  62. /* Get exponent
  63. * ALGO: coef_exp = 14 - highest set bit position */
  64. coef_exp = ilog2(coef_scaled);
  65. /* Doesn't make sense if it's zero*/
  66. if (!coef_scaled || !coef_exp)
  67. return -EINVAL;
  68. /* Note: we've shifted coef_scaled by 24 */
  69. coef_exp = 14 - (coef_exp - 24);
  70. /* Get mantissa (significant digits)
  71. * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */
  72. coef_man = coef_scaled +
  73. (1 << (24 - coef_exp - 1));
  74. /* Calculate delta slope coefficient exponent
  75. * and mantissa (remove scaling) and set them on hw */
  76. ds_coef_man = coef_man >> (24 - coef_exp);
  77. ds_coef_exp = coef_exp - 16;
  78. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  79. AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
  80. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  81. AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
  82. return 0;
  83. }
  84. /*
  85. * index into rates for control rates, we can set it up like this because
  86. * this is only used for AR5212 and we know it supports G mode
  87. */
  88. static const unsigned int control_rates[] =
  89. { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
  90. /**
  91. * ath5k_hw_write_rate_duration - fill rate code to duration table
  92. *
  93. * @ah: the &struct ath5k_hw
  94. * @mode: one of enum ath5k_driver_mode
  95. *
  96. * Write the rate code to duration table upon hw reset. This is a helper for
  97. * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout on
  98. * the hardware, based on current mode, for each rate. The rates which are
  99. * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
  100. * different rate code so we write their value twice (one for long preample
  101. * and one for short).
  102. *
  103. * Note: Band doesn't matter here, if we set the values for OFDM it works
  104. * on both a and g modes. So all we have to do is set values for all g rates
  105. * that include all OFDM and CCK rates. If we operate in turbo or xr/half/
  106. * quarter rate mode, we need to use another set of bitrates (that's why we
  107. * need the mode parameter) but we don't handle these proprietary modes yet.
  108. */
  109. static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
  110. unsigned int mode)
  111. {
  112. struct ath5k_softc *sc = ah->ah_sc;
  113. struct ieee80211_rate *rate;
  114. unsigned int i;
  115. /* Write rate duration table */
  116. for (i = 0; i < sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates; i++) {
  117. u32 reg;
  118. u16 tx_time;
  119. rate = &sc->sbands[IEEE80211_BAND_2GHZ].bitrates[control_rates[i]];
  120. /* Set ACK timeout */
  121. reg = AR5K_RATE_DUR(rate->hw_value);
  122. /* An ACK frame consists of 10 bytes. If you add the FCS,
  123. * which ieee80211_generic_frame_duration() adds,
  124. * its 14 bytes. Note we use the control rate and not the
  125. * actual rate for this rate. See mac80211 tx.c
  126. * ieee80211_duration() for a brief description of
  127. * what rate we should choose to TX ACKs. */
  128. tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
  129. sc->vif, 10, rate));
  130. ath5k_hw_reg_write(ah, tx_time, reg);
  131. if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
  132. continue;
  133. /*
  134. * We're not distinguishing short preamble here,
  135. * This is true, all we'll get is a longer value here
  136. * which is not necessarilly bad. We could use
  137. * export ieee80211_frame_duration() but that needs to be
  138. * fixed first to be properly used by mac802111 drivers:
  139. *
  140. * - remove erp stuff and let the routine figure ofdm
  141. * erp rates
  142. * - remove passing argument ieee80211_local as
  143. * drivers don't have access to it
  144. * - move drivers using ieee80211_generic_frame_duration()
  145. * to this
  146. */
  147. ath5k_hw_reg_write(ah, tx_time,
  148. reg + (AR5K_SET_SHORT_PREAMBLE << 2));
  149. }
  150. }
  151. /*
  152. * Reset chipset
  153. */
  154. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  155. {
  156. int ret;
  157. u32 mask = val ? val : ~0U;
  158. ATH5K_TRACE(ah->ah_sc);
  159. /* Read-and-clear RX Descriptor Pointer*/
  160. ath5k_hw_reg_read(ah, AR5K_RXDP);
  161. /*
  162. * Reset the device and wait until success
  163. */
  164. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  165. /* Wait at least 128 PCI clocks */
  166. udelay(15);
  167. if (ah->ah_version == AR5K_AR5210) {
  168. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  169. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  170. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  171. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  172. } else {
  173. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  174. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  175. }
  176. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  177. /*
  178. * Reset configuration register (for hw byte-swap). Note that this
  179. * is only set for big endian. We do the necessary magic in
  180. * AR5K_INIT_CFG.
  181. */
  182. if ((val & AR5K_RESET_CTL_PCU) == 0)
  183. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  184. return ret;
  185. }
  186. /*
  187. * Sleep control
  188. */
  189. int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  190. bool set_chip, u16 sleep_duration)
  191. {
  192. unsigned int i;
  193. u32 staid, data;
  194. ATH5K_TRACE(ah->ah_sc);
  195. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  196. switch (mode) {
  197. case AR5K_PM_AUTO:
  198. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  199. /* fallthrough */
  200. case AR5K_PM_NETWORK_SLEEP:
  201. if (set_chip)
  202. ath5k_hw_reg_write(ah,
  203. AR5K_SLEEP_CTL_SLE_ALLOW |
  204. sleep_duration,
  205. AR5K_SLEEP_CTL);
  206. staid |= AR5K_STA_ID1_PWR_SV;
  207. break;
  208. case AR5K_PM_FULL_SLEEP:
  209. if (set_chip)
  210. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  211. AR5K_SLEEP_CTL);
  212. staid |= AR5K_STA_ID1_PWR_SV;
  213. break;
  214. case AR5K_PM_AWAKE:
  215. staid &= ~AR5K_STA_ID1_PWR_SV;
  216. if (!set_chip)
  217. goto commit;
  218. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  219. /* If card is down we 'll get 0xffff... so we
  220. * need to clean this up before we write the register
  221. */
  222. if (data & 0xffc00000)
  223. data = 0;
  224. else
  225. /* Preserve sleep duration etc */
  226. data = data & ~AR5K_SLEEP_CTL_SLE;
  227. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  228. AR5K_SLEEP_CTL);
  229. udelay(15);
  230. for (i = 200; i > 0; i--) {
  231. /* Check if the chip did wake up */
  232. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  233. AR5K_PCICFG_SPWR_DN) == 0)
  234. break;
  235. /* Wait a bit and retry */
  236. udelay(50);
  237. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  238. AR5K_SLEEP_CTL);
  239. }
  240. /* Fail if the chip didn't wake up */
  241. if (i == 0)
  242. return -EIO;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. commit:
  248. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  249. return 0;
  250. }
  251. /*
  252. * Put device on hold
  253. *
  254. * Put MAC and Baseband on warm reset and
  255. * keep that state (don't clean sleep control
  256. * register). After this MAC and Baseband are
  257. * disabled and a full reset is needed to come
  258. * back. This way we save as much power as possible
  259. * without puting the card on full sleep.
  260. */
  261. int ath5k_hw_on_hold(struct ath5k_hw *ah)
  262. {
  263. struct pci_dev *pdev = ah->ah_sc->pdev;
  264. u32 bus_flags;
  265. int ret;
  266. /* Make sure device is awake */
  267. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  268. if (ret) {
  269. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  270. return ret;
  271. }
  272. /*
  273. * Put chipset on warm reset...
  274. *
  275. * Note: puting PCI core on warm reset on PCI-E cards
  276. * results card to hang and always return 0xffff... so
  277. * we ingore that flag for PCI-E cards. On PCI cards
  278. * this flag gets cleared after 64 PCI clocks.
  279. */
  280. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  281. if (ah->ah_version == AR5K_AR5210) {
  282. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  283. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  284. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  285. mdelay(2);
  286. } else {
  287. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  288. AR5K_RESET_CTL_BASEBAND | bus_flags);
  289. }
  290. if (ret) {
  291. ATH5K_ERR(ah->ah_sc, "failed to put device on warm reset\n");
  292. return -EIO;
  293. }
  294. /* ...wakeup again!*/
  295. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  296. if (ret) {
  297. ATH5K_ERR(ah->ah_sc, "failed to put device on hold\n");
  298. return ret;
  299. }
  300. return ret;
  301. }
  302. /*
  303. * Bring up MAC + PHY Chips and program PLL
  304. * TODO: Half/Quarter rate support
  305. */
  306. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  307. {
  308. struct pci_dev *pdev = ah->ah_sc->pdev;
  309. u32 turbo, mode, clock, bus_flags;
  310. int ret;
  311. turbo = 0;
  312. mode = 0;
  313. clock = 0;
  314. ATH5K_TRACE(ah->ah_sc);
  315. /* Wakeup the device */
  316. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  317. if (ret) {
  318. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  319. return ret;
  320. }
  321. /*
  322. * Put chipset on warm reset...
  323. *
  324. * Note: puting PCI core on warm reset on PCI-E cards
  325. * results card to hang and always return 0xffff... so
  326. * we ingore that flag for PCI-E cards. On PCI cards
  327. * this flag gets cleared after 64 PCI clocks.
  328. */
  329. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  330. if (ah->ah_version == AR5K_AR5210) {
  331. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  332. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  333. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  334. mdelay(2);
  335. } else {
  336. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  337. AR5K_RESET_CTL_BASEBAND | bus_flags);
  338. }
  339. if (ret) {
  340. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
  341. return -EIO;
  342. }
  343. /* ...wakeup again!...*/
  344. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  345. if (ret) {
  346. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  347. return ret;
  348. }
  349. /* ...clear reset control register and pull device out of
  350. * warm reset */
  351. if (ath5k_hw_nic_reset(ah, 0)) {
  352. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  353. return -EIO;
  354. }
  355. /* On initialization skip PLL programming since we don't have
  356. * a channel / mode set yet */
  357. if (initial)
  358. return 0;
  359. if (ah->ah_version != AR5K_AR5210) {
  360. /*
  361. * Get channel mode flags
  362. */
  363. if (ah->ah_radio >= AR5K_RF5112) {
  364. mode = AR5K_PHY_MODE_RAD_RF5112;
  365. clock = AR5K_PHY_PLL_RF5112;
  366. } else {
  367. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  368. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  369. }
  370. if (flags & CHANNEL_2GHZ) {
  371. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  372. clock |= AR5K_PHY_PLL_44MHZ;
  373. if (flags & CHANNEL_CCK) {
  374. mode |= AR5K_PHY_MODE_MOD_CCK;
  375. } else if (flags & CHANNEL_OFDM) {
  376. /* XXX Dynamic OFDM/CCK is not supported by the
  377. * AR5211 so we set MOD_OFDM for plain g (no
  378. * CCK headers) operation. We need to test
  379. * this, 5211 might support ofdm-only g after
  380. * all, there are also initial register values
  381. * in the code for g mode (see initvals.c). */
  382. if (ah->ah_version == AR5K_AR5211)
  383. mode |= AR5K_PHY_MODE_MOD_OFDM;
  384. else
  385. mode |= AR5K_PHY_MODE_MOD_DYN;
  386. } else {
  387. ATH5K_ERR(ah->ah_sc,
  388. "invalid radio modulation mode\n");
  389. return -EINVAL;
  390. }
  391. } else if (flags & CHANNEL_5GHZ) {
  392. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  393. if (ah->ah_radio == AR5K_RF5413)
  394. clock = AR5K_PHY_PLL_40MHZ_5413;
  395. else
  396. clock |= AR5K_PHY_PLL_40MHZ;
  397. if (flags & CHANNEL_OFDM)
  398. mode |= AR5K_PHY_MODE_MOD_OFDM;
  399. else {
  400. ATH5K_ERR(ah->ah_sc,
  401. "invalid radio modulation mode\n");
  402. return -EINVAL;
  403. }
  404. } else {
  405. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  406. return -EINVAL;
  407. }
  408. if (flags & CHANNEL_TURBO)
  409. turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
  410. } else { /* Reset the device */
  411. /* ...enable Atheros turbo mode if requested */
  412. if (flags & CHANNEL_TURBO)
  413. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  414. AR5K_PHY_TURBO);
  415. }
  416. if (ah->ah_version != AR5K_AR5210) {
  417. /* ...update PLL if needed */
  418. if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
  419. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  420. udelay(300);
  421. }
  422. /* ...set the PHY operating mode */
  423. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  424. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  425. }
  426. return 0;
  427. }
  428. /*
  429. * If there is an external 32KHz crystal available, use it
  430. * as ref. clock instead of 32/40MHz clock and baseband clocks
  431. * to save power during sleep or restore normal 32/40MHz
  432. * operation.
  433. *
  434. * XXX: When operating on 32KHz certain PHY registers (27 - 31,
  435. * 123 - 127) require delay on access.
  436. */
  437. static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
  438. {
  439. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  440. u32 scal, spending, usec32;
  441. /* Only set 32KHz settings if we have an external
  442. * 32KHz crystal present */
  443. if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
  444. AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
  445. enable) {
  446. /* 1 usec/cycle */
  447. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
  448. /* Set up tsf increment on each cycle */
  449. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
  450. /* Set baseband sleep control registers
  451. * and sleep control rate */
  452. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  453. if ((ah->ah_radio == AR5K_RF5112) ||
  454. (ah->ah_radio == AR5K_RF5413) ||
  455. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  456. spending = 0x14;
  457. else
  458. spending = 0x18;
  459. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  460. if ((ah->ah_radio == AR5K_RF5112) ||
  461. (ah->ah_radio == AR5K_RF5413) ||
  462. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  463. ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
  464. ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
  465. ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
  466. ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
  467. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  468. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
  469. } else {
  470. ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
  471. ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
  472. ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
  473. ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
  474. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  475. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
  476. }
  477. /* Enable sleep clock operation */
  478. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
  479. AR5K_PCICFG_SLEEP_CLOCK_EN);
  480. } else {
  481. /* Disable sleep clock operation and
  482. * restore default parameters */
  483. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  484. AR5K_PCICFG_SLEEP_CLOCK_EN);
  485. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  486. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
  487. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  488. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  489. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  490. scal = AR5K_PHY_SCAL_32MHZ_2417;
  491. else if (ee->ee_is_hb63)
  492. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  493. else
  494. scal = AR5K_PHY_SCAL_32MHZ;
  495. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  496. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  497. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  498. if ((ah->ah_radio == AR5K_RF5112) ||
  499. (ah->ah_radio == AR5K_RF5413) ||
  500. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  501. spending = 0x14;
  502. else
  503. spending = 0x18;
  504. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  505. if ((ah->ah_radio == AR5K_RF5112) ||
  506. (ah->ah_radio == AR5K_RF5413))
  507. usec32 = 39;
  508. else
  509. usec32 = 31;
  510. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
  511. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
  512. }
  513. return;
  514. }
  515. /* TODO: Half/Quarter rate */
  516. static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
  517. struct ieee80211_channel *channel)
  518. {
  519. if (ah->ah_version == AR5K_AR5212 &&
  520. ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  521. /* Setup ADC control */
  522. ath5k_hw_reg_write(ah,
  523. (AR5K_REG_SM(2,
  524. AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
  525. AR5K_REG_SM(2,
  526. AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
  527. AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
  528. AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
  529. AR5K_PHY_ADC_CTL);
  530. /* Disable barker RSSI threshold */
  531. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  532. AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
  533. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  534. AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
  535. /* Set the mute mask */
  536. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  537. }
  538. /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
  539. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
  540. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
  541. /* Enable DCU double buffering */
  542. if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
  543. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  544. AR5K_TXCFG_DCU_DBL_BUF_DIS);
  545. /* Set DAC/ADC delays */
  546. if (ah->ah_version == AR5K_AR5212) {
  547. u32 scal;
  548. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  549. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  550. scal = AR5K_PHY_SCAL_32MHZ_2417;
  551. else if (ee->ee_is_hb63)
  552. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  553. else
  554. scal = AR5K_PHY_SCAL_32MHZ;
  555. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  556. }
  557. /* Set fast ADC */
  558. if ((ah->ah_radio == AR5K_RF5413) ||
  559. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  560. u32 fast_adc = true;
  561. if (channel->center_freq == 2462 ||
  562. channel->center_freq == 2467)
  563. fast_adc = 0;
  564. /* Only update if needed */
  565. if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
  566. ath5k_hw_reg_write(ah, fast_adc,
  567. AR5K_PHY_FAST_ADC);
  568. }
  569. /* Fix for first revision of the RF5112 RF chipset */
  570. if (ah->ah_radio == AR5K_RF5112 &&
  571. ah->ah_radio_5ghz_revision <
  572. AR5K_SREV_RAD_5112A) {
  573. u32 data;
  574. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  575. AR5K_PHY_CCKTXCTL);
  576. if (channel->hw_value & CHANNEL_5GHZ)
  577. data = 0xffb81020;
  578. else
  579. data = 0xffb80d20;
  580. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  581. }
  582. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  583. u32 usec_reg;
  584. /* 5311 has different tx/rx latency masks
  585. * from 5211, since we deal 5311 the same
  586. * as 5211 when setting initvals, shift
  587. * values here to their proper locations */
  588. usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
  589. ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
  590. AR5K_USEC_32 |
  591. AR5K_USEC_TX_LATENCY_5211 |
  592. AR5K_REG_SM(29,
  593. AR5K_USEC_RX_LATENCY_5210)),
  594. AR5K_USEC_5211);
  595. /* Clear QCU/DCU clock gating register */
  596. ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
  597. /* Set DAC/ADC delays */
  598. ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
  599. /* Enable PCU FIFO corruption ECO */
  600. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  601. AR5K_DIAG_SW_ECO_ENABLE);
  602. }
  603. }
  604. static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
  605. struct ieee80211_channel *channel, u8 *ant, u8 ee_mode)
  606. {
  607. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  608. s16 cck_ofdm_pwr_delta;
  609. /* Adjust power delta for channel 14 */
  610. if (channel->center_freq == 2484)
  611. cck_ofdm_pwr_delta =
  612. ((ee->ee_cck_ofdm_power_delta -
  613. ee->ee_scaled_cck_delta) * 2) / 10;
  614. else
  615. cck_ofdm_pwr_delta =
  616. (ee->ee_cck_ofdm_power_delta * 2) / 10;
  617. /* Set CCK to OFDM power delta on tx power
  618. * adjustment register */
  619. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  620. if (channel->hw_value == CHANNEL_G)
  621. ath5k_hw_reg_write(ah,
  622. AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
  623. AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
  624. AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
  625. AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
  626. AR5K_PHY_TX_PWR_ADJ);
  627. else
  628. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
  629. } else {
  630. /* For older revs we scale power on sw during tx power
  631. * setup */
  632. ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
  633. ah->ah_txpower.txp_cck_ofdm_gainf_delta =
  634. ee->ee_cck_ofdm_gain_delta;
  635. }
  636. /* Set antenna idle switch table */
  637. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
  638. AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
  639. (ah->ah_ant_ctl[ee_mode][0] |
  640. AR5K_PHY_ANT_CTL_TXRX_EN));
  641. /* Set antenna switch tables */
  642. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[0]],
  643. AR5K_PHY_ANT_SWITCH_TABLE_0);
  644. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant[1]],
  645. AR5K_PHY_ANT_SWITCH_TABLE_1);
  646. /* Noise floor threshold */
  647. ath5k_hw_reg_write(ah,
  648. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  649. AR5K_PHY_NFTHRES);
  650. if ((channel->hw_value & CHANNEL_TURBO) &&
  651. (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
  652. /* Switch settling time (Turbo) */
  653. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  654. AR5K_PHY_SETTLING_SWITCH,
  655. ee->ee_switch_settling_turbo[ee_mode]);
  656. /* Tx/Rx attenuation (Turbo) */
  657. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  658. AR5K_PHY_GAIN_TXRX_ATTEN,
  659. ee->ee_atn_tx_rx_turbo[ee_mode]);
  660. /* ADC/PGA desired size (Turbo) */
  661. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  662. AR5K_PHY_DESIRED_SIZE_ADC,
  663. ee->ee_adc_desired_size_turbo[ee_mode]);
  664. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  665. AR5K_PHY_DESIRED_SIZE_PGA,
  666. ee->ee_pga_desired_size_turbo[ee_mode]);
  667. /* Tx/Rx margin (Turbo) */
  668. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  669. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  670. ee->ee_margin_tx_rx_turbo[ee_mode]);
  671. } else {
  672. /* Switch settling time */
  673. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  674. AR5K_PHY_SETTLING_SWITCH,
  675. ee->ee_switch_settling[ee_mode]);
  676. /* Tx/Rx attenuation */
  677. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  678. AR5K_PHY_GAIN_TXRX_ATTEN,
  679. ee->ee_atn_tx_rx[ee_mode]);
  680. /* ADC/PGA desired size */
  681. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  682. AR5K_PHY_DESIRED_SIZE_ADC,
  683. ee->ee_adc_desired_size[ee_mode]);
  684. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  685. AR5K_PHY_DESIRED_SIZE_PGA,
  686. ee->ee_pga_desired_size[ee_mode]);
  687. /* Tx/Rx margin */
  688. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  689. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  690. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  691. ee->ee_margin_tx_rx[ee_mode]);
  692. }
  693. /* XPA delays */
  694. ath5k_hw_reg_write(ah,
  695. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  696. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  697. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  698. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  699. /* XLNA delay */
  700. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
  701. AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
  702. ee->ee_tx_end2xlna_enable[ee_mode]);
  703. /* Thresh64 (ANI) */
  704. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
  705. AR5K_PHY_NF_THRESH62,
  706. ee->ee_thr_62[ee_mode]);
  707. /* False detect backoff for channels
  708. * that have spur noise. Write the new
  709. * cyclic power RSSI threshold. */
  710. if (ath5k_hw_chan_has_spur_noise(ah, channel))
  711. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  712. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  713. AR5K_INIT_CYCRSSI_THR1 +
  714. ee->ee_false_detect[ee_mode]);
  715. else
  716. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  717. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  718. AR5K_INIT_CYCRSSI_THR1);
  719. /* I/Q correction (set enable bit last to match HAL sources) */
  720. /* TODO: Per channel i/q infos ? */
  721. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  722. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
  723. ee->ee_i_cal[ee_mode]);
  724. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
  725. ee->ee_q_cal[ee_mode]);
  726. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  727. }
  728. /* Heavy clipping -disable for now */
  729. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  730. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  731. return;
  732. }
  733. /*
  734. * Main reset function
  735. */
  736. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  737. struct ieee80211_channel *channel, bool change_channel)
  738. {
  739. struct ath_common *common = ath5k_hw_common(ah);
  740. u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
  741. u32 phy_tst1;
  742. u8 mode, freq, ee_mode, ant[2];
  743. int i, ret;
  744. ATH5K_TRACE(ah->ah_sc);
  745. s_ant = 0;
  746. ee_mode = 0;
  747. staid1_flags = 0;
  748. tsf_up = 0;
  749. tsf_lo = 0;
  750. freq = 0;
  751. mode = 0;
  752. /*
  753. * Save some registers before a reset
  754. */
  755. /*DCU/Antenna selection not available on 5210*/
  756. if (ah->ah_version != AR5K_AR5210) {
  757. switch (channel->hw_value & CHANNEL_MODES) {
  758. case CHANNEL_A:
  759. mode = AR5K_MODE_11A;
  760. freq = AR5K_INI_RFGAIN_5GHZ;
  761. ee_mode = AR5K_EEPROM_MODE_11A;
  762. break;
  763. case CHANNEL_G:
  764. mode = AR5K_MODE_11G;
  765. freq = AR5K_INI_RFGAIN_2GHZ;
  766. ee_mode = AR5K_EEPROM_MODE_11G;
  767. break;
  768. case CHANNEL_B:
  769. mode = AR5K_MODE_11B;
  770. freq = AR5K_INI_RFGAIN_2GHZ;
  771. ee_mode = AR5K_EEPROM_MODE_11B;
  772. break;
  773. case CHANNEL_T:
  774. mode = AR5K_MODE_11A_TURBO;
  775. freq = AR5K_INI_RFGAIN_5GHZ;
  776. ee_mode = AR5K_EEPROM_MODE_11A;
  777. break;
  778. case CHANNEL_TG:
  779. if (ah->ah_version == AR5K_AR5211) {
  780. ATH5K_ERR(ah->ah_sc,
  781. "TurboG mode not available on 5211");
  782. return -EINVAL;
  783. }
  784. mode = AR5K_MODE_11G_TURBO;
  785. freq = AR5K_INI_RFGAIN_2GHZ;
  786. ee_mode = AR5K_EEPROM_MODE_11G;
  787. break;
  788. case CHANNEL_XR:
  789. if (ah->ah_version == AR5K_AR5211) {
  790. ATH5K_ERR(ah->ah_sc,
  791. "XR mode not available on 5211");
  792. return -EINVAL;
  793. }
  794. mode = AR5K_MODE_XR;
  795. freq = AR5K_INI_RFGAIN_5GHZ;
  796. ee_mode = AR5K_EEPROM_MODE_11A;
  797. break;
  798. default:
  799. ATH5K_ERR(ah->ah_sc,
  800. "invalid channel: %d\n", channel->center_freq);
  801. return -EINVAL;
  802. }
  803. if (change_channel) {
  804. /*
  805. * Save frame sequence count
  806. * For revs. after Oahu, only save
  807. * seq num for DCU 0 (Global seq num)
  808. */
  809. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  810. for (i = 0; i < 10; i++)
  811. s_seq[i] = ath5k_hw_reg_read(ah,
  812. AR5K_QUEUE_DCU_SEQNUM(i));
  813. } else {
  814. s_seq[0] = ath5k_hw_reg_read(ah,
  815. AR5K_QUEUE_DCU_SEQNUM(0));
  816. }
  817. /* TSF accelerates on AR5211 durring reset
  818. * As a workaround save it here and restore
  819. * it later so that it's back in time after
  820. * reset. This way it'll get re-synced on the
  821. * next beacon without breaking ad-hoc.
  822. *
  823. * On AR5212 TSF is almost preserved across a
  824. * reset so it stays back in time anyway and
  825. * we don't have to save/restore it.
  826. *
  827. * XXX: Since this breaks power saving we have
  828. * to disable power saving until we receive the
  829. * next beacon, so we can resync beacon timers */
  830. if (ah->ah_version == AR5K_AR5211) {
  831. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  832. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  833. }
  834. }
  835. /* Save default antenna */
  836. s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  837. if (ah->ah_version == AR5K_AR5212) {
  838. /* Restore normal 32/40MHz clock operation
  839. * to avoid register access delay on certain
  840. * PHY registers */
  841. ath5k_hw_set_sleep_clock(ah, false);
  842. /* Since we are going to write rf buffer
  843. * check if we have any pending gain_F
  844. * optimization settings */
  845. if (change_channel && ah->ah_rf_banks != NULL)
  846. ath5k_hw_gainf_calibrate(ah);
  847. }
  848. }
  849. /*GPIOs*/
  850. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  851. AR5K_PCICFG_LEDSTATE;
  852. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  853. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  854. /* AR5K_STA_ID1 flags, only preserve antenna
  855. * settings and ack/cts rate mode */
  856. staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
  857. (AR5K_STA_ID1_DEFAULT_ANTENNA |
  858. AR5K_STA_ID1_DESC_ANTENNA |
  859. AR5K_STA_ID1_RTS_DEF_ANTENNA |
  860. AR5K_STA_ID1_ACKCTS_6MB |
  861. AR5K_STA_ID1_BASE_RATE_11B |
  862. AR5K_STA_ID1_SELFGEN_DEF_ANT);
  863. /* Wakeup the device */
  864. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  865. if (ret)
  866. return ret;
  867. /*
  868. * Initialize operating mode
  869. */
  870. ah->ah_op_mode = op_mode;
  871. /* PHY access enable */
  872. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  873. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  874. else
  875. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  876. AR5K_PHY(0));
  877. /* Write initial settings */
  878. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  879. if (ret)
  880. return ret;
  881. /*
  882. * 5211/5212 Specific
  883. */
  884. if (ah->ah_version != AR5K_AR5210) {
  885. /*
  886. * Write initial RF gain settings
  887. * This should work for both 5111/5112
  888. */
  889. ret = ath5k_hw_rfgain_init(ah, freq);
  890. if (ret)
  891. return ret;
  892. mdelay(1);
  893. /*
  894. * Tweak initval settings for revised
  895. * chipsets and add some more config
  896. * bits
  897. */
  898. ath5k_hw_tweak_initval_settings(ah, channel);
  899. /*
  900. * Set TX power
  901. */
  902. ret = ath5k_hw_txpower(ah, channel, ee_mode,
  903. ah->ah_txpower.txp_max_pwr / 2);
  904. if (ret)
  905. return ret;
  906. /* Write rate duration table only on AR5212 and if
  907. * virtual interface has already been brought up
  908. * XXX: rethink this after new mode changes to
  909. * mac80211 are integrated */
  910. if (ah->ah_version == AR5K_AR5212 &&
  911. ah->ah_sc->vif != NULL)
  912. ath5k_hw_write_rate_duration(ah, mode);
  913. /*
  914. * Write RF buffer
  915. */
  916. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  917. if (ret)
  918. return ret;
  919. /* Write OFDM timings on 5212*/
  920. if (ah->ah_version == AR5K_AR5212 &&
  921. channel->hw_value & CHANNEL_OFDM) {
  922. struct ath5k_eeprom_info *ee =
  923. &ah->ah_capabilities.cap_eeprom;
  924. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  925. if (ret)
  926. return ret;
  927. /* Note: According to docs we can have a newer
  928. * EEPROM on old hardware, so we need to verify
  929. * that our hardware is new enough to have spur
  930. * mitigation registers (delta phase etc) */
  931. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 ||
  932. (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  933. ee->ee_version >= AR5K_EEPROM_VERSION_5_3))
  934. ath5k_hw_set_spur_mitigation_filter(ah,
  935. channel);
  936. }
  937. /*Enable/disable 802.11b mode on 5111
  938. (enable 2111 frequency converter + CCK)*/
  939. if (ah->ah_radio == AR5K_RF5111) {
  940. if (mode == AR5K_MODE_11B)
  941. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  942. AR5K_TXCFG_B_MODE);
  943. else
  944. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  945. AR5K_TXCFG_B_MODE);
  946. }
  947. /*
  948. * In case a fixed antenna was set as default
  949. * use the same switch table twice.
  950. */
  951. if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
  952. ant[0] = ant[1] = AR5K_ANT_SWTABLE_A;
  953. else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
  954. ant[0] = ant[1] = AR5K_ANT_SWTABLE_B;
  955. else {
  956. ant[0] = AR5K_ANT_SWTABLE_A;
  957. ant[1] = AR5K_ANT_SWTABLE_B;
  958. }
  959. /* Commit values from EEPROM */
  960. ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
  961. } else {
  962. /*
  963. * For 5210 we do all initialization using
  964. * initvals, so we don't have to modify
  965. * any settings (5210 also only supports
  966. * a/aturbo modes)
  967. */
  968. mdelay(1);
  969. /* Disable phy and wait */
  970. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  971. mdelay(1);
  972. }
  973. /*
  974. * Restore saved values
  975. */
  976. /*DCU/Antenna selection not available on 5210*/
  977. if (ah->ah_version != AR5K_AR5210) {
  978. if (change_channel) {
  979. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  980. for (i = 0; i < 10; i++)
  981. ath5k_hw_reg_write(ah, s_seq[i],
  982. AR5K_QUEUE_DCU_SEQNUM(i));
  983. } else {
  984. ath5k_hw_reg_write(ah, s_seq[0],
  985. AR5K_QUEUE_DCU_SEQNUM(0));
  986. }
  987. if (ah->ah_version == AR5K_AR5211) {
  988. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  989. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  990. }
  991. }
  992. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  993. }
  994. /* Ledstate */
  995. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  996. /* Gpio settings */
  997. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  998. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  999. /* Restore sta_id flags and preserve our mac address*/
  1000. ath5k_hw_reg_write(ah,
  1001. get_unaligned_le32(common->macaddr),
  1002. AR5K_STA_ID0);
  1003. ath5k_hw_reg_write(ah,
  1004. staid1_flags | get_unaligned_le16(common->macaddr + 4),
  1005. AR5K_STA_ID1);
  1006. /*
  1007. * Configure PCU
  1008. */
  1009. /* Restore bssid and bssid mask */
  1010. ath5k_hw_set_associd(ah);
  1011. /* Set PCU config */
  1012. ath5k_hw_set_opmode(ah);
  1013. /* Clear any pending interrupts
  1014. * PISR/SISR Not available on 5210 */
  1015. if (ah->ah_version != AR5K_AR5210)
  1016. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  1017. /* Set RSSI/BRSSI thresholds
  1018. *
  1019. * Note: If we decide to set this value
  1020. * dynamicaly, have in mind that when AR5K_RSSI_THR
  1021. * register is read it might return 0x40 if we haven't
  1022. * wrote anything to it plus BMISS RSSI threshold is zeroed.
  1023. * So doing a save/restore procedure here isn't the right
  1024. * choice. Instead store it on ath5k_hw */
  1025. ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
  1026. AR5K_TUNE_BMISS_THRES <<
  1027. AR5K_RSSI_THR_BMISS_S),
  1028. AR5K_RSSI_THR);
  1029. /* MIC QoS support */
  1030. if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
  1031. ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
  1032. ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
  1033. }
  1034. /* QoS NOACK Policy */
  1035. if (ah->ah_version == AR5K_AR5212) {
  1036. ath5k_hw_reg_write(ah,
  1037. AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
  1038. AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
  1039. AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
  1040. AR5K_QOS_NOACK);
  1041. }
  1042. /*
  1043. * Configure PHY
  1044. */
  1045. /* Set channel on PHY */
  1046. ret = ath5k_hw_channel(ah, channel);
  1047. if (ret)
  1048. return ret;
  1049. /*
  1050. * Enable the PHY and wait until completion
  1051. * This includes BaseBand and Synthesizer
  1052. * activation.
  1053. */
  1054. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1055. /*
  1056. * On 5211+ read activation -> rx delay
  1057. * and use it.
  1058. *
  1059. * TODO: Half/quarter rate support
  1060. */
  1061. if (ah->ah_version != AR5K_AR5210) {
  1062. u32 delay;
  1063. delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  1064. AR5K_PHY_RX_DELAY_M;
  1065. delay = (channel->hw_value & CHANNEL_CCK) ?
  1066. ((delay << 2) / 22) : (delay / 10);
  1067. udelay(100 + (2 * delay));
  1068. } else {
  1069. mdelay(1);
  1070. }
  1071. /*
  1072. * Perform ADC test to see if baseband is ready
  1073. * Set tx hold and check adc test register
  1074. */
  1075. phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  1076. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  1077. for (i = 0; i <= 20; i++) {
  1078. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  1079. break;
  1080. udelay(200);
  1081. }
  1082. ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
  1083. /*
  1084. * Start automatic gain control calibration
  1085. *
  1086. * During AGC calibration RX path is re-routed to
  1087. * a power detector so we don't receive anything.
  1088. *
  1089. * This method is used to calibrate some static offsets
  1090. * used together with on-the fly I/Q calibration (the
  1091. * one performed via ath5k_hw_phy_calibrate), that doesn't
  1092. * interrupt rx path.
  1093. *
  1094. * While rx path is re-routed to the power detector we also
  1095. * start a noise floor calibration, to measure the
  1096. * card's noise floor (the noise we measure when we are not
  1097. * transmiting or receiving anything).
  1098. *
  1099. * If we are in a noisy environment AGC calibration may time
  1100. * out and/or noise floor calibration might timeout.
  1101. */
  1102. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1103. AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF);
  1104. /* At the same time start I/Q calibration for QAM constellation
  1105. * -no need for CCK- */
  1106. ah->ah_calibration = false;
  1107. if (!(mode == AR5K_MODE_11B)) {
  1108. ah->ah_calibration = true;
  1109. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1110. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1111. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1112. AR5K_PHY_IQ_RUN);
  1113. }
  1114. /* Wait for gain calibration to finish (we check for I/Q calibration
  1115. * during ath5k_phy_calibrate) */
  1116. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1117. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  1118. ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
  1119. channel->center_freq);
  1120. }
  1121. /* Restore antenna mode */
  1122. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  1123. /* Restore slot time and ACK timeouts */
  1124. if (ah->ah_coverage_class > 0)
  1125. ath5k_hw_set_coverage_class(ah, ah->ah_coverage_class);
  1126. /*
  1127. * Configure QCUs/DCUs
  1128. */
  1129. /* TODO: HW Compression support for data queues */
  1130. /* TODO: Burst prefetch for data queues */
  1131. /*
  1132. * Reset queues and start beacon timers at the end of the reset routine
  1133. * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
  1134. * Note: If we want we can assign multiple qcus on one dcu.
  1135. */
  1136. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  1137. ret = ath5k_hw_reset_tx_queue(ah, i);
  1138. if (ret) {
  1139. ATH5K_ERR(ah->ah_sc,
  1140. "failed to reset TX queue #%d\n", i);
  1141. return ret;
  1142. }
  1143. }
  1144. /*
  1145. * Configure DMA/Interrupts
  1146. */
  1147. /*
  1148. * Set Rx/Tx DMA Configuration
  1149. *
  1150. * Set standard DMA size (128). Note that
  1151. * a DMA size of 512 causes rx overruns and tx errors
  1152. * on pci-e cards (tested on 5424 but since rx overruns
  1153. * also occur on 5416/5418 with madwifi we set 128
  1154. * for all PCI-E cards to be safe).
  1155. *
  1156. * XXX: need to check 5210 for this
  1157. * TODO: Check out tx triger level, it's always 64 on dumps but I
  1158. * guess we can tweak it and see how it goes ;-)
  1159. */
  1160. if (ah->ah_version != AR5K_AR5210) {
  1161. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  1162. AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
  1163. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  1164. AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
  1165. }
  1166. /* Pre-enable interrupts on 5211/5212*/
  1167. if (ah->ah_version != AR5K_AR5210)
  1168. ath5k_hw_set_imr(ah, ah->ah_imr);
  1169. /* Enable 32KHz clock function for AR5212+ chips
  1170. * Set clocks to 32KHz operation and use an
  1171. * external 32KHz crystal when sleeping if one
  1172. * exists */
  1173. if (ah->ah_version == AR5K_AR5212 &&
  1174. ah->ah_op_mode != NL80211_IFTYPE_AP)
  1175. ath5k_hw_set_sleep_clock(ah, true);
  1176. /*
  1177. * Disable beacons and reset the TSF
  1178. */
  1179. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
  1180. ath5k_hw_reset_tsf(ah);
  1181. return 0;
  1182. }
  1183. #undef _ATH5K_RESET