reset.c 36 KB

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