reset.c 38 KB

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