hw.c 74 KB

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