hw.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. /*
  2. * Copyright (c) 2008-2010 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <asm/unaligned.h>
  19. #include "hw.h"
  20. #include "hw-ops.h"
  21. #include "rc.h"
  22. #include "ar9003_mac.h"
  23. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
  24. MODULE_AUTHOR("Atheros Communications");
  25. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  26. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  27. MODULE_LICENSE("Dual BSD/GPL");
  28. static int __init ath9k_init(void)
  29. {
  30. return 0;
  31. }
  32. module_init(ath9k_init);
  33. static void __exit ath9k_exit(void)
  34. {
  35. return;
  36. }
  37. module_exit(ath9k_exit);
  38. /* Private hardware callbacks */
  39. static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
  40. {
  41. ath9k_hw_private_ops(ah)->init_cal_settings(ah);
  42. }
  43. static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
  44. {
  45. ath9k_hw_private_ops(ah)->init_mode_regs(ah);
  46. }
  47. static bool ath9k_hw_macversion_supported(struct ath_hw *ah)
  48. {
  49. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  50. return priv_ops->macversion_supported(ah->hw_version.macVersion);
  51. }
  52. static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
  53. struct ath9k_channel *chan)
  54. {
  55. return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
  56. }
  57. static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
  58. {
  59. if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
  60. return;
  61. ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
  62. }
  63. static void ath9k_hw_ani_cache_ini_regs(struct ath_hw *ah)
  64. {
  65. /* You will not have this callback if using the old ANI */
  66. if (!ath9k_hw_private_ops(ah)->ani_cache_ini_regs)
  67. return;
  68. ath9k_hw_private_ops(ah)->ani_cache_ini_regs(ah);
  69. }
  70. /********************/
  71. /* Helper Functions */
  72. /********************/
  73. static u32 ath9k_hw_mac_clks(struct ath_hw *ah, u32 usecs)
  74. {
  75. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  76. if (!ah->curchan) /* should really check for CCK instead */
  77. return usecs *ATH9K_CLOCK_RATE_CCK;
  78. if (conf->channel->band == IEEE80211_BAND_2GHZ)
  79. return usecs *ATH9K_CLOCK_RATE_2GHZ_OFDM;
  80. if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
  81. return usecs * ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
  82. else
  83. return usecs * ATH9K_CLOCK_RATE_5GHZ_OFDM;
  84. }
  85. static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
  86. {
  87. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  88. if (conf_is_ht40(conf))
  89. return ath9k_hw_mac_clks(ah, usecs) * 2;
  90. else
  91. return ath9k_hw_mac_clks(ah, usecs);
  92. }
  93. bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
  94. {
  95. int i;
  96. BUG_ON(timeout < AH_TIME_QUANTUM);
  97. for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
  98. if ((REG_READ(ah, reg) & mask) == val)
  99. return true;
  100. udelay(AH_TIME_QUANTUM);
  101. }
  102. ath_print(ath9k_hw_common(ah), ATH_DBG_ANY,
  103. "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
  104. timeout, reg, REG_READ(ah, reg), mask, val);
  105. return false;
  106. }
  107. EXPORT_SYMBOL(ath9k_hw_wait);
  108. u32 ath9k_hw_reverse_bits(u32 val, u32 n)
  109. {
  110. u32 retval;
  111. int i;
  112. for (i = 0, retval = 0; i < n; i++) {
  113. retval = (retval << 1) | (val & 1);
  114. val >>= 1;
  115. }
  116. return retval;
  117. }
  118. bool ath9k_get_channel_edges(struct ath_hw *ah,
  119. u16 flags, u16 *low,
  120. u16 *high)
  121. {
  122. struct ath9k_hw_capabilities *pCap = &ah->caps;
  123. if (flags & CHANNEL_5GHZ) {
  124. *low = pCap->low_5ghz_chan;
  125. *high = pCap->high_5ghz_chan;
  126. return true;
  127. }
  128. if ((flags & CHANNEL_2GHZ)) {
  129. *low = pCap->low_2ghz_chan;
  130. *high = pCap->high_2ghz_chan;
  131. return true;
  132. }
  133. return false;
  134. }
  135. u16 ath9k_hw_computetxtime(struct ath_hw *ah,
  136. u8 phy, int kbps,
  137. u32 frameLen, u16 rateix,
  138. bool shortPreamble)
  139. {
  140. u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
  141. if (kbps == 0)
  142. return 0;
  143. switch (phy) {
  144. case WLAN_RC_PHY_CCK:
  145. phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
  146. if (shortPreamble)
  147. phyTime >>= 1;
  148. numBits = frameLen << 3;
  149. txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
  150. break;
  151. case WLAN_RC_PHY_OFDM:
  152. if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
  153. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
  154. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  155. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  156. txTime = OFDM_SIFS_TIME_QUARTER
  157. + OFDM_PREAMBLE_TIME_QUARTER
  158. + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
  159. } else if (ah->curchan &&
  160. IS_CHAN_HALF_RATE(ah->curchan)) {
  161. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
  162. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  163. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  164. txTime = OFDM_SIFS_TIME_HALF +
  165. OFDM_PREAMBLE_TIME_HALF
  166. + (numSymbols * OFDM_SYMBOL_TIME_HALF);
  167. } else {
  168. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
  169. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  170. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  171. txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
  172. + (numSymbols * OFDM_SYMBOL_TIME);
  173. }
  174. break;
  175. default:
  176. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  177. "Unknown phy %u (rate ix %u)\n", phy, rateix);
  178. txTime = 0;
  179. break;
  180. }
  181. return txTime;
  182. }
  183. EXPORT_SYMBOL(ath9k_hw_computetxtime);
  184. void ath9k_hw_get_channel_centers(struct ath_hw *ah,
  185. struct ath9k_channel *chan,
  186. struct chan_centers *centers)
  187. {
  188. int8_t extoff;
  189. if (!IS_CHAN_HT40(chan)) {
  190. centers->ctl_center = centers->ext_center =
  191. centers->synth_center = chan->channel;
  192. return;
  193. }
  194. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  195. (chan->chanmode == CHANNEL_G_HT40PLUS)) {
  196. centers->synth_center =
  197. chan->channel + HT40_CHANNEL_CENTER_SHIFT;
  198. extoff = 1;
  199. } else {
  200. centers->synth_center =
  201. chan->channel - HT40_CHANNEL_CENTER_SHIFT;
  202. extoff = -1;
  203. }
  204. centers->ctl_center =
  205. centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
  206. /* 25 MHz spacing is supported by hw but not on upper layers */
  207. centers->ext_center =
  208. centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
  209. }
  210. /******************/
  211. /* Chip Revisions */
  212. /******************/
  213. static void ath9k_hw_read_revisions(struct ath_hw *ah)
  214. {
  215. u32 val;
  216. val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
  217. if (val == 0xFF) {
  218. val = REG_READ(ah, AR_SREV);
  219. ah->hw_version.macVersion =
  220. (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
  221. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  222. ah->is_pciexpress = (val & AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
  223. } else {
  224. if (!AR_SREV_9100(ah))
  225. ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
  226. ah->hw_version.macRev = val & AR_SREV_REVISION;
  227. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
  228. ah->is_pciexpress = true;
  229. }
  230. }
  231. /************************************/
  232. /* HW Attach, Detach, Init Routines */
  233. /************************************/
  234. static void ath9k_hw_disablepcie(struct ath_hw *ah)
  235. {
  236. if (AR_SREV_9100(ah))
  237. return;
  238. ENABLE_REGWRITE_BUFFER(ah);
  239. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  240. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  241. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
  242. REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
  243. REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
  244. REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
  245. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  246. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  247. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
  248. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  249. REGWRITE_BUFFER_FLUSH(ah);
  250. DISABLE_REGWRITE_BUFFER(ah);
  251. }
  252. /* This should work for all families including legacy */
  253. static bool ath9k_hw_chip_test(struct ath_hw *ah)
  254. {
  255. struct ath_common *common = ath9k_hw_common(ah);
  256. u32 regAddr[2] = { AR_STA_ID0 };
  257. u32 regHold[2];
  258. u32 patternData[4] = { 0x55555555,
  259. 0xaaaaaaaa,
  260. 0x66666666,
  261. 0x99999999 };
  262. int i, j, loop_max;
  263. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  264. loop_max = 2;
  265. regAddr[1] = AR_PHY_BASE + (8 << 2);
  266. } else
  267. loop_max = 1;
  268. for (i = 0; i < loop_max; i++) {
  269. u32 addr = regAddr[i];
  270. u32 wrData, rdData;
  271. regHold[i] = REG_READ(ah, addr);
  272. for (j = 0; j < 0x100; j++) {
  273. wrData = (j << 16) | j;
  274. REG_WRITE(ah, addr, wrData);
  275. rdData = REG_READ(ah, addr);
  276. if (rdData != wrData) {
  277. ath_print(common, ATH_DBG_FATAL,
  278. "address test failed "
  279. "addr: 0x%08x - wr:0x%08x != "
  280. "rd:0x%08x\n",
  281. addr, wrData, rdData);
  282. return false;
  283. }
  284. }
  285. for (j = 0; j < 4; j++) {
  286. wrData = patternData[j];
  287. REG_WRITE(ah, addr, wrData);
  288. rdData = REG_READ(ah, addr);
  289. if (wrData != rdData) {
  290. ath_print(common, ATH_DBG_FATAL,
  291. "address test failed "
  292. "addr: 0x%08x - wr:0x%08x != "
  293. "rd:0x%08x\n",
  294. addr, wrData, rdData);
  295. return false;
  296. }
  297. }
  298. REG_WRITE(ah, regAddr[i], regHold[i]);
  299. }
  300. udelay(100);
  301. return true;
  302. }
  303. static void ath9k_hw_init_config(struct ath_hw *ah)
  304. {
  305. int i;
  306. ah->config.dma_beacon_response_time = 2;
  307. ah->config.sw_beacon_response_time = 10;
  308. ah->config.additional_swba_backoff = 0;
  309. ah->config.ack_6mb = 0x0;
  310. ah->config.cwm_ignore_extcca = 0;
  311. ah->config.pcie_powersave_enable = 0;
  312. ah->config.pcie_clock_req = 0;
  313. ah->config.pcie_waen = 0;
  314. ah->config.analog_shiftreg = 1;
  315. ah->config.ofdm_trig_low = 200;
  316. ah->config.ofdm_trig_high = 500;
  317. ah->config.cck_trig_high = 200;
  318. ah->config.cck_trig_low = 100;
  319. ah->config.enable_ani = true;
  320. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  321. ah->config.spurchans[i][0] = AR_NO_SPUR;
  322. ah->config.spurchans[i][1] = AR_NO_SPUR;
  323. }
  324. if (ah->hw_version.devid != AR2427_DEVID_PCIE)
  325. ah->config.ht_enable = 1;
  326. else
  327. ah->config.ht_enable = 0;
  328. ah->config.rx_intr_mitigation = true;
  329. /*
  330. * We need this for PCI devices only (Cardbus, PCI, miniPCI)
  331. * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
  332. * This means we use it for all AR5416 devices, and the few
  333. * minor PCI AR9280 devices out there.
  334. *
  335. * Serialization is required because these devices do not handle
  336. * well the case of two concurrent reads/writes due to the latency
  337. * involved. During one read/write another read/write can be issued
  338. * on another CPU while the previous read/write may still be working
  339. * on our hardware, if we hit this case the hardware poops in a loop.
  340. * We prevent this by serializing reads and writes.
  341. *
  342. * This issue is not present on PCI-Express devices or pre-AR5416
  343. * devices (legacy, 802.11abg).
  344. */
  345. if (num_possible_cpus() > 1)
  346. ah->config.serialize_regmode = SER_REG_MODE_AUTO;
  347. }
  348. static void ath9k_hw_init_defaults(struct ath_hw *ah)
  349. {
  350. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  351. regulatory->country_code = CTRY_DEFAULT;
  352. regulatory->power_limit = MAX_RATE_POWER;
  353. regulatory->tp_scale = ATH9K_TP_SCALE_MAX;
  354. ah->hw_version.magic = AR5416_MAGIC;
  355. ah->hw_version.subvendorid = 0;
  356. ah->ah_flags = 0;
  357. if (!AR_SREV_9100(ah))
  358. ah->ah_flags = AH_USE_EEPROM;
  359. ah->atim_window = 0;
  360. ah->sta_id1_defaults =
  361. AR_STA_ID1_CRPT_MIC_ENABLE |
  362. AR_STA_ID1_MCAST_KSRCH;
  363. ah->beacon_interval = 100;
  364. ah->enable_32kHz_clock = DONT_USE_32KHZ;
  365. ah->slottime = (u32) -1;
  366. ah->globaltxtimeout = (u32) -1;
  367. ah->power_mode = ATH9K_PM_UNDEFINED;
  368. }
  369. static int ath9k_hw_init_macaddr(struct ath_hw *ah)
  370. {
  371. struct ath_common *common = ath9k_hw_common(ah);
  372. u32 sum;
  373. int i;
  374. u16 eeval;
  375. u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
  376. sum = 0;
  377. for (i = 0; i < 3; i++) {
  378. eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
  379. sum += eeval;
  380. common->macaddr[2 * i] = eeval >> 8;
  381. common->macaddr[2 * i + 1] = eeval & 0xff;
  382. }
  383. if (sum == 0 || sum == 0xffff * 3)
  384. return -EADDRNOTAVAIL;
  385. return 0;
  386. }
  387. static int ath9k_hw_post_init(struct ath_hw *ah)
  388. {
  389. int ecode;
  390. if (!AR_SREV_9271(ah)) {
  391. if (!ath9k_hw_chip_test(ah))
  392. return -ENODEV;
  393. }
  394. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  395. ecode = ar9002_hw_rf_claim(ah);
  396. if (ecode != 0)
  397. return ecode;
  398. }
  399. ecode = ath9k_hw_eeprom_init(ah);
  400. if (ecode != 0)
  401. return ecode;
  402. ath_print(ath9k_hw_common(ah), ATH_DBG_CONFIG,
  403. "Eeprom VER: %d, REV: %d\n",
  404. ah->eep_ops->get_eeprom_ver(ah),
  405. ah->eep_ops->get_eeprom_rev(ah));
  406. ecode = ath9k_hw_rf_alloc_ext_banks(ah);
  407. if (ecode) {
  408. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  409. "Failed allocating banks for "
  410. "external radio\n");
  411. return ecode;
  412. }
  413. if (!AR_SREV_9100(ah)) {
  414. ath9k_hw_ani_setup(ah);
  415. ath9k_hw_ani_init(ah);
  416. }
  417. return 0;
  418. }
  419. static void ath9k_hw_attach_ops(struct ath_hw *ah)
  420. {
  421. if (AR_SREV_9300_20_OR_LATER(ah))
  422. ar9003_hw_attach_ops(ah);
  423. else
  424. ar9002_hw_attach_ops(ah);
  425. }
  426. /* Called for all hardware families */
  427. static int __ath9k_hw_init(struct ath_hw *ah)
  428. {
  429. struct ath_common *common = ath9k_hw_common(ah);
  430. int r = 0;
  431. if (ah->hw_version.devid == AR5416_AR9100_DEVID)
  432. ah->hw_version.macVersion = AR_SREV_VERSION_9100;
  433. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  434. ath_print(common, ATH_DBG_FATAL,
  435. "Couldn't reset chip\n");
  436. return -EIO;
  437. }
  438. ath9k_hw_init_defaults(ah);
  439. ath9k_hw_init_config(ah);
  440. ath9k_hw_attach_ops(ah);
  441. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  442. ath_print(common, ATH_DBG_FATAL, "Couldn't wakeup chip\n");
  443. return -EIO;
  444. }
  445. if (ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
  446. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
  447. (AR_SREV_9280(ah) && !ah->is_pciexpress)) {
  448. ah->config.serialize_regmode =
  449. SER_REG_MODE_ON;
  450. } else {
  451. ah->config.serialize_regmode =
  452. SER_REG_MODE_OFF;
  453. }
  454. }
  455. ath_print(common, ATH_DBG_RESET, "serialize_regmode is %d\n",
  456. ah->config.serialize_regmode);
  457. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  458. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
  459. else
  460. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
  461. if (!ath9k_hw_macversion_supported(ah)) {
  462. ath_print(common, ATH_DBG_FATAL,
  463. "Mac Chip Rev 0x%02x.%x is not supported by "
  464. "this driver\n", ah->hw_version.macVersion,
  465. ah->hw_version.macRev);
  466. return -EOPNOTSUPP;
  467. }
  468. if (AR_SREV_9271(ah) || AR_SREV_9100(ah))
  469. ah->is_pciexpress = false;
  470. ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
  471. ath9k_hw_init_cal_settings(ah);
  472. ah->ani_function = ATH9K_ANI_ALL;
  473. if (AR_SREV_9280_10_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  474. ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
  475. if (!AR_SREV_9300_20_OR_LATER(ah))
  476. ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
  477. ath9k_hw_init_mode_regs(ah);
  478. /*
  479. * Configire PCIE after Ini init. SERDES values now come from ini file
  480. * This enables PCIe low power mode.
  481. */
  482. if (AR_SREV_9300_20_OR_LATER(ah)) {
  483. u32 regval;
  484. unsigned int i;
  485. /* Set Bits 16 and 17 in the AR_WA register. */
  486. regval = REG_READ(ah, AR_WA);
  487. regval |= 0x00030000;
  488. REG_WRITE(ah, AR_WA, regval);
  489. for (i = 0; i < ah->iniPcieSerdesLowPower.ia_rows; i++) {
  490. REG_WRITE(ah,
  491. INI_RA(&ah->iniPcieSerdesLowPower, i, 0),
  492. INI_RA(&ah->iniPcieSerdesLowPower, i, 1));
  493. }
  494. }
  495. if (ah->is_pciexpress)
  496. ath9k_hw_configpcipowersave(ah, 0, 0);
  497. else
  498. ath9k_hw_disablepcie(ah);
  499. if (!AR_SREV_9300_20_OR_LATER(ah))
  500. ar9002_hw_cck_chan14_spread(ah);
  501. r = ath9k_hw_post_init(ah);
  502. if (r)
  503. return r;
  504. ath9k_hw_init_mode_gain_regs(ah);
  505. r = ath9k_hw_fill_cap_info(ah);
  506. if (r)
  507. return r;
  508. r = ath9k_hw_init_macaddr(ah);
  509. if (r) {
  510. ath_print(common, ATH_DBG_FATAL,
  511. "Failed to initialize MAC address\n");
  512. return r;
  513. }
  514. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  515. ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
  516. else
  517. ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
  518. if (AR_SREV_9300_20_OR_LATER(ah))
  519. ar9003_hw_set_nf_limits(ah);
  520. ath9k_init_nfcal_hist_buffer(ah);
  521. ah->bb_watchdog_timeout_ms = 25;
  522. common->state = ATH_HW_INITIALIZED;
  523. return 0;
  524. }
  525. int ath9k_hw_init(struct ath_hw *ah)
  526. {
  527. int ret;
  528. struct ath_common *common = ath9k_hw_common(ah);
  529. /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
  530. switch (ah->hw_version.devid) {
  531. case AR5416_DEVID_PCI:
  532. case AR5416_DEVID_PCIE:
  533. case AR5416_AR9100_DEVID:
  534. case AR9160_DEVID_PCI:
  535. case AR9280_DEVID_PCI:
  536. case AR9280_DEVID_PCIE:
  537. case AR9285_DEVID_PCIE:
  538. case AR9287_DEVID_PCI:
  539. case AR9287_DEVID_PCIE:
  540. case AR2427_DEVID_PCIE:
  541. case AR9300_DEVID_PCIE:
  542. break;
  543. default:
  544. if (common->bus_ops->ath_bus_type == ATH_USB)
  545. break;
  546. ath_print(common, ATH_DBG_FATAL,
  547. "Hardware device ID 0x%04x not supported\n",
  548. ah->hw_version.devid);
  549. return -EOPNOTSUPP;
  550. }
  551. ret = __ath9k_hw_init(ah);
  552. if (ret) {
  553. ath_print(common, ATH_DBG_FATAL,
  554. "Unable to initialize hardware; "
  555. "initialization status: %d\n", ret);
  556. return ret;
  557. }
  558. return 0;
  559. }
  560. EXPORT_SYMBOL(ath9k_hw_init);
  561. static void ath9k_hw_init_qos(struct ath_hw *ah)
  562. {
  563. ENABLE_REGWRITE_BUFFER(ah);
  564. REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
  565. REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
  566. REG_WRITE(ah, AR_QOS_NO_ACK,
  567. SM(2, AR_QOS_NO_ACK_TWO_BIT) |
  568. SM(5, AR_QOS_NO_ACK_BIT_OFF) |
  569. SM(0, AR_QOS_NO_ACK_BYTE_OFF));
  570. REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
  571. REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
  572. REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
  573. REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
  574. REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
  575. REGWRITE_BUFFER_FLUSH(ah);
  576. DISABLE_REGWRITE_BUFFER(ah);
  577. }
  578. static void ath9k_hw_init_pll(struct ath_hw *ah,
  579. struct ath9k_channel *chan)
  580. {
  581. u32 pll = ath9k_hw_compute_pll_control(ah, chan);
  582. REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
  583. /* Switch the core clock for ar9271 to 117Mhz */
  584. if (AR_SREV_9271(ah)) {
  585. udelay(500);
  586. REG_WRITE(ah, 0x50040, 0x304);
  587. }
  588. udelay(RTC_PLL_SETTLE_DELAY);
  589. REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
  590. }
  591. static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
  592. enum nl80211_iftype opmode)
  593. {
  594. u32 imr_reg = AR_IMR_TXERR |
  595. AR_IMR_TXURN |
  596. AR_IMR_RXERR |
  597. AR_IMR_RXORN |
  598. AR_IMR_BCNMISC;
  599. if (AR_SREV_9300_20_OR_LATER(ah)) {
  600. imr_reg |= AR_IMR_RXOK_HP;
  601. if (ah->config.rx_intr_mitigation)
  602. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  603. else
  604. imr_reg |= AR_IMR_RXOK_LP;
  605. } else {
  606. if (ah->config.rx_intr_mitigation)
  607. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  608. else
  609. imr_reg |= AR_IMR_RXOK;
  610. }
  611. if (ah->config.tx_intr_mitigation)
  612. imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
  613. else
  614. imr_reg |= AR_IMR_TXOK;
  615. if (opmode == NL80211_IFTYPE_AP)
  616. imr_reg |= AR_IMR_MIB;
  617. ENABLE_REGWRITE_BUFFER(ah);
  618. REG_WRITE(ah, AR_IMR, imr_reg);
  619. ah->imrs2_reg |= AR_IMR_S2_GTT;
  620. REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
  621. if (!AR_SREV_9100(ah)) {
  622. REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
  623. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_DEFAULT);
  624. REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
  625. }
  626. REGWRITE_BUFFER_FLUSH(ah);
  627. DISABLE_REGWRITE_BUFFER(ah);
  628. if (AR_SREV_9300_20_OR_LATER(ah)) {
  629. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
  630. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
  631. REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
  632. REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
  633. }
  634. }
  635. static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
  636. {
  637. u32 val = ath9k_hw_mac_to_clks(ah, us);
  638. val = min(val, (u32) 0xFFFF);
  639. REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
  640. }
  641. static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
  642. {
  643. u32 val = ath9k_hw_mac_to_clks(ah, us);
  644. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
  645. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
  646. }
  647. static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
  648. {
  649. u32 val = ath9k_hw_mac_to_clks(ah, us);
  650. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
  651. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
  652. }
  653. static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
  654. {
  655. if (tu > 0xFFFF) {
  656. ath_print(ath9k_hw_common(ah), ATH_DBG_XMIT,
  657. "bad global tx timeout %u\n", tu);
  658. ah->globaltxtimeout = (u32) -1;
  659. return false;
  660. } else {
  661. REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
  662. ah->globaltxtimeout = tu;
  663. return true;
  664. }
  665. }
  666. void ath9k_hw_init_global_settings(struct ath_hw *ah)
  667. {
  668. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  669. int acktimeout;
  670. int slottime;
  671. int sifstime;
  672. ath_print(ath9k_hw_common(ah), ATH_DBG_RESET, "ah->misc_mode 0x%x\n",
  673. ah->misc_mode);
  674. if (ah->misc_mode != 0)
  675. REG_WRITE(ah, AR_PCU_MISC,
  676. REG_READ(ah, AR_PCU_MISC) | ah->misc_mode);
  677. if (conf->channel && conf->channel->band == IEEE80211_BAND_5GHZ)
  678. sifstime = 16;
  679. else
  680. sifstime = 10;
  681. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  682. slottime = ah->slottime + 3 * ah->coverage_class;
  683. acktimeout = slottime + sifstime;
  684. /*
  685. * Workaround for early ACK timeouts, add an offset to match the
  686. * initval's 64us ack timeout value.
  687. * This was initially only meant to work around an issue with delayed
  688. * BA frames in some implementations, but it has been found to fix ACK
  689. * timeout issues in other cases as well.
  690. */
  691. if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ)
  692. acktimeout += 64 - sifstime - ah->slottime;
  693. ath9k_hw_setslottime(ah, slottime);
  694. ath9k_hw_set_ack_timeout(ah, acktimeout);
  695. ath9k_hw_set_cts_timeout(ah, acktimeout);
  696. if (ah->globaltxtimeout != (u32) -1)
  697. ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
  698. }
  699. EXPORT_SYMBOL(ath9k_hw_init_global_settings);
  700. void ath9k_hw_deinit(struct ath_hw *ah)
  701. {
  702. struct ath_common *common = ath9k_hw_common(ah);
  703. if (common->state < ATH_HW_INITIALIZED)
  704. goto free_hw;
  705. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  706. free_hw:
  707. ath9k_hw_rf_free_ext_banks(ah);
  708. }
  709. EXPORT_SYMBOL(ath9k_hw_deinit);
  710. /*******/
  711. /* INI */
  712. /*******/
  713. u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
  714. {
  715. u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
  716. if (IS_CHAN_B(chan))
  717. ctl |= CTL_11B;
  718. else if (IS_CHAN_G(chan))
  719. ctl |= CTL_11G;
  720. else
  721. ctl |= CTL_11A;
  722. return ctl;
  723. }
  724. /****************************************/
  725. /* Reset and Channel Switching Routines */
  726. /****************************************/
  727. static inline void ath9k_hw_set_dma(struct ath_hw *ah)
  728. {
  729. struct ath_common *common = ath9k_hw_common(ah);
  730. u32 regval;
  731. ENABLE_REGWRITE_BUFFER(ah);
  732. /*
  733. * set AHB_MODE not to do cacheline prefetches
  734. */
  735. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  736. regval = REG_READ(ah, AR_AHB_MODE);
  737. REG_WRITE(ah, AR_AHB_MODE, regval | AR_AHB_PREFETCH_RD_EN);
  738. }
  739. /*
  740. * let mac dma reads be in 128 byte chunks
  741. */
  742. regval = REG_READ(ah, AR_TXCFG) & ~AR_TXCFG_DMASZ_MASK;
  743. REG_WRITE(ah, AR_TXCFG, regval | AR_TXCFG_DMASZ_128B);
  744. REGWRITE_BUFFER_FLUSH(ah);
  745. DISABLE_REGWRITE_BUFFER(ah);
  746. /*
  747. * Restore TX Trigger Level to its pre-reset value.
  748. * The initial value depends on whether aggregation is enabled, and is
  749. * adjusted whenever underruns are detected.
  750. */
  751. if (!AR_SREV_9300_20_OR_LATER(ah))
  752. REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
  753. ENABLE_REGWRITE_BUFFER(ah);
  754. /*
  755. * let mac dma writes be in 128 byte chunks
  756. */
  757. regval = REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_DMASZ_MASK;
  758. REG_WRITE(ah, AR_RXCFG, regval | AR_RXCFG_DMASZ_128B);
  759. /*
  760. * Setup receive FIFO threshold to hold off TX activities
  761. */
  762. REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
  763. if (AR_SREV_9300_20_OR_LATER(ah)) {
  764. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
  765. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
  766. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  767. ah->caps.rx_status_len);
  768. }
  769. /*
  770. * reduce the number of usable entries in PCU TXBUF to avoid
  771. * wrap around issues.
  772. */
  773. if (AR_SREV_9285(ah)) {
  774. /* For AR9285 the number of Fifos are reduced to half.
  775. * So set the usable tx buf size also to half to
  776. * avoid data/delimiter underruns
  777. */
  778. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  779. AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
  780. } else if (!AR_SREV_9271(ah)) {
  781. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  782. AR_PCU_TXBUF_CTRL_USABLE_SIZE);
  783. }
  784. REGWRITE_BUFFER_FLUSH(ah);
  785. DISABLE_REGWRITE_BUFFER(ah);
  786. if (AR_SREV_9300_20_OR_LATER(ah))
  787. ath9k_hw_reset_txstatus_ring(ah);
  788. }
  789. static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
  790. {
  791. u32 val;
  792. val = REG_READ(ah, AR_STA_ID1);
  793. val &= ~(AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC);
  794. switch (opmode) {
  795. case NL80211_IFTYPE_AP:
  796. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_STA_AP
  797. | AR_STA_ID1_KSRCH_MODE);
  798. REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  799. break;
  800. case NL80211_IFTYPE_ADHOC:
  801. case NL80211_IFTYPE_MESH_POINT:
  802. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_ADHOC
  803. | AR_STA_ID1_KSRCH_MODE);
  804. REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  805. break;
  806. case NL80211_IFTYPE_STATION:
  807. case NL80211_IFTYPE_MONITOR:
  808. REG_WRITE(ah, AR_STA_ID1, val | AR_STA_ID1_KSRCH_MODE);
  809. break;
  810. }
  811. }
  812. void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
  813. u32 *coef_mantissa, u32 *coef_exponent)
  814. {
  815. u32 coef_exp, coef_man;
  816. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  817. if ((coef_scaled >> coef_exp) & 0x1)
  818. break;
  819. coef_exp = 14 - (coef_exp - COEF_SCALE_S);
  820. coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
  821. *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
  822. *coef_exponent = coef_exp - 16;
  823. }
  824. static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
  825. {
  826. u32 rst_flags;
  827. u32 tmpReg;
  828. if (AR_SREV_9100(ah)) {
  829. u32 val = REG_READ(ah, AR_RTC_DERIVED_CLK);
  830. val &= ~AR_RTC_DERIVED_CLK_PERIOD;
  831. val |= SM(1, AR_RTC_DERIVED_CLK_PERIOD);
  832. REG_WRITE(ah, AR_RTC_DERIVED_CLK, val);
  833. (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
  834. }
  835. ENABLE_REGWRITE_BUFFER(ah);
  836. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  837. AR_RTC_FORCE_WAKE_ON_INT);
  838. if (AR_SREV_9100(ah)) {
  839. rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
  840. AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
  841. } else {
  842. tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  843. if (tmpReg &
  844. (AR_INTR_SYNC_LOCAL_TIMEOUT |
  845. AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
  846. u32 val;
  847. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  848. val = AR_RC_HOSTIF;
  849. if (!AR_SREV_9300_20_OR_LATER(ah))
  850. val |= AR_RC_AHB;
  851. REG_WRITE(ah, AR_RC, val);
  852. } else if (!AR_SREV_9300_20_OR_LATER(ah))
  853. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  854. rst_flags = AR_RTC_RC_MAC_WARM;
  855. if (type == ATH9K_RESET_COLD)
  856. rst_flags |= AR_RTC_RC_MAC_COLD;
  857. }
  858. REG_WRITE(ah, AR_RTC_RC, rst_flags);
  859. REGWRITE_BUFFER_FLUSH(ah);
  860. DISABLE_REGWRITE_BUFFER(ah);
  861. udelay(50);
  862. REG_WRITE(ah, AR_RTC_RC, 0);
  863. if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
  864. ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
  865. "RTC stuck in MAC reset\n");
  866. return false;
  867. }
  868. if (!AR_SREV_9100(ah))
  869. REG_WRITE(ah, AR_RC, 0);
  870. if (AR_SREV_9100(ah))
  871. udelay(50);
  872. return true;
  873. }
  874. static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
  875. {
  876. ENABLE_REGWRITE_BUFFER(ah);
  877. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  878. AR_RTC_FORCE_WAKE_ON_INT);
  879. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  880. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  881. REG_WRITE(ah, AR_RTC_RESET, 0);
  882. REGWRITE_BUFFER_FLUSH(ah);
  883. DISABLE_REGWRITE_BUFFER(ah);
  884. if (!AR_SREV_9300_20_OR_LATER(ah))
  885. udelay(2);
  886. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  887. REG_WRITE(ah, AR_RC, 0);
  888. REG_WRITE(ah, AR_RTC_RESET, 1);
  889. if (!ath9k_hw_wait(ah,
  890. AR_RTC_STATUS,
  891. AR_RTC_STATUS_M,
  892. AR_RTC_STATUS_ON,
  893. AH_WAIT_TIMEOUT)) {
  894. ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
  895. "RTC not waking up\n");
  896. return false;
  897. }
  898. ath9k_hw_read_revisions(ah);
  899. return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
  900. }
  901. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
  902. {
  903. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  904. AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
  905. switch (type) {
  906. case ATH9K_RESET_POWER_ON:
  907. return ath9k_hw_set_reset_power_on(ah);
  908. case ATH9K_RESET_WARM:
  909. case ATH9K_RESET_COLD:
  910. return ath9k_hw_set_reset(ah, type);
  911. default:
  912. return false;
  913. }
  914. }
  915. static bool ath9k_hw_chip_reset(struct ath_hw *ah,
  916. struct ath9k_channel *chan)
  917. {
  918. if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)) {
  919. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON))
  920. return false;
  921. } else if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  922. return false;
  923. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  924. return false;
  925. ah->chip_fullsleep = false;
  926. ath9k_hw_init_pll(ah, chan);
  927. ath9k_hw_set_rfmode(ah, chan);
  928. return true;
  929. }
  930. static bool ath9k_hw_channel_change(struct ath_hw *ah,
  931. struct ath9k_channel *chan)
  932. {
  933. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  934. struct ath_common *common = ath9k_hw_common(ah);
  935. struct ieee80211_channel *channel = chan->chan;
  936. u32 qnum;
  937. int r;
  938. for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
  939. if (ath9k_hw_numtxpending(ah, qnum)) {
  940. ath_print(common, ATH_DBG_QUEUE,
  941. "Transmit frames pending on "
  942. "queue %d\n", qnum);
  943. return false;
  944. }
  945. }
  946. if (!ath9k_hw_rfbus_req(ah)) {
  947. ath_print(common, ATH_DBG_FATAL,
  948. "Could not kill baseband RX\n");
  949. return false;
  950. }
  951. ath9k_hw_set_channel_regs(ah, chan);
  952. r = ath9k_hw_rf_set_freq(ah, chan);
  953. if (r) {
  954. ath_print(common, ATH_DBG_FATAL,
  955. "Failed to set channel\n");
  956. return false;
  957. }
  958. ah->eep_ops->set_txpower(ah, chan,
  959. ath9k_regd_get_ctl(regulatory, chan),
  960. channel->max_antenna_gain * 2,
  961. channel->max_power * 2,
  962. min((u32) MAX_RATE_POWER,
  963. (u32) regulatory->power_limit));
  964. ath9k_hw_rfbus_done(ah);
  965. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  966. ath9k_hw_set_delta_slope(ah, chan);
  967. ath9k_hw_spur_mitigate_freq(ah, chan);
  968. if (!chan->oneTimeCalsDone)
  969. chan->oneTimeCalsDone = true;
  970. return true;
  971. }
  972. bool ath9k_hw_check_alive(struct ath_hw *ah)
  973. {
  974. int count = 50;
  975. u32 reg;
  976. if (AR_SREV_9285_10_OR_LATER(ah))
  977. return true;
  978. do {
  979. reg = REG_READ(ah, AR_OBS_BUS_1);
  980. if ((reg & 0x7E7FFFEF) == 0x00702400)
  981. continue;
  982. switch (reg & 0x7E000B00) {
  983. case 0x1E000000:
  984. case 0x52000B00:
  985. case 0x18000B00:
  986. continue;
  987. default:
  988. return true;
  989. }
  990. } while (count-- > 0);
  991. return false;
  992. }
  993. EXPORT_SYMBOL(ath9k_hw_check_alive);
  994. int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
  995. bool bChannelChange)
  996. {
  997. struct ath_common *common = ath9k_hw_common(ah);
  998. u32 saveLedState;
  999. struct ath9k_channel *curchan = ah->curchan;
  1000. u32 saveDefAntenna;
  1001. u32 macStaId1;
  1002. u64 tsf = 0;
  1003. int i, r;
  1004. ah->txchainmask = common->tx_chainmask;
  1005. ah->rxchainmask = common->rx_chainmask;
  1006. if (!ah->chip_fullsleep) {
  1007. ath9k_hw_abortpcurecv(ah);
  1008. if (!ath9k_hw_stopdmarecv(ah))
  1009. ath_print(common, ATH_DBG_XMIT,
  1010. "Failed to stop receive dma\n");
  1011. }
  1012. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1013. return -EIO;
  1014. if (curchan && !ah->chip_fullsleep)
  1015. ath9k_hw_getnf(ah, curchan);
  1016. if (bChannelChange &&
  1017. (ah->chip_fullsleep != true) &&
  1018. (ah->curchan != NULL) &&
  1019. (chan->channel != ah->curchan->channel) &&
  1020. ((chan->channelFlags & CHANNEL_ALL) ==
  1021. (ah->curchan->channelFlags & CHANNEL_ALL)) &&
  1022. !AR_SREV_9280(ah)) {
  1023. if (ath9k_hw_channel_change(ah, chan)) {
  1024. ath9k_hw_loadnf(ah, ah->curchan);
  1025. ath9k_hw_start_nfcal(ah);
  1026. return 0;
  1027. }
  1028. }
  1029. saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
  1030. if (saveDefAntenna == 0)
  1031. saveDefAntenna = 1;
  1032. macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
  1033. /* For chips on which RTC reset is done, save TSF before it gets cleared */
  1034. if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
  1035. tsf = ath9k_hw_gettsf64(ah);
  1036. saveLedState = REG_READ(ah, AR_CFG_LED) &
  1037. (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
  1038. AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
  1039. ath9k_hw_mark_phy_inactive(ah);
  1040. /* Only required on the first reset */
  1041. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1042. REG_WRITE(ah,
  1043. AR9271_RESET_POWER_DOWN_CONTROL,
  1044. AR9271_RADIO_RF_RST);
  1045. udelay(50);
  1046. }
  1047. if (!ath9k_hw_chip_reset(ah, chan)) {
  1048. ath_print(common, ATH_DBG_FATAL, "Chip reset failed\n");
  1049. return -EINVAL;
  1050. }
  1051. /* Only required on the first reset */
  1052. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1053. ah->htc_reset_init = false;
  1054. REG_WRITE(ah,
  1055. AR9271_RESET_POWER_DOWN_CONTROL,
  1056. AR9271_GATE_MAC_CTL);
  1057. udelay(50);
  1058. }
  1059. /* Restore TSF */
  1060. if (tsf && AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
  1061. ath9k_hw_settsf64(ah, tsf);
  1062. if (AR_SREV_9280_10_OR_LATER(ah))
  1063. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
  1064. if (!AR_SREV_9300_20_OR_LATER(ah))
  1065. ar9002_hw_enable_async_fifo(ah);
  1066. r = ath9k_hw_process_ini(ah, chan);
  1067. if (r)
  1068. return r;
  1069. /* Setup MFP options for CCMP */
  1070. if (AR_SREV_9280_20_OR_LATER(ah)) {
  1071. /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
  1072. * frames when constructing CCMP AAD. */
  1073. REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
  1074. 0xc7ff);
  1075. ah->sw_mgmt_crypto = false;
  1076. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  1077. /* Disable hardware crypto for management frames */
  1078. REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
  1079. AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
  1080. REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
  1081. AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
  1082. ah->sw_mgmt_crypto = true;
  1083. } else
  1084. ah->sw_mgmt_crypto = true;
  1085. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  1086. ath9k_hw_set_delta_slope(ah, chan);
  1087. ath9k_hw_spur_mitigate_freq(ah, chan);
  1088. ah->eep_ops->set_board_values(ah, chan);
  1089. ath9k_hw_set_operating_mode(ah, ah->opmode);
  1090. ENABLE_REGWRITE_BUFFER(ah);
  1091. REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
  1092. REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
  1093. | macStaId1
  1094. | AR_STA_ID1_RTS_USE_DEF
  1095. | (ah->config.
  1096. ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
  1097. | ah->sta_id1_defaults);
  1098. ath_hw_setbssidmask(common);
  1099. REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
  1100. ath9k_hw_write_associd(ah);
  1101. REG_WRITE(ah, AR_ISR, ~0);
  1102. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  1103. REGWRITE_BUFFER_FLUSH(ah);
  1104. DISABLE_REGWRITE_BUFFER(ah);
  1105. r = ath9k_hw_rf_set_freq(ah, chan);
  1106. if (r)
  1107. return r;
  1108. ENABLE_REGWRITE_BUFFER(ah);
  1109. for (i = 0; i < AR_NUM_DCU; i++)
  1110. REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
  1111. REGWRITE_BUFFER_FLUSH(ah);
  1112. DISABLE_REGWRITE_BUFFER(ah);
  1113. ah->intr_txqs = 0;
  1114. for (i = 0; i < ah->caps.total_queues; i++)
  1115. ath9k_hw_resettxqueue(ah, i);
  1116. ath9k_hw_init_interrupt_masks(ah, ah->opmode);
  1117. ath9k_hw_ani_cache_ini_regs(ah);
  1118. ath9k_hw_init_qos(ah);
  1119. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1120. ath9k_enable_rfkill(ah);
  1121. ath9k_hw_init_global_settings(ah);
  1122. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  1123. ar9002_hw_update_async_fifo(ah);
  1124. ar9002_hw_enable_wep_aggregation(ah);
  1125. }
  1126. REG_WRITE(ah, AR_STA_ID1,
  1127. REG_READ(ah, AR_STA_ID1) | AR_STA_ID1_PRESERVE_SEQNUM);
  1128. ath9k_hw_set_dma(ah);
  1129. REG_WRITE(ah, AR_OBS, 8);
  1130. if (ah->config.rx_intr_mitigation) {
  1131. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
  1132. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
  1133. }
  1134. if (ah->config.tx_intr_mitigation) {
  1135. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
  1136. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
  1137. }
  1138. ath9k_hw_init_bb(ah, chan);
  1139. if (!ath9k_hw_init_cal(ah, chan))
  1140. return -EIO;
  1141. ENABLE_REGWRITE_BUFFER(ah);
  1142. ath9k_hw_restore_chainmask(ah);
  1143. REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
  1144. REGWRITE_BUFFER_FLUSH(ah);
  1145. DISABLE_REGWRITE_BUFFER(ah);
  1146. /*
  1147. * For big endian systems turn on swapping for descriptors
  1148. */
  1149. if (AR_SREV_9100(ah)) {
  1150. u32 mask;
  1151. mask = REG_READ(ah, AR_CFG);
  1152. if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
  1153. ath_print(common, ATH_DBG_RESET,
  1154. "CFG Byte Swap Set 0x%x\n", mask);
  1155. } else {
  1156. mask =
  1157. INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
  1158. REG_WRITE(ah, AR_CFG, mask);
  1159. ath_print(common, ATH_DBG_RESET,
  1160. "Setting CFG 0x%x\n", REG_READ(ah, AR_CFG));
  1161. }
  1162. } else {
  1163. if (common->bus_ops->ath_bus_type == ATH_USB) {
  1164. /* Configure AR9271 target WLAN */
  1165. if (AR_SREV_9271(ah))
  1166. REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
  1167. else
  1168. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1169. }
  1170. #ifdef __BIG_ENDIAN
  1171. else
  1172. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1173. #endif
  1174. }
  1175. if (ah->btcoex_hw.enabled)
  1176. ath9k_hw_btcoex_enable(ah);
  1177. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1178. ath9k_hw_loadnf(ah, curchan);
  1179. ath9k_hw_start_nfcal(ah);
  1180. ar9003_hw_bb_watchdog_config(ah);
  1181. }
  1182. return 0;
  1183. }
  1184. EXPORT_SYMBOL(ath9k_hw_reset);
  1185. /************************/
  1186. /* Key Cache Management */
  1187. /************************/
  1188. bool ath9k_hw_keyreset(struct ath_hw *ah, u16 entry)
  1189. {
  1190. u32 keyType;
  1191. if (entry >= ah->caps.keycache_size) {
  1192. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  1193. "keychache entry %u out of range\n", entry);
  1194. return false;
  1195. }
  1196. keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
  1197. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
  1198. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
  1199. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
  1200. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
  1201. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
  1202. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
  1203. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
  1204. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
  1205. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  1206. u16 micentry = entry + 64;
  1207. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
  1208. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  1209. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
  1210. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  1211. }
  1212. return true;
  1213. }
  1214. EXPORT_SYMBOL(ath9k_hw_keyreset);
  1215. bool ath9k_hw_keysetmac(struct ath_hw *ah, u16 entry, const u8 *mac)
  1216. {
  1217. u32 macHi, macLo;
  1218. u32 unicast_flag = AR_KEYTABLE_VALID;
  1219. if (entry >= ah->caps.keycache_size) {
  1220. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  1221. "keychache entry %u out of range\n", entry);
  1222. return false;
  1223. }
  1224. if (mac != NULL) {
  1225. /*
  1226. * AR_KEYTABLE_VALID indicates that the address is a unicast
  1227. * address, which must match the transmitter address for
  1228. * decrypting frames.
  1229. * Not setting this bit allows the hardware to use the key
  1230. * for multicast frame decryption.
  1231. */
  1232. if (mac[0] & 0x01)
  1233. unicast_flag = 0;
  1234. macHi = (mac[5] << 8) | mac[4];
  1235. macLo = (mac[3] << 24) |
  1236. (mac[2] << 16) |
  1237. (mac[1] << 8) |
  1238. mac[0];
  1239. macLo >>= 1;
  1240. macLo |= (macHi & 1) << 31;
  1241. macHi >>= 1;
  1242. } else {
  1243. macLo = macHi = 0;
  1244. }
  1245. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
  1246. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | unicast_flag);
  1247. return true;
  1248. }
  1249. EXPORT_SYMBOL(ath9k_hw_keysetmac);
  1250. bool ath9k_hw_set_keycache_entry(struct ath_hw *ah, u16 entry,
  1251. const struct ath9k_keyval *k,
  1252. const u8 *mac)
  1253. {
  1254. const struct ath9k_hw_capabilities *pCap = &ah->caps;
  1255. struct ath_common *common = ath9k_hw_common(ah);
  1256. u32 key0, key1, key2, key3, key4;
  1257. u32 keyType;
  1258. if (entry >= pCap->keycache_size) {
  1259. ath_print(common, ATH_DBG_FATAL,
  1260. "keycache entry %u out of range\n", entry);
  1261. return false;
  1262. }
  1263. switch (k->kv_type) {
  1264. case ATH9K_CIPHER_AES_OCB:
  1265. keyType = AR_KEYTABLE_TYPE_AES;
  1266. break;
  1267. case ATH9K_CIPHER_AES_CCM:
  1268. if (!(pCap->hw_caps & ATH9K_HW_CAP_CIPHER_AESCCM)) {
  1269. ath_print(common, ATH_DBG_ANY,
  1270. "AES-CCM not supported by mac rev 0x%x\n",
  1271. ah->hw_version.macRev);
  1272. return false;
  1273. }
  1274. keyType = AR_KEYTABLE_TYPE_CCM;
  1275. break;
  1276. case ATH9K_CIPHER_TKIP:
  1277. keyType = AR_KEYTABLE_TYPE_TKIP;
  1278. if (ATH9K_IS_MIC_ENABLED(ah)
  1279. && entry + 64 >= pCap->keycache_size) {
  1280. ath_print(common, ATH_DBG_ANY,
  1281. "entry %u inappropriate for TKIP\n", entry);
  1282. return false;
  1283. }
  1284. break;
  1285. case ATH9K_CIPHER_WEP:
  1286. if (k->kv_len < WLAN_KEY_LEN_WEP40) {
  1287. ath_print(common, ATH_DBG_ANY,
  1288. "WEP key length %u too small\n", k->kv_len);
  1289. return false;
  1290. }
  1291. if (k->kv_len <= WLAN_KEY_LEN_WEP40)
  1292. keyType = AR_KEYTABLE_TYPE_40;
  1293. else if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  1294. keyType = AR_KEYTABLE_TYPE_104;
  1295. else
  1296. keyType = AR_KEYTABLE_TYPE_128;
  1297. break;
  1298. case ATH9K_CIPHER_CLR:
  1299. keyType = AR_KEYTABLE_TYPE_CLR;
  1300. break;
  1301. default:
  1302. ath_print(common, ATH_DBG_FATAL,
  1303. "cipher %u not supported\n", k->kv_type);
  1304. return false;
  1305. }
  1306. key0 = get_unaligned_le32(k->kv_val + 0);
  1307. key1 = get_unaligned_le16(k->kv_val + 4);
  1308. key2 = get_unaligned_le32(k->kv_val + 6);
  1309. key3 = get_unaligned_le16(k->kv_val + 10);
  1310. key4 = get_unaligned_le32(k->kv_val + 12);
  1311. if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  1312. key4 &= 0xff;
  1313. /*
  1314. * Note: Key cache registers access special memory area that requires
  1315. * two 32-bit writes to actually update the values in the internal
  1316. * memory. Consequently, the exact order and pairs used here must be
  1317. * maintained.
  1318. */
  1319. if (keyType == AR_KEYTABLE_TYPE_TKIP && ATH9K_IS_MIC_ENABLED(ah)) {
  1320. u16 micentry = entry + 64;
  1321. /*
  1322. * Write inverted key[47:0] first to avoid Michael MIC errors
  1323. * on frames that could be sent or received at the same time.
  1324. * The correct key will be written in the end once everything
  1325. * else is ready.
  1326. */
  1327. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
  1328. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
  1329. /* Write key[95:48] */
  1330. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  1331. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  1332. /* Write key[127:96] and key type */
  1333. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  1334. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  1335. /* Write MAC address for the entry */
  1336. (void) ath9k_hw_keysetmac(ah, entry, mac);
  1337. if (ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA) {
  1338. /*
  1339. * TKIP uses two key cache entries:
  1340. * Michael MIC TX/RX keys in the same key cache entry
  1341. * (idx = main index + 64):
  1342. * key0 [31:0] = RX key [31:0]
  1343. * key1 [15:0] = TX key [31:16]
  1344. * key1 [31:16] = reserved
  1345. * key2 [31:0] = RX key [63:32]
  1346. * key3 [15:0] = TX key [15:0]
  1347. * key3 [31:16] = reserved
  1348. * key4 [31:0] = TX key [63:32]
  1349. */
  1350. u32 mic0, mic1, mic2, mic3, mic4;
  1351. mic0 = get_unaligned_le32(k->kv_mic + 0);
  1352. mic2 = get_unaligned_le32(k->kv_mic + 4);
  1353. mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
  1354. mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
  1355. mic4 = get_unaligned_le32(k->kv_txmic + 4);
  1356. /* Write RX[31:0] and TX[31:16] */
  1357. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  1358. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
  1359. /* Write RX[63:32] and TX[15:0] */
  1360. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  1361. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
  1362. /* Write TX[63:32] and keyType(reserved) */
  1363. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
  1364. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  1365. AR_KEYTABLE_TYPE_CLR);
  1366. } else {
  1367. /*
  1368. * TKIP uses four key cache entries (two for group
  1369. * keys):
  1370. * Michael MIC TX/RX keys are in different key cache
  1371. * entries (idx = main index + 64 for TX and
  1372. * main index + 32 + 96 for RX):
  1373. * key0 [31:0] = TX/RX MIC key [31:0]
  1374. * key1 [31:0] = reserved
  1375. * key2 [31:0] = TX/RX MIC key [63:32]
  1376. * key3 [31:0] = reserved
  1377. * key4 [31:0] = reserved
  1378. *
  1379. * Upper layer code will call this function separately
  1380. * for TX and RX keys when these registers offsets are
  1381. * used.
  1382. */
  1383. u32 mic0, mic2;
  1384. mic0 = get_unaligned_le32(k->kv_mic + 0);
  1385. mic2 = get_unaligned_le32(k->kv_mic + 4);
  1386. /* Write MIC key[31:0] */
  1387. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  1388. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  1389. /* Write MIC key[63:32] */
  1390. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  1391. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  1392. /* Write TX[63:32] and keyType(reserved) */
  1393. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  1394. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  1395. AR_KEYTABLE_TYPE_CLR);
  1396. }
  1397. /* MAC address registers are reserved for the MIC entry */
  1398. REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
  1399. REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
  1400. /*
  1401. * Write the correct (un-inverted) key[47:0] last to enable
  1402. * TKIP now that all other registers are set with correct
  1403. * values.
  1404. */
  1405. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  1406. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  1407. } else {
  1408. /* Write key[47:0] */
  1409. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  1410. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  1411. /* Write key[95:48] */
  1412. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  1413. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  1414. /* Write key[127:96] and key type */
  1415. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  1416. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  1417. /* Write MAC address for the entry */
  1418. (void) ath9k_hw_keysetmac(ah, entry, mac);
  1419. }
  1420. return true;
  1421. }
  1422. EXPORT_SYMBOL(ath9k_hw_set_keycache_entry);
  1423. bool ath9k_hw_keyisvalid(struct ath_hw *ah, u16 entry)
  1424. {
  1425. if (entry < ah->caps.keycache_size) {
  1426. u32 val = REG_READ(ah, AR_KEYTABLE_MAC1(entry));
  1427. if (val & AR_KEYTABLE_VALID)
  1428. return true;
  1429. }
  1430. return false;
  1431. }
  1432. EXPORT_SYMBOL(ath9k_hw_keyisvalid);
  1433. /******************************/
  1434. /* Power Management (Chipset) */
  1435. /******************************/
  1436. /*
  1437. * Notify Power Mgt is disabled in self-generated frames.
  1438. * If requested, force chip to sleep.
  1439. */
  1440. static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
  1441. {
  1442. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1443. if (setChip) {
  1444. /*
  1445. * Clear the RTC force wake bit to allow the
  1446. * mac to go to sleep.
  1447. */
  1448. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  1449. AR_RTC_FORCE_WAKE_EN);
  1450. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1451. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  1452. /* Shutdown chip. Active low */
  1453. if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah))
  1454. REG_CLR_BIT(ah, (AR_RTC_RESET),
  1455. AR_RTC_RESET_EN);
  1456. }
  1457. }
  1458. /*
  1459. * Notify Power Management is enabled in self-generating
  1460. * frames. If request, set power mode of chip to
  1461. * auto/normal. Duration in units of 128us (1/8 TU).
  1462. */
  1463. static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
  1464. {
  1465. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1466. if (setChip) {
  1467. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1468. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1469. /* Set WakeOnInterrupt bit; clear ForceWake bit */
  1470. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1471. AR_RTC_FORCE_WAKE_ON_INT);
  1472. } else {
  1473. /*
  1474. * Clear the RTC force wake bit to allow the
  1475. * mac to go to sleep.
  1476. */
  1477. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  1478. AR_RTC_FORCE_WAKE_EN);
  1479. }
  1480. }
  1481. }
  1482. static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
  1483. {
  1484. u32 val;
  1485. int i;
  1486. if (setChip) {
  1487. if ((REG_READ(ah, AR_RTC_STATUS) &
  1488. AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
  1489. if (ath9k_hw_set_reset_reg(ah,
  1490. ATH9K_RESET_POWER_ON) != true) {
  1491. return false;
  1492. }
  1493. if (!AR_SREV_9300_20_OR_LATER(ah))
  1494. ath9k_hw_init_pll(ah, NULL);
  1495. }
  1496. if (AR_SREV_9100(ah))
  1497. REG_SET_BIT(ah, AR_RTC_RESET,
  1498. AR_RTC_RESET_EN);
  1499. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1500. AR_RTC_FORCE_WAKE_EN);
  1501. udelay(50);
  1502. for (i = POWER_UP_TIME / 50; i > 0; i--) {
  1503. val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
  1504. if (val == AR_RTC_STATUS_ON)
  1505. break;
  1506. udelay(50);
  1507. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1508. AR_RTC_FORCE_WAKE_EN);
  1509. }
  1510. if (i == 0) {
  1511. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  1512. "Failed to wakeup in %uus\n",
  1513. POWER_UP_TIME / 20);
  1514. return false;
  1515. }
  1516. }
  1517. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1518. return true;
  1519. }
  1520. bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
  1521. {
  1522. struct ath_common *common = ath9k_hw_common(ah);
  1523. int status = true, setChip = true;
  1524. static const char *modes[] = {
  1525. "AWAKE",
  1526. "FULL-SLEEP",
  1527. "NETWORK SLEEP",
  1528. "UNDEFINED"
  1529. };
  1530. if (ah->power_mode == mode)
  1531. return status;
  1532. ath_print(common, ATH_DBG_RESET, "%s -> %s\n",
  1533. modes[ah->power_mode], modes[mode]);
  1534. switch (mode) {
  1535. case ATH9K_PM_AWAKE:
  1536. status = ath9k_hw_set_power_awake(ah, setChip);
  1537. break;
  1538. case ATH9K_PM_FULL_SLEEP:
  1539. ath9k_set_power_sleep(ah, setChip);
  1540. ah->chip_fullsleep = true;
  1541. break;
  1542. case ATH9K_PM_NETWORK_SLEEP:
  1543. ath9k_set_power_network_sleep(ah, setChip);
  1544. break;
  1545. default:
  1546. ath_print(common, ATH_DBG_FATAL,
  1547. "Unknown power mode %u\n", mode);
  1548. return false;
  1549. }
  1550. ah->power_mode = mode;
  1551. return status;
  1552. }
  1553. EXPORT_SYMBOL(ath9k_hw_setpower);
  1554. /*******************/
  1555. /* Beacon Handling */
  1556. /*******************/
  1557. void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
  1558. {
  1559. int flags = 0;
  1560. ah->beacon_interval = beacon_period;
  1561. ENABLE_REGWRITE_BUFFER(ah);
  1562. switch (ah->opmode) {
  1563. case NL80211_IFTYPE_STATION:
  1564. case NL80211_IFTYPE_MONITOR:
  1565. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  1566. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, 0xffff);
  1567. REG_WRITE(ah, AR_NEXT_SWBA, 0x7ffff);
  1568. flags |= AR_TBTT_TIMER_EN;
  1569. break;
  1570. case NL80211_IFTYPE_ADHOC:
  1571. case NL80211_IFTYPE_MESH_POINT:
  1572. REG_SET_BIT(ah, AR_TXCFG,
  1573. AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
  1574. REG_WRITE(ah, AR_NEXT_NDP_TIMER,
  1575. TU_TO_USEC(next_beacon +
  1576. (ah->atim_window ? ah->
  1577. atim_window : 1)));
  1578. flags |= AR_NDP_TIMER_EN;
  1579. case NL80211_IFTYPE_AP:
  1580. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(next_beacon));
  1581. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT,
  1582. TU_TO_USEC(next_beacon -
  1583. ah->config.
  1584. dma_beacon_response_time));
  1585. REG_WRITE(ah, AR_NEXT_SWBA,
  1586. TU_TO_USEC(next_beacon -
  1587. ah->config.
  1588. sw_beacon_response_time));
  1589. flags |=
  1590. AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
  1591. break;
  1592. default:
  1593. ath_print(ath9k_hw_common(ah), ATH_DBG_BEACON,
  1594. "%s: unsupported opmode: %d\n",
  1595. __func__, ah->opmode);
  1596. return;
  1597. break;
  1598. }
  1599. REG_WRITE(ah, AR_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  1600. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, TU_TO_USEC(beacon_period));
  1601. REG_WRITE(ah, AR_SWBA_PERIOD, TU_TO_USEC(beacon_period));
  1602. REG_WRITE(ah, AR_NDP_PERIOD, TU_TO_USEC(beacon_period));
  1603. REGWRITE_BUFFER_FLUSH(ah);
  1604. DISABLE_REGWRITE_BUFFER(ah);
  1605. beacon_period &= ~ATH9K_BEACON_ENA;
  1606. if (beacon_period & ATH9K_BEACON_RESET_TSF) {
  1607. ath9k_hw_reset_tsf(ah);
  1608. }
  1609. REG_SET_BIT(ah, AR_TIMER_MODE, flags);
  1610. }
  1611. EXPORT_SYMBOL(ath9k_hw_beaconinit);
  1612. void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
  1613. const struct ath9k_beacon_state *bs)
  1614. {
  1615. u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
  1616. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1617. struct ath_common *common = ath9k_hw_common(ah);
  1618. ENABLE_REGWRITE_BUFFER(ah);
  1619. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
  1620. REG_WRITE(ah, AR_BEACON_PERIOD,
  1621. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  1622. REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
  1623. TU_TO_USEC(bs->bs_intval & ATH9K_BEACON_PERIOD));
  1624. REGWRITE_BUFFER_FLUSH(ah);
  1625. DISABLE_REGWRITE_BUFFER(ah);
  1626. REG_RMW_FIELD(ah, AR_RSSI_THR,
  1627. AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
  1628. beaconintval = bs->bs_intval & ATH9K_BEACON_PERIOD;
  1629. if (bs->bs_sleepduration > beaconintval)
  1630. beaconintval = bs->bs_sleepduration;
  1631. dtimperiod = bs->bs_dtimperiod;
  1632. if (bs->bs_sleepduration > dtimperiod)
  1633. dtimperiod = bs->bs_sleepduration;
  1634. if (beaconintval == dtimperiod)
  1635. nextTbtt = bs->bs_nextdtim;
  1636. else
  1637. nextTbtt = bs->bs_nexttbtt;
  1638. ath_print(common, ATH_DBG_BEACON, "next DTIM %d\n", bs->bs_nextdtim);
  1639. ath_print(common, ATH_DBG_BEACON, "next beacon %d\n", nextTbtt);
  1640. ath_print(common, ATH_DBG_BEACON, "beacon period %d\n", beaconintval);
  1641. ath_print(common, ATH_DBG_BEACON, "DTIM period %d\n", dtimperiod);
  1642. ENABLE_REGWRITE_BUFFER(ah);
  1643. REG_WRITE(ah, AR_NEXT_DTIM,
  1644. TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
  1645. REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
  1646. REG_WRITE(ah, AR_SLEEP1,
  1647. SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
  1648. | AR_SLEEP1_ASSUME_DTIM);
  1649. if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
  1650. beacontimeout = (BEACON_TIMEOUT_VAL << 3);
  1651. else
  1652. beacontimeout = MIN_BEACON_TIMEOUT_VAL;
  1653. REG_WRITE(ah, AR_SLEEP2,
  1654. SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
  1655. REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
  1656. REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
  1657. REGWRITE_BUFFER_FLUSH(ah);
  1658. DISABLE_REGWRITE_BUFFER(ah);
  1659. REG_SET_BIT(ah, AR_TIMER_MODE,
  1660. AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
  1661. AR_DTIM_TIMER_EN);
  1662. /* TSF Out of Range Threshold */
  1663. REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
  1664. }
  1665. EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
  1666. /*******************/
  1667. /* HW Capabilities */
  1668. /*******************/
  1669. int ath9k_hw_fill_cap_info(struct ath_hw *ah)
  1670. {
  1671. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1672. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  1673. struct ath_common *common = ath9k_hw_common(ah);
  1674. struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
  1675. u16 capField = 0, eeval;
  1676. eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
  1677. regulatory->current_rd = eeval;
  1678. eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_1);
  1679. if (AR_SREV_9285_10_OR_LATER(ah))
  1680. eeval |= AR9285_RDEXT_DEFAULT;
  1681. regulatory->current_rd_ext = eeval;
  1682. capField = ah->eep_ops->get_eeprom(ah, EEP_OP_CAP);
  1683. if (ah->opmode != NL80211_IFTYPE_AP &&
  1684. ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
  1685. if (regulatory->current_rd == 0x64 ||
  1686. regulatory->current_rd == 0x65)
  1687. regulatory->current_rd += 5;
  1688. else if (regulatory->current_rd == 0x41)
  1689. regulatory->current_rd = 0x43;
  1690. ath_print(common, ATH_DBG_REGULATORY,
  1691. "regdomain mapped to 0x%x\n", regulatory->current_rd);
  1692. }
  1693. eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
  1694. if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
  1695. ath_print(common, ATH_DBG_FATAL,
  1696. "no band has been marked as supported in EEPROM.\n");
  1697. return -EINVAL;
  1698. }
  1699. bitmap_zero(pCap->wireless_modes, ATH9K_MODE_MAX);
  1700. if (eeval & AR5416_OPFLAGS_11A) {
  1701. set_bit(ATH9K_MODE_11A, pCap->wireless_modes);
  1702. if (ah->config.ht_enable) {
  1703. if (!(eeval & AR5416_OPFLAGS_N_5G_HT20))
  1704. set_bit(ATH9K_MODE_11NA_HT20,
  1705. pCap->wireless_modes);
  1706. if (!(eeval & AR5416_OPFLAGS_N_5G_HT40)) {
  1707. set_bit(ATH9K_MODE_11NA_HT40PLUS,
  1708. pCap->wireless_modes);
  1709. set_bit(ATH9K_MODE_11NA_HT40MINUS,
  1710. pCap->wireless_modes);
  1711. }
  1712. }
  1713. }
  1714. if (eeval & AR5416_OPFLAGS_11G) {
  1715. set_bit(ATH9K_MODE_11G, pCap->wireless_modes);
  1716. if (ah->config.ht_enable) {
  1717. if (!(eeval & AR5416_OPFLAGS_N_2G_HT20))
  1718. set_bit(ATH9K_MODE_11NG_HT20,
  1719. pCap->wireless_modes);
  1720. if (!(eeval & AR5416_OPFLAGS_N_2G_HT40)) {
  1721. set_bit(ATH9K_MODE_11NG_HT40PLUS,
  1722. pCap->wireless_modes);
  1723. set_bit(ATH9K_MODE_11NG_HT40MINUS,
  1724. pCap->wireless_modes);
  1725. }
  1726. }
  1727. }
  1728. pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
  1729. /*
  1730. * For AR9271 we will temporarilly uses the rx chainmax as read from
  1731. * the EEPROM.
  1732. */
  1733. if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
  1734. !(eeval & AR5416_OPFLAGS_11A) &&
  1735. !(AR_SREV_9271(ah)))
  1736. /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
  1737. pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
  1738. else
  1739. /* Use rx_chainmask from EEPROM. */
  1740. pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
  1741. if (!(AR_SREV_9280(ah) && (ah->hw_version.macRev == 0)))
  1742. ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
  1743. pCap->low_2ghz_chan = 2312;
  1744. pCap->high_2ghz_chan = 2732;
  1745. pCap->low_5ghz_chan = 4920;
  1746. pCap->high_5ghz_chan = 6100;
  1747. pCap->hw_caps &= ~ATH9K_HW_CAP_CIPHER_CKIP;
  1748. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_TKIP;
  1749. pCap->hw_caps |= ATH9K_HW_CAP_CIPHER_AESCCM;
  1750. pCap->hw_caps &= ~ATH9K_HW_CAP_MIC_CKIP;
  1751. pCap->hw_caps |= ATH9K_HW_CAP_MIC_TKIP;
  1752. pCap->hw_caps |= ATH9K_HW_CAP_MIC_AESCCM;
  1753. if (ah->config.ht_enable)
  1754. pCap->hw_caps |= ATH9K_HW_CAP_HT;
  1755. else
  1756. pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
  1757. pCap->hw_caps |= ATH9K_HW_CAP_GTT;
  1758. pCap->hw_caps |= ATH9K_HW_CAP_VEOL;
  1759. pCap->hw_caps |= ATH9K_HW_CAP_BSSIDMASK;
  1760. pCap->hw_caps &= ~ATH9K_HW_CAP_MCAST_KEYSEARCH;
  1761. if (capField & AR_EEPROM_EEPCAP_MAXQCU)
  1762. pCap->total_queues =
  1763. MS(capField, AR_EEPROM_EEPCAP_MAXQCU);
  1764. else
  1765. pCap->total_queues = ATH9K_NUM_TX_QUEUES;
  1766. if (capField & AR_EEPROM_EEPCAP_KC_ENTRIES)
  1767. pCap->keycache_size =
  1768. 1 << MS(capField, AR_EEPROM_EEPCAP_KC_ENTRIES);
  1769. else
  1770. pCap->keycache_size = AR_KEYTABLE_SIZE;
  1771. pCap->hw_caps |= ATH9K_HW_CAP_FASTCC;
  1772. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  1773. pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD >> 1;
  1774. else
  1775. pCap->tx_triglevel_max = MAX_TX_FIFO_THRESHOLD;
  1776. if (AR_SREV_9271(ah))
  1777. pCap->num_gpio_pins = AR9271_NUM_GPIO;
  1778. else if (AR_SREV_9285_10_OR_LATER(ah))
  1779. pCap->num_gpio_pins = AR9285_NUM_GPIO;
  1780. else if (AR_SREV_9280_10_OR_LATER(ah))
  1781. pCap->num_gpio_pins = AR928X_NUM_GPIO;
  1782. else
  1783. pCap->num_gpio_pins = AR_NUM_GPIO;
  1784. if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
  1785. pCap->hw_caps |= ATH9K_HW_CAP_CST;
  1786. pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
  1787. } else {
  1788. pCap->rts_aggr_limit = (8 * 1024);
  1789. }
  1790. pCap->hw_caps |= ATH9K_HW_CAP_ENHANCEDPM;
  1791. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1792. ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
  1793. if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
  1794. ah->rfkill_gpio =
  1795. MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
  1796. ah->rfkill_polarity =
  1797. MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
  1798. pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
  1799. }
  1800. #endif
  1801. if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
  1802. pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
  1803. else
  1804. pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
  1805. if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
  1806. pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
  1807. else
  1808. pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
  1809. if (regulatory->current_rd_ext & (1 << REG_EXT_JAPAN_MIDBAND)) {
  1810. pCap->reg_cap =
  1811. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  1812. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
  1813. AR_EEPROM_EEREGCAP_EN_KK_U2 |
  1814. AR_EEPROM_EEREGCAP_EN_KK_MIDBAND;
  1815. } else {
  1816. pCap->reg_cap =
  1817. AR_EEPROM_EEREGCAP_EN_KK_NEW_11A |
  1818. AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN;
  1819. }
  1820. /* Advertise midband for AR5416 with FCC midband set in eeprom */
  1821. if (regulatory->current_rd_ext & (1 << REG_EXT_FCC_MIDBAND) &&
  1822. AR_SREV_5416(ah))
  1823. pCap->reg_cap |= AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND;
  1824. pCap->num_antcfg_5ghz =
  1825. ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_5GHZ);
  1826. pCap->num_antcfg_2ghz =
  1827. ah->eep_ops->get_num_ant_config(ah, ATH9K_HAL_FREQ_BAND_2GHZ);
  1828. if (AR_SREV_9280_10_OR_LATER(ah) &&
  1829. ath9k_hw_btcoex_supported(ah)) {
  1830. btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO;
  1831. btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO;
  1832. if (AR_SREV_9285(ah)) {
  1833. btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
  1834. btcoex_hw->btpriority_gpio = ATH_BTPRIORITY_GPIO;
  1835. } else {
  1836. btcoex_hw->scheme = ATH_BTCOEX_CFG_2WIRE;
  1837. }
  1838. } else {
  1839. btcoex_hw->scheme = ATH_BTCOEX_CFG_NONE;
  1840. }
  1841. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1842. pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_LDPC |
  1843. ATH9K_HW_CAP_FASTCLOCK;
  1844. pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
  1845. pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
  1846. pCap->rx_status_len = sizeof(struct ar9003_rxs);
  1847. pCap->tx_desc_len = sizeof(struct ar9003_txc);
  1848. pCap->txs_len = sizeof(struct ar9003_txs);
  1849. if (ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
  1850. pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
  1851. } else {
  1852. pCap->tx_desc_len = sizeof(struct ath_desc);
  1853. if (AR_SREV_9280_20(ah) &&
  1854. ((ah->eep_ops->get_eeprom(ah, EEP_MINOR_REV) <=
  1855. AR5416_EEP_MINOR_VER_16) ||
  1856. ah->eep_ops->get_eeprom(ah, EEP_FSTCLK_5G)))
  1857. pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
  1858. }
  1859. if (AR_SREV_9300_20_OR_LATER(ah))
  1860. pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
  1861. if (AR_SREV_9287_10_OR_LATER(ah) || AR_SREV_9271(ah))
  1862. pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
  1863. return 0;
  1864. }
  1865. /****************************/
  1866. /* GPIO / RFKILL / Antennae */
  1867. /****************************/
  1868. static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
  1869. u32 gpio, u32 type)
  1870. {
  1871. int addr;
  1872. u32 gpio_shift, tmp;
  1873. if (gpio > 11)
  1874. addr = AR_GPIO_OUTPUT_MUX3;
  1875. else if (gpio > 5)
  1876. addr = AR_GPIO_OUTPUT_MUX2;
  1877. else
  1878. addr = AR_GPIO_OUTPUT_MUX1;
  1879. gpio_shift = (gpio % 6) * 5;
  1880. if (AR_SREV_9280_20_OR_LATER(ah)
  1881. || (addr != AR_GPIO_OUTPUT_MUX1)) {
  1882. REG_RMW(ah, addr, (type << gpio_shift),
  1883. (0x1f << gpio_shift));
  1884. } else {
  1885. tmp = REG_READ(ah, addr);
  1886. tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
  1887. tmp &= ~(0x1f << gpio_shift);
  1888. tmp |= (type << gpio_shift);
  1889. REG_WRITE(ah, addr, tmp);
  1890. }
  1891. }
  1892. void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
  1893. {
  1894. u32 gpio_shift;
  1895. BUG_ON(gpio >= ah->caps.num_gpio_pins);
  1896. gpio_shift = gpio << 1;
  1897. REG_RMW(ah,
  1898. AR_GPIO_OE_OUT,
  1899. (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
  1900. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  1901. }
  1902. EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
  1903. u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
  1904. {
  1905. #define MS_REG_READ(x, y) \
  1906. (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
  1907. if (gpio >= ah->caps.num_gpio_pins)
  1908. return 0xffffffff;
  1909. if (AR_SREV_9300_20_OR_LATER(ah))
  1910. return MS_REG_READ(AR9300, gpio) != 0;
  1911. else if (AR_SREV_9271(ah))
  1912. return MS_REG_READ(AR9271, gpio) != 0;
  1913. else if (AR_SREV_9287_10_OR_LATER(ah))
  1914. return MS_REG_READ(AR9287, gpio) != 0;
  1915. else if (AR_SREV_9285_10_OR_LATER(ah))
  1916. return MS_REG_READ(AR9285, gpio) != 0;
  1917. else if (AR_SREV_9280_10_OR_LATER(ah))
  1918. return MS_REG_READ(AR928X, gpio) != 0;
  1919. else
  1920. return MS_REG_READ(AR, gpio) != 0;
  1921. }
  1922. EXPORT_SYMBOL(ath9k_hw_gpio_get);
  1923. void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
  1924. u32 ah_signal_type)
  1925. {
  1926. u32 gpio_shift;
  1927. ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
  1928. gpio_shift = 2 * gpio;
  1929. REG_RMW(ah,
  1930. AR_GPIO_OE_OUT,
  1931. (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
  1932. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  1933. }
  1934. EXPORT_SYMBOL(ath9k_hw_cfg_output);
  1935. void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
  1936. {
  1937. if (AR_SREV_9271(ah))
  1938. val = ~val;
  1939. REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
  1940. AR_GPIO_BIT(gpio));
  1941. }
  1942. EXPORT_SYMBOL(ath9k_hw_set_gpio);
  1943. u32 ath9k_hw_getdefantenna(struct ath_hw *ah)
  1944. {
  1945. return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
  1946. }
  1947. EXPORT_SYMBOL(ath9k_hw_getdefantenna);
  1948. void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
  1949. {
  1950. REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
  1951. }
  1952. EXPORT_SYMBOL(ath9k_hw_setantenna);
  1953. /*********************/
  1954. /* General Operation */
  1955. /*********************/
  1956. u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
  1957. {
  1958. u32 bits = REG_READ(ah, AR_RX_FILTER);
  1959. u32 phybits = REG_READ(ah, AR_PHY_ERR);
  1960. if (phybits & AR_PHY_ERR_RADAR)
  1961. bits |= ATH9K_RX_FILTER_PHYRADAR;
  1962. if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
  1963. bits |= ATH9K_RX_FILTER_PHYERR;
  1964. return bits;
  1965. }
  1966. EXPORT_SYMBOL(ath9k_hw_getrxfilter);
  1967. void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
  1968. {
  1969. u32 phybits;
  1970. ENABLE_REGWRITE_BUFFER(ah);
  1971. REG_WRITE(ah, AR_RX_FILTER, bits);
  1972. phybits = 0;
  1973. if (bits & ATH9K_RX_FILTER_PHYRADAR)
  1974. phybits |= AR_PHY_ERR_RADAR;
  1975. if (bits & ATH9K_RX_FILTER_PHYERR)
  1976. phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
  1977. REG_WRITE(ah, AR_PHY_ERR, phybits);
  1978. if (phybits)
  1979. REG_WRITE(ah, AR_RXCFG,
  1980. REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
  1981. else
  1982. REG_WRITE(ah, AR_RXCFG,
  1983. REG_READ(ah, AR_RXCFG) & ~AR_RXCFG_ZLFDMA);
  1984. REGWRITE_BUFFER_FLUSH(ah);
  1985. DISABLE_REGWRITE_BUFFER(ah);
  1986. }
  1987. EXPORT_SYMBOL(ath9k_hw_setrxfilter);
  1988. bool ath9k_hw_phy_disable(struct ath_hw *ah)
  1989. {
  1990. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  1991. return false;
  1992. ath9k_hw_init_pll(ah, NULL);
  1993. return true;
  1994. }
  1995. EXPORT_SYMBOL(ath9k_hw_phy_disable);
  1996. bool ath9k_hw_disable(struct ath_hw *ah)
  1997. {
  1998. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1999. return false;
  2000. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
  2001. return false;
  2002. ath9k_hw_init_pll(ah, NULL);
  2003. return true;
  2004. }
  2005. EXPORT_SYMBOL(ath9k_hw_disable);
  2006. void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit)
  2007. {
  2008. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  2009. struct ath9k_channel *chan = ah->curchan;
  2010. struct ieee80211_channel *channel = chan->chan;
  2011. regulatory->power_limit = min(limit, (u32) MAX_RATE_POWER);
  2012. ah->eep_ops->set_txpower(ah, chan,
  2013. ath9k_regd_get_ctl(regulatory, chan),
  2014. channel->max_antenna_gain * 2,
  2015. channel->max_power * 2,
  2016. min((u32) MAX_RATE_POWER,
  2017. (u32) regulatory->power_limit));
  2018. }
  2019. EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
  2020. void ath9k_hw_setopmode(struct ath_hw *ah)
  2021. {
  2022. ath9k_hw_set_operating_mode(ah, ah->opmode);
  2023. }
  2024. EXPORT_SYMBOL(ath9k_hw_setopmode);
  2025. void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
  2026. {
  2027. REG_WRITE(ah, AR_MCAST_FIL0, filter0);
  2028. REG_WRITE(ah, AR_MCAST_FIL1, filter1);
  2029. }
  2030. EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
  2031. void ath9k_hw_write_associd(struct ath_hw *ah)
  2032. {
  2033. struct ath_common *common = ath9k_hw_common(ah);
  2034. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
  2035. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
  2036. ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
  2037. }
  2038. EXPORT_SYMBOL(ath9k_hw_write_associd);
  2039. #define ATH9K_MAX_TSF_READ 10
  2040. u64 ath9k_hw_gettsf64(struct ath_hw *ah)
  2041. {
  2042. u32 tsf_lower, tsf_upper1, tsf_upper2;
  2043. int i;
  2044. tsf_upper1 = REG_READ(ah, AR_TSF_U32);
  2045. for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
  2046. tsf_lower = REG_READ(ah, AR_TSF_L32);
  2047. tsf_upper2 = REG_READ(ah, AR_TSF_U32);
  2048. if (tsf_upper2 == tsf_upper1)
  2049. break;
  2050. tsf_upper1 = tsf_upper2;
  2051. }
  2052. WARN_ON( i == ATH9K_MAX_TSF_READ );
  2053. return (((u64)tsf_upper1 << 32) | tsf_lower);
  2054. }
  2055. EXPORT_SYMBOL(ath9k_hw_gettsf64);
  2056. void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
  2057. {
  2058. REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
  2059. REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
  2060. }
  2061. EXPORT_SYMBOL(ath9k_hw_settsf64);
  2062. void ath9k_hw_reset_tsf(struct ath_hw *ah)
  2063. {
  2064. if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
  2065. AH_TSF_WRITE_TIMEOUT))
  2066. ath_print(ath9k_hw_common(ah), ATH_DBG_RESET,
  2067. "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
  2068. REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
  2069. }
  2070. EXPORT_SYMBOL(ath9k_hw_reset_tsf);
  2071. void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
  2072. {
  2073. if (setting)
  2074. ah->misc_mode |= AR_PCU_TX_ADD_TSF;
  2075. else
  2076. ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
  2077. }
  2078. EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
  2079. void ath9k_hw_set11nmac2040(struct ath_hw *ah)
  2080. {
  2081. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  2082. u32 macmode;
  2083. if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
  2084. macmode = AR_2040_JOINED_RX_CLEAR;
  2085. else
  2086. macmode = 0;
  2087. REG_WRITE(ah, AR_2040_MODE, macmode);
  2088. }
  2089. /* HW Generic timers configuration */
  2090. static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
  2091. {
  2092. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2093. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2094. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2095. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2096. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2097. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2098. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2099. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2100. {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
  2101. {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
  2102. AR_NDP2_TIMER_MODE, 0x0002},
  2103. {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
  2104. AR_NDP2_TIMER_MODE, 0x0004},
  2105. {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
  2106. AR_NDP2_TIMER_MODE, 0x0008},
  2107. {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
  2108. AR_NDP2_TIMER_MODE, 0x0010},
  2109. {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
  2110. AR_NDP2_TIMER_MODE, 0x0020},
  2111. {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
  2112. AR_NDP2_TIMER_MODE, 0x0040},
  2113. {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
  2114. AR_NDP2_TIMER_MODE, 0x0080}
  2115. };
  2116. /* HW generic timer primitives */
  2117. /* compute and clear index of rightmost 1 */
  2118. static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
  2119. {
  2120. u32 b;
  2121. b = *mask;
  2122. b &= (0-b);
  2123. *mask &= ~b;
  2124. b *= debruijn32;
  2125. b >>= 27;
  2126. return timer_table->gen_timer_index[b];
  2127. }
  2128. u32 ath9k_hw_gettsf32(struct ath_hw *ah)
  2129. {
  2130. return REG_READ(ah, AR_TSF_L32);
  2131. }
  2132. EXPORT_SYMBOL(ath9k_hw_gettsf32);
  2133. struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
  2134. void (*trigger)(void *),
  2135. void (*overflow)(void *),
  2136. void *arg,
  2137. u8 timer_index)
  2138. {
  2139. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2140. struct ath_gen_timer *timer;
  2141. timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
  2142. if (timer == NULL) {
  2143. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  2144. "Failed to allocate memory"
  2145. "for hw timer[%d]\n", timer_index);
  2146. return NULL;
  2147. }
  2148. /* allocate a hardware generic timer slot */
  2149. timer_table->timers[timer_index] = timer;
  2150. timer->index = timer_index;
  2151. timer->trigger = trigger;
  2152. timer->overflow = overflow;
  2153. timer->arg = arg;
  2154. return timer;
  2155. }
  2156. EXPORT_SYMBOL(ath_gen_timer_alloc);
  2157. void ath9k_hw_gen_timer_start(struct ath_hw *ah,
  2158. struct ath_gen_timer *timer,
  2159. u32 timer_next,
  2160. u32 timer_period)
  2161. {
  2162. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2163. u32 tsf;
  2164. BUG_ON(!timer_period);
  2165. set_bit(timer->index, &timer_table->timer_mask.timer_bits);
  2166. tsf = ath9k_hw_gettsf32(ah);
  2167. ath_print(ath9k_hw_common(ah), ATH_DBG_HWTIMER,
  2168. "curent tsf %x period %x"
  2169. "timer_next %x\n", tsf, timer_period, timer_next);
  2170. /*
  2171. * Pull timer_next forward if the current TSF already passed it
  2172. * because of software latency
  2173. */
  2174. if (timer_next < tsf)
  2175. timer_next = tsf + timer_period;
  2176. /*
  2177. * Program generic timer registers
  2178. */
  2179. REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
  2180. timer_next);
  2181. REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
  2182. timer_period);
  2183. REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2184. gen_tmr_configuration[timer->index].mode_mask);
  2185. /* Enable both trigger and thresh interrupt masks */
  2186. REG_SET_BIT(ah, AR_IMR_S5,
  2187. (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
  2188. SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
  2189. }
  2190. EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
  2191. void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  2192. {
  2193. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2194. if ((timer->index < AR_FIRST_NDP_TIMER) ||
  2195. (timer->index >= ATH_MAX_GEN_TIMER)) {
  2196. return;
  2197. }
  2198. /* Clear generic timer enable bits. */
  2199. REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2200. gen_tmr_configuration[timer->index].mode_mask);
  2201. /* Disable both trigger and thresh interrupt masks */
  2202. REG_CLR_BIT(ah, AR_IMR_S5,
  2203. (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
  2204. SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
  2205. clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
  2206. }
  2207. EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
  2208. void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
  2209. {
  2210. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2211. /* free the hardware generic timer slot */
  2212. timer_table->timers[timer->index] = NULL;
  2213. kfree(timer);
  2214. }
  2215. EXPORT_SYMBOL(ath_gen_timer_free);
  2216. /*
  2217. * Generic Timer Interrupts handling
  2218. */
  2219. void ath_gen_timer_isr(struct ath_hw *ah)
  2220. {
  2221. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2222. struct ath_gen_timer *timer;
  2223. struct ath_common *common = ath9k_hw_common(ah);
  2224. u32 trigger_mask, thresh_mask, index;
  2225. /* get hardware generic timer interrupt status */
  2226. trigger_mask = ah->intr_gen_timer_trigger;
  2227. thresh_mask = ah->intr_gen_timer_thresh;
  2228. trigger_mask &= timer_table->timer_mask.val;
  2229. thresh_mask &= timer_table->timer_mask.val;
  2230. trigger_mask &= ~thresh_mask;
  2231. while (thresh_mask) {
  2232. index = rightmost_index(timer_table, &thresh_mask);
  2233. timer = timer_table->timers[index];
  2234. BUG_ON(!timer);
  2235. ath_print(common, ATH_DBG_HWTIMER,
  2236. "TSF overflow for Gen timer %d\n", index);
  2237. timer->overflow(timer->arg);
  2238. }
  2239. while (trigger_mask) {
  2240. index = rightmost_index(timer_table, &trigger_mask);
  2241. timer = timer_table->timers[index];
  2242. BUG_ON(!timer);
  2243. ath_print(common, ATH_DBG_HWTIMER,
  2244. "Gen timer[%d] trigger\n", index);
  2245. timer->trigger(timer->arg);
  2246. }
  2247. }
  2248. EXPORT_SYMBOL(ath_gen_timer_isr);
  2249. /********/
  2250. /* HTC */
  2251. /********/
  2252. void ath9k_hw_htc_resetinit(struct ath_hw *ah)
  2253. {
  2254. ah->htc_reset_init = true;
  2255. }
  2256. EXPORT_SYMBOL(ath9k_hw_htc_resetinit);
  2257. static struct {
  2258. u32 version;
  2259. const char * name;
  2260. } ath_mac_bb_names[] = {
  2261. /* Devices with external radios */
  2262. { AR_SREV_VERSION_5416_PCI, "5416" },
  2263. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2264. { AR_SREV_VERSION_9100, "9100" },
  2265. { AR_SREV_VERSION_9160, "9160" },
  2266. /* Single-chip solutions */
  2267. { AR_SREV_VERSION_9280, "9280" },
  2268. { AR_SREV_VERSION_9285, "9285" },
  2269. { AR_SREV_VERSION_9287, "9287" },
  2270. { AR_SREV_VERSION_9271, "9271" },
  2271. { AR_SREV_VERSION_9300, "9300" },
  2272. };
  2273. /* For devices with external radios */
  2274. static struct {
  2275. u16 version;
  2276. const char * name;
  2277. } ath_rf_names[] = {
  2278. { 0, "5133" },
  2279. { AR_RAD5133_SREV_MAJOR, "5133" },
  2280. { AR_RAD5122_SREV_MAJOR, "5122" },
  2281. { AR_RAD2133_SREV_MAJOR, "2133" },
  2282. { AR_RAD2122_SREV_MAJOR, "2122" }
  2283. };
  2284. /*
  2285. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2286. */
  2287. static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
  2288. {
  2289. int i;
  2290. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2291. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2292. return ath_mac_bb_names[i].name;
  2293. }
  2294. }
  2295. return "????";
  2296. }
  2297. /*
  2298. * Return the RF name. "????" is returned if the RF is unknown.
  2299. * Used for devices with external radios.
  2300. */
  2301. static const char *ath9k_hw_rf_name(u16 rf_version)
  2302. {
  2303. int i;
  2304. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2305. if (ath_rf_names[i].version == rf_version) {
  2306. return ath_rf_names[i].name;
  2307. }
  2308. }
  2309. return "????";
  2310. }
  2311. void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
  2312. {
  2313. int used;
  2314. /* chipsets >= AR9280 are single-chip */
  2315. if (AR_SREV_9280_10_OR_LATER(ah)) {
  2316. used = snprintf(hw_name, len,
  2317. "Atheros AR%s Rev:%x",
  2318. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2319. ah->hw_version.macRev);
  2320. }
  2321. else {
  2322. used = snprintf(hw_name, len,
  2323. "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
  2324. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2325. ah->hw_version.macRev,
  2326. ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
  2327. AR_RADIO_SREV_MAJOR)),
  2328. ah->hw_version.phyRev);
  2329. }
  2330. hw_name[used] = '\0';
  2331. }
  2332. EXPORT_SYMBOL(ath9k_hw_name);