hw.c 76 KB

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