reset.c 37 KB

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