reset.c 25 KB

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