reset.c 38 KB

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