reset.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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 = ath5k_hw_htoclock(1, channel->hw_value & CHANNEL_TURBO);
  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
  720. * TODO: Per channel i/q infos ? */
  721. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  722. AR5K_PHY_IQ_CORR_ENABLE |
  723. (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
  724. ee->ee_q_cal[ee_mode]);
  725. /* Heavy clipping -disable for now */
  726. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  727. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  728. return;
  729. }
  730. /*
  731. * Main reset function
  732. */
  733. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  734. struct ieee80211_channel *channel, bool change_channel)
  735. {
  736. struct ath_common *common = ath5k_hw_common(ah);
  737. u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
  738. u32 phy_tst1;
  739. u8 mode, freq, ee_mode, ant[2];
  740. int i, ret;
  741. ATH5K_TRACE(ah->ah_sc);
  742. s_ant = 0;
  743. ee_mode = 0;
  744. staid1_flags = 0;
  745. tsf_up = 0;
  746. tsf_lo = 0;
  747. freq = 0;
  748. mode = 0;
  749. /*
  750. * Save some registers before a reset
  751. */
  752. /*DCU/Antenna selection not available on 5210*/
  753. if (ah->ah_version != AR5K_AR5210) {
  754. switch (channel->hw_value & CHANNEL_MODES) {
  755. case CHANNEL_A:
  756. mode = AR5K_MODE_11A;
  757. freq = AR5K_INI_RFGAIN_5GHZ;
  758. ee_mode = AR5K_EEPROM_MODE_11A;
  759. break;
  760. case CHANNEL_G:
  761. mode = AR5K_MODE_11G;
  762. freq = AR5K_INI_RFGAIN_2GHZ;
  763. ee_mode = AR5K_EEPROM_MODE_11G;
  764. break;
  765. case CHANNEL_B:
  766. mode = AR5K_MODE_11B;
  767. freq = AR5K_INI_RFGAIN_2GHZ;
  768. ee_mode = AR5K_EEPROM_MODE_11B;
  769. break;
  770. case CHANNEL_T:
  771. mode = AR5K_MODE_11A_TURBO;
  772. freq = AR5K_INI_RFGAIN_5GHZ;
  773. ee_mode = AR5K_EEPROM_MODE_11A;
  774. break;
  775. case CHANNEL_TG:
  776. if (ah->ah_version == AR5K_AR5211) {
  777. ATH5K_ERR(ah->ah_sc,
  778. "TurboG mode not available on 5211");
  779. return -EINVAL;
  780. }
  781. mode = AR5K_MODE_11G_TURBO;
  782. freq = AR5K_INI_RFGAIN_2GHZ;
  783. ee_mode = AR5K_EEPROM_MODE_11G;
  784. break;
  785. case CHANNEL_XR:
  786. if (ah->ah_version == AR5K_AR5211) {
  787. ATH5K_ERR(ah->ah_sc,
  788. "XR mode not available on 5211");
  789. return -EINVAL;
  790. }
  791. mode = AR5K_MODE_XR;
  792. freq = AR5K_INI_RFGAIN_5GHZ;
  793. ee_mode = AR5K_EEPROM_MODE_11A;
  794. break;
  795. default:
  796. ATH5K_ERR(ah->ah_sc,
  797. "invalid channel: %d\n", channel->center_freq);
  798. return -EINVAL;
  799. }
  800. if (change_channel) {
  801. /*
  802. * Save frame sequence count
  803. * For revs. after Oahu, only save
  804. * seq num for DCU 0 (Global seq num)
  805. */
  806. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  807. for (i = 0; i < 10; i++)
  808. s_seq[i] = ath5k_hw_reg_read(ah,
  809. AR5K_QUEUE_DCU_SEQNUM(i));
  810. } else {
  811. s_seq[0] = ath5k_hw_reg_read(ah,
  812. AR5K_QUEUE_DCU_SEQNUM(0));
  813. }
  814. /* TSF accelerates on AR5211 durring reset
  815. * As a workaround save it here and restore
  816. * it later so that it's back in time after
  817. * reset. This way it'll get re-synced on the
  818. * next beacon without breaking ad-hoc.
  819. *
  820. * On AR5212 TSF is almost preserved across a
  821. * reset so it stays back in time anyway and
  822. * we don't have to save/restore it.
  823. *
  824. * XXX: Since this breaks power saving we have
  825. * to disable power saving until we receive the
  826. * next beacon, so we can resync beacon timers */
  827. if (ah->ah_version == AR5K_AR5211) {
  828. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  829. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  830. }
  831. }
  832. /* Save default antenna */
  833. s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  834. if (ah->ah_version == AR5K_AR5212) {
  835. /* Restore normal 32/40MHz clock operation
  836. * to avoid register access delay on certain
  837. * PHY registers */
  838. ath5k_hw_set_sleep_clock(ah, false);
  839. /* Since we are going to write rf buffer
  840. * check if we have any pending gain_F
  841. * optimization settings */
  842. if (change_channel && ah->ah_rf_banks != NULL)
  843. ath5k_hw_gainf_calibrate(ah);
  844. }
  845. }
  846. /*GPIOs*/
  847. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  848. AR5K_PCICFG_LEDSTATE;
  849. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  850. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  851. /* AR5K_STA_ID1 flags, only preserve antenna
  852. * settings and ack/cts rate mode */
  853. staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
  854. (AR5K_STA_ID1_DEFAULT_ANTENNA |
  855. AR5K_STA_ID1_DESC_ANTENNA |
  856. AR5K_STA_ID1_RTS_DEF_ANTENNA |
  857. AR5K_STA_ID1_ACKCTS_6MB |
  858. AR5K_STA_ID1_BASE_RATE_11B |
  859. AR5K_STA_ID1_SELFGEN_DEF_ANT);
  860. /* Wakeup the device */
  861. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  862. if (ret)
  863. return ret;
  864. /*
  865. * Initialize operating mode
  866. */
  867. ah->ah_op_mode = op_mode;
  868. /* PHY access enable */
  869. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  870. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  871. else
  872. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  873. AR5K_PHY(0));
  874. /* Write initial settings */
  875. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  876. if (ret)
  877. return ret;
  878. /*
  879. * 5211/5212 Specific
  880. */
  881. if (ah->ah_version != AR5K_AR5210) {
  882. /*
  883. * Write initial RF gain settings
  884. * This should work for both 5111/5112
  885. */
  886. ret = ath5k_hw_rfgain_init(ah, freq);
  887. if (ret)
  888. return ret;
  889. mdelay(1);
  890. /*
  891. * Tweak initval settings for revised
  892. * chipsets and add some more config
  893. * bits
  894. */
  895. ath5k_hw_tweak_initval_settings(ah, channel);
  896. /*
  897. * Set TX power
  898. */
  899. ret = ath5k_hw_txpower(ah, channel, ee_mode,
  900. ah->ah_txpower.txp_max_pwr / 2);
  901. if (ret)
  902. return ret;
  903. /* Write rate duration table only on AR5212 and if
  904. * virtual interface has already been brought up
  905. * XXX: rethink this after new mode changes to
  906. * mac80211 are integrated */
  907. if (ah->ah_version == AR5K_AR5212 &&
  908. ah->ah_sc->vif != NULL)
  909. ath5k_hw_write_rate_duration(ah, mode);
  910. /*
  911. * Write RF buffer
  912. */
  913. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  914. if (ret)
  915. return ret;
  916. /* Write OFDM timings on 5212*/
  917. if (ah->ah_version == AR5K_AR5212 &&
  918. channel->hw_value & CHANNEL_OFDM) {
  919. struct ath5k_eeprom_info *ee =
  920. &ah->ah_capabilities.cap_eeprom;
  921. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  922. if (ret)
  923. return ret;
  924. /* Note: According to docs we can have a newer
  925. * EEPROM on old hardware, so we need to verify
  926. * that our hardware is new enough to have spur
  927. * mitigation registers (delta phase etc) */
  928. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 ||
  929. (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  930. ee->ee_version >= AR5K_EEPROM_VERSION_5_3))
  931. ath5k_hw_set_spur_mitigation_filter(ah,
  932. channel);
  933. }
  934. /*Enable/disable 802.11b mode on 5111
  935. (enable 2111 frequency converter + CCK)*/
  936. if (ah->ah_radio == AR5K_RF5111) {
  937. if (mode == AR5K_MODE_11B)
  938. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  939. AR5K_TXCFG_B_MODE);
  940. else
  941. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  942. AR5K_TXCFG_B_MODE);
  943. }
  944. /*
  945. * In case a fixed antenna was set as default
  946. * use the same switch table twice.
  947. */
  948. if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
  949. ant[0] = ant[1] = AR5K_ANT_SWTABLE_A;
  950. else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
  951. ant[0] = ant[1] = AR5K_ANT_SWTABLE_B;
  952. else {
  953. ant[0] = AR5K_ANT_SWTABLE_A;
  954. ant[1] = AR5K_ANT_SWTABLE_B;
  955. }
  956. /* Commit values from EEPROM */
  957. ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
  958. } else {
  959. /*
  960. * For 5210 we do all initialization using
  961. * initvals, so we don't have to modify
  962. * any settings (5210 also only supports
  963. * a/aturbo modes)
  964. */
  965. mdelay(1);
  966. /* Disable phy and wait */
  967. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  968. mdelay(1);
  969. }
  970. /*
  971. * Restore saved values
  972. */
  973. /*DCU/Antenna selection not available on 5210*/
  974. if (ah->ah_version != AR5K_AR5210) {
  975. if (change_channel) {
  976. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  977. for (i = 0; i < 10; i++)
  978. ath5k_hw_reg_write(ah, s_seq[i],
  979. AR5K_QUEUE_DCU_SEQNUM(i));
  980. } else {
  981. ath5k_hw_reg_write(ah, s_seq[0],
  982. AR5K_QUEUE_DCU_SEQNUM(0));
  983. }
  984. if (ah->ah_version == AR5K_AR5211) {
  985. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  986. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  987. }
  988. }
  989. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  990. }
  991. /* Ledstate */
  992. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  993. /* Gpio settings */
  994. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  995. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  996. /* Restore sta_id flags and preserve our mac address*/
  997. ath5k_hw_reg_write(ah,
  998. get_unaligned_le32(common->macaddr),
  999. AR5K_STA_ID0);
  1000. ath5k_hw_reg_write(ah,
  1001. staid1_flags | get_unaligned_le16(common->macaddr + 4),
  1002. AR5K_STA_ID1);
  1003. /*
  1004. * Configure PCU
  1005. */
  1006. /* Restore bssid and bssid mask */
  1007. ath5k_hw_set_associd(ah);
  1008. /* Set PCU config */
  1009. ath5k_hw_set_opmode(ah);
  1010. /* Clear any pending interrupts
  1011. * PISR/SISR Not available on 5210 */
  1012. if (ah->ah_version != AR5K_AR5210)
  1013. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  1014. /* Set RSSI/BRSSI thresholds
  1015. *
  1016. * Note: If we decide to set this value
  1017. * dynamicaly, have in mind that when AR5K_RSSI_THR
  1018. * register is read it might return 0x40 if we haven't
  1019. * wrote anything to it plus BMISS RSSI threshold is zeroed.
  1020. * So doing a save/restore procedure here isn't the right
  1021. * choice. Instead store it on ath5k_hw */
  1022. ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
  1023. AR5K_TUNE_BMISS_THRES <<
  1024. AR5K_RSSI_THR_BMISS_S),
  1025. AR5K_RSSI_THR);
  1026. /* MIC QoS support */
  1027. if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
  1028. ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
  1029. ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
  1030. }
  1031. /* QoS NOACK Policy */
  1032. if (ah->ah_version == AR5K_AR5212) {
  1033. ath5k_hw_reg_write(ah,
  1034. AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
  1035. AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
  1036. AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
  1037. AR5K_QOS_NOACK);
  1038. }
  1039. /*
  1040. * Configure PHY
  1041. */
  1042. /* Set channel on PHY */
  1043. ret = ath5k_hw_channel(ah, channel);
  1044. if (ret)
  1045. return ret;
  1046. /*
  1047. * Enable the PHY and wait until completion
  1048. * This includes BaseBand and Synthesizer
  1049. * activation.
  1050. */
  1051. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1052. /*
  1053. * On 5211+ read activation -> rx delay
  1054. * and use it.
  1055. *
  1056. * TODO: Half/quarter rate support
  1057. */
  1058. if (ah->ah_version != AR5K_AR5210) {
  1059. u32 delay;
  1060. delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  1061. AR5K_PHY_RX_DELAY_M;
  1062. delay = (channel->hw_value & CHANNEL_CCK) ?
  1063. ((delay << 2) / 22) : (delay / 10);
  1064. udelay(100 + (2 * delay));
  1065. } else {
  1066. mdelay(1);
  1067. }
  1068. /*
  1069. * Perform ADC test to see if baseband is ready
  1070. * Set tx hold and check adc test register
  1071. */
  1072. phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  1073. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  1074. for (i = 0; i <= 20; i++) {
  1075. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  1076. break;
  1077. udelay(200);
  1078. }
  1079. ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
  1080. /*
  1081. * Start automatic gain control calibration
  1082. *
  1083. * During AGC calibration RX path is re-routed to
  1084. * a power detector so we don't receive anything.
  1085. *
  1086. * This method is used to calibrate some static offsets
  1087. * used together with on-the fly I/Q calibration (the
  1088. * one performed via ath5k_hw_phy_calibrate), that doesn't
  1089. * interrupt rx path.
  1090. *
  1091. * While rx path is re-routed to the power detector we also
  1092. * start a noise floor calibration, to measure the
  1093. * card's noise floor (the noise we measure when we are not
  1094. * transmiting or receiving anything).
  1095. *
  1096. * If we are in a noisy environment AGC calibration may time
  1097. * out and/or noise floor calibration might timeout.
  1098. */
  1099. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1100. AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF);
  1101. /* At the same time start I/Q calibration for QAM constellation
  1102. * -no need for CCK- */
  1103. ah->ah_calibration = false;
  1104. if (!(mode == AR5K_MODE_11B)) {
  1105. ah->ah_calibration = true;
  1106. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1107. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1108. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1109. AR5K_PHY_IQ_RUN);
  1110. }
  1111. /* Wait for gain calibration to finish (we check for I/Q calibration
  1112. * during ath5k_phy_calibrate) */
  1113. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1114. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  1115. ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
  1116. channel->center_freq);
  1117. }
  1118. /* Restore antenna mode */
  1119. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  1120. /*
  1121. * Configure QCUs/DCUs
  1122. */
  1123. /* TODO: HW Compression support for data queues */
  1124. /* TODO: Burst prefetch for data queues */
  1125. /*
  1126. * Reset queues and start beacon timers at the end of the reset routine
  1127. * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
  1128. * Note: If we want we can assign multiple qcus on one dcu.
  1129. */
  1130. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  1131. ret = ath5k_hw_reset_tx_queue(ah, i);
  1132. if (ret) {
  1133. ATH5K_ERR(ah->ah_sc,
  1134. "failed to reset TX queue #%d\n", i);
  1135. return ret;
  1136. }
  1137. }
  1138. /*
  1139. * Configure DMA/Interrupts
  1140. */
  1141. /*
  1142. * Set Rx/Tx DMA Configuration
  1143. *
  1144. * Set standard DMA size (128). Note that
  1145. * a DMA size of 512 causes rx overruns and tx errors
  1146. * on pci-e cards (tested on 5424 but since rx overruns
  1147. * also occur on 5416/5418 with madwifi we set 128
  1148. * for all PCI-E cards to be safe).
  1149. *
  1150. * XXX: need to check 5210 for this
  1151. * TODO: Check out tx triger level, it's always 64 on dumps but I
  1152. * guess we can tweak it and see how it goes ;-)
  1153. */
  1154. if (ah->ah_version != AR5K_AR5210) {
  1155. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  1156. AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
  1157. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  1158. AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
  1159. }
  1160. /* Pre-enable interrupts on 5211/5212*/
  1161. if (ah->ah_version != AR5K_AR5210)
  1162. ath5k_hw_set_imr(ah, ah->ah_imr);
  1163. /* Enable 32KHz clock function for AR5212+ chips
  1164. * Set clocks to 32KHz operation and use an
  1165. * external 32KHz crystal when sleeping if one
  1166. * exists */
  1167. if (ah->ah_version == AR5K_AR5212)
  1168. ath5k_hw_set_sleep_clock(ah, true);
  1169. /*
  1170. * Disable beacons and reset the register
  1171. */
  1172. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
  1173. AR5K_BEACON_RESET_TSF);
  1174. return 0;
  1175. }
  1176. #undef _ATH5K_RESET