reset.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. /*Wakeup the device*/
  373. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  374. if (ret)
  375. return ret;
  376. /*
  377. * Initialize operating mode
  378. */
  379. ah->ah_op_mode = op_mode;
  380. /*
  381. * 5111/5112 Settings
  382. * 5210 only comes with RF5110
  383. */
  384. if (ah->ah_version != AR5K_AR5210) {
  385. if (ah->ah_radio != AR5K_RF5111 &&
  386. ah->ah_radio != AR5K_RF5112 &&
  387. ah->ah_radio != AR5K_RF5413 &&
  388. ah->ah_radio != AR5K_RF2413 &&
  389. ah->ah_radio != AR5K_RF2425) {
  390. ATH5K_ERR(ah->ah_sc,
  391. "invalid phy radio: %u\n", ah->ah_radio);
  392. return -EINVAL;
  393. }
  394. switch (channel->hw_value & CHANNEL_MODES) {
  395. case CHANNEL_A:
  396. mode = AR5K_MODE_11A;
  397. freq = AR5K_INI_RFGAIN_5GHZ;
  398. ee_mode = AR5K_EEPROM_MODE_11A;
  399. break;
  400. case CHANNEL_G:
  401. mode = AR5K_MODE_11G;
  402. freq = AR5K_INI_RFGAIN_2GHZ;
  403. ee_mode = AR5K_EEPROM_MODE_11G;
  404. break;
  405. case CHANNEL_B:
  406. mode = AR5K_MODE_11B;
  407. freq = AR5K_INI_RFGAIN_2GHZ;
  408. ee_mode = AR5K_EEPROM_MODE_11B;
  409. break;
  410. case CHANNEL_T:
  411. mode = AR5K_MODE_11A_TURBO;
  412. freq = AR5K_INI_RFGAIN_5GHZ;
  413. ee_mode = AR5K_EEPROM_MODE_11A;
  414. break;
  415. /*Is this ok on 5211 too ?*/
  416. case CHANNEL_TG:
  417. mode = AR5K_MODE_11G_TURBO;
  418. freq = AR5K_INI_RFGAIN_2GHZ;
  419. ee_mode = AR5K_EEPROM_MODE_11G;
  420. break;
  421. case CHANNEL_XR:
  422. if (ah->ah_version == AR5K_AR5211) {
  423. ATH5K_ERR(ah->ah_sc,
  424. "XR mode not available on 5211");
  425. return -EINVAL;
  426. }
  427. mode = AR5K_MODE_XR;
  428. freq = AR5K_INI_RFGAIN_5GHZ;
  429. ee_mode = AR5K_EEPROM_MODE_11A;
  430. break;
  431. default:
  432. ATH5K_ERR(ah->ah_sc,
  433. "invalid channel: %d\n", channel->center_freq);
  434. return -EINVAL;
  435. }
  436. /* PHY access enable */
  437. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  438. }
  439. ret = ath5k_hw_write_initvals(ah, mode, change_channel);
  440. if (ret)
  441. return ret;
  442. /*
  443. * 5211/5212 Specific
  444. */
  445. if (ah->ah_version != AR5K_AR5210) {
  446. /*
  447. * Write initial RF gain settings
  448. * This should work for both 5111/5112
  449. */
  450. ret = ath5k_hw_rfgain_init(ah, freq);
  451. if (ret)
  452. return ret;
  453. mdelay(1);
  454. /*
  455. * Write some more initial register settings for revised chips
  456. */
  457. if (ah->ah_version == AR5K_AR5212 &&
  458. ah->ah_phy_revision > 0x41) {
  459. ath5k_hw_reg_write(ah, 0x0002a002, 0x982c);
  460. if (channel->hw_value == CHANNEL_G)
  461. if (ah->ah_mac_srev < AR5K_SREV_AR2413)
  462. ath5k_hw_reg_write(ah, 0x00f80d80,
  463. 0x994c);
  464. else if (ah->ah_mac_srev < AR5K_SREV_AR5424)
  465. ath5k_hw_reg_write(ah, 0x00380140,
  466. 0x994c);
  467. else if (ah->ah_mac_srev < AR5K_SREV_AR2425)
  468. ath5k_hw_reg_write(ah, 0x00fc0ec0,
  469. 0x994c);
  470. else /* 2425 */
  471. ath5k_hw_reg_write(ah, 0x00fc0fc0,
  472. 0x994c);
  473. else
  474. ath5k_hw_reg_write(ah, 0x00000000, 0x994c);
  475. /* Got this from legacy-hal */
  476. AR5K_REG_DISABLE_BITS(ah, 0xa228, 0x200);
  477. AR5K_REG_MASKED_BITS(ah, 0xa228, 0x800, 0xfffe03ff);
  478. /* Just write 0x9b5 ? */
  479. /* ath5k_hw_reg_write(ah, 0x000009b5, 0xa228); */
  480. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  481. ath5k_hw_reg_write(ah, 0x00000000, 0xa254);
  482. ath5k_hw_reg_write(ah, 0x0000000e, AR5K_PHY_SCAL);
  483. }
  484. /* Fix for first revision of the RF5112 RF chipset */
  485. if (ah->ah_radio >= AR5K_RF5112 &&
  486. ah->ah_radio_5ghz_revision <
  487. AR5K_SREV_RAD_5112A) {
  488. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  489. AR5K_PHY_CCKTXCTL);
  490. if (channel->hw_value & CHANNEL_5GHZ)
  491. data = 0xffb81020;
  492. else
  493. data = 0xffb80d20;
  494. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  495. data = 0;
  496. }
  497. /*
  498. * Set TX power (FIXME)
  499. */
  500. ret = ath5k_hw_txpower(ah, channel, AR5K_TUNE_DEFAULT_TXPOWER);
  501. if (ret)
  502. return ret;
  503. /* Write rate duration table only on AR5212 and if
  504. * virtual interface has already been brought up
  505. * XXX: rethink this after new mode changes to
  506. * mac80211 are integrated */
  507. if (ah->ah_version == AR5K_AR5212 &&
  508. ah->ah_sc->vif != NULL)
  509. ath5k_hw_write_rate_duration(ah, mode);
  510. /*
  511. * Write RF registers
  512. */
  513. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  514. if (ret)
  515. return ret;
  516. /*
  517. * Configure additional registers
  518. */
  519. /* Write OFDM timings on 5212*/
  520. if (ah->ah_version == AR5K_AR5212 &&
  521. channel->hw_value & CHANNEL_OFDM) {
  522. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  523. if (ret)
  524. return ret;
  525. }
  526. /*Enable/disable 802.11b mode on 5111
  527. (enable 2111 frequency converter + CCK)*/
  528. if (ah->ah_radio == AR5K_RF5111) {
  529. if (mode == AR5K_MODE_11B)
  530. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  531. AR5K_TXCFG_B_MODE);
  532. else
  533. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  534. AR5K_TXCFG_B_MODE);
  535. }
  536. /*
  537. * Set channel and calibrate the PHY
  538. */
  539. ret = ath5k_hw_channel(ah, channel);
  540. if (ret)
  541. return ret;
  542. /* Set antenna mode */
  543. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_ANT_CTL,
  544. ah->ah_antenna[ee_mode][0], 0xfffffc06);
  545. /*
  546. * In case a fixed antenna was set as default
  547. * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
  548. * registers.
  549. */
  550. if (s_ant != 0) {
  551. if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
  552. ant[0] = ant[1] = AR5K_ANT_FIXED_A;
  553. else /* 2 - Aux */
  554. ant[0] = ant[1] = AR5K_ANT_FIXED_B;
  555. } else {
  556. ant[0] = AR5K_ANT_FIXED_A;
  557. ant[1] = AR5K_ANT_FIXED_B;
  558. }
  559. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
  560. AR5K_PHY_ANT_SWITCH_TABLE_0);
  561. ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
  562. AR5K_PHY_ANT_SWITCH_TABLE_1);
  563. /* Commit values from EEPROM */
  564. if (ah->ah_radio == AR5K_RF5111)
  565. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  566. AR5K_PHY_FRAME_CTL_TX_CLIP, ee->ee_tx_clip);
  567. ath5k_hw_reg_write(ah,
  568. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  569. AR5K_PHY_NFTHRES);
  570. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_SETTLING,
  571. (ee->ee_switch_settling[ee_mode] << 7) & 0x3f80,
  572. 0xffffc07f);
  573. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_GAIN,
  574. (ee->ee_atn_tx_rx[ee_mode] << 12) & 0x3f000,
  575. 0xfffc0fff);
  576. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  577. (ee->ee_adc_desired_size[ee_mode] & 0x00ff) |
  578. ((ee->ee_pga_desired_size[ee_mode] << 8) & 0xff00),
  579. 0xffff0000);
  580. ath5k_hw_reg_write(ah,
  581. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  582. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  583. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  584. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  585. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_RF_CTL3,
  586. ee->ee_tx_end2xlna_enable[ee_mode] << 8, 0xffff00ff);
  587. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_NF,
  588. (ee->ee_thr_62[ee_mode] << 12) & 0x7f000, 0xfff80fff);
  589. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_OFDM_SELFCORR, 4, 0xffffff01);
  590. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  591. AR5K_PHY_IQ_CORR_ENABLE |
  592. (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
  593. ee->ee_q_cal[ee_mode]);
  594. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  595. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  596. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  597. ee->ee_margin_tx_rx[ee_mode]);
  598. } else {
  599. mdelay(1);
  600. /* Disable phy and wait */
  601. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  602. mdelay(1);
  603. }
  604. /*
  605. * Restore saved values
  606. */
  607. /*DCU/Antenna selection not available on 5210*/
  608. if (ah->ah_version != AR5K_AR5210) {
  609. ath5k_hw_reg_write(ah, s_seq, AR5K_QUEUE_DFS_SEQNUM(0));
  610. ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
  611. }
  612. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  613. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  614. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  615. /*
  616. * Misc
  617. */
  618. /* XXX: add ah->aid once mac80211 gives this to us */
  619. ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
  620. ath5k_hw_set_opmode(ah);
  621. /*PISR/SISR Not available on 5210*/
  622. if (ah->ah_version != AR5K_AR5210) {
  623. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
  624. /* If we later allow tuning for this, store into sc structure */
  625. data = AR5K_TUNE_RSSI_THRES |
  626. AR5K_TUNE_BMISS_THRES << AR5K_RSSI_THR_BMISS_S;
  627. ath5k_hw_reg_write(ah, data, AR5K_RSSI_THR);
  628. }
  629. /*
  630. * Set Rx/Tx DMA Configuration
  631. *
  632. * Set maximum DMA size (512) except for PCI-E cards since
  633. * it causes rx overruns and tx errors (tested on 5424 but since
  634. * rx overruns also occur on 5416/5418 with madwifi we set 128
  635. * for all PCI-E cards to be safe).
  636. *
  637. * In dumps this is 128 for allchips.
  638. *
  639. * XXX: need to check 5210 for this
  640. * TODO: Check out tx triger level, it's always 64 on dumps but I
  641. * guess we can tweak it and see how it goes ;-)
  642. */
  643. dma_size = (pdev->is_pcie) ? AR5K_DMASIZE_128B : AR5K_DMASIZE_512B;
  644. if (ah->ah_version != AR5K_AR5210) {
  645. AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
  646. AR5K_TXCFG_SDMAMR, dma_size);
  647. AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
  648. AR5K_RXCFG_SDMAMW, dma_size);
  649. }
  650. /*
  651. * Enable the PHY and wait until completion
  652. */
  653. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  654. /*
  655. * On 5211+ read activation -> rx delay
  656. * and use it.
  657. */
  658. if (ah->ah_version != AR5K_AR5210) {
  659. data = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  660. AR5K_PHY_RX_DELAY_M;
  661. data = (channel->hw_value & CHANNEL_CCK) ?
  662. ((data << 2) / 22) : (data / 10);
  663. udelay(100 + (2 * data));
  664. data = 0;
  665. } else {
  666. mdelay(1);
  667. }
  668. /*
  669. * Perform ADC test (?)
  670. */
  671. data = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  672. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  673. for (i = 0; i <= 20; i++) {
  674. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  675. break;
  676. udelay(200);
  677. }
  678. ath5k_hw_reg_write(ah, data, AR5K_PHY_TST1);
  679. data = 0;
  680. /*
  681. * Start automatic gain calibration
  682. *
  683. * During AGC calibration RX path is re-routed to
  684. * a signal detector so we don't receive anything.
  685. *
  686. * This method is used to calibrate some static offsets
  687. * used together with on-the fly I/Q calibration (the
  688. * one performed via ath5k_hw_phy_calibrate), that doesn't
  689. * interrupt rx path.
  690. *
  691. * If we are in a noisy environment AGC calibration may time
  692. * out.
  693. */
  694. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  695. AR5K_PHY_AGCCTL_CAL);
  696. /* At the same time start I/Q calibration for QAM constellation
  697. * -no need for CCK- */
  698. ah->ah_calibration = false;
  699. if (!(mode == AR5K_MODE_11B)) {
  700. ah->ah_calibration = true;
  701. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  702. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  703. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  704. AR5K_PHY_IQ_RUN);
  705. }
  706. /* Wait for gain calibration to finish (we check for I/Q calibration
  707. * during ath5k_phy_calibrate) */
  708. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  709. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  710. ATH5K_ERR(ah->ah_sc, "gain calibration timeout (%uMHz)\n",
  711. channel->center_freq);
  712. return -EAGAIN;
  713. }
  714. /*
  715. * Start noise floor calibration
  716. *
  717. * If we run NF calibration before AGC, it always times out.
  718. * Binary HAL starts NF and AGC calibration at the same time
  719. * and only waits for AGC to finish. I believe that's wrong because
  720. * during NF calibration, rx path is also routed to a detector, so if
  721. * it doesn't finish we won't have RX.
  722. *
  723. * XXX: Find an interval that's OK for all cards...
  724. */
  725. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  726. /*
  727. * Reset queues and start beacon timers at the end of the reset routine
  728. */
  729. for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
  730. /*No QCU on 5210*/
  731. if (ah->ah_version != AR5K_AR5210)
  732. AR5K_REG_WRITE_Q(ah, AR5K_QUEUE_QCUMASK(i), i);
  733. ret = ath5k_hw_reset_tx_queue(ah, i);
  734. if (ret) {
  735. ATH5K_ERR(ah->ah_sc,
  736. "failed to reset TX queue #%d\n", i);
  737. return ret;
  738. }
  739. }
  740. /* Pre-enable interrupts on 5211/5212*/
  741. if (ah->ah_version != AR5K_AR5210)
  742. ath5k_hw_set_imr(ah, ah->ah_imr);
  743. /*
  744. * Set RF kill flags if supported by the device (read from the EEPROM)
  745. * Disable gpio_intr for now since it results system hang.
  746. * TODO: Handle this in ath5k_intr
  747. */
  748. #if 0
  749. if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
  750. ath5k_hw_set_gpio_input(ah, 0);
  751. ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
  752. if (ah->ah_gpio[0] == 0)
  753. ath5k_hw_set_gpio_intr(ah, 0, 1);
  754. else
  755. ath5k_hw_set_gpio_intr(ah, 0, 0);
  756. }
  757. #endif
  758. /*
  759. * Set the 32MHz reference clock on 5212 phy clock sleep register
  760. *
  761. * TODO: Find out how to switch to external 32Khz clock to save power
  762. */
  763. if (ah->ah_version == AR5K_AR5212) {
  764. ath5k_hw_reg_write(ah, AR5K_PHY_SCR_32MHZ, AR5K_PHY_SCR);
  765. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  766. ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ, AR5K_PHY_SCAL);
  767. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  768. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  769. ath5k_hw_reg_write(ah, ah->ah_phy_spending, AR5K_PHY_SPENDING);
  770. data = ath5k_hw_reg_read(ah, AR5K_USEC_5211) & 0xffffc07f ;
  771. data |= (ah->ah_phy_spending == AR5K_PHY_SPENDING_18) ?
  772. 0x00000f80 : 0x00001380 ;
  773. ath5k_hw_reg_write(ah, data, AR5K_USEC_5211);
  774. data = 0;
  775. }
  776. if (ah->ah_version == AR5K_AR5212) {
  777. ath5k_hw_reg_write(ah, 0x000100aa, 0x8118);
  778. ath5k_hw_reg_write(ah, 0x00003210, 0x811c);
  779. ath5k_hw_reg_write(ah, 0x00000052, 0x8108);
  780. if (ah->ah_mac_srev >= AR5K_SREV_AR2413)
  781. ath5k_hw_reg_write(ah, 0x00000004, 0x8120);
  782. }
  783. /*
  784. * Disable beacons and reset the register
  785. */
  786. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
  787. AR5K_BEACON_RESET_TSF);
  788. return 0;
  789. }
  790. #undef _ATH5K_RESET