reset.c 38 KB

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