reset.c 37 KB

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