reset.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 <linux/pci.h> /* To determine if a card is pci-e */
  26. #include <linux/bitops.h> /* For get_bitmask_order */
  27. #include "ath5k.h"
  28. #include "reg.h"
  29. #include "base.h"
  30. #include "debug.h"
  31. /**
  32. * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
  33. *
  34. * @ah: the &struct ath5k_hw
  35. * @channel: the currently set channel upon reset
  36. *
  37. * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
  38. * operation on the AR5212 upon reset. This is a helper for ath5k_hw_reset().
  39. *
  40. * Since delta slope is floating point we split it on its exponent and
  41. * mantissa and provide these values on hw.
  42. *
  43. * For more infos i think this patent is related
  44. * http://www.freepatentsonline.com/7184495.html
  45. */
  46. static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  47. struct ieee80211_channel *channel)
  48. {
  49. /* Get exponent and mantissa and set it */
  50. u32 coef_scaled, coef_exp, coef_man,
  51. ds_coef_exp, ds_coef_man, clock;
  52. if (!(ah->ah_version == AR5K_AR5212) ||
  53. !(channel->hw_value & CHANNEL_OFDM))
  54. BUG();
  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 = get_bitmask_order(coef_scaled);
  65. /* Doesn't make sense if it's zero*/
  66. if (!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. /* Preserve sleep duration */
  219. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  220. if (data & 0xffc00000)
  221. data = 0;
  222. else
  223. data = data & 0xfffcffff;
  224. ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
  225. udelay(15);
  226. for (i = 50; i > 0; i--) {
  227. /* Check if the chip did wake up */
  228. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  229. AR5K_PCICFG_SPWR_DN) == 0)
  230. break;
  231. /* Wait a bit and retry */
  232. udelay(200);
  233. ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
  234. }
  235. /* Fail if the chip didn't wake up */
  236. if (i <= 0)
  237. return -EIO;
  238. break;
  239. default:
  240. return -EINVAL;
  241. }
  242. commit:
  243. ah->ah_power_mode = mode;
  244. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  245. return 0;
  246. }
  247. /*
  248. * Bring up MAC + PHY Chips and program PLL
  249. * TODO: Half/Quarter rate support
  250. */
  251. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  252. {
  253. struct pci_dev *pdev = ah->ah_sc->pdev;
  254. u32 turbo, mode, clock, bus_flags;
  255. int ret;
  256. turbo = 0;
  257. mode = 0;
  258. clock = 0;
  259. ATH5K_TRACE(ah->ah_sc);
  260. /* Wakeup the device */
  261. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  262. if (ret) {
  263. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  264. return ret;
  265. }
  266. if (ah->ah_version != AR5K_AR5210) {
  267. /*
  268. * Get channel mode flags
  269. */
  270. if (ah->ah_radio >= AR5K_RF5112) {
  271. mode = AR5K_PHY_MODE_RAD_RF5112;
  272. clock = AR5K_PHY_PLL_RF5112;
  273. } else {
  274. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  275. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  276. }
  277. if (flags & CHANNEL_2GHZ) {
  278. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  279. clock |= AR5K_PHY_PLL_44MHZ;
  280. if (flags & CHANNEL_CCK) {
  281. mode |= AR5K_PHY_MODE_MOD_CCK;
  282. } else if (flags & CHANNEL_OFDM) {
  283. /* XXX Dynamic OFDM/CCK is not supported by the
  284. * AR5211 so we set MOD_OFDM for plain g (no
  285. * CCK headers) operation. We need to test
  286. * this, 5211 might support ofdm-only g after
  287. * all, there are also initial register values
  288. * in the code for g mode (see initvals.c). */
  289. if (ah->ah_version == AR5K_AR5211)
  290. mode |= AR5K_PHY_MODE_MOD_OFDM;
  291. else
  292. mode |= AR5K_PHY_MODE_MOD_DYN;
  293. } else {
  294. ATH5K_ERR(ah->ah_sc,
  295. "invalid radio modulation mode\n");
  296. return -EINVAL;
  297. }
  298. } else if (flags & CHANNEL_5GHZ) {
  299. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  300. if (ah->ah_radio == AR5K_RF5413)
  301. clock |= AR5K_PHY_PLL_40MHZ_5413;
  302. else
  303. clock |= AR5K_PHY_PLL_40MHZ;
  304. if (flags & CHANNEL_OFDM)
  305. mode |= AR5K_PHY_MODE_MOD_OFDM;
  306. else {
  307. ATH5K_ERR(ah->ah_sc,
  308. "invalid radio modulation mode\n");
  309. return -EINVAL;
  310. }
  311. } else {
  312. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  313. return -EINVAL;
  314. }
  315. if (flags & CHANNEL_TURBO)
  316. turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
  317. } else { /* Reset the device */
  318. /* ...enable Atheros turbo mode if requested */
  319. if (flags & CHANNEL_TURBO)
  320. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  321. AR5K_PHY_TURBO);
  322. }
  323. /* reseting PCI on PCI-E cards results card to hang
  324. * and always return 0xffff... so we ingore that flag
  325. * for PCI-E cards */
  326. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  327. /* Reset chipset */
  328. if (ah->ah_version == AR5K_AR5210) {
  329. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  330. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  331. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  332. mdelay(2);
  333. } else {
  334. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  335. AR5K_RESET_CTL_BASEBAND | bus_flags);
  336. }
  337. if (ret) {
  338. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
  339. return -EIO;
  340. }
  341. /* ...wakeup again!*/
  342. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  343. if (ret) {
  344. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  345. return ret;
  346. }
  347. /* ...final warm reset */
  348. if (ath5k_hw_nic_reset(ah, 0)) {
  349. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  350. return -EIO;
  351. }
  352. if (ah->ah_version != AR5K_AR5210) {
  353. /* ...update PLL if needed */
  354. if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
  355. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  356. udelay(300);
  357. }
  358. /* ...set the PHY operating mode */
  359. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  360. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  361. }
  362. return 0;
  363. }
  364. /*
  365. * If there is an external 32KHz crystal available, use it
  366. * as ref. clock instead of 32/40MHz clock and baseband clocks
  367. * to save power during sleep or restore normal 32/40MHz
  368. * operation.
  369. *
  370. * XXX: When operating on 32KHz certain PHY registers (27 - 31,
  371. * 123 - 127) require delay on access.
  372. */
  373. static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
  374. {
  375. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  376. u32 scal, spending, usec32;
  377. /* Only set 32KHz settings if we have an external
  378. * 32KHz crystal present */
  379. if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
  380. AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
  381. enable) {
  382. /* 1 usec/cycle */
  383. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
  384. /* Set up tsf increment on each cycle */
  385. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
  386. /* Set baseband sleep control registers
  387. * and sleep control rate */
  388. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  389. if ((ah->ah_radio == AR5K_RF5112) ||
  390. (ah->ah_radio == AR5K_RF5413) ||
  391. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  392. spending = 0x14;
  393. else
  394. spending = 0x18;
  395. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  396. if ((ah->ah_radio == AR5K_RF5112) ||
  397. (ah->ah_radio == AR5K_RF5413) ||
  398. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  399. ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
  400. ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
  401. ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
  402. ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
  403. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  404. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
  405. } else {
  406. ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
  407. ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
  408. ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
  409. ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
  410. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  411. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
  412. }
  413. /* Enable sleep clock operation */
  414. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
  415. AR5K_PCICFG_SLEEP_CLOCK_EN);
  416. } else {
  417. /* Disable sleep clock operation and
  418. * restore default parameters */
  419. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  420. AR5K_PCICFG_SLEEP_CLOCK_EN);
  421. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  422. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
  423. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  424. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  425. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  426. scal = AR5K_PHY_SCAL_32MHZ_2417;
  427. else if (ath5k_eeprom_is_hb63(ah))
  428. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  429. else
  430. scal = AR5K_PHY_SCAL_32MHZ;
  431. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  432. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  433. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  434. if ((ah->ah_radio == AR5K_RF5112) ||
  435. (ah->ah_radio == AR5K_RF5413) ||
  436. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  437. spending = 0x14;
  438. else
  439. spending = 0x18;
  440. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  441. if ((ah->ah_radio == AR5K_RF5112) ||
  442. (ah->ah_radio == AR5K_RF5413))
  443. usec32 = 39;
  444. else
  445. usec32 = 31;
  446. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, usec32);
  447. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
  448. }
  449. return;
  450. }
  451. static bool ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
  452. struct ieee80211_channel *channel)
  453. {
  454. u8 refclk_freq;
  455. if ((ah->ah_radio == AR5K_RF5112) ||
  456. (ah->ah_radio == AR5K_RF5413) ||
  457. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  458. refclk_freq = 40;
  459. else
  460. refclk_freq = 32;
  461. if ((channel->center_freq % refclk_freq != 0) &&
  462. ((channel->center_freq % refclk_freq < 10) ||
  463. (channel->center_freq % refclk_freq > 22)))
  464. return true;
  465. else
  466. return false;
  467. }
  468. /* TODO: Half/Quarter rate */
  469. static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
  470. struct ieee80211_channel *channel)
  471. {
  472. if (ah->ah_version == AR5K_AR5212 &&
  473. ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  474. /* Setup ADC control */
  475. ath5k_hw_reg_write(ah,
  476. (AR5K_REG_SM(2,
  477. AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
  478. AR5K_REG_SM(2,
  479. AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
  480. AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
  481. AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
  482. AR5K_PHY_ADC_CTL);
  483. /* Disable barker RSSI threshold */
  484. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  485. AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
  486. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  487. AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
  488. /* Set the mute mask */
  489. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  490. }
  491. /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
  492. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
  493. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
  494. /* Enable DCU double buffering */
  495. if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
  496. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  497. AR5K_TXCFG_DCU_DBL_BUF_DIS);
  498. /* Set DAC/ADC delays */
  499. if (ah->ah_version == AR5K_AR5212) {
  500. u32 scal;
  501. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  502. scal = AR5K_PHY_SCAL_32MHZ_2417;
  503. else if (ath5k_eeprom_is_hb63(ah))
  504. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  505. else
  506. scal = AR5K_PHY_SCAL_32MHZ;
  507. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  508. }
  509. /* Set fast ADC */
  510. if ((ah->ah_radio == AR5K_RF5413) ||
  511. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  512. u32 fast_adc = true;
  513. if (channel->center_freq == 2462 ||
  514. channel->center_freq == 2467)
  515. fast_adc = 0;
  516. /* Only update if needed */
  517. if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
  518. ath5k_hw_reg_write(ah, fast_adc,
  519. AR5K_PHY_FAST_ADC);
  520. }
  521. /* Fix for first revision of the RF5112 RF chipset */
  522. if (ah->ah_radio == AR5K_RF5112 &&
  523. ah->ah_radio_5ghz_revision <
  524. AR5K_SREV_RAD_5112A) {
  525. u32 data;
  526. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  527. AR5K_PHY_CCKTXCTL);
  528. if (channel->hw_value & CHANNEL_5GHZ)
  529. data = 0xffb81020;
  530. else
  531. data = 0xffb80d20;
  532. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  533. }
  534. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  535. u32 usec_reg;
  536. /* 5311 has different tx/rx latency masks
  537. * from 5211, since we deal 5311 the same
  538. * as 5211 when setting initvals, shift
  539. * values here to their proper locations */
  540. usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
  541. ath5k_hw_reg_write(ah, usec_reg & (AR5K_USEC_1 |
  542. AR5K_USEC_32 |
  543. AR5K_USEC_TX_LATENCY_5211 |
  544. AR5K_REG_SM(29,
  545. AR5K_USEC_RX_LATENCY_5210)),
  546. AR5K_USEC_5211);
  547. /* Clear QCU/DCU clock gating register */
  548. ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
  549. /* Set DAC/ADC delays */
  550. ath5k_hw_reg_write(ah, 0x08, AR5K_PHY_SCAL);
  551. /* Enable PCU FIFO corruption ECO */
  552. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  553. AR5K_DIAG_SW_ECO_ENABLE);
  554. }
  555. }
  556. static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
  557. struct ieee80211_channel *channel, u8 *ant, u8 ee_mode)
  558. {
  559. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  560. s16 cck_ofdm_pwr_delta;
  561. /* Adjust power delta for channel 14 */
  562. if (channel->center_freq == 2484)
  563. cck_ofdm_pwr_delta =
  564. ((ee->ee_cck_ofdm_power_delta -
  565. ee->ee_scaled_cck_delta) * 2) / 10;
  566. else
  567. cck_ofdm_pwr_delta =
  568. (ee->ee_cck_ofdm_power_delta * 2) / 10;
  569. /* Set CCK to OFDM power delta on tx power
  570. * adjustment register */
  571. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  572. if (channel->hw_value == CHANNEL_G)
  573. ath5k_hw_reg_write(ah,
  574. AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
  575. AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
  576. AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
  577. AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
  578. AR5K_PHY_TX_PWR_ADJ);
  579. else
  580. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
  581. } else {
  582. /* For older revs we scale power on sw during tx power
  583. * setup */
  584. ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
  585. ah->ah_txpower.txp_cck_ofdm_gainf_delta =
  586. ee->ee_cck_ofdm_gain_delta;
  587. }
  588. /* Set antenna idle switch table */
  589. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
  590. AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
  591. (ah->ah_antenna[ee_mode][0] |
  592. AR5K_PHY_ANT_CTL_TXRX_EN));
  593. /* Set antenna switch table */
  594. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
  595. AR5K_PHY_ANT_SWITCH_TABLE_0);
  596. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
  597. AR5K_PHY_ANT_SWITCH_TABLE_1);
  598. /* Noise floor threshold */
  599. ath5k_hw_reg_write(ah,
  600. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  601. AR5K_PHY_NFTHRES);
  602. if ((channel->hw_value & CHANNEL_TURBO) &&
  603. (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
  604. /* Switch settling time (Turbo) */
  605. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  606. AR5K_PHY_SETTLING_SWITCH,
  607. ee->ee_switch_settling_turbo[ee_mode]);
  608. /* Tx/Rx attenuation (Turbo) */
  609. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  610. AR5K_PHY_GAIN_TXRX_ATTEN,
  611. ee->ee_atn_tx_rx_turbo[ee_mode]);
  612. /* ADC/PGA desired size (Turbo) */
  613. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  614. AR5K_PHY_DESIRED_SIZE_ADC,
  615. ee->ee_adc_desired_size_turbo[ee_mode]);
  616. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  617. AR5K_PHY_DESIRED_SIZE_PGA,
  618. ee->ee_pga_desired_size_turbo[ee_mode]);
  619. /* Tx/Rx margin (Turbo) */
  620. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  621. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  622. ee->ee_margin_tx_rx_turbo[ee_mode]);
  623. } else {
  624. /* Switch settling time */
  625. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  626. AR5K_PHY_SETTLING_SWITCH,
  627. ee->ee_switch_settling[ee_mode]);
  628. /* Tx/Rx attenuation */
  629. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  630. AR5K_PHY_GAIN_TXRX_ATTEN,
  631. ee->ee_atn_tx_rx[ee_mode]);
  632. /* ADC/PGA desired size */
  633. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  634. AR5K_PHY_DESIRED_SIZE_ADC,
  635. ee->ee_adc_desired_size[ee_mode]);
  636. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  637. AR5K_PHY_DESIRED_SIZE_PGA,
  638. ee->ee_pga_desired_size[ee_mode]);
  639. /* Tx/Rx margin */
  640. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  641. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  642. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  643. ee->ee_margin_tx_rx[ee_mode]);
  644. }
  645. /* XPA delays */
  646. ath5k_hw_reg_write(ah,
  647. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  648. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  649. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  650. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  651. /* XLNA delay */
  652. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
  653. AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
  654. ee->ee_tx_end2xlna_enable[ee_mode]);
  655. /* Thresh64 (ANI) */
  656. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
  657. AR5K_PHY_NF_THRESH62,
  658. ee->ee_thr_62[ee_mode]);
  659. /* False detect backoff for channels
  660. * that have spur noise. Write the new
  661. * cyclic power RSSI threshold. */
  662. if (ath5k_hw_chan_has_spur_noise(ah, channel))
  663. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  664. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  665. AR5K_INIT_CYCRSSI_THR1 +
  666. ee->ee_false_detect[ee_mode]);
  667. else
  668. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  669. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  670. AR5K_INIT_CYCRSSI_THR1);
  671. /* I/Q correction
  672. * TODO: Per channel i/q infos ? */
  673. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  674. AR5K_PHY_IQ_CORR_ENABLE |
  675. (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
  676. ee->ee_q_cal[ee_mode]);
  677. /* Heavy clipping -disable for now */
  678. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  679. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  680. return;
  681. }
  682. /*
  683. * Main reset function
  684. */
  685. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  686. struct ieee80211_channel *channel, bool change_channel)
  687. {
  688. u32 s_seq[10], s_ant, s_led[3], staid1_flags, tsf_up, tsf_lo;
  689. u32 phy_tst1;
  690. u8 mode, freq, ee_mode, ant[2];
  691. int i, ret;
  692. ATH5K_TRACE(ah->ah_sc);
  693. s_ant = 0;
  694. ee_mode = 0;
  695. staid1_flags = 0;
  696. tsf_up = 0;
  697. tsf_lo = 0;
  698. freq = 0;
  699. mode = 0;
  700. /*
  701. * Save some registers before a reset
  702. */
  703. /*DCU/Antenna selection not available on 5210*/
  704. if (ah->ah_version != AR5K_AR5210) {
  705. switch (channel->hw_value & CHANNEL_MODES) {
  706. case CHANNEL_A:
  707. mode = AR5K_MODE_11A;
  708. freq = AR5K_INI_RFGAIN_5GHZ;
  709. ee_mode = AR5K_EEPROM_MODE_11A;
  710. break;
  711. case CHANNEL_G:
  712. mode = AR5K_MODE_11G;
  713. freq = AR5K_INI_RFGAIN_2GHZ;
  714. ee_mode = AR5K_EEPROM_MODE_11G;
  715. break;
  716. case CHANNEL_B:
  717. mode = AR5K_MODE_11B;
  718. freq = AR5K_INI_RFGAIN_2GHZ;
  719. ee_mode = AR5K_EEPROM_MODE_11B;
  720. break;
  721. case CHANNEL_T:
  722. mode = AR5K_MODE_11A_TURBO;
  723. freq = AR5K_INI_RFGAIN_5GHZ;
  724. ee_mode = AR5K_EEPROM_MODE_11A;
  725. break;
  726. case CHANNEL_TG:
  727. if (ah->ah_version == AR5K_AR5211) {
  728. ATH5K_ERR(ah->ah_sc,
  729. "TurboG mode not available on 5211");
  730. return -EINVAL;
  731. }
  732. mode = AR5K_MODE_11G_TURBO;
  733. freq = AR5K_INI_RFGAIN_2GHZ;
  734. ee_mode = AR5K_EEPROM_MODE_11G;
  735. break;
  736. case CHANNEL_XR:
  737. if (ah->ah_version == AR5K_AR5211) {
  738. ATH5K_ERR(ah->ah_sc,
  739. "XR mode not available on 5211");
  740. return -EINVAL;
  741. }
  742. mode = AR5K_MODE_XR;
  743. freq = AR5K_INI_RFGAIN_5GHZ;
  744. ee_mode = AR5K_EEPROM_MODE_11A;
  745. break;
  746. default:
  747. ATH5K_ERR(ah->ah_sc,
  748. "invalid channel: %d\n", channel->center_freq);
  749. return -EINVAL;
  750. }
  751. if (change_channel) {
  752. /*
  753. * Save frame sequence count
  754. * For revs. after Oahu, only save
  755. * seq num for DCU 0 (Global seq num)
  756. */
  757. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  758. for (i = 0; i < 10; i++)
  759. s_seq[i] = ath5k_hw_reg_read(ah,
  760. AR5K_QUEUE_DCU_SEQNUM(i));
  761. } else {
  762. s_seq[0] = ath5k_hw_reg_read(ah,
  763. AR5K_QUEUE_DCU_SEQNUM(0));
  764. }
  765. /* TSF accelerates on AR5211 durring reset
  766. * As a workaround save it here and restore
  767. * it later so that it's back in time after
  768. * reset. This way it'll get re-synced on the
  769. * next beacon without breaking ad-hoc.
  770. *
  771. * On AR5212 TSF is almost preserved across a
  772. * reset so it stays back in time anyway and
  773. * we don't have to save/restore it.
  774. *
  775. * XXX: Since this breaks power saving we have
  776. * to disable power saving until we receive the
  777. * next beacon, so we can resync beacon timers */
  778. if (ah->ah_version == AR5K_AR5211) {
  779. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  780. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  781. }
  782. }
  783. /* Save default antenna */
  784. s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  785. if (ah->ah_version == AR5K_AR5212) {
  786. /* Restore normal 32/40MHz clock operation
  787. * to avoid register access delay on certain
  788. * PHY registers */
  789. ath5k_hw_set_sleep_clock(ah, false);
  790. /* Since we are going to write rf buffer
  791. * check if we have any pending gain_F
  792. * optimization settings */
  793. if (change_channel && ah->ah_rf_banks != NULL)
  794. ath5k_hw_gainf_calibrate(ah);
  795. }
  796. }
  797. /*GPIOs*/
  798. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  799. AR5K_PCICFG_LEDSTATE;
  800. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  801. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  802. /* AR5K_STA_ID1 flags, only preserve antenna
  803. * settings and ack/cts rate mode */
  804. staid1_flags = ath5k_hw_reg_read(ah, AR5K_STA_ID1) &
  805. (AR5K_STA_ID1_DEFAULT_ANTENNA |
  806. AR5K_STA_ID1_DESC_ANTENNA |
  807. AR5K_STA_ID1_RTS_DEF_ANTENNA |
  808. AR5K_STA_ID1_ACKCTS_6MB |
  809. AR5K_STA_ID1_BASE_RATE_11B |
  810. AR5K_STA_ID1_SELFGEN_DEF_ANT);
  811. /* Wakeup the device */
  812. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  813. if (ret)
  814. return ret;
  815. /*
  816. * Initialize operating mode
  817. */
  818. ah->ah_op_mode = op_mode;
  819. /* PHY access enable */
  820. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  821. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  822. else
  823. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  824. AR5K_PHY(0));
  825. /* Write initial settings */
  826. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  827. if (ret)
  828. return ret;
  829. /*
  830. * 5211/5212 Specific
  831. */
  832. if (ah->ah_version != AR5K_AR5210) {
  833. /*
  834. * Write initial RF gain settings
  835. * This should work for both 5111/5112
  836. */
  837. ret = ath5k_hw_rfgain_init(ah, freq);
  838. if (ret)
  839. return ret;
  840. mdelay(1);
  841. /*
  842. * Tweak initval settings for revised
  843. * chipsets and add some more config
  844. * bits
  845. */
  846. ath5k_hw_tweak_initval_settings(ah, channel);
  847. /*
  848. * Set TX power (FIXME)
  849. */
  850. ret = ath5k_hw_txpower(ah, channel, ee_mode,
  851. AR5K_TUNE_DEFAULT_TXPOWER);
  852. if (ret)
  853. return ret;
  854. /* Write rate duration table only on AR5212 and if
  855. * virtual interface has already been brought up
  856. * XXX: rethink this after new mode changes to
  857. * mac80211 are integrated */
  858. if (ah->ah_version == AR5K_AR5212 &&
  859. ah->ah_sc->vif != NULL)
  860. ath5k_hw_write_rate_duration(ah, mode);
  861. /*
  862. * Write RF buffer
  863. */
  864. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  865. if (ret)
  866. return ret;
  867. /* Write OFDM timings on 5212*/
  868. if (ah->ah_version == AR5K_AR5212 &&
  869. channel->hw_value & CHANNEL_OFDM) {
  870. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  871. if (ret)
  872. return ret;
  873. }
  874. /*Enable/disable 802.11b mode on 5111
  875. (enable 2111 frequency converter + CCK)*/
  876. if (ah->ah_radio == AR5K_RF5111) {
  877. if (mode == AR5K_MODE_11B)
  878. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  879. AR5K_TXCFG_B_MODE);
  880. else
  881. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  882. AR5K_TXCFG_B_MODE);
  883. }
  884. /*
  885. * In case a fixed antenna was set as default
  886. * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
  887. * registers.
  888. */
  889. if (s_ant != 0) {
  890. if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
  891. ant[0] = ant[1] = AR5K_ANT_FIXED_A;
  892. else /* 2 - Aux */
  893. ant[0] = ant[1] = AR5K_ANT_FIXED_B;
  894. } else {
  895. ant[0] = AR5K_ANT_FIXED_A;
  896. ant[1] = AR5K_ANT_FIXED_B;
  897. }
  898. /* Commit values from EEPROM */
  899. ath5k_hw_commit_eeprom_settings(ah, channel, ant, ee_mode);
  900. } else {
  901. /*
  902. * For 5210 we do all initialization using
  903. * initvals, so we don't have to modify
  904. * any settings (5210 also only supports
  905. * a/aturbo modes)
  906. */
  907. mdelay(1);
  908. /* Disable phy and wait */
  909. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  910. mdelay(1);
  911. }
  912. /*
  913. * Restore saved values
  914. */
  915. /*DCU/Antenna selection not available on 5210*/
  916. if (ah->ah_version != AR5K_AR5210) {
  917. if (change_channel) {
  918. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  919. for (i = 0; i < 10; i++)
  920. ath5k_hw_reg_write(ah, s_seq[i],
  921. AR5K_QUEUE_DCU_SEQNUM(i));
  922. } else {
  923. ath5k_hw_reg_write(ah, s_seq[0],
  924. AR5K_QUEUE_DCU_SEQNUM(0));
  925. }
  926. if (ah->ah_version == AR5K_AR5211) {
  927. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  928. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  929. }
  930. }
  931. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  932. }
  933. /* Ledstate */
  934. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  935. /* Gpio settings */
  936. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  937. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  938. /* Restore sta_id flags and preserve our mac address*/
  939. ath5k_hw_reg_write(ah, AR5K_LOW_ID(ah->ah_sta_id),
  940. AR5K_STA_ID0);
  941. ath5k_hw_reg_write(ah, staid1_flags | AR5K_HIGH_ID(ah->ah_sta_id),
  942. AR5K_STA_ID1);
  943. /*
  944. * Configure PCU
  945. */
  946. /* Restore bssid and bssid mask */
  947. /* XXX: add ah->aid once mac80211 gives this to us */
  948. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  949. /* Set PCU config */
  950. ath5k_hw_set_opmode(ah);
  951. /* Clear any pending interrupts
  952. * PISR/SISR Not available on 5210 */
  953. if (ah->ah_version != AR5K_AR5210)
  954. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  955. /* Set RSSI/BRSSI thresholds
  956. *
  957. * Note: If we decide to set this value
  958. * dynamicaly, have in mind that when AR5K_RSSI_THR
  959. * register is read it might return 0x40 if we haven't
  960. * wrote anything to it plus BMISS RSSI threshold is zeroed.
  961. * So doing a save/restore procedure here isn't the right
  962. * choice. Instead store it on ath5k_hw */
  963. ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
  964. AR5K_TUNE_BMISS_THRES <<
  965. AR5K_RSSI_THR_BMISS_S),
  966. AR5K_RSSI_THR);
  967. /* MIC QoS support */
  968. if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
  969. ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
  970. ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
  971. }
  972. /* QoS NOACK Policy */
  973. if (ah->ah_version == AR5K_AR5212) {
  974. ath5k_hw_reg_write(ah,
  975. AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
  976. AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
  977. AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
  978. AR5K_QOS_NOACK);
  979. }
  980. /*
  981. * Configure PHY
  982. */
  983. /* Set channel on PHY */
  984. ret = ath5k_hw_channel(ah, channel);
  985. if (ret)
  986. return ret;
  987. /*
  988. * Enable the PHY and wait until completion
  989. * This includes BaseBand and Synthesizer
  990. * activation.
  991. */
  992. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  993. /*
  994. * On 5211+ read activation -> rx delay
  995. * and use it.
  996. *
  997. * TODO: Half/quarter rate support
  998. */
  999. if (ah->ah_version != AR5K_AR5210) {
  1000. u32 delay;
  1001. delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  1002. AR5K_PHY_RX_DELAY_M;
  1003. delay = (channel->hw_value & CHANNEL_CCK) ?
  1004. ((delay << 2) / 22) : (delay / 10);
  1005. udelay(100 + (2 * delay));
  1006. } else {
  1007. mdelay(1);
  1008. }
  1009. /*
  1010. * Perform ADC test to see if baseband is ready
  1011. * Set tx hold and check adc test register
  1012. */
  1013. phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  1014. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  1015. for (i = 0; i <= 20; i++) {
  1016. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  1017. break;
  1018. udelay(200);
  1019. }
  1020. ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
  1021. /*
  1022. * Start automatic gain control calibration
  1023. *
  1024. * During AGC calibration RX path is re-routed to
  1025. * a power detector so we don't receive anything.
  1026. *
  1027. * This method is used to calibrate some static offsets
  1028. * used together with on-the fly I/Q calibration (the
  1029. * one performed via ath5k_hw_phy_calibrate), that doesn't
  1030. * interrupt rx path.
  1031. *
  1032. * While rx path is re-routed to the power detector we also
  1033. * start a noise floor calibration, to measure the
  1034. * card's noise floor (the noise we measure when we are not
  1035. * transmiting or receiving anything).
  1036. *
  1037. * If we are in a noisy environment AGC calibration may time
  1038. * out and/or noise floor calibration might timeout.
  1039. */
  1040. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1041. AR5K_PHY_AGCCTL_CAL);
  1042. /* At the same time start I/Q calibration for QAM constellation
  1043. * -no need for CCK- */
  1044. ah->ah_calibration = false;
  1045. if (!(mode == AR5K_MODE_11B)) {
  1046. ah->ah_calibration = true;
  1047. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1048. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1049. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1050. AR5K_PHY_IQ_RUN);
  1051. }
  1052. /* Wait for gain calibration to finish (we check for I/Q calibration
  1053. * during ath5k_phy_calibrate) */
  1054. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1055. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  1056. ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
  1057. channel->center_freq);
  1058. }
  1059. /*
  1060. * If we run NF calibration before AGC, it always times out.
  1061. * Binary HAL starts NF and AGC calibration at the same time
  1062. * and only waits for AGC to finish. Also if AGC or NF cal.
  1063. * times out, reset doesn't fail on binary HAL. I believe
  1064. * that's wrong because since rx path is routed to a detector,
  1065. * if cal. doesn't finish we won't have RX. Sam's HAL for AR5210/5211
  1066. * enables noise floor calibration after offset calibration and if noise
  1067. * floor calibration fails, reset fails. I believe that's
  1068. * a better approach, we just need to find a polling interval
  1069. * that suits best, even if reset continues we need to make
  1070. * sure that rx path is ready.
  1071. */
  1072. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1073. /*
  1074. * Configure QCUs/DCUs
  1075. */
  1076. /* TODO: HW Compression support for data queues */
  1077. /* TODO: Burst prefetch for data queues */
  1078. /*
  1079. * Reset queues and start beacon timers at the end of the reset routine
  1080. * This also sets QCU mask on each DCU for 1:1 qcu to dcu mapping
  1081. * Note: If we want we can assign multiple qcus on one dcu.
  1082. */
  1083. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  1084. ret = ath5k_hw_reset_tx_queue(ah, i);
  1085. if (ret) {
  1086. ATH5K_ERR(ah->ah_sc,
  1087. "failed to reset TX queue #%d\n", i);
  1088. return ret;
  1089. }
  1090. }
  1091. /*
  1092. * Configure DMA/Interrupts
  1093. */
  1094. /*
  1095. * Set Rx/Tx DMA Configuration
  1096. *
  1097. * Set standard DMA size (128). Note that
  1098. * a DMA size of 512 causes rx overruns and tx errors
  1099. * on pci-e cards (tested on 5424 but since rx overruns
  1100. * also occur on 5416/5418 with madwifi we set 128
  1101. * for all PCI-E cards to be safe).
  1102. *
  1103. * XXX: need to check 5210 for this
  1104. * TODO: Check out tx triger level, it's always 64 on dumps but I
  1105. * guess we can tweak it and see how it goes ;-)
  1106. */
  1107. if (ah->ah_version != AR5K_AR5210) {
  1108. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  1109. AR5K_TXCFG_SDMAMR, AR5K_DMASIZE_128B);
  1110. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  1111. AR5K_RXCFG_SDMAMW, AR5K_DMASIZE_128B);
  1112. }
  1113. /* Pre-enable interrupts on 5211/5212*/
  1114. if (ah->ah_version != AR5K_AR5210)
  1115. ath5k_hw_set_imr(ah, ah->ah_imr);
  1116. /*
  1117. * Setup RFKill interrupt if rfkill flag is set on eeprom.
  1118. * TODO: Use gpio pin and polarity infos from eeprom
  1119. * TODO: Handle this in ath5k_intr because it'll result
  1120. * a nasty interrupt storm.
  1121. */
  1122. #if 0
  1123. if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
  1124. ath5k_hw_set_gpio_input(ah, 0);
  1125. ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
  1126. if (ah->ah_gpio[0] == 0)
  1127. ath5k_hw_set_gpio_intr(ah, 0, 1);
  1128. else
  1129. ath5k_hw_set_gpio_intr(ah, 0, 0);
  1130. }
  1131. #endif
  1132. /* Enable 32KHz clock function for AR5212+ chips
  1133. * Set clocks to 32KHz operation and use an
  1134. * external 32KHz crystal when sleeping if one
  1135. * exists */
  1136. if (ah->ah_version == AR5K_AR5212)
  1137. ath5k_hw_set_sleep_clock(ah, true);
  1138. /*
  1139. * Disable beacons and reset the register
  1140. */
  1141. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
  1142. AR5K_BEACON_RESET_TSF);
  1143. return 0;
  1144. }
  1145. #undef _ATH5K_RESET