reset.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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>
  26. #include "ath5k.h"
  27. #include "reg.h"
  28. #include "base.h"
  29. #include "debug.h"
  30. /**
  31. * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
  32. *
  33. * @ah: the &struct ath5k_hw
  34. * @channel: the currently set channel upon reset
  35. *
  36. * Write the OFDM timings for the AR5212 upon reset. This is a helper for
  37. * ath5k_hw_reset(). This seems to tune the PLL a specified frequency
  38. * depending on the bandwidth of the channel.
  39. *
  40. */
  41. static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  42. struct ieee80211_channel *channel)
  43. {
  44. /* Get exponent and mantissa and set it */
  45. u32 coef_scaled, coef_exp, coef_man,
  46. ds_coef_exp, ds_coef_man, clock;
  47. if (!(ah->ah_version == AR5K_AR5212) ||
  48. !(channel->hw_value & CHANNEL_OFDM))
  49. BUG();
  50. /* Seems there are two PLLs, one for baseband sampling and one
  51. * for tuning. Tuning basebands are 40 MHz or 80MHz when in
  52. * turbo. */
  53. clock = channel->hw_value & CHANNEL_TURBO ? 80 : 40;
  54. coef_scaled = ((5 * (clock << 24)) / 2) /
  55. channel->center_freq;
  56. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  57. if ((coef_scaled >> coef_exp) & 0x1)
  58. break;
  59. if (!coef_exp)
  60. return -EINVAL;
  61. coef_exp = 14 - (coef_exp - 24);
  62. coef_man = coef_scaled +
  63. (1 << (24 - coef_exp - 1));
  64. ds_coef_man = coef_man >> (24 - coef_exp);
  65. ds_coef_exp = coef_exp - 16;
  66. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  67. AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
  68. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  69. AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
  70. return 0;
  71. }
  72. /*
  73. * index into rates for control rates, we can set it up like this because
  74. * this is only used for AR5212 and we know it supports G mode
  75. */
  76. static int control_rates[] =
  77. { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
  78. /**
  79. * ath5k_hw_write_rate_duration - set rate duration during hw resets
  80. *
  81. * @ah: the &struct ath5k_hw
  82. * @mode: one of enum ath5k_driver_mode
  83. *
  84. * Write the rate duration table upon hw reset. This is a helper for
  85. * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout for
  86. * the hardware for the current mode for each rate. The rates which are capable
  87. * of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have another
  88. * register for the short preamble ACK timeout calculation.
  89. */
  90. static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
  91. unsigned int mode)
  92. {
  93. struct ath5k_softc *sc = ah->ah_sc;
  94. struct ieee80211_rate *rate;
  95. unsigned int i;
  96. /* Write rate duration table */
  97. for (i = 0; i < sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates; i++) {
  98. u32 reg;
  99. u16 tx_time;
  100. rate = &sc->sbands[IEEE80211_BAND_2GHZ].bitrates[control_rates[i]];
  101. /* Set ACK timeout */
  102. reg = AR5K_RATE_DUR(rate->hw_value);
  103. /* An ACK frame consists of 10 bytes. If you add the FCS,
  104. * which ieee80211_generic_frame_duration() adds,
  105. * its 14 bytes. Note we use the control rate and not the
  106. * actual rate for this rate. See mac80211 tx.c
  107. * ieee80211_duration() for a brief description of
  108. * what rate we should choose to TX ACKs. */
  109. tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
  110. sc->vif, 10, rate));
  111. ath5k_hw_reg_write(ah, tx_time, reg);
  112. if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
  113. continue;
  114. /*
  115. * We're not distinguishing short preamble here,
  116. * This is true, all we'll get is a longer value here
  117. * which is not necessarilly bad. We could use
  118. * export ieee80211_frame_duration() but that needs to be
  119. * fixed first to be properly used by mac802111 drivers:
  120. *
  121. * - remove erp stuff and let the routine figure ofdm
  122. * erp rates
  123. * - remove passing argument ieee80211_local as
  124. * drivers don't have access to it
  125. * - move drivers using ieee80211_generic_frame_duration()
  126. * to this
  127. */
  128. ath5k_hw_reg_write(ah, tx_time,
  129. reg + (AR5K_SET_SHORT_PREAMBLE << 2));
  130. }
  131. }
  132. /*
  133. * Reset chipset
  134. */
  135. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  136. {
  137. int ret;
  138. u32 mask = val ? val : ~0U;
  139. ATH5K_TRACE(ah->ah_sc);
  140. /* Read-and-clear RX Descriptor Pointer*/
  141. ath5k_hw_reg_read(ah, AR5K_RXDP);
  142. /*
  143. * Reset the device and wait until success
  144. */
  145. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  146. /* Wait at least 128 PCI clocks */
  147. udelay(15);
  148. if (ah->ah_version == AR5K_AR5210) {
  149. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  150. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  151. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  152. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  153. } else {
  154. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  155. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  156. }
  157. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  158. /*
  159. * Reset configuration register (for hw byte-swap). Note that this
  160. * is only set for big endian. We do the necessary magic in
  161. * AR5K_INIT_CFG.
  162. */
  163. if ((val & AR5K_RESET_CTL_PCU) == 0)
  164. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  165. return ret;
  166. }
  167. /*
  168. * Sleep control
  169. */
  170. int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  171. bool set_chip, u16 sleep_duration)
  172. {
  173. unsigned int i;
  174. u32 staid, data;
  175. ATH5K_TRACE(ah->ah_sc);
  176. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  177. switch (mode) {
  178. case AR5K_PM_AUTO:
  179. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  180. /* fallthrough */
  181. case AR5K_PM_NETWORK_SLEEP:
  182. if (set_chip)
  183. ath5k_hw_reg_write(ah,
  184. AR5K_SLEEP_CTL_SLE_ALLOW |
  185. sleep_duration,
  186. AR5K_SLEEP_CTL);
  187. staid |= AR5K_STA_ID1_PWR_SV;
  188. break;
  189. case AR5K_PM_FULL_SLEEP:
  190. if (set_chip)
  191. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  192. AR5K_SLEEP_CTL);
  193. staid |= AR5K_STA_ID1_PWR_SV;
  194. break;
  195. case AR5K_PM_AWAKE:
  196. staid &= ~AR5K_STA_ID1_PWR_SV;
  197. if (!set_chip)
  198. goto commit;
  199. /* Preserve sleep duration */
  200. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  201. if (data & 0xffc00000)
  202. data = 0;
  203. else
  204. data = data & 0xfffcffff;
  205. ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
  206. udelay(15);
  207. for (i = 50; i > 0; i--) {
  208. /* Check if the chip did wake up */
  209. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  210. AR5K_PCICFG_SPWR_DN) == 0)
  211. break;
  212. /* Wait a bit and retry */
  213. udelay(200);
  214. ath5k_hw_reg_write(ah, data, AR5K_SLEEP_CTL);
  215. }
  216. /* Fail if the chip didn't wake up */
  217. if (i <= 0)
  218. return -EIO;
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. commit:
  224. ah->ah_power_mode = mode;
  225. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  226. return 0;
  227. }
  228. /*
  229. * Bring up MAC + PHY Chips
  230. */
  231. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  232. {
  233. struct pci_dev *pdev = ah->ah_sc->pdev;
  234. u32 turbo, mode, clock, bus_flags;
  235. int ret;
  236. turbo = 0;
  237. mode = 0;
  238. clock = 0;
  239. ATH5K_TRACE(ah->ah_sc);
  240. /* Wakeup the device */
  241. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  242. if (ret) {
  243. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  244. return ret;
  245. }
  246. if (ah->ah_version != AR5K_AR5210) {
  247. /*
  248. * Get channel mode flags
  249. */
  250. if (ah->ah_radio >= AR5K_RF5112) {
  251. mode = AR5K_PHY_MODE_RAD_RF5112;
  252. clock = AR5K_PHY_PLL_RF5112;
  253. } else {
  254. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  255. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  256. }
  257. if (flags & CHANNEL_2GHZ) {
  258. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  259. clock |= AR5K_PHY_PLL_44MHZ;
  260. if (flags & CHANNEL_CCK) {
  261. mode |= AR5K_PHY_MODE_MOD_CCK;
  262. } else if (flags & CHANNEL_OFDM) {
  263. /* XXX Dynamic OFDM/CCK is not supported by the
  264. * AR5211 so we set MOD_OFDM for plain g (no
  265. * CCK headers) operation. We need to test
  266. * this, 5211 might support ofdm-only g after
  267. * all, there are also initial register values
  268. * in the code for g mode (see initvals.c). */
  269. if (ah->ah_version == AR5K_AR5211)
  270. mode |= AR5K_PHY_MODE_MOD_OFDM;
  271. else
  272. mode |= AR5K_PHY_MODE_MOD_DYN;
  273. } else {
  274. ATH5K_ERR(ah->ah_sc,
  275. "invalid radio modulation mode\n");
  276. return -EINVAL;
  277. }
  278. } else if (flags & CHANNEL_5GHZ) {
  279. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  280. clock |= AR5K_PHY_PLL_40MHZ;
  281. if (flags & CHANNEL_OFDM)
  282. mode |= AR5K_PHY_MODE_MOD_OFDM;
  283. else {
  284. ATH5K_ERR(ah->ah_sc,
  285. "invalid radio modulation mode\n");
  286. return -EINVAL;
  287. }
  288. } else {
  289. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  290. return -EINVAL;
  291. }
  292. if (flags & CHANNEL_TURBO)
  293. turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
  294. } else { /* Reset the device */
  295. /* ...enable Atheros turbo mode if requested */
  296. if (flags & CHANNEL_TURBO)
  297. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  298. AR5K_PHY_TURBO);
  299. }
  300. /* reseting PCI on PCI-E cards results card to hang
  301. * and always return 0xffff... so we ingore that flag
  302. * for PCI-E cards */
  303. bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
  304. /* Reset chipset */
  305. if (ah->ah_version == AR5K_AR5210) {
  306. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  307. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  308. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  309. mdelay(2);
  310. } else {
  311. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  312. AR5K_RESET_CTL_BASEBAND | bus_flags);
  313. }
  314. if (ret) {
  315. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
  316. return -EIO;
  317. }
  318. /* ...wakeup again!*/
  319. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  320. if (ret) {
  321. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  322. return ret;
  323. }
  324. /* ...final warm reset */
  325. if (ath5k_hw_nic_reset(ah, 0)) {
  326. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  327. return -EIO;
  328. }
  329. if (ah->ah_version != AR5K_AR5210) {
  330. /* ...set the PHY operating mode */
  331. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  332. udelay(300);
  333. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  334. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  335. }
  336. return 0;
  337. }
  338. /*
  339. * Main reset function
  340. */
  341. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  342. struct ieee80211_channel *channel, bool change_channel)
  343. {
  344. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  345. struct pci_dev *pdev = ah->ah_sc->pdev;
  346. u32 data, s_seq, s_ant, s_led[3], dma_size;
  347. unsigned int i, mode, freq, ee_mode, ant[2];
  348. int ret;
  349. ATH5K_TRACE(ah->ah_sc);
  350. s_seq = 0;
  351. s_ant = 0;
  352. ee_mode = 0;
  353. freq = 0;
  354. mode = 0;
  355. /*
  356. * Save some registers before a reset
  357. */
  358. /*DCU/Antenna selection not available on 5210*/
  359. if (ah->ah_version != AR5K_AR5210) {
  360. if (change_channel) {
  361. /* Seq number for queue 0 -do this for all queues ? */
  362. s_seq = ath5k_hw_reg_read(ah,
  363. AR5K_QUEUE_DFS_SEQNUM(0));
  364. /*Default antenna*/
  365. s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  366. }
  367. }
  368. /*GPIOs*/
  369. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) & AR5K_PCICFG_LEDSTATE;
  370. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  371. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  372. if (change_channel && ah->ah_rf_banks != NULL)
  373. ath5k_hw_get_rf_gain(ah);
  374. /*Wakeup the device*/
  375. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  376. if (ret)
  377. return ret;
  378. /*
  379. * Initialize operating mode
  380. */
  381. ah->ah_op_mode = op_mode;
  382. /*
  383. * 5111/5112 Settings
  384. * 5210 only comes with RF5110
  385. */
  386. if (ah->ah_version != AR5K_AR5210) {
  387. if (ah->ah_radio != AR5K_RF5111 &&
  388. ah->ah_radio != AR5K_RF5112 &&
  389. ah->ah_radio != AR5K_RF5413 &&
  390. ah->ah_radio != AR5K_RF2413 &&
  391. ah->ah_radio != AR5K_RF2425) {
  392. ATH5K_ERR(ah->ah_sc,
  393. "invalid phy radio: %u\n", ah->ah_radio);
  394. return -EINVAL;
  395. }
  396. switch (channel->hw_value & CHANNEL_MODES) {
  397. case CHANNEL_A:
  398. mode = AR5K_MODE_11A;
  399. freq = AR5K_INI_RFGAIN_5GHZ;
  400. ee_mode = AR5K_EEPROM_MODE_11A;
  401. break;
  402. case CHANNEL_G:
  403. mode = AR5K_MODE_11G;
  404. freq = AR5K_INI_RFGAIN_2GHZ;
  405. ee_mode = AR5K_EEPROM_MODE_11G;
  406. break;
  407. case CHANNEL_B:
  408. mode = AR5K_MODE_11B;
  409. freq = AR5K_INI_RFGAIN_2GHZ;
  410. ee_mode = AR5K_EEPROM_MODE_11B;
  411. break;
  412. case CHANNEL_T:
  413. mode = AR5K_MODE_11A_TURBO;
  414. freq = AR5K_INI_RFGAIN_5GHZ;
  415. ee_mode = AR5K_EEPROM_MODE_11A;
  416. break;
  417. /*Is this ok on 5211 too ?*/
  418. case CHANNEL_TG:
  419. mode = AR5K_MODE_11G_TURBO;
  420. freq = AR5K_INI_RFGAIN_2GHZ;
  421. ee_mode = AR5K_EEPROM_MODE_11G;
  422. break;
  423. case CHANNEL_XR:
  424. if (ah->ah_version == AR5K_AR5211) {
  425. ATH5K_ERR(ah->ah_sc,
  426. "XR mode not available on 5211");
  427. return -EINVAL;
  428. }
  429. mode = AR5K_MODE_XR;
  430. freq = AR5K_INI_RFGAIN_5GHZ;
  431. ee_mode = AR5K_EEPROM_MODE_11A;
  432. break;
  433. default:
  434. ATH5K_ERR(ah->ah_sc,
  435. "invalid channel: %d\n", channel->center_freq);
  436. return -EINVAL;
  437. }
  438. /* PHY access enable */
  439. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  440. }
  441. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  442. if (ret)
  443. return ret;
  444. /*
  445. * 5211/5212 Specific
  446. */
  447. if (ah->ah_version != AR5K_AR5210) {
  448. /*
  449. * Write initial RF gain settings
  450. * This should work for both 5111/5112
  451. */
  452. ret = ath5k_hw_rfgain(ah, freq);
  453. if (ret)
  454. return ret;
  455. mdelay(1);
  456. /*
  457. * Write some more initial register settings for revised chips
  458. */
  459. if (ah->ah_version == AR5K_AR5212 &&
  460. ah->ah_phy_revision > 0x41) {
  461. ath5k_hw_reg_write(ah, 0x0002a002, 0x982c);
  462. if (channel->hw_value == CHANNEL_G)
  463. if (ah->ah_mac_srev < AR5K_SREV_AR2413)
  464. ath5k_hw_reg_write(ah, 0x00f80d80,
  465. 0x994c);
  466. else if (ah->ah_mac_srev < AR5K_SREV_AR5424)
  467. ath5k_hw_reg_write(ah, 0x00380140,
  468. 0x994c);
  469. else if (ah->ah_mac_srev < AR5K_SREV_AR2425)
  470. ath5k_hw_reg_write(ah, 0x00fc0ec0,
  471. 0x994c);
  472. else /* 2425 */
  473. ath5k_hw_reg_write(ah, 0x00fc0fc0,
  474. 0x994c);
  475. else
  476. ath5k_hw_reg_write(ah, 0x00000000, 0x994c);
  477. /* Got this from legacy-hal */
  478. AR5K_REG_DISABLE_BITS(ah, 0xa228, 0x200);
  479. AR5K_REG_MASKED_BITS(ah, 0xa228, 0x800, 0xfffe03ff);
  480. /* Just write 0x9b5 ? */
  481. /* ath5k_hw_reg_write(ah, 0x000009b5, 0xa228); */
  482. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  483. ath5k_hw_reg_write(ah, 0x00000000, 0xa254);
  484. ath5k_hw_reg_write(ah, 0x0000000e, AR5K_PHY_SCAL);
  485. }
  486. /* Fix for first revision of the RF5112 RF chipset */
  487. if (ah->ah_radio >= AR5K_RF5112 &&
  488. ah->ah_radio_5ghz_revision <
  489. AR5K_SREV_RAD_5112A) {
  490. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  491. AR5K_PHY_CCKTXCTL);
  492. if (channel->hw_value & CHANNEL_5GHZ)
  493. data = 0xffb81020;
  494. else
  495. data = 0xffb80d20;
  496. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  497. data = 0;
  498. }
  499. /*
  500. * Set TX power (FIXME)
  501. */
  502. ret = ath5k_hw_txpower(ah, channel, AR5K_TUNE_DEFAULT_TXPOWER);
  503. if (ret)
  504. return ret;
  505. /* Write rate duration table only on AR5212 and if
  506. * virtual interface has already been brought up
  507. * XXX: rethink this after new mode changes to
  508. * mac80211 are integrated */
  509. if (ah->ah_version == AR5K_AR5212 &&
  510. ah->ah_sc->vif != NULL)
  511. ath5k_hw_write_rate_duration(ah, mode);
  512. /*
  513. * Write RF registers
  514. */
  515. ret = ath5k_hw_rfregs(ah, channel, mode);
  516. if (ret)
  517. return ret;
  518. /*
  519. * Configure additional registers
  520. */
  521. /* Write OFDM timings on 5212*/
  522. if (ah->ah_version == AR5K_AR5212 &&
  523. channel->hw_value & CHANNEL_OFDM) {
  524. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  525. if (ret)
  526. return ret;
  527. }
  528. /*Enable/disable 802.11b mode on 5111
  529. (enable 2111 frequency converter + CCK)*/
  530. if (ah->ah_radio == AR5K_RF5111) {
  531. if (mode == AR5K_MODE_11B)
  532. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  533. AR5K_TXCFG_B_MODE);
  534. else
  535. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  536. AR5K_TXCFG_B_MODE);
  537. }
  538. /*
  539. * Set channel and calibrate the PHY
  540. */
  541. ret = ath5k_hw_channel(ah, channel);
  542. if (ret)
  543. return ret;
  544. /* Set antenna mode */
  545. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_ANT_CTL,
  546. ah->ah_antenna[ee_mode][0], 0xfffffc06);
  547. /*
  548. * In case a fixed antenna was set as default
  549. * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
  550. * registers.
  551. */
  552. if (s_ant != 0) {
  553. if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
  554. ant[0] = ant[1] = AR5K_ANT_FIXED_A;
  555. else /* 2 - Aux */
  556. ant[0] = ant[1] = AR5K_ANT_FIXED_B;
  557. } else {
  558. ant[0] = AR5K_ANT_FIXED_A;
  559. ant[1] = AR5K_ANT_FIXED_B;
  560. }
  561. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
  562. AR5K_PHY_ANT_SWITCH_TABLE_0);
  563. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
  564. AR5K_PHY_ANT_SWITCH_TABLE_1);
  565. /* Commit values from EEPROM */
  566. if (ah->ah_radio == AR5K_RF5111)
  567. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  568. AR5K_PHY_FRAME_CTL_TX_CLIP, ee->ee_tx_clip);
  569. ath5k_hw_reg_write(ah,
  570. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  571. AR5K_PHY_NFTHRES);
  572. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_SETTLING,
  573. (ee->ee_switch_settling[ee_mode] << 7) & 0x3f80,
  574. 0xffffc07f);
  575. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_GAIN,
  576. (ee->ee_atn_tx_rx[ee_mode] << 12) & 0x3f000,
  577. 0xfffc0fff);
  578. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  579. (ee->ee_adc_desired_size[ee_mode] & 0x00ff) |
  580. ((ee->ee_pga_desired_size[ee_mode] << 8) & 0xff00),
  581. 0xffff0000);
  582. ath5k_hw_reg_write(ah,
  583. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  584. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  585. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  586. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  587. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_RF_CTL3,
  588. ee->ee_tx_end2xlna_enable[ee_mode] << 8, 0xffff00ff);
  589. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_NF,
  590. (ee->ee_thr_62[ee_mode] << 12) & 0x7f000, 0xfff80fff);
  591. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_OFDM_SELFCORR, 4, 0xffffff01);
  592. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  593. AR5K_PHY_IQ_CORR_ENABLE |
  594. (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
  595. ee->ee_q_cal[ee_mode]);
  596. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  597. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  598. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  599. ee->ee_margin_tx_rx[ee_mode]);
  600. } else {
  601. mdelay(1);
  602. /* Disable phy and wait */
  603. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  604. mdelay(1);
  605. }
  606. /*
  607. * Restore saved values
  608. */
  609. /*DCU/Antenna selection not available on 5210*/
  610. if (ah->ah_version != AR5K_AR5210) {
  611. ath5k_hw_reg_write(ah, s_seq, AR5K_QUEUE_DFS_SEQNUM(0));
  612. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  613. }
  614. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  615. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  616. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  617. /*
  618. * Misc
  619. */
  620. /* XXX: add ah->aid once mac80211 gives this to us */
  621. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  622. ath5k_hw_set_opmode(ah);
  623. /*PISR/SISR Not available on 5210*/
  624. if (ah->ah_version != AR5K_AR5210) {
  625. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  626. /* If we later allow tuning for this, store into sc structure */
  627. data = AR5K_TUNE_RSSI_THRES |
  628. AR5K_TUNE_BMISS_THRES << AR5K_RSSI_THR_BMISS_S;
  629. ath5k_hw_reg_write(ah, data, AR5K_RSSI_THR);
  630. }
  631. /*
  632. * Set Rx/Tx DMA Configuration
  633. *
  634. * Set maximum DMA size (512) except for PCI-E cards since
  635. * it causes rx overruns and tx errors (tested on 5424 but since
  636. * rx overruns also occur on 5416/5418 with madwifi we set 128
  637. * for all PCI-E cards to be safe).
  638. *
  639. * In dumps this is 128 for allchips.
  640. *
  641. * XXX: need to check 5210 for this
  642. * TODO: Check out tx triger level, it's always 64 on dumps but I
  643. * guess we can tweak it and see how it goes ;-)
  644. */
  645. dma_size = (pdev->is_pcie) ? AR5K_DMASIZE_128B : AR5K_DMASIZE_512B;
  646. if (ah->ah_version != AR5K_AR5210) {
  647. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  648. AR5K_TXCFG_SDMAMR, dma_size);
  649. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  650. AR5K_RXCFG_SDMAMW, dma_size);
  651. }
  652. /*
  653. * Enable the PHY and wait until completion
  654. */
  655. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  656. /*
  657. * On 5211+ read activation -> rx delay
  658. * and use it.
  659. */
  660. if (ah->ah_version != AR5K_AR5210) {
  661. data = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  662. AR5K_PHY_RX_DELAY_M;
  663. data = (channel->hw_value & CHANNEL_CCK) ?
  664. ((data << 2) / 22) : (data / 10);
  665. udelay(100 + (2 * data));
  666. data = 0;
  667. } else {
  668. mdelay(1);
  669. }
  670. /*
  671. * Perform ADC test (?)
  672. */
  673. data = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  674. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  675. for (i = 0; i <= 20; i++) {
  676. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  677. break;
  678. udelay(200);
  679. }
  680. ath5k_hw_reg_write(ah, data, AR5K_PHY_TST1);
  681. data = 0;
  682. /*
  683. * Start automatic gain calibration
  684. *
  685. * During AGC calibration RX path is re-routed to
  686. * a signal detector so we don't receive anything.
  687. *
  688. * This method is used to calibrate some static offsets
  689. * used together with on-the fly I/Q calibration (the
  690. * one performed via ath5k_hw_phy_calibrate), that doesn't
  691. * interrupt rx path.
  692. *
  693. * If we are in a noisy environment AGC calibration may time
  694. * out.
  695. */
  696. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  697. AR5K_PHY_AGCCTL_CAL);
  698. /* At the same time start I/Q calibration for QAM constellation
  699. * -no need for CCK- */
  700. ah->ah_calibration = false;
  701. if (!(mode == AR5K_MODE_11B)) {
  702. ah->ah_calibration = true;
  703. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  704. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  705. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  706. AR5K_PHY_IQ_RUN);
  707. }
  708. /* Wait for gain calibration to finish (we check for I/Q calibration
  709. * during ath5k_phy_calibrate) */
  710. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  711. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  712. ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
  713. channel->center_freq);
  714. return -EAGAIN;
  715. }
  716. /*
  717. * Start noise floor calibration
  718. *
  719. * If we run NF calibration before AGC, it always times out.
  720. * Binary HAL starts NF and AGC calibration at the same time
  721. * and only waits for AGC to finish. I believe that's wrong because
  722. * during NF calibration, rx path is also routed to a detector, so if
  723. * it doesn't finish we won't have RX.
  724. *
  725. * XXX: Find an interval that's OK for all cards...
  726. */
  727. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  728. /*
  729. * Reset queues and start beacon timers at the end of the reset routine
  730. */
  731. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  732. /*No QCU on 5210*/
  733. if (ah->ah_version != AR5K_AR5210)
  734. AR5K_REG_WRITE_Q(ah, AR5K_QUEUE_QCUMASK(i), i);
  735. ret = ath5k_hw_reset_tx_queue(ah, i);
  736. if (ret) {
  737. ATH5K_ERR(ah->ah_sc,
  738. "failed to reset TX queue #%d\n", i);
  739. return ret;
  740. }
  741. }
  742. /* Pre-enable interrupts on 5211/5212*/
  743. if (ah->ah_version != AR5K_AR5210)
  744. ath5k_hw_set_imr(ah, ah->ah_imr);
  745. /*
  746. * Set RF kill flags if supported by the device (read from the EEPROM)
  747. * Disable gpio_intr for now since it results system hang.
  748. * TODO: Handle this in ath5k_intr
  749. */
  750. #if 0
  751. if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
  752. ath5k_hw_set_gpio_input(ah, 0);
  753. ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
  754. if (ah->ah_gpio[0] == 0)
  755. ath5k_hw_set_gpio_intr(ah, 0, 1);
  756. else
  757. ath5k_hw_set_gpio_intr(ah, 0, 0);
  758. }
  759. #endif
  760. /*
  761. * Set the 32MHz reference clock on 5212 phy clock sleep register
  762. *
  763. * TODO: Find out how to switch to external 32Khz clock to save power
  764. */
  765. if (ah->ah_version == AR5K_AR5212) {
  766. ath5k_hw_reg_write(ah, AR5K_PHY_SCR_32MHZ, AR5K_PHY_SCR);
  767. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  768. ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ, AR5K_PHY_SCAL);
  769. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  770. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  771. ath5k_hw_reg_write(ah, ah->ah_phy_spending, AR5K_PHY_SPENDING);
  772. data = ath5k_hw_reg_read(ah, AR5K_USEC_5211) & 0xffffc07f ;
  773. data |= (ah->ah_phy_spending == AR5K_PHY_SPENDING_18) ?
  774. 0x00000f80 : 0x00001380 ;
  775. ath5k_hw_reg_write(ah, data, AR5K_USEC_5211);
  776. data = 0;
  777. }
  778. if (ah->ah_version == AR5K_AR5212) {
  779. ath5k_hw_reg_write(ah, 0x000100aa, 0x8118);
  780. ath5k_hw_reg_write(ah, 0x00003210, 0x811c);
  781. ath5k_hw_reg_write(ah, 0x00000052, 0x8108);
  782. if (ah->ah_mac_srev >= AR5K_SREV_AR2413)
  783. ath5k_hw_reg_write(ah, 0x00000004, 0x8120);
  784. }
  785. /*
  786. * Disable beacons and reset the register
  787. */
  788. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
  789. AR5K_BEACON_RESET_TSF);
  790. return 0;
  791. }
  792. #undef _ATH5K_RESET