reset.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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 <linux/platform_device.h>
  28. #include "ath5k.h"
  29. #include "reg.h"
  30. #include "base.h"
  31. #include "debug.h"
  32. /******************\
  33. * Helper functions *
  34. \******************/
  35. /*
  36. * Check if a register write has been completed
  37. */
  38. int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
  39. bool is_set)
  40. {
  41. int i;
  42. u32 data;
  43. for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
  44. data = ath5k_hw_reg_read(ah, reg);
  45. if (is_set && (data & flag))
  46. break;
  47. else if ((data & flag) == val)
  48. break;
  49. udelay(15);
  50. }
  51. return (i <= 0) ? -EAGAIN : 0;
  52. }
  53. /*************************\
  54. * Clock related functions *
  55. \*************************/
  56. /**
  57. * ath5k_hw_htoclock - Translate usec to hw clock units
  58. *
  59. * @ah: The &struct ath5k_hw
  60. * @usec: value in microseconds
  61. */
  62. unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
  63. {
  64. struct ath_common *common = ath5k_hw_common(ah);
  65. return usec * common->clockrate;
  66. }
  67. /**
  68. * ath5k_hw_clocktoh - Translate hw clock units to usec
  69. * @clock: value in hw clock units
  70. */
  71. unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
  72. {
  73. struct ath_common *common = ath5k_hw_common(ah);
  74. return clock / common->clockrate;
  75. }
  76. /**
  77. * ath5k_hw_init_core_clock - Initialize core clock
  78. *
  79. * @ah The &struct ath5k_hw
  80. *
  81. * Initialize core clock parameters (usec, usec32, latencies etc).
  82. */
  83. static void ath5k_hw_init_core_clock(struct ath5k_hw *ah)
  84. {
  85. struct ieee80211_channel *channel = ah->ah_current_channel;
  86. struct ath_common *common = ath5k_hw_common(ah);
  87. u32 usec_reg, txlat, rxlat, usec, clock, sclock, txf2txs;
  88. /*
  89. * Set core clock frequency
  90. */
  91. if (channel->hw_value & CHANNEL_5GHZ)
  92. clock = 40; /* 802.11a */
  93. else if (channel->hw_value & CHANNEL_CCK)
  94. clock = 22; /* 802.11b */
  95. else
  96. clock = 44; /* 802.11g */
  97. /* Use clock multiplier for non-default
  98. * bwmode */
  99. switch (ah->ah_bwmode) {
  100. case AR5K_BWMODE_40MHZ:
  101. clock *= 2;
  102. break;
  103. case AR5K_BWMODE_10MHZ:
  104. clock /= 2;
  105. break;
  106. case AR5K_BWMODE_5MHZ:
  107. clock /= 4;
  108. break;
  109. default:
  110. break;
  111. }
  112. common->clockrate = clock;
  113. /*
  114. * Set USEC parameters
  115. */
  116. /* Set USEC counter on PCU*/
  117. usec = clock - 1;
  118. usec = AR5K_REG_SM(usec, AR5K_USEC_1);
  119. /* Set usec duration on DCU */
  120. if (ah->ah_version != AR5K_AR5210)
  121. AR5K_REG_WRITE_BITS(ah, AR5K_DCU_GBL_IFS_MISC,
  122. AR5K_DCU_GBL_IFS_MISC_USEC_DUR,
  123. clock);
  124. /* Set 32MHz USEC counter */
  125. if ((ah->ah_radio == AR5K_RF5112) ||
  126. (ah->ah_radio == AR5K_RF5413) ||
  127. (ah->ah_radio == AR5K_RF2316) ||
  128. (ah->ah_radio == AR5K_RF2317))
  129. /* Remain on 40MHz clock ? */
  130. sclock = 40 - 1;
  131. else
  132. sclock = 32 - 1;
  133. sclock = AR5K_REG_SM(sclock, AR5K_USEC_32);
  134. /*
  135. * Set tx/rx latencies
  136. */
  137. usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
  138. txlat = AR5K_REG_MS(usec_reg, AR5K_USEC_TX_LATENCY_5211);
  139. rxlat = AR5K_REG_MS(usec_reg, AR5K_USEC_RX_LATENCY_5211);
  140. /*
  141. * 5210 initvals don't include usec settings
  142. * so we need to use magic values here for
  143. * tx/rx latencies
  144. */
  145. if (ah->ah_version == AR5K_AR5210) {
  146. /* same for turbo */
  147. txlat = AR5K_INIT_TX_LATENCY_5210;
  148. rxlat = AR5K_INIT_RX_LATENCY_5210;
  149. }
  150. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  151. /* 5311 has different tx/rx latency masks
  152. * from 5211, since we deal 5311 the same
  153. * as 5211 when setting initvals, shift
  154. * values here to their proper locations
  155. *
  156. * Note: Initvals indicate tx/rx/ latencies
  157. * are the same for turbo mode */
  158. txlat = AR5K_REG_SM(txlat, AR5K_USEC_TX_LATENCY_5210);
  159. rxlat = AR5K_REG_SM(rxlat, AR5K_USEC_RX_LATENCY_5210);
  160. } else
  161. switch (ah->ah_bwmode) {
  162. case AR5K_BWMODE_10MHZ:
  163. txlat = AR5K_REG_SM(txlat * 2,
  164. AR5K_USEC_TX_LATENCY_5211);
  165. rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
  166. AR5K_USEC_RX_LATENCY_5211);
  167. txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_10MHZ;
  168. break;
  169. case AR5K_BWMODE_5MHZ:
  170. txlat = AR5K_REG_SM(txlat * 4,
  171. AR5K_USEC_TX_LATENCY_5211);
  172. rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
  173. AR5K_USEC_RX_LATENCY_5211);
  174. txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_5MHZ;
  175. break;
  176. case AR5K_BWMODE_40MHZ:
  177. txlat = AR5K_INIT_TX_LAT_MIN;
  178. rxlat = AR5K_REG_SM(rxlat / 2,
  179. AR5K_USEC_RX_LATENCY_5211);
  180. txf2txs = AR5K_INIT_TXF2TXD_START_DEFAULT;
  181. break;
  182. default:
  183. break;
  184. }
  185. usec_reg = (usec | sclock | txlat | rxlat);
  186. ath5k_hw_reg_write(ah, usec_reg, AR5K_USEC);
  187. /* On 5112 set tx frane to tx data start delay */
  188. if (ah->ah_radio == AR5K_RF5112) {
  189. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL2,
  190. AR5K_PHY_RF_CTL2_TXF2TXD_START,
  191. txf2txs);
  192. }
  193. }
  194. /*
  195. * If there is an external 32KHz crystal available, use it
  196. * as ref. clock instead of 32/40MHz clock and baseband clocks
  197. * to save power during sleep or restore normal 32/40MHz
  198. * operation.
  199. *
  200. * XXX: When operating on 32KHz certain PHY registers (27 - 31,
  201. * 123 - 127) require delay on access.
  202. */
  203. static void ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
  204. {
  205. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  206. u32 scal, spending;
  207. /* Only set 32KHz settings if we have an external
  208. * 32KHz crystal present */
  209. if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
  210. AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
  211. enable) {
  212. /* 1 usec/cycle */
  213. AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
  214. /* Set up tsf increment on each cycle */
  215. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
  216. /* Set baseband sleep control registers
  217. * and sleep control rate */
  218. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  219. if ((ah->ah_radio == AR5K_RF5112) ||
  220. (ah->ah_radio == AR5K_RF5413) ||
  221. (ah->ah_radio == AR5K_RF2316) ||
  222. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  223. spending = 0x14;
  224. else
  225. spending = 0x18;
  226. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  227. if ((ah->ah_radio == AR5K_RF5112) ||
  228. (ah->ah_radio == AR5K_RF5413) ||
  229. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  230. ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
  231. ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
  232. ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
  233. ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
  234. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  235. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
  236. } else {
  237. ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
  238. ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
  239. ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
  240. ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
  241. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  242. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
  243. }
  244. /* Enable sleep clock operation */
  245. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
  246. AR5K_PCICFG_SLEEP_CLOCK_EN);
  247. } else {
  248. /* Disable sleep clock operation and
  249. * restore default parameters */
  250. AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
  251. AR5K_PCICFG_SLEEP_CLOCK_EN);
  252. AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
  253. AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
  254. /* Set DAC/ADC delays */
  255. ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
  256. ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
  257. if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
  258. scal = AR5K_PHY_SCAL_32MHZ_2417;
  259. else if (ee->ee_is_hb63)
  260. scal = AR5K_PHY_SCAL_32MHZ_HB63;
  261. else
  262. scal = AR5K_PHY_SCAL_32MHZ;
  263. ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
  264. ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
  265. ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
  266. if ((ah->ah_radio == AR5K_RF5112) ||
  267. (ah->ah_radio == AR5K_RF5413) ||
  268. (ah->ah_radio == AR5K_RF2316) ||
  269. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  270. spending = 0x14;
  271. else
  272. spending = 0x18;
  273. ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
  274. /* Set up tsf increment on each cycle */
  275. AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
  276. }
  277. }
  278. /*********************\
  279. * Reset/Sleep control *
  280. \*********************/
  281. /*
  282. * Reset chipset
  283. */
  284. static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
  285. {
  286. int ret;
  287. u32 mask = val ? val : ~0U;
  288. /* Read-and-clear RX Descriptor Pointer*/
  289. ath5k_hw_reg_read(ah, AR5K_RXDP);
  290. /*
  291. * Reset the device and wait until success
  292. */
  293. ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
  294. /* Wait at least 128 PCI clocks */
  295. udelay(15);
  296. if (ah->ah_version == AR5K_AR5210) {
  297. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  298. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  299. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
  300. | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
  301. } else {
  302. val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  303. mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
  304. }
  305. ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
  306. /*
  307. * Reset configuration register (for hw byte-swap). Note that this
  308. * is only set for big endian. We do the necessary magic in
  309. * AR5K_INIT_CFG.
  310. */
  311. if ((val & AR5K_RESET_CTL_PCU) == 0)
  312. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  313. return ret;
  314. }
  315. /*
  316. * Reset AHB chipset
  317. * AR5K_RESET_CTL_PCU flag resets WMAC
  318. * AR5K_RESET_CTL_BASEBAND flag resets WBB
  319. */
  320. static int ath5k_hw_wisoc_reset(struct ath5k_hw *ah, u32 flags)
  321. {
  322. u32 mask = flags ? flags : ~0U;
  323. volatile u32 *reg;
  324. u32 regval;
  325. u32 val = 0;
  326. /* ah->ah_mac_srev is not available at this point yet */
  327. if (ah->ah_sc->devid >= AR5K_SREV_AR2315_R6) {
  328. reg = (u32 *) AR5K_AR2315_RESET;
  329. if (mask & AR5K_RESET_CTL_PCU)
  330. val |= AR5K_AR2315_RESET_WMAC;
  331. if (mask & AR5K_RESET_CTL_BASEBAND)
  332. val |= AR5K_AR2315_RESET_BB_WARM;
  333. } else {
  334. reg = (u32 *) AR5K_AR5312_RESET;
  335. if (to_platform_device(ah->ah_sc->dev)->id == 0) {
  336. if (mask & AR5K_RESET_CTL_PCU)
  337. val |= AR5K_AR5312_RESET_WMAC0;
  338. if (mask & AR5K_RESET_CTL_BASEBAND)
  339. val |= AR5K_AR5312_RESET_BB0_COLD |
  340. AR5K_AR5312_RESET_BB0_WARM;
  341. } else {
  342. if (mask & AR5K_RESET_CTL_PCU)
  343. val |= AR5K_AR5312_RESET_WMAC1;
  344. if (mask & AR5K_RESET_CTL_BASEBAND)
  345. val |= AR5K_AR5312_RESET_BB1_COLD |
  346. AR5K_AR5312_RESET_BB1_WARM;
  347. }
  348. }
  349. /* Put BB/MAC into reset */
  350. regval = __raw_readl(reg);
  351. __raw_writel(regval | val, reg);
  352. regval = __raw_readl(reg);
  353. udelay(100);
  354. /* Bring BB/MAC out of reset */
  355. __raw_writel(regval & ~val, reg);
  356. regval = __raw_readl(reg);
  357. /*
  358. * Reset configuration register (for hw byte-swap). Note that this
  359. * is only set for big endian. We do the necessary magic in
  360. * AR5K_INIT_CFG.
  361. */
  362. if ((flags & AR5K_RESET_CTL_PCU) == 0)
  363. ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
  364. return 0;
  365. }
  366. /*
  367. * Sleep control
  368. */
  369. static int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
  370. bool set_chip, u16 sleep_duration)
  371. {
  372. unsigned int i;
  373. u32 staid, data;
  374. staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
  375. switch (mode) {
  376. case AR5K_PM_AUTO:
  377. staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
  378. /* fallthrough */
  379. case AR5K_PM_NETWORK_SLEEP:
  380. if (set_chip)
  381. ath5k_hw_reg_write(ah,
  382. AR5K_SLEEP_CTL_SLE_ALLOW |
  383. sleep_duration,
  384. AR5K_SLEEP_CTL);
  385. staid |= AR5K_STA_ID1_PWR_SV;
  386. break;
  387. case AR5K_PM_FULL_SLEEP:
  388. if (set_chip)
  389. ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
  390. AR5K_SLEEP_CTL);
  391. staid |= AR5K_STA_ID1_PWR_SV;
  392. break;
  393. case AR5K_PM_AWAKE:
  394. staid &= ~AR5K_STA_ID1_PWR_SV;
  395. if (!set_chip)
  396. goto commit;
  397. data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
  398. /* If card is down we 'll get 0xffff... so we
  399. * need to clean this up before we write the register
  400. */
  401. if (data & 0xffc00000)
  402. data = 0;
  403. else
  404. /* Preserve sleep duration etc */
  405. data = data & ~AR5K_SLEEP_CTL_SLE;
  406. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  407. AR5K_SLEEP_CTL);
  408. udelay(15);
  409. for (i = 200; i > 0; i--) {
  410. /* Check if the chip did wake up */
  411. if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  412. AR5K_PCICFG_SPWR_DN) == 0)
  413. break;
  414. /* Wait a bit and retry */
  415. udelay(50);
  416. ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
  417. AR5K_SLEEP_CTL);
  418. }
  419. /* Fail if the chip didn't wake up */
  420. if (i == 0)
  421. return -EIO;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. commit:
  427. ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
  428. return 0;
  429. }
  430. /*
  431. * Put device on hold
  432. *
  433. * Put MAC and Baseband on warm reset and
  434. * keep that state (don't clean sleep control
  435. * register). After this MAC and Baseband are
  436. * disabled and a full reset is needed to come
  437. * back. This way we save as much power as possible
  438. * without putting the card on full sleep.
  439. */
  440. int ath5k_hw_on_hold(struct ath5k_hw *ah)
  441. {
  442. struct pci_dev *pdev = ah->ah_sc->pdev;
  443. u32 bus_flags;
  444. int ret;
  445. if (ath5k_get_bus_type(ah) == ATH_AHB)
  446. return 0;
  447. /* Make sure device is awake */
  448. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  449. if (ret) {
  450. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  451. return ret;
  452. }
  453. /*
  454. * Put chipset on warm reset...
  455. *
  456. * Note: putting PCI core on warm reset on PCI-E cards
  457. * results card to hang and always return 0xffff... so
  458. * we ingore that flag for PCI-E cards. On PCI cards
  459. * this flag gets cleared after 64 PCI clocks.
  460. */
  461. bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
  462. if (ah->ah_version == AR5K_AR5210) {
  463. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  464. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  465. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  466. mdelay(2);
  467. } else {
  468. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  469. AR5K_RESET_CTL_BASEBAND | bus_flags);
  470. }
  471. if (ret) {
  472. ATH5K_ERR(ah->ah_sc, "failed to put device on warm reset\n");
  473. return -EIO;
  474. }
  475. /* ...wakeup again!*/
  476. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  477. if (ret) {
  478. ATH5K_ERR(ah->ah_sc, "failed to put device on hold\n");
  479. return ret;
  480. }
  481. return ret;
  482. }
  483. /*
  484. * Bring up MAC + PHY Chips and program PLL
  485. */
  486. int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
  487. {
  488. struct pci_dev *pdev = ah->ah_sc->pdev;
  489. u32 turbo, mode, clock, bus_flags;
  490. int ret;
  491. turbo = 0;
  492. mode = 0;
  493. clock = 0;
  494. if ((ath5k_get_bus_type(ah) != ATH_AHB) || !initial) {
  495. /* Wakeup the device */
  496. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  497. if (ret) {
  498. ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
  499. return ret;
  500. }
  501. }
  502. /*
  503. * Put chipset on warm reset...
  504. *
  505. * Note: putting PCI core on warm reset on PCI-E cards
  506. * results card to hang and always return 0xffff... so
  507. * we ingore that flag for PCI-E cards. On PCI cards
  508. * this flag gets cleared after 64 PCI clocks.
  509. */
  510. bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
  511. if (ah->ah_version == AR5K_AR5210) {
  512. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  513. AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
  514. AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
  515. mdelay(2);
  516. } else {
  517. if (ath5k_get_bus_type(ah) == ATH_AHB)
  518. ret = ath5k_hw_wisoc_reset(ah, AR5K_RESET_CTL_PCU |
  519. AR5K_RESET_CTL_BASEBAND);
  520. else
  521. ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
  522. AR5K_RESET_CTL_BASEBAND | bus_flags);
  523. }
  524. if (ret) {
  525. ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip\n");
  526. return -EIO;
  527. }
  528. /* ...wakeup again!...*/
  529. ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
  530. if (ret) {
  531. ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
  532. return ret;
  533. }
  534. /* ...reset configuration regiter on Wisoc ...
  535. * ...clear reset control register and pull device out of
  536. * warm reset on others */
  537. if (ath5k_get_bus_type(ah) == ATH_AHB)
  538. ret = ath5k_hw_wisoc_reset(ah, 0);
  539. else
  540. ret = ath5k_hw_nic_reset(ah, 0);
  541. if (ret) {
  542. ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
  543. return -EIO;
  544. }
  545. /* On initialization skip PLL programming since we don't have
  546. * a channel / mode set yet */
  547. if (initial)
  548. return 0;
  549. if (ah->ah_version != AR5K_AR5210) {
  550. /*
  551. * Get channel mode flags
  552. */
  553. if (ah->ah_radio >= AR5K_RF5112) {
  554. mode = AR5K_PHY_MODE_RAD_RF5112;
  555. clock = AR5K_PHY_PLL_RF5112;
  556. } else {
  557. mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
  558. clock = AR5K_PHY_PLL_RF5111; /*Zero*/
  559. }
  560. if (flags & CHANNEL_2GHZ) {
  561. mode |= AR5K_PHY_MODE_FREQ_2GHZ;
  562. clock |= AR5K_PHY_PLL_44MHZ;
  563. if (flags & CHANNEL_CCK) {
  564. mode |= AR5K_PHY_MODE_MOD_CCK;
  565. } else if (flags & CHANNEL_OFDM) {
  566. /* XXX Dynamic OFDM/CCK is not supported by the
  567. * AR5211 so we set MOD_OFDM for plain g (no
  568. * CCK headers) operation. We need to test
  569. * this, 5211 might support ofdm-only g after
  570. * all, there are also initial register values
  571. * in the code for g mode (see initvals.c).
  572. */
  573. if (ah->ah_version == AR5K_AR5211)
  574. mode |= AR5K_PHY_MODE_MOD_OFDM;
  575. else
  576. mode |= AR5K_PHY_MODE_MOD_DYN;
  577. } else {
  578. ATH5K_ERR(ah->ah_sc,
  579. "invalid radio modulation mode\n");
  580. return -EINVAL;
  581. }
  582. } else if (flags & CHANNEL_5GHZ) {
  583. mode |= AR5K_PHY_MODE_FREQ_5GHZ;
  584. /* Different PLL setting for 5413 */
  585. if (ah->ah_radio == AR5K_RF5413)
  586. clock = AR5K_PHY_PLL_40MHZ_5413;
  587. else
  588. clock |= AR5K_PHY_PLL_40MHZ;
  589. if (flags & CHANNEL_OFDM)
  590. mode |= AR5K_PHY_MODE_MOD_OFDM;
  591. else {
  592. ATH5K_ERR(ah->ah_sc,
  593. "invalid radio modulation mode\n");
  594. return -EINVAL;
  595. }
  596. } else {
  597. ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
  598. return -EINVAL;
  599. }
  600. /*XXX: Can bwmode be used with dynamic mode ?
  601. * (I don't think it supports 44MHz) */
  602. /* On 2425 initvals TURBO_SHORT is not pressent */
  603. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
  604. turbo = AR5K_PHY_TURBO_MODE |
  605. (ah->ah_radio == AR5K_RF2425) ? 0 :
  606. AR5K_PHY_TURBO_SHORT;
  607. } else if (ah->ah_bwmode != AR5K_BWMODE_DEFAULT) {
  608. if (ah->ah_radio == AR5K_RF5413) {
  609. mode |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
  610. AR5K_PHY_MODE_HALF_RATE :
  611. AR5K_PHY_MODE_QUARTER_RATE;
  612. } else if (ah->ah_version == AR5K_AR5212) {
  613. clock |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
  614. AR5K_PHY_PLL_HALF_RATE :
  615. AR5K_PHY_PLL_QUARTER_RATE;
  616. }
  617. }
  618. } else { /* Reset the device */
  619. /* ...enable Atheros turbo mode if requested */
  620. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  621. ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
  622. AR5K_PHY_TURBO);
  623. }
  624. if (ah->ah_version != AR5K_AR5210) {
  625. /* ...update PLL if needed */
  626. if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
  627. ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
  628. udelay(300);
  629. }
  630. /* ...set the PHY operating mode */
  631. ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
  632. ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
  633. }
  634. return 0;
  635. }
  636. /**************************************\
  637. * Post-initvals register modifications *
  638. \**************************************/
  639. /* TODO: Half/Quarter rate */
  640. static void ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
  641. struct ieee80211_channel *channel)
  642. {
  643. if (ah->ah_version == AR5K_AR5212 &&
  644. ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  645. /* Setup ADC control */
  646. ath5k_hw_reg_write(ah,
  647. (AR5K_REG_SM(2,
  648. AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
  649. AR5K_REG_SM(2,
  650. AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
  651. AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
  652. AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
  653. AR5K_PHY_ADC_CTL);
  654. /* Disable barker RSSI threshold */
  655. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  656. AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
  657. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
  658. AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
  659. /* Set the mute mask */
  660. ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
  661. }
  662. /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
  663. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
  664. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
  665. /* Enable DCU double buffering */
  666. if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
  667. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  668. AR5K_TXCFG_DCU_DBL_BUF_DIS);
  669. /* Set fast ADC */
  670. if ((ah->ah_radio == AR5K_RF5413) ||
  671. (ah->ah_radio == AR5K_RF2317) ||
  672. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
  673. u32 fast_adc = true;
  674. if (channel->center_freq == 2462 ||
  675. channel->center_freq == 2467)
  676. fast_adc = 0;
  677. /* Only update if needed */
  678. if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
  679. ath5k_hw_reg_write(ah, fast_adc,
  680. AR5K_PHY_FAST_ADC);
  681. }
  682. /* Fix for first revision of the RF5112 RF chipset */
  683. if (ah->ah_radio == AR5K_RF5112 &&
  684. ah->ah_radio_5ghz_revision <
  685. AR5K_SREV_RAD_5112A) {
  686. u32 data;
  687. ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
  688. AR5K_PHY_CCKTXCTL);
  689. if (channel->hw_value & CHANNEL_5GHZ)
  690. data = 0xffb81020;
  691. else
  692. data = 0xffb80d20;
  693. ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
  694. }
  695. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  696. /* Clear QCU/DCU clock gating register */
  697. ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
  698. /* Set DAC/ADC delays */
  699. ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ_5311,
  700. AR5K_PHY_SCAL);
  701. /* Enable PCU FIFO corruption ECO */
  702. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
  703. AR5K_DIAG_SW_ECO_ENABLE);
  704. }
  705. if (ah->ah_bwmode) {
  706. /* Increase PHY switch and AGC settling time
  707. * on turbo mode (ath5k_hw_commit_eeprom_settings
  708. * will override settling time if available) */
  709. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
  710. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  711. AR5K_PHY_SETTLING_AGC,
  712. AR5K_AGC_SETTLING_TURBO);
  713. /* XXX: Initvals indicate we only increase
  714. * switch time on AR5212, 5211 and 5210
  715. * only change agc time (bug?) */
  716. if (ah->ah_version == AR5K_AR5212)
  717. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  718. AR5K_PHY_SETTLING_SWITCH,
  719. AR5K_SWITCH_SETTLING_TURBO);
  720. if (ah->ah_version == AR5K_AR5210) {
  721. /* Set Frame Control Register */
  722. ath5k_hw_reg_write(ah,
  723. (AR5K_PHY_FRAME_CTL_INI |
  724. AR5K_PHY_TURBO_MODE |
  725. AR5K_PHY_TURBO_SHORT | 0x2020),
  726. AR5K_PHY_FRAME_CTL_5210);
  727. }
  728. /* On 5413 PHY force window length for half/quarter rate*/
  729. } else if ((ah->ah_mac_srev >= AR5K_SREV_AR5424) &&
  730. (ah->ah_mac_srev <= AR5K_SREV_AR5414)) {
  731. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL_5211,
  732. AR5K_PHY_FRAME_CTL_WIN_LEN,
  733. 3);
  734. }
  735. } else if (ah->ah_version == AR5K_AR5210) {
  736. /* Set Frame Control Register for normal operation */
  737. ath5k_hw_reg_write(ah, (AR5K_PHY_FRAME_CTL_INI | 0x1020),
  738. AR5K_PHY_FRAME_CTL_5210);
  739. }
  740. }
  741. static void ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
  742. struct ieee80211_channel *channel)
  743. {
  744. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  745. s16 cck_ofdm_pwr_delta;
  746. u8 ee_mode;
  747. /* TODO: Add support for AR5210 EEPROM */
  748. if (ah->ah_version == AR5K_AR5210)
  749. return;
  750. ee_mode = ath5k_eeprom_mode_from_channel(channel);
  751. /* Adjust power delta for channel 14 */
  752. if (channel->center_freq == 2484)
  753. cck_ofdm_pwr_delta =
  754. ((ee->ee_cck_ofdm_power_delta -
  755. ee->ee_scaled_cck_delta) * 2) / 10;
  756. else
  757. cck_ofdm_pwr_delta =
  758. (ee->ee_cck_ofdm_power_delta * 2) / 10;
  759. /* Set CCK to OFDM power delta on tx power
  760. * adjustment register */
  761. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  762. if (channel->hw_value == CHANNEL_G)
  763. ath5k_hw_reg_write(ah,
  764. AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
  765. AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
  766. AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
  767. AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
  768. AR5K_PHY_TX_PWR_ADJ);
  769. else
  770. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
  771. } else {
  772. /* For older revs we scale power on sw during tx power
  773. * setup */
  774. ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
  775. ah->ah_txpower.txp_cck_ofdm_gainf_delta =
  776. ee->ee_cck_ofdm_gain_delta;
  777. }
  778. /* XXX: necessary here? is called from ath5k_hw_set_antenna_mode()
  779. * too */
  780. ath5k_hw_set_antenna_switch(ah, ee_mode);
  781. /* Noise floor threshold */
  782. ath5k_hw_reg_write(ah,
  783. AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
  784. AR5K_PHY_NFTHRES);
  785. if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) &&
  786. (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
  787. /* Switch settling time (Turbo) */
  788. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  789. AR5K_PHY_SETTLING_SWITCH,
  790. ee->ee_switch_settling_turbo[ee_mode]);
  791. /* Tx/Rx attenuation (Turbo) */
  792. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  793. AR5K_PHY_GAIN_TXRX_ATTEN,
  794. ee->ee_atn_tx_rx_turbo[ee_mode]);
  795. /* ADC/PGA desired size (Turbo) */
  796. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  797. AR5K_PHY_DESIRED_SIZE_ADC,
  798. ee->ee_adc_desired_size_turbo[ee_mode]);
  799. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  800. AR5K_PHY_DESIRED_SIZE_PGA,
  801. ee->ee_pga_desired_size_turbo[ee_mode]);
  802. /* Tx/Rx margin (Turbo) */
  803. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  804. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  805. ee->ee_margin_tx_rx_turbo[ee_mode]);
  806. } else {
  807. /* Switch settling time */
  808. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
  809. AR5K_PHY_SETTLING_SWITCH,
  810. ee->ee_switch_settling[ee_mode]);
  811. /* Tx/Rx attenuation */
  812. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
  813. AR5K_PHY_GAIN_TXRX_ATTEN,
  814. ee->ee_atn_tx_rx[ee_mode]);
  815. /* ADC/PGA desired size */
  816. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  817. AR5K_PHY_DESIRED_SIZE_ADC,
  818. ee->ee_adc_desired_size[ee_mode]);
  819. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
  820. AR5K_PHY_DESIRED_SIZE_PGA,
  821. ee->ee_pga_desired_size[ee_mode]);
  822. /* Tx/Rx margin */
  823. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  824. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
  825. AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
  826. ee->ee_margin_tx_rx[ee_mode]);
  827. }
  828. /* XPA delays */
  829. ath5k_hw_reg_write(ah,
  830. (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
  831. (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
  832. (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
  833. (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
  834. /* XLNA delay */
  835. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
  836. AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
  837. ee->ee_tx_end2xlna_enable[ee_mode]);
  838. /* Thresh64 (ANI) */
  839. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
  840. AR5K_PHY_NF_THRESH62,
  841. ee->ee_thr_62[ee_mode]);
  842. /* False detect backoff for channels
  843. * that have spur noise. Write the new
  844. * cyclic power RSSI threshold. */
  845. if (ath5k_hw_chan_has_spur_noise(ah, channel))
  846. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  847. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  848. AR5K_INIT_CYCRSSI_THR1 +
  849. ee->ee_false_detect[ee_mode]);
  850. else
  851. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
  852. AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
  853. AR5K_INIT_CYCRSSI_THR1);
  854. /* I/Q correction (set enable bit last to match HAL sources) */
  855. /* TODO: Per channel i/q infos ? */
  856. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  857. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
  858. ee->ee_i_cal[ee_mode]);
  859. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
  860. ee->ee_q_cal[ee_mode]);
  861. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  862. }
  863. /* Heavy clipping -disable for now */
  864. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
  865. ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
  866. }
  867. /*********************\
  868. * Main reset function *
  869. \*********************/
  870. int ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
  871. struct ieee80211_channel *channel, bool fast, bool skip_pcu)
  872. {
  873. u32 s_seq[10], s_led[3], tsf_up, tsf_lo;
  874. u8 mode;
  875. int i, ret;
  876. tsf_up = 0;
  877. tsf_lo = 0;
  878. mode = 0;
  879. /*
  880. * Sanity check for fast flag
  881. * Fast channel change only available
  882. * on AR2413/AR5413.
  883. */
  884. if (fast && (ah->ah_radio != AR5K_RF2413) &&
  885. (ah->ah_radio != AR5K_RF5413))
  886. fast = 0;
  887. /* Disable sleep clock operation
  888. * to avoid register access delay on certain
  889. * PHY registers */
  890. if (ah->ah_version == AR5K_AR5212)
  891. ath5k_hw_set_sleep_clock(ah, false);
  892. /*
  893. * Stop PCU
  894. */
  895. ath5k_hw_stop_rx_pcu(ah);
  896. /*
  897. * Stop DMA
  898. *
  899. * Note: If DMA didn't stop continue
  900. * since only a reset will fix it.
  901. */
  902. ret = ath5k_hw_dma_stop(ah);
  903. /* RF Bus grant won't work if we have pending
  904. * frames */
  905. if (ret && fast) {
  906. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_RESET,
  907. "DMA didn't stop, falling back to normal reset\n");
  908. fast = 0;
  909. /* Non fatal, just continue with
  910. * normal reset */
  911. ret = 0;
  912. }
  913. switch (channel->hw_value & CHANNEL_MODES) {
  914. case CHANNEL_A:
  915. mode = AR5K_MODE_11A;
  916. break;
  917. case CHANNEL_G:
  918. if (ah->ah_version <= AR5K_AR5211) {
  919. ATH5K_ERR(ah->ah_sc,
  920. "G mode not available on 5210/5211");
  921. return -EINVAL;
  922. }
  923. mode = AR5K_MODE_11G;
  924. break;
  925. case CHANNEL_B:
  926. if (ah->ah_version < AR5K_AR5211) {
  927. ATH5K_ERR(ah->ah_sc,
  928. "B mode not available on 5210");
  929. return -EINVAL;
  930. }
  931. mode = AR5K_MODE_11B;
  932. break;
  933. case CHANNEL_XR:
  934. if (ah->ah_version == AR5K_AR5211) {
  935. ATH5K_ERR(ah->ah_sc,
  936. "XR mode not available on 5211");
  937. return -EINVAL;
  938. }
  939. mode = AR5K_MODE_XR;
  940. break;
  941. default:
  942. ATH5K_ERR(ah->ah_sc,
  943. "invalid channel: %d\n", channel->center_freq);
  944. return -EINVAL;
  945. }
  946. /*
  947. * If driver requested fast channel change and DMA has stopped
  948. * go on. If it fails continue with a normal reset.
  949. */
  950. if (fast) {
  951. ret = ath5k_hw_phy_init(ah, channel, mode, true);
  952. if (ret) {
  953. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_RESET,
  954. "fast chan change failed, falling back to normal reset\n");
  955. /* Non fatal, can happen eg.
  956. * on mode change */
  957. ret = 0;
  958. } else
  959. return 0;
  960. }
  961. /*
  962. * Save some registers before a reset
  963. */
  964. if (ah->ah_version != AR5K_AR5210) {
  965. /*
  966. * Save frame sequence count
  967. * For revs. after Oahu, only save
  968. * seq num for DCU 0 (Global seq num)
  969. */
  970. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  971. for (i = 0; i < 10; i++)
  972. s_seq[i] = ath5k_hw_reg_read(ah,
  973. AR5K_QUEUE_DCU_SEQNUM(i));
  974. } else {
  975. s_seq[0] = ath5k_hw_reg_read(ah,
  976. AR5K_QUEUE_DCU_SEQNUM(0));
  977. }
  978. /* TSF accelerates on AR5211 during reset
  979. * As a workaround save it here and restore
  980. * it later so that it's back in time after
  981. * reset. This way it'll get re-synced on the
  982. * next beacon without breaking ad-hoc.
  983. *
  984. * On AR5212 TSF is almost preserved across a
  985. * reset so it stays back in time anyway and
  986. * we don't have to save/restore it.
  987. *
  988. * XXX: Since this breaks power saving we have
  989. * to disable power saving until we receive the
  990. * next beacon, so we can resync beacon timers */
  991. if (ah->ah_version == AR5K_AR5211) {
  992. tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  993. tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  994. }
  995. }
  996. /*GPIOs*/
  997. s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
  998. AR5K_PCICFG_LEDSTATE;
  999. s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
  1000. s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
  1001. /*
  1002. * Since we are going to write rf buffer
  1003. * check if we have any pending gain_F
  1004. * optimization settings
  1005. */
  1006. if (ah->ah_version == AR5K_AR5212 &&
  1007. (ah->ah_radio <= AR5K_RF5112)) {
  1008. if (!fast && ah->ah_rf_banks != NULL)
  1009. ath5k_hw_gainf_calibrate(ah);
  1010. }
  1011. /* Wakeup the device */
  1012. ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
  1013. if (ret)
  1014. return ret;
  1015. /* PHY access enable */
  1016. if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
  1017. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1018. else
  1019. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
  1020. AR5K_PHY(0));
  1021. /* Write initial settings */
  1022. ret = ath5k_hw_write_initvals(ah, mode, skip_pcu);
  1023. if (ret)
  1024. return ret;
  1025. /* Initialize core clock settings */
  1026. ath5k_hw_init_core_clock(ah);
  1027. /*
  1028. * Tweak initval settings for revised
  1029. * chipsets and add some more config
  1030. * bits
  1031. */
  1032. ath5k_hw_tweak_initval_settings(ah, channel);
  1033. /* Commit values from EEPROM */
  1034. ath5k_hw_commit_eeprom_settings(ah, channel);
  1035. /*
  1036. * Restore saved values
  1037. */
  1038. /* Seqnum, TSF */
  1039. if (ah->ah_version != AR5K_AR5210) {
  1040. if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
  1041. for (i = 0; i < 10; i++)
  1042. ath5k_hw_reg_write(ah, s_seq[i],
  1043. AR5K_QUEUE_DCU_SEQNUM(i));
  1044. } else {
  1045. ath5k_hw_reg_write(ah, s_seq[0],
  1046. AR5K_QUEUE_DCU_SEQNUM(0));
  1047. }
  1048. if (ah->ah_version == AR5K_AR5211) {
  1049. ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
  1050. ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
  1051. }
  1052. }
  1053. /* Ledstate */
  1054. AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
  1055. /* Gpio settings */
  1056. ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
  1057. ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
  1058. /*
  1059. * Initialize PCU
  1060. */
  1061. ath5k_hw_pcu_init(ah, op_mode, mode);
  1062. /*
  1063. * Initialize PHY
  1064. */
  1065. ret = ath5k_hw_phy_init(ah, channel, mode, false);
  1066. if (ret) {
  1067. ATH5K_ERR(ah->ah_sc,
  1068. "failed to initialize PHY (%i) !\n", ret);
  1069. return ret;
  1070. }
  1071. /*
  1072. * Configure QCUs/DCUs
  1073. */
  1074. ret = ath5k_hw_init_queues(ah);
  1075. if (ret)
  1076. return ret;
  1077. /*
  1078. * Initialize DMA/Interrupts
  1079. */
  1080. ath5k_hw_dma_init(ah);
  1081. /* Enable 32KHz clock function for AR5212+ chips
  1082. * Set clocks to 32KHz operation and use an
  1083. * external 32KHz crystal when sleeping if one
  1084. * exists */
  1085. if (ah->ah_version == AR5K_AR5212 &&
  1086. op_mode != NL80211_IFTYPE_AP)
  1087. ath5k_hw_set_sleep_clock(ah, true);
  1088. /*
  1089. * Disable beacons and reset the TSF
  1090. */
  1091. AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
  1092. ath5k_hw_reset_tsf(ah);
  1093. return 0;
  1094. }